bunch of stuff.

This commit is contained in:
morkt
2023-08-24 01:33:50 +04:00
parent ea096c52ef
commit 77fde27d26
119 changed files with 11078 additions and 619 deletions

View File

@@ -27,6 +27,7 @@ using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Text;
using GameRes.Compression;
namespace GameRes.Formats.AliceSoft
@@ -40,6 +41,16 @@ namespace GameRes.Formats.AliceSoft
public override bool IsHierarchic { get { return true; } }
public override bool CanWrite { get { return false; } }
public AfaOpener ()
{
ContainedFormats = new[] { "QNT", "AJP", "DCF", "OGG" };
Settings = new[] { AfaEncoding };
}
internal readonly EncodingSetting AfaEncoding = new EncodingSetting ("AFAEncodingCP", "DefaultEncoding");
internal Encoding NameEncoding { get { return AfaEncoding.Get<Encoding>(); } }
public override ArcFile TryOpen (ArcView file)
{
if (!file.View.AsciiEqual (8, "AlicArch"))
@@ -54,6 +65,7 @@ namespace GameRes.Formats.AliceSoft
if (!IsSaneCount (count))
return null;
var default_enc = NameEncoding;
var dir = new List<Entry> (count);
var name_buf = new byte[0x40];
using (var input = file.CreateStream (0x2C, packed_size))
@@ -70,7 +82,7 @@ namespace GameRes.Formats.AliceSoft
name_buf = new byte[index_step];
if (index_step != index.Read (name_buf, 0, index_step))
return null;
var name = Encodings.cp932.GetString (name_buf, 0, name_length);
var name = default_enc.GetString (name_buf, 0, name_length);
var entry = FormatCatalog.Instance.Create<Entry> (name);
index.ReadInt32();
index.ReadInt32();

View File

@@ -2,7 +2,7 @@
//! \date Fri Jul 29 14:07:15 2016
//! \brief AliceSoft incremental image format.
//
// Copyright (C) 2016-2018 by morkt
// Copyright (C) 2016-2022 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
@@ -27,6 +27,7 @@ using System;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Media;
using GameRes.Compression;
using GameRes.Utility;
@@ -37,6 +38,15 @@ namespace GameRes.Formats.AliceSoft
{
public string BaseName;
public long DataOffset;
public bool IsPcf;
}
internal interface IBaseImageReader
{
int BPP { get; }
byte[] Data { get; }
void Unpack ();
}
[Export(typeof(ImageFormat))]
@@ -46,17 +56,25 @@ namespace GameRes.Formats.AliceSoft
public override string Description { get { return "AliceSoft System incremental image"; } }
public override uint Signature { get { return 0x20666364; } } // 'dcf '
public DcfFormat ()
{
Extensions = new[] { "dcf", "pcf" };
Signatures = new[] { 0x20666364u, 0x20666370u };
}
static readonly ResourceInstance<AfaOpener> Afa = new ResourceInstance<AfaOpener> ("AFA");
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
stream.Seek (4, SeekOrigin.Current);
uint header_size = stream.ReadUInt32();
long data_pos = stream.Position + header_size;
if (stream.ReadInt32() != 1)
var header = stream.ReadHeader (0x1C);
uint header_size = header.ToUInt32 (4);
long data_pos = 8 + header_size;
if (header.ToInt32 (8) != 1)
return null;
uint width = stream.ReadUInt32();
uint height = stream.ReadUInt32();
int bpp = stream.ReadInt32();
int name_length = stream.ReadInt32();
uint width = header.ToUInt32 (0x0C);
uint height = header.ToUInt32 (0x10);
int bpp = header.ToInt32 (0x14);
int name_length = header.ToInt32 (0x18);
if (name_length <= 0)
return null;
int shift = (name_length % 7) + 1;
@@ -70,18 +88,17 @@ namespace GameRes.Formats.AliceSoft
Width = width,
Height = height,
BPP = bpp,
BaseName = Encodings.cp932.GetString (name_bits),
BaseName = Afa.Value.NameEncoding.GetString (name_bits),
DataOffset = data_pos,
IsPcf = stream.Signature == 0x20666370u,
};
}
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
using (var reader = new DcfReader (stream, (DcfMetaData)info))
{
reader.Unpack();
return ImageData.Create (info, reader.Format, null, reader.Data, reader.Stride);
}
var reader = new DcfReader (stream, (DcfMetaData)info);
reader.Unpack();
return ImageData.Create (reader.Info, reader.Format, null, reader.Data, reader.Stride);
}
public override void Write (Stream file, ImageData image)
@@ -90,7 +107,7 @@ namespace GameRes.Formats.AliceSoft
}
}
internal sealed class DcfReader : IDisposable
internal sealed class DcfReader : IBaseImageReader
{
IBinaryStream m_input;
DcfMetaData m_info;
@@ -100,10 +117,14 @@ namespace GameRes.Formats.AliceSoft
int m_overlay_bpp;
int m_base_bpp;
static readonly Lazy<ImageFormat> s_QntFormat = new Lazy<ImageFormat> (() => ImageFormat.FindByTag ("QNT"));
static readonly ResourceInstance<ImageFormat> s_QntFormat = new ResourceInstance<ImageFormat> ("QNT");
static readonly ResourceInstance<ImageFormat> s_DcfFormat = new ResourceInstance<ImageFormat> ("DCF");
internal ImageFormat Qnt { get { return s_QntFormat.Value; } }
internal ImageFormat Dcf { get { return s_DcfFormat.Value; } }
public int BPP { get { return m_base_bpp; } }
public ImageMetaData Info { get; private set; }
public byte[] Data { get { return m_output; } }
public PixelFormat Format { get; private set; }
public int Stride { get; private set; }
@@ -112,10 +133,13 @@ namespace GameRes.Formats.AliceSoft
{
m_input = input;
m_info = info;
Info = info;
}
public void Unpack ()
{
int pt_x = 0;
int pt_y = 0;
long next_pos = m_info.DataOffset;
for (;;)
{
@@ -131,7 +155,12 @@ namespace GameRes.Formats.AliceSoft
using (var input = new ZLibStream (m_input.AsStream, CompressionMode.Decompress, true))
input.Read (m_mask, 0, unpacked_size);
}
else if (0x64676364 == id) // 'dcgd'
else if (0x6C647470 == id) // 'ptdl'
{
pt_x = m_input.ReadInt32();
pt_y = m_input.ReadInt32();
}
else if (0x64676364 == id || 0x64676370 == id) // 'dcgd' || 'pcgd'
break;
}
long qnt_pos = m_input.Position;
@@ -147,18 +176,28 @@ namespace GameRes.Formats.AliceSoft
var overlay = new QntFormat.Reader (reg, qnt_info);
overlay.Unpack();
m_overlay_bpp = overlay.BPP;
if (m_mask != null)
if (m_mask != null || m_info.IsPcf)
ReadBaseImage();
if (m_base != null)
if (m_info.IsPcf)
{
m_output = ApplyOverlay (overlay.Data);
SetFormat ((int)m_info.Width, m_overlay_bpp);
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);
}
else
{
m_output = overlay.Data;
SetFormat ((int)qnt_info.Width, m_overlay_bpp);
SetFormat (qnt_info.iWidth, m_overlay_bpp);
}
}
}
@@ -169,14 +208,20 @@ namespace GameRes.Formats.AliceSoft
Stride = width * (bpp / 8);
}
byte[] ApplyOverlay (byte[] overlay)
void SetEmptyBase ()
{
int blocks_x = (int)m_info.Width / 0x10;
int blocks_y = (int)m_info.Height / 0x10;
m_base_bpp = 32;
m_base = new byte[m_info.Width * m_info.Height * 4];
}
byte[] MaskOverlay (byte[] overlay)
{
int blocks_x = m_info.iWidth / 0x10;
int blocks_y = m_info.iHeight / 0x10;
int base_step = m_base_bpp / 8;
int overlay_step = m_overlay_bpp / 8;
int base_stride = (int)m_info.Width * base_step;
int overlay_stride = (int)m_info.Width * overlay_step;
int base_stride = m_info.iWidth * base_step;
int overlay_stride = m_info.iWidth * overlay_step;
int mask_pos = 4;
for (int y = 0; y < blocks_y; ++y)
{
@@ -208,6 +253,58 @@ namespace GameRes.Formats.AliceSoft
return overlay;
}
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;
}
}
void ReadBaseImage ()
{
try
@@ -215,13 +312,29 @@ namespace GameRes.Formats.AliceSoft
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);
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);
}
using (var base_file = VFS.OpenBinaryStream (base_name))
{
var base_info = Qnt.ReadMetaData (base_file) as QntMetaData;
var base_info = base_format.ReadMetaData (base_file);
if (null != base_info && m_info.Width == base_info.Width && m_info.Height == base_info.Height)
{
base_info.FileName = base_name;
var reader = new QntFormat.Reader (base_file.AsStream, base_info);
var reader = create_reader (base_file, base_info);
reader.Unpack();
m_base_bpp = reader.BPP;
m_base = reader.Data;
@@ -233,11 +346,5 @@ namespace GameRes.Formats.AliceSoft
Trace.WriteLine (X.Message, "[DCF]");
}
}
#region IDisposable Members
public void Dispose ()
{
}
#endregion
}
}

View File

@@ -97,7 +97,7 @@ namespace GameRes.Formats.AliceSoft
return ImageData.Create (info, format, null, reader.Data, stride);
}
internal class Reader
internal class Reader : IBaseImageReader
{
byte[] m_input;
byte[] m_alpha;