Browse Source

optimize ResizeProcessor parallel behavior and Span<T> usage

pull/475/head
Anton Firszov 8 years ago
parent
commit
de67364081
  1. 42
      src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs

42
src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs

@ -132,33 +132,35 @@ namespace SixLabors.ImageSharp.Processing.Processors
0, 0,
sourceRectangle.Bottom, sourceRectangle.Bottom,
configuration.ParallelOptions, configuration.ParallelOptions,
y => () => this.MemoryManager.Allocate<Vector4>(source.Width),
(int y, ParallelLoopState sate, IBuffer<Vector4> tempRowBuffer) =>
{ {
// TODO: Without Parallel.For() this buffer object could be reused: Span<Vector4> firstPassRow = firstPassPixels.GetRowSpan(y);
using (IBuffer<Vector4> tempRowBuffer = this.MemoryManager.Allocate<Vector4>(source.Width)) Span<TPixel> sourceRow = source.GetPixelRowSpan(y);
{ Span<Vector4> tempRowSpan = tempRowBuffer.Span;
Span<Vector4> firstPassRow = firstPassPixels.GetRowSpan(y);
Span<TPixel> sourceRow = source.GetPixelRowSpan(y);
PixelOperations<TPixel>.Instance.ToVector4(sourceRow, tempRowBuffer.Span, sourceRow.Length);
if (this.Compand) PixelOperations<TPixel>.Instance.ToVector4(sourceRow, tempRowSpan, sourceRow.Length);
if (this.Compand)
{
for (int x = minX; x < maxX; x++)
{ {
for (int x = minX; x < maxX; x++) WeightsWindow window = this.HorizontalWeights.Weights[x - startX];
{ firstPassRow[x] = window.ComputeExpandedWeightedRowSum(tempRowSpan, sourceX);
WeightsWindow window = this.HorizontalWeights.Weights[x - startX];
firstPassRow[x] = window.ComputeExpandedWeightedRowSum(tempRowBuffer.Span, sourceX);
}
} }
else }
else
{
for (int x = minX; x < maxX; x++)
{ {
for (int x = minX; x < maxX; x++) WeightsWindow window = this.HorizontalWeights.Weights[x - startX];
{ firstPassRow[x] = window.ComputeWeightedRowSum(tempRowSpan, sourceX);
WeightsWindow window = this.HorizontalWeights.Weights[x - startX];
firstPassRow[x] = window.ComputeWeightedRowSum(tempRowBuffer.Span, sourceX);
}
} }
} }
});
return tempRowBuffer;
},
(IBuffer<Vector4> tmp) => tmp.Dispose());
// Now process the rows. // Now process the rows.
Parallel.For( Parallel.For(

Loading…
Cancel
Save