Legacy updated.

GRP/RG image format.
improved 'BData' images handling.
YK archives.
'1AWF' archives.
This commit is contained in:
morkt
2023-09-14 18:33:35 +04:00
parent 419a5f4e31
commit 3a11f5fc7a
7 changed files with 1419 additions and 147 deletions

View File

@@ -2,7 +2,7 @@
//! \date 2018 Mar 30
//! \brief BOM image format.
//
// Copyright (C) 2018 by morkt
// Copyright (C) 2018-2023 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,9 @@ using System.ComponentModel.Composition;
using System.IO;
using System.Windows.Media;
// [020705][BOM] Ikihaji@
// [021025][Crystal Vision] Confession ~Hajimete no Kokuhaku~
namespace GameRes.Formats.Bom
{
internal class GrpMetaData : ImageMetaData
@@ -71,8 +74,7 @@ namespace GameRes.Formats.Bom
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var reader = new GrpReader (file, (GrpMetaData)info);
var pixels = reader.Unpack();
return ImageData.Create (info, reader.Format, null, pixels);
return reader.Unpack();
}
public override void Write (Stream file, ImageData image)
@@ -87,24 +89,38 @@ namespace GameRes.Formats.Bom
GrpMetaData m_info;
byte[] m_output;
public PixelFormat Format { get; private set; }
public GrpReader (IBinaryStream file, GrpMetaData info)
{
m_input = file;
m_info = info;
m_output = new byte[m_info.Stride * (int)m_info.Height];
Format = info.BPP == 16 ? PixelFormats.Bgr555
: info.BPP == 24 ? PixelFormats.Bgr24
: info.BPP == 32 ? PixelFormats.Bgra32
: PixelFormats.Gray8;
}
int m_dst;
public byte[] Unpack ()
public ImageData Unpack ()
{
m_input.Position = m_info.DataOffset;
uint size = m_input.ReadUInt32();
if ((size & 0x80000000) != 0)
{
m_input.Read (m_output, 0, m_output.Length);
return m_output;
}
else
{
UnpackLz();
}
return ImageData.Create (m_info, Format, null, m_output, m_info.Stride);
}
void UnpackLz ()
{
Init();
var frame = new byte[0x1000];
for (int i = 0; i < 0xFC0; ++i)
@@ -113,12 +129,11 @@ namespace GameRes.Formats.Bom
m_dst = 0;
while (m_dst < m_output.Length)
{
int ctl = sub_408BE0();
int ctl = GetControlWord();
if (ctl >= 0x100)
{
v5 = 0;
int count = ctl - 0xFD;
int offset = (frame_pos - sub_408E50() - 1) & 0xFFF;
int offset = (frame_pos - GetOffset() - 1) & 0xFFF;
for (int i = 0; i < count; ++i)
{
byte v = frame[(offset + i) & 0xFFF];
@@ -128,125 +143,170 @@ namespace GameRes.Formats.Bom
}
else
{
PutByte (m_dst, ctl);
PutByte (ctl);
frame[frame_pos++ & 0xFFF] = (byte)ctl;
}
}
return m_output;
}
int dword_6FFE54;
int dword_6FF460;
int dword_6FFE54;
int dword_709868;
int dword_703E64;
int m_cached_bits;
int[] dword_6FF464 = new int[636];
int[] dword_703E68 = new int[635];
int[] dword_70986C = new int[953];
void Init ()
{
dword_6FFE54 = -1;
dword_6FF460 = 0;
dword_709868 = 0;
dword_703E64 = 0;
int v0 = 0;
for (int i = 0; i < 318; ++i)
{
dword_6FF464[i] = 1;
dword_70A258[i] = i;
dword_70986C[i + 635] = i;
dword_703E68[i] = i + 635;
}
int v1 = 0;
int v2 = 318;
for (int i = 0; i <= 316; ++i)
{
v4 = dword_6FF464[v1] + dword_6FF468[v1];
dword_70986C[v1 + 1] = v2;
dword_6FF95C[i] = v4;
dword_704360[i] = v1;
int v4 = dword_6FF464[v1] + dword_6FF464[v1 + 1];
dword_70986C[v1] = v2;
dword_70986C[v1 + 1] = v2;
dword_6FF464[i + 318] = v4;
dword_703E68[i + 318] = v1;
v1 += 2;
++v2;
}
dword_70A254 = 0;
dword_70986C[634] = 0;
m_cached_bits = 0;
byte_7137B4 = 0;
m_bits = 0;
dword_7137B0 = 0;
dword_6FFE50 = 0xFFFF;
dword_6FF464[635] = 0xFFFF;
}
int[] dword_704360 = new int[317];
int[] dword_6FF95C = new int[317];
int[] dword_70986C = new int[634];
int sub_408BE0 ()
int GetControlWord ()
{
for (int i = dword_704360[316]; i < 635; i = dword_703E68[GetNextBit() + i])
int ctl;
for (ctl = dword_703E68[634]; ctl < 635; ctl = dword_703E68[GetNextBit() + ctl])
;
int ctl = i - 635;
ctl -= 635;
sub_408C80 (ctl);
return ctl;
}
int GetOffset ()
{
int v0 = GetByte();
int v1 = byte_438D10[v0] - 2;
int v2 = byte_438C10[v0] << 6;
int bits = GetBits (v1);
return v2 | (((v0 << v1) & 0xFF) | (bits & 0xFF)) & 0x3F;
}
static readonly byte[] byte_438C10 = new byte[] {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D, 0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F,
0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11, 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13,
0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15, 0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17,
0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B, 0x1C, 0x1C, 0x1D,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 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
};
int m_bits;
int m_cached_bits;
int GetNextBit ()
{
if (m_cached_bits <= 8)
{
byte v = m_input.ReadUInt8();
m_bits |= (v << (8 - m_cached_bits));
m_cached_bits += 8;
}
FillBitCache();
m_bits <<= 1;
m_cached_bits--;
return (m_bits >> 16) & 1;
}
int GetByte()
{
FillBitCache();
int result = m_bits >> 8;
m_bits <<= 8;
m_cached_bits -= 8;
return result & 0xFF;
}
int GetBits (int n)
{
FillBitCache();
int bits = m_bits << n;
m_cached_bits -= n;
int result = m_bits >> BitShiftTable[n];
m_bits = bits;
return result & BitMaskTable[n];
}
void FillBitCache ()
{
if (m_cached_bits <= 8)
{
int v = m_input.ReadByte();
if (-1 == v)
v = 0;
m_bits |= v << (8 - m_cached_bits);
m_cached_bits += 8;
}
}
static readonly ushort[] BitMaskTable = new ushort[] {
0, 1, 3, 7, 0x0F, 0x1F, 0x3F, 0x7F, 0x0FF, 0x1FF, 0x3FF, 0x7FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x0FFF, 0xFFFF
};
static readonly byte[] BitShiftTable = new byte[] {
0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
};
void sub_408C80 (int a1)
{
unsigned int v2; // ecx@4
unsigned int v3; // esi@4
int *v4; // ecx@5
unsigned int v5; // eax@6
int v6; // eax@7
int v7; // ecx@7
int v8; // eax@7
int v9; // esi@9
if (dword_6FF95C[316] == 0x8000)
if (dword_6FF464[634] == 0x8000)
sub_408D50();
int v1 = dword_70A258[a1];
int v1 = dword_70986C[a1 + 635];
do
{
v2 = dword_6FF464[v1] + 1;
dword_6FF464[v1] = v2;
v3 = v2;
if ( v2 > dword_6FF468[v1] )
int v2 = ++dword_6FF464[v1];
int v3 = v2;
if (v2 > dword_6FF464[v1 + 1])
{
v4 = &dword_6FF46C[v1 + 1];
if ( v3 > dword_6FF46C[v1] )
{
do
{
v5 = *v4;
++v4;
}
while ( v3 > v5 );
}
dword_6FF464[v1] = *(v4 - 2);
*(v4 - 2) = v3;
v6 = v4 - dword_6FF464;
v7 = dword_703E68[v1];
v8 = v6 - 2;
int v4 = v1 + 2; // &dword_6FF464[v1 + 2];
while (v3 > dword_6FF464[v4++])
;
dword_6FF464[v1] = dword_6FF464[v4 - 2];
dword_6FF464[v4 - 2] = v3;
int v6 = v4; // v4 - dword_6FF464;
int v7 = dword_703E68[v1];
int v8 = v6 - 2;
dword_70986C[v7] = v8;
if ( v7 < 635 )
dword_709870[v7] = v8;
v9 = dword_703E68[v8];
if (v7 < 635)
dword_70986C[v7 + 1] = v8;
int v9 = dword_703E68[v8];
dword_703E68[v8] = v7;
dword_70986C[v9] = v1;
if ( v9 < 635 )
dword_709870[v9] = v1;
if (v9 < 635)
dword_70986C[v9 + 1] = v1;
dword_703E68[v1] = v9;
v1 = v8;
}
@@ -257,94 +317,60 @@ namespace GameRes.Formats.Bom
void sub_408D50 ()
{
int v3; // ecx@2
int v4; // edi@3
int v5; // ecx@5
int *v6; // ebx@5
unsigned int v7; // eax@6
int *v8; // edx@6
int v9; // ecx@6
int *v10; // edi@6
unsigned int v11; // ebp@7
int v12; // ecx@8
int *i; // edi@8
int *v14; // eax@10
int *v15; // ecx@10
int v16; // edx@13
int *v17; // ecx@13
int v18; // eax@14
int v19; // [sp+10h] [bp-8h]@5
signed int v20; // [sp+14h] [bp-4h]@5
int v1 = 0;
int v2 = 0;
for (int i = 0; i < 635; ++i)
{
int v3 = dword_703E68[i];
if (v3 >= 635)
{
v4 = dword_6FF464[i];
int v4 = dword_6FF464[i];
dword_703E68[v2] = v3;
dword_6FF464[v2] = (int)((uint)(v4 + 1) >> 1);
dword_6FF464[v2] = (v4 + 1) >> 1;
++v2;
}
}
int v5 = 318;
v19 = 0;
v20 = 318;
v6 = dword_6FF464;
do
int v19 = 0;
int v6 = 0; // dword_6FF464;
int v1 = 0;
while (v1 < 317)
{
v7 = *v6 + v6[1];
v8 = &dword_6FF95C[v1];
v9 = v5 - 1;
v10 = &dword_6FF95C[v1 - 1];
dword_6FF95C[v1] = v7;
if ( v7 < *v10 )
int v7 = dword_6FF464[v6] + dword_6FF464[v6 + 1];
int v9 = v5;
int v10 = v1 + 317; // &dword_6FF464[v1 + 317];
dword_6FF464[v1 + 318] = v7;
while (v7 < dword_6FF464[v10])
{
do
{
v11 = *(v10 - 1);
--v10;
--v9;
}
while ( v7 < v11 );
--v10;
--v9;
}
v12 = v9 + 1;
for ( i = &dword_6FF464[v12]; v8 > i; --v8 )
*v8 = *(v8 - 1);
*i = v7;
v14 = &dword_704360[v1];
v15 = &dword_703E68[v12];
if ( &dword_704360[v1] > v15 )
int p = v9; // &dword_6FF464[v9]
for (int v8 = v1 + 318; v8 > p; --v8)
dword_6FF464[v8] = dword_6FF464[v8 - 1];
dword_6FF464[p] = v7;
int v14 = v1 + 318; // &dword_703E68[v1 + 318];
int v15 = v9; // &dword_703E68[v9];
while (v14 > v15)
{
do
{
*v14 = *(v14 - 1);
--v14;
}
while ( v14 > v15 );
dword_703E68[v14] = dword_703E68[v14 - 1];
--v14;
}
v6 += 2;
*v15 = v19;
++v1;
v5 = v20 + 1;
dword_703E68[v15] = v19;
v19 += 2;
++v20;
++v5;
++v1;
}
while ( v1 < 317 );
v16 = 0;
v17 = dword_703E68;
do
int v16 = 0;
for (int i = 0; i < 635; ++i)
{
v18 = *v17;
if ( *v17 < 635 )
int v18 = dword_703E68[i];
if (v18 < 635)
dword_70986C[v18 + 1] = v16;
dword_70986C[v18] = v16;
++v17;
++v16;
}
while ( (signed int)v17 < (signed int)&unk_704854 );
}
void PutByte (int a1)
@@ -377,7 +403,8 @@ namespace GameRes.Formats.Bom
}
else
{
m_output[m_dst++] = (byte)dword_6FFE54;
if (m_dst < m_output.Length)
m_output[m_dst++] = (byte)dword_6FFE54;
dword_6FFE54 = a1;
}
}