2015-04-17 02:41:34 +08:00
|
|
|
//! \file ImageGS.cs
|
|
|
|
//! \date Thu Apr 16 16:03:19 2015
|
|
|
|
//! \brief GsPack image format implementation.
|
|
|
|
//
|
2016-02-10 11:15:12 +08:00
|
|
|
// Copyright (C) 2015-2016 by morkt
|
2015-04-17 02:41:34 +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.Collections.Generic;
|
|
|
|
using System.ComponentModel.Composition;
|
|
|
|
using System.IO;
|
|
|
|
using System.Windows;
|
|
|
|
using System.Windows.Media;
|
|
|
|
using System.Windows.Media.Imaging;
|
2015-07-25 08:20:20 +08:00
|
|
|
using GameRes.Compression;
|
2015-04-17 02:41:34 +08:00
|
|
|
using GameRes.Utility;
|
|
|
|
|
|
|
|
namespace GameRes.Formats.Gs
|
|
|
|
{
|
|
|
|
internal class PicMetaData : ImageMetaData
|
|
|
|
{
|
|
|
|
public uint PackedSize;
|
|
|
|
public uint UnpackedSize;
|
|
|
|
public uint HeaderSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Export(typeof(ImageFormat))]
|
|
|
|
public class PicFormat : ImageFormat
|
|
|
|
{
|
|
|
|
public override string Tag { get { return "GsPIC"; } }
|
|
|
|
public override string Description { get { return "GsPack image format"; } }
|
|
|
|
public override uint Signature { get { return 0x00040000; } }
|
|
|
|
|
|
|
|
public PicFormat ()
|
|
|
|
{
|
|
|
|
Extensions = new string[] { "pic" }; // made-up
|
|
|
|
}
|
|
|
|
|
2016-10-16 13:22:53 +08:00
|
|
|
public override ImageMetaData ReadMetaData (IBinaryStream file)
|
2015-04-17 02:41:34 +08:00
|
|
|
{
|
2016-10-16 13:22:53 +08:00
|
|
|
file.Position = 4;
|
|
|
|
var info = new PicMetaData();
|
|
|
|
info.PackedSize = file.ReadUInt32();
|
|
|
|
info.UnpackedSize = file.ReadUInt32();
|
|
|
|
info.HeaderSize = file.ReadUInt32();
|
|
|
|
if (info.HeaderSize >= file.Length || info.PackedSize + info.HeaderSize > file.Length)
|
|
|
|
return null;
|
|
|
|
file.ReadUInt32();
|
|
|
|
info.Width = file.ReadUInt32();
|
|
|
|
info.Height = file.ReadUInt32();
|
|
|
|
info.BPP = file.ReadInt32();
|
|
|
|
if (info.HeaderSize >= 0x2C)
|
2015-04-17 02:41:34 +08:00
|
|
|
{
|
2016-10-16 13:22:53 +08:00
|
|
|
file.ReadInt32();
|
|
|
|
info.OffsetX = file.ReadInt32();
|
|
|
|
info.OffsetY = file.ReadInt32();
|
2015-04-17 02:41:34 +08:00
|
|
|
}
|
2016-10-16 13:22:53 +08:00
|
|
|
return info;
|
2015-04-17 02:41:34 +08:00
|
|
|
}
|
|
|
|
|
2016-10-16 13:22:53 +08:00
|
|
|
public override ImageData Read (IBinaryStream file, ImageMetaData info)
|
2015-04-17 02:41:34 +08:00
|
|
|
{
|
2016-02-10 11:15:12 +08:00
|
|
|
var meta = (PicMetaData)info;
|
2016-10-16 13:22:53 +08:00
|
|
|
file.Position = meta.HeaderSize;
|
|
|
|
using (var input = new LzssStream (file.AsStream, LzssMode.Decompress, true))
|
2015-04-17 02:41:34 +08:00
|
|
|
{
|
|
|
|
BitmapPalette palette = null;
|
|
|
|
PixelFormat format;
|
|
|
|
if (8 == meta.BPP) // read palette
|
|
|
|
{
|
|
|
|
format = PixelFormats.Indexed8;
|
2015-07-07 15:30:50 +08:00
|
|
|
palette = ReadPalette (input);
|
2015-04-17 02:41:34 +08:00
|
|
|
}
|
|
|
|
else if (24 == meta.BPP)
|
|
|
|
format = PixelFormats.Bgr24;
|
|
|
|
else if (16 == meta.BPP)
|
|
|
|
format = PixelFormats.Bgr565;
|
|
|
|
else
|
2015-07-07 15:30:50 +08:00
|
|
|
format = PixelFormats.Bgr32;
|
|
|
|
|
|
|
|
int stride = (int)meta.Width*((info.BPP+7)/8);
|
|
|
|
var pixels = new byte[stride*meta.Height];
|
|
|
|
input.Read (pixels, 0, pixels.Length);
|
|
|
|
if (32 == meta.BPP)
|
2015-04-17 02:41:34 +08:00
|
|
|
{
|
|
|
|
for (int i = 3; i < pixels.Length; i += 4)
|
|
|
|
{
|
|
|
|
if (0 != pixels[i])
|
|
|
|
{
|
2015-07-07 15:30:50 +08:00
|
|
|
format = PixelFormats.Bgra32;
|
2015-04-17 02:41:34 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-07 15:30:50 +08:00
|
|
|
return ImageData.Create (meta, format, palette, pixels);
|
2015-04-17 02:41:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Write (Stream file, ImageData image)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException ("PicFormat.Write not implemented");
|
|
|
|
}
|
2015-07-07 15:30:50 +08:00
|
|
|
|
|
|
|
private static BitmapPalette ReadPalette (Stream input)
|
|
|
|
{
|
|
|
|
var colors = new byte[0x400];
|
|
|
|
if (colors.Length != input.Read (colors, 0, colors.Length))
|
|
|
|
throw new InvalidFormatException();
|
|
|
|
var color_data = new Color[0x100];
|
|
|
|
int n = 0;
|
|
|
|
for (int i = 0; i < 0x400; i += 4)
|
|
|
|
color_data[n++] = Color.FromRgb (colors[i+2], colors[i+1], colors[i]);
|
|
|
|
return new BitmapPalette (color_data);
|
|
|
|
}
|
2015-04-17 02:41:34 +08:00
|
|
|
}
|
|
|
|
}
|