(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

@ -306,31 +306,35 @@ namespace GARbro.GUI
void ExtractImage (ArcFile arc, Entry entry, ImageFormat target_format) void ExtractImage (ArcFile arc, Entry entry, ImageFormat target_format)
{ {
using (var file = arc.OpenBinaryEntry (entry)) try
{ {
var src_format = ImageFormat.FindFormat (file); using (var decoder = arc.OpenImage (entry))
if (null == src_format)
throw new InvalidFormatException (string.Format ("{1}: {0}", guiStrings.MsgUnableInterpretImage, entry.Name));
file.Position = 0;
string target_ext = target_format.Extensions.FirstOrDefault() ?? "";
string outname = FindUniqueFileName (entry.Name, target_ext);
if (src_format.Item1 == target_format)
{ {
// source format is the same as a target, copy file as is var src_format = decoder.Format; // could be null
using (var output = ArchiveFormat.CreateFile (outname)) string target_ext = target_format.Extensions.FirstOrDefault() ?? "";
file.AsStream.CopyTo (output); string outname = FindUniqueFileName (entry.Name, target_ext);
return; if (src_format == target_format)
} {
ImageData image = src_format.Item1.Read (file, src_format.Item2); // source format is the same as a target, copy file as is
if (m_adjust_image_offset) using (var output = ArchiveFormat.CreateFile (outname))
{ decoder.Input.CopyTo (output);
image = AdjustImageOffset (image); return;
} }
using (var outfile = ArchiveFormat.CreateFile (outname)) ImageData image = decoder.Image;
{ if (m_adjust_image_offset)
target_format.Write (outfile, image); {
image = AdjustImageOffset (image);
}
using (var outfile = ArchiveFormat.CreateFile (outname))
{
target_format.Write (outfile, image);
}
} }
} }
catch
{
throw new InvalidFormatException (string.Format ("{1}: {0}", guiStrings.MsgUnableInterpretImage, entry.Name));
}
} }
static ImageData AdjustImageOffset (ImageData image) static ImageData AdjustImageOffset (ImageData image)

View File

@ -225,13 +225,9 @@ namespace GARbro.GUI
{ {
try try
{ {
using (var file = VFS.OpenBinaryStream (preview.Entry)) using (var data = VFS.OpenImage (preview.Entry))
{ {
var data = ImageFormat.Read (file); SetPreviewImage (preview, data.Image.Bitmap);
if (null != data)
SetPreviewImage (preview, data.Bitmap);
else
Trace.WriteLine ("Cannot parse image format", preview.Name);
} }
} }
catch (Exception X) catch (Exception X)

View File

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

View File

@ -722,6 +722,17 @@ namespace GameRes
return m_vfs.Top.OpenView (entry); 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) public static Stream OpenStream (string filename)
{ {
return m_vfs.Top.OpenStream (m_vfs.Top.FindFile (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); 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> /// <summary>
/// Create file corresponding to <paramref name="entry"/> in current directory and open it /// Create file corresponding to <paramref name="entry"/> in current directory and open it
/// for writing. Overwrites existing file, if any. /// 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 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;
}
}
}
} }