overhauled HG3 reader.

This commit is contained in:
morkt 2015-11-29 12:27:49 +04:00
parent da51f3d7b2
commit bc0af8d910
2 changed files with 118 additions and 101 deletions

View File

@ -23,13 +23,10 @@
// IN THE SOFTWARE. // IN THE SOFTWARE.
// //
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.IO; using System.IO;
using System.Windows;
using GameRes.Utility; using GameRes.Utility;
using GameRes.Compression;
namespace GameRes.Formats.CatSystem namespace GameRes.Formats.CatSystem
{ {
@ -77,7 +74,7 @@ namespace GameRes.Formats.CatSystem
{ {
// emulate TGA image // emulate TGA image
var offset = entry.Offset+8; var offset = entry.Offset+8;
var info = new Hg3MetaData var info = new HgMetaData
{ {
HeaderSize = arc.File.View.ReadUInt32 (offset), HeaderSize = arc.File.View.ReadUInt32 (offset),
Width = arc.File.View.ReadUInt32 (offset+8), Width = arc.File.View.ReadUInt32 (offset+8),

View File

@ -34,7 +34,7 @@ using GameRes.Utility;
namespace GameRes.Formats.CatSystem namespace GameRes.Formats.CatSystem
{ {
internal class Hg3MetaData : ImageMetaData internal class HgMetaData : ImageMetaData
{ {
public uint CanvasWidth; public uint CanvasWidth;
public uint CanvasHeight; public uint CanvasHeight;
@ -57,7 +57,7 @@ namespace GameRes.Formats.CatSystem
return null; return null;
if (!Binary.AsciiEqual (header, 0x14, "stdinfo\0")) if (!Binary.AsciiEqual (header, 0x14, "stdinfo\0"))
return null; return null;
return new Hg3MetaData return new HgMetaData
{ {
HeaderSize = LittleEndian.ToUInt32 (header, 0x1C), HeaderSize = LittleEndian.ToUInt32 (header, 0x1C),
Width = LittleEndian.ToUInt32 (header, 0x24), Width = LittleEndian.ToUInt32 (header, 0x24),
@ -72,9 +72,7 @@ namespace GameRes.Formats.CatSystem
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (Stream stream, ImageMetaData info)
{ {
var meta = info as Hg3MetaData; var meta = (HgMetaData)info;
if (null == meta)
throw new ArgumentException ("Hg3Format.Read should be supplied with Hg3MetaData", "info");
if (0x20 != meta.BPP) if (0x20 != meta.BPP)
throw new NotSupportedException ("Not supported HG-3 color depth"); throw new NotSupportedException ("Not supported HG-3 color depth");
@ -82,11 +80,10 @@ namespace GameRes.Formats.CatSystem
using (var reader = new Hg3Reader (input, meta)) using (var reader = new Hg3Reader (input, meta))
{ {
var pixels = reader.Unpack(); var pixels = reader.Unpack();
int stride = (int)info.Width * info.BPP / 8;
if (reader.Flipped) if (reader.Flipped)
return ImageData.CreateFlipped (info, PixelFormats.Bgra32, null, pixels, stride); return ImageData.CreateFlipped (info, PixelFormats.Bgra32, null, pixels, reader.Stride);
else else
return ImageData.Create (info, PixelFormats.Bgra32, null, pixels, stride); return ImageData.Create (info, PixelFormats.Bgra32, null, pixels, reader.Stride);
} }
} }
@ -96,51 +93,34 @@ namespace GameRes.Formats.CatSystem
} }
} }
internal sealed class Hg3Reader : IDisposable internal class HgReader : IDisposable
{ {
BinaryReader m_input; private BinaryReader m_input;
Hg3MetaData m_info; protected HgMetaData m_info;
int m_pixel_size; protected int m_pixel_size;
public bool Flipped { get; private set; } protected BinaryReader Input { get { return m_input; } }
protected Stream InputStream { get { return m_input.BaseStream; } }
public int Stride { get; protected set; }
public Hg3Reader (Stream input, Hg3MetaData info) protected HgReader (Stream input, HgMetaData info)
{ {
m_input = new ArcView.Reader (input); m_input = new ArcView.Reader (input);
m_info = info; m_info = info;
m_pixel_size = m_info.BPP / 8; m_pixel_size = m_info.BPP / 8;
Stride = (int)m_info.Width * m_pixel_size;
} }
public byte[] Unpack () public byte[] UnpackStream (long data_offset, int data_packed, int data_unpacked, int ctl_packed, int ctl_unpacked)
{ {
m_input.BaseStream.Position = m_info.HeaderSize; var ctl_offset = data_offset + data_packed;
var img_type = m_input.ReadChars (8); var data = new byte[data_unpacked];
if (img_type.SequenceEqual ("img0000\0")) using (var z = new StreamRegion (InputStream, data_offset, data_packed, true))
return UnpackImg0000();
else if (img_type.SequenceEqual ("img_jpg\0"))
return UnpackJpeg();
else
throw new NotSupportedException ("Not supported HG-3 image");
}
byte[] UnpackImg0000 ()
{
Flipped = true;
m_input.BaseStream.Position = m_info.HeaderSize+0x18;
uint packed_data_size = m_input.ReadUInt32();
int data_size = m_input.ReadInt32();
uint packed_ctl_size = m_input.ReadUInt32();
int ctl_size = m_input.ReadInt32();
uint data_offset = m_info.HeaderSize + 0x28;
uint ctl_offset = data_offset + packed_data_size;
var data = new byte[data_size];
using (var z = new StreamRegion (m_input.BaseStream, data_offset, packed_data_size, true))
using (var data_in = new ZLibStream (z, CompressionMode.Decompress)) using (var data_in = new ZLibStream (z, CompressionMode.Decompress))
if (data_size != data_in.Read (data, 0, data.Length)) if (data.Length != data_in.Read (data, 0, data.Length))
throw new EndOfStreamException(); throw new EndOfStreamException();
using (var z = new StreamRegion (m_input.BaseStream, ctl_offset, packed_ctl_size, true)) using (var z = new StreamRegion (InputStream, ctl_offset, ctl_packed, true))
using (var ctl_in = new ZLibStream (z, CompressionMode.Decompress)) using (var ctl_in = new ZLibStream (z, CompressionMode.Decompress))
using (var bits = new LsbBitStream (ctl_in)) using (var bits = new LsbBitStream (ctl_in))
{ {
@ -164,54 +144,6 @@ namespace GameRes.Formats.CatSystem
} }
} }
byte[] UnpackJpeg ()
{
Flipped = false;
m_input.ReadInt32();
var jpeg_size = m_input.ReadInt32();
long next_section = m_input.BaseStream.Position + jpeg_size;
var decoder = new JpegBitmapDecoder (m_input.BaseStream,
BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
var frame = decoder.Frames[0];
if (frame.Format.BitsPerPixel < 24)
throw new NotSupportedException ("Not supported HG-3 JPEG color depth");
int src_pixel_size = frame.Format.BitsPerPixel/8;
int stride = (int)m_info.Width * src_pixel_size;
var pixels = new byte[stride*(int)m_info.Height];
frame.CopyPixels (pixels, stride, 0);
var output = new byte[m_info.Width*m_info.Height*4];
uint total = m_info.Width * m_info.Height;
int src = 0;
int dst = 0;
for (uint i = 0; i < total; ++i)
{
output[dst++] = pixels[src];
output[dst++] = pixels[src+1];
output[dst++] = pixels[src+2];
output[dst++] = 0xFF;
src += src_pixel_size;
}
m_input.BaseStream.Position = next_section;
var section_header = m_input.ReadChars (8);
if (!section_header.SequenceEqual ("img_al\0\0"))
return output;
m_input.BaseStream.Seek (8, SeekOrigin.Current);
int alpha_size = m_input.ReadInt32();
using (var alpha_in = new StreamRegion (m_input.BaseStream, m_input.BaseStream.Position+4, alpha_size, true))
using (var alpha = new ZLibStream (alpha_in, CompressionMode.Decompress))
{
for (int i = 3; i < output.Length; i += 4)
{
int b = alpha.ReadByte();
if (-1 == b)
throw new EndOfStreamException();
output[i] = (byte)b;
}
return output;
}
}
static int GetBitCount (LsbBitStream bits) static int GetBitCount (LsbBitStream bits)
{ {
int n = 0; int n = 0;
@ -219,7 +151,7 @@ namespace GameRes.Formats.CatSystem
{ {
++n; ++n;
if (n >= 0x20) if (n >= 0x20)
throw new InvalidFormatException ("Overflow at Hg3Reader.GetBitCount"); throw new InvalidFormatException ("Overflow at HgReader.GetBitCount");
} }
int value = 1; int value = 1;
while (n --> 0) while (n --> 0)
@ -270,22 +202,20 @@ namespace GameRes.Formats.CatSystem
output[dst++] = ConvertValue ((byte)(val >> 24)); output[dst++] = ConvertValue ((byte)(val >> 24));
} }
int stride = (int)m_info.Width * m_pixel_size; for (int x = m_pixel_size; x < Stride; x++)
for (int x = m_pixel_size; x < stride; x++)
{ {
output[x] += output[x - m_pixel_size]; output[x] += output[x - m_pixel_size];
} }
int line = stride; int line = Stride;
for (uint y = 1; y < m_info.Height; y++) for (uint y = 1; y < m_info.Height; y++)
{ {
int prev = line - stride; int prev = line - Stride;
for (int x = 0; x < stride; x++) for (int x = 0; x < Stride; x++)
{ {
output[line+x] += output[prev+x]; output[line+x] += output[prev+x];
} }
line += stride; line += Stride;
} }
return output; return output;
} }
@ -299,14 +229,104 @@ namespace GameRes.Formats.CatSystem
#region IDisposable Members #region IDisposable Members
bool _disposed = false; bool _disposed = false;
public void Dispose () public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{ {
if (!_disposed) if (!_disposed)
{ {
m_input.Dispose(); if (disposing)
{
m_input.Dispose();
}
_disposed = true; _disposed = true;
} }
} }
#endregion #endregion
} }
internal sealed class Hg3Reader : HgReader
{
public bool Flipped { get; private set; }
public Hg3Reader (Stream input, HgMetaData info) : base (input, info)
{
}
public byte[] Unpack ()
{
InputStream.Position = m_info.HeaderSize;
var img_type = Input.ReadChars (8);
if (img_type.SequenceEqual ("img0000\0"))
return UnpackImg0000();
else if (img_type.SequenceEqual ("img_jpg\0"))
return UnpackJpeg();
else
throw new NotSupportedException ("Not supported HG-3 image");
}
byte[] UnpackImg0000 ()
{
Flipped = true;
InputStream.Position = m_info.HeaderSize+0x18;
int packed_data_size = Input.ReadInt32();
int data_size = Input.ReadInt32();
int packed_ctl_size = Input.ReadInt32();
int ctl_size = Input.ReadInt32();
return UnpackStream (m_info.HeaderSize+0x28, packed_data_size, data_size, packed_ctl_size, ctl_size);
}
byte[] UnpackJpeg ()
{
Flipped = false;
Input.ReadInt32();
var jpeg_size = Input.ReadInt32();
long next_section = InputStream.Position + jpeg_size;
var decoder = new JpegBitmapDecoder (InputStream,
BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
var frame = decoder.Frames[0];
if (frame.Format.BitsPerPixel < 24)
throw new NotSupportedException ("Not supported HG-3 JPEG color depth");
int src_pixel_size = frame.Format.BitsPerPixel/8;
int stride = (int)m_info.Width * src_pixel_size;
var pixels = new byte[stride*(int)m_info.Height];
frame.CopyPixels (pixels, stride, 0);
var output = new byte[m_info.Width*m_info.Height*4];
uint total = m_info.Width * m_info.Height;
int src = 0;
int dst = 0;
for (uint i = 0; i < total; ++i)
{
output[dst++] = pixels[src];
output[dst++] = pixels[src+1];
output[dst++] = pixels[src+2];
output[dst++] = 0xFF;
src += src_pixel_size;
}
InputStream.Position = next_section;
var section_header = Input.ReadChars (8);
if (!section_header.SequenceEqual ("img_al\0\0"))
return output;
InputStream.Seek (8, SeekOrigin.Current);
int alpha_size = Input.ReadInt32();
using (var alpha_in = new StreamRegion (InputStream, InputStream.Position+4, alpha_size, true))
using (var alpha = new ZLibStream (alpha_in, CompressionMode.Decompress))
{
for (int i = 3; i < output.Length; i += 4)
{
int b = alpha.ReadByte();
if (-1 == b)
throw new EndOfStreamException();
output[i] = (byte)b;
}
return output;
}
}
}
} }