Skip to content

Commit d7ef0e2

Browse files
Merge pull request #2769 from SixLabors/js/fix-2752
Correctly break during Png decoding
2 parents 1d4a287 + 8adcabe commit d7ef0e2

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

src/ImageSharp/Formats/Png/PngDecoderCore.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,13 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
204204
break;
205205
case PngChunkType.FrameControl:
206206
frameCount++;
207-
if (frameCount == this.maxFrames)
208-
{
209-
break;
210-
}
211-
212207
currentFrame = null;
213208
currentFrameControl = this.ReadFrameControlChunk(chunk.Data.GetSpan());
214209
break;
215210
case PngChunkType.FrameData:
216-
if (frameCount == this.maxFrames)
211+
if (frameCount >= this.maxFrames)
217212
{
218-
break;
213+
goto EOF;
219214
}
220215

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

269+
if (frameCount >= this.maxFrames)
270+
{
271+
goto EOF;
272+
}
273+
274274
break;
275275
case PngChunkType.Palette:
276276
this.palette = chunk.Data.GetSpan().ToArray();
@@ -389,15 +389,15 @@ public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellat
389389
break;
390390
case PngChunkType.FrameControl:
391391
++frameCount;
392-
if (frameCount == this.maxFrames)
392+
if (frameCount >= this.maxFrames)
393393
{
394394
break;
395395
}
396396

397397
lastFrameControl = this.ReadFrameControlChunk(chunk.Data.GetSpan());
398398
break;
399399
case PngChunkType.FrameData:
400-
if (frameCount == this.maxFrames)
400+
if (frameCount >= this.maxFrames)
401401
{
402402
break;
403403
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,4 +701,14 @@ public void Decode_BadPalette(string file)
701701
string path = Path.GetFullPath(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, file));
702702
using Image image = Image.Load(path);
703703
}
704+
705+
[Theory]
706+
[WithFile(TestImages.Png.Issue2752, PixelTypes.Rgba32)]
707+
public void CanDecodeJustOneFrame<TPixel>(TestImageProvider<TPixel> provider)
708+
where TPixel : unmanaged, IPixel<TPixel>
709+
{
710+
DecoderOptions options = new() { MaxFrames = 1 };
711+
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance, options);
712+
Assert.Equal(1, image.Frames.Count);
713+
}
704714
}

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ public static class Png
156156
// Issue 2668: https://github.com/SixLabors/ImageSharp/issues/2668
157157
public const string Issue2668 = "Png/issues/Issue_2668.png";
158158

159+
// Issue 2752: https://github.com/SixLabors/ImageSharp/issues/2752
160+
public const string Issue2752 = "Png/issues/Issue_2752.png";
161+
159162
public static class Bad
160163
{
161164
public const string MissingDataChunk = "Png/xdtn0g01.png";
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)