MapImageModule.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. using System;
  28. using System.Drawing;
  29. using Nini.Config;
  30. using OpenJPEGNet;
  31. using OpenSim.Region.Environment.Interfaces;
  32. using OpenSim.Region.Environment.Scenes;
  33. namespace OpenSim.Region.Environment.Modules.World.Terrain
  34. {
  35. internal class MapImageModule : IMapImageGenerator, IRegionModule
  36. {
  37. private Scene m_scene;
  38. #region IMapImageGenerator Members
  39. public byte[] WriteJpeg2000Image(string gradientmap)
  40. {
  41. byte[] imageData = null;
  42. Bitmap bmp = TerrainToBitmap(gradientmap);
  43. try
  44. {
  45. imageData = OpenJPEG.EncodeFromImage(bmp, true);
  46. }
  47. catch (Exception e) // LEGIT: Catching problems caused by OpenJPEG p/invoke
  48. {
  49. Console.WriteLine("Failed generating terrain map: " + e);
  50. }
  51. return imageData;
  52. }
  53. #endregion
  54. #region IRegionModule Members
  55. public void Initialise(Scene scene, IConfigSource source)
  56. {
  57. m_scene = scene;
  58. m_scene.RegisterModuleInterface<IMapImageGenerator>(this);
  59. }
  60. public void PostInitialise()
  61. {
  62. }
  63. public void Close()
  64. {
  65. }
  66. public string Name
  67. {
  68. get { return "MapImageModule"; }
  69. }
  70. public bool IsSharedModule
  71. {
  72. get { return false; }
  73. }
  74. #endregion
  75. private void ShadeBuildings(ref Bitmap map)
  76. {
  77. lock (map)
  78. {
  79. lock (m_scene.Entities)
  80. {
  81. foreach (EntityBase entity in m_scene.Entities.Values)
  82. {
  83. if (entity is SceneObjectGroup)
  84. {
  85. SceneObjectGroup sog = (SceneObjectGroup) entity;
  86. foreach (SceneObjectPart primitive in sog.Children.Values)
  87. {
  88. int x, y, w, h;
  89. x = (int) (primitive.AbsolutePosition.X - (primitive.Scale.X / 2));
  90. y = (int) (primitive.AbsolutePosition.Y - (primitive.Scale.Y / 2));
  91. w = (int) primitive.Scale.X;
  92. h = (int) primitive.Scale.Y;
  93. int dx;
  94. for (dx = x; dx < x + w; dx++)
  95. {
  96. int dy;
  97. for (dy = y; dy < y + h; dy++)
  98. {
  99. if (x < 0 || y < 0)
  100. continue;
  101. if (x >= map.Width || y >= map.Height)
  102. continue;
  103. map.SetPixel(dx, dy, Color.DarkGray);
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. private Bitmap TerrainToBitmap(string gradientmap)
  113. {
  114. Bitmap gradientmapLd = new Bitmap(gradientmap);
  115. int pallete = gradientmapLd.Height;
  116. Bitmap bmp = new Bitmap(m_scene.Heightmap.Width, m_scene.Heightmap.Height);
  117. Color[] colours = new Color[pallete];
  118. for (int i = 0; i < pallete; i++)
  119. {
  120. colours[i] = gradientmapLd.GetPixel(0, i);
  121. }
  122. lock (m_scene.Heightmap)
  123. {
  124. ITerrainChannel copy = m_scene.Heightmap;
  125. for (int y = 0; y < copy.Height; y++)
  126. {
  127. for (int x = 0; x < copy.Width; x++)
  128. {
  129. // 512 is the largest possible height before colours clamp
  130. int colorindex = (int) (Math.Max(Math.Min(1.0, copy[x, y] / 512.0), 0.0) * (pallete - 1));
  131. // Handle error conditions
  132. if (colorindex > pallete - 1 || colorindex < 0)
  133. bmp.SetPixel(x, copy.Height - y - 1, Color.Red);
  134. else
  135. bmp.SetPixel(x, copy.Height - y - 1, colours[colorindex]);
  136. }
  137. }
  138. ShadeBuildings(ref bmp);
  139. return bmp;
  140. }
  141. }
  142. }
  143. }