diff --git a/src/ImageSharp/Processing/Processors/Convolution/MedianBlurProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Convolution/MedianBlurProcessor{TPixel}.cs index c9b6abbc7..8867c84e8 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/MedianBlurProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/MedianBlurProcessor{TPixel}.cs @@ -31,13 +31,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution source.CopyTo(targetPixels); - var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); // We use a rectangle with width set to 2 * kernelSize^2 + width, to allocate a buffer big enough // for kernel source and target bulk pixel conversion. - var operationBounds = new Rectangle(interest.X, interest.Y, (2 * (kernelSize * kernelSize)) + interest.Width, interest.Height); + Rectangle operationBounds = new Rectangle(interest.X, interest.Y, (2 * (kernelSize * kernelSize)) + interest.Width, interest.Height); - using var map = new KernelSamplingMap(this.Configuration.MemoryAllocator); + using KernelSamplingMap map = new KernelSamplingMap(this.Configuration.MemoryAllocator); map.BuildSamplingOffsetMap(kernelSize, kernelSize, interest, this.definition.BorderWrapModeX, this.definition.BorderWrapModeY); var operation = new MedianRowOperation( diff --git a/src/ImageSharp/Processing/Processors/Convolution/MedianRowOperation{TPixel}.cs b/src/ImageSharp/Processing/Processors/Convolution/MedianRowOperation{TPixel}.cs index c3f32a717..764782f29 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/MedianRowOperation{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/MedianRowOperation{TPixel}.cs @@ -56,28 +56,28 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution // Stack 4 channels of floats in the space of Vector4's. Span channelBuffer = MemoryMarshal.Cast(channelVectorBuffer); - var xChannel = channelBuffer.Slice(0, kernelCount); - var yChannel = channelBuffer.Slice(this.yChannelStart, kernelCount); - var zChannel = channelBuffer.Slice(this.zChannelStart, kernelCount); + Span xChannel = channelBuffer.Slice(0, kernelCount); + Span yChannel = channelBuffer.Slice(this.yChannelStart, kernelCount); + Span zChannel = channelBuffer.Slice(this.zChannelStart, kernelCount); - var xOffsets = this.map.GetColumnOffsetSpan(); - var yOffsets = this.map.GetRowOffsetSpan(); - var baseXOffsetIndex = 0; - var baseYOffsetIndex = (y - this.bounds.Top) * this.kernelSize; + Span xOffsets = this.map.GetColumnOffsetSpan(); + Span yOffsets = this.map.GetRowOffsetSpan(); + int baseXOffsetIndex = 0; + int baseYOffsetIndex = (y - this.bounds.Top) * this.kernelSize; if (this.preserveAlpha) { - for (var x = boundsLeft; x < boundsRight; x++) + for (int x = boundsLeft; x < boundsRight; x++) { - var index = 0; - for (var w = 0; w < this.kernelSize; w++) + int index = 0; + for (int w = 0; w < this.kernelSize; w++) { - var j = yOffsets[baseYOffsetIndex + w]; - var row = this.sourcePixels.DangerousGetRowSpan(j); - for (var z = 0; z < this.kernelSize; z++) + int j = yOffsets[baseYOffsetIndex + w]; + Span row = this.sourcePixels.DangerousGetRowSpan(j); + for (int z = 0; z < this.kernelSize; z++) { - var k = xOffsets[baseXOffsetIndex + z]; - var pixel = row[k]; + int k = xOffsets[baseXOffsetIndex + z]; + TPixel pixel = row[k]; kernelBuffer[index + z] = pixel.ToVector4(); } @@ -90,18 +90,18 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution } else { - var wChannel = channelBuffer.Slice(this.wChannelStart, kernelCount); - for (var x = boundsLeft; x < boundsRight; x++) + Span wChannel = channelBuffer.Slice(this.wChannelStart, kernelCount); + for (int x = boundsLeft; x < boundsRight; x++) { - var index = 0; - for (var w = 0; w < this.kernelSize; w++) + int index = 0; + for (int w = 0; w < this.kernelSize; w++) { - var j = yOffsets[baseYOffsetIndex + w]; - var row = this.sourcePixels.DangerousGetRowSpan(j); - for (var z = 0; z < this.kernelSize; z++) + int j = yOffsets[baseYOffsetIndex + w]; + Span row = this.sourcePixels.DangerousGetRowSpan(j); + for (int z = 0; z < this.kernelSize; z++) { - var k = xOffsets[baseXOffsetIndex + z]; - var pixel = row[k]; + int k = xOffsets[baseXOffsetIndex + z]; + TPixel pixel = row[k]; kernelBuffer[index + z] = pixel.ToVector4(); } @@ -135,6 +135,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution yChannel.Sort(); zChannel.Sort(); + // Taking the W value from the source pixels, where the middle index in the kernelSpan is by definition the resulting pixel. + // This will preserve the alpha value. return new Vector4(xChannel[halfLength], yChannel[halfLength], zChannel[halfLength], kernelSpan[halfLength].W); }