2016-05-21 01:53:09 +08:00
|
|
|
//! \file ImageGRP.cs
|
|
|
|
//! \date Fri May 20 20:30:01 2016
|
|
|
|
//! \brief NeXAS GRP image format.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2016 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;
|
2017-12-09 22:10:40 +08:00
|
|
|
using System.Windows.Media.Imaging;
|
2016-05-21 01:53:09 +08:00
|
|
|
using GameRes.Utility;
|
|
|
|
|
|
|
|
namespace GameRes.Formats.NeXAS
|
|
|
|
{
|
|
|
|
internal class GrpMetaData : ImageMetaData
|
|
|
|
{
|
2017-12-09 22:10:40 +08:00
|
|
|
public int Version;
|
2016-05-21 01:53:09 +08:00
|
|
|
public int UnpackedSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Export(typeof(ImageFormat))]
|
|
|
|
public class GrpFormat : ImageFormat
|
|
|
|
{
|
|
|
|
public override string Tag { get { return "GR3"; } }
|
|
|
|
public override string Description { get { return "NeXAS engine image format"; } }
|
|
|
|
public override uint Signature { get { return 0x18335247; } }
|
|
|
|
|
|
|
|
public GrpFormat ()
|
|
|
|
{
|
2017-12-09 22:10:40 +08:00
|
|
|
Signatures = new uint[] { 0x18335247, 0x08325247, 0x10325247, 0x18325247, 0 };
|
2016-05-21 01:53:09 +08:00
|
|
|
Extensions = new string[] { "grp" };
|
|
|
|
}
|
|
|
|
|
2016-10-16 13:22:53 +08:00
|
|
|
public override ImageMetaData ReadMetaData (IBinaryStream stream)
|
2016-05-21 01:53:09 +08:00
|
|
|
{
|
2016-10-16 13:22:53 +08:00
|
|
|
var header = stream.ReadHeader (0x11);
|
2017-12-09 22:10:40 +08:00
|
|
|
int version = header[2] - '0';
|
|
|
|
if (!header.AsciiEqual ("GR") || version < 1 || version > 3)
|
|
|
|
return null;
|
|
|
|
var info = new GrpMetaData {
|
2016-10-16 13:22:53 +08:00
|
|
|
Width = header.ToUInt32 (5),
|
|
|
|
Height = header.ToUInt32 (9),
|
|
|
|
BPP = header.ToUInt16 (3),
|
2017-12-09 22:10:40 +08:00
|
|
|
Version = version
|
2016-05-21 01:53:09 +08:00
|
|
|
};
|
2017-12-09 22:10:40 +08:00
|
|
|
if (version > 1)
|
|
|
|
info.UnpackedSize = header.ToInt32 (0xD);
|
|
|
|
else
|
|
|
|
info.UnpackedSize = (int)info.Width * (int)info.Height * info.BPP / 8
|
|
|
|
+ (info.BPP == 8 ? 0x300 : 0);
|
|
|
|
return info;
|
2016-05-21 01:53:09 +08:00
|
|
|
}
|
|
|
|
|
2016-10-16 13:22:53 +08:00
|
|
|
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
|
2016-05-21 01:53:09 +08:00
|
|
|
{
|
2017-12-09 22:10:40 +08:00
|
|
|
var reader = new GrpReader (stream, (GrpMetaData)info);
|
|
|
|
var pixels = reader.Unpack();
|
|
|
|
return ImageData.Create (info, reader.Format, reader.Palette, reader.Data);
|
2016-05-21 01:53:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void Write (Stream file, ImageData image)
|
|
|
|
{
|
|
|
|
throw new System.NotImplementedException ("GrpFormat.Write not implemented");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-09 22:10:40 +08:00
|
|
|
internal sealed class GrpReader
|
2016-05-21 01:53:09 +08:00
|
|
|
{
|
2016-10-16 13:22:53 +08:00
|
|
|
IBinaryStream m_input;
|
2016-05-21 01:53:09 +08:00
|
|
|
byte[] m_output;
|
2017-12-09 22:10:40 +08:00
|
|
|
GrpMetaData m_info;
|
|
|
|
readonly int m_count_bits;
|
|
|
|
readonly int m_count_mask;
|
2016-05-21 01:53:09 +08:00
|
|
|
|
2017-12-09 22:10:40 +08:00
|
|
|
public byte[] Data { get { return m_output; } }
|
|
|
|
public PixelFormat Format { get; private set; }
|
|
|
|
public BitmapPalette Palette { get; private set; }
|
2016-05-21 01:53:09 +08:00
|
|
|
|
2016-10-16 13:22:53 +08:00
|
|
|
public GrpReader (IBinaryStream input, GrpMetaData info)
|
2016-05-21 01:53:09 +08:00
|
|
|
{
|
2016-10-16 13:22:53 +08:00
|
|
|
m_input = input;
|
2016-05-21 01:53:09 +08:00
|
|
|
m_output = new byte[info.UnpackedSize];
|
2017-12-09 22:10:40 +08:00
|
|
|
m_info = info;
|
|
|
|
switch (info.BPP)
|
|
|
|
{
|
|
|
|
case 8: Format = PixelFormats.Indexed8; break;
|
|
|
|
case 16: Format = PixelFormats.Bgr555; break;
|
|
|
|
case 24: Format = PixelFormats.Bgr24; break;
|
|
|
|
case 32: Format = PixelFormats.Bgr32; break;
|
|
|
|
default: throw new NotSupportedException ("Not supported GRP image color depth");
|
|
|
|
}
|
|
|
|
m_count_bits = info.Version > 2 ? 3 : 5;
|
|
|
|
m_count_mask = ~(-1 << m_count_bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] Unpack ()
|
|
|
|
{
|
|
|
|
if (m_info.Version < 2)
|
|
|
|
{
|
|
|
|
m_input.Position = 0xD;
|
|
|
|
m_input.Read (m_output, 0, m_output.Length);
|
|
|
|
}
|
2016-05-21 01:53:09 +08:00
|
|
|
else
|
2017-12-09 22:10:40 +08:00
|
|
|
{
|
|
|
|
Decompress();
|
|
|
|
}
|
|
|
|
if (8 == m_info.BPP)
|
|
|
|
{
|
|
|
|
using (var input = new MemoryStream (m_output, m_output.Length-0x300, 0x300))
|
|
|
|
Palette = ImageFormat.ReadPalette (input, 0x100, PaletteFormat.Rgb);
|
|
|
|
}
|
|
|
|
return m_output;
|
2016-05-21 01:53:09 +08:00
|
|
|
}
|
|
|
|
|
2017-12-09 22:10:40 +08:00
|
|
|
void Decompress ()
|
2016-05-21 01:53:09 +08:00
|
|
|
{
|
2016-10-16 13:22:53 +08:00
|
|
|
m_input.Position = 0x11;
|
2016-05-21 01:53:09 +08:00
|
|
|
int ctl_length = (m_input.ReadInt32() + 7) / 8;
|
|
|
|
var ctl_bytes = m_input.ReadBytes (ctl_length);
|
|
|
|
m_input.ReadInt32();
|
|
|
|
using (var ctl_mem = new MemoryStream (ctl_bytes))
|
|
|
|
using (var bits = new LsbBitStream (ctl_mem))
|
|
|
|
{
|
|
|
|
int dst = 0;
|
|
|
|
while (dst < m_output.Length)
|
|
|
|
{
|
|
|
|
int bit = bits.GetNextBit();
|
|
|
|
if (-1 == bit)
|
|
|
|
break;
|
|
|
|
if (0 == bit)
|
|
|
|
{
|
2016-10-16 13:22:53 +08:00
|
|
|
m_output[dst++] = m_input.ReadUInt8();
|
2016-05-21 01:53:09 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int offset = m_input.ReadUInt16();
|
2017-12-09 22:10:40 +08:00
|
|
|
int count = (offset & m_count_mask) + 1;
|
|
|
|
offset = (offset >> m_count_bits) + 1;
|
2016-05-21 01:53:09 +08:00
|
|
|
Binary.CopyOverlapped (m_output, dst - offset, dst, count);
|
|
|
|
dst += count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|