2016-10-26 09:08:12 +08:00
|
|
|
//! \file ImageDecoder.cs
|
|
|
|
//! \date Wed Oct 26 00:33:00 2016
|
|
|
|
//! \brief Image decoder interface.
|
|
|
|
//
|
|
|
|
// 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.IO;
|
2018-10-09 18:36:18 +08:00
|
|
|
using System.Windows.Media.Imaging;
|
2016-10-26 09:08:12 +08:00
|
|
|
|
|
|
|
namespace GameRes
|
|
|
|
{
|
|
|
|
public interface IImageDecoder : IDisposable
|
|
|
|
{
|
2016-10-26 19:42:27 +08:00
|
|
|
Stream Source { get; }
|
2016-10-26 09:08:12 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Underlying image format or null if image is not represented by any format.
|
|
|
|
/// </summary>
|
2016-10-26 19:42:27 +08:00
|
|
|
ImageFormat SourceFormat { get; }
|
2016-10-26 09:08:12 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Image parameters.
|
|
|
|
/// </summary>
|
|
|
|
ImageMetaData Info { get; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Decoded image data.
|
|
|
|
/// </summary>
|
|
|
|
ImageData Image { get; }
|
|
|
|
}
|
|
|
|
|
2016-10-26 19:42:27 +08:00
|
|
|
public sealed class ImageFormatDecoder : IImageDecoder
|
2016-10-26 09:08:12 +08:00
|
|
|
{
|
|
|
|
IBinaryStream m_file;
|
|
|
|
ImageData m_image;
|
|
|
|
|
2016-10-26 19:42:27 +08:00
|
|
|
public Stream Source { get { m_file.Position = 0; return m_file.AsStream; } }
|
|
|
|
public ImageFormat SourceFormat { get; private set; }
|
|
|
|
public ImageMetaData Info { get; private set; }
|
2016-10-26 09:08:12 +08:00
|
|
|
|
|
|
|
public ImageData Image
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (null == m_image)
|
|
|
|
{
|
|
|
|
m_file.Position = 0;
|
2016-10-26 19:42:27 +08:00
|
|
|
m_image = SourceFormat.Read (m_file, Info);
|
2016-10-26 09:08:12 +08:00
|
|
|
}
|
|
|
|
return m_image;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-26 19:42:27 +08:00
|
|
|
public ImageFormatDecoder (IBinaryStream file)
|
2016-10-26 09:08:12 +08:00
|
|
|
{
|
|
|
|
m_file = file;
|
|
|
|
var format = ImageFormat.FindFormat (file);
|
|
|
|
if (null == format)
|
|
|
|
throw new InvalidFormatException();
|
2016-10-26 19:42:27 +08:00
|
|
|
SourceFormat = format.Item1;
|
2016-10-26 09:08:12 +08:00
|
|
|
Info = format.Item2;
|
|
|
|
}
|
|
|
|
|
2016-10-29 06:03:11 +08:00
|
|
|
public ImageFormatDecoder (IBinaryStream file, ImageFormat format, ImageMetaData info)
|
|
|
|
{
|
|
|
|
m_file = file;
|
|
|
|
SourceFormat = format;
|
|
|
|
Info = info;
|
|
|
|
}
|
|
|
|
|
2018-08-31 08:41:02 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Create instance of ImageFormatDecoder from input binary stream.
|
|
|
|
/// In case of error input stream is disposed.
|
|
|
|
/// </summary>
|
|
|
|
public static ImageFormatDecoder Create (IBinaryStream input)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return new ImageFormatDecoder (input);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
input.Dispose();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-26 09:08:12 +08:00
|
|
|
bool m_disposed = false;
|
|
|
|
public void Dispose ()
|
|
|
|
{
|
|
|
|
if (!m_disposed)
|
|
|
|
{
|
|
|
|
m_file.Dispose();
|
|
|
|
m_disposed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract class BinaryImageDecoder : IImageDecoder
|
|
|
|
{
|
|
|
|
protected IBinaryStream m_input;
|
|
|
|
protected ImageData m_image;
|
|
|
|
|
2016-10-26 19:42:27 +08:00
|
|
|
public Stream Source { get { m_input.Position = 0; return m_input.AsStream; } }
|
2023-09-07 16:30:47 +08:00
|
|
|
public ImageFormat SourceFormat { get; protected set; }
|
2016-10-26 19:42:27 +08:00
|
|
|
public ImageMetaData Info { get; protected set; }
|
2018-12-02 23:45:13 +08:00
|
|
|
public ImageData Image { get { return m_image ?? (m_image = GetImageData()); } }
|
2016-10-26 09:08:12 +08:00
|
|
|
|
|
|
|
protected BinaryImageDecoder (IBinaryStream input)
|
|
|
|
{
|
|
|
|
m_input = input;
|
|
|
|
}
|
|
|
|
|
2016-10-29 06:03:11 +08:00
|
|
|
protected BinaryImageDecoder (IBinaryStream input, ImageMetaData info)
|
|
|
|
{
|
|
|
|
m_input = input;
|
|
|
|
Info = info;
|
|
|
|
}
|
|
|
|
|
2016-10-26 09:08:12 +08:00
|
|
|
protected abstract ImageData GetImageData ();
|
|
|
|
|
|
|
|
#region IDisposable members
|
|
|
|
public void Dispose ()
|
|
|
|
{
|
|
|
|
Dispose (true);
|
|
|
|
GC.SuppressFinalize (this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool m_disposed = false;
|
|
|
|
protected virtual void Dispose (bool disposing)
|
|
|
|
{
|
|
|
|
if (!m_disposed)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
m_input.Dispose();
|
|
|
|
m_disposed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
2018-10-09 18:36:18 +08:00
|
|
|
|
|
|
|
public class BitmapSourceDecoder : IImageDecoder
|
|
|
|
{
|
|
|
|
public Stream Source { get; set; }
|
|
|
|
public ImageFormat SourceFormat { get; set; }
|
|
|
|
public ImageMetaData Info { get; private set; }
|
|
|
|
public ImageData Image { get; private set; }
|
|
|
|
|
|
|
|
public BitmapSourceDecoder (BitmapSource bitmap)
|
|
|
|
{
|
|
|
|
Info = new ImageMetaData {
|
|
|
|
Width = (uint)bitmap.PixelWidth,
|
|
|
|
Height = (uint)bitmap.PixelHeight,
|
|
|
|
BPP = bitmap.Format.BitsPerPixel,
|
|
|
|
};
|
|
|
|
Image = new ImageData (bitmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2016-10-26 09:08:12 +08:00
|
|
|
}
|