mirror of
https://github.com/crskycode/GARbro.git
synced 2026-06-14 09:44:20 +08:00
Merge pull request #14 from gopicolo/master
Add KID P2T archive support and fix global TIM2 transparency scaling
This commit is contained in:
@@ -133,19 +133,32 @@ namespace GameRes.Formats.DigitalWorks
|
||||
double pixel_size = (double)m_info.BPP / 8;
|
||||
int image_size = (int)((int)m_info.Width * (int)m_info.Height * pixel_size);
|
||||
var output = m_input.ReadBytes (image_size);
|
||||
if (pixel_size <= 8 && m_info.Colors > 0)
|
||||
if (pixel_size <= 1 && m_info.Colors > 0) // Indexed images
|
||||
Palette = ReadPalette (m_info.Colors, m_info.Alpha);
|
||||
|
||||
if (pixel_size == 3 || pixel_size == 4 && m_info.Alpha == 8)
|
||||
// Handle 24bpp and 32bpp (Direct Color)
|
||||
if (pixel_size == 3 || (pixel_size == 4 && m_info.Alpha == 8))
|
||||
{
|
||||
for (int i = 0; i < image_size; i += (int)pixel_size)
|
||||
{
|
||||
// Swap R and B channels
|
||||
byte r = output[i];
|
||||
output[i] = output[i+2];
|
||||
output[i+2] = r;
|
||||
|
||||
// Fix Alpha for 32bpp (Double Alpha Scaling)
|
||||
if (pixel_size == 4)
|
||||
{
|
||||
byte a = output[i + 3];
|
||||
if (a > 0)
|
||||
{
|
||||
output[i + 3] = (byte)Math.Min(255, a * 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (pixel_size == 4 && m_info.Alpha == 7)
|
||||
// Handle specific RGBX (Alpha 7) case
|
||||
else if (pixel_size == 4 && m_info.Alpha == 7)
|
||||
{
|
||||
for (int i = 0; i < image_size; i += 4)
|
||||
{
|
||||
@@ -158,7 +171,8 @@ namespace GameRes.Formats.DigitalWorks
|
||||
output[i + 3] = (byte)(output[i + 3] << 1);
|
||||
}
|
||||
}
|
||||
if (pixel_size == 4 && m_info.Alpha == 0)
|
||||
// Handle No Alpha case
|
||||
else if (pixel_size == 4 && m_info.Alpha == 0)
|
||||
{
|
||||
for (int i = 0; i < image_size; i += 4)
|
||||
{
|
||||
@@ -175,6 +189,21 @@ namespace GameRes.Formats.DigitalWorks
|
||||
{
|
||||
var source = ImageFormat.ReadColorMap (m_input.AsStream,
|
||||
color_num, X_A == 7 ? PaletteFormat.RgbA7 : X_A == 0 ? PaletteFormat.RgbX : PaletteFormat.RgbA);
|
||||
|
||||
// Scaled Alpha for Indexed Palettes (KID/PS2 Fix)
|
||||
if (X_A != 0)
|
||||
{
|
||||
for (int i = 0; i < source.Length; i++)
|
||||
{
|
||||
byte a = source[i].A;
|
||||
if (a > 0)
|
||||
{
|
||||
byte newAlpha = (byte)Math.Min(255, a * 2);
|
||||
source[i] = Color.FromArgb(newAlpha, source[i].R, source[i].G, source[i].B);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var color_map = new Color[color_num];
|
||||
|
||||
if (color_num == 16){
|
||||
|
||||
158
ArcFormats/Kid/ArcP2T.cs
Normal file
158
ArcFormats/Kid/ArcP2T.cs
Normal file
@@ -0,0 +1,158 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.IO;
|
||||
using GameRes.Compression;
|
||||
|
||||
namespace GameRes.Formats.KID
|
||||
{
|
||||
// Custom entry class to store metadata required for P2T reconstruction
|
||||
public class P2tEntry : Entry
|
||||
{
|
||||
public byte[] TimHeaderChunk { get; set; }
|
||||
public int RealUncompressedSize { get; set; }
|
||||
public uint CompressedSize { get; set; }
|
||||
}
|
||||
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class P2tOpener : ArchiveFormat
|
||||
{
|
||||
public override string Tag { get { return "P2T"; } }
|
||||
public override string Description { get { return "KID P2T archive"; } }
|
||||
public override uint Signature { get { return 0; } }
|
||||
public override bool IsHierarchic { get { return false; } }
|
||||
public override bool CanWrite { get { return false; } }
|
||||
|
||||
// Standard TIM2 header magic prefix
|
||||
private static readonly byte[] Tim2Magic = new byte[] {
|
||||
0x54, 0x49, 0x4D, 0x32, 0x04, 0x00, 0x01, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
public override ArcFile TryOpen(ArcView file)
|
||||
{
|
||||
if (file.MaxOffset < 0x20) return null;
|
||||
|
||||
uint headerEnd = file.View.ReadUInt32(0x08);
|
||||
uint numFiles = file.View.ReadUInt32(0x0C);
|
||||
uint dataStartBase = file.View.ReadUInt32(0x10);
|
||||
|
||||
if (numFiles == 0 || numFiles > 0xFFFF || headerEnd >= file.MaxOffset) return null;
|
||||
|
||||
// Basic validation check at the start of the file table
|
||||
uint checkFF = file.View.ReadUInt32(headerEnd + 48);
|
||||
if (checkFF != 0xFFFFFFFF) return null;
|
||||
|
||||
var dir = new List<Entry>((int)numFiles);
|
||||
long currentEntryPtr = headerEnd;
|
||||
const int EntrySize = 64;
|
||||
|
||||
for (int i = 0; i < numFiles; i++)
|
||||
{
|
||||
// Mapping structure:
|
||||
// 48-51: FFFFFFFF padding
|
||||
// 52-55: Data Offset (Raw)
|
||||
// 56-59: Flag (usually 01)
|
||||
// 60-63: Compressed Length
|
||||
uint rawOffset = file.View.ReadUInt32(currentEntryPtr + 52);
|
||||
uint compressedLen = file.View.ReadUInt32(currentEntryPtr + 60);
|
||||
|
||||
long sizeInfoOffset = dataStartBase + rawOffset;
|
||||
if (sizeInfoOffset + 4 > file.MaxOffset) break;
|
||||
|
||||
uint realUncompressedSize = file.View.ReadUInt32(sizeInfoOffset);
|
||||
byte[] timHeaderChunk = file.View.ReadBytes(currentEntryPtr, 48);
|
||||
|
||||
var entry = new P2tEntry
|
||||
{
|
||||
Name = string.Format("{0:D4}.tm2", i),
|
||||
Type = "image",
|
||||
Offset = sizeInfoOffset + 4,
|
||||
Size = realUncompressedSize, // Display uncompressed size in list
|
||||
RealUncompressedSize = (int)realUncompressedSize,
|
||||
CompressedSize = compressedLen,
|
||||
TimHeaderChunk = timHeaderChunk
|
||||
};
|
||||
|
||||
dir.Add(entry);
|
||||
currentEntryPtr += EntrySize;
|
||||
}
|
||||
|
||||
return new ArcFile(file, this, dir);
|
||||
}
|
||||
|
||||
public override Stream OpenEntry(ArcFile arc, Entry entry)
|
||||
{
|
||||
var p2tEntry = entry as P2tEntry;
|
||||
if (p2tEntry == null) return base.OpenEntry(arc, entry);
|
||||
|
||||
// Read the compressed block using the stored length
|
||||
var compressedData = arc.File.View.ReadBytes(entry.Offset, p2tEntry.CompressedSize);
|
||||
|
||||
// LZSS decompression (Okumura variant)
|
||||
var decompressedBody = LzssDecompress(compressedData);
|
||||
|
||||
// Align data to expected uncompressed size
|
||||
if (decompressedBody.Length != p2tEntry.RealUncompressedSize)
|
||||
Array.Resize(ref decompressedBody, p2tEntry.RealUncompressedSize);
|
||||
|
||||
// Reconstruct final file: Magic + Table Header Chunk + Decompressed Data
|
||||
int finalSize = Tim2Magic.Length + p2tEntry.TimHeaderChunk.Length + decompressedBody.Length;
|
||||
var outputData = new byte[finalSize];
|
||||
|
||||
Buffer.BlockCopy(Tim2Magic, 0, outputData, 0, Tim2Magic.Length);
|
||||
Buffer.BlockCopy(p2tEntry.TimHeaderChunk, 0, outputData, Tim2Magic.Length, p2tEntry.TimHeaderChunk.Length);
|
||||
Buffer.BlockCopy(decompressedBody, 0, outputData, Tim2Magic.Length + p2tEntry.TimHeaderChunk.Length, decompressedBody.Length);
|
||||
|
||||
return new MemoryStream(outputData);
|
||||
}
|
||||
|
||||
private byte[] LzssDecompress(byte[] input)
|
||||
{
|
||||
var output = new List<byte>(input.Length * 2);
|
||||
const int n = 4096;
|
||||
const int threshold = 2;
|
||||
int r = n - 18;
|
||||
var textBuf = new byte[n + 18 - 1];
|
||||
Array.Clear(textBuf, 0, textBuf.Length);
|
||||
|
||||
int flags = 0;
|
||||
int srcPos = 0;
|
||||
|
||||
while (srcPos < input.Length)
|
||||
{
|
||||
flags >>= 1;
|
||||
if ((flags & 0x100) == 0)
|
||||
{
|
||||
if (srcPos >= input.Length) break;
|
||||
flags = input[srcPos++] | 0xFF00;
|
||||
}
|
||||
|
||||
if ((flags & 1) != 0)
|
||||
{
|
||||
if (srcPos >= input.Length) break;
|
||||
byte c = input[srcPos++];
|
||||
output.Add(c);
|
||||
textBuf[r] = c;
|
||||
r = (r + 1) & (n - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (srcPos + 1 >= input.Length) break;
|
||||
int i = input[srcPos++];
|
||||
int j = input[srcPos++];
|
||||
int offset = i | ((j & 0xF0) << 4);
|
||||
int len = (j & 0x0F) + threshold;
|
||||
for (int k = 0; k <= len; k++)
|
||||
{
|
||||
byte c = textBuf[(offset + k) & (n - 1)];
|
||||
output.Add(c);
|
||||
textBuf[r] = c;
|
||||
r = (r + 1) & (n - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return output.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
173
ArcFormats/Ps1/ImageTIM.cs
Normal file
173
ArcFormats/Ps1/ImageTIM.cs
Normal file
@@ -0,0 +1,173 @@
|
||||
//! \file ImageTIM.cs
|
||||
//! \date 2026 Jan 05
|
||||
//! \brief Standard PlayStation (PS1) image format.
|
||||
//
|
||||
// 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.Sony
|
||||
{
|
||||
internal class TimMetaData : ImageMetaData
|
||||
{
|
||||
public uint BppMode;
|
||||
public bool HasClut;
|
||||
public uint ClutOffset;
|
||||
public uint ImageOffset;
|
||||
}
|
||||
|
||||
[Export(typeof(ImageFormat))]
|
||||
public class TimFormat : ImageFormat
|
||||
{
|
||||
public override string Tag { get { return "TIM"; } }
|
||||
public override string Description { get { return "PlayStation image format"; } }
|
||||
public override uint Signature { get { return 0x00000010; } } // Magic: 10 00 00 00
|
||||
|
||||
public override ImageMetaData ReadMetaData(IBinaryStream stream)
|
||||
{
|
||||
var header = stream.ReadHeader(8);
|
||||
if (header[0] != 0x10 || header[1] != 0)
|
||||
return null;
|
||||
|
||||
ushort flag = header.ToUInt16(4);
|
||||
uint mode = (uint)(flag & 3);
|
||||
bool hasClut = (flag & 8) != 0;
|
||||
|
||||
uint currentOffset = 8;
|
||||
uint clutOffset = 0;
|
||||
|
||||
if (hasClut)
|
||||
{
|
||||
clutOffset = currentOffset;
|
||||
stream.Position = currentOffset;
|
||||
uint clutSize = stream.ReadUInt32();
|
||||
currentOffset += clutSize;
|
||||
}
|
||||
|
||||
uint imageOffset = currentOffset;
|
||||
if (imageOffset + 12 > stream.Length) return null;
|
||||
|
||||
// Image Header: BlockSize(4), VramX(2), VramY(2), WordWidth(2), Height(2)
|
||||
stream.Position = imageOffset + 8;
|
||||
ushort wordWidth = stream.ReadUInt16();
|
||||
ushort height = stream.ReadUInt16();
|
||||
|
||||
int bpp;
|
||||
int width;
|
||||
switch (mode)
|
||||
{
|
||||
case 0: bpp = 4; width = wordWidth * 4; break;
|
||||
case 1: bpp = 8; width = wordWidth * 2; break;
|
||||
case 2: bpp = 16; width = wordWidth; break;
|
||||
case 3: bpp = 24; width = (wordWidth * 2) / 3; break;
|
||||
default: return null;
|
||||
}
|
||||
|
||||
return new TimMetaData
|
||||
{
|
||||
Width = (uint)width,
|
||||
Height = height,
|
||||
BPP = bpp,
|
||||
BppMode = mode,
|
||||
HasClut = hasClut,
|
||||
ClutOffset = clutOffset,
|
||||
ImageOffset = imageOffset
|
||||
};
|
||||
}
|
||||
|
||||
public override ImageData Read(IBinaryStream stream, ImageMetaData info)
|
||||
{
|
||||
var meta = (TimMetaData)info;
|
||||
BitmapPalette palette = null;
|
||||
|
||||
if (meta.HasClut)
|
||||
{
|
||||
// CLUT Header: BlockSize(4), X(2), Y(2), Width(2), Height(2)
|
||||
// Width/Height are at offsets 8 and 10 within the CLUT block.
|
||||
stream.Position = meta.ClutOffset + 8;
|
||||
ushort colorsCount = stream.ReadUInt16();
|
||||
ushort rows = stream.ReadUInt16();
|
||||
int totalColors = colorsCount * rows;
|
||||
|
||||
// Color data starts at offset 12
|
||||
var colorData = stream.ReadBytes(totalColors * 2);
|
||||
var colors = new Color[totalColors];
|
||||
for (int i = 0; i < totalColors; i++)
|
||||
{
|
||||
ushort c = BitConverter.ToUInt16(colorData, i * 2);
|
||||
colors[i] = ConvertBgr1555(c);
|
||||
}
|
||||
palette = new BitmapPalette(colors);
|
||||
}
|
||||
|
||||
// Image data starts at ImageOffset + 12 (skipping the 12-byte header)
|
||||
stream.Position = meta.ImageOffset + 12;
|
||||
int pixelDataSize = (int)(meta.Width * meta.Height * meta.BPP / 8);
|
||||
if (meta.BPP == 4) pixelDataSize = (int)(meta.Width * meta.Height / 2);
|
||||
|
||||
var pixels = stream.ReadBytes(pixelDataSize);
|
||||
PixelFormat format;
|
||||
|
||||
switch (meta.BppMode)
|
||||
{
|
||||
case 0: format = PixelFormats.Indexed4; break;
|
||||
case 1: format = PixelFormats.Indexed8; break;
|
||||
case 2: format = PixelFormats.Bgr555; break;
|
||||
case 3: format = PixelFormats.Bgr24; break;
|
||||
default: throw new NotSupportedException("Unsupported TIM mode");
|
||||
}
|
||||
|
||||
// PS1 4bpp nibbles are stored in reverse order compared to Windows standard
|
||||
if (meta.BppMode == 0)
|
||||
{
|
||||
for (int i = 0; i < pixels.Length; i++)
|
||||
{
|
||||
byte b = pixels[i];
|
||||
pixels[i] = (byte)((b >> 4) | (b << 4));
|
||||
}
|
||||
}
|
||||
|
||||
return ImageData.Create(info, format, palette, pixels);
|
||||
}
|
||||
|
||||
public override void Write(Stream file, ImageData image)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private Color ConvertBgr1555(ushort c)
|
||||
{
|
||||
// PS1 BGR1555: Bit 15=STP, 14-10=B, 9-5=G, 4-0=R
|
||||
byte r = (byte)((c & 0x1F) << 3);
|
||||
byte g = (byte)(((c >> 5) & 0x1F) << 3);
|
||||
byte b = (byte)(((c >> 10) & 0x1F) << 3);
|
||||
|
||||
// Transparency logic: 0,0,0 is transparent unless STP bit is set.
|
||||
byte a = (c == 0) ? (byte)0 : (byte)255;
|
||||
if ((c & 0x8000) != 0) a = 255;
|
||||
|
||||
return Color.FromArgb(a, r, g, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user