IBinaryStream migration.

This commit is contained in:
morkt 2016-10-16 09:22:53 +04:00
parent d0c1d5da01
commit bb18303eb4
251 changed files with 3277 additions and 3630 deletions

View File

@ -248,7 +248,7 @@ namespace GameRes.Formats.AZSys
Buffer.BlockCopy (data, 0, header, 0, 0x10);
return new PrefixStream (header, asb);
}
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
uint ReadSysenvSeed (ArcView file, IEnumerable<Entry> dir, uint key)

View File

@ -54,51 +54,48 @@ namespace GameRes.Formats.AZSys
throw new System.NotImplementedException ("CpbFormat.Write not implemented");
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
stream.Seek (4, SeekOrigin.Current);
int type = stream.ReadByte();
int bpp = stream.ReadByte();
file.Position = 4;
int type = file.ReadByte();
int bpp = file.ReadByte();
if (24 != bpp && 32 != bpp)
throw new NotSupportedException ("Not supported CPB image format");
using (var input = new ArcView.Reader (stream))
int version = file.ReadInt16();
if (1 != version && 0 != version)
throw new NotSupportedException ("Not supported CPB image version");
var info = new CpbMetaData {
Type = type,
Version = version,
BPP = bpp,
};
if (1 == version)
{
int version = input.ReadInt16 ();
if (1 != version && 0 != version)
throw new NotSupportedException ("Not supported CPB image version");
var info = new CpbMetaData {
Type = type,
Version = version,
BPP = bpp,
};
if (1 == version)
{
input.ReadUInt32();
info.Width = input.ReadUInt16();
info.Height = input.ReadUInt16();
info.Channel[0] = input.ReadUInt32();
info.Channel[1] = input.ReadUInt32();
info.Channel[2] = input.ReadUInt32();
info.Channel[3] = input.ReadUInt32();
}
else
{
info.Width = input.ReadUInt16();
info.Height = input.ReadUInt16();
input.ReadUInt32();
info.Channel[0] = input.ReadUInt32();
info.Channel[1] = input.ReadUInt32();
info.Channel[2] = input.ReadUInt32();
info.Channel[3] = input.ReadUInt32();
}
info.DataOffset = (uint)stream.Position;
return info;
file.ReadUInt32();
info.Width = file.ReadUInt16();
info.Height = file.ReadUInt16();
info.Channel[0] = file.ReadUInt32();
info.Channel[1] = file.ReadUInt32();
info.Channel[2] = file.ReadUInt32();
info.Channel[3] = file.ReadUInt32();
}
else
{
info.Width = file.ReadUInt16();
info.Height = file.ReadUInt16();
file.ReadUInt32();
info.Channel[0] = file.ReadUInt32();
info.Channel[1] = file.ReadUInt32();
info.Channel[2] = file.ReadUInt32();
info.Channel[3] = file.ReadUInt32();
}
info.DataOffset = (uint)file.Position;
return info;
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var reader = new Reader (stream, (CpbMetaData)info);
var reader = new Reader (stream.AsStream, (CpbMetaData)info);
reader.Unpack();
return ImageData.Create (info, reader.Format, reader.Palette, reader.Data);
}

View File

@ -45,46 +45,38 @@ namespace GameRes.Formats.AZSys
public override string Description { get { return "AZ system image format"; } }
public override uint Signature { get { return 0x31505954; } } // 'TYP1'
public Typ1Format ()
{
Extensions = new string[] { "cpb" };
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
stream.Position = 4;
int bpp = stream.ReadByte();
bool has_palette = stream.ReadByte() != 0;
using (var input = new ArcView.Reader (stream))
var info = new Typ1MetaData { BPP = bpp };
info.Width = stream.ReadUInt16();
info.Height = stream.ReadUInt16();
uint packed_size = stream.ReadUInt32();
uint palette_size = 8 == bpp ? 0x400u : 0u;
if (packed_size+palette_size+0xE == stream.Length)
{
var info = new Typ1MetaData { BPP = bpp };
info.Width = input.ReadUInt16();
info.Height = input.ReadUInt16();
uint packed_size = input.ReadUInt32();
uint palette_size = 8 == bpp ? 0x400u : 0u;
if (packed_size+palette_size+0xE == stream.Length)
{
info.SeparateChannels = false;
info.HasPalette = palette_size > 0;
info.PackedSize = packed_size;
}
else
{
info.SeparateChannels = true;
info.HasPalette = has_palette;
info.Channel[0] = input.ReadUInt32();
info.Channel[1] = input.ReadUInt32();
info.Channel[2] = input.ReadUInt32();
info.Channel[3] = input.ReadUInt32();
}
return info;
info.SeparateChannels = false;
info.HasPalette = palette_size > 0;
info.PackedSize = packed_size;
}
else
{
info.SeparateChannels = true;
info.HasPalette = has_palette;
info.Channel[0] = stream.ReadUInt32();
info.Channel[1] = stream.ReadUInt32();
info.Channel[2] = stream.ReadUInt32();
info.Channel[3] = stream.ReadUInt32();
}
return info;
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (Typ1MetaData)info;
var reader = new Reader (stream, meta);
var reader = new Reader (stream.AsStream, meta);
reader.Unpack();
return ImageData.Create (meta, reader.Format, reader.Palette, reader.Data);
}

View File

@ -102,7 +102,7 @@ namespace GameRes.Formats.Abel
{
data[i] = (byte)(0xFF - data[i]);
}
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
Stream OpenCmpEntry (ArcFile arc, Entry entry)

View File

@ -52,9 +52,9 @@ namespace GameRes.Formats.Abel
Extensions = new string[] { "gps", "cmp" };
}
public override ImageMetaData ReadMetaData (IBinaryStream stream)
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
var header = stream.ReadHeader (0x29);
var header = file.ReadHeader (0x29);
if (header.Length != 0x29)
return null;
var gps = new GpsMetaData
@ -66,7 +66,8 @@ namespace GameRes.Formats.Abel
PackedSize = header.ToInt32 (0x15),
};
// read BMP header
using (var input = OpenGpsStream (stream, gps.Compression, 0x54))
using (var stream = OpenGpsStream (file, gps.Compression, 0x54))
using (var input = BinaryStream.FromStream (stream, file.Name))
{
var bmp_info = base.ReadMetaData (input);
if (null == bmp_info)
@ -76,11 +77,12 @@ namespace GameRes.Formats.Abel
}
}
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var gps = (GpsMetaData)info;
stream.Position = 0x29;
using (var input = OpenGpsStream (stream, gps.Compression, gps.UnpackedSize))
file.Position = 0x29;
using (var stream = OpenGpsStream (file, gps.Compression, gps.UnpackedSize))
using (var input = BinaryStream.FromStream (stream, file.Name))
return base.Read (input, info);
}
@ -89,30 +91,28 @@ namespace GameRes.Formats.Abel
throw new System.NotImplementedException ("GpsFormat.Write not implemented");
}
IBinaryStream OpenGpsStream (IBinaryStream input, byte compression, int unpacked_size)
Stream OpenGpsStream (IBinaryStream input, byte compression, int unpacked_size)
{
Stream gps = null;
if (0 == compression)
gps = new StreamRegion (input.AsStream, 0x29, true);
return new StreamRegion (input.AsStream, 0x29, true);
else if (1 == compression)
gps = OpenRLEStream (input.AsStream, unpacked_size);
return OpenRLEStream (input.AsStream, unpacked_size);
else if (2 == compression)
gps = new LzssStream (input.AsStream, LzssMode.Decompress, true);
return new LzssStream (input.AsStream, LzssMode.Decompress, true);
else if (3 == compression)
{
using (var lzss = new LzssStream (input.AsStream, LzssMode.Decompress, true))
gps = OpenRLEStream (lzss, unpacked_size);
return OpenRLEStream (lzss, unpacked_size);
}
else
throw new InvalidFormatException();
return new BinaryStream (gps);
}
Stream OpenRLEStream (Stream input, int output_size)
{
var output = new byte[output_size];
UnpackRLE (input, output);
return new MemoryStream (output);
return new BinMemoryStream (output, "");
}
void UnpackRLE (Stream input, byte[] output)

View File

@ -123,7 +123,7 @@ namespace GameRes.Formats.Actgs
arc.File.View.Read (entry.Offset, data, 0, entry.Size);
Decrypt (data, 1, data.Length-1, actarc.Key);
data[0] = (byte)'N';
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
if (arc.File.View.AsciiEqual (entry.Offset, "PAK "))
{
@ -136,7 +136,7 @@ namespace GameRes.Formats.Actgs
arc.File.View.Read (entry.Offset, header, 0, length);
Decrypt (header, 0, (int)length, actarc.Key);
if (entry.Size <= 0x20)
return new MemoryStream (header);
return new BinMemoryStream (header, entry.Name);
var rest = arc.File.CreateStream (entry.Offset+0x20, entry.Size-0x20);
return new PrefixStream (header, rest);
}

View File

@ -107,17 +107,15 @@ namespace GameRes.Formats.AdPack
throw new NotImplementedException ("EdtFormat.Write not implemented");
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x22];
if (header.Length != stream.Read (header, 0, header.Length))
var header = stream.ReadHeader (0x22);
if (!header.AsciiEqual (".TRUE\x8d\x5d\x8c\xcb\x00"))
return null;
if (!Binary.AsciiEqual (header, ".TRUE\x8d\x5d\x8c\xcb\x00"))
return null;
uint width = LittleEndian.ToUInt16 (header, 0x0e);
uint height = LittleEndian.ToUInt16 (header, 0x10);
uint comp_size = LittleEndian.ToUInt32 (header, 0x1a);
uint extra_size = LittleEndian.ToUInt32 (header, 0x1e);
uint width = header.ToUInt16 (0x0e);
uint height = header.ToUInt16 (0x10);
uint comp_size = header.ToUInt32 (0x1a);
uint extra_size = header.ToUInt32 (0x1e);
if (extra_size % 3 != 0 || 0 == extra_size)
return null;
@ -131,21 +129,18 @@ namespace GameRes.Formats.AdPack
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = info as EdtMetaData;
if (null == meta)
throw new ArgumentException ("EdtFormat.Read should be supplied with EdtMetaData", "info");
var meta = (EdtMetaData)info;
stream.Position = 0x22;
using (var reader = new Reader (stream, meta))
using (var reader = new Reader (stream.AsStream, meta))
{
reader.Unpack();
return ImageData.Create (meta, PixelFormats.Bgr24, null, reader.Data, (int)meta.Width*3);
}
}
internal class Reader : BitReader, IDisposable
internal sealed class Reader : BitReader, IDisposable
{
MemoryStream m_packed;
MemoryStream m_extra;
@ -244,24 +239,15 @@ namespace GameRes.Formats.AdPack
#region IDisposable Members
bool disposed = false;
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (!disposed)
{
if (disposing)
{
m_packed.Dispose();
m_extra.Dispose();
}
m_packed.Dispose();
m_extra.Dispose();
disposed = true;
}
GC.SuppressFinalize (this);
}
#endregion
}
@ -284,17 +270,15 @@ namespace GameRes.Formats.AdPack
throw new NotImplementedException ("Ed8Format.Write not implemented");
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x1a];
if (header.Length != stream.Read (header, 0, header.Length))
var header = stream.ReadHeader (0x1A);
if (!header.AsciiEqual (".8Bit\x8d\x5d\x8c\xcb\x00"))
return null;
if (!Binary.AsciiEqual (header, ".8Bit\x8d\x5d\x8c\xcb\x00"))
return null;
uint width = LittleEndian.ToUInt16 (header, 0x0e);
uint height = LittleEndian.ToUInt16 (header, 0x10);
uint palette_size = LittleEndian.ToUInt32 (header, 0x12);
uint comp_size = LittleEndian.ToUInt32 (header, 0x16);
uint width = header.ToUInt16 (0x0e);
uint height = header.ToUInt16 (0x10);
uint palette_size = header.ToUInt32 (0x12);
uint comp_size = header.ToUInt32 (0x16);
if (palette_size > 0x100)
return null;
@ -308,14 +292,11 @@ namespace GameRes.Formats.AdPack
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = info as Ed8MetaData;
if (null == meta)
throw new ArgumentException ("Ed8Format.Read should be supplied with Ed8MetaData", "info");
var meta = (Ed8MetaData)info;
stream.Position = 0x1a;
var reader = new Reader (stream, meta);
var reader = new Reader (stream.AsStream, meta);
reader.Unpack();
var palette = new BitmapPalette (reader.Palette);
return ImageData.Create (info, PixelFormats.Indexed8, palette, reader.Data, (int)info.Width);

View File

