Browse Source

Ensure pad color is never overwritten

pull/2052/head
James Jackson-South 4 years ago
parent
commit
e6ab983374
  1. 3
      src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs
  2. 16
      src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeWorker.cs

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

@ -61,6 +61,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
Rectangle destinationRectangle = this.destinationRectangle; Rectangle destinationRectangle = this.destinationRectangle;
bool compand = this.options.Compand; bool compand = this.options.Compand;
bool premultiplyAlpha = this.options.PremultiplyAlpha; bool premultiplyAlpha = this.options.PremultiplyAlpha;
this.options.PadColor = Color.GreenYellow;
bool shouldFill = (this.options.Mode == ResizeMode.BoxPad || this.options.Mode == ResizeMode.Pad) bool shouldFill = (this.options.Mode == ResizeMode.BoxPad || this.options.Mode == ResizeMode.Pad)
&& this.options.PadColor != default; && this.options.PadColor != default;
TPixel fillColor = this.options.PadColor.ToPixel<TPixel>(); TPixel fillColor = this.options.PadColor.ToPixel<TPixel>();

16
src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeWorker.cs

@ -80,19 +80,19 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
int numberOfWindowBands = ResizeHelper.CalculateResizeWorkerHeightInWindowBands( int numberOfWindowBands = ResizeHelper.CalculateResizeWorkerHeightInWindowBands(
this.windowBandHeight, this.windowBandHeight,
destWidth, targetWorkingRect.Width,
workingBufferLimitHintInBytes); workingBufferLimitHintInBytes);
this.workerHeight = Math.Min(this.sourceRectangle.Height, numberOfWindowBands * this.windowBandHeight); this.workerHeight = Math.Min(this.sourceRectangle.Height, numberOfWindowBands * this.windowBandHeight);
this.transposedFirstPassBuffer = configuration.MemoryAllocator.Allocate2D<Vector4>( this.transposedFirstPassBuffer = configuration.MemoryAllocator.Allocate2D<Vector4>(
this.workerHeight, this.workerHeight,
destWidth, targetWorkingRect.Width,
preferContiguosImageBuffers: true, preferContiguosImageBuffers: true,
options: AllocationOptions.Clean); options: AllocationOptions.Clean);
this.tempRowBuffer = configuration.MemoryAllocator.Allocate<Vector4>(this.sourceRectangle.Width); this.tempRowBuffer = configuration.MemoryAllocator.Allocate<Vector4>(this.sourceRectangle.Width);
this.tempColumnBuffer = configuration.MemoryAllocator.Allocate<Vector4>(destWidth); this.tempColumnBuffer = configuration.MemoryAllocator.Allocate<Vector4>(targetWorkingRect.Width);
this.currentWindow = new RowInterval(0, this.workerHeight); this.currentWindow = new RowInterval(0, this.workerHeight);
} }
@ -118,6 +118,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
// When creating transposedFirstPassBuffer, we made sure it's contiguous: // When creating transposedFirstPassBuffer, we made sure it's contiguous:
Span<Vector4> transposedFirstPassBufferSpan = this.transposedFirstPassBuffer.DangerousGetSingleSpan(); Span<Vector4> transposedFirstPassBufferSpan = this.transposedFirstPassBuffer.DangerousGetSingleSpan();
int left = this.targetWorkingRect.Left;
int right = this.targetWorkingRect.Right;
int width = this.targetWorkingRect.Width;
for (int y = rowInterval.Min; y < rowInterval.Max; y++) for (int y = rowInterval.Min; y < rowInterval.Max; y++)
{ {
// Ensure offsets are normalized for cropping and padding. // Ensure offsets are normalized for cropping and padding.
@ -131,17 +134,18 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
ref Vector4 tempRowBase = ref MemoryMarshal.GetReference(tempColSpan); ref Vector4 tempRowBase = ref MemoryMarshal.GetReference(tempColSpan);
int top = kernel.StartIndex - this.currentWindow.Min; int top = kernel.StartIndex - this.currentWindow.Min;
ref Vector4 fpBase = ref transposedFirstPassBufferSpan[top]; ref Vector4 fpBase = ref transposedFirstPassBufferSpan[top];
for (int x = 0; x < this.destWidth; x++) for (int x = left; x < right; x++)
{ {
ref Vector4 firstPassColumnBase = ref Unsafe.Add(ref fpBase, x * this.workerHeight); ref Vector4 firstPassColumnBase = ref Unsafe.Add(ref fpBase, x * this.workerHeight);
// Destination color components // Destination color components
Unsafe.Add(ref tempRowBase, x) = kernel.ConvolveCore(ref firstPassColumnBase); Unsafe.Add(ref tempRowBase, x - left) = kernel.ConvolveCore(ref firstPassColumnBase);
} }
Span<TPixel> targetRowSpan = destination.DangerousGetRowSpan(y); Span<TPixel> targetRowSpan = destination.DangerousGetRowSpan(y).Slice(left, width);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, tempColSpan, targetRowSpan, this.conversionModifiers); PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, tempColSpan, targetRowSpan, this.conversionModifiers);
} }

Loading…
Cancel
Save