Skip to content

Commit 6a60638

Browse files
brianpopowJimBobSquarePants
authored andcommitted
Fix Decoding 8-Bit grayscale png's with alpha (#830)
1 parent 9c48c19 commit 6a60638

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

src/ImageSharp/Formats/Png/PngScanlineProcessor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ public static void ProcessInterlacedGrayscaleWithAlphaScanline<TPixel>(
240240
else
241241
{
242242
Rgba32 rgba32 = default;
243+
int offset = 0;
243244
for (int x = pixelOffset; x < header.Width; x += increment)
244245
{
245-
int offset = x * bytesPerPixel;
246246
byte luminance = Unsafe.Add(ref scanlineSpanRef, offset);
247247
byte alpha = Unsafe.Add(ref scanlineSpanRef, offset + bytesPerSample);
248248
rgba32.R = luminance;
@@ -252,6 +252,7 @@ public static void ProcessInterlacedGrayscaleWithAlphaScanline<TPixel>(
252252

253253
pixel.FromRgba32(rgba32);
254254
Unsafe.Add(ref rowSpanRef, x) = pixel;
255+
offset += bytesPerPixel;
255256
}
256257
}
257258
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ public partial class PngDecoderTests
6767
TestImages.Png.GrayTrns16BitInterlaced
6868
};
6969

70+
public static readonly string[] TestImagesGrayAlpha8Bit =
71+
{
72+
TestImages.Png.GrayAlpha8Bit,
73+
TestImages.Png.GrayAlpha8Bit2
74+
};
75+
7076
public static readonly TheoryData<string, int, int, PixelResolutionUnit> RatioFiles =
7177
new TheoryData<string, int, int, PixelResolutionUnit>
7278
{
@@ -123,6 +129,18 @@ public void Decode_64Bpp<TPixel>(TestImageProvider<TPixel> provider)
123129
}
124130
}
125131

132+
[Theory]
133+
[WithFileCollection(nameof(TestImagesGrayAlpha8Bit), PixelTypes.Rgba32)]
134+
public void Decoder_Gray8bitWithAlpha<TPixel>(TestImageProvider<TPixel> provider)
135+
where TPixel : struct, IPixel<TPixel>
136+
{
137+
using (Image<TPixel> image = provider.GetImage(new PngDecoder()))
138+
{
139+
image.DebugSave(provider);
140+
image.CompareToOriginal(provider, ImageComparer.Exact);
141+
}
142+
}
143+
126144
[Theory]
127145
[WithFileCollection(nameof(TestImagesGray16Bit), PixelTypes.Rgb48)]
128146
public void Decode_Gray16Bit<TPixel>(TestImageProvider<TPixel> provider)
@@ -159,6 +177,18 @@ public void Decoder_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel
159177
}
160178
}
161179

180+
[Theory]
181+
[WithFile(TestImages.Png.GrayAlpha8Bit2, PixelTypes)]
182+
public void Decoder_CanDecodeGrey8bitWithAlpha<TPixel>(TestImageProvider<TPixel> provider)
183+
where TPixel : struct, IPixel<TPixel>
184+
{
185+
using (Image<TPixel> image = provider.GetImage(new PngDecoder()))
186+
{
187+
image.DebugSave(provider);
188+
image.CompareToOriginal(provider, ImageComparer.Exact);
189+
}
190+
}
191+
162192
[Fact]
163193
public void Decode_IgnoreMetadataIsFalse_TextChunckIsRead()
164194
{

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static class Png
2929
public const string Gray4Bpp = "Png/gray_4bpp.png";
3030
public const string Gray16Bit = "Png/gray-16.png";
3131
public const string GrayAlpha8Bit = "Png/gray-alpha-8.png";
32+
public const string GrayAlpha8Bit2 = "Png/rollsroyce.png";
3233
public const string GrayAlpha16Bit = "Png/gray-alpha-16.png";
3334
public const string GrayTrns16BitInterlaced = "Png/gray-16-tRNS-interlaced.png";
3435
public const string Rgb24BppTrans = "Png/rgb-8-tRNS.png";
24.8 KB
Loading

0 commit comments

Comments
 (0)