GARbro-mirror/ArcFormats/AliceSoft/ImageDCF.cs

351 lines
14 KiB
C#
Raw Normal View History

2016-07-29 22:10:13 +08:00
//! \file ImageDCF.cs
//! \date Fri Jul 29 14:07:15 2016
//! \brief AliceSoft incremental image format.
//
2023-08-24 05:33:50 +08:00
// Copyright (C) 2016-2022 by morkt
2016-07-29 22:10:13 +08: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.ComponentModel.Composition;
using System.Diagnostics;
using System.IO;
2023-08-24 05:33:50 +08:00
using System.Text;
2016-07-29 22:10:13 +08:00
using System.Windows.Media;
using GameRes.Compression;
using GameRes.Utility;
namespace GameRes.Formats.AliceSoft
{
internal class DcfMetaData : ImageMetaData
{
public string BaseName;
public long DataOffset;
2023-08-24 05:33:50 +08:00
public bool IsPcf;
}
internal interface IBaseImageReader
{
int BPP { get; }
byte[] Data { get; }
void Unpack ();
2016-07-29 22:10:13 +08:00
}
[Export(typeof(ImageFormat))]
public class DcfFormat : ImageFormat
{
public override string Tag { get { return "DCF"; } }
public override string Description { get { return "AliceSoft System incremental image"; } }
public override uint Signature { get { return 0x20666364; } } // 'dcf '
2023-08-24 05:33:50 +08:00
public DcfFormat ()
{
Extensions = new[] { "dcf", "pcf" };
Signatures = new[] { 0x20666364u, 0x20666370u };
}
static readonly ResourceInstance<AfaOpener> Afa = new ResourceInstance<AfaOpener> ("AFA");
2016-10-15 16:21:12 +08:00
public override ImageMetaData ReadMetaData (IBinaryStream stream)
2016-07-29 22:10:13 +08:00
{
2023-08-24 05:33:50 +08:00
var header = stream.ReadHeader (0x1C);
uint header_size = header.ToUInt32 (4);
long data_pos = 8 + header_size;
if (header.ToInt32 (8) != 1)
2016-10-15 16:21:12 +08:00
return null;
2023-08-24 05:33:50 +08:00
uint width = header.ToUInt32 (0x0C);
uint height = header.ToUInt32 (0x10);
int bpp = header.ToInt32 (0x14);
int name_length = header.ToInt32 (0x18);
2016-10-15 16:21:12 +08:00
if (name_length <= 0)
return null;
int shift = (name_length % 7) + 1;
var name_bits = stream.ReadBytes (name_length);
for (int i = 0; i < name_length; ++i)
2016-07-29 22:10:13 +08:00
{
2016-10-15 16:21:12 +08:00
name_bits[i] = Binary.RotByteL (name_bits[i], shift);
2016-07-29 22:10:13 +08:00
}
2016-10-15 16:21:12 +08:00
return new DcfMetaData
{
Width = width,
Height = height,
BPP = bpp,
2023-08-24 05:33:50 +08:00
BaseName = Afa.Value.NameEncoding.GetString (name_bits),
2016-10-15 16:21:12 +08:00
DataOffset = data_pos,
2023-08-24 05:33:50 +08:00
IsPcf = stream.Signature == 0x20666370u,
2016-10-15 16:21:12 +08:00
};
2016-07-29 22:10:13 +08:00
}
2016-10-15 16:21:12 +08:00
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
2016-07-29 22:10:13 +08:00
{
2023-08-24 05:33:50 +08:00
var reader = new DcfReader (stream, (DcfMetaData)info);
reader.Unpack();
return ImageData.Create (reader.Info, reader.Format, null, reader.Data, reader.Stride);
2016-07-29 22:10:13 +08:00
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("DcfFormat.Write not implemented");
}
}
2023-08-24 05:33:50 +08:00
internal sealed class DcfReader : IBaseImageReader
2016-07-29 22:10:13 +08:00
{
2016-10-15 16:21:12 +08:00
IBinaryStream m_input;
2016-07-29 22:10:13 +08:00
DcfMetaData m_info;
byte[] m_output;
byte[] m_mask = null;
byte[] m_base = null;
int m_overlay_bpp;
int m_base_bpp;
2023-08-24 05:33:50 +08:00
static readonly ResourceInstance<ImageFormat> s_QntFormat = new ResourceInstance<ImageFormat> ("QNT");
static readonly ResourceInstance<ImageFormat> s_DcfFormat = new ResourceInstance<ImageFormat> ("DCF");
2016-07-29 22:10:13 +08:00
internal ImageFormat Qnt { get { return s_QntFormat.Value; } }
2023-08-24 05:33:50 +08:00
internal ImageFormat Dcf { get { return s_DcfFormat.Value; } }
2016-07-29 22:10:13 +08:00
2023-08-24 05:33:50 +08:00
public int BPP { get { return m_base_bpp; } }
public ImageMetaData Info { get; private set; }
2016-07-29 22:10:13 +08:00
public byte[] Data { get { return m_output; } }
public PixelFormat Format { get; private set; }
public int Stride { get; private set; }
2016-10-15 16:21:12 +08:00
public DcfReader (IBinaryStream input, DcfMetaData info)
2016-07-29 22:10:13 +08:00
{
2016-10-15 16:21:12 +08:00
m_input = input;
2016-07-29 22:10:13 +08:00
m_info = info;
2023-08-24 05:33:50 +08:00
Info = info;
2016-07-29 22:10:13 +08:00
}
public void Unpack ()
{
2023-08-24 05:33:50 +08:00
int pt_x = 0;
int pt_y = 0;
2016-07-29 22:10:13 +08:00
long next_pos = m_info.DataOffset;
for (;;)
{
2016-10-15 16:21:12 +08:00
m_input.Position = next_pos;
2016-07-29 22:10:13 +08:00
uint id = m_input.ReadUInt32();
next_pos += 8 + m_input.ReadUInt32();
if (0x6C646664 == id) // 'dfdl'
{
int unpacked_size = m_input.ReadInt32();
if (unpacked_size <= 0)
continue;
m_mask = new byte[unpacked_size];
2016-10-15 16:21:12 +08:00
using (var input = new ZLibStream (m_input.AsStream, CompressionMode.Decompress, true))
2016-07-29 22:10:13 +08:00
input.Read (m_mask, 0, unpacked_size);
}
2023-08-24 05:33:50 +08:00
else if (0x6C647470 == id) // 'ptdl'
{
pt_x = m_input.ReadInt32();
pt_y = m_input.ReadInt32();
}
else if (0x64676364 == id || 0x64676370 == id) // 'dcgd' || 'pcgd'
2016-07-29 22:10:13 +08:00
break;
}
2016-10-15 16:21:12 +08:00
long qnt_pos = m_input.Position;
2016-07-29 22:10:13 +08:00
if (m_input.ReadUInt32() != Qnt.Signature)
throw new InvalidFormatException();
2016-10-16 13:22:53 +08:00
using (var reg = new StreamRegion (m_input.AsStream, qnt_pos, true))
using (var qnt = new BinaryStream (reg, m_input.Name))
2018-09-07 19:49:12 +08:00
{
var qnt_info = Qnt.ReadMetaData (qnt) as QntMetaData;
if (null == qnt_info)
throw new InvalidFormatException();
2016-07-29 22:10:13 +08:00
2018-09-07 19:49:12 +08:00
var overlay = new QntFormat.Reader (reg, qnt_info);
overlay.Unpack();
m_overlay_bpp = overlay.BPP;
2023-08-24 05:33:50 +08:00
if (m_mask != null || m_info.IsPcf)
2018-09-07 19:49:12 +08:00
ReadBaseImage();
2016-07-29 22:10:13 +08:00
2023-08-24 05:33:50 +08:00
if (m_info.IsPcf)
2018-09-07 19:49:12 +08:00
{
2023-08-24 05:33:50 +08:00
if (null == m_base)
SetEmptyBase();
qnt_info.OffsetX = pt_x;
qnt_info.OffsetY = pt_y;
BlendOverlay (qnt_info, overlay.Data);
m_output = m_base;
SetFormat (m_info.iWidth, m_base_bpp);
}
else if (m_base != null)
{
m_output = MaskOverlay (overlay.Data);
SetFormat (m_info.iWidth, m_overlay_bpp);
2018-09-07 19:49:12 +08:00
}
else
{
m_output = overlay.Data;
2023-08-24 05:33:50 +08:00
SetFormat (qnt_info.iWidth, m_overlay_bpp);
2018-09-07 19:49:12 +08:00
}
2016-07-29 22:10:13 +08:00
}
}
void SetFormat (int width, int bpp)
{
Format = 24 == bpp ? PixelFormats.Bgr24 : PixelFormats.Bgra32;
Stride = width * (bpp / 8);
}
2023-08-24 05:33:50 +08:00
void SetEmptyBase ()
{
m_base_bpp = 32;
m_base = new byte[m_info.Width * m_info.Height * 4];
}
byte[] MaskOverlay (byte[] overlay)
2016-07-29 22:10:13 +08:00
{
2023-08-24 05:33:50 +08:00
int blocks_x = m_info.iWidth / 0x10;
int blocks_y = m_info.iHeight / 0x10;
2016-07-29 22:10:13 +08:00
int base_step = m_base_bpp / 8;
int overlay_step = m_overlay_bpp / 8;
2023-08-24 05:33:50 +08:00
int base_stride = m_info.iWidth * base_step;
int overlay_stride = m_info.iWidth * overlay_step;
2016-07-29 22:10:13 +08:00
int mask_pos = 4;
for (int y = 0; y < blocks_y; ++y)
{
int base_pos = y * 0x10 * base_stride;
2018-09-07 19:49:12 +08:00
int dst_pos = y * 0x10 * overlay_stride;
2016-07-29 22:10:13 +08:00
for (int x = 0; x < blocks_x; ++x)
{
2018-09-07 19:49:12 +08:00
if (0 == m_mask[mask_pos++])
2016-07-29 22:10:13 +08:00
continue;
for (int by = 0; by < 0x10; ++by)
{
2018-09-07 19:49:12 +08:00
int src = base_pos + by * base_stride + x * 0x10 * base_step;
int dst = dst_pos + by * overlay_stride + x * 0x10 * overlay_step;
2016-07-29 22:10:13 +08:00
for (int bx = 0; bx < 0x10; ++bx)
{
2018-09-07 19:49:12 +08:00
overlay[dst ] = m_base[src ];
overlay[dst+1] = m_base[src+1];
overlay[dst+2] = m_base[src+2];
if (4 == overlay_step)
2016-07-29 22:10:13 +08:00
{
2018-09-07 19:49:12 +08:00
overlay[dst+3] = 4 == base_step ? m_base[src+3] : (byte)0xFF;
2016-07-29 22:10:13 +08:00
}
2018-09-07 19:49:12 +08:00
src += base_step;
dst += overlay_step;
2016-07-29 22:10:13 +08:00
}
}
}
}
2018-09-07 19:49:12 +08:00
return overlay;
2016-07-29 22:10:13 +08:00
}
2023-08-24 05:33:50 +08:00
void BlendOverlay (ImageMetaData overlay_info, byte[] overlay)
{
int ovl_x = overlay_info.OffsetX;
int ovl_y = overlay_info.OffsetY;
int ovl_width = overlay_info.iWidth;
int ovl_height = overlay_info.iHeight;
int base_width = m_info.iWidth;
int base_height = m_info.iHeight;
if (checked(ovl_x + ovl_width) > base_width)
ovl_width = base_width - ovl_x;
if (checked(ovl_y + ovl_height) > base_height)
ovl_height = base_height - ovl_y;
if (ovl_height <= 0 || ovl_width <= 0)
return;
int dst_stride = m_info.iWidth * 4;
int src_stride = overlay_info.iWidth * 4;
int dst = ovl_y * dst_stride + ovl_x * 4;
int src = 0;
int gap = dst_stride - src_stride;
for (int y = 0; y < overlay_info.iHeight; ++y)
{
for (int x = 0; x < overlay_info.iWidth; ++x)
{
byte src_alpha = overlay[src+3];
if (src_alpha != 0)
{
if (0xFF == src_alpha || 0 == m_base[dst+3])
{
m_base[dst] = overlay[src];
m_base[dst+1] = overlay[src+1];
m_base[dst+2] = overlay[src+2];
m_base[dst+3] = src_alpha;
}
else
{
m_base[dst+0] = (byte)((overlay[src+0] * src_alpha
+ m_base[dst+0] * (0xFF - src_alpha)) / 0xFF);
m_base[dst+1] = (byte)((overlay[src+1] * src_alpha
+ m_base[dst+1] * (0xFF - src_alpha)) / 0xFF);
m_base[dst+2] = (byte)((overlay[src+2] * src_alpha
+ m_base[dst+2] * (0xFF - src_alpha)) / 0xFF);
m_base[dst+3] = (byte)Math.Max (src_alpha, m_base[dst+3]);
}
}
dst += 4;
src += 4;
}
dst += gap;
}
}
2016-07-29 22:10:13 +08:00
void ReadBaseImage ()
{
try
{
string dir_name = VFS.GetDirectoryName (m_info.FileName);
string base_name = Path.ChangeExtension (m_info.BaseName, "qnt");
base_name = VFS.CombinePath (dir_name, base_name);
2023-08-24 05:33:50 +08:00
ImageFormat base_format = null;
Func<IBinaryStream, ImageMetaData, IBaseImageReader> create_reader;
if (VFS.FileExists (base_name))
{
base_format = Qnt;
create_reader = (s, m) => new QntFormat.Reader (s.AsStream, (QntMetaData)m);
}
else
{
base_name = Path.ChangeExtension (m_info.BaseName, "pcf");
if (VFS.IsPathEqualsToFileName (m_info.FileName, base_name))
return;
base_name = VFS.CombinePath (dir_name, base_name);
base_format = Dcf;
create_reader = (s, m) => new DcfReader (s, (DcfMetaData)m);
}
2016-10-16 13:22:53 +08:00
using (var base_file = VFS.OpenBinaryStream (base_name))
2016-07-29 22:10:13 +08:00
{
2023-08-24 05:33:50 +08:00
var base_info = base_format.ReadMetaData (base_file);
2016-07-29 22:10:13 +08:00
if (null != base_info && m_info.Width == base_info.Width && m_info.Height == base_info.Height)
{
base_info.FileName = base_name;
2023-08-24 05:33:50 +08:00
var reader = create_reader (base_file, base_info);
2016-07-29 22:10:13 +08:00
reader.Unpack();
m_base_bpp = reader.BPP;
m_base = reader.Data;
}
}
}
catch (Exception X)
{
Trace.WriteLine (X.Message, "[DCF]");
}
}
}
}