GenericSystemDrawing.cs 6.6 KB

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