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,24 +62,22 @@ 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();
var bmp = reader.Data;
if (bmp[0] != 'B' || bmp[1] != 'M')
return null;
int width = LittleEndian.ToInt32 (bmp, 0x12);
int height = LittleEndian.ToInt32 (bmp, 0x16);
int bpp = LittleEndian.ToInt16 (bmp, 0x1c);
return new GcpMetaData
{ {
reader.Unpack(); Width = (uint)width,
var bmp = reader.Data; Height = (uint)height,
if (bmp[0] != 'B' || bmp[1] != 'M') BPP = bpp,
return null; DataSize = data_size,
int width = LittleEndian.ToInt32 (bmp, 0x12); PackedSize = pack_size,
int height = LittleEndian.ToInt32 (bmp, 0x16); };
int bpp = LittleEndian.ToInt16 (bmp, 0x1c);
return new GcpMetaData
{
Width = (uint)width,
Height = (uint)height,
BPP = bpp,
DataSize = data_size,
PackedSize = pack_size,
};
}
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (Stream stream, ImageMetaData info)
@ -89,22 +87,20 @@ 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();
using (var bmp = new MemoryStream (reader.Data, false))
{ {
reader.Unpack(); var decoder = new BmpBitmapDecoder (bmp,
using (var bmp = new MemoryStream (reader.Data, false)) BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
{ BitmapSource frame = decoder.Frames[0];
var decoder = new BmpBitmapDecoder (bmp, frame.Freeze();
BitmapCreateOptions.None, BitmapCacheOption.OnLoad); return new ImageData (frame, info);
BitmapSource frame = decoder.Frames[0];
frame.Freeze();
return new ImageData (frame, info);
}
} }
} }
} }
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;
} }
return data; int mask = (1 << count) - 1;
m_cached_bits -= count;
return (m_bits >> m_cached_bits) & mask;
} }
#region IDisposable Members
public void Dispose ()
{
if (null != m_input)
{
m_input = null;
}
GC.SuppressFinalize (this);
}
#endregion
} }
} }