(Legacy): CPA and AAP archives, CP2 images.

This commit is contained in:
morkt 2018-09-27 18:29:04 +04:00
parent 1878ba1afd
commit 841bbf274a
4 changed files with 352 additions and 1 deletions

79
Legacy/Aquarium/ArcAAP.cs Normal file
View File

@ -0,0 +1,79 @@
//! \file ArcAAP.cs
//! \date 2018 Sep 27
//! \brief Aquarium resource archive.
//
// Copyright (C) 2018 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.Aquarium
{
[Export(typeof(ArchiveFormat))]
public class AapOpener : ArchiveFormat
{
public override string Tag { get { return "AAP"; } }
public override string Description { get { return "Aquarium resource archive"; } }
public override uint Signature { get { return 0x52415046; } } // 'FPARC10'
public override bool IsHierarchic { get { return false; } }
public override bool CanWrite { get { return false; } }
public override ArcFile TryOpen (ArcView file)
{
if (!file.View.AsciiEqual (0, "FPARC10\0"))
return null;
uint index_offset = file.View.ReadUInt32 (0x10);
int count = file.View.ReadInt32 (0x14);
if (index_offset >= file.MaxOffset || !IsSaneCount (count))
return null;
uint data_offset = index_offset + (uint)count * 0x30;
var dir = new List<Entry> (count);
for (int i = 0; i < count; ++i)
{
var name = file.View.ReadString (index_offset, 0x10);
var entry = Create<PackedEntry> (name);
entry.Offset = file.View.ReadUInt32 (index_offset+0x10) + data_offset;
entry.UnpackedSize = file.View.ReadUInt32 (index_offset+0x14);
entry.Size = file.View.ReadUInt32 (index_offset+0x18);
if (!entry.CheckPlacement (file.MaxOffset))
return null;
entry.IsPacked = entry.UnpackedSize != 0;
dir.Add (entry);
index_offset += 0x30;
}
return new ArcFile (file, this, dir);
}
public override Stream OpenEntry (ArcFile arc, Entry entry)
{
var pent = entry as PackedEntry;
if (null == pent || !pent.IsPacked)
return base.OpenEntry (arc, entry);
var data = new byte[pent.UnpackedSize];
using (var input = arc.File.CreateStream (entry.Offset, entry.Size))
Cp2Reader.DecompressLz (input, data);
return new BinMemoryStream (data, entry.Name);
}
}
}

71
Legacy/Aquarium/ArcCPA.cs Normal file
View File

@ -0,0 +1,71 @@
//! \file ArcCPA.cs
//! \date 2018 Sep 27
//! \brief Aquarium resource archive.
//
// Copyright (C) 2018 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;
// [000324][Fuuro] Over the Rainbow
namespace GameRes.Formats.Aquarium
{
[Export(typeof(ArchiveFormat))]
public class PakOpener : ArchiveFormat
{
public override string Tag { get { return "CPA"; } }
public override string Description { get { return "Aquarium resource archive"; } }
public override uint Signature { get { return 0x00415043; } } // 'CPA'
public override bool IsHierarchic { get { return false; } }
public override bool CanWrite { get { return false; } }
public override ArcFile TryOpen (ArcView file)
{
int count = file.View.ReadInt32 (12);
if (!IsSaneCount (count))
return null;
uint data_offset = file.View.ReadUInt32 (8);
if (data_offset >= file.MaxOffset)
return null;
uint index_offset = 0x20;
var dir = new List<Entry> (count);
for (int i = 0; i < count; ++i)
{
if (file.View.ReadByte (index_offset) != 0)
{
var name = file.View.ReadString (index_offset, 0x10);
var entry = Create<Entry> (name);
entry.Offset = file.View.ReadUInt32 (index_offset+0x10) + data_offset;
entry.Size = file.View.ReadUInt32 (index_offset+0x14);
if (!entry.CheckPlacement (file.MaxOffset))
return null;
dir.Add (entry);
}
index_offset += 0x20;
}
return new ArcFile (file, this, dir);
}
}
}

197
Legacy/Aquarium/ImageCP2.cs Normal file
View File

@ -0,0 +1,197 @@
//! \file ImageCP2.cs
//! \date 2018 Sep 27
//! \brief Aquarium image format.
//
// Copyright (C) 2018 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.Utility;
namespace GameRes.Formats.Aquarium
{
internal class Cp2MetaData : ImageMetaData
{
public int Flags;
public bool IsCompressed { get { return (Flags & 0x0F) != 0; } }
public bool HasAlpha { get { return (Flags & 0x20) != 0; } }
}
[Export(typeof(ImageFormat))]
public class Cp2Format : ImageFormat
{
public override string Tag { get { return "CP2"; } }
public override string Description { get { return "Aquarium image format"; } }
public override uint Signature { get { return 0x325043; } } // 'CP2'
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
var header = file.ReadHeader (0x20);
return new Cp2MetaData {
Width = header.ToUInt32 (4),
Height = header.ToUInt32 (8),
BPP = header.ToInt32 (0x0C),
Flags = header.ToInt32 (0x14),
};
}
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var reader = new Cp2Reader (file, (Cp2MetaData)info);
return reader.Unpack();
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("Cp2Format.Write not implemented");
}
}
internal class Cp2Reader
{
IBinaryStream m_input;
Cp2MetaData m_info;
byte[] m_output;
public BitmapPalette Palette { get; private set; }
public PixelFormat Format { get; private set; }
public int Stride { get; private set; }
public Cp2Reader (IBinaryStream input, Cp2MetaData info)
{
m_input = input;
m_info = info;
switch (info.BPP)
{
case 8: Format = PixelFormats.Indexed8; break;
case 24: Format = PixelFormats.Bgr24; break;
case 32: Format = PixelFormats.Bgr32; break;
default: throw new InvalidFormatException();
}
Stride = (((int)info.Width + 31) & ~31) * (info.BPP / 8);
m_output = new byte[Stride * (int)info.Height];
}
public ImageData Unpack ()
{
m_input.Position = 0x20;
if (8 == m_info.BPP)
Palette = ImageFormat.ReadPalette (m_input.AsStream);
if (m_info.IsCompressed)
DecompressLz (m_input, m_output);
else
m_input.Read (m_output, 0, m_output.Length);
if (m_info.HasAlpha)
{
int stride = Stride / (m_info.BPP / 8);
var alpha = new byte[stride * (int)m_info.Height];
if (m_info.IsCompressed)
DecompressLz (m_input, alpha);
else
m_input.Read (alpha, 0, alpha.Length);
return ApplyAlpha (alpha, stride);
}
return ImageData.CreateFlipped (m_info, Format, Palette, m_output, Stride);
}
ImageData ApplyAlpha (byte[] alpha, int alpha_stride)
{
int width = (int)m_info.Width;
int dst_stride = width * 4;
var pixels = new byte[dst_stride * (int)m_info.Height];
int src = m_output.Length - Stride;
int dst = 0;
int asrc = 0;
while (src >= 0)
{
int i = 0;
for (int x = 0; x < width; ++x)
{
if (8 == m_info.BPP)
{
var color = Palette.Colors[m_output[src+i++]];
pixels[dst++] = color.B;
pixels[dst++] = color.G;
pixels[dst++] = color.R;
}
else if (16 == m_info.BPP)
{
var color = m_output.ToUInt16 (src+i);
pixels[dst++] = (byte)((color & 0x1F) * 0xFF / 0x1F);
pixels[dst++] = (byte)(((color >> 5) & 0x1F) * 0xFF / 0x1F);
pixels[dst++] = (byte)(((color >> 10) & 0x1F) * 0xFF / 0x1F);
i += 2;
}
else
{
pixels[dst++] = m_output[src+i++];
pixels[dst++] = m_output[src+i++];
pixels[dst++] = m_output[src+i++];
}
pixels[dst++] = alpha[asrc+x];
}
src -= Stride;
asrc += alpha_stride;
}
return ImageData.Create (m_info, PixelFormats.Bgra32, null, pixels, dst_stride);
}
internal static void DecompressLz (IBinaryStream input, byte[] output)
{
int remaining = input.ReadInt32();
input.ReadInt32();
int dst = 0;
while (dst < output.Length && remaining > 0)
{
int b = input.ReadByte();
if (-1 == b)
break;
if (b != 0)
{
output[dst++] = (byte)b;
--remaining;
}
else
{
int count = input.ReadByte();
if (count != 0)
{
int offset = input.ReadUInt16();
Binary.CopyOverlapped (output, dst - offset, dst, count);
dst += count;
remaining -= 4;
}
else
{
output[dst++] = 0;
remaining -= 2;
}
}
}
}
}
}

View File

@ -65,11 +65,15 @@
<Compile Include="Aaru\ArcFL4.cs" />
<Compile Include="Aaru\ImageBM2.cs" />
<Compile Include="Acme\ImageARD.cs" />
<Compile Include="Acme\ImagePMG.cs" />
<Compile Include="Ags32i\AudioAGS.cs" />
<Compile Include="Ags32i\ImageGSS.cs" />
<Compile Include="Airyu\ArcCHR.cs" />
<Compile Include="Akatombo\ArcX.cs" />
<Compile Include="Akatombo\ImageFB.cs" />
<Compile Include="Aquarium\ArcAAP.cs" />
<Compile Include="Aquarium\ArcCPA.cs" />
<Compile Include="Aquarium\ImageCP2.cs" />
<Compile Include="Ark\ImageCMP.cs" />
<Compile Include="Kasane\ArcAR2.cs" />
<Compile Include="KeroQ\ArcDAT.cs" />
@ -121,7 +125,7 @@
<Compile Include="Rune\ArcYK.cs" />
<Compile Include="Sarang\ImageABC.cs" />
<Compile Include="Sogna\ArcSGS.cs" />
<Compile Include="Speed\ImageDAT.cs" />
<None Include="Speed\ImageDAT.cs" />
<Compile Include="Tako\ArcMPK.cs" />
<Compile Include="Tetratech\ArcBND.cs" />
<Compile Include="Tigerman\ArcCHR.cs" />