ImageFormat.Read method made static.

This commit is contained in:
morkt 2014-07-25 08:56:32 +04:00
parent 2e75b3e183
commit 46c05b25de
3 changed files with 40 additions and 106 deletions

View File

@ -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<ImageFormat, ImageMetaData> FindFormat (Stream file)
{
uint signature = FormatCatalog.ReadSignature (file);
for (;;)
{
var range = FormatCatalog.Instance.LookupSignature<ImageFormat> (signature);
foreach (var impl in range)
{
try
{
file.Position = 0;
ImageMetaData metadata = impl.ReadMetaData (file);
if (null != metadata)
return new System.Tuple<ImageFormat, ImageMetaData> (impl, metadata);
}
catch { }
}
if (0 == signature)
break;
signature = 0;
}
return null;
}
public override Entry CreateEntry ()
{

View File

@ -42,47 +42,11 @@ namespace GARbro
return range.FirstOrDefault();
}
Tuple<ImageFormat, ImageMetaData> FindImageFormat (ArcView arc)
{
uint signature = arc.View.ReadUInt32 (0);
using (var stream = arc.CreateStream())
{
for (;;)
{
var range = FormatCatalog.Instance.LookupSignature<ImageFormat> (signature);
foreach (var impl in range)
{
try
{
ImageMetaData metadata = impl.ReadMetaData (stream);
if (null != metadata)
return new Tuple<ImageFormat, ImageMetaData> (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);

View File

@ -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<ImageFormat, ImageMetaData> FindImageFormat (Stream file)
{
uint signature = FormatCatalog.ReadSignature (file);
for (;;)
{
var range = FormatCatalog.Instance.LookupSignature<ImageFormat> (signature);
foreach (var impl in range)
{
try
{
file.Position = 0;
ImageMetaData metadata = impl.ReadMetaData (file);
if (null != metadata)
return new Tuple<ImageFormat, ImageMetaData> (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();