1: /// <summary>
2: /// Does smoothing using a SNN blur
3: /// </summary>
4: /// <param name="OriginalImage">Image to manipulate</param>
5: /// <param name="Size">Size of the aperture</param>
6: public static Bitmap SNNBlur(Bitmap OriginalImage, int Size)
7: {
8: Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
9: BitmapData NewData = Image.LockImage(NewBitmap);
10: BitmapData OldData = Image.LockImage(OriginalImage);
11: int NewPixelSize = Image.GetPixelSize(NewData);
12: int OldPixelSize = Image.GetPixelSize(OldData);
13: int ApetureMinX = -(Size / 2);
14: int ApetureMaxX = (Size / 2);
15: int ApetureMinY = -(Size / 2);
16: int ApetureMaxY = (Size / 2);
17: for (int x = 0; x < NewBitmap.Width; ++x)
18: {
19: for (int y = 0; y < NewBitmap.Height; ++y)
20: {
21: int RValue = 0;
22: int GValue = 0;
23: int BValue = 0;
24: int NumPixels = 0;
25: for (int x2 = ApetureMinX; x2 < ApetureMaxX; ++x2)
26: {
27: int TempX1 = x + x2;
28: int TempX2 = x - x2;
29: if (TempX1 >= 0 && TempX1 < NewBitmap.Width && TempX2 >= 0 && TempX2 < NewBitmap.Width)
30: {
31: for (int y2 = ApetureMinY; y2 < ApetureMaxY; ++y2)
32: {
33: int TempY1 = y + y2;
34: int TempY2 = y - y2;
35: if (TempY1 >= 0 && TempY1 < NewBitmap.Height && TempY2 >= 0 && TempY2 < NewBitmap.Height)
36: {
37: Color TempColor = Image.GetPixel(OldData, x, y, OldPixelSize);
38: Color TempColor2 = Image.GetPixel(OldData, TempX1, TempY1, OldPixelSize);
39: Color TempColor3 = Image.GetPixel(OldData, TempX2, TempY2, OldPixelSize);
40: if (Distance(TempColor.R, TempColor2.R, TempColor.G, TempColor2.G, TempColor.B, TempColor2.B) <
41: Distance(TempColor.R, TempColor3.R, TempColor.G, TempColor3.G, TempColor.B, TempColor3.B))
42: {
43: RValue += TempColor2.R;
44: GValue += TempColor2.G;
45: BValue += TempColor2.B;
46: }
47: else
48: {
49: RValue += TempColor3.R;
50: GValue += TempColor3.G;
51: BValue += TempColor3.B;
52: }
53: ++NumPixels;
54: }
55: }
56: }
57: }
58: Color MeanPixel = Color.FromArgb(RValue / NumPixels,
59: GValue / NumPixels,
60: BValue / NumPixels);
61: Image.SetPixel(NewData, x, y, MeanPixel, NewPixelSize);
62: }
63: }
64: Image.UnlockImage(NewBitmap, NewData);
65: Image.UnlockImage(OriginalImage, OldData);
66: return NewBitmap;
67: }