(EME): switched EmFormat to EmeImageDecoder.

use OpenImage method override to decode image.
This commit is contained in:
morkt 2017-12-21 04:16:09 +04:00
parent 5923894106
commit 2aa471bc5f
2 changed files with 38 additions and 59 deletions

View File

@ -2,7 +2,7 @@
//! \date Tue Mar 15 08:13:00 2016 //! \date Tue Mar 15 08:13:00 2016
//! \brief Emon Engine (えもんエンジン) resource archives. //! \brief Emon Engine (えもんエンジン) resource archives.
// //
// Copyright (C) 2016 by morkt // Copyright (C) 2016-2017 by morkt
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to // of this software and associated documentation files (the "Software"), to
@ -27,7 +27,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.IO; using System.IO;
using System.Text;
using GameRes.Compression; using GameRes.Compression;
using GameRes.Utility; using GameRes.Utility;
@ -91,8 +90,6 @@ namespace GameRes.Formats.EmonEngine
return base.OpenEntry (arc, entry); return base.OpenEntry (arc, entry);
if (3 == ement.SubType) if (3 == ement.SubType)
return OpenScript (emarc, ement); return OpenScript (emarc, ement);
else if (4 == ement.SubType)
return OpenImage (emarc, ement);
else if (5 == ement.SubType && entry.Size > 4) else if (5 == ement.SubType && entry.Size > 4)
return OpenT5 (emarc, ement); return OpenT5 (emarc, ement);
else else
@ -140,20 +137,32 @@ namespace GameRes.Formats.EmonEngine
} }
} }
Stream OpenImage (EmeArchive arc, EmEntry entry) public override IImageDecoder OpenImage (ArcFile arc, Entry entry)
{ {
var header = new byte[40]; const int header_size = 32;
Encoding.ASCII.GetBytes ("EMBM", 0, 4, header, 0); var ement = (EmEntry)entry;
LittleEndian.Pack ((ushort)entry.LzssFrameSize, header, 4); if (ement.SubType != 4)
LittleEndian.Pack ((ushort)entry.LzssInitPos, header, 6); return base.OpenImage (arc, entry);
arc.File.View.Read (entry.Offset, header, 8, 32); var emarc = (EmeArchive)arc;
Decrypt (header, 8, 32, arc.Key); var header = arc.File.View.ReadBytes (entry.Offset, header_size);
uint entry_size = entry.Size; Decrypt (header, 0, header_size, emarc.Key);
uint colors = LittleEndian.ToUInt16 (header, 14); var info = new EmMetaData {
if (0 != colors && header[0] != 7) LzssFrameSize = ement.LzssFrameSize,
entry_size += Math.Max (colors, 3u) * 4; LzssInitPos = ement.LzssInitPos,
var input = arc.File.CreateStream (entry.Offset+32, entry_size); BPP = header.ToUInt16 (0) & 0xFF,
return new PrefixStream (header, input); Width = header.ToUInt16 (2),
Height = header.ToUInt16 (4),
Colors = header.ToUInt16 (6),
Stride = header.ToInt32 (8),
OffsetX = header.ToInt32 (0xC),
OffsetY = header.ToInt32 (0x10),
DataOffset = header_size,
};
uint entry_size = entry.Size + header_size;
if (0 != info.Colors && header[0] != 7)
entry_size += (uint)Math.Max (info.Colors, 3) * 4;
var input = arc.File.CreateStream (entry.Offset, entry_size);
return new EmeImageDecoder (input, info);
} }
Stream OpenT5 (EmeArchive arc, EmEntry entry) Stream OpenT5 (EmeArchive arc, EmEntry entry)

View File

