Browse Source

minor refactor on ResizeKernel

pull/781/head
Anton Firszov 8 years ago
parent
commit
32e0497dca
  1. 3
      src/ImageSharp/Processing/Processors/Transforms/KernelMap.cs
  2. 22
      src/ImageSharp/Processing/Processors/Transforms/ResizeKernel.cs
  3. 14
      tests/ImageSharp.Tests/Processing/Processors/Transforms/KernelMapTests.cs

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

@ -3,6 +3,7 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
@ -89,7 +90,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
ResizeKernel ws = result.CreateKernel(i, left, right);
result.Kernels[i] = ws;
ref float weightsBaseRef = ref ws.GetStartReference();
ref float weightsBaseRef = ref MemoryMarshal.GetReference(ws.GetValues());
for (int j = left; j <= right; j++)
{

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

@ -2,13 +2,11 @@
// Licensed under the Apache License, Version 2.0.
using System;
using System.Buffers;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
namespace SixLabors.ImageSharp.Processing.Processors.Transforms
{
@ -18,12 +16,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
internal struct ResizeKernel
{
/// <summary>
/// The local left index position
/// The left index for the destination row
/// </summary>
public int Left;
/// <summary>
/// The length of the weights window
/// The length of the kernel
/// </summary>
public int Length;
@ -48,34 +46,22 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
this.Length = length;
}
/// <summary>
/// Gets a reference to the first item of the window.
/// </summary>
/// <returns>The reference to the first item of the window</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public ref float GetStartReference()
{
Span<float> span = this.buffer.Span;
return ref span[0];
}
/// <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> GetSpan() => this.buffer.Span;
public Span<float> GetValues() => this.buffer.Span;
/// <summary>
/// Computes the sum of vectors in 'rowSpan' weighted by weight values, pointed by this <see cref="ResizeKernel"/> instance.
/// </summary>
/// <param name="rowSpan">The input span of vectors</param>
/// <param name="sourceX">The source row position.</param>
/// <returns>The weighted sum</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public Vector4 Convolve(Span<Vector4> rowSpan)
{
ref float horizontalValues = ref this.GetStartReference();
ref float horizontalValues = ref MemoryMarshal.GetReference(this.GetValues());
int left = this.Left;
ref Vector4 vecPtr = ref Unsafe.Add(ref MemoryMarshal.GetReference(rowSpan), left);

14
tests/ImageSharp.Tests/Processing/Processors/Transforms/KernelMapTests.cs

@ -21,10 +21,11 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
this.Output = output;
}
[Theory(Skip = "TODO: Add asserionts")]
[Theory]
[InlineData(500, 200, nameof(KnownResamplers.Bicubic))]
[InlineData(50, 40, nameof(KnownResamplers.Bicubic))]
[InlineData(40, 30, nameof(KnownResamplers.Bicubic))]
[InlineData(15, 10, nameof(KnownResamplers.Bicubic))]
[InlineData(500, 200, nameof(KnownResamplers.Lanczos8))]
[InlineData(100, 80, nameof(KnownResamplers.Lanczos8))]
[InlineData(100, 10, nameof(KnownResamplers.Lanczos8))]
@ -37,14 +38,15 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
var bld = new StringBuilder();
foreach (ResizeKernel window in kernelMap.Kernels)
foreach (ResizeKernel kernel in kernelMap.Kernels)
{
Span<float> span = window.GetSpan();
for (int i = 0; i < window.Length; i++)
bld.Append($"({kernel.Left:D3}) || ");
Span<float> span = kernel.GetValues();
for (int i = 0; i < kernel.Length; i++)
{
float value = span[i];
bld.Append($"{value,7:F4}");
bld.Append("| ");
bld.Append($"{value,7:F5}");
bld.Append(" | ");
}
bld.AppendLine();

Loading…
Cancel
Save