Browse Source

re-enstate threashold

re-enstate the pngencoderoptions threashold but how set the default to 255 so all alpha values are retained.
af/merge-core
Scott Williams 9 years ago
parent
commit
b4fba454d5
  1. 5
      src/ImageSharp/Formats/Png/IPngEncoderOptions.cs
  2. 7
      src/ImageSharp/Formats/Png/PngEncoderCore.cs
  3. 5
      src/ImageSharp/Formats/Png/PngEncoderOptions.cs
  4. 13
      tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs

5
src/ImageSharp/Formats/Png/IPngEncoderOptions.cs

@ -40,6 +40,11 @@ namespace ImageSharp.Formats
/// </summary>
IQuantizer Quantizer { get; }
/// <summary>
/// Gets the transparency threshold.
/// </summary>
byte Threshold { get; }
/// <summary>
/// Gets a value indicating whether this instance should write
/// gamma information to the stream.

7
src/ImageSharp/Formats/Png/PngEncoderCore.cs

@ -495,7 +495,7 @@ namespace ImageSharp.Formats
// Grab the palette and write it to the stream.
TColor[] palette = quantized.Palette;
byte pixelCount = (byte)palette.Length;
byte pixelCount = palette.Length.ToByte();
// Get max colors for bit depth.
int colorTableLength = (int)Math.Pow(2, header.BitDepth) * 3;
@ -518,6 +518,11 @@ namespace ImageSharp.Formats
colorTable[offset + 1] = bytes[1];
colorTable[offset + 2] = bytes[2];
if (alpha > this.options.Threshold)
{
alpha = 255;
}
anyAlpha = anyAlpha || alpha < 255;
alphaTable[i] = alpha;
}

5
src/ImageSharp/Formats/Png/PngEncoderOptions.cs

@ -57,6 +57,11 @@ namespace ImageSharp.Formats
/// </summary>
public IQuantizer Quantizer { get; set; }
/// <summary>
/// Gets or sets the transparency threshold.
/// </summary>
public byte Threshold { get; set; } = 255;
/// <summary>
/// Gets or sets a value indicating whether this instance should write
/// gamma information to the stream. The default value is false.

13
tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs

@ -13,6 +13,7 @@ namespace ImageSharp.Tests.Formats.Png
using ImageSharp.Formats;
using System.Linq;
using ImageSharp.IO;
using System.Numerics;
public class PngSmokeTests
{
@ -67,15 +68,19 @@ namespace ImageSharp.Tests.Formats.Png
using (Image<TColor> source = provider.GetImage())
using (MemoryStream ms = new MemoryStream())
{
// image.Save(provider.Utility.GetTestOutputFileName("bmp"));
source.MetaData.Quality = 256;
source.Save(ms, new PngEncoder());
source.Save(ms, new PngEncoder(), new PngEncoderOptions {
Threshold = 200
});
ms.Position = 0;
using (Image img1 = Image.Load(ms, new PngDecoder()))
{
using (MemoryStream ms2 = new MemoryStream())
{
img1.Save(ms2, new PngEncoder());
img1.Save(ms2, new PngEncoder(), new PngEncoderOptions
{
Threshold = 200
});
ms2.Position = 0;
using (Image img2 = Image.Load(ms2, new PngDecoder()))
{
@ -90,8 +95,6 @@ namespace ImageSharp.Tests.Formats.Png
}
}
}
// img2.Save(provider.Utility.GetTestOutputFileName("bmp", "_loaded"), new BmpEncoder());
ImageComparer.CheckSimilarity(source, img2);
}
}
}

Loading…
Cancel
Save