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