mirror of
https://github.com/crskycode/GARbro.git
synced 2026-06-06 05:38:48 +08:00
ImageFormat.Read method made static.
This commit is contained in:
@@ -20,12 +20,6 @@ namespace GameRes
|
|||||||
public class ImageEntry : Entry
|
public class ImageEntry : Entry
|
||||||
{
|
{
|
||||||
public override string Type { get { return "image"; } }
|
public override string Type { get { return "image"; } }
|
||||||
/*
|
|
||||||
public ImageEntry ()
|
|
||||||
{
|
|
||||||
Type = "image";
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ImageData
|
public class ImageData
|
||||||
@@ -58,7 +52,12 @@ namespace GameRes
|
|||||||
{
|
{
|
||||||
public override string Type { get { return "image"; } }
|
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;
|
bool need_dispose = false;
|
||||||
try
|
try
|
||||||
@@ -70,10 +69,11 @@ namespace GameRes
|
|||||||
file = stream;
|
file = stream;
|
||||||
need_dispose = true;
|
need_dispose = true;
|
||||||
}
|
}
|
||||||
var info = ReadMetaData (file);
|
var format = FindFormat (file);
|
||||||
if (null == info)
|
if (null == format)
|
||||||
throw new InvalidFormatException();
|
return null;
|
||||||
return Read (file, info);
|
file.Position = 0;
|
||||||
|
return format.Item1.Read (file, format.Item2);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -82,10 +82,29 @@ namespace GameRes
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract ImageData Read (Stream file, ImageMetaData info);
|
public static System.Tuple<ImageFormat, ImageMetaData> FindFormat (Stream file)
|
||||||
public abstract void Write (Stream file, ImageData bitmap);
|
{
|
||||||
|
uint signature = FormatCatalog.ReadSignature (file);
|
||||||
public abstract ImageMetaData ReadMetaData (Stream 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 ()
|
public override Entry CreateEntry ()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -42,47 +42,11 @@ namespace GARbro
|
|||||||
return range.FirstOrDefault();
|
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)
|
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)
|
if (null == format)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine ("{0}: file format not recognized", filename);
|
Console.Error.WriteLine ("{0}: file format not recognized", filename);
|
||||||
@@ -96,9 +60,9 @@ namespace GARbro
|
|||||||
void ConvertFile (string filename, ImageFormat format)
|
void ConvertFile (string filename, ImageFormat format)
|
||||||
{
|
{
|
||||||
ImageData image;
|
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)
|
if (null == image)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine ("{0}: Unknown image format", filename);
|
Console.Error.WriteLine ("{0}: Unknown image format", filename);
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ namespace GARbro.GUI
|
|||||||
}
|
}
|
||||||
using (file)
|
using (file)
|
||||||
{
|
{
|
||||||
var data = ReadImage (file);
|
var data = ImageFormat.Read (file);
|
||||||
if (null != data)
|
if (null != data)
|
||||||
SetPreviewImage (preview, data.Bitmap);
|
SetPreviewImage (preview, data.Bitmap);
|
||||||
else
|
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)
|
void ExtractImage (ArcFile arc, Entry entry, ImageFormat target_format)
|
||||||
{
|
{
|
||||||
using (var file = arc.OpenEntry (entry))
|
using (var file = arc.OpenEntry (entry))
|
||||||
{
|
{
|
||||||
ImageData image = ReadImage (file);
|
ImageData image = ImageFormat.Read (file);
|
||||||
if (null == image)
|
if (null == image)
|
||||||
throw new InvalidFormatException (string.Format ("{1}: {0}", guiStrings.MsgUnableInterpret, entry.Name));
|
throw new InvalidFormatException (string.Format ("{1}: {0}", guiStrings.MsgUnableInterpret, entry.Name));
|
||||||
string target_ext = target_format.Extensions.First();
|
string target_ext = target_format.Extensions.First();
|
||||||
|
|||||||
Reference in New Issue
Block a user