diff --git a/ArcFormats/ArcFormats.csproj b/ArcFormats/ArcFormats.csproj index 601a1da0..48223afa 100644 --- a/ArcFormats/ArcFormats.csproj +++ b/ArcFormats/ArcFormats.csproj @@ -90,6 +90,7 @@ + diff --git a/ArcFormats/GameSystem/ArcCHR.cs b/ArcFormats/GameSystem/ArcCHR.cs index 4d1c026e..3a5585de 100644 --- a/ArcFormats/GameSystem/ArcCHR.cs +++ b/ArcFormats/GameSystem/ArcCHR.cs @@ -96,7 +96,7 @@ namespace GameRes.Formats.GameSystem var cent = (ChrEntry)entry; var input = arc.File.CreateStream (entry.Offset, entry.Size); if (cent.Info is ChrMetaData) - return new ChrDecoder (input, cent.Info); + return new ChrDecoder (input, cent.Info as ChrMetaData); else return new ChrFrameDecoder (input, cent.Info); } @@ -109,16 +109,15 @@ namespace GameRes.Formats.GameSystem public ImageMetaData Info; } - internal class ChrDecoder : BinaryImageDecoder + internal class ChrDecoder : ChrReader { - public ChrDecoder (IBinaryStream input, ImageMetaData info) : base (input, info) + public ChrDecoder (IBinaryStream input, ChrMetaData info) : base (input, info) { } protected override ImageData GetImageData () { - var reader = new ChrReader (m_input, (ChrMetaData)Info); - var pixels = reader.UnpackBaseline(); - return ImageData.CreateFlipped (Info, PixelFormats.Bgra32, null, pixels, reader.Stride); + var pixels = UnpackBaseline(); + return ImageData.CreateFlipped (Info, PixelFormats.Bgra32, null, pixels, Stride); } } diff --git a/ArcFormats/GameSystem/ArcDAT.cs b/ArcFormats/GameSystem/ArcDAT.cs index c749cd09..d47f4448 100644 --- a/ArcFormats/GameSystem/ArcDAT.cs +++ b/ArcFormats/GameSystem/ArcDAT.cs @@ -65,6 +65,8 @@ namespace GameRes.Formats.GameSystem long next_offset = (long)file.View.ReadUInt32 (index_offset+12) << 9; if (next_offset < offset || next_offset > file.MaxOffset) return null; + if (name.EndsWith (".CRGB") || name.EndsWith (".CHAR")) + entry.Type = "image"; entry.Offset = offset; entry.Size = (uint)(next_offset - offset); dir.Add (entry); @@ -101,5 +103,25 @@ namespace GameRes.Formats.GameSystem var ext = Encoding.ASCII.GetString (name_buf, 12, ext_end-12); return name + '.' + ext; } + + public override IImageDecoder OpenImage (ArcFile arc, Entry entry) + { + if (entry.Name.EndsWith (".BGD") || entry.Name.EndsWith (".CRGB")) + { + var input = arc.File.CreateStream (entry.Offset, entry.Size); + var info = new ImageMetaData { Width = 800, Height = 600, BPP = 24 }; + return new CgdReader (input, info); + } + else if (entry.Name.EndsWith (".CHAR")) + { + var input = arc.File.CreateStream (entry.Offset, entry.Size); + var info = new ChrMetaData { + Width = 800, Height = 600, BPP = 32, + DataOffset = 0, RgbSize = (int)input.Length, + }; + return new ChrReader (input, info); + } + return base.OpenImage (arc, entry); + } } } diff --git a/ArcFormats/GameSystem/ArcPureMail.cs b/ArcFormats/GameSystem/ArcPureMail.cs new file mode 100644 index 00000000..cfea4e36 --- /dev/null +++ b/ArcFormats/GameSystem/ArcPureMail.cs @@ -0,0 +1,201 @@ +//! \file ArcPureMail.cs +//! \date Sun Feb 12 04:18:56 2017 +//! \brief 0verflow Game System resource archive. +// +// Copyright (C) 2017 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.Windows.Media; + +namespace GameRes.Formats.GameSystem +{ + internal class PmDatEntry : PackedEntry + { + public bool StoredSize; + } + + [Export(typeof(ArchiveFormat))] + public class PmDatOpener : ArchiveFormat + { + public override string Tag { get { return "DAT/PUREMAIL"; } } + public override string Description { get { return "PureMail 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) + { + if (file.MaxOffset <= 12 || file.MaxOffset > uint.MaxValue) + return null; + uint packed_size = file.View.ReadUInt32 (file.MaxOffset-12) ^ 0xF0F0F0F0; + uint unpacked_size = file.View.ReadUInt32 (file.MaxOffset-8) ^ 0xF0F0F0F0; + const uint entry_record_size = 0x50; + int count = (int)(unpacked_size / entry_record_size); + if (unpacked_size % entry_record_size != 0 || packed_size >= file.MaxOffset || !IsSaneCount (count)) + return null; + + var unpacked = new byte[unpacked_size]; + using (var packed = file.CreateStream (file.MaxOffset-12-packed_size, packed_size)) + LzUnpack (packed, unpacked); + using (var index = new BinMemoryStream (unpacked)) + { + var dir = new List (count); + for (int i = 0; i < count; ++i) + { + int flags = index.ReadInt32(); + var name = index.ReadCString (0x40); + var entry = FormatCatalog.Instance.Create (name); + entry.Offset = index.ReadUInt32(); + entry.Size = index.ReadUInt32(); + entry.UnpackedSize = index.ReadUInt32(); + if (!entry.CheckPlacement (file.MaxOffset)) + return null; + if (entry.Name.EndsWith (".CRGB", StringComparison.InvariantCultureIgnoreCase) || + entry.Name.EndsWith (".CHAR", StringComparison.InvariantCultureIgnoreCase) || + entry.Name.EndsWith (".rol", StringComparison.InvariantCultureIgnoreCase) || + entry.Name.EndsWith (".edg", StringComparison.InvariantCultureIgnoreCase)) + entry.Type = "image"; + entry.IsPacked = (flags & 0xFF0000) != 0; + entry.StoredSize = (flags & 0x2000000) != 0; + dir.Add (entry); + } + return new ArcFile (file, this, dir); + } + } + + public override Stream OpenEntry (ArcFile arc, Entry entry) + { + var pent = entry as PmDatEntry; + if (null == pent || !pent.IsPacked) + return base.OpenEntry (arc, entry); + using (var input = arc.File.CreateStream (entry.Offset, entry.Size)) + { + var unpacked_size = pent.UnpackedSize; + if (pent.StoredSize) + { + unpacked_size = input.ReadUInt32(); + pent.UnpackedSize = unpacked_size; + } + var output = new byte[unpacked_size]; + LzUnpack (input, output); + return new BinMemoryStream (output); + } + } + + void LzUnpack (IBinaryStream input, byte[] output) + { + var frame = new byte[0x1000]; + int dst = 0; + int bits = 0; + int mask = 0; + int frame_pos = 0xFEE; + while (dst < output.Length) + { + mask >>= 1; + if (0 == mask) + { + bits = input.ReadByte(); + if (-1 == bits) + break; + mask = 0x80; + } + if (0 == (bits & mask)) + { + int b = input.ReadByte(); + if (-1 == b) + break; + output[dst++] = (byte)b; + frame[frame_pos++ & 0xFFF] = (byte)b; + } + else + { + int offset = input.ReadUInt16(); + int count = (offset & 0xF) + 3; + offset >>= 4; + while (count --> 0 && dst < output.Length) + { + byte v = frame[offset++ & 0xFFF]; + frame[frame_pos++ & 0xFFF] = v; + output[dst++] = v; + } + } + } + } + + public override IImageDecoder OpenImage (ArcFile arc, Entry entry) + { + if (entry.Name.EndsWith (".BGD", StringComparison.InvariantCultureIgnoreCase) || + entry.Name.EndsWith (".CRGB", StringComparison.InvariantCultureIgnoreCase)) + { + var input = OpenEntry (arc, entry); + var info = new ImageMetaData { Width = 800, Height = 600, BPP = 24 }; + return new CgdReader (BinaryStream.FromStream (input, entry.Name), info); + } + else if (entry.Name.EndsWith (".edg", StringComparison.InvariantCultureIgnoreCase)) + { + var input = OpenEntry (arc, entry); + var info = new ImageMetaData { Width = 460, Height = 345, BPP = 24 }; + return new ImgReader (BinaryStream.FromStream (input, entry.Name), info); + } + else if (entry.Name.EndsWith (".rol", StringComparison.InvariantCultureIgnoreCase)) + { + var input = OpenEntry (arc, entry); + uint width = 202; + uint height = (uint)input.Length / (((width * 3u) + 3u) & ~3u); + var info = new ImageMetaData { Width = width, Height = height, BPP = 24 }; + return new ImgReader (BinaryStream.FromStream (input, entry.Name), info); + } + else if (entry.Name.EndsWith (".CHAR", StringComparison.InvariantCultureIgnoreCase)) + { + var input = OpenEntry (arc, entry); + var info = new ChrMetaData { + Width = 800, Height = 600, BPP = 32, + DataOffset = 0, RgbSize = (int)input.Length, + }; + return new ChrReader (BinaryStream.FromStream (input, entry.Name), info); + } + return base.OpenImage (arc, entry); + } + } + + internal class ImgReader : BinaryImageDecoder + { + public ImgReader (IBinaryStream input, ImageMetaData info) : base (input, info) + { + } + + protected override ImageData GetImageData () + { + m_input.Position = 0; + int stride = (int)Info.Width * Info.BPP / 8; + stride = (stride + 3) & ~3; + int total = stride * (int)Info.Height; + var pixels = m_input.ReadBytes (total); + if (pixels.Length != total) + throw new EndOfStreamException(); + return ImageData.CreateFlipped (Info, PixelFormats.Bgr24, null, pixels, stride); + } + } +} diff --git a/ArcFormats/GameSystem/ImageCGD.cs b/ArcFormats/GameSystem/ImageCGD.cs index e92075ba..414aa482 100644 --- a/ArcFormats/GameSystem/ImageCGD.cs +++ b/ArcFormats/GameSystem/ImageCGD.cs @@ -36,6 +36,11 @@ namespace GameRes.Formats.GameSystem public override string Description { get { return "'GameSystem' CG image format"; } } public override uint Signature { get { return 0; } } + public CgdFormat () + { + Extensions = new string[] { "cgd", "crgb" }; + } + public override ImageMetaData ReadMetaData (IBinaryStream file) { if (file.Signature != file.Length) @@ -55,9 +60,9 @@ namespace GameRes.Formats.GameSystem public override ImageData Read (IBinaryStream file, ImageMetaData info) { + file.Position = 0x10; var reader = new CgdReader (file, info); - var pixels = reader.Unpack(); - return ImageData.CreateFlipped (info, reader.Format, null, pixels, reader.Stride); + return reader.Image; } public override void Write (Stream file, ImageData image) @@ -66,29 +71,27 @@ namespace GameRes.Formats.GameSystem } } - internal sealed class CgdReader + internal sealed class CgdReader : BinaryImageDecoder { - IBinaryStream m_input; - int m_width; - int m_height; byte[] m_output; public int Stride { get; private set; } public PixelFormat Format { get { return PixelFormats.Bgr24; } } - public byte[] Data { get { return m_output; } } - public CgdReader (IBinaryStream input, ImageMetaData info) + public CgdReader (IBinaryStream input, ImageMetaData info) : base (input, info) { - m_input = input; - m_width = (int)info.Width; - m_height = (int)info.Height; - Stride = 3 * m_width; - m_output = new byte[Stride * m_height]; + Stride = 3 * (int)info.Width; + m_output = new byte[Stride * (int)info.Height]; } - public byte[] Unpack () + protected override ImageData GetImageData () + { + var pixels = Unpack(); + return ImageData.CreateFlipped (Info, Format, null, pixels, Stride); + } + + byte[] Unpack () { - m_input.Position = 0x10; int dst = 0; byte r = 0, g = 0, b = 0; while (dst < m_output.Length) @@ -131,7 +134,7 @@ namespace GameRes.Formats.GameSystem return m_output; } - static readonly byte[,] ColorTable = InitColorTable(); + internal static readonly byte[,] ColorTable = InitColorTable(); private static byte[,] InitColorTable () { diff --git a/ArcFormats/GameSystem/ImageCHR.cs b/ArcFormats/GameSystem/ImageCHR.cs index cf3f196f..ff5c6022 100644 --- a/ArcFormats/GameSystem/ImageCHR.cs +++ b/ArcFormats/GameSystem/ImageCHR.cs @@ -32,6 +32,7 @@ namespace GameRes.Formats.GameSystem { internal class ChrMetaData : ImageMetaData { + public uint DataOffset; public int RgbSize; } @@ -65,14 +66,14 @@ namespace GameRes.Formats.GameSystem OffsetY = y, BPP = 32, RgbSize = rgb_size, + DataOffset = 0x20, }; } public override ImageData Read (IBinaryStream file, ImageMetaData info) { var reader = new ChrReader (file, (ChrMetaData)info); - var pixels = reader.Unpack(); - return ImageData.CreateFlipped (info, PixelFormats.Bgra32, null, pixels, reader.Stride); + return reader.Image; } public override void Write (Stream file, ImageData image) @@ -81,9 +82,8 @@ namespace GameRes.Formats.GameSystem } } - internal sealed class ChrReader + internal class ChrReader : BinaryImageDecoder { - IBinaryStream m_input; ChrMetaData m_info; byte[] m_output; int m_stride; @@ -91,14 +91,19 @@ namespace GameRes.Formats.GameSystem public byte[] Data { get { return m_output; } } public int Stride { get { return m_stride; } } - public ChrReader (IBinaryStream input, ChrMetaData info) + public ChrReader (IBinaryStream input, ChrMetaData info) : base (input, info) { - m_input = input; m_info = info; m_stride = (int)m_info.Width * 4; m_output = new byte[m_stride * (int)m_info.Height]; } + protected override ImageData GetImageData () + { + var pixels = Unpack(); + return ImageData.CreateFlipped (Info, PixelFormats.Bgra32, null, pixels, Stride); + } + public byte[] Unpack () { UnpackBaseline(); @@ -114,7 +119,7 @@ namespace GameRes.Formats.GameSystem public byte[] UnpackBaseline () { - m_input.Position = 0x20; + m_input.Position = m_info.DataOffset; UnpackRgb ((int)m_info.Height); return m_output; }