aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs')
-rw-r--r--OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs195
1 files changed, 195 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs
new file mode 100644
index 0000000..477c73c
--- /dev/null
+++ b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs
@@ -0,0 +1,195 @@
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 OpenSim 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
28using System;
29using System.Drawing;
30using System.Drawing.Imaging;
31using System.IO;
32using OpenSim.Region.Framework.Interfaces;
33using OpenSim.Region.Framework.Scenes;
34
35namespace 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 return LoadBitmap(new Bitmap(filename));
63 }
64
65 public ITerrainChannel LoadFile(string filename, int x, int y, int fileWidth, int fileHeight, int w, int h)
66 {
67 throw new NotImplementedException();
68 }
69
70 public virtual ITerrainChannel LoadStream(Stream stream)
71 {
72 return LoadBitmap(new Bitmap(stream));
73 }
74
75 protected virtual ITerrainChannel LoadBitmap(Bitmap bitmap)
76 {
77 ITerrainChannel retval = new TerrainChannel(bitmap.Width, bitmap.Height);
78
79 int x;
80 for (x = 0; x < bitmap.Width; x++)
81 {
82 int y;
83 for (y = 0; y < bitmap.Height; y++)
84 {
85 retval[x, y] = bitmap.GetPixel(x, bitmap.Height - y - 1).GetBrightness() * 128;
86 }
87 }
88
89 return retval;
90 }
91
92 /// <summary>
93 /// Exports a file to a image on the disk using a System.Drawing exporter.
94 /// </summary>
95 /// <param name="filename">The target filename</param>
96 /// <param name="map">The terrain channel being saved</param>
97 public virtual void SaveFile(string filename, ITerrainChannel map)
98 {
99 Bitmap colours = CreateGrayscaleBitmapFromMap(map);
100
101 colours.Save(filename, ImageFormat.Png);
102 }
103
104 /// <summary>
105 /// Exports a stream using a System.Drawing exporter.
106 /// </summary>
107 /// <param name="stream">The target stream</param>
108 /// <param name="map">The terrain channel being saved</param>
109 public virtual void SaveStream(Stream stream, ITerrainChannel map)
110 {
111 Bitmap colours = CreateGrayscaleBitmapFromMap(map);
112
113 colours.Save(stream, ImageFormat.Png);
114 }
115
116 #endregion
117
118 public override string ToString()
119 {
120 return "SYS.DRAWING";
121 }
122
123 /// <summary>
124 /// Protected method, generates a grayscale bitmap
125 /// image from a specified terrain channel.
126 /// </summary>
127 /// <param name="map">The terrain channel to export to bitmap</param>
128 /// <returns>A System.Drawing.Bitmap containing a grayscale image</returns>
129 protected static Bitmap CreateGrayscaleBitmapFromMap(ITerrainChannel map)
130 {
131 Bitmap bmp = new Bitmap(map.Width, map.Height);
132
133 const int pallete = 256;
134
135 Color[] grays = new Color[pallete];
136 for (int i = 0; i < grays.Length; i++)
137 {
138 grays[i] = Color.FromArgb(i, i, i);
139 }
140
141 for (int y = 0; y < map.Height; y++)
142 {
143 for (int x = 0; x < map.Width; x++)
144 {
145 // 512 is the largest possible height before colours clamp
146 int colorindex = (int) (Math.Max(Math.Min(1.0, map[x, y] / 128.0), 0.0) * (pallete - 1));
147
148 // Handle error conditions
149 if (colorindex > pallete - 1 || colorindex < 0)
150 bmp.SetPixel(x, map.Height - y - 1, Color.Red);
151 else
152 bmp.SetPixel(x, map.Height - y - 1, grays[colorindex]);
153 }
154 }
155 return bmp;
156 }
157
158 /// <summary>
159 /// Protected method, generates a coloured bitmap
160 /// image from a specified terrain channel.
161 /// </summary>
162 /// <param name="map">The terrain channel to export to bitmap</param>
163 /// <returns>A System.Drawing.Bitmap containing a coloured image</returns>
164 protected static Bitmap CreateBitmapFromMap(ITerrainChannel map)
165 {
166 Bitmap gradientmapLd = new Bitmap("defaultstripe.png");
167
168 int pallete = gradientmapLd.Height;
169
170 Bitmap bmp = new Bitmap(map.Width, map.Height);
171 Color[] colours = new Color[pallete];
172
173 for (int i = 0; i < pallete; i++)
174 {
175 colours[i] = gradientmapLd.GetPixel(0, i);
176 }
177
178 for (int y = 0; y < map.Height; y++)
179 {
180 for (int x = 0; x < map.Width; x++)
181 {
182 // 512 is the largest possible height before colours clamp
183 int colorindex = (int) (Math.Max(Math.Min(1.0, map[x, y] / 512.0), 0.0) * (pallete - 1));
184
185 // Handle error conditions
186 if (colorindex > pallete - 1 || colorindex < 0)
187 bmp.SetPixel(x, map.Height - y - 1, Color.Red);
188 else
189 bmp.SetPixel(x, map.Height - y - 1, colours[colorindex]);
190 }
191 }
192 return bmp;
193 }
194 }
195}