MapImageModule.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.WorldMap
  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(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 = (int) (primitive.AbsolutePosition.X - (primitive.Scale.X / 2));
  89. int y = (int) (primitive.AbsolutePosition.Y - (primitive.Scale.Y / 2));
  90. int w = (int) primitive.Scale.X;
  91. int h = (int) primitive.Scale.Y;
  92. int dx;
  93. for (dx = x; dx < x + w; dx++)
  94. {
  95. int dy;
  96. for (dy = y; dy < y + h; dy++)
  97. {
  98. if (x < 0 || y < 0)
  99. continue;
  100. if (x >= map.Width || y >= map.Height)
  101. continue;
  102. map.SetPixel(dx, dy, Color.DarkGray);
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. private Bitmap TerrainToBitmap(string gradientmap)
  112. {
  113. Bitmap gradientmapLd = new Bitmap(gradientmap);
  114. int pallete = gradientmapLd.Height;
  115. Bitmap bmp = new Bitmap(m_scene.Heightmap.Width, m_scene.Heightmap.Height);
  116. Color[] colours = new Color[pallete];
  117. for (int i = 0; i < pallete; i++)
  118. {
  119. colours[i] = gradientmapLd.GetPixel(0, i);
  120. }
  121. lock (m_scene.Heightmap)
  122. {
  123. ITerrainChannel copy = m_scene.Heightmap;
  124. for (int y = 0; y < copy.Height; y++)
  125. {
  126. for (int x = 0; x < copy.Width; x++)
  127. {
  128. // 512 is the largest possible height before colours clamp
  129. int colorindex = (int) (Math.Max(Math.Min(1.0, copy[x, y] / 512.0), 0.0) * (pallete - 1));
  130. // Handle error conditions
  131. if (colorindex > pallete - 1 || colorindex < 0)
  132. bmp.SetPixel(x, copy.Height - y - 1, Color.Red);
  133. else
  134. bmp.SetPixel(x, copy.Height - y - 1, colours[colorindex]);
  135. }
  136. }
  137. ShadeBuildings(bmp);
  138. return bmp;
  139. }
  140. }
  141. }
  142. }