implemented GPK2 archives and GFB images.

This commit is contained in:
morkt 2015-10-25 15:27:38 +04:00
parent 1ea054050c
commit b5e535835d
4 changed files with 230 additions and 0 deletions

View File

@ -51,6 +51,7 @@
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" />
@ -73,6 +74,8 @@
<Compile Include="Debonosu\ArcPAK.cs" />
<Compile Include="elf\ImageG24.cs" />
<Compile Include="elf\ImageGP8.cs" />
<Compile Include="Gpk2\ArcGPK2.cs" />
<Compile Include="Gpk2\ImageGFB.cs" />
<Compile Include="Sas5\ArcIAR.cs" />
<Compile Include="Sas5\ArcSec5.cs" />
<Compile Include="Sas5\ArcWAR.cs" />

View File

@ -0,0 +1,73 @@
//! \file ArcGPK2.cs
//! \date Sun Oct 25 12:53:49 2015
//! \brief GPK2 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.Collections.Generic;
using System.ComponentModel.Composition;
namespace GameRes.Formats.Gpk2
{
[Export(typeof(ArchiveFormat))]
public class GpkOpener : ArchiveFormat
{
public override string Tag { get { return "GPK2"; } }
public override string Description { get { return "GPK2 resource archive"; } }
public override uint Signature { get { return 0x324B5047; } } // 'GPK2'
public override bool IsHierarchic { get { return false; } }
public override bool CanCreate { get { return false; } }
public GpkOpener ()
{
Extensions = new string[] { "" };
}
public override ArcFile TryOpen (ArcView file)
{
uint index_offset = file.View.ReadUInt32 (4);
if (index_offset >= file.MaxOffset)
return null;
int count = file.View.ReadInt32 (index_offset);
if (!IsSaneCount (count))
return null;
index_offset += 4;
uint index_size = (uint)(count * 0x88);
if (index_size > file.View.Reserve (index_offset, index_size))
return null;
var dir = new List<Entry> (count);
for (int i = 0; i < count; ++i)
{
string name = file.View.ReadString (index_offset+8, 0x80);
var entry = FormatCatalog.Instance.Create<Entry> (name);
entry.Offset = file.View.ReadUInt32 (index_offset);
entry.Size = file.View.ReadUInt32 (index_offset+4);
if (!entry.CheckPlacement (file.MaxOffset))
return null;
dir.Add (entry);
index_offset += 0x88;
}
return new ArcFile (file, this, dir);
}
}
}

150
ArcFormats/Gpk2/ImageGFB.cs Normal file
View File

@ -0,0 +1,150 @@
//! \file ImageGFB.cs
//! \date Sun Oct 25 13:06:47 2015
//! \brief GPK2 image resource.
//
// 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 System.Windows.Media.Imaging;
using GameRes.Compression;
using GameRes.Utility;
namespace GameRes.Formats.Gpk2
{
internal class GfbMetaData : ImageMetaData
{
public int PackedSize;
public int UnpackedSize;
public int DataOffset;
}
[Export(typeof(ImageFormat))]
public class GfbFormat : ImageFormat
{
public override string Tag { get { return "GFB"; } }
public override string Description { get { return "GPK2 image format"; } }
public override uint Signature { get { return 0x20424647; } } // 'GFB '
public override ImageMetaData ReadMetaData (Stream stream)
{
var header = new byte[0x40];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
return new GfbMetaData
{
Width = LittleEndian.ToUInt32 (header, 0x1C),
Height = LittleEndian.ToUInt32 (header, 0x20),
BPP = LittleEndian.ToUInt16 (header, 0x26),
PackedSize = LittleEndian.ToInt32 (header,0x0C),
UnpackedSize = LittleEndian.ToInt32 (header,0x10),
DataOffset = LittleEndian.ToInt32 (header,0x14),
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
{
var meta = (GfbMetaData)info;
BitmapPalette palette = null;
if (8 == meta.BPP && meta.DataOffset != 0x40)
{
stream.Position = 0x40;
palette = ReadPalette (stream, meta.DataOffset - 0x40);
}
stream.Position = meta.DataOffset;
byte[] pixels = new byte[meta.UnpackedSize];
if (0 != meta.PackedSize)
{
using (var lzss = new LzssStream (stream, LzssMode.Decompress, true))
lzss.Read (pixels, 0, pixels.Length);
}
else
stream.Read (pixels, 0, pixels.Length);
PixelFormat format;
switch (meta.BPP)
{
case 32:
if (HasAlphaChannel (pixels))
format = PixelFormats.Bgra32;
else
format = PixelFormats.Bgr32;
break;
case 24:
format = PixelFormats.Bgr24;
break;
case 16:
format = PixelFormats.Bgr565;
break;
case 8:
if (null != palette)
format = PixelFormats.Indexed8;
else
format = PixelFormats.Gray8;
break;
default:
throw new NotSupportedException ("Not supported GFB color depth");
}
int stride = pixels.Length / (int)info.Height;
return ImageData.CreateFlipped (info, format, palette, pixels, stride);
}
BitmapPalette ReadPalette (Stream input, int palette_size)
{
palette_size = Math.Min (0x400, palette_size);
var palette_data = new byte[palette_size];
if (palette_data.Length != input.Read (palette_data, 0, palette_data.Length))
throw new EndOfStreamException();
int color_size = palette_size / 0x100;
var palette = new Color[0x100];
for (int i = 0; i < palette.Length; ++i)
{
int c = i * color_size;
palette[i] = Color.FromRgb (palette_data[c+2], palette_data[c+1], palette_data[c]);
}
return new BitmapPalette (palette);
}
static bool HasAlphaChannel (byte[] pixels)
{
for (int p = 3; p < pixels.Length; p += 4)
{
if (pixels[p] > 0)
return true;
}
return false;
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("GfbFormat.Write not implemented");
}
}
}

View File

@ -441,6 +441,10 @@ Houkago ~Nureta Seifuku~<br/>
Yakata ~Kannou Kitan~<br/>
</td></tr>
<tr class="odd"><td>*.bsg</td><td><tt>BSS-Graphics</tt><br/><tt>BSS-Composition</tt></td><td>No</td></tr>
<tr><td>*</td><td><tt>GPK2</tt></td><td>No</td><td rowspan="2">Studio Ryokucha</td><td rowspan="2">
Princess Serenade<br/>
</td></tr>
<tr><td>*.gfb</td><td><tt>GFB</tt></td><td>No</td></tr>
</table>
<p><a name="note-1" class="footnote">1</a> Non-encrypted only</p>
</body>