mirror of
https://github.com/crskycode/GARbro.git
synced 2024-12-24 20:04:13 +08:00
introduced extensions infrastructure.
This commit is contained in:
parent
df901a3670
commit
0732a9cf45
@ -85,20 +85,31 @@ namespace GameRes
|
||||
catalog.Catalogs.Add (new DirectoryCatalog (Path.GetDirectoryName (System.Reflection.Assembly.GetExecutingAssembly().Location), "Arc*.dll"));
|
||||
|
||||
//Create the CompositionContainer with the parts in the catalog
|
||||
var container = new CompositionContainer (catalog);
|
||||
|
||||
using (var container = new CompositionContainer (catalog))
|
||||
{
|
||||
//Fill the imports of this object
|
||||
container.ComposeParts (this);
|
||||
AddResourceImpl (m_image_formats);
|
||||
AddResourceImpl (m_arc_formats);
|
||||
AddResourceImpl (m_audio_formats);
|
||||
AddResourceImpl (m_script_formats);
|
||||
AddResourceImpl (m_image_formats, container);
|
||||
AddResourceImpl (m_arc_formats, container);
|
||||
AddResourceImpl (m_audio_formats, container);
|
||||
AddResourceImpl (m_script_formats, container);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddResourceImpl (IEnumerable<IResource> formats)
|
||||
private void AddResourceImpl (IEnumerable<IResource> formats, CompositionContainer container)
|
||||
{
|
||||
foreach (var impl in formats)
|
||||
{
|
||||
try
|
||||
{
|
||||
var part = AttributedModelServices.CreatePart (impl);
|
||||
if (part.ImportDefinitions.Any())
|
||||
container.SatisfyImportsOnce (part);
|
||||
}
|
||||
catch (Exception X)
|
||||
{
|
||||
System.Diagnostics.Trace.WriteLine (X.Message, impl.Tag);
|
||||
}
|
||||
foreach (var ext in impl.Extensions)
|
||||
{
|
||||
m_extension_map.Add (ext.ToUpperInvariant(), impl);
|
||||
|
@ -30,6 +30,7 @@ using System.ComponentModel.Composition;
|
||||
using System.Windows.Media.Imaging;
|
||||
using GameRes.Utility;
|
||||
using System.Windows.Media;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace GameRes
|
||||
{
|
||||
@ -39,6 +40,11 @@ namespace GameRes
|
||||
public uint HeaderLength;
|
||||
}
|
||||
|
||||
public interface IBmpExtension
|
||||
{
|
||||
ImageData Read (Stream file, BmpMetaData info);
|
||||
}
|
||||
|
||||
[Export(typeof(ImageFormat))]
|
||||
public class BmpFormat : ImageFormat
|
||||
{
|
||||
@ -46,22 +52,27 @@ namespace GameRes
|
||||
public override string Description { get { return "Windows device independent bitmap"; } }
|
||||
public override uint Signature { get { return 0; } }
|
||||
|
||||
#pragma warning disable 649
|
||||
[ImportMany(typeof(IBmpExtension))]
|
||||
private IEnumerable<IBmpExtension> m_extensions;
|
||||
#pragma warning restore 649
|
||||
|
||||
public override ImageData Read (Stream file, ImageMetaData info)
|
||||
{
|
||||
var bmp_info = info as BmpMetaData;
|
||||
if (bmp_info != null && file.CanSeek)
|
||||
if (bmp_info != null)
|
||||
{
|
||||
uint bmp_length = info.Width * info.Height * (uint)info.BPP/8 + bmp_info.HeaderLength;
|
||||
if (bmp_length == bmp_info.ImageLength || bmp_length+2 == bmp_info.ImageLength)
|
||||
foreach (var ext in m_extensions)
|
||||
{
|
||||
if (0x20 == info.BPP)
|
||||
try
|
||||
{
|
||||
return ReadBitmapBGRA (file, bmp_info);
|
||||
var image = ext.Read (file, bmp_info);
|
||||
if (null != image)
|
||||
return image;
|
||||
}
|
||||
else if (0x18 == info.BPP
|
||||
&& (bmp_info.ImageLength + info.Width * info.Height) == file.Length)
|
||||
catch (System.Exception X)
|
||||
{
|
||||
return ReadBitmapWithAlpha (file, bmp_info);
|
||||
System.Diagnostics.Trace.WriteLine (X.Message, ext.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -79,46 +90,6 @@ namespace GameRes
|
||||
encoder.Save (file);
|
||||
}
|
||||
|
||||
private ImageData ReadBitmapWithAlpha (Stream file, BmpMetaData info)
|
||||
{
|
||||
file.Position = info.ImageLength;
|
||||
var alpha = new byte[info.Width*info.Height];
|
||||
if (alpha.Length != file.Read (alpha, 0, alpha.Length))
|
||||
throw new EndOfStreamException();
|
||||
|
||||
file.Position = info.HeaderLength;
|
||||
int dst_stride = (int)info.Width * 4;
|
||||
var pixels = new byte[(int)info.Height * dst_stride];
|
||||
int a_src = 0;
|
||||
for (int y = (int)info.Height-1; y >= 0; --y)
|
||||
{
|
||||
int dst = dst_stride * y;
|
||||
for (int x = 0; x < dst_stride; x += 4)
|
||||
{
|
||||
file.Read (pixels, dst+x, 3);
|
||||
pixels[dst+x+3] = alpha[a_src++];
|
||||
}
|
||||
}
|
||||
return ImageData.Create (info, PixelFormats.Bgra32, null, pixels, dst_stride);
|
||||
}
|
||||
|
||||
private ImageData ReadBitmapBGRA (Stream file, BmpMetaData info)
|
||||
{
|
||||
file.Position = info.HeaderLength;
|
||||
int stride = (int)info.Width * 4;
|
||||
var pixels = new byte[(int)info.Height * stride];
|
||||
bool has_alpha = false;
|
||||
for (int y = (int)info.Height-1; y >= 0; --y)
|
||||
{
|
||||
int dst = stride * y;
|
||||
file.Read (pixels, dst, stride);
|
||||
for (int x = 3; !has_alpha && x < stride; x += 4)
|
||||
has_alpha = pixels[dst+x] != 0;
|
||||
}
|
||||
PixelFormat format = has_alpha ? PixelFormats.Bgra32 : PixelFormats.Bgr32;
|
||||
return ImageData.Create (info, format, null, pixels, stride);
|
||||
}
|
||||
|
||||
void SkipBytes (BinaryReader file, uint num)
|
||||
{
|
||||
if (file.BaseStream.CanSeek)
|
||||
@ -163,4 +134,69 @@ namespace GameRes
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Export(typeof(IBmpExtension))]
|
||||
public class BitmapWithAlpha : IBmpExtension
|
||||
{
|
||||
public ImageData Read (Stream file, BmpMetaData info)
|
||||
{
|
||||
if (file.CanSeek)
|
||||
{
|
||||
var width_x_height = info.Width * info.Height;
|
||||
uint bmp_length = width_x_height * (uint)info.BPP/8 + info.HeaderLength;
|
||||
if (bmp_length == info.ImageLength || bmp_length+2 == info.ImageLength)
|
||||
{
|
||||
if (0x20 == info.BPP)
|
||||
{
|
||||
return ReadBitmapBGRA (file, info);
|
||||
}
|
||||
else if (0x18 == info.BPP && (info.ImageLength + width_x_height) == file.Length)
|
||||
{
|
||||
return ReadBitmapWithAlpha (file, info);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private ImageData ReadBitmapWithAlpha (Stream file, BmpMetaData info)
|
||||
{
|
||||
file.Position = info.ImageLength;
|
||||
var alpha = new byte[info.Width*info.Height];
|
||||
if (alpha.Length != file.Read (alpha, 0, alpha.Length))
|
||||
throw new EndOfStreamException();
|
||||
|
||||
file.Position = info.HeaderLength;
|
||||
int dst_stride = (int)info.Width * 4;
|
||||
var pixels = new byte[(int)info.Height * dst_stride];
|
||||
int a_src = 0;
|
||||
for (int y = (int)info.Height-1; y >= 0; --y)
|
||||
{
|
||||
int dst = dst_stride * y;
|
||||
for (int x = 0; x < dst_stride; x += 4)
|
||||
{
|
||||
file.Read (pixels, dst+x, 3);
|
||||
pixels[dst+x+3] = alpha[a_src++];
|
||||
}
|
||||
}
|
||||
return ImageData.Create (info, PixelFormats.Bgra32, null, pixels, dst_stride);
|
||||
}
|
||||
|
||||
private ImageData ReadBitmapBGRA (Stream file, BmpMetaData info)
|
||||
{
|
||||
file.Position = info.HeaderLength;
|
||||
int stride = (int)info.Width * 4;
|
||||
var pixels = new byte[(int)info.Height * stride];
|
||||
bool has_alpha = false;
|
||||
for (int y = (int)info.Height-1; y >= 0; --y)
|
||||
{
|
||||
int dst = stride * y;
|
||||
file.Read (pixels, dst, stride);
|
||||
for (int x = 3; !has_alpha && x < stride; x += 4)
|
||||
has_alpha = pixels[dst+x] != 0;
|
||||
}
|
||||
PixelFormat format = has_alpha ? PixelFormats.Bgra32 : PixelFormats.Bgr32;
|
||||
return ImageData.Create (info, format, null, pixels, stride);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user