opensim – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 1 /*
2 * Copyright (c) Contributors, http://opensimulator.org/
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 using System;
29 using System.Drawing;
30 using System.Drawing.Imaging;
31 using System.IO;
32 using OpenSim.Region.Framework.Interfaces;
33 using OpenSim.Region.Framework.Scenes;
34  
35 namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
36 {
37 /// <summary>
38 /// A virtual class designed to have methods overloaded,
39 /// this class provides an interface for a generic image
40 /// saving and loading mechanism, but does not specify the
41 /// format. It should not be insubstantiated directly.
42 /// </summary>
43 public class GenericSystemDrawing : ITerrainLoader
44 {
45 #region ITerrainLoader Members
46  
47 public string FileExtension
48 {
49 get { return ".gsd"; }
50 }
51  
52 /// <summary>
53 /// Loads a file from a specified filename on the disk,
54 /// parses the image using the System.Drawing parsers
55 /// then returns a terrain channel. Values are
56 /// returned based on HSL brightness between 0m and 128m
57 /// </summary>
58 /// <param name="filename">The target image to load</param>
59 /// <returns>A terrain channel generated from the image.</returns>
60 public virtual ITerrainChannel LoadFile(string filename)
61 {
62 using (Bitmap b = new Bitmap(filename))
63 return LoadBitmap(b);
64 }
65  
66 public virtual ITerrainChannel LoadFile(string filename, int offsetX, int offsetY, int fileWidth, int fileHeight, int w, int h)
67 {
68 using (Bitmap bitmap = new Bitmap(filename))
69 {
70 ITerrainChannel retval = new TerrainChannel(w, h);
71  
72 for (int x = 0; x < retval.Width; x++)
73 {
74 for (int y = 0; y < retval.Height; y++)
75 {
76 retval[x, y] = bitmap.GetPixel(offsetX * retval.Width + x, (bitmap.Height - (retval.Height * (offsetY + 1))) + retval.Height - y - 1).GetBrightness() * 128;
77 }
78 }
79  
80 return retval;
81 }
82 }
83  
84 public virtual ITerrainChannel LoadStream(Stream stream)
85 {
86 using (Bitmap b = new Bitmap(stream))
87 return LoadBitmap(b);
88 }
89  
90 protected virtual ITerrainChannel LoadBitmap(Bitmap bitmap)
91 {
92 ITerrainChannel retval = new TerrainChannel(bitmap.Width, bitmap.Height);
93  
94 int x;
95 for (x = 0; x < bitmap.Width; x++)
96 {
97 int y;
98 for (y = 0; y < bitmap.Height; y++)
99 {
100 retval[x, y] = bitmap.GetPixel(x, bitmap.Height - y - 1).GetBrightness() * 128;
101 }
102 }
103  
104 return retval;
105 }
106  
107 /// <summary>
108 /// Exports a file to a image on the disk using a System.Drawing exporter.
109 /// </summary>
110 /// <param name="filename">The target filename</param>
111 /// <param name="map">The terrain channel being saved</param>
112 public virtual void SaveFile(string filename, ITerrainChannel map)
113 {
114 Bitmap colours = CreateGrayscaleBitmapFromMap(map);
115  
116 colours.Save(filename, ImageFormat.Png);
117 }
118  
119 /// <summary>
120 /// Exports a stream using a System.Drawing exporter.
121 /// </summary>
122 /// <param name="stream">The target stream</param>
123 /// <param name="map">The terrain channel being saved</param>
124 public virtual void SaveStream(Stream stream, ITerrainChannel map)
125 {
126 Bitmap colours = CreateGrayscaleBitmapFromMap(map);
127  
128 colours.Save(stream, ImageFormat.Png);
129 }
130  
131 public virtual void SaveFile(ITerrainChannel m_channel, string filename,
132 int offsetX, int offsetY,
133 int fileWidth, int fileHeight,
134 int regionSizeX, int regionSizeY)
135  
136 {
137 // We need to do this because:
138 // "Saving the image to the same file it was constructed from is not allowed and throws an exception."
139 string tempName = Path.GetTempFileName();
140  
141 Bitmap existingBitmap = null;
142 Bitmap thisBitmap = null;
143 Bitmap newBitmap = null;
144  
145 try
146 {
147 if (File.Exists(filename))
148 {
149 File.Copy(filename, tempName, true);
150 existingBitmap = new Bitmap(tempName);
151 if (existingBitmap.Width != fileWidth * regionSizeX || existingBitmap.Height != fileHeight * regionSizeY)
152 {
153 // old file, let's overwrite it
154 newBitmap = new Bitmap(fileWidth * regionSizeX, fileHeight * regionSizeY);
155 }
156 else
157 {
158 newBitmap = existingBitmap;
159 }
160 }
161 else
162 {
163 newBitmap = new Bitmap(fileWidth * regionSizeX, fileHeight * regionSizeY);
164 }
165  
166 thisBitmap = CreateGrayscaleBitmapFromMap(m_channel);
167 // Console.WriteLine("offsetX=" + offsetX + " offsetY=" + offsetY);
168 for (int x = 0; x < regionSizeX; x++)
169 for (int y = 0; y < regionSizeY; y++)
170 newBitmap.SetPixel(x + offsetX * regionSizeX, y + (fileHeight - 1 - offsetY) * regionSizeY, thisBitmap.GetPixel(x, y));
171  
172 Save(newBitmap, filename);
173 }
174 finally
175 {
176 if (existingBitmap != null)
177 existingBitmap.Dispose();
178  
179 if (thisBitmap != null)
180 thisBitmap.Dispose();
181  
182 if (newBitmap != null)
183 newBitmap.Dispose();
184  
185 if (File.Exists(tempName))
186 File.Delete(tempName);
187 }
188 }
189  
190 protected virtual void Save(Bitmap bmp, string filename)
191 {
192 bmp.Save(filename, ImageFormat.Png);
193 }
194  
195 #endregion
196  
197 public override string ToString()
198 {
199 return "SYS.DRAWING";
200 }
201  
202 //Returns true if this extension is supported for terrain save-tile
203 public virtual bool SupportsTileSave()
204 {
205 return false;
206 }
207  
208 /// <summary>
209 /// Protected method, generates a grayscale bitmap
210 /// image from a specified terrain channel.
211 /// </summary>
212 /// <param name="map">The terrain channel to export to bitmap</param>
213 /// <returns>A System.Drawing.Bitmap containing a grayscale image</returns>
214 protected static Bitmap CreateGrayscaleBitmapFromMap(ITerrainChannel map)
215 {
216 Bitmap bmp = new Bitmap(map.Width, map.Height);
217  
218 const int pallete = 256;
219  
220 Color[] grays = new Color[pallete];
221 for (int i = 0; i < grays.Length; i++)
222 {
223 grays[i] = Color.FromArgb(i, i, i);
224 }
225  
226 for (int y = 0; y < map.Height; y++)
227 {
228 for (int x = 0; x < map.Width; x++)
229 {
230 // 512 is the largest possible height before colours clamp
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  
255 using (Bitmap gradientmapLd = new Bitmap("defaultstripe.png"))
256 {
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  
268 for (int y = 0; y < map.Height; y++)
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  
275 // Handle error conditions
276 if (colorindex > pallete - 1 || colorindex < 0)
277 bmp.SetPixel(x, map.Height - y - 1, Color.Red);
278 else
279 bmp.SetPixel(x, map.Height - y - 1, colours[colorindex]);
280 }
281 }
282 return bmp;
283 }
284 }
285 }