(MCG): implemented 8bpp images.

This commit is contained in:
morkt
2017-01-14 12:24:11 +04:00
parent 6e9064bc50
commit 6604bbe698
2 changed files with 86 additions and 27 deletions

View File

@@ -30,17 +30,22 @@ using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace GameRes.Formats.FC01
{
internal class McaArchive : ArcFile
{
public readonly byte Key;
public readonly int BPP;
public readonly BitmapPalette Palette;
public McaArchive (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, byte key)
public McaArchive (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, byte key, int bpp = 24, BitmapPalette palette = null)
: base (arc, impl, dir)
{
Key = key;
BPP = bpp;
Palette = palette;
}
}
@@ -60,6 +65,13 @@ namespace GameRes.Formats.FC01
if (index_offset >= file.MaxOffset || !IsSaneCount (count))
return null;
int bpp = file.View.ReadInt32 (0x14);
BitmapPalette palette = null;
if (8 == bpp)
{
palette = ReadPalette (file, index_offset);
index_offset += 0x400;
}
string base_name = Path.GetFileNameWithoutExtension (file.Name);
long next_offset = file.View.ReadUInt32 (index_offset);
var dir = new List<Entry> (count);
@@ -82,7 +94,7 @@ namespace GameRes.Formats.FC01
if (0 == dir.Count)
return null;
var options = Query<McgOptions> (arcStrings.MCAEncryptedNotice);
return new McaArchive (file, this, dir, options.Key);
return new McaArchive (file, this, dir, options.Key, bpp, palette);
}
public override IImageDecoder OpenImage (ArcFile arc, Entry entry)
@@ -91,7 +103,7 @@ namespace GameRes.Formats.FC01
var input = arc.File.CreateStream (entry.Offset, entry.Size);
try
{
return new McaDecoder (input, mca.Key);
return new McaDecoder (input, mca.Key, mca.BPP, mca.Palette);
}
catch
{
@@ -100,6 +112,19 @@ namespace GameRes.Formats.FC01
}
}
BitmapPalette ReadPalette (ArcView file, uint offset)
{
var palette = file.View.ReadBytes (offset, 0x400);
int src = 0;
var colors = new Color[0x100];
for (int i = 0; i < 0x100; ++i)
{
colors[i] = Color.FromRgb (palette[src+2], palette[src+1], palette[src]);
src += 4;
}
return new BitmapPalette (colors);
}
public override ResourceOptions GetDefaultOptions ()
{
return new McgOptions { Key = Settings.Default.MCGLastKey };
@@ -122,22 +147,25 @@ namespace GameRes.Formats.FC01
internal sealed class McaDecoder : BinaryImageDecoder
{
byte m_key;
int m_method;
bool m_compressed;
int m_packed_size;
int m_unpacked_size;
BitmapPalette m_palette;
public McaDecoder (IBinaryStream input, byte key) : base (input)
public McaDecoder (IBinaryStream input, byte key, int bpp, BitmapPalette palette) : base (input)
{
m_key = key;
var header = m_input.ReadHeader (0x20);
m_method = header.ToInt32 (0);
if (m_method < 0 || m_method > 1)
int method = header.ToInt32 (0);
if (method < 0 || method > 1)
throw new InvalidFormatException();
m_compressed = method != 0;
uint width = header.ToUInt32 (0xC);
uint height = header.ToUInt32 (0x10);
m_packed_size = header.ToInt32 (0x14);
m_unpacked_size = header.ToInt32 (0x18);
Info = new ImageMetaData { Width = width, Height = height, BPP = 24 };
Info = new ImageMetaData { Width = width, Height = height, BPP = bpp };
m_palette = palette;
}
protected override ImageData GetImageData ()
@@ -145,7 +173,7 @@ namespace GameRes.Formats.FC01
m_input.Position = 0x20;
var data = m_input.ReadBytes (m_packed_size);
MrgOpener.Decrypt (data, 0, data.Length, m_key);
if (m_method > 0)
if (m_compressed)
{
using (var input = new BinMemoryStream (data))
using (var lzss = new MrgLzssReader (input, data.Length, m_unpacked_size))
@@ -154,8 +182,9 @@ namespace GameRes.Formats.FC01
data = lzss.Data;
}
}
int stride = ((int)Info.Width * 3 + 3) & ~3;
return ImageData.Create (Info, PixelFormats.Bgr24, null, data, stride);
int stride = ((int)Info.Width * Info.BPP / 8 + 3) & ~3;
var format = 8 == Info.BPP ? PixelFormats.Indexed8 : PixelFormats.Bgr24;
return ImageData.Create (Info, format, m_palette, data, stride);
}
}
}