Browse Source

Cleanup

js/color-alpha-handling
James Jackson-South 5 years ago
parent
commit
a4ff07edd2
  1. 78
      src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor{TPixel}.cs
  2. 36
      src/ImageSharp/Processing/Processors/Convolution/Convolution2DRowOperation{TPixel}.cs
  3. 38
      src/ImageSharp/Processing/Processors/Convolution/ConvolutionRowOperation{TPixel}.cs
  4. 309
      src/ImageSharp/Processing/Processors/Convolution/Convolver.cs

78
src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor{TPixel}.cs

@ -1,10 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
@ -94,80 +91,5 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
Buffer2D<TPixel>.SwapOrCopyContent(source.PixelBuffer, targetPixels);
}
/// <summary>
/// A <see langword="struct"/> implementing the convolution logic for <see cref="Convolution2DProcessor{T}"/>.
/// </summary>
private readonly struct RowOperation : IRowOperation<Vector4>
{
private readonly Rectangle bounds;
private readonly Buffer2D<TPixel> targetPixels;
private readonly Buffer2D<TPixel> sourcePixels;
private readonly KernelSamplingMap map;
private readonly DenseMatrix<float> kernelY;
private readonly DenseMatrix<float> kernelX;
private readonly Configuration configuration;
private readonly bool preserveAlpha;
[MethodImpl(InliningOptions.ShortMethod)]
public RowOperation(
Rectangle bounds,
Buffer2D<TPixel> targetPixels,
Buffer2D<TPixel> sourcePixels,
KernelSamplingMap map,
DenseMatrix<float> kernelY,
DenseMatrix<float> kernelX,
Configuration configuration,
bool preserveAlpha)
{
this.bounds = bounds;
this.targetPixels = targetPixels;
this.sourcePixels = sourcePixels;
this.map = map;
this.kernelY = kernelY;
this.kernelX = kernelX;
this.configuration = configuration;
this.preserveAlpha = preserveAlpha;
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
public void Invoke(int y, Span<Vector4> span)
{
ref Vector4 targetRowRef = ref MemoryMarshal.GetReference(span);
Span<TPixel> targetRowSpan = this.targetPixels.GetRowSpan(y).Slice(this.bounds.X);
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, targetRowSpan.Slice(0, span.Length), span);
var state = new Convolution2DState(this.kernelY, this.kernelX, this.map);
int row = y - this.bounds.Y;
if (this.preserveAlpha)
{
for (int column = 0; column < this.bounds.Width; column++)
{
Convolver.Convolve2D3(
in state,
this.sourcePixels,
ref targetRowRef,
row,
column);
}
}
else
{
for (int column = 0; column < this.bounds.Width; column++)
{
Convolver.Convolve2D4(
in state,
this.sourcePixels,
ref targetRowRef,
row,
column);
}
}
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, span, targetRowSpan);
}
}
}
}

36
src/ImageSharp/Processing/Processors/Convolution/Convolution2DRowOperation{TPixel}.cs

