diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor.cs
index 576f97a93..3d6900683 100644
--- a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor.cs
@@ -26,6 +26,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
this.DestinationHeight = size.Height;
this.DestinationRectangle = rectangle;
this.Compand = options.Compand;
+ this.PremultiplyAlpha = options.PremultiplyAlpha;
}
///
@@ -53,6 +54,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
///
public bool Compand { get; }
+ ///
+ /// Gets a value indicating whether to premultiply the alpha (if it exists) during the resize operation.
+ ///
+ public bool PremultiplyAlpha { get; }
+
///
public override ICloningImageProcessor CreatePixelSpecificCloningProcessor(Configuration configuration, Image source, Rectangle sourceRectangle)
=> new ResizeProcessor(configuration, this, source, sourceRectangle);
diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs
index 3c033d89e..642da8230 100644
--- a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs
+++ b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs
@@ -21,6 +21,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
private readonly IResampler resampler;
private readonly Rectangle destinationRectangle;
private readonly bool compand;
+ private readonly bool premultiplyAlpha;
private Image destination;
public ResizeProcessor(Configuration configuration, ResizeProcessor definition, Image source, Rectangle sourceRectangle)
@@ -30,6 +31,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
this.destinationHeight = definition.DestinationHeight;
this.destinationRectangle = definition.DestinationRectangle;
this.resampler = definition.Sampler;
+ this.premultiplyAlpha = definition.PremultiplyAlpha;
this.compand = definition.Compand;
}
@@ -60,6 +62,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
Rectangle sourceRectangle = this.SourceRectangle;
Rectangle destinationRectangle = this.destinationRectangle;
bool compand = this.compand;
+ bool premultiplyAlpha = this.premultiplyAlpha;
// Handle resize dimensions identical to the original
if (source.Width == destination.Width
@@ -128,7 +131,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
sourceRectangle,
destinationRectangle,
interest,
- compand);
+ compand,
+ premultiplyAlpha);
}
}
@@ -168,10 +172,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
Rectangle sourceRectangle,
Rectangle destinationRectangle,
Rectangle interest,
- bool compand)
+ bool compand,
+ bool premultiplyAlpha)
{
- PixelConversionModifiers conversionModifiers =
- PixelConversionModifiers.Premultiply.ApplyCompanding(compand);
+ PixelConversionModifiers conversionModifiers = premultiplyAlpha ?
+ PixelConversionModifiers.Premultiply.ApplyCompanding(compand) :
+ PixelConversionModifiers.None.ApplyCompanding(compand);
Buffer2DRegion sourceRegion = source.PixelBuffer.GetRegion(sourceRectangle);
diff --git a/src/ImageSharp/Processing/ResizeOptions.cs b/src/ImageSharp/Processing/ResizeOptions.cs
index bad8bd455..4b31998da 100644
--- a/src/ImageSharp/Processing/ResizeOptions.cs
+++ b/src/ImageSharp/Processing/ResizeOptions.cs
@@ -45,5 +45,11 @@ namespace SixLabors.ImageSharp.Processing
/// Gets or sets the target rectangle to resize into.
///
public Rectangle? TargetRectangle { get; set; }
+
+ ///
+ /// Gets or sets a value indicating whether to premultiply
+ /// the alpha (if it exists) during the resize operation.
+ ///
+ public bool PremultiplyAlpha { get; set; } = true;
}
}
diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs
index 4a20f4e56..f4a94782f 100644
--- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs
+++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs
@@ -216,6 +216,32 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
appendSourceFileOrDescription: false);
}
+ [Theory]
+ [WithFile(TestImages.Png.Kaboom, DefaultPixelType, false)]
+ [WithFile(TestImages.Png.Kaboom, DefaultPixelType, true)]
+ public void Resize_PremultiplyAlpha(TestImageProvider provider, bool premultiplyAlpha)
+ where TPixel : unmanaged, IPixel
+ {
+ string details = premultiplyAlpha ? "On" : "Off";
+
+ provider.RunValidatingProcessorTest(
+ x =>
+ {
+ var resizeOptions = new ResizeOptions()
+ {
+ Size = x.GetCurrentSize() / 2,
+ Mode = ResizeMode.Crop,
+ Sampler = KnownResamplers.Bicubic,
+ Compand = false,
+ PremultiplyAlpha = premultiplyAlpha
+ };
+ x.Resize(resizeOptions);
+ },
+ details,
+ appendPixelTypeToFileName: false,
+ appendSourceFileOrDescription: false);
+ }
+
[Theory]
[WithFile(TestImages.Gif.Giphy, DefaultPixelType)]
public void Resize_IsAppliedToAllFrames(TestImageProvider provider)