Browse Source

Fix Decoding 8-Bit grayscale png's with alpha (#830)

af/merge-core
Brian Popow 7 years ago
committed by James Jackson-South
parent
commit
fb9e23020b
  1. 3
      src/ImageSharp/Formats/Png/PngScanlineProcessor.cs
  2. 30
      tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs
  3. 1
      tests/ImageSharp.Tests/TestImages.cs
  4. 3
      tests/Images/Input/Png/rollsroyce.png

3
src/ImageSharp/Formats/Png/PngScanlineProcessor.cs

@ -240,9 +240,9 @@ namespace SixLabors.ImageSharp.Formats.Png
else
{
Rgba32 rgba32 = default;
int offset = 0;
for (int x = pixelOffset; x < header.Width; x += increment)
{
int offset = x * bytesPerPixel;
byte luminance = Unsafe.Add(ref scanlineSpanRef, offset);
byte alpha = Unsafe.Add(ref scanlineSpanRef, offset + bytesPerSample);
rgba32.R = luminance;
@ -252,6 +252,7 @@ namespace SixLabors.ImageSharp.Formats.Png
pixel.FromRgba32(rgba32);
Unsafe.Add(ref rowSpanRef, x) = pixel;
offset += bytesPerPixel;
}
}
}

30
tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs

@ -67,6 +67,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
TestImages.Png.GrayTrns16BitInterlaced
};
public static readonly string[] TestImagesGrayAlpha8Bit =
{
TestImages.Png.GrayAlpha8Bit,
TestImages.Png.GrayAlpha8Bit2
};
public static readonly TheoryData<string, int, int, PixelResolutionUnit> RatioFiles =
new TheoryData<string, int, int, PixelResolutionUnit>
{
@ -123,6 +129,18 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
}
}
[Theory]
[WithFileCollection(nameof(TestImagesGrayAlpha8Bit), PixelTypes.Rgba32)]
public void Decoder_Gray8bitWithAlpha<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage(new PngDecoder()))
{
image.DebugSave(provider);
image.CompareToOriginal(provider, ImageComparer.Exact);
}
}
[Theory]
[WithFileCollection(nameof(TestImagesGray16Bit), PixelTypes.Rgb48)]
public void Decode_Gray16Bit<TPixel>(TestImageProvider<TPixel> provider)
@ -159,6 +177,18 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
}
}
[Theory]
[WithFile(TestImages.Png.GrayAlpha8Bit2, PixelTypes)]
public void Decoder_CanDecodeGrey8bitWithAlpha<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage(new PngDecoder()))
{
image.DebugSave(provider);
image.CompareToOriginal(provider, ImageComparer.Exact);
}
}
[Fact]
public void Decode_IgnoreMetadataIsFalse_TextChunckIsRead()
{

1
tests/ImageSharp.Tests/TestImages.cs

@ -29,6 +29,7 @@ namespace SixLabors.ImageSharp.Tests
public const string Gray4Bpp = "Png/gray_4bpp.png";
public const string Gray16Bit = "Png/gray-16.png";
public const string GrayAlpha8Bit = "Png/gray-alpha-8.png";
public const string GrayAlpha8Bit2 = "Png/rollsroyce.png";
public const string GrayAlpha16Bit = "Png/gray-alpha-16.png";
public const string GrayTrns16BitInterlaced = "Png/gray-16-tRNS-interlaced.png";
public const string Rgb24BppTrans = "Png/rgb-8-tRNS.png";

3
tests/Images/Input/Png/rollsroyce.png

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