using System; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading.Tasks; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; using SixLabors.Primitives; namespace ComputeSharp.BokehBlur.Processors { /// /// Applies Gaussian blur processing to an image. /// /// The pixel format. internal class HlslGaussianBlurProcessor : IImageProcessor where TPixel : struct, IPixel { /// /// The 1D kernel to apply /// private readonly float[] Kernel; /// /// The source instance to modify /// private readonly Image Source; /// /// Initializes a new instance of the class /// /// The defining the processor parameters /// The source for the current processor instance /// The source area to process for the current processor instance public HlslGaussianBlurProcessor(HlslGaussianBlurProcessor definition, Image source, Rectangle sourceRectangle) { int kernelSize = definition.Radius * 2 + 1; Kernel = CreateGaussianBlurKernel(kernelSize, definition.Sigma); Source = source; } /// /// Creates a 1 dimensional Gaussian kernel using the Gaussian G(x) function /// private static float[] CreateGaussianBlurKernel(int size, float weight) { float[] kernel = new float[size]; ref float rKernel = ref kernel[0]; float sum = 0F; float midpoint = (size - 1) / 2F; for (int i = 0; i < size; i++) { float x = i - midpoint; float gx = Gaussian(x, weight); sum += gx; Unsafe.Add(ref rKernel, i) = gx; } // Normalize kernel so that the sum of all weights equals 1 for (int i = 0; i < size; i++) { Unsafe.Add(ref rKernel, i) /= sum; } return kernel; } /// /// Implementation of 1D Gaussian G(x) function /// /// The x provided to G(x) /// The spread of the blur /// The Gaussian G(x) private static float Gaussian(float x, float sigma) { const float Numerator = 1.0f; float denominator = MathF.Sqrt(2 * MathF.PI) * sigma; float exponentNumerator = -x * x; float exponentDenominator = 2 * sigma * sigma; float left = Numerator / denominator; float right = MathF.Exp(exponentNumerator / exponentDenominator); return left * right; } /// public void Apply() { using Image source = Source.CloneAs(); Span sourceSpan = MemoryMarshal.Cast(source.GetPixelSpan()); using ReadWriteBuffer sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceSpan); using ReadWriteBuffer firstPassBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer.Size); using ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(Kernel); ApplyVerticalConvolution(sourceBuffer, firstPassBuffer, kernelBuffer); ApplyHorizontalConvolution(firstPassBuffer, sourceBuffer, kernelBuffer); sourceBuffer.GetData(sourceSpan); WriteResultData(source); } /// /// Performs a vertical 1D complex convolution with the specified parameters /// /// The source to read data from /// The target to write the results to /// The with the values for the current complex kernel private void ApplyVerticalConvolution( ReadWriteBuffer source, ReadWriteBuffer target, ReadOnlyBuffer kernel) { int height = Source.Height; int width = Source.Width; int maxY = height - 1; int maxX = width - 1; int kernelLength = kernel.Size; Gpu.Default.For(width, height, id => { Vector4 result = Vector4.Zero; int radiusY = kernelLength >> 1; int sourceOffsetColumnBase = id.X; for (int i = 0; i < kernelLength; i++) { int offsetY = Hlsl.Clamp(id.Y + i - radiusY, 0, maxY); int offsetX = Hlsl.Clamp(sourceOffsetColumnBase, 0, maxX); Vector4 color = source[offsetY * width + offsetX]; result += kernel[i] * color; } int offsetXY = id.Y * width + id.X; target[offsetXY] = result; }); } /// /// Performs an horizontal 1D complex convolution with the specified parameters /// /// The source to read data from /// The target to write the results to /// The with the values for the current complex kernel private void ApplyHorizontalConvolution( ReadWriteBuffer source, ReadWriteBuffer target, ReadOnlyBuffer kernel) { int height = Source.Height; int width = Source.Width; int maxY = height - 1; int maxX = width - 1; int kernelLength = kernel.Size; Gpu.Default.For(width, height, id => { Vector4 result = Vector4.Zero; int radiusX = kernelLength >> 1; int sourceOffsetColumnBase = id.X; int offsetY = Hlsl.Clamp(id.Y, 0, maxY); int offsetXY; for (int i = 0; i < kernelLength; i++) { int offsetX = Hlsl.Clamp(sourceOffsetColumnBase + i - radiusX, 0, maxX); offsetXY = offsetY * width + offsetX; Vector4 color = source[offsetXY]; result += kernel[i] * color; } offsetXY = id.Y * width + id.X; target[offsetXY] = result; }); } /// /// Writes the final data to the source image /// /// The source to read from private void WriteResultData(Image image) { int width = Source.Width; Parallel.For(0, Source.Height, y => { ref TPixel rPixel = ref Source.GetPixelRowSpan(y).GetPinnableReference(); ref Vector4 r4 = ref MemoryMarshal.Cast(image.GetPixelRowSpan(y)).GetPinnableReference(); for (int x = 0; x < width; x++) { Unsafe.Add(ref rPixel, x).FromVector4(Unsafe.Add(ref r4, x)); } }); } /// public void Dispose() { } } }