Skip to content

Commit 1b6728b

Browse files
Merge pull request #2215 from SixLabors/bp/pngindexedtrans
Identify transparency for indexed PNG correctly
2 parents f456ba0 + e69658e commit 1b6728b

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

src/ImageSharp/Formats/Png/PngDecoderCore.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Buffers;
66
using System.Buffers.Binary;
7-
using System.Collections.Generic;
87
using System.IO;
98
using System.IO.Compression;
109
using System.Runtime.CompilerServices;
@@ -380,8 +379,8 @@ public IImageInfo Identify(BufferedReadStream stream, CancellationToken cancella
380379
/// <summary>
381380
/// Reads the least significant bits from the byte pair with the others set to 0.
382381
/// </summary>
383-
/// <param name="buffer">The source buffer</param>
384-
/// <param name="offset">THe offset</param>
382+
/// <param name="buffer">The source buffer.</param>
383+
/// <param name="offset">THe offset.</param>
385384
/// <returns>The <see cref="int"/></returns>
386385
[MethodImpl(MethodImplOptions.AggressiveInlining)]
387386
private static byte ReadByteLittleEndian(ReadOnlySpan<byte> buffer, int offset)
@@ -392,7 +391,7 @@ private static byte ReadByteLittleEndian(ReadOnlySpan<byte> buffer, int offset)
392391
/// specified number of bits.
393392
/// </summary>
394393
/// <param name="source">The bytes to convert from. Cannot be empty.</param>
395-
/// <param name="bytesPerScanline">The number of bytes per scanline</param>
394+
/// <param name="bytesPerScanline">The number of bytes per scanline.</param>
396395
/// <param name="bits">The number of bits per value.</param>
397396
/// <param name="buffer">The new array.</param>
398397
/// <returns>The resulting <see cref="ReadOnlySpan{Byte}"/> array.</returns>
@@ -973,6 +972,10 @@ private void AssignTransparentMarkers(ReadOnlySpan<byte> alpha, PngMetadata pngM
973972
pngMetadata.HasTransparency = true;
974973
}
975974
}
975+
else if (this.pngColorType == PngColorType.Palette && alpha.Length > 0)
976+
{
977+
pngMetadata.HasTransparency = true;
978+
}
976979
}
977980

978981
/// <summary>

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,30 @@ public void Issue1765<TPixel>(TestImageProvider<TPixel> provider)
470470
Assert.Null(ex);
471471
}
472472

473+
// https://github.com/SixLabors/ImageSharp/issues/2209
474+
[Theory]
475+
[WithFile(TestImages.Png.Issue2209IndexedWithTransparency, PixelTypes.Rgba32)]
476+
public void Issue2209_Decode_HasTransparencyIsTrue<TPixel>(TestImageProvider<TPixel> provider)
477+
where TPixel : unmanaged, IPixel<TPixel>
478+
{
479+
using Image<TPixel> image = provider.GetImage(PngDecoder);
480+
image.DebugSave(provider);
481+
PngMetadata metadata = image.Metadata.GetPngMetadata();
482+
Assert.True(metadata.HasTransparency);
483+
}
484+
485+
// https://github.com/SixLabors/ImageSharp/issues/2209
486+
[Theory]
487+
[InlineData(TestImages.Png.Issue2209IndexedWithTransparency)]
488+
public void Issue2209_Identify_HasTransparencyIsTrue(string imagePath)
489+
{
490+
var testFile = TestFile.Create(imagePath);
491+
using var stream = new MemoryStream(testFile.Bytes, false);
492+
IImageInfo imageInfo = Image.Identify(stream);
493+
PngMetadata metadata = imageInfo.Metadata.GetPngMetadata();
494+
Assert.True(metadata.HasTransparency);
495+
}
496+
473497
// https://github.com/SixLabors/ImageSharp/issues/410
474498
[Theory]
475499
[WithFile(TestImages.Png.Bad.Issue410_MalformedApplePng, PixelTypes.Rgba32)]

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ public static class Png
125125
// Discussion 1875: https://github.com/SixLabors/ImageSharp/discussions/1875
126126
public const string Issue1875 = "Png/raw-profile-type-exif.png";
127127

128+
// Issue 2209: https://github.com/SixLabors/ImageSharp/issues/2209
129+
public const string Issue2209IndexedWithTransparency = "Png/issues/Issue_2209.png";
130+
128131
public static class Bad
129132
{
130133
public const string MissingDataChunk = "Png/xdtn0g01.png";
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)