Skip to content

Commit 8540f83

Browse files
Revert "Merge pull request #1108 from SixLabors/sp/single-row-parallel-value-delegate"
This reverts commit 1835475, reversing changes made to dd7bd7b.
1 parent fcf7c44 commit 8540f83

24 files changed

+606
-664
lines changed

src/ImageSharp/Advanced/IRowAction.cs

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/ImageSharp/Advanced/IRowAction{TBuffer}.cs

Lines changed: 0 additions & 88 deletions
This file was deleted.

src/ImageSharp/Advanced/ParallelRowIterator.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,32 @@ namespace SixLabors.ImageSharp.Advanced
1919
public static class ParallelRowIterator
2020
{
2121
/// <summary>
22-
/// Iterate through the rows of a rectangle in optimized batches.
22+
/// Iterate through the rows of a rectangle in optimized batches defined by <see cref="RowInterval"/>-s.
2323
/// </summary>
2424
/// <typeparam name="T">The type of row action to perform.</typeparam>
2525
/// <param name="rectangle">The <see cref="Rectangle"/>.</param>
2626
/// <param name="configuration">The <see cref="Configuration"/> to get the parallel settings from.</param>
27-
/// <param name="body">The method body defining the iteration logic on a single row.</param>
27+
/// <param name="body">The method body defining the iteration logic on a single <see cref="RowInterval"/>.</param>
2828
[MethodImpl(InliningOptions.ShortMethod)]
2929
public static void IterateRows<T>(Rectangle rectangle, Configuration configuration, in T body)
30-
where T : struct, IRowAction
30+
where T : struct, IRowIntervalAction
3131
{
3232
var parallelSettings = ParallelExecutionSettings.FromConfiguration(configuration);
3333
IterateRows(rectangle, in parallelSettings, in body);
3434
}
3535

3636
/// <summary>
37-
/// Iterate through the rows of a rectangle in optimized batches.
37+
/// Iterate through the rows of a rectangle in optimized batches defined by <see cref="RowInterval"/>-s.
3838
/// </summary>
3939
/// <typeparam name="T">The type of row action to perform.</typeparam>
4040
/// <param name="rectangle">The <see cref="Rectangle"/>.</param>
4141
/// <param name="parallelSettings">The <see cref="ParallelExecutionSettings"/>.</param>
42-
/// <param name="body">The method body defining the iteration logic on a single row.</param>
42+
/// <param name="body">The method body defining the iteration logic on a single <see cref="RowInterval"/>.</param>
4343
public static void IterateRows<T>(
4444
Rectangle rectangle,
4545
in ParallelExecutionSettings parallelSettings,
4646
in T body)
47-
where T : struct, IRowAction
47+
where T : struct, IRowIntervalAction
4848
{
4949
ValidateRectangle(rectangle);
5050

@@ -59,17 +59,15 @@ public static void IterateRows<T>(
5959
// Avoid TPL overhead in this trivial case:
6060
if (numOfSteps == 1)
6161
{
62-
for (int y = top; y < bottom; y++)
63-
{
64-
Unsafe.AsRef(body).Invoke(y);
65-
}
66-
62+
var rows = new RowInterval(top, bottom);
63+
Unsafe.AsRef(body).Invoke(in rows);
6764
return;
6865
}
6966

7067
int verticalStep = DivideCeil(rectangle.Height, numOfSteps);
7168
var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps };
72-
var rowAction = new WrappingRowAction<T>(top, bottom, verticalStep, in body);
69+
var rowInfo = new WrappingRowIntervalInfo(top, bottom, verticalStep);
70+
var rowAction = new WrappingRowIntervalAction<T>(in rowInfo, in body);
7371

7472
Parallel.For(
7573
0,
@@ -88,7 +86,7 @@ public static void IterateRows<T>(
8886
/// <param name="configuration">The <see cref="Configuration"/> to get the parallel settings from.</param>
8987
/// <param name="body">The method body defining the iteration logic on a single <see cref="RowInterval"/>.</param>
9088
public static void IterateRows<T, TBuffer>(Rectangle rectangle, Configuration configuration, in T body)
91-
where T : struct, IRowAction<TBuffer>
89+
where T : struct, IRowIntervalAction<TBuffer>
9290
where TBuffer : unmanaged
9391
{
9492
var parallelSettings = ParallelExecutionSettings.FromConfiguration(configuration);
@@ -103,7 +101,7 @@ internal static void IterateRows<T, TBuffer>(
103101
Rectangle rectangle,
104102
in ParallelExecutionSettings parallelSettings,
105103
in T body)
106-
where T : struct, IRowAction<TBuffer>
104+
where T : struct, IRowIntervalAction<TBuffer>
107105
where TBuffer : unmanaged
108106
{
109107
ValidateRectangle(rectangle);
@@ -120,22 +118,19 @@ internal static void IterateRows<T, TBuffer>(
120118
// Avoid TPL overhead in this trivial case:
121119
if (numOfSteps == 1)
122120
{
121+
var rows = new RowInterval(top, bottom);
123122
using (IMemoryOwner<TBuffer> buffer = allocator.Allocate<TBuffer>(width))
124123
{
125-
Span<TBuffer> span = buffer.Memory.Span;
126-
127-
for (int y = top; y < bottom; y++)
128-
{
129-
Unsafe.AsRef(body).Invoke(y, span);
130-
}
124+
Unsafe.AsRef(body).Invoke(rows, buffer.Memory);
131125
}
132126

133127
return;
134128
}
135129

136130
int verticalStep = DivideCeil(height, numOfSteps);
137131
var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps };
138-
var rowAction = new WrappingRowAction<T, TBuffer>(top, bottom, verticalStep, width, allocator, in body);
132+
var rowInfo = new WrappingRowIntervalInfo(top, bottom, verticalStep, width);
133+
var rowAction = new WrappingRowIntervalBufferAction<T, TBuffer>(in rowInfo, allocator, in body);
139134

140135
Parallel.For(
141136
0,

src/ImageSharp/ImageFrame{TPixel}.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ internal ImageFrame<TPixel2> CloneAs<TPixel2>(Configuration configuration)
263263
ParallelRowIterator.IterateRows(
264264
this.Bounds(),
265265
configuration,
266-
new RowAction<TPixel2>(this, target, configuration));
266+
new RowIntervalAction<TPixel2>(this, target, configuration));
267267

268268
return target;
269269
}
@@ -289,15 +289,15 @@ internal void Clear(TPixel value)
289289
/// <summary>
290290
/// A <see langword="struct"/> implementing the clone logic for <see cref="ImageFrame{TPixel}"/>.
291291
/// </summary>
292-
private readonly struct RowAction<TPixel2> : IRowAction
292+
private readonly struct RowIntervalAction<TPixel2> : IRowIntervalAction
293293
where TPixel2 : struct, IPixel<TPixel2>
294294
{
295295
private readonly ImageFrame<TPixel> source;
296296
private readonly ImageFrame<TPixel2> target;
297297
private readonly Configuration configuration;
298298

299299
[MethodImpl(InliningOptions.ShortMethod)]
300-
public RowAction(
300+
public RowIntervalAction(
301301
ImageFrame<TPixel> source,
302302
ImageFrame<TPixel2> target,
303303
Configuration configuration)
@@ -309,11 +309,14 @@ public RowAction(
309309

310310
/// <inheritdoc/>
311311
[MethodImpl(InliningOptions.ShortMethod)]
312-
public void Invoke(int y)
312+
public void Invoke(in RowInterval rows)
313313
{
314-
Span<TPixel> sourceRow = this.source.GetPixelRowSpan(y);
315-
Span<TPixel2> targetRow = this.target.GetPixelRowSpan(y);
316-
PixelOperations<TPixel>.Instance.To(this.configuration, sourceRow, targetRow);
314+
for (int y = rows.Min; y < rows.Max; y++)
315+
{
316+
Span<TPixel> sourceRow = this.source.GetPixelRowSpan(y);
317+
Span<TPixel2> targetRow = this.target.GetPixelRowSpan(y);
318+
PixelOperations<TPixel>.Instance.To(this.configuration, sourceRow, targetRow);
319+
}
317320
}
318321
}
319322
}

src/ImageSharp/Processing/Processors/Binarization/BinaryThresholdProcessor{TPixel}.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Runtime.CompilerServices;
66
using SixLabors.ImageSharp.Advanced;
7+
using SixLabors.ImageSharp.Memory;
78
using SixLabors.ImageSharp.PixelFormats;
89

910
namespace SixLabors.ImageSharp.Processing.Processors.Binarization
@@ -53,13 +54,13 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
5354
ParallelRowIterator.IterateRows(
5455
workingRect,
5556
configuration,
56-
new RowAction(source, upper, lower, threshold, startX, endX, isAlphaOnly));
57+
new RowIntervalAction(source, upper, lower, threshold, startX, endX, isAlphaOnly));
5758
}
5859

5960
/// <summary>
6061
/// A <see langword="struct"/> implementing the clone logic for <see cref="BinaryThresholdProcessor{TPixel}"/>.
6162
/// </summary>
62-
private readonly struct RowAction : IRowAction
63+
private readonly struct RowIntervalAction : IRowIntervalAction
6364
{
6465
private readonly ImageFrame<TPixel> source;
6566
private readonly TPixel upper;
@@ -70,7 +71,7 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
7071
private readonly bool isAlphaOnly;
7172

7273
[MethodImpl(InliningOptions.ShortMethod)]
73-
public RowAction(
74+
public RowIntervalAction(
7475
ImageFrame<TPixel> source,
7576
TPixel upper,
7677
TPixel lower,
@@ -90,20 +91,22 @@ public RowAction(
9091

9192
/// <inheritdoc/>
9293
[MethodImpl(InliningOptions.ShortMethod)]
93-
public void Invoke(int y)
94+
public void Invoke(in RowInterval rows)
9495
{
9596
Rgba32 rgba = default;
96-
97-
Span<TPixel> row = this.source.GetPixelRowSpan(y);
98-
99-
for (int x = this.startX; x < this.endX; x++)
97+
for (int y = rows.Min; y < rows.Max; y++)
10098
{
101-
ref TPixel color = ref row[x];
102-
color.ToRgba32(ref rgba);
99+
Span<TPixel> row = this.source.GetPixelRowSpan(y);
100+
101+
for (int x = this.startX; x < this.endX; x++)
102+
{
103+
ref TPixel color = ref row[x];
104+
color.ToRgba32(ref rgba);
103105

104-
// Convert to grayscale using ITU-R Recommendation BT.709 if required
105-
byte luminance = this.isAlphaOnly ? rgba.A : ImageMaths.Get8BitBT709Luminance(rgba.R, rgba.G, rgba.B);
106-
color = luminance >= this.threshold ? this.upper : this.lower;
106+
// Convert to grayscale using ITU-R Recommendation BT.709 if required
107+
byte luminance = this.isAlphaOnly ? rgba.A : ImageMaths.Get8BitBT709Luminance(rgba.R, rgba.G, rgba.B);
108+
color = luminance >= this.threshold ? this.upper : this.lower;
109+
}
107110
}
108111
}
109112
}

0 commit comments

Comments
 (0)