updated legacy formats.

(BIZ): moved to Adviz folder.
(GIZ, GIZ2, BIZ2, PR1): new PC-98 image formats.
(DATA, BGM, SED): MyHarvest resource formats.
(HTF): compressed image format.
(PAK): Mina resource archives.
(GCmp): read palette from external resource.
(NCG): Nekotaro image format.
(BND, TCZ, TSZ): Ponytail Soft PC-98 formats.
(NOR): Sophia resource archive.
(SDA, PLA): Squadra D resource archives.
(UCA): added checks to avoid false positives.
This commit is contained in:
morkt
2023-10-11 19:03:45 +04:00
parent 4d26cdcff6
commit f90e2e851c
24 changed files with 3504 additions and 279 deletions

366
Legacy/Adviz/ImageBIZ.cs Normal file
View File

@@ -0,0 +1,366 @@
//! \file ImageBIZ.cs
//! \date 2023 Sep 30
//! \brief ADVIZ engine image format.
//
// Copyright (C) 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
// 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.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Media;
using System.Windows.Media.Imaging;
// [970829][Ange] Coin
namespace GameRes.Formats.Adviz
{
[Export(typeof(ImageFormat))]
public class BizFormat : ImageFormat
{
public override string Tag => "BIZ";
public override string Description => "ADVIZ engine image format";
public override uint Signature => 0;
const byte DefaultKey = 0x39;
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
if (!file.Name.HasExtension (".BIZ"))
return null;
var header = file.ReadHeader (4);
uint width = header.ToUInt16 (0);
uint height = header.ToUInt16 (2);
if (width * height + 4 != file.Length)
return null;
return new ImageMetaData {
Width = width,
Height = height,
BPP = 8,
};
}
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var palette = ReadPalette (file.Name, 0x300, (pal, off) => ReadPalette (pal, off, 0x100, PaletteFormat.Rgb));
if (null == palette)
throw new FileNotFoundException ("Unable to retrieve palette.");
file.Position = 4;
var pixels = file.ReadBytes (info.iWidth * info.iHeight);
byte key = DefaultKey;
for (int i = 0; i < pixels.Length; ++i)
{
pixels[i] ^= key;
key += pixels[i];
}
return ImageData.CreateFlipped (info, PixelFormats.Indexed8, palette, pixels, info.iWidth);
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("BizFormat.Write not implemented");
}
internal delegate BitmapPalette PaletteReader (ArcView file, int offset);
static readonly Regex TachieRe = new Regex (@"^(T[^._]+_)[2-9][^.]*\.GIZ$", RegexOptions.Compiled);
internal static BitmapPalette ReadPalette (string base_name, int pal_size, PaletteReader read_pal)
{
var dir_name = Path.GetDirectoryName (base_name);
var grp_tbl_name = Path.Combine (dir_name, @"..\GRP_TBL.SYS");
var plt_tbl_name = Path.Combine (dir_name, @"..\PLT_TBL.SYS");
if (!File.Exists (grp_tbl_name) || !File.Exists (plt_tbl_name))
return null;
int index = 0;
uint grp_size = 0;
base_name = Path.GetFileName (base_name).ToUpperInvariant();
var name = base_name;
var ext = Path.GetExtension (name).TrimStart('.');
var match = TachieRe.Match (name);
if (match.Success)
name = match.Groups[1].Value + "1";
else
name = Path.GetFileNameWithoutExtension (name);
if (name.Length < 8)
name += ' ';
using (var grp = new ArcView (grp_tbl_name))
{
grp_size = (uint)grp.MaxOffset;
int pos = 0;
while (pos + 12 <= grp.MaxOffset)
{
if (grp.View.AsciiEqual (pos, name) &&
grp.View.AsciiEqual (pos+8, ext))
{
break;
}
++index;
pos += 12;
}
if (pos >= grp.MaxOffset)
return null;
}
using (var pal = new ArcView (plt_tbl_name))
{
uint plt_size = (uint)pal.MaxOffset;
var id = new GrpIdentifier (grp_size, plt_size);
IGrpMapper mapper;
if (!GrpMap.TryGetValue (id, out mapper))
mapper = new DirectMapper();
index = mapper.GetPaletteIndex (index, base_name);
int pal_offset = index * pal_size;
if (pal_offset + pal_size > pal.MaxOffset)
{
int count = (int)(pal.MaxOffset / pal_size) - 1;
pal_offset = count * pal_size;
}
return read_pal (pal, pal_offset);
}
}
static readonly Dictionary<GrpIdentifier, IGrpMapper> GrpMap = new Dictionary<GrpIdentifier, IGrpMapper> {
{ new GrpIdentifier (1584, 139008), new GrpShiftMapper (52) },
{ new GrpIdentifier (2160, 12288),
new GrpNameMapper { NameMap = new Dictionary<string, int> {
{ "BG01.BIZ", 14 },
{ "BG02.BIZ", 8 },
{ "BG03.BIZ", 8 },
{ "BG04.BIZ", 8 },
{ "BG05.BIZ", 8 },
{ "BG06.BIZ", 8 },
{ "BG07.BIZ", 8 },
{ "BG08.BIZ", 8 },
{ "BG09.BIZ", 8 },
{ "BG10.BIZ", 8 },
{ "BG11.BIZ", 8 },
{ "BG12.BIZ", 8 },
{ "BG13.BIZ", 8 },
{ "BG14.BIZ", 8 },
{ "BG15.BIZ", 8 },
{ "BG16.BIZ", 8 },
{ "BG17.BIZ", 8 },
{ "BG18.BIZ", 8 },
{ "BG19.BIZ", 8 },
{ "CA01.BIZ", 12 },
{ "CA02.BIZ", 12 },
{ "CA03.BIZ", 4 },
{ "CA04.BIZ", 8 },
{ "CA05.BIZ", 8 },
{ "CA06.BIZ", 8 },
{ "CA07.BIZ", 8 },
{ "CA08.BIZ", 8 },
{ "CA09.BIZ", 8 },
{ "CA10.BIZ", 8 },
{ "CA11.BIZ", 8 },
{ "CA12.BIZ", 8 },
{ "CA13.BIZ", 8 },
{ "CA14.BIZ", 8 },
{ "CA15.BIZ", 8 },
{ "CA16.BIZ", 8 },
{ "CA17.BIZ", 8 },
{ "CA18.BIZ", 8 },
{ "CA19.BIZ", 8 },
{ "CA20.BIZ", 8 },
{ "CA21.BIZ", 8 },
{ "CA22.BIZ", 8 },
{ "CA23.BIZ", 8 },
{ "CA24.BIZ", 8 },
{ "CA25.BIZ", 8 },
{ "CA26.BIZ", 8 },
{ "CA27.BIZ", 8 },
{ "CA28.BIZ", 8 },
{ "CA29.BIZ", 8 },
{ "CA30.BIZ", 8 },
{ "CA31.BIZ", 8 },
{ "CA32.BIZ", 8 },
{ "CA33.BIZ", 8 },
{ "CA34.BIZ", 8 },
{ "CA35.BIZ", 8 },
{ "CA36.BIZ", 8 },
{ "CA37.BIZ", 8 },
{ "CA38.BIZ", 8 },
{ "CA39.BIZ", 8 },
{ "CA40.BIZ", 8 },
{ "CA41.BIZ", 8 },
{ "CA42.BIZ", 8 },
{ "CA43.BIZ", 8 },
{ "CA44.BIZ", 8 },
{ "CA45.BIZ", 8 },
{ "CA46.BIZ", 8 },
{ "CA47.BIZ", 8 },
{ "CA48.BIZ", 8 },
{ "CA49.BIZ", 8 },
{ "CA50.BIZ", 8 },
{ "CA51.BIZ", 8 },
{ "CA52.BIZ", 8 },
{ "CA53.BIZ", 8 },
{ "CA54.BIZ", 8 },
{ "CA55.BIZ", 8 },
{ "CA56.BIZ", 8 },
{ "CA57.BIZ", 8 },
{ "CA58.BIZ", 8 },
{ "CA59.BIZ", 8 },
{ "CA60.BIZ", 8 },
{ "E02.BIZ", 8 },
{ "E03.BIZ", 8 },
{ "E04.BIZ", 8 },
{ "E05.BIZ", 8 },
{ "E06.BIZ", 8 },
{ "E07.BIZ", 8 },
{ "E08.BIZ", 8 },
{ "E09.BIZ", 8 },
{ "E10.BIZ", 8 },
{ "E11.BIZ", 8 },
{ "E12.BIZ", 8 },
{ "E13.BIZ", 8 },
{ "E14.BIZ", 8 },
{ "E15.BIZ", 8 },
{ "E16.BIZ", 8 },
{ "E17.BIZ", 8 },
{ "E18.BIZ", 8 },
{ "END.BIZ", 6 },
{ "IPL.BIZ", 12 },
{ "S01.BIZ", 8 },
{ "S02.BIZ", 8 },
{ "S03.BIZ", 8 },
{ "S04.BIZ", 8 },
{ "S05.BIZ", 8 },
{ "S06.BIZ", 8 },
{ "S07.BIZ", 8 },
{ "S08.BIZ", 8 },
{ "S09.BIZ", 8 },
{ "S10.BIZ", 8 },
{ "S11.BIZ", 8 },
{ "S12.BIZ", 8 },
{ "S13.BIZ", 8 },
{ "S14.BIZ", 8 },
{ "S15.BIZ", 8 },
{ "S16.BIZ", 8 },
{ "S17.BIZ", 8 },
{ "S18.BIZ", 8 },
{ "S19.BIZ", 8 },
{ "S20.BIZ", 8 },
{ "S21.BIZ", 8 },
{ "S22.BIZ", 8 },
{ "S23.BIZ", 8 },
{ "S24.BIZ", 8 },
{ "S25.BIZ", 8 },
{ "S26.BIZ", 8 },
{ "S27.BIZ", 8 },
{ "S28.BIZ", 8 },
{ "S29.BIZ", 8 },
{ "S30.BIZ", 8 },
{ "S31.BIZ", 8 },
{ "S32.BIZ", 8 },
{ "S33.BIZ", 8 },
{ "S34.BIZ", 8 },
{ "S35.BIZ", 8 },
{ "S36.BIZ", 8 },
{ "S37.BIZ", 8 },
{ "S38.BIZ", 8 },
{ "S39.BIZ", 8 },
{ "S40.BIZ", 8 },
{ "S41.BIZ", 8 },
{ "S42.BIZ", 8 },
{ "S43.BIZ", 8 },
{ "S44.BIZ", 8 },
{ "S45.BIZ", 8 },
{ "S46.BIZ", 8 },
{ "S47.BIZ", 8 },
{ "S48.BIZ", 8 },
{ "S49.BIZ", 8 },
{ "T01.BIZ", 8 },
{ "T02.BIZ", 8 },
{ "T03.BIZ", 8 },
{ "WAKU1.BIZ", 8 },
{ "WAKU2.BIZ", 8 },
} } },
};
}
public struct GrpIdentifier
{
public uint GrpSize;
public uint PltSize;
public GrpIdentifier (uint grp_size, uint plt_size)
{
GrpSize = grp_size;
PltSize = plt_size;
}
public override int GetHashCode ()
{
return (int)((GrpSize + 1) * (PltSize + 1));
}
public override bool Equals (object obj)
{
if (null == obj)
return false;
var other = (GrpIdentifier)obj;
return this.GrpSize == other.GrpSize && this.PltSize == other.PltSize;
}
}
internal interface IGrpMapper
{
int GetPaletteIndex (int id, string name);
}
internal class DirectMapper : IGrpMapper
{
public int GetPaletteIndex (int id, string name)
{
return id;
}
}
internal class GrpShiftMapper : IGrpMapper
{
int m_shift;
public GrpShiftMapper (int shift)
{
m_shift = shift;
}
public int GetPaletteIndex (int id, string name)
{
return id + m_shift;
}
}
internal class GrpNameMapper : IGrpMapper
{
public Dictionary<string, int> NameMap;
public int GetPaletteIndex (int id, string name)
{
int index;
if (NameMap.TryGetValue (name, out index))
return index;
return id;
}
}
}

