Browse Source

Refactor single row APIs

af/octree-no-pixelmap
Sergio Pedri 6 years ago
parent
commit
78c245c5f4
  1. 64
      src/ImageSharp/Advanced/ParallelRowIterator.cs
  2. 2
      src/ImageSharp/ImageFrame{TPixel}.cs
  3. 2
      src/ImageSharp/Processing/Processors/Binarization/BinaryThresholdProcessor{TPixel}.cs
  4. 6
      src/ImageSharp/Processing/Processors/Convolution/BokehBlurProcessor{TPixel}.cs
  5. 2
      src/ImageSharp/Processing/Processors/Convolution/EdgeDetectorCompassProcessor{TPixel}.cs
  6. 2
      src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor{TPixelBg,TPixelFg}.cs
  7. 2
      src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor{TPixel}.cs
  8. 4
      src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor{TPixel}.cs
  9. 2
      src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor{TPixel}.cs
  10. 2
      src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor{TPixel}.cs
  11. 2
      src/ImageSharp/Processing/Processors/Transforms/CropProcessor{TPixel}.cs
  12. 2
      src/ImageSharp/Processing/Processors/Transforms/FlipProcessor{TPixel}.cs
  13. 2
      src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor{TPixel}.cs
  14. 2
      src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs
  15. 6
      src/ImageSharp/Processing/Processors/Transforms/RotateProcessor{TPixel}.cs

64
src/ImageSharp/Advanced/ParallelRowIterator.cs

@ -18,64 +18,6 @@ namespace SixLabors.ImageSharp.Advanced
/// </summary>
public static class ParallelRowIterator
{
/// <summary>
/// Iterate through the rows of a rectangle in optimized batches defined by <see cref="RowInterval"/>-s.
/// </summary>
/// <typeparam name="T">The type of row action to perform.</typeparam>
/// <param name="rectangle">The <see cref="Rectangle"/>.</param>
/// <param name="configuration">The <see cref="Configuration"/> to get the parallel settings from.</param>
/// <param name="body">The method body defining the iteration logic on a single <see cref="RowInterval"/>.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public static void IterateRows<T>(Rectangle rectangle, Configuration configuration, in T body)
where T : struct, IRowIntervalAction
{
var parallelSettings = ParallelExecutionSettings.FromConfiguration(configuration);
IterateRows(rectangle, in parallelSettings, in body);
}
/// <summary>
/// Iterate through the rows of a rectangle in optimized batches defined by <see cref="RowInterval"/>-s.
/// </summary>
/// <typeparam name="T">The type of row action to perform.</typeparam>
/// <param name="rectangle">The <see cref="Rectangle"/>.</param>
/// <param name="parallelSettings">The <see cref="ParallelExecutionSettings"/>.</param>
/// <param name="body">The method body defining the iteration logic on a single <see cref="RowInterval"/>.</param>
public static void IterateRows<T>(
Rectangle rectangle,
in ParallelExecutionSettings parallelSettings,
in T body)
where T : struct, IRowIntervalAction
{
ValidateRectangle(rectangle);
int top = rectangle.Top;
int bottom = rectangle.Bottom;
int width = rectangle.Width;
int height = rectangle.Height;
int maxSteps = DivideCeil(width * height, parallelSettings.MinimumPixelsProcessedPerTask);
int numOfSteps = Math.Min(parallelSettings.MaxDegreeOfParallelism, maxSteps);
// Avoid TPL overhead in this trivial case:
if (numOfSteps == 1)
{
var rows = new RowInterval(top, bottom);
Unsafe.AsRef(body).Invoke(in rows);
return;
}
int verticalStep = DivideCeil(rectangle.Height, numOfSteps);
var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps };
var rowInfo = new WrappingRowIntervalInfo(top, bottom, verticalStep);
var rowAction = new WrappingRowIntervalAction<T>(in rowInfo, in body);
Parallel.For(
0,
numOfSteps,
parallelOptions,
rowAction.Invoke);
}
/// <summary>
/// Iterate through the rows of a rectangle in optimized batches.
/// </summary>
@ -84,11 +26,11 @@ namespace SixLabors.ImageSharp.Advanced
/// <param name="configuration">The <see cref="Configuration"/> to get the parallel settings from.</param>
/// <param name="body">The method body defining the iteration logic on a single row.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public static void IterateRows2<T>(Rectangle rectangle, Configuration configuration, in T body)
public static void IterateRows<T>(Rectangle rectangle, Configuration configuration, in T body)
where T : struct, IRowAction
{
var parallelSettings = ParallelExecutionSettings.FromConfiguration(configuration);
IterateRows2(rectangle, in parallelSettings, in body);
IterateRows(rectangle, in parallelSettings, in body);
}
/// <summary>
@ -98,7 +40,7 @@ namespace SixLabors.ImageSharp.Advanced
/// <param name="rectangle">The <see cref="Rectangle"/>.</param>
/// <param name="parallelSettings">The <see cref="ParallelExecutionSettings"/>.</param>
/// <param name="body">The method body defining the iteration logic on a single row.</param>
public static void IterateRows2<T>(
public static void IterateRows<T>(
Rectangle rectangle,
in ParallelExecutionSettings parallelSettings,
in T body)

2
src/ImageSharp/ImageFrame{TPixel}.cs

@ -260,7 +260,7 @@ namespace SixLabors.ImageSharp
var target = new ImageFrame<TPixel2>(configuration, this.Width, this.Height, this.Metadata.DeepClone());
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
this.Bounds(),
configuration,
new RowAction<TPixel2>(this, target, configuration));

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

