GARbro-mirror/ArcFormats/RealLive/ImagePDT.cs

178 lines
5.7 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
{
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);
if (!header.AsciiEqual ("PDT10\0"))
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,
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;
m_output = new byte[m_info.Width * m_info.Height * 4];
if (0 == m_info.AlphaOffset)
Format = PixelFormats.Bgr32;
else
Format = PixelFormats.Bgra32;
}
public void Unpack ()
{
2016-10-16 13:22:53 +08:00
m_input.Position = 0x20;
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++];
}
}
}
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;
}
#region IDisposable Members
public void Dispose ()
{
}
#endregion
}
}