diff options
Diffstat (limited to 'OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs')
-rw-r--r-- | OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs | 81 |
1 files changed, 23 insertions, 58 deletions
diff --git a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs index d5c77ec..bcd9dcd 100644 --- a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs +++ b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs | |||
@@ -59,7 +59,7 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
59 | /// <returns>A terrain channel generated from the image.</returns> | 59 | /// <returns>A terrain channel generated from the image.</returns> |
60 | public virtual ITerrainChannel LoadFile(string filename) | 60 | public virtual ITerrainChannel LoadFile(string filename) |
61 | { | 61 | { |
62 | using (Bitmap b = new Bitmap(filename)) | 62 | using(Bitmap b = new Bitmap(filename)) |
63 | return LoadBitmap(b); | 63 | return LoadBitmap(b); |
64 | } | 64 | } |
65 | 65 | ||
@@ -111,9 +111,8 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
111 | /// <param name="map">The terrain channel being saved</param> | 111 | /// <param name="map">The terrain channel being saved</param> |
112 | public virtual void SaveFile(string filename, ITerrainChannel map) | 112 | public virtual void SaveFile(string filename, ITerrainChannel map) |
113 | { | 113 | { |
114 | Bitmap colours = CreateGrayscaleBitmapFromMap(map); | 114 | using(Bitmap colours = CreateGrayscaleBitmapFromMap(map)) |
115 | 115 | colours.Save(filename,ImageFormat.Png); | |
116 | colours.Save(filename, ImageFormat.Png); | ||
117 | } | 116 | } |
118 | 117 | ||
119 | /// <summary> | 118 | /// <summary> |
@@ -123,12 +122,11 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
123 | /// <param name="map">The terrain channel being saved</param> | 122 | /// <param name="map">The terrain channel being saved</param> |
124 | public virtual void SaveStream(Stream stream, ITerrainChannel map) | 123 | public virtual void SaveStream(Stream stream, ITerrainChannel map) |
125 | { | 124 | { |
126 | Bitmap colours = CreateGrayscaleBitmapFromMap(map); | 125 | using(Bitmap colours = CreateGrayscaleBitmapFromMap(map)) |
127 | 126 | colours.Save(stream,ImageFormat.Png); | |
128 | colours.Save(stream, ImageFormat.Png); | ||
129 | } | 127 | } |
130 | 128 | ||
131 | public virtual void SaveFile(ITerrainChannel m_channel, string filename, | 129 | public virtual void SaveFile(ITerrainChannel m_channel, string filename, |
132 | int offsetX, int offsetY, | 130 | int offsetX, int offsetY, |
133 | int fileWidth, int fileHeight, | 131 | int fileWidth, int fileHeight, |
134 | int regionSizeX, int regionSizeY) | 132 | int regionSizeX, int regionSizeY) |
@@ -162,13 +160,13 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
162 | { | 160 | { |
163 | newBitmap = new Bitmap(fileWidth * regionSizeX, fileHeight * regionSizeY); | 161 | newBitmap = new Bitmap(fileWidth * regionSizeX, fileHeight * regionSizeY); |
164 | } | 162 | } |
165 | 163 | ||
166 | thisBitmap = CreateGrayscaleBitmapFromMap(m_channel); | 164 | thisBitmap = CreateGrayscaleBitmapFromMap(m_channel); |
167 | // Console.WriteLine("offsetX=" + offsetX + " offsetY=" + offsetY); | 165 | // Console.WriteLine("offsetX=" + offsetX + " offsetY=" + offsetY); |
168 | for (int x = 0; x < regionSizeX; x++) | 166 | for (int x = 0; x < regionSizeX; x++) |
169 | for (int y = 0; y < regionSizeY; y++) | 167 | for (int y = 0; y < regionSizeY; y++) |
170 | newBitmap.SetPixel(x + offsetX * regionSizeX, y + (fileHeight - 1 - offsetY) * regionSizeY, thisBitmap.GetPixel(x, y)); | 168 | newBitmap.SetPixel(x + offsetX * regionSizeX, y + (fileHeight - 1 - offsetY) * regionSizeY, thisBitmap.GetPixel(x, y)); |
171 | 169 | ||
172 | Save(newBitmap, filename); | 170 | Save(newBitmap, filename); |
173 | } | 171 | } |
174 | finally | 172 | finally |
@@ -213,8 +211,10 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
213 | /// <returns>A System.Drawing.Bitmap containing a grayscale image</returns> | 211 | /// <returns>A System.Drawing.Bitmap containing a grayscale image</returns> |
214 | protected static Bitmap CreateGrayscaleBitmapFromMap(ITerrainChannel map) | 212 | protected static Bitmap CreateGrayscaleBitmapFromMap(ITerrainChannel map) |
215 | { | 213 | { |
214 | // Bitmap bmp = new Bitmap(map.Width, map.Height, PixelFormat.Format24bppRgb); | ||
216 | Bitmap bmp = new Bitmap(map.Width, map.Height); | 215 | Bitmap bmp = new Bitmap(map.Width, map.Height); |
217 | 216 | ||
217 | |||
218 | const int pallete = 256; | 218 | const int pallete = 256; |
219 | 219 | ||
220 | Color[] grays = new Color[pallete]; | 220 | Color[] grays = new Color[pallete]; |
@@ -227,59 +227,24 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
227 | { | 227 | { |
228 | for (int x = 0; x < map.Width; x++) | 228 | for (int x = 0; x < map.Width; x++) |
229 | { | 229 | { |
230 | // 512 is the largest possible height before colours clamp | 230 | // to change this, loading also needs change |
231 | int colorindex = (int) (Math.Max(Math.Min(1.0, map[x, y] / 128.0), 0.0) * (pallete - 1)); | ||
232 | |||
233 | // Handle error conditions | ||
234 | if (colorindex > pallete - 1 || colorindex < 0) | ||
235 | bmp.SetPixel(x, map.Height - y - 1, Color.Red); | ||
236 | else | ||
237 | bmp.SetPixel(x, map.Height - y - 1, grays[colorindex]); | ||
238 | } | ||
239 | } | ||
240 | return bmp; | ||
241 | } | ||
242 | |||
243 | /// <summary> | ||
244 | /// Protected method, generates a coloured bitmap | ||
245 | /// image from a specified terrain channel. | ||
246 | /// </summary> | ||
247 | /// <param name="map">The terrain channel to export to bitmap</param> | ||
248 | /// <returns>A System.Drawing.Bitmap containing a coloured image</returns> | ||
249 | protected static Bitmap CreateBitmapFromMap(ITerrainChannel map) | ||
250 | { | ||
251 | int pallete; | ||
252 | Bitmap bmp; | ||
253 | Color[] colours; | ||
254 | 231 | ||
255 | using (Bitmap gradientmapLd = new Bitmap("defaultstripe.png")) | 232 | // int colorindex = (int)map[x, y]; // one to one conversion 0 - 255m range |
256 | { | 233 | // int colorindex = (int)map[x, y] / 2; // 0 - 510 range |
257 | pallete = gradientmapLd.Height; | ||
258 | |||
259 | bmp = new Bitmap(map.Width, map.Height); | ||
260 | colours = new Color[pallete]; | ||
261 | |||
262 | for (int i = 0; i < pallete; i++) | ||
263 | { | ||
264 | colours[i] = gradientmapLd.GetPixel(0, i); | ||
265 | } | ||
266 | } | ||
267 | 234 | ||
268 | for (int y = 0; y < map.Height; y++) | 235 | int colorindex = (int)map[x, y] * 2; // the original 0 - 127.5 range |
269 | { | ||
270 | for (int x = 0; x < map.Width; x++) | ||
271 | { | ||
272 | // 512 is the largest possible height before colours clamp | ||
273 | int colorindex = (int) (Math.Max(Math.Min(1.0, map[x, y] / 512.0), 0.0) * (pallete - 1)); | ||
274 | 236 | ||
275 | // Handle error conditions | 237 | // clamp it not adding the red warning |
276 | if (colorindex > pallete - 1 || colorindex < 0) | 238 | if (colorindex < 0) |
277 | bmp.SetPixel(x, map.Height - y - 1, Color.Red); | 239 | colorindex = 0; |
278 | else | 240 | else if (colorindex >= pallete) |
279 | bmp.SetPixel(x, map.Height - y - 1, colours[colorindex]); | 241 | colorindex = pallete - 1; |
242 | bmp.SetPixel(x, map.Height - y - 1, grays[colorindex]); | ||
280 | } | 243 | } |
281 | } | 244 | } |
282 | return bmp; | 245 | return bmp; |
283 | } | 246 | } |
284 | } | 247 | } |
285 | } | 248 | } |
249 | |||
250 | |||