92
Legacy/Adviz/ImageBIZ2.cs Normal file
View File

@@ -0,0 +1,92 @@
//! \file ImageBIZ2.cs
//! \date 2018 Feb 11
//! \brief ADVIZ engine compressed image.
//
// Copyright (C) 2018 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.ComponentModel.Composition;
using System.IO;
using System.Windows.Media;
using GameRes.Compression;
// [000225][Sorciere] Karei
// [011012][Ange] Nyuunyuu
namespace GameRes.Formats.Adviz
{
[Export(typeof(ImageFormat))]
public class Biz2Format : ImageFormat
{
public override string Tag { get { return "BIZ/2"; } }
public override string Description { get { return "ADVIZ engine compressed image"; } }
public override uint Signature { get { return 0x325A4942; } } // 'BIZ2'
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
var header = file.ReadHeader (8);
return new ImageMetaData {
Width = header.ToUInt16 (4),
Height = header.ToUInt16 (6),
BPP = 24,
};
}
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
file.Position = 8;
using (var lzss = new LzssStream (file.AsStream, LzssMode.Decompress, true))
using (var input = new BinaryStream (lzss, file.Name))
{
int stride = info.iWidth * 3;
var rgb = new byte[stride * info.Height];
if (rgb.Length != input.Read (rgb, 0, rgb.Length))
throw new InvalidFormatException();
if (input.PeekByte() != -1) // possible alpha channel
{
var alpha = input.ReadBytes (rgb.Length);
if (alpha.Length == rgb.Length)
{
int stride32bpp = info.iWidth * 4;
var rgba = new byte[stride32bpp * info.iHeight];
int src = 0;
int dst = 0;
while (src < rgb.Length)
{
rgba[dst++] = rgb[src ];
rgba[dst++] = rgb[src+1];
rgba[dst++] = rgb[src+2];
rgba[dst++] = alpha[src]; // presumably it's grayscale and R/G/B values are equal
src += 3;
}
return ImageData.CreateFlipped (info, PixelFormats.Bgra32, null, rgba, stride32bpp);
}
}
return ImageData.CreateFlipped (info, PixelFormats.Bgr24, null, rgb, stride);
}
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("BizFormat.Write not implemented");
}
}
}

