OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
SculptMap.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 // to build without references to System.Drawing, comment this out
29 #define SYSTEM_DRAWING
30 
31 using System;
32 using System.Collections.Generic;
33 using System.Text;
34 
35 #if SYSTEM_DRAWING
36 using System.Drawing;
37 using System.Drawing.Imaging;
38 
39 namespace PrimMesher
40 {
41  public class SculptMap
42  {
43  public int width;
44  public int height;
45  public byte[] redBytes;
46  public byte[] greenBytes;
47  public byte[] blueBytes;
48 
49  public SculptMap()
50  {
51  }
52 
53  public SculptMap(Bitmap bm, int lod)
54  {
55  int bmW = bm.Width;
56  int bmH = bm.Height;
57 
58  if (bmW == 0 || bmH == 0)
59  throw new Exception("SculptMap: bitmap has no data");
60 
61  int numLodPixels = lod * lod; // (32 * 2)^2 = 64^2 pixels for default sculpt map image
62 
63  bool smallMap = bmW * bmH <= numLodPixels;
64  bool needsScaling = false;
65 
66  width = bmW;
67  height = bmH;
68  while (width * height > numLodPixels * 4)
69  {
70  width >>= 1;
71  height >>= 1;
72  needsScaling = true;
73  }
74 
75  try
76  {
77  if (needsScaling)
78  bm = ScaleImage(bm, width, height);
79  }
80 
81  catch (Exception e)
82  {
83  throw new Exception("Exception in ScaleImage(): e: " + e.ToString());
84  }
85 
86  if (width * height > numLodPixels)
87  {
88  width >>= 1;
89  height >>= 1;
90  }
91 
92  int numBytes = (width + 1) * (height + 1);
93  redBytes = new byte[numBytes];
94  greenBytes = new byte[numBytes];
95  blueBytes = new byte[numBytes];
96 
97  int byteNdx = 0;
98 
99  try
100  {
101  for (int y = 0; y <= height; y++)
102  {
103  for (int x = 0; x <= width; x++)
104  {
105  Color c;
106 
107  if (smallMap)
108  c = bm.GetPixel(x < width ? x : x - 1,
109  y < height ? y : y - 1);
110  else
111  c = bm.GetPixel(x < width ? x * 2 : x * 2 - 1,
112  y < height ? y * 2 : y * 2 - 1);
113 
114  redBytes[byteNdx] = c.R;
115  greenBytes[byteNdx] = c.G;
116  blueBytes[byteNdx] = c.B;
117 
118  ++byteNdx;
119  }
120  }
121  }
122  catch (Exception e)
123  {
124  throw new Exception("Caught exception processing byte arrays in SculptMap(): e: " + e.ToString());
125  }
126 
127  width++;
128  height++;
129  }
130 
131  public List<List<Coord>> ToRows(bool mirror)
132  {
133  int numRows = height;
134  int numCols = width;
135 
136  List<List<Coord>> rows = new List<List<Coord>>(numRows);
137 
138  float pixScale = 1.0f / 255;
139 
140  int rowNdx, colNdx;
141  int smNdx = 0;
142 
143 
144  for (rowNdx = 0; rowNdx < numRows; rowNdx++)
145  {
146  List<Coord> row = new List<Coord>(numCols);
147  for (colNdx = 0; colNdx < numCols; colNdx++)
148  {
149 
150  if (mirror)
151  row.Add(new Coord(-((float)redBytes[smNdx] * pixScale - 0.5f), ((float)greenBytes[smNdx] * pixScale - 0.5f), (float)blueBytes[smNdx] * pixScale - 0.5f));
152  else
153  row.Add(new Coord((float)redBytes[smNdx] * pixScale - 0.5f, (float)greenBytes[smNdx] * pixScale - 0.5f, (float)blueBytes[smNdx] * pixScale - 0.5f));
154 
155  ++smNdx;
156  }
157  rows.Add(row);
158  }
159  return rows;
160  }
161 
162  private Bitmap ScaleImage(Bitmap srcImage, int destWidth, int destHeight)
163  {
164 
165  Bitmap scaledImage = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
166 
167  Color c;
168  float xscale = srcImage.Width / destWidth;
169  float yscale = srcImage.Height / destHeight;
170 
171  float sy = 0.5f;
172  for (int y = 0; y < destHeight; y++)
173  {
174  float sx = 0.5f;
175  for (int x = 0; x < destWidth; x++)
176  {
177  try
178  {
179  c = srcImage.GetPixel((int)(sx), (int)(sy));
180  scaledImage.SetPixel(x, y, Color.FromArgb(c.R, c.G, c.B));
181  }
182  catch (IndexOutOfRangeException)
183  {
184  }
185 
186  sx += xscale;
187  }
188  sy += yscale;
189  }
190  srcImage.Dispose();
191  return scaledImage;
192  }
193 
194  }
195 
196  }
197 #endif
SculptMap(Bitmap bm, int lod)
Definition: SculptMap.cs:53
List< List< Coord > > ToRows(bool mirror)
Definition: SculptMap.cs:131