From 5059597bbfa5a05b7b07de34872d9eca39319801 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 5 Dec 2020 13:55:49 +0000 Subject: [PATCH] Update KernelSamplingMap.cs --- .../Processors/Convolution/KernelSamplingMap.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Convolution/KernelSamplingMap.cs b/src/ImageSharp/Processing/Processors/Convolution/KernelSamplingMap.cs index 73a4fa4004..144d356c6e 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/KernelSamplingMap.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/KernelSamplingMap.cs @@ -4,6 +4,7 @@ using System; using System.Buffers; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using SixLabors.ImageSharp.Memory; namespace SixLabors.ImageSharp.Processing.Processors.Convolution @@ -44,13 +45,16 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution int radiusY = kernelHeight >> 1; int radiusX = kernelWidth >> 1; - // Calculate the potential sampling y-offsets. + // Calculate the y and x sampling offsets clamped to the given rectangle. + // While this isn't a hotpath we still dip into unsafe to avoid the span bounds + // checks as the can potentially be looping over large arrays. Span ySpan = this.yOffsets.GetSpan(); + ref int ySpanBase = ref MemoryMarshal.GetReference(ySpan); for (int row = 0; row < bounds.Height; row++) { for (int y = 0; y < kernelHeight; y++) { - ySpan[(row * kernelHeight) + y] = row + y + minY - radiusY; + Unsafe.Add(ref ySpanBase, (row * kernelHeight) + y) = row + y + minY - radiusY; } } @@ -59,13 +63,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution Numerics.Clamp(ySpan, minY, maxY); } - // Calculate the potential sampling x-offsets. Span xSpan = this.xOffsets.GetSpan(); + ref int xSpanBase = ref MemoryMarshal.GetReference(xSpan); for (int column = 0; column < bounds.Width; column++) { for (int x = 0; x < kernelWidth; x++) { - xSpan[(column * kernelWidth) + x] = column + x + minX - radiusX; + Unsafe.Add(ref xSpanBase, (column * kernelWidth) + x) = column + x + minX - radiusX; } }