Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/ImageSharp/Formats/Png/PngDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ internal sealed class PngDecoderCore : ImageDecoderCore
/// </summary>
private readonly int maxUncompressedLength;

/// <summary>
/// A value indicating whether the image data has been read.
/// </summary>
private bool hasImageData;

/// <summary>
/// Initializes a new instance of the <see cref="PngDecoderCore"/> class.
/// </summary>
Expand Down Expand Up @@ -746,7 +751,11 @@ private void ReadScanlines<TPixel>(
where TPixel : unmanaged, IPixel<TPixel>
{
using ZlibInflateStream inflateStream = new(this.currentStream, getData);
inflateStream.AllocateNewBytes(chunkLength, true);
if (!inflateStream.AllocateNewBytes(chunkLength, !this.hasImageData))
{
return;
}

DeflateStream dataStream = inflateStream.CompressedStream!;

if (this.header.InterlaceMethod is PngInterlaceMode.Adam7)
Expand Down Expand Up @@ -800,7 +809,7 @@ private void DecodePixelData<TPixel>(
int bytesRead = compressedStream.Read(scanSpan, currentRowBytesRead, bytesPerFrameScanline - currentRowBytesRead);
if (bytesRead <= 0)
{
return;
goto EXIT;
}

currentRowBytesRead += bytesRead;
Expand Down Expand Up @@ -845,6 +854,7 @@ private void DecodePixelData<TPixel>(
}

EXIT:
this.hasImageData = true;
blendMemory?.Dispose();
}

Expand Down Expand Up @@ -903,7 +913,7 @@ private void DecodeInterlacedPixelData<TPixel>(
int bytesRead = compressedStream.Read(this.scanline.GetSpan(), currentRowBytesRead, bytesPerInterlaceScanline - currentRowBytesRead);
if (bytesRead <= 0)
{
return;
goto EXIT;
}

currentRowBytesRead += bytesRead;
Expand Down Expand Up @@ -976,6 +986,7 @@ private void DecodeInterlacedPixelData<TPixel>(
}

EXIT:
this.hasImageData = true;
blendMemory?.Dispose();
}

Expand Down
12 changes: 11 additions & 1 deletion tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -705,10 +705,20 @@ public void Decode_BadPalette(string file)
[Theory]
[WithFile(TestImages.Png.Issue2752, PixelTypes.Rgba32)]
public void CanDecodeJustOneFrame<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
DecoderOptions options = new() { MaxFrames = 1 };
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance, options);
Assert.Equal(1, image.Frames.Count);
}

[Theory]
[WithFile(TestImages.Png.Issue2924, PixelTypes.Rgba32)]
public void CanDecode_Issue2924<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance);
image.DebugSave(provider);
image.CompareToReferenceOutput(provider);
}
}
3 changes: 3 additions & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ public static class Png
// Issue 2752: https://github.com/SixLabors/ImageSharp/issues/2752
public const string Issue2752 = "Png/issues/Issue_2752.png";

// Issue 2924: https://github.com/SixLabors/ImageSharp/issues/2924
public const string Issue2924 = "Png/issues/Issue_2924.png";

public static class Bad
{
public const string MissingDataChunk = "Png/xdtn0g01.png";
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions tests/Images/Input/Png/issues/Issue_2924.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading