mirror of
https://github.com/crskycode/GARbro.git
synced 2026-07-01 10:50:22 +08:00
reorganized project directory structure.
This commit is contained in:
168
ArcFormats/elf/ArcAi5Win.cs
Normal file
168
ArcFormats/elf/ArcAi5Win.cs
Normal file
@@ -0,0 +1,168 @@
|
||||
//! \file ArcAi5Win.cs
|
||||
//! \date Mon Jun 29 04:41:29 2015
|
||||
//! \brief Ai5Win engine resource archive.
|
||||
//
|
||||
// Copyright (C) 2015 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;
|
||||
using System.Linq;
|
||||
using GameRes.Utility;
|
||||
|
||||
namespace GameRes.Formats.Elf
|
||||
{
|
||||
public class ArcIndexScheme
|
||||
{
|
||||
public int NameLength;
|
||||
public byte NameKey;
|
||||
public uint SizeKey;
|
||||
public uint OffsetKey;
|
||||
}
|
||||
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class ArcOpener : ArchiveFormat
|
||||
{
|
||||
public override string Tag { get { return "ARC/AI5WIN"; } }
|
||||
public override string Description { get { return "AI5WIN engine resource archive"; } }
|
||||
public override uint Signature { get { return 0; } }
|
||||
public override bool IsHierarchic { get { return false; } }
|
||||
public override bool CanCreate { get { return false; } }
|
||||
|
||||
public static readonly Dictionary<string, ArcIndexScheme> KnownSchemes = new Dictionary<string, ArcIndexScheme> {
|
||||
{ "Jokei Kazoku ~Inbou~", new ArcIndexScheme
|
||||
{ NameLength = 0x1E, NameKey = 0x73, SizeKey = 0xAF5789BC, OffsetKey = 0x59FACB45 } },
|
||||
};
|
||||
|
||||
public ArcOpener ()
|
||||
{
|
||||
Extensions = new string[] { "arc" };
|
||||
}
|
||||
|
||||
public override ArcFile TryOpen (ArcView file)
|
||||
{
|
||||
int count = file.View.ReadInt32 (0);
|
||||
if (count <= 0 || count > 0xfffff)
|
||||
return null;
|
||||
long index_offset = 4;
|
||||
var scheme = KnownSchemes.First().Value;
|
||||
uint index_size = (uint)(count * (scheme.NameLength + 8));
|
||||
if (index_size > file.View.Reserve (index_offset, index_size))
|
||||
return null;
|
||||
var name_buf = new byte[scheme.NameLength];
|
||||
var dir = new List<Entry>();
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
file.View.Read (index_offset, name_buf, 0, (uint)scheme.NameLength);
|
||||
for (int n = 0; n < name_buf.Length; ++n)
|
||||
{
|
||||
name_buf[n] ^= scheme.NameKey;
|
||||
if (0 == name_buf[n])
|
||||
break;
|
||||
}
|
||||
string name = Binary.GetCString (name_buf, 0, name_buf.Length);
|
||||
if (0 == name.Length)
|
||||
return null;
|
||||
index_offset += scheme.NameLength;
|
||||
var entry = FormatCatalog.Instance.CreateEntry (name);
|
||||
entry.Size = file.View.ReadUInt32 (index_offset) ^ scheme.SizeKey;
|
||||
entry.Offset = file.View.ReadUInt32 (index_offset+4) ^ scheme.OffsetKey;
|
||||
if (entry.Offset < index_size+4 || !entry.CheckPlacement (file.MaxOffset))
|
||||
return null;
|
||||
dir.Add (entry);
|
||||
index_offset += 8;
|
||||
}
|
||||
return new ArcFile (file, this, dir);
|
||||
}
|
||||
|
||||
/*
|
||||
internal class IndexReader
|
||||
{
|
||||
ArcView m_file;
|
||||
int m_count;
|
||||
byte[] m_first = new byte[0x108];
|
||||
|
||||
public const int MinNameLength = 0x10;
|
||||
public const int MaxNameLength = 0x100;
|
||||
|
||||
public IndexReader (ArcView file)
|
||||
{
|
||||
m_file = file;
|
||||
m_count = m_file.View.ReadInt32 (0);
|
||||
m_file.View.Read (4, m_first, 0, m_first.Length);
|
||||
}
|
||||
|
||||
ArcIndexScheme m_scheme = new ArcIndexScheme();
|
||||
|
||||
public ArcIndexScheme Parse ()
|
||||
{
|
||||
if (m_count <= 0 || m_count > 0xfffff)
|
||||
return null;
|
||||
|
||||
uint supposed_first_offset = (uint)(m_count * (name_length + 8));
|
||||
|
||||
uint first_size = LittleEndian.ToUInt32 (first_entry, name_length);
|
||||
uint first_offset = LittleEndian.ToUInt32 (first_entry, name_length+4);
|
||||
|
||||
uint supposed_offset_key = first_offset ^ supposed_first_offset;
|
||||
int last_index_offset = 4 + (m_count - 1) * (name_length + 8);
|
||||
uint last_size = m_file.View.ReadUInt32 (last_index_offset + name_length);
|
||||
uint last_offset = m_file.View.ReadUInt32 (last_index_offset + name_length + 4);
|
||||
last_offset ^= supposed_offset_key;
|
||||
}
|
||||
|
||||
bool ParseFirstEntry (int name_length)
|
||||
{
|
||||
int index_offset = 4;
|
||||
}
|
||||
|
||||
public byte NameKey { get; private set; }
|
||||
|
||||
int GuessNameLength (int initial)
|
||||
{
|
||||
int name_pos = initial;
|
||||
byte sym;
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
sym = first_entry[name_pos++];
|
||||
}
|
||||
while (name_pos < MaxNameLength && sym != first_entry[name_pos]);
|
||||
if (MaxNameLength == name_pos)
|
||||
return 0;
|
||||
while (name_pos < MaxNameLength && sym == first_entry[name_pos])
|
||||
{
|
||||
++name_pos;
|
||||
}
|
||||
if (MaxNameLength == name_pos && sym == first_entry[name_pos] && sym == first_entry[name_pos+1])
|
||||
return 0;
|
||||
}
|
||||
while (name_pos < MinNameLength || 0 != (name_pos & 1));
|
||||
NameKey = sym;
|
||||
return name_pos;
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
207
ArcFormats/elf/ArcHED.cs
Normal file
207
ArcFormats/elf/ArcHED.cs
Normal file
@@ -0,0 +1,207 @@
|
||||
//! \file ArcHED.cs
|
||||
//! \date Fri Jul 31 19:51:15 2015
|
||||
//! \brief elf AV king archive.
|
||||
//
|
||||
// Copyright (C) 2015 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;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using GameRes.Utility;
|
||||
|
||||
namespace GameRes.Formats.Elf
|
||||
{
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class PakOpener : ArchiveFormat
|
||||
{
|
||||
public override string Tag { get { return "BIN/HED"; } }
|
||||
public override string Description { get { return "elf AV King resource archive"; } }
|
||||
public override uint Signature { get { return 0; } }
|
||||
public override bool IsHierarchic { get { return true; } }
|
||||
public override bool CanCreate { get { return false; } }
|
||||
|
||||
public PakOpener ()
|
||||
{
|
||||
Extensions = new string[] { "bin" };
|
||||
}
|
||||
|
||||
public override ArcFile TryOpen (ArcView file)
|
||||
{
|
||||
string pak_name = Path.ChangeExtension (file.Name, "pak");
|
||||
if (pak_name == file.Name || !File.Exists (pak_name))
|
||||
return null;
|
||||
var file_map = GetFileMap (pak_name);
|
||||
if (null == file_map)
|
||||
return null;
|
||||
string base_name = Path.GetFileNameWithoutExtension (pak_name);
|
||||
|
||||
using (var pak = new ArcView (pak_name))
|
||||
{
|
||||
if (0x00646568 != pak.View.ReadUInt32 (0))
|
||||
return null;
|
||||
int count = pak.View.ReadInt32 (4);
|
||||
if (count != file_map.Count)
|
||||
return null;
|
||||
List<Entry> dir;
|
||||
if ("cg" == base_name)
|
||||
dir = ReadCgPak (pak, file, file_map);
|
||||
else
|
||||
dir = ReadVoicePak (pak, file, file_map);
|
||||
if (null == dir)
|
||||
return null;
|
||||
return new ArcFile (file, this, dir);
|
||||
}
|
||||
}
|
||||
|
||||
List<Entry> ReadCgPak (ArcView pak, ArcView bin, List<string> file_map)
|
||||
{
|
||||
uint index_offset = 8;
|
||||
uint index_size = (uint)file_map.Count * 8u;
|
||||
if (index_size > pak.View.Reserve (index_offset, index_size))
|
||||
return null;
|
||||
var dir = new List<Entry> (file_map.Count);
|
||||
for (int i = 0; i < file_map.Count; ++i)
|
||||
{
|
||||
var entry = FormatCatalog.Instance.CreateEntry (file_map[i]);
|
||||
entry.Offset = pak.View.ReadUInt32 (index_offset);
|
||||
entry.Size = pak.View.ReadUInt32 (index_offset + 4);
|
||||
if (!entry.CheckPlacement (bin.MaxOffset))
|
||||
return null;
|
||||
dir.Add (entry);
|
||||
index_offset += 8;
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
List<Entry> ReadVoicePak (ArcView pak, ArcView bin, List<string> file_map)
|
||||
{
|
||||
uint index_offset = 8;
|
||||
uint index_size = (uint)file_map.Count * 0x18u;
|
||||
if (index_size > pak.View.Reserve (index_offset, index_size))
|
||||
return null;
|
||||
var dir = new List<Entry> (file_map.Count);
|
||||
for (int i = 0; i < file_map.Count; ++i)
|
||||
{
|
||||
var entry = FormatCatalog.Instance.CreateEntry (file_map[i]);
|
||||
entry.Offset = pak.View.ReadUInt32 (index_offset);
|
||||
entry.Size = pak.View.ReadUInt32 (index_offset + 4);
|
||||
if (!entry.CheckPlacement (bin.MaxOffset))
|
||||
return null;
|
||||
dir.Add (entry);
|
||||
index_offset += 0x18;
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
private List<string> CgMap { get; set; }
|
||||
private List<string> VoiceMap { get; set; }
|
||||
private string CurrentMapName { get; set; }
|
||||
|
||||
private List<string> GetFileMap (string pak_name, string map_name = "avking.map")
|
||||
{
|
||||
string base_name = Path.GetFileNameWithoutExtension (pak_name);
|
||||
List<string> map;
|
||||
if ("cg" == base_name)
|
||||
map = CgMap;
|
||||
else if ("voice" == base_name)
|
||||
map = VoiceMap;
|
||||
else
|
||||
return null;
|
||||
if (null != map && File.Exists (CurrentMapName))
|
||||
return map;
|
||||
CgMap = null;
|
||||
VoiceMap = null;
|
||||
string dir_name = Path.GetDirectoryName (pak_name);
|
||||
if (string.IsNullOrEmpty (dir_name))
|
||||
dir_name = ".";
|
||||
while (!string.IsNullOrEmpty (dir_name))
|
||||
{
|
||||
string map_file = Path.Combine (dir_name, map_name);
|
||||
if (File.Exists (map_file))
|
||||
{
|
||||
if (!ReadMap (map_file))
|
||||
return null;
|
||||
CurrentMapName = map_file;
|
||||
if ("cg" == base_name)
|
||||
return CgMap;
|
||||
if ("voice" == base_name)
|
||||
return VoiceMap;
|
||||
}
|
||||
dir_name = Path.GetDirectoryName (dir_name);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static readonly Regex FilesTypeRe = new Regex (@"^//([A-Z]+) FILES = (\d+)");
|
||||
|
||||
private bool ReadMap (string map_file)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var map = File.OpenRead (map_file))
|
||||
using (var input = new StreamReader (map, Encoding.ASCII))
|
||||
{
|
||||
var cg = new List<string>();
|
||||
var voice = new List<string>();
|
||||
List<string> current_list = null;
|
||||
for (;;)
|
||||
{
|
||||
string line = input.ReadLine();
|
||||
if (null == line)
|
||||
break;
|
||||
var match = FilesTypeRe.Match (line);
|
||||
if (!match.Success)
|
||||
return false;
|
||||
string type = match.Groups[1].Value;
|
||||
if ("BG" == type || "CHR" == type)
|
||||
current_list = cg;
|
||||
else if ("VOICE" == type)
|
||||
current_list = voice;
|
||||
else
|
||||
current_list = null;
|
||||
int count = UInt16.Parse (match.Groups[2].Value);
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
line = input.ReadLine();
|
||||
if (null == line)
|
||||
break;
|
||||
if (null != current_list)
|
||||
current_list.Add (line.TrimEnd ('\0'));
|
||||
}
|
||||
}
|
||||
CgMap = cg;
|
||||
VoiceMap = voice;
|
||||
return cg.Count > 0 || voice.Count > 0;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
473
ArcFormats/elf/ImageGCC.cs
Normal file
473
ArcFormats/elf/ImageGCC.cs
Normal file
@@ -0,0 +1,473 @@
|
||||
//! \file ImageGCC.cs
|
||||
//! \date Mon Jun 29 05:12:05 2015
|
||||
//! \brief Ai5Win engine image format.
|
||||
//
|
||||
// Copyright (C) 2015 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.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Windows.Media;
|
||||
using GameRes.Compression;
|
||||
using GameRes.Utility;
|
||||
|
||||
namespace GameRes.Formats.Elf
|
||||
{
|
||||
internal class GccMetaData : ImageMetaData
|
||||
{
|
||||
public uint Signature;
|
||||
}
|
||||
|
||||
[Export(typeof(ImageFormat))]
|
||||
public class GccFormat : ImageFormat
|
||||
{
|
||||
public override string Tag { get { return "GCC"; } }
|
||||
public override string Description { get { return "AI5WIN engine image format"; } }
|
||||
public override uint Signature { get { return 0x6d343252; } } // 'R24m'
|
||||
|
||||
public GccFormat ()
|
||||
{
|
||||
// 'R24m', 'R24n', 'G24m', 'G24n'
|
||||
Signatures = new uint[] { 0x6d343252, 0x6E343252, 0x6D343247, 0x6E343247 };
|
||||
}
|
||||
|
||||
public override ImageMetaData ReadMetaData (Stream stream)
|
||||
{
|
||||
var header = new byte[12];
|
||||
if (header.Length != stream.Read (header, 0, header.Length))
|
||||
return null;
|
||||
|
||||
return new GccMetaData
|
||||
{
|
||||
Width = LittleEndian.ToUInt16 (header, 8),
|
||||
Height = LittleEndian.ToUInt16 (header, 10),
|
||||
BPP = 'm' == header[3] ? 32 : 24,
|
||||
OffsetX = LittleEndian.ToInt16 (header, 4),
|
||||
OffsetY = LittleEndian.ToInt16 (header, 6),
|
||||
Signature = LittleEndian.ToUInt32 (header, 0),
|
||||
};
|
||||
}
|
||||
|
||||
public override ImageData Read (Stream stream, ImageMetaData info)
|
||||
{
|
||||
var meta = info as GccMetaData;
|
||||
if (null == meta)
|
||||
throw new ArgumentException ("GccFormat.Read should be supplied with GccMetaData", "info");
|
||||
|
||||
var reader = new Reader (stream, meta);
|
||||
{
|
||||
reader.Unpack();
|
||||
return ImageData.Create (info, reader.Format, null, reader.Data);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write (Stream file, ImageData image)
|
||||
{
|
||||
throw new NotImplementedException ("GccFormat.Write not implemented");
|
||||
}
|
||||
|
||||
internal class Reader
|
||||
{
|
||||
byte[] m_input;
|
||||
GccMetaData m_info;
|
||||
byte[] m_output;
|
||||
int m_width;
|
||||
int m_height;
|
||||
int m_alpha_w;
|
||||
int m_alpha_h;
|
||||
|
||||
public PixelFormat Format { get; private set; }
|
||||
public byte[] Data { get { return m_output; } }
|
||||
|
||||
public Reader (Stream input, GccMetaData info)
|
||||
{
|
||||
m_input = new byte[input.Length];
|
||||
input.Read (m_input, 0, m_input.Length);
|
||||
m_info = info;
|
||||
m_width = (int)m_info.Width;
|
||||
m_height = (int)m_info.Height;
|
||||
}
|
||||
|
||||
public void Unpack ()
|
||||
{
|
||||
switch (m_info.Signature)
|
||||
{
|
||||
case 0x6E343247: UnpackNormal (LzssUnpack); break; // G24n
|
||||
case 0x6D343247: UnpackMasked (LzssUnpack); break; // G24m
|
||||
case 0x6E343252: UnpackNormal (AltUnpack); break; // R24n
|
||||
case 0x6D343252: UnpackMasked (AltUnpack); break; // R24m
|
||||
default: throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
private void UnpackNormal (Action<int> unpacker)
|
||||
{
|
||||
unpacker (0x14);
|
||||
FlipPixels (m_width*3);
|
||||
Format = PixelFormats.Bgr24;
|
||||
}
|
||||
|
||||
private void UnpackMasked (Action<int> unpacker)
|
||||
{
|
||||
unpacker (0x20);
|
||||
var alpha = UnpackAlpha();
|
||||
if (m_alpha_w < (m_info.OffsetX + m_width) || m_alpha_h < (m_info.OffsetY + m_height))
|
||||
{
|
||||
FlipPixels (m_width*3);
|
||||
Format = PixelFormats.Bgr24;
|
||||
}
|
||||
else
|
||||
{
|
||||
Convert24To32 (alpha);
|
||||
Format = PixelFormats.Bgra32;
|
||||
}
|
||||
}
|
||||
|
||||
private void FlipPixels (int stride)
|
||||
{
|
||||
// flip pixels vertically
|
||||
var pixels = new byte[m_output.Length];
|
||||
int dst = 0;
|
||||
for (int src = stride * (m_height-1); src >= 0; src -= stride)
|
||||
{
|
||||
Buffer.BlockCopy (m_output, src, pixels, dst, stride);
|
||||
dst += stride;
|
||||
}
|
||||
m_output = pixels;
|
||||
}
|
||||
|
||||
private void Convert24To32 (byte[] alpha)
|
||||
{
|
||||
Debug.Assert (m_alpha_w >= (m_info.OffsetX + m_width) && m_alpha_h >= (m_info.OffsetY + m_height));
|
||||
int src_stride = m_width * 3;
|
||||
var pixels = new byte[m_width * m_height * 4];
|
||||
int dst = 0;
|
||||
int alpha_row = m_alpha_w * (m_alpha_h - m_info.OffsetY - 1);
|
||||
for (int row = m_width * (m_height-1); row >= 0; row -= m_width)
|
||||
{
|
||||
int src = row*3;
|
||||
for (int x = 0; x < m_width; ++x)
|
||||
{
|
||||
pixels[dst++] = m_output[src++];
|
||||
pixels[dst++] = m_output[src++];
|
||||
pixels[dst++] = m_output[src++];
|
||||
pixels[dst++] = alpha[alpha_row + m_info.OffsetX + x];
|
||||
}
|
||||
alpha_row -= m_alpha_w;
|
||||
}
|
||||
m_output = pixels;
|
||||
}
|
||||
|
||||
void LzssUnpack (int offset)
|
||||
{
|
||||
int out_length = m_width * m_height * 3;
|
||||
using (var input = new MemoryStream (m_input, offset, m_input.Length-offset))
|
||||
using (var lzss = new LzssReader (input, (int)input.Length, out_length))
|
||||
{
|
||||
lzss.Unpack();
|
||||
m_output = lzss.Data;
|
||||
}
|
||||
}
|
||||
|
||||
int m_index;
|
||||
int m_current;
|
||||
int m_mask;
|
||||
|
||||
void ResetBitInput (int idx)
|
||||
{
|
||||
m_index = idx;
|
||||
m_mask = 0x80;
|
||||
}
|
||||
|
||||
bool NextBit ()
|
||||
{
|
||||
m_mask <<= 1;
|
||||
if (0x100 == m_mask)
|
||||
{
|
||||
m_current = m_input[m_index++];
|
||||
m_mask = 1;
|
||||
}
|
||||
return 0 != (m_current & m_mask);
|
||||
}
|
||||
|
||||
byte[] UnpackAlpha () // sub_444FF0
|
||||
{
|
||||
m_alpha_w = LittleEndian.ToUInt16 (m_input, 0x18);
|
||||
m_alpha_h = LittleEndian.ToUInt16 (m_input, 0x1A);
|
||||
int total = m_alpha_w * m_alpha_h;
|
||||
var alpha = new byte[total];
|
||||
int offset = 0x20 + LittleEndian.ToInt32 (m_input, 0x0C);
|
||||
ResetBitInput (offset);
|
||||
int src = offset + LittleEndian.ToInt32 (m_input, 0x1C);
|
||||
int dst = 0;
|
||||
while (dst < total)
|
||||
{
|
||||
if (NextBit())
|
||||
{
|
||||
int count = ReadCount();
|
||||
byte v = m_input[src++];
|
||||
for (int i = 0; i < count; ++ i)
|
||||
{
|
||||
alpha[dst++] = v;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
alpha[dst++] = m_input[src++];
|
||||
}
|
||||
}
|
||||
return alpha;
|
||||
}
|
||||
|
||||
int ReadCount () // sub_444F60
|
||||
{
|
||||
int result = 1;
|
||||
int bit_count = 0;
|
||||
while (!NextBit())
|
||||
++bit_count;
|
||||
while (bit_count != 0)
|
||||
{
|
||||
--bit_count;
|
||||
result <<= 1;
|
||||
if (NextBit())
|
||||
result |= 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int m_dst;
|
||||
|
||||
private void AltUnpack (int offset) // sub_445620
|
||||
{
|
||||
byte[] chunk = new byte[0x10001];
|
||||
|
||||
int src = offset + LittleEndian.ToInt32 (m_input, 0x10); // within m_input
|
||||
ResetBitInput (offset);
|
||||
int total = 3 * m_width * m_height;
|
||||
m_output = new byte[total];
|
||||
m_dst = 0;
|
||||
int dst = 0;
|
||||
while (dst < total)
|
||||
{
|
||||
int chunk_size = Math.Min (total - dst, 0xffff);
|
||||
if (NextBit())
|
||||
{
|
||||
src = ReadCompressedChunk (src, chunk, chunk_size + 2);
|
||||
DecodeChunk (chunk, chunk_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
src = ReadRawChunk (src, chunk_size);
|
||||
}
|
||||
dst += chunk_size;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ushort[] v15 = new ushort[0x100];
|
||||
ushort[] v16 = new ushort[0x100];
|
||||
ushort[] v17 = new ushort[0x10000];
|
||||
|
||||
void DecodeChunk (byte[] chunk, int chunk_size) // sub_444E40
|
||||
{
|
||||
for (int i = 0; i < v15.Length; ++i)
|
||||
v15[i] = 0;
|
||||
for (int i = 0; i < chunk_size; ++i)
|
||||
++v15[chunk[2+i]];
|
||||
ushort v7 = 0;
|
||||
for (int r = 0; r < 0x100; ++r)
|
||||
{
|
||||
v16[r] = v7;
|
||||
v7 += v15[r];
|
||||
v15[r] = 0;
|
||||
}
|
||||
for (int v9 = 0; v9 < chunk_size; ++v9)
|
||||
{
|
||||
int v10 = chunk[2+v9];
|
||||
int r = v15[v10] + v16[v10];
|
||||
v17[r] = (ushort)v9;
|
||||
v15[v10]++;
|
||||
}
|
||||
int a3 = LittleEndian.ToUInt16 (chunk, 0);
|
||||
int v12 = v17[a3];
|
||||
for (int i = 0; i < chunk_size; ++i)
|
||||
{
|
||||
m_output[m_dst++] = chunk[2+v12];
|
||||
v12 = v17[v12];
|
||||
}
|
||||
}
|
||||
|
||||
int ReadCompressedChunk (int src, byte[] chunk, int chunk_size) // sub_4450E0
|
||||
{
|
||||
byte[] v33 = new byte[0x10];
|
||||
byte[] v35 = new byte[0x10];
|
||||
|
||||
for (byte v6 = 0; v6 < 0x10; ++v6)
|
||||
{
|
||||
v33[v6] = v6;
|
||||
v35[v6] = v6;
|
||||
}
|
||||
int v31 = 0;
|
||||
sbyte v5 = -1;
|
||||
while ( v31 < chunk_size )
|
||||
{
|
||||
int v16;
|
||||
int v26;
|
||||
if (!NextBit())
|
||||
{
|
||||
if (NextBit())
|
||||
{
|
||||
v26 = ReadCount();
|
||||
v16 = v35[v26];
|
||||
chunk[v31++] = (byte)v16;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (NextBit())
|
||||
{
|
||||
int v27 = ReadCount();
|
||||
if (NextBit())
|
||||
v16 = (v5 - v27) & 0xff;
|
||||
else
|
||||
v16 = (v5 + v27) & 0xff;
|
||||
}
|
||||
else
|
||||
{
|
||||
v16 = m_input[src++];
|
||||
}
|
||||
chunk[v31++] = (byte)v16;
|
||||
v26 = 0;
|
||||
while (v35[v26] != v16)
|
||||
{
|
||||
++v26;
|
||||
if (v26 >= 0x10)
|
||||
{
|
||||
v26 = 0xff;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int v17;
|
||||
int count = ReadCount();
|
||||
if (NextBit())
|
||||
{
|
||||
v17 = 0;
|
||||
v16 = v33[0];
|
||||
}
|
||||
else if (NextBit())
|
||||
{
|
||||
v17 = ReadCount();
|
||||
v16 = v33[v17];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (NextBit())
|
||||
{
|
||||
int v20 = ReadCount();
|
||||
if (NextBit())
|
||||
v16 = (v5 - v20) & 0xff;
|
||||
else
|
||||
v16 = (v5 + v20) & 0xff;
|
||||
}
|
||||
else
|
||||
{
|
||||
v16 = m_input[src++];
|
||||
}
|
||||
v17 = 0;
|
||||
while (v33[v17] != v16)
|
||||
{
|
||||
++v17;
|
||||
if (v17 >= 0x10)
|
||||
{
|
||||
v17 = 0xff;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (v17 != 0)
|
||||
{
|
||||
for (int i = v17 & 0xF; i != 0; --i)
|
||||
v33[i] = v33[i-1];
|
||||
v33[0] = (byte)v16;
|
||||
}
|
||||
for (int n = 0; n < count; ++n)
|
||||
chunk[v31++] = (byte)v16;
|
||||
|
||||
v26 = 0;
|
||||
while (v35[v26] != v16)
|
||||
{
|
||||
++v26;
|
||||
if (v26 >= 0x10)
|
||||
{
|
||||
v26 = 0xff;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (0 != (byte)v26)
|
||||
{
|
||||
for (int k = v26 & 0xF; k != 0; --k)
|
||||
v35[k] = v35[k-1];
|
||||
v35[0] = (byte)v16;
|
||||
}
|
||||
v5 = (sbyte)v16;
|
||||
}
|
||||
return src;
|
||||
}
|
||||
|
||||
int ReadRawChunk (int src, int chunk_size) // sub_445400
|
||||
{
|
||||
int n = 0;
|
||||
while (n < chunk_size)
|
||||
{
|
||||
if (!NextBit())
|
||||
{
|
||||
m_output[m_dst++] = m_input[src++];
|
||||
m_output[m_dst++] = m_input[src++];
|
||||
m_output[m_dst++] = m_input[src++];
|
||||
n += 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
int count = ReadCount();
|
||||
byte b = m_input[src++];
|
||||
byte g = m_input[src++];
|
||||
byte r = m_input[src++];
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
m_output[m_dst++] = b;
|
||||
m_output[m_dst++] = g;
|
||||
m_output[m_dst++] = r;
|
||||
}
|
||||
n += 3 * count;
|
||||
}
|
||||
}
|
||||
return src;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
151
ArcFormats/elf/ImageHIZ.cs
Normal file
151
ArcFormats/elf/ImageHIZ.cs
Normal file
@@ -0,0 +1,151 @@
|
||||
//! \file ImageHIZ.cs
|
||||
//! \date Fri Jul 31 19:07:24 2015
|
||||
//! \brief elf HIZ bitmap format.
|
||||
//
|
||||
// Copyright (C) 2015 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;
|
||||
using GameRes.Utility;
|
||||
|
||||
namespace GameRes.Formats.Elf
|
||||
{
|
||||
internal class HizMetaData : ImageMetaData
|
||||
{
|
||||
public bool IsPacked;
|
||||
public uint DataOffset;
|
||||
public uint UnpackedSize;
|
||||
}
|
||||
|
||||
[Export(typeof(ImageFormat))]
|
||||
public class HizFormat : ImageFormat
|
||||
{
|
||||
public override string Tag { get { return "HIZ"; } }
|
||||
public override string Description { get { return "elf bitmap format"; } }
|
||||
public override uint Signature { get { return 0x007A6968; } } // 'hiz'
|
||||
|
||||
public override ImageMetaData ReadMetaData (Stream stream)
|
||||
{
|
||||
using (var input = new ArcView.Reader (stream)) // sub_4BF900
|
||||
{
|
||||
input.ReadInt32();
|
||||
int n = input.ReadInt32();
|
||||
if (100 != n)
|
||||
return null;
|
||||
uint right = input.ReadUInt32() ^ 0xAA5A5A5A;
|
||||
uint bottom = input.ReadUInt32() ^ 0xAC9326AF;
|
||||
int unknown1 = input.ReadInt32(); // @0x10
|
||||
if (unknown1 == 0x375A8436)
|
||||
return null;
|
||||
uint unpacked_size = input.ReadUInt32() ^ 0x19739D6A; // @0x14
|
||||
if (unpacked_size != right*bottom*4)
|
||||
return null;
|
||||
return new HizMetaData
|
||||
{
|
||||
Width = right,
|
||||
Height = bottom,
|
||||
BPP = 32,
|
||||
IsPacked = true,
|
||||
DataOffset = 0x4c,
|
||||
UnpackedSize = unpacked_size,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public override ImageData Read (Stream stream, ImageMetaData info)
|
||||
{
|
||||
var meta = info as HizMetaData;
|
||||
if (null == meta)
|
||||
throw new ArgumentException ("HizFormat.Read should be supplied with HizMetaData", "info");
|
||||
|
||||
var pixels = new byte[meta.UnpackedSize];
|
||||
stream.Position = meta.DataOffset;
|
||||
using (var lzss = new LzssStream (stream, LzssMode.Decompress, true))
|
||||
{
|
||||
var channel = new byte[info.Width*info.Height];
|
||||
for (int p = 0; p < 4; ++p)
|
||||
{
|
||||
if (channel.Length != lzss.Read (channel, 0, channel.Length))
|
||||
throw new InvalidFormatException ("Unexpected end of file");
|
||||
int src = 0;
|
||||
for (int i = p; i < pixels.Length; i += 4)
|
||||
pixels[i] = channel[src++];
|
||||
}
|
||||
return ImageData.Create (info, PixelFormats.Bgra32, null, pixels);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write (Stream file, ImageData image)
|
||||
{
|
||||
throw new System.NotImplementedException ("HizFormat.Write not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
[Export(typeof(ImageFormat))]
|
||||
public class HipFormat : HizFormat
|
||||
{
|
||||
public override string Tag { get { return "HIP"; } }
|
||||
public override string Description { get { return "elf composite image format"; } }
|
||||
public override uint Signature { get { return 0x00706968; } } // 'hip'
|
||||
|
||||
public override ImageMetaData ReadMetaData (Stream stream)
|
||||
{
|
||||
byte[] header = new byte[0x18];
|
||||
if (0x18 != stream.Read (header, 0, 0x18))
|
||||
return null;
|
||||
int index_offset = 0xC;
|
||||
uint first_offset = LittleEndian.ToUInt32 (header, index_offset);
|
||||
if (0 == first_offset)
|
||||
{
|
||||
index_offset += 4;
|
||||
first_offset = LittleEndian.ToUInt32 (header, index_offset);
|
||||
if (0 == first_offset)
|
||||
return null;
|
||||
}
|
||||
index_offset += 4;
|
||||
|
||||
long first_length;
|
||||
uint second_offset = LittleEndian.ToUInt32 (header, index_offset);
|
||||
if (0 == second_offset)
|
||||
first_length = stream.Length - first_offset;
|
||||
else if (second_offset < first_offset)
|
||||
return null;
|
||||
else
|
||||
first_length = second_offset - first_offset;
|
||||
|
||||
using (var hiz = new StreamRegion (stream, first_offset, first_length, true))
|
||||
{
|
||||
var info = base.ReadMetaData (hiz);
|
||||
(info as HizMetaData).DataOffset += 0x18;
|
||||
return info;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write (Stream file, ImageData image)
|
||||
{
|
||||
throw new System.NotImplementedException ("HipFormat.Write not implemented");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user