RAW32.cs 5.6 KB

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