Browse Source

Optimize memory usage for resize pad operations

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

2
src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeKernelMap.cs

@ -101,7 +101,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
/// Returns a <see cref="ResizeKernel"/> for an index value between 0 and DestinationSize - 1. /// Returns a <see cref="ResizeKernel"/> for an index value between 0 and DestinationSize - 1.
/// </summary> /// </summary>
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
internal ref ResizeKernel GetKernel(int destIdx) => ref this.kernels[destIdx]; internal ref ResizeKernel GetKernel(nint destIdx) => ref this.kernels[destIdx];
/// <summary> /// <summary>
/// Computes the weights to apply at each pixel when resizing. /// Computes the weights to apply at each pixel when resizing.

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

@ -209,21 +209,18 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
// To reintroduce parallel processing, we would launch multiple workers // To reintroduce parallel processing, we would launch multiple workers
// for different row intervals of the image. // for different row intervals of the image.
using (var worker = new ResizeWorker<TPixel>( using var worker = new ResizeWorker<TPixel>(
configuration, configuration,
sourceRegion, sourceRegion,
conversionModifiers, conversionModifiers,
horizontalKernelMap, horizontalKernelMap,
verticalKernelMap, verticalKernelMap,
destination.Width,
interest, interest,
destinationRectangle.Location)) destinationRectangle.Location);
{ worker.Initialize();
worker.Initialize();
var workingInterval = new RowInterval(interest.Top, interest.Bottom); var workingInterval = new RowInterval(interest.Top, interest.Bottom);
worker.FillDestinationPixels(workingInterval, destination.PixelBuffer); worker.FillDestinationPixels(workingInterval, destination.PixelBuffer);
}
} }
private readonly struct NNRowOperation : IRowOperation private readonly struct NNRowOperation : IRowOperation

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

@ -39,8 +39,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
private readonly ResizeKernelMap verticalKernelMap; private readonly ResizeKernelMap verticalKernelMap;
private readonly int destWidth;
private readonly Rectangle targetWorkingRect; private readonly Rectangle targetWorkingRect;
private readonly Point targetOrigin; private readonly Point targetOrigin;
@ -57,7 +55,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
PixelConversionModifiers conversionModifiers, PixelConversionModifiers conversionModifiers,
ResizeKernelMap horizontalKernelMap, ResizeKernelMap horizontalKernelMap,
ResizeKernelMap verticalKernelMap, ResizeKernelMap verticalKernelMap,
int destWidth,
Rectangle targetWorkingRect, Rectangle targetWorkingRect,
Point targetOrigin) Point targetOrigin)
{ {
@ -67,7 +64,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
this.conversionModifiers = conversionModifiers; this.conversionModifiers = conversionModifiers;
this.horizontalKernelMap = horizontalKernelMap; this.horizontalKernelMap = horizontalKernelMap;
this.verticalKernelMap = verticalKernelMap; this.verticalKernelMap = verticalKernelMap;
this.destWidth = destWidth;
this.targetWorkingRect = targetWorkingRect; this.targetWorkingRect = targetWorkingRect;
this.targetOrigin = targetOrigin; this.targetOrigin = targetOrigin;
@ -80,14 +76,14 @@ 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);
@ -137,9 +133,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
ref Vector4 fpBase = ref transposedFirstPassBufferSpan[top]; ref Vector4 fpBase = ref transposedFirstPassBufferSpan[top];
for (int x = left; x < right; x++) for (nint 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 - left) * this.workerHeight);
// Destination color components // Destination color components
Unsafe.Add(ref tempRowBase, x - left) = kernel.ConvolveCore(ref firstPassColumnBase); Unsafe.Add(ref tempRowBase, x - left) = kernel.ConvolveCore(ref firstPassColumnBase);
@ -174,6 +170,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
Span<Vector4> tempRowSpan = this.tempRowBuffer.GetSpan(); Span<Vector4> tempRowSpan = this.tempRowBuffer.GetSpan();
Span<Vector4> transposedFirstPassBufferSpan = this.transposedFirstPassBuffer.DangerousGetSingleSpan(); Span<Vector4> transposedFirstPassBufferSpan = this.transposedFirstPassBuffer.DangerousGetSingleSpan();
int left = this.targetWorkingRect.Left;
int right = this.targetWorkingRect.Right;
for (int y = calculationInterval.Min; y < calculationInterval.Max; y++) for (int y = calculationInterval.Min; y < calculationInterval.Max; y++)
{ {
Span<TPixel> sourceRow = this.source.DangerousGetRowSpan(y); Span<TPixel> sourceRow = this.source.DangerousGetRowSpan(y);
@ -188,13 +186,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
// Span<Vector4> firstPassSpan = transposedFirstPassBufferSpan.Slice(y - this.currentWindow.Min); // Span<Vector4> firstPassSpan = transposedFirstPassBufferSpan.Slice(y - this.currentWindow.Min);
ref Vector4 firstPassBaseRef = ref transposedFirstPassBufferSpan[y - this.currentWindow.Min]; ref Vector4 firstPassBaseRef = ref transposedFirstPassBufferSpan[y - this.currentWindow.Min];
for (int x = this.targetWorkingRect.Left; x < this.targetWorkingRect.Right; x++) for (nint x = left; x < right; x++)
{ {
ResizeKernel kernel = this.horizontalKernelMap.GetKernel(x - this.targetOrigin.X); ResizeKernel kernel = this.horizontalKernelMap.GetKernel(x - this.targetOrigin.X);
// optimization for: // optimization for:
// firstPassSpan[x * this.workerHeight] = kernel.Convolve(tempRowSpan); // firstPassSpan[x * this.workerHeight] = kernel.Convolve(tempRowSpan);
Unsafe.Add(ref firstPassBaseRef, x * this.workerHeight) = kernel.Convolve(tempRowSpan); Unsafe.Add(ref firstPassBaseRef, (x - left) * this.workerHeight) = kernel.Convolve(tempRowSpan);
} }
} }
} }

Loading…
Cancel
Save