@ -38,20 +38,18 @@ namespace GameRes.Formats.AdvSys
public override string Description { get { return "AdvSys engine image format"; } }
public override uint Signature { get { return 0x5F325247; } } // 'GR2_'
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[16];
if (16 != stream.Read (header, 0, 16))
return null;
var header = stream.ReadHeader (0x10);
return new ImageMetaData
{
Width = LittleEndian.ToUInt16 (header, 4),
Height = LittleEndian.ToUInt16 (header, 6),
BPP = LittleEndian.ToInt16 (header, 12) * 8
Width = header.ToUInt16 (4),
Height = header.ToUInt16 (6),
BPP = header.ToInt16 (12) * 8
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
stream.Position = 0x10;
int stride = ((int)info.Width * info.BPP/8 + 3) & ~3;
@ -92,18 +90,16 @@ namespace GameRes.Formats.AdvSys
Extensions = new string[] { "gr2" };
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[20];
if (20 != stream.Read (header, 0, 20))
var header = stream.ReadHeader (20);
if (!header.AsciiEqual ("*Pola* "))
return null;
if (!Binary.AsciiEqual (header, "*Pola* "))
return null;
int unpacked_size = LittleEndian.ToInt32 (header, 8);
int unpacked_size = header.ToInt32 (8);
using (var reader = new PolaReader (stream, 64))
{
reader.Unpack();
using (var temp = new MemoryStream (reader.Data))
using (var temp = BinaryStream.FromArray (reader.Data, stream.Name))
{
var info = base.ReadMetaData (temp);
if (null == info)
@ -119,17 +115,14 @@ namespace GameRes.Formats.AdvSys
}
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = info as PolaMetaData;
if (null == meta)
throw new ArgumentException ("PolaFormat.Read should be supplied with PolaMetaData", "info");
var meta = (PolaMetaData)info;
stream.Position = 0x14;
using (var reader = new PolaReader (stream, meta.UnpackedSize))
{
reader.Unpack();
using (var temp = new MemoryStream (reader.Data))
using (var temp = BinaryStream.FromArray (reader.Data, stream.Name))
return base.Read (temp, info);
}
}
@ -142,14 +135,14 @@ namespace GameRes.Formats.AdvSys
internal sealed class PolaReader : IDisposable
{
BinaryReader m_input;
IBinaryStream m_input;
byte[] m_output;
public byte[] Data { get { return m_output; } }
public PolaReader (Stream input, int unpacked_size)
public PolaReader (IBinaryStream input, int unpacked_size)
{
m_input = new ArcView.Reader (input);
m_input = input;
m_output = new byte[unpacked_size+2];
}
@ -161,14 +154,14 @@ namespace GameRes.Formats.AdvSys
{
if (0 != NextBit())
{
m_output[dst++] = m_input.ReadByte();
m_output[dst++] = m_input.ReadUInt8();
continue;
}
int offset, count = 0;
if (0 != NextBit())
{
offset = m_input.ReadByte() - 256;
offset = m_input.ReadUInt8() - 256;
if (0 == NextBit())
{
@ -228,7 +221,7 @@ namespace GameRes.Formats.AdvSys
}
else
{
count = m_input.ReadByte() + 17;
count = m_input.ReadUInt8() + 17;
}
if (dst + count > m_output.Length)
count = m_output.Length - dst;
@ -237,7 +230,7 @@ namespace GameRes.Formats.AdvSys
}
else
{
offset = m_input.ReadByte() - 256;
offset = m_input.ReadUInt8() - 256;
if (0 == NextBit())
{
if (offset != -1)
@ -280,14 +273,8 @@ namespace GameRes.Formats.AdvSys
}
#region IDisposable Members
bool m_disposed = false;
public void Dispose ()
{
if (!m_disposed)
{
m_input.Dispose();
m_disposed = true;
}
}
#endregion
}

View File

@ -48,7 +48,7 @@ namespace GameRes.Formats.AdvSys
var header = stream.ReadHeader (12);
if (header.Length != 12)
return null;
if (!Binary.AsciiEqual (header, 4, "GWD"))
if (!header.AsciiEqual (4, "GWD"))
return null;
return new GwdMetaData
{
@ -72,7 +72,7 @@ namespace GameRes.Formats.AdvSys
if (24 == info.BPP && 1 == stream.ReadByte())
{
using (var part = new StreamRegion (stream.AsStream, stream.Position, true))
using (var alpha_stream = new BinaryStream (part))
using (var alpha_stream = new BinaryStream (part, stream.Name))
{
var alpha_info = ReadMetaData (alpha_stream) as GwdMetaData;
if (null != alpha_info && 8 == alpha_info.BPP

View File

@ -101,7 +101,7 @@ namespace GameRes.Formats.AliceSoft
for (int i = 0; i < prefix.Length; ++i)
prefix[i] ^= AffKey[i & 0xF];
if (data_size <= 0x40)
return new MemoryStream (prefix);
return new BinMemoryStream (prefix, entry.Name);
var rest = arc.File.CreateStream (entry.Offset+0x10+encrypted_length, data_size-encrypted_length);
return new PrefixStream (prefix, rest);
}

View File

@ -137,13 +137,15 @@ namespace GameRes.Formats.AliceSoft
long qnt_pos = m_input.Position;
if (m_input.ReadUInt32() != Qnt.Signature)
throw new InvalidFormatException();
m_input.Seek (-4, SeekOrigin.Current);
var qnt_info = Qnt.ReadMetaData (m_input) as QntMetaData;
QntMetaData qnt_info;
using (var reg = new StreamRegion (m_input.AsStream, qnt_pos, true))
using (var qnt = new BinaryStream (reg, m_input.Name))
qnt_info = Qnt.ReadMetaData (qnt) as QntMetaData;
if (null == qnt_info)
throw new InvalidFormatException();
m_input.Position = qnt_pos + 0x44;
var overlay = new QntFormat.Reader (m_input, qnt_info);
var overlay = new QntFormat.Reader (m_input.AsStream, qnt_info);
overlay.Unpack();
m_overlay_bpp = overlay.BPP;
if (m_mask != null)
@ -213,13 +215,13 @@ namespace GameRes.Formats.AliceSoft
string dir_name = VFS.GetDirectoryName (m_info.FileName);
string base_name = Path.ChangeExtension (m_info.BaseName, "qnt");
base_name = VFS.CombinePath (dir_name, base_name);
using (var base_file = VFS.OpenSeekableStream (base_name))
using (var base_file = VFS.OpenBinaryStream (base_name))
{
var base_info = Qnt.ReadMetaData (base_file) as QntMetaData;
if (null != base_info && m_info.Width == base_info.Width && m_info.Height == base_info.Height)
{
base_info.FileName = base_name;
var reader = new QntFormat.Reader (base_file, base_info);
var reader = new QntFormat.Reader (base_file.AsStream, base_info);
reader.Unpack();
m_base_bpp = reader.BPP;
m_base = reader.Data;

View File

@ -49,36 +49,34 @@ namespace GameRes.Formats.AliceSoft
throw new System.NotImplementedException ("QntFormat.Write not implemented");
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x44];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
int version = LittleEndian.ToInt32 (header, 4);
var header = stream.ReadHeader (0x44);
int version = header.ToInt32 (4);
if (version <= 0 || version > 2)
return null;
if (0x44 != LittleEndian.ToUInt32 (header, 8))
if (0x44 != header.ToUInt32 (8))
return null;
uint width = LittleEndian.ToUInt32 (header, 0x14);
uint height = LittleEndian.ToUInt32 (header, 0x18);
uint width = header.ToUInt32 (0x14);
uint height = header.ToUInt32 (0x18);
if (0 == width || 0 == height)
return null;
return new QntMetaData
{
Width = width,
Height = height,
OffsetX = LittleEndian.ToInt32 (header, 0x0c),
OffsetY = LittleEndian.ToInt32 (header, 0x10),
BPP = LittleEndian.ToInt32 (header, 0x1c),
RGBSize = LittleEndian.ToUInt32 (header, 0x24),
AlphaSize = LittleEndian.ToUInt32 (header, 0x28),
OffsetX = header.ToInt32 (0x0c),
OffsetY = header.ToInt32 (0x10),
BPP = header.ToInt32 (0x1c),
RGBSize = header.ToUInt32 (0x24),
AlphaSize = header.ToUInt32 (0x28),
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
stream.Position = 0x44;
var reader = new Reader (stream, (QntMetaData)info);
var reader = new Reader (stream.AsStream, (QntMetaData)info);
reader.Unpack();
int stride = (int)info.Width * (reader.BPP / 8);
PixelFormat format = 24 == reader.BPP ? PixelFormats.Bgr24 : PixelFormats.Bgra32;

View File

@ -272,7 +272,7 @@ namespace GameRes.Formats.Amaterasu
uint WriteAmiEntry (PackedEntry entry, Stream output)
{
uint packed_size = 0;
using (var input = File.OpenRead (entry.Name))
using (var input = VFS.OpenBinaryStream (entry))
{
long file_size = input.Length;
if (file_size > uint.MaxValue)
@ -284,7 +284,7 @@ namespace GameRes.Formats.Amaterasu
}
else
{
input.CopyTo (output);
input.AsStream.CopyTo (output);
}
}
return packed_size;
@ -293,19 +293,19 @@ namespace GameRes.Formats.Amaterasu
static Lazy<GrpFormat> s_grp_format = new Lazy<GrpFormat> (() =>
FormatCatalog.Instance.ImageFormats.OfType<GrpFormat>().FirstOrDefault());
uint WriteImageEntry (PackedEntry entry, Stream input, Stream output)
uint WriteImageEntry (PackedEntry entry, IBinaryStream input, Stream output)
{
var grp = s_grp_format.Value;
if (null == grp) // probably never happens
throw new FileFormatException ("GRP image encoder not available");
bool is_grp = grp.Signature == FormatCatalog.ReadSignature (input);
bool is_grp = grp.Signature == input.Signature;
input.Position = 0;
var start = output.Position;
using (var zstream = new ZLibStream (output, CompressionMode.Compress, CompressionLevel.Level9, true))
{
if (is_grp)
{
input.CopyTo (zstream);
input.AsStream.CopyTo (zstream);
}
else
{

View File

@ -40,23 +40,19 @@ namespace GameRes.Formats.Amaterasu
public override uint Signature { get { return 0x00505247; } } // 'GRP'
public override bool CanWrite { get { return true; } }
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
using (var file = new BinaryReader (stream, Encoding.ASCII, true))
{
if (file.ReadUInt32() != Signature)
return null;
var meta = new ImageMetaData();
meta.OffsetX = file.ReadInt16();
meta.OffsetY = file.ReadInt16();
meta.Width = file.ReadUInt16();
meta.Height = file.ReadUInt16();
meta.BPP = 32;
return meta;
}
file.Position = 4;
var meta = new ImageMetaData();
meta.OffsetX = file.ReadInt16();
meta.OffsetY = file.ReadInt16();
meta.Width = file.ReadUInt16();
meta.Height = file.ReadUInt16();
meta.BPP = 32;
return meta;
}
public override ImageData Read (Stream file, ImageMetaData info)
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
int width = (int)info.Width;
int height = (int)info.Height;

View File

@ -92,7 +92,7 @@ namespace GameRes.Formats.Ags
data[i] ^= key;
key += earc.Key.Increment;
}
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
public static readonly EncryptionScheme DefaultScheme = new EncryptionScheme {

View File

@ -41,12 +41,12 @@ namespace GameRes.Formats.Ags
Extensions = new string[] { "pcm" };
}
public override SoundInput TryOpen (Stream file)
public override SoundInput TryOpen (IBinaryStream file)
{
uint signature = FormatCatalog.ReadSignature (file) & 0xF0FFFFFF;
uint signature = file.Signature & 0xF0FFFFFF;
if (0x564157 != signature) // 'WAV'
return null;
return new PcmInput (file);
return new PcmInput (file.AsStream);
}
}

View File

@ -28,10 +28,7 @@ using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using GameRes.Utility;
namespace GameRes.Formats.Ags
@ -196,40 +193,37 @@ namespace GameRes.Formats.Ags
throw new System.NotImplementedException ("CgFormat.Write not implemented");
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
int sig = stream.ReadByte();
int sig = file.ReadByte();
if (sig >= 0x20)
return null;
using (var input = new ArcView.Reader (stream))
int width = file.ReadInt16();
int height = file.ReadInt16();
if (width <= 0 || height <= 0 || width > 4096 || height > 4096)
return null;
var meta = new CgMetaData
{
int width = input.ReadInt16();
int height = input.ReadInt16();
if (width <= 0 || height <= 0 || width > 4096 || height > 4096)
Width = (uint)width,
Height = (uint)height,
BPP = 24,
Type = sig,
};
if (0 != (sig & 7))
{
meta.OffsetX = file.ReadInt16();
meta.OffsetY = file.ReadInt16();
meta.Right = file.ReadInt16();
meta.Bottom = file.ReadInt16();
if (meta.OffsetX > meta.Right || meta.OffsetY > meta.Bottom ||
meta.Right > width || meta.Bottom > height ||
meta.OffsetX < 0 || meta.OffsetY < 0)
return null;
var meta = new CgMetaData
{
Width = (uint)width,
Height = (uint)height,
BPP = 24,
Type = sig,
};
if (0 != (sig & 7))
{
meta.OffsetX = input.ReadInt16();
meta.OffsetY = input.ReadInt16();
meta.Right = input.ReadInt16();
meta.Bottom = input.ReadInt16();
if (meta.OffsetX > meta.Right || meta.OffsetY > meta.Bottom ||
meta.Right > width || meta.Bottom > height ||
meta.OffsetX < 0 || meta.OffsetY < 0)
return null;
}
return meta;
}
return meta;
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (CgMetaData)info;
using (var reader = new Reader (stream, meta))
@ -239,9 +233,9 @@ namespace GameRes.Formats.Ags
}
}
internal class Reader : IDisposable
internal sealed class Reader : IDisposable
{
BinaryReader m_input;
IBinaryStream m_input;
byte[] m_output;
int m_type;
int m_width;
@ -253,7 +247,7 @@ namespace GameRes.Formats.Ags
public byte[] Data { get { return m_output; } }
public Reader (Stream file, CgMetaData info, byte[] base_image = null)
public Reader (IBinaryStream file, CgMetaData info, byte[] base_image = null)
{
m_type = info.Type;
m_width = (int)info.Width;
@ -263,13 +257,13 @@ namespace GameRes.Formats.Ags
m_right = info.Right == 0 ? m_width : info.Right;
m_bottom = info.Bottom == 0 ? m_height : info.Bottom;
m_output = base_image ?? new byte[3*m_width*m_height];
m_input = new BinaryReader (file, Encoding.ASCII, true);
m_input = file;
ShiftTable = InitShiftTable();
if (0 != (info.Type & 7))
file.Position = 13;
m_input.Position = 13;
else
file.Position = 5;
m_input.Position = 5;
}
static readonly short[] ShiftX = new short[] { // 409b6c
@ -307,7 +301,7 @@ namespace GameRes.Formats.Ags
int dst = left;
while (dst != right)
{
byte v9 = m_input.ReadByte();
byte v9 = m_input.ReadUInt8();
if (0 != (v9 & 0x80))
{
if (0 != (v9 & 0x40))
@ -318,10 +312,10 @@ namespace GameRes.Formats.Ags
}
else
{
byte v15 = m_input.ReadByte();
byte v15 = m_input.ReadUInt8();
m_output[dst] = (byte)(((v9 << 1) + (v15 & 1)) << 1);
m_output[dst + 1] = (byte)(v15 & 0xfe);
m_output[dst + 2] = m_input.ReadByte();
m_output[dst + 2] = m_input.ReadUInt8();
}
dst += 3;
continue;
@ -330,13 +324,13 @@ namespace GameRes.Formats.Ags
int count = v9 & 0xF;
if (0 == count)
{
count = (int)m_input.ReadByte() + 15;
count = (int)m_input.ReadUInt8() + 15;
if (270 == count)
{
int v12;
do
{
v12 = m_input.ReadByte();
v12 = m_input.ReadUInt8();
count += v12;
}
while (v12 == 0xff);
@ -364,7 +358,7 @@ namespace GameRes.Formats.Ags
int dst = left;
while (dst != right)
{
byte v13 = m_input.ReadByte();
byte v13 = m_input.ReadUInt8();
if (0 != (v13 & 0x80))
{
int color = 3 * (v13 & 0x7F);
@ -378,13 +372,13 @@ namespace GameRes.Formats.Ags
int count = v13 & 0xF;
if (0 == count)
{
count = m_input.ReadByte() + 15;
count = m_input.ReadUInt8() + 15;
if (270 == count)
{
int v16;
do
{
v16 = m_input.ReadByte();
v16 = m_input.ReadUInt8();
count += v16;
}
while (v16 == 0xff);
@ -403,25 +397,10 @@ namespace GameRes.Formats.Ags
}
#region IDisposable Members
bool disposed = false;
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (!disposed)
{
if (disposing)
{
m_input.Dispose();
}
disposed = true;
}
}
#endregion
}
}

View File

@ -178,7 +178,7 @@ namespace GameRes.Formats.Ankh
{
var unpacked = new byte[entry.UnpackedSize];
reader.UnpackHDJ (unpacked, 0);
return new MemoryStream (unpacked);
return new BinMemoryStream (unpacked, entry.Name);
}
}
@ -201,7 +201,7 @@ namespace GameRes.Formats.Ankh
reader.UnpackA (unpacked, header_size, channels);
else
reader.UnpackS (unpacked, header_size, channels);
return new MemoryStream (unpacked);
return new BinMemoryStream (unpacked, entry.Name);
}
}
@ -276,7 +276,7 @@ namespace GameRes.Formats.Ankh
dst += count;
}
}
return new MemoryStream (output);
return new BinMemoryStream (output, entry.Name);
}
}
}

View File

@ -65,7 +65,7 @@ namespace GameRes.Formats.Ice
if (0 == format.AverageBytesPerSecond
|| format.SamplesPerSecond * format.BlockAlign != format.AverageBytesPerSecond)
return null;
var pcm = new StreamRegion (file, 0x16, pcm_size);
var pcm = new StreamRegion (file.AsStream, 0x16, pcm_size);
return new RawPcmInput (pcm, format);
}
}

View File

@ -188,7 +188,7 @@ namespace GameRes.Formats.Aoi
{
data[i] ^= KeyFromOffset (offset++);
}
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
static byte KeyFromOffset (uint offset)

View File

@ -36,14 +36,12 @@ namespace GameRes.Formats.Aoi
public override string Description { get { return "Aoi engine audio format"; } }
public override uint Signature { get { return 0x4F696F41; } } // 'AoiO'
public override SoundInput TryOpen (Stream file)
public override SoundInput TryOpen (IBinaryStream file)
{
var header = new byte[0x30];
if (header.Length != file.Read (header, 0, header.Length))
var header = file.ReadHeader (0x30);
if (!header.AsciiEqual (0, "AoiOgg") || !header.AsciiEqual (0x2C, "OggS"))
return null;
if (!Binary.AsciiEqual (header, 0, "AoiOgg") || !Binary.AsciiEqual (header, 0x2C, "OggS"))
return null;
var ogg = new StreamRegion (file, 0x2C);
var ogg = new StreamRegion (file.AsStream, 0x2C);
return new OggInput (ogg);
}
}

View File

