Browse Source

Auto repair Png options to use Bit8. Fixes #935

js/color-alpha-handling
James Jackson-South 5 years ago
parent
commit
abe1263b17
  1. 22
      src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs
  2. 11
      tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs
  3. 3
      tests/ImageSharp.Tests/TestImages.cs
  4. 3
      tests/Images/Input/Png/issues/Issue_935.png

22
src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs

@ -35,6 +35,15 @@ namespace SixLabors.ImageSharp.Formats.Png
options.ColorType ??= pngMetadata.ColorType ?? SuggestColorType<TPixel>();
options.BitDepth ??= pngMetadata.BitDepth ?? SuggestBitDepth<TPixel>();
// Ensure bit depth and color type are a supported combination.
// Bit8 is the only bit depth supported by all color types.
byte bits = (byte)options.BitDepth;
byte[] validBitDepths = PngConstants.ColorTypes[options.ColorType.Value];
if (Array.IndexOf(validBitDepths, bits) == -1)
{
options.BitDepth = PngBitDepth.Bit8;
}
options.InterlaceMethod ??= pngMetadata.InterlaceMethod;
use16Bit = options.BitDepth == PngBitDepth.Bit16;
@ -44,12 +53,6 @@ namespace SixLabors.ImageSharp.Formats.Png
{
options.ChunkFilter = PngChunkFilter.ExcludeAll;
}
// Ensure we are not allowing impossible combinations.
if (!PngConstants.ColorTypes.ContainsKey(options.ColorType.Value))
{
throw new NotSupportedException("Color type is not supported or not valid.");
}
}
/// <summary>
@ -68,15 +71,10 @@ namespace SixLabors.ImageSharp.Formats.Png
return null;
}
byte bits = (byte)options.BitDepth;
if (Array.IndexOf(PngConstants.ColorTypes[options.ColorType.Value], bits) == -1)
{
throw new NotSupportedException("Bit depth is not supported or not valid.");
}
// Use the metadata to determine what quantization depth to use if no quantizer has been set.
if (options.Quantizer is null)
{
byte bits = (byte)options.BitDepth;
var maxColors = ImageMaths.GetColorCountForBitDepth(bits);
options.Quantizer = new WuQuantizer(new QuantizerOptions { MaxColors = maxColors });
}

11
tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs

@ -556,6 +556,17 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
provider);
}
[Fact]
public void EncodeFixesInvalidOptions()
{
// https://github.com/SixLabors/ImageSharp/issues/935
using var ms = new MemoryStream();
var testFile = TestFile.Create(TestImages.Png.Issue935);
using Image<Rgba32> image = testFile.CreateRgba32Image(new PngDecoder());
image.Save(ms, new PngEncoder { ColorType = PngColorType.RgbWithAlpha });
}
private static void TestPngEncoderCore<TPixel>(
TestImageProvider<TPixel> provider,
PngColorType pngColorType,

3
tests/ImageSharp.Tests/TestImages.cs

@ -107,6 +107,9 @@ namespace SixLabors.ImageSharp.Tests
public const string Issue1177_1 = "Png/issues/Issue_1177_1.png";
public const string Issue1177_2 = "Png/issues/Issue_1177_2.png";
// Issue 935: https://github.com/SixLabors/ImageSharp/issues/935
public const string Issue935 = "Png/issues/Issue_935.png";
public static class Bad
{
public const string MissingDataChunk = "Png/xdtn0g01.png";

3
tests/Images/Input/Png/issues/Issue_935.png

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:6a9c5cdacc9bedf481c883828de5bfb7902e2bec038fff08830171cf7075e4f9
size 870
Loading…
Cancel
Save