2016-08-19 10:57:46 +04:00
|
|
|
//! \file ImagePX.cs
|
|
|
|
//! \date Thu Aug 18 05:35:34 2016
|
|
|
|
//! \brief Leaf 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;
|
|
|
|
using GameRes.Utility;
|
|
|
|
|
|
|
|
namespace GameRes.Formats.Leaf
|
|
|
|
{
|
|
|
|
internal class PxMetaData : ImageMetaData
|
|
|
|
{
|
|
|
|
public int Type;
|
|
|
|
public int FrameCount;
|
|
|
|
public int BlockSize;
|
|
|
|
public int BlocksWidth;
|
|
|
|
public int BlocksHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX this format changes significantly from game to game
|
|
|
|
|
|
|
|
[Export(typeof(ImageFormat))]
|
|
|
|
public class PxFormat : ImageFormat
|
|
|
|
{
|
|
|
|
public override string Tag { get { return "PX"; } }
|
|
|
|
public override string Description { get { return "Leaf image format"; } }
|
|
|
|
public override uint Signature { get { return 0; } }
|
|
|
|
|
2016-10-15 12:21:12 +04:00
|
|
|
public override ImageMetaData ReadMetaData (IBinaryStream stream)
|
2016-08-19 10:57:46 +04:00
|
|
|
{
|
2016-10-15 12:21:12 +04:00
|
|
|
var header = stream.ReadHeader (0x20);
|
|
|
|
int type = header.ToUInt16 (0x10);
|
2016-08-19 10:57:46 +04:00
|
|
|
if (0x0C == type)
|
|
|
|
{
|
2016-10-15 12:21:12 +04:00
|
|
|
int count = header.ToInt32 (0);
|
2016-08-19 10:57:46 +04:00
|
|
|
if (!ArchiveFormat.IsSaneCount (count))
|
|
|
|
return null;
|
2016-10-15 12:21:12 +04:00
|
|
|
int block_size = header.ToInt32 (4);
|
2016-08-19 10:57:46 +04:00
|
|
|
if (block_size <= 0)
|
|
|
|
return null;
|
2016-10-15 12:21:12 +04:00
|
|
|
int bpp = header.ToUInt16 (0x12);
|
|
|
|
uint width = header.ToUInt16 (0x14);
|
|
|
|
uint height = header.ToUInt16 (0x16);
|
2016-08-19 10:57:46 +04:00
|
|
|
if (bpp != 32 || 0 == width || 0 == height)
|
|
|
|
return null;
|
|
|
|
return new PxMetaData
|
|
|
|
{
|
|
|
|
Width = width,
|
|
|
|
Height = height,
|
|
|
|
BPP = bpp,
|
|
|
|
Type = type,
|
|
|
|
FrameCount = count,
|
|
|
|
BlockSize = block_size,
|
2016-10-15 12:21:12 +04:00
|
|
|
BlocksWidth = header.ToUInt16 (0x1C),
|
|
|
|
BlocksHeight = header.ToUInt16 (0x1E),
|
2016-08-19 10:57:46 +04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
else if (0x80 == type || 0x90 == type)
|
|
|
|
{
|
2016-10-15 12:21:12 +04:00
|
|
|
if (!header.AsciiEqual (0x14, "Leaf"))
|
2016-08-19 10:57:46 +04:00
|
|
|
return null;
|
2016-10-15 12:21:12 +04:00
|
|
|
int count = header.ToInt32 (4);
|
2016-08-19 10:57:46 +04:00
|
|
|
if (!ArchiveFormat.IsSaneCount (count))
|
|
|
|
return null;
|
2016-10-15 12:21:12 +04:00
|
|
|
var header_ex = stream.ReadBytes (0x20);
|
|
|
|
if (0x20 != header_ex.Length)
|
2016-08-19 10:57:46 +04:00
|
|
|
return null;
|
2016-10-15 12:21:12 +04:00
|
|
|
if (0x0A != LittleEndian.ToUInt16 (header_ex, 0x10))
|
2016-08-19 10:57:46 +04:00
|
|
|
return null;
|
|
|
|
return new PxMetaData
|
|
|
|
{
|
2016-10-15 12:21:12 +04:00
|
|
|
Width = LittleEndian.ToUInt32 (header_ex, 0),
|
2016-10-18 17:22:50 +04:00
|
|
|
Height = LittleEndian.ToUInt32 (header_ex, 4),
|
2016-10-15 12:21:12 +04:00
|
|
|
BPP = LittleEndian.ToUInt16 (header_ex, 0x12),
|
|
|
|
Type = type,
|
2016-08-19 10:57:46 +04:00
|
|
|
FrameCount = count,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-10-15 12:21:12 +04:00
|
|
|
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
|
2016-08-19 10:57:46 +04:00
|
|
|
{
|
|
|
|
using (var reader = new PxReader (stream, (PxMetaData)info))
|
|
|
|
{
|
|
|
|
var pixels = reader.Unpack();
|
|
|
|
return ImageData.Create (info, reader.Format, null, pixels, reader.Stride);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Write (Stream file, ImageData image)
|
|
|
|
{
|
|
|
|
throw new System.NotImplementedException ("PxFormat.Write not implemented");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal sealed class PxReader : IDisposable
|
|
|
|
{
|
2016-10-15 12:21:12 +04:00
|
|
|
IBinaryStream m_input;
|
2016-08-19 10:57:46 +04:00
|
|
|
PxMetaData m_info;
|
|
|
|
byte[] m_output;
|
|
|
|
int m_pixel_size;
|
|
|
|
int m_stride;
|
|
|
|
|
|
|
|
public PixelFormat Format { get { return PixelFormats.Bgra32; } }
|
|
|
|
public byte[] Data { get { return m_output; } }
|
|
|
|
public int Stride { get { return m_stride; } }
|
|
|
|
public int FrameCount { get { return m_info.FrameCount; } }
|
|
|
|
|
2016-10-15 12:21:12 +04:00
|
|
|
public PxReader (IBinaryStream input, PxMetaData info)
|
2016-08-19 10:57:46 +04:00
|
|
|
{
|
2016-10-15 12:21:12 +04:00
|
|
|
m_input = input;
|
2016-08-19 10:57:46 +04:00
|
|
|
m_info = info;
|
|
|
|
m_pixel_size = m_info.BPP / 8;
|
|
|
|
m_stride = (int)m_info.Width * m_pixel_size;
|
|
|
|
m_output = new byte[m_stride * (int)m_info.Height];
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] Unpack (int frame = 0)
|
|
|
|
{
|
|
|
|
if (frame < 0 || frame >= FrameCount)
|
|
|
|
throw new ArgumentException ("[PX] Invalid frame number", "frame");
|
|
|
|
switch (m_info.Type)
|
|
|
|
{
|
|
|
|
case 0x0C: Unpack0C (frame); break;
|
|
|
|
case 0x90: Unpack90 (frame); break;
|
|
|
|
case 0x80: Unpack80 (frame); break;
|
|
|
|
default: throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
return m_output;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unpack0C (int frame)
|
|
|
|
{
|
|
|
|
int block_count = m_info.BlocksWidth * m_info.BlocksHeight;
|
|
|
|
var block_table = new ushort[block_count];
|
2016-10-15 12:21:12 +04:00
|
|
|
m_input.Position = 0x20 + frame * block_count * 2;
|
2016-08-19 10:57:46 +04:00
|
|
|
for (int i = 0; i < block_count; ++i)
|
|
|
|
block_table[i] = m_input.ReadUInt16();
|
|
|
|
int data_pos = 0x20 + FrameCount * block_count * 2;
|
|
|
|
int block_length = 2 + (m_info.BlockSize + 2) * (m_info.BlockSize + 2) * m_pixel_size;
|
|
|
|
int current_block = 0;
|
|
|
|
int dst_line = 0;
|
|
|
|
for (int by = 0; by < m_info.BlocksHeight; ++by)
|
|
|
|
{
|
|
|
|
for (int bx = 0; bx < m_info.BlocksWidth; ++bx)
|
|
|
|
{
|
|
|
|
int dst = dst_line + bx * m_info.BlockSize * m_pixel_size;
|
|
|
|
int block_num = block_table[current_block++];
|
|
|
|
if (block_num != 0)
|
|
|
|
{
|
2016-10-15 12:21:12 +04:00
|
|
|
m_input.Position = data_pos + (block_num - 1) * block_length;
|
2016-08-19 10:57:46 +04:00
|
|
|
int block_width = m_input.ReadByte() - 2;
|
|
|
|
int block_height = m_input.ReadByte() - 2;
|
|
|
|
int line_length = block_width * m_pixel_size;
|
|
|
|
for (int y = 0; y < block_height; ++y)
|
|
|
|
{
|
|
|
|
m_input.Read (m_output, dst, line_length);
|
|
|
|
dst += m_stride;
|
2016-10-15 12:21:12 +04:00
|
|
|
m_input.Seek (8, SeekOrigin.Current);
|
2016-08-19 10:57:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dst_line += m_info.BlockSize * m_stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unpack90 (int frame)
|
|
|
|
{
|
2016-10-15 12:21:12 +04:00
|
|
|
m_input.Position = 0x40 + frame * (0x20 + m_output.Length);
|
2016-08-19 10:57:46 +04:00
|
|
|
if (m_output.Length != m_input.Read (m_output, 0, m_output.Length))
|
|
|
|
throw new EndOfStreamException();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unpack80 (int frame)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
#region IDisposable Members
|
|
|
|
public void Dispose ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|