@ -118,7 +118,7 @@ namespace GameRes.Formats.Aoi
case 5:
count = (count >> 8) & 0xFF;
input.BaseStream.Seek ((count - count / 4) * 4, SeekOrigin.Current);
input.Seek ((count - count / 4) * 4, SeekOrigin.Current);
count *= 4;
break;
@ -134,9 +134,9 @@ namespace GameRes.Formats.Aoi
var base_name = ReadBaseName (input, meta);
if (VFS.FileExists (base_name))
{
using (var base_file = VFS.OpenSeekableStream (base_name))
using (var base_file = VFS.OpenBinaryStream (base_name))
{
var base_image = Read (base_name, base_file);
var base_image = Read (base_file);
BlendImage (meta, pixels, base_image.Bitmap);
}
}
@ -152,13 +152,13 @@ namespace GameRes.Formats.Aoi
string ReadBaseName (IBinaryStream input, AgfMetaData info)
{
input.BaseStream.Position = info.DataOffset + info.BaseNameOffset;
input.Position = info.DataOffset + info.BaseNameOffset;
using (var reader = new BinaryReader (input.AsStream, Encoding.Unicode, true))
{
var name = new StringBuilder();
for (;;)
{
char c = input.ReadChar();
char c = reader.ReadChar();
if (0 == c)
break;
name.Append (c);

View File

@ -125,14 +125,14 @@ namespace GameRes.Formats.AST
var data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
for (int i = 0; i < data.Length; ++i)
data[i] ^= 0xff;
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
return arc.File.CreateStream (entry.Offset, entry.Size);
}
using (var input = arc.File.CreateStream (entry.Offset, entry.Size))
{
var data = UnpackLzss (input, pent.Size, pent.UnpackedSize);
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
}

View File

@ -91,7 +91,7 @@ namespace GameRes.Formats.AVC
int base_offset = (int)(entry.Offset-arcf.HeaderOffset);
for (int i = 0; i < data.Length; ++i)
data[i] ^= arcf.Key[((base_offset+i)&7)];
return new MemoryStream (data, false);
return new BinMemoryStream (data, entry.Name);
}
internal class AdvReader

View File

@ -122,7 +122,7 @@ namespace GameRes.Formats.Ail
{
byte[] data = new byte[pentry.UnpackedSize];
LzssUnpack (input, data);
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
}

View File

@ -159,7 +159,7 @@ namespace GameRes.Formats.Kogado
var unpacked = new byte[packed_entry.UnpackedSize];
var mariel = new MarielEncoder();
mariel.Unpack (input, unpacked, unpacked.Length);
return new MemoryStream (unpacked);
return new BinMemoryStream (unpacked, entry.Name);
}
finally
{

View File

@ -150,7 +150,7 @@ namespace GameRes.Formats.Nexton
var data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
for (int i = 0; i != data.Length; ++i)
data[i] ^= nxent.Key;
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
private static string ReadName (ArcView view, long offset, uint size, uint key, Encoding enc)

View File

@ -50,7 +50,7 @@ namespace GameRes.Formats.Neko
/// <summary>
/// Read a directory record from archive index.
/// </summary>
DirRecord ReadDir (BinaryReader input);
DirRecord ReadDir (IBinaryStream input);
/// <summary>
/// Returns offset of an entry that immediately follows specified one.
/// </summary>
@ -65,15 +65,14 @@ namespace GameRes.Formats.Neko
internal sealed class IndexReader : IDisposable
{
BinaryReader m_input;
IBinaryStream m_input;
int m_index_size;
long m_max_offset;
INekoFormat m_format;
public IndexReader (ArcView file, INekoFormat enc, byte[] index, int index_size)
{
var input = new MemoryStream (index, 0, index_size);
m_input = new BinaryReader (input);
m_input = new BinMemoryStream (index, 0, index_size, file.Name);
m_index_size = index_size;
m_max_offset = file.MaxOffset;
m_format = enc;
@ -85,7 +84,7 @@ namespace GameRes.Formats.Neko
var files_map = GetNamesMap (KnownFileNames);
var dir = new List<Entry>();
while (m_input.BaseStream.Position < m_index_size)
while (m_input.Position < m_index_size)
{
var dir_info = m_format.ReadDir (m_input);
string dir_name;
@ -273,7 +272,7 @@ namespace GameRes.Formats.Neko
return arc.File.CreateStream (entry.Offset, entry.Size);
int length;
var data = ReadBlock (arc.File.View, pak.Decoder, entry.Offset, out length);
return new MemoryStream (data, 0, length);
return new BinMemoryStream (data, 0, length, entry.Name);
}
static uint HashFromString (uint seed, byte[] str, int offset, int length)
@ -358,7 +357,7 @@ namespace GameRes.Formats.Neko
return hash;
}
public DirRecord ReadDir (BinaryReader input)
public DirRecord ReadDir (IBinaryStream input)
{
return new DirRecord {
Hash = input.ReadUInt32(),
@ -468,7 +467,7 @@ namespace GameRes.Formats.Neko
data = new byte[aligned_size];
arc.File.View.Read (entry.Offset+12, data, 0, (uint)size);
narc.Decoder.Decrypt (key, data, 0, aligned_size);
return new MemoryStream (data, 0, size);
return new BinMemoryStream (data, 0, size, entry.Name);
}
}
@ -506,7 +505,7 @@ namespace GameRes.Formats.Neko
return hash;
}
public DirRecord ReadDir (BinaryReader input)
public DirRecord ReadDir (IBinaryStream input)
{
uint hash = input.ReadUInt32();
int count = input.ReadInt32();

View File

@ -102,7 +102,7 @@ namespace GameRes.Formats.SystemEpsylon
}
}
}
return new MemoryStream (input);
return new BinMemoryStream (input, entry.Name);
}
}
}

View File

@ -76,7 +76,7 @@ namespace GameRes.Formats.Terios
using (var reader = new PandoraCompression (input, unpacked_size))
{
var data = reader.Unpack();
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
}
catch

View File

@ -108,7 +108,7 @@ namespace GameRes.Formats.SPack
using (var reader = new PackedReader (packed_entry, input))
{
reader.Unpack();
return new MemoryStream (reader.Data);
return new BinMemoryStream (reader.Data, entry.Name);
}
}
return input;

View File

