diff --git a/Legacy/Akatombo/ArcX.cs b/Legacy/Akatombo/ArcX.cs new file mode 100644 index 00000000..f4efb660 --- /dev/null +++ b/Legacy/Akatombo/ArcX.cs @@ -0,0 +1,81 @@ +//! \file ArcX.cs +//! \date 2018 Jan 25 +//! \brief Akatombo 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.Akatombo +{ +#if DEBUG + [Export(typeof(ArchiveFormat))] +#endif + public class PakOpener : ArchiveFormat + { + public override string Tag { get { return "X/AKATOMBO"; } } + public override string Description { get { return "Akatombo resource archive"; } } + public override uint Signature { get { return 0; } } + public override bool IsHierarchic { get { return false; } } + public override bool CanWrite { get { return false; } } + + public override ArcFile TryOpen (ArcView file) + { + int count = file.View.ReadInt16 (0); + if (!IsSaneCount (count)) + return null; + uint index_offset = 2; + uint next_offset = file.View.ReadUInt32 (index_offset); + if (next_offset != count * 4 + 6) + return null; + var base_name = Path.GetFileNameWithoutExtension (file.Name); + var dir = new List (count); + for (int i = 0; i < count; ++i) + { + index_offset += 4; + var entry = new Entry { + Name = string.Format ("{0}#{1:D4}", base_name, i), + Offset = next_offset, + }; + next_offset = file.View.ReadUInt32 (index_offset); + if (next_offset < entry.Offset) + return null; + entry.Size = (uint)(next_offset - entry.Offset); + if (!entry.CheckPlacement (file.MaxOffset)) + return null; + dir.Add (entry); + } + foreach (var entry in dir) + { + uint signature = file.View.ReadUInt32 (entry.Offset); + if ((signature & 0xFFFF) == 0x4246) // 'FB' + entry.Type = "image"; + else + entry.ChangeType (AutoEntry.DetectFileType (signature)); + } + return new ArcFile (file, this, dir); + } + } +} diff --git a/Legacy/Akatombo/ImageFB.cs b/Legacy/Akatombo/ImageFB.cs new file mode 100644 index 00000000..eab9b77d --- /dev/null +++ b/Legacy/Akatombo/ImageFB.cs @@ -0,0 +1,144 @@ +//! \file ImageFB.cs +//! \date 2018 Jan 25 +//! \brief Akatombo compressed 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.Utility; + +namespace GameRes.Formats.Akatombo +{ + [Export(typeof(ImageFormat))] + public class FbFormat : ImageFormat + { + public override string Tag { get { return "FB"; } } + public override string Description { get { return "Akatombo image format"; } } + public override uint Signature { get { return 0x184246; } } + + public override ImageMetaData ReadMetaData (IBinaryStream file) + { + var header = file.ReadHeader (8); + if (!header.AsciiEqual ("FB")) + return null; + return new ImageMetaData { + Width = header.ToUInt32 (4), + Height = header.ToUInt32 (6), + BPP = header.ToUInt16 (2), + }; + } + + public override ImageData Read (IBinaryStream file, ImageMetaData info) + { + var reader = new FbReader (file, info); + var pixels = reader.Unpack(); + return ImageData.Create (info, reader.Format, null, pixels); + } + + public override void Write (Stream file, ImageData image) + { + throw new System.NotImplementedException ("FbFormat.Write not implemented"); + } + } + + internal class FbReader + { + IBinaryStream m_input; + int m_width; + int m_pixel_count; + byte[] m_output; + + public PixelFormat Format { get; private set; } + + public FbReader (IBinaryStream input, ImageMetaData info) + { + m_input = input; + m_width = (int)info.Width; + m_pixel_count = m_width * (int)info.Height; + m_output = new byte[m_pixel_count * 4]; + Format = PixelFormats.Bgr32; + } + + uint m_bits; + + public byte[] Unpack () + { + m_input.Position = 8; + m_bits = 0x80000000; + int dst = 0; + for (int i = 0; i < m_pixel_count; ++i) + { + int bit = GetNextBit(); + if (0 == bit) + { + bit = GetNextBit(); + int pos = GetNextBit(); + if (bit != 0) + pos = dst + 4 * (pos - m_width); + else + pos = dst + 4 * (-m_width & -pos) - 4; + Buffer.BlockCopy (m_output, pos, m_output, dst, 4); + } + m_output[dst ] += ReadDiff(); + m_output[dst+1] += ReadDiff(); + m_output[dst+2] += ReadDiff(); + dst += 4; + } + return m_output; + } + + byte ReadDiff () + { + uint n = 1; + do + { + n = Binary.RotR (n, 1); + } + while (GetNextBit() != 0); + ++n; + byte bit; + do + { + bit = (byte)GetNextBit(); + n = (n << 1) | bit; + } + while (0 == bit); + return (byte)(-(n & 1) ^ ((n >> 1) - 1)); + } + + int GetNextBit () + { + uint bit = m_bits >> 31; + m_bits <<= 1; + if (0 == m_bits) + { + m_bits = Binary.BigEndian (m_input.ReadUInt32()); + bit = m_bits >> 31; + m_bits = (m_bits << 1) | 1; + } + return (int)bit; + } + } +}