Skip to content

Commit 71357d2

Browse files
Fix PNG encoder blending.
1 parent eb6b0ab commit 71357d2

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

src/ImageSharp/Formats/Png/PngEncoderCore.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,17 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
272272
ImageFrame<TPixel>? nextFrame = currentFrameIndex < image.Frames.Count - 1 ? image.Frames[currentFrameIndex + 1] : null;
273273

274274
frameMetadata = GetPngFrameMetadata(currentFrame);
275-
bool blend = frameMetadata.BlendMethod == PngBlendMethod.Over;
275+
276+
// Determine whether to blend the current frame over the existing canvas.
277+
// Blending is applied only when the blend method is 'Over' (source-over blending)
278+
// and when the frame's disposal method is not 'RestoreToPrevious', which indicates that
279+
// the frame should not permanently alter the canvas.
280+
bool blend = frameMetadata.BlendMethod == PngBlendMethod.Over
281+
&& frameMetadata.DisposalMethod != PngDisposalMethod.RestoreToPrevious;
282+
283+
// Establish the background color for the current frame.
284+
// If the disposal method is 'RestoreToBackground', use the predefined background color;
285+
// otherwise, use transparent, as no explicit background restoration is needed.
276286
Color background = frameMetadata.DisposalMethod == PngDisposalMethod.RestoreToBackground
277287
? this.backgroundColor.Value
278288
: Color.Transparent;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ public void Encode_WithPngTransparentColorBehaviorClear_Works(PngColorType color
449449
[WithFile(TestImages.Png.APng, PixelTypes.Rgba32)]
450450
[WithFile(TestImages.Png.DefaultNotAnimated, PixelTypes.Rgba32)]
451451
[WithFile(TestImages.Png.FrameOffset, PixelTypes.Rgba32)]
452+
[WithFile(TestImages.Png.Issue2882, PixelTypes.Rgba32)]
452453
public void Encode_APng<TPixel>(TestImageProvider<TPixel> provider)
453454
where TPixel : unmanaged, IPixel<TPixel>
454455
{

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public static class Png
7676
public const string FrameOffset = "Png/animated/frame-offset.png";
7777
public const string DefaultNotAnimated = "Png/animated/default-not-animated.png";
7878
public const string Issue2666 = "Png/issues/Issue_2666.png";
79+
public const string Issue2882 = "Png/issues/Issue_2882.png";
7980

8081
// Filtered test images from http://www.schaik.com/pngsuite/pngsuite_fil_png.html
8182
public const string Filter0 = "Png/filter0.png";
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)