/Aurora/Modules/World/Terrain/FileLoaders/GenericSystemDrawing.cs

https://bitbucket.org/VirtualReality/software-testing · C# · 233 lines · 126 code · 31 blank · 76 comment · 12 complexity · 3820ecdb12a917aeee89feaef232949b MD5 · raw file

  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.org/, 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 Aurora-Sim 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 Aurora.Framework;
  28. using System;
  29. using System.Drawing;
  30. using System.Drawing.Imaging;
  31. using System.IO;
  32. using Aurora.Framework.ConsoleFramework;
  33. using Aurora.Framework.Modules;
  34. using Aurora.Framework.SceneInfo;
  35. namespace Aurora.Modules.Terrain.FileLoaders
  36. {
  37. /// <summary>
  38. /// A virtual class designed to have methods overloaded,
  39. /// this class provides an interface for a generic image
  40. /// saving and loading mechanism, but does not specify the
  41. /// format. It should not be insubstantiated directly.
  42. /// </summary>
  43. public class GenericSystemDrawing : ITerrainLoader
  44. {
  45. #region ITerrainLoader Members
  46. public string FileExtension
  47. {
  48. get { return ".gsd"; }
  49. }
  50. /// <summary>
  51. /// Loads a file from a specified filename on the disk,
  52. /// parses the image using the System.Drawing parsers
  53. /// then returns a terrain channel. Values are
  54. /// returned based on HSL brightness between 0m and 128m
  55. /// </summary>
  56. /// <param name="filename">The target image to load</param>
  57. /// <param name="scene"></param>
  58. /// <returns>A terrain channel generated from the image.</returns>
  59. public virtual ITerrainChannel LoadFile(string filename, IScene scene)
  60. {
  61. return LoadBitmap(new Bitmap(filename));
  62. }
  63. public ITerrainChannel LoadFile(string filename, int x, int y, int fileWidth, int fileHeight, int w, int h)
  64. {
  65. // [[THEMAJOR]] Some work on tile loading..
  66. // terrain load-tile Tile.png 5 5 10000 10050
  67. Bitmap tilemap = new Bitmap(filename);
  68. // Prevents off-by-one issue
  69. fileHeight--;
  70. int xoffset = w*x;
  71. int yoffset = h*(fileHeight - y);
  72. //MainConsole.Instance.DebugFormat(
  73. // "[TERRAIN]: Loading tile {0},{1} (offset {2},{3}) from tilemap size of {4},{5}",
  74. // x, y, xoffset, yoffset, fileWidth, fileHeight);
  75. Rectangle tileRect = new Rectangle(xoffset, yoffset, w, h);
  76. PixelFormat format = tilemap.PixelFormat;
  77. Bitmap cloneBitmap = null;
  78. try
  79. {
  80. cloneBitmap = tilemap.Clone(tileRect, format);
  81. }
  82. catch (OutOfMemoryException e)
  83. {
  84. // This error WILL appear if the number of Y tiles is too high because of how it works from the bottom up
  85. // However, this still spits out ugly unreferenced object errors on the console
  86. MainConsole.Instance.ErrorFormat(
  87. "[TERRAIN]: Couldn't load tile {0},{1} (from bitmap coordinates {2},{3}). Number of specified Y tiles may be too high: {4}",
  88. x, y, xoffset, yoffset, e);
  89. }
  90. finally
  91. {
  92. // Some attempt at keeping a clean memory
  93. tilemap.Dispose();
  94. }
  95. return LoadBitmap(cloneBitmap);
  96. }
  97. public virtual ITerrainChannel LoadStream(Stream stream, IScene scene)
  98. {
  99. return LoadBitmap(new Bitmap(stream));
  100. }
  101. /// <summary>
  102. /// Exports a file to a image on the disk using a System.Drawing exporter.
  103. /// </summary>
  104. /// <param name="filename">The target filename</param>
  105. /// <param name="map">The terrain channel being saved</param>
  106. public virtual void SaveFile(string filename, ITerrainChannel map)
  107. {
  108. Bitmap colours = CreateGrayscaleBitmapFromMap(map);
  109. colours.Save(filename, ImageFormat.Png);
  110. }
  111. /// <summary>
  112. /// Exports a stream using a System.Drawing exporter.
  113. /// </summary>
  114. /// <param name="stream">The target stream</param>
  115. /// <param name="map">The terrain channel being saved</param>
  116. public virtual void SaveStream(Stream stream, ITerrainChannel map)
  117. {
  118. Bitmap colours = CreateGrayscaleBitmapFromMap(map);
  119. colours.Save(stream, ImageFormat.Png);
  120. }
  121. #endregion
  122. protected virtual ITerrainChannel LoadBitmap(Bitmap bitmap)
  123. {
  124. ITerrainChannel retval = new TerrainChannel(bitmap.Width, bitmap.Height, null);
  125. int x;
  126. for (x = 0; x < bitmap.Width; x++)
  127. {
  128. int y;
  129. for (y = 0; y < bitmap.Height; y++)
  130. {
  131. retval[x, y] = bitmap.GetPixel(x, bitmap.Height - y - 1).GetBrightness()*128;
  132. }
  133. }
  134. return retval;
  135. }
  136. public override string ToString()
  137. {
  138. return "SYS.DRAWING";
  139. }
  140. /// <summary>
  141. /// Protected method, generates a grayscale bitmap
  142. /// image from a specified terrain channel.
  143. /// </summary>
  144. /// <param name="map">The terrain channel to export to bitmap</param>
  145. /// <returns>A System.Drawing.Bitmap containing a grayscale image</returns>
  146. protected static Bitmap CreateGrayscaleBitmapFromMap(ITerrainChannel map)
  147. {
  148. Bitmap bmp = new Bitmap(map.Width, map.Height);
  149. const int pallete = 256;
  150. Color[] grays = new Color[pallete];
  151. for (int i = 0; i < grays.Length; i++)
  152. {
  153. grays[i] = Color.FromArgb(i, i, i);
  154. }
  155. for (int y = 0; y < map.Height; y++)
  156. {
  157. for (int x = 0; x < map.Width; x++)
  158. {
  159. // 512 is the largest possible height before colours clamp
  160. int colorindex = (int) (Math.Max(Math.Min(1.0, map[x, y]/128.0), 0.0)*(pallete - 1));
  161. // Handle error conditions
  162. if (colorindex > pallete - 1 || colorindex < 0)
  163. bmp.SetPixel(x, map.Height - y - 1, Color.Red);
  164. else
  165. bmp.SetPixel(x, map.Height - y - 1, grays[colorindex]);
  166. }
  167. }
  168. return bmp;
  169. }
  170. /// <summary>
  171. /// Protected method, generates a coloured bitmap
  172. /// image from a specified terrain channel.
  173. /// </summary>
  174. /// <param name="map">The terrain channel to export to bitmap</param>
  175. /// <returns>A System.Drawing.Bitmap containing a coloured image</returns>
  176. protected static Bitmap CreateBitmapFromMap(ITerrainChannel map)
  177. {
  178. Bitmap gradientmapLd = new Bitmap("defaultstripe.png");
  179. int pallete = gradientmapLd.Height;
  180. Bitmap bmp = new Bitmap(map.Width, map.Height);
  181. Color[] colours = new Color[pallete];
  182. for (int i = 0; i < pallete; i++)
  183. {
  184. colours[i] = gradientmapLd.GetPixel(0, i);
  185. }
  186. for (int y = 0; y < map.Height; y++)
  187. {
  188. for (int x = 0; x < map.Width; x++)
  189. {
  190. // 512 is the largest possible height before colours clamp
  191. int colorindex = (int) (Math.Max(Math.Min(1.0, map[x, y]/512.0), 0.0)*(pallete - 1));
  192. // Handle error conditions
  193. if (colorindex > pallete - 1 || colorindex < 0)
  194. bmp.SetPixel(x, map.Height - y - 1, Color.Red);
  195. else
  196. bmp.SetPixel(x, map.Height - y - 1, colours[colorindex]);
  197. }
  198. }
  199. return bmp;
  200. }
  201. }
  202. }