Browse Source

Restore temporary changes

js/color-alpha-handling
Sergio Pedri 5 years ago
parent
commit
68eeca9282
  1. 8
      src/ImageSharp/Processing/Processors/Convolution/Convolution2DRowOperation{TPixel}.cs
  2. 8
      src/ImageSharp/Processing/Processors/Convolution/Convolution2DState.cs
  3. 2
      src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor{TPixel}.cs
  4. 12
      src/ImageSharp/Processing/Processors/Convolution/ConvolutionRowOperation{TPixel}.cs
  5. 23
      src/ImageSharp/Processing/Processors/Convolution/ConvolutionState.cs
  6. 19
      src/ImageSharp/Processing/Processors/Convolution/ReadOnlyKernel.cs

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

@ -80,8 +80,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
ref Vector4 targetBaseY = ref MemoryMarshal.GetReference(targetYBuffer);
ref Vector4 targetBaseX = ref MemoryMarshal.GetReference(targetXBuffer);
ReadOnlyKernel<float> kernelY = state.KernelY;
ReadOnlyKernel<float> kernelX = state.KernelX;
ReadOnlyKernel kernelY = state.KernelY;
ReadOnlyKernel kernelX = state.KernelX;
Span<TPixel> sourceRow;
for (int kY = 0; kY < kernelY.Rows; kY++)
{
@ -146,8 +146,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
ref Vector4 targetBaseY = ref MemoryMarshal.GetReference(targetYBuffer);
ref Vector4 targetBaseX = ref MemoryMarshal.GetReference(targetXBuffer);
ReadOnlyKernel<float> kernelY = state.KernelY;
ReadOnlyKernel<float> kernelX = state.KernelX;
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.

8
src/ImageSharp/Processing/Processors/Convolution/Convolution2DState.cs

@ -23,21 +23,21 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
KernelSamplingMap map)
{
// We check the kernels are the same size upstream.
this.KernelY = new ReadOnlyKernel<float>(kernelY);
this.KernelX = new ReadOnlyKernel<float>(kernelX);
this.KernelY = new ReadOnlyKernel(kernelY);
this.KernelX = new ReadOnlyKernel(kernelX);
this.kernelHeight = kernelY.Rows;
this.kernelWidth = kernelY.Columns;
this.rowOffsetMap = map.GetRowOffsetSpan();
this.columnOffsetMap = map.GetColumnOffsetSpan();
}
public readonly ReadOnlyKernel<float> KernelY
public readonly ReadOnlyKernel KernelY
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;
}
public readonly ReadOnlyKernel<float> KernelX
public readonly ReadOnlyKernel KernelX
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;

2
src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor{TPixel}.cs

@ -120,7 +120,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
ref Vector4 targetRowRef = ref MemoryMarshal.GetReference(span);
Span<TPixel> targetRowSpan = this.targetPixels.GetRowSpan(y).Slice(boundsX, boundsWidth);
var state = new ConvolutionState<float>(in this.kernel, this.map);
var state = new ConvolutionState(in this.kernel, this.map);
int row = y - this.bounds.Y;
ref int sampleRowBase = ref state.GetSampleRow(row);

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

@ -67,14 +67,14 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
Span<Vector4> sourceBuffer = span.Slice(0, this.bounds.Width);
Span<Vector4> targetBuffer = span.Slice(this.bounds.Width);
var state = new ConvolutionState<float>(in this.kernelMatrix, this.map);
var state = new ConvolutionState(in this.kernelMatrix, this.map);
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<float> kernel = state.Kernel;
ReadOnlyKernel kernel = state.Kernel;
Span<TPixel> sourceRow;
for (int kY = 0; kY < kernel.Rows; kY++)
{
@ -119,17 +119,17 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
// Span is 2x bounds.
int boundsX = this.bounds.X;
int boundsWidth = this.bounds.Width;
Span<Vector4> sourceBuffer = span.Slice(0, boundsWidth);
Span<Vector4> targetBuffer = span.Slice(boundsWidth);
Span<Vector4> sourceBuffer = span.Slice(0, this.bounds.Width);
Span<Vector4> targetBuffer = span.Slice(this.bounds.Width);
var state = new ConvolutionState<float>(in this.kernelMatrix, this.map);
var state = new ConvolutionState(in this.kernelMatrix, this.map);
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<float> kernel = state.Kernel;
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.

23
src/ImageSharp/Processing/Processors/Convolution/ConvolutionState.cs

@ -10,9 +10,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
/// <summary>
/// A stack only struct used for reducing reference indirection during convolution operations.
/// </summary>
/// <typeparam name="T">The type of values for the kernel in use.</typeparam>
internal readonly ref struct ConvolutionState<T>
where T : unmanaged, IEquatable<T>
internal readonly ref struct ConvolutionState
{
private readonly Span<int> rowOffsetMap;
private readonly Span<int> columnOffsetMap;
@ -20,30 +18,17 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
private readonly int kernelWidth;
public ConvolutionState(
in DenseMatrix<T> kernel,
in DenseMatrix<float> kernel,
KernelSamplingMap map)
{
this.Kernel = new ReadOnlyKernel<T>(kernel);
this.Kernel = new ReadOnlyKernel(kernel);
this.kernelHeight = kernel.Rows;
this.kernelWidth = kernel.Columns;
this.rowOffsetMap = map.GetRowOffsetSpan();
this.columnOffsetMap = map.GetColumnOffsetSpan();
}
public ConvolutionState(
T[] kernel,
int height,
int width,
KernelSamplingMap map)
{
this.Kernel = new ReadOnlyKernel<T>(kernel, height, width);
this.kernelHeight = height;
this.kernelWidth = width;
this.rowOffsetMap = map.GetRowOffsetSpan();
this.columnOffsetMap = map.GetColumnOffsetSpan();
}
public readonly ReadOnlyKernel<T> Kernel
public readonly ReadOnlyKernel Kernel
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;

19
src/ImageSharp/Processing/Processors/Convolution/ReadOnlyKernel{T}.cs → src/ImageSharp/Processing/Processors/Convolution/ReadOnlyKernel.cs

@ -12,26 +12,17 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
/// A stack only, readonly, kernel matrix that can be indexed without
/// bounds checks when compiled in release mode.
/// </summary>
/// <typeparam name="T">The type of items in the kernel.</typeparam>
internal readonly ref struct ReadOnlyKernel<T>
where T : unmanaged, IEquatable<T>
internal readonly ref struct ReadOnlyKernel
{
private readonly ReadOnlySpan<T> values;
private readonly ReadOnlySpan<float> values;
public ReadOnlyKernel(DenseMatrix<T> matrix)
public ReadOnlyKernel(DenseMatrix<float> matrix)
{
this.Columns = matrix.Columns;
this.Rows = matrix.Rows;
this.values = matrix.Span;
}
public ReadOnlyKernel(T[] kernel, int height, int width)
{
this.Columns = width;
this.Rows = height;
this.values = kernel;
}
public int Columns
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -44,13 +35,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
get;
}
public T this[int row, int column]
public float this[int row, int column]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
this.CheckCoordinates(row, column);
ref T vBase = ref MemoryMarshal.GetReference(this.values);
ref float vBase = ref MemoryMarshal.GetReference(this.values);
return Unsafe.Add(ref vBase, (row * this.Columns) + column);
}
}
Loading…
Cancel
Save