(Legacy): bunch of formats.

(GSX): K5 archives + K4 images.
(HyperWorks): G images.
(IDA): better support packed entries.
(Logg): ARF, MBM archives, FRM images.
(Omi): DAT archives.
(Rare): X archives.
(RHSS): 'CRG' archives.
(SplushWave): better SWG images support.
This commit is contained in:
morkt
2023-09-07 12:47:22 +04:00
parent 8b23273fa9
commit 419a5f4e31
15 changed files with 1564 additions and 107 deletions

View File

@@ -27,6 +27,9 @@ using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Windows.Media;
// [030817][Splush Wave] Knock Out -Taisengata Datsui Mahjong-
namespace GameRes.Formats.SplushWave
{
@@ -129,5 +132,92 @@ namespace GameRes.Formats.SplushWave
}
return new BinMemoryStream (output, 0, dst);
}
public override IImageDecoder OpenImage (ArcFile arc, Entry entry)
{
var fent = (FlkEntry)entry;
var input = BinaryStream.FromStream (OpenEntry (arc, fent), fent.Name);
if ((fent.Flags & 0x10) == 0)
return ImageFormatDecoder.Create (input);
try
{
var info = Swg.ReadMetaData (input) as SwgMetaData;
if (null == info)
{
input.Position = 0;
return new ImageFormatDecoder(input);
}
return new Swg1ImageDecoder (input, info);
}
catch
{
input.Dispose();
throw;
}
}
static readonly ResourceInstance<SwgFormat> s_swg = new ResourceInstance<SwgFormat> ("SWG");
internal static SwgFormat Swg { get => s_swg.Value; }
}
internal sealed class Swg1ImageDecoder : BinaryImageDecoder
{
SwgMetaData m_info;
public Swg1ImageDecoder (IBinaryStream input, SwgMetaData info) : base (input, info)
{
SourceFormat = DatOpener.Swg;
m_info = info;
}
static readonly byte[] PlaneMap = { 3, 2, 1, 0 };
protected override ImageData GetImageData ()
{
m_input.Position = m_info.DataOffset;
int stride = 4 * m_info.iWidth;
int plane_size = m_info.iWidth * m_info.iHeight;
var output = new byte[stride * m_info.iHeight];
ushort[] ctl_buf = new ushort[m_info.iHeight];
for (int c = 0; c < 4; ++c)
{
int compress_method = ReadU16BE();
if (0 == compress_method)
{
int dst = PlaneMap[c] + stride * (m_info.iHeight - 1);
for (int y = 0; y < m_info.iHeight; ++y)
{
for (int x = 0; x < stride; x += 4)
{
output[dst+x] = m_input.ReadUInt8();
}
dst -= stride;
}
continue;
}
if (compress_method != 1)
throw new InvalidFormatException();
for (int y = 0; y < m_info.iHeight; ++y)
{
ctl_buf[y] = ReadU16BE();
}
int row = PlaneMap[c];
for (int y = 0; y < m_info.iHeight; ++y)
{
int dst = row;
int row_size = ctl_buf[y];
SwgFormat.DecompressRow (m_input, row_size, output, dst, 4);
row += stride;
}
}
return ImageData.Create (m_info, PixelFormats.Bgra32, null, output, stride);
}
ushort ReadU16BE ()
{
ushort le = m_input.ReadUInt16();
return (ushort)(le >> 8 | le << 8);
}
}
}

View File

