aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Modules/World/Terrain/FileLoaders/GenericSystemDrawing.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Environment/Modules/World/Terrain/FileLoaders/GenericSystemDrawing.cs')
-rw-r--r--OpenSim/Region/Environment/Modules/World/Terrain/FileLoaders/GenericSystemDrawing.cs342
1 files changed, 171 insertions, 171 deletions
diff --git a/OpenSim/Region/Environment/Modules/World/Terrain/FileLoaders/GenericSystemDrawing.cs b/OpenSim/Region/Environment/Modules/World/Terrain/FileLoaders/GenericSystemDrawing.cs
index b5e6bd9..9730b16 100644
--- a/OpenSim/Region/Environment/Modules/World/Terrain/FileLoaders/GenericSystemDrawing.cs
+++ b/OpenSim/Region/Environment/Modules/World/Terrain/FileLoaders/GenericSystemDrawing.cs
@@ -1,172 +1,172 @@
1/* 1/*
2 * Copyright (c) Contributors, http://opensimulator.org/ 2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders. 3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met: 6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright 7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright 9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSim Project nor the 12 * * Neither the name of the OpenSim Project nor the
13 * names of its contributors may be used to endorse or promote products 13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission. 14 * derived from this software without specific prior written permission.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY 16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY 19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 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 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 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 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. 25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */ 26 */
27 27
28using System; 28using System;
29using System.Drawing; 29using System.Drawing;
30using System.Drawing.Imaging; 30using System.Drawing.Imaging;
31using OpenSim.Region.Environment.Interfaces; 31using OpenSim.Region.Environment.Interfaces;
32 32
33namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders 33namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
34{ 34{
35 /// <summary> 35 /// <summary>
36 /// A virtual class designed to have methods overloaded, 36 /// A virtual class designed to have methods overloaded,
37 /// this class provides an interface for a generic image 37 /// this class provides an interface for a generic image
38 /// saving and loading mechanism, but does not specify the 38 /// saving and loading mechanism, but does not specify the
39 /// format. It should not be insubstantiated directly. 39 /// format. It should not be insubstantiated directly.
40 /// </summary> 40 /// </summary>
41 public class GenericSystemDrawing : ITerrainLoader 41 public class GenericSystemDrawing : ITerrainLoader
42 { 42 {
43 #region ITerrainLoader Members 43 #region ITerrainLoader Members
44 44
45 public string FileExtension 45 public string FileExtension
46 { 46 {
47 get { return ".gsd"; } 47 get { return ".gsd"; }
48 } 48 }
49 49
50 /// <summary> 50 /// <summary>
51 /// Loads a file from a specified filename on the disk, 51 /// Loads a file from a specified filename on the disk,
52 /// parses the image using the System.Drawing parsers 52 /// parses the image using the System.Drawing parsers
53 /// then returns a terrain channel. Values are 53 /// then returns a terrain channel. Values are
54 /// returned based on HSL brightness between 0m and 128m 54 /// returned based on HSL brightness between 0m and 128m
55 /// </summary> 55 /// </summary>
56 /// <param name="filename">The target image to load</param> 56 /// <param name="filename">The target image to load</param>
57 /// <returns>A terrain channel generated from the image.</returns> 57 /// <returns>A terrain channel generated from the image.</returns>
58 public virtual ITerrainChannel LoadFile(string filename) 58 public virtual ITerrainChannel LoadFile(string filename)
59 { 59 {
60 Bitmap file = new Bitmap(filename); 60 Bitmap file = new Bitmap(filename);
61 61
62 ITerrainChannel retval = new TerrainChannel(file.Width, file.Height); 62 ITerrainChannel retval = new TerrainChannel(file.Width, file.Height);
63 63
64 int x, y; 64 int x, y;
65 for (x = 0; x < file.Width; x++) 65 for (x = 0; x < file.Width; x++)
66 { 66 {
67 for (y = 0; y < file.Height; y++) 67 for (y = 0; y < file.Height; y++)
68 { 68 {
69 retval[x, y] = file.GetPixel(x, y).GetBrightness() * 128; 69 retval[x, y] = file.GetPixel(x, y).GetBrightness() * 128;
70 } 70 }
71 } 71 }
72 72
73 return retval; 73 return retval;
74 } 74 }
75 75
76 public ITerrainChannel LoadFile(string filename, int x, int y, int fileWidth, int fileHeight, int w, int h) 76 public ITerrainChannel LoadFile(string filename, int x, int y, int fileWidth, int fileHeight, int w, int h)
77 { 77 {
78 throw new NotImplementedException(); 78 throw new NotImplementedException();
79 } 79 }
80 80
81 /// <summary> 81 /// <summary>
82 /// Exports a file to a image on the disk using a System.Drawing exporter. 82 /// Exports a file to a image on the disk using a System.Drawing exporter.
83 /// </summary> 83 /// </summary>
84 /// <param name="filename">The target filename</param> 84 /// <param name="filename">The target filename</param>
85 /// <param name="map">The terrain channel being saved</param> 85 /// <param name="map">The terrain channel being saved</param>
86 public virtual void SaveFile(string filename, ITerrainChannel map) 86 public virtual void SaveFile(string filename, ITerrainChannel map)
87 { 87 {
88 Bitmap colours = CreateGrayscaleBitmapFromMap(map); 88 Bitmap colours = CreateGrayscaleBitmapFromMap(map);
89 89
90 colours.Save(filename, ImageFormat.Png); 90 colours.Save(filename, ImageFormat.Png);
91 } 91 }
92 92
93 #endregion 93 #endregion
94 94
95 public override string ToString() 95 public override string ToString()
96 { 96 {
97 return "SYS.DRAWING"; 97 return "SYS.DRAWING";
98 } 98 }
99 99
100 /// <summary> 100 /// <summary>
101 /// Protected method, generates a grayscale bitmap 101 /// Protected method, generates a grayscale bitmap
102 /// image from a specified terrain channel. 102 /// image from a specified terrain channel.
103 /// </summary> 103 /// </summary>
104 /// <param name="map">The terrain channel to export to bitmap</param> 104 /// <param name="map">The terrain channel to export to bitmap</param>
105 /// <returns>A System.Drawing.Bitmap containing a grayscale image</returns> 105 /// <returns>A System.Drawing.Bitmap containing a grayscale image</returns>
106 protected Bitmap CreateGrayscaleBitmapFromMap(ITerrainChannel map) 106 protected Bitmap CreateGrayscaleBitmapFromMap(ITerrainChannel map)
107 { 107 {
108 Bitmap bmp = new Bitmap(map.Width, map.Height); 108 Bitmap bmp = new Bitmap(map.Width, map.Height);
109 109
110 int pallete = 256; 110 int pallete = 256;
111 111
112 Color[] grays = new Color[pallete]; 112 Color[] grays = new Color[pallete];
113 for (int i = 0; i < grays.Length; i++) 113 for (int i = 0; i < grays.Length; i++)
114 { 114 {
115 grays[i] = Color.FromArgb(i, i, i); 115 grays[i] = Color.FromArgb(i, i, i);
116 } 116 }
117 117
118 for (int y = 0; y < map.Height; y++) 118 for (int y = 0; y < map.Height; y++)
119 { 119 {
120 for (int x = 0; x < map.Width; x++) 120 for (int x = 0; x < map.Width; x++)
121 { 121 {
122 // 512 is the largest possible height before colours clamp 122 // 512 is the largest possible height before colours clamp
123 int colorindex = (int) (Math.Max(Math.Min(1.0, map[x, y] / 128.0), 0.0) * (pallete - 1)); 123 int colorindex = (int) (Math.Max(Math.Min(1.0, map[x, y] / 128.0), 0.0) * (pallete - 1));
124 124
125 // Handle error conditions 125 // Handle error conditions
126 if (colorindex > pallete - 1 || colorindex < 0) 126 if (colorindex > pallete - 1 || colorindex < 0)
127 bmp.SetPixel(x, map.Height - y - 1, Color.Red); 127 bmp.SetPixel(x, map.Height - y - 1, Color.Red);
128 else 128 else
129 bmp.SetPixel(x, map.Height - y - 1, grays[colorindex]); 129 bmp.SetPixel(x, map.Height - y - 1, grays[colorindex]);
130 } 130 }
131 } 131 }
132 return bmp; 132 return bmp;
133 } 133 }
134 134
135 /// <summary> 135 /// <summary>
136 /// Protected method, generates a coloured bitmap 136 /// Protected method, generates a coloured bitmap
137 /// image from a specified terrain channel. 137 /// image from a specified terrain channel.
138 /// </summary> 138 /// </summary>
139 /// <param name="map">The terrain channel to export to bitmap</param> 139 /// <param name="map">The terrain channel to export to bitmap</param>
140 /// <returns>A System.Drawing.Bitmap containing a coloured image</returns> 140 /// <returns>A System.Drawing.Bitmap containing a coloured image</returns>
141 protected Bitmap CreateBitmapFromMap(ITerrainChannel map) 141 protected Bitmap CreateBitmapFromMap(ITerrainChannel map)
142 { 142 {
143 Bitmap gradientmapLd = new Bitmap("defaultstripe.png"); 143 Bitmap gradientmapLd = new Bitmap("defaultstripe.png");
144 144
145 int pallete = gradientmapLd.Height; 145 int pallete = gradientmapLd.Height;
146 146
147 Bitmap bmp = new Bitmap(map.Width, map.Height); 147 Bitmap bmp = new Bitmap(map.Width, map.Height);
148 Color[] colours = new Color[pallete]; 148 Color[] colours = new Color[pallete];
149 149
150 for (int i = 0; i < pallete; i++) 150 for (int i = 0; i < pallete; i++)
151 { 151 {
152 colours[i] = gradientmapLd.GetPixel(0, i); 152 colours[i] = gradientmapLd.GetPixel(0, i);
153 } 153 }
154 154
155 for (int y = 0; y < map.Height; y++) 155 for (int y = 0; y < map.Height; y++)
156 { 156 {
157 for (int x = 0; x < map.Width; x++) 157 for (int x = 0; x < map.Width; x++)
158 { 158 {
159 // 512 is the largest possible height before colours clamp 159 // 512 is the largest possible height before colours clamp
160 int colorindex = (int) (Math.Max(Math.Min(1.0, map[x, y] / 512.0), 0.0) * (pallete - 1)); 160 int colorindex = (int) (Math.Max(Math.Min(1.0, map[x, y] / 512.0), 0.0) * (pallete - 1));
161 161
162 // Handle error conditions 162 // Handle error conditions
163 if (colorindex > pallete - 1 || colorindex < 0) 163 if (colorindex > pallete - 1 || colorindex < 0)
164 bmp.SetPixel(x, map.Height - y - 1, Color.Red); 164 bmp.SetPixel(x, map.Height - y - 1, Color.Red);
165 else 165 else
166 bmp.SetPixel(x, map.Height - y - 1, colours[colorindex]); 166 bmp.SetPixel(x, map.Height - y - 1, colours[colorindex]);
167 } 167 }
168 } 168 }
169 return bmp; 169 return bmp;
170 } 170 }
171 } 171 }
172} \ No newline at end of file 172} \ No newline at end of file