LLRAW.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #region ITerrainLoader Members
  35. public ITerrainChannel LoadFile(string filename)
  36. {
  37. TerrainChannel retval = new TerrainChannel();
  38. FileInfo file = new FileInfo(filename);
  39. FileStream s = file.Open(FileMode.Open, FileAccess.Read);
  40. BinaryReader bs = new BinaryReader(s);
  41. int x, y;
  42. for (y = 0; y < retval.Height; y++)
  43. {
  44. for (x = 0; x < retval.Width; x++)
  45. {
  46. retval[x, y] = (double) bs.ReadByte() * ((double) bs.ReadByte() / 127.0);
  47. bs.ReadBytes(11); // Advance the stream to next bytes.
  48. }
  49. }
  50. bs.Close();
  51. s.Close();
  52. return retval;
  53. }
  54. public ITerrainChannel LoadFile(string filename, int x, int y, int fileWidth, int fileHeight, int w, int h)
  55. {
  56. throw new NotImplementedException();
  57. }
  58. public void SaveFile(string filename, ITerrainChannel map)
  59. {
  60. FileInfo file = new FileInfo(filename);
  61. FileStream s = file.Open(FileMode.CreateNew, FileAccess.Write);
  62. BinaryWriter binStream = new BinaryWriter(s);
  63. // Generate a smegging big lookup table to speed the operation up (it needs it)
  64. double[] lookupHeightTable = new double[65536];
  65. int i, j, x, y;
  66. for (i = 0; i < 256; i++)
  67. {
  68. for (j = 0; j < 256; j++)
  69. {
  70. lookupHeightTable[i + (j * 256)] = ((double) i * ((double) j / 127.0));
  71. }
  72. }
  73. // Output the calculated raw
  74. for (y = 0; y < map.Height; y++)
  75. {
  76. for (x = 0; x < map.Width; x++)
  77. {
  78. double t = map[x, y];
  79. double min = double.MaxValue;
  80. int index = 0;
  81. for (i = 0; i < 65536; i++)
  82. {
  83. if (Math.Abs(t - lookupHeightTable[i]) < min)
  84. {
  85. min = Math.Abs(t - lookupHeightTable[i]);
  86. index = i;
  87. }
  88. }
  89. byte red = (byte) (index & 0xFF);
  90. byte green = (byte) ((index >> 8) & 0xFF);
  91. byte blue = 20;
  92. byte alpha1 = 0; // Land Parcels
  93. byte alpha2 = 0; // For Sale Land
  94. byte alpha3 = 0; // Public Edit Object
  95. byte alpha4 = 0; // Public Edit Land
  96. byte alpha5 = 255; // Safe Land
  97. byte alpha6 = 255; // Flying Allowed
  98. byte alpha7 = 255; // Create Landmark
  99. byte alpha8 = 255; // Outside Scripts
  100. byte alpha9 = red;
  101. byte alpha10 = green;
  102. binStream.Write(red);
  103. binStream.Write(green);
  104. binStream.Write(blue);
  105. binStream.Write(alpha1);
  106. binStream.Write(alpha2);
  107. binStream.Write(alpha3);
  108. binStream.Write(alpha4);
  109. binStream.Write(alpha5);
  110. binStream.Write(alpha6);
  111. binStream.Write(alpha7);
  112. binStream.Write(alpha8);
  113. binStream.Write(alpha9);
  114. binStream.Write(alpha10);
  115. }
  116. }
  117. binStream.Close();
  118. s.Close();
  119. }
  120. public string FileExtension
  121. {
  122. get { return ".raw"; }
  123. }
  124. #endregion
  125. public override string ToString()
  126. {
  127. return "LL/SL RAW";
  128. }
  129. }
  130. }