GenericSystemDrawing.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 OpenSimulator 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 System.Drawing.Imaging;
  30. using System.IO;
  31. using OpenSim.Region.Framework.Interfaces;
  32. using OpenSim.Region.Framework.Scenes;
  33. namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
  34. {
  35. /// <summary>
  36. /// A virtual class designed to have methods overloaded,
  37. /// this class provides an interface for a generic image
  38. /// saving and loading mechanism, but does not specify the
  39. /// format. It should not be insubstantiated directly.
  40. /// </summary>
  41. public class GenericSystemDrawing : ITerrainLoader
  42. {
  43. #region ITerrainLoader Members
  44. public string FileExtension
  45. {
  46. get { return ".gsd"; }
  47. }
  48. /// <summary>
  49. /// Loads a file from a specified filename on the disk,
  50. /// parses the image using the System.Drawing parsers
  51. /// then returns a terrain channel. Values are
  52. /// returned based on HSL brightness between 0m and 128m
  53. /// </summary>
  54. /// <param name="filename">The target image to load</param>
  55. /// <returns>A terrain channel generated from the image.</returns>
  56. public virtual ITerrainChannel LoadFile(string filename)
  57. {
  58. return LoadBitmap(new Bitmap(filename));
  59. }
  60. public ITerrainChannel LoadFile(string filename, int x, int y, int fileWidth, int fileHeight, int w, int h)
  61. {
  62. throw new NotImplementedException();
  63. }
  64. public virtual ITerrainChannel LoadStream(Stream stream)
  65. {
  66. return LoadBitmap(new Bitmap(stream));
  67. }
  68. protected virtual ITerrainChannel LoadBitmap(Bitmap bitmap)
  69. {
  70. ITerrainChannel retval = new TerrainChannel(bitmap.Width, bitmap.Height);
  71. int x;
  72. for (x = 0; x < bitmap.Width; x++)
  73. {
  74. int y;
  75. for (y = 0; y < bitmap.Height; y++)
  76. {
  77. retval[x, y] = bitmap.GetPixel(x, bitmap.Height - y - 1).GetBrightness() * 128;
  78. }
  79. }
  80. return retval;
  81. }
  82. /// <summary>
  83. /// Exports a file to a image on the disk using a System.Drawing exporter.
  84. /// </summary>
  85. /// <param name="filename">The target filename</param>
  86. /// <param name="map">The terrain channel being saved</param>
  87. public virtual void SaveFile(string filename, ITerrainChannel map)
  88. {
  89. Bitmap colours = CreateGrayscaleBitmapFromMap(map);
  90. colours.Save(filename, ImageFormat.Png);
  91. }
  92. /// <summary>
  93. /// Exports a stream using a System.Drawing exporter.
  94. /// </summary>
  95. /// <param name="stream">The target stream</param>
  96. /// <param name="map">The terrain channel being saved</param>
  97. public virtual void SaveStream(Stream stream, ITerrainChannel map)
  98. {
  99. Bitmap colours = CreateGrayscaleBitmapFromMap(map);
  100. colours.Save(stream, ImageFormat.Png);
  101. }
  102. #endregion
  103. public override string ToString()
  104. {
  105. return "SYS.DRAWING";
  106. }
  107. /// <summary>
  108. /// Protected method, generates a grayscale bitmap
  109. /// image from a specified terrain channel.
  110. /// </summary>
  111. /// <param name="map">The terrain channel to export to bitmap</param>
  112. /// <returns>A System.Drawing.Bitmap containing a grayscale image</returns>
  113. protected static Bitmap CreateGrayscaleBitmapFromMap(ITerrainChannel map)
  114. {
  115. Bitmap bmp = new Bitmap(map.Width, map.Height);
  116. const int pallete = 256;
  117. Color[] grays = new Color[pallete];
  118. for (int i = 0; i < grays.Length; i++)
  119. {
  120. grays[i] = Color.FromArgb(i, i, i);
  121. }
  122. for (int y = 0; y < map.Height; y++)
  123. {
  124. for (int x = 0; x < map.Width; x++)
  125. {
  126. // 512 is the largest possible height before colours clamp
  127. int colorindex = (int) (Math.Max(Math.Min(1.0, map[x, y] / 128.0), 0.0) * (pallete - 1));
  128. // Handle error conditions
  129. if (colorindex > pallete - 1 || colorindex < 0)
  130. bmp.SetPixel(x, map.Height - y - 1, Color.Red);
  131. else
  132. bmp.SetPixel(x, map.Height - y - 1, grays[colorindex]);
  133. }
  134. }
  135. return bmp;
  136. }
  137. /// <summary>
  138. /// Protected method, generates a coloured bitmap
  139. /// image from a specified terrain channel.
  140. /// </summary>
  141. /// <param name="map">The terrain channel to export to bitmap</param>
  142. /// <returns>A System.Drawing.Bitmap containing a coloured image</returns>
  143. protected static Bitmap CreateBitmapFromMap(ITerrainChannel map)
  144. {
  145. Bitmap gradientmapLd = new Bitmap("defaultstripe.png");
  146. int pallete = gradientmapLd.Height;
  147. Bitmap bmp = new Bitmap(map.Width, map.Height);
  148. Color[] colours = new Color[pallete];
  149. for (int i = 0; i < pallete; i++)
  150. {
  151. colours[i] = gradientmapLd.GetPixel(0, i);
  152. }
  153. for (int y = 0; y < map.Height; y++)
  154. {
  155. for (int x = 0; x < map.Width; x++)
  156. {
  157. // 512 is the largest possible height before colours clamp
  158. int colorindex = (int) (Math.Max(Math.Min(1.0, map[x, y] / 512.0), 0.0) * (pallete - 1));
  159. // Handle error conditions
  160. if (colorindex > pallete - 1 || colorindex < 0)
  161. bmp.SetPixel(x, map.Height - y - 1, Color.Red);
  162. else
  163. bmp.SetPixel(x, map.Height - y - 1, colours[colorindex]);
  164. }
  165. }
  166. return bmp;
  167. }
  168. }
  169. }