shp writer

This commit is contained in:
Chris Forbes
2011-01-22 21:16:12 +13:00
committed by Paul Chote
parent 848622054d
commit 1b3f769f34
5 changed files with 66 additions and 7 deletions

View File

@@ -10,10 +10,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Drawing;
namespace OpenRA namespace OpenRA
{ {

View File

@@ -9,6 +9,7 @@
#endregion #endregion
using System; using System;
using System.IO;
namespace OpenRA.FileFormats namespace OpenRA.FileFormats
{ {
@@ -124,24 +125,24 @@ namespace OpenRA.FileFormats
} }
} }
public static int EncodeInto(byte[] src, byte[] dest) public static byte[] Encode(byte[] src)
{ {
/* quick & dirty format80 encoder -- only uses raw copy operator, terminated with a zero-run. */ /* quick & dirty format80 encoder -- only uses raw copy operator, terminated with a zero-run. */
/* this does not produce good compression, but it's valid format80 */ /* this does not produce good compression, but it's valid format80 */
var destIndex = 0;
var ctx = new FastByteReader(src); var ctx = new FastByteReader(src);
var ms = new MemoryStream();
do do
{ {
var len = Math.Min(ctx.Remaining(), 0x3F); var len = Math.Min(ctx.Remaining(), 0x3F);
dest[destIndex++] = (byte)(0x80 | len); ms.WriteByte((byte)(0x80 | len));
if (len > 0) while (len-- > 0)
ctx.CopyTo(dest, destIndex, len); ms.WriteByte(ctx.ReadByte());
} }
while (!ctx.Done()); while (!ctx.Done());
return destIndex; return ms.ToArray();
} }
} }
} }

View File

@@ -26,6 +26,8 @@ namespace OpenRA.FileFormats
public byte[] Image; public byte[] Image;
public ImageHeader() { }
public ImageHeader( BinaryReader reader ) public ImageHeader( BinaryReader reader )
{ {
Offset = reader.ReadUInt32(); Offset = reader.ReadUInt32();
@@ -35,6 +37,15 @@ namespace OpenRA.FileFormats
RefOffset = reader.ReadUInt16(); RefOffset = reader.ReadUInt16();
RefFormat = (Format)reader.ReadUInt16(); RefFormat = (Format)reader.ReadUInt16();
} }
public static readonly int SizeOnDisk = 8;
public void WriteTo(BinaryWriter writer)
{
writer.Write(Offset | ((uint)Format << 24));
writer.Write((ushort)RefOffset);
writer.Write((ushort)RefFormat);
}
} }
public enum Format public enum Format

View File

@@ -0,0 +1,46 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace OpenRA.FileFormats.Graphics
{
// format80-only SHP writer
public static class ShpWriter
{
public static void Write(Stream s, int width, int height, IEnumerable<byte[]> frames)
{
var compressedFrames = frames.Select(f => Format80.Encode(f)).ToArray();
// note: end-of-file and all-zeroes headers
var dataOffset = 14 + (compressedFrames.Length + 2) * ImageHeader.SizeOnDisk;
using (var bw = new BinaryWriter(s))
{
bw.Write((ushort)compressedFrames.Length);
bw.Write((ushort)0); // unused
bw.Write((ushort)0); // unused
bw.Write((ushort)width);
bw.Write((ushort)height);
bw.Write((uint)0); // unused
foreach (var f in compressedFrames)
{
var ih = new ImageHeader { Format = Format.Format80, Offset = (uint)dataOffset };
dataOffset += f.Length;
ih.WriteTo(bw);
}
var eof = new ImageHeader { Format = Format.Format80, Offset = (uint)dataOffset };
eof.WriteTo(bw);
var allZeroes = new ImageHeader { };
allZeroes.WriteTo(bw);
foreach (var f in compressedFrames)
bw.Write(f);
}
}
}
}

View File

@@ -66,6 +66,7 @@
<Compile Include="FieldLoader.cs" /> <Compile Include="FieldLoader.cs" />
<Compile Include="Graphics\IGraphicsDevice.cs" /> <Compile Include="Graphics\IGraphicsDevice.cs" />
<Compile Include="Graphics\IInputHandler.cs" /> <Compile Include="Graphics\IInputHandler.cs" />
<Compile Include="Graphics\ShpWriter.cs" />
<Compile Include="Graphics\Vertex.cs" /> <Compile Include="Graphics\Vertex.cs" />
<Compile Include="HttpUtil.cs" /> <Compile Include="HttpUtil.cs" />
<Compile Include="Manifest.cs" /> <Compile Include="Manifest.cs" />