aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Modules/Terrain/MapImageModule.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Environment/Modules/Terrain/MapImageModule.cs')
-rw-r--r--OpenSim/Region/Environment/Modules/Terrain/MapImageModule.cs94
1 files changed, 94 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Modules/Terrain/MapImageModule.cs b/OpenSim/Region/Environment/Modules/Terrain/MapImageModule.cs
new file mode 100644
index 0000000..01695de
--- /dev/null
+++ b/OpenSim/Region/Environment/Modules/Terrain/MapImageModule.cs
@@ -0,0 +1,94 @@
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using libsecondlife;
5using Nini.Config;
6using OpenSim.Framework;
7using OpenSim.Region.Environment.Interfaces;
8using OpenSim.Region.Environment.Scenes;
9using OpenSim.Region.Environment.Modules.ModuleFramework;
10
11namespace OpenSim.Region.Environment.Modules.Terrain
12{
13 class MapImageModule : ITerrainTemp, IRegionModule
14 {
15 private Scene m_scene;
16 #region IRegionModule Members
17
18 public void Initialise(Scene scene, IConfigSource source)
19 {
20 m_scene = scene;
21 m_scene.RegisterModuleInterface<ITerrainTemp>(this);
22 }
23
24 public void PostInitialise()
25 {
26 }
27
28 public void Close()
29 {
30 }
31
32 public string Name
33 {
34 get { return "MapImageModule"; }
35 }
36
37 public bool IsSharedModule
38 {
39 get { return false; }
40 }
41
42
43 public byte[] WriteJpegImage(string gradientmap)
44 {
45 byte[] imageData = null;
46 try
47 {
48 Bitmap bmp = TerrainToBitmap(gradientmap);
49
50 imageData = OpenJPEGNet.OpenJPEG.EncodeFromImage(bmp, true);
51
52 }
53 catch (Exception e) // LEGIT: Catching problems caused by OpenJPEG p/invoke
54 {
55 Console.WriteLine("Failed generating terrain map: " + e.ToString());
56 }
57
58 return imageData;
59 }
60
61 private Bitmap TerrainToBitmap(string gradientmap)
62 {
63 Bitmap gradientmapLd = new Bitmap(gradientmap);
64
65 int pallete = gradientmapLd.Height;
66
67 Bitmap bmp = new Bitmap(m_scene.Heightmap.Width, m_scene.Heightmap.Height);
68 Color[] colours = new Color[pallete];
69
70 for (int i = 0; i < pallete; i++)
71 {
72 colours[i] = gradientmapLd.GetPixel(0, i);
73 }
74
75 lock (m_scene.Heightmap)
76 {
77 ITerrainChannel copy = m_scene.Heightmap;
78 for (int y = 0; y < copy.Height; y++)
79 {
80 for (int x = 0; x < copy.Width; x++)
81 {
82 // 512 is the largest possible height before colours clamp
83 int colorindex = (int)(Math.Max(Math.Min(1.0, copy[x, y] / 512.0), 0.0) * (pallete - 1));
84 bmp.SetPixel(x, copy.Height - y - 1, colours[colorindex]);
85 }
86 }
87 }
88 return bmp;
89 }
90
91
92 #endregion
93 }
94}