From 41f17ba6b575ea5604dd56e774695b85f9e781ff Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 25 Jul 2026 23:43:27 +1000 Subject: [PATCH] Use tensor division for Gaussian kernels --- .../Convolution/ConvolutionProcessorHelpers.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessorHelpers.cs b/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessorHelpers.cs index 52cd9dbc5..db9eff55c 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessorHelpers.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessorHelpers.cs @@ -37,11 +37,8 @@ internal static class ConvolutionProcessorHelpers kernel[i] = gx; } - // Normalize kernel so that the sum of all weights equals 1 - for (int i = 0; i < size; i++) - { - kernel[i] /= sum; - } + // Divide every weight by the accumulated Gaussian sum so the kernel has unit response. + TensorPrimitives_.Divide(kernel, sum, kernel); return kernel; } @@ -76,11 +73,8 @@ internal static class ConvolutionProcessorHelpers // center while adding twice the Gaussian sum so the complete kernel retains unit response. kernel[midpointRounded] = (2F * sum) - midpointValue; - // Normalize kernel so that the sum of all weights equals 1 - for (int i = 0; i < size; i++) - { - kernel[i] /= sum; - } + // Sharpening changes signs but preserves the Gaussian sum, so the same divisor produces unit response. + TensorPrimitives_.Divide(kernel, sum, kernel); return kernel; }