RAW32.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.IO;
  28. using OpenSim.Region.Environment.Interfaces;
  29. namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
  30. {
  31. public class RAW32 : ITerrainLoader
  32. {
  33. #region ITerrainLoader Members
  34. public string FileExtension
  35. {
  36. get { return ".r32"; }
  37. }
  38. public ITerrainChannel LoadFile(string filename)
  39. {
  40. FileInfo file = new FileInfo(filename);
  41. FileStream s = file.Open(FileMode.Open, FileAccess.Read);
  42. ITerrainChannel retval = LoadStream(s);
  43. s.Close();
  44. return retval;
  45. }
  46. public ITerrainChannel LoadFile(string filename, int offsetX, int offsetY, int fileWidth, int fileHeight, int sectionWidth, int sectionHeight)
  47. {
  48. TerrainChannel retval = new TerrainChannel(sectionWidth, sectionHeight);
  49. FileInfo file = new FileInfo(filename);
  50. FileStream s = file.Open(FileMode.Open, FileAccess.Read);
  51. BinaryReader bs = new BinaryReader(s);
  52. int currFileYOffset = 0;
  53. // if our region isn't on the first Y section of the areas to be landscaped, then
  54. // advance to our section of the file
  55. while (currFileYOffset < offsetY)
  56. {
  57. // read a whole strip of regions
  58. int heightsToRead = sectionHeight * (fileWidth * sectionWidth);
  59. bs.ReadBytes(heightsToRead * 4); // because the floats are 4 bytes in the file
  60. currFileYOffset++;
  61. }
  62. // got to the Y start offset within the file of our region
  63. // so read the file bits associated with our region
  64. int y;
  65. // for each Y within our Y offset
  66. for (y = 0; y < sectionHeight; y++)
  67. {
  68. int currFileXOffset = 0;
  69. // if our region isn't the first X section of the areas to be landscaped, then
  70. // advance the stream to the X start pos of our section in the file
  71. // i.e. eat X upto where we start
  72. while (currFileXOffset < offsetX)
  73. {
  74. bs.ReadBytes(sectionWidth * 4); // 4 bytes = single
  75. currFileXOffset++;
  76. }
  77. // got to our X offset, so write our regions X line
  78. int x;
  79. for (x = 0; x < sectionWidth; x++)
  80. {
  81. // Read a strip and continue
  82. retval[x, y] = bs.ReadSingle();
  83. }
  84. // record that we wrote it
  85. currFileXOffset++;
  86. // if our region isn't the last X section of the areas to be landscaped, then
  87. // advance the stream to the end of this Y column
  88. while (currFileXOffset < fileWidth)
  89. {
  90. // eat the next regions x line
  91. bs.ReadBytes(sectionWidth * 4); // 4 bytes = single
  92. currFileXOffset++;
  93. }
  94. }
  95. bs.Close();
  96. s.Close();
  97. return retval;
  98. }
  99. public ITerrainChannel LoadStream(Stream s)
  100. {
  101. TerrainChannel retval = new TerrainChannel();
  102. BinaryReader bs = new BinaryReader(s);
  103. int y;
  104. for (y = 0; y < retval.Height; y++)
  105. {
  106. int x;
  107. for (x = 0; x < retval.Width; x++)
  108. {
  109. retval[x, y] = bs.ReadSingle();
  110. }
  111. }
  112. bs.Close();
  113. return retval;
  114. }
  115. public void SaveFile(string filename, ITerrainChannel map)
  116. {
  117. FileInfo file = new FileInfo(filename);
  118. FileStream s = file.Open(FileMode.Create, FileAccess.Write);
  119. SaveStream(s, map);
  120. s.Close();
  121. }
  122. public void SaveStream(Stream s, ITerrainChannel map)
  123. {
  124. BinaryWriter bs = new BinaryWriter(s);
  125. int y;
  126. for (y = 0; y < map.Height; y++)
  127. {
  128. int x;
  129. for (x = 0; x < map.Width; x++)
  130. {
  131. bs.Write((float) map[x, y]);
  132. }
  133. }
  134. bs.Close();
  135. }
  136. #endregion
  137. public override string ToString()
  138. {
  139. return "RAW32";
  140. }
  141. }
  142. }