refactored HG-3 decoder.

This commit is contained in:
morkt
2015-10-19 09:08:23 +04:00
parent a6832a6a8c
commit 19d707828c

View File

@@ -1,8 +1,8 @@
//! \file ImageHG3.cs //! \file ImageHG3.cs
//! \date Sat Jul 19 17:31:09 2014 //! \date Sat Jul 19 17:31:09 2014
//! \brief Frontwing HG3 image format implementation. //! \brief CatSystem HG3 image format implementation.
// //
// Copyright (C) 2014 by morkt // Copyright (C) 2014-2015 by morkt
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to // of this software and associated documentation files (the "Software"), to
@@ -25,6 +25,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Media; using System.Windows.Media;
@@ -33,321 +34,283 @@ using GameRes.Utility;
namespace GameRes.Formats.CatSystem namespace GameRes.Formats.CatSystem
{ {
internal class Hg3MetaData : ImageMetaData
{
public uint CanvasWidth;
public uint CanvasHeight;
public uint HeaderSize;
}
[Export(typeof(ImageFormat))] [Export(typeof(ImageFormat))]
public class Hg3Format : ImageFormat public class Hg3Format : ImageFormat
{ {
public override string Tag { get { return "HG3"; } } public override string Tag { get { return "HG3"; } }
public override string Description { get { return "Frontwing proprietary image format"; } } public override string Description { get { return "CatSystem engine image format"; } }
public override uint Signature { get { return 0x332d4748; } } public override uint Signature { get { return 0x332d4748; } } // 'HG-3'
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (Stream stream)
{ {
var header = new byte[0x4c]; var header = new byte[0x4c];
if (0x4c != stream.Read (header, 0, header.Length)) if (0x4c != stream.Read (header, 0, header.Length))
return null; return null;
if (LittleEndian.ToUInt32 (header, 0) != Signature)
return null;
if (LittleEndian.ToUInt32 (header, 4) != 0x0c) if (LittleEndian.ToUInt32 (header, 4) != 0x0c)
return null; return null;
if (!Binary.AsciiEqual (header, 0x14, "stdinfo\0")) if (!Binary.AsciiEqual (header, 0x14, "stdinfo\0"))
return null; return null;
if (0x38 != LittleEndian.ToUInt32 (header, 0x1c)) return new Hg3MetaData
return null;
if (0x20 != LittleEndian.ToUInt32 (header, 0x2c))
return null;
uint width = LittleEndian.ToUInt32 (header, 0x24); // @@L0
uint height = LittleEndian.ToUInt32 (header, 0x28);
int pos_x = LittleEndian.ToInt32 (header, 0x30);
int pos_y = LittleEndian.ToInt32 (header, 0x34);
pos_x -= LittleEndian.ToInt32 (header, 0x44);
pos_y -= LittleEndian.ToInt32 (header, 0x48);
return new ImageMetaData
{ {
Width = width, HeaderSize = LittleEndian.ToUInt32 (header, 0x1C),
Height = height, Width = LittleEndian.ToUInt32 (header, 0x24),
OffsetX = pos_x, Height = LittleEndian.ToUInt32 (header, 0x28),
OffsetY = pos_y, OffsetX = LittleEndian.ToInt32 (header, 0x30),
BPP = 32, OffsetY = LittleEndian.ToInt32 (header, 0x34),
BPP = LittleEndian.ToInt32 (header, 0x2C),
CanvasWidth = LittleEndian.ToUInt32 (header, 0x44),
CanvasHeight = LittleEndian.ToUInt32 (header, 0x48),
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (Stream stream, ImageMetaData info)
{ {
stream.Position = 0x40; var meta = info as Hg3MetaData;
bool flipped = 0 == (stream.ReadByte() & 1) || info.OffsetY < 0; if (null == meta)
stream.Position = 0x4c; throw new ArgumentException ("Hg3Format.Read should be supplied with Hg3MetaData", "info");
var header = new byte[0x28]; if (0x20 != meta.BPP)
if (0x28 != stream.Read (header, 0, header.Length)) throw new NotSupportedException ("Not supported HG-3 color depth");
return null;
if (!Binary.AsciiEqual (header, "img0000\0"))
return null;
uint data_size = LittleEndian.ToUInt32 (header, 0x0c);
if (data_size < 0x18)
return null;
if (info.Height != LittleEndian.ToUInt32 (header, 0x14))
return null;
uint packed2_size = LittleEndian.ToUInt32 (header, 0x18);
uint unpacked2_size = LittleEndian.ToUInt32 (header, 0x1c);
uint packed1_size = LittleEndian.ToUInt32 (header, 0x20);
uint unpacked1_size = LittleEndian.ToUInt32 (header, 0x24);
if (packed2_size + packed1_size < packed2_size)
return null;
if (unpacked2_size + unpacked1_size < unpacked2_size)
return null;
long data_pos = stream.Position; using (var input = new StreamRegion (stream, 0x14, true))
using (var unpacked2 = ZLibCompressor.DeCompress (stream)) using (var reader = new Hg3Reader (input, meta))
{ {
stream.Position = data_pos + packed2_size; var pixels = reader.Unpack();
using (var unpacked1 = ZLibCompressor.DeCompress (stream)) int stride = (int)info.Width * info.BPP / 8;
{ if (reader.Flipped)
var decoder = new Decoder (unpacked1.GetBuffer(), unpacked1_size,
unpacked2.GetBuffer(), unpacked2_size,
info.Width, info.Height);
decoder.Unpack();
var pixels = decoder.Data;
int stride = (int)info.Width * 4;
if (flipped)
return ImageData.CreateFlipped (info, PixelFormats.Bgra32, null, pixels, stride); return ImageData.CreateFlipped (info, PixelFormats.Bgra32, null, pixels, stride);
else else
return ImageData.Create (info, PixelFormats.Bgra32, null, pixels, stride); return ImageData.Create (info, PixelFormats.Bgra32, null, pixels, stride);
} }
} }
}
public override void Write (Stream file, ImageData image) public override void Write (Stream file, ImageData image)
{ {
throw new NotImplementedException ("Hg3Format.Write not implemented"); throw new NotImplementedException ("Hg3Format.Write not implemented");
} }
class Decoder
{
byte[] m_in1;
byte[] m_in2;
byte[] m_image;
uint m_in1_size;
uint m_in2_size;
uint m_dst_size;
uint m_width;
uint m_height;
public byte[] Data { get { return m_image; } }
public Decoder (byte[] in1, uint in1_size, byte[] in2, uint in2_size,
uint width, uint height)
{
m_in1 = in1;
m_in1_size = in1_size;
m_in2 = in2;
m_in2_size = in2_size;
m_width = width;
m_height = height;
m_dst_size = width*height*4;
m_image = new byte[(int)m_dst_size];
} }
uint esi; internal sealed class Hg3Reader : IDisposable
uint edi; {
uint eax; BinaryReader m_input;
uint ebx; Hg3MetaData m_info;
uint ecx; int m_pixel_size;
uint edx;
uint L0, L1, L2, L3, L4; public bool Flipped { get; private set; }
uint m_plane;
public void Unpack () public Hg3Reader (Stream input, Hg3MetaData info)
{ {
m_plane = 0; m_input = new ArcView.Reader (input);
edi = 0; m_info = info;
esi = 0; m_pixel_size = m_info.BPP / 8;
ebx = 0;
eax = m_in1_size;
L0 = m_in2_size;
L1 = 0;
bool skip_first = GetNextBit();
Proc4();
ecx = m_dst_size;
if (eax > m_dst_size)
throw new InvalidFormatException ("Underflow at Hg3Format.Decoder.Unpack()");
m_dst_size -= eax;
ecx >>= 2;
L2 = eax;
L3 = ecx;
L4 = ecx;
for (;;)
{
if (!skip_first)
{
// @@1:
if (0 == L2)
break;
Proc4();
ecx = eax;
if (ecx > L2)
throw new InvalidFormatException ("Overflow at Hg3Format.Decoder.Unpack()");
L2 -= ecx;
eax = 0;
do
Proc2();
while (0 != --ecx);
}
// @@1a:
if (0 == L2)
break;
Proc4();
ecx = eax;
if (ecx > L2 || ecx > L0)
throw new InvalidFormatException ("Overflow (2) at Hg3Format.Decoder.Unpack()");
L2 -= ecx;
L0 -= ecx;
do
{
eax = m_in2[L1++];
Proc2();
}
while (0 != --ecx);
skip_first = false;
}
// @@7:
ecx = m_dst_size;
esi = 0;
if (0 != ecx)
{
eax = 0;
do
Proc2();
while (0 != --ecx);
}
Proc6 (m_width, m_height);
// @@9:
eax = 0;
edx = m_in1_size;
} }
void Proc2 () // @@2 public byte[] Unpack ()
{ {
m_image[edi] = (byte)eax; m_input.BaseStream.Position = m_info.HeaderSize;
edi += 4; var img_type = m_input.ReadChars (8);
if (0 == --L3) 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 ()
{ {
edi = ++m_plane; Flipped = true;
L3 = L4; 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))
if (data_size != data_in.Read (data, 0, data.Length))
throw new EndOfStreamException();
using (var z = new StreamRegion (m_input.BaseStream, ctl_offset, packed_ctl_size, true))
using (var ctl_in = new ZLibStream (z, CompressionMode.Decompress))
using (var bits = new LsbBitStream (ctl_in))
{
bool copy = bits.GetNextBit() != 0;
int output_size = GetBitCount (bits);
var output = new byte[output_size];
int src = 0;
int dst = 0;
while (dst < output_size)
{
int count = GetBitCount (bits);
if (copy)
{
Buffer.BlockCopy (data, src, output, dst, count);
src += count;
}
dst += count;
copy = !copy;
}
return ApplyDelta (output);
} }
} }
bool GetNextBit () // @@3 byte[] UnpackJpeg ()
{ {
bool carry = 0 != (ebx & 1); Flipped = false;
ebx >>= 1; m_input.ReadInt32();
if (0 == ebx) 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)
{ {
if (0 == m_in1_size--) output[dst++] = pixels[src];
throw new InvalidFormatException ("Hg3Format.Decoder.Underflow at GetNextBit()"); output[dst++] = pixels[src+1];
ebx = (uint)(m_in1[esi++] | 0x100); output[dst++] = pixels[src+2];
carry = 0 != (ebx & 1); output[dst++] = 0xFF;
ebx >>= 1; src += src_pixel_size;
}
return carry;
} }
void Proc4 () // @@4 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))
{ {
ecx = 0; for (int i = 3; i < output.Length; i += 4)
eax = 0;
do
{ {
if (ecx >= 0x20) int b = alpha.ReadByte();
throw new InvalidFormatException ("Hg3Format.Decoder.Overflow at Proc4"); if (-1 == b)
++ecx; throw new EndOfStreamException();
output[i] = (byte)b;
} }
while (!GetNextBit()); return output;
++eax;
while (0 != --ecx)
{
eax += eax + (uint)(GetNextBit() ? 1 : 0);
} }
} }
void Proc6 (uint width, uint height) static int GetBitCount (LsbBitStream bits)
{ {
uint[] table = new uint[0x100]; int n = 0;
ecx = 0; while (0 == bits.GetNextBit())
{
++n;
if (n >= 0x20)
throw new InvalidFormatException ("Overflow at Hg3Reader.GetBitCount");
}
int value = 1;
while (n --> 0)
{
value = (value << 1) | bits.GetNextBit();
}
return value;
}
byte[] ApplyDelta (byte[] pixels)
{
uint[] table1 = new uint[0x100];
uint[] table2 = new uint[0x100];
uint[] table3 = new uint[0x100];
uint[] table4 = new uint[0x100];
for (uint i = 0; i < 0x100; ++i) for (uint i = 0; i < 0x100; ++i)
{ {
eax = 0xffffffff; uint val = i & 0xC0;
edx = i;
do val <<= 6;
val |= i & 0x30;
val <<= 6;
val |= i & 0x0C;
val <<= 6;
val |= i & 0x03;
table4[i] = val;
table3[i] = val << 2;
table2[i] = val << 4;
table1[i] = val << 6;
}
int plane_size = pixels.Length / 4;
int plane1 = 0;
int plane2 = plane1 + plane_size;
int plane3 = plane2 + plane_size;
int plane4 = plane3 + plane_size;
byte[] output = new byte[pixels.Length];
int dst = 0;
while (dst < output.Length)
{ {
eax >>= 2; uint val = table1[pixels[plane1++]] | table2[pixels[plane2++]]
eax |= (edx & 3) << 30; | table3[pixels[plane3++]] | table4[pixels[plane4++]];
eax >>= 6;
edx >>= 2; output[dst++] = ConvertValue ((byte)val);
output[dst++] = ConvertValue ((byte)(val >> 8));
output[dst++] = ConvertValue ((byte)(val >> 16));
output[dst++] = ConvertValue ((byte)(val >> 24));
} }
while (0 != (0x80 & eax));
table[i] = eax; int stride = (int)m_info.Width * m_pixel_size;
}
ecx = width * height * 4; for (int x = m_pixel_size; x < stride; x++)
for (uint i = 0; i < ecx; i += 4)
{ {
eax = m_image[i]; output[x] += output[x - m_pixel_size];
edx = table[eax];
edx <<= 2;
eax = m_image[i+1];
edx += table[eax];
edx <<= 2;
eax = m_image[i+2];
edx += table[eax];
edx <<= 2;
eax = m_image[i+3];
edx += table[eax];
m_image[i] = (byte)(edx);
m_image[i+1] = (byte)(edx >> 8);
m_image[i+2] = (byte)(edx >> 16);
m_image[i+3] = (byte)(edx >> 24);
} }
edi = 0;
for (int i = 0; i < 4; ++i) int line = stride;
for (uint y = 1; y < m_info.Height; y++)
{ {
eax = m_image[edi]; int prev = line - stride;
edx = (uint)(0 == (eax & 1) ? 0 : 0xff); for (int x = 0; x < stride; x++)
eax >>= 1;
eax ^= edx;
m_image[edi++] = (byte)eax;
}
ecx = width;
if (0 != --ecx)
{ {
ecx <<= 2; output[line+x] += output[prev+x];
do }
line += stride;
}
return output;
}
static byte ConvertValue (byte val)
{ {
eax = m_image[edi]; bool carry = 0 != (val & 1);
edx = (uint)(0 == (eax & 1) ? 0 : 0xff); val >>= 1;
eax >>= 1; return (byte)(carry ? val ^ 0xFF : val);
eax ^= edx;
eax += m_image[edi-4];
m_image[edi++] = (byte)eax;
} }
while (0 != --ecx);
} #region IDisposable Members
ecx = height; bool _disposed = false;
if (0 != --ecx) public void Dispose ()
{ {
uint stride = width*4; if (!_disposed)
ecx *= stride;
do
{ {
eax = m_image[edi]; m_input.Dispose();
edx = (uint)(0 == (eax & 1) ? 0 : 0xff); _disposed = true;
eax >>= 1;
eax ^= edx;
eax += m_image[edi-stride];
m_image[edi++] = (byte)eax;
}
while (0 != --ecx);
}
} }
} }
#endregion
} }
} }