@ -72,10 +72,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
Span<Vector4> targetXBuffer = span.Slice(boundsWidth * 2, boundsWidth);
var state = new Convolution2DState(in this.kernelMatrixY, in this.kernelMatrixX, this.map);
ReadOnlyKernel kernelY = state.KernelY;
ReadOnlyKernel kernelX = state.KernelX;
int row = y - this.bounds.Y;
ref int sampleRowBase = ref state.GetSampleRow(row);
ref int sampleRowBase = ref state.GetSampleRow(y - this.bounds.Y);
// Clear the target buffers for each row run.
targetYBuffer.Clear();
@ -83,12 +80,14 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
ref Vector4 targetBaseY = ref MemoryMarshal.GetReference(targetYBuffer);
ref Vector4 targetBaseX = ref MemoryMarshal.GetReference(targetXBuffer);
ReadOnlyKernel kernelY = state.KernelY;
ReadOnlyKernel kernelX = state.KernelX;
Span<TPixel> sourceRow;
for (int kY = 0; kY < kernelY.Rows; kY++)
{
// Get the precalculated source sample row for this kernel row and copy to our buffer.
int offsetY = Unsafe.Add(ref sampleRowBase, kY);
sourceRow = this.sourcePixels.GetRowSpan(offsetY).Slice(boundsX, boundsWidth);
int sampleY = Unsafe.Add(ref sampleRowBase, kY);
sourceRow = this.sourcePixels.GetRowSpan(sampleY).Slice(boundsX, boundsWidth);
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, sourceRow, sourceBuffer);
ref Vector4 sourceBase = ref MemoryMarshal.GetReference(sourceBuffer);
@ -101,15 +100,16 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
for (int kX = 0; kX < kernelY.Columns; kX++)
{
int offsetX = Unsafe.Add(ref sampleColumnBase, kX) - boundsX;
Vector4 sample = Unsafe.Add(ref sourceBase, offsetX);
int sampleX = Unsafe.Add(ref sampleColumnBase, kX) - boundsX;
Vector4 sample = Unsafe.Add(ref sourceBase, sampleX);
targetY += kernelX[kY, kX] * sample;
targetX += kernelY[kY, kX] * sample;
}
}
}
// Now we need to combine the values and copy the original alpha values from the source row.
// Now we need to combine the values and copy the original alpha values
// from the source row.
sourceRow = this.sourcePixels.GetRowSpan(y).Slice(boundsX, boundsWidth);
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, sourceRow, sourceBuffer);
@ -130,7 +130,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void Convolve4(int y, Span<Vector4> span)
{
// Span is 2x bounds.
// Span is 3x bounds.
int boundsX = this.bounds.X;
int boundsWidth = this.bounds.Width;
Span<Vector4> sourceBuffer = span.Slice(0, boundsWidth);
@ -138,10 +138,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
Span<Vector4> targetXBuffer = span.Slice(boundsWidth * 2, boundsWidth);
var state = new Convolution2DState(in this.kernelMatrixY, in this.kernelMatrixX, this.map);
ReadOnlyKernel kernelY = state.KernelY;
ReadOnlyKernel kernelX = state.KernelX;
int row = y - this.bounds.Y;
ref int sampleRowBase = ref state.GetSampleRow(row);
ref int sampleRowBase = ref state.GetSampleRow(y - this.bounds.Y);
// Clear the target buffers for each row run.
targetYBuffer.Clear();
@ -149,11 +146,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
ref Vector4 targetBaseY = ref MemoryMarshal.GetReference(targetYBuffer);
ref Vector4 targetBaseX = ref MemoryMarshal.GetReference(targetXBuffer);
ReadOnlyKernel kernelY = state.KernelY;
ReadOnlyKernel kernelX = state.KernelX;
for (int kY = 0; kY < kernelY.Rows; kY++)
{
// Get the precalculated source sample row for this kernel row and copy to our buffer.
int offsetY = Unsafe.Add(ref sampleRowBase, kY);
Span<TPixel> sourceRow = this.sourcePixels.GetRowSpan(offsetY).Slice(boundsX, boundsWidth);
int sampleY = Unsafe.Add(ref sampleRowBase, kY);
Span<TPixel> sourceRow = this.sourcePixels.GetRowSpan(sampleY).Slice(boundsX, boundsWidth);
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, sourceRow, sourceBuffer);
Numerics.Premultiply(sourceBuffer);
@ -167,14 +166,15 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
for (int kX = 0; kX < kernelY.Columns; kX++)
{
int offsetX = Unsafe.Add(ref sampleColumnBase, kX) - boundsX;
Vector4 sample = Unsafe.Add(ref sourceBase, offsetX);
int sampleX = Unsafe.Add(ref sampleColumnBase, kX) - boundsX;
Vector4 sample = Unsafe.Add(ref sourceBase, sampleX);
targetY += kernelX[kY, kX] * sample;
targetX += kernelY[kY, kX] * sample;
}
}
}
// Now we need to combine the values
for (int x = 0; x < targetYBuffer.Length; x++)
{
ref Vector4 target = ref Unsafe.Add(ref targetBaseY, x);

38
src/ImageSharp/Processing/Processors/Convolution/ConvolutionRowOperation{TPixel}.cs

@ -67,24 +67,20 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
Span<Vector4> sourceBuffer = span.Slice(0, this.bounds.Width);
Span<Vector4> targetBuffer = span.Slice(this.bounds.Width);
ref Vector4 targetRowRef = ref MemoryMarshal.GetReference(span);
Span<TPixel> targetRowSpan = this.targetPixels.GetRowSpan(y).Slice(boundsX, boundsWidth);
var state = new ConvolutionState(in this.kernelMatrix, this.map);
ReadOnlyKernel kernel = state.Kernel;
int row = y - this.bounds.Y;
ref int sampleRowBase = ref state.GetSampleRow(row);
ref int sampleRowBase = ref state.GetSampleRow(y - this.bounds.Y);
// Clear the target buffer for each row run.
targetBuffer.Clear();
ref Vector4 targetBase = ref MemoryMarshal.GetReference(targetBuffer);
ReadOnlyKernel kernel = state.Kernel;
Span<TPixel> sourceRow;
for (int kY = 0; kY < kernel.Rows; kY++)
{
// Get the precalculated source sample row for this kernel row and copy to our buffer.
int offsetY = Unsafe.Add(ref sampleRowBase, kY);
sourceRow = this.sourcePixels.GetRowSpan(offsetY).Slice(boundsX, boundsWidth);
int sampleY = Unsafe.Add(ref sampleRowBase, kY);
sourceRow = this.sourcePixels.GetRowSpan(sampleY).Slice(boundsX, boundsWidth);
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, sourceRow, sourceBuffer);
ref Vector4 sourceBase = ref MemoryMarshal.GetReference(sourceBuffer);
@ -96,8 +92,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
for (int kX = 0; kX < kernel.Columns; kX++)
{
int offsetX = Unsafe.Add(ref sampleColumnBase, kX) - boundsX;
Vector4 sample = Unsafe.Add(ref sourceBase, offsetX);
int sampleX = Unsafe.Add(ref sampleColumnBase, kX) - boundsX;
Vector4 sample = Unsafe.Add(ref sourceBase, sampleX);
target += kernel[kY, kX] * sample;
}
}
@ -113,7 +109,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
target.W = Unsafe.Add(ref MemoryMarshal.GetReference(sourceBuffer), x).W;
}
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, targetBuffer, targetRowSpan);
Span<TPixel> targetRow = this.targetPixels.GetRowSpan(y).Slice(boundsX, boundsWidth);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, targetBuffer, targetRow);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -125,23 +122,19 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
Span<Vector4> sourceBuffer = span.Slice(0, this.bounds.Width);
Span<Vector4> targetBuffer = span.Slice(this.bounds.Width);
ref Vector4 targetRowRef = ref MemoryMarshal.GetReference(span);
Span<TPixel> targetRowSpan = this.targetPixels.GetRowSpan(y).Slice(boundsX, boundsWidth);
var state = new ConvolutionState(in this.kernelMatrix, this.map);
ReadOnlyKernel kernel = state.Kernel;
int row = y - this.bounds.Y;
ref int sampleRowBase = ref state.GetSampleRow(row);
ref int sampleRowBase = ref state.GetSampleRow(y - this.bounds.Y);
// Clear the target buffer for each row run.
targetBuffer.Clear();
ref Vector4 targetBase = ref MemoryMarshal.GetReference(targetBuffer);
ReadOnlyKernel kernel = state.Kernel;
for (int kY = 0; kY < kernel.Rows; kY++)
{
// Get the precalculated source sample row for this kernel row and copy to our buffer.
int offsetY = Unsafe.Add(ref sampleRowBase, kY);
Span<TPixel> sourceRow = this.sourcePixels.GetRowSpan(offsetY).Slice(boundsX, boundsWidth);
int sampleY = Unsafe.Add(ref sampleRowBase, kY);
Span<TPixel> sourceRow = this.sourcePixels.GetRowSpan(sampleY).Slice(boundsX, boundsWidth);
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, sourceRow, sourceBuffer);
Numerics.Premultiply(sourceBuffer);
@ -154,8 +147,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
for (int kX = 0; kX < kernel.Columns; kX++)
{
int offsetX = Unsafe.Add(ref sampleColumnBase, kX) - boundsX;
Vector4 sample = Unsafe.Add(ref sourceBase, offsetX);
int sampleX = Unsafe.Add(ref sampleColumnBase, kX) - boundsX;
Vector4 sample = Unsafe.Add(ref sourceBase, sampleX);
target += kernel[kY, kX] * sample;
}
}
@ -163,7 +156,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
Numerics.UnPremultiply(targetBuffer);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, targetBuffer, targetRowSpan);
Span<TPixel> targetRow = this.targetPixels.GetRowSpan(y).Slice(boundsX, boundsWidth);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, targetBuffer, targetRow);
}
}
}

