(Image.Read): sort matched formats according to filename extension.

This commit is contained in:
morkt 2015-07-25 10:13:56 +04:00
parent 60479757ac
commit 097672845d

View File

@ -23,7 +23,9 @@
// IN THE SOFTWARE. // IN THE SOFTWARE.
// //
using System;
using System.IO; using System.IO;
using System.Linq;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
@ -108,6 +110,11 @@ namespace GameRes
public abstract void Write (Stream file, ImageData bitmap); public abstract void Write (Stream file, ImageData bitmap);
public static ImageData Read (Stream file) public static ImageData Read (Stream file)
{
return Read (null, file);
}
public static ImageData Read (string filename, Stream file)
{ {
bool need_dispose = false; bool need_dispose = false;
try try
@ -119,7 +126,7 @@ namespace GameRes
file = stream; file = stream;
need_dispose = true; need_dispose = true;
} }
var format = FindFormat (file); var format = FindFormat (file, filename);
if (null == format) if (null == format)
return null; return null;
file.Position = 0; file.Position = 0;
@ -132,12 +139,18 @@ namespace GameRes
} }
} }
public static System.Tuple<ImageFormat, ImageMetaData> FindFormat (Stream file) public static System.Tuple<ImageFormat, ImageMetaData> FindFormat (Stream file, string filename = null)
{ {
uint signature = FormatCatalog.ReadSignature (file); uint signature = FormatCatalog.ReadSignature (file);
Lazy<string> ext = null;
if (!string.IsNullOrEmpty (filename))
ext = new Lazy<string> (() => Path.GetExtension (filename).TrimStart ('.').ToLowerInvariant());
for (;;) for (;;)
{ {
var range = FormatCatalog.Instance.LookupSignature<ImageFormat> (signature); var range = FormatCatalog.Instance.LookupSignature<ImageFormat> (signature);
// check formats that match filename extension first
if (ext != null && range.Skip(1).Any())
range = range.OrderByDescending (f => f.Extensions.Any (e => e == ext.Value));
foreach (var impl in range) foreach (var impl in range)
{ {
try try