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..ec238608e 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);
}
}
@@ -159,6 +163,18 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
in operation);
}
+ private static PixelConversionModifiers GetModifiers(bool compand, bool premultiplyAlpha)
+ {
+ if (premultiplyAlpha)
+ {
+ return PixelConversionModifiers.Premultiply.ApplyCompanding(compand);
+ }
+ else
+ {
+ return PixelConversionModifiers.None.ApplyCompanding(compand);
+ }
+ }
+
private static void ApplyResizeFrameTransform(
Configuration configuration,
ImageFrame source,
@@ -168,10 +184,10 @@ 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 = GetModifiers(compand, premultiplyAlpha);
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)
diff --git a/tests/Images/External b/tests/Images/External
index 8b43d14d2..346070e5b 160000
--- a/tests/Images/External
+++ b/tests/Images/External
@@ -1 +1 @@
-Subproject commit 8b43d14d21ce9b436af3d12a70d38402cdba176b
+Subproject commit 346070e5ba538f1a3bbafc0ea7367404c5f8c9ab