mirror of
https://github.com/crskycode/GARbro.git
synced 2026-07-08 01:30:18 +08:00
IBinaryStream migration.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user