forked from RosaryMala/DwarvenRealms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGaussianBlur.cs
62 lines (61 loc) · 2.42 KB
/
GaussianBlur.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DwarvenRealms
{
static class Blur
{
public static int[,] gaussianBlur(int[,] inputImage, int radius)
{
double[,] pass1 = new double[inputImage.GetUpperBound(0), inputImage.GetUpperBound(1)];
int[,] pass2 = new int[inputImage.GetUpperBound(0), inputImage.GetUpperBound(1)];
double sigma = (float)radius / 3.0;
double[] kernel = new double[radius];
for (int i = 0; i < radius; i++)
{
kernel[i] = (1 / (Math.Sqrt(2 * Math.PI * sigma))) * Math.Pow(Math.E, -((i * i) / (2 * sigma * sigma)));
}
double total = 0.0;
for (int i = 0; i < radius; i++)
{
total += kernel[i];
if (i > 0)
total += kernel[i];
}
for (int i = 0; i < radius; i++)
{
kernel[i] /= total; //this ensures that it always adds up to 1
}
//First do horizontal smoothing.
for (int xi = 0; xi < inputImage.GetUpperBound(0); xi++)
{
for (int yi = 0; yi < inputImage.GetUpperBound(1); yi++)
{
pass1[xi, yi] = kernel[0] * inputImage[xi, yi];
for (int i = 1; i < radius; i++)
{
pass1[xi, yi] += DwarfWorldMap.getClampedCoord(inputImage, xi + i, yi) * kernel[i];
pass1[xi, yi] += DwarfWorldMap.getClampedCoord(inputImage, xi - i, yi) * kernel[i];
}
}
}
//Next do vertical. Also convert back to int.
for (int xi = 0; xi < inputImage.GetUpperBound(0); xi++)
{
for (int yi = 0; yi < inputImage.GetUpperBound(1); yi++)
{
double pixel = kernel[0] * inputImage[xi, yi];
for (int i = 1; i < radius; i++)
{
pixel += DwarfWorldMap.getClampedCoord(pass1, xi, yi + i) * kernel[i];
pixel += DwarfWorldMap.getClampedCoord(pass1, xi, yi - i) * kernel[i];
}
pass2[xi, yi] = (int)Math.Floor(pixel + 0.5);
}
}
return pass2;
}
}
}