(IImageDecoder): moved to separate file.

This commit is contained in:
morkt 2016-10-26 05:08:12 +04:00
parent 4901c2f1d3
commit d02a9b180a
3 changed files with 140 additions and 66 deletions

View File

@ -74,6 +74,7 @@
<Compile Include="GameRes.cs" /> <Compile Include="GameRes.cs" />
<Compile Include="Image.cs" /> <Compile Include="Image.cs" />
<Compile Include="ImageBMP.cs" /> <Compile Include="ImageBMP.cs" />
<Compile Include="ImageDecoder.cs" />
<Compile Include="ImageJPEG.cs" /> <Compile Include="ImageJPEG.cs" />
<Compile Include="ImagePNG.cs" /> <Compile Include="ImagePNG.cs" />
<Compile Include="ImageTGA.cs" /> <Compile Include="ImageTGA.cs" />

View File

@ -186,70 +186,4 @@ namespace GameRes
public static ImageFormat Bmp { get { return s_BmpFormat.Value; } } public static ImageFormat Bmp { get { return s_BmpFormat.Value; } }
public static ImageFormat Tga { get { return s_TgaFormat.Value; } } public static ImageFormat Tga { get { return s_TgaFormat.Value; } }
} }
public interface IImageDecoder : IDisposable
{
Stream Input { get; }
/// <summary>
/// Underlying image format or null if image is not represented by any format.
/// </summary>
ImageFormat Format { get; }
/// <summary>
/// Image parameters.
/// </summary>
ImageMetaData Info { get; }
/// <summary>
/// Decoded image data.
/// </summary>
ImageData Image { get; }
}
public sealed class ImageStreamDecoder : IImageDecoder
{
IBinaryStream m_file;
ImageFormat m_format;
ImageMetaData m_info;
ImageData m_image;
public Stream Input { get { m_file.Position = 0; return m_file.AsStream; } }
public ImageFormat Format { get { return m_format; } }
public ImageMetaData Info { get { return m_info; } }
public ImageData Image
{
get
{
if (null == m_image)
{
m_file.Position = 0;
m_image = m_format.Read (m_file, m_info);
}
return m_image;
}
}
public ImageStreamDecoder (IBinaryStream file)
{
m_file = file;
var format = ImageFormat.FindFormat (file);
if (null == format)
throw new InvalidFormatException();
m_format = format.Item1;
m_info = format.Item2;
}
bool m_disposed = false;
public void Dispose ()
{
if (!m_disposed)
{
m_file.Dispose();
m_disposed = true;
}
}
}
} }

139
GameRes/ImageDecoder.cs Normal file
View File

@ -0,0 +1,139 @@
//! \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;
namespace GameRes
{
public interface IImageDecoder : IDisposable
{
Stream Input { get; }
/// <summary>
/// Underlying image format or null if image is not represented by any format.
/// </summary>
ImageFormat Format { get; }
/// <summary>
/// Image parameters.
/// </summary>
ImageMetaData Info { get; }
/// <summary>
/// Decoded image data.
/// </summary>
ImageData Image { get; }
}
public sealed class ImageStreamDecoder : IImageDecoder
{
IBinaryStream m_file;
ImageData m_image;
public Stream Input { get { m_file.Position = 0; return m_file.AsStream; } }
public ImageFormat Format { get; private set; }
public ImageMetaData Info { get; private set; }
public ImageData Image
{
get
{
if (null == m_image)
{
m_file.Position = 0;
m_image = Format.Read (m_file, Info);
}
return m_image;
}
}
public ImageStreamDecoder (IBinaryStream file)
{
m_file = file;
var format = ImageFormat.FindFormat (file);
if (null == format)
throw new InvalidFormatException();
Format = format.Item1;
Info = format.Item2;
}
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;
public Stream Input { get { m_input.Position = 0; return m_input.AsStream; } }
public ImageFormat Format { get { return null; } }
public ImageMetaData Info { get; protected set; }
public ImageData Image
{
get
{
if (null == m_image)
m_image = GetImageData();
return m_image;
}
}
protected BinaryImageDecoder (IBinaryStream input)
{
m_input = input;
}
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
}
}