added various Strikes resource formats.

This commit is contained in:
morkt 2020-03-30 17:38:58 +04:00
parent 2c96d146b7
commit c5e13f6db1
5 changed files with 899 additions and 9 deletions

225
ArcFormats/Ads/ArcPAC.cs Normal file
View File

@ -0,0 +1,225 @@
//! \file ArcPAC.cs
//! \date 2020 Mar 30
//! \brief ads engine resource archive.
//
// Copyright (C) 2020 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 GameRes.Compression;
namespace GameRes.Formats.Ads
{
internal class AdsEntry : PackedEntry
{
public int CompressionMethod;
}
[Export(typeof(ArchiveFormat))]
public class PacOpener : ArchiveFormat
{
public override string Tag { get { return "PAC/ADS"; } }
public override string Description { get { return "ads engine resource archive"; } }
public override uint Signature { get { return 0; } }
public override bool IsHierarchic { get { return true; } }
public override bool CanWrite { get { return false; } }
public PacOpener ()
{
ContainedFormats = new[] { "TGA", "TXT" };
}
public override ArcFile TryOpen (ArcView file)
{
if (!file.Name.HasExtension (".pac"))
return null;
uint index_size = file.View.ReadUInt32 (0);
if (index_size < 0x110 || index_size >= file.MaxOffset)
return null;
using (var reader = new IndexReader (file, index_size))
{
var dir = reader.ReadIndex();
if (null == dir)
return null;
return new ArcFile (file, this, dir);
}
}
public override Stream OpenEntry (ArcFile arc, Entry entry)
{
var input = arc.File.CreateStream (entry.Offset, entry.Size);
var pent = entry as AdsEntry;
if (null == pent || pent.CompressionMethod != 1)
return input;
return new PackedStream<RleDecompressor> (input);
}
}
internal class IndexReader : IDisposable
{
ArcViewStream m_input;
List<Entry> m_dir;
readonly long m_max_offset;
public IndexReader (ArcView file, uint index_size)
{
m_input = file.CreateStream (0, index_size);
m_dir = new List<Entry>();
m_max_offset = file.MaxOffset;
}
bool ReadDir (uint dir_offset, string dir_name)
{
m_input.Position = dir_offset;
int dir_count = m_input.ReadInt32();
if (dir_count < 0 || dir_count > 0x200)
return false;
int file_count = m_input.ReadInt32();
if (file_count < 0 || file_count > 0x40000)
return false;
if (string.IsNullOrEmpty (dir_name))
{
var root_name = ReadCString();
if (string.IsNullOrWhiteSpace (root_name))
return false;
}
m_dir.Capacity = m_dir.Count + file_count;
for (int j = 0; j < file_count; ++j)
{
var name = ReadCString();
if (string.IsNullOrWhiteSpace (name))
return false;
if (dir_name.Length > 0)
name = dir_name + '/' + name;
var entry = new AdsEntry { Name = name };
entry.Type = FormatCatalog.Instance.GetTypeFromName (name);
entry.Size = m_input.ReadUInt32();
entry.Offset = m_input.ReadUInt32();
if (!entry.CheckPlacement (m_max_offset))
return false;
entry.CompressionMethod = m_input.ReadInt32();
entry.IsPacked = entry.CompressionMethod != 0;
m_dir.Add (entry);
}
for (int j = 0; j < dir_count; ++j)
{
uint offset = m_input.ReadUInt32();
if (offset >= m_input.Length)
return false;
var name = ReadCString();
if (string.IsNullOrWhiteSpace (name))
return false;
if (dir_name.Length > 0)
name = dir_name + '/' + name;
var current_pos = m_input.Position;
if (!ReadDir (offset, name))
return false;
m_input.Position = current_pos;
}
return true;
}
public List<Entry> ReadIndex ()
{
if (!ReadDir (4, ""))
return null;
return m_dir;
}
byte[] m_buffer = new byte[0x104];
internal string ReadCString ()
{
int length = m_input.Read (m_buffer, 0, 0x104);
int end = 0;
while (end < length && m_buffer[end] != 0)
{
m_buffer[end++] ^= 0xFF;
}
return Encodings.cp932.GetString (m_buffer, 0, end);
}
#region IDisposable Members
bool m_disposed = false;
public void Dispose ()
{
if (!m_disposed)
{
m_input.Dispose();
m_disposed = true;
}
}
#endregion
}
internal sealed class RleDecompressor : Decompressor
{
Stream m_input;
public override void Initialize (Stream input)
{
m_input = input;
}
protected override IEnumerator<int> Unpack ()
{
var buffer = new byte[8];
int chunk_size = 3;
for (;;)
{
int ctl = m_input.ReadByte();
if (-1 == ctl)
yield break;
if (ctl != 0)
{
if (m_input.Read (buffer, 4, 4) != 4)
yield break;
int count = buffer.ToInt32 (4) - 1;
while (count --> 0)
{
for (int i = 0; i < chunk_size; ++i)
{
m_buffer[m_pos++] = buffer[i];
if (0 == --m_length)
yield return m_pos;
}
}
}
else
{
chunk_size = m_input.Read (buffer, 0, 3);
if (0 == chunk_size)
yield break;
for (int i = 0; i < chunk_size; ++i)
{
m_buffer[m_pos++] = buffer[i];
if (0 == --m_length)
yield return m_pos;
}
}
}
}
}
}

