Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public ResizeProcessor(ResizeOptions options, Size sourceSize)
this.DestinationHeight = size.Height;
this.DestinationRectangle = rectangle;
this.Compand = options.Compand;
this.PremultiplyAlpha = options.PremultiplyAlpha;
}

/// <summary>
Expand Down Expand Up @@ -53,6 +54,11 @@ public ResizeProcessor(ResizeOptions options, Size sourceSize)
/// </summary>
public bool Compand { get; }

/// <summary>
/// Gets a value indicating whether to premultiply the alpha (if it exists) during the resize operation.
/// </summary>
public bool PremultiplyAlpha { get; }

/// <inheritdoc />
public override ICloningImageProcessor<TPixel> CreatePixelSpecificCloningProcessor<TPixel>(Configuration configuration, Image<TPixel> source, Rectangle sourceRectangle)
=> new ResizeProcessor<TPixel>(configuration, this, source, sourceRectangle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal class ResizeProcessor<TPixel> : TransformProcessor<TPixel>, IResampling
private readonly IResampler resampler;
private readonly Rectangle destinationRectangle;
private readonly bool compand;
private readonly bool premultiplyAlpha;
private Image<TPixel> destination;

public ResizeProcessor(Configuration configuration, ResizeProcessor definition, Image<TPixel> source, Rectangle sourceRectangle)
Expand All @@ -30,6 +31,7 @@ public ResizeProcessor(Configuration configuration, ResizeProcessor definition,
this.destinationHeight = definition.DestinationHeight;
this.destinationRectangle = definition.DestinationRectangle;
this.resampler = definition.Sampler;
this.premultiplyAlpha = definition.PremultiplyAlpha;
this.compand = definition.Compand;
}

Expand Down Expand Up @@ -60,6 +62,7 @@ public void ApplyTransform<TResampler>(in TResampler sampler)
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
Expand Down Expand Up @@ -128,7 +131,8 @@ public void ApplyTransform<TResampler>(in TResampler sampler)
sourceRectangle,
destinationRectangle,
interest,
compand);
compand,
premultiplyAlpha);
}
}

Expand Down Expand Up @@ -159,6 +163,18 @@ private static void ApplyNNResizeFrameTransform(
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<TPixel> source,
Expand All @@ -168,10 +184,10 @@ private static void ApplyResizeFrameTransform(
Rectangle sourceRectangle,
Rectangle destinationRectangle,
Rectangle interest,
bool compand)
bool compand,
bool premultiplyAlpha)
{
PixelConversionModifiers conversionModifiers =
PixelConversionModifiers.Premultiply.ApplyCompanding(compand);
PixelConversionModifiers conversionModifiers = GetModifiers(compand, premultiplyAlpha);

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

Expand Down
6 changes: 6 additions & 0 deletions src/ImageSharp/Processing/ResizeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,11 @@ public class ResizeOptions
/// Gets or sets the target rectangle to resize into.
/// </summary>
public Rectangle? TargetRectangle { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to premultiply
/// the alpha (if it exists) during the resize operation.
/// </summary>
public bool PremultiplyAlpha { get; set; } = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,32 @@ public void Resize_DoesNotBleedAlphaPixels<TPixel>(TestImageProvider<TPixel> pro
appendSourceFileOrDescription: false);
}

[Theory]
[WithFile(TestImages.Png.Kaboom, DefaultPixelType, false)]
[WithFile(TestImages.Png.Kaboom, DefaultPixelType, true)]
public void Resize_PremultiplyAlpha<TPixel>(TestImageProvider<TPixel> provider, bool premultiplyAlpha)
where TPixel : unmanaged, IPixel<TPixel>
{
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<TPixel>(TestImageProvider<TPixel> provider)
Expand Down