IBinaryStream migration - continued.

This commit is contained in:
morkt
2016-10-15 12:21:12 +04:00
parent 503b734645
commit d0c1d5da01
39 changed files with 529 additions and 574 deletions

View File

@@ -43,7 +43,7 @@ namespace GameRes.Formats.Ivory
Signatures = new uint[] { 0x20585066, 0x4B525463 };
}
public override SoundInput TryOpen (Stream file)
public override SoundInput TryOpen (IBinaryStream file)
{
var header = new byte[0x24];
if (8 != file.Read (header, 0, 8))
@@ -77,12 +77,12 @@ namespace GameRes.Formats.Ivory
};
format.BlockAlign = (ushort)(format.BitsPerSample * format.Channels / 8);
format.AverageBytesPerSecond = format.SamplesPerSecond * format.BlockAlign;
var input = new StreamRegion (file, start_offset, data_length);
var input = new StreamRegion (file.AsStream, start_offset, data_length);
return new RawPcmInput (input, format);
}
else if (2 == type)
{
using (var decoder = new TrkDecoder (file, header, start_offset, data_length))
using (var decoder = new TrkDecoder (file.AsStream, header, start_offset, data_length))
{
var pcm = decoder.Decode();
return new RawPcmInput (new MemoryStream (pcm), decoder.Format);

View File

@@ -47,78 +47,73 @@ namespace GameRes.Formats.Ivory
public override string Description { get { return "Ivory image format"; } }
public override uint Signature { get { return 0x1A444D4D; } } // 'MMD'
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;
var header = stream.ReadHeader (0x18);
var info = new MmdMetaData
{
Width = LittleEndian.ToUInt16 (header, 4),
Height = LittleEndian.ToUInt16 (header, 6),
Width = header.ToUInt16 (4),
Height = header.ToUInt16 (6),
BPP = 8,
Size1 = LittleEndian.ToInt32 (header, 8),
Size2 = LittleEndian.ToInt32 (header, 0x0C),
Size3 = LittleEndian.ToInt32 (header, 0x10),
Colors = LittleEndian.ToInt32 (header, 0x14),
Size1 = header.ToInt32 (8),
Size2 = header.ToInt32 (0x0C),
Size3 = header.ToInt32 (0x10),
Colors = header.ToInt32 (0x14),
};
if (info.Size1 <= 0 || info.Size2 <= info.Size1 || info.Size3 <= 0)
return null;
return info;
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream input, ImageMetaData info)
{
var meta = (MmdMetaData)info;
var pixels = new byte[info.Width * info.Height];
stream.Position = 0x18;
using (var input = new ArcView.Reader (stream))
input.Position = 0x18;
var buf1 = input.ReadBytes (meta.Size1);
var buf2 = input.ReadBytes (meta.Size2 - meta.Size1);
int w = (int)info.Width / 4;
var line = new byte[w];
int mask = 0x80;
int b1 = 0;
int b2 = 0;
int dst = 0;
for (int y = (int)info.Height; y > 0; --y)
{
var buf1 = input.ReadBytes (meta.Size1);
var buf2 = input.ReadBytes (meta.Size2 - meta.Size1);
int w = (int)info.Width / 4;
var line = new byte[w];
int mask = 0x80;
int b1 = 0;
int b2 = 0;
int dst = 0;
for (int y = (int)info.Height; y > 0; --y)
for (int x = 0; x < w; ++x)
{
for (int x = 0; x < w; ++x)
if (0 != (mask & buf1[b1]))
{
if (0 != (mask & buf1[b1]))
line[x] ^= buf2[b2++];
}
mask >>= 1;
if (0 == mask)
{
mask = 0x80;
++b1;
}
byte p = line[x];
int q = p >> 4;
for (int j = 0; j < 2; ++j)
{
if (0 != q)
{
line[x] ^= buf2[b2++];
int offset = ShiftTable[q + 16] + (int)info.Width * ShiftTable[q];
int src = dst - offset;
pixels[dst++] = pixels[src];
pixels[dst++] = pixels[src+1];
}
mask >>= 1;
if (0 == mask)
else
{
mask = 0x80;
++b1;
}
byte p = line[x];
int q = p >> 4;
for (int j = 0; j < 2; ++j)
{
if (0 != q)
{
int offset = ShiftTable[q + 16] + (int)info.Width * ShiftTable[q];
int src = dst - offset;
pixels[dst++] = pixels[src];
pixels[dst++] = pixels[src+1];
}
else
{
input.Read (pixels, dst, 2);
dst += 2;
}
q = p & 0xF;
input.Read (pixels, dst, 2);
dst += 2;
}
q = p & 0xF;
}
}
}
stream.Position = 0x18 + meta.Size2 + meta.Size3;
var palette = ReadPalette (stream, meta.Colors);
input.Position = 0x18 + meta.Size2 + meta.Size3;
var palette = ReadPalette (input.AsStream, meta.Colors);
return ImageData.Create (info, PixelFormats.Indexed8, palette, pixels);
}

View File

@@ -37,19 +37,19 @@ namespace GameRes.Formats.Ivory
public override string Description { get { return "Ivory image format"; } }
public override uint Signature { get { return 0; } }
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var wh = FormatCatalog.ReadSignature (stream);
var wh = stream.Signature;
uint width = wh & 0xFFFF;
uint height = wh >> 16;
if (0 == width || width > 800 || 0 == height || height > 600)
return null;
if (!IsValidInput (stream, width, height))
if (!IsValidInput (stream.AsStream, width, height))
return null;
return new ImageMetaData { Width = width, Height = height, BPP = 24 };
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
stream.Position = 4;
var pixels = new byte[3 * info.Width * info.Height];

View File

@@ -53,11 +53,11 @@ namespace GameRes.Formats.Ivory
public override string Description { get { return "Ivory image format"; } }
public override uint Signature { get { return 0x20475366; } } // 'fSG '
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
stream.Position = 8;
var header = new byte[0x24];
if (header.Length != stream.Read (header, 0, header.Length))
var header = stream.ReadBytes (0x24);
if (header.Length != 0x24)
return null;
int header_size = LittleEndian.ToInt32 (header, 8);
if (Binary.AsciiEqual (header, "cRGB"))
@@ -90,7 +90,7 @@ namespace GameRes.Formats.Ivory
return null;
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (SgMetaData)info;
if (SgType.cRGB == meta.Type)
@@ -107,11 +107,11 @@ namespace GameRes.Formats.Ivory
}
}
ImageData ReadJpeg (Stream stream, SgMetaData info)
ImageData ReadJpeg (IBinaryStream stream, SgMetaData info)
{
stream.Position = info.DataOffset;
var input = new byte[info.DataSize];
if (input.Length != stream.Read (input, 0, input.Length))
var input = stream.ReadBytes (info.DataSize);
if (input.Length != info.DataSize)
throw new EndOfStreamException();
PakOpener.Decrypt (input, info.JpegKey);
using (var img = new MemoryStream (input))
@@ -131,7 +131,7 @@ namespace GameRes.Formats.Ivory
internal sealed class SgRgbReader : IDisposable
{
BinaryReader m_input;
IBinaryStream m_input;
SgMetaData m_info;
int m_width;
int m_height;
@@ -148,7 +148,7 @@ namespace GameRes.Formats.Ivory
{
if (info.Type != SgType.cRGB || !(0x18 == info.BPP || 0x20 == info.BPP))
throw new InvalidFormatException();
m_input = new ArcView.Reader (input);
m_input = input;
m_info = info;
m_width = (int)info.Width;
m_height = (int)info.Height;
@@ -158,7 +158,7 @@ namespace GameRes.Formats.Ivory
public void Unpack ()
{
m_input.BaseStream.Position = m_info.DataOffset;
m_input.Position = m_info.DataOffset;
switch (m_info.RgbMode)
{
case 0: UnpackV0(); break;
@@ -243,11 +243,11 @@ namespace GameRes.Formats.Ivory
var index = new int[m_height];
for (int i = 0; i < m_height; ++i)
index[i] = m_input.ReadInt32();
var data_pos = m_input.BaseStream.Position;
var data_pos = m_input.Position;
int dst = 0;
for (int y = 0; y < m_height; ++y)
{
m_input.BaseStream.Position = data_pos + index[y];
m_input.Position = data_pos + index[y];
for (int x = 0; x < m_width; )
{
int ctl = m_input.ReadByte();
@@ -282,9 +282,9 @@ namespace GameRes.Formats.Ivory
var index = new int[m_height];
for (int i = 0; i < m_height; ++i)
index[i] = m_input.ReadInt32();
var data_pos = m_input.BaseStream.Position;
var data_pos = m_input.Position;
int dst = 0;
using (var bits = new LsbBitStream (m_input.BaseStream, true))
using (var bits = new LsbBitStream (m_input.AsStream, true))
{
for (int y = 0; y < m_height; ++y)
{
@@ -358,14 +358,8 @@ namespace GameRes.Formats.Ivory
}
#region IDisposable Members
bool _disposed = false;
public void Dispose ()
{
if (!_disposed)
{
m_input.Dispose();
_disposed = true;
}
}
#endregion
}