diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs index 1e76422508..0708a6c232 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs @@ -132,33 +132,35 @@ namespace SixLabors.ImageSharp.Processing.Processors 0, sourceRectangle.Bottom, configuration.ParallelOptions, - y => + () => this.MemoryManager.Allocate(source.Width), + (int y, ParallelLoopState sate, IBuffer tempRowBuffer) => { - // TODO: Without Parallel.For() this buffer object could be reused: - using (IBuffer tempRowBuffer = this.MemoryManager.Allocate(source.Width)) - { - Span firstPassRow = firstPassPixels.GetRowSpan(y); - Span sourceRow = source.GetPixelRowSpan(y); - PixelOperations.Instance.ToVector4(sourceRow, tempRowBuffer.Span, sourceRow.Length); + Span firstPassRow = firstPassPixels.GetRowSpan(y); + Span sourceRow = source.GetPixelRowSpan(y); + Span tempRowSpan = tempRowBuffer.Span; - if (this.Compand) + PixelOperations.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(tempRowBuffer.Span, sourceX); - } + WeightsWindow window = this.HorizontalWeights.Weights[x - startX]; + firstPassRow[x] = window.ComputeExpandedWeightedRowSum(tempRowSpan, 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(tempRowBuffer.Span, sourceX); - } + WeightsWindow window = this.HorizontalWeights.Weights[x - startX]; + firstPassRow[x] = window.ComputeWeightedRowSum(tempRowSpan, sourceX); } } - }); + + return tempRowBuffer; + }, + (IBuffer tmp) => tmp.Dispose()); // Now process the rows. Parallel.For(