From 22af95a8d58e37e6d7d5774da4b91809630030e8 Mon Sep 17 00:00:00 2001 From: Ynse Hoornenborg Date: Thu, 1 Sep 2022 16:49:26 +0200 Subject: [PATCH] Minor rework --- .../Convolution/MedianRowOperation{TPixel}.cs | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Convolution/MedianRowOperation{TPixel}.cs b/src/ImageSharp/Processing/Processors/Convolution/MedianRowOperation{TPixel}.cs index 764782f291..a926abb803 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/MedianRowOperation{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/MedianRowOperation{TPixel}.cs @@ -46,9 +46,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution public void Invoke(int y, Span span) { // Span has kernelSize^2 followed by bound width. - int boundsLeft = this.bounds.Left; + int boundsX = this.bounds.X; int boundsWidth = this.bounds.Width; - int boundsRight = this.bounds.Right; int kernelCount = this.kernelSize * this.kernelSize; Span kernelBuffer = span.Slice(0, kernelCount); Span channelVectorBuffer = span.Slice(kernelCount, kernelCount); @@ -67,53 +66,51 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution if (this.preserveAlpha) { - for (int x = boundsLeft; x < boundsRight; x++) + for (int x = 0; x < boundsWidth; x++) { int index = 0; - for (int w = 0; w < this.kernelSize; w++) + for (int kY = 0; kY < this.kernelSize; kY++) { - int j = yOffsets[baseYOffsetIndex + w]; - Span row = this.sourcePixels.DangerousGetRowSpan(j); - for (int z = 0; z < this.kernelSize; z++) + int currentY = yOffsets[baseYOffsetIndex + kY]; + Span row = this.sourcePixels.DangerousGetRowSpan(currentY); + for (int kX = 0; kX < this.kernelSize; kX++) { - int k = xOffsets[baseXOffsetIndex + z]; - TPixel pixel = row[k]; - kernelBuffer[index + z] = pixel.ToVector4(); + int currentX = xOffsets[baseXOffsetIndex + kX]; + TPixel pixel = row[currentX]; + kernelBuffer[index] = pixel.ToVector4(); + index++; } - - index += this.kernelSize; } - targetBuffer[x - boundsLeft] = this.FindMedian3(kernelBuffer, xChannel, yChannel, zChannel, kernelCount); + targetBuffer[x] = this.FindMedian3(kernelBuffer, xChannel, yChannel, zChannel, kernelCount); baseXOffsetIndex += this.kernelSize; } } else { Span wChannel = channelBuffer.Slice(this.wChannelStart, kernelCount); - for (int x = boundsLeft; x < boundsRight; x++) + for (int x = 0; x < boundsWidth; x++) { int index = 0; - for (int w = 0; w < this.kernelSize; w++) + for (int kY = 0; kY < this.kernelSize; kY++) { - int j = yOffsets[baseYOffsetIndex + w]; + int j = yOffsets[baseYOffsetIndex + kY]; Span row = this.sourcePixels.DangerousGetRowSpan(j); - for (int z = 0; z < this.kernelSize; z++) + for (int kX = 0; kX < this.kernelSize; kX++) { - int k = xOffsets[baseXOffsetIndex + z]; - TPixel pixel = row[k]; - kernelBuffer[index + z] = pixel.ToVector4(); + int currentX = xOffsets[baseXOffsetIndex + kX]; + TPixel pixel = row[currentX]; + kernelBuffer[index] = pixel.ToVector4(); + index++; } - - index += this.kernelSize; } - targetBuffer[x - boundsLeft] = this.FindMedian4(kernelBuffer, xChannel, yChannel, zChannel, wChannel, kernelCount); + targetBuffer[x] = this.FindMedian4(kernelBuffer, xChannel, yChannel, zChannel, wChannel, kernelCount); baseXOffsetIndex += this.kernelSize; } } - Span targetRowSpan = this.targetPixels.DangerousGetRowSpan(y).Slice(boundsLeft, boundsWidth); + Span targetRowSpan = this.targetPixels.DangerousGetRowSpan(y).Slice(boundsX, boundsWidth); PixelOperations.Instance.FromVector4Destructive(this.configuration, targetBuffer, targetRowSpan); }