Skip to content

Commit 3da200d

Browse files
committed
Refactor processors to single row with buffer APIs
1 parent dd4285d commit 3da200d

10 files changed

+251
-288
lines changed

src/ImageSharp/Processing/Processors/Convolution/BokehBlurProcessor{TPixel}.cs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,10 @@ private void NormalizeKernels()
268268
protected override void OnFrameApply(ImageFrame<TPixel> source)
269269
{
270270
// Preliminary gamma highlight pass
271-
ParallelRowIterator.IterateRows<ApplyGammaExposureRowIntervalAction, Vector4>(
271+
ParallelRowIterator.IterateRows2<ApplyGammaExposureRowAction, Vector4>(
272272
this.SourceRectangle,
273273
this.Configuration,
274-
new ApplyGammaExposureRowIntervalAction(this.SourceRectangle, source.PixelBuffer, this.Configuration, this.gamma));
274+
new ApplyGammaExposureRowAction(this.SourceRectangle, source.PixelBuffer, this.Configuration, this.gamma));
275275

276276
// Create a 0-filled buffer to use to store the result of the component convolutions
277277
using Buffer2D<Vector4> processingBuffer = this.Configuration.MemoryAllocator.Allocate2D<Vector4>(source.Size(), AllocationOptions.Clean);
@@ -416,15 +416,15 @@ public void Invoke(int y)
416416
/// <summary>
417417
/// A <see langword="struct"/> implementing the gamma exposure logic for <see cref="BokehBlurProcessor{T}"/>.
418418
/// </summary>
419-
private readonly struct ApplyGammaExposureRowIntervalAction : IRowIntervalAction<Vector4>
419+
private readonly struct ApplyGammaExposureRowAction : IRowAction<Vector4>
420420
{
421421
private readonly Rectangle bounds;
422422
private readonly Buffer2D<TPixel> targetPixels;
423423
private readonly Configuration configuration;
424424
private readonly float gamma;
425425

426426
[MethodImpl(InliningOptions.ShortMethod)]
427-
public ApplyGammaExposureRowIntervalAction(
427+
public ApplyGammaExposureRowAction(
428428
Rectangle bounds,
429429
Buffer2D<TPixel> targetPixels,
430430
Configuration configuration,
@@ -438,27 +438,23 @@ public ApplyGammaExposureRowIntervalAction(
438438

439439
/// <inheritdoc/>
440440
[MethodImpl(InliningOptions.ShortMethod)]
441-
public void Invoke(in RowInterval rows, Memory<Vector4> memory)
441+
public void Invoke(int y, Span<Vector4> span)
442442
{
443-
Span<Vector4> vectorSpan = memory.Span;
444-
int length = vectorSpan.Length;
443+
int length = span.Length;
445444

446-
for (int y = rows.Min; y < rows.Max; y++)
447-
{
448-
Span<TPixel> targetRowSpan = this.targetPixels.GetRowSpan(y).Slice(this.bounds.X);
449-
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, targetRowSpan.Slice(0, length), vectorSpan, PixelConversionModifiers.Premultiply);
450-
ref Vector4 baseRef = ref MemoryMarshal.GetReference(vectorSpan);
451-
452-
for (int x = 0; x < this.bounds.Width; x++)
453-
{
454-
ref Vector4 v = ref Unsafe.Add(ref baseRef, x);
455-
v.X = MathF.Pow(v.X, this.gamma);
456-
v.Y = MathF.Pow(v.Y, this.gamma);
457-
v.Z = MathF.Pow(v.Z, this.gamma);
458-
}
445+
Span<TPixel> targetRowSpan = this.targetPixels.GetRowSpan(y).Slice(this.bounds.X);
446+
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, targetRowSpan.Slice(0, length), span, PixelConversionModifiers.Premultiply);
447+
ref Vector4 baseRef = ref MemoryMarshal.GetReference(span);
459448

460-
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorSpan.Slice(0, length), targetRowSpan);
449+
for (int x = 0; x < this.bounds.Width; x++)
450+
{
451+
ref Vector4 v = ref Unsafe.Add(ref baseRef, x);
452+
v.X = MathF.Pow(v.X, this.gamma);
453+
v.Y = MathF.Pow(v.Y, this.gamma);
454+
v.Z = MathF.Pow(v.Z, this.gamma);
461455
}
456+
457+
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, span.Slice(0, length), targetRowSpan);
462458
}
463459
}
464460

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

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,18 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
6666

6767
var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds());
6868

