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