Browse Source

simplify ResizeProcessor

pull/731/head
Anton Firszov 7 years ago
parent
commit
e907ec9561
  1. 27
      src/ImageSharp/Processing/Processors/Transforms/ResizeKernel.cs
  2. 65
      src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs
  3. 2
      tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs

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

@ -96,32 +96,5 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
return result;
}
/// <summary>
/// Computes the sum of vectors in 'rowSpan' weighted by weight values, pointed by this <see cref="ResizeKernel"/> instance.
/// Applies <see cref="Vector4Extensions.Expand(float)"/> to all input vectors.
/// </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 ConvolveExpand(Span<Vector4> rowSpan, int sourceX)
{
ref float horizontalValues = ref this.GetStartReference();
int left = this.Left;
ref Vector4 vecPtr = ref Unsafe.Add(ref MemoryMarshal.GetReference(rowSpan), left + sourceX);
// Destination color components
Vector4 result = Vector4.Zero;
for (int i = 0; i < this.Length; i++)
{
float weight = Unsafe.Add(ref horizontalValues, i);
Vector4 v = Unsafe.Add(ref vecPtr, i);
result += v.Expand() * weight;
}
return result;
}
}
}

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

@ -2,13 +2,12 @@
// Licensed under the Apache License, Version 2.0.
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.ParallelUtils;
@ -258,22 +257,14 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
if (this.Compand)
{
for (int x = minX; x < maxX; x++)
{
ResizeKernel window = this.horizontalKernelMap.Kernels[x - startX];
Unsafe.Add(ref firstPassBaseRef, x * sourceHeight) =
window.ConvolveExpand(tempRowSpan, sourceX);
}
Vector4Extensions.Expand(tempRowSpan);
}
else
for (int x = minX; x < maxX; x++)
{
for (int x = minX; x < maxX; x++)
{
ResizeKernel window = this.horizontalKernelMap.Kernels[x - startX];
Unsafe.Add(ref firstPassBaseRef, x * sourceHeight) =
window.Convolve(tempRowSpan, sourceX);
}
ResizeKernel window = this.horizontalKernelMap.Kernels[x - startX];
Unsafe.Add(ref firstPassBaseRef, x * sourceHeight) =
window.Convolve(tempRowSpan, sourceX);
}
}
});
@ -281,43 +272,37 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
var processRowsRect = Rectangle.FromLTRB(0, minY, width, maxY);
// Now process the rows.
ParallelHelper.IterateRows(
ParallelHelper.IterateRowsWithTempBuffer<Vector4>(
processRowsRect,
configuration,
rows =>
(rows, tempRowBuffer) =>
{
Span<Vector4> tempRowSpan = tempRowBuffer.Span;
for (int y = rows.Min; y < rows.Max; y++)
{
// Ensure offsets are normalized for cropping and padding.
ResizeKernel window = this.verticalKernelMap.Kernels[y - startY];
ref TPixel targetRow = ref MemoryMarshal.GetReference(destination.GetPixelRowSpan(y));
if (this.Compand)
{
for (int x = 0; x < width; x++)
{
Span<Vector4> firstPassColumn = firstPassPixelsTransposed.GetRowSpan(x);
ref Vector4 tempRowBase = ref MemoryMarshal.GetReference(tempRowSpan);
// Destination color components
Vector4 destinationVector = window.Convolve(firstPassColumn, sourceY);
destinationVector = destinationVector.UnPremultiply().Compress();
for (int x = 0; x < width; x++)
{
Span<Vector4> firstPassColumn = firstPassPixelsTransposed.GetRowSpan(x);
ref TPixel pixel = ref Unsafe.Add(ref targetRow, x);
pixel.PackFromVector4(destinationVector);
}
// Destination color components
Unsafe.Add(ref tempRowBase, x) = window.Convolve(firstPassColumn, sourceY);
}
else
Vector4Extensions.UnPremultiply(tempRowSpan);
if (this.Compand)
{
for (int x = 0; x < width; x++)
{
Span<Vector4> firstPassColumn = firstPassPixelsTransposed.GetRowSpan(x);
// Destination color components
Vector4 destinationVector = window.Convolve(firstPassColumn, sourceY).UnPremultiply();
ref TPixel pixel = ref Unsafe.Add(ref targetRow, x);
pixel.PackFromVector4(destinationVector);
}
Vector4Extensions.Compress(tempRowSpan);
}
Span<TPixel> targetRowSpan = destination.GetPixelRowSpan(y);
PixelOperations<TPixel>.Instance.PackFromVector4(tempRowSpan, targetRowSpan, tempRowSpan.Length);
}
});
}

2
tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs

@ -53,7 +53,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
FormattableString details = $"{name}-{ratio.ToString(System.Globalization.CultureInfo.InvariantCulture)}";
image.DebugSave(provider, details);
image.CompareToReferenceOutput(ImageComparer.TolerantPercentage(0.005f), provider, details);
image.CompareToReferenceOutput(ImageComparer.TolerantPercentage(0.02f), provider, details);
}
}

Loading…
Cancel
Save