2015-08-01 22:32:07 +08:00
|
|
|
//! \file ImageDWQ.cs
|
|
|
|
//! \date Sat Aug 01 13:18:46 2015
|
|
|
|
//! \brief Black Cyc image format.
|
|
|
|
//
|
2016-05-20 08:10:12 +08:00
|
|
|
// Copyright (C) 2015-2016 by morkt
|
2015-08-01 22:32:07 +08:00
|
|
|
//
|
|
|
|
// 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.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using System.Windows.Media;
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
using GameRes.Utility;
|
|
|
|
|
|
|
|
namespace GameRes.Formats.BlackCyc
|
|
|
|
{
|
|
|
|
internal class ResourceHeader
|
|
|
|
{
|
|
|
|
public static readonly Regex PackTypeRe = new Regex (@"^PACKTYPE=(\d+)(A?) +$");
|
|
|
|
|
|
|
|
public byte[] Bytes { get; private set; }
|
|
|
|
public int PackType { get; private set; }
|
|
|
|
public bool AType { get; private set; }
|
|
|
|
|
|
|
|
public static ResourceHeader Read (Stream file)
|
|
|
|
{
|
2015-08-03 02:59:57 +08:00
|
|
|
var header = new byte[0x40];
|
|
|
|
if (0x40 != file.Read (header, 0, 0x40))
|
2015-08-01 22:32:07 +08:00
|
|
|
return null;
|
|
|
|
|
2015-08-03 02:59:57 +08:00
|
|
|
var header_string = Encoding.ASCII.GetString (header, 0x30, 0x10);
|
2015-08-01 22:32:07 +08:00
|
|
|
var match = PackTypeRe.Match (header_string);
|
|
|
|
if (!match.Success)
|
|
|
|
return null;
|
2015-08-03 02:59:57 +08:00
|
|
|
return new ResourceHeader {
|
|
|
|
Bytes = header,
|
|
|
|
PackType = ushort.Parse (match.Groups[1].Value),
|
|
|
|
AType = match.Groups[2].Value.Length > 0,
|
|
|
|
};
|
2015-08-01 22:32:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal class DwqMetaData : ImageMetaData
|
|
|
|
{
|
|
|
|
public string BaseType;
|
|
|
|
public int PackedSize;
|
|
|
|
public int PackType;
|
2016-05-20 08:10:12 +08:00
|
|
|
public bool HasAlpha;
|
2015-08-01 22:32:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Export(typeof(ImageFormat))]
|
|
|
|
public class DwqFormat : ImageFormat
|
|
|
|
{
|
|
|
|
public override string Tag { get { return "DWQ"; } }
|
|
|
|
public override string Description { get { return "Black Cyc image format"; } }
|
|
|
|
public override uint Signature { get { return 0; } }
|
|
|
|
|
|
|
|
public DwqFormat ()
|
|
|
|
{
|
2015-08-06 01:22:02 +08:00
|
|
|
Signatures = new uint[] {
|
|
|
|
0x4745504A, // JPEG
|
|
|
|
0x20504D42, // BMP
|
|
|
|
0x20474E50, // PNG
|
|
|
|
0x4B434150, // PACKBMP
|
|
|
|
0x2B504D42, // BMP+MASK
|
2015-11-18 05:34:09 +08:00
|
|
|
0x50204649, // IF PACKTYPE==0 CUT THIS 64 BYTETHEN REMAKE BMP
|
2015-08-06 01:22:02 +08:00
|
|
|
};
|
2015-08-01 22:32:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override ImageMetaData ReadMetaData (Stream stream)
|
|
|
|
{
|
|
|
|
var header = ResourceHeader.Read (stream);
|
|
|
|
if (null == header)
|
|
|
|
return null;
|
2015-11-18 05:34:09 +08:00
|
|
|
if (Binary.AsciiEqual (header.Bytes, "IF PACKTYPE=="))
|
|
|
|
{
|
|
|
|
if (!Binary.AsciiEqual (header.Bytes, 0x0D, "0 ") ||
|
|
|
|
!Binary.AsciiEqual (header.Bytes, 0x2C, "BMP ") ||
|
|
|
|
header.PackType != 0)
|
|
|
|
return null;
|
|
|
|
using (var bmp = new StreamRegion (stream, 0x40, true))
|
|
|
|
{
|
|
|
|
var info = Bmp.ReadMetaData (bmp);
|
|
|
|
if (null == info)
|
|
|
|
return null;
|
|
|
|
return new DwqMetaData
|
|
|
|
{
|
|
|
|
Width = info.Width,
|
|
|
|
Height = info.Height,
|
|
|
|
BPP = info.BPP,
|
|
|
|
BaseType = "BMP",
|
|
|
|
PackedSize = (int)(stream.Length-0x40),
|
|
|
|
PackType = header.PackType,
|
2016-05-20 08:10:12 +08:00
|
|
|
HasAlpha = header.AType,
|
2015-11-18 05:34:09 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2015-08-06 01:22:02 +08:00
|
|
|
int packed_size;
|
|
|
|
switch (header.PackType)
|
|
|
|
{
|
|
|
|
case 0: // BMP
|
|
|
|
case 5: // JPEG
|
|
|
|
case 8: // PNG
|
|
|
|
packed_size = (int)(stream.Length-0x40);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: // BMP+MASK
|
|
|
|
case 3: // PACKBMP+MASK
|
|
|
|
case 7: // JPEG+MASK
|
|
|
|
packed_size = LittleEndian.ToInt32 (header.Bytes, 0x20);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: // unknown format
|
|
|
|
return null;
|
|
|
|
}
|
2015-08-01 22:32:07 +08:00
|
|
|
return new DwqMetaData
|
|
|
|
{
|
|
|
|
Width = LittleEndian.ToUInt32 (header.Bytes, 0x24),
|
|
|
|
Height = LittleEndian.ToUInt32 (header.Bytes, 0x28),
|
|
|
|
BPP = 32,
|
|
|
|
BaseType = Encoding.ASCII.GetString (header.Bytes, 0, 0x10).TrimEnd(),
|
2015-08-07 15:45:03 +08:00
|
|
|
PackedSize = packed_size,
|
2015-08-01 22:32:07 +08:00
|
|
|
PackType = header.PackType,
|
2016-05-20 08:10:12 +08:00
|
|
|
HasAlpha = header.AType || 7 == header.PackType,
|
2015-08-01 22:32:07 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public override ImageData Read (Stream stream, ImageMetaData info)
|
|
|
|
{
|
2015-11-18 01:21:23 +08:00
|
|
|
var meta = (DwqMetaData)info;
|
2015-08-01 22:32:07 +08:00
|
|
|
|
|
|
|
BitmapSource bitmap = null;
|
2015-08-06 01:22:02 +08:00
|
|
|
using (var input = new StreamRegion (stream, 0x40, meta.PackedSize, true))
|
2015-08-01 22:32:07 +08:00
|
|
|
{
|
2015-08-06 01:22:02 +08:00
|
|
|
switch (meta.PackType)
|
|
|
|
{
|
|
|
|
case 5: // JPEG
|
2015-08-14 10:07:55 +08:00
|
|
|
return Jpeg.Read (input, info);
|
2015-08-01 22:32:07 +08:00
|
|
|
|
2015-08-06 01:22:02 +08:00
|
|
|
case 8: // PNG
|
2015-08-14 10:07:55 +08:00
|
|
|
return Png.Read (input, info);
|
2015-08-01 22:32:07 +08:00
|
|
|
|
2015-08-06 01:22:02 +08:00
|
|
|
case 0: // BMP
|
|
|
|
case 2: // BMP+MASK
|
|
|
|
bitmap = ReadFuckedUpBmpImage (input, info);
|
|
|
|
break;
|
2015-08-01 22:32:07 +08:00
|
|
|
|
2015-08-06 01:22:02 +08:00
|
|
|
case 7: // JPEG+MASK
|
|
|
|
{
|
|
|
|
var decoder = new JpegBitmapDecoder (input, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
|
|
|
|
bitmap = decoder.Frames[0];
|
|
|
|
break;
|
|
|
|
}
|
2015-08-01 22:32:07 +08:00
|
|
|
|
2015-08-06 01:22:02 +08:00
|
|
|
case 3: // PACKBMP+MASK
|
|
|
|
{
|
|
|
|
var reader = new DwqBmpReader (input, meta);
|
|
|
|
reader.Unpack();
|
|
|
|
bitmap = BitmapSource.Create ((int)info.Width, (int)info.Height,
|
|
|
|
ImageData.DefaultDpiX, ImageData.DefaultDpiY,
|
|
|
|
reader.Format, reader.Palette, reader.Data, reader.Stride);
|
|
|
|
break;
|
|
|
|
}
|
2015-08-01 22:32:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (null == bitmap)
|
|
|
|
throw new NotImplementedException();
|
2016-05-20 08:10:12 +08:00
|
|
|
if (meta.HasAlpha)
|
2015-08-01 22:32:07 +08:00
|
|
|
{
|
|
|
|
int mask_offset = 0x40+meta.PackedSize;
|
2015-11-18 05:34:09 +08:00
|
|
|
if (mask_offset != stream.Length)
|
2015-08-01 22:32:07 +08:00
|
|
|
{
|
2015-11-18 05:34:09 +08:00
|
|
|
using (var mask = new StreamRegion (stream, mask_offset, true))
|
2015-08-01 22:32:07 +08:00
|
|
|
{
|
2015-11-18 05:34:09 +08:00
|
|
|
var reader = new DwqBmpReader (mask, meta);
|
|
|
|
if (8 == reader.Format.BitsPerPixel) // mask should be represented as 8bpp bitmap
|
2015-08-01 22:32:07 +08:00
|
|
|
{
|
2015-11-18 05:34:09 +08:00
|
|
|
reader.Unpack();
|
|
|
|
var alpha = reader.Data;
|
|
|
|
var palette = reader.Palette.Colors;
|
|
|
|
for (int i = 0; i < alpha.Length; ++i)
|
|
|
|
{
|
|
|
|
var color = palette[alpha[i]];
|
|
|
|
int A = (color.R + color.G + color.B) / 3;
|
|
|
|
alpha[i] = (byte)A;
|
|
|
|
}
|
|
|
|
bitmap = ApplyAlphaChannel (bitmap, alpha);
|
2015-08-01 22:32:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bitmap.Freeze();
|
|
|
|
return new ImageData (bitmap, meta);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Write (Stream file, ImageData image)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException ("DwqFormat.Write not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
private BitmapSource ApplyAlphaChannel (BitmapSource bitmap, byte[] alpha)
|
|
|
|
{
|
|
|
|
if (bitmap.Format.BitsPerPixel != 32)
|
|
|
|
bitmap = new FormatConvertedBitmap (bitmap, PixelFormats.Bgr32, null, 0);
|
|
|
|
|
|
|
|
int stride = bitmap.PixelWidth * 4;
|
|
|
|
byte[] pixels = new byte[stride * bitmap.PixelHeight];
|
|
|
|
int asrc = 0;
|
|
|
|
bitmap.CopyPixels (pixels, stride, 0);
|
|
|
|
for (int dst = 3; dst < pixels.Length; dst += 4)
|
|
|
|
{
|
|
|
|
pixels[dst] = alpha[asrc++];
|
|
|
|
}
|
|
|
|
return BitmapSource.Create (bitmap.PixelWidth, bitmap.PixelHeight,
|
|
|
|
ImageData.DefaultDpiX, ImageData.DefaultDpiY,
|
|
|
|
PixelFormats.Bgra32, null, pixels, stride);
|
|
|
|
}
|
2015-08-06 01:22:02 +08:00
|
|
|
|
|
|
|
private BitmapSource ReadFuckedUpBmpImage (Stream file, ImageMetaData info)
|
|
|
|
{
|
|
|
|
var header = new byte[0x36];
|
|
|
|
if (header.Length != file.Read (header, 0, header.Length))
|
|
|
|
throw new InvalidFormatException();
|
2015-08-07 15:45:03 +08:00
|
|
|
int w = LittleEndian.ToInt32 (header, 0x12);
|
|
|
|
int h = LittleEndian.ToInt32 (header, 0x16);
|
|
|
|
if (w != info.Width || h != info.Height)
|
|
|
|
throw new InvalidFormatException();
|
|
|
|
|
2015-08-06 01:22:02 +08:00
|
|
|
int bpp = LittleEndian.ToUInt16 (header, 0x1c);
|
2015-08-07 15:45:03 +08:00
|
|
|
PixelFormat format;
|
|
|
|
switch (bpp)
|
2015-08-06 01:22:02 +08:00
|
|
|
{
|
2015-08-07 15:45:03 +08:00
|
|
|
case 32: format = PixelFormats.Bgr32; break;
|
|
|
|
case 24: format = PixelFormats.Bgr24; break;
|
|
|
|
case 16: format = PixelFormats.Bgr565; break;
|
|
|
|
case 8: format = PixelFormats.Indexed8; break;
|
|
|
|
default: throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
BitmapPalette palette = null;
|
|
|
|
if (8 == bpp)
|
|
|
|
{
|
|
|
|
int colors = Math.Min (LittleEndian.ToInt32 (header, 0x2E), 0x100);
|
|
|
|
palette = DwqBmpReader.ReadPalette (file, colors);
|
2015-08-06 01:22:02 +08:00
|
|
|
}
|
|
|
|
int pixel_size = bpp / 8;
|
|
|
|
int stride = ((int)info.Width * pixel_size + 3) & ~3;
|
|
|
|
var pixels = new byte[stride * info.Height];
|
|
|
|
if (pixels.Length != file.Read (pixels, 0, pixels.Length))
|
|
|
|
throw new EndOfStreamException();
|
2015-08-07 15:45:03 +08:00
|
|
|
if (bpp >= 24)
|
2015-08-06 01:22:02 +08:00
|
|
|
{
|
2015-08-07 15:45:03 +08:00
|
|
|
for (int row = 0; row < pixels.Length; row += stride)
|
2015-08-06 01:22:02 +08:00
|
|
|
{
|
2015-08-07 15:45:03 +08:00
|
|
|
for (int i = 2; i < stride; i += pixel_size)
|
|
|
|
{
|
|
|
|
var t = pixels[row+i];
|
|
|
|
pixels[row+i] = pixels[row+i-2];
|
|
|
|
pixels[row+i-2] = t;
|
|
|
|
}
|
2015-08-06 01:22:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return BitmapSource.Create ((int)info.Width, (int)info.Height,
|
|
|
|
ImageData.DefaultDpiX, ImageData.DefaultDpiY,
|
2015-08-07 15:45:03 +08:00
|
|
|
format, palette, pixels, stride);
|
2015-08-06 01:22:02 +08:00
|
|
|
}
|
2015-08-01 22:32:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
internal class DwqBmpReader
|
|
|
|
{
|
|
|
|
Stream m_input;
|
|
|
|
byte[] m_pixels;
|
|
|
|
int m_width;
|
|
|
|
int m_height;
|
|
|
|
|
|
|
|
public byte[] Data { get { return m_pixels; } }
|
|
|
|
public int Stride { get; private set; }
|
|
|
|
public PixelFormat Format { get; private set; }
|
|
|
|
public BitmapPalette Palette { get; private set; }
|
|
|
|
|
|
|
|
public DwqBmpReader (Stream input, DwqMetaData info)
|
|
|
|
{
|
|
|
|
m_input = input;
|
|
|
|
m_width = (int)info.Width;
|
|
|
|
m_height = (int)info.Height;
|
|
|
|
var header = new byte[0x36];
|
|
|
|
if (header.Length != m_input.Read (header, 0, header.Length))
|
|
|
|
throw new InvalidFormatException();
|
|
|
|
int w = LittleEndian.ToInt32 (header, 0x12);
|
|
|
|
int h = LittleEndian.ToInt32 (header, 0x16);
|
2015-11-18 05:34:09 +08:00
|
|
|
if (w != m_width || Math.Abs (h) != m_height)
|
2015-08-01 22:32:07 +08:00
|
|
|
throw new InvalidFormatException();
|
|
|
|
|
|
|
|
int bpp = LittleEndian.ToUInt16 (header, 0x1C);
|
|
|
|
switch (bpp)
|
|
|
|
{
|
|
|
|
case 8: Format = PixelFormats.Indexed8; Stride = m_width; break;
|
|
|
|
case 16: Format = PixelFormats.Bgr565; Stride = m_width*2; break;
|
|
|
|
case 24: Format = PixelFormats.Bgr24; Stride = m_width*3; break;
|
|
|
|
case 32: Format = PixelFormats.Bgr32; Stride = m_width*4; break;
|
|
|
|
default: throw new InvalidFormatException();
|
|
|
|
}
|
|
|
|
if (8 == bpp)
|
|
|
|
{
|
|
|
|
int colors = Math.Min (LittleEndian.ToInt32 (header, 0x2E), 0x100);
|
2015-11-18 01:21:23 +08:00
|
|
|
if (0 == colors)
|
|
|
|
colors = 0x100;
|
2015-08-07 15:45:03 +08:00
|
|
|
Palette = ReadPalette (m_input, colors);
|
2015-08-01 22:32:07 +08:00
|
|
|
}
|
|
|
|
uint data_position = LittleEndian.ToUInt32 (header, 0xA);
|
|
|
|
m_input.Position = data_position;
|
|
|
|
m_pixels = new byte[Stride*m_height];
|
|
|
|
}
|
|
|
|
|
2015-08-07 15:45:03 +08:00
|
|
|
public static BitmapPalette ReadPalette (Stream input, int colors)
|
2015-08-01 22:32:07 +08:00
|
|
|
{
|
|
|
|
int palette_size = colors * 4;
|
|
|
|
var palette_data = new byte[palette_size];
|
2015-08-07 15:45:03 +08:00
|
|
|
if (palette_size != input.Read (palette_data, 0, palette_size))
|
2015-08-01 22:32:07 +08:00
|
|
|
throw new InvalidFormatException();
|
|
|
|
var palette = new Color[colors];
|
|
|
|
for (int i = 0; i < palette.Length; ++i)
|
|
|
|
{
|
2015-08-07 15:45:03 +08:00
|
|
|
byte r = palette_data[i*4+2];
|
2015-08-01 22:32:07 +08:00
|
|
|
byte g = palette_data[i*4+1];
|
2015-08-07 15:45:03 +08:00
|
|
|
byte b = palette_data[i*4];
|
2015-08-01 22:32:07 +08:00
|
|
|
palette[i] = Color.FromRgb (r, g, b);
|
|
|
|
}
|
2015-08-07 15:45:03 +08:00
|
|
|
return new BitmapPalette (palette);
|
2015-08-01 22:32:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Unpack () // sub_408990
|
|
|
|
{
|
|
|
|
var prev_line = new byte[Stride];
|
|
|
|
int dst = 0;
|
|
|
|
for (int y = 0; y < m_height; ++y)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < Stride; )
|
|
|
|
{
|
|
|
|
int b = m_input.ReadByte();
|
|
|
|
if (0 != b)
|
|
|
|
{
|
|
|
|
if (-1 == b)
|
|
|
|
throw new EndOfStreamException();
|
|
|
|
m_pixels[dst + x++] = (byte)b;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int count = m_input.ReadByte();
|
|
|
|
if (-1 == count)
|
|
|
|
throw new EndOfStreamException();
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
m_pixels[dst + x++] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < Stride; ++i)
|
|
|
|
{
|
|
|
|
m_pixels[dst] ^= prev_line[i];
|
|
|
|
prev_line[i] = m_pixels[dst++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|