diff --git a/src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor{TPixel}.cs index aefbeaff88..cbdd0c6f83 100644 --- a/src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor{TPixel}.cs @@ -73,73 +73,74 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects ref Vector4 vectorSpanRef = ref MemoryMarshal.GetReference(vectorSpan); // Rent the shared buffer only once per parallel item - IMemoryOwner bins = configuration.MemoryAllocator.Allocate(levels * 4); - - ref float binsRef = ref bins.GetReference(); - ref int intensityBinRef = ref Unsafe.As(ref binsRef); - ref float redBinRef = ref Unsafe.Add(ref binsRef, levels); - ref float blueBinRef = ref Unsafe.Add(ref redBinRef, levels); - ref float greenBinRef = ref Unsafe.Add(ref blueBinRef, levels); - - for (int y = rows.Min; y < rows.Max; y++) + using (IMemoryOwner bins = configuration.MemoryAllocator.Allocate(levels * 4)) { - Span sourceRow = source.GetPixelRowSpan(y); - Span targetRowSpan = targetPixels.GetRowSpan(y).Slice(startX, length); - PixelOperations.Instance.ToVector4(configuration, targetRowSpan, vectorSpan); + ref float binsRef = ref bins.GetReference(); + ref int intensityBinRef = ref Unsafe.As(ref binsRef); + ref float redBinRef = ref Unsafe.Add(ref binsRef, levels); + ref float blueBinRef = ref Unsafe.Add(ref redBinRef, levels); + ref float greenBinRef = ref Unsafe.Add(ref blueBinRef, levels); - for (int x = startX; x < endX; x++) + for (int y = rows.Min; y < rows.Max; y++) { - int maxIntensity = 0; - int maxIndex = 0; + Span sourceRow = source.GetPixelRowSpan(y); + Span targetRowSpan = targetPixels.GetRowSpan(y).Slice(startX, length); + PixelOperations.Instance.ToVector4(configuration, targetRowSpan, vectorSpan); - // Clear the current shared buffer before processing each target pixel - bins.Memory.Span.Clear(); - - for (int fy = 0; fy <= radius; fy++) + for (int x = startX; x < endX; x++) { - int fyr = fy - radius; - int offsetY = y + fyr; - - offsetY = offsetY.Clamp(0, maxY); + int maxIntensity = 0; + int maxIndex = 0; - Span sourceOffsetRow = source.GetPixelRowSpan(offsetY); + // Clear the current shared buffer before processing each target pixel + bins.Memory.Span.Clear(); - for (int fx = 0; fx <= radius; fx++) + for (int fy = 0; fy <= radius; fy++) { - int fxr = fx - radius; - int offsetX = x + fxr; - offsetX = offsetX.Clamp(0, maxX); + int fyr = fy - radius; + int offsetY = y + fyr; - var vector = sourceOffsetRow[offsetX].ToVector4(); + offsetY = offsetY.Clamp(0, maxY); - float sourceRed = vector.X; - float sourceBlue = vector.Z; - float sourceGreen = vector.Y; + Span sourceOffsetRow = source.GetPixelRowSpan(offsetY); - int currentIntensity = (int)MathF.Round((sourceBlue + sourceGreen + sourceRed) / 3F * (levels - 1)); + for (int fx = 0; fx <= radius; fx++) + { + int fxr = fx - radius; + int offsetX = x + fxr; + offsetX = offsetX.Clamp(0, maxX); - Unsafe.Add(ref intensityBinRef, currentIntensity)++; - Unsafe.Add(ref redBinRef, currentIntensity) += sourceRed; - Unsafe.Add(ref blueBinRef, currentIntensity) += sourceBlue; - Unsafe.Add(ref greenBinRef, currentIntensity) += sourceGreen; + var vector = sourceOffsetRow[offsetX].ToVector4(); - if (Unsafe.Add(ref intensityBinRef, currentIntensity) > maxIntensity) - { - maxIntensity = Unsafe.Add(ref intensityBinRef, currentIntensity); - maxIndex = currentIntensity; + float sourceRed = vector.X; + float sourceBlue = vector.Z; + float sourceGreen = vector.Y; + + int currentIntensity = (int)MathF.Round((sourceBlue + sourceGreen + sourceRed) / 3F * (levels - 1)); + + Unsafe.Add(ref intensityBinRef, currentIntensity)++; + Unsafe.Add(ref redBinRef, currentIntensity) += sourceRed; + Unsafe.Add(ref blueBinRef, currentIntensity) += sourceBlue; + Unsafe.Add(ref greenBinRef, currentIntensity) += sourceGreen; + + if (Unsafe.Add(ref intensityBinRef, currentIntensity) > maxIntensity) + { + maxIntensity = Unsafe.Add(ref intensityBinRef, currentIntensity); + maxIndex = currentIntensity; + } } - } - float red = MathF.Abs(Unsafe.Add(ref redBinRef, maxIndex) / maxIntensity); - float blue = MathF.Abs(Unsafe.Add(ref blueBinRef, maxIndex) / maxIntensity); - float green = MathF.Abs(Unsafe.Add(ref greenBinRef, maxIndex) / maxIntensity); - float alpha = sourceRow[x].ToVector4().W; + float red = MathF.Abs(Unsafe.Add(ref redBinRef, maxIndex) / maxIntensity); + float blue = MathF.Abs(Unsafe.Add(ref blueBinRef, maxIndex) / maxIntensity); + float green = MathF.Abs(Unsafe.Add(ref greenBinRef, maxIndex) / maxIntensity); + float alpha = sourceRow[x].ToVector4().W; - Unsafe.Add(ref vectorSpanRef, x) = new Vector4(red, green, blue, alpha); + Unsafe.Add(ref vectorSpanRef, x) = new Vector4(red, green, blue, alpha); + } } - } - PixelOperations.Instance.FromVector4Destructive(configuration, vectorSpan, targetRowSpan); + PixelOperations.Instance.FromVector4Destructive(configuration, vectorSpan, targetRowSpan); + } } });