1
0
mirror of https://github.com/crskycode/GARbro.git synced 2024-12-23 19:34:15 +08:00

105 lines
3.8 KiB
C#
Raw Normal View History

2015-04-30 21:07:11 +04:00
//! \file ImageMFG.cs
//! \date Wed Apr 29 13:47:56 2015
//! \brief Silky's MFG image format implementation.
//
// Copyright (C) 2015 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 System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using GameRes.Utility;
namespace GameRes.Formats.Silky
{
internal class MfgMetaData : ImageMetaData
{
public int Type;
public int Stride;
}
[Export (typeof (ImageFormat))]
public class MfgFormat : ImageFormat
{
public override string Tag { get { return "MFG"; } }
public override string Description { get { return "Silky's RGB image format"; } }
public override uint Signature { get { return 0x5f47464du; } } // 'MFG_'
public MfgFormat ()
{
Signatures = new uint[] { 0x5f47464d, 0x4147464d, 0x4347464d }; // 'MFG_', 'MFGA', 'MFGC'
Extensions = new string[] { "mfp" }; // made-up
}
public override void Write (Stream file, ImageData image)
{
throw new NotImplementedException ("MfgFormat.Write not implemented");
}
2016-10-16 09:22:53 +04:00
public override ImageMetaData ReadMetaData (IBinaryStream file)
2015-04-30 21:07:11 +04:00
{
2016-10-16 09:22:53 +04:00
file.Position = 3;
byte id = file.ReadUInt8();
uint data_size = file.ReadUInt32();
uint width = file.ReadUInt32();
uint height = file.ReadUInt32();
uint stride = file.ReadUInt32();
if (stride < width)
throw new NotSupportedException();
if (stride*height != data_size)
return null;
return new MfgMetaData
2015-04-30 21:07:11 +04:00
{
2016-10-16 09:22:53 +04:00
Width = width,
Height = height,
BPP = (int)(stride*8/width),
Type = id,
Stride = (int)stride,
};
2015-04-30 21:07:11 +04:00
}
2016-10-16 09:22:53 +04:00
public override ImageData Read (IBinaryStream file, ImageMetaData info)
2015-04-30 21:07:11 +04:00
{
2016-10-16 09:22:53 +04:00
var meta = (MfgMetaData)info;
file.Position = 0x14;
2015-04-30 21:07:11 +04:00
if ('_' != meta.Type)
2016-10-16 09:22:53 +04:00
for (uint i = 0; i < meta.Height; ++i)
2015-04-30 21:07:11 +04:00
{
2016-10-16 09:22:53 +04:00
uint n = file.ReadUInt32();
file.Seek (n*8, SeekOrigin.Current);
2015-04-30 21:07:11 +04:00
}
byte[] pixels = new byte[meta.Stride*info.Height];
2016-10-16 09:22:53 +04:00
if (pixels.Length != file.Read (pixels, 0, pixels.Length))
2015-04-30 21:07:11 +04:00
throw new InvalidFormatException ("Unexpected end of file");
PixelFormat format;
if (24 == meta.BPP)
format = PixelFormats.Bgr24;
else
format = PixelFormats.Bgra32;
return ImageData.Create (info, format, null, pixels, meta.Stride);
2015-04-30 21:07:11 +04:00
}
}
}