Skip to content

Commit

Permalink
Revert "Merge pull request #1108 from SixLabors/sp/single-row-paralle…
Browse files Browse the repository at this point in the history
…l-value-delegate"

This reverts commit 1835475, reversing
changes made to dd7bd7b.
  • Loading branch information
JimBobSquarePants committed Feb 8, 2020
1 parent fcf7c44 commit 8540f83
Show file tree
Hide file tree
Showing 24 changed files with 606 additions and 664 deletions.
70 changes: 0 additions & 70 deletions src/ImageSharp/Advanced/IRowAction.cs

This file was deleted.

88 changes: 0 additions & 88 deletions src/ImageSharp/Advanced/IRowAction{TBuffer}.cs

This file was deleted.

37 changes: 16 additions & 21 deletions src/ImageSharp/Advanced/ParallelRowIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,32 @@ namespace SixLabors.ImageSharp.Advanced
public static class ParallelRowIterator
{
/// <summary>
/// Iterate through the rows of a rectangle in optimized batches.
/// 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 row.</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, IRowAction
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.
/// 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 row.</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, IRowAction
where T : struct, IRowIntervalAction
{
ValidateRectangle(rectangle);

Expand All @@ -59,17 +59,15 @@ public static void IterateRows<T>(
// Avoid TPL overhead in this trivial case:
if (numOfSteps == 1)
{
for (int y = top; y < bottom; y++)
{
Unsafe.AsRef(body).Invoke(y);
}

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 rowAction = new WrappingRowAction<T>(top, bottom, verticalStep, in body);
var rowInfo = new WrappingRowIntervalInfo(top, bottom, verticalStep);
var rowAction = new WrappingRowIntervalAction<T>(in rowInfo, in body);

Parallel.For(
0,
Expand All @@ -88,7 +86,7 @@ public static void IterateRows<T>(
/// <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>
public static void IterateRows<T, TBuffer>(Rectangle rectangle, Configuration configuration, in T body)
where T : struct, IRowAction<TBuffer>
where T : struct, IRowIntervalAction<TBuffer>
where TBuffer : unmanaged
{
var parallelSettings = ParallelExecutionSettings.FromConfiguration(configuration);
Expand All @@ -103,7 +101,7 @@ internal static void IterateRows<T, TBuffer>(
Rectangle rectangle,
in ParallelExecutionSettings parallelSettings,
in T body)
where T : struct, IRowAction<TBuffer>
where T : struct, IRowIntervalAction<TBuffer>
where TBuffer : unmanaged
{
ValidateRectangle(rectangle);
Expand All @@ -120,22 +118,19 @@ internal static void IterateRows<T, TBuffer>(
// Avoid TPL overhead in this trivial case:
if (numOfSteps == 1)
{
var rows = new RowInterval(top, bottom);
using (IMemoryOwner<TBuffer> buffer = allocator.Allocate<TBuffer>(width))
{
Span<TBuffer> span = buffer.Memory.Span;

for (int y = top; y < bottom; y++)
{
Unsafe.AsRef(body).Invoke(y, span);
}
Unsafe.AsRef(body).Invoke(rows, buffer.Memory);
}

return;
}

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

Parallel.For(
0,
Expand Down
17 changes: 10 additions & 7 deletions src/ImageSharp/ImageFrame{TPixel}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ internal ImageFrame<TPixel2> CloneAs<TPixel2>(Configuration configuration)
ParallelRowIterator.IterateRows(
this.Bounds(),
configuration,
new RowAction<TPixel2>(this, target, configuration));
new RowIntervalAction<TPixel2>(this, target, configuration));

return target;
}
Expand All @@ -289,15 +289,15 @@ internal void Clear(TPixel value)
/// <summary>
/// A <see langword="struct"/> implementing the clone logic for <see cref="ImageFrame{TPixel}"/>.
/// </summary>
private readonly struct RowAction<TPixel2> : IRowAction
private readonly struct RowIntervalAction<TPixel2> : IRowIntervalAction
where TPixel2 : struct, IPixel<TPixel2>
{
private readonly ImageFrame<TPixel> source;
private readonly ImageFrame<TPixel2> target;
private readonly Configuration configuration;

[MethodImpl(InliningOptions.ShortMethod)]
public RowAction(
public RowIntervalAction(
ImageFrame<TPixel> source,
ImageFrame<TPixel2> target,
Configuration configuration)
Expand All @@ -309,11 +309,14 @@ public RowAction(

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
public void Invoke(int y)
public void Invoke(in RowInterval rows)
{
Span<TPixel> sourceRow = this.source.GetPixelRowSpan(y);
Span<TPixel2> targetRow = this.target.GetPixelRowSpan(y);
PixelOperations<TPixel>.Instance.To(this.configuration, sourceRow, targetRow);
for (int y = rows.Min; y < rows.Max; y++)
{
Span<TPixel> sourceRow = this.source.GetPixelRowSpan(y);
Span<TPixel2> targetRow = this.target.GetPixelRowSpan(y);
PixelOperations<TPixel>.Instance.To(this.configuration, sourceRow, targetRow);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;

namespace SixLabors.ImageSharp.Processing.Processors.Binarization
Expand Down Expand Up @@ -53,13 +54,13 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
ParallelRowIterator.IterateRows(
workingRect,
configuration,
new RowAction(source, upper, lower, threshold, startX, endX, isAlphaOnly));
new RowIntervalAction(source, upper, lower, threshold, startX, endX, isAlphaOnly));
}

/// <summary>
/// A <see langword="struct"/> implementing the clone logic for <see cref="BinaryThresholdProcessor{TPixel}"/>.
/// </summary>
private readonly struct RowAction : IRowAction
private readonly struct RowIntervalAction : IRowIntervalAction
{
private readonly ImageFrame<TPixel> source;
private readonly TPixel upper;
Expand All @@ -70,7 +71,7 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
private readonly bool isAlphaOnly;

[MethodImpl(InliningOptions.ShortMethod)]
public RowAction(
public RowIntervalAction(
ImageFrame<TPixel> source,
TPixel upper,
TPixel lower,
Expand All @@ -90,20 +91,22 @@ public RowAction(

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
public void Invoke(int y)
public void Invoke(in RowInterval rows)
{
Rgba32 rgba = default;

Span<TPixel> row = this.source.GetPixelRowSpan(y);

for (int x = this.startX; x < this.endX; x++)
for (int y = rows.Min; y < rows.Max; y++)
{
ref TPixel color = ref row[x];
color.ToRgba32(ref rgba);
Span<TPixel> row = this.source.GetPixelRowSpan(y);

for (int x = this.startX; x < this.endX; x++)
{
ref TPixel color = ref row[x];
color.ToRgba32(ref rgba);

// Convert to grayscale using ITU-R Recommendation BT.709 if required
byte luminance = this.isAlphaOnly ? rgba.A : ImageMaths.Get8BitBT709Luminance(rgba.R, rgba.G, rgba.B);
color = luminance >= this.threshold ? this.upper : this.lower;
// Convert to grayscale using ITU-R Recommendation BT.709 if required
byte luminance = this.isAlphaOnly ? rgba.A : ImageMaths.Get8BitBT709Luminance(rgba.R, rgba.G, rgba.B);
color = luminance >= this.threshold ? this.upper : this.lower;
}
}
}
}
Expand Down
Loading

0 comments on commit 8540f83

Please sign in to comment.