69-
ParallelRowIterator.IterateRows<RowIntervalAction, Vector4>(
69+
ParallelRowIterator.IterateRows2<RowAction, Vector4>(
7070
interest,
7171
this.Configuration,
72-
new RowIntervalAction(interest, targetPixels, source.PixelBuffer, this.KernelY, this.KernelX, this.Configuration, this.PreserveAlpha));
72+
new RowAction(interest, targetPixels, source.PixelBuffer, this.KernelY, this.KernelX, this.Configuration, this.PreserveAlpha));
7373

7474
Buffer2D<TPixel>.SwapOrCopyContent(source.PixelBuffer, targetPixels);
7575
}
7676

7777
/// <summary>
7878
/// A <see langword="struct"/> implementing the convolution logic for <see cref="Convolution2DProcessor{T}"/>.
7979
/// </summary>
80-
private readonly struct RowIntervalAction : IRowIntervalAction<Vector4>
80+
private readonly struct RowAction : IRowAction<Vector4>
8181
{
8282
private readonly Rectangle bounds;
8383
private readonly int maxY;
@@ -90,7 +90,7 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
9090
private readonly bool preserveAlpha;
9191

9292
[MethodImpl(InliningOptions.ShortMethod)]
93-
public RowIntervalAction(
93+
public RowAction(
9494
Rectangle bounds,
9595
Buffer2D<TPixel> targetPixels,
9696
Buffer2D<TPixel> sourcePixels,
@@ -112,54 +112,50 @@ public RowIntervalAction(
112112

113113
/// <inheritdoc/>
114114
[MethodImpl(InliningOptions.ShortMethod)]
115-
public void Invoke(in RowInterval rows, Memory<Vector4> memory)
115+
public void Invoke(int y, Span<Vector4> span)
116116
{
117-
Span<Vector4> vectorSpan = memory.Span;
118-
int length = vectorSpan.Length;
119-
ref Vector4 vectorSpanRef = ref MemoryMarshal.GetReference(vectorSpan);
117+
int length = span.Length;
118+
ref Vector4 vectorSpanRef = ref MemoryMarshal.GetReference(span);
120119

121-
for (int y = rows.Min; y < rows.Max; y++)
122-
{
123-
Span<TPixel> targetRowSpan = this.targetPixels.GetRowSpan(y).Slice(this.bounds.X);
124-
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, targetRowSpan.Slice(0, length), vectorSpan);
120+
Span<TPixel> targetRowSpan = this.targetPixels.GetRowSpan(y).Slice(this.bounds.X);
121+
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, targetRowSpan.Slice(0, length), span);
125122

126-
if (this.preserveAlpha)
123+
if (this.preserveAlpha)
124+
{
125+
for (int x = 0; x < this.bounds.Width; x++)
127126
{
128-
for (int x = 0; x < this.bounds.Width; x++)
129-
{
130-
DenseMatrixUtils.Convolve2D3(
131-
in this.kernelY,
132-
in this.kernelX,
133-
this.sourcePixels,
134-
ref vectorSpanRef,
135-
y,
136-
x,
137-
this.bounds.Y,
138-
this.maxY,
139-
this.bounds.X,
140-
this.maxX);
141-
}
127+
DenseMatrixUtils.Convolve2D3(
128+
in this.kernelY,
129+
in this.kernelX,
130+
this.sourcePixels,
131+
ref vectorSpanRef,
132+
y,
133+
x,
134+
this.bounds.Y,
135+
this.maxY,
136+
this.bounds.X,
137+
this.maxX);
142138
}
143-
else
139+
}
140+
else
141+
{
142+
for (int x = 0; x < this.bounds.Width; x++)
144143
{
145-
for (int x = 0; x < this.bounds.Width; x++)
146-
{
147-
DenseMatrixUtils.Convolve2D4(
148-
in this.kernelY,
149-
in this.kernelX,
150-
this.sourcePixels,
151-
ref vectorSpanRef,
152-
y,
153-
x,
154-
this.bounds.Y,
155-
this.maxY,
156-
this.bounds.X,
157-
this.maxX);
158-
}
144+
DenseMatrixUtils.Convolve2D4(
145+
in this.kernelY,
146+
in this.kernelX,
147+
this.sourcePixels,
148+
ref vectorSpanRef,
149+
y,
150+
x,
151+
this.bounds.Y,
152+
this.maxY,
153+
this.bounds.X,
154+
this.maxX);
159155
}
160-
161-
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorSpan, targetRowSpan);
162156
}
157+
158+
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, span, targetRowSpan);
163159
}
164160
}
165161
}

src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor{TPixel}.cs

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,22 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
6464
var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds());
6565

6666
// Horizontal convolution
67-
ParallelRowIterator.IterateRows<RowIntervalAction, Vector4>(
67+
ParallelRowIterator.IterateRows2<RowAction, Vector4>(
6868
interest,
6969
this.Configuration,
70-
new RowIntervalAction(interest, firstPassPixels, source.PixelBuffer, this.KernelX, this.Configuration, this.PreserveAlpha));
70+
new RowAction(interest, firstPassPixels, source.PixelBuffer, this.KernelX, this.Configuration, this.PreserveAlpha));
7171

