2015-08-19 21:40:32 +04:00
|
|
|
//! \file ImageHG3.cs
|
|
|
|
//! \date Sat Jul 19 17:31:09 2014
|
2015-10-19 09:08:23 +04:00
|
|
|
//! \brief CatSystem HG3 image format implementation.
|
2015-08-19 21:40:32 +04:00
|
|
|
//
|
2015-10-19 09:08:23 +04:00
|
|
|
// Copyright (C) 2014-2015 by morkt
|
2015-08-19 21:40:32 +04:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to
|
|
|
|
// deal in the Software without restriction, including without limitation the
|
|
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
// IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.ComponentModel.Composition;
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
using System.Windows.Media;
|
|
|
|
using GameRes.Compression;
|
|
|
|
using GameRes.Utility;
|
|
|
|
|
|
|
|
namespace GameRes.Formats.CatSystem
|
|
|
|
{
|
2015-11-29 12:27:49 +04:00
|
|
|
internal class HgMetaData : ImageMetaData
|
2015-10-19 09:08:23 +04:00
|
|
|
{
|
|
|
|
public uint CanvasWidth;
|
|
|
|
public uint CanvasHeight;
|
|
|
|
public uint HeaderSize;
|
|
|
|
}
|
|
|
|
|
2015-08-19 21:40:32 +04:00
|
|
|
[Export(typeof(ImageFormat))]
|
|
|
|
public class Hg3Format : ImageFormat
|
|
|
|
{
|
2015-10-19 09:08:23 +04:00
|
|
|
public override string Tag { get { return "HG3"; } }
|
|
|
|
public override string Description { get { return "CatSystem engine image format"; } }
|
|
|
|
public override uint Signature { get { return 0x332d4748; } } // 'HG-3'
|
2015-08-19 21:40:32 +04:00
|
|
|
|
2016-10-16 09:22:53 +04:00
|
|
|
public override ImageMetaData ReadMetaData (IBinaryStream stream)
|
2015-08-19 21:40:32 +04:00
|
|
|
{
|
2016-10-16 09:22:53 +04:00
|
|
|
var header = stream.ReadHeader (0x4c);
|
|
|
|
if (header.ToUInt32 (4) != 0x0c)
|
2015-08-19 21:40:32 +04:00
|
|
|
return null;
|
2016-10-16 09:22:53 +04:00
|
|
|
if (!header.AsciiEqual (0x14, "stdinfo\0"))
|
2015-08-19 21:40:32 +04:00
|
|
|
return null;
|
2015-11-29 12:27:49 +04:00
|
|
|
return new HgMetaData
|
2015-08-19 21:40:32 +04:00
|
|
|
{
|
2016-10-16 09:22:53 +04:00
|
|
|
HeaderSize = header.ToUInt32 (0x1C),
|
|
|
|
Width = header.ToUInt32 (0x24),
|
|
|
|
Height = header.ToUInt32 (0x28),
|
|
|
|
OffsetX = header.ToInt32 (0x30),
|
|
|
|
OffsetY = header.ToInt32 (0x34),
|
|
|
|
BPP = header.ToInt32 (0x2C),
|
|
|
|
CanvasWidth = header.ToUInt32 (0x44),
|
|
|
|
CanvasHeight = header.ToUInt32 (0x48),
|
2015-08-19 21:40:32 +04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-16 09:22:53 +04:00
|
|
|
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
|
2015-08-19 21:40:32 +04:00
|
|
|
{
|
2015-11-29 12:27:49 +04:00
|
|
|
var meta = (HgMetaData)info;
|
2018-09-13 20:45:20 +04:00
|
|
|
if (32 != meta.BPP && 24 != meta.BPP)
|
2015-10-19 09:08:23 +04:00
|
|
|
throw new NotSupportedException ("Not supported HG-3 color depth");
|
2015-08-19 21:40:32 +04:00
|
|
|
|
2016-10-16 09:22:53 +04:00
|
|
|
using (var reg = new StreamRegion (stream.AsStream, 0x14, true))
|
|
|
|
using (var input = new BinaryStream (reg, stream.Name))
|
2015-10-19 09:08:23 +04:00
|
|
|
using (var reader = new Hg3Reader (input, meta))
|
2015-08-19 21:40:32 +04:00
|
|
|
{
|
2016-10-26 05:10:30 +04:00
|
|
|
return reader.Image;
|
2015-08-19 21:40:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Write (Stream file, ImageData image)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException ("Hg3Format.Write not implemented");
|
|
|
|
}
|
2015-10-19 09:08:23 +04:00
|
|
|
}
|
2015-08-19 21:40:32 +04:00
|
|
|
|
2016-10-26 05:10:30 +04:00
|
|
|
internal class HgReader : IImageDecoder
|
2015-10-19 09:08:23 +04:00
|
|
|
{
|
2016-10-26 05:10:30 +04:00
|
|
|
protected IBinaryStream m_input;
|
2015-11-29 12:27:49 +04:00
|
|
|
protected HgMetaData m_info;
|
|
|
|
protected int m_pixel_size;
|
2016-10-26 05:10:30 +04:00
|
|
|
protected ImageData m_image;
|
2015-08-19 21:40:32 +04:00
|
|
|
|
2016-10-26 15:42:27 +04:00
|
|
|
public Stream Source { get { return m_input.AsStream; } }
|
|
|
|
public ImageFormat SourceFormat { get { return null; } }
|
|
|
|
public ImageMetaData Info { get { return m_info; } }
|
|
|
|
public virtual ImageData Image { get { throw new NotImplementedException(); } }
|
|
|
|
public int Stride { get; protected set; }
|
2015-08-19 21:40:32 +04:00
|
|
|
|
2016-10-16 09:22:53 +04:00
|
|
|
protected HgReader (IBinaryStream input, HgMetaData info)
|
2015-10-19 09:08:23 +04:00
|
|
|
{
|
2016-10-16 09:22:53 +04:00
|
|
|
m_input = input;
|
2015-10-19 09:08:23 +04:00
|
|
|
m_info = info;
|
|
|
|
m_pixel_size = m_info.BPP / 8;
|
2015-11-29 12:27:49 +04:00
|
|
|
Stride = (int)m_info.Width * m_pixel_size;
|
2015-10-19 09:08:23 +04:00
|
|
|
}
|
|
|
|
|
2015-11-29 12:27:49 +04:00
|
|
|
public byte[] UnpackStream (long data_offset, int data_packed, int data_unpacked, int ctl_packed, int ctl_unpacked)
|
2015-10-19 09:08:23 +04:00
|
|
|
{
|
2015-11-29 12:27:49 +04:00
|
|
|
var ctl_offset = data_offset + data_packed;
|
|
|
|
var data = new byte[data_unpacked];
|
2016-10-26 15:42:27 +04:00
|
|
|
using (var z = new StreamRegion (Source, data_offset, data_packed, true))
|
2015-10-19 09:08:23 +04:00
|
|
|
using (var data_in = new ZLibStream (z, CompressionMode.Decompress))
|
2015-11-29 12:27:49 +04:00
|
|
|
if (data.Length != data_in.Read (data, 0, data.Length))
|
2015-10-19 09:08:23 +04:00
|
|
|
throw new EndOfStreamException();
|
2015-08-19 21:40:32 +04:00
|
|
|
|
2016-10-26 15:42:27 +04:00
|
|
|
using (var z = new StreamRegion (Source, ctl_offset, ctl_packed, true))
|
2015-10-19 09:08:23 +04:00
|
|
|
using (var ctl_in = new ZLibStream (z, CompressionMode.Decompress))
|
|
|
|
using (var bits = new LsbBitStream (ctl_in))
|
2015-08-19 21:40:32 +04:00
|
|
|
{
|
2015-10-19 09:08:23 +04:00
|
|
|
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)
|
2015-08-19 21:40:32 +04:00
|
|
|
{
|
2015-10-19 09:08:23 +04:00
|
|
|
int count = GetBitCount (bits);
|
|
|
|
if (copy)
|
2015-08-19 21:40:32 +04:00
|
|
|
{
|
2015-10-19 09:08:23 +04:00
|
|
|
Buffer.BlockCopy (data, src, output, dst, count);
|
|
|
|
src += count;
|
2015-08-19 21:40:32 +04:00
|
|
|
}
|
2015-10-19 09:08:23 +04:00
|
|
|
dst += count;
|
|
|
|
copy = !copy;
|
2015-08-19 21:40:32 +04:00
|
|
|
}
|
2015-10-19 09:08:23 +04:00
|
|
|
return ApplyDelta (output);
|
2015-08-19 21:40:32 +04:00
|
|
|
}
|
2015-10-19 09:08:23 +04:00
|
|
|
}
|
2015-08-19 21:40:32 +04:00
|
|
|
|
2015-10-19 09:08:23 +04:00
|
|
|
static int GetBitCount (LsbBitStream bits)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
while (0 == bits.GetNextBit())
|
2015-08-19 21:40:32 +04:00
|
|
|
{
|
2015-10-19 09:08:23 +04:00
|
|
|
++n;
|
|
|
|
if (n >= 0x20)
|
2015-11-29 12:27:49 +04:00
|
|
|
throw new InvalidFormatException ("Overflow at HgReader.GetBitCount");
|
2015-10-19 09:08:23 +04:00
|
|
|
}
|
|
|
|
int value = 1;
|
|
|
|
while (n --> 0)
|
|
|
|
{
|
|
|
|
value = (value << 1) | bits.GetNextBit();
|
2015-08-19 21:40:32 +04:00
|
|
|
}
|
2015-10-19 09:08:23 +04:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] ApplyDelta (byte[] pixels)
|
|
|
|
{
|
2015-11-21 10:25:42 +04:00
|
|
|
var table = new uint[4, 0x100];
|
2015-10-19 09:08:23 +04:00
|
|
|
for (uint i = 0; i < 0x100; ++i)
|
2015-08-19 21:40:32 +04:00
|
|
|
{
|
2015-10-19 09:08:23 +04:00
|
|
|
uint val = i & 0xC0;
|
|
|
|
|
|
|
|
val <<= 6;
|
|
|
|
val |= i & 0x30;
|
|
|
|
|
|
|
|
val <<= 6;
|
|
|
|
val |= i & 0x0C;
|
|
|
|
|
|
|
|
val <<= 6;
|
|
|
|
val |= i & 0x03;
|
|
|
|
|
2015-11-21 10:25:42 +04:00
|
|
|
table[0,i] = val << 6;
|
|
|
|
table[1,i] = val << 4;
|
|
|
|
table[2,i] = val << 2;
|
|
|
|
table[3,i] = val;
|
2015-10-19 09:08:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int plane_size = pixels.Length / 4;
|
2015-11-21 10:25:42 +04:00
|
|
|
int plane0 = 0;
|
|
|
|
int plane1 = plane0 + plane_size;
|
2015-10-19 09:08:23 +04:00
|
|
|
int plane2 = plane1 + plane_size;
|
|
|
|
int plane3 = plane2 + plane_size;
|
|
|
|
|
|
|
|
byte[] output = new byte[pixels.Length];
|
|
|
|
int dst = 0;
|
|
|
|
while (dst < output.Length)
|
|
|
|
{
|
2015-11-21 10:25:42 +04:00
|
|
|
uint val = table[0,pixels[plane0++]] | table[1,pixels[plane1++]]
|
|
|
|
| table[2,pixels[plane2++]] | table[3,pixels[plane3++]];
|
2015-10-19 09:08:23 +04:00
|
|
|
|
|
|
|
output[dst++] = ConvertValue ((byte)val);
|
|
|
|
output[dst++] = ConvertValue ((byte)(val >> 8));
|
|
|
|
output[dst++] = ConvertValue ((byte)(val >> 16));
|
|
|
|
output[dst++] = ConvertValue ((byte)(val >> 24));
|
|
|
|
}
|
|
|
|
|
2015-11-29 12:27:49 +04:00
|
|
|
for (int x = m_pixel_size; x < Stride; x++)
|
2015-10-19 09:08:23 +04:00
|
|
|
{
|
|
|
|
output[x] += output[x - m_pixel_size];
|
|
|
|
}
|
|
|
|
|
2018-09-13 20:45:20 +04:00
|
|
|
int prev = 0;
|
2015-10-19 09:08:23 +04:00
|
|
|
for (uint y = 1; y < m_info.Height; y++)
|
|
|
|
{
|
2018-09-13 20:45:20 +04:00
|
|
|
int line = prev + Stride;
|
2015-11-29 12:27:49 +04:00
|
|
|
for (int x = 0; x < Stride; x++)
|
2015-08-19 21:40:32 +04:00
|
|
|
{
|
2015-10-19 09:08:23 +04:00
|
|
|
output[line+x] += output[prev+x];
|
2015-08-19 21:40:32 +04:00
|
|
|
}
|
2018-09-13 20:45:20 +04:00
|
|
|
prev = line;
|
2015-10-19 09:08:23 +04:00
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
static byte ConvertValue (byte val)
|
|
|
|
{
|
|
|
|
bool carry = 0 != (val & 1);
|
|
|
|
val >>= 1;
|
|
|
|
return (byte)(carry ? val ^ 0xFF : val);
|
|
|
|
}
|
|
|
|
|
|
|
|
#region IDisposable Members
|
|
|
|
public void Dispose ()
|
2015-11-29 12:27:49 +04:00
|
|
|
{
|
|
|
|
GC.SuppressFinalize (this);
|
|
|
|
}
|
2015-10-19 09:08:23 +04:00
|
|
|
#endregion
|
2015-08-19 21:40:32 +04:00
|
|
|
}
|
2015-11-29 12:27:49 +04:00
|
|
|
|
|
|
|
internal sealed class Hg3Reader : HgReader
|
|
|
|
{
|
|
|
|
public bool Flipped { get; private set; }
|
|
|
|
|
2016-10-26 05:10:30 +04:00
|
|
|
public override ImageData Image
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (null == m_image)
|
|
|
|
{
|
|
|
|
var pixels = Unpack();
|
2018-09-13 20:45:20 +04:00
|
|
|
PixelFormat format = 24 == m_info.BPP ? PixelFormats.Bgr24 : PixelFormats.Bgra32;
|
2016-10-26 05:10:30 +04:00
|
|
|
if (Flipped)
|
2018-09-13 20:45:20 +04:00
|
|
|
m_image = ImageData.CreateFlipped (Info, format, null, pixels, Stride);
|
2016-10-26 05:10:30 +04:00
|
|
|
else
|
2018-09-13 20:45:20 +04:00
|
|
|
m_image = ImageData.Create (Info, format, null, pixels, Stride);
|
2016-10-26 05:10:30 +04:00
|
|
|
}
|
|
|
|
return m_image;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-16 09:22:53 +04:00
|
|
|
public Hg3Reader (IBinaryStream input, HgMetaData info) : base (input, info)
|
2015-11-29 12:27:49 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] Unpack ()
|
|
|
|
{
|
2016-10-26 15:42:27 +04:00
|
|
|
Source.Position = m_info.HeaderSize;
|
2016-10-26 05:10:30 +04:00
|
|
|
var img_type = m_input.ReadBytes (8);
|
2016-10-16 09:22:53 +04:00
|
|
|
if (Binary.AsciiEqual (img_type, "img0000\0"))
|
2015-11-29 12:27:49 +04:00
|
|
|
return UnpackImg0000();
|
2016-10-16 09:22:53 +04:00
|
|
|
else if (Binary.AsciiEqual (img_type, "img_jpg\0"))
|
2015-11-29 12:27:49 +04:00
|
|
|
return UnpackJpeg();
|
2018-09-13 20:45:20 +04:00
|
|
|
else if (Binary.AsciiEqual (img_type, "img_wbp\0"))
|
|
|
|
return UnpackWebp();
|
2015-11-29 12:27:49 +04:00
|
|
|
else
|
|
|
|
throw new NotSupportedException ("Not supported HG-3 image");
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] UnpackImg0000 ()
|
|
|
|
{
|
|
|
|
Flipped = true;
|
2016-10-26 15:42:27 +04:00
|
|
|
Source.Position = m_info.HeaderSize+0x18;
|
2016-10-26 05:10:30 +04:00
|
|
|
int packed_data_size = m_input.ReadInt32();
|
|
|
|
int data_size = m_input.ReadInt32();
|
|
|
|
int packed_ctl_size = m_input.ReadInt32();
|
|
|
|
int ctl_size = m_input.ReadInt32();
|
2015-11-29 12:27:49 +04:00
|
|
|
return UnpackStream (m_info.HeaderSize+0x28, packed_data_size, data_size, packed_ctl_size, ctl_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] UnpackJpeg ()
|
|
|
|
{
|
|
|
|
Flipped = false;
|
2016-10-26 05:10:30 +04:00
|
|
|
m_input.ReadInt32();
|
|
|
|
var jpeg_size = m_input.ReadInt32();
|
2016-10-26 15:42:27 +04:00
|
|
|
long next_section = Source.Position + jpeg_size;
|
|
|
|
var decoder = new JpegBitmapDecoder (Source,
|
2015-11-29 12:27:49 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-26 05:10:30 +04:00
|
|
|
m_input.Position = next_section;
|
|
|
|
var section_header = m_input.ReadBytes (8);
|
2016-10-16 09:22:53 +04:00
|
|
|
if (!Binary.AsciiEqual (section_header, "img_al\0\0"))
|
2015-11-29 12:27:49 +04:00
|
|
|
return output;
|
2016-10-26 05:10:30 +04:00
|
|
|
m_input.Seek (8, SeekOrigin.Current);
|
|
|
|
int alpha_size = m_input.ReadInt32();
|
2016-10-26 15:42:27 +04:00
|
|
|
using (var alpha_in = new StreamRegion (Source, Source.Position+4, alpha_size, true))
|
2015-11-29 12:27:49 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2018-09-13 20:45:20 +04:00
|
|
|
|
|
|
|
byte[] UnpackWebp ()
|
|
|
|
{
|
|
|
|
throw new NotImplementedException ("HG-3 WebP decoder not implemented.");
|
|
|
|
}
|
2015-11-29 12:27:49 +04:00
|
|
|
}
|
2015-08-19 21:40:32 +04:00
|
|
|
}
|