(IImageDecoder): new interface.

This commit is contained in:
morkt
2016-10-25 18:18:51 +04:00
parent 27dbb52b89
commit b05c54047d
6 changed files with 118 additions and 27 deletions

View File

@@ -217,6 +217,11 @@ namespace GameRes
return BinaryStream.FromStream (input, entry.Name);
}
public IImageDecoder OpenImage (Entry entry)
{
return m_interface.OpenImage (this, entry);
}
public ArchiveFileSystem CreateFileSystem ()
{
if (m_interface.IsHierarchic)

View File

@@ -722,6 +722,17 @@ namespace GameRes
return m_vfs.Top.OpenView (entry);
}
public static IImageDecoder OpenImage (Entry entry)
{
var fs = m_vfs.Top;
var arc_fs = fs as ArchiveFileSystem;
if (arc_fs != null)
return arc_fs.Source.OpenImage (entry);
var input = fs.OpenBinaryStream (entry);
return new ImageStreamDecoder (input);
}
public static Stream OpenStream (string filename)
{
return m_vfs.Top.OpenStream (m_vfs.Top.FindFile (filename));

View File

@@ -197,6 +197,15 @@ namespace GameRes
return arc.File.CreateStream (entry.Offset, entry.Size, entry.Name);
}
/// <summary>
/// Open <paramref name="entry"> as image. Throws InvalidFormatException if entry is not an image.
/// </summary>
public virtual IImageDecoder OpenImage (ArcFile arc, Entry entry)
{
var input = arc.OpenBinaryEntry (entry);
return new ImageStreamDecoder (input);
}
/// <summary>
/// Create file corresponding to <paramref name="entry"/> in current directory and open it
/// for writing. Overwrites existing file, if any.

View File

@@ -186,4 +186,70 @@ namespace GameRes
public static ImageFormat Bmp { get { return s_BmpFormat.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;
}
}
}
}