LLRAW.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.IO;
  29. using OpenSim.Region.Environment.Interfaces;
  30. namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
  31. {
  32. public class LLRAW : ITerrainLoader
  33. {
  34. public struct HeightmapLookupValue : IComparable<HeightmapLookupValue>
  35. {
  36. public int Index;
  37. public double Value;
  38. public HeightmapLookupValue(int index, double value)
  39. {
  40. Index = index;
  41. Value = value;
  42. }
  43. public int CompareTo(HeightmapLookupValue val)
  44. {
  45. return Value.CompareTo(val.Value);
  46. }
  47. }
  48. /// <summary>Lookup table to speed up terrain exports</summary>
  49. HeightmapLookupValue[] LookupHeightTable;
  50. public LLRAW()
  51. {
  52. LookupHeightTable = new HeightmapLookupValue[256 * 256];
  53. for (int i = 0; i < 256; i++)
  54. {
  55. for (int j = 0; j < 256; j++)
  56. {
  57. LookupHeightTable[i + (j * 256)] = new HeightmapLookupValue(i + (j * 256), ((double)i * ((double)j / 128.0d)));
  58. }
  59. }
  60. Array.Sort<HeightmapLookupValue>(LookupHeightTable);
  61. }
  62. #region ITerrainLoader Members
  63. public ITerrainChannel LoadFile(string filename)
  64. {
  65. FileInfo file = new FileInfo(filename);
  66. FileStream s = file.Open(FileMode.Open, FileAccess.Read);
  67. ITerrainChannel retval = LoadStream(s);
  68. s.Close();
  69. return retval;
  70. }
  71. public ITerrainChannel LoadFile(string filename, int offsetX, int offsetY, int fileWidth, int fileHeight, int sectionWidth, int sectionHeight)
  72. {
  73. TerrainChannel retval = new TerrainChannel(sectionWidth, sectionHeight);
  74. FileInfo file = new FileInfo(filename);
  75. FileStream s = file.Open(FileMode.Open, FileAccess.Read);
  76. BinaryReader bs = new BinaryReader(s);
  77. int currFileYOffset = fileHeight - 1;
  78. // if our region isn't on the first Y section of the areas to be landscaped, then
  79. // advance to our section of the file
  80. while (currFileYOffset > offsetY)
  81. {
  82. // read a whole strip of regions
  83. int heightsToRead = sectionHeight * (fileWidth * sectionWidth);
  84. bs.ReadBytes(heightsToRead * 13); // because there are 13 fun channels
  85. currFileYOffset--;
  86. }
  87. // got to the Y start offset within the file of our region
  88. // so read the file bits associated with our region
  89. int y;
  90. // for each Y within our Y offset
  91. for (y = sectionHeight - 1; y >= 0; y--)
  92. {
  93. int currFileXOffset = 0;
  94. // if our region isn't the first X section of the areas to be landscaped, then
  95. // advance the stream to the X start pos of our section in the file
  96. // i.e. eat X upto where we start
  97. while (currFileXOffset < offsetX)
  98. {
  99. bs.ReadBytes(sectionWidth * 13);
  100. currFileXOffset++;
  101. }
  102. // got to our X offset, so write our regions X line
  103. int x;
  104. for (x = 0; x < sectionWidth; x++)
  105. {
  106. // Read a strip and continue
  107. retval[x, y] = bs.ReadByte() * (bs.ReadByte() / 128.0);
  108. bs.ReadBytes(11);
  109. }
  110. // record that we wrote it
  111. currFileXOffset++;
  112. // if our region isn't the last X section of the areas to be landscaped, then
  113. // advance the stream to the end of this Y column
  114. while (currFileXOffset < fileWidth)
  115. {
  116. // eat the next regions x line
  117. bs.ReadBytes(sectionWidth * 13); //The 13 channels again
  118. currFileXOffset++;
  119. }
  120. }
  121. bs.Close();
  122. s.Close();
  123. return retval;
  124. }
  125. public ITerrainChannel LoadStream(Stream s)
  126. {
  127. TerrainChannel retval = new TerrainChannel();
  128. BinaryReader bs = new BinaryReader(s);
  129. int y;
  130. for (y = 0; y < retval.Height; y++)
  131. {
  132. int x;
  133. for (x = 0; x < retval.Width; x++)
  134. {
  135. retval[x, (retval.Height - 1) - y] = bs.ReadByte() * (bs.ReadByte() / 128.0);
  136. bs.ReadBytes(11); // Advance the stream to next bytes.
  137. }
  138. }
  139. bs.Close();
  140. return retval;
  141. }
  142. public void SaveFile(string filename, ITerrainChannel map)
  143. {
  144. FileInfo file = new FileInfo(filename);
  145. FileStream s = file.Open(FileMode.CreateNew, FileAccess.Write);
  146. SaveStream(s, map);
  147. s.Close();
  148. }
  149. public void SaveStream(Stream s, ITerrainChannel map)
  150. {
  151. BinaryWriter binStream = new BinaryWriter(s);
  152. // Output the calculated raw
  153. for (int y = 0; y < map.Height; y++)
  154. {
  155. for (int x = 0; x < map.Width; x++)
  156. {
  157. double t = map[x, (map.Height - 1) - y];
  158. int index = 0;
  159. // The lookup table is pre-sorted, so we either find an exact match or
  160. // the next closest (smaller) match with a binary search
  161. index = Array.BinarySearch<HeightmapLookupValue>(LookupHeightTable, new HeightmapLookupValue(0, t));
  162. if (index < 0)
  163. index = ~index - 1;
  164. index = LookupHeightTable[index].Index;
  165. byte red = (byte) (index & 0xFF);
  166. byte green = (byte) ((index >> 8) & 0xFF);
  167. const byte blue = 20;
  168. const byte alpha1 = 0;
  169. const byte alpha2 = 0;
  170. const byte alpha3 = 0;
  171. const byte alpha4 = 0;
  172. const byte alpha5 = 255;
  173. const byte alpha6 = 255;
  174. const byte alpha7 = 255;
  175. const byte alpha8 = 255;
  176. byte alpha9 = red;
  177. byte alpha10 = green;
  178. binStream.Write(red);
  179. binStream.Write(green);
  180. binStream.Write(blue);
  181. binStream.Write(alpha1);
  182. binStream.Write(alpha2);
  183. binStream.Write(alpha3);
  184. binStream.Write(alpha4);
  185. binStream.Write(alpha5);
  186. binStream.Write(alpha6);
  187. binStream.Write(alpha7);
  188. binStream.Write(alpha8);
  189. binStream.Write(alpha9);
  190. binStream.Write(alpha10);
  191. }
  192. }
  193. binStream.Close();
  194. }
  195. public string FileExtension
  196. {
  197. get { return ".raw"; }
  198. }
  199. #endregion
  200. public override string ToString()
  201. {
  202. return "LL/SL RAW";
  203. }
  204. }
  205. }