309
Legacy/Adviz/ImageGIZ.cs Normal file
View File

@@ -0,0 +1,309 @@
//! \file ImageGIZ.cs
//! \date 2023 Oct 02
//! \brief ADVIZ engine image format (PC-98).
//
// Copyright (C) 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
// 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 GameRes.Utility;
using System.ComponentModel.Composition;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
// [960830][Ange] Coin
namespace GameRes.Formats.Adviz
{
internal class GizMetaData : ImageMetaData
{
public byte RleCode;
public byte PlaneMap;
public bool HasPalette;
}
[Export(typeof(ImageFormat))]
public class Giz3Format : ImageFormat
{
public override string Tag => "GIZ";
public override string Description => "ADVIZ engine image format";
public override uint Signature => 0x335A4947; // 'GIZ3'
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
var header = file.ReadHeader (0x10);
int xy = header.ToUInt16 (4);
return new GizMetaData {
Width = (uint)header.ToUInt16 (6) << 3,
Height = header.ToUInt16 (8),
OffsetX = (xy % 0x50) << 3,
OffsetY = xy / 0x50,
HasPalette = header[0xC] != 0,
PlaneMap = header[0xE],
BPP = 4,
};
}
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var reader = new Giz3Reader (file, (GizMetaData)info);
return reader.Unpack();
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("Giz3Format.Write not implemented");
}
}
internal class Giz3Reader
{
IBinaryStream m_input;
GizMetaData m_info;
BitmapPalette m_palette;
int m_stride;
int m_output_stride;
public Giz3Reader (IBinaryStream input, GizMetaData info)
{
m_input = input;
m_info = info;
}
byte[] m_buffer;
byte[] m_output;
public ImageData Unpack ()
{
m_input.Position = 0x10;
if (m_info.HasPalette)
m_palette = ReadPalette();
else
m_palette = BitmapPalettes.Gray16; // palette is stored somewhere else
long data_pos = m_input.Position;
ReadHuffmanTree();
data_pos += m_dataOffset;
m_bitCount = 1;
m_stride = m_info.iWidth >> 3;
m_buffer = new byte[0x2000];
m_output_stride = m_info.iWidth >> 1;
m_output = new byte[m_output_stride * m_info.iHeight];
m_input.Position = data_pos;
UnpackBits();
return ImageData.Create (m_info, PixelFormats.Indexed4, m_palette, m_output, m_output_stride);
}
int m_dataOffset;
int m_gizColumn;
int m_outputPos1;
int m_outputPos2;
void UnpackBits () // sub_15426
{
m_gizColumn = 0;
m_outputPos1 = 0;
m_outputPos2 = 0;
int dst = 0;
for (int x = 0; x < m_stride; ++x)
{
int src1 = m_outputPos1;
int src2 = 0;
for (int j = 0; j < 2; ++j)
{
src2 = m_outputPos1;
int plane_mask = 1;
for (int i = 0; i < 4; ++i)
{
if ((m_info.PlaneMap & plane_mask) == 0)
{
UnpackPlane (m_outputPos1);
}
plane_mask <<= 1;
m_outputPos1 += 0x800;
m_outputPos2 += 0x800;
}
m_gizColumn = (m_gizColumn + 1) & 3;
m_outputPos1 = m_gizColumn << 9;
m_outputPos2 = 0;
}
CopyPlanes (src1, src2, dst);
dst += 4;
}
}
void CopyPlanes (int src1, int src2, int dst)
{
for (int y = 0; y < m_info.iHeight; ++y)
{
int b0 = m_buffer[src1+y ] << 4 | m_buffer[src2+y ];
int b1 = m_buffer[src1+y+0x0800] << 4 | m_buffer[src2+y+0x0800];
int b2 = m_buffer[src1+y+0x1000] << 4 | m_buffer[src2+y+0x1000];
int b3 = m_buffer[src1+y+0x1800] << 4 | m_buffer[src2+y+0x1800];
for (int j = 0; j < 8; j += 2)
{
byte px = (byte)((((b0 << j) & 0x80) >> 3)
| (((b1 << j) & 0x80) >> 2)
| (((b2 << j) & 0x80) >> 1)
| (((b3 << j) & 0x80) ));
px |= (byte)((((b0 << j) & 0x40) >> 6)
| (((b1 << j) & 0x40) >> 5)
| (((b2 << j) & 0x40) >> 4)
| (((b3 << j) & 0x40) >> 3));
m_output[dst+j/2] = px;
}
dst += m_output_stride;
}
}
int m_root;
ushort[] m_treeTable;
void ReadHuffmanTree ()
{
m_dataOffset = m_input.ReadUInt16();
m_treeTable = new ushort[(m_dataOffset-2) * 2 / 3 + 1];
int di = 0;
for (int si = 2; si + 2 < m_dataOffset; si += 3)
{
ushort bx = m_input.ReadUInt16();
int ax = bx & 0xFFF;
if ((ax & 0x800) == 0)
ax = (ax - 2) >> 1;
m_treeTable[di++] = (ushort)ax;
ax = m_input.ReadUInt8() << 4;
ax |= bx >> 12;
if ((ax & 0x800) == 0)
ax = (ax - 2) >> 1;
m_treeTable[di++] = (ushort)ax;
}
m_root = di - 2;
}
byte ReadToken ()
{
int token = m_root;
do
{
if (GetNextBit())
++token;
token = m_treeTable[token];
}
while ((token & 0x800) == 0);
return (byte)token;
}
void UnpackPlane (int dst)
{
int y = 0;
while (y < m_info.iHeight)
{
byte ctl = ReadToken();
if (ctl < 0x10)
{
m_buffer[dst++] = ctl;
++y;
}
else
{
int count = ReadToken() + 2;
ctl -= 0x10;
switch (ctl)
{
case 0:
for (int i = 0; i < count; ++i)
m_buffer[dst+i] = 0;
break;
case 1:
for (int i = 0; i < count; ++i)
m_buffer[dst+i] = 0xF;
break;
case 2:
Binary.CopyOverlapped (m_buffer, dst-1, dst, count);
break;
case 3:
Binary.CopyOverlapped (m_buffer, dst-2, dst, count);
break;
case 4:
case 5:
case 6:
{
int off = (ctl - 3) << 11;
Binary.CopyOverlapped (m_buffer, dst-off, dst, count);
break;
}
case 7:
{
int src = dst - m_outputPos1;
int ax = (m_gizColumn - 1) & 3;
src += (ax << 9) + m_outputPos2;
Binary.CopyOverlapped (m_buffer, src, dst, count);
break;
}
case 8:
{
int src = dst - m_outputPos1;
int ax = (m_gizColumn - 2) & 3;
src += (ax << 9) + m_outputPos2;
Binary.CopyOverlapped (m_buffer, src, dst, count);
break;
}
}
dst += count;
y += count;
}
}
}
BitmapPalette ReadPalette ()
{
const int count = 16;
var colors = new Color[count];
for (int i = 0; i < count; ++i)
{
byte b = m_input.ReadUInt8();
byte r = m_input.ReadUInt8();
byte g = m_input.ReadUInt8();
colors[i] = Color.FromRgb ((byte)(r * 0x11), (byte)(g * 0x11), (byte)(b * 0x11));
}
return new BitmapPalette (colors);
}
int m_bitCount;
int m_bits;
bool GetNextBit ()
{
if (--m_bitCount == 0)
{
m_bits = m_input.ReadUInt16();
m_bitCount = 16;
}
bool bit = (m_bits & 0x8000) != 0;
m_bits <<= 1;
return bit;
}
}
}

