@@ -234,8 +234,7 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
234
234
PngThrowHelper . ThrowMissingFrameControl ( ) ;
235
235
}
236
236
237
- previousFrameControl ??= new ( ( uint ) this . header . Width , ( uint ) this . header . Height ) ;
238
- this . InitializeFrame ( previousFrameControl . Value , currentFrameControl . Value , image , previousFrame , out currentFrame ) ;
237
+ this . InitializeFrame ( previousFrameControl , currentFrameControl . Value , image , previousFrame , out currentFrame ) ;
239
238
240
239
this . currentStream . Position += 4 ;
241
240
this . ReadScanlines (
@@ -255,7 +254,7 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
255
254
256
255
break ;
257
256
case PngChunkType . Data :
258
-
257
+ pngMetadata . DefaultImageAnimated = currentFrameControl != null ;
259
258
currentFrameControl ??= new ( ( uint ) this . header . Width , ( uint ) this . header . Height ) ;
260
259
if ( image is null )
261
260
{
@@ -272,9 +271,12 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
272
271
this . ReadNextDataChunk ,
273
272
currentFrameControl . Value ,
274
273
cancellationToken ) ;
274
+ if ( pngMetadata . DefaultImageAnimated )
275
+ {
276
+ previousFrame = currentFrame ;
277
+ previousFrameControl = currentFrameControl ;
278
+ }
275
279
276
- previousFrame = currentFrame ;
277
- previousFrameControl = currentFrameControl ;
278
280
break ;
279
281
case PngChunkType . Palette :
280
282
this . palette = chunk . Data . GetSpan ( ) . ToArray ( ) ;
@@ -643,7 +645,7 @@ private void InitializeImage<TPixel>(ImageMetadata metadata, FrameControl frameC
643
645
/// <param name="previousFrame">The previous frame.</param>
644
646
/// <param name="frame">The created frame</param>
645
647
private void InitializeFrame < TPixel > (
646
- FrameControl previousFrameControl ,
648
+ FrameControl ? previousFrameControl ,
647
649
FrameControl currentFrameControl ,
648
650
Image < TPixel > image ,
649
651
ImageFrame < TPixel > ? previousFrame ,
@@ -652,15 +654,15 @@ private void InitializeFrame<TPixel>(
652
654
{
653
655
frame = image . Frames . AddFrame ( previousFrame ?? image . Frames . RootFrame ) ;
654
656
655
- // if restoring to before first frame, restore to background
656
- if ( previousFrame is null && previousFrameControl . DisposeOperation == PngDisposalMethod . RestoreToPrevious )
657
+ // If restoring to before first frame, restore to background. Same if first frame (previousFrameControl null).
658
+ if ( previousFrameControl == null || ( previousFrame is null && previousFrameControl . Value . DisposeOperation == PngDisposalMethod . RestoreToPrevious ) )
657
659
{
658
660
Buffer2DRegion < TPixel > pixelRegion = frame . PixelBuffer . GetRegion ( ) ;
659
661
pixelRegion . Clear ( ) ;
660
662
}
661
- else if ( previousFrameControl . DisposeOperation == PngDisposalMethod . RestoreToBackground )
663
+ else if ( previousFrameControl . Value . DisposeOperation == PngDisposalMethod . RestoreToBackground )
662
664
{
663
- Rectangle restoreArea = previousFrameControl . Bounds ;
665
+ Rectangle restoreArea = previousFrameControl . Value . Bounds ;
664
666
Buffer2DRegion < TPixel > pixelRegion = frame . PixelBuffer . GetRegion ( restoreArea ) ;
665
667
pixelRegion . Clear ( ) ;
666
668
}
0 commit comments