diff --git a/ArcFormats/ArcFormats.csproj b/ArcFormats/ArcFormats.csproj index e558dade..020ccfaa 100644 --- a/ArcFormats/ArcFormats.csproj +++ b/ArcFormats/ArcFormats.csproj @@ -63,6 +63,7 @@ + diff --git a/ArcFormats/Entis/ArcERI.cs b/ArcFormats/Entis/ArcERI.cs new file mode 100644 index 00000000..ee2b7c8a --- /dev/null +++ b/ArcFormats/Entis/ArcERI.cs @@ -0,0 +1,163 @@ +//! \file ArcERI.cs +//! \date Wed Jan 27 18:24:06 2016 +//! \brief Entis multi-frame image format. +// +// Copyright (C) 2016 by morkt +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. +// + +using System; +using System.Collections.Generic; +using System.ComponentModel.Composition; +using System.IO; +using System.Linq; +using System.Windows.Media; +using GameRes.Utility; + +namespace GameRes.Formats.Entis +{ + [Export(typeof(ArchiveFormat))] + public class EriOpener : ArchiveFormat + { + public override string Tag { get { return "ERI/MULTI"; } } + public override string Description { get { return "Entis multi-frame image format"; } } + public override uint Signature { get { return 0x69746e45u; } } // 'Enti' + public override bool IsHierarchic { get { return false; } } + public override bool CanCreate { get { return false; } } + + public EriOpener () + { + Extensions = new string[] { "eri" }; + } + + static readonly Lazy s_EriFormat = new Lazy (() => ImageFormat.FindByTag ("ERI")); + + public override ArcFile TryOpen (ArcView file) + { + if (!file.View.AsciiEqual (0x10, "Entis Rasterized Image")) + return null; + EriMetaData info; + using (var eris = file.CreateStream()) + info = s_EriFormat.Value.ReadMetaData (eris) as EriMetaData; + + if (null == info || null == info.Header || !IsSaneCount (info.Header.FrameCount)) + return null; + info.FileName = file.Name; + string base_name = Path.GetFileNameWithoutExtension (file.Name); + + int count = info.Header.FrameCount; + long current_offset = info.StreamPos; + var dir = new List (count); + var id = new AsciiString (8); + Color[] palette = null; + int i = 0; + while (i < count && current_offset < file.MaxOffset) + { + if (file.View.Read (current_offset, id.Value, 0, 8) < 8) + break; + if ("Stream " == id) + { + current_offset += 0x10; + continue; + } + long section_size = file.View.ReadInt64 (current_offset+8); + if (section_size < 0 || section_size > int.MaxValue) + throw new FileSizeException(); + current_offset += 0x10; + if (0 == section_size) + continue; + if ("Palette " == id) + { + using (var stream = file.CreateStream (current_offset, (uint)section_size)) + palette = EriFormat.ReadPalette (stream, (int)section_size); + } + else if ("ImageFrm" == id || "DiffeFrm" == id) + { + var entry = new EriEntry { + Name = string.Format ("{0}#{1:D4}.tga", base_name, i++), + Type = "image", + Offset = current_offset, + Size = (uint)section_size, + FrameIndex = dir.Count, + IsDiff = "DiffeFrm" == id, + }; + if (!entry.CheckPlacement (file.MaxOffset)) + return null; + dir.Add (entry); + } + current_offset += section_size; + } + if (0 == dir.Count) + return null; + return new EriMultiImage (file, this, dir, info, palette); + } + + public override Stream OpenEntry (ArcFile arc, Entry entry) + { + var earc = (EriMultiImage)arc; + var eent = (EriEntry)entry; + var pixels = earc.GetFrame (eent.FrameIndex); + return TgaStream.Create (earc.Info, pixels); + } + } + + internal class EriMultiImage : ArcFile + { + public readonly EriMetaData Info; + public readonly Color[] Palette; + byte[][] Frames; + + public EriMultiImage (ArcView arc, ArchiveFormat impl, ICollection dir, EriMetaData info, Color[] palette) + : base (arc, impl, dir) + { + Info = info; + Palette = palette; + Frames = new byte[dir.Count][]; + } + + public byte[] GetFrame (int index) + { + if (index >= Frames.Length) + throw new ArgumentException ("index"); + if (null != Frames[index]) + return Frames[index]; + + var entry = Dir.ElementAt (index) as EriEntry; + byte[] prev_frame = null; + if (index > 0 && entry.IsDiff) + { + prev_frame = GetFrame (index-1); + } + using (var stream = File.CreateStream (entry.Offset, entry.Size)) + { + var reader = new EriReader (stream, Info, Palette, prev_frame); + reader.DecodeImage(); + Frames[index] = reader.Data; + } + return Frames[index]; + } + } + + internal class EriEntry : Entry + { + public int FrameIndex; + public bool IsDiff; + } +} diff --git a/ArcFormats/Entis/EriReader.cs b/ArcFormats/Entis/EriReader.cs index 980e99c8..14752f49 100644 --- a/ArcFormats/Entis/EriReader.cs +++ b/ArcFormats/Entis/EriReader.cs @@ -49,15 +49,17 @@ namespace GameRes.Formats.Entis ErisaProbModel m_pProbERISA; PtrProcedure[] m_pfnColorOperation; + byte[] m_src_frame; public byte[] Data { get { return m_output; } } public PixelFormat Format { get; private set; } public int Stride { get { return Math.Abs (m_dwBytesPerLine); } } public BitmapPalette Palette { get; private set; } - public EriReader (Stream stream, EriMetaData info, Color[] palette) + public EriReader (Stream stream, EriMetaData info, Color[] palette, byte[] key_frame = null) { m_info = info; + m_src_frame = key_frame; if (CvType.Lossless_ERI == m_info.Transformation) InitializeLossless(); else if (CvType.LOT_ERI == m_info.Transformation @@ -98,12 +100,14 @@ namespace GameRes.Formats.Entis case EriCode.Nemesis: case EriCode.RunlengthHuffman: case EriCode.RunlengthGamma: + if (0 == m_info.BlockingDegree) + throw new InvalidFormatException(); + break; + case EriCode.ArithmeticCode: break; default: throw new InvalidFormatException(); } - if (0 == m_info.BlockingDegree) - throw new InvalidFormatException(); switch (m_info.FormatType & (int)EriImage.TypeMask) { @@ -124,19 +128,22 @@ namespace GameRes.Formats.Entis throw new InvalidFormatException(); } - m_nBlockSize = (uint) (1 << m_info.BlockingDegree); - m_nBlockArea = (uint) (1 << (m_info.BlockingDegree * 2)); - m_nBlockSamples = m_nBlockArea * m_nChannelCount; - m_nWidthBlocks = (uint)((m_info.Width + m_nBlockSize - 1) >> m_info.BlockingDegree); - m_nHeightBlocks = (uint)((m_info.Height + m_nBlockSize - 1) >> m_info.BlockingDegree); + if (0 != m_info.BlockingDegree) + { + m_nBlockSize = (uint) (1 << m_info.BlockingDegree); + m_nBlockArea = (uint) (1 << (m_info.BlockingDegree * 2)); + m_nBlockSamples = m_nBlockArea * m_nChannelCount; + m_nWidthBlocks = (uint)((m_info.Width + m_nBlockSize - 1) >> m_info.BlockingDegree); + m_nHeightBlocks = (uint)((m_info.Height + m_nBlockSize - 1) >> m_info.BlockingDegree); - m_ptrOperations = new byte[m_nWidthBlocks * m_nHeightBlocks]; - m_ptrColumnBuf = new sbyte[m_nBlockSize * m_nChannelCount]; - m_ptrLineBuf = new sbyte[m_nChannelCount * (m_nWidthBlocks << m_info.BlockingDegree)]; - m_ptrDecodeBuf = new sbyte[m_nBlockSamples]; - m_ptrArrangeBuf = new sbyte[m_nBlockSamples]; + m_ptrOperations = new byte[m_nWidthBlocks * m_nHeightBlocks]; + m_ptrColumnBuf = new sbyte[m_nBlockSize * m_nChannelCount]; + m_ptrLineBuf = new sbyte[m_nChannelCount * (m_nWidthBlocks << m_info.BlockingDegree)]; + m_ptrDecodeBuf = new sbyte[m_nBlockSamples]; + m_ptrArrangeBuf = new sbyte[m_nBlockSamples]; - InitializeArrangeTable(); + InitializeArrangeTable(); + } if (0x00020200 == m_info.Version) { if (EriCode.RunlengthHuffman == m_info.Architecture) @@ -223,10 +230,8 @@ namespace GameRes.Formats.Entis private void CreateImageBuffer () { - uint stride = ((m_info.Width * (uint)m_info.BPP / 8u) + 0x03u) & ~0x03u; - uint image_bytes = stride * m_info.Height; - m_output = new byte[image_bytes]; - m_dwBytesPerLine = (int)stride; + m_dwBytesPerLine = (((int)m_info.Width * m_info.BPP / 8) + 3) & ~3; + m_output = new byte[m_dwBytesPerLine * (int)m_info.Height]; if (!m_info.VerticalFlip) { m_dst = ((int)m_info.Height - 1) * m_dwBytesPerLine; @@ -267,6 +272,11 @@ namespace GameRes.Formats.Entis if (nBitCount != 0) throw new InvalidFormatException(); break; + case 2: + if (nBitCount != 0 || fEncodeType != 0) + throw new InvalidFormatException(); + DecodeType2Image (context); + return; case 8: if (nBitCount != 8) throw new InvalidFormatException(); @@ -407,6 +417,30 @@ namespace GameRes.Formats.Entis } } + private void DecodeType2Image (RLEDecodeContext context) + { + if (m_info.BPP != 8) + throw new InvalidFormatException(); + + if (EriCode.ArithmeticCode == m_info.Architecture) + { +// (context as ArithmeticContext).InitArithmeticContext (8); + m_ptrLineBuf = new sbyte[m_info.Width*4]; + } + else + throw new NotImplementedException(); + + int dst = m_dst; + for (int nPosY = 0; nPosY < (int)m_info.Height; ++nPosY) + { + if (context.DecodeBytes (m_ptrLineBuf, m_info.Width) < m_info.Width) + throw new InvalidFormatException(); + for (int x = 0; x < (int)m_info.Width; ++x) + m_output[dst+x] = (byte)m_ptrLineBuf[4 * x]; + dst += m_dwBytesPerLine; + } + } + private void DecodeLossyImage (HuffmanDecodeContext context) { throw new NotImplementedException ("Lossy ERI compression not implemented"); @@ -489,13 +523,22 @@ namespace GameRes.Formats.Entis if ((int)EriImage.RGBA == fdwFormatType) { Format = PixelFormats.Bgra32; - return RestoreRGBA32; + if (null == m_src_frame) + return RestoreRGBA32; + else + return RestoreDeltaRGBA32; } Format = PixelFormats.Bgr32; - return RestoreRGB24; + if (null == m_src_frame) + return RestoreRGB24; + else + return RestoreDeltaRGB24; case 24: Format = PixelFormats.Bgr24; - return RestoreRGB24; + if (null == m_src_frame) + return RestoreRGB24; + else + return RestoreDeltaRGB24; case 16: Format = PixelFormats.Bgr555; return RestoreRGB16; @@ -559,6 +602,57 @@ namespace GameRes.Formats.Entis } } + void RestoreDeltaRGBA32 () + { + int ptrDstLine = m_ptrDstBlock; + int ptrSrcLine = 0; //m_ptrDecodeBuf; + int nBlockSamples = (int)m_nBlockArea; + int nBlockSamplesX3 = nBlockSamples * 3; + + for (uint y = 0; y < m_nDstHeight; y++) + { + int ptrDstNext = ptrDstLine; + int ptrSrcNext = ptrSrcLine; + + for (uint x = 0; x < m_nDstWidth; x++) + { + m_output[ptrDstNext] = (byte)(m_src_frame[ptrDstNext] + m_ptrDecodeBuf[ptrSrcNext]); + m_output[ptrDstNext+1] = (byte)(m_src_frame[ptrDstNext+1] + m_ptrDecodeBuf[ptrSrcNext + nBlockSamples]); + m_output[ptrDstNext+2] = (byte)(m_src_frame[ptrDstNext+2] + m_ptrDecodeBuf[ptrSrcNext + nBlockSamples * 2]); + m_output[ptrDstNext+3] = (byte)(m_src_frame[ptrDstNext+3] + m_ptrDecodeBuf[ptrSrcNext + nBlockSamplesX3]); + ptrSrcNext ++; + ptrDstNext += 4; + } + ptrSrcLine += (int)m_nBlockSize; + ptrDstLine += m_nDstLineBytes; + } + } + + void RestoreDeltaRGB24() + { + int ptrDstLine = m_ptrDstBlock; + int ptrSrcLine = 0; //m_ptrDecodeBuf; + int nBytesPerPixel = m_nDstPixelBytes; + int nBlockSamples = (int)m_nBlockArea; + + for (uint y = 0; y < m_nDstHeight; y++) + { + int ptrDstNext = ptrDstLine; + int ptrSrcNext = ptrSrcLine; + + for (uint x = 0; x < m_nDstWidth; x++) + { + m_output[ptrDstNext] = (byte)(m_src_frame[ptrDstNext] + m_ptrDecodeBuf[ptrSrcNext]); + m_output[ptrDstNext+1] = (byte)(m_src_frame[ptrDstNext+1] + m_ptrDecodeBuf[ptrSrcNext + nBlockSamples]); + m_output[ptrDstNext+2] = (byte)(m_src_frame[ptrDstNext+2] + m_ptrDecodeBuf[ptrSrcNext + nBlockSamples * 2]); + ptrSrcNext ++; + ptrDstNext += nBytesPerPixel; + } + ptrSrcLine += (int)m_nBlockSize; + ptrDstLine += m_nDstLineBytes; + } + } + void RestoreRGB16() { int ptrDstLine = m_ptrDstBlock; diff --git a/ArcFormats/Entis/ImageERI.cs b/ArcFormats/Entis/ImageERI.cs index e0dc0122..831360f9 100644 --- a/ArcFormats/Entis/ImageERI.cs +++ b/ArcFormats/Entis/ImageERI.cs @@ -237,7 +237,7 @@ namespace GameRes.Formats.Entis return ImageData.Create (info, reader.Format, reader.Palette, reader.Data, reader.Stride); } - internal Color[] ReadPalette (Stream input, int palette_length) + internal static Color[] ReadPalette (Stream input, int palette_length) { var palette_data = new byte[0x400]; if (palette_length > palette_data.Length)