diff options
Diffstat (limited to 'OpenSim/Region/CoreModules/World/Terrain/FileLoaders/LLRAW.cs')
-rw-r--r-- | OpenSim/Region/CoreModules/World/Terrain/FileLoaders/LLRAW.cs | 235 |
1 files changed, 121 insertions, 114 deletions
diff --git a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/LLRAW.cs b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/LLRAW.cs index 62d232e..be1fb24 100644 --- a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/LLRAW.cs +++ b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/LLRAW.cs | |||
@@ -27,6 +27,7 @@ | |||
27 | 27 | ||
28 | using System; | 28 | using System; |
29 | using System.IO; | 29 | using System.IO; |
30 | using OpenSim.Framework; | ||
30 | using OpenSim.Region.Framework.Interfaces; | 31 | using OpenSim.Region.Framework.Interfaces; |
31 | using OpenSim.Region.Framework.Scenes; | 32 | using OpenSim.Region.Framework.Scenes; |
32 | 33 | ||
@@ -73,12 +74,13 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
73 | public ITerrainChannel LoadFile(string filename) | 74 | public ITerrainChannel LoadFile(string filename) |
74 | { | 75 | { |
75 | FileInfo file = new FileInfo(filename); | 76 | FileInfo file = new FileInfo(filename); |
76 | FileStream s = file.Open(FileMode.Open, FileAccess.Read); | ||
77 | ITerrainChannel retval = LoadStream(s); | ||
78 | 77 | ||
79 | s.Close(); | 78 | ITerrainChannel channel; |
80 | 79 | ||
81 | return retval; | 80 | using (FileStream s = file.Open(FileMode.Open, FileAccess.Read)) |
81 | channel = LoadStream(s); | ||
82 | |||
83 | return channel; | ||
82 | } | 84 | } |
83 | 85 | ||
84 | public ITerrainChannel LoadFile(string filename, int offsetX, int offsetY, int fileWidth, int fileHeight, int sectionWidth, int sectionHeight) | 86 | public ITerrainChannel LoadFile(string filename, int offsetX, int offsetY, int fileWidth, int fileHeight, int sectionWidth, int sectionHeight) |
@@ -86,153 +88,159 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
86 | TerrainChannel retval = new TerrainChannel(sectionWidth, sectionHeight); | 88 | TerrainChannel retval = new TerrainChannel(sectionWidth, sectionHeight); |
87 | 89 | ||
88 | FileInfo file = new FileInfo(filename); | 90 | FileInfo file = new FileInfo(filename); |
89 | FileStream s = file.Open(FileMode.Open, FileAccess.Read); | ||
90 | BinaryReader bs = new BinaryReader(s); | ||
91 | 91 | ||
92 | int currFileYOffset = fileHeight - 1; | 92 | using (FileStream s = file.Open(FileMode.Open, FileAccess.Read)) |
93 | 93 | using (BinaryReader bs = new BinaryReader(s)) | |
94 | // if our region isn't on the first Y section of the areas to be landscaped, then | ||
95 | // advance to our section of the file | ||
96 | while (currFileYOffset > offsetY) | ||
97 | { | 94 | { |
98 | // read a whole strip of regions | 95 | int currFileYOffset = fileHeight - 1; |
99 | int heightsToRead = sectionHeight * (fileWidth * sectionWidth); | ||
100 | bs.ReadBytes(heightsToRead * 13); // because there are 13 fun channels | ||
101 | currFileYOffset--; | ||
102 | } | ||
103 | 96 | ||
104 | // got to the Y start offset within the file of our region | 97 | // if our region isn't on the first Y section of the areas to be landscaped, then |
105 | // so read the file bits associated with our region | 98 | // advance to our section of the file |
106 | int y; | 99 | while (currFileYOffset > offsetY) |
107 | // for each Y within our Y offset | ||
108 | for (y = sectionHeight - 1; y >= 0; y--) | ||
109 | { | ||
110 | int currFileXOffset = 0; | ||
111 | |||
112 | // if our region isn't the first X section of the areas to be landscaped, then | ||
113 | // advance the stream to the X start pos of our section in the file | ||
114 | // i.e. eat X upto where we start | ||
115 | while (currFileXOffset < offsetX) | ||
116 | { | 100 | { |
117 | bs.ReadBytes(sectionWidth * 13); | 101 | // read a whole strip of regions |
118 | currFileXOffset++; | 102 | int heightsToRead = sectionHeight * (fileWidth * sectionWidth); |
103 | bs.ReadBytes(heightsToRead * 13); // because there are 13 fun channels | ||
104 | currFileYOffset--; | ||
119 | } | 105 | } |
120 | 106 | ||
121 | // got to our X offset, so write our regions X line | 107 | // got to the Y start offset within the file of our region |
122 | int x; | 108 | // so read the file bits associated with our region |
123 | for (x = 0; x < sectionWidth; x++) | 109 | int y; |
124 | { | ||
125 | // Read a strip and continue | ||
126 | retval[x, y] = bs.ReadByte() * (bs.ReadByte() / 128.0); | ||
127 | bs.ReadBytes(11); | ||
128 | } | ||
129 | // record that we wrote it | ||
130 | currFileXOffset++; | ||
131 | 110 | ||
132 | // if our region isn't the last X section of the areas to be landscaped, then | 111 | // for each Y within our Y offset |
133 | // advance the stream to the end of this Y column | 112 | for (y = sectionHeight - 1; y >= 0; y--) |
134 | while (currFileXOffset < fileWidth) | ||
135 | { | 113 | { |
136 | // eat the next regions x line | 114 | int currFileXOffset = 0; |
137 | bs.ReadBytes(sectionWidth * 13); //The 13 channels again | 115 | |
116 | // if our region isn't the first X section of the areas to be landscaped, then | ||
117 | // advance the stream to the X start pos of our section in the file | ||
118 | // i.e. eat X upto where we start | ||
119 | while (currFileXOffset < offsetX) | ||
120 | { | ||
121 | bs.ReadBytes(sectionWidth * 13); | ||
122 | currFileXOffset++; | ||
123 | } | ||
124 | |||
125 | // got to our X offset, so write our regions X line | ||
126 | int x; | ||
127 | for (x = 0; x < sectionWidth; x++) | ||
128 | { | ||
129 | // Read a strip and continue | ||
130 | retval[x, y] = bs.ReadByte() * (bs.ReadByte() / 128.0); | ||
131 | bs.ReadBytes(11); | ||
132 | } | ||
133 | // record that we wrote it | ||
138 | currFileXOffset++; | 134 | currFileXOffset++; |
135 | |||
136 | // if our region isn't the last X section of the areas to be landscaped, then | ||
137 | // advance the stream to the end of this Y column | ||
138 | while (currFileXOffset < fileWidth) | ||
139 | { | ||
140 | // eat the next regions x line | ||
141 | bs.ReadBytes(sectionWidth * 13); //The 13 channels again | ||
142 | currFileXOffset++; | ||
143 | } | ||
139 | } | 144 | } |
140 | } | 145 | } |
141 | 146 | ||
142 | bs.Close(); | ||
143 | s.Close(); | ||
144 | |||
145 | return retval; | 147 | return retval; |
146 | } | 148 | } |
147 | 149 | ||
148 | public ITerrainChannel LoadStream(Stream s) | 150 | public ITerrainChannel LoadStream(Stream s) |
149 | { | 151 | { |
150 | TerrainChannel retval = new TerrainChannel(); | 152 | // The raw format doesn't contain any dimension information. |
153 | // Guess the square dimensions by using the length of the raw file. | ||
154 | double dimension = Math.Sqrt((double)(s.Length / 13)); | ||
155 | // Regions are always multiples of 256. | ||
156 | int trimmedDimension = (int)dimension - ((int)dimension % (int)Constants.RegionSize); | ||
157 | if (trimmedDimension < Constants.RegionSize) | ||
158 | trimmedDimension = (int)Constants.RegionSize; | ||
159 | |||
160 | TerrainChannel retval = new TerrainChannel(trimmedDimension, trimmedDimension); | ||
151 | 161 | ||
152 | BinaryReader bs = new BinaryReader(s); | 162 | using (BinaryReader bs = new BinaryReader(s)) |
153 | int y; | ||
154 | for (y = 0; y < retval.Height; y++) | ||
155 | { | 163 | { |
156 | int x; | 164 | int y; |
157 | for (x = 0; x < retval.Width; x++) | 165 | for (y = 0; y < retval.Height; y++) |
158 | { | 166 | { |
159 | retval[x, (retval.Height - 1) - y] = bs.ReadByte() * (bs.ReadByte() / 128.0); | 167 | int x; |
160 | bs.ReadBytes(11); // Advance the stream to next bytes. | 168 | for (x = 0; x < retval.Width; x++) |
169 | { | ||
170 | retval[x, (retval.Height - 1) - y] = bs.ReadByte() * (bs.ReadByte() / 128.0); | ||
171 | bs.ReadBytes(11); // Advance the stream to next bytes. | ||
172 | } | ||
161 | } | 173 | } |
162 | } | 174 | } |
163 | 175 | ||
164 | bs.Close(); | ||
165 | |||
166 | return retval; | 176 | return retval; |
167 | } | 177 | } |
168 | 178 | ||
169 | public void SaveFile(string filename, ITerrainChannel map) | 179 | public void SaveFile(string filename, ITerrainChannel map) |
170 | { | 180 | { |
171 | FileInfo file = new FileInfo(filename); | 181 | FileInfo file = new FileInfo(filename); |
172 | FileStream s = file.Open(FileMode.CreateNew, FileAccess.Write); | ||
173 | SaveStream(s, map); | ||
174 | 182 | ||
175 | s.Close(); | 183 | using (FileStream s = file.Open(FileMode.CreateNew, FileAccess.Write)) |
184 | SaveStream(s, map); | ||
176 | } | 185 | } |
177 | 186 | ||
178 | public void SaveStream(Stream s, ITerrainChannel map) | 187 | public void SaveStream(Stream s, ITerrainChannel map) |
179 | { | 188 | { |
180 | BinaryWriter binStream = new BinaryWriter(s); | 189 | using (BinaryWriter binStream = new BinaryWriter(s)) |
181 | |||
182 | // Output the calculated raw | ||
183 | for (int y = 0; y < map.Height; y++) | ||
184 | { | 190 | { |
185 | for (int x = 0; x < map.Width; x++) | 191 | // Output the calculated raw |
192 | for (int y = 0; y < map.Height; y++) | ||
186 | { | 193 | { |
187 | double t = map[x, (map.Height - 1) - y]; | 194 | for (int x = 0; x < map.Width; x++) |
188 | //if height is less than 0, set it to 0 as | ||
189 | //can't save -ve values in a LLRAW file | ||
190 | if (t < 0d) | ||
191 | { | 195 | { |
192 | t = 0d; | 196 | double t = map[x, (map.Height - 1) - y]; |
197 | //if height is less than 0, set it to 0 as | ||
198 | //can't save -ve values in a LLRAW file | ||
199 | if (t < 0d) | ||
200 | { | ||
201 | t = 0d; | ||
202 | } | ||
203 | |||
204 | int index = 0; | ||
205 | |||
206 | // The lookup table is pre-sorted, so we either find an exact match or | ||
207 | // the next closest (smaller) match with a binary search | ||
208 | index = Array.BinarySearch<HeightmapLookupValue>(LookupHeightTable, new HeightmapLookupValue(0, (float)t)); | ||
209 | if (index < 0) | ||
210 | index = ~index - 1; | ||
211 | |||
212 | index = LookupHeightTable[index].Index; | ||
213 | |||
214 | byte red = (byte) (index & 0xFF); | ||
215 | byte green = (byte) ((index >> 8) & 0xFF); | ||
216 | const byte blue = 20; | ||
217 | const byte alpha1 = 0; | ||
218 | const byte alpha2 = 0; | ||
219 | const byte alpha3 = 0; | ||
220 | const byte alpha4 = 0; | ||
221 | const byte alpha5 = 255; | ||
222 | const byte alpha6 = 255; | ||
223 | const byte alpha7 = 255; | ||
224 | const byte alpha8 = 255; | ||
225 | byte alpha9 = red; | ||
226 | byte alpha10 = green; | ||
227 | |||
228 | binStream.Write(red); | ||
229 | binStream.Write(green); | ||
230 | binStream.Write(blue); | ||
231 | binStream.Write(alpha1); | ||
232 | binStream.Write(alpha2); | ||
233 | binStream.Write(alpha3); | ||
234 | binStream.Write(alpha4); | ||
235 | binStream.Write(alpha5); | ||
236 | binStream.Write(alpha6); | ||
237 | binStream.Write(alpha7); | ||
238 | binStream.Write(alpha8); | ||
239 | binStream.Write(alpha9); | ||
240 | binStream.Write(alpha10); | ||
193 | } | 241 | } |
194 | |||
195 | int index = 0; | ||
196 | |||
197 | // The lookup table is pre-sorted, so we either find an exact match or | ||
198 | // the next closest (smaller) match with a binary search | ||
199 | index = Array.BinarySearch<HeightmapLookupValue>(LookupHeightTable, new HeightmapLookupValue(0, (float)t)); | ||
200 | if (index < 0) | ||
201 | index = ~index - 1; | ||
202 | |||
203 | index = LookupHeightTable[index].Index; | ||
204 | |||
205 | byte red = (byte) (index & 0xFF); | ||
206 | byte green = (byte) ((index >> 8) & 0xFF); | ||
207 | const byte blue = 20; | ||
208 | const byte alpha1 = 0; | ||
209 | const byte alpha2 = 0; | ||
210 | const byte alpha3 = 0; | ||
211 | const byte alpha4 = 0; | ||
212 | const byte alpha5 = 255; | ||
213 | const byte alpha6 = 255; | ||
214 | const byte alpha7 = 255; | ||
215 | const byte alpha8 = 255; | ||
216 | byte alpha9 = red; | ||
217 | byte alpha10 = green; | ||
218 | |||
219 | binStream.Write(red); | ||
220 | binStream.Write(green); | ||
221 | binStream.Write(blue); | ||
222 | binStream.Write(alpha1); | ||
223 | binStream.Write(alpha2); | ||
224 | binStream.Write(alpha3); | ||
225 | binStream.Write(alpha4); | ||
226 | binStream.Write(alpha5); | ||
227 | binStream.Write(alpha6); | ||
228 | binStream.Write(alpha7); | ||
229 | binStream.Write(alpha8); | ||
230 | binStream.Write(alpha9); | ||
231 | binStream.Write(alpha10); | ||
232 | } | 242 | } |
233 | } | 243 | } |
234 | |||
235 | binStream.Close(); | ||
236 | } | 244 | } |
237 | 245 | ||
238 | public string FileExtension | 246 | public string FileExtension |
@@ -259,7 +267,6 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
259 | public bool SupportsTileSave() | 267 | public bool SupportsTileSave() |
260 | { | 268 | { |
261 | return false; | 269 | return false; |
262 | } | 270 | } |
263 | |||
264 | } | 271 | } |
265 | } | 272 | } \ No newline at end of file |