87
ArcFormats/Ads/ArcPOG.cs Normal file
View File

@ -0,0 +1,87 @@
//! \file ArcPOG.cs
//! \date 2020 Mar 30
//! \brief 'ads' engine audio archive.
//
// Copyright (C) 2020 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;
namespace GameRes.Formats.Ads
{
[Export(typeof(ArchiveFormat))]
public class PogOpener : ArchiveFormat
{
public override string Tag { get { return "060/POG"; } }
public override string Description { get { return "ads engine audio archive"; } }
public override uint Signature { get { return 0x474F50; } } // 'POG'
public override bool IsHierarchic { get { return true; } }
public override bool CanWrite { get { return false; } }
public PogOpener ()
{
ContainedFormats = new[] { "OGG", "WAV" };
}
public override ArcFile TryOpen (ArcView file)
{
uint names_offset = file.View.ReadUInt32 (4);
int count = file.View.ReadInt32 (8);
if (!IsSaneCount (count))
return null;
var dir = new List<Entry> (count);
uint pos = 0x10;
uint next_offset = file.View.ReadUInt32 (pos);
for (int i = 0; i < count; ++i)
{
pos += 4;
uint offset = next_offset;
next_offset = file.View.ReadUInt32 (pos);
var entry = new Entry {
Offset = offset,
Size = next_offset - offset,
Type = "audio",
};
if (!entry.CheckPlacement (file.MaxOffset))
return null;
dir.Add (entry);
}
using (var names = file.CreateStream (names_offset))
{
names.ReadInt32();
while (names.PeekByte() != -1)
{
int length = names.ReadInt32();
int index = names.ReadInt32();
if (index < 0 || index >= count)
return null;
var name = names.ReadCString (length - 4);
dir[index].Name = name;
}
}
return new ArcFile (file, this, dir);
}
}
}

View File

