@@ -246,8 +246,13 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
246
246
currentFrameControl . Value ,
247
247
cancellationToken ) ;
248
248
249
- previousFrame = currentFrame ;
250
- previousFrameControl = currentFrameControl ;
249
+ // if current frame dispose is restore to previous, then from future frame's perspective, it never happened
250
+ if ( currentFrameControl . Value . DisposeOperation != PngDisposalMethod . RestoreToPrevious )
251
+ {
252
+ previousFrame = currentFrame ;
253
+ previousFrameControl = currentFrameControl ;
254
+ }
255
+
251
256
break ;
252
257
case PngChunkType . Data :
253
258
@@ -645,18 +650,18 @@ private void InitializeFrame<TPixel>(
645
650
out ImageFrame < TPixel > frame )
646
651
where TPixel : unmanaged, IPixel < TPixel >
647
652
{
648
- // We create a clone of the previous frame and add it.
649
- // We will overpaint the difference of pixels on the current frame to create a complete image.
650
- // This ensures that we have enough pixel data to process without distortion. #2450
651
653
frame = image . Frames . AddFrame ( previousFrame ?? image . Frames . RootFrame ) ;
652
654
653
- // If the first `fcTL` chunk uses a `dispose_op` of APNG_DISPOSE_OP_PREVIOUS it should be treated as APNG_DISPOSE_OP_BACKGROUND.
654
- if ( previousFrameControl . DisposeOperation == PngDisposalMethod . RestoreToBackground
655
- || ( previousFrame is null && previousFrameControl . DisposeOperation == PngDisposalMethod . RestoreToPrevious ) )
655
+ // if restoring to before first frame, restore to background
656
+ if ( previousFrame is null && previousFrameControl . DisposeOperation == PngDisposalMethod . RestoreToPrevious )
657
+ {
658
+ Buffer2DRegion < TPixel > pixelRegion = frame . PixelBuffer . GetRegion ( ) ;
659
+ pixelRegion . Clear ( ) ;
660
+ }
661
+ else if ( previousFrameControl . DisposeOperation == PngDisposalMethod . RestoreToBackground )
656
662
{
657
663
Rectangle restoreArea = previousFrameControl . Bounds ;
658
- Rectangle interest = Rectangle . Intersect ( frame . Bounds ( ) , restoreArea ) ;
659
- Buffer2DRegion < TPixel > pixelRegion = frame . PixelBuffer . GetRegion ( interest ) ;
664
+ Buffer2DRegion < TPixel > pixelRegion = frame . PixelBuffer . GetRegion ( restoreArea ) ;
660
665
pixelRegion . Clear ( ) ;
661
666
}
662
667
0 commit comments