diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 784d9aa11f..29b28c9180 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -4,7 +4,6 @@ using System; using System.Buffers; using System.Buffers.Binary; -using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Runtime.CompilerServices; @@ -380,8 +379,8 @@ public IImageInfo Identify(BufferedReadStream stream, CancellationToken cancella /// /// Reads the least significant bits from the byte pair with the others set to 0. /// - /// The source buffer - /// THe offset + /// The source buffer. + /// THe offset. /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] private static byte ReadByteLittleEndian(ReadOnlySpan buffer, int offset) @@ -392,7 +391,7 @@ private static byte ReadByteLittleEndian(ReadOnlySpan buffer, int offset) /// specified number of bits. /// /// The bytes to convert from. Cannot be empty. - /// The number of bytes per scanline + /// The number of bytes per scanline. /// The number of bits per value. /// The new array. /// The resulting array. @@ -973,6 +972,10 @@ private void AssignTransparentMarkers(ReadOnlySpan alpha, PngMetadata pngM pngMetadata.HasTransparency = true; } } + else if (this.pngColorType == PngColorType.Palette && alpha.Length > 0) + { + pngMetadata.HasTransparency = true; + } } /// diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index 29e0af9c68..22454d2573 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -470,6 +470,30 @@ public void Issue1765(TestImageProvider provider) Assert.Null(ex); } + // https://github.com/SixLabors/ImageSharp/issues/2209 + [Theory] + [WithFile(TestImages.Png.Issue2209IndexedWithTransparency, PixelTypes.Rgba32)] + public void Issue2209_Decode_HasTransparencyIsTrue(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + using Image image = provider.GetImage(PngDecoder); + image.DebugSave(provider); + PngMetadata metadata = image.Metadata.GetPngMetadata(); + Assert.True(metadata.HasTransparency); + } + + // https://github.com/SixLabors/ImageSharp/issues/2209 + [Theory] + [InlineData(TestImages.Png.Issue2209IndexedWithTransparency)] + public void Issue2209_Identify_HasTransparencyIsTrue(string imagePath) + { + var testFile = TestFile.Create(imagePath); + using var stream = new MemoryStream(testFile.Bytes, false); + IImageInfo imageInfo = Image.Identify(stream); + PngMetadata metadata = imageInfo.Metadata.GetPngMetadata(); + Assert.True(metadata.HasTransparency); + } + // https://github.com/SixLabors/ImageSharp/issues/410 [Theory] [WithFile(TestImages.Png.Bad.Issue410_MalformedApplePng, PixelTypes.Rgba32)] diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 306a28dae9..ddaf672b43 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -125,6 +125,9 @@ public static class Png // Discussion 1875: https://github.com/SixLabors/ImageSharp/discussions/1875 public const string Issue1875 = "Png/raw-profile-type-exif.png"; + // Issue 2209: https://github.com/SixLabors/ImageSharp/issues/2209 + public const string Issue2209IndexedWithTransparency = "Png/issues/Issue_2209.png"; + public static class Bad { public const string MissingDataChunk = "Png/xdtn0g01.png"; diff --git a/tests/Images/Input/Png/issues/Issue_2209.png b/tests/Images/Input/Png/issues/Issue_2209.png new file mode 100644 index 0000000000..6cc26bce57 --- /dev/null +++ b/tests/Images/Input/Png/issues/Issue_2209.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:58eae3863a2107bf3359a87492bc95524b54dd091683e333ac73d76f229a1f71 +size 621275