@ -56,8 +56,8 @@
<Reference Include="NAudio"> <Reference Include="NAudio">
<HintPath>..\packages\NAudio.1.7.3\lib\net35\NAudio.dll</HintPath> <HintPath>..\packages\NAudio.1.7.3\lib\net35\NAudio.dll</HintPath>
</Reference> </Reference>
<Reference Include="NVorbis"> <Reference Include="NVorbis, Version=0.8.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NVorbis.0.8.5.0\lib\NVorbis.dll</HintPath> <HintPath>..\packages\NVorbis.0.8.6\lib\net35\NVorbis.dll</HintPath>
<Private>True</Private> <Private>True</Private>
</Reference> </Reference>
<Reference Include="PresentationCore" /> <Reference Include="PresentationCore" />
@ -67,14 +67,7 @@
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.IO.Compression" /> <Reference Include="System.IO.Compression" />
<Reference Include="System.IO.FileSystem">
<HintPath>..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" /> <Reference Include="System.Numerics" />
<Reference Include="System.Security.Cryptography.Primitives, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Xaml" /> <Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
@ -95,6 +88,7 @@
<Compile Include="Abogado\ImageKG.cs" /> <Compile Include="Abogado\ImageKG.cs" />
<Compile Include="Actgs\ArcCG.cs" /> <Compile Include="Actgs\ArcCG.cs" />
<Compile Include="Actgs\ArcDAT.cs" /> <Compile Include="Actgs\ArcDAT.cs" />
<Compile Include="Ads\ArcPAC.cs" />
<Compile Include="AdvDx\ArcPKD.cs" /> <Compile Include="AdvDx\ArcPKD.cs" />
<Compile Include="AdvScripter\ArcPAK.cs" /> <Compile Include="AdvScripter\ArcPAK.cs" />
<Compile Include="AdvSys\ArcAdvSys3.cs" /> <Compile Include="AdvSys\ArcAdvSys3.cs" />
@ -215,6 +209,7 @@
<Compile Include="KiriKiri\YuzCrypt.cs" /> <Compile Include="KiriKiri\YuzCrypt.cs" />
<Compile Include="Lambda\ArcLAX.cs" /> <Compile Include="Lambda\ArcLAX.cs" />
<Compile Include="Lambda\ImageCLS.cs" /> <Compile Include="Lambda\ImageCLS.cs" />
<Compile Include="Leaf\ArcAR2.cs" />
<Compile Include="Leaf\ArcLEAF.cs" /> <Compile Include="Leaf\ArcLEAF.cs" />
<Compile Include="Leaf\ArcPAK.cs" /> <Compile Include="Leaf\ArcPAK.cs" />
<Compile Include="Leaf\AudioP16.cs" /> <Compile Include="Leaf\AudioP16.cs" />
@ -327,6 +322,9 @@
<Compile Include="Software House Parsley\ImagePCG.cs" /> <Compile Include="Software House Parsley\ImagePCG.cs" />
<Compile Include="Speed\ArcARC.cs" /> <Compile Include="Speed\ArcARC.cs" />
<Compile Include="Stack\ArcGPK.cs" /> <Compile Include="Stack\ArcGPK.cs" />
<Compile Include="Strikes\ArcPCK.cs" />
<Compile Include="Ads\ArcPOG.cs" />
<Compile Include="Strikes\ImageLAG.cs" />
<Compile Include="StudioJikkenshitsu\ArcUMPK.cs" /> <Compile Include="StudioJikkenshitsu\ArcUMPK.cs" />
<Compile Include="StudioJikkenshitsu\ImageDAT.cs" /> <Compile Include="StudioJikkenshitsu\ImageDAT.cs" />
<Compile Include="StudioJikkenshitsu\ImageGRC.cs" /> <Compile Include="StudioJikkenshitsu\ImageGRC.cs" />

View File

