aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Modules/World/WorldMap/MapImageModule.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Environment/Modules/World/WorldMap/MapImageModule.cs')
-rw-r--r--OpenSim/Region/Environment/Modules/World/WorldMap/MapImageModule.cs167
1 files changed, 167 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Modules/World/WorldMap/MapImageModule.cs b/OpenSim/Region/Environment/Modules/World/WorldMap/MapImageModule.cs
new file mode 100644
index 0000000..8ebe33e
--- /dev/null
+++ b/OpenSim/Region/Environment/Modules/World/WorldMap/MapImageModule.cs
@@ -0,0 +1,167 @@
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 Nini.Config;
31using OpenJPEGNet;
32using OpenSim.Region.Environment.Interfaces;
33using OpenSim.Region.Environment.Scenes;
34
35namespace OpenSim.Region.Environment.Modules.World.WorldMap
36{
37 internal class MapImageModule : IMapImageGenerator, IRegionModule
38 {
39 private Scene m_scene;
40
41 #region IMapImageGenerator Members
42
43 public byte[] WriteJpeg2000Image(string gradientmap)
44 {
45 byte[] imageData = null;
46
47 Bitmap bmp = TerrainToBitmap(gradientmap);
48
49 try
50 {
51 imageData = OpenJPEG.EncodeFromImage(bmp, true);
52 }
53 catch (Exception e) // LEGIT: Catching problems caused by OpenJPEG p/invoke
54 {
55 Console.WriteLine("Failed generating terrain map: " + e);
56 }
57
58 return imageData;
59 }
60
61 #endregion
62
63 #region IRegionModule Members
64
65 public void Initialise(Scene scene, IConfigSource source)
66 {
67 m_scene = scene;
68 m_scene.RegisterModuleInterface<IMapImageGenerator>(this);
69 }
70
71 public void PostInitialise()
72 {
73 }
74
75 public void Close()
76 {
77 }
78
79 public string Name
80 {
81 get { return "MapImageModule"; }
82 }
83
84 public bool IsSharedModule
85 {
86 get { return false; }
87 }
88
89 #endregion
90
91 private void ShadeBuildings(Bitmap map)
92 {
93 lock (map)
94 {
95 lock (m_scene.Entities)
96 {
97 foreach (EntityBase entity in m_scene.Entities.Values)
98 {
99 if (entity is SceneObjectGroup)
100 {
101 SceneObjectGroup sog = (SceneObjectGroup) entity;
102
103 foreach (SceneObjectPart primitive in sog.Children.Values)
104 {
105 int x = (int) (primitive.AbsolutePosition.X - (primitive.Scale.X / 2));
106 int y = (int) (primitive.AbsolutePosition.Y - (primitive.Scale.Y / 2));
107 int w = (int) primitive.Scale.X;
108 int h = (int) primitive.Scale.Y;
109
110 int dx;
111 for (dx = x; dx < x + w; dx++)
112 {
113 int dy;
114 for (dy = y; dy < y + h; dy++)
115 {
116 if (x < 0 || y < 0)
117 continue;
118 if (x >= map.Width || y >= map.Height)
119 continue;
120
121 map.SetPixel(dx, dy, Color.DarkGray);
122 }
123 }
124 }
125 }
126 }
127 }
128 }
129 }
130
131 private Bitmap TerrainToBitmap(string gradientmap)
132 {
133 Bitmap gradientmapLd = new Bitmap(gradientmap);
134
135 int pallete = gradientmapLd.Height;
136
137 Bitmap bmp = new Bitmap(m_scene.Heightmap.Width, m_scene.Heightmap.Height);
138 Color[] colours = new Color[pallete];
139
140 for (int i = 0; i < pallete; i++)
141 {
142 colours[i] = gradientmapLd.GetPixel(0, i);
143 }
144
145 lock (m_scene.Heightmap)
146 {
147 ITerrainChannel copy = m_scene.Heightmap;
148 for (int y = 0; y < copy.Height; y++)
149 {
150 for (int x = 0; x < copy.Width; x++)
151 {
152 // 512 is the largest possible height before colours clamp
153 int colorindex = (int) (Math.Max(Math.Min(1.0, copy[x, y] / 512.0), 0.0) * (pallete - 1));
154
155 // Handle error conditions
156 if (colorindex > pallete - 1 || colorindex < 0)
157 bmp.SetPixel(x, copy.Height - y - 1, Color.Red);
158 else
159 bmp.SetPixel(x, copy.Height - y - 1, colours[colorindex]);
160 }
161 }
162 ShadeBuildings(bmp);
163 return bmp;
164 }
165 }
166 }
167} \ No newline at end of file