2015-04-19 19:29:35 +08:00
|
|
|
//! \file ImagePT1.cs
|
|
|
|
//! \date Wed Apr 15 15:17:24 2015
|
2015-05-14 03:21:41 +08:00
|
|
|
//! \brief FFA System image format implementation.
|
2015-04-19 19:29:35 +08:00
|
|
|
//
|
|
|
|
// Copyright (C) 2015 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.Collections.Generic;
|
|
|
|
using System.ComponentModel.Composition;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
using System.Windows;
|
|
|
|
using System.Windows.Media;
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
using GameRes.Utility;
|
|
|
|
|
2015-05-14 03:21:41 +08:00
|
|
|
namespace GameRes.Formats.Ffa
|
2015-04-19 19:29:35 +08:00
|
|
|
{
|
|
|
|
internal class Pt1MetaData : ImageMetaData
|
|
|
|
{
|
2015-05-14 03:21:41 +08:00
|
|
|
public int Type;
|
2016-10-17 15:12:36 +08:00
|
|
|
public int PackedSize;
|
|
|
|
public int UnpackedSize;
|
2015-04-19 19:29:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Export(typeof(ImageFormat))]
|
|
|
|
public class Pt1Format : ImageFormat
|
|
|
|
{
|
|
|
|
public override string Tag { get { return "PT1"; } }
|
2015-05-14 03:21:41 +08:00
|
|
|
public override string Description { get { return "FFA System RGB image format"; } }
|
2015-04-19 19:29:35 +08:00
|
|
|
public override uint Signature { get { return 2u; } }
|
|
|
|
|
2015-05-14 03:21:41 +08:00
|
|
|
public Pt1Format ()
|
|
|
|
{
|
2015-11-12 00:52:19 +08:00
|
|
|
Signatures = new uint[] { 3, 2, 1, 0 };
|
2015-05-14 03:21:41 +08:00
|
|
|
}
|
|
|
|
|
2015-04-19 19:29:35 +08:00
|
|
|
public override void Write (Stream file, ImageData image)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException ("Pt1Format.Write not implemented");
|
|
|
|
}
|
|
|
|
|
2016-10-16 13:22:53 +08:00
|
|
|
public override ImageMetaData ReadMetaData (IBinaryStream file)
|
2015-04-19 19:29:35 +08:00
|
|
|
{
|
2016-10-16 13:22:53 +08:00
|
|
|
int type = file.ReadInt32();
|
|
|
|
if (type < 0 || type > 3)
|
|
|
|
return null;
|
|
|
|
if (-1 != file.ReadInt32())
|
|
|
|
return null;
|
|
|
|
int x = file.ReadInt32();
|
|
|
|
int y = file.ReadInt32();
|
|
|
|
uint width = file.ReadUInt32();
|
|
|
|
uint height = file.ReadUInt32();
|
2016-10-17 15:12:36 +08:00
|
|
|
int comp_size = file.ReadInt32();
|
|
|
|
int uncomp_size = file.ReadInt32();
|
2016-10-16 13:22:53 +08:00
|
|
|
if (uncomp_size != width*height*3u)
|
|
|
|
return null;
|
|
|
|
return new Pt1MetaData {
|
|
|
|
Width = width,
|
|
|
|
Height = height,
|
|
|
|
OffsetX = x,
|
|
|
|
OffsetY = y,
|
|
|
|
BPP = 3 == type ? 32 : 24,
|
|
|
|
Type = type,
|
|
|
|
PackedSize = comp_size,
|
|
|
|
UnpackedSize = uncomp_size
|
|
|
|
};
|
2015-04-19 19:29:35 +08:00
|
|
|
}
|
|
|
|
|
2016-10-16 13:22:53 +08:00
|
|
|
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
|
2015-04-19 19:29:35 +08:00
|
|
|
{
|
2016-10-17 15:12:36 +08:00
|
|
|
var reader = new Reader (stream, (Pt1MetaData)info);
|
2015-05-14 03:21:41 +08:00
|
|
|
reader.Unpack();
|
2016-10-17 15:12:36 +08:00
|
|
|
return ImageData.Create (info, reader.Format, null, reader.Data);
|
2015-04-19 19:29:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
internal class Reader
|
|
|
|
{
|
|
|
|
byte[] m_input;
|
|
|
|
byte[] m_output;
|
2015-11-12 00:52:19 +08:00
|
|
|
byte[] m_alpha_packed;
|
2015-05-14 03:21:41 +08:00
|
|
|
int m_type;
|
2015-04-19 19:29:35 +08:00
|
|
|
int m_width;
|
|
|
|
int m_height;
|
2015-04-20 18:13:10 +08:00
|
|
|
int m_stride;
|
2015-04-19 19:29:35 +08:00
|
|
|
|
2015-11-12 00:52:19 +08:00
|
|
|
public PixelFormat Format { get; private set; }
|
|
|
|
public byte[] Data { get { return m_output; } }
|
2015-04-19 19:29:35 +08:00
|
|
|
|
2016-10-17 15:12:36 +08:00
|
|
|
public Reader (IBinaryStream input, Pt1MetaData info)
|
2015-04-19 19:29:35 +08:00
|
|
|
{
|
2015-05-14 03:21:41 +08:00
|
|
|
m_type = info.Type;
|
2015-04-19 19:29:35 +08:00
|
|
|
m_input = new byte[info.PackedSize+8];
|
2015-11-12 00:52:19 +08:00
|
|
|
input.Position = 0x20;
|
2016-10-17 15:12:36 +08:00
|
|
|
if (info.PackedSize != input.Read (m_input, 0, info.PackedSize))
|
2015-04-19 19:29:35 +08:00
|
|
|
throw new InvalidFormatException ("Unexpected end of file");
|
|
|
|
m_width = (int)info.Width;
|
|
|
|
m_height = (int)info.Height;
|
|
|
|
m_output = new byte[info.UnpackedSize];
|
|
|
|
m_stride = m_width*3;
|
2015-11-12 00:52:19 +08:00
|
|
|
if (3 == m_type)
|
|
|
|
{
|
|
|
|
Format = PixelFormats.Bgra32;
|
2016-10-17 15:12:36 +08:00
|
|
|
int packed_size = input.ReadInt32();
|
|
|
|
m_alpha_packed = input.ReadBytes (packed_size);
|
|
|
|
if (m_alpha_packed.Length != packed_size)
|
|
|
|
throw new EndOfStreamException();
|
2015-11-12 00:52:19 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Format = PixelFormats.Bgr24;
|
|
|
|
}
|
2015-04-19 19:29:35 +08:00
|
|
|
}
|
|
|
|
|
2015-05-14 03:21:41 +08:00
|
|
|
public byte[] Unpack ()
|
|
|
|
{
|
2015-05-14 19:42:58 +08:00
|
|
|
switch (m_type)
|
|
|
|
{
|
2015-11-12 00:52:19 +08:00
|
|
|
case 3: UnpackV3(); break;
|
2015-05-14 19:42:58 +08:00
|
|
|
case 2: UnpackV2(); break;
|
|
|
|
case 1: UnpackV1(); break;
|
2015-11-12 00:52:19 +08:00
|
|
|
case 0: UnpackV0 (m_input, m_output); break;
|
2015-05-14 19:42:58 +08:00
|
|
|
}
|
2015-05-14 03:21:41 +08:00
|
|
|
return m_output;
|
|
|
|
}
|
|
|
|
|
2015-11-12 00:52:19 +08:00
|
|
|
void UnpackV3 ()
|
|
|
|
{
|
|
|
|
UnpackV2();
|
|
|
|
int total = m_width * m_height;
|
|
|
|
var alpha = new byte[total];
|
|
|
|
UnpackV0 (m_alpha_packed, alpha);
|
|
|
|
var pixels = new byte[total * 4];
|
|
|
|
int src = 0;
|
|
|
|
int dst = 0;
|
|
|
|
for (int i = 0; i < total; ++i)
|
|
|
|
{
|
|
|
|
pixels[dst++] = m_output[src++];
|
|
|
|
pixels[dst++] = m_output[src++];
|
|
|
|
pixels[dst++] = m_output[src++];
|
|
|
|
pixels[dst++] = alpha[i];
|
|
|
|
}
|
|
|
|
m_output = pixels;
|
|
|
|
}
|
|
|
|
|
2015-04-19 19:29:35 +08:00
|
|
|
uint edx;
|
|
|
|
byte ch;
|
|
|
|
int src;
|
|
|
|
|
|
|
|
void ReadNext ()
|
|
|
|
{
|
|
|
|
byte cl = (byte)(32 - ch);
|
|
|
|
edx &= 0xFFFFFFFFu >> cl;
|
|
|
|
edx += LittleEndian.ToUInt32 (m_input, src) << ch;
|
|
|
|
src += cl >> 3;
|
|
|
|
ch += (byte)(cl & 0xf8);
|
|
|
|
}
|
|
|
|
|
2015-05-14 03:21:41 +08:00
|
|
|
void UnpackV2 ()
|
2015-04-19 19:29:35 +08:00
|
|
|
{
|
|
|
|
src = 0;
|
|
|
|
int dst = 0;
|
|
|
|
Buffer.BlockCopy (m_input, src, m_output, dst, 3);
|
|
|
|
src += 3;
|
|
|
|
dst += 3;
|
|
|
|
edx = LittleEndian.ToUInt32 (m_input, src);
|
|
|
|
src += 3;
|
|
|
|
ch = 0x18;
|
|
|
|
uint _CF;
|
|
|
|
uint ebx;
|
|
|
|
sbyte ah;
|
|
|
|
byte al;
|
|
|
|
|
|
|
|
// [ebp+var_8] = i
|
|
|
|
for (int i = 1; i < m_width; ++i)
|
|
|
|
{
|
|
|
|
ReadNext();
|
|
|
|
|
|
|
|
_CF = edx & 1;
|
|
|
|
edx >>= 1;
|
|
|
|
|
|
|
|
if (0 != _CF)
|
|
|
|
{
|
|
|
|
--ch;
|
|
|
|
Buffer.BlockCopy (m_output, dst-3, m_output, dst, 3);
|
|
|
|
dst += 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ch -= 2;
|
|
|
|
_CF = edx & 1;
|
|
|
|
edx >>= 1;
|
|
|
|
if (0 != _CF)
|
|
|
|
{
|
|
|
|
ah = sub_4225EA();
|
|
|
|
al = (byte)(ah + m_output[dst-3]);
|
|
|
|
m_output[dst++] = al;
|
|
|
|
|
|
|
|
ReadNext();
|
|
|
|
ah = sub_4225EA();
|
|
|
|
al = (byte)(ah + m_output[dst-3]);
|
|
|
|
m_output[dst++] = al;
|
|
|
|
|
|
|
|
ReadNext();
|
|
|
|
ah = sub_4225EA();
|
|
|
|
al = (byte)(ah + m_output[dst-3]);
|
|
|
|
m_output[dst++] = al;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ReadNext();
|
|
|
|
LittleEndian.Pack ((ushort)edx, m_output, dst);
|
|
|
|
edx >>= 16;
|
|
|
|
m_output[dst+2] = (byte)edx;
|
|
|
|
dst += 3;
|
|
|
|
edx >>= 8;
|
|
|
|
ch -= 24;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 1; i < m_height; ++i)
|
|
|
|
{
|
|
|
|
ReadNext();
|
|
|
|
_CF = edx & 1;
|
|
|
|
edx >>= 1;
|
|
|
|
if (0 != _CF)
|
|
|
|
{
|
|
|
|
--ch;
|
|
|
|
Buffer.BlockCopy (m_output, dst-m_stride, m_output, dst, 3);
|
|
|
|
dst += 3;
|
|
|
|
}
|
|
|
|
else // loc_42207F
|
|
|
|
{
|
|
|
|
ch -= 2;
|
|
|
|
_CF = edx & 1;
|
|
|
|
edx >>= 1;
|
|
|
|
if (0 != _CF)
|
|
|
|
{
|
|
|
|
ah = sub_4225EA();
|
|
|
|
al = (byte)(ah + m_output[dst-m_stride]);
|
|
|
|
m_output[dst++] = al;
|
|
|
|
|
|
|
|
ReadNext();
|
|
|
|
ah = sub_4225EA();
|
|
|
|
al = (byte)(ah + m_output[dst-m_stride]);
|
|
|
|
m_output[dst++] = al;
|
|
|
|
|
|
|
|
ReadNext();
|
|
|
|
ah = sub_4225EA();
|
|
|
|
al = (byte)(ah + m_output[dst-m_stride]);
|
|
|
|
m_output[dst++] = al;
|
|
|
|
}
|
|
|
|
else // loc_4220FC
|
|
|
|
{
|
|
|
|
ReadNext();
|
|
|
|
LittleEndian.Pack ((ushort)edx, m_output, dst);
|
|
|
|
edx >>= 16;
|
|
|
|
m_output[dst+2] = (byte)edx;
|
|
|
|
dst += 3;
|
|
|
|
edx >>= 8;
|
|
|
|
ch -= 24;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int j = 1; j < m_width; ++j)
|
|
|
|
{
|
|
|
|
ReadNext();
|
|
|
|
_CF = edx & 1;
|
|
|
|
edx >>= 1;
|
|
|
|
if (0 != _CF)
|
|
|
|
{
|
|
|
|
--ch;
|
|
|
|
ebx = (uint)(dst - m_stride);
|
|
|
|
ah = sub_4225EA();
|
|
|
|
al = (byte)(m_output[dst-3] - m_output[ebx-3] + m_output[ebx] + ah);
|
|
|
|
m_output[dst++] = al;
|
|
|
|
|
|
|
|
ReadNext();
|
|
|
|
ah = sub_4225EA();
|
|
|
|
al = (byte)(m_output[dst-3] - m_output[ebx-2] + m_output[ebx+1] + ah);
|
|
|
|
m_output[dst++] = al;
|
|
|
|
|
|
|
|
ReadNext();
|
|
|
|
ah = sub_4225EA();
|
|
|
|
al = (byte)(m_output[dst-3] - m_output[ebx-1] + m_output[ebx+2] + ah);
|
|
|
|
m_output[dst++] = al;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_CF = edx & 1;
|
|
|
|
edx >>= 1;
|
|
|
|
if (0 != _CF)
|
|
|
|
{
|
|
|
|
ch -= 2;
|
|
|
|
ebx = (uint)(dst - m_stride);
|
|
|
|
al = (byte)(m_output[dst-3] - m_output[ebx-3] + m_output[ebx]);
|
|
|
|
m_output[dst++] = al;
|
|
|
|
al = (byte)(m_output[dst-3] - m_output[ebx-2] + m_output[ebx+1]);
|
|
|
|
m_output[dst++] = al;
|
|
|
|
al = (byte)(m_output[dst-3] - m_output[ebx-1] + m_output[ebx+2]);
|
|
|
|
m_output[dst++] = al;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ebx = edx & 3;
|
|
|
|
if (3 == ebx)
|
|
|
|
{
|
|
|
|
edx >>= 2;
|
|
|
|
ch -= 4;
|
|
|
|
Buffer.BlockCopy (m_output, dst-3, m_output, dst, 3);
|
|
|
|
dst += 3;
|
|
|
|
}
|
|
|
|
else if (2 == ebx)
|
|
|
|
{
|
|
|
|
edx >>= 2;
|
|
|
|
ch -= 4;
|
|
|
|
|
|
|
|
ReadNext();
|
|
|
|
LittleEndian.Pack ((ushort)edx, m_output, dst);
|
|
|
|
edx >>= 16;
|
|
|
|
m_output[dst+2] = (byte)edx;
|
|
|
|
dst += 3;
|
|
|
|
edx >>= 8;
|
|
|
|
ch -= 24;
|
|
|
|
}
|
|
|
|
else if (1 == ebx)
|
|
|
|
{
|
|
|
|
edx >>= 2;
|
|
|
|
ch -= 4;
|
|
|
|
ah = sub_4225EA();
|
|
|
|
al = (byte)(ah + m_output[dst-3]);
|
|
|
|
m_output[dst++] = al;
|
|
|
|
|
|
|
|
ReadNext();
|
|
|
|
ah = sub_4225EA();
|
|
|
|
al = (byte)(ah + m_output[dst-3]);
|
|
|
|
m_output[dst++] = al;
|
|
|
|
|
|
|
|
ReadNext();
|
|
|
|
ah = sub_4225EA();
|
|
|
|
al = (byte)(ah + m_output[dst-3]);
|
|
|
|
m_output[dst++] = al;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ebx = edx & 0xf;
|
|
|
|
edx >>= 4;
|
|
|
|
ch -= 6;
|
|
|
|
if (0 == ebx)
|
|
|
|
{
|
|
|
|
Buffer.BlockCopy (m_output, dst - m_stride - 3, m_output, dst, 3);
|
|
|
|
dst += 3;
|
|
|
|
}
|
|
|
|
else if (8 == ebx)
|
|
|
|
{
|
|
|
|
Buffer.BlockCopy (m_output, dst - m_stride, m_output, dst, 3);
|
|
|
|
dst += 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-12 00:52:19 +08:00
|
|
|
int off = dst - m_stride;
|
|
|
|
if (4 == ebx) off -= 3;
|
2015-04-19 19:29:35 +08:00
|
|
|
ah = sub_4225EA();
|
2015-11-12 00:52:19 +08:00
|
|
|
m_output[dst++] = (byte)(ah + m_output[off++]);
|
2015-04-19 19:29:35 +08:00
|
|
|
|
|
|
|
ReadNext();
|
|
|
|
ah = sub_4225EA();
|
2015-11-12 00:52:19 +08:00
|
|
|
m_output[dst++] = (byte)(ah + m_output[off++]);
|
2015-04-19 19:29:35 +08:00
|
|
|
|
|
|
|
ReadNext();
|
|
|
|
ah = sub_4225EA();
|
2015-11-12 00:52:19 +08:00
|
|
|
m_output[dst++] = (byte)(ah + m_output[off++]);
|
2015-04-19 19:29:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sbyte sub_4225EA ()
|
|
|
|
{
|
|
|
|
uint _CF = edx & 1;
|
|
|
|
edx >>= 1;
|
|
|
|
if (0 != _CF)
|
|
|
|
{
|
|
|
|
--ch;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint bits = edx & 3;
|
|
|
|
if (2 == bits)
|
|
|
|
{
|
|
|
|
edx >>= 2;
|
|
|
|
ch -= 3;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (1 == bits)
|
|
|
|
{
|
|
|
|
edx >>= 2;
|
|
|
|
ch -= 3;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
switch (edx & 7)
|
|
|
|
{
|
|
|
|
case 7:
|
|
|
|
edx >>= 3;
|
|
|
|
ch -= 4;
|
|
|
|
return -2;
|
|
|
|
case 3:
|
|
|
|
edx >>= 3;
|
|
|
|
ch -= 4;
|
|
|
|
return 2;
|
|
|
|
case 4:
|
|
|
|
edx >>= 3;
|
|
|
|
ch -= 4;
|
|
|
|
return -3;
|
|
|
|
default:
|
|
|
|
switch (edx & 0x3f)
|
|
|
|
{
|
|
|
|
case 0x38:
|
|
|
|
edx >>= 6;
|
|
|
|
ch -= 7;
|
|
|
|
return 3;
|
|
|
|
case 0x18:
|
|
|
|
edx >>= 6;
|
|
|
|
ch -= 7;
|
|
|
|
return -4;
|
|
|
|
case 0x28:
|
|
|
|
edx >>= 6;
|
|
|
|
ch -= 7;
|
|
|
|
return 4;
|
|
|
|
case 0x08:
|
|
|
|
edx >>= 6;
|
|
|
|
ch -= 7;
|
|
|
|
return -5;
|
|
|
|
case 0x30:
|
|
|
|
edx >>= 6;
|
|
|
|
ch -= 7;
|
|
|
|
return 5;
|
|
|
|
case 0x10:
|
|
|
|
edx >>= 6;
|
|
|
|
ch -= 7;
|
|
|
|
return -6;
|
|
|
|
case 0x20:
|
|
|
|
edx >>= 6;
|
|
|
|
ch -= 7;
|
|
|
|
return 6;
|
|
|
|
default:
|
|
|
|
switch (edx & 0xff)
|
|
|
|
{
|
|
|
|
case 0xc0:
|
|
|
|
edx >>= 8;
|
|
|
|
ch -= 9;
|
|
|
|
return -7;
|
|
|
|
case 0x40:
|
|
|
|
edx >>= 8;
|
|
|
|
ch -= 9;
|
|
|
|
return 7;
|
|
|
|
case 0x80:
|
|
|
|
edx >>= 8;
|
|
|
|
ch -= 9;
|
|
|
|
return -8;
|
|
|
|
default:
|
|
|
|
switch (edx & 0x3ff)
|
|
|
|
{
|
|
|
|
case 0x300:
|
|
|
|
edx >>= 10;
|
|
|
|
ch -= 11;
|
|
|
|
return 8;
|
|
|
|
case 0x100:
|
|
|
|
edx >>= 10;
|
|
|
|
ch -= 11;
|
|
|
|
return -9;
|
|
|
|
case 0x200:
|
|
|
|
edx >>= 10;
|
|
|
|
ch -= 11;
|
|
|
|
return 9;
|
|
|
|
default:
|
|
|
|
switch (edx & 0xfff)
|
|
|
|
{
|
|
|
|
case 0xc00:
|
|
|
|
edx >>= 12;
|
|
|
|
ch -= 13;
|
|
|
|
return -10;
|
|
|
|
case 0x400:
|
|
|
|
edx >>= 12;
|
|
|
|
ch -= 13;
|
|
|
|
return 10;
|
|
|
|
case 0x800:
|
|
|
|
edx >>= 12;
|
|
|
|
ch -= 13;
|
|
|
|
return -11;
|
|
|
|
default:
|
|
|
|
switch (edx & 0x3fff)
|
|
|
|
{
|
|
|
|
case 0x3000:
|
|
|
|
edx >>= 14;
|
|
|
|
ch -= 15;
|
|
|
|
return 0x0b;
|
|
|
|
case 0x1000:
|
|
|
|
edx >>= 14;
|
|
|
|
ch -= 15;
|
|
|
|
return -12;
|
|
|
|
case 0x2000:
|
|
|
|
edx >>= 14;
|
|
|
|
ch -= 15;
|
|
|
|
return 0x0c;
|
|
|
|
default:
|
|
|
|
edx >>= 14;
|
|
|
|
ch -= 15;
|
|
|
|
return -13;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-14 03:21:41 +08:00
|
|
|
|
|
|
|
void UnpackV1 ()
|
|
|
|
{
|
|
|
|
int src = 0; // dword_462E74
|
|
|
|
int dst = 0; // dword_462E78
|
|
|
|
byte[] frame = new byte[0x1000]; // word_461A28
|
2015-05-14 19:42:58 +08:00
|
|
|
PopulateLzssFrame (frame);
|
2015-05-14 03:21:41 +08:00
|
|
|
int ebp = 0xfee;
|
|
|
|
while (src < m_input.Length)
|
|
|
|
{
|
|
|
|
byte ah = m_input[src++];
|
|
|
|
for (int mask = 1; mask != 0x100; mask <<= 1)
|
|
|
|
{
|
|
|
|
if (0 != (ah & mask))
|
|
|
|
{
|
|
|
|
byte al = m_input[src++];
|
|
|
|
frame[ebp++] = al;
|
|
|
|
ebp &= 0xfff;
|
|
|
|
m_output[dst++] = al;
|
|
|
|
m_output[dst++] = al;
|
|
|
|
m_output[dst++] = al;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int offset = m_input[src++];
|
|
|
|
int count = m_input[src++];
|
|
|
|
offset |= (count & 0xf0) << 4;
|
|
|
|
count = (count & 0x0f) + 3;
|
|
|
|
for (; count != 0; --count)
|
|
|
|
{
|
|
|
|
byte al = frame[offset++];
|
|
|
|
frame[ebp++] = al;
|
|
|
|
offset &= 0xfff;
|
|
|
|
ebp &= 0xfff;
|
|
|
|
m_output[dst++] = al;
|
|
|
|
m_output[dst++] = al;
|
|
|
|
m_output[dst++] = al;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dst >= m_output.Length)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-14 19:42:58 +08:00
|
|
|
|
2015-11-12 00:52:19 +08:00
|
|
|
void UnpackV0 (byte[] input, byte[] output)
|
2015-05-14 19:42:58 +08:00
|
|
|
{
|
|
|
|
int src = 0;
|
|
|
|
int dst = 0;
|
|
|
|
byte[] frame = new byte[0x1000]; // word_461A28
|
|
|
|
PopulateLzssFrame (frame);
|
|
|
|
int ebp = 0xfee;
|
2015-11-12 00:52:19 +08:00
|
|
|
while (src < input.Length)
|
2015-05-14 19:42:58 +08:00
|
|
|
{
|
2015-11-12 00:52:19 +08:00
|
|
|
byte ah = input[src++];
|
2015-05-14 19:42:58 +08:00
|
|
|
for (int mask = 1; mask != 0x100; mask <<= 1)
|
|
|
|
{
|
|
|
|
if (0 != (ah & mask))
|
|
|
|
{
|
2015-11-12 00:52:19 +08:00
|
|
|
byte al = input[src++];
|
2015-05-14 19:42:58 +08:00
|
|
|
frame[ebp++] = al;
|
|
|
|
ebp &= 0xfff;
|
2015-11-12 00:52:19 +08:00
|
|
|
output[dst++] = al;
|
2015-05-14 19:42:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-12 00:52:19 +08:00
|
|
|
int offset = input[src++];
|
|
|
|
int count = input[src++];
|
2015-05-14 19:42:58 +08:00
|
|
|
offset |= (count & 0xf0) << 4;
|
|
|
|
count = (count & 0x0f) + 3;
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
byte al = frame[offset++];
|
|
|
|
frame[ebp++] = al;
|
|
|
|
offset &= 0xfff;
|
|
|
|
ebp &= 0xfff;
|
2015-11-12 00:52:19 +08:00
|
|
|
output[dst++] = al;
|
2015-05-14 19:42:58 +08:00
|
|
|
}
|
|
|
|
}
|
2015-11-12 00:52:19 +08:00
|
|
|
if (dst >= output.Length)
|
2015-05-14 19:42:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PopulateLzssFrame (byte[] frame)
|
|
|
|
{
|
|
|
|
int fill = 0;
|
|
|
|
int ecx;
|
|
|
|
for (int al = 0; al < 0x100; ++al)
|
|
|
|
for (ecx = 0x0d; ecx > 0; --ecx)
|
|
|
|
frame[fill++] = (byte)al;
|
|
|
|
for (int al = 0; al < 0x100; ++al)
|
|
|
|
frame[fill++] = (byte)al;
|
|
|
|
for (int al = 0xff; al >= 0; --al)
|
|
|
|
frame[fill++] = (byte)al;
|
|
|
|
for (ecx = 0x80; ecx > 0; --ecx)
|
|
|
|
frame[fill++] = 0;
|
|
|
|
for (ecx = 0x6e; ecx > 0; --ecx)
|
|
|
|
frame[fill++] = 0x20;
|
|
|
|
for (ecx = 0x12; ecx > 0; --ecx)
|
|
|
|
frame[fill++] = 0;
|
|
|
|
}
|
2015-04-19 19:29:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|