@ -0,0 +1,275 @@
//! \file ArcPCK.cs
//! \date 2020 Mar 29
//! \brief Strikes resource archive.
//
// Copyright (C) 2020 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 GameRes.Compression;
using GameRes.Utility;
namespace GameRes.Formats.Strikes
{
internal class PckEntry : PackedEntry
{
public bool IsEncrypted { get; set; }
}
[Export(typeof(ArchiveFormat))]
public class PckOpener : ArchiveFormat
{
public override string Tag { get { return "PCK/AVG"; } }
public override string Description { get { return "Strikes resource archive"; } }
public override uint Signature { get { return 0; } }
public override bool IsHierarchic { get { return true; } }
public override bool CanWrite { get { return false; } }
public PckOpener ()
{
ContainedFormats = new[] { "LAG", "BMP", "OGG", "WAV", "TXT" };
}
public override ArcFile TryOpen (ArcView file)
{
if (!VFS.IsPathEqualsToFileName (file.Name, "AVGDatas.pck"))
return null;
int seed = Binary.BigEndian (file.View.ReadInt32 (file.MaxOffset - 104));
var header = file.View.ReadBytes (file.MaxOffset - 100, 100);
var rnd = new RandomGenerator();
rnd.Init (seed);
rnd.Decrypt (header, 0, header.Length);
uint checksum = (uint)(header[1] | header[2] << 8 | header[0] << 16 | header[3] << 24) ^ 0xDEFD32D3;
uint length = BigEndian.ToUInt32 (header, 24);
if (checksum != length)
return null;
uint idx_pos = BigEndian.ToUInt32 (header, 28);
uint idx_size = Binary.BigEndian (file.View.ReadUInt32 (idx_pos));
uint index_size;
var index = ReadChunk (file, 8, idx_pos + 4, out index_size);
if (index_size >= 0x80000000)
{
index_size &= 0x7FFFFFFF;
var unpacked = new byte[idx_size];
LzssUnpack (index, index.Length, unpacked);
index = unpacked;
}
using (var input = new BinMemoryStream (index))
{
var dir = new List<Entry>();
int dir_count = 0;
while (input.PeekByte() != -1)
{
input.ReadInt32();
input.ReadInt32();
input.ReadInt32();
int count = Binary.BigEndian (input.ReadInt32());
var dir_name = dir_count.ToString ("X4");
for (int i = 0; i < count; ++i)
{
var name = input.ReadCString (0x28);
name = Path.Combine (dir_name, name);
var entry = Create<PckEntry> (name);
entry.Offset = Binary.BigEndian (input.ReadUInt32());
entry.Size = Binary.BigEndian (input.ReadUInt32());
entry.IsEncrypted = input.ReadInt32() != 0;
entry.UnpackedSize = input.ReadUInt32();
entry.IsPacked = entry.Size != entry.UnpackedSize;
if (!entry.CheckPlacement (file.MaxOffset))
return null;
dir.Add (entry);
}
++dir_count;
}
return new ArcFile (file, this, dir);
}
}
public override Stream OpenEntry (ArcFile arc, Entry entry)
{
var pent = entry as PckEntry;
if (null == pent || !(pent.IsEncrypted || pent.IsPacked))
return base.OpenEntry (arc, entry);
arc.File.View.Reserve (pent.Offset, pent.Size);
int skip_size = 8;
do
{
uint test_size = Binary.BigEndian (arc.File.View.ReadUInt32 (pent.Offset + skip_size * 4));
if (test_size + 4 == pent.Size)
break;
}
while (--skip_size > 0);
byte[] data;
uint data_size = pent.Size;
if (0 == skip_size)
{
data = arc.File.View.ReadBytes (pent.Offset, pent.Size);
}
else
{
data = ReadChunk (arc.File, skip_size, pent.Offset, out data_size);
}
if (pent.IsEncrypted)
{
if (data_size >= 0x10)
DecryptData (data, 0x10, 0xC53A9A6C);
else
DecryptData (data, data.Length, 0x6C9A3AC5);
}
Stream input = new BinMemoryStream (data, pent.Name);
if (pent.IsPacked)
input = new LzssStream (input);
return input;
}
void DecryptData (byte[] data, int length, uint key)
{
for (int i = 0; i < length; ++i)
{
data[i] ^= (byte)(key >> ((i & 3) << 3));
}
}
byte[] ReadChunk (ArcView file, int skipSize, long offset, out uint chunkSize)
{
skipSize *= 4;
var header = file.View.ReadUInt32 (offset);
chunkSize = Binary.BigEndian (file.View.ReadUInt32 (offset + skipSize));
uint size = chunkSize & 0x7FFFFFFF;
var chunk = file.View.ReadBytes (offset + 4, size);
if (skipSize > 0)
{
System.Buffer.BlockCopy (chunk, 0, chunk, 4, skipSize - 4);
LittleEndian.Pack (header, chunk, 0);
}
return chunk;
}
static internal int LzssUnpack (byte[] input, int in_length, byte[] output)
{
var frame = new byte[0x1000];
int frame_pos = 0xFEE;
int src = 0;
int dst = 0;
while (src < in_length)
{
int ctl = input[src++];
for (int bit = 1; bit != 0x100; bit <<= 1)
{
if (0 != (ctl & bit))
{
if (src >= in_length)
return dst;
byte b = input[src++];
frame[frame_pos++ & 0xFFF] = b;
output[dst++] = b;
}
else
{
if (src + 2 > in_length)
return dst;
int lo = input[src++];
int hi = input[src++];
int offset = (hi & 0xF0) << 4 | lo;
int count = Math.Min (3 + (hi & 0xF), output.Length - dst);
while (count --> 0)
{
byte b = frame[offset++ & 0xFFF];
frame[frame_pos++ & 0xFFF] = b;
output[dst++] = b;
}
}
}
}
return dst;
}
}
internal class RandomGenerator
{
int m_count;
int[] m_state = new int[56];
public void Init (int seed)
{
int n = 1;
m_state[55] = seed;
for (int i = 1; i <= 54; ++i)
{
int pos = 21 * i % 55;
m_state[pos] = n;
n = seed - n;
if (n < 0)
n += 1000000000;
seed = m_state[pos];
}
Shuffle();
Shuffle();
Shuffle();
m_count = 55;
}
public uint Rand ()
{
if (++m_count > 55)
{
Shuffle();
m_count = 1;
}
return (uint)m_state[m_count];
}
private void Shuffle ()
{
for (int i = 1; i <= 24; ++i)
{
m_state[i] = m_state[i] - m_state[i+31];
if (m_state[i] < 0)
m_state[i] += 1000000000;
}
for (int i = 25; i <= 55; ++i)
{
m_state[i] = m_state[i] - m_state[i-24];
if (m_state[i] < 0)
m_state[i] += 1000000000;
}
}
public void Decrypt (byte[] input, int src, int length)
{
for (int i = src; i < length; i += 4)
{
uint key = Rand();
input[i ] ^= (byte)(key >> 24);
input[i+1] ^= (byte)(key >> 16);
input[i+2] ^= (byte)(key >> 8);
input[i+3] ^= (byte)(key);
}
}
}
}

