Browse Source

refactor ResizeKernel creation

pull/781/head
Anton Firszov 8 years ago
parent
commit
41972e90c8
  1. 13
      src/ImageSharp/Processing/Processors/Transforms/KernelMap.cs
  2. 34
      src/ImageSharp/Processing/Processors/Transforms/ResizeKernel.cs

13
src/ImageSharp/Processing/Processors/Transforms/KernelMap.cs

@ -25,7 +25,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
/// <param name="memoryAllocator">The <see cref="MemoryAllocator"/> to use for allocations.</param>
/// <param name="destinationSize">The size of the destination window</param>
/// <param name="kernelRadius">The radius of the kernel</param>
public KernelMap(MemoryAllocator memoryAllocator, int destinationSize, float kernelRadius)
private KernelMap(MemoryAllocator memoryAllocator, int destinationSize, float kernelRadius)
{
this.DestinationSize = destinationSize;
int width = (int)Math.Ceiling(kernelRadius * 2);
@ -125,13 +125,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
/// <summary>
/// Slices a weights value at the given positions.
/// </summary>
/// <param name="destIdx">The index in destination buffer</param>
/// <param name="leftIdx">The local left index value</param>
/// <param name="rightIdx">The local right index value</param>
/// <returns>The weights</returns>
private ResizeKernel CreateKernel(int destIdx, int leftIdx, int rightIdx)
private ResizeKernel CreateKernel(int destIdx, int left, int rightIdx)
{
return new ResizeKernel(destIdx, leftIdx, this.data, rightIdx - leftIdx + 1);
int flatStartIndex = destIdx * this.data.Width;
int length = rightIdx - left + 1;
Memory<float> bufferSlice = this.data.Memory.Slice(flatStartIndex, length);
return new ResizeKernel(left, bufferSlice);
}
}
}

34
src/ImageSharp/Processing/Processors/Transforms/ResizeKernel.cs

@ -16,42 +16,36 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
internal struct ResizeKernel
{
/// <summary>
/// The left index for the destination row
/// Initializes a new instance of the <see cref="ResizeKernel"/> struct.
/// </summary>
public int Left;
[MethodImpl(InliningOptions.ShortMethod)]
internal ResizeKernel(int left, Memory<float> bufferSlice)
{
this.Left = left;
this.BufferSlice = bufferSlice;
}
/// <summary>
/// The length of the kernel
/// Gets the left index for the destination row
/// </summary>
public int Length;
public int Left { get; }
/// <summary>
/// The buffer containing the weights values.
/// Gets the slice of the buffer containing the weights values.
/// </summary>
private readonly Memory<float> buffer;
public Memory<float> BufferSlice { get; }
/// <summary>
/// Initializes a new instance of the <see cref="ResizeKernel"/> struct.
/// Gets the the length of the kernel
/// </summary>
/// <param name="index">The destination index in the buffer</param>
/// <param name="left">The local left index</param>
/// <param name="buffer">The span</param>
/// <param name="length">The length of the window</param>
[MethodImpl(InliningOptions.ShortMethod)]
internal ResizeKernel(int index, int left, Buffer2D<float> buffer, int length)
{
int flatStartIndex = index * buffer.Width;
this.Left = left;
this.buffer = buffer.MemorySource.Memory.Slice(flatStartIndex, length);
this.Length = length;
}
public int Length => this.BufferSlice.Length;
/// <summary>
/// Gets the span representing the portion of the <see cref="KernelMap"/> that this window covers
/// </summary>
/// <returns>The <see cref="Span{T}"/></returns>
[MethodImpl(InliningOptions.ShortMethod)]
public Span<float> GetValues() => this.buffer.Span;
public Span<float> GetValues() => this.BufferSlice.Span;
/// <summary>
/// Computes the sum of vectors in 'rowSpan' weighted by weight values, pointed by this <see cref="ResizeKernel"/> instance.

Loading…
Cancel
Save