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: }