Added base gaussian blur processor
このコミットが含まれているのは:
1 親
488e8095a1
コミット
edbe492f42
2ファイル変更
+152
検索条件に一致するファイルはありません
@@ -0,0 +1,55 @@
|
||||
using ComputeSharp.BokehBlur.Processor;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.Primitives;
|
||||
|
||||
namespace SixLabors.ImageSharp.Processing.Processors.Convolution
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines Gaussian blur by a (Sigma, Radius) pair
|
||||
/// </summary>
|
||||
public sealed class HlslGaussianBlurProcessor : IImageProcessor
|
||||
{
|
||||
/// <summary>
|
||||
/// The default value for <see cref="Sigma"/>.
|
||||
/// </summary>
|
||||
public const float DefaultSigma = 3f;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HlslGaussianBlurProcessor"/> class
|
||||
/// </summary>
|
||||
public HlslGaussianBlurProcessor() : this(DefaultSigma, 9) { }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HlslGaussianBlurProcessor"/> class
|
||||
/// </summary>
|
||||
/// <param name="radius">The radius value representing the size of the area to sample</param>
|
||||
public HlslGaussianBlurProcessor(int radius) : this(radius / 3F, radius) { }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HlslGaussianBlurProcessor"/> class
|
||||
/// </summary>
|
||||
/// <param name="sigma"> The sigma value representing the weight of the blur </param>
|
||||
/// <param name="radius">The radius value representing the size of the area to sample (this should be at least twice the sigma value)</param>
|
||||
public HlslGaussianBlurProcessor(float sigma, int radius)
|
||||
{
|
||||
Sigma = sigma;
|
||||
Radius = radius;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the sigma value representing the weight of the blur
|
||||
/// </summary>
|
||||
public float Sigma { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the radius defining the size of the area to sample
|
||||
/// </summary>
|
||||
public int Radius { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Image<TPixel> source, Rectangle sourceRectangle) where TPixel : struct, IPixel<TPixel>
|
||||
{
|
||||
return new HlslGaussianBlurProcessor<TPixel>(this, source, sourceRectangle);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Applies Gaussian blur processing to an image.
|
||||
/// </summary>
|
||||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
||||
internal class HlslGaussianBlurProcessor<TPixel> : IImageProcessor<TPixel> where TPixel : struct, IPixel<TPixel>
|
||||
{
|
||||
/// <summary>
|
||||
/// The 1D kernel to apply
|
||||
/// </summary>
|
||||
private readonly float[] Kernel;
|
||||
|
||||
/// <summary>
|
||||
/// The source <see cref="Image{TPixel}"/> instance to modify
|
||||
/// </summary>
|
||||
private readonly Image<TPixel> Source;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HlslGaussianBlurProcessor"/> class
|
||||
/// </summary>
|
||||
/// <param name="definition">The <see cref="HlslGaussianBlurProcessor"/> defining the processor parameters</param>
|
||||
/// <param name="source">The source <see cref="Image{TPixel}"/> for the current processor instance</param>
|
||||
/// <param name="sourceRectangle">The source area to process for the current processor instance</param>
|
||||
public HlslGaussianBlurProcessor(HlslGaussianBlurProcessor definition, Image<TPixel> source, Rectangle sourceRectangle)
|
||||
{
|
||||
int kernelSize = definition.Radius * 2 + 1;
|
||||
Kernel = CreateGaussianBlurKernel(kernelSize, definition.Sigma);
|
||||
Source = source;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a 1 dimensional Gaussian kernel using the Gaussian G(x) function
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implementation of 1D Gaussian G(x) function
|
||||
/// </summary>
|
||||
/// <param name="x">The x provided to G(x)</param>
|
||||
/// <param name="sigma">The spread of the blur</param>
|
||||
/// <returns>The Gaussian G(x)</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Apply()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose() { }
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする