GARbro-mirror/ArcFormats/Ivory/ImageMOE.cs

134 lines
4.9 KiB
C#
Raw Normal View History

2016-09-09 17:13:26 +08:00
//! \file ImageMOE.cs
//! \date Thu Sep 08 17:56:51 2016
//! \brief Ivory 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.
//
2017-12-29 05:13:19 +08:00
using System;
2016-09-09 17:13:26 +08:00
using System.ComponentModel.Composition;
using System.IO;
using System.Windows.Media;
2017-12-29 05:13:19 +08:00
using System.Windows.Media.Imaging;
2016-09-09 17:13:26 +08:00
using GameRes.Utility;
namespace GameRes.Formats.Ivory
{
[Export(typeof(ImageFormat))]
public class MoeFormat : ImageFormat
{
public override string Tag { get { return "MOE"; } }
public override string Description { get { return "Ivory image format"; } }
public override uint Signature { get { return 0; } }
2017-12-29 05:13:19 +08:00
public MoeFormat ()
{
Extensions = new string[] { "moe", "shw" };
}
2016-10-15 16:21:12 +08:00
public override ImageMetaData ReadMetaData (IBinaryStream stream)
2016-09-09 17:13:26 +08:00
{
2016-10-15 16:21:12 +08:00
var wh = stream.Signature;
2016-09-09 17:13:26 +08:00
uint width = wh & 0xFFFF;
uint height = wh >> 16;
if (0 == width || width > 800 || 0 == height || height > 600)
return null;
2017-12-29 05:13:19 +08:00
int bpp = stream.Name.HasExtension (".SHW") ? 8 : 24;
stream.Position = 4;
2017-12-29 05:13:19 +08:00
if (!IsValidInput (stream.AsStream, width, height, bpp / 8))
2016-09-09 17:13:26 +08:00
return null;
2017-12-29 05:13:19 +08:00
return new ImageMetaData { Width = width, Height = height, BPP = bpp };
2016-09-09 17:13:26 +08:00
}
2016-10-15 16:21:12 +08:00
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
2016-09-09 17:13:26 +08:00
{
stream.Position = 4;
2017-12-29 05:13:19 +08:00
int pixel_size = info.BPP / 8;
var pixels = new byte[pixel_size * (int)info.Width * (int)info.Height];
2016-09-09 17:13:26 +08:00
int dst = 0;
while (dst < pixels.Length)
{
int count = stream.ReadByte();
if (-1 == count)
throw new EndOfStreamException();
if (0 != (count & 0x80))
{
2017-12-29 05:13:19 +08:00
count = Math.Min (pixel_size * (count & 0x7F), pixels.Length - dst);
2016-09-09 17:13:26 +08:00
stream.Read (pixels, dst, count);
dst += count;
}
else
{
2017-12-29 05:13:19 +08:00
count *= pixel_size;
stream.Read (pixels, dst, pixel_size);
Binary.CopyOverlapped (pixels, dst, dst+pixel_size, count-pixel_size);
2016-09-09 17:13:26 +08:00
dst += count;
}
}
2017-12-29 05:13:19 +08:00
if (24 == info.BPP)
return ImageData.Create (info, PixelFormats.Bgr24, null, pixels);
const int MaxAlpha = 0x10;
var colors = new Color[MaxAlpha+1];
for (int i = 0; i <= MaxAlpha; ++i)
{
byte g = (byte)(i * 0xFF / MaxAlpha);
colors[i] = Color.FromRgb (g, g, g);
}
var palette = new BitmapPalette (colors);
return ImageData.Create (info, PixelFormats.Indexed8, palette, pixels);
2016-09-09 17:13:26 +08:00
}
/// <summary>
/// Try to interpret input stream as a compressed image.
/// </summary>
2017-12-29 05:13:19 +08:00
bool IsValidInput (Stream input, uint width, uint height, int pixel_size)
2016-09-09 17:13:26 +08:00
{
int total = (int)width * (int)height;
int dst = 0;
while (dst < total)
{
int count = input.ReadByte();
if (-1 == count)
return false;
if (0 != (count & 0x80))
{
2017-12-29 05:13:19 +08:00
count = Math.Min (count & 0x7F, total - dst);
input.Seek (count * pixel_size, SeekOrigin.Current);
2016-09-09 17:13:26 +08:00
}
else
{
2017-12-29 05:13:19 +08:00
input.Seek (pixel_size, SeekOrigin.Current);
2016-09-09 17:13:26 +08:00
}
dst += count;
if (dst > total)
return false;
}
return true;
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("MoeFormat.Write not implemented");
}
}
}