slightly improved CMP1 decompression performance.

This commit is contained in:
morkt 2015-08-21 02:21:31 +04:00
parent d38386ae16
commit 18e7d1f849

View File

@ -45,7 +45,7 @@ namespace GameRes.Formats.Riddle
public class GcpFormat : ImageFormat public class GcpFormat : ImageFormat
{ {
public override string Tag { get { return "GCP"; } } public override string Tag { get { return "GCP"; } }
public override string Description { get { return "Riddle Soft RGB image format"; } } public override string Description { get { return "Riddle Soft compressed bitmap"; } }
public override uint Signature { get { return 0x31504d43u; } } // 'CMP1' public override uint Signature { get { return 0x31504d43u; } } // 'CMP1'
public override void Write (Stream file, ImageData image) public override void Write (Stream file, ImageData image)
@ -62,8 +62,7 @@ namespace GameRes.Formats.Riddle
int pack_size = LittleEndian.ToInt32 (header, 8); int pack_size = LittleEndian.ToInt32 (header, 8);
if (data_size < 54) if (data_size < 54)
return null; return null;
using (var reader = new Reader (stream, pack_size, 0x22)) // BMP header var reader = new CmpReader (stream, pack_size, 0x22); // BMP header
{
reader.Unpack(); reader.Unpack();
var bmp = reader.Data; var bmp = reader.Data;
if (bmp[0] != 'B' || bmp[1] != 'M') if (bmp[0] != 'B' || bmp[1] != 'M')
@ -80,7 +79,6 @@ namespace GameRes.Formats.Riddle
PackedSize = pack_size, PackedSize = pack_size,
}; };
} }
}
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (Stream stream, ImageMetaData info)
{ {
@ -89,8 +87,7 @@ namespace GameRes.Formats.Riddle
throw new ArgumentException ("GcpFormat.Read should be supplied with GcpMetaData", "info"); throw new ArgumentException ("GcpFormat.Read should be supplied with GcpMetaData", "info");
stream.Position = 12; stream.Position = 12;
using (var reader = new Reader (stream, meta.PackedSize, meta.DataSize)) var reader = new CmpReader (stream, meta.PackedSize, meta.DataSize);
{
reader.Unpack(); reader.Unpack();
using (var bmp = new MemoryStream (reader.Data, false)) using (var bmp = new MemoryStream (reader.Data, false))
{ {
@ -102,9 +99,8 @@ namespace GameRes.Formats.Riddle
} }
} }
} }
}
internal class Reader : IDisposable internal class CmpReader
{ {
Stream m_input; Stream m_input;
byte[] m_output; byte[] m_output;
@ -113,7 +109,7 @@ namespace GameRes.Formats.Riddle
public byte[] Data { get { return m_output; } } public byte[] Data { get { return m_output; } }
public Reader (Stream file, int src_size, int dst_size) public CmpReader (Stream file, int src_size, int dst_size)
{ {
m_input = file; m_input = file;
m_output = new byte[dst_size]; m_output = new byte[dst_size];
@ -129,7 +125,7 @@ namespace GameRes.Formats.Riddle
shift[i] = 0x20; shift[i] = 0x20;
while (dst < m_output.Length) while (dst < m_output.Length)
{ {
int bit = GetNextBit(); int bit = GetBits (1);
if (-1 == bit) if (-1 == bit)
break; break;
if (1 == bit) if (1 == bit)
@ -164,46 +160,23 @@ namespace GameRes.Formats.Riddle
} }
int m_bits = 0; int m_bits = 0;
int m_cached_bits = 0;
int GetNextBit () int GetBits (int count)
{ {
bool bit = 0 != (m_bits & 0x80); while (m_cached_bits < count)
m_bits <<= 1;
if (0 == (m_bits & 0xff))
{ {
if (m_src_count++ >= m_src_total) if (m_src_count++ >= m_src_total)
return -1; return -1;
m_bits = m_input.ReadByte(); int b = m_input.ReadByte();
if (-1 == m_bits) if (-1 == b)
throw new EndOfStreamException ("Invalid compressed stream");
bit = 0 != (m_bits & 0x80);
m_bits = (m_bits << 1) | 1;
}
return bit ? 1 : 0;
}
int GetBits (int count, int data = 0)
{
while (count > 0)
{
int bit = GetNextBit();
if (-1 == bit)
return -1; return -1;
data = (data << 1) | bit; m_bits = (m_bits << 8) | b;
--count; m_cached_bits += 8;
}
int mask = (1 << count) - 1;
m_cached_bits -= count;
return (m_bits >> m_cached_bits) & mask;
} }
return data;
}
#region IDisposable Members
public void Dispose ()
{
if (null != m_input)
{
m_input = null;
}
GC.SuppressFinalize (this);
}
#endregion
} }
} }