7272
// Vertical convolution
73-
ParallelRowIterator.IterateRows<RowIntervalAction, Vector4>(
73+
ParallelRowIterator.IterateRows2<RowAction, Vector4>(
7474
interest,
7575
this.Configuration,
76-
new RowIntervalAction(interest, source.PixelBuffer, firstPassPixels, this.KernelY, this.Configuration, this.PreserveAlpha));
76+
new RowAction(interest, source.PixelBuffer, firstPassPixels, this.KernelY, this.Configuration, this.PreserveAlpha));
7777
}
7878

7979
/// <summary>
8080
/// A <see langword="struct"/> implementing the convolution logic for <see cref="Convolution2PassProcessor{T}"/>.
8181
/// </summary>
82-
private readonly struct RowIntervalAction : IRowIntervalAction<Vector4>
82+
private readonly struct RowAction : IRowAction<Vector4>
8383
{
8484
private readonly Rectangle bounds;
8585
private readonly Buffer2D<TPixel> targetPixels;
@@ -89,7 +89,7 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
8989
private readonly bool preserveAlpha;
9090

9191
[MethodImpl(InliningOptions.ShortMethod)]
92-
public RowIntervalAction(
92+
public RowAction(
9393
Rectangle bounds,
9494
Buffer2D<TPixel> targetPixels,
9595
Buffer2D<TPixel> sourcePixels,
@@ -107,55 +107,51 @@ public RowIntervalAction(
107107

108108
/// <inheritdoc/>
109109
[MethodImpl(InliningOptions.ShortMethod)]
110-
public void Invoke(in RowInterval rows, Memory<Vector4> memory)
110+
public void Invoke(int y, Span<Vector4> span)
111111
{
112-
Span<Vector4> vectorSpan = memory.Span;
113-
int length = vectorSpan.Length;
114-
ref Vector4 vectorSpanRef = ref MemoryMarshal.GetReference(vectorSpan);
112+
int length = span.Length;
113+
ref Vector4 vectorSpanRef = ref MemoryMarshal.GetReference(span);
115114

116115
int maxY = this.bounds.Bottom - 1;
117116
int maxX = this.bounds.Right - 1;
118117

119-
for (int y = rows.Min; y < rows.Max; y++)
120-
{
121-
Span<TPixel> targetRowSpan = this.targetPixels.GetRowSpan(y).Slice(this.bounds.X);
122-
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, targetRowSpan.Slice(0, length), vectorSpan);
118+
Span<TPixel> targetRowSpan = this.targetPixels.GetRowSpan(y).Slice(this.bounds.X);
119+
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, targetRowSpan.Slice(0, length), span);
123120

124-
if (this.preserveAlpha)
121+
if (this.preserveAlpha)
122+
{
123+
for (int x = 0; x < this.bounds.Width; x++)
125124
{
126-
for (int x = 0; x < this.bounds.Width; x++)
127-
{
128-
DenseMatrixUtils.Convolve3(
129-
in this.kernel,
130-
this.sourcePixels,
131-
ref vectorSpanRef,
132-
y,
133-
x,
134-
this.bounds.Y,
135-
maxY,
136-
this.bounds.X,
137-
maxX);
138-
}
125+
DenseMatrixUtils.Convolve3(
126+
in this.kernel,
127+
this.sourcePixels,
128+
ref vectorSpanRef,
129+
y,
130+
x,
131+
this.bounds.Y,
132+
maxY,
133+
this.bounds.X,
134+
maxX);
139135
}
140-
else
136+
}
137+
else
138+
{
139+
for (int x = 0; x < this.bounds.Width; x++)
141140
{
142-
for (int x = 0; x < this.bounds.Width; x++)
143-
{
144-
DenseMatrixUtils.Convolve4(
145-
in this.kernel,
146-
this.sourcePixels,
147-
ref vectorSpanRef,
148-
y,
149-
x,
150-
this.bounds.Y,
151-
maxY,
152-
this.bounds.X,
153-
maxX);
154-
}
141+
DenseMatrixUtils.Convolve4(
142+
in this.kernel,
143+
this.sourcePixels,
144+
ref vectorSpanRef,
145+
y,
146+
x,
147+
this.bounds.Y,
148+
maxY,
149+
this.bounds.X,
150+
maxX);
155151
}
156-
157-
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorSpan, targetRowSpan);
158152
}
153+
154+
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, span, targetRowSpan);
159155
}
160156
}
161157
}

0 commit comments

Comments
 (0)