From 0df047222b21ee2eeaa382513ec7c526e93bb37c Mon Sep 17 00:00:00 2001 From: Petar Tasev Date: Tue, 12 Jan 2021 20:15:02 -0800 Subject: [PATCH 1/3] Add PremultiplyAlpha to ResizeOptions --- .../Transforms/Resize/ResizeProcessor.cs | 6 +++++ .../Resize/ResizeProcessor{TPixel}.cs | 14 +++++++--- src/ImageSharp/Processing/ResizeOptions.cs | 6 +++++ .../Processors/Transforms/ResizeTests.cs | 26 +++++++++++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) 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) From 62a01b74755c5dde02e83a9051ebb9e29ce5a929 Mon Sep 17 00:00:00 2001 From: Petar Tasev Date: Wed, 13 Jan 2021 09:05:56 -0800 Subject: [PATCH 2/3] Split PixelConversionModifiers into a separate function. --- .../Transforms/Resize/ResizeProcessor{TPixel}.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs index 642da8230..ec238608e 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs @@ -163,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, @@ -175,9 +187,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms bool compand, bool premultiplyAlpha) { - PixelConversionModifiers conversionModifiers = premultiplyAlpha ? - PixelConversionModifiers.Premultiply.ApplyCompanding(compand) : - PixelConversionModifiers.None.ApplyCompanding(compand); + PixelConversionModifiers conversionModifiers = GetModifiers(compand, premultiplyAlpha); Buffer2DRegion sourceRegion = source.PixelBuffer.GetRegion(sourceRectangle); From 8c77c494d6a3d62d71bbf9a449ad36132f3e388d Mon Sep 17 00:00:00 2001 From: Petar Tasev Date: Wed, 13 Jan 2021 14:29:54 -0800 Subject: [PATCH 3/3] Updated referenced image submodule to latest origin master. --- tests/Images/External | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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