309
src/ImageSharp/Processing/Processors/Convolution/Convolver.cs

@ -1,309 +0,0 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Processors.Convolution;
namespace SixLabors.ImageSharp
{
/// <summary>
/// Provides methods to perform convolution operations.
/// </summary>
internal static class Convolver
{
/// <summary>
/// Computes the sum of vectors in the span referenced by <paramref name="targetRowRef"/> weighted by the two kernel weight values.
/// Using this method the convolution filter is not applied to alpha in addition to the color channels.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="state">The 2D convolution kernels state.</param>
/// <param name="sourcePixels">The source frame.</param>
/// <param name="targetRowRef">The target row base reference.</param>
/// <param name="row">The current row.</param>
/// <param name="column">The current column.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Convolve2D3<TPixel>(
in Convolution2DState state,
Buffer2D<TPixel> sourcePixels,
ref Vector4 targetRowRef,
int row,
int column)
where TPixel : unmanaged, IPixel<TPixel>
{
Vector4 vector = default;
Convolve2DImpl(
in state,
sourcePixels,
row,
column,
ref vector);
ref Vector4 target = ref Unsafe.Add(ref targetRowRef, column);
vector.W = target.W;
Numerics.UnPremultiply(ref vector);
target = vector;
}
/// <summary>
/// Computes the sum of vectors in the span referenced by <paramref name="targetRowRef"/> weighted by the two kernel weight values.
/// Using this method the convolution filter is applied to alpha in addition to the color channels.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="state">The 2D convolution kernels state.</param>
/// <param name="sourcePixels">The source frame.</param>
/// <param name="targetRowRef">The target row base reference.</param>
/// <param name="row">The current row.</param>
/// <param name="column">The current column.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Convolve2D4<TPixel>(
in Convolution2DState state,
Buffer2D<TPixel> sourcePixels,
ref Vector4 targetRowRef,
int row,
int column)
where TPixel : unmanaged, IPixel<TPixel>
{
Vector4 vector = default;
Convolve2DImpl(
in state,
sourcePixels,
row,
column,
ref vector);
ref Vector4 target = ref Unsafe.Add(ref targetRowRef, column);
Numerics.UnPremultiply(ref vector);
target = vector;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Convolve2DImpl<TPixel>(
in Convolution2DState state,
Buffer2D<TPixel> sourcePixels,
int row,
int column,
ref Vector4 targetVector)
where TPixel : unmanaged, IPixel<TPixel>
{
ReadOnlyKernel kernelY = state.KernelY;
ReadOnlyKernel kernelX = state.KernelX;
int kernelHeight = kernelY.Rows;
int kernelWidth = kernelY.Columns;
Vector4 vectorY = default;
Vector4 vectorX = default;
ref int sampleOffsetRowBase = ref state.GetSampleRow(row);
for (int y = 0; y < kernelHeight; y++)
{
int offsetY = Unsafe.Add(ref sampleOffsetRowBase, y);
ref TPixel sourceRowBase = ref MemoryMarshal.GetReference(sourcePixels.GetRowSpan(offsetY));
ref int sampleOffsetColumnBase = ref state.GetSampleColumn(column);
for (int x = 0; x < kernelWidth; x++)
{
int offsetX = Unsafe.Add(ref sampleOffsetColumnBase, x);
var sample = Unsafe.Add(ref sourceRowBase, offsetX).ToVector4();
Numerics.Premultiply(ref sample);
vectorX += kernelX[y, x] * sample;
vectorY += kernelY[y, x] * sample;
}
}
targetVector = Vector4.SquareRoot((vectorX * vectorX) + (vectorY * vectorY));
}
/// <summary>
/// Computes the sum of vectors in the span referenced by <paramref name="targetRowRef"/> weighted by the kernel weight values.
/// Using this method the convolution filter is not applied to alpha in addition to the color channels.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="state">The convolution kernel state.</param>
/// <param name="sourcePixels">The source frame.</param>
/// <param name="targetRowRef">The target row base reference.</param>
/// <param name="row">The current row.</param>
/// <param name="column">The current column.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Convolve3<TPixel>(
in ConvolutionState state,
Buffer2D<TPixel> sourcePixels,
ref Vector4 targetRowRef,
int row,
int column)
where TPixel : unmanaged, IPixel<TPixel>
{
Vector4 vector = default;
ConvolveImpl(
state,
sourcePixels,
row,
column,
ref vector);
ref Vector4 target = ref Unsafe.Add(ref targetRowRef, column);
vector.W = target.W;
Numerics.UnPremultiply(ref vector);
target = vector;
}
/// <summary>
/// Computes the sum of vectors in the span referenced by <paramref name="targetRowRef"/> weighted by the kernel weight values.
/// Using this method the convolution filter is applied to alpha in addition to the color channels.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="state">The convolution kernel state.</param>
/// <param name="sourcePixels">The source frame.</param>
/// <param name="targetRowRef">The target row base reference.</param>
/// <param name="row">The current row.</param>
/// <param name="column">The current column.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Convolve4<TPixel>(
in ConvolutionState state,
Buffer2D<TPixel> sourcePixels,
ref Vector4 targetRowRef,
int row,
int column)
where TPixel : unmanaged, IPixel<TPixel>
{
Vector4 vector = default;
ConvolveImpl(
state,
sourcePixels,
row,
column,
ref vector);
ref Vector4 target = ref Unsafe.Add(ref targetRowRef, column);
Numerics.UnPremultiply(ref vector);
target = vector;
}
/// <summary>
/// Computes the sum of vectors in the span referenced by <paramref name="sourceRow"/> weighted
/// by the kernel weight values.
/// Using this method the convolution filter is not applied to alpha in addition
/// to the color channels.
/// </summary>
/// <param name="state">The convolution kernel state.</param>
/// <param name="sourceRow">The source row.</param>
/// <param name="targetRow">The target row.</param>
/// <param name="kY">The current kernel row.</param>
/// <param name="bX">The interest x-bounds relative to the interest image.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ConvolveRow3(
in ConvolutionState state,
Span<Vector4> sourceRow,
Span<Vector4> targetRow,
int kY,
int bX)
{
ReadOnlyKernel kernel = state.Kernel;
ref Vector4 sourceBase = ref MemoryMarshal.GetReference(sourceRow);
ref Vector4 targetBase = ref MemoryMarshal.GetReference(targetRow);
Numerics.Premultiply(sourceRow);
for (int x = 0; x < sourceRow.Length; x++)
{
Vector4 vector = default;
ref int sampleOffsetColumnBase = ref state.GetSampleColumn(x);
for (int kX = 0; kX < kernel.Columns; kX++)
{
int offsetX = Unsafe.Add(ref sampleOffsetColumnBase, kX) - bX;
Vector4 sample = Unsafe.Add(ref sourceBase, offsetX);
vector += kernel[kY, kX] * sample;
}
ref Vector4 target = ref Unsafe.Add(ref targetBase, x);
vector.W = target.W;
Numerics.UnPremultiply(ref vector);
target = vector;
}
}
/// <summary>
/// Computes the sum of vectors in the span referenced by <paramref name="sourceRow"/> weighted
/// by the kernel weight values.
/// Using this method the convolution filter is applied to alpha in addition to the
/// color channels.
/// </summary>
/// <param name="state">The convolution kernel state.</param>
/// <param name="sourceRow">The source row.</param>
/// <param name="targetRow">The target row.</param>
/// <param name="kY">The current kernel row.</param>
/// <param name="bX">The interest x-bounds relative to the interest image.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ConvolveRow4<TPixel>(
in ConvolutionState state,
Span<Vector4> sourceRow,
Span<Vector4> targetRow,
int kY,
int bX)
where TPixel : unmanaged, IPixel<TPixel>
{
ReadOnlyKernel kernel = state.Kernel;
ref Vector4 sourceBase = ref MemoryMarshal.GetReference(sourceRow);
ref Vector4 targetBase = ref MemoryMarshal.GetReference(targetRow);
Numerics.Premultiply(sourceRow);
for (int x = 0; x < sourceRow.Length; x++)
{
ref int sampleOffsetColumnBase = ref state.GetSampleColumn(x);
ref Vector4 target = ref Unsafe.Add(ref targetBase, x);
for (int kX = 0; kX < kernel.Columns; kX++)
{
int offsetX = Unsafe.Add(ref sampleOffsetColumnBase, kX) - bX;
Vector4 sample = Unsafe.Add(ref sourceBase, offsetX);
target += kernel[kY, kX] * sample;
}
}
Numerics.UnPremultiply(targetRow);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ConvolveImpl<TPixel>(
in ConvolutionState state,
Buffer2D<TPixel> sourcePixels,
int row,
int column,
ref Vector4 targetVector)
where TPixel : unmanaged, IPixel<TPixel>
{
ReadOnlyKernel kernel = state.Kernel;
int kernelHeight = kernel.Rows;
int kernelWidth = kernel.Columns;
ref int sampleOffsetRowBase = ref state.GetSampleRow(row);
for (int y = 0; y < kernelHeight; y++)
{
int offsetY = Unsafe.Add(ref sampleOffsetRowBase, y);
ref TPixel sourceRowBase = ref MemoryMarshal.GetReference(sourcePixels.GetRowSpan(offsetY));
ref int sampleOffsetColumnBase = ref state.GetSampleColumn(column);
for (int x = 0; x < kernelWidth; x++)
{
int offsetX = Unsafe.Add(ref sampleOffsetColumnBase, x);
var sample = Unsafe.Add(ref sourceRowBase, offsetX).ToVector4();
Numerics.Premultiply(ref sample);
targetVector += kernel[y, x] * sample;
}
}
}
}
}
Loading…
Cancel
Save