2016-07-06 08:14:42 +08:00
|
|
|
//! \file ImageTINK.cs
|
|
|
|
//! \date Fri Jun 17 18:49:04 2016
|
|
|
|
//! \brief Tinker Bell encrypted image file.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2016 by morkt
|
|
|
|
//
|
|
|
|
// 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.ComponentModel.Composition;
|
|
|
|
using System.IO;
|
2016-10-26 00:57:04 +08:00
|
|
|
using System.Windows.Media;
|
2016-07-06 08:14:42 +08:00
|
|
|
|
|
|
|
namespace GameRes.Formats.Cyberworks
|
|
|
|
{
|
|
|
|
public enum AImageHeader
|
|
|
|
{
|
|
|
|
Flags = 0,
|
|
|
|
Field1 = 1,
|
|
|
|
Field2 = 2,
|
|
|
|
Height = 3,
|
|
|
|
Width = 4,
|
|
|
|
UnpackedSize = 5,
|
|
|
|
AlphaSize = 6,
|
|
|
|
BitsSize = 7,
|
2016-07-07 21:38:02 +08:00
|
|
|
Ignored = Field1,
|
2016-07-06 08:14:42 +08:00
|
|
|
}
|
|
|
|
|
2016-10-26 00:57:04 +08:00
|
|
|
internal sealed class AImageReader : IImageDecoder
|
2016-07-06 08:14:42 +08:00
|
|
|
{
|
2016-10-26 00:57:04 +08:00
|
|
|
readonly ImageMetaData m_info = new ImageMetaData();
|
2016-07-06 08:14:42 +08:00
|
|
|
|
2016-10-26 00:57:04 +08:00
|
|
|
IBinaryStream m_input;
|
2016-07-06 08:14:42 +08:00
|
|
|
byte[] m_output;
|
|
|
|
AImageScheme m_scheme;
|
2016-10-26 00:57:04 +08:00
|
|
|
ImageData m_image;
|
2017-03-26 11:27:52 +08:00
|
|
|
int[] m_header;
|
2016-10-26 00:57:04 +08:00
|
|
|
|
2016-10-26 19:42:27 +08:00
|
|
|
public Stream Source { get { m_input.Position = 0; return m_input.AsStream; } }
|
|
|
|
public ImageFormat SourceFormat { get { return null; } }
|
|
|
|
public ImageMetaData Info { get { return m_info; } }
|
2017-03-26 11:27:52 +08:00
|
|
|
public byte[] Baseline { get; set; }
|
2016-10-26 00:57:04 +08:00
|
|
|
|
|
|
|
public ImageData Image
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (null == m_image)
|
|
|
|
{
|
|
|
|
Unpack();
|
|
|
|
int stride = (int)Info.Width * Info.BPP / 8;
|
|
|
|
if (m_scheme.Flipped)
|
|
|
|
m_image = ImageData.CreateFlipped (Info, GetPixelFormat(), null, Data, stride);
|
|
|
|
else
|
|
|
|
m_image = ImageData.Create (Info, GetPixelFormat(), null, Data, stride);
|
|
|
|
}
|
|
|
|
return m_image;
|
|
|
|
}
|
|
|
|
}
|
2016-07-06 08:14:42 +08:00
|
|
|
|
|
|
|
public byte[] Data { get { return m_output; } }
|
|
|
|
|
2016-10-26 00:57:04 +08:00
|
|
|
public AImageReader (IBinaryStream input, AImageScheme scheme)
|
2016-07-06 08:14:42 +08:00
|
|
|
{
|
2016-10-26 00:57:04 +08:00
|
|
|
m_input = input;
|
2016-07-06 08:14:42 +08:00
|
|
|
m_scheme = scheme;
|
|
|
|
}
|
|
|
|
|
2017-03-26 11:27:52 +08:00
|
|
|
internal int[] ReadHeader ()
|
2016-07-06 08:14:42 +08:00
|
|
|
{
|
2017-03-26 11:27:52 +08:00
|
|
|
if (m_header != null)
|
|
|
|
return m_header;
|
2016-07-31 22:54:58 +08:00
|
|
|
int header_length = Math.Max (8, m_scheme.HeaderOrder.Length);
|
2017-03-26 11:27:52 +08:00
|
|
|
m_header = new int[header_length];
|
2016-07-06 08:14:42 +08:00
|
|
|
for (int i = 0; i < m_scheme.HeaderOrder.Length; ++i)
|
|
|
|
{
|
|
|
|
int b = GetInt();
|
2017-03-26 11:27:52 +08:00
|
|
|
m_header[m_scheme.HeaderOrder[i]] = b;
|
2016-07-06 08:14:42 +08:00
|
|
|
}
|
2017-03-26 11:27:52 +08:00
|
|
|
Info.Width = (uint)m_header[4];
|
|
|
|
Info.Height = (uint)m_header[3];
|
|
|
|
return m_header;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Unpack ()
|
|
|
|
{
|
|
|
|
var header = ReadHeader();
|
2016-07-06 08:14:42 +08:00
|
|
|
if (0 == Info.Width || Info.Width >= 0x8000 || 0 == Info.Height || Info.Height >= 0x8000)
|
|
|
|
throw new InvalidFormatException();
|
|
|
|
int unpacked_size = header[5];
|
|
|
|
if (unpacked_size <= 0)
|
|
|
|
throw new InvalidFormatException();
|
|
|
|
int flags = header[0];
|
|
|
|
int bits_size = header[7];
|
|
|
|
int data_offset = bits_size * 2;
|
|
|
|
if (0 == flags)
|
|
|
|
CopyV0 (unpacked_size);
|
|
|
|
else if (2 == (flags & 6))
|
|
|
|
UnpackV2 (bits_size, data_offset);
|
|
|
|
else if (6 == (flags & 6))
|
|
|
|
{
|
|
|
|
if (0 == bits_size)
|
|
|
|
CopyV6 (unpacked_size, header[6]);
|
|
|
|
else
|
|
|
|
UnpackV6 (bits_size, data_offset, data_offset + header[6]);
|
|
|
|
}
|
2017-03-26 08:06:44 +08:00
|
|
|
else if (0 == bits_size)
|
|
|
|
CopyV0 (unpacked_size);
|
2016-07-06 08:14:42 +08:00
|
|
|
else
|
2017-03-26 08:06:44 +08:00
|
|
|
UnpackV1 (bits_size, unpacked_size);
|
2016-07-06 08:14:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CopyV0 (int data_size)
|
|
|
|
{
|
|
|
|
int plane_size = (int)Info.Width * (int)Info.Height;
|
|
|
|
if (plane_size == data_size)
|
|
|
|
{
|
|
|
|
Info.BPP = 8;
|
|
|
|
m_output = m_input.ReadBytes (data_size);
|
|
|
|
}
|
|
|
|
else if (3 * plane_size == data_size)
|
|
|
|
{
|
|
|
|
Info.BPP = 24;
|
|
|
|
m_output = m_input.ReadBytes (data_size);
|
|
|
|
}
|
|
|
|
else if (4 * plane_size == data_size)
|
|
|
|
{
|
|
|
|
Info.BPP = 32;
|
|
|
|
m_output = m_input.ReadBytes (data_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Info.BPP = 24;
|
|
|
|
int dst_stride = (int)Info.Width * 3;
|
|
|
|
int src_stride = (dst_stride + 3) & ~3;
|
|
|
|
if (src_stride * (int)Info.Height != data_size)
|
|
|
|
throw new InvalidFormatException();
|
|
|
|
m_output = new byte[dst_stride * (int)Info.Height];
|
2016-07-07 18:44:19 +08:00
|
|
|
var gap = new byte[src_stride-dst_stride];
|
2016-07-06 08:14:42 +08:00
|
|
|
int dst = 0;
|
|
|
|
for (uint y = 0; y < Info.Height; ++y)
|
|
|
|
{
|
|
|
|
m_input.Read (m_output, dst, dst_stride);
|
2016-07-07 18:44:19 +08:00
|
|
|
m_input.Read (gap, 0, gap.Length);
|
2016-07-06 08:14:42 +08:00
|
|
|
dst += dst_stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 08:06:44 +08:00
|
|
|
void UnpackV1 (int alpha_size, int rgb_size)
|
|
|
|
{
|
|
|
|
var alpha_map = m_input.ReadBytes (alpha_size);
|
|
|
|
if (alpha_map.Length != alpha_size)
|
|
|
|
throw new InvalidFormatException();
|
|
|
|
|
|
|
|
int plane_size = (int)Info.Width * (int)Info.Height;
|
2017-03-26 11:27:52 +08:00
|
|
|
if (Baseline != null)
|
|
|
|
{
|
|
|
|
Info.BPP = 24;
|
|
|
|
m_output = Baseline;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Info.BPP = 32;
|
|
|
|
m_output = new byte[plane_size * 4];
|
|
|
|
}
|
|
|
|
int pixel_size = Info.BPP / 8;
|
2017-03-26 08:06:44 +08:00
|
|
|
int bit = 1;
|
|
|
|
int bit_src = 0;
|
|
|
|
int dst = 0;
|
|
|
|
for (int i = 0; i < plane_size; ++i)
|
|
|
|
{
|
2017-03-26 11:27:52 +08:00
|
|
|
byte alpha = 0;
|
2017-03-26 08:06:44 +08:00
|
|
|
if ((bit & alpha_map[bit_src]) != 0)
|
|
|
|
{
|
|
|
|
m_input.Read (m_output, dst, 3);
|
2017-03-26 11:27:52 +08:00
|
|
|
alpha = 0xFF;
|
2017-03-26 08:06:44 +08:00
|
|
|
}
|
2017-03-26 11:27:52 +08:00
|
|
|
if (4 == pixel_size)
|
|
|
|
m_output[dst+3] = alpha;
|
|
|
|
dst += pixel_size;
|
2017-03-26 08:06:44 +08:00
|
|
|
if (0x80 == bit)
|
|
|
|
{
|
|
|
|
++bit_src;
|
|
|
|
bit = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bit <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 08:14:42 +08:00
|
|
|
void UnpackV2 (int offset1, int rgb_offset)
|
|
|
|
{
|
|
|
|
Info.BPP = 24;
|
|
|
|
var rgb_map = m_input.ReadBytes (offset1);
|
|
|
|
var alpha_map = m_input.ReadBytes (rgb_offset-offset1);
|
|
|
|
int plane_size = (int)Info.Width * (int)Info.Height;
|
|
|
|
m_output = new byte[plane_size * 3];
|
|
|
|
|
|
|
|
int bit = 1;
|
|
|
|
int bit_src = 0;
|
|
|
|
int dst = 0;
|
|
|
|
for (int i = 0; i < plane_size; ++i)
|
|
|
|
{
|
|
|
|
if ((bit & alpha_map[bit_src]) == 0 && (bit & rgb_map[bit_src]) != 0)
|
|
|
|
{
|
|
|
|
m_input.Read (m_output, dst, 3);
|
|
|
|
}
|
|
|
|
dst += 3;
|
|
|
|
if (0x80 == bit)
|
|
|
|
{
|
|
|
|
++bit_src;
|
|
|
|
bit = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bit <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CopyV6 (int alpha_size, int rgb_size)
|
|
|
|
{
|
|
|
|
Info.BPP = 32;
|
|
|
|
int plane_size = (int)Info.Width * (int)Info.Height;
|
|
|
|
m_output = new byte[plane_size * 4];
|
|
|
|
int stride = ((int)Info.Width * 3 + 3) & ~3;
|
|
|
|
var line = new byte[stride];
|
|
|
|
int dst = 3;
|
|
|
|
for (uint y = 0; y < Info.Height; ++y)
|
|
|
|
{
|
|
|
|
m_input.Read (line, 0, stride);
|
|
|
|
int src = 0;
|
|
|
|
for (uint x = 0; x < Info.Width; ++x)
|
|
|
|
{
|
|
|
|
m_output[dst] = line[src];
|
|
|
|
dst += 4;
|
|
|
|
src += 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dst = 0;
|
|
|
|
for (uint y = 0; y < Info.Height; ++y)
|
|
|
|
{
|
|
|
|
m_input.Read (line, 0, stride);
|
|
|
|
int src = 0;
|
|
|
|
for (uint x = 0; x < Info.Width; ++x)
|
|
|
|
{
|
|
|
|
m_output[dst ] = line[src++];
|
|
|
|
m_output[dst+1] = line[src++];
|
|
|
|
m_output[dst+2] = line[src++];
|
|
|
|
dst += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnpackV6 (int offset1, int alpha_offset, int rgb_offset)
|
|
|
|
{
|
|
|
|
Info.BPP = 32;
|
|
|
|
var rgb_map = m_input.ReadBytes (offset1);
|
|
|
|
var alpha_map = m_input.ReadBytes (alpha_offset - offset1);
|
|
|
|
var alpha = m_input.ReadBytes (rgb_offset - alpha_offset);
|
|
|
|
int plane_size = (int)Info.Width * (int)Info.Height;
|
|
|
|
m_output = new byte[plane_size * 4];
|
|
|
|
|
|
|
|
int bit = 1;
|
|
|
|
int bit_src = 0;
|
|
|
|
int alpha_src = 0;
|
|
|
|
int dst = 0;
|
|
|
|
for (int i = 0; i < plane_size; ++i)
|
|
|
|
{
|
|
|
|
bool has_alpha = (bit & alpha_map[bit_src]) != 0;
|
|
|
|
if (has_alpha || (bit & rgb_map[bit_src]) != 0)
|
|
|
|
{
|
|
|
|
m_input.Read (m_output, dst, 3);
|
2016-10-24 08:58:18 +08:00
|
|
|
if (has_alpha && alpha_src < alpha.Length)
|
2016-07-06 08:14:42 +08:00
|
|
|
{
|
|
|
|
m_output[dst+3] = alpha[alpha_src];
|
|
|
|
alpha_src += 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_output[dst+3] = 0xFF;
|
|
|
|
}
|
|
|
|
dst += 4;
|
|
|
|
if (0x80 == bit)
|
|
|
|
{
|
|
|
|
++bit_src;
|
|
|
|
bit = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bit <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetInt ()
|
|
|
|
{
|
2016-10-26 00:57:04 +08:00
|
|
|
byte a = m_input.ReadUInt8();
|
2016-07-06 08:14:42 +08:00
|
|
|
if (a == m_scheme.Value3)
|
|
|
|
a = 0;
|
|
|
|
int d = 0;
|
|
|
|
int c = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
2016-10-26 00:57:04 +08:00
|
|
|
byte a1 = m_input.ReadUInt8();
|
2016-07-06 08:14:42 +08:00
|
|
|
if (a1 == m_scheme.Value2)
|
|
|
|
break;
|
|
|
|
if (a1 != m_scheme.Value1)
|
|
|
|
{
|
|
|
|
c = (a1 == m_scheme.Value3) ? 0 : a1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a + (c + d * m_scheme.Value1) * m_scheme.Value1;
|
|
|
|
}
|
|
|
|
|
2016-10-26 00:57:04 +08:00
|
|
|
PixelFormat GetPixelFormat ()
|
|
|
|
{
|
|
|
|
switch (Info.BPP)
|
|
|
|
{
|
|
|
|
case 8: return PixelFormats.Gray8;
|
|
|
|
case 24: return PixelFormats.Bgr24;
|
|
|
|
case 32: return PixelFormats.Bgra32;
|
|
|
|
default: throw new InvalidFormatException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 08:14:42 +08:00
|
|
|
#region IDisposable Members
|
|
|
|
bool _disposed = false;
|
|
|
|
public void Dispose ()
|
|
|
|
{
|
|
|
|
if (!_disposed)
|
|
|
|
{
|
|
|
|
m_input.Dispose();
|
|
|
|
_disposed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|