Skip to content

Commit d6fb30e

Browse files
committed
Refactor single row APIs
1 parent a0ad4ce commit d6fb30e

15 files changed

+22
-80
lines changed

src/ImageSharp/Advanced/ParallelRowIterator.cs

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -18,64 +18,6 @@ namespace SixLabors.ImageSharp.Advanced
1818
/// </summary>
1919
public static class ParallelRowIterator
2020
{
21-
/// <summary>
22-
/// Iterate through the rows of a rectangle in optimized batches defined by <see cref="RowInterval"/>-s.
23-
/// </summary>
24-
/// <typeparam name="T">The type of row action to perform.</typeparam>
25-
/// <param name="rectangle">The <see cref="Rectangle"/>.</param>
26-
/// <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 <see cref="RowInterval"/>.</param>
28-
[MethodImpl(InliningOptions.ShortMethod)]
29-
public static void IterateRows<T>(Rectangle rectangle, Configuration configuration, in T body)
30-
where T : struct, IRowIntervalAction
31-
{
32-
var parallelSettings = ParallelExecutionSettings.FromConfiguration(configuration);
33-
IterateRows(rectangle, in parallelSettings, in body);
34-
}
35-
36-
/// <summary>
37-
/// Iterate through the rows of a rectangle in optimized batches defined by <see cref="RowInterval"/>-s.
38-
/// </summary>
39-
/// <typeparam name="T">The type of row action to perform.</typeparam>
40-
/// <param name="rectangle">The <see cref="Rectangle"/>.</param>
41-
/// <param name="parallelSettings">The <see cref="ParallelExecutionSettings"/>.</param>
42-
/// <param name="body">The method body defining the iteration logic on a single <see cref="RowInterval"/>.</param>
43-
public static void IterateRows<T>(
44-
Rectangle rectangle,
45-
in ParallelExecutionSettings parallelSettings,
46-
in T body)
47-
where T : struct, IRowIntervalAction
48-
{
49-
ValidateRectangle(rectangle);
50-
51-
int top = rectangle.Top;
52-
int bottom = rectangle.Bottom;
53-
int width = rectangle.Width;
54-
int height = rectangle.Height;
55-
56-
int maxSteps = DivideCeil(width * height, parallelSettings.MinimumPixelsProcessedPerTask);
57-
int numOfSteps = Math.Min(parallelSettings.MaxDegreeOfParallelism, maxSteps);
58-
59-
// Avoid TPL overhead in this trivial case:
60-
if (numOfSteps == 1)
61-
{
62-
var rows = new RowInterval(top, bottom);
63-
Unsafe.AsRef(body).Invoke(in rows);
64-
return;
65-
}
66-
67-
int verticalStep = DivideCeil(rectangle.Height, numOfSteps);
68-
var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps };
69-
var rowInfo = new WrappingRowIntervalInfo(top, bottom, verticalStep);
70-
var rowAction = new WrappingRowIntervalAction<T>(in rowInfo, in body);
71-
72-
Parallel.For(
73-
0,
74-
numOfSteps,
75-
parallelOptions,
76-
rowAction.Invoke);
77-
}
78-
7921
/// <summary>
8022
/// Iterate through the rows of a rectangle in optimized batches.
8123
/// </summary>
@@ -84,11 +26,11 @@ public static void IterateRows<T>(
8426
/// <param name="configuration">The <see cref="Configuration"/> to get the parallel settings from.</param>
8527
/// <param name="body">The method body defining the iteration logic on a single row.</param>
8628
[MethodImpl(InliningOptions.ShortMethod)]
87-
public static void IterateRows2<T>(Rectangle rectangle, Configuration configuration, in T body)
29+
public static void IterateRows<T>(Rectangle rectangle, Configuration configuration, in T body)
8830
where T : struct, IRowAction
8931
{
9032
var parallelSettings = ParallelExecutionSettings.FromConfiguration(configuration);
91-
IterateRows2(rectangle, in parallelSettings, in body);
33+
IterateRows(rectangle, in parallelSettings, in body);
9234
}
9335

9436
/// <summary>
@@ -98,7 +40,7 @@ public static void IterateRows2<T>(Rectangle rectangle, Configuration configurat
9840
/// <param name="rectangle">The <see cref="Rectangle"/>.</param>
9941
/// <param name="parallelSettings">The <see cref="ParallelExecutionSettings"/>.</param>
10042
/// <param name="body">The method body defining the iteration logic on a single row.</param>
101-
public static void IterateRows2<T>(
43+
public static void IterateRows<T>(
10244
Rectangle rectangle,
10345
in ParallelExecutionSettings parallelSettings,
10446
in T body)

src/ImageSharp/ImageFrame{TPixel}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ internal ImageFrame<TPixel2> CloneAs<TPixel2>(Configuration configuration)
260260

261261
var target = new ImageFrame<TPixel2>(configuration, this.Width, this.Height, this.Metadata.DeepClone());
262262

263-
ParallelRowIterator.IterateRows2(
263+
ParallelRowIterator.IterateRows(
264264
this.Bounds(),
265265
configuration,
266266
new RowAction<TPixel2>(this, target, configuration));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
5050

5151
var workingRect = Rectangle.FromLTRB(startX, startY, endX, endY);
5252

53-
ParallelRowIterator.IterateRows2(
53+
ParallelRowIterator.IterateRows(
5454
workingRect,
5555
configuration,
5656
new RowAction(source, upper, lower, threshold, startX, endX, isAlphaOnly));

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
282282
float inverseGamma = 1 / this.gamma;
283283

284284
// Apply the inverse gamma exposure pass, and write the final pixel data
285-
ParallelRowIterator.IterateRows2(
285+
ParallelRowIterator.IterateRows(
286286
this.SourceRectangle,
287287
this.Configuration,
288288
new ApplyInverseGammaExposureRowAction(this.SourceRectangle, source.PixelBuffer, processingBuffer, this.Configuration, inverseGamma));
@@ -314,13 +314,13 @@ private void OnFrameApplyCore(
314314
Vector4 parameters = Unsafe.Add(ref paramsRef, i);
315315

316316
// Compute the vertical 1D convolution
317-
ParallelRowIterator.IterateRows2(
317+
ParallelRowIterator.IterateRows(
318318
sourceRectangle,
319319
configuration,
320320
new ApplyVerticalConvolutionRowAction(ref sourceRectangle, firstPassBuffer, source.PixelBuffer, kernel));
321321

322322
// Compute the horizontal 1D convolutions and accumulate the partial results on the target buffer
323-
ParallelRowIterator.IterateRows2(
323+
ParallelRowIterator.IterateRows(
324324
sourceRectangle,
325325
configuration,
326326
new ApplyHorizontalConvolutionRowAction(ref sourceRectangle, processingBuffer, firstPassBuffer, kernel, parameters.Z, parameters.W));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
102102
processor.Apply(pass);
103103
}
104104

105-
ParallelRowIterator.IterateRows2(
105+
ParallelRowIterator.IterateRows(
106106
Rectangle.FromLTRB(minX, minY, maxX, maxY),
107107
this.Configuration,
108108
new RowAction(source.PixelBuffer, pass.PixelBuffer, minX, maxX, shiftY, shiftX));

src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor{TPixelBg,TPixelFg}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ protected override void OnFrameApply(ImageFrame<TPixelBg> source)
9898
"Cannot draw image because the source image does not overlap the target image.");
9999
}
100100

101-
ParallelRowIterator.IterateRows2(
101+
ParallelRowIterator.IterateRows(
102102
workingRect,
103103
configuration,
104104
new RowAction(source, targetImage, blender, configuration, minX, width, locationY, targetX, this.Opacity));

src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor{TPixel}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
4747

4848
source.CopyTo(targetPixels);
4949

50-
ParallelRowIterator.IterateRows2(
50+
ParallelRowIterator.IterateRows(
5151
this.SourceRectangle,
5252
this.Configuration,
5353
new RowAction(this.SourceRectangle, targetPixels, source, this.Configuration, brushSize >> 1, this.definition.Levels));

src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor{TPixel}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
5252
using IMemoryOwner<int> histogramBuffer = memoryAllocator.Allocate<int>(this.LuminanceLevels, AllocationOptions.Clean);
5353

5454
// Build the histogram of the grayscale levels
55-
ParallelRowIterator.IterateRows2(
55+
ParallelRowIterator.IterateRows(
5656
workingRect,
5757
this.Configuration,
5858
new GrayscaleLevelsRowAction(workingRect, histogramBuffer, source, this.LuminanceLevels));
@@ -74,7 +74,7 @@ ref MemoryMarshal.GetReference(histogram),
7474
float numberOfPixelsMinusCdfMin = numberOfPixels - cdfMin;
7575

7676
// Apply the cdf to each pixel of the image
77-
ParallelRowIterator.IterateRows2(
77+
ParallelRowIterator.IterateRows(
7878
workingRect,
7979
this.Configuration,
8080
new CdfApplicationRowAction(workingRect, cdfBuffer, source, this.LuminanceLevels, numberOfPixelsMinusCdfMin));

src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor{TPixel}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
4949

5050
PixelBlender<TPixel> blender = PixelOperations<TPixel>.Instance.GetPixelBlender(graphicsOptions);
5151

52-
ParallelRowIterator.IterateRows2(
52+
ParallelRowIterator.IterateRows(
5353
interest,
5454
configuration,
5555
new RowAction(configuration, interest, blender, amount, colors, source));

src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor{TPixel}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected override void OnFrameApply(ImageFrame<TPixel> source, ImageFrame<TPixe
5858

5959
if (this.resampler is NearestNeighborResampler)
6060
{
61-
ParallelRowIterator.IterateRows2(
61+
ParallelRowIterator.IterateRows(
6262
targetBounds,
6363
configuration,
6464
new NearestNeighborRowAction(this.SourceRectangle, ref matrix, width, source, destination));

0 commit comments

Comments
 (0)