@ -50,7 +50,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Binarization
var workingRect = Rectangle.FromLTRB(startX, startY, endX, endY);
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
workingRect,
configuration,
new RowAction(source, upper, lower, threshold, startX, endX, isAlphaOnly));

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

@ -282,7 +282,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
float inverseGamma = 1 / this.gamma;
// Apply the inverse gamma exposure pass, and write the final pixel data
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
this.SourceRectangle,
this.Configuration,
new ApplyInverseGammaExposureRowAction(this.SourceRectangle, source.PixelBuffer, processingBuffer, this.Configuration, inverseGamma));
@ -314,13 +314,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
Vector4 parameters = Unsafe.Add(ref paramsRef, i);
// Compute the vertical 1D convolution
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
sourceRectangle,
configuration,
new ApplyVerticalConvolutionRowAction(ref sourceRectangle, firstPassBuffer, source.PixelBuffer, kernel));
// Compute the horizontal 1D convolutions and accumulate the partial results on the target buffer
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
sourceRectangle,
configuration,
new ApplyHorizontalConvolutionRowAction(ref sourceRectangle, processingBuffer, firstPassBuffer, kernel, parameters.Z, parameters.W));

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

@ -102,7 +102,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
processor.Apply(pass);
}
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
Rectangle.FromLTRB(minX, minY, maxX, maxY),
this.Configuration,
new RowAction(source.PixelBuffer, pass.PixelBuffer, minX, maxX, shiftY, shiftX));

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

@ -98,7 +98,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Drawing
"Cannot draw image because the source image does not overlap the target image.");
}
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
workingRect,
configuration,
new RowAction(source, targetImage, blender, configuration, minX, width, locationY, targetX, this.Opacity));

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

@ -47,7 +47,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects
source.CopyTo(targetPixels);
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
this.SourceRectangle,
this.Configuration,
new RowAction(this.SourceRectangle, targetPixels, source, this.Configuration, brushSize >> 1, this.definition.Levels));

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

@ -52,7 +52,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization
using IMemoryOwner<int> histogramBuffer = memoryAllocator.Allocate<int>(this.LuminanceLevels, AllocationOptions.Clean);
// Build the histogram of the grayscale levels
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
workingRect,
this.Configuration,
new GrayscaleLevelsRowAction(workingRect, histogramBuffer, source, this.LuminanceLevels));
@ -74,7 +74,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization
float numberOfPixelsMinusCdfMin = numberOfPixels - cdfMin;
// Apply the cdf to each pixel of the image
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
workingRect,
this.Configuration,
new CdfApplicationRowAction(workingRect, cdfBuffer, source, this.LuminanceLevels, numberOfPixelsMinusCdfMin));

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

@ -49,7 +49,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
PixelBlender<TPixel> blender = PixelOperations<TPixel>.Instance.GetPixelBlender(graphicsOptions);
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
interest,
configuration,
new RowAction(configuration, interest, blender, amount, colors, source));

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

@ -58,7 +58,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
if (this.resampler is NearestNeighborResampler)
{
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
targetBounds,
configuration,
new NearestNeighborRowAction(this.SourceRectangle, ref matrix, width, source, destination));

2
src/ImageSharp/Processing/Processors/Transforms/CropProcessor{TPixel}.cs

@ -49,7 +49,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
// Copying is cheap, we should process more pixels per task:
ParallelExecutionSettings parallelSettings = ParallelExecutionSettings.FromConfiguration(this.Configuration).MultiplyMinimumPixelsPerTask(4);
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
bounds,
in parallelSettings,
new RowAction(ref bounds, source, destination));

2
src/ImageSharp/Processing/Processors/Transforms/FlipProcessor{TPixel}.cs

@ -75,7 +75,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
/// <param name="configuration">The configuration.</param>
private void FlipY(ImageFrame<TPixel> source, Configuration configuration)
{
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
source.Bounds(),
configuration,
new RowAction(source));

2
src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor{TPixel}.cs

@ -60,7 +60,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
{
Rectangle sourceBounds = this.SourceRectangle;
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
targetBounds,
configuration,
new NearestNeighborRowAction(ref sourceBounds, ref matrix, width, source, destination));

2
src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs

@ -95,7 +95,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
float widthFactor = sourceRectangle.Width / (float)this.targetRectangle.Width;
float heightFactor = sourceRectangle.Height / (float)this.targetRectangle.Height;
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
interest,
configuration,
new RowAction(sourceRectangle, this.targetRectangle, widthFactor, heightFactor, source, destination));

6
src/ImageSharp/Processing/Processors/Transforms/RotateProcessor{TPixel}.cs

@ -131,7 +131,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
/// <param name="configuration">The configuration.</param>
private void Rotate180(ImageFrame<TPixel> source, ImageFrame<TPixel> destination, Configuration configuration)
{
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
source.Bounds(),
configuration,
new Rotate180RowAction(source.Width, source.Height, source, destination));
@ -145,7 +145,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
/// <param name="configuration">The configuration.</param>
private void Rotate270(ImageFrame<TPixel> source, ImageFrame<TPixel> destination, Configuration configuration)
{
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
source.Bounds(),
configuration,
new Rotate270RowAction(destination.Bounds(), source.Width, source.Height, source, destination));
@ -159,7 +159,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
/// <param name="configuration">The configuration.</param>
private void Rotate90(ImageFrame<TPixel> source, ImageFrame<TPixel> destination, Configuration configuration)
{
ParallelRowIterator.IterateRows2(
ParallelRowIterator.IterateRows(
source.Bounds(),
configuration,
new Rotate90RowAction(destination.Bounds(), source.Width, source.Height, source, destination));

Loading…
Cancel
Save