Skip to content

Commit

Permalink
IBinaryStream migration.
Browse files Browse the repository at this point in the history
  • Loading branch information
morkt committed Oct 16, 2016
1 parent d0c1d5d commit bb18303
Show file tree
Hide file tree
Showing 251 changed files with 3,273 additions and 3,626 deletions.
2 changes: 1 addition & 1 deletion ArcFormats/AZSys/ArcEncrypted.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public override Stream OpenEntry (ArcFile arc, Entry entry)
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)
Expand Down
71 changes: 34 additions & 37 deletions ArcFormats/AZSys/ImageCPB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,51 +54,48 @@ public override void Write (Stream file, ImageData image)
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);
}
Expand Down
52 changes: 22 additions & 30 deletions ArcFormats/AZSys/ImageTYP1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,46 +45,38 @@ public class Typ1Format : ImageFormat
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);
}
Expand Down
2 changes: 1 addition & 1 deletion ArcFormats/Abel/ArcARC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Stream OpenAcdEntry (ArcFile arc, Entry entry)
{
data[i] = (byte)(0xFF - data[i]);
}
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}

Stream OpenCmpEntry (ArcFile arc, Entry entry)
Expand Down
28 changes: 14 additions & 14 deletions ArcFormats/Abel/ImageGPS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public GpsFormat ()
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
Expand All @@ -66,7 +66,8 @@ public override ImageMetaData ReadMetaData (IBinaryStream stream)
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)
Expand All @@ -76,11 +77,12 @@ public override ImageMetaData ReadMetaData (IBinaryStream stream)
}
}

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);
}

Expand All @@ -89,30 +91,28 @@ public override void Write (Stream file, ImageData image)
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)
Expand Down
4 changes: 2 additions & 2 deletions ArcFormats/Actgs/ArcDAT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public override Stream OpenEntry (ArcFile arc, Entry entry)
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 "))
{
Expand All @@ -136,7 +136,7 @@ public override Stream OpenEntry (ArcFile arc, Entry entry)
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);
}
Expand Down
67 changes: 24 additions & 43 deletions ArcFormats/ActiveSoft/ImageEDT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,15 @@ public override void Write (Stream file, ImageData image)
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;

Expand All @@ -131,21 +129,18 @@ public override ImageMetaData ReadMetaData (Stream stream)
};
}

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;
Expand Down Expand Up @@ -244,24 +239,15 @@ public void Unpack ()

#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
}
Expand All @@ -284,17 +270,15 @@ public override void Write (Stream file, ImageData image)
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))
return null;
if (!Binary.AsciiEqual (header, ".8Bit\x8d\x5d\x8c\xcb\x00"))
var header = stream.ReadHeader (0x1A);
if (!header.AsciiEqual (".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;

Expand All @@ -308,14 +292,11 @@ public override ImageMetaData ReadMetaData (Stream stream)
};
}

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);
Expand Down
Loading

0 comments on commit bb18303

Please sign in to comment.