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;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.Memory; using SixLabors.Memory;
@ -89,7 +90,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
ResizeKernel ws = result.CreateKernel(i, left, right); ResizeKernel ws = result.CreateKernel(i, left, right);
result.Kernels[i] = ws; 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++) 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. // Licensed under the Apache License, Version 2.0.
using System; using System;
using System.Buffers;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
namespace SixLabors.ImageSharp.Processing.Processors.Transforms namespace SixLabors.ImageSharp.Processing.Processors.Transforms
{ {
@ -18,12 +16,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
internal struct ResizeKernel internal struct ResizeKernel
{ {
/// <summary> /// <summary>
/// The local left index position /// The left index for the destination row
/// </summary> /// </summary>
public int Left; public int Left;
/// <summary> /// <summary>
/// The length of the weights window /// The length of the kernel
/// </summary> /// </summary>
public int Length; public int Length;
@ -48,34 +46,22 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
this.Length = length; 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> /// <summary>
/// Gets the span representing the portion of the <see cref="KernelMap"/> that this window covers /// Gets the span representing the portion of the <see cref="KernelMap"/> that this window covers
/// </summary> /// </summary>
/// <returns>The <see cref="Span{T}"/></returns> /// <returns>The <see cref="Span{T}"/></returns>
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public Span<float> GetSpan() => this.buffer.Span; public Span<float> GetValues() => this.buffer.Span;
/// <summary> /// <summary>
/// Computes the sum of vectors in 'rowSpan' weighted by weight values, pointed by this <see cref="ResizeKernel"/> instance. /// Computes the sum of vectors in 'rowSpan' weighted by weight values, pointed by this <see cref="ResizeKernel"/> instance.
/// </summary> /// </summary>
/// <param name="rowSpan">The input span of vectors</param> /// <param name="rowSpan">The input span of vectors</param>
/// <param name="sourceX">The source row position.</param>
/// <returns>The weighted sum</returns> /// <returns>The weighted sum</returns>
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public Vector4 Convolve(Span<Vector4> rowSpan) public Vector4 Convolve(Span<Vector4> rowSpan)
{ {
ref float horizontalValues = ref this.GetStartReference(); ref float horizontalValues = ref MemoryMarshal.GetReference(this.GetValues());
int left = this.Left; int left = this.Left;
ref Vector4 vecPtr = ref Unsafe.Add(ref MemoryMarshal.GetReference(rowSpan), 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; this.Output = output;
} }
[Theory(Skip = "TODO: Add asserionts")] [Theory]
[InlineData(500, 200, nameof(KnownResamplers.Bicubic))] [InlineData(500, 200, nameof(KnownResamplers.Bicubic))]
[InlineData(50, 40, nameof(KnownResamplers.Bicubic))] [InlineData(50, 40, nameof(KnownResamplers.Bicubic))]
[InlineData(40, 30, nameof(KnownResamplers.Bicubic))] [InlineData(40, 30, nameof(KnownResamplers.Bicubic))]
[InlineData(15, 10, nameof(KnownResamplers.Bicubic))]
[InlineData(500, 200, nameof(KnownResamplers.Lanczos8))] [InlineData(500, 200, nameof(KnownResamplers.Lanczos8))]
[InlineData(100, 80, nameof(KnownResamplers.Lanczos8))] [InlineData(100, 80, nameof(KnownResamplers.Lanczos8))]
[InlineData(100, 10, nameof(KnownResamplers.Lanczos8))] [InlineData(100, 10, nameof(KnownResamplers.Lanczos8))]
@ -37,14 +38,15 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
var bld = new StringBuilder(); var bld = new StringBuilder();
foreach (ResizeKernel window in kernelMap.Kernels) foreach (ResizeKernel kernel in kernelMap.Kernels)
{ {
Span<float> span = window.GetSpan(); bld.Append($"({kernel.Left:D3}) || ");
for (int i = 0; i < window.Length; i++) Span<float> span = kernel.GetValues();
for (int i = 0; i < kernel.Length; i++)
{ {
float value = span[i]; float value = span[i];
bld.Append($"{value,7:F4}"); bld.Append($"{value,7:F5}");
bld.Append("| "); bld.Append(" | ");
} }
bld.AppendLine(); bld.AppendLine();

Loading…
Cancel
Save