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
21 changes: 18 additions & 3 deletions src/ImageSharp/Formats/Png/PngDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
/// </summary>
private PngChunk? nextChunk;

/// <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 @@ -565,7 +570,11 @@ private void ReadScanlines<TPixel>(PngChunk chunk, ImageFrame<TPixel> image, Png
where TPixel : unmanaged, IPixel<TPixel>
{
using var deframeStream = new ZlibInflateStream(this.currentStream, this.ReadNextDataChunk);
deframeStream.AllocateNewBytes(chunk.Length, true);
if (!deframeStream.AllocateNewBytes(chunk.Length, !this.hasImageData))
{
return;
}

DeflateStream dataStream = deframeStream.CompressedStream;

if (this.header.InterlaceMethod == PngInterlaceMode.Adam7)
Expand Down Expand Up @@ -596,7 +605,7 @@ private void DecodePixelData<TPixel>(DeflateStream compressedStream, ImageFrame<
int bytesRead = compressedStream.Read(scanlineSpan, this.currentRowBytesRead, this.bytesPerScanline - this.currentRowBytesRead);
if (bytesRead <= 0)
{
return;
goto EXIT;
}

this.currentRowBytesRead += bytesRead;
Expand Down Expand Up @@ -635,6 +644,9 @@ private void DecodePixelData<TPixel>(DeflateStream compressedStream, ImageFrame<
this.SwapScanlineBuffers();
this.currentRow++;
}

EXIT:
this.hasImageData = true;
}

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

this.currentRowBytesRead += bytesRead;
Expand Down Expand Up @@ -729,6 +741,9 @@ private void DecodeInterlacedPixelData<TPixel>(DeflateStream compressedStream, I
pass = 0;
break;
}

EXIT:
this.hasImageData = true;
}
}

Expand Down
10 changes: 10 additions & 0 deletions tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,5 +534,15 @@ public void Decode_BadPalette(string file)
string path = Path.GetFullPath(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, file));
using Image image = Image.Load(path);
}

[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);
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 @@ -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 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