GenericSystemDrawing.cs 7.4 KB

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