* 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 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO;
using OpenSim.Region.Environment.Interfaces; using OpenSim.Region.Environment.Interfaces;
namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
@ -50,6 +52,18 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
colours.Save(filename, ImageFormat.Bmp); 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> /// <summary>
/// The human readable version of the file format(s) this loader handles /// The human readable version of the file format(s) this loader handles
/// </summary> /// </summary>

View File

@ -24,8 +24,10 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO;
using OpenSim.Region.Environment.Interfaces; using OpenSim.Region.Environment.Interfaces;
namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
@ -39,6 +41,18 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
colours.Save(filename, ImageFormat.Gif); 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() public override string ToString()
{ {
return "GIF"; return "GIF";

View File

@ -28,6 +28,7 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO;
using OpenSim.Region.Environment.Interfaces; using OpenSim.Region.Environment.Interfaces;
namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
@ -57,21 +58,7 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
/// <returns>A terrain channel generated from the image.</returns> /// <returns>A terrain channel generated from the image.</returns>
public virtual ITerrainChannel LoadFile(string filename) public virtual ITerrainChannel LoadFile(string filename)
{ {
Bitmap file = new Bitmap(filename); return LoadBitmap(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;
} }
public ITerrainChannel LoadFile(string filename, int x, int y, int fileWidth, int fileHeight, int w, int h) public ITerrainChannel LoadFile(string filename, int x, int y, int fileWidth, int fileHeight, int w, int h)
@ -79,6 +66,28 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
throw new NotImplementedException(); 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> /// <summary>
/// Exports a file to a image on the disk using a System.Drawing exporter. /// Exports a file to a image on the disk using a System.Drawing exporter.
/// </summary> /// </summary>
@ -91,6 +100,18 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
colours.Save(filename, ImageFormat.Png); 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 #endregion
public override string ToString() public override string ToString()

View File

@ -28,6 +28,7 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO;
using OpenSim.Region.Environment.Interfaces; using OpenSim.Region.Environment.Interfaces;
namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
@ -51,6 +52,11 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
throw new NotImplementedException(); throw new NotImplementedException();
} }
public ITerrainChannel LoadStream(Stream stream)
{
throw new NotImplementedException();
}
public void SaveFile(string filename, ITerrainChannel map) public void SaveFile(string filename, ITerrainChannel map)
{ {
Bitmap colours = CreateBitmapFromMap(map); Bitmap colours = CreateBitmapFromMap(map);
@ -58,6 +64,18 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
colours.Save(filename, ImageFormat.Jpeg); 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 #endregion
public override string ToString() public override string ToString()

View File

@ -71,10 +71,24 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
public ITerrainChannel LoadFile(string filename) public ITerrainChannel LoadFile(string filename)
{ {
TerrainChannel retval = new TerrainChannel();
FileInfo file = new FileInfo(filename); FileInfo file = new FileInfo(filename);
FileStream s = file.Open(FileMode.Open, FileAccess.Read); 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); BinaryReader bs = new BinaryReader(s);
int y; int y;
for (y = 0; y < retval.Height; y++) for (y = 0; y < retval.Height; y++)
@ -88,20 +102,21 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
} }
bs.Close(); bs.Close();
s.Close();
return retval; 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) public void SaveFile(string filename, ITerrainChannel map)
{ {
FileInfo file = new FileInfo(filename); FileInfo file = new FileInfo(filename);
FileStream s = file.Open(FileMode.CreateNew, FileAccess.Write); 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); BinaryWriter binStream = new BinaryWriter(s);
// Output the calculated raw // Output the calculated raw
@ -151,10 +166,8 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
} }
binStream.Close(); binStream.Close();
s.Close();
} }
public string FileExtension public string FileExtension
{ {
get { return ".raw"; } get { return ".raw"; }

View File

@ -24,8 +24,10 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO;
using OpenSim.Region.Environment.Interfaces; using OpenSim.Region.Environment.Interfaces;
namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
@ -39,6 +41,18 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
colours.Save(filename, ImageFormat.Png); 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() public override string ToString()
{ {
return "PNG"; return "PNG";

View File

@ -41,22 +41,10 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
public ITerrainChannel LoadFile(string filename) public ITerrainChannel LoadFile(string filename)
{ {
TerrainChannel retval = new TerrainChannel();
FileInfo file = new FileInfo(filename); FileInfo file = new FileInfo(filename);
FileStream s = file.Open(FileMode.Open, FileAccess.Read); FileStream s = file.Open(FileMode.Open, FileAccess.Read);
BinaryReader bs = new BinaryReader(s); ITerrainChannel retval = LoadStream(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();
s.Close(); s.Close();
return retval; return retval;
@ -125,10 +113,37 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
return retval; 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) public void SaveFile(string filename, ITerrainChannel map)
{ {
FileInfo file = new FileInfo(filename); FileInfo file = new FileInfo(filename);
FileStream s = file.Open(FileMode.Create, FileAccess.Write); 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); BinaryWriter bs = new BinaryWriter(s);
int y; int y;
@ -142,7 +157,6 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
} }
bs.Close(); bs.Close();
s.Close();
} }
#endregion #endregion

View File

@ -24,8 +24,10 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO;
using OpenSim.Region.Environment.Interfaces; using OpenSim.Region.Environment.Interfaces;
namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
@ -39,6 +41,18 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders
colours.Save(filename, ImageFormat.Tiff); 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() public override string ToString()
{ {
return "TIFF"; return "TIFF";

View File

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

View File

@ -25,6 +25,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
using System.IO;
using OpenSim.Region.Environment.Interfaces; using OpenSim.Region.Environment.Interfaces;
namespace OpenSim.Region.Environment.Modules.World.Terrain namespace OpenSim.Region.Environment.Modules.World.Terrain
@ -34,6 +35,8 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain
string FileExtension { get; } string FileExtension { get; }
ITerrainChannel LoadFile(string filename); ITerrainChannel LoadFile(string filename);
ITerrainChannel LoadFile(string filename, int fileStartX, int fileStartY, int fileWidth, int fileHeight, int sectionWidth, int sectionHeight); 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 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. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
using System.IO;
namespace OpenSim.Region.Environment.Modules.World.Terrain namespace OpenSim.Region.Environment.Modules.World.Terrain
{ {
public interface ITerrainModule public interface ITerrainModule
{ {
void LoadFromFile(string filename); void LoadFromFile(string filename);
void SaveToFile(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); void InstallPlugin(string name, ITerrainEffect plug);
} }
} }

View File

@ -222,6 +222,67 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain
} }
} }
/// <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 #region Plugin Loading Methods
private void LoadPlugins() private void LoadPlugins()