2016-02-20 22:23:02 +08:00
|
|
|
//! \file ImageGPS.cs
|
|
|
|
//! \date Sat Feb 20 14:13:10 2016
|
|
|
|
//! \brief ADVEngine compressed bitmap.
|
|
|
|
//
|
|
|
|
// 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.Collections.Generic;
|
|
|
|
using System.ComponentModel.Composition;
|
|
|
|
using System.IO;
|
|
|
|
using GameRes.Compression;
|
|
|
|
using GameRes.Utility;
|
|
|
|
|
|
|
|
namespace GameRes.Formats.Abel
|
|
|
|
{
|
|
|
|
internal class GpsMetaData : ImageMetaData
|
|
|
|
{
|
|
|
|
public byte Compression;
|
|
|
|
public int PackedSize;
|
|
|
|
public int UnpackedSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Export(typeof(ImageFormat))]
|
|
|
|
public class GpsFormat : BmpFormat
|
|
|
|
{
|
|
|
|
public override string Tag { get { return "GPS"; } }
|
|
|
|
public override string Description { get { return "ADVEngine compressed bitmap"; } }
|
|
|
|
public override uint Signature { get { return 0x535047; } } // 'GPS'
|
2016-10-11 04:05:22 +08:00
|
|
|
public override bool CanWrite { get { return false; } }
|
2016-02-20 22:23:02 +08:00
|
|
|
|
|
|
|
public GpsFormat ()
|
|
|
|
{
|
|
|
|
Extensions = new string[] { "gps", "cmp" };
|
|
|
|
}
|
|
|
|
|
2016-10-16 13:22:53 +08:00
|
|
|
public override ImageMetaData ReadMetaData (IBinaryStream file)
|
2016-02-20 22:23:02 +08:00
|
|
|
{
|
2016-10-16 13:22:53 +08:00
|
|
|
var header = file.ReadHeader (0x29);
|
2016-10-15 16:21:12 +08:00
|
|
|
if (header.Length != 0x29)
|
2016-02-20 22:23:02 +08:00
|
|
|
return null;
|
|
|
|
var gps = new GpsMetaData
|
|
|
|
{
|
2016-10-15 16:21:12 +08:00
|
|
|
Width = header.ToUInt32 (0x19),
|
|
|
|
Height = header.ToUInt32 (0x1D),
|
2016-02-20 22:23:02 +08:00
|
|
|
Compression = header[0x10],
|
2016-10-15 16:21:12 +08:00
|
|
|
UnpackedSize = header.ToInt32 (0x11),
|
|
|
|
PackedSize = header.ToInt32 (0x15),
|
2016-02-20 22:23:02 +08:00
|
|
|
};
|
|
|
|
// read BMP header
|
2016-10-16 13:22:53 +08:00
|
|
|
using (var stream = OpenGpsStream (file, gps.Compression, 0x54))
|
|
|
|
using (var input = BinaryStream.FromStream (stream, file.Name))
|
2016-02-20 22:23:02 +08:00
|
|
|
{
|
|
|
|
var bmp_info = base.ReadMetaData (input);
|
|
|
|
if (null == bmp_info)
|
|
|
|
return null;
|
|
|
|
gps.BPP = bmp_info.BPP;
|
|
|
|
return gps;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-16 13:22:53 +08:00
|
|
|
public override ImageData Read (IBinaryStream file, ImageMetaData info)
|
2016-02-20 22:23:02 +08:00
|
|
|
{
|
|
|
|
var gps = (GpsMetaData)info;
|
2016-10-16 13:22:53 +08:00
|
|
|
file.Position = 0x29;
|
|
|
|
using (var stream = OpenGpsStream (file, gps.Compression, gps.UnpackedSize))
|
|
|
|
using (var input = BinaryStream.FromStream (stream, file.Name))
|
2016-02-20 22:23:02 +08:00
|
|
|
return base.Read (input, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Write (Stream file, ImageData image)
|
|
|
|
{
|
|
|
|
throw new System.NotImplementedException ("GpsFormat.Write not implemented");
|
|
|
|
}
|
|
|
|
|
2016-10-16 13:22:53 +08:00
|
|
|
Stream OpenGpsStream (IBinaryStream input, byte compression, int unpacked_size)
|
2016-02-20 22:23:02 +08:00
|
|
|
{
|
|
|
|
if (0 == compression)
|
2016-10-16 13:22:53 +08:00
|
|
|
return new StreamRegion (input.AsStream, 0x29, true);
|
2016-02-20 22:23:02 +08:00
|
|
|
else if (1 == compression)
|
2016-10-16 13:22:53 +08:00
|
|
|
return OpenRLEStream (input.AsStream, unpacked_size);
|
2016-02-20 22:23:02 +08:00
|
|
|
else if (2 == compression)
|
2016-10-16 13:22:53 +08:00
|
|
|
return new LzssStream (input.AsStream, LzssMode.Decompress, true);
|
2016-02-20 22:23:02 +08:00
|
|
|
else if (3 == compression)
|
|
|
|
{
|
2016-10-15 16:21:12 +08:00
|
|
|
using (var lzss = new LzssStream (input.AsStream, LzssMode.Decompress, true))
|
2016-10-16 13:22:53 +08:00
|
|
|
return OpenRLEStream (lzss, unpacked_size);
|
2016-02-20 22:23:02 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw new InvalidFormatException();
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream OpenRLEStream (Stream input, int output_size)
|
|
|
|
{
|
|
|
|
var output = new byte[output_size];
|
|
|
|
UnpackRLE (input, output);
|
2016-10-16 13:22:53 +08:00
|
|
|
return new BinMemoryStream (output, "");
|
2016-02-20 22:23:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnpackRLE (Stream input, byte[] output)
|
|
|
|
{
|
|
|
|
int dst = 0;
|
|
|
|
while (dst < output.Length)
|
|
|
|
{
|
|
|
|
int count = Math.Min (3, output.Length-dst);
|
|
|
|
count = input.Read (output, dst, count);
|
|
|
|
if (count < 3)
|
|
|
|
break;
|
|
|
|
count = input.ReadByte();
|
|
|
|
if (-1 == count)
|
|
|
|
break;
|
|
|
|
dst += 3;
|
|
|
|
if (count > 1)
|
|
|
|
{
|
|
|
|
count = Math.Min ((count-1) * 3, output.Length-dst);
|
|
|
|
Binary.CopyOverlapped (output, dst-3, dst, count);
|
|
|
|
dst += count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|