diff options
Diffstat (limited to 'OpenSim/Region/Physics/Meshing/SculptMap.cs')
-rw-r--r-- | OpenSim/Region/Physics/Meshing/SculptMap.cs | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/Meshing/SculptMap.cs b/OpenSim/Region/Physics/Meshing/SculptMap.cs new file mode 100644 index 0000000..d2d71de --- /dev/null +++ b/OpenSim/Region/Physics/Meshing/SculptMap.cs | |||
@@ -0,0 +1,176 @@ | |||
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 * 2 * lod * 2; // (32 * 2)^2 = 64^2 pixels for default sculpt map image | ||
62 | |||
63 | bool needsScaling = false; | ||
64 | |||
65 | width = bmW; | ||
66 | height = bmH; | ||
67 | while (width * height > numLodPixels) | ||
68 | { | ||
69 | width >>= 1; | ||
70 | height >>= 1; | ||
71 | needsScaling = true; | ||
72 | } | ||
73 | |||
74 | |||
75 | |||
76 | try | ||
77 | { | ||
78 | if (needsScaling) | ||
79 | bm = ScaleImage(bm, width, height, | ||
80 | System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor); | ||
81 | } | ||
82 | |||
83 | catch (Exception e) | ||
84 | { | ||
85 | throw new Exception("Exception in ScaleImage(): e: " + e.ToString()); | ||
86 | } | ||
87 | |||
88 | if (width * height > lod * lod) | ||
89 | { | ||
90 | width >>= 1; | ||
91 | height >>= 1; | ||
92 | } | ||
93 | |||
94 | int numBytes = (width + 1) * (height + 1); | ||
95 | redBytes = new byte[numBytes]; | ||
96 | greenBytes = new byte[numBytes]; | ||
97 | blueBytes = new byte[numBytes]; | ||
98 | |||
99 | int byteNdx = 0; | ||
100 | |||
101 | try | ||
102 | { | ||
103 | for (int y = 0; y <= height; y++) | ||
104 | { | ||
105 | for (int x = 0; x <= width; x++) | ||
106 | { | ||
107 | int bmY = y < height ? y * 2 : y * 2 - 1; | ||
108 | int bmX = x < width ? x * 2 : x * 2 - 1; | ||
109 | Color c = bm.GetPixel(bmX, bmY); | ||
110 | |||
111 | redBytes[byteNdx] = c.R; | ||
112 | greenBytes[byteNdx] = c.G; | ||
113 | blueBytes[byteNdx] = c.B; | ||
114 | |||
115 | ++byteNdx; | ||
116 | } | ||
117 | } | ||
118 | } | ||
119 | catch (Exception e) | ||
120 | { | ||
121 | throw new Exception("Caught exception processing byte arrays in SculptMap(): e: " + e.ToString()); | ||
122 | } | ||
123 | |||
124 | width++; | ||
125 | height++; | ||
126 | } | ||
127 | |||
128 | public List<List<Coord>> ToRows(bool mirror) | ||
129 | { | ||
130 | int numRows = height; | ||
131 | int numCols = width; | ||
132 | |||
133 | List<List<Coord>> rows = new List<List<Coord>>(numRows); | ||
134 | |||
135 | float pixScale = 1.0f / 255; | ||
136 | |||
137 | int rowNdx, colNdx; | ||
138 | int smNdx = 0; | ||
139 | |||
140 | for (rowNdx = 0; rowNdx < numRows; rowNdx++) | ||
141 | { | ||
142 | List<Coord> row = new List<Coord>(numCols); | ||
143 | for (colNdx = 0; colNdx < numCols; colNdx++) | ||
144 | { | ||
145 | if (mirror) | ||
146 | row.Add(new Coord(-(redBytes[smNdx] * pixScale - 0.5f), (greenBytes[smNdx] * pixScale - 0.5f), blueBytes[smNdx] * pixScale - 0.5f)); | ||
147 | else | ||
148 | row.Add(new Coord(redBytes[smNdx] * pixScale - 0.5f, greenBytes[smNdx] * pixScale - 0.5f, blueBytes[smNdx] * pixScale - 0.5f)); | ||
149 | |||
150 | ++smNdx; | ||
151 | } | ||
152 | rows.Add(row); | ||
153 | } | ||
154 | return rows; | ||
155 | } | ||
156 | |||
157 | private Bitmap ScaleImage(Bitmap srcImage, int destWidth, int destHeight, | ||
158 | System.Drawing.Drawing2D.InterpolationMode interpMode) | ||
159 | { | ||
160 | Bitmap scaledImage = new Bitmap(srcImage, destWidth, destHeight); | ||
161 | scaledImage.SetResolution(96.0f, 96.0f); | ||
162 | |||
163 | Graphics grPhoto = Graphics.FromImage(scaledImage); | ||
164 | grPhoto.InterpolationMode = interpMode; | ||
165 | |||
166 | grPhoto.DrawImage(srcImage, | ||
167 | new Rectangle(0, 0, destWidth, destHeight), | ||
168 | new Rectangle(0, 0, srcImage.Width, srcImage.Height), | ||
169 | GraphicsUnit.Pixel); | ||
170 | |||
171 | grPhoto.Dispose(); | ||
172 | return scaledImage; | ||
173 | } | ||
174 | } | ||
175 | } | ||
176 | #endif | ||