231
Legacy/Adviz/ImageGIZ2.cs Normal file
View File

@@ -0,0 +1,231 @@
//! \file ImageGIZ2.cs
//! \date 2023 Oct 02
//! \brief ADVIZ engine image format (PC-98).
//
// Copyright (C) 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
// 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 GameRes.Utility;
using System.ComponentModel.Composition;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
// [951027][Ange] Leap Toki ni Sarawareta Shoujo
namespace GameRes.Formats.Adviz
{
[Export(typeof(ImageFormat))]
public class GizFormat : ImageFormat
{
public override string Tag => "GIZ/2";
public override string Description => "ADVIZ engine image format";
public override uint Signature => 0x325A4947; // 'GIZ2'
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
var header = file.ReadHeader (0x10);
int xy = header.ToUInt16 (4);
return new GizMetaData {
Width = (uint)header.ToUInt16 (6) << 3,
Height = header.ToUInt16 (8),
OffsetX = (xy % 0x50) << 3,
OffsetY = xy / 0x50,
RleCode = header[0xC],
PlaneMap = header[0xE],
BPP = 4,
};
}
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var reader = new Giz2Reader (file, (GizMetaData)info);
return reader.Unpack();
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("GizFormat.Write not implemented");
}
}
internal class Giz2Reader
{
IBinaryStream m_input;
GizMetaData m_info;
BitmapPalette m_palette;
public BitmapPalette Palette => m_palette;
public Giz2Reader (IBinaryStream input, GizMetaData info)
{
m_input = input;
m_info = info;
}
int m_stride;
byte[][] m_planes;
int m_output_stride;
byte[] m_output;
public ImageData Unpack ()
{
m_palette = BizFormat.ReadPalette (m_input.Name, 0x30, (pal, off) => ReadPalette (pal, off));
if (null == m_palette)
{
// m_palette = BitmapPalettes.Gray16;
throw new FileNotFoundException ("Unable to retrieve palette.");
}
m_input.Position = 0x10;
m_stride = m_info.iWidth >> 3;
int plane_size = m_info.iHeight;
m_planes = new byte[][] {
new byte[plane_size], new byte[plane_size], new byte[plane_size], new byte[plane_size],
};
m_output_stride = m_info.iWidth >> 1;
m_output = new byte[m_output_stride * m_info.iHeight];
int dst = 0;
for (int x = 0; x < m_stride; ++x)
{
int plane_mask = 1;
for (int i = 0; i < 4; ++i)
{
if ((m_info.PlaneMap & plane_mask) == 0)
UnpackPlane (m_planes[i], 0);
plane_mask <<= 1;
}
CopyPlanes (dst);
dst += 4;
}
return ImageData.Create (m_info, PixelFormats.Indexed4, Palette, m_output, m_output_stride);
}
bool UnpackPlane (byte[] output, int dst)
{
for (int y = 0; y < m_info.iHeight; )
{
byte b = m_input.ReadUInt8();
int ctl = (b - m_info.RleCode) & 0xFF;
if (2 == ctl)
{
output[dst++] = m_input.ReadUInt8();
}
else if (ctl < 4)
{
if (0 == ctl)
b = 0;
else if (1 == ctl)
b = 0xFF;
else
b = m_input.ReadUInt8();
int count = ((m_input.ReadUInt8() - 1) & 0xFF) + 1;
y += count;
while (count --> 0)
{
output[dst++] = b;
}
continue;
}
else if (ctl < 7)
{
byte b0 = m_input.ReadUInt8();
byte b1 = m_input.ReadUInt8();
int count;
if (4 == ctl)
{
count = ((b1 - 1) & 0x7F) + 1;
if (b1 < 0x80)
b1 = Binary.RotByteL (b0, 1);
else
b1 = Binary.RotByteR (b0, 1);
}
else if (5 == ctl)
{
count = ((b1 - 1) & 0x7F) + 1;
if (b1 < 0x80)
b1 = Binary.RotByteL (b0, 2);
else
b1 = Binary.RotByteR (b0, 2);
}
else
{
count = ((m_input.ReadUInt8() - 1) & 0xFF) + 1;
count *= 2;
}
y += count;
do
{
output[dst++] = b0;
if (--count <= 0)
break;
output[dst++] = b1;
}
while (--count > 0);
continue;
}
else
{
output[dst++] = b;
}
++y;
}
return true;
}
void CopyPlanes (int dst)
{
for (int y = 0; y < m_info.iHeight; ++y)
{
int b0 = m_planes[0][y];
int b1 = m_planes[1][y];
int b2 = m_planes[2][y];
int b3 = m_planes[3][y];
for (int j = 0; j < 8; j += 2)
{
byte px = (byte)((((b0 << j) & 0x80) >> 3)
| (((b1 << j) & 0x80) >> 2)
| (((b2 << j) & 0x80) >> 1)
| (((b3 << j) & 0x80) ));
px |= (byte)((((b0 << j) & 0x40) >> 6)
| (((b1 << j) & 0x40) >> 5)
| (((b2 << j) & 0x40) >> 4)
| (((b3 << j) & 0x40) >> 3));
m_output[dst+j/2] = px;
}
dst += m_output_stride;
}
}
BitmapPalette ReadPalette (ArcView file, int offset)
{
const int count = 16;
var colors = new Color[count];
for (int i = 0; i < count; ++i)
{
byte b = file.View.ReadByte (offset++);
byte r = file.View.ReadByte (offset++);
byte g = file.View.ReadByte (offset++);
colors[i] = Color.FromRgb ((byte)(r * 0x11), (byte)(g * 0x11), (byte)(b * 0x11));
}
return new BitmapPalette (colors);
}
}
}