Skip to content

Commit

Permalink
Import/export of 16 bit raw data works. Also support version 2 of TER…
Browse files Browse the repository at this point in the history
… files.
  • Loading branch information
nitroxis committed Feb 23, 2014
1 parent 4795205 commit e190dfa
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 42 deletions.
2 changes: 2 additions & 0 deletions BZ2TerrainEditor/Editor.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 104 additions & 11 deletions BZ2TerrainEditor/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,24 @@ public Editor(string fileName)
return;
}

this.terrain = Terrain.Read(fileName);
this.initialize();
try
{
this.terrain = Terrain.Read(fileName);
}
catch (Exception bug)
{
MessageBox.Show(bug.ToString(), "Failed to load terrain.");
}

Properties.Settings.Default.OpenFileInitialDirectory = this.currentFile.DirectoryName;
if (this.terrain != null)
{
this.initialize();
Properties.Settings.Default.OpenFileInitialDirectory = this.currentFile.DirectoryName;
}
else
{
this.currentFile = null;
}
}

public Editor(Terrain terrain)
Expand All @@ -84,6 +98,11 @@ private void initialize()
{
this.free();

this.updateTitle();

if (this.terrain == null)
return;

this.heightMapPreview.Image = this.generate16BitImage(this.terrain.HeightMap, this.terrain.HeightMapMin, this.terrain.HeightMapMax);
this.colorMapPreview.Image = this.generateColorMapImage(this.terrain.ColorMap);
this.normalMapPreview.Image = this.generate8BitImage(this.terrain.NormalMap);
Expand All @@ -92,8 +111,6 @@ private void initialize()
this.alphaMap2Preview.Image = this.generate8BitImage(this.terrain.AlphaMap2);
this.alphaMap3Preview.Image = this.generate8BitImage(this.terrain.AlphaMap3);
this.flowLayout.Enabled = true;

this.updateTitle();
}

/// <summary>
Expand Down Expand Up @@ -308,10 +325,21 @@ private void openTerrain(object sender, EventArgs e)
{
if (this.terrain == null)
{
this.currentFile = new FileInfo(dialog.FileName);
Properties.Settings.Default.OpenFileInitialDirectory = this.currentFile.DirectoryName;
this.terrain = Terrain.Read(dialog.FileName);
this.initialize();
try
{
this.terrain = Terrain.Read(dialog.FileName);
}
catch (Exception bug)
{
MessageBox.Show(bug.ToString(), "Failed to load terrain.");
}

if (this.terrain != null)
{
this.initialize();
this.currentFile = new FileInfo(dialog.FileName);
Properties.Settings.Default.OpenFileInitialDirectory = this.currentFile.DirectoryName;
}
}
else
{
Expand Down Expand Up @@ -413,11 +441,10 @@ private void heightMapImport_Click(object sender, EventArgs e)
Marshal.Copy(data.Scan0, buffer, 0, buffer.Length);

for (int y = 0; y < data.Height; y++)
{
for (int x = 0; x < data.Width; x++)
terrain.HeightMap[x, y] = (short)((float)buffer[y * data.Stride + x * 3] * (float)(rangeDialog.Maximum - rangeDialog.Minimum) / 255.0f + (float)rangeDialog.Minimum);
}

this.changed = true;
this.terrain.UpdateMinMax();
this.initialize();
}
Expand All @@ -436,6 +463,70 @@ private void heightMapExport_Click(object sender, EventArgs e)
this.heightMapPreview.Image.Save(dialog.FileName);
}

private void heightMapImport16Bit_Click(object sender, EventArgs e)
{
if (this.terrain == null)
return;

OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = Properties.Settings.Default.OpenFileInitialDirectory;
if (dialog.ShowDialog() != DialogResult.OK)
return;

FileInfo input = new FileInfo(dialog.FileName);
if (input.Length < this.terrain.Width * this.terrain.Height * 2)
{
MessageBox.Show("The selected file is too small.", "Import");
return;
}

using (Stream stream = input.OpenRead())
{
byte[] row = new byte[terrain.Width * 2];

for (int y = 0; y < this.terrain.Height; y++)
{
if (stream.Read(row, 0, row.Length) < row.Length)
throw new Exception("Unexpected end of stream.");

for (int x = 0; x < this.terrain.Width; x++)
this.terrain.HeightMap[x, y] = (short)(row[x * 2] | row[x * 2 + 1] << 8);
}
}

this.changed = true;
this.terrain.UpdateMinMax();
this.initialize();
}

