(GrpFormat): added GR2 images implementation.

This commit is contained in:
morkt 2017-12-09 18:10:40 +04:00
parent 0b2f44e005
commit 3052cccd50

View File

@ -27,12 +27,14 @@ using System;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.IO; using System.IO;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging;
using GameRes.Utility; using GameRes.Utility;
namespace GameRes.Formats.NeXAS namespace GameRes.Formats.NeXAS
{ {
internal class GrpMetaData : ImageMetaData internal class GrpMetaData : ImageMetaData
{ {
public int Version;
public int UnpackedSize; public int UnpackedSize;
} }
@ -45,28 +47,35 @@ namespace GameRes.Formats.NeXAS
public GrpFormat () public GrpFormat ()
{ {
Signatures = new uint[] { 0x18335247, 0x08325247, 0x10325247, 0x18325247, 0 };
Extensions = new string[] { "grp" }; Extensions = new string[] { "grp" };
} }
public override ImageMetaData ReadMetaData (IBinaryStream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = stream.ReadHeader (0x11); var header = stream.ReadHeader (0x11);
return new GrpMetaData int version = header[2] - '0';
{ if (!header.AsciiEqual ("GR") || version < 1 || version > 3)
return null;
var info = new GrpMetaData {
Width = header.ToUInt32 (5), Width = header.ToUInt32 (5),
Height = header.ToUInt32 (9), Height = header.ToUInt32 (9),
BPP = header.ToUInt16 (3), BPP = header.ToUInt16 (3),
UnpackedSize = header.ToInt32 (0xD), Version = version
}; };
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;
} }
public override ImageData Read (IBinaryStream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
using (var reader = new GrpReader (stream, (GrpMetaData)info)) var reader = new GrpReader (stream, (GrpMetaData)info);
{ var pixels = reader.Unpack();
reader.Unpack(); return ImageData.Create (info, reader.Format, reader.Palette, reader.Data);
return ImageData.Create (info, reader.Format, null, reader.Data);
}
} }
public override void Write (Stream file, ImageData image) public override void Write (Stream file, ImageData image)
@ -75,27 +84,55 @@ namespace GameRes.Formats.NeXAS
} }
} }
internal sealed class GrpReader : IDisposable internal sealed class GrpReader
{ {
IBinaryStream m_input; IBinaryStream m_input;
byte[] m_output; byte[] m_output;
GrpMetaData m_info;
readonly int m_count_bits;
readonly int m_count_mask;
public byte[] Data { get { return m_output; } } public byte[] Data { get { return m_output; } }
public PixelFormat Format { get; private set; } public PixelFormat Format { get; private set; }
public BitmapPalette Palette { get; private set; }
public GrpReader (IBinaryStream input, GrpMetaData info) public GrpReader (IBinaryStream input, GrpMetaData info)
{ {
m_input = input; m_input = input;
m_output = new byte[info.UnpackedSize]; m_output = new byte[info.UnpackedSize];
if (24 == info.BPP) m_info = info;
Format = PixelFormats.Bgr24; switch (info.BPP)
else if (32 == info.BPP) {
Format = PixelFormats.Bgr32; case 8: Format = PixelFormats.Indexed8; break;
else case 16: Format = PixelFormats.Bgr555; break;
throw new NotSupportedException ("Not supported GRP image color depth"); 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 void Unpack () public byte[] Unpack ()
{
if (m_info.Version < 2)
{
m_input.Position = 0xD;
m_input.Read (m_output, 0, m_output.Length);
}
else
{
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;
}
void Decompress ()
{ {
m_input.Position = 0x11; m_input.Position = 0x11;
int ctl_length = (m_input.ReadInt32() + 7) / 8; int ctl_length = (m_input.ReadInt32() + 7) / 8;
@ -117,19 +154,13 @@ namespace GameRes.Formats.NeXAS
else else
{ {
int offset = m_input.ReadUInt16(); int offset = m_input.ReadUInt16();
int count = (offset & 7) + 1; int count = (offset & m_count_mask) + 1;
offset = (offset >> 3) + 1; offset = (offset >> m_count_bits) + 1;
Binary.CopyOverlapped (m_output, dst - offset, dst, count); Binary.CopyOverlapped (m_output, dst - offset, dst, count);
dst += count; dst += count;
} }
} }
} }
} }
#region IDisposable Members
public void Dispose ()
{
}
#endregion
} }
} }