@@ -228,8 +228,7 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
228
228
PngThrowHelper . ThrowMissingFrameControl ( ) ;
229
229
}
230
230
231
- previousFrameControl ??= new ( ( uint ) this . header . Width , ( uint ) this . header . Height ) ;
232
- this . InitializeFrame ( previousFrameControl . Value , currentFrameControl . Value , image , previousFrame , out currentFrame ) ;
231
+ this . InitializeFrame ( previousFrameControl , currentFrameControl . Value , image , previousFrame , out currentFrame ) ;
233
232
234
233
this . currentStream . Position += 4 ;
235
234
this . ReadScanlines (
@@ -249,7 +248,7 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
249
248
250
249
break ;
251
250
case PngChunkType . Data :
252
-
251
+ pngMetadata . DefaultImageAnimated = currentFrameControl != null ;
253
252
currentFrameControl ??= new ( ( uint ) this . header . Width , ( uint ) this . header . Height ) ;
254
253
if ( image is null )
255
254
{
@@ -266,9 +265,12 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
266
265
this . ReadNextDataChunk ,
267
266
currentFrameControl . Value ,
268
267
cancellationToken ) ;
268
+ if ( pngMetadata . DefaultImageAnimated )
269
+ {
270
+ previousFrame = currentFrame ;
271
+ previousFrameControl = currentFrameControl ;
272
+ }
269
273
270
- previousFrame = currentFrame ;
271
- previousFrameControl = currentFrameControl ;
272
274
break ;
273
275
case PngChunkType . Palette :
274
276
this . palette = chunk . Data . GetSpan ( ) . ToArray ( ) ;
@@ -637,7 +639,7 @@ private void InitializeImage<TPixel>(ImageMetadata metadata, FrameControl frameC
637
639
/// <param name="previousFrame">The previous frame.</param>
638
640
/// <param name="frame">The created frame</param>
639
641
private void InitializeFrame < TPixel > (
640
- FrameControl previousFrameControl ,
642
+ FrameControl ? previousFrameControl ,
641
643
FrameControl currentFrameControl ,
642
644
Image < TPixel > image ,
643
645
ImageFrame < TPixel > ? previousFrame ,
@@ -646,15 +648,15 @@ private void InitializeFrame<TPixel>(
646
648
{
647
649
frame = image . Frames . AddFrame ( previousFrame ?? image . Frames . RootFrame ) ;
648
650
649
- // if restoring to before first frame, restore to background
650
- if ( previousFrame is null && previousFrameControl . DisposeOperation == PngDisposalMethod . RestoreToPrevious )
651
+ // If restoring to before first frame, restore to background. Same if first frame (previousFrameControl null).
652
+ if ( previousFrameControl == null || ( previousFrame is null && previousFrameControl . Value . DisposeOperation == PngDisposalMethod . RestoreToPrevious ) )
651
653
{
652
654
Buffer2DRegion < TPixel > pixelRegion = frame . PixelBuffer . GetRegion ( ) ;
653
655
pixelRegion . Clear ( ) ;
654
656
}
655
- else if ( previousFrameControl . DisposeOperation == PngDisposalMethod . RestoreToBackground )
657
+ else if ( previousFrameControl . Value . DisposeOperation == PngDisposalMethod . RestoreToBackground )
656
658
{
657
- Rectangle restoreArea = previousFrameControl . Bounds ;
659
+ Rectangle restoreArea = previousFrameControl . Value . Bounds ;
658
660
Buffer2DRegion < TPixel > pixelRegion = frame . PixelBuffer . GetRegion ( restoreArea ) ;
659
661
pixelRegion . Clear ( ) ;
660
662
}
0 commit comments