private void HeightMapExport16Bit_Click(object sender, EventArgs e)
{
if (this.terrain == null)
return;

SaveFileDialog dialog = new SaveFileDialog();
dialog.InitialDirectory = Properties.Settings.Default.SaveFileInitialDirectory;
if (dialog.ShowDialog() != DialogResult.OK)
return;

FileInfo output = new FileInfo(dialog.FileName);
using (Stream stream = output.Create())
{
byte[] row = new byte[terrain.Width * 2];

for (int y = 0; y < this.terrain.Height; y++)
{
for (int x = 0; x < this.terrain.Width; x++)
{
row[x * 2 + 0] = unchecked((byte)(this.terrain.HeightMap[x, y] & 0xFF));
row[x * 2 + 1] = unchecked((byte)(this.terrain.HeightMap[x, y] >> 8));
}

stream.Write(row, 0, row.Length);
}
}
}

#endregion

private void colorMapShow_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -500,7 +591,9 @@ private void alphaMap3Show_Click(object sender, EventArgs e)

#endregion


#endregion


}
}
48 changes: 33 additions & 15 deletions BZ2TerrainEditor/Editor.resx
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,39 @@
<metadata name="colorMapToolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>356, 16</value>
</metadata>
<metadata name="colorMapToolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>356, 16</value>
</metadata>
<metadata name="normalMapToolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>494, 19</value>
</metadata>
<metadata name="normalMapToolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>494, 19</value>
</metadata>
<metadata name="cellMapToolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>640, 18</value>
</metadata>
<metadata name="cellMapToolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>640, 18</value>
</metadata>
<metadata name="alphaMap1Toolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>774, 19</value>
</metadata>
<metadata name="alphaMap1Toolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>774, 19</value>
</metadata>
<metadata name="alphaMap2Toolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>921, 18</value>
</metadata>
<metadata name="alphaMap2Toolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>921, 18</value>
</metadata>
<metadata name="alphaMap3Toolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>1063, 18</value>
</metadata>
<metadata name="alphaMap3Toolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>1063, 18</value>
</metadata>
<data name="colorMapShow.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
Expand Down Expand Up @@ -216,9 +249,6 @@
O+QJGUst0RPTnMRxT1rKwaBcXi1Ga0VTk65KINj6T5CWcij3B2Y7WkVObicWAAAAAElFTkSuQmCC
</value>
</data>
<metadata name="normalMapToolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>494, 19</value>
</metadata>
<data name="normalMapShow.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
Expand Down Expand Up @@ -262,9 +292,6 @@
O+QJGUst0RPTnMRxT1rKwaBcXi1Ga0VTk65KINj6T5CWcij3B2Y7WkVObicWAAAAAElFTkSuQmCC
</value>
</data>
<metadata name="cellMapToolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>640, 18</value>
</metadata>
<data name="cellMapShow.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
Expand All @@ -280,9 +307,6 @@
BACizC0hHuvVJQAAAABJRU5ErkJggg==
</value>
</data>
<metadata name="alphaMap1Toolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>774, 19</value>
</metadata>
<data name="alphaMap1Show.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
Expand Down Expand Up @@ -326,9 +350,6 @@
O+QJGUst0RPTnMRxT1rKwaBcXi1Ga0VTk65KINj6T5CWcij3B2Y7WkVObicWAAAAAElFTkSuQmCC
</value>
</data>
<metadata name="alphaMap2Toolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>921, 18</value>
</metadata>
<data name="alphaMap2Show.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
Expand Down Expand Up @@ -372,9 +393,6 @@
O+QJGUst0RPTnMRxT1rKwaBcXi1Ga0VTk65KINj6T5CWcij3B2Y7WkVObicWAAAAAElFTkSuQmCC
</value>
</data>
<metadata name="alphaMap3Toolbar.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>1063, 18</value>
</metadata>
<data name="alphaMap3Show.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
Expand Down
33 changes: 17 additions & 16 deletions BZ2TerrainEditor/Terrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public static Terrain Read(Stream stream)