@@ -68,7 +68,7 @@ namespace GameRes.Formats.SplushWave
{
var meta = (SwgMetaData)info;
PixelFormat format = meta.BPP == 8 ? PixelFormats.Indexed8
: meta.BPP == 32 ? PixelFormats.Bgr32 : PixelFormats.Bgr24;
: meta.BPP == 32 ? PixelFormats.Bgra32 : PixelFormats.Bgr24;
BitmapPalette palette = null;
if (meta.BPP == 8)
{
@@ -77,36 +77,40 @@ namespace GameRes.Formats.SplushWave
}
int stride = meta.iWidth * meta.BPP / 8;
file.Position = meta.DataOffset;
// var pixels = new byte[stride * meta.iHeight];
var pixels = new byte[4 * meta.iWidth * meta.iHeight];
var pixels = new byte[stride * meta.iHeight];
if (!meta.IsCompressed)
{
file.Read (pixels, 0, pixels.Length);
return ImageData.CreateFlipped (meta, format, palette, pixels, stride);
}
var input = file.ReadBytes ((int)(file.Length - file.Position));
if (!Decompress (input, pixels, meta.Depth + 2, meta.iWidth, meta.iHeight))
if (!Decompress (file, pixels, meta.Depth + 2, meta.iWidth, meta.iHeight))
throw new InvalidFormatException ("Invalid SWG file.");
return ImageData.CreateFlipped (meta, format, palette, pixels, stride);
}
bool Decompress (byte[] input, byte[] output, int channels, int width, int height)
static readonly byte[] PlaneMap = { 2, 1, 0, 3 };
bool Decompress (IBinaryStream input, byte[] output, int channels, int width, int height)
{
int src = 0;
if (input[0] != 0 || input[1] != 1)
long start_pos = input.Position;
byte hi = input.ReadUInt8();
byte lo = input.ReadUInt8();
if (hi != 0 || lo != 1)
{
input.Position = start_pos;
int n = 0;
for (int i = 0; i < channels; ++i)
{
if (0 == input[i])
if (0 == input.ReadByte())
++n;
}
if (n != channels)
return false;
src = 4;
input.Position = start_pos + 4;
hi = input.ReadUInt8();
lo = input.ReadUInt8();
}
int compress_method = input[src+1] + (input[src] << 8);
src += 2;
int compress_method = lo | hi << 8;
if (0 == compress_method)
{
for (int i = 0; i < channels; ++i)
@@ -115,7 +119,7 @@ namespace GameRes.Formats.SplushWave
int count = height * width;
while (count --> 0)
{
output[pos] = input[src++];
output[pos] = input.ReadUInt8();
pos += channels;
}
}
@@ -123,63 +127,57 @@ namespace GameRes.Formats.SplushWave
}
if (compress_method != 1)
return false;
int dst = 0;
int v33 = src;
int v37 = height * channels;
src += 2 * v37;
for (int row = 0; row < v37; ++row)
int stride = width * channels;
var row_sizes = input.ReadBytes (2 * height * channels);
int ctl_pos = 0;
for (int c = 0; c < channels; ++c)
for (int y = height - 1; y >= 0; --y)
{
int y = row % height;
dst = channels * (width * (height - y - 1) + 1) - row / height - 1;
if (dst > output.Length)
return true;
int v24 = 0;
int v36 = input[v33+1] + (input[v33] << 8);
v33 += 2;
do
{
byte lo = input[src];
byte hi = input[src+1];
if (lo != 0)
{
if (lo < 0x81)
{
++src;
int count = lo + 1;
v24 += count + 1;
while (count --> 0)
{
output[dst] = input[src++];
dst += channels;
}
}
else
{
src += 2;
v24 += 2;
int count = Math.Min (0x101 - lo, output.Length - dst);
while (count --> 0)
{
output[dst] = hi;
dst += channels;
}
}
}
else
{
src += 2;
v24 += 2;
output[dst] = hi;
dst += channels;
}
if (dst >= output.Length)
return true;
}
while (v24 < v36);
int dst = stride * y + PlaneMap[c];
int row_size = row_sizes[ctl_pos+1] | row_sizes[ctl_pos] << 8;
ctl_pos += 2;
DecompressRow (input, row_size, output, dst, channels);
}
return true;
}
internal static void DecompressRow (IBinaryStream input, int row_size, byte[] output, int dst, int step)
{
int x = 0;
while (x < row_size)
{
byte ctl = input.ReadUInt8();
if (ctl == 0)
{
byte v = input.ReadUInt8();
x += 2;
output[dst] = v;
dst += step;
}
else if (ctl < 0x81u)
{
int count = ctl + 1;
x += count + 1;
while (count --> 0)
{
output[dst] = input.ReadUInt8();
dst += step;
}
}
else
{
byte v = input.ReadUInt8();
x += 2;
int count = 0x101 - ctl;
while (count --> 0)
{
output[dst] = v;
dst += step;
}
}
}
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("SwgFormat.Write not implemented");