diff --git a/ArcFormats/ArcFormats.csproj b/ArcFormats/ArcFormats.csproj index dbece756..62839f43 100644 --- a/ArcFormats/ArcFormats.csproj +++ b/ArcFormats/ArcFormats.csproj @@ -66,6 +66,8 @@ + + diff --git a/ArcFormats/Eushully/ArcGPC.cs b/ArcFormats/Eushully/ArcGPC.cs new file mode 100644 index 00000000..b81d813a --- /dev/null +++ b/ArcFormats/Eushully/ArcGPC.cs @@ -0,0 +1,139 @@ +//! \file ArcGPC.cs +//! \date Thu Nov 12 12:33:40 2015 +//! \brief Old Eushully archives. +// +// 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 GameRes.Utility; +using System.Collections.Generic; +using System.ComponentModel.Composition; +using System.IO; + +namespace GameRes.Formats.Eushully +{ + public abstract class HOpener : ArchiveFormat + { + public override uint Signature { get { return 0; } } + public override bool IsHierarchic { get { return false; } } + public override bool CanCreate { get { return false; } } + + protected ArcFile TryOpenWithIndex (ArcView file, string entry_type, string entry_ext = "") + { + var ext = Path.GetExtension (file.Name).ToUpperInvariant(); + if (ext.Length != 4 || 'H' == ext[3]) + return null; + // GPC -> GPH, SNR -> SNH, etc + var idx_name = Path.ChangeExtension (file.Name, string.Concat (ext.Substring (0, 3), "H")); + if (!VFS.FileExists (idx_name)) + return null; + + using (var idx = VFS.OpenView (idx_name)) + { + long idx_offset = 0; + var name_buffer = new byte[0x40]; + var dir = new List(); + while (idx_offset < idx.MaxOffset) + { + int name_length = idx.View.ReadByte (idx_offset++); + if (name_length > name_buffer.Length) + name_buffer = new byte[name_length]; + if (name_length != idx.View.Read (idx_offset, name_buffer, 0, (uint)name_length)) + return null; + for (int i = 0; i < name_length; ++i) + name_buffer[i] ^= 0xFF; + var name = Encodings.cp932.GetString (name_buffer, 0, name_length); + var entry = new Entry { Name = name + entry_ext, Type = entry_type }; + idx_offset += name_length; + entry.Offset = idx.View.ReadUInt32 (idx_offset); + if (entry.Offset > file.MaxOffset) + return null; + idx_offset += 4; + dir.Add (entry); + } + if (0 == dir.Count) + return null; + dir.Sort ((a, b) => (int)(a.Offset - b.Offset)); + for (int i = 0; i < dir.Count; ++i) + { + long next_offset = i+1 == dir.Count ? file.MaxOffset : dir[i+1].Offset; + dir[i].Size = (uint)(next_offset - dir[i].Offset); + } + return new ArcFile (file, this, dir); + } + } + } + + [Export(typeof(ArchiveFormat))] + public class GpcOpener : HOpener + { + public override string Tag { get { return "GPC"; } } + public override string Description { get { return "Eushully graphic archive"; } } + + public override ArcFile TryOpen (ArcView file) + { + return TryOpenWithIndex (file, "image", ".gpcf"); + } + } + + [Export(typeof(ArchiveFormat))] + public class SndOpener : HOpener + { + public override string Tag { get { return "SND"; } } + public override string Description { get { return "Eushully audio archive"; } } + + public override ArcFile TryOpen (ArcView file) + { + return TryOpenWithIndex (file, "audio", ".wav"); + } + + public override Stream OpenEntry (ArcFile arc, Entry entry) + { + if (entry.Size < 0x16) + return base.OpenEntry (arc, entry); + // emulate WAV file + var header = new byte[0x2C]; + LittleEndian.Pack (0x46464952, header, 0x0); // 'RIFF' + LittleEndian.Pack (0x45564157, header, 0x8); // 'WAVE' + LittleEndian.Pack (0x20746d66, header, 0xC); // 'fmt ' + header[0x10] = 0x10; + arc.File.View.Read (entry.Offset+1, header, 0x14, 0x10); + LittleEndian.Pack (0x61746164, header, 0x24); // 'data' + uint data_size = arc.File.View.ReadUInt32 (entry.Offset+0x11); + LittleEndian.Pack (data_size, header, 0x28); + LittleEndian.Pack (data_size+0x24u, header, 4); + var pcm = arc.File.CreateStream (entry.Offset+0x15, data_size); + return new PrefixStream (header, pcm); + } + } + + [Export(typeof(ArchiveFormat))] + public class SnrOpener : HOpener + { + public override string Tag { get { return "SNR"; } } + public override string Description { get { return "Eushully script archive"; } } + + public override ArcFile TryOpen (ArcView file) + { + return TryOpenWithIndex (file, "script"); + } + } +} diff --git a/ArcFormats/Eushully/ImageGP.cs b/ArcFormats/Eushully/ImageGP.cs new file mode 100644 index 00000000..87a87557 --- /dev/null +++ b/ArcFormats/Eushully/ImageGP.cs @@ -0,0 +1,317 @@ +//! \file ImageGP.cs +//! \date Thu Nov 12 14:00:52 2015 +//! \brief old Eushully 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 GameRes.Utility; +using System; +using System.ComponentModel.Composition; +using System.IO; +using System.Windows.Media; +using System.Windows.Media.Imaging; + +namespace GameRes.Formats.Eushully +{ + internal class GpMetaData : ImageMetaData + { + public bool HasAlpha; + public int Method; + public int ElementSize; + public int PixelsPerElement; + public int PaletteSize; + } + + [Export(typeof(ImageFormat))] + public class GpFormat : ImageFormat + { + public override string Tag { get { return "GP/EUSHULLY"; } } + public override string Description { get { return "Old Eushully graphic format"; } } + public override uint Signature { get { return 0; } } + + public GpFormat () + { + Extensions = new string[] { "gpcf" }; // made-up, real files have no extension + } + + public override ImageMetaData ReadMetaData (Stream stream) + { + int alpha_channel = stream.ReadByte(); + int method = stream.ReadByte(); + int align1 = stream.ReadByte(); + int align2 = stream.ReadByte(); + int bpp = stream.ReadByte(); + if (alpha_channel < 0 || alpha_channel > 1 || method < 0 || method > 2 + || align1 < 0 || align1 > 4 || align2 < 0 || align2 > 4 + || bpp < 0 || !(bpp <= 16 || 24 == bpp || 32 == bpp)) + return null; + using (var reader = new ArcView.Reader (stream)) + { + int palette_size = reader.ReadInt32(); + uint width = reader.ReadUInt16(); + uint height = reader.ReadUInt16(); + if (0 == palette_size || 0 == width || 0 == height || palette_size >= stream.Length) + return null; + return new GpMetaData + { + Width = width, + Height = height, + BPP = bpp, + HasAlpha = alpha_channel != 0, + Method = method, + ElementSize = align1, + PixelsPerElement = align2, + PaletteSize = palette_size, + }; + } + } + + public override ImageData Read (Stream stream, ImageMetaData info) + { + var meta = (GpMetaData)info; + using (var reader = new GpReader (stream, meta)) + { + reader.Unpack(); + return ImageData.Create (info, reader.Format, reader.Palette, reader.Data, reader.Stride); + } + } + + public override void Write (Stream file, ImageData image) + { + throw new System.NotImplementedException ("GpFormat.Write not implemented"); + } + } + + internal sealed class GpReader : IDisposable + { + BinaryReader m_input; + GpMetaData m_info; + int m_width; + int m_height; + + public PixelFormat Format { get; private set; } + public BitmapPalette Palette { get; private set; } + public byte[] Data { get; private set; } + public int Stride { get; private set; } + + public GpReader (Stream input, GpMetaData info) + { + m_info = info; + m_width = (int)m_info.Width; + m_height = (int)m_info.Height; + m_input = new ArcView.Reader (input); + } + + public void Unpack () + { + m_input.BaseStream.Position = 0xD; + switch (m_info.Method) + { + case 0: UnpackV0(); break; + case 1: UnpackV1(); break; + case 2: UnpackV2(); break; + default: throw new NotSupportedException ("Not supported GPC image format"); + } + if (m_info.HasAlpha) + { + if (ReadAlpha()) + Format = PixelFormats.Bgra32; + } + } + + void UnpackV0 () + { + var image = m_input.ReadBytes (m_height * m_width * 3); + Stride = m_width * 4; + var pixels = new byte[Stride * m_height]; + int src = 0; + int dst = 0; + while (src < image.Length) + { + pixels[dst++] = image[src+2]; + pixels[dst++] = image[src+1]; + pixels[dst++] = image[src]; + src += 3; + dst++; + } + Data = pixels; + Format = PixelFormats.Bgr32; + } + + void UnpackV1 () + { + var palette = m_input.ReadBytes (3 * m_info.PaletteSize); + if (8 == m_info.BPP && !m_info.HasAlpha) + { + SetPalette (palette, m_info.PaletteSize); + Data = m_input.ReadBytes (m_width*m_height); + Format = PixelFormats.Indexed8; + Stride = m_width; + } + else + { + Data = ReadIndexedImage (palette); + Format = PixelFormats.Bgr32; + Stride = m_width * 4; + } + } + + byte[] ReadIndexedImage (byte[] palette) + { + int rgb_mask = (1 << m_info.BPP) - 1; + var chunk = new byte[4]; + var pixels = new byte[m_width*m_height*4]; + int dst = 0; + for (int y = 0; y < m_height; ++y) + { + int x = 0; + while (x < m_width) + { + m_input.Read (chunk, 0, m_info.ElementSize); + int color = LittleEndian.ToInt32 (chunk, 0); + for (int i = 0; i < m_info.PixelsPerElement & x < m_width; ++i) + { + int index = 3 * (color & rgb_mask); + if (index >= palette.Length) + throw new InvalidFormatException(); + color >>= m_info.BPP; + pixels[dst++] = palette[index+2]; + pixels[dst++] = palette[index+1]; + pixels[dst++] = palette[index]; + ++dst; + ++x; + } + } + } + return pixels; + } + + void SetPalette (byte[] palette_data, int colors) + { + var palette = new Color[colors]; + for (int i = 0; i < palette.Length; ++i) + { + int c = i * 3; + palette[i] = Color.FromRgb (palette_data[c], palette_data[c+1], palette_data[c+2]); + } + Palette = new BitmapPalette (palette); + } + + void UnpackV2 () + { + Stride = m_width * 4; + var palette = m_input.ReadBytes (3 * m_info.PaletteSize); + + int back1 = m_input.ReadInt32() * 3; // index within palette + int back2 = m_input.ReadInt32(); + if (back2 < 0) + back2 += m_info.PaletteSize; + back2 *= 3; + m_input.ReadInt32(); // data_size + + int rgb_mask = (1 << m_info.BPP) - 1; + + var pixels = new byte[Stride*m_height]; + int dst = 0; + var chunk = new byte[4]; + for (int y = 0; y < m_height; ++y) + { + int x = 0; + while (x < m_width) + { + ushort background_length = m_input.ReadUInt16(); + ushort foreground_length = m_input.ReadUInt16(); + int color; // index within palette + if ((background_length & 0x8000) == 0) + { + color = back1; + } + else + { + color = back2; + background_length &= 0x7FFF; + } + int i; + for (i = 0; i < background_length; ++i) + { + pixels[dst++] = palette[color+2]; + pixels[dst++] = palette[color+1]; + pixels[dst++] = palette[color]; + ++dst; + ++x; + } + i = 0; + while (i < foreground_length && x < m_width) + { + m_input.Read (chunk, 0, m_info.ElementSize); + int element = LittleEndian.ToInt32 (chunk, 0); + for (int j = 0; j != m_info.PixelsPerElement && x < m_width && i < foreground_length; ++j) + { + color = 3 * (element & rgb_mask); + element >>= m_info.BPP; + pixels[dst++] = palette[color+2]; + pixels[dst++] = palette[color+1]; + pixels[dst++] = palette[color]; + ++dst; + ++x; + ++i; + } + } + } + } + Data = pixels; + Format = PixelFormats.Bgr32; + } + + bool ReadAlpha () + { + var w = m_input.ReadInt32(); + var h = m_input.ReadInt32(); + if (w != m_width || h != m_height) + return false; + int i = 3; + while (i < Data.Length) + { + byte alpha = m_input.ReadByte(); + int count = m_input.ReadByte(); + for (int j = 0; j < count && i < Data.Length; ++j) + { + Data[i] = alpha; + i += 4; + } + } + return true; + } + + #region IDisposable Members + bool _disposed = false; + public void Dispose () + { + if (!_disposed) + { + m_input.Dispose(); + _disposed = true; + } + } + #endregion + } +} diff --git a/supported.html b/supported.html index 9b6c7a6e..686649e1 100644 --- a/supported.html +++ b/supported.html @@ -494,6 +494,9 @@ Natsumero
Ippai Shimasho
*.bmpFeNo +*.gpc+*.gph
*.snd+*.snh
*.snr+*.snh-NoEushully +Genrin no Kishougun
+

1 Non-encrypted only