int version = reader.ReadInt32();

if(version != 1 && version != 3)
if(version < 1 || version > 3)
throw new NotSupportedException(string.Format("Version {0} is not supported.", version));

reader.ReadUInt16(); // some other width?
Expand All @@ -322,9 +322,9 @@ public static Terrain Read(Stream stream)
if (value < terrain.heightMapMin) terrain.heightMapMin = value;
if (value > terrain.heightMapMax) terrain.heightMapMax = value;
}
if (version == 1) reader.ReadInt16();
if (version < 3) reader.ReadInt16();
}
if (version == 1) reader.ReadBytes(10);
if (version < 3) reader.ReadBytes(10);

// normal map
for (int cy = 0; cy < 4; cy++)
Expand All @@ -333,9 +333,9 @@ public static Terrain Read(Stream stream)
{
terrain.NormalMap[x + cx, y + cy] = reader.ReadByte();
}
if (version == 1) reader.ReadByte();
if (version < 3) reader.ReadByte();
}
if (version == 1) reader.ReadBytes(5);
if (version < 3) reader.ReadBytes(5);

// color map
for (int cy = 0; cy < 4; cy++)
Expand All @@ -346,9 +346,9 @@ public static Terrain Read(Stream stream)
terrain.ColorMap[x + cx, y + cy].G = reader.ReadByte();
terrain.ColorMap[x + cx, y + cy].B = reader.ReadByte();
}
if (version == 1) reader.ReadBytes(3);
if (version < 3) reader.ReadBytes(3);
}
if (version == 1) reader.ReadBytes(15);
if (version < 3) reader.ReadBytes(15);

// alpha map 1
for (int cy = 0; cy < 4; cy++)
Expand All @@ -357,9 +357,9 @@ public static Terrain Read(Stream stream)
{
terrain.AlphaMap1[x + cx, y + cy] = reader.ReadByte();
}
if (version == 1) reader.ReadByte();
if (version < 3) reader.ReadByte();
}
if (version == 1) reader.ReadBytes(5);
if (version < 3) reader.ReadBytes(5);

// alpha map 2
for (int cy = 0; cy < 4; cy++)
Expand All @@ -368,9 +368,9 @@ public static Terrain Read(Stream stream)
{
terrain.AlphaMap2[x + cx, y + cy] = reader.ReadByte();
}
if (version == 1) reader.ReadByte();
if (version < 3) reader.ReadByte();
}
if (version == 1) reader.ReadBytes(5);
if (version < 3) reader.ReadBytes(5);

// alpha map 3
for (int cy = 0; cy < 4; cy++)
Expand All @@ -379,9 +379,9 @@ public static Terrain Read(Stream stream)
{
terrain.AlphaMap3[x + cx, y + cy] = reader.ReadByte();
}
if (version == 1) reader.ReadByte();
if (version < 3) reader.ReadByte();
}
if (version == 1) reader.ReadBytes(5);
if (version < 3) reader.ReadBytes(5);

// cliff map
for (int cy = 0; cy < 4; cy++)
Expand All @@ -390,15 +390,16 @@ public static Terrain Read(Stream stream)
{
terrain.CellMap[x + cx, y + cy] = (CellType)reader.ReadByte();
}
if (version == 1) reader.ReadByte();
if (version < 3) reader.ReadByte();
}
if (version == 1) reader.ReadBytes(5);
if (version < 3) reader.ReadBytes(5);

// info map
terrain.InfoMap[x / 4, y / 4] = reader.ReadUInt32();

// ???
if (version == 1) reader.ReadBytes(25);
if (version < 3) reader.ReadBytes(25);
if (version == 2) reader.ReadByte();
}
}

Expand Down

0 comments on commit e190dfa

Please sign in to comment.