From 156aaa3446029c748f678d3e4c663e736bfb908a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 26 Jan 2020 22:12:09 +0100 Subject: [PATCH] Reimplemented bulk conversion for target row --- .../Effects/OilPaintingProcessor{TPixel}.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor{TPixel}.cs index 5d90101988..35c14449f9 100644 --- a/src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor{TPixel}.cs @@ -68,12 +68,18 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects this.Configuration, (rows) => { - // Allocate the reusable source buffer, to enable vectorized bulk conversion + // Allocate the reusable source row buffer, to enable vectorized bulk conversions using IMemoryOwner sourceRowBuffer = configuration.MemoryAllocator.Allocate(rowWidth); Span sourceRowVector4Span = sourceRowBuffer.Memory.Span; Span sourceRowAreaVector4Span = sourceRowVector4Span.Slice(startX, rectangleWidth); + // Allocate the reusable target row buffer + using IMemoryOwner targetRowBuffer = configuration.MemoryAllocator.Allocate(rowWidth); + + Span targetRowVector4Span = targetRowBuffer.Memory.Span; + Span targetRowAreaVector4Span = targetRowVector4Span.Slice(startX, rectangleWidth); + // Rent the shared buffer only once per parallel item. using IMemoryOwner bins = configuration.MemoryAllocator.Allocate(levels * 4); @@ -90,8 +96,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects PixelOperations.Instance.ToVector4(configuration, sourceRowAreaPixelSpan, sourceRowAreaVector4Span); - Span targetRow = targetPixels.GetRowSpan(y); - for (int x = startX; x < endX; x++) { int maxIntensity = 0; @@ -140,10 +144,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects float green = MathF.Abs(Unsafe.Add(ref greenBinRef, maxIndex) / maxIntensity); float alpha = sourceRowVector4Span[x].W; - ref TPixel pixel = ref targetRow[x]; - pixel.FromVector4(new Vector4(red, green, blue, alpha)); + targetRowVector4Span[x] = new Vector4(red, green, blue, alpha); } } + + Span targetRowAreaPixelSpan = targetPixels.GetRowSpan(y).Slice(startX, rectangleWidth); + + PixelOperations.Instance.FromVector4Destructive(configuration, targetRowAreaVector4Span, targetRowAreaPixelSpan); } });