View File

@ -0,0 +1,305 @@
//! \file ImageLAG.cs
//! \date 2020 Mar 29
//! \brief Strikes image format.
//
// Copyright (C) 2020 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.ComponentModel.Composition;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using GameRes.Compression;
using GameRes.Utility;
namespace GameRes.Formats.Strikes
{
internal class LagMetaData : ImageMetaData
{
public int ScanLineSize;
public bool HasPalette;
public bool HasAlpha;
public int LastChunkSize;
public int ChunkCount;
}
[Export(typeof(ImageFormat))]
public class LagFormat : ImageFormat
{
public override string Tag { get { return "LAG"; } }
public override string Description { get { return "Strikes image format"; } }
public override uint Signature { get { return 0x414C1001; } }
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
var header = file.ReadHeader (0x20);
int bpp = header[10] & 0x1F;
if (!(bpp == 24 || bpp == 16 || bpp == 8))
return null;
return new LagMetaData
{
Width = BigEndian.ToUInt16 (header, 4),
Height = BigEndian.ToUInt16 (header, 6),
BPP = bpp,
ScanLineSize = BigEndian.ToUInt16 (header, 8),
HasPalette = (header[10] & 0x80) != 0,
HasAlpha = (header[10] & 0x20) != 0,
LastChunkSize = BigEndian.ToInt32 (header, 0x14),
ChunkCount = BigEndian.ToUInt16 (header, 0x18),
};
}
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var reader = new LagReader (file, (LagMetaData)info);
var pixels = reader.Unpack();
return ImageData.Create (info, reader.Format, reader.Palette, pixels);
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("LagFormat.Write not implemented");
}
}
internal class LagReader
{
IBinaryStream m_input;
byte[] m_output;
LagMetaData m_info;
public PixelFormat Format { get; private set; }
public BitmapPalette Palette { get; private set; }
public LagReader (IBinaryStream input, LagMetaData info)
{
m_input = input;
m_info = info;
m_output = new byte[4 * m_info.Width * m_info.Height];
switch (m_info.BPP)
{
case 24:
if (m_info.HasAlpha)
Format = PixelFormats.Bgra32;
else
Format = PixelFormats.Bgr24;
break;
default:
throw new NotImplementedException ("Not supported LAG color depth.");
}
}
public byte[] Unpack ()
{
m_input.Position = 0x20;
if (m_info.HasPalette)
Palette = ImageFormat.ReadPalette (m_input.AsStream, 0x100, PaletteFormat.Bgr);
var buffer = ReadChunks();
using (var input = new BinMemoryStream (buffer))
{
int g_pos = m_info.iWidth;
int b_pos = m_info.iWidth * 2;
int a_pos = m_info.iWidth * 3;
var scanline = new byte[m_info.ScanLineSize * 2];
var unpack_buffer = new byte[scanline.Length];
int dst = 0;
for (int y = 0; y < m_info.iHeight; ++y)
{
int packed_size = Binary.BigEndian (input.ReadInt24() << 8);
byte flags = input.ReadUInt8();
if (packed_size > scanline.Length)
break;
input.Read (scanline, 0, packed_size);
if ((flags & 4) != 0) // LZSS compression
{
packed_size = PckOpener.LzssUnpack (scanline, packed_size, unpack_buffer);
var swap = scanline;
scanline = unpack_buffer;
unpack_buffer = swap;
}
if ((flags & 2) != 0) // RLE compression
{
packed_size = RleUnpack (scanline, packed_size, unpack_buffer);
var swap = scanline;
scanline = unpack_buffer;
unpack_buffer = swap;
}
if ((flags & 1) != 0)
{
RestoreScanline (scanline, packed_size);
}
switch (m_info.BPP)
{
case 24:
{
int src_r = 0;
int src_g = g_pos;
int src_b = b_pos;
int src_a = a_pos;
for (int x = 0; x < m_info.iWidth; ++x)
{
m_output[dst++] = scanline[src_b++];
m_output[dst++] = scanline[src_g++];
m_output[dst++] = scanline[src_r++];
if (m_info.HasAlpha)
m_output[dst++] = scanline[src_a++];
}
break;
}
default:
throw new NotImplementedException ("Not supported LAG color depth.");
}
}
}
return m_output;
}
byte[] ReadChunks ()
{
var buffer = new byte[0x10000 * m_info.ChunkCount + m_info.LastChunkSize];
int dst = 0;
for (int i = 0; i <= m_info.ChunkCount; ++i)
{
int length = Binary.BigEndian (m_input.ReadInt32());
bool is_compressed = length < 0;
length &= 0x7FFFFFFF;
if (is_compressed)
{
int unpacked_size = Math.Min (0x10000, buffer.Length - dst);
using (var region = new StreamRegion (m_input.AsStream, m_input.Position, length, true))
using (var zinput = new ZLibStream (region, CompressionMode.Decompress, true))
{
dst += zinput.Read (buffer, dst, unpacked_size);
}
}
else
{
dst += m_input.Read (buffer, dst, length);
}
}
return buffer;
}
int RleUnpack (byte[] input, int in_length, byte[] output)
{
int dst = 0;
int src = 0;
while (src < in_length && dst < output.Length)
{
byte code = input[src++];
if (src >= in_length)
break;
int count = (code & 0x3F) + 1;
if (dst + count > output.Length)
break;
int val;
int i;
switch ((code & 0xC0) >> 6)
{
case 0:
for (i = 0; i < count; ++i)
{
int n = i & 3;
if (n == 0)
val = (input[src] & 0xC0) >> 6;
else if (n == 1)
val = (input[src] & 0x30) >> 4;
else if (n == 2)
val = (input[src] & 0xC) >> 2;
else
val = input[src++] & 3;
if ((val & 2) != 0)
val |= 0xFC;
output[dst++] = (byte)val;
}
if ((i & 3) != 0)
++src;
break;
case 1:
for (i = 0; i < count; ++i)
{
if ((i & 1) != 0)
val = input[src++] & 0xF;
else
val = (input[src] & 0xF0) >> 4;
if ((val & 8) != 0)
val |= 0xF0;
output[dst++] = (byte)val;
}
if ((i & 1) != 0)
++src;
break;
case 2:
for (i = 0; i < count; ++i)
{
int n = i & 3;
if (n == 0)
{
val = (input[src] & 0xFC) >> 2;
}
else if (n == 1)
{
byte v = input[src++];
val = ((input[src] & 0xF0) >> 4) | (v & 3) << 4;
}
else if (n == 2)
{
byte v = input[src++];
val = ((input[src] & 0xC0) >> 6) | (v & 0xF) << 2;
}
else
{
val = input[src++] & 0x3F;
}
if ((val & 0x20) != 0)
val |= 0xC0;
output[dst++] = (byte)val;
}
if ((i & 3) != 0)
++src;
break;
case 3:
Buffer.BlockCopy (input, src, output, dst, count);
src += count;
dst += count;
break;
}
}
return dst;
}
void RestoreScanline (byte[] input, int in_length)
{
for (int pos = 1; pos < in_length; ++pos)
{
input[pos] += input[pos-1];
}
}
}
}