Browse Source

do not allocate unnecessaryly large buffer in WeightsBuffer

af/merge-core
Anton Firszov 8 years ago
parent
commit
607cec8fdb
  1. 2
      src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs
  2. 7
      src/ImageSharp/Processing/Processors/Transforms/WeightsBuffer.cs
  3. 10
      src/ImageSharp/Processing/Processors/Transforms/WeightsWindow.cs

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

@ -162,7 +162,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
IResampler sampler = this.Sampler; IResampler sampler = this.Sampler;
float radius = MathF.Ceiling(scale * sampler.Radius); float radius = MathF.Ceiling(scale * sampler.Radius);
var result = new WeightsBuffer(memoryAllocator, sourceSize, destinationSize); var result = new WeightsBuffer(memoryAllocator, destinationSize, radius);
for (int i = 0; i < destinationSize; i++) for (int i = 0; i < destinationSize; i++)
{ {

7
src/ImageSharp/Processing/Processors/Transforms/WeightsBuffer.cs

@ -19,11 +19,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
/// Initializes a new instance of the <see cref="WeightsBuffer"/> class. /// Initializes a new instance of the <see cref="WeightsBuffer"/> class.
/// </summary> /// </summary>
/// <param name="memoryAllocator">The <see cref="MemoryAllocator"/> to use for allocations.</param> /// <param name="memoryAllocator">The <see cref="MemoryAllocator"/> to use for allocations.</param>
/// <param name="sourceSize">The size of the source window</param>
/// <param name="destinationSize">The size of the destination window</param> /// <param name="destinationSize">The size of the destination window</param>
public WeightsBuffer(MemoryAllocator memoryAllocator, int sourceSize, int destinationSize) /// <param name="kernelRadius">The radius of the kernel</param>
public WeightsBuffer(MemoryAllocator memoryAllocator, int destinationSize, float kernelRadius)
{ {
this.dataBuffer = memoryAllocator.Allocate2D<float>(sourceSize, destinationSize, AllocationOptions.Clean); int width = (int)Math.Ceiling(kernelRadius * 2);
this.dataBuffer = memoryAllocator.Allocate2D<float>(width, destinationSize, AllocationOptions.Clean);
this.Weights = new WeightsWindow[destinationSize]; this.Weights = new WeightsWindow[destinationSize];
} }

10
src/ImageSharp/Processing/Processors/Transforms/WeightsWindow.cs

@ -35,7 +35,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
/// <summary> /// <summary>
/// The buffer containing the weights values. /// The buffer containing the weights values.
/// </summary> /// </summary>
private readonly MemorySource<float> buffer; private readonly Memory<float> buffer;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WeightsWindow"/> struct. /// Initializes a new instance of the <see cref="WeightsWindow"/> struct.
@ -47,9 +47,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal WeightsWindow(int index, int left, Buffer2D<float> buffer, int length) internal WeightsWindow(int index, int left, Buffer2D<float> buffer, int length)
{ {
this.flatStartIndex = (index * buffer.Width) + left; this.flatStartIndex = index * buffer.Width;
this.Left = left; this.Left = left;
this.buffer = buffer.MemorySource; this.buffer = buffer.MemorySource.Memory;
this.Length = length; this.Length = length;
} }
@ -60,7 +60,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref float GetStartReference() public ref float GetStartReference()
{ {
Span<float> span = this.buffer.GetSpan(); Span<float> span = this.buffer.Span;
return ref span[this.flatStartIndex]; return ref span[this.flatStartIndex];
} }
@ -69,7 +69,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
/// </summary> /// </summary>
/// <returns>The <see cref="Span{T}"/></returns> /// <returns>The <see cref="Span{T}"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<float> GetWindowSpan() => this.buffer.GetSpan().Slice(this.flatStartIndex, this.Length); public Span<float> GetWindowSpan() => this.buffer.Span.Slice(this.flatStartIndex, this.Length);
/// <summary> /// <summary>
/// Computes the sum of vectors in 'rowSpan' weighted by weight values, pointed by this <see cref="WeightsWindow"/> instance. /// Computes the sum of vectors in 'rowSpan' weighted by weight values, pointed by this <see cref="WeightsWindow"/> instance.

Loading…
Cancel
Save