Skip to content

Commit b3146a7

Browse files
Merge branch 'master' into bp/Issue1505
2 parents 0830a97 + da7a8b7 commit b3146a7

File tree

5 files changed

+59
-5
lines changed

5 files changed

+59
-5
lines changed

src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public ResizeProcessor(ResizeOptions options, Size sourceSize)
2626
this.DestinationHeight = size.Height;
2727
this.DestinationRectangle = rectangle;
2828
this.Compand = options.Compand;
29+
this.PremultiplyAlpha = options.PremultiplyAlpha;
2930
}
3031

3132
/// <summary>
@@ -53,6 +54,11 @@ public ResizeProcessor(ResizeOptions options, Size sourceSize)
5354
/// </summary>
5455
public bool Compand { get; }
5556

57+
/// <summary>
58+
/// Gets a value indicating whether to premultiply the alpha (if it exists) during the resize operation.
59+
/// </summary>
60+
public bool PremultiplyAlpha { get; }
61+
5662
/// <inheritdoc />
5763
public override ICloningImageProcessor<TPixel> CreatePixelSpecificCloningProcessor<TPixel>(Configuration configuration, Image<TPixel> source, Rectangle sourceRectangle)
5864
=> new ResizeProcessor<TPixel>(configuration, this, source, sourceRectangle);

src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal class ResizeProcessor<TPixel> : TransformProcessor<TPixel>, IResampling
2121
private readonly IResampler resampler;
2222
private readonly Rectangle destinationRectangle;
2323
private readonly bool compand;
24+
private readonly bool premultiplyAlpha;
2425
private Image<TPixel> destination;
2526

2627
public ResizeProcessor(Configuration configuration, ResizeProcessor definition, Image<TPixel> source, Rectangle sourceRectangle)
@@ -30,6 +31,7 @@ public ResizeProcessor(Configuration configuration, ResizeProcessor definition,
3031
this.destinationHeight = definition.DestinationHeight;
3132
this.destinationRectangle = definition.DestinationRectangle;
3233
this.resampler = definition.Sampler;
34+
this.premultiplyAlpha = definition.PremultiplyAlpha;
3335
this.compand = definition.Compand;
3436
}
3537

@@ -60,6 +62,7 @@ public void ApplyTransform<TResampler>(in TResampler sampler)
6062
Rectangle sourceRectangle = this.SourceRectangle;
6163
Rectangle destinationRectangle = this.destinationRectangle;
6264
bool compand = this.compand;
65+
bool premultiplyAlpha = this.premultiplyAlpha;
6366

6467
// Handle resize dimensions identical to the original
6568
if (source.Width == destination.Width
@@ -128,7 +131,8 @@ public void ApplyTransform<TResampler>(in TResampler sampler)
128131
sourceRectangle,
129132
destinationRectangle,
130133
interest,
131-
compand);
134+
compand,
135+
premultiplyAlpha);
132136
}
133137
}
134138

@@ -159,6 +163,18 @@ private static void ApplyNNResizeFrameTransform(
159163
in operation);
160164
}
161165

166+
private static PixelConversionModifiers GetModifiers(bool compand, bool premultiplyAlpha)
167+
{
168+
if (premultiplyAlpha)
169+
{
170+
return PixelConversionModifiers.Premultiply.ApplyCompanding(compand);
171+
}
172+
else
173+
{
174+
return PixelConversionModifiers.None.ApplyCompanding(compand);
175+
}
176+
}
177+
162178
private static void ApplyResizeFrameTransform(
163179
Configuration configuration,
164180
ImageFrame<TPixel> source,
@@ -168,10 +184,10 @@ private static void ApplyResizeFrameTransform(
168184
Rectangle sourceRectangle,
169185
Rectangle destinationRectangle,
170186
Rectangle interest,
171-
bool compand)
187+
bool compand,
188+
bool premultiplyAlpha)
172189
{
173-
PixelConversionModifiers conversionModifiers =
174-
PixelConversionModifiers.Premultiply.ApplyCompanding(compand);
190+
PixelConversionModifiers conversionModifiers = GetModifiers(compand, premultiplyAlpha);
175191

176192
Buffer2DRegion<TPixel> sourceRegion = source.PixelBuffer.GetRegion(sourceRectangle);
177193

src/ImageSharp/Processing/ResizeOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,11 @@ public class ResizeOptions
4545
/// Gets or sets the target rectangle to resize into.
4646
/// </summary>
4747
public Rectangle? TargetRectangle { get; set; }
48+
49+
/// <summary>
50+
/// Gets or sets a value indicating whether to premultiply
51+
/// the alpha (if it exists) during the resize operation.
52+
/// </summary>
53+
public bool PremultiplyAlpha { get; set; } = true;
4854
}
4955
}

tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,32 @@ public void Resize_DoesNotBleedAlphaPixels<TPixel>(TestImageProvider<TPixel> pro
216216
appendSourceFileOrDescription: false);
217217
}
218218

219+
[Theory]
220+
[WithFile(TestImages.Png.Kaboom, DefaultPixelType, false)]
221+
[WithFile(TestImages.Png.Kaboom, DefaultPixelType, true)]
222+
public void Resize_PremultiplyAlpha<TPixel>(TestImageProvider<TPixel> provider, bool premultiplyAlpha)
223+
where TPixel : unmanaged, IPixel<TPixel>
224+
{
225+
string details = premultiplyAlpha ? "On" : "Off";
226+
227+
provider.RunValidatingProcessorTest(
228+
x =>
229+
{
230+
var resizeOptions = new ResizeOptions()
231+
{
232+
Size = x.GetCurrentSize() / 2,
233+
Mode = ResizeMode.Crop,
234+
Sampler = KnownResamplers.Bicubic,
235+
Compand = false,
236+
PremultiplyAlpha = premultiplyAlpha
237+
};
238+
x.Resize(resizeOptions);
239+
},
240+
details,
241+
appendPixelTypeToFileName: false,
242+
appendSourceFileOrDescription: false);
243+
}
244+
219245
[Theory]
220246
[WithFile(TestImages.Gif.Giphy, DefaultPixelType)]
221247
public void Resize_IsAppliedToAllFrames<TPixel>(TestImageProvider<TPixel> provider)

0 commit comments

Comments
 (0)