mirror of
https://github.com/crskycode/GARbro.git
synced 2026-07-08 01:30:18 +08:00
bunch of stuff.
This commit is contained in:
133
Legacy/SplushWave/ArcDAT.cs
Normal file
133
Legacy/SplushWave/ArcDAT.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
//! \file ArcDAT.cs
|
||||
//! \date 2023 Aug 14
|
||||
//! \brief Splush Wave resource archive.
|
||||
//
|
||||
// Copyright (C) 2023 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.SplushWave
|
||||
{
|
||||
internal class FlkEntry : Entry
|
||||
{
|
||||
public byte Flags;
|
||||
}
|
||||
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class DatOpener : ArchiveFormat
|
||||
{
|
||||
public override string Tag { get { return "DAT/FLK"; } }
|
||||
public override string Description { get { return "Splush Wave resource archive"; } }
|
||||
public override uint Signature { get { return 0x4B4C46; } } // 'FLK'
|
||||
public override bool IsHierarchic { get { return false; } }
|
||||
public override bool CanWrite { get { return false; } }
|
||||
|
||||
public override ArcFile TryOpen (ArcView file)
|
||||
{
|
||||
uint arc_size = file.View.ReadUInt32 (0x14);
|
||||
if (arc_size != file.MaxOffset)
|
||||
return null;
|
||||
int count = file.View.ReadInt32 (0x18);
|
||||
if (!IsSaneCount (count))
|
||||
return null;
|
||||
|
||||
uint index_offset = 0x20;
|
||||
var base_name = Path.GetFileNameWithoutExtension (file.Name);
|
||||
var dir = new List<Entry> (count);
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
var entry = new FlkEntry {
|
||||
Name = string.Format ("{0}#{1:D4}", base_name, i),
|
||||
Offset = file.View.ReadUInt32 (index_offset),
|
||||
Size = file.View.ReadUInt32 (index_offset+4),
|
||||
Flags = file.View.ReadByte (index_offset+0xF),
|
||||
};
|
||||
if (!entry.CheckPlacement (file.MaxOffset))
|
||||
return null;
|
||||
dir.Add (entry);
|
||||
index_offset += 0x10;
|
||||
}
|
||||
foreach (var entry in dir)
|
||||
{
|
||||
var type = file.View.ReadUInt32 (entry.Offset);
|
||||
if (0x475753 == type || 0x475753 == (type >> 8))
|
||||
entry.Type = "image";
|
||||
}
|
||||
return new ArcFile (file, this, dir);
|
||||
}
|
||||
|
||||
public override Stream OpenEntry (ArcFile arc, Entry entry)
|
||||
{
|
||||
var fent = (FlkEntry)entry;
|
||||
var input = arc.File.CreateStream (entry.Offset, entry.Size);
|
||||
if ((fent.Flags & 1) != 0)
|
||||
{
|
||||
using (input)
|
||||
{
|
||||
return LzssUnpack (input);
|
||||
}
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
internal Stream LzssUnpack (IBinaryStream input)
|
||||
{
|
||||
var frame = new byte[0x400];
|
||||
var output = new byte[0x200000];
|
||||
int dst = 0;
|
||||
int frame_pos = 0x3BE;
|
||||
int ctl = 0;
|
||||
while (input.PeekByte() != -1)
|
||||
{
|
||||
ctl >>= 1;
|
||||
if (0 == (ctl & 0x100))
|
||||
{
|
||||
ctl = input.ReadByte() | 0xFF00;
|
||||
}
|
||||
if (0 == (ctl & 1))
|
||||
{
|
||||
int next = input.ReadByte();
|
||||
if (-1 == next)
|
||||
break;
|
||||
output[dst++] = frame[frame_pos++ & 0x3FF] = (byte)next;
|
||||
}
|
||||
else
|
||||
{
|
||||
int lo = input.ReadByte();
|
||||
int hi = input.ReadByte();
|
||||
if (lo == -1 || hi == -1)
|
||||
break;
|
||||
int offset = lo + ((hi & 0xC0) << 2);
|
||||
int count = (hi & 0x3F) + 3;
|
||||
while (count --> 0)
|
||||
{
|
||||
output[dst++] = frame[frame_pos++ & 0x3FF] = frame[offset++ & 0x3FF];
|
||||
}
|
||||
}
|
||||
}
|
||||
return new BinMemoryStream (output, 0, dst);
|
||||
}
|
||||
}
|
||||
}
|
||||
188
Legacy/SplushWave/ImageSWG.cs
Normal file
188
Legacy/SplushWave/ImageSWG.cs
Normal file
@@ -0,0 +1,188 @@
|
||||
//! \file ImageSWG.cs
|
||||
//! \date 2023 Aug 14
|
||||
//! \brief Splush Wave graphics format.
|
||||
//
|
||||
// Copyright (C) 2023 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;
|
||||
|
||||
namespace GameRes.Formats.SplushWave
|
||||
{
|
||||
internal class SwgMetaData : ImageMetaData
|
||||
{
|
||||
public uint PaletteOffset;
|
||||
public uint DataOffset;
|
||||
public byte Depth;
|
||||
public bool IsCompressed;
|
||||
}
|
||||
|
||||
[Export(typeof(ImageFormat))]
|
||||
public class SwgFormat : ImageFormat
|
||||
{
|
||||
public override string Tag { get { return "SWG"; } }
|
||||
public override string Description { get { return "Splush Wave Graphics format"; } }
|
||||
public override uint Signature { get { return 0x475753; } } // 'SWG'
|
||||
|
||||
public override ImageMetaData ReadMetaData (IBinaryStream file)
|
||||
{
|
||||
var header = file.ReadHeader (0x30);
|
||||
uint pal_offset = header.ToUInt32 (0x14);
|
||||
if (pal_offset != 0)
|
||||
pal_offset += 0x10;
|
||||
byte depth = header[0x28];
|
||||
return new SwgMetaData {
|
||||
Width = header.ToUInt16 (0x20),
|
||||
Height = header.ToUInt16 (0x22),
|
||||
BPP = pal_offset != 0 ? 8 : depth == 2 ? 32 : 24,
|
||||
DataOffset = header.ToUInt32 (0x10) + 0x10,
|
||||
PaletteOffset = pal_offset,
|
||||
Depth = depth,
|
||||
IsCompressed = header[0x2F] != 0,
|
||||
};
|
||||
}
|
||||
|
||||
public override ImageData Read (IBinaryStream file, ImageMetaData info)
|
||||
{
|
||||
var meta = (SwgMetaData)info;
|
||||
PixelFormat format = meta.BPP == 8 ? PixelFormats.Indexed8
|
||||
: meta.BPP == 32 ? PixelFormats.Bgr32 : PixelFormats.Bgr24;
|
||||
BitmapPalette palette = null;
|
||||
if (meta.BPP == 8)
|
||||
{
|
||||
file.Position = meta.PaletteOffset;
|
||||
palette = ReadPalette (file.AsStream);
|
||||
}
|
||||
int stride = meta.iWidth * meta.BPP / 8;
|
||||
file.Position = meta.DataOffset;
|
||||
// var pixels = new byte[stride * meta.iHeight];
|
||||
var pixels = new byte[4 * meta.iWidth * meta.iHeight];
|
||||
if (!meta.IsCompressed)
|
||||
{
|
||||
file.Read (pixels, 0, pixels.Length);
|
||||
return ImageData.CreateFlipped (meta, format, palette, pixels, stride);
|
||||
}
|
||||
var input = file.ReadBytes ((int)(file.Length - file.Position));
|
||||
if (!Decompress (input, pixels, meta.Depth + 2, meta.iWidth, meta.iHeight))
|
||||
throw new InvalidFormatException ("Invalid SWG file.");
|
||||
return ImageData.CreateFlipped (meta, format, palette, pixels, stride);
|
||||
}
|
||||
|
||||
bool Decompress (byte[] input, byte[] output, int channels, int width, int height)
|
||||
{
|
||||
int src = 0;
|
||||
if (input[0] != 0 || input[1] != 1)
|
||||
{
|
||||
int n = 0;
|
||||
for (int i = 0; i < channels; ++i)
|
||||
{
|
||||
if (0 == input[i])
|
||||
++n;
|
||||
}
|
||||
if (n != channels)
|
||||
return false;
|
||||
src = 4;
|
||||
}
|
||||
int compress_method = input[src+1] + (input[src] << 8);
|
||||
src += 2;
|
||||
if (0 == compress_method)
|
||||
{
|
||||
for (int i = 0; i < channels; ++i)
|
||||
{
|
||||
int pos = i;
|
||||
int count = height * width;
|
||||
while (count --> 0)
|
||||
{
|
||||
output[pos] = input[src++];
|
||||
pos += channels;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (compress_method != 1)
|
||||
return false;
|
||||
int dst = 0;
|
||||
int v33 = src;
|
||||
int v37 = height * channels;
|
||||
src += 2 * v37;
|
||||
for (int row = 0; row < v37; ++row)
|
||||
{
|
||||
int y = row % height;
|
||||
dst = channels * (width * (height - y - 1) + 1) - row / height - 1;
|
||||
if (dst > output.Length)
|
||||
return true;
|
||||
int v24 = 0;
|
||||
int v36 = input[v33+1] + (input[v33] << 8);
|
||||
v33 += 2;
|
||||
do
|
||||
{
|
||||
byte lo = input[src];
|
||||
byte hi = input[src+1];
|
||||
if (lo != 0)
|
||||
{
|
||||
if (lo < 0x81)
|
||||
{
|
||||
++src;
|
||||
int count = lo + 1;
|
||||
v24 += count + 1;
|
||||
while (count --> 0)
|
||||
{
|
||||
output[dst] = input[src++];
|
||||
dst += channels;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
src += 2;
|
||||
v24 += 2;
|
||||
int count = Math.Min (0x101 - lo, output.Length - dst);
|
||||
while (count --> 0)
|
||||
{
|
||||
output[dst] = hi;
|
||||
dst += channels;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
src += 2;
|
||||
v24 += 2;
|
||||
output[dst] = hi;
|
||||
dst += channels;
|
||||
}
|
||||
if (dst >= output.Length)
|
||||
return true;
|
||||
}
|
||||
while (v24 < v36);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void Write (Stream file, ImageData image)
|
||||
{
|
||||
throw new System.NotImplementedException ("SwgFormat.Write not implemented");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user