Formats update.

(HG3): attempt to recognize swapped Red/Blue channels.
Fairytale BDT archives.
(XP3, VF): add 'exe' to extensions list.
(DXR): consolidated Macromedia Director archives.
(SND): support mp3 streams.
(QPK): PSP resource archive.
(GRC): support encrypted images.
This commit is contained in:
morkt
2023-09-14 19:17:31 +04:00
parent b73bd0fb4d
commit bfd81f9ec4
23 changed files with 1081 additions and 426 deletions

View File

@@ -1,8 +1,8 @@
//! \file ImageBITD.cs
//! \date Fri Jun 26 07:45:01 2015
//! \brief Selen image format.
//! \brief Macromedia Director image format.
//
// Copyright (C) 2015 by morkt
// Copyright (C) 2015-2023 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
@@ -34,174 +34,9 @@ using GameRes.Utility;
namespace GameRes.Formats.Macromedia
{
[Export(typeof(ImageFormat))]
public class BitdFormat : ImageFormat
internal class BitdMetaData : ImageMetaData
{
public override string Tag { get { return "BITD"; } }
public override string Description { get { return "Selen RLE-compressed bitmap"; } }
public override uint Signature { get { return 0; } }
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
if (stream.Length > 0xffffff)
return null;
var scanner = new BitdScanner (stream.AsStream);
return scanner.GetInfo();
}
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var reader = new BitdReader (stream.AsStream, info);
reader.Unpack();
return ImageData.Create (info, reader.Format, null, reader.Data);
}
public override void Write (Stream file, ImageData image)
{
throw new NotImplementedException ("BitdFormat.Write not implemented");
}
}
internal class BitdScanner
{
Stream m_input;
protected Stream Input { get { return m_input; } }
public BitdScanner (Stream input)
{
m_input = input;
}
const int MaxScanLine = 2048;
public ImageMetaData GetInfo ()
{
int total = 0;
var scan_lines = new Dictionary<int, int>();
var key_lines = new List<int>();
for (;;)
{
int b = m_input.ReadByte();
if (-1 == b)
break;
int count = b;
if (b > 0x7f)
count = (byte)-(sbyte)b;
++count;
if (count > 0x7f)
return null;
if (b > 0x7f)
{
if (-1 == m_input.ReadByte())
return null;
}
else
m_input.Seek (count, SeekOrigin.Current);
key_lines.Clear();
key_lines.AddRange (scan_lines.Keys);
foreach (var line in key_lines)
{
int width = scan_lines[line];
if (width < count)
scan_lines.Remove (line);
else if (width == count)
scan_lines[line] = line;
else
scan_lines[line] = width - count;
}
total += count;
if (total <= MaxScanLine && total >= 8)
scan_lines[total] = total;
if (total > MaxScanLine && !scan_lines.Any())
return null;
}
int rem;
total = Math.DivRem (total, 4, out rem);
if (rem != 0)
return null;
var valid_lines = from line in scan_lines where line.Key == line.Value
orderby line.Key
select line.Key;
bool is_eof = -1 == m_input.ReadByte();
foreach (var width in valid_lines)
{
int height = Math.DivRem (total, width, out rem);
if (0 == rem)
{
return new ImageMetaData
{
Width = (uint)width,
Height = (uint)height,
BPP = 32,
};
}
}
return null;
}
}
internal class BitdReader : BitdScanner
{
byte[] m_output;
int m_width;
int m_height;
public byte[] Data { get { return m_output; } }
public PixelFormat Format { get; private set; }
public BitdReader (Stream input, ImageMetaData info) : base (input)
{
m_width = (int)info.Width;
m_height = (int)info.Height;
m_output = new byte[m_width * m_height * 4];
Format = PixelFormats.Bgra32;
}
public void Unpack ()
{
int stride = m_width * 4;
var scan_line = new byte[stride];
for (int line = 0; line < m_output.Length; line += stride)
{
int dst = 0;
while (dst < stride)
{
int b = Input.ReadByte();
if (-1 == b)
throw new InvalidFormatException ("Unexpected end of file");
int count = b;
if (b > 0x7f)
count = (byte)-(sbyte)b;
++count;
if (dst + count > stride)
throw new InvalidFormatException();
if (b > 0x7f)
{
b = Input.ReadByte();
if (-1 == b)
throw new InvalidFormatException ("Unexpected end of file");
for (int i = 0; i < count; ++i)
scan_line[dst++] = (byte)b;
}
else
{
Input.Read (scan_line, dst, count);
dst += count;
}
}
dst = line;
for (int x = 0; x < m_width; ++x)
{
m_output[dst++] = scan_line[x+m_width*3];
m_output[dst++] = scan_line[x+m_width*2];
m_output[dst++] = scan_line[x+m_width];
m_output[dst++] = scan_line[x];
}
}
}
public byte DepthType;
}
internal class BitdDecoder : IImageDecoder
@@ -216,13 +51,13 @@ namespace GameRes.Formats.Macromedia
BitmapPalette m_palette;
public Stream Source => m_input;
public ImageFormat SourceFormat => null;
public ImageFormat SourceFormat { get; private set; }
public ImageMetaData Info => m_info;
public ImageData Image => m_image ?? (m_image = GetImageData());
public PixelFormat Format { get; private set; }
public byte[] AlphaChannel { get; set; }
public BitdDecoder (Stream input, ImageMetaData info, BitmapPalette palette)
public BitdDecoder (Stream input, BitdMetaData info, BitmapPalette palette)
{
m_input = input;
m_info = info;
@@ -235,23 +70,55 @@ namespace GameRes.Formats.Macromedia
: info.BPP == 4 ? PixelFormats.Indexed4
: info.BPP == 8 ? PixelFormats.Indexed8
: info.BPP == 16 ? PixelFormats.Bgr555
: PixelFormats.Bgr32;
: info.DepthType == 0x87 // i have no clue what this is
|| info.DepthType == 0x8A ? PixelFormats.Bgra32 // depth type 0x87/0x8A
: PixelFormats.Bgra32; // depth type 0x82/84/85/86/8C
m_palette = palette;
}
private BitdDecoder (Stream input, ImageMetaData info, byte[] alpha_channel)
{
m_input = input;
m_info = info;
m_width = info.iWidth;
m_height = info.iHeight;
m_stride = (m_width * m_info.BPP + 7) / 8;
Format = PixelFormats.Bgra32;
AlphaChannel = alpha_channel;
SourceFormat = ImageFormat.Jpeg;
}
public static IImageDecoder FromJpeg (Stream input, ImageMetaData info, byte[] alpha_channel)
{
return new BitdDecoder (input, info, alpha_channel);
}
protected ImageData GetImageData ()
{
BitmapSource bitmap = null;
m_input.Position = 0;
if (Info.BPP <= 8)
if (SourceFormat == ImageFormat.Jpeg)
{
var decoder = new JpegBitmapDecoder (m_input, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
bitmap = decoder.Frames[0];
if (null == AlphaChannel)
return new ImageData (bitmap, m_info);
}
else if (Info.BPP > 8)
UnpackChannels (Info.BPP / 8);
else if (m_output.Length != m_input.Length)
Unpack8bpp();
else
UnpackChannels (Info.BPP / 8);
m_input.Read (m_output, 0, m_output.Length);
if (AlphaChannel != null)
{
if (Info.BPP != 32)
if (Info.BPP != 32 || bitmap != null)
{
BitmapSource bitmap = BitmapSource.Create (Info.iWidth, Info.iHeight, ImageData.DefaultDpiX, ImageData.DefaultDpiY, Format, m_palette, m_output, m_stride);
bitmap = new FormatConvertedBitmap (bitmap, PixelFormats.Bgr32, null, 0);
if (bitmap == null)
bitmap = BitmapSource.Create (m_width, m_height, ImageData.DefaultDpiX, ImageData.DefaultDpiY, Format, m_palette, m_output, m_stride);
if (Info.BPP != 32)
bitmap = new FormatConvertedBitmap (bitmap, PixelFormats.Bgr32, null, 0);
m_stride = bitmap.PixelWidth * 4;
m_output = new byte[bitmap.PixelHeight * m_stride];
bitmap.CopyPixels (m_output, m_stride, 0);
@@ -324,7 +191,8 @@ namespace GameRes.Formats.Macromedia
{
int b = m_input.ReadByte();
if (-1 == b)
throw new InvalidFormatException ("Unexpected end of file");
break; // one in 5000 images somehow stumbles here
// throw new InvalidFormatException ("Unexpected end of file");
int count = b;
if (b > 0x7f)
count = (byte)-(sbyte)b;