GARbro-mirror/ArcFormats/RealLive/ImagePDT.cs

244 lines
7.6 KiB
C#
Raw Normal View History

2016-04-19 20:19:24 +08:00
//! \file ImagePDT.cs
//! \date Tue Apr 19 15:32:31 2016
//! \brief AVG32 engine 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 System.Windows.Media.Imaging;
using GameRes.Utility;
namespace GameRes.Formats.RealLive
{
internal class PdtMetaData : ImageMetaData
{
2017-12-29 05:14:46 +08:00
public int Version;
2016-04-19 20:19:24 +08:00
public uint AlphaOffset;
}
[Export(typeof(ImageFormat))]
public class PdtFormat : ImageFormat
{
public override string Tag { get { return "PDT"; } }
public override string Description { get { return "AVG32 engine image format"; } }
public override uint Signature { get { return 0x31544450; } } // 'PDT1'
2016-10-16 13:22:53 +08:00
public override ImageMetaData ReadMetaData (IBinaryStream stream)
2016-04-19 20:19:24 +08:00
{
2016-10-16 13:22:53 +08:00
var header = stream.ReadHeader (32);
2017-12-29 05:14:46 +08:00
int version = header[4] - '0';
if (version < 0 || version > 1)
2016-04-19 20:19:24 +08:00
return null;
return new PdtMetaData
{
2016-10-16 13:22:53 +08:00
Width = header.ToUInt32 (0x0C),
Height = header.ToUInt32 (0x10),
2016-04-19 20:19:24 +08:00
BPP = 32,
2017-12-29 05:14:46 +08:00
Version = version,
2016-10-16 13:22:53 +08:00
AlphaOffset = header.ToUInt32 (0x1C),
2016-04-19 20:19:24 +08:00
};
}
2016-10-16 13:22:53 +08:00
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
2016-04-19 20:19:24 +08:00
{
using (var reader = new PdtReader (stream, (PdtMetaData)info))
{
reader.Unpack();
return ImageData.Create (info, reader.Format, reader.Palette, reader.Data);
}
}
public override void Write (Stream file, ImageData image)
{
throw new NotImplementedException ("PdtFormat.Write not implemented");
}
}
internal sealed class PdtReader : IDisposable
{
2016-10-16 13:22:53 +08:00
IBinaryStream m_input;
2016-04-19 20:19:24 +08:00
byte[] m_output;
PdtMetaData m_info;
public byte[] Data { get { return m_output; } }
public PixelFormat Format { get; private set; }
public BitmapPalette Palette { get; private set; }
2016-10-16 13:22:53 +08:00
public PdtReader (IBinaryStream input, PdtMetaData info)
2016-04-19 20:19:24 +08:00
{
2016-10-16 13:22:53 +08:00
m_input = input;
2016-04-19 20:19:24 +08:00
m_info = info;
2017-12-29 05:14:46 +08:00
if (0 != m_info.AlphaOffset)
2016-04-19 20:19:24 +08:00
Format = PixelFormats.Bgra32;
2017-12-29 05:14:46 +08:00
else if (1 == m_info.Version)
Format = PixelFormats.Indexed8;
else
Format = PixelFormats.Bgr32;
m_output = new byte[m_info.Width * m_info.Height * Format.BitsPerPixel / 8];
2016-04-19 20:19:24 +08:00
}
public void Unpack ()
{
2016-10-16 13:22:53 +08:00
m_input.Position = 0x20;
2017-12-29 05:14:46 +08:00
if (0 == m_info.Version)
UnpackV0();
else
UnpackV1();
}
void UnpackV0 ()
{
2016-04-19 20:19:24 +08:00
Unpack24();
if (0 != m_info.AlphaOffset)
{
2016-10-16 13:22:53 +08:00
m_input.Position = m_info.AlphaOffset;
2016-04-19 20:19:24 +08:00
var alpha = Unpack8();
int src = 0;
for (int i = 3; i < m_output.Length; i += 4)
{
m_output[i] = alpha[src++];
}
}
}
2017-12-29 05:14:46 +08:00
void UnpackV1 ()
{
Palette = ImageFormat.ReadPalette (m_input.AsStream);
var offsets = new int[16];
for (int i = 0; i < offsets.Length; ++i)
offsets[i] = m_input.ReadInt32();
LzUnpack (offsets);
if (0 != m_info.AlphaOffset)
{
m_input.Position = m_info.AlphaOffset;
var alpha = Unpack8();
}
}
2016-04-19 20:19:24 +08:00
void Unpack24 ()
{
int dst = 0;
int bits = 0;
int mask = 0;
while (dst < m_output.Length)
{
mask >>= 1;
if (0 == mask)
{
bits = m_input.ReadByte();
mask = 0x80;
}
if (0 != (bits & mask))
{
m_input.Read (m_output, dst, 3);
dst += 4;
}
else
{
int offset = m_input.ReadUInt16();
int count = (1 + (offset & 0xF)) * 4;
offset = (1 + (offset >> 4)) * 4;
Binary.CopyOverlapped (m_output, dst-offset, dst, count);
dst += count;
}
}
}
byte[] Unpack8 ()
{
var output = new byte[m_info.Width * m_info.Height];
int dst = 0;
int bits = 0;
int mask = 0;
while (dst < output.Length)
{
mask >>= 1;
if (0 == mask)
{
2016-10-16 13:22:53 +08:00
bits = m_input.ReadUInt8();
2016-04-19 20:19:24 +08:00
mask = 0x80;
}
if (0 != (bits & mask))
{
2016-10-16 13:22:53 +08:00
output[dst++] = m_input.ReadUInt8();
2016-04-19 20:19:24 +08:00
}
else
{
2016-10-16 13:22:53 +08:00
int count = 2 + m_input.ReadUInt8();
int offset = 1 + m_input.ReadUInt8();
2016-04-19 20:19:24 +08:00
Binary.CopyOverlapped (output, dst-offset, dst, count);
dst += count;
}
}
return output;
}
2017-12-29 05:14:46 +08:00
void LzUnpack (int[] offsets)
{
int dst = 0;
int bits = 0;
int mask = 0;
while (dst < m_output.Length)
{
mask >>= 1;
if (0 == mask)
{
bits = m_input.ReadUInt8();
mask = 0x80;
}
if (0 != (bits & mask))
{
m_output[dst++] = m_input.ReadUInt8();
}
else
{
int offset = m_input.ReadUInt8();
int count = Math.Min (2 + (offset >> 4), m_output.Length - dst);
offset = offsets[offset & 0xF];
if (dst < offset)
{
int gap = Math.Min (offset - dst, count);
dst += gap;
count -= gap;
}
if (count > 0)
{
Binary.CopyOverlapped (m_output, dst-offset, dst, count);
dst += count;
}
}
}
}
2016-04-19 20:19:24 +08:00
#region IDisposable Members
public void Dispose ()
{
}
#endregion
}
}