diff --git a/samples/ComputeSharp.BokehBlur/Processor/HlslGaussianBlurProcessor.cs b/samples/ComputeSharp.BokehBlur/Processor/HlslGaussianBlurProcessor.cs new file mode 100644 index 00000000..7a26434a --- /dev/null +++ b/samples/ComputeSharp.BokehBlur/Processor/HlslGaussianBlurProcessor.cs @@ -0,0 +1,55 @@ +using ComputeSharp.BokehBlur.Processor; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.Primitives; + +namespace SixLabors.ImageSharp.Processing.Processors.Convolution +{ + /// + /// Defines Gaussian blur by a (Sigma, Radius) pair + /// + public sealed class HlslGaussianBlurProcessor : IImageProcessor + { + /// + /// The default value for . + /// + public const float DefaultSigma = 3f; + + /// + /// Initializes a new instance of the class + /// + public HlslGaussianBlurProcessor() : this(DefaultSigma, 9) { } + + /// + /// Initializes a new instance of the class + /// + /// The radius value representing the size of the area to sample + public HlslGaussianBlurProcessor(int radius) : this(radius / 3F, radius) { } + + /// + /// Initializes a new instance of the class + /// + /// The sigma value representing the weight of the blur + /// The radius value representing the size of the area to sample (this should be at least twice the sigma value) + public HlslGaussianBlurProcessor(float sigma, int radius) + { + Sigma = sigma; + Radius = radius; + } + + /// + /// Gets the sigma value representing the weight of the blur + /// + public float Sigma { get; } + + /// + /// Gets the radius defining the size of the area to sample + /// + public int Radius { get; } + + /// + public IImageProcessor CreatePixelSpecificProcessor(Image source, Rectangle sourceRectangle) where TPixel : struct, IPixel + { + return new HlslGaussianBlurProcessor(this, source, sourceRectangle); + } + } +} diff --git a/samples/ComputeSharp.BokehBlur/Processor/HlslGaussianBlurProcessor{TPixel}.cs b/samples/ComputeSharp.BokehBlur/Processor/HlslGaussianBlurProcessor{TPixel}.cs new file mode 100644 index 00000000..42673721 --- /dev/null +++ b/samples/ComputeSharp.BokehBlur/Processor/HlslGaussianBlurProcessor{TPixel}.cs @@ -0,0 +1,97 @@ +using System; +using System.Runtime.CompilerServices; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing.Processors; +using SixLabors.ImageSharp.Processing.Processors.Convolution; +using SixLabors.Primitives; + +namespace ComputeSharp.BokehBlur.Processor +{ + /// + /// 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() + { + + } + + /// + public void Dispose() { } + } +}