From 34ecd2c289fed44cfd3b51713f5d60211ff1a5cc Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 19 Aug 2019 00:16:39 +0200 Subject: [PATCH] Initial naive bokeh blur code (broken) --- samples/ComputeSharp.BokehBlur/Program.cs | 30 +++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/samples/ComputeSharp.BokehBlur/Program.cs b/samples/ComputeSharp.BokehBlur/Program.cs index f5b3e2e0..91111902 100644 --- a/samples/ComputeSharp.BokehBlur/Program.cs +++ b/samples/ComputeSharp.BokehBlur/Program.cs @@ -16,6 +16,11 @@ namespace ComputeSharp.BokehBlur /// private const string ImagePath = @""; + /// + /// The radius of the bokeh blur effect to apply + /// + private const int Radius = 12; + static void Main() { // Load the input image @@ -48,8 +53,29 @@ namespace ComputeSharp.BokehBlur int j = ij % width; ref Vector4 rin = ref vectorArray[0]; ref Vector4 rout = ref resultArray[0]; + Vector4 total = Vector4.Zero; + int pixels = 0; - Unsafe.Add(ref rout, i * width + j) = Unsafe.Add(ref rin, i * width + j); + for (int y = -Radius; y <= Radius; y++) + { + for (int x = -Radius; x <= Radius; x++) + { + int iy = i + y; + int jx = j + x; + + if (iy < 0 || iy > height) continue; + if (jx < 0 || jx > width) continue; + int dx2x1 = jx - j; + int dy2y1 = iy - i; + if (MathF.Sqrt(dx2x1 * dx2x1 + dy2y1 * dy2y1) > Radius) continue; + + ref Vector4 v4 = ref Unsafe.Add(ref rin, iy * width + jx); + total += v4; + pixels++; + } + } + + Unsafe.Add(ref rout, i * width + j) = total / pixels; }); // Copy the modified image back @@ -61,7 +87,7 @@ namespace ComputeSharp.BokehBlur for (int j = 0; j < width; j++) { - Unsafe.Add(ref rPixel, i).FromVector4(Unsafe.Add(ref r4, i)); + Unsafe.Add(ref rPixel, j).FromVector4(Unsafe.Add(ref r4, j)); } });