(Legacy): PAC archives and KGD images.

This commit is contained in:
morkt 2018-08-23 23:56:39 +04:00
parent 6e5c60ee2c
commit 11e59fc221
5 changed files with 511 additions and 0 deletions

77
Legacy/KeroQ/ArcDAT.cs Normal file
View File

@ -0,0 +1,77 @@
//! \file ArcDAT.cs
//! \date 2018 Aug 22
//! \brief KeroQ resource archive.
//
// 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;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
namespace GameRes.Formats.KeroQ
{
[Export(typeof(ArchiveFormat))]
public class PacOpener : ArchiveFormat
{
public override string Tag { get { return "DAT/PAC"; } }
public override string Description { get { return "KeroQ resource archive"; } }
public override uint Signature { get { return 0x43415089; } } // '\x89PAC'
public override bool IsHierarchic { get { return false; } }
public override bool CanWrite { get { return false; } }
public override ArcFile TryOpen (ArcView file)
{
int count = file.View.ReadInt32 (4);
if (!IsSaneCount (count))
return null;
var pac_name = Path.GetFileNameWithoutExtension (file.Name);
int pac_num;
if (!Int32.TryParse (pac_name, out pac_num))
return null;
var hdr_name = string.Format ("{0:D3}.dat", pac_num - 1);
hdr_name = VFS.ChangeFileName (file.Name, hdr_name);
if (!VFS.FileExists (hdr_name))
return null;
using (var index = VFS.OpenBinaryStream (hdr_name))
{
var header = index.ReadHeader (8);
if (!header.AsciiEqual ("\x89HDR"))
return null;
if (header.ToInt32 (4) != count)
return null;
var dir = new List<Entry> (count);
for (int i = 0; i < count; ++i)
{
var name = index.ReadCString (0x10);
var entry = FormatCatalog.Instance.Create<Entry> (name);
entry.Size = index.ReadUInt32();
entry.Offset = index.ReadUInt32();
if (!entry.CheckPlacement (file.MaxOffset))
return null;
dir.Add (entry);
}
return new ArcFile (file, this, dir);
}
}
}
}

94
Legacy/KeroQ/ImageCBM.cs Normal file
View File

@ -0,0 +1,94 @@
//! \file ImageCBM.cs
//! \date 2018 Aug 22
//! \brief KeroQ bitmap format.
//
// 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.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace GameRes.Formats.KeroQ
{
[Export(typeof(ImageFormat))]
public class CbmFormat : ImageFormat
{
public override string Tag { get { return "CBM"; } }
public override string Description { get { return "KeroQ bitmap format"; } }
public override uint Signature { get { return 0x004D4243; } } // 'CBM'
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
var header = file.ReadHeader (0x10);
var length = header.ToUInt32 (0xC);
if (file.Length - 0x10 != length)
return null;
return new ImageMetaData {
Width = header.ToUInt32 (4),
Height = header.ToUInt32 (8),
BPP = 8,
};
}
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
BitmapPalette palette = null;
PixelFormat format = PixelFormats.Gray8;
foreach (var pal_name in GetPaletteNames (info.FileName))
{
if (!VFS.FileExists (pal_name))
continue;
try
{
using (var pal = VFS.OpenStream (pal_name))
{
palette = ReadPalette (pal, 0x100, PaletteFormat.Bgr);
format = PixelFormats.Indexed8;
}
}
catch { /* ignore palette read errors */ }
break;
}
file.Position = 0x10;
var pixels = file.ReadBytes ((int)info.Width * (int)info.Height);
return ImageData.Create (info, format, palette, pixels);
}
IEnumerable<string> GetPaletteNames (string filename)
{
var base_name = Path.GetFileNameWithoutExtension (filename);
yield return VFS.ChangeFileName (filename, base_name + ".pal");
if (base_name.Length > 3)
base_name = base_name.Substring (0, 3);
yield return VFS.ChangeFileName (filename, base_name + ".pal");
yield return VFS.ChangeFileName (filename, base_name + "_2.pal");
yield return VFS.ChangeFileName (filename, base_name + "_1.pal");
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("CbmFormat.Write not implemented");
}
}
}

209
Legacy/KeroQ/ImageKGD.cs Normal file
View File

@ -0,0 +1,209 @@
//! \file ImageKGD.cs
//! \date 2018 Aug 22
//! \brief KeroQ image format.
//
// 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;
using System.ComponentModel.Composition;
using System.IO;
using System.Windows.Media;
using GameRes.Compression;
// [011130][KeroQ] Nijuubako
namespace GameRes.Formats.KeroQ
{
[Export(typeof(ImageFormat))]
public class KgdFormat : ImageFormat
{
public override string Tag { get { return "KGD"; } }
public override string Description { get { return "KeroQ image format"; } }
public override uint Signature { get { return 0x44474B89; } } // '\x89KGD'
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
var header = file.ReadHeader (0x19);
if (header.ToInt32 (4) != 0x10 || header[8] != 1)
return null;
return new ImageMetaData {
Width = header.ToUInt32 (9),
Height = header.ToUInt32 (0xD),
BPP = 2 == header[0x12] ? 24 : 32,
};
}
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
file.Position = 0x19;
using (var packed = new KgdStream (file))
using (var input = new ZLibStream (packed, CompressionMode.Decompress))
{
int stride = ((int)info.Width * info.BPP + 7) / 8;
int pixel_size = (info.BPP + 7) / 8;
var buffer = new byte[stride+1];
var prev_line = new byte[stride];
var pixels = new byte[stride * (int)info.Height];
int dst = 0;
for (uint i = 0; i < info.Height; ++i)
{
if (input.Read (buffer, 0, buffer.Length) == 0)
break;
switch (buffer[0])
{
case 1: // PNG_FILTER_VALUE_SUB
for (int j = pixel_size; j < stride; ++j)
{
buffer[1+j] += buffer[1+j-pixel_size];
}
break;
case 2: // PNG_FILTER_VALUE_UP
for (int j = 0; j < stride; ++j)
{
buffer[1+j] += prev_line[j];
}
break;
case 3: // PNG_FILTER_VALUE_AVG
for (int j = 0; j < pixel_size; ++j)
{
buffer[1+j] += (byte)(prev_line[j] >> 1);
}
for (int j = pixel_size; j < stride; ++j)
{
int v = (prev_line[j] + buffer[1+j-pixel_size]) >> 1;
buffer[1+j] += (byte)v;
}
break;
case 4: // PNG_FILTER_VALUE_PAETH
for (int j = 0; j < pixel_size; ++j)
{
buffer[1+j] += prev_line[j];
}
int src = 1;
for (int j = pixel_size; j < stride; ++j)
{
byte y = prev_line[j];
byte x = buffer[src++];
byte z = prev_line[j-pixel_size];
int yz = y - z;
int xz = x - z;
int ayz = Math.Abs (yz);
int axz = Math.Abs (xz);
int axy = Math.Abs (xz + yz);
if (!(ayz > axz || ayz > axy))
z = x;
else if (axz <= axy)
z = y;
buffer[1+j] += (byte)z;
}
break;
case 0:
break;
}
Buffer.BlockCopy (buffer, 1, prev_line, 0, stride);
Buffer.BlockCopy (buffer, 1, pixels, dst, stride);
dst += stride;
}
PixelFormat format = 24 == info.BPP ? PixelFormats.Bgr24 : PixelFormats.Bgr32;
return ImageData.Create (info, PixelFormats.Bgr24, null, pixels);
}
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("KgdFormat.Write not implemented");
}
}
internal class KgdStream : InputProxyStream
{
IBinaryStream m_input;
bool m_eof = false;
byte[] m_buffer = new byte[0x2000];
int m_buffer_pos = 0;
int m_buffer_size = 0;
public KgdStream (IBinaryStream input) : base (input.AsStream, true)
{
m_input = input;
}
public override bool CanSeek { get { return false; } }
public override int Read (byte[] buffer, int offset, int count)
{
int read = 0;
while (!m_eof && count > 0)
{
if (m_buffer_pos >= m_buffer_size)
{
FillBuffer();
continue;
}
int avail = Math.Min (count, m_buffer_size - m_buffer_pos);
Buffer.BlockCopy (m_buffer, m_buffer_pos, buffer, offset, avail);
m_buffer_pos += avail;
offset += avail;
count -= avail;
read += avail;
}
return read;
}
public override int ReadByte ()
{
if (m_eof)
return -1;
if (m_buffer_pos >= m_buffer_size)
{
FillBuffer();
if (m_eof)
return -1;
}
return m_buffer[m_buffer_pos++];
}
void FillBuffer ()
{
if (m_input.PeekByte() == -1)
{
m_eof = true;
return;
}
int chunk_size = m_input.ReadInt32();
int type = m_input.ReadByte();
if (type != 2)
{
m_eof = true;
return;
}
if (chunk_size > m_buffer.Length)
m_buffer = new byte[chunk_size];
m_buffer_size = m_input.Read (m_buffer, 0, chunk_size);
m_buffer_pos = 0;
}
}
}

127
Legacy/KeroQ/ImageKGD1.cs Normal file
View File

@ -0,0 +1,127 @@
//! \file ImageKGD1.cs
//! \date 2018 Aug 22
//! \brief KeroQ image format.
//
// 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 System.Windows.Media.Imaging;
namespace GameRes.Formats.KeroQ
{
internal class Kgd1MetaData : ImageMetaData
{
public int AlphaLength;
}
[Export(typeof(ImageFormat))]
public class Kgd1Format : ImageFormat
{
public override string Tag { get { return "KGD1"; } }
public override string Description { get { return "KeroQ image format"; } }
public override uint Signature { get { return 0x3144474B; } } // 'KGD1'
public Kgd1Format ()
{
Extensions = new string[] { "kgd" };
}
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
var header = file.ReadHeader (0x18);
int bpp = header.ToInt16 (6);
if (bpp != 8 && bpp != 24)
return null;
return new Kgd1MetaData {
Width = header.ToUInt32 (8),
Height = header.ToUInt32 (0xC),
BPP = bpp,
AlphaLength = header.ToInt32 (0x10),
};
}
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var meta = (Kgd1MetaData)info;
file.Position = 0x18;
byte[] alpha = null;
if (meta.AlphaLength != 0)
alpha = file.ReadBytes (meta.AlphaLength);
BitmapPalette palette = null;
PixelFormat format;
if (8 == info.BPP)
{
palette = ReadPalette (file.AsStream);
format = PixelFormats.Indexed8;
}
else
format = PixelFormats.Bgr24;
var pixels = new byte[(int)info.Width * (int)info.Height * (info.BPP / 8)];
file.Read (pixels, 0, pixels.Length);
if (alpha != null)
{
pixels = ApplyAlphaChannel (meta, pixels, palette, alpha);
format = PixelFormats.Bgra32;
}
return ImageData.Create (info, format, palette, pixels);
}
byte[] ApplyAlphaChannel (ImageMetaData info, byte[] image, BitmapPalette palette, byte[] alpha)
{
var output = new byte[4 * (int)info.Width * (int)info.Height];
if (24 == info.BPP)
{
int dst = 0;
int asrc = 0;
for (int src = 0; src < image.Length; src += 3)
{
output[dst++] = image[src];
output[dst++] = image[src+1];
output[dst++] = image[src+2];
output[dst++] = (byte)~alpha[asrc++];
}
}
else
{
int dst = 0;
var colors = palette.Colors;
for (int src = 0; src < image.Length; ++src)
{
byte c = image[src];
output[dst++] = colors[c].B;
output[dst++] = colors[c].G;
output[dst++] = colors[c].R;
output[dst++] = (byte)~alpha[src];
}
}
return output;
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("Kgd1Format.Write not implemented");
}
}
}

View File

@ -71,6 +71,10 @@
<Compile Include="Akatombo\ArcX.cs" />
<Compile Include="Akatombo\ImageFB.cs" />
<Compile Include="Kasane\ArcAR2.cs" />
<Compile Include="KeroQ\ArcDAT.cs" />
<Compile Include="KeroQ\ImageCBM.cs" />
<Compile Include="KeroQ\ImageKGD.cs" />
<Compile Include="KeroQ\ImageKGD1.cs" />
<Compile Include="RSystem\ArcRAD.cs" />
<Compile Include="RSystem\ImageRSG.cs" />
<Compile Include="Brownie\ArcNAF.cs" />