Skip to content

Commit

Permalink
Correctly break during decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
JimBobSquarePants committed Jul 10, 2024
1 parent b496109 commit d7f7d5a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/ImageSharp/Formats/Png/PngDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,13 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
break;
case PngChunkType.FrameControl:
frameCount++;
if (frameCount == this.maxFrames)
{
break;
}

currentFrame = null;
currentFrameControl = this.ReadFrameControlChunk(chunk.Data.GetSpan());
break;
case PngChunkType.FrameData:
if (frameCount == this.maxFrames)
{
break;
goto EOF;
}

if (image is null)
Expand Down Expand Up @@ -271,6 +266,11 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
previousFrameControl = currentFrameControl;
}

if (frameCount == this.maxFrames)
{
goto EOF;
}

break;
case PngChunkType.Palette:
this.palette = chunk.Data.GetSpan().ToArray();
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 @@ -701,4 +701,14 @@ 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.Issue2752, PixelTypes.Rgba32)]
public void CanDecodeJustOneFrame<TPixel>(TestImageProvider<TPixel> provider)
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);
}
}
3 changes: 3 additions & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ public static class Png
// Issue 2668: https://github.com/SixLabors/ImageSharp/issues/2668
public const string Issue2668 = "Png/issues/Issue_2668.png";

// Issue 2752: https://github.com/SixLabors/ImageSharp/issues/2752
public const string Issue2752 = "Png/issues/Issue_2752.png";

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

0 comments on commit d7f7d5a

Please sign in to comment.