From 46c05b25de6a18efc138ca5220f64951173e558f Mon Sep 17 00:00:00 2001 From: morkt Date: Fri, 25 Jul 2014 08:56:32 +0400 Subject: [PATCH] ImageFormat.Read method made static. --- GameRes/Image.cs | 49 +++++++++++++++++++++++++------------ Image.Convert/Program.cs | 44 +++------------------------------ ImagePreview.cs | 53 ++-------------------------------------- 3 files changed, 40 insertions(+), 106 deletions(-) diff --git a/GameRes/Image.cs b/GameRes/Image.cs index bf3ac6c2..05b00220 100644 --- a/GameRes/Image.cs +++ b/GameRes/Image.cs @@ -20,12 +20,6 @@ namespace GameRes public class ImageEntry : Entry { public override string Type { get { return "image"; } } - /* - public ImageEntry () - { - Type = "image"; - } - */ } public class ImageData @@ -58,7 +52,12 @@ namespace GameRes { public override string Type { get { return "image"; } } - public ImageData Read (Stream file) + 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) { bool need_dispose = false; try @@ -70,10 +69,11 @@ namespace GameRes file = stream; need_dispose = true; } - var info = ReadMetaData (file); - if (null == info) - throw new InvalidFormatException(); - return Read (file, info); + var format = FindFormat (file); + if (null == format) + return null; + file.Position = 0; + return format.Item1.Read (file, format.Item2); } finally { @@ -82,10 +82,29 @@ namespace GameRes } } - public abstract ImageData Read (Stream file, ImageMetaData info); - public abstract void Write (Stream file, ImageData bitmap); - - public abstract ImageMetaData ReadMetaData (Stream file); + public static System.Tuple FindFormat (Stream file) + { + uint signature = FormatCatalog.ReadSignature (file); + for (;;) + { + var range = FormatCatalog.Instance.LookupSignature (signature); + foreach (var impl in range) + { + try + { + file.Position = 0; + ImageMetaData metadata = impl.ReadMetaData (file); + if (null != metadata) + return new System.Tuple (impl, metadata); + } + catch { } + } + if (0 == signature) + break; + signature = 0; + } + return null; + } public override Entry CreateEntry () { diff --git a/Image.Convert/Program.cs b/Image.Convert/Program.cs index 5d66def1..2604b96f 100644 --- a/Image.Convert/Program.cs +++ b/Image.Convert/Program.cs @@ -42,47 +42,11 @@ namespace GARbro return range.FirstOrDefault(); } - Tuple FindImageFormat (ArcView arc) - { - uint signature = arc.View.ReadUInt32 (0); - using (var stream = arc.CreateStream()) - { - for (;;) - { - var range = FormatCatalog.Instance.LookupSignature (signature); - foreach (var impl in range) - { - try - { - ImageMetaData metadata = impl.ReadMetaData (stream); - if (null != metadata) - return new Tuple (impl, metadata); - stream.Position = 0; - } - catch { } - } - if (0 == signature) - break; - signature = 0; - } - } - return null; - } - - ImageData ReadImage (ArcView file) - { - var format = FindImageFormat (file); - if (null == format) - return null; - using (var stream = file.CreateStream()) - return format.Item1.Read (stream, format.Item2); - } - void PrintMetaData (string filename) { - using (ArcView file = new ArcView (filename)) + using (var file = File.Open (filename, FileMode.Open, FileAccess.Read)) { - var format = FindImageFormat (file); + var format = ImageFormat.FindFormat (file); if (null == format) { Console.Error.WriteLine ("{0}: file format not recognized", filename); @@ -96,9 +60,9 @@ namespace GARbro void ConvertFile (string filename, ImageFormat format) { ImageData image; - using (var file = new ArcView (filename)) + using (var file = File.Open (filename, FileMode.Open, FileAccess.Read)) { - image = ReadImage (file); + image = ImageFormat.Read (file); if (null == image) { Console.Error.WriteLine ("{0}: Unknown image format", filename); diff --git a/ImagePreview.cs b/ImagePreview.cs index 1b35ad1c..93fb72d4 100644 --- a/ImagePreview.cs +++ b/ImagePreview.cs @@ -99,7 +99,7 @@ namespace GARbro.GUI } using (file) { - var data = ReadImage (file); + var data = ImageFormat.Read (file); if (null != data) SetPreviewImage (preview, data.Bitmap); else @@ -136,60 +136,11 @@ namespace GARbro.GUI }); } - ImageData ReadImage (Stream file) - { - bool need_dispose = false; - try - { - if (!file.CanSeek) - { - var stream = new MemoryStream(); - file.CopyTo (stream); - file = stream; - need_dispose = true; - } - var format = FindImageFormat (file); - if (null == format) - return null; - file.Position = 0; - return format.Item1.Read (file, format.Item2); - } - finally - { - if (need_dispose) - file.Dispose(); - } - } - - Tuple FindImageFormat (Stream file) - { - uint signature = FormatCatalog.ReadSignature (file); - for (;;) - { - var range = FormatCatalog.Instance.LookupSignature (signature); - foreach (var impl in range) - { - try - { - file.Position = 0; - ImageMetaData metadata = impl.ReadMetaData (file); - if (null != metadata) - return new Tuple (impl, metadata); - } - catch { } - } - if (0 == signature) - break; - signature = 0; - } - return null; - } - void ExtractImage (ArcFile arc, Entry entry, ImageFormat target_format) { using (var file = arc.OpenEntry (entry)) { - ImageData image = ReadImage (file); + ImageData image = ImageFormat.Read (file); if (null == image) throw new InvalidFormatException (string.Format ("{1}: {0}", guiStrings.MsgUnableInterpret, entry.Name)); string target_ext = target_format.Extensions.First();