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

@@ -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
}