(BipFormat.Read): refactored.

This commit is contained in:
morkt 2015-11-07 00:18:31 +04:00
parent 6e11938080
commit 2871af397a

View File

@ -86,6 +86,7 @@ namespace GameRes.Formats.PS2
if (0 == w || 0 == h) if (0 == w || 0 == h)
return null; return null;
var meta = new BipMetaData { Width = w, Height = h, BPP = 32 }; var meta = new BipMetaData { Width = w, Height = h, BPP = 32 };
meta.Tiles.Capacity = tile_count;
for (int i = 0; i < tile_count; ++i) for (int i = 0; i < tile_count; ++i)
{ {
input.ReadInt64(); input.ReadInt64();
@ -112,14 +113,13 @@ namespace GameRes.Formats.PS2
if (null == meta) if (null == meta)
throw new ArgumentException ("BipFormat.Read should be supplied with BipMetaData", "info"); throw new ArgumentException ("BipFormat.Read should be supplied with BipMetaData", "info");
using (var input = new BinaryReader (stream, Encoding.ASCII, true))
{
var header = new byte[0x7c]; var header = new byte[0x7c];
var bitmap = new WriteableBitmap ((int)meta.Width, (int)meta.Height, 96, 96, PixelFormats.Bgra32, null); var bitmap = new WriteableBitmap ((int)meta.Width, (int)meta.Height,
ImageData.DefaultDpiX, ImageData.DefaultDpiY, PixelFormats.Bgra32, null);
foreach (var tile in meta.Tiles) foreach (var tile in meta.Tiles)
{ {
input.BaseStream.Position = tile.Offset; stream.Position = tile.Offset;
if (header.Length != input.Read (header, 0, header.Length)) if (header.Length != stream.Read (header, 0, header.Length))
throw new InvalidFormatException ("Invalid tile header"); throw new InvalidFormatException ("Invalid tile header");
if (!Binary.AsciiEqual (header, "PNGFILE2")) if (!Binary.AsciiEqual (header, "PNGFILE2"))
throw new InvalidFormatException ("Unknown tile format"); throw new InvalidFormatException ("Unknown tile format");
@ -127,10 +127,7 @@ namespace GameRes.Formats.PS2
int alpha = LittleEndian.ToInt32 (header, 0x68); int alpha = LittleEndian.ToInt32 (header, 0x68);
int x = LittleEndian.ToInt32 (header, 0x6c); int x = LittleEndian.ToInt32 (header, 0x6c);
int y = LittleEndian.ToInt32 (header, 0x70); int y = LittleEndian.ToInt32 (header, 0x70);
var data = new byte[data_size]; using (var png = new StreamRegion (stream, stream.Position, data_size, true))
if (data_size != input.Read (data, 0, data_size))
throw new InvalidFormatException ("Unexpected end of file");
using (var png = new MemoryStream (data, false))
{ {
var decoder = new PngBitmapDecoder (png, var decoder = new PngBitmapDecoder (png,
BitmapCreateOptions.None, BitmapCacheOption.OnLoad); BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
@ -157,5 +154,4 @@ namespace GameRes.Formats.PS2
return new ImageData (bitmap, meta); return new ImageData (bitmap, meta);
} }
} }
}
} }