@ -86,7 +86,7 @@ namespace GameRes.Formats.Astronauts
{
var data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
Decrypt (data, entry.Size);
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
static void Decrypt (byte[] data, uint length)

View File

@ -94,11 +94,9 @@ namespace GameRes.Formats
public override uint Signature { get { return 0; } }
public override bool CanWrite { get { return false; } }
public override SoundInput TryOpen (Stream file)
public override SoundInput TryOpen (IBinaryStream file)
{
byte[] header = new byte[10];
if (header.Length != file.Read (header, 0, header.Length))
return null;
var header = file.ReadHeader (10).ToArray();
long start_offset = SkipId3Tag (header);
if (0 != start_offset)
{
@ -109,7 +107,7 @@ namespace GameRes.Formats
if (0xff != header[0] || 0xe2 != (header[1] & 0xe6) || 0xf0 == (header[2] & 0xf0))
return null;
file.Position = 0;
return new Mp3Input (file);
return new Mp3Input (file.AsStream);
}
long SkipId3Tag (byte[] buffer)

View File

@ -131,9 +131,9 @@ namespace GameRes.Formats
public override uint Signature { get { return 0x5367674f; } } // 'OggS'
public override bool CanWrite { get { return false; } }
public override SoundInput TryOpen (Stream file)
public override SoundInput TryOpen (IBinaryStream file)
{
return new OggInput (file);
return new OggInput (file.AsStream);
}
public static AudioFormat Instance { get { return s_OggFormat.Value; } }

View File

@ -67,8 +67,8 @@ namespace GameRes.Formats.Creative
switch (block_type)
{
case 1:
freq = m_input.ReadByte();
codec = m_input.ReadByte();
freq = m_input.ReadUInt8();
codec = m_input.ReadUInt8();
Copy (block_size-2, pcm);
m_format.Channels = 1;
m_format.SamplesPerSecond = 1000000u / (256u - freq);
@ -80,16 +80,16 @@ namespace GameRes.Formats.Creative
break;
case 8:
freq = m_input.ReadUInt16();
codec = m_input.ReadByte();
m_format.Channels = (ushort)(m_input.ReadByte()+1);
codec = m_input.ReadUInt8();
m_format.Channels = (ushort)(m_input.ReadUInt8()+1);
m_format.SamplesPerSecond = 256000000u / (Format.Channels * (65536u - freq));
m_format.BitsPerSample = 8;
format_read = true;
break;
case 9:
m_format.SamplesPerSecond = m_input.ReadUInt32();
m_format.BitsPerSample = m_input.ReadByte();
m_format.Channels = (ushort)(m_input.ReadByte()+1);
m_format.BitsPerSample = m_input.ReadUInt8();
m_format.Channels = (ushort)(m_input.ReadUInt8()+1);
codec = m_input.ReadUInt16();
format_read = true;
m_input.ReadInt32();

View File

@ -45,20 +45,18 @@ namespace GameRes.Formats.Banana
public override string Description { get { return "BANANA Shu-Shu image format"; } }
public override uint Signature { get { return 0; } }
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x24];
if (header.Length != stream.Read (header, 0, header.Length))
var header = stream.ReadHeader (0x24);
if (0 != header.ToInt32 (0x10) || 0 != header.ToInt32 (0x14))
return null;
if (0 != LittleEndian.ToInt32 (header, 0x10) || 0 != LittleEndian.ToInt32 (header, 0x14))
return null;
int left = LittleEndian.ToInt32 (header, 0);
int top = LittleEndian.ToInt32 (header, 4);
int right = LittleEndian.ToInt32 (header, 8);
int bottom = LittleEndian.ToInt32 (header, 0xC);
int back_width = LittleEndian.ToInt32 (header, 0x18);
int back_height = LittleEndian.ToInt32 (header, 0x1C);
uint alpha_channel = LittleEndian.ToUInt32 (header, 0x20);
int left = header.ToInt32 (0);
int top = header.ToInt32 (4);
int right = header.ToInt32 (8);
int bottom = header.ToInt32 (0xC);
int back_width = header.ToInt32 (0x18);
int back_height = header.ToInt32 (0x1C);
uint alpha_channel = header.ToUInt32 (0x20);
int width = right - left;
int height = bottom - top;
if (left >= back_width || top >= back_height
@ -78,12 +76,12 @@ namespace GameRes.Formats.Banana
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
int stride = (int)info.Width * 3;
var pixels = new byte[stride * (int)info.Height];
stream.Position = 0x24;
using (var lz = new LzssStream (stream, LzssMode.Decompress, true))
using (var lz = new LzssStream (stream.AsStream, LzssMode.Decompress, true))
{
if (pixels.Length != lz.Read (pixels, 0, pixels.Length))
throw new InvalidFormatException();
@ -104,7 +102,7 @@ namespace GameRes.Formats.Banana
stream.Position = 0x24 + meta.AlphaOffset;
var alpha = new byte[meta.BackWidth*meta.BackHeight];
using (var lz = new LzssStream (stream, LzssMode.Decompress, true))
using (var lz = new LzssStream (stream.AsStream, LzssMode.Decompress, true))
{
if (alpha.Length != lz.Read (alpha, 0, alpha.Length))
throw new InvalidFormatException();

View File

@ -49,36 +49,34 @@ namespace GameRes.Formats.Bishop
public override string Description { get { return "Bishop image format"; } }
public override uint Signature { get { return 0x2D535342; } } // 'BSS-'
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x60];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
var header = stream.ReadHeader (0x60);
int base_offset = 0;
if (Binary.AsciiEqual (header, 0, "BSS-Composition\0"))
if (header.AsciiEqual ("BSS-Composition\0"))
base_offset = 0x20;
if (!Binary.AsciiEqual (header, base_offset, "BSS-Graphics\0"))
if (!header.AsciiEqual (base_offset, "BSS-Graphics\0"))
return null;
int type = header[base_offset+0x30];
if (type > 2)
return null;
return new BsgMetaData
{
Width = LittleEndian.ToUInt16 (header, base_offset+0x16),
Height = LittleEndian.ToUInt16 (header, base_offset+0x18),
OffsetX = LittleEndian.ToInt16 (header, base_offset+0x20),
OffsetY = LittleEndian.ToInt16 (header, base_offset+0x22),
UnpackedSize = LittleEndian.ToInt32 (header, base_offset+0x12),
Width = header.ToUInt16 (base_offset+0x16),
Height = header.ToUInt16 (base_offset+0x18),
OffsetX = header.ToInt16 (base_offset+0x20),
OffsetY = header.ToInt16 (base_offset+0x22),
UnpackedSize = header.ToInt32 (base_offset+0x12),
BPP = 2 == type ? 8 : 32,
ColorMode = type,
CompressionMode = header[base_offset+0x31],
DataOffset = LittleEndian.ToInt32 (header, base_offset+0x32)+base_offset,
DataSize = LittleEndian.ToInt32 (header, base_offset+0x36),
PaletteOffset = LittleEndian.ToInt32 (header, base_offset+0x3A)+base_offset,
DataOffset = header.ToInt32 (base_offset+0x32)+base_offset,
DataSize = header.ToInt32 (base_offset+0x36),
PaletteOffset = header.ToInt32 (base_offset+0x3A)+base_offset,
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (BsgMetaData)info;
using (var reader = new BsgReader (stream, meta))
@ -96,7 +94,7 @@ namespace GameRes.Formats.Bishop
internal sealed class BsgReader : IDisposable
{
BinaryReader m_input;
IBinaryStream m_input;
BsgMetaData m_info;
byte[] m_output;
@ -105,13 +103,13 @@ namespace GameRes.Formats.Bishop
public BitmapPalette Palette { get; private set; }
public int Stride { get; private set; }
public BsgReader (Stream input, BsgMetaData info)
public BsgReader (IBinaryStream input, BsgMetaData info)
{
m_info = info;
if (m_info.CompressionMode > 2)
throw new NotSupportedException ("Not supported BSS Graphics compression");
m_input = new ArcView.Reader (input);
m_input = input;
m_output = new byte[m_info.UnpackedSize];
switch (m_info.ColorMode)
{
@ -133,7 +131,7 @@ namespace GameRes.Formats.Bishop
public void Unpack ()
{
m_input.BaseStream.Position = m_info.DataOffset;
m_input.Position = m_info.DataOffset;
if (0 == m_info.CompressionMode)
{
if (1 == m_info.ColorMode)
@ -179,13 +177,13 @@ namespace GameRes.Formats.Bishop
int remaining = m_input.ReadInt32();
while (remaining > 0)
{
int count = m_input.ReadSByte();
int count = m_input.ReadInt8();
--remaining;
if (count >= 0)
{
for (int i = 0; i <= count; ++i)
{
m_output[dst] = m_input.ReadByte();
m_output[dst] = m_input.ReadUInt8();
--remaining;
dst += pixel_size;
}
@ -193,7 +191,7 @@ namespace GameRes.Formats.Bishop
else
{
count = 1 - count;
byte repeat = m_input.ReadByte();
byte repeat = m_input.ReadUInt8();
--remaining;
for (int i = 0; i < count; ++i)
{
@ -207,20 +205,20 @@ namespace GameRes.Formats.Bishop
void UnpackLz (int plane, int pixel_size)
{
int dst = plane;
byte control = m_input.ReadByte();
byte control = m_input.ReadUInt8();
int remaining = m_input.ReadInt32() - 5;
while (remaining > 0)
{
byte c = m_input.ReadByte();
byte c = m_input.ReadUInt8();
--remaining;
if (c == control)
{
int offset = m_input.ReadByte();
int offset = m_input.ReadUInt8();
--remaining;
if (offset != control)
{
int count = m_input.ReadByte();
int count = m_input.ReadUInt8();
--remaining;
if (offset > control)
@ -245,7 +243,7 @@ namespace GameRes.Formats.Bishop
BitmapPalette ReadPalette ()
{
m_input.BaseStream.Position = m_info.PaletteOffset;
m_input.Position = m_info.PaletteOffset;
var palette_data = new byte[0x400];
if (palette_data.Length != m_input.Read (palette_data, 0, palette_data.Length))
throw new InvalidFormatException();
@ -259,14 +257,8 @@ namespace GameRes.Formats.Bishop
}
#region IDisposable Members
bool _disposed = false;
public void Dispose ()
{
if (!_disposed)
{
m_input.Dispose();
_disposed = true;
}
}
#endregion
}

View File

@ -44,7 +44,7 @@ namespace GameRes.Formats.BlackCyc
Extensions = new string[] { "vaw", "wgq" };
}
public override SoundInput TryOpen (Stream file)
public override SoundInput TryOpen (IBinaryStream file)
{
var header = ResourceHeader.Read (file);
if (null == header)
@ -76,8 +76,8 @@ namespace GameRes.Formats.BlackCyc
}
else
return null;
var input = new StreamRegion (file, offset, file.Length-offset);
return format.TryOpen (input);
var input = new StreamRegion (file.AsStream, offset, file.Length-offset);
return format.TryOpen (new BinaryStream (input, file.Name));
}
public override void Write (SoundInput source, Stream output)
@ -85,7 +85,7 @@ namespace GameRes.Formats.BlackCyc
throw new System.NotImplementedException ("EdimFormat.Write not implemenented");
}
SoundInput Unpack (Stream input)
SoundInput Unpack (IBinaryStream input)
{
input.Position = 0x40;
var header = new byte[0x24];
@ -106,7 +106,7 @@ namespace GameRes.Formats.BlackCyc
{
pcm.Write (header, 0, header_size);
using (var output = new BinaryWriter (pcm, Encoding.Default, true))
using (var bits = new LsbBitStream (input, true))
using (var bits = new LsbBitStream (input.AsStream, true))
{
int written = 0;
short sample = 0;
@ -128,7 +128,7 @@ namespace GameRes.Formats.BlackCyc
}
}
pcm.Position = 0;
var sound = Wav.TryOpen (pcm);
var sound = Wav.TryOpen (new BinMemoryStream (pcm, input.Name));
if (sound != null)
input.Dispose();
else

View File

@ -43,11 +43,9 @@ namespace GameRes.Formats.BlackCyc
public int PackType { get; private set; }
public bool AType { get; private set; }
public static ResourceHeader Read (Stream file)
public static ResourceHeader Read (IBinaryStream file)
{
var header = new byte[0x40];
if (0x40 != file.Read (header, 0, 0x40))
return null;
var header = file.ReadHeader (0x40).ToArray();
var header_string = Encoding.ASCII.GetString (header, 0x30, 0x10);
var match = PackTypeRe.Match (header_string);
@ -88,9 +86,9 @@ namespace GameRes.Formats.BlackCyc
};
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
var header = ResourceHeader.Read (stream);
var header = ResourceHeader.Read (file);
if (null == header)
return null;
if (Binary.AsciiEqual (header.Bytes, "IF PACKTYPE=="))
@ -99,7 +97,8 @@ namespace GameRes.Formats.BlackCyc
!Binary.AsciiEqual (header.Bytes, 0x2C, "BMP ") ||
header.PackType != 0 && header.PackType != 1)
return null;
using (var bmp = new StreamRegion (stream, 0x40, true))
using (var reg = new StreamRegion (file.AsStream, 0x40, true))
using (var bmp = new BinaryStream (reg, file.Name))
{
var info = Bmp.ReadMetaData (bmp);
if (null == info)
@ -110,7 +109,7 @@ namespace GameRes.Formats.BlackCyc
Height = info.Height,
BPP = info.BPP,
BaseType = "BMP",
PackedSize = (int)(stream.Length-0x40),
PackedSize = (int)(file.Length-0x40),
PackType = header.PackType,
HasAlpha = header.AType,
};
@ -122,7 +121,7 @@ namespace GameRes.Formats.BlackCyc
case 0: // BMP
case 5: // JPEG
case 8: // PNG
packed_size = (int)(stream.Length-0x40);
packed_size = (int)(file.Length-0x40);
break;
case 2: // BMP+MASK
@ -146,12 +145,13 @@ namespace GameRes.Formats.BlackCyc
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (DwqMetaData)info;
BitmapSource bitmap = null;
using (var input = new StreamRegion (stream, 0x40, meta.PackedSize, true))
using (var sreg = new StreamRegion (stream.AsStream, 0x40, meta.PackedSize, true))
using (var input = new BinaryStream (sreg, stream.Name))
{
switch (meta.PackType)
{
@ -192,7 +192,7 @@ namespace GameRes.Formats.BlackCyc
int mask_offset = 0x40+meta.PackedSize;
if (mask_offset != stream.Length)
{
using (var mask = new StreamRegion (stream, mask_offset, true))
using (var mask = new StreamRegion (stream.AsStream, mask_offset, true))
{
var reader = new DwqBmpReader (mask, meta);
if (8 == reader.Format.BitsPerPixel) // mask should be represented as 8bpp bitmap
@ -238,17 +238,15 @@ namespace GameRes.Formats.BlackCyc
PixelFormats.Bgra32, null, pixels, stride);
}
private BitmapSource ReadFuckedUpBmpImage (Stream file, ImageMetaData info)
private BitmapSource ReadFuckedUpBmpImage (IBinaryStream file, ImageMetaData info)
{
var header = new byte[0x36];
if (header.Length != file.Read (header, 0, header.Length))
throw new InvalidFormatException();
int w = LittleEndian.ToInt32 (header, 0x12);
int h = LittleEndian.ToInt32 (header, 0x16);
var header = file.ReadHeader (0x36);
int w = header.ToInt32 (0x12);
int h = header.ToInt32 (0x16);
if (w != info.Width || h != info.Height)
throw new InvalidFormatException();
int bpp = LittleEndian.ToUInt16 (header, 0x1c);
int bpp = header.ToUInt16 (0x1c);
PixelFormat format;
switch (bpp)
{
@ -261,8 +259,8 @@ namespace GameRes.Formats.BlackCyc
BitmapPalette palette = null;
if (8 == bpp)
{
int colors = Math.Min (LittleEndian.ToInt32 (header, 0x2E), 0x100);
palette = DwqBmpReader.ReadPalette (file, colors);
int colors = Math.Min (header.ToInt32 (0x2E), 0x100);
palette = DwqBmpReader.ReadPalette (file.AsStream, colors);
}
int pixel_size = bpp / 8;
int stride = ((int)info.Width * pixel_size + 3) & ~3;

View File

@ -51,31 +51,25 @@ namespace GameRes.Formats.BlackRainbow
public override uint Signature { get { return 0x444d425fu; } } // '_BMD'
public override bool CanWrite { get { return true; } }
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x14];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
var header = stream.ReadHeader (0x14);
return new BmdMetaData
{
Width = LittleEndian.ToUInt32 (header, 8),
Height = LittleEndian.ToUInt32 (header, 12),
Width = header.ToUInt32 (8),
Height = header.ToUInt32 (12),
BPP = 32,
PackedSize = LittleEndian.ToUInt32 (header, 4),
Flag = LittleEndian.ToInt32 (header, 0x10),
PackedSize = header.ToUInt32 (4),
Flag = header.ToInt32 (0x10),
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = info as BmdMetaData;
if (null == meta)
throw new ArgumentException ("BmdFormat.Read should be supplied with BmdMetaData", "info");
var meta = (BmdMetaData)info;
stream.Position = 0x14;
int image_size = (int)(meta.Width*meta.Height*4);
using (var reader = new LzssReader (stream, (int)meta.PackedSize, image_size))
using (var reader = new LzssReader (stream.AsStream, (int)meta.PackedSize, image_size))
{
PixelFormat format = meta.Flag != 0 ? PixelFormats.Bgra32 : PixelFormats.Bgr32;
reader.Unpack();

View File

@ -55,21 +55,21 @@ namespace GameRes.Formats.BlackRainbow
}
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
var header = new byte[8];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
using (var zstream = new ZLibStream (stream, CompressionMode.Decompress, true))
return base.ReadMetaData (zstream);
var header = file.ReadHeader (8);
using (var zstream = new ZLibStream (file.AsStream, CompressionMode.Decompress, true))
using (var bmp = new BinaryStream (zstream, file.Name))
return base.ReadMetaData (bmp);
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
stream.Seek (8, SeekOrigin.Current);
using (var zstream = new ZLibStream (stream, CompressionMode.Decompress, true))
file.Seek (8, SeekOrigin.Current);
using (var zstream = new ZLibStream (file.AsStream, CompressionMode.Decompress, true))
using (var input = new SeekableStream (zstream))
return base.Read (input, info);
using (var bmp = new BinaryStream (input, file.Name))
return base.Read (bmp, info);
}
}
}

View File

@ -43,48 +43,40 @@ namespace GameRes.Formats.BlueGale
public override string Description { get { return "BlueGale compressed image format"; } }
public override uint Signature { get { return 0x5F706D61; } } // 'amp_'
public ZbmFormat ()
{
Extensions = new string[] { "zbm" };
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
stream.Position = 4;
using (var reader = new ArcView.Reader (stream))
int version = stream.ReadInt16();
if (version != 1)
return null;
int unpacked_size = stream.ReadInt32();
int data_offset = stream.ReadInt32();
if (unpacked_size < 0x36 || data_offset < stream.Position)
return null;
var header = new byte[0x20];
stream.Position = data_offset;
Unpack (stream.AsStream, header);
Decrypt (header);
if ('B' != header[0] || 'M' != header[1])
return null;
return new ZbmMetaData
{
int version = reader.ReadInt16();
if (version != 1)
return null;
int unpacked_size = reader.ReadInt32();
int data_offset = reader.ReadInt32();
if (unpacked_size < 0x36 || data_offset < stream.Position)
return null;
var header = new byte[0x20];
stream.Position = data_offset;
Unpack (stream, header);
Decrypt (header);
if ('B' != header[0] || 'M' != header[1])
return null;
return new ZbmMetaData
{
Width = LittleEndian.ToUInt32 (header, 0x12),
Height = LittleEndian.ToUInt32 (header, 0x16),
BPP = LittleEndian.ToInt16 (header, 0x1C),
UnpackedSize = unpacked_size,
DataOffset = data_offset,
};
}
Width = LittleEndian.ToUInt32 (header, 0x12),
Height = LittleEndian.ToUInt32 (header, 0x16),
BPP = LittleEndian.ToInt16 (header, 0x1C),
UnpackedSize = unpacked_size,
DataOffset = data_offset,
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (ZbmMetaData)info;
var data = new byte[meta.UnpackedSize];
stream.Position = meta.DataOffset;
Unpack (stream, data);
Unpack (stream.AsStream, data);
Decrypt (data);
using (var bmp = new MemoryStream (data))
using (var bmp = new BinMemoryStream (data, stream.Name))
return Bmp.Read (bmp, info);
}

View File

@ -90,7 +90,7 @@ namespace GameRes.Formats.BlueGale
LittleEndian.Pack (pent.UnpackedSize, output, 2);
int header_size = LittleEndian.ToInt32 (output, 0xE);
LittleEndian.Pack (header_size+0xE, output, 0xA);
return new MemoryStream (output);
return new BinMemoryStream (output, entry.Name);
}
}
}

View File

@ -102,7 +102,7 @@ namespace GameRes.Formats.Bruns
if (meta.Compressed)
input = new ZLibStream (input, CompressionMode.Decompress);
using (var bin = new BinaryStream (input, stream.Name, true))
return meta.Format.Read (input, meta.Info);
return meta.Format.Read (bin, meta.Info);
}
finally
{

View File

@ -200,7 +200,7 @@ namespace GameRes.Formats.CaramelBox
using (input)
{
var data = UnpackLze (input, a3ent.UnpackedSize);
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
}

View File

@ -130,7 +130,7 @@ namespace GameRes.Formats.CaramelBox
return input;
using (input)
using (var tz = new TzCompression (input))
return new MemoryStream (tz.Unpack());
return new BinMemoryStream (tz.Unpack(), entry.Name);
}
static int ReadInt24 (byte[] data, int pos)

View File

@ -208,7 +208,7 @@ namespace GameRes.Formats.CatSystem
{
byte[] data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
arc.Encryption.Decipher (data, data.Length/8*8);
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
public override Stream OpenEntry (ArcFile arc, Entry entry)

View File

@ -44,37 +44,34 @@ namespace GameRes.Formats.CatSystem
public override string Description { get { return "CatSystem engine image format"; } }
public override uint Signature { get { return 0x322D4748; } } // 'HG-2'
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
stream.Position = 8;
using (var header = new ArcView.Reader (stream))
{
var info = new Hg2MetaData();
int type = header.ReadInt32();
if (0x25 == type)
info.HeaderSize = 0x58;
else if (0x20 == type)
info.HeaderSize = 0x50;
else
return null;
info.Width = header.ReadUInt32();
info.Height = header.ReadUInt32();
info.BPP = header.ReadInt32();
header.BaseStream.Seek (8, SeekOrigin.Current);
info.DataPacked = header.ReadInt32();
info.DataUnpacked = header.ReadInt32();
info.CtlPacked = header.ReadInt32();
info.CtlUnpacked = header.ReadInt32();
header.BaseStream.Seek (8, SeekOrigin.Current);
info.CanvasWidth = header.ReadUInt32();
info.CanvasHeight = header.ReadUInt32();
info.OffsetX = header.ReadInt32();
info.OffsetY = header.ReadInt32();
return info;
}
var info = new Hg2MetaData();
int type = stream.ReadInt32();
if (0x25 == type)
info.HeaderSize = 0x58;
else if (0x20 == type)
info.HeaderSize = 0x50;
else
return null;
info.Width = stream.ReadUInt32();
info.Height = stream.ReadUInt32();
info.BPP = stream.ReadInt32();
stream.Seek (8, SeekOrigin.Current);
info.DataPacked = stream.ReadInt32();
info.DataUnpacked = stream.ReadInt32();
info.CtlPacked = stream.ReadInt32();
info.CtlUnpacked = stream.ReadInt32();
stream.Seek (8, SeekOrigin.Current);
info.CanvasWidth = stream.ReadUInt32();
info.CanvasHeight = stream.ReadUInt32();
info.OffsetX = stream.ReadInt32();
info.OffsetY = stream.ReadInt32();
return info;
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
using (var reader = new Hg2Reader (stream, (Hg2MetaData)info))
{
@ -94,7 +91,7 @@ namespace GameRes.Formats.CatSystem
{
Hg2MetaData m_hg2;
public Hg2Reader (Stream input, Hg2MetaData info) : base (input, info)
public Hg2Reader (IBinaryStream input, Hg2MetaData info) : base (input, info)
{
m_hg2 = info;
}

View File

@ -25,7 +25,6 @@
using System;
using System.IO;
using System.Linq;
using System.ComponentModel.Composition;
using System.Windows.Media.Imaging;
using System.Windows.Media;
@ -48,35 +47,34 @@ namespace GameRes.Formats.CatSystem
public override string Description { get { return "CatSystem engine image format"; } }
public override uint Signature { get { return 0x332d4748; } } // 'HG-3'
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x4c];
if (0x4c != stream.Read (header, 0, header.Length))
var header = stream.ReadHeader (0x4c);
if (header.ToUInt32 (4) != 0x0c)
return null;
if (LittleEndian.ToUInt32 (header, 4) != 0x0c)
return null;
if (!Binary.AsciiEqual (header, 0x14, "stdinfo\0"))
if (!header.AsciiEqual (0x14, "stdinfo\0"))
return null;
return new HgMetaData
{
HeaderSize = LittleEndian.ToUInt32 (header, 0x1C),
Width = LittleEndian.ToUInt32 (header, 0x24),
Height = LittleEndian.ToUInt32 (header, 0x28),
OffsetX = LittleEndian.ToInt32 (header, 0x30),
OffsetY = LittleEndian.ToInt32 (header, 0x34),
BPP = LittleEndian.ToInt32 (header, 0x2C),
CanvasWidth = LittleEndian.ToUInt32 (header, 0x44),
CanvasHeight = LittleEndian.ToUInt32 (header, 0x48),
HeaderSize = header.ToUInt32 (0x1C),
Width = header.ToUInt32 (0x24),
Height = header.ToUInt32 (0x28),
OffsetX = header.ToInt32 (0x30),
OffsetY = header.ToInt32 (0x34),
BPP = header.ToInt32 (0x2C),
CanvasWidth = header.ToUInt32 (0x44),
CanvasHeight = header.ToUInt32 (0x48),
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (HgMetaData)info;
if (0x20 != meta.BPP)
throw new NotSupportedException ("Not supported HG-3 color depth");
using (var input = new StreamRegion (stream, 0x14, true))
using (var reg = new StreamRegion (stream.AsStream, 0x14, true))
using (var input = new BinaryStream (reg, stream.Name))
using (var reader = new Hg3Reader (input, meta))
{
var pixels = reader.Unpack();
@ -95,17 +93,17 @@ namespace GameRes.Formats.CatSystem
internal class HgReader : IDisposable
{
private BinaryReader m_input;
private IBinaryStream m_input;
protected HgMetaData m_info;
protected int m_pixel_size;
protected BinaryReader Input { get { return m_input; } }
protected Stream InputStream { get { return m_input.BaseStream; } }
protected IBinaryStream Input { get { return m_input; } }
protected Stream InputStream { get { return m_input.AsStream; } }
public int Stride { get; protected set; }
protected HgReader (Stream input, HgMetaData info)
protected HgReader (IBinaryStream input, HgMetaData info)
{
m_input = new ArcView.Reader (input);
m_input = input;
m_info = info;
m_pixel_size = m_info.BPP / 8;
Stride = (int)m_info.Width * m_pixel_size;
@ -228,25 +226,10 @@ namespace GameRes.Formats.CatSystem
}
#region IDisposable Members
bool _disposed = false;
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (!_disposed)
{
if (disposing)
{
m_input.Dispose();
}
_disposed = true;
}
}
#endregion
}
@ -254,17 +237,17 @@ namespace GameRes.Formats.CatSystem
{
public bool Flipped { get; private set; }
public Hg3Reader (Stream input, HgMetaData info) : base (input, info)
public Hg3Reader (IBinaryStream input, HgMetaData info) : base (input, info)
{
}
public byte[] Unpack ()
{
InputStream.Position = m_info.HeaderSize;
var img_type = Input.ReadChars (8);
if (img_type.SequenceEqual ("img0000\0"))
var img_type = Input.ReadBytes (8);
if (Binary.AsciiEqual (img_type, "img0000\0"))
return UnpackImg0000();
else if (img_type.SequenceEqual ("img_jpg\0"))
else if (Binary.AsciiEqual (img_type, "img_jpg\0"))
return UnpackJpeg();
else
throw new NotSupportedException ("Not supported HG-3 image");
@ -309,11 +292,11 @@ namespace GameRes.Formats.CatSystem
src += src_pixel_size;
}
InputStream.Position = next_section;
var section_header = Input.ReadChars (8);
if (!section_header.SequenceEqual ("img_al\0\0"))
Input.Position = next_section;
var section_header = Input.ReadBytes (8);
if (!Binary.AsciiEqual (section_header, "img_al\0\0"))
return output;
InputStream.Seek (8, SeekOrigin.Current);
Input.Seek (8, SeekOrigin.Current);
int alpha_size = Input.ReadInt32();
using (var alpha_in = new StreamRegion (InputStream, InputStream.Position+4, alpha_size, true))
using (var alpha = new ZLibStream (alpha_in, CompressionMode.Decompress))

View File

@ -110,7 +110,7 @@ namespace GameRes.Formats.Cherry
{
data[text_offset+i] ^= (byte)i;
}
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
}
@ -209,7 +209,7 @@ namespace GameRes.Formats.Cherry
}
Decrypt (data, 0x18, (int)(data.Length - 0x18));
}
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
}
}

View File

@ -54,16 +54,14 @@ namespace GameRes.Formats.Cherry
Extensions = new string[] { "grp" };
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x18];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
uint width = LittleEndian.ToUInt32 (header, 0);
uint height = LittleEndian.ToUInt32 (header, 4);
int bpp = LittleEndian.ToInt32 (header, 8);
int packed_size = LittleEndian.ToInt32 (header, 0x0C);
int unpacked_size = LittleEndian.ToInt32 (header, 0x10);
var header = stream.ReadHeader (0x18);
uint width = header.ToUInt32 (0);
uint height = header.ToUInt32 (4);
int bpp = header.ToInt32 (8);
int packed_size = header.ToInt32 (0x0C);
int unpacked_size = header.ToInt32 (0x10);
if (0 == width || 0 == height || width > 0x7fff || height > 0x7fff
|| (bpp != 24 && bpp != 8)
|| unpacked_size <= 0 || packed_size < 0)
@ -75,16 +73,16 @@ namespace GameRes.Formats.Cherry
BPP = bpp,
PackedSize = packed_size,
UnpackedSize = unpacked_size,
Offset = LittleEndian.ToInt32 (header, 0x14),
Offset = header.ToInt32 (0x14),
HeaderSize = 0x18,
AlphaChannel = false,
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var meta = (GrpMetaData)info;
var reader = new GrpReader (stream, meta);
var reader = new GrpReader (file.AsStream, meta);
return reader.CreateImage();
}
@ -101,18 +99,16 @@ namespace GameRes.Formats.Cherry
public override string Description { get { return "Cherry Soft compressed image format"; } }
public override uint Signature { get { return 0; } }
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x28];
if (header.Length != stream.Read (header, 0, header.Length))
var header = stream.ReadHeader (0x28);
if (0xFFFF != header.ToInt32 (8))
return null;
if (0xFFFF != LittleEndian.ToInt32 (header, 8))
return null;
int packed_size = LittleEndian.ToInt32 (header, 0);
int unpacked_size = LittleEndian.ToInt32 (header, 4);
uint width = LittleEndian.ToUInt32 (header, 0x10);
uint height = LittleEndian.ToUInt32 (header, 0x14);
int bpp = LittleEndian.ToInt32 (header, 0x18);
int packed_size = header.ToInt32 (0);
int unpacked_size = header.ToInt32 (4);
uint width = header.ToUInt32 (0x10);
uint height = header.ToUInt32 (0x14);
int bpp = header.ToInt32 (0x18);
if (0 == width || 0 == height || width > 0x7fff || height > 0x7fff
|| (bpp != 32 && bpp != 24 && bpp != 8)
|| unpacked_size <= 0 || packed_size < 0)
@ -126,7 +122,7 @@ namespace GameRes.Formats.Cherry
UnpackedSize = unpacked_size,
Offset = 0xFFFF,
HeaderSize = 0x28,
AlphaChannel = LittleEndian.ToInt32 (header, 0x24) != 0,
AlphaChannel = header.ToInt32 (0x24) != 0,
};
}
}

View File

@ -117,7 +117,7 @@ namespace GameRes.Formats.Circus
{
data = UnpackCps (data);
}
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
byte[] UnpackCps (byte[] input)

View File

@ -38,46 +38,43 @@ namespace GameRes.Formats.Circus
public override string Description { get { return "Circus PCM audio"; } }
public override uint Signature { get { return 0x4d435058; } } // 'XPCM'
public override SoundInput TryOpen (Stream file)
public override SoundInput TryOpen (IBinaryStream file)
{
file.Position = 4;
using (var input = new ArcView.Reader (file))
int src_size = file.ReadInt32();
if (src_size <= 0)
throw new InvalidFormatException();
int mode = file.ReadInt32();
int extra = (mode >> 8) & 0xff;
mode &= 0xff;
if (5 == mode)
{
int src_size = input.ReadInt32();
if (src_size <= 0)
throw new InvalidFormatException();
int mode = input.ReadInt32();
int extra = (mode >> 8) & 0xff;
mode &= 0xff;
if (5 == mode)
{
uint ogg_size = input.ReadUInt32();
var ogg = new StreamRegion (file, 0x10, ogg_size);
return new OggInput (ogg);
}
var format = new WaveFormat();
format.FormatTag = input.ReadUInt16();
format.Channels = input.ReadUInt16();
format.SamplesPerSecond = input.ReadUInt32();
format.AverageBytesPerSecond = input.ReadUInt32();
format.BlockAlign = input.ReadUInt16();
format.BitsPerSample = input.ReadUInt16();
Stream pcm;
if (0 == mode)
{
pcm = new StreamRegion (file, file.Position, src_size);
}
else if (1 == mode || 3 == mode)
{
var decoder = new PcmDecoder (input, src_size, extra, (XpcmCompression)mode);
pcm = new MemoryStream (decoder.Unpack(), 0, src_size);
file.Dispose();
}
else
throw new NotSupportedException ("Not supported Circus PCM audio compression");
return new RawPcmInput (pcm, format);
uint ogg_size = file.ReadUInt32();
var ogg = new StreamRegion (file.AsStream, 0x10, ogg_size);
return new OggInput (ogg);
}
var format = new WaveFormat();
format.FormatTag = file.ReadUInt16();
format.Channels = file.ReadUInt16();
format.SamplesPerSecond = file.ReadUInt32();
format.AverageBytesPerSecond = file.ReadUInt32();
format.BlockAlign = file.ReadUInt16();
format.BitsPerSample = file.ReadUInt16();
Stream pcm;
if (0 == mode)
{
pcm = new StreamRegion (file.AsStream, file.Position, src_size);
}
else if (1 == mode || 3 == mode)
{
var decoder = new PcmDecoder (file, src_size, extra, (XpcmCompression)mode);
pcm = new MemoryStream (decoder.Unpack(), 0, src_size);
file.Dispose();
}
else
throw new NotSupportedException ("Not supported Circus PCM audio compression");
return new RawPcmInput (pcm, format);
}
}
@ -97,7 +94,7 @@ namespace GameRes.Formats.Circus
public byte[] Data { get { return m_pcm_data; } }
public PcmDecoder (BinaryReader input, int pcm_size, int extra, XpcmCompression mode)
public PcmDecoder (IBinaryStream input, int pcm_size, int extra, XpcmCompression mode)
{
if (extra < 0 || extra > 3)
throw new InvalidFormatException();
@ -114,7 +111,7 @@ namespace GameRes.Formats.Circus
}
else if (XpcmCompression.Zlib == mode)
{
using (var z = new ZLibStream (input.BaseStream, CompressionMode.Decompress, true))
using (var z = new ZLibStream (input.AsStream, CompressionMode.Decompress, true))
z.Read (m_encoded, 0, m_encoded.Length);
}
else

View File

@ -266,23 +266,23 @@ namespace GameRes.Formats.Circus
{
flag >>= 1;
if (0 == (flag & 0x100))
flag = m_input.ReadByte() | 0xff00;
flag = m_input.ReadUInt8() | 0xff00;
if (0 != (flag & 1))
{
byte dat = m_input.ReadByte();
byte dat = m_input.ReadUInt8();
window[win_pos++] = dat;
win_pos &= 0xffff;
m_output[dst++] = dat;
}
else
{
byte control = m_input.ReadByte();
byte control = m_input.ReadUInt8();
int count, offset;
if (control >= 0xc0)
{
offset = ((control & 3) << 8) | m_input.ReadByte();
offset = ((control & 3) << 8) | m_input.ReadUInt8();
count = 4 + ((control >> 2) & 0xf);
}
else if (0 != (control & 0x80))
@ -290,7 +290,7 @@ namespace GameRes.Formats.Circus
offset = control & 0x1f;
count = 2 + ((control >> 5) & 3);
if (0 == offset)
offset = m_input.ReadByte();
offset = m_input.ReadUInt8();
}
else if (0x7f == control)
{

View File

@ -70,7 +70,7 @@ namespace GameRes.Formats.Circus
else if (header.AsciiEqual (0x20, "CRXG"))
{
using (var crx_input = new StreamRegion (stream.AsStream, 0x20, true))
using (var crx = new BinaryStream (crx_input))
using (var crx = new BinaryStream (crx_input, stream.Name))
{
var diff_info = base.ReadMetaData (crx) as CrxMetaData;
if (null == diff_info)
@ -90,12 +90,12 @@ namespace GameRes.Formats.Circus
if (info != null)
{
info.BaseOffset = header.ToUInt32 (8);
info.BaseFileName = Binary.GetCString (header, 0xC, 0x14);
info.BaseFileName = header.GetCString (0xC, 0x14);
}
return info;
}
Stream OpenByOffset (uint offset)
IBinaryStream OpenByOffset (uint offset)
{
var vfs = VFS.Top as ArchiveFileSystem;
if (null == vfs)
@ -118,9 +118,9 @@ namespace GameRes.Formats.Circus
diff = OpenByOffset (info.DiffOffset);
if (null == diff)
throw new FileNotFoundException ("Referenced diff image not found");
input = new StreamRegion (diff, 0x20);
input = new StreamRegion (diff.AsStream, 0x20);
}
return new BinaryStream (input);
return new BinaryStream (input, diff.Name);
}
public override ImageData Read (IBinaryStream stream, ImageMetaData info)

View File

@ -279,7 +279,7 @@ namespace GameRes.Formats.Purple
data = UnpackPs2 (data);
else if (data.Length > 0x40 && Binary.AsciiEqual (data, 0, "PB3B"))
DecryptPb3 (data);
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
void DecryptIndexStage1 (byte[] data, uint key, CmvsScheme scheme)

View File

@ -122,7 +122,7 @@ namespace GameRes.Formats.Pvns
{
data = DecryptScript (data, parc.ScriptKey);
}
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
static void Decrypt (byte[] data, byte[] key)

View File

@ -47,33 +47,30 @@ namespace GameRes.Formats.Purple
public override string Description { get { return "Purple Software image format"; } }
public override uint Signature { get { return 0x42334250; } } // 'PB3B'
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
stream.Position = 4;
using (var reader = new ArcView.Reader (stream))
int input_size = stream.ReadInt32();
stream.Position = 0x18;
int t2 = stream.ReadInt32();
int t1 = stream.ReadUInt16();
uint width = stream.ReadUInt16();
uint height = stream.ReadUInt16();
int bpp = stream.ReadUInt16();
return new Pb3MetaData
{
int input_size = reader.ReadInt32();
stream.Position = 0x18;
int t2 = reader.ReadInt32();
int t1 = reader.ReadUInt16();
uint width = reader.ReadUInt16();
uint height = reader.ReadUInt16();
int bpp = reader.ReadUInt16();
return new Pb3MetaData
{
Width = width,
Height = height,
BPP = bpp,
Type = t1,
SubType = t2,
InputSize = input_size,
};
}
Width = width,
Height = height,
BPP = bpp,
Type = t1,
SubType = t2,
InputSize = input_size,
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var reader = new Pb3Reader (stream, (Pb3MetaData)info);
var reader = new Pb3Reader (stream.AsStream, (Pb3MetaData)info);
reader.Unpack();
return ImageData.Create (info, reader.Format, null, reader.Data);
}
@ -350,9 +347,9 @@ namespace GameRes.Formats.Purple
if (name.Equals (m_info.FileName, StringComparison.InvariantCultureIgnoreCase))
throw new InvalidFormatException();
// two files referencing each other still could create infinite recursion
using (var base_file = VFS.OpenSeekableStream (name))
using (var base_file = VFS.OpenBinaryStream (name))
{
var image_data = ImageFormat.Read (name, base_file);
var image_data = ImageFormat.Read (base_file);
int stride = image_data.Bitmap.PixelWidth * 4;
var pixels = new byte[stride * image_data.Bitmap.PixelHeight];
image_data.Bitmap.CopyPixels (pixels, stride, 0);

View File

@ -104,7 +104,7 @@ namespace GameRes.Formats.Cri
}
Array.Reverse (output, (int)prefix_size, unpacked_size);
arc.File.View.Read (entry.Offset+0x10+packed_size, output, 0, prefix_size);
return new MemoryStream (output);
return new BinMemoryStream (output, entry.Name);
}
void DetectFileTypes (ArcView file, List<Entry> dir)

View File

@ -43,7 +43,7 @@ namespace GameRes.Formats.Cri
uint signature = file.Signature;
if (0x80 != (signature & 0xFFFF))
return null;
uint header_size = Binary.BigEndian (signature & 0xFFFF0000);
int header_size = (int)Binary.BigEndian (signature & 0xFFFF0000);
if (header_size < 0x10 || header_size >= file.Length)
return null;
var header = file.ReadBytes (header_size);

View File

@ -61,57 +61,52 @@ namespace GameRes.Formats.Cri
throw new NotImplementedException ("BipFormat.Write not implemented");
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream input)
{
using (var input = new BinaryReader (stream, Encoding.ASCII, true))
{
int sig = input.ReadInt32();
if (sig != 5 && sig != 10)
return null;
uint header_end = (uint)sig*4;
uint index_offset = input.ReadUInt32();
int sig = input.ReadInt32();
if (sig != 5 && sig != 10)
return null;
uint header_end = (uint)sig*4;
uint index_offset = input.ReadUInt32();
input.BaseStream.Position = header_end-4;
uint data_offset = input.ReadUInt32() + 8;
if (index_offset >= data_offset || index_offset < header_end)
return null;
input.BaseStream.Position = index_offset;
int tile_count = input.ReadInt16();
int flag = input.ReadInt16();
if (tile_count <= 0 || 0 != flag)
return null;
input.ReadInt32();
uint w = input.ReadUInt16();
uint h = input.ReadUInt16();
if (0 == w || 0 == h)
return null;
var meta = new BipMetaData { Width = w, Height = h, BPP = 32 };
meta.Tiles.Capacity = tile_count;
for (int i = 0; i < tile_count; ++i)
{
input.ReadInt64();
var tile = new BipTile();
tile.Left = input.ReadUInt16();
tile.Top = input.ReadUInt16();
tile.Width = input.ReadUInt16();
tile.Height = input.ReadUInt16();
if (tile.Left + tile.Width > meta.Width)
meta.Width = (uint)(tile.Left + tile.Width);
if (tile.Top + tile.Height > meta.Height)
meta.Height = (uint)(tile.Top + tile.Height);
input.ReadInt64();
tile.Offset = input.ReadUInt32() + data_offset;
meta.Tiles.Add (tile);
}
return meta;
input.Position = header_end-4;
uint data_offset = input.ReadUInt32() + 8;
if (index_offset >= data_offset || index_offset < header_end)
return null;
input.Position = index_offset;
int tile_count = input.ReadInt16();
int flag = input.ReadInt16();
if (tile_count <= 0 || 0 != flag)
return null;
input.ReadInt32();
uint w = input.ReadUInt16();
uint h = input.ReadUInt16();
if (0 == w || 0 == h)
return null;
var meta = new BipMetaData { Width = w, Height = h, BPP = 32 };
meta.Tiles.Capacity = tile_count;
for (int i = 0; i < tile_count; ++i)
{
input.ReadInt64();
var tile = new BipTile();
tile.Left = input.ReadUInt16();
tile.Top = input.ReadUInt16();
tile.Width = input.ReadUInt16();
tile.Height = input.ReadUInt16();
if (tile.Left + tile.Width > meta.Width)
meta.Width = (uint)(tile.Left + tile.Width);
if (tile.Top + tile.Height > meta.Height)
meta.Height = (uint)(tile.Top + tile.Height);
input.ReadInt64();
tile.Offset = input.ReadUInt32() + data_offset;
meta.Tiles.Add (tile);
}
return meta;
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = info as BipMetaData;
if (null == meta)
throw new ArgumentException ("BipFormat.Read should be supplied with BipMetaData", "info");
var meta = (BipMetaData)info;
var header = new byte[0x7c];
var bitmap = new WriteableBitmap ((int)meta.Width, (int)meta.Height,
@ -127,7 +122,7 @@ namespace GameRes.Formats.Cri
int alpha = LittleEndian.ToInt32 (header, 0x68);
int x = LittleEndian.ToInt32 (header, 0x6c);
int y = LittleEndian.ToInt32 (header, 0x70);
using (var png = new StreamRegion (stream, stream.Position, data_size, true))
using (var png = new StreamRegion (stream.AsStream, stream.Position, data_size, true))
{
var decoder = new PngBitmapDecoder (png,
BitmapCreateOptions.None, BitmapCacheOption.OnLoad);

View File

@ -49,7 +49,7 @@ namespace GameRes.Formats.Cri
return null;
using (var lzss = new LzssStream (stream.AsStream, LzssMode.Decompress, true))
using (var input = new SeekableStream (lzss))
using (var xtx = new BinaryStream (input))
using (var xtx = new BinaryStream (input, stream.Name))
return base.ReadMetaData (xtx);
}
@ -58,7 +58,7 @@ namespace GameRes.Formats.Cri
stream.Position = 4;
using (var lzss = new LzssStream (stream.AsStream, LzssMode.Decompress, true))
using (var input = new SeekableStream (lzss))
using (var xtx = new BinaryStream (input))
using (var xtx = new BinaryStream (input, stream.Name))
return base.Read (xtx, info);
}

View File

@ -36,9 +36,9 @@ namespace GameRes.Formats.Crowd
public override string Description { get { return "Crowd engine audio format (Ogg/Vorbis)"; } }
public override uint Signature { get { return 0x004D5243; } } // 'CRM'
public override SoundInput TryOpen (Stream file)
public override SoundInput TryOpen (IBinaryStream file)
{
var ogg = new StreamRegion (file, 8);
var ogg = new StreamRegion (file.AsStream, 8);
return new OggInput (ogg);
// in case of exception ogg stream is left undisposed
}

View File

@ -43,23 +43,21 @@ namespace GameRes.Formats.Crowd
static readonly byte[] SignatureText = Encoding.ASCII.GetBytes ("cwd format - version 1.00 -");
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x38];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
var header = stream.ReadHeader (0x38);
if (!header.Take (SignatureText.Length).SequenceEqual (SignatureText))
return null;
uint key = header[0x34] + 0x259Au;
return new ImageMetaData
{
Width = LittleEndian.ToUInt32 (header, 0x2c) + key,
Height = LittleEndian.ToUInt32 (header, 0x30) + key,
Width = header.ToUInt32 (0x2c) + key,
Height = header.ToUInt32 (0x30) + key,
BPP = 15,
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
stream.Position = 0x38;
int size = (int)info.Width * (int)info.Height * 2;
@ -82,36 +80,34 @@ namespace GameRes.Formats.Crowd
public override string Description { get { return "LZ-compressed Crowd bitmap"; } }
public override uint Signature { get { return 0x44445A53u; } } // 'SZDD'
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
stream.Position = 0x0e;
using (var lz = new LzssReader (stream, 100, 0x38)) // extract CWD header
using (var lz = new LzssReader (stream.AsStream, 100, 0x38)) // extract CWD header
{
lz.FrameSize = 0x1000;
lz.FrameFill = 0x20;
lz.FrameInitPos = 0x1000 - 0x10;
lz.Unpack();
using (var cwd = new MemoryStream (lz.Data))
using (var cwd = new BinMemoryStream (lz.Data, stream.Name))
return base.ReadMetaData (cwd);
}
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
if (stream.Length > int.MaxValue)
if (file.Length > int.MaxValue)
throw new FileSizeException();
var header = new byte[14];
if (header.Length != stream.Read (header, 0, header.Length))
throw new InvalidFormatException();
int data_length = LittleEndian.ToInt32 (header, 10);
int input_length = (int)(stream.Length-stream.Position);
using (var lz = new LzssReader (stream, input_length, data_length))
var header = file.ReadHeader (14);
int data_length = header.ToInt32 (10);
int input_length = (int)(file.Length-file.Position);
using (var lz = new LzssReader (file.AsStream, input_length, data_length))
{
lz.FrameSize = 0x1000;
lz.FrameFill = 0x20;
lz.FrameInitPos = 0x1000 - 0x10;
lz.Unpack();
using (var cwd = new MemoryStream (lz.Data))
using (var cwd = new BinMemoryStream (lz.Data, file.Name))
return base.Read (cwd, info);
}
}

View File

@ -40,50 +40,47 @@ namespace GameRes.Formats.Crowd
public override uint Signature { get { return 0x50445743; } } // 'CWDP'
public override bool CanWrite { get { return true; } }
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream input)
{
using (var input = new ArcView.Reader (stream))
input.Position = 4;
uint width = Binary.BigEndian (input.ReadUInt32());
uint height = Binary.BigEndian (input.ReadUInt32());
if (0 == width || 0 == height)
return null;
int bpp = input.ReadByte();
int color_type = input.ReadByte();
switch (color_type)
{
input.ReadInt32();
uint width = Binary.BigEndian (input.ReadUInt32());
uint height = Binary.BigEndian (input.ReadUInt32());
if (0 == width || 0 == height)
return null;
int bpp = input.ReadByte();
int color_type = input.ReadByte();
switch (color_type)
{
case 2: bpp *= 3; break;
case 4: bpp *= 2; break;
case 6: bpp *= 4; break;
case 3:
case 0: break;
default: return null;
}
return new ImageMetaData
{
Width = width,
Height = height,
BPP = bpp,
};
case 2: bpp *= 3; break;
case 4: bpp *= 2; break;
case 6: bpp *= 4; break;
case 3:
case 0: break;
default: return null;
}
return new ImageMetaData
{
Width = width,
Height = height,
BPP = bpp,
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var header = new byte[0x15];
using (var mem = new MemoryStream((int)(0x14 + stream.Length + 12)))
using (var mem = new MemoryStream((int)(0x14 + file.Length + 12)))
using (var png = new BinaryWriter (mem))
{
png.Write (0x474E5089u); // png header
png.Write (0x0A1A0A0Du);
png.Write (0x0D000000u);
png.Write (0x52444849u); // 'IHDR'
stream.Position = 4;
stream.Read (header, 0, header.Length);
file.Position = 4;
file.Read (header, 0, header.Length);
png.Write (header, 0, header.Length);
png.Write (0x54414449u); // 'IDAT'
stream.CopyTo (mem);
file.AsStream.CopyTo (mem);
header[1] = 0;
header[2] = 0;
header[3] = 0;
@ -149,7 +146,9 @@ namespace GameRes.Formats.Crowd
png.Read (header, 0, header.Length);
cwp.Write (0x50445743u); // 'CWDP'
cwp.Write (header, 0, header.Length);
var idat = PngFormat.FindChunk (png, "IDAT");
long idat;
using (var bin = new BinMemoryStream (png, ""))
idat = PngFormat.FindChunk (bin, "IDAT");
if (-1 == idat)
throw new InvalidFormatException ("CWP conversion failed");
png.Position = idat;

View File

@ -39,10 +39,10 @@ namespace GameRes.Formats.Crowd
public override uint Signature { get { return 0x44445A53u; } } // 'SZDD'
public override bool CanWrite { get { return false; } }
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
stream.Position = 0x0e;
using (var lz = new LzssReader (stream, 100, 54)) // extract BMP header
using (var lz = new LzssReader (stream.AsStream, 100, 54)) // extract BMP header
{
lz.FrameSize = 0x1000;
lz.FrameFill = 0x20;
@ -51,21 +51,19 @@ namespace GameRes.Formats.Crowd
var header = lz.Data;
for (int i = 0; i < 54; ++i)
header[i] ^= 0xff;
using (var bmp = new MemoryStream (header))
using (var bmp = new BinMemoryStream (header, stream.Name))
return base.ReadMetaData (bmp);
}
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
if (stream.Length > int.MaxValue)
throw new FileSizeException();
var header = new byte[14];
if (header.Length != stream.Read (header, 0, header.Length))
throw new InvalidFormatException();
int data_length = LittleEndian.ToInt32 (header, 10);
var header = stream.ReadHeader (14);
int data_length = header.ToInt32 (10);
int input_length = (int)(stream.Length-stream.Position);
using (var lz = new LzssReader (stream, input_length, data_length))
using (var lz = new LzssReader (stream.AsStream, input_length, data_length))
{
lz.FrameSize = 0x1000;
lz.FrameFill = 0x20;
@ -75,7 +73,7 @@ namespace GameRes.Formats.Crowd
int count = Math.Min (100, data.Length);
for (int i = 0; i < count; ++i)
data[i] ^= 0xff;
using (var bmp = new MemoryStream (data))
using (var bmp = new BinMemoryStream (data, stream.Name))
return base.Read (bmp, info);
}
}

View File

@ -149,7 +149,7 @@ namespace GameRes.Formats.CsWare
header[i] = (byte)(pent.Key - header[i] - 1);
}
if (header_size == entry.Size)
return new MemoryStream (header);
return new BinMemoryStream (header, entry.Name);
var rest = arc.File.CreateStream (entry.Offset+512, entry.Size-512);
return new PrefixStream (header, rest);
}

View File

@ -90,9 +90,8 @@ namespace GameRes.Formats.Cyberworks
input = new MemoryStream (header);
else
input = new PrefixStream (header, new StreamRegion (file.AsStream, file.Position));
var ogg = new BinaryStream (input, file.Name);
var sound = OggAudio.Instance.TryOpen (ogg);
if (sound != null && header.Length >= file.Length)
var sound = new OggInput (input);
if (header.Length >= file.Length)
file.Dispose();
return sound;
}

View File

@ -130,7 +130,7 @@ namespace GameRes.Formats.DDSystem
reader.Unpack (data);
if (data.Length > 16 && Binary.AsciiEqual (data, 0, "DDSxHXB"))
DecryptHxb (data);
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
}

View File

@ -157,7 +157,7 @@ namespace GameRes.Formats.Dac
var data = new byte[entry.Size];
arc.File.View.Read (entry.Offset, data, 0, entry.Size);
DecryptEntry (data, parc.Key1, parc.Key2, pentry);
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
private void DecryptIndex (byte[] buf, int base_offset, int length, byte last)

View File

@ -43,28 +43,22 @@ namespace GameRes.Formats.Dac
public override string Description { get { return "DAC engine image format"; } }
public override uint Signature { get { return 0x00434744; } } // 'DGC'
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
using (var input = new ArcView.Reader (stream))
{
input.ReadInt32();
var info = new DgcMetaData();
info.Flags = input.ReadUInt32();
info.Width = input.ReadUInt16();
info.Height = input.ReadUInt16();
if (info.Width > 0x7fff || info.Height > 0x7fff)
return null;
info.BPP = 0 == (info.Flags & Reader.FlagAlphaChannel) ? 24 : 32;
return info;
}
file.Position = 4;
var info = new DgcMetaData();
info.Flags = file.ReadUInt32();
info.Width = file.ReadUInt16();
info.Height = file.ReadUInt16();
if (info.Width > 0x7fff || info.Height > 0x7fff)
return null;
info.BPP = 0 == (info.Flags & Reader.FlagAlphaChannel) ? 24 : 32;
return info;
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = info as DgcMetaData;
if (null == meta)
throw new ArgumentException ("DgcFormat.Read should be supplied with DgcMetaData", "info");
var meta = (DgcMetaData)info;
stream.Position = 12;
using (var reader = new Reader (stream, meta))
{
@ -80,7 +74,7 @@ namespace GameRes.Formats.Dac
internal class Reader : IDataUnpacker, IDisposable
{
BinaryReader m_input;
IBinaryStream m_input;
byte[] m_output;
readonly int m_width;
readonly int m_height;
@ -96,11 +90,11 @@ namespace GameRes.Formats.Dac
public byte[] Data { get { return m_output; } }
public PixelFormat Format { get; private set; }
public Reader (Stream input, DgcMetaData info)
public Reader (IBinaryStream input, DgcMetaData info)
{
m_width = (int)info.Width;
m_height = (int)info.Height;
m_input = new ArcView.Reader (input);
m_input = input;
m_use_dict = 0 != (info.Flags & FlagUseDictionary);
m_has_alpha = 0 != (info.Flags & FlagAlphaChannel);
m_max_dict_size = (int)(info.Flags & 0xffffff);
@ -162,7 +156,7 @@ namespace GameRes.Formats.Dac
if (dict_len > 256)
i = m_input.ReadUInt16();
else
i = m_input.ReadByte();
i = m_input.ReadUInt8();
i *= 3;
m_output[dst] = dict[i];
m_output[dst+1] = dict[i+1];
@ -178,7 +172,7 @@ namespace GameRes.Formats.Dac
{
var dict = new byte[m_max_dict_size * 3];
int dict_len = m_input.ReadByte() + 1;
int dict_len = m_input.ReadUInt8() + 1;
m_input.Read (dict, 0, dict_len * 3);
for (int y = 0; y < m_height; y++)
@ -198,7 +192,7 @@ namespace GameRes.Formats.Dac
{
for (int x = 0; x < m_width; x++)
{
int i = 3 * m_input.ReadByte();
int i = 3 * m_input.ReadUInt8();
m_output[dst] = dict[i];
m_output[dst+1] = dict[i+1];
m_output[dst+2] = dict[i+2];
@ -260,7 +254,7 @@ namespace GameRes.Formats.Dac
{
for (int x = 0; x < m_width; x++)
{
m_output[dst] = m_input.ReadByte();
m_output[dst] = m_input.ReadUInt8();
dst += m_pixel_size;
}
}
@ -290,7 +284,7 @@ namespace GameRes.Formats.Dac
int index = 0;
if (0 != (ctl & 0x2000))
{
index = m_input.ReadByte();
index = m_input.ReadUInt8();
--length;
}
else
@ -314,7 +308,7 @@ namespace GameRes.Formats.Dac
int index = 0;
if (0 != (ctl & 0x2000))
{
index = m_input.ReadByte();
index = m_input.ReadUInt8();
--length;
}
else
@ -337,11 +331,11 @@ namespace GameRes.Formats.Dac
{
while (length > 0)
{
byte ctl = m_input.ReadByte();
byte ctl = m_input.ReadUInt8();
--length;
if (0 != ctl)
{
int index = 3 * m_input.ReadByte();
int index = 3 * m_input.ReadUInt8();
--length;
while (0 != ctl--)
{
@ -353,13 +347,13 @@ namespace GameRes.Formats.Dac
}
else
{
ctl = m_input.ReadByte();
ctl = m_input.ReadUInt8();
--length;
if (0 == (ctl & 0x80))
{
for (int count = ctl + 2; 0 != count; --count)
{
int src = 3 * m_input.ReadByte();
int src = 3 * m_input.ReadUInt8();
--length;
m_output[dst] = dict[src];
m_output[dst+1] = dict[src+1];
@ -369,7 +363,7 @@ namespace GameRes.Formats.Dac
}
else
{
int offset = (short)((ctl << 8) | m_input.ReadByte());
int offset = (short)((ctl << 8) | m_input.ReadUInt8());
--length;
int count = (offset & 0x3F) + 4;
offset >>= 6;
@ -386,11 +380,11 @@ namespace GameRes.Formats.Dac
{
while (length > 0)
{
byte ctl = m_input.ReadByte();
byte ctl = m_input.ReadUInt8();
--length;
if (0 != ctl)
{
byte alpha = m_input.ReadByte();
byte alpha = m_input.ReadUInt8();
--length;
while (0 != ctl--)
{
@ -400,7 +394,7 @@ namespace GameRes.Formats.Dac
}
else
{
ctl = m_input.ReadByte();
ctl = m_input.ReadUInt8();
--length;
if (0 == (ctl & 0x80))
{
@ -408,13 +402,13 @@ namespace GameRes.Formats.Dac
length -= count;
while (0 != count--)
{
m_output[dst] = m_input.ReadByte();
m_output[dst] = m_input.ReadUInt8();
dst += m_pixel_size;
}
}
else
{
int offset = (short)((ctl << 8) | m_input.ReadByte());
int offset = (short)((ctl << 8) | m_input.ReadUInt8());
--length;
int count = (offset & 0x3F) + 4;
offset >>= 6;
@ -479,25 +473,10 @@ namespace GameRes.Formats.Dac
}
#region IDisposable Members
bool disposed = false;
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (!disposed)
{
if (disposing)
{
m_input.Dispose();
}
disposed = true;
}
}
#endregion
}
}

View File

@ -53,13 +53,14 @@ namespace GameRes.Formats.Dogenzaka
public static readonly byte[] KnownKey = Encoding.ASCII.GetBytes ("Hlk9D28p");
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
using (var sha = SHA1.Create())
{
var key = sha.ComputeHash (KnownKey).Take (16).ToArray();
using (var proxy = new InputProxyStream (stream, true))
using (var input = new CryptoStream (proxy, new Rc4Transform (key), CryptoStreamMode.Read))
using (var proxy = new InputProxyStream (stream.AsStream, true))
using (var crypto = new CryptoStream (proxy, new Rc4Transform (key), CryptoStreamMode.Read))
using (var input = new BinaryStream (crypto, stream.Name))
{
var info = base.ReadMetaData (input);
if (null == info)
@ -77,12 +78,13 @@ namespace GameRes.Formats.Dogenzaka
}
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var rc4 = (Rc4PngMetaData)info;
using (var sha = SHA1.Create())
using (var proxy = new InputProxyStream (stream, true))
using (var input = new CryptoStream (proxy, new Rc4Transform (rc4.Key), CryptoStreamMode.Read))
using (var proxy = new InputProxyStream (stream.AsStream, true))
using (var crypto = new CryptoStream (proxy, new Rc4Transform (rc4.Key), CryptoStreamMode.Read))
using (var input = new BinaryStream (crypto, stream.Name))
return base.Read (input, info);
}

View File

@ -31,11 +31,11 @@ namespace GameRes.Formats.??????
public override string Description { get { return "?????? image format"; } }
public override uint Signature { get { return 0; } }
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var meta = (xxxMetaData)info;

View File

@ -145,7 +145,7 @@ namespace GameRes.Formats.DxLib
using (input)
{
var data = Unpack (input);
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
}

View File

@ -162,7 +162,7 @@ namespace GameRes.Formats.DxLib
var offset = scr_arc.Encryption.StartOffset;
scr_arc.Encryption.Decrypt (data, offset, data.Length-offset);
}
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
public override ResourceOptions GetDefaultOptions ()

View File

@ -333,7 +333,7 @@ namespace GameRes.Formats.Eagls
{
byte[] input = File.View.ReadBytes (entry.Offset, entry.Size);
Encryption.Decrypt (input);
return new MemoryStream (input);
return new BinMemoryStream (input, entry.Name);
}
}
}

View File

@ -45,9 +45,9 @@ namespace GameRes.Formats.Eagls
public override uint Signature { get { return 0; } }
public override bool CanWrite { get { return false; } }
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
using (var lzs = new LzssStream (stream, LzssMode.Decompress, true))
using (var lzs = new LzssStream (file.AsStream, LzssMode.Decompress, true))
{
if (lzs.ReadByte() != 'B' || lzs.ReadByte() != 'M')
return null;
@ -71,10 +71,11 @@ namespace GameRes.Formats.Eagls
}
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var meta = (GrMetaData)info;
using (var bmp = new LzssStream (stream, LzssMode.Decompress, true))
using (var lzs = new LzssStream (file.AsStream, LzssMode.Decompress, true))
using (var bmp = new BinaryStream (lzs, file.Name))
{
if (32 != info.BPP)
return base.Read (bmp, info);

View File

@ -43,24 +43,22 @@ namespace GameRes.Formats.Emic
Signatures = new uint[] { 0x1050574D, 0x4C594554 }; // 'TEYL'
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
var header = new byte[12];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
file.Position = 4;
return new ImageMetaData
{
Width = LittleEndian.ToUInt32 (header, 4),
Height = LittleEndian.ToUInt32 (header, 8),
Width = file.ReadUInt32(),
Height = file.ReadUInt32(),
BPP = 32,
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var pixels = new byte[info.Width*info.Height*4];
stream.Position = 12;
if (pixels.Length != stream.Read (pixels, 0, pixels.Length))
file.Position = 12;
if (pixels.Length != file.Read (pixels, 0, pixels.Length))
throw new EndOfStreamException();
return ImageData.Create (info, PixelFormats.Bgra32, null, pixels);
}

View File

@ -128,7 +128,7 @@ namespace GameRes.Formats.EmonEngine
lzss.Config.FrameInitPos = entry.LzssInitPos;
lzss.Read (data, part1_size, unpacked_size);
}
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
else
{

View File

@ -583,10 +583,10 @@ namespace GameRes.Formats.Emote
Extensions = new string[0];
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
stream.Position = 4;
using (var reader = new BinaryReader (stream, Encoding.UTF8, true))
using (var reader = new BinaryReader (stream.AsStream, Encoding.UTF8, true))
{
var info = new PsbTexMetaData { BPP = 32 };
info.DataOffset = reader.ReadInt32();
@ -599,14 +599,14 @@ namespace GameRes.Formats.Emote
}
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (PsbTexMetaData)info;
var pixels = new byte[meta.Width * meta.Height * 4];
if ("RGBA8" == meta.TexType)
ReadRgba8 (stream, meta, pixels);
ReadRgba8 (stream.AsStream, meta, pixels);
else if ("RGBA4444" == meta.TexType)
ReadRgba4444 (stream, meta, pixels);
ReadRgba4444 (stream.AsStream, meta, pixels);
else
throw new NotImplementedException (string.Format ("PSB texture format '{0}' not implemented", meta.TexType));
return ImageData.Create (info, PixelFormats.Bgra32, null, pixels);

View File

@ -42,17 +42,15 @@ namespace GameRes.Formats.Entis
public override string Description { get { return "Entis engine compressed audio format"; } }
public override uint Signature { get { return 0x69746e45u; } } // 'Enti'
public override SoundInput TryOpen (Stream file)
public override SoundInput TryOpen (IBinaryStream file)
{
byte[] header = new byte[0x40];
if (header.Length != file.Read (header, 0, header.Length))
var header = file.ReadHeader (0x40);
if (0x03000100 != header.ToUInt32 (8))
return null;
if (0x03000100 != LittleEndian.ToUInt32 (header, 8))
return null;
if (!Binary.AsciiEqual (header, 0x10, "Music Interleaved"))
if (!header.AsciiEqual (0x10, "Music Interleaved"))
return null;
return new MioInput (file);
return new MioInput (file.AsStream);
}
}

View File

@ -149,17 +149,15 @@ namespace GameRes.Formats.Entis
public override string Description { get { return "Entis rasterized image format"; } }
public override uint Signature { get { return 0x69746e45u; } } // 'Enti'
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
byte[] header = new byte[0x40];
if (header.Length != stream.Read (header, 0, header.Length))
var header = stream.ReadHeader (0x40);
if (0x03000100 != header.ToUInt32 (8))
return null;
if (0x03000100 != LittleEndian.ToUInt32 (header, 8))
if (!header.AsciiEqual (0x10, "Entis Rasterized Image")
&& !header.AsciiEqual (0x10, "Moving Entis Image"))
return null;
if (!Binary.AsciiEqual (header, 0x10, "Entis Rasterized Image")
&& !Binary.AsciiEqual (header, 0x10, "Moving Entis Image"))
return null;
using (var reader = new EriFile (stream))
using (var reader = new EriFile (stream.AsStream))
{
var section = reader.ReadSection();
if (section.Id != "Header " || section.Length <= 0)
@ -240,7 +238,7 @@ namespace GameRes.Formats.Entis
}
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var reader = ReadImageData (stream, (EriMetaData)info);
return ImageData.Create (info, reader.Format, reader.Palette, reader.Data, reader.Stride);
@ -261,11 +259,11 @@ namespace GameRes.Formats.Entis
return colors;
}
internal EriReader ReadImageData (Stream stream, EriMetaData meta)
internal EriReader ReadImageData (IBinaryStream stream, EriMetaData meta)
{
stream.Position = meta.StreamPos;
Color[] palette = null;
using (var input = new EriFile (stream))
using (var input = new EriFile (stream.AsStream))
{
for (;;) // ReadSection throws an exception in case of EOF
{
@ -276,13 +274,13 @@ namespace GameRes.Formats.Entis
break;
if ("Palette " == section.Id && meta.BPP <= 8 && section.Length <= 0x400)
{
palette = ReadPalette (stream, (int)section.Length);
palette = ReadPalette (stream.AsStream, (int)section.Length);
continue;
}
input.BaseStream.Seek (section.Length, SeekOrigin.Current);
}
}
var reader = new EriReader (stream, meta, palette);
var reader = new EriReader (stream.AsStream, meta, palette);
reader.DecodeImage();
if (!string.IsNullOrEmpty (meta.Description))
@ -298,7 +296,7 @@ namespace GameRes.Formats.Entis
throw new InvalidFormatException();
ref_file = VFS.CombinePath (VFS.GetDirectoryName (meta.FileName), ref_file);
using (var ref_src = VFS.OpenSeekableStream (ref_file))
using (var ref_src = VFS.OpenBinaryStream (ref_file))
{
var ref_info = ReadMetaData (ref_src) as EriMetaData;
if (null == ref_info)

View File

@ -83,7 +83,7 @@ namespace GameRes.Formats.BGI
using (var decoder = new DscDecoder (input))
{
decoder.Unpack();
return new MemoryStream (decoder.Output);
return new BinMemoryStream (decoder.Output, entry.Name);
}
}
return new ArcView.ArcStream (input, entry_offset, entry.Size);

View File

@ -41,18 +41,16 @@ namespace GameRes.Formats.BGI
Extensions = new string[] { "" };
}
public override SoundInput TryOpen (Stream file)
public override SoundInput TryOpen (IBinaryStream file)
{
var header = new byte[8];
if (8 != file.Read (header, 0, 8))
var header = file.ReadHeader (8);
if (!header.AsciiEqual (4, "bw "))
return null;
if (!Binary.AsciiEqual (header, 4, "bw "))
return null;
uint offset = LittleEndian.ToUInt32 (header, 0);
uint offset = header.ToUInt32 (0);
if (offset >= file.Length)
return null;
var input = new StreamRegion (file, offset);
var input = new StreamRegion (file.AsStream, offset);
return new OggInput (input);
// input is left undisposed in case of exception.
}

View File

@ -51,29 +51,26 @@ namespace GameRes.Formats.BGI
throw new System.NotImplementedException ("BgiFormat.Write not implemented");
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
using (var input = new ArcView.Reader (stream))
int width = stream.ReadInt16();
int height = stream.ReadInt16();
if (width <= 0 || height <= 0)
return null;
int bpp = stream.ReadInt32();
if (24 != bpp && 32 != bpp && 8 != bpp)
return null;
if (0 != stream.ReadInt64())
return null;
return new ImageMetaData
{
int width = input.ReadInt16();
int height = input.ReadInt16();
if (width <= 0 || height <= 0)
return null;
int bpp = input.ReadInt32();
if (24 != bpp && 32 != bpp && 8 != bpp)
return null;
if (0 != input.ReadInt64())
return null;
return new ImageMetaData
{
Width = (uint)width,
Height = (uint)height,
BPP = bpp,
};
}
Width = (uint)width,
Height = (uint)height,
BPP = bpp,
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
PixelFormat format;
if (24 == info.BPP)
@ -119,33 +116,29 @@ namespace GameRes.Formats.BGI
throw new System.NotImplementedException ("BgiFormat.Write not implemented");
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x30];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
if (!Binary.AsciiEqual (header, "CompressedBG___"))
var header = stream.ReadHeader (0x30);
if (!header.AsciiEqual ("CompressedBG___"))
return null;
return new CbgMetaData
{
Width = LittleEndian.ToUInt16 (header, 0x10),
Height = LittleEndian.ToUInt16 (header, 0x12),
BPP = LittleEndian.ToInt32 (header, 0x14),
IntermediateLength = LittleEndian.ToInt32 (header, 0x20),
Key = LittleEndian.ToUInt32 (header, 0x24),
EncLength = LittleEndian.ToInt32 (header, 0x28),
Width = header.ToUInt16 (0x10),
Height = header.ToUInt16 (0x12),
BPP = header.ToInt32 (0x14),
IntermediateLength = header.ToInt32 (0x20),
Key = header.ToUInt32 (0x24),
EncLength = header.ToInt32 (0x28),
CheckSum = header[0x2C],
CheckXor = header[0x2D],
Version = LittleEndian.ToUInt16 (header, 0x2E),
Version = header.ToUInt16 (0x2E),
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = info as CbgMetaData;
if (null == meta)
throw new ArgumentException ("CompressedBGFormat.Read should be supplied with CbgMetaData", "info");
using (var reader = new CbgReader (stream, meta))
var meta = (CbgMetaData)info as CbgMetaData;
using (var reader = new CbgReader (stream.AsStream, meta))
{
reader.Unpack();
return ImageData.Create (meta, reader.Format, null, reader.Data, reader.Stride);

View File

@ -51,7 +51,7 @@ namespace GameRes.Formats.Eushully
Signatures = new uint[] { 0x46474341, 0 };
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x20];
if (0x18 != stream.Read (header, 0, 0x18))
@ -64,7 +64,7 @@ namespace GameRes.Formats.Eushully
return null;
int unpacked_size = LittleEndian.ToInt32 (header, 0x10);
int packed_size = LittleEndian.ToInt32 (header, 0x14);
using (var unpacked = AgfReader.OpenSection (stream, unpacked_size, packed_size))
using (var unpacked = AgfReader.OpenSection (stream.AsStream, unpacked_size, packed_size))
using (var reader = new BinaryReader (unpacked))
{
if (0x20 != reader.Read (header, 0, 0x20))
@ -101,7 +101,7 @@ namespace GameRes.Formats.Eushully
return palette;
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
using (var reader = new AgfReader (stream, (AgfMetaData)info))
{
@ -118,7 +118,7 @@ namespace GameRes.Formats.Eushully
internal sealed class AgfReader : IDisposable
{
BinaryReader m_input;
IBinaryStream m_input;
byte[] m_output;
int m_width;
int m_height;
@ -129,9 +129,9 @@ namespace GameRes.Formats.Eushully
public PixelFormat Format { get; private set; }
public byte[] Data { get { return m_output; } }
public AgfReader (Stream input, AgfMetaData info)
public AgfReader (IBinaryStream input, AgfMetaData info)
{
m_input = new ArcView.Reader (input);
m_input = input;
m_bpp = info.BPP;
m_source_bpp = info.SourceBPP;
input.Position = info.DataOffset;
@ -155,15 +155,15 @@ namespace GameRes.Formats.Eushully
m_input.ReadInt32();
int data_size = m_input.ReadInt32();
int packed_size = m_input.ReadInt32();
var data_pos = m_input.BaseStream.Position;
var data_pos = m_input.Position;
var bmp_data = new byte[data_size];
using (var unpacked = OpenSection (m_input.BaseStream, data_size, packed_size))
using (var unpacked = OpenSection (m_input.AsStream, data_size, packed_size))
if (data_size != unpacked.Read (bmp_data, 0, data_size))
throw new EndOfStreamException();
byte[] alpha = null;
if (32 == m_bpp)
{
m_input.BaseStream.Position = data_pos + packed_size;
m_input.Position = data_pos + packed_size;
alpha = ReadAlphaChannel();
if (null == alpha)
m_bpp = 24;
@ -231,21 +231,15 @@ namespace GameRes.Formats.Eushully
if (m_width*m_height != unpacked_size)
return null;
var alpha = new byte[unpacked_size];
using (var unpacked = OpenSection (m_input.BaseStream, unpacked_size, packed_size))
using (var unpacked = OpenSection (m_input.AsStream, unpacked_size, packed_size))
if (unpacked_size != unpacked.Read (alpha, 0, unpacked_size))
return null;
return alpha;
}
#region IDisposable methods
bool _disposed = false;
public void Dispose ()
{
if (!_disposed)
{
m_input.Dispose();
_disposed = true;
}
}
#endregion
}

View File

@ -53,7 +53,7 @@ namespace GameRes.Formats.Eushully
Extensions = new string[] { "gpcf" }; // made-up, real files have no extension
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
int alpha_channel = stream.ReadByte();
int method = stream.ReadByte();
@ -64,30 +64,28 @@ namespace GameRes.Formats.Eushully
|| align1 < 0 || align1 > 4 || align2 < 0 || align2 > 4
|| bpp <= 0 || !(bpp <= 16 || 24 == bpp || 32 == bpp))
return null;
using (var reader = new ArcView.Reader (stream))
int palette_size = stream.ReadInt32();
uint width = stream.ReadUInt16();
uint height = stream.ReadUInt16();
if (palette_size <= 0 || 0 == width || 0 == height || palette_size >= stream.Length)
return null;
if (bpp <= 8 && palette_size > 0x100)
return null;
return new GpMetaData
{
int palette_size = reader.ReadInt32();
uint width = reader.ReadUInt16();
uint height = reader.ReadUInt16();
if (palette_size <= 0 || 0 == width || 0 == height || palette_size >= stream.Length)
return null;
if (bpp <= 8 && palette_size > 0x100)
return null;
return new GpMetaData
{
Width = width,
Height = height,
BPP = bpp,
HasAlpha = alpha_channel != 0,
Method = method,
ElementSize = align1,
PixelsPerElement = align2,
PaletteSize = palette_size,
};
}
Width = width,
Height = height,
BPP = bpp,
HasAlpha = alpha_channel != 0,
Method = method,
ElementSize = align1,
PixelsPerElement = align2,
PaletteSize = palette_size,
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (GpMetaData)info;
using (var reader = new GpReader (stream, meta))
@ -105,7 +103,7 @@ namespace GameRes.Formats.Eushully
internal sealed class GpReader : IDisposable
{
BinaryReader m_input;
IBinaryStream m_input;
GpMetaData m_info;
int m_width;
int m_height;
@ -115,17 +113,17 @@ namespace GameRes.Formats.Eushully
public byte[] Data { get; private set; }
public int Stride { get; private set; }
public GpReader (Stream input, GpMetaData info)
public GpReader (IBinaryStream input, GpMetaData info)
{
m_info = info;
m_width = (int)m_info.Width;
m_height = (int)m_info.Height;
m_input = new ArcView.Reader (input);
m_input = input;
}
public void Unpack ()
{
m_input.BaseStream.Position = 0xD;
m_input.Position = 0xD;
switch (m_info.Method)
{
case 0: UnpackV0(); break;
@ -293,8 +291,8 @@ namespace GameRes.Formats.Eushully
int i = 3;
while (i < Data.Length)
{
byte alpha = m_input.ReadByte();
int count = m_input.ReadByte();
byte alpha = m_input.ReadUInt8();
int count = m_input.ReadUInt8();
for (int j = 0; j < count && i < Data.Length; ++j)
{
Data[i] = alpha;
@ -305,14 +303,8 @@ namespace GameRes.Formats.Eushully
}
#region IDisposable Members
bool _disposed = false;
public void Dispose ()
{
if (!_disposed)
{
m_input.Dispose();
_disposed = true;
}
}
#endregion
}

View File

@ -69,29 +69,26 @@ namespace GameRes.Formats.ExHibit
set { KnownKeys = ((GyuMap)value).KnownKeys; }
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
using (var reader = new ArcView.Reader (stream))
stream.Position = 4;
return new GyuMetaData
{
reader.ReadInt32();
return new GyuMetaData
{
Flags = reader.ReadUInt16(),
CompressionMode = reader.ReadUInt16(),
Key = reader.ReadUInt32(),
BPP = reader.ReadInt32(),
Width = reader.ReadUInt32(),
Height = reader.ReadUInt32(),
DataSize = reader.ReadInt32(),
AlphaSize = reader.ReadInt32(),
PaletteSize = reader.ReadInt32(),
};
}
Flags = stream.ReadUInt16(),
CompressionMode = stream.ReadUInt16(),
Key = stream.ReadUInt32(),
BPP = stream.ReadInt32(),
Width = stream.ReadUInt32(),
Height = stream.ReadUInt32(),
DataSize = stream.ReadInt32(),
AlphaSize = stream.ReadInt32(),
PaletteSize = stream.ReadInt32(),
};
}
IDictionary<int, uint> CurrentMap = null;
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (GyuMetaData)info;
if (0 == meta.Key)
@ -111,7 +108,7 @@ namespace GameRes.Formats.ExHibit
throw new UnknownEncryptionScheme ("Unknown image encryption key");
}
}
var reader = new GyuReader (stream, meta);
var reader = new GyuReader (stream.AsStream, meta);
reader.Unpack();
return ImageData.CreateFlipped (meta, reader.Format, reader.Palette, reader.Data, reader.Stride);
}

View File

@ -114,7 +114,7 @@ namespace GameRes.Formats.FC01
var data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
var reader = new MrgDecoder (data);
reader.Unpack();
input = new MemoryStream (reader.Data);
input = new BinMemoryStream (reader.Data, entry.Name);
}
else
input = arc.File.CreateStream (entry.Offset, entry.Size);
@ -124,7 +124,7 @@ namespace GameRes.Formats.FC01
using (var reader = new MrgLzssReader (input, (int)input.Length, (int)packed_entry.UnpackedSize))
{
reader.Unpack();
return new MemoryStream (reader.Data);
return new BinMemoryStream (reader.Data, entry.Name);
}
}
return input;
@ -240,14 +240,14 @@ namespace GameRes.Formats.FC01
{
var data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
Decrypt (data, 0, data.Length, mrg_entry.Key, mrg_entry.ArcKey);
input = new MemoryStream (data);
input = new BinMemoryStream (data, entry.Name);
}
else if (mrg_entry.Method >= 2)
{
var data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
var reader = new MrgDecoder (data);
reader.Unpack();
input = new MemoryStream (reader.Data);
input = new BinMemoryStream (reader.Data, entry.Name);
}
else
input = arc.File.CreateStream (entry.Offset, entry.Size);
@ -257,7 +257,7 @@ namespace GameRes.Formats.FC01
using (var reader = new MrgLzssReader (input, (int)input.Length, (int)mrg_entry.UnpackedSize))
{
reader.Unpack();
return new MemoryStream (reader.Data);
return new BinMemoryStream (reader.Data, entry.Name);
}
}
return input;

View File

@ -45,20 +45,18 @@ namespace GameRes.Formats.FC01
public override string Description { get { return "F&C Co. image format"; } }
public override uint Signature { get { return 0x20444341; } } // 'ACD'
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x1c];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
int header_size = LittleEndian.ToInt32 (header, 8);
if (!Binary.AsciiEqual (header, 4, "1.00") || header_size < 0x1c)
var header = stream.ReadHeader (0x1c);
int header_size = header.ToInt32 (8);
if (!header.AsciiEqual (4, "1.00") || header_size < 0x1c)
throw new NotSupportedException ("Not supported ACD image version");
int packed_size = LittleEndian.ToInt32 (header, 0x0C);
int unpacked_size = LittleEndian.ToInt32 (header, 0x10);
int packed_size = header.ToInt32 (0x0C);
int unpacked_size = header.ToInt32 (0x10);
return new AcdMetaData
{
Width = LittleEndian.ToUInt32 (header, 0x14),
Height = LittleEndian.ToUInt32 (header, 0x18),
Width = header.ToUInt32 (0x14),
Height = header.ToUInt32 (0x18),
BPP = 24,
DataOffset = header_size,
PackedSize = packed_size,
@ -66,14 +64,12 @@ namespace GameRes.Formats.FC01
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = info as AcdMetaData;
if (null == meta)
throw new ArgumentException ("AcdFormat.Read should be supplied with AcdMetaData", "info");
var meta = (AcdMetaData)info;
stream.Position = meta.DataOffset;
using (var reader = new MrgLzssReader (stream, meta.PackedSize, meta.UnpackedSize))
using (var reader = new MrgLzssReader (stream.AsStream, meta.PackedSize, meta.UnpackedSize))
{
reader.Unpack();
var decoder = new AcdDecoder (reader.Data, meta);

View File

@ -45,20 +45,18 @@ namespace GameRes.Formats.FC01
public override string Description { get { return "F&C Co. image format"; } }
public override uint Signature { get { return 0x204D4C43; } } // 'CLM'
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x40];
if (header.Length != stream.Read (header, 0, header.Length))
var header = stream.ReadHeader (0x40);
if (!header.AsciiEqual (4, "1.00"))
return null;
if (!Binary.AsciiEqual (header, 4, "1.00"))
return null;
uint data_offset = LittleEndian.ToUInt32 (header, 0x10);
uint data_offset = header.ToUInt32 (0x10);
if (data_offset < 0x40)
return null;
uint width = LittleEndian.ToUInt32 (header, 0x1C);
uint height = LittleEndian.ToUInt32 (header, 0x20);
int bpp = LittleEndian.ToInt32 (header, 0x24);
int unpacked_size = LittleEndian.ToInt32 (header, 0x28);
uint width = header.ToUInt32 (0x1C);
uint height = header.ToUInt32 (0x20);
int bpp = header.ToInt32 (0x24);
int unpacked_size = header.ToInt32 (0x28);
return new ClmMetaData
{
Width = width,
@ -69,7 +67,7 @@ namespace GameRes.Formats.FC01
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (ClmMetaData)info;
stream.Position = meta.DataOffset;
@ -78,7 +76,7 @@ namespace GameRes.Formats.FC01
if (8 == meta.BPP)
{
format = PixelFormats.Indexed8;
palette = ReadPalette (stream);
palette = ReadPalette (stream.AsStream);
}
else if (24 == meta.BPP)
format = PixelFormats.Bgr24;
@ -87,7 +85,7 @@ namespace GameRes.Formats.FC01
else
throw new NotSupportedException ("Not supported CLM color depth");
int packed_size = (int)(stream.Length - stream.Position);
using (var reader = new MrgLzssReader (stream, packed_size, meta.UnpackedSize))
using (var reader = new MrgLzssReader (stream.AsStream, packed_size, meta.UnpackedSize))
{
reader.Unpack();
return ImageData.Create (info, format, palette, reader.Data);

View File

@ -67,31 +67,29 @@ namespace GameRes.Formats.FC01
set { KnownKeys = ((McgScheme)value).KnownKeys; }
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
byte[] header = new byte[0x40];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
var header = stream.ReadHeader (0x40);
if (header[5] != '.')
return null;
int version = header[4] * 100 + header[6] * 10 + header[7] - 0x14D0;
if (version != 200 && version != 101)
throw new NotSupportedException ("Not supported MCG format version");
int header_size = LittleEndian.ToInt32 (header, 0x10);
int header_size = header.ToInt32 (0x10);
if (header_size < 0x40)
return null;
int bpp = LittleEndian.ToInt32 (header, 0x24);
int bpp = header.ToInt32 (0x24);
if (24 != bpp)
throw new NotSupportedException ("Not supported MCG image bitdepth");
return new McgMetaData
{
Width = LittleEndian.ToUInt32 (header, 0x1c),
Height = LittleEndian.ToUInt32 (header, 0x20),
OffsetX = LittleEndian.ToInt32 (header, 0x14),
OffsetY = LittleEndian.ToInt32 (header, 0x18),
Width = header.ToUInt32 (0x1c),
Height = header.ToUInt32 (0x20),
OffsetX = header.ToInt32 (0x14),
OffsetY = header.ToInt32 (0x18),
BPP = bpp,
DataOffset = header_size,
PackedSize = LittleEndian.ToInt32 (header, 0x38) - header_size,
PackedSize = header.ToInt32 (0x38) - header_size,
Version = version,
};
}
@ -99,7 +97,7 @@ namespace GameRes.Formats.FC01
// cache key value so that dialog does not pop up on every file accessed.
byte? LastKey = null;
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (McgMetaData)info;
byte key = Settings.Default.MCGLastKey;
@ -113,7 +111,7 @@ namespace GameRes.Formats.FC01
else
key = LastKey.Value;
}
var reader = new McgDecoder (stream, meta, key);
var reader = new McgDecoder (stream.AsStream, meta, key);
reader.Unpack();
if (reader.Key != 0)
LastKey = reader.Key;

View File

@ -80,7 +80,7 @@ namespace GameRes.Formats.FVP
using (var decoder = new LzwDecoder (input, unpacked_size))
{
decoder.Unpack();
return new MemoryStream (decoder.Output);
return new BinMemoryStream (decoder.Output, entry.Name);
}
}
}

View File

@ -47,28 +47,26 @@ namespace GameRes.Formats.FVP
public override string Description { get { return "Favorite View Point image format"; } }
public override uint Signature { get { return 0x31637A68; } } // 'HZC1'
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x2C];
if (header.Length != stream.Read (header, 0, header.Length))
var header = stream.ReadHeader (0x2C);
if (!header.AsciiEqual (0xC, "NVSG"))
return null;
if (!Binary.AsciiEqual (header, 0xC, "NVSG"))
return null;
int type = LittleEndian.ToUInt16 (header, 0x12);
int type = header.ToUInt16 (0x12);
return new HzcMetaData
{
Width = LittleEndian.ToUInt16 (header, 0x14),
Height = LittleEndian.ToUInt16 (header, 0x16),
OffsetX = LittleEndian.ToInt16 (header, 0x18),
OffsetY = LittleEndian.ToInt16 (header, 0x1A),
Width = header.ToUInt16 (0x14),
Height = header.ToUInt16 (0x16),
OffsetX = header.ToInt16 (0x18),
OffsetY = header.ToInt16 (0x1A),
BPP = 0 == type ? 24 : type > 2 ? 8 : 32,
Type = type,
UnpackedSize = LittleEndian.ToInt32 (header, 4),
HeaderSize = LittleEndian.ToInt32 (header, 8),
UnpackedSize = header.ToInt32 (4),
HeaderSize = header.ToInt32 (8),
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (HzcMetaData)info;
BitmapPalette palette = null;
@ -90,7 +88,7 @@ namespace GameRes.Formats.FVP
}
}
stream.Position = 12 + meta.HeaderSize;
using (var z = new ZLibStream (stream, CompressionMode.Decompress, true))
using (var z = new ZLibStream (stream.AsStream, CompressionMode.Decompress, true))
{
var pixels = new byte[stride * (int)meta.Height];
if (pixels.Length != z.Read (pixels, 0, pixels.Length))

View File

@ -92,7 +92,7 @@ namespace GameRes.Formats.Ffa
using (var reader = new LzssReader (input, packed, unpacked))
{
reader.Unpack();
return new MemoryStream (reader.Data);
return new BinMemoryStream (reader.Data, entry.Name);
}
}
}

View File

@ -39,18 +39,9 @@ namespace GameRes.Formats.Ffa
public override string Description { get { return "FFA System wave audio format"; } }
public override uint Signature { get { return 0; } }
private static int ReadInt32 (Stream file)
public override SoundInput TryOpen (IBinaryStream file)
{
int dword = file.ReadByte();
dword |= file.ReadByte() << 8;
dword |= file.ReadByte() << 16;
dword |= file.ReadByte() << 24;
return dword;
}
public override SoundInput TryOpen (Stream file)
{
int packed = ReadInt32 (file);
int packed = file.ReadInt32();
if (packed < 0)
return null;
byte[] input;
@ -58,10 +49,10 @@ namespace GameRes.Formats.Ffa
{
if ((packed + 8) != file.Length)
return null;
int unpacked = ReadInt32 (file);
int unpacked = file.ReadInt32();
if (unpacked <= 0)
return null;
using (var reader = new LzssReader (file, packed, unpacked))
using (var reader = new LzssReader (file.AsStream, packed, unpacked))
{
reader.Unpack();
if (Binary.AsciiEqual (reader.Data, 0, "RIFF"))
@ -75,7 +66,7 @@ namespace GameRes.Formats.Ffa
}
else
{
if (0x46464952 != ReadInt32 (file)) // 'RIFF'
if (0x46464952 != file.ReadInt32()) // 'RIFF'
return null;
file.Position = 0;
input = new byte[file.Length];

View File

@ -36,7 +36,7 @@ namespace GameRes.Formats.Ffa
public override string Description { get { return "FFA System PCM audio format"; } }
public override uint Signature { get { return 0x4D435041; } } // 'APCM'
public override SoundInput TryOpen (Stream file)
public override SoundInput TryOpen (IBinaryStream file)
{
return new Wa2Input (file);
}
@ -51,27 +51,25 @@ namespace GameRes.Formats.Ffa
get { return (int)Format.AverageBytesPerSecond * 8; }
}
public Wa2Input (Stream file) : base (null)
public Wa2Input (IBinaryStream file) : base (null)
{
var header = new byte[0x2C];
if (header.Length != file.Read (header, 0, header.Length))
throw new EndOfStreamException();
if (!Binary.AsciiEqual (header, 8, "WAVEfmt "))
var header = file.ReadHeader (0x2C);
if (!header.AsciiEqual (8, "WAVEfmt "))
throw new InvalidFormatException();
var format = new WaveFormat();
format.FormatTag = LittleEndian.ToUInt16 (header, 0x14);
format.Channels = LittleEndian.ToUInt16 (header, 0x16);
format.SamplesPerSecond = LittleEndian.ToUInt32 (header, 0x18);
format.AverageBytesPerSecond = LittleEndian.ToUInt32 (header, 0x1C);
format.BlockAlign = LittleEndian.ToUInt16 (header, 0x20);
format.BitsPerSample = LittleEndian.ToUInt16 (header, 0x22);
format.FormatTag = header.ToUInt16 (0x14);
format.Channels = header.ToUInt16 (0x16);
format.SamplesPerSecond = header.ToUInt32 (0x18);
format.AverageBytesPerSecond = header.ToUInt32 (0x1C);
format.BlockAlign = header.ToUInt16 (0x20);
format.BitsPerSample = header.ToUInt16 (0x22);
format.ExtraSize = 0;
this.Format = format;
uint pcm_size = LittleEndian.ToUInt32 (header, 0x28);
uint pcm_size = header.ToUInt32 (0x28);
var pcm = new byte[pcm_size];
Decode (file, pcm);
Decode (file.AsStream, pcm);
Source = new MemoryStream (pcm);
this.PcmSize = pcm_size;
file.Dispose();

View File

@ -59,40 +59,37 @@ namespace GameRes.Formats.Ffa
throw new NotImplementedException ("Pt1Format.Write not implemented");
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
using (var input = new ArcView.Reader (stream))
{
int type = input.ReadInt32();
if (type < 0 || type > 3)
return null;
if (-1 != input.ReadInt32())
return null;
int x = input.ReadInt32();
int y = input.ReadInt32();
uint width = input.ReadUInt32();
uint height = input.ReadUInt32();
uint comp_size = input.ReadUInt32();
uint uncomp_size = input.ReadUInt32();
if (uncomp_size != width*height*3u)
return null;
return new Pt1MetaData {
Width = width,
Height = height,
OffsetX = x,
OffsetY = y,
BPP = 3 == type ? 32 : 24,
Type = type,
PackedSize = comp_size,
UnpackedSize = uncomp_size
};
}
int type = file.ReadInt32();
if (type < 0 || type > 3)
return null;
if (-1 != file.ReadInt32())
return null;
int x = file.ReadInt32();
int y = file.ReadInt32();
uint width = file.ReadUInt32();
uint height = file.ReadUInt32();
uint comp_size = file.ReadUInt32();
uint uncomp_size = file.ReadUInt32();
if (uncomp_size != width*height*3u)
return null;
return new Pt1MetaData {
Width = width,
Height = height,
OffsetX = x,
OffsetY = y,
BPP = 3 == type ? 32 : 24,
Type = type,
PackedSize = comp_size,
UnpackedSize = uncomp_size
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (Pt1MetaData)info;
var reader = new Reader (stream, meta);
var reader = new Reader (stream.AsStream, meta);
reader.Unpack();
return ImageData.Create (meta, reader.Format, null, reader.Data);
}

View File

@ -125,7 +125,7 @@ namespace GameRes.Formats.G2
using (input)
using (var reader = new GceReader (input, (int)pentry.UnpackedSize))
{
return new MemoryStream (reader.Data);
return new BinMemoryStream (reader.Data, entry.Name);
}
}
}

View File

@ -42,25 +42,23 @@ namespace GameRes.Formats.G2
Extensions = new string[] { "argb", "arg" };
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x10];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
uint bitmap = LittleEndian.ToUInt32 (header, 4);
stream.Position = 4;
uint bitmap = stream.ReadUInt32();
if (0x08080808 != bitmap)
return null;
return new ImageMetaData
{
Width = LittleEndian.ToUInt32 (header, 8),
Height = LittleEndian.ToUInt32 (header, 12),
Width = stream.ReadUInt32(),
Height = stream.ReadUInt32(),
BPP = 32,
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
stream.Seek (0x10, SeekOrigin.Current);
stream.Position = 0x10;
var pixels = new byte[info.Width*info.Height*4];
if (pixels.Length != stream.Read (pixels, 0, pixels.Length))
throw new EndOfStreamException();

Some files were not shown because too many files have changed in this diff Show More