# Procedural Textures and Dilation

5/15/2008

It's amazing how I tend to get sucked into projects or think up new projects when I already have too many (including such projects as a web portal, multiplayer game, a proof for an algorithm to find a p-like cycle in polynomial time, etc.). However I find image editing to be the one that interests me the most.  Anyway, I've been looking back through a couple projects that I've used in the past, including FxGen and found an interesting function, Dilate. After a quick bit of work, I created a C# function to accomplish it:

`   1: /// <summary>`
`   2: /// Does dilation`
`   3: /// </summary>`
`   4: /// <param name="OriginalImage">Image to manipulate</param>`
`   5: /// <param name="Size">Size of the aperture</param>`
`   6: public static Bitmap Dilate(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 ApetureMin = -(Size / 2);`
`  14:     int ApetureMax = (Size / 2);`
`  15:     for (int x = 0; x < NewBitmap.Width; ++x)`
`  16:     {`
`  17:         for (int y = 0; y < NewBitmap.Height; ++y)`
`  18:         {`
`  19:             int RValue = 0;`
`  20:             int GValue = 0;`
`  21:             int BValue = 0;`
`  22:             for (int x2 = ApetureMin; x2 < ApetureMax; ++x2)`
`  23:             {`
`  24:                 int TempX = x + x2;`
`  25:                 if (TempX >= 0 && TempX < NewBitmap.Width)`
`  26:                 {`
`  27:                     for (int y2 = ApetureMin; y2 < ApetureMax; ++y2)`
`  28:                     {`
`  29:                         int TempY = y + y2;`
`  30:                         if (TempY >= 0 && TempY < NewBitmap.Height)`
`  31:                         {`
`  32:                             Color TempColor = Image.GetPixel(OldData, TempX, TempY, OldPixelSize);`
`  33:                             if (TempColor.R > RValue)`
`  34:                                 RValue = TempColor.R;`
`  35:                             if (TempColor.G > GValue)`
`  36:                                 GValue = TempColor.G;`
`  37:                             if (TempColor.B > BValue)`
`  38:                                 BValue = TempColor.B;`
`  39:                         }`
`  40:                     }`
`  41:                 }`
`  42:             }`
`  43:             Color TempPixel = Color.FromArgb(RValue, GValue, BValue);`
`  44:             Image.SetPixel(NewData, x, y, TempPixel, NewPixelSize);`
`  45:         }`
`  46:     }`
`  47:     Image.UnlockImage(NewBitmap, NewData);`
`  48:     Image.UnlockImage(OriginalImage, OldData);`
`  49:     return NewBitmap;`
`  50: }`

The basic concept is we got through and check each pixel against its neighbors and whichever neighbor is the highest, we set the pixel to that value. In turn it looks like an image that's dilated. It's probably not the most useful function, but it can come in handy every once in a while. Anyway, try the code, leave feedback, and happy coding.