MOAR PERF

git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1956 993157c7-ee19-0410-b2c4-bb4e9862e678
This commit is contained in:
chrisf
2008-03-25 02:07:15 +00:00
parent f218d212ff
commit b77a116f60
7 changed files with 98 additions and 123 deletions

View File

@@ -7,35 +7,22 @@ namespace OpenRa.FileFormats
{
public static class Format40
{
static byte ReadByte( MemoryStream input )
{
int inp = input.ReadByte();
if( inp == -1 )
throw new InvalidDataException();
return (byte)inp;
}
static int ReadWord( MemoryStream input )
{
int inp = ReadByte( input );
return inp + ( ReadByte( input ) << 8 );
}
public static int DecodeInto( MemoryStream input, byte[] dest )
public static int DecodeInto( byte[] src, byte[] dest )
{
var ctx = new FastByteReader(src);
int destIndex = 0;
while( true )
{
byte i = ReadByte( input );
byte i = ctx.ReadByte();
if( ( i & 0x80 ) == 0 )
{
int count = i & 0x7F;
if( count == 0 )
{
// case 6
count = ReadByte( input );
byte value = ReadByte( input );
count = ctx.ReadByte();
byte value = ctx.ReadByte();
for( int end = destIndex + count ; destIndex < end ; destIndex++ )
dest[ destIndex ] ^= value;
}
@@ -43,7 +30,7 @@ namespace OpenRa.FileFormats
{
// case 5
for( int end = destIndex + count ; destIndex < end ; destIndex++ )
dest[ destIndex ] ^= ReadByte( input );
dest[destIndex] ^= ctx.ReadByte();
}
}
else
@@ -51,7 +38,7 @@ namespace OpenRa.FileFormats
int count = i & 0x7F;
if( count == 0 )
{
count = ReadWord( input );
count = ctx.ReadWord();
if( count == 0 )
return destIndex;
@@ -64,12 +51,12 @@ namespace OpenRa.FileFormats
{
// case 3
for( int end = destIndex + ( count & 0x3FFF ) ; destIndex < end ; destIndex++ )
dest[ destIndex ] ^= ReadByte( input );
dest[destIndex] ^= ctx.ReadByte();
}
else
{
// case 4
byte value = ReadByte( input );
byte value = ctx.ReadByte();
for( int end = destIndex + ( count & 0x3FFF ) ; destIndex < end ; destIndex++ )
dest[ destIndex ] ^= value;
}

View File

@@ -5,23 +5,33 @@ using System.IO;
namespace OpenRa.FileFormats
{
class FastByteReader
{
readonly byte[] src;
int offset = 0;
public FastByteReader(byte[] src)
{
this.src = src;
}
public bool Done() { return offset >= src.Length; }
public byte ReadByte() { return src[offset++]; }
public int ReadWord()
{
int x = ReadByte();
return x | (ReadByte() << 8);
}
public void CopyTo(byte[] dest, int offset, int count)
{
Array.Copy(src, this.offset, dest, offset, count);
this.offset += count;
}
}
public static class Format80
{
static byte ReadByte( MemoryStream input )
{
int inp = input.ReadByte();
if( inp == -1 )
throw new InvalidDataException();
return (byte)inp;
}
static int ReadWord( MemoryStream input )
{
int inp = ReadByte( input );
return inp + ( ReadByte( input ) << 8 );
}
static void ReplicatePrevious( byte[] dest, int destIndex, int srcIndex, int count )
{
if( srcIndex >= destIndex )
@@ -39,16 +49,18 @@ namespace OpenRa.FileFormats
}
}
public static int DecodeInto( MemoryStream input, byte[] dest )
public static int DecodeInto( byte[] src, byte[] dest )
{
var ctx = new FastByteReader(src);
int destIndex = 0;
while( true )
{
byte i = ReadByte( input );
byte i = ctx.ReadByte();
if( ( i & 0x80 ) == 0 )
{
// case 2
byte secondByte = ReadByte( input );
byte secondByte = ctx.ReadByte();
int count = ( ( i & 0x70 ) >> 4 ) + 3;
int rpos = ( ( i & 0xf ) << 8 ) + secondByte;
@@ -62,7 +74,7 @@ namespace OpenRa.FileFormats
if( count == 0 )
return destIndex;
input.Read( dest, destIndex, count );
ctx.CopyTo( dest, destIndex, count );
destIndex += count;
}
else
@@ -71,8 +83,8 @@ namespace OpenRa.FileFormats
if( count3 == 0x3E )
{
// case 4
int count = ReadWord( input );
byte color = ReadByte( input );
int count = ctx.ReadWord();
byte color = ctx.ReadByte();
for( int end = destIndex + count ; destIndex < end ; destIndex++ )
dest[ destIndex ] = color;
@@ -80,8 +92,8 @@ namespace OpenRa.FileFormats
else if( count3 == 0x3F )
{
// case 5
int count = ReadWord( input );
int srcIndex = ReadWord( input );
int count = ctx.ReadWord();
int srcIndex = ctx.ReadWord();
if( srcIndex >= destIndex )
throw new NotImplementedException( string.Format( "srcIndex >= destIndex {0} {1}", srcIndex, destIndex ) );
@@ -92,7 +104,7 @@ namespace OpenRa.FileFormats
{
// case 3
int count = count3 + 3;
int srcIndex = ReadWord( input );
int srcIndex = ctx.ReadWord();
if( srcIndex >= destIndex )
throw new NotImplementedException( string.Format( "srcIndex >= destIndex {0} {1}", srcIndex, destIndex ) );

View File

@@ -72,7 +72,7 @@ namespace OpenRa.FileFormats
byte[] dest = new byte[8192];
byte[] src = reader.ReadBytes((int)length);
int actualLength = Format80.DecodeInto(new MemoryStream(src), dest);
int actualLength = Format80.DecodeInto(src, dest);
chunks.Add(dest);
}
@@ -113,7 +113,7 @@ namespace OpenRa.FileFormats
for( int i = 0 ; i < 128 ; i++ )
for( int j = 0 ; j < 128 ; j++ )
{
MapTiles[ j, i ].image = ReadByte( ms );
MapTiles[j, i].image = (byte)ms.ReadByte();// ReadByte(ms);
if( MapTiles[ j, i ].tile == 0xff || MapTiles[ j, i ].tile == 0xffff )
MapTiles[ j, i ].image = (byte)( i % 4 + ( j % 4 ) * 4 );
}

View File

@@ -109,16 +109,13 @@ namespace OpenRa.FileFormats
}
h.Image = CopyImageData( h.RefImage.Image );
MemoryStream ms = ReadCompressedData( stream, h );
Format40.DecodeInto( ms, h.Image );
Format40.DecodeInto(ReadCompressedData(stream, h), h.Image);
break;
}
case Format.Format80:
{
MemoryStream ms = ReadCompressedData( stream, h );
byte[] imageBytes = new byte[ Width * Height ];
Format80.DecodeInto( ms, imageBytes );
Format80.DecodeInto( ReadCompressedData( stream, h ), imageBytes );
h.Image = imageBytes;
break;
}
@@ -127,7 +124,7 @@ namespace OpenRa.FileFormats
}
}
private static MemoryStream ReadCompressedData( Stream stream, ImageHeader h )
static byte[] ReadCompressedData( Stream stream, ImageHeader h )
{
stream.Position = h.Offset;
// Actually, far too big. There's no length field with the correct length though :(
@@ -136,8 +133,8 @@ namespace OpenRa.FileFormats
byte[] compressedBytes = new byte[ compressedLength ];
stream.Read( compressedBytes, 0, compressedLength );
MemoryStream ms = new MemoryStream( compressedBytes );
return ms;
//MemoryStream ms = new MemoryStream( compressedBytes );
return compressedBytes;
}
private byte[] CopyImageData( byte[] baseImage )