Other Posts in Image Editing

  1. Perlin Noise
  2. Fault Formation
  3. Cellular Textures
  4. Resizing an Image in C#
  5. Box Blur and Gaussian Blur... Sort of...
  6. Thermal Erosion
  7. Using Mid Point Displacement to Create Cracks
  8. Fluvial Erosion
  9. Creating Marble Like Textures Procedurally
  10. Procedural Textures and Dilation
  11. Converting Image to Black and White in C#
  12. Getting an HTML Based Color Palette from an Image in C#
  13. Adding Noise/Jitter to an Image in C#
  14. Creating Pixelated Images in C#
  15. Edge detection in C#
  16. Using Sin to Get What You Want... In C#...
  17. Noise Reduction of an Image in C# using Median Filters
  18. Image Dilation in C#
  19. Sepia Tone in C#
  20. Kuwahara Filter in C#
  21. Matrix Convolution Filters in C#
  22. Symmetric Nearest Neighbor in C#
  23. Bump Map Creation Using C#
  24. Normal Map Creation Using C#
  25. Creating Negative Images using C#
  26. Red, Blue, and Green Filters in C#
  27. Converting an Image to ASCII Art in C#
  28. Adjusting Brightness of an Image in C#
  29. Adding Noise to an Image in C#
  30. Adjusting the Gamma of an Image Using C#
  31. Adjusting Contrast of an Image in C#
  32. Drawing a Box With Rounded Corners in C#
  33. Anding Two Images Together Using C#
  34. Motion Detection in C#
  35. Creating Thermometer Chart in C#
  36. Colorizing a Black and White Image in C#
  37. Extracting an Icon From a File
  38. Setting the Pixel Format and Image Format of an Image in .Net
  39. Using Unsafe Code for Faster Image Manipulation
  40. Sobel Edge Detection and Laplace Edge Detection in C#

Bump Map Creation Using C#

2/24/2009

I've been posting a lot of C# code with regard to image manipulation. Half of the reason for this is that at work, I'm working on the initial planning/setup of a SharePoint project (lots of planning, not much programming involved at the moment). The other half is that one of my personal projects is nearing completion and I plan on putting out that code all at once. So I'm left with code that gets added to my utility library as I work on my personal projects... In this case, it's an update to my procedural texture generator (that has been shelved for a bit). Specifically I created a class to help with the generation of bump maps.

For those of you that don't know what a bump map is, it is a height map of sorts that is applied to 3D geometry prior to lighting that gives the impression that there is more detail than there actually is on the object. There are a few different techniques for creating bump maps (as there are a few different types of bump maps), but the easiest to create is the 1D bump map. If you've seen them, they're the grey images that look like they have some depth to them. And as always, I've created some code for creating them, which is why you're here:

   1: /*
   2: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
   3: 
   4: Permission is hereby granted, free of charge, to any person obtaining a copy
   5: of this software and associated documentation files (the "Software"), to deal
   6: in the Software without restriction, including without limitation the rights
   7: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   8: copies of the Software, and to permit persons to whom the Software is
   9: furnished to do so, subject to the following conditions:
  10: 
  11: The above copyright notice and this permission notice shall be included in
  12: all copies or substantial portions of the Software.
  13: 
  14: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20: THE SOFTWARE.*/
  21:  
  22: #region Usings
  23: using System.Drawing;
  24: #endregion
  25:  
  26: namespace Utilities.Media.Image
  27: {
  28:     /// <summary>
  29:     /// Used for creating bump maps
  30:     /// </summary>
  31:     public class BumpMap
  32:     {
  33:         #region Constructors
  34:  
  35:         /// <summary>
  36:         /// Constructor
  37:         /// </summary>
  38:         public BumpMap()
  39:         {
  40:             Invert = false;
  41:             Direction = Direction.TopBottom;
  42:         }
  43:  
  44:         #endregion
  45:  
  46:         #region Protected Properties
  47:  
  48:         protected Filter EdgeDetectionFilter { get; set; }
  49:  
  50:         #endregion
  51:  
  52:         #region Public Properties
  53:  
  54:         /// <summary>
  55:         /// Determines the direction of the bump map
  56:         /// </summary>
  57:         public bool Invert { get; set; }
  58:  
  59:         /// <summary>
  60:         /// Determines the direction of the bump map
  61:         /// </summary>
  62:         public Direction Direction { get; set; }
  63:  
  64:         #endregion
  65:  
  66:         #region Protected Functions
  67:  
  68:         /// <summary>
  69:         /// Sets up the edge detection filter
  70:         /// </summary>
  71:         protected void CreateFilter()
  72:         {
  73:             EdgeDetectionFilter = new Filter(3, 3);
  74:             if (Direction == Direction.TopBottom)
  75:             {
  76:                 if (!Invert)
  77:                 {
  78:                     EdgeDetectionFilter.MyFilter[0, 0] = 1;
  79:                     EdgeDetectionFilter.MyFilter[1, 0] = 2;
  80:                     EdgeDetectionFilter.MyFilter[2, 0] = 1;
  81:                     EdgeDetectionFilter.MyFilter[0, 1] = 0;
  82:                     EdgeDetectionFilter.MyFilter[1, 1] = 0;
  83:                     EdgeDetectionFilter.MyFilter[2, 1] = 0;
  84:                     EdgeDetectionFilter.MyFilter[0, 2] = -1;
  85:                     EdgeDetectionFilter.MyFilter[1, 2] = -2;
  86:                     EdgeDetectionFilter.MyFilter[2, 2] = -1;
  87:                 }
  88:                 else
  89:                 {
  90:                     EdgeDetectionFilter.MyFilter[0, 0] = -1;
  91:                     EdgeDetectionFilter.MyFilter[1, 0] = -2;
  92:                     EdgeDetectionFilter.MyFilter[2, 0] = -1;
  93:                     EdgeDetectionFilter.MyFilter[0, 1] = 0;
  94:                     EdgeDetectionFilter.MyFilter[1, 1] = 0;
  95:                     EdgeDetectionFilter.MyFilter[2, 1] = 0;
  96:                     EdgeDetectionFilter.MyFilter[0, 2] = 1;
  97:                     EdgeDetectionFilter.MyFilter[1, 2] = 2;
  98:                     EdgeDetectionFilter.MyFilter[2, 2] = 1;
  99:                 }
 100:             }
 101:             else
 102:             {
 103:                 if (!Invert)
 104:                 {
 105:                     EdgeDetectionFilter.MyFilter[0, 0] = -1;
 106:                     EdgeDetectionFilter.MyFilter[0, 1] = -2;
 107:                     EdgeDetectionFilter.MyFilter[0, 2] = -1;
 108:                     EdgeDetectionFilter.MyFilter[1, 0] = 0;
 109:                     EdgeDetectionFilter.MyFilter[1, 1] = 0;
 110:                     EdgeDetectionFilter.MyFilter[1, 2] = 0;
 111:                     EdgeDetectionFilter.MyFilter[2, 0] = 1;
 112:                     EdgeDetectionFilter.MyFilter[2, 1] = 2;
 113:                     EdgeDetectionFilter.MyFilter[2, 2] = 1;
 114:                 }
 115:                 else
 116:                 {
 117:                     EdgeDetectionFilter.MyFilter[0, 0] = 1;
 118:                     EdgeDetectionFilter.MyFilter[0, 1] = 2;
 119:                     EdgeDetectionFilter.MyFilter[0, 2] = 1;
 120:                     EdgeDetectionFilter.MyFilter[1, 0] = 0;
 121:                     EdgeDetectionFilter.MyFilter[1, 1] = 0;
 122:                     EdgeDetectionFilter.MyFilter[1, 2] = 0;
 123:                     EdgeDetectionFilter.MyFilter[2, 0] = -1;
 124:                     EdgeDetectionFilter.MyFilter[2, 1] = -2;
 125:                     EdgeDetectionFilter.MyFilter[2, 2] = -1;
 126:                 }
 127:             }
 128:             EdgeDetectionFilter.Offset = 127;
 129:         }
 130:  
 131:         #endregion
 132:  
 133:         #region Public Functions
 134:  
 135:         /// <summary>
 136:         /// Creates the bump map
 137:         /// </summary>
 138:         public Bitmap Create(Bitmap Image)
 139:         {
 140:             CreateFilter();
 141:             using (Bitmap TempImage = EdgeDetectionFilter.ApplyFilter(Image))
 142:             {
 143:                 return Media.Image.Image.ConvertBlackAndWhite(TempImage);
 144:             }
 145:         }
 146:  
 147:         #endregion
 148:     }
 149:  
 150:     #region Enum
 151:  
 152:     /// <summary>
 153:     /// Direction
 154:     /// </summary>
 155:     public enum Direction
 156:     {
 157:         TopBottom = 0,
 158:         LeftRight
 159:     };
 160:     #endregion
 161: }

The code above really doesn't have much to it. It simply uses the Filter class that I showed a little bit ago, so you're going to need that in order to create the bump map. Also a function from my utility library to convert the image to black and white is used. Other than that, all the code does, is it sets up a sobel emboss filter:

1   2   1
0   0   0
-1 -2 -1

And runs it on the image. Please note that you can tell it to flip the filter, go left to right instead of top to bottom, etc. so it does have some flexibility. The image, after the filter is applied, then goes through a grey scale process so we end up with the correct image (otherwise you'll notice some artifacts from the color image). From there the Image property will contain your bump map. But that's all there is to it. So give it a try, leave feedback, and happy coding.



Comments