* Allow terrains to be loaded and saved from streams as well as directly to and from files

* Should be making use of this in the next revisions
0.6.0-stable
Justin Clarke Casey 2008-06-29 19:21:43 +00:00
parent 31c63558c8
commit 7d5a21ddbf
12 changed files with 268 additions and 48 deletions

View File

@ -24,8 +24,10 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using OpenSim.Region.Environment.Interfaces;
namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
@ -49,6 +51,18 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
colours.Save(filename, ImageFormat.Bmp);
}
/// <summary>
/// Exports a stream using a System.Drawing exporter.
/// </summary>
/// <param name="stream">The target stream</param>
/// <param name="map">The terrain channel being saved</param>
public override void SaveStream(Stream stream, ITerrainChannel map)
{
Bitmap colours = CreateGrayscaleBitmapFromMap(map);
colours.Save(stream, ImageFormat.Png);
}
/// <summary>
/// The human readable version of the file format(s) this loader handles

View File

@ -24,8 +24,10 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using OpenSim.Region.Environment.Interfaces;
namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
@ -38,6 +40,18 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
colours.Save(filename, ImageFormat.Gif);
}
/// <summary>
/// Exports a stream using a System.Drawing exporter.
/// </summary>
/// <param name="stream">The target stream</param>
/// <param name="map">The terrain channel being saved</param>
public override void SaveStream(Stream stream, ITerrainChannel map)
{
Bitmap colours = CreateGrayscaleBitmapFromMap(map);
colours.Save(stream, ImageFormat.Gif);
}
public override string ToString()
{

View File

@ -28,6 +28,7 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using OpenSim.Region.Environment.Interfaces;
namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
@ -57,27 +58,35 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
/// <returns>A terrain channel generated from the image.</returns>
public virtual ITerrainChannel LoadFile(string filename)
{
Bitmap file = new Bitmap(filename);
ITerrainChannel retval = new TerrainChannel(file.Width, file.Height);
int x;
for (x = 0; x < file.Width; x++)
{
int y;
for (y = 0; y < file.Height; y++)
{
retval[x, y] = file.GetPixel(x, file.Height - y - 1).GetBrightness() * 128;
}
}
return retval;
return LoadBitmap(new Bitmap(filename));
}
public ITerrainChannel LoadFile(string filename, int x, int y, int fileWidth, int fileHeight, int w, int h)
{
throw new NotImplementedException();
}
public virtual ITerrainChannel LoadStream(Stream stream)
{
return LoadBitmap(new Bitmap(stream));
}
protected virtual ITerrainChannel LoadBitmap(Bitmap bitmap)
{
ITerrainChannel retval = new TerrainChannel(bitmap.Width, bitmap.Height);
int x;
for (x = 0; x < bitmap.Width; x++)
{
int y;
for (y = 0; y < bitmap.Height; y++)
{
retval[x, y] = bitmap.GetPixel(x, bitmap.Height - y - 1).GetBrightness() * 128;
}
}
return retval;
}
/// <summary>
/// Exports a file to a image on the disk using a System.Drawing exporter.
@ -90,6 +99,18 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
colours.Save(filename, ImageFormat.Png);
}
/// <summary>
/// Exports a stream using a System.Drawing exporter.
/// </summary>
/// <param name="stream">The target stream</param>
/// <param name="map">The terrain channel being saved</param>
public virtual void SaveStream(Stream stream, ITerrainChannel map)
{
Bitmap colours = CreateGrayscaleBitmapFromMap(map);
colours.Save(stream, ImageFormat.Png);
}
#endregion

View File

@ -28,6 +28,7 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using OpenSim.Region.Environment.Interfaces;
namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
@ -50,6 +51,11 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
{
throw new NotImplementedException();
}
public ITerrainChannel LoadStream(Stream stream)
{
throw new NotImplementedException();
}
public void SaveFile(string filename, ITerrainChannel map)
{
@ -57,6 +63,18 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
colours.Save(filename, ImageFormat.Jpeg);
}
/// <summary>
/// Exports a stream using a System.Drawing exporter.
/// </summary>
/// <param name="stream">The target stream</param>
/// <param name="map">The terrain channel being saved</param>
public void SaveStream(Stream stream, ITerrainChannel map)
{
Bitmap colours = CreateBitmapFromMap(map);
colours.Save(stream, ImageFormat.Jpeg);
}
#endregion

View File

@ -71,10 +71,24 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
public ITerrainChannel LoadFile(string filename)
{
TerrainChannel retval = new TerrainChannel();
FileInfo file = new FileInfo(filename);
FileStream s = file.Open(FileMode.Open, FileAccess.Read);
ITerrainChannel retval = LoadStream(s);
s.Close();
return retval;
}
public ITerrainChannel LoadFile(string filename, int x, int y, int fileWidth, int fileHeight, int w, int h)
{
throw new NotImplementedException();
}
public ITerrainChannel LoadStream(Stream s)
{
TerrainChannel retval = new TerrainChannel();
BinaryReader bs = new BinaryReader(s);
int y;
for (y = 0; y < retval.Height; y++)
@ -87,21 +101,22 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
}
}
bs.Close();
s.Close();
bs.Close();
return retval;
}
public ITerrainChannel LoadFile(string filename, int x, int y, int fileWidth, int fileHeight, int w, int h)
{
throw new NotImplementedException();
}
public void SaveFile(string filename, ITerrainChannel map)
{
FileInfo file = new FileInfo(filename);
FileStream s = file.Open(FileMode.CreateNew, FileAccess.Write);
SaveStream(s, map);
s.Close();
}
public void SaveStream(Stream s, ITerrainChannel map)
{
BinaryWriter binStream = new BinaryWriter(s);
// Output the calculated raw
@ -150,11 +165,9 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
}
}
binStream.Close();
s.Close();
binStream.Close();
}
public string FileExtension
{
get { return ".raw"; }

View File

@ -24,8 +24,10 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using OpenSim.Region.Environment.Interfaces;
namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
@ -38,6 +40,18 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
colours.Save(filename, ImageFormat.Png);
}
/// <summary>
/// Exports a stream using a System.Drawing exporter.
/// </summary>
/// <param name="stream">The target stream</param>
/// <param name="map">The terrain channel being saved</param>
public override void SaveStream(Stream stream, ITerrainChannel map)
{
Bitmap colours = CreateGrayscaleBitmapFromMap(map);
colours.Save(stream, ImageFormat.Png);
}
public override string ToString()
{

View File

@ -41,22 +41,10 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
public ITerrainChannel LoadFile(string filename)
{
TerrainChannel retval = new TerrainChannel();
FileInfo file = new FileInfo(filename);
FileStream s = file.Open(FileMode.Open, FileAccess.Read);
BinaryReader bs = new BinaryReader(s);
int y;
for (y = 0; y < retval.Height; y++)
{
int x;
for (x = 0; x < retval.Width; x++)
{
retval[x, y] = bs.ReadSingle();
}
}
ITerrainChannel retval = LoadStream(s);
bs.Close();
s.Close();
return retval;
@ -124,11 +112,38 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
return retval;
}
public ITerrainChannel LoadStream(Stream s)
{
TerrainChannel retval = new TerrainChannel();
BinaryReader bs = new BinaryReader(s);
int y;
for (y = 0; y < retval.Height; y++)
{
int x;
for (x = 0; x < retval.Width; x++)
{
retval[x, y] = bs.ReadSingle();
}
}
bs.Close();
return retval;
}
public void SaveFile(string filename, ITerrainChannel map)
{
FileInfo file = new FileInfo(filename);
FileStream s = file.Open(FileMode.Create, FileAccess.Write);
SaveStream(s, map);
s.Close();
}
public void SaveStream(Stream s, ITerrainChannel map)
{
BinaryWriter bs = new BinaryWriter(s);
int y;
@ -141,8 +156,7 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
}
}
bs.Close();
s.Close();
bs.Close();
}
#endregion

View File

@ -24,8 +24,10 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using OpenSim.Region.Environment.Interfaces;
namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
@ -38,6 +40,18 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
colours.Save(filename, ImageFormat.Tiff);
}
/// <summary>
/// Exports a stream using a System.Drawing exporter.
/// </summary>
/// <param name="stream">The target stream</param>
/// <param name="map">The terrain channel being saved</param>
public override void SaveStream(Stream stream, ITerrainChannel map)
{
Bitmap colours = CreateGrayscaleBitmapFromMap(map);
colours.Save(stream, ImageFormat.Tiff);
}
public override string ToString()
{

View File

@ -43,10 +43,19 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
public ITerrainChannel LoadFile(string filename)
{
TerrainChannel retval = new TerrainChannel();
FileInfo file = new FileInfo(filename);
FileStream s = file.Open(FileMode.Open, FileAccess.Read);
ITerrainChannel retval = LoadStream(s);
s.Close();
return retval;
}
public ITerrainChannel LoadStream(Stream s)
{
TerrainChannel retval = new TerrainChannel();
BinaryReader bs = new BinaryReader(s);
bool eof = false;
@ -98,8 +107,7 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
}
bs.Close();
s.Close();
return retval;
}
@ -107,6 +115,11 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
{
throw new NotImplementedException();
}
public void SaveStream(Stream stream, ITerrainChannel map)
{
throw new NotImplementedException();
}
public string FileExtension
{

View File

@ -25,6 +25,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System.IO;
using OpenSim.Region.Environment.Interfaces;
namespace OpenSim.Region.Environment.Modules.World.Terrain
@ -34,6 +35,8 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain
string FileExtension { get; }
ITerrainChannel LoadFile(string filename);
ITerrainChannel LoadFile(string filename, int fileStartX, int fileStartY, int fileWidth, int fileHeight, int sectionWidth, int sectionHeight);
ITerrainChannel LoadStream(Stream stream);
void SaveFile(string filename, ITerrainChannel map);
void SaveStream(Stream stream, ITerrainChannel map);
}
}

View File

@ -25,12 +25,33 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System.IO;
namespace OpenSim.Region.Environment.Modules.World.Terrain
{
public interface ITerrainModule
{
void LoadFromFile(string filename);
void SaveToFile(string filename);
/// <summary>
/// Load a terrain from a stream.
/// </summary>
/// <param name="filename">
/// Only required here to identify the image type. Not otherwise used in the loading itself.
/// </param>
/// <param name="stream"></param>
void LoadFromStream(string filename, Stream stream);
/// <summary>
/// Save a terrain to a stream.
/// </summary>
/// <param name="filename">
/// Only required here to identify the image type. Not otherwise used in the saving itself.
/// </param>
/// <param name="stream"></param>
void SaveToStream(string filename, Stream stream);
void InstallPlugin(string name, ITerrainEffect plug);
}
}

View File

@ -221,6 +221,67 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain
throw new TerrainException(String.Format("Unable to save heightmap: saving of this file format not implemented"));
}
}
/// <summary>
/// Loads a terrain file from a stream and installs it in the scene.
/// </summary>
/// <param name="filename">Filename to terrain file. Type is determined by extension.</param>
/// <param name="stream"></param>
public void LoadFromStream(string filename, Stream stream)
{
foreach (KeyValuePair<string, ITerrainLoader> loader in m_loaders)
{
if (filename.EndsWith(loader.Key))
{
lock (m_scene)
{
try
{
ITerrainChannel channel = loader.Value.LoadStream(stream);
m_scene.Heightmap = channel;
m_channel = channel;
UpdateRevertMap();
}
catch (NotImplementedException)
{
m_log.Error("[TERRAIN]: Unable to load heightmap, the " + loader.Value +
" parser does not support file loading. (May be save only)");
throw new TerrainException(String.Format("unable to load heightmap: parser {0} does not support loading", loader.Value));
}
}
CheckForTerrainUpdates();
m_log.Info("[TERRAIN]: File (" + filename + ") loaded successfully");
return;
}
}
m_log.Error("[TERRAIN]: Unable to load heightmap, no file loader availible for that format.");
throw new TerrainException(String.Format("unable to load heightmap from file {0}: no loader available for that format", filename));
}
/// <summary>
/// Saves the current heightmap to a specified stream.
/// </summary>
/// <param name="filename">The destination filename. Used here only to identify the image type</param>
/// <param name="stream"></param>
public void SaveToStream(string filename, Stream stream)
{
try
{
foreach (KeyValuePair<string, ITerrainLoader> loader in m_loaders)
{
if (filename.EndsWith(loader.Key))
{
loader.Value.SaveStream(stream, m_channel);
return;
}
}
}
catch (NotImplementedException)
{
m_log.Error("Unable to save to " + filename + ", saving of this file format has not been implemented.");
throw new TerrainException(String.Format("Unable to save heightmap: saving of this file format not implemented"));
}
}
#region Plugin Loading Methods