@ -2,7 +2,7 @@
//! \date Wed Mar 16 01:08:47 2016 //! \date Wed Mar 16 01:08:47 2016
//! \brief Emon Engine compressed images. //! \brief Emon Engine compressed images.
// //
// Copyright (C) 2016 by morkt // Copyright (C) 2016-2017 by morkt
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to // of this software and associated documentation files (the "Software"), to
@ -24,7 +24,6 @@
// //
using System; using System;
using System.ComponentModel.Composition;
using System.IO; using System.IO;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
@ -41,47 +40,23 @@ namespace GameRes.Formats.EmonEngine
public int DataOffset; public int DataOffset;
} }
[Export(typeof(ImageFormat))] internal class EmeImageDecoder : BinaryImageDecoder
public class EmFormat : ImageFormat
{ {
public override string Tag { get { return "EM/BMP"; } } public EmeImageDecoder (IBinaryStream input, EmMetaData info) : base (input, info)
public override string Description { get { return "Emon Engine image format"; } }
// this is an artificial prefix embedded into stream by EmeOpener.OpenImage
public override uint Signature { get { return 0x4D424D45; } } // 'EMBM'
public EmFormat ()
{ {
Extensions = new string[] { "bmp" };
} }
public override ImageMetaData ReadMetaData (IBinaryStream stream) protected override ImageData GetImageData ()
{ {
stream.ReadInt32(); var meta = (EmMetaData)Info;
var info = new EmMetaData(); m_input.Position = meta.DataOffset;
info.LzssFrameSize = stream.ReadUInt16();
info.LzssInitPos = stream.ReadUInt16();
info.BPP = stream.ReadUInt16() & 0xFF;
info.Width = stream.ReadUInt16();
info.Height = stream.ReadUInt16();
info.Colors = stream.ReadUInt16();
info.Stride = stream.ReadInt32();
info.OffsetX = stream.ReadInt32();
info.OffsetY = stream.ReadInt32();
info.DataOffset = 40;
return info;
}
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (EmMetaData)info;
stream.Position = meta.DataOffset;
BitmapPalette palette = null; BitmapPalette palette = null;
if (meta.Colors != 0) if (meta.Colors != 0)
palette = ReadPalette (stream.AsStream, Math.Max (meta.Colors, 3), PaletteFormat.RgbX); palette = ImageFormat.ReadPalette (m_input.AsStream, Math.Max (meta.Colors, 3), PaletteFormat.RgbX);
var pixels = new byte[meta.Stride * (int)info.Height]; var pixels = new byte[meta.Stride * (int)meta.Height];
if (meta.LzssFrameSize != 0) if (meta.LzssFrameSize != 0)
{ {
using (var lzss = new LzssStream (stream.AsStream, LzssMode.Decompress, true)) using (var lzss = new LzssStream (m_input.AsStream, LzssMode.Decompress, true))
{ {
lzss.Config.FrameSize = meta.LzssFrameSize; lzss.Config.FrameSize = meta.LzssFrameSize;
lzss.Config.FrameInitPos = meta.LzssInitPos; lzss.Config.FrameInitPos = meta.LzssInitPos;
@ -91,11 +66,11 @@ namespace GameRes.Formats.EmonEngine
} }
else else
{ {
if (pixels.Length != stream.Read (pixels, 0, pixels.Length)) if (pixels.Length != m_input.Read (pixels, 0, pixels.Length))
throw new EndOfStreamException(); throw new EndOfStreamException();
} }
if (7 == meta.BPP) if (7 == meta.BPP)
return ImageData.Create (info, PixelFormats.Gray8, palette, pixels, meta.Stride); return ImageData.Create (Info, PixelFormats.Gray8, palette, pixels, meta.Stride);
PixelFormat format; PixelFormat format;
if (32 == meta.BPP) if (32 == meta.BPP)
@ -104,12 +79,7 @@ namespace GameRes.Formats.EmonEngine
format = PixelFormats.Bgr24; format = PixelFormats.Bgr24;
else else
format = PixelFormats.Indexed8; format = PixelFormats.Indexed8;
return ImageData.CreateFlipped (info, format, palette, pixels, meta.Stride); return ImageData.CreateFlipped (Info, format, palette, pixels, meta.Stride);
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("EmFormat.Write not implemented");
} }
} }
} }