Merge pull request #43 from Sergio0694/feature_gaussian-blur
Added gaussian blur sample
このコミットが含まれているのは:
10ファイル変更
+341
-67
検索条件に一致するファイルはありません
@@ -331,3 +331,4 @@ ASALocalRun/
|
||||
|
||||
# Sample bokeh blur image
|
||||
**/*_bokeh.jpg
|
||||
**/*_gaussian.jpg
|
||||
+36
-36
@@ -5,7 +5,7 @@ VisualStudioVersion = 16.0.29201.188
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp", "src\ComputeSharp\ComputeSharp.csproj", "{314EB353-E7E3-4146-8111-685CC83EC1DE}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.Core", "src\ComputeSharp.Core\ComputeSharp.Core.csproj", "{9F9581FB-6EF7-418E-BFA5-0EFED874E980}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Core", "src\ComputeSharp.Core\ComputeSharp.Core.csproj", "{9F9581FB-6EF7-418E-BFA5-0EFED874E980}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Graphics", "src\ComputeSharp.Graphics\ComputeSharp.Graphics.csproj", "{2499CC74-BC9A-4B30-A3B6-05F9C7BF9B34}"
|
||||
EndProject
|
||||
@@ -15,7 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{0ED8
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Benchmark", "samples\ComputeSharp.Benchmark\ComputeSharp.Benchmark.csproj", "{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.BokehBlur", "samples\ComputeSharp.BokehBlur\ComputeSharp.BokehBlur.csproj", "{524CC5DD-1DEE-4A30-9515-AB90965E5207}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.ImageProcessing", "samples\ComputeSharp.ImageProcessing\ComputeSharp.ImageProcessing.csproj", "{524CC5DD-1DEE-4A30-9515-AB90965E5207}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Sample", "samples\ComputeSharp.Sample\ComputeSharp.Sample.csproj", "{7A48A85D-8866-4DED-A404-653ED09BF396}"
|
||||
EndProject
|
||||
@@ -40,6 +40,22 @@ Global
|
||||
Release|x64 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{314EB353-E7E3-4146-8111-685CC83EC1DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{314EB353-E7E3-4146-8111-685CC83EC1DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{314EB353-E7E3-4146-8111-685CC83EC1DE}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{314EB353-E7E3-4146-8111-685CC83EC1DE}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{314EB353-E7E3-4146-8111-685CC83EC1DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{314EB353-E7E3-4146-8111-685CC83EC1DE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{314EB353-E7E3-4146-8111-685CC83EC1DE}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{314EB353-E7E3-4146-8111-685CC83EC1DE}.Release|x64.Build.0 = Release|Any CPU
|
||||
{9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Release|x64.Build.0 = Release|Any CPU
|
||||
{2499CC74-BC9A-4B30-A3B6-05F9C7BF9B34}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{2499CC74-BC9A-4B30-A3B6-05F9C7BF9B34}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2499CC74-BC9A-4B30-A3B6-05F9C7BF9B34}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
@@ -56,14 +72,22 @@ Global
|
||||
{23AE6EED-79CD-4ADD-9A85-0148BD957D19}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{23AE6EED-79CD-4ADD-9A85-0148BD957D19}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{23AE6EED-79CD-4ADD-9A85-0148BD957D19}.Release|x64.Build.0 = Release|Any CPU
|
||||
{314EB353-E7E3-4146-8111-685CC83EC1DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{314EB353-E7E3-4146-8111-685CC83EC1DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{314EB353-E7E3-4146-8111-685CC83EC1DE}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{314EB353-E7E3-4146-8111-685CC83EC1DE}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{314EB353-E7E3-4146-8111-685CC83EC1DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{314EB353-E7E3-4146-8111-685CC83EC1DE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{314EB353-E7E3-4146-8111-685CC83EC1DE}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{314EB353-E7E3-4146-8111-685CC83EC1DE}.Release|x64.Build.0 = Release|Any CPU
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|x64.Build.0 = Release|Any CPU
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|x64.Build.0 = Release|Any CPU
|
||||
{7A48A85D-8866-4DED-A404-653ED09BF396}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{7A48A85D-8866-4DED-A404-653ED09BF396}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{7A48A85D-8866-4DED-A404-653ED09BF396}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
@@ -88,40 +112,16 @@ Global
|
||||
{E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|x64.Build.0 = Release|Any CPU
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|x64.Build.0 = Release|Any CPU
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|x64.Build.0 = Release|Any CPU
|
||||
{9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Release|x64.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1}
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1}
|
||||
{7A48A85D-8866-4DED-A404-653ED09BF396} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1}
|
||||
{E91F4D24-922A-4921-A8D7-9C45A143C45D} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7}
|
||||
{E889E6D6-A814-4950-AAC1-68F1ECFC5BD6} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7}
|
||||
{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1}
|
||||
{524CC5DD-1DEE-4A30-9515-AB90965E5207} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {4664C5E3-0340-4E22-BCFD-98AAEDF5F2DC}
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using ComputeSharp.BokehBlur.Processor;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
|
||||
namespace ComputeSharp.BokehBlur
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
Console.WriteLine(">> Loading image");
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "city.jpg");
|
||||
using Image<Rgba32> image = Image.Load<Rgba32>(path);
|
||||
|
||||
Console.WriteLine(">> Applying blur");
|
||||
image.Mutate(c => c.ApplyProcessor(new HlslBokehBlurProcessor(80, 2, 3)));
|
||||
|
||||
Console.WriteLine(">> Saving to disk");
|
||||
string targetPath = Path.Combine(
|
||||
Path.GetRelativePath(Path.GetDirectoryName(path), @"..\..\..\"),
|
||||
$"{Path.GetFileNameWithoutExtension(path)}_bokeh{Path.GetExtension(path)}");
|
||||
image.Save(targetPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
差分なしでファイル名を変更。
+1
-1
@@ -3,7 +3,7 @@ using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing.Processors;
|
||||
using SixLabors.Primitives;
|
||||
|
||||
namespace ComputeSharp.BokehBlur.Processor
|
||||
namespace ComputeSharp.BokehBlur.Processors
|
||||
{
|
||||
/// <summary>
|
||||
/// Applies bokeh blur processing to the image
|
||||
+1
-1
@@ -13,7 +13,7 @@ using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing.Processors;
|
||||
using SixLabors.Primitives;
|
||||
|
||||
namespace ComputeSharp.BokehBlur.Processor
|
||||
namespace ComputeSharp.BokehBlur.Processors
|
||||
{
|
||||
/// <summary>
|
||||
/// Applies bokeh blur processing to the image
|
||||
@@ -0,0 +1,56 @@
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing.Processors;
|
||||
using SixLabors.Primitives;
|
||||
|
||||
namespace ComputeSharp.BokehBlur.Processors
|
||||
{
|
||||
/// <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,207 @@
|
||||
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
|
||||
{
|
||||
/// <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()
|
||||
{
|
||||
using Image<RgbaVector> source = Source.CloneAs<RgbaVector>();
|
||||
Span<Vector4> sourceSpan = MemoryMarshal.Cast<RgbaVector, Vector4>(source.GetPixelSpan());
|
||||
|
||||
using ReadWriteBuffer<Vector4> sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceSpan);
|
||||
using ReadWriteBuffer<Vector4> firstPassBuffer = Gpu.Default.AllocateReadWriteBuffer<Vector4>(sourceBuffer.Size);
|
||||
using ReadOnlyBuffer<float> kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(Kernel);
|
||||
|
||||
ApplyVerticalConvolution(sourceBuffer, firstPassBuffer, kernelBuffer);
|
||||
ApplyHorizontalConvolution(firstPassBuffer, sourceBuffer, kernelBuffer);
|
||||
|
||||
sourceBuffer.GetData(sourceSpan);
|
||||
WriteResultData(source);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs a vertical 1D complex convolution with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="source">The source <see cref="ReadWriteBuffer{T}"/> to read data from</param>
|
||||
/// <param name="target">The target <see cref="ReadWriteBuffer{T}"/> to write the results to</param>
|
||||
/// <param name="kernel">The <see cref="ReadOnlyBuffer{T}"/> with the values for the current complex kernel</param>
|
||||
private void ApplyVerticalConvolution(
|
||||
ReadWriteBuffer<Vector4> source,
|
||||
ReadWriteBuffer<Vector4> target,
|
||||
ReadOnlyBuffer<float> 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;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs an horizontal 1D complex convolution with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="source">The source <see cref="ReadWriteBuffer{T}"/> to read data from</param>
|
||||
/// <param name="target">The target <see cref="ReadWriteBuffer{T}"/> to write the results to</param>
|
||||
/// <param name="kernel">The <see cref="ReadOnlyBuffer{T}"/> with the values for the current complex kernel</param>
|
||||
private void ApplyHorizontalConvolution(
|
||||
ReadWriteBuffer<Vector4> source,
|
||||
ReadWriteBuffer<Vector4> target,
|
||||
ReadOnlyBuffer<float> 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;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the final data to the source image
|
||||
/// </summary>
|
||||
/// <param name="image">The source <see cref="Image{TPixel}"/> to read from</param>
|
||||
private void WriteResultData(Image<RgbaVector> 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<RgbaVector, Vector4>(image.GetPixelRowSpan(y)).GetPinnableReference();
|
||||
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
Unsafe.Add(ref rPixel, x).FromVector4(Unsafe.Add(ref r4, x));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose() { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using ComputeSharp.BokehBlur.Processors;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
using SixLabors.ImageSharp.Processing.Processors;
|
||||
|
||||
namespace ComputeSharp.ImageProcessing
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
Console.WriteLine(">> Loading image");
|
||||
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "city.jpg");
|
||||
using Image<Rgb24> image = Image.Load<Rgb24>(path);
|
||||
|
||||
// Apply a series of processors and save the results
|
||||
foreach (var effect in new (string Name, IImageProcessor Processor)[]
|
||||
{
|
||||
("bokeh", new HlslBokehBlurProcessor(80, 2, 3)),
|
||||
("gaussian", new HlslGaussianBlurProcessor(80))
|
||||
})
|
||||
{
|
||||
Console.WriteLine($">> Applying {effect.Name}");
|
||||
using Image<Rgb24> copy = image.Clone();
|
||||
copy.Mutate(c => c.ApplyProcessor(effect.Processor));
|
||||
|
||||
Console.WriteLine($">> Saving {effect.Name} to disk");
|
||||
string targetPath = Path.Combine(
|
||||
Path.GetRelativePath(Path.GetDirectoryName(path), @"..\..\..\"),
|
||||
$"{Path.GetFileNameWithoutExtension(path)}_{effect.Name}{Path.GetExtension(path)}");
|
||||
copy.Save(targetPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
差分なしでファイル名を変更。
|
変更前 幅: | 高さ: | サイズ: 2.0 MiB 変更後 幅: | 高さ: | サイズ: 2.0 MiB |
新しいイシューから参照
ユーザーをブロックする