2014-07-21 23:26:28 +04:00
|
|
|
//! \file Image.cs
|
|
|
|
//! \date Tue Jul 01 11:29:52 2014
|
|
|
|
//! \brief image class.
|
|
|
|
//
|
2014-07-28 00:50:18 +04:00
|
|
|
// Copyright (C) 2014 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.
|
|
|
|
//
|
2014-07-21 23:26:28 +04:00
|
|
|
|
|
|
|
using System.IO;
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
|
|
|
namespace GameRes
|
|
|
|
{
|
|
|
|
public class ImageMetaData
|
|
|
|
{
|
|
|
|
public uint Width { get; set; }
|
|
|
|
public uint Height { get; set; }
|
|
|
|
public int OffsetX { get; set; }
|
|
|
|
public int OffsetY { get; set; }
|
|
|
|
public int BPP { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ImageEntry : Entry
|
|
|
|
{
|
|
|
|
public override string Type { get { return "image"; } }
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ImageData
|
|
|
|
{
|
|
|
|
private BitmapSource m_bitmap;
|
|
|
|
|
|
|
|
public BitmapSource Bitmap { get { return m_bitmap; } }
|
|
|
|
public uint Width { get { return (uint)m_bitmap.PixelWidth; } }
|
|
|
|
public uint Height { get { return (uint)m_bitmap.PixelHeight; } }
|
|
|
|
public int OffsetX { get; set; }
|
|
|
|
public int OffsetY { get; set; }
|
|
|
|
public int BPP { get { return m_bitmap.Format.BitsPerPixel; } }
|
|
|
|
|
|
|
|
public ImageData (BitmapSource data, ImageMetaData meta)
|
|
|
|
{
|
|
|
|
m_bitmap = data;
|
|
|
|
OffsetX = meta.OffsetX;
|
|
|
|
OffsetY = meta.OffsetY;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ImageData (BitmapSource data, int x = 0, int y = 0)
|
|
|
|
{
|
|
|
|
m_bitmap = data;
|
|
|
|
OffsetX = x;
|
|
|
|
OffsetY = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract class ImageFormat : IResource
|
|
|
|
{
|
|
|
|
public override string Type { get { return "image"; } }
|
|
|
|
|
2014-07-25 08:56:32 +04:00
|
|
|
public abstract ImageMetaData ReadMetaData (Stream file);
|
|
|
|
|
|
|
|
public abstract ImageData Read (Stream file, ImageMetaData info);
|
|
|
|
public abstract void Write (Stream file, ImageData bitmap);
|
|
|
|
|
|
|
|
public static ImageData Read (Stream file)
|
2014-07-21 23:26:28 +04:00
|
|
|
{
|
|
|
|
bool need_dispose = false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!file.CanSeek)
|
|
|
|
{
|
|
|
|
var stream = new MemoryStream();
|
|
|
|
file.CopyTo (stream);
|
|
|
|
file = stream;
|
|
|
|
need_dispose = true;
|
|
|
|
}
|
2014-07-25 08:56:32 +04:00
|
|
|
var format = FindFormat (file);
|
|
|
|
if (null == format)
|
|
|
|
return null;
|
|
|
|
file.Position = 0;
|
|
|
|
return format.Item1.Read (file, format.Item2);
|
2014-07-21 23:26:28 +04:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (need_dispose)
|
|
|
|
file.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 08:56:32 +04:00
|
|
|
public static System.Tuple<ImageFormat, ImageMetaData> FindFormat (Stream file)
|
|
|
|
{
|
|
|
|
uint signature = FormatCatalog.ReadSignature (file);
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
var range = FormatCatalog.Instance.LookupSignature<ImageFormat> (signature);
|
|
|
|
foreach (var impl in range)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
file.Position = 0;
|
|
|
|
ImageMetaData metadata = impl.ReadMetaData (file);
|
|
|
|
if (null != metadata)
|
|
|
|
return new System.Tuple<ImageFormat, ImageMetaData> (impl, metadata);
|
|
|
|
}
|
|
|
|
catch { }
|
|
|
|
}
|
|
|
|
if (0 == signature)
|
|
|
|
break;
|
|
|
|
signature = 0;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2014-07-21 23:26:28 +04:00
|
|
|
|
|
|
|
public override Entry CreateEntry ()
|
|
|
|
{
|
|
|
|
return new ImageEntry();
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsBuiltin
|
|
|
|
{
|
|
|
|
get { return this.GetType().Assembly == typeof(ImageFormat).Assembly; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|