From 3502db1f482d234ecb7f2b323ca5175f34a87424 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 18 Mar 2017 20:27:27 +0100 Subject: [PATCH] make it parallel again! --- .../Processors/Transforms/ResizeProcessor.cs | 104 ++++++++++-------- 1 file changed, 58 insertions(+), 46 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs index 0e1a215e0..944e245ac 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs @@ -110,64 +110,76 @@ namespace ImageSharp.Processing.Processors { firstPassPixels.Clear(); - using (PinnedBuffer tempRowBuffer = new PinnedBuffer(sourcePixels.Width)) - { - for (int y = 0; y < sourceRectangle.Bottom; y++) - { - BufferSpan sourceRow = sourcePixels.GetRowSpan(y); + Parallel.For( + 0, + sourceRectangle.Bottom, + this.ParallelOptions, + y => + { + // TODO: Without Parallel.For() this buffer object could be reused: + using (PinnedBuffer tempRowBuffer = new PinnedBuffer(sourcePixels.Width)) + { + BufferSpan sourceRow = sourcePixels.GetRowSpan(y); + + BulkPixelOperations.Instance.ToVector4( + sourceRow, + tempRowBuffer, + sourceRow.Length); + + if (this.Compand) + { + for (int x = minX; x < maxX; x++) + { + WeightsWindow window = this.HorizontalWeights.Weights[x - startX]; + firstPassPixels[x, y] = window.ComputeExpandedWeightedRowSum(tempRowBuffer); + } + } + else + { + for (int x = minX; x < maxX; x++) + { + WeightsWindow window = this.HorizontalWeights.Weights[x - startX]; + firstPassPixels[x, y] = window.ComputeWeightedRowSum(tempRowBuffer); + } + } + } + }); - BulkPixelOperations.Instance.ToVector4(sourceRow, tempRowBuffer, sourceRow.Length); + // Now process the rows. + Parallel.For( + minY, + maxY, + this.ParallelOptions, + y => + { + // Ensure offsets are normalised for cropping and padding. + WeightsWindow window = this.VerticalWeights.Weights[y - startY]; if (this.Compand) { - for (int x = minX; x < maxX; x++) + for (int x = 0; x < width; x++) { - WeightsWindow window = this.HorizontalWeights.Weights[x - startX]; - firstPassPixels[x, y] = window.ComputeExpandedWeightedRowSum(tempRowBuffer); + // Destination color components + Vector4 destination = window.ComputeWeightedColumnSum(firstPassPixels, x); + destination = destination.Compress(); + TColor d = default(TColor); + d.PackFromVector4(destination); + targetPixels[x, y] = d; } } else { - for (int x = minX; x < maxX; x++) + for (int x = 0; x < width; x++) { - WeightsWindow window = this.HorizontalWeights.Weights[x - startX]; - firstPassPixels[x, y] = window.ComputeWeightedRowSum(tempRowBuffer); - } - } - } - } - - // Now process the rows. - for (int y = minY; y < maxY; y++) - { - // Ensure offsets are normalised for cropping and padding. - WeightsWindow window = this.VerticalWeights.Weights[y - startY]; - - if (this.Compand) - { - for (int x = 0; x < width; x++) - { - // Destination color components - Vector4 destination = window.ComputeWeightedColumnSum(firstPassPixels, x); - destination = destination.Compress(); - TColor d = default(TColor); - d.PackFromVector4(destination); - targetPixels[x, y] = d; - } - } - else - { - for (int x = 0; x < width; x++) - { - // Destination color components - Vector4 destination = window.ComputeWeightedColumnSum(firstPassPixels, x); + // Destination color components + Vector4 destination = window.ComputeWeightedColumnSum(firstPassPixels, x); - TColor d = default(TColor); - d.PackFromVector4(destination); - targetPixels[x, y] = d; + TColor d = default(TColor); + d.PackFromVector4(destination); + targetPixels[x, y] = d; + } } - } - } + }); } source.SwapPixelsBuffers(targetPixels);