RAW32.cs 7.0 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 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.IO;
  29. using OpenSim.Framework;
  30. using OpenSim.Region.Framework.Interfaces;
  31. using OpenSim.Region.Framework.Scenes;
  32. namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
  33. {
  34. public class RAW32 : ITerrainLoader
  35. {
  36. #region ITerrainLoader Members
  37. public string FileExtension
  38. {
  39. get { return ".r32"; }
  40. }
  41. public ITerrainChannel LoadFile(string filename)
  42. {
  43. FileInfo file = new FileInfo(filename);
  44. FileStream s = file.Open(FileMode.Open, FileAccess.Read);
  45. ITerrainChannel retval = LoadStream(s);
  46. s.Close();
  47. return retval;
  48. }
  49. public ITerrainChannel LoadFile(string filename, int offsetX, int offsetY, int fileWidth, int fileHeight, int sectionWidth, int sectionHeight)
  50. {
  51. TerrainChannel retval = new TerrainChannel(sectionWidth, sectionHeight);
  52. FileInfo file = new FileInfo(filename);
  53. FileStream s = file.Open(FileMode.Open, FileAccess.Read);
  54. BinaryReader bs = new BinaryReader(s);
  55. int currFileYOffset = 0;
  56. // if our region isn't on the first Y section of the areas to be landscaped, then
  57. // advance to our section of the file
  58. while (currFileYOffset < offsetY)
  59. {
  60. // read a whole strip of regions
  61. int heightsToRead = sectionHeight * (fileWidth * sectionWidth);
  62. bs.ReadBytes(heightsToRead * 4); // because the floats are 4 bytes in the file
  63. currFileYOffset++;
  64. }
  65. // got to the Y start offset within the file of our region
  66. // so read the file bits associated with our region
  67. int y;
  68. // for each Y within our Y offset
  69. for (y = 0; y < sectionHeight; y++)
  70. {
  71. int currFileXOffset = 0;
  72. // if our region isn't the first X section of the areas to be landscaped, then
  73. // advance the stream to the X start pos of our section in the file
  74. // i.e. eat X upto where we start
  75. while (currFileXOffset < offsetX)
  76. {
  77. bs.ReadBytes(sectionWidth * 4); // 4 bytes = single
  78. currFileXOffset++;
  79. }
  80. // got to our X offset, so write our regions X line
  81. int x;
  82. for (x = 0; x < sectionWidth; x++)
  83. {
  84. // Read a strip and continue
  85. retval[x, y] = bs.ReadSingle();
  86. }
  87. // record that we wrote it
  88. currFileXOffset++;
  89. // if our region isn't the last X section of the areas to be landscaped, then
  90. // advance the stream to the end of this Y column
  91. while (currFileXOffset < fileWidth)
  92. {
  93. // eat the next regions x line
  94. bs.ReadBytes(sectionWidth * 4); // 4 bytes = single
  95. currFileXOffset++;
  96. }
  97. }
  98. bs.Close();
  99. s.Close();
  100. return retval;
  101. }
  102. public ITerrainChannel LoadStream(Stream s)
  103. {
  104. // The raw format doesn't contain any dimension information.
  105. // Guess the square dimensions by using the length of the raw file.
  106. double dimension = Math.Sqrt((double)(s.Length / 4));
  107. // Regions are always multiples of 256.
  108. int trimmedDimension = (int)dimension - ((int)dimension % (int)Constants.RegionSize);
  109. if (trimmedDimension < Constants.RegionSize)
  110. trimmedDimension = (int)Constants.RegionSize;
  111. TerrainChannel retval = new TerrainChannel(trimmedDimension, trimmedDimension);
  112. BinaryReader bs = new BinaryReader(s);
  113. int y;
  114. for (y = 0; y < retval.Height; y++)
  115. {
  116. int x;
  117. for (x = 0; x < retval.Width; x++)
  118. {
  119. retval[x, y] = bs.ReadSingle();
  120. }
  121. }
  122. bs.Close();
  123. return retval;
  124. }
  125. public void SaveFile(string filename, ITerrainChannel map)
  126. {
  127. FileInfo file = new FileInfo(filename);
  128. FileStream s = file.Open(FileMode.Create, FileAccess.Write);
  129. SaveStream(s, map);
  130. s.Close();
  131. }
  132. public void SaveStream(Stream s, ITerrainChannel map)
  133. {
  134. BinaryWriter bs = new BinaryWriter(s);
  135. int y;
  136. for (y = 0; y < map.Height; y++)
  137. {
  138. int x;
  139. for (x = 0; x < map.Width; x++)
  140. {
  141. bs.Write((float) map[x, y]);
  142. }
  143. }
  144. bs.Close();
  145. }
  146. public virtual void SaveFile(ITerrainChannel m_channel, string filename,
  147. int offsetX, int offsetY,
  148. int fileWidth, int fileHeight,
  149. int regionSizeX, int regionSizeY)
  150. {
  151. throw new System.Exception("Not Implemented");
  152. }
  153. #endregion
  154. public override string ToString()
  155. {
  156. return "RAW32";
  157. }
  158. //Returns true if this extension is supported for terrain save-tile
  159. public bool SupportsTileSave()
  160. {
  161. return false;
  162. }
  163. }
  164. }