diff --git a/ArcFormats/ArcFormats.csproj b/ArcFormats/ArcFormats.csproj index 4c8e7e6b..c3ab6ab9 100644 --- a/ArcFormats/ArcFormats.csproj +++ b/ArcFormats/ArcFormats.csproj @@ -119,6 +119,7 @@ + @@ -199,6 +200,7 @@ + diff --git a/ArcFormats/ArcPK.cs b/ArcFormats/ArcPK.cs new file mode 100644 index 00000000..9f8a60dd --- /dev/null +++ b/ArcFormats/ArcPK.cs @@ -0,0 +1,82 @@ +//! \file ArcPK.cs +//! \date Wed Jul 15 00:19:23 2015 +//! \brief U-Me soft resource 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 System; +using System.Collections.Generic; +using System.ComponentModel.Composition; +using System.IO; +using GameRes.Utility; + +namespace GameRes.Formats.UMeSoft +{ + [Export(typeof(ArchiveFormat))] + public class PkOpener : ArchiveFormat + { + public override string Tag { get { return "PK"; } } + public override string Description { get { return "U-Me Soft resources archive"; } } + public override uint Signature { get { return 0; } } + public override bool IsHierarchic { get { return false; } } + public override bool CanCreate { get { return false; } } + + public PkOpener () + { + Extensions = new string[] { "pk", "gpk", "tpk", "wpk", "mpk" }; + } + + public override ArcFile TryOpen (ArcView file) + { + long index_end = file.MaxOffset - 4; + uint index_size = file.View.ReadUInt32 (index_end); + if (0 == index_size || index_size >= index_end) + return null; + + long index_offset = index_end - index_size; + if (index_size > file.View.Reserve (index_offset, index_size)) + return null; + + var dir = new List(); + while (index_offset < index_end) + { + uint name_len = file.View.ReadByte (index_offset++); + if (0 == name_len) + break; + if (name_len+14 > index_end-index_offset) + return null; + string name = file.View.ReadString (index_offset, name_len); + index_offset += name_len+6; + var entry = FormatCatalog.Instance.CreateEntry (name); + entry.Size = file.View.ReadUInt32 (index_offset); + entry.Offset = file.View.ReadUInt32 (index_offset+4); + if (!entry.CheckPlacement (index_offset)) + return null; + dir.Add (entry); + index_offset += 8; + } + if (0 == dir.Count) + return null; + return new ArcFile (file, this, dir); + } + } +} diff --git a/ArcFormats/ImageGRX.cs b/ArcFormats/ImageGRX.cs new file mode 100644 index 00000000..233cf816 --- /dev/null +++ b/ArcFormats/ImageGRX.cs @@ -0,0 +1,298 @@ +//! \file ImageGRX.cs +//! \date Wed Jul 15 00:59:44 2015 +//! \brief U-Me Soft 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.ComponentModel.Composition; +using System.IO; +using System.Windows.Media; +using GameRes.Utility; + +namespace GameRes.Formats.UMeSoft +{ + internal class GrxMetaData : ImageMetaData + { + public bool IsPacked; + public bool HasAlpha; + public int AlphaOffset; + } + + [Export(typeof(ImageFormat))] + public class GrxFormat : ImageFormat + { + public override string Tag { get { return "GRX"; } } + public override string Description { get { return "U-Me Soft image format"; } } + public override uint Signature { get { return 0x1A585247; } } // 'GRX' + + public override ImageMetaData ReadMetaData (Stream stream) + { + byte[] header = new byte[0x10]; + if (header.Length != stream.Read (header, 0, header.Length)) + return null; + return new GrxMetaData + { + Width = LittleEndian.ToUInt16 (header, 8), + Height = LittleEndian.ToUInt16 (header, 10), + BPP = LittleEndian.ToUInt16 (header, 6), + IsPacked = 0 != header[4], + HasAlpha = 0 != header[5], + AlphaOffset = LittleEndian.ToInt32 (header, 12), + }; + } + + public override ImageData Read (Stream stream, ImageMetaData info) + { + var meta = info as GrxMetaData; + if (null == meta) + throw new ArgumentException ("GrxFormat.Read should be supplied with GrxMetaData", "info"); + + var reader = new Reader (stream, meta); + reader.Unpack(); + return ImageData.Create (info, reader.Format, null, reader.Data, reader.Stride); + } + + public override void Write (Stream file, ImageData image) + { + throw new System.NotImplementedException ("GrxFormat.Write not implemented"); + } + + internal class Reader + { + Stream m_input; + byte[] m_output; + GrxMetaData m_info; + int m_pixel_size; + uint m_aligned_width; + + public byte[] Data { get { return m_output; } } + public PixelFormat Format { get; private set; } + public int Stride { get; private set; } + + public Reader (Stream input, GrxMetaData info) + { + m_input = input; + m_info = info; + m_input.Seek (0x10, SeekOrigin.Current); + m_pixel_size = m_info.BPP / 8; + switch (m_info.BPP) + { + case 32: + Format = PixelFormats.Bgr32; + break; + case 24: + Format = PixelFormats.Bgr32; + m_pixel_size = 4; + break; + case 16: + Format = PixelFormats.Bgr565; + break; + case 8: + Format = PixelFormats.Gray8; + break; + default: + throw new InvalidFormatException(); + } + m_aligned_width = (m_info.Width + 3u) & ~3u; + Stride = (int)(m_aligned_width * m_pixel_size); + m_output = new byte[Stride * (int)info.Height]; + } + + public void Unpack () + { + if (!m_info.IsPacked) + m_input.Read (m_output, 0, m_output.Length); + else + UnpackColorData (m_output, m_info.BPP/8, m_pixel_size); + + if (m_info.BPP >= 24 && m_info.HasAlpha && m_info.AlphaOffset > 0) + { + Format = PixelFormats.Bgra32; + m_input.Position = m_info.AlphaOffset; + var alpha = new byte[m_aligned_width * m_info.Height]; + UnpackColorData (alpha, 1, 1); + int dst = 3; + int src = 0; + for (uint y = 0; y < m_info.Height; ++y) + { + for (uint x = 0; x < m_aligned_width; ++x) + { + m_output[dst] = alpha[src++]; + dst += 4; + } + } + } + } + + static readonly int[,] OffsetTable = new int[2,16] { + { 0, -1, -1, -1, 0, -2, -2, -2, 0, -4, -4, -4, -2, -2, -4, -4 }, + { 0, 0, -1, 1, -2, 0, -2, 2, -4, 0, -4, 4, -4, 4, -2, 2 }, + }; + + void UnpackColorData (byte[] output, int src_pixel_size, int dst_pixel_size) + { + int[] offset_step = new int[16]; + + int stride = ((int)m_info.Width * dst_pixel_size + 3) & ~3; + int delta = stride - (int)m_info.Width * dst_pixel_size; + for (int i = 0; i < 16; i++) + offset_step[i] = OffsetTable[0,i] * stride + OffsetTable[1,i] * dst_pixel_size; + + int dst = 0; + for (uint y = 0; y < m_info.Height; ++y) + { + int w = (int)m_info.Width; + while (w > 0) + { + int flag = m_input.ReadByte(); + if (-1 == flag) + throw new InvalidFormatException(); + + int count = flag & 3; + if (0 != (flag & 4)) + { + count |= m_input.ReadByte() << 2; + } + w -= ++count; + if (0 == (flag & 0xF0)) + { + if (0 != (flag & 8)) + { + if (src_pixel_size == dst_pixel_size) + { + count *= dst_pixel_size; + if (count != m_input.Read (output, dst, count)) + throw new InvalidFormatException(); + dst += count; + } + else + { + for (int i = 0; i < count; ++i) + { + if (src_pixel_size != m_input.Read (output, dst, src_pixel_size)) + throw new InvalidFormatException(); + dst += dst_pixel_size; + } + } + } + else + { + if (src_pixel_size != m_input.Read (output, dst, src_pixel_size)) + throw new InvalidFormatException(); + --count; + dst += dst_pixel_size; + for (int i = count*dst_pixel_size; i > 0; i--) + { + output[dst] = output[dst-dst_pixel_size]; + dst++; + } + } + } + else + { + int src = dst + offset_step[flag >> 4]; + if (0 == (flag & 8)) + { + for (int i = 0; i < count; i++) + { + for (int j = 0; j < src_pixel_size; ++j) + output[dst+j] = output[src+j]; + dst += dst_pixel_size; + } + } + else + { + count *= dst_pixel_size; + Binary.CopyOverlapped (output, src, dst, count); + dst += count; + } + } + } + dst += delta; + } + } + } + } + + // SGX header format + // signature 32bit + // GRX offset 32bit + // number of frames 32bit + // ... frames info (* number of frames) + // x coordinate 16bit + // y coordinate 16bit + // frame width 16bit + // frame height 16bit + // transparency color 32bit + + internal class SgxMetaData : ImageMetaData + { + public int GrxOffset; + public ImageMetaData GrxInfo; + } + + [Export(typeof(ImageFormat))] + public class SgxFormat : GrxFormat + { + public override string Tag { get { return "SGX"; } } + public override string Description { get { return "U-Me Soft multi-frame image format"; } } + public override uint Signature { get { return 0x1A584753; } } // 'SGX' + + public SgxFormat () + { + Extensions = new string[] { "grx" }; + } + + public override ImageMetaData ReadMetaData (Stream stream) + { + using (var reader = new ArcView.Reader (stream)) + { + stream.Seek (4, SeekOrigin.Current); + int offset = reader.ReadInt32(); + if (offset <= 8) + return null; + stream.Position = offset; + uint signature = reader.ReadUInt32(); + if (signature != base.Signature) + return null; + stream.Seek (-4, SeekOrigin.Current); + var info = base.ReadMetaData (stream) as GrxMetaData; + if (null == info) + return null; + if (info.AlphaOffset > 0) + info.AlphaOffset += offset; + return new SgxMetaData { GrxOffset = offset, GrxInfo = info }; + } + } + + public override ImageData Read (Stream stream, ImageMetaData info) + { + var meta = info as SgxMetaData; + if (null == meta) + throw new ArgumentException ("SgxFormat.Read should be supplied with SgxMetaData", "info"); + + stream.Position = meta.GrxOffset; + return base.Read (stream, meta.GrxInfo); + } + } +} diff --git a/supported.html b/supported.html index 9fe50e8d..0bcc8f37 100644 --- a/supported.html +++ b/supported.html @@ -126,11 +126,14 @@ Swan Song
*.dat-NoBlack RainbowSaiminjutsu *.bmd_BMDYes *.dat-YesStudio e.go!Men at Work 2 -*.mbl-NoMarble -Ikusa Otome Valkyrie
+*.mbl-NoMarble Chikatetsu Fuusa Jiken
+Eien no Owari ni
+Ikusa Otome Valkyrie
+Sakura Machizaka Stories vol.1
*.prsYBNo +*.wayWADYNo *.dat-NoM no VioletNanase Ren *gra
masNo *.ald-NoAlice SoftTsuma Shibori @@ -250,6 +253,10 @@ Jokei Kazoku ~Inbou~
*.mrgMRGNoF&CKonata yori Kanata made *.mcgMCG 2.00No *.acdACD 1.00No +*.pk
*.gpk
*.tpk
*.wpk
-NoU-Me Soft +Fetish 2: Omote no Kioku/Ura no Kioku
+ +*.grxGRX\x1a
SGX\x1aNo

[1] Non-encrypted only