Reduce diff for easier review

This commit is contained in:
ななみ
2026-01-07 01:54:23 +08:00
committed by GitHub
parent 2abbe6f3b7
commit 8679559e2b

View File

@@ -34,20 +34,20 @@ namespace GameRes.Formats.DigitalWorks
{ {
internal class Tim2MetaData : ImageMetaData internal class Tim2MetaData : ImageMetaData
{ {
public int PaletteSize; public int PaletteSize;
public int HeaderSize; public int HeaderSize;
public int Colors; public int Colors;
public byte Alpha; public byte Alpha;
} }
[Export(typeof(ImageFormat))] [Export(typeof(ImageFormat))]
public class Tim2Format : ImageFormat public class Tim2Format : ImageFormat
{ {
public override string Tag { get { return "TIM2"; } } public override string Tag { get { return "TIM2"; } }
public override string Description { get { return "PlayStation/2 image format"; } } public override string Description { get { return "PlayStation/2 image format"; } }
public override uint Signature { get { return 0x324D4954; } } // 'TIM2' public override uint Signature { get { return 0x324D4954; } } // 'TIM2'
public Tim2Format() public Tim2Format ()
{ {
Extensions = new string[] { "tm2", "ext" }; Extensions = new string[] { "tm2", "ext" };
Settings = new[] { AlphaFormat }; Settings = new[] { AlphaFormat };
@@ -60,16 +60,16 @@ namespace GameRes.Formats.DigitalWorks
ValuesSet = new[] { "No Alpha", "RGBX", "RGBA" }, ValuesSet = new[] { "No Alpha", "RGBX", "RGBA" },
}; };
public override ImageMetaData ReadMetaData(IBinaryStream file) public override ImageMetaData ReadMetaData (IBinaryStream file)
{ {
var header = file.ReadHeader(0x40); var header = file.ReadHeader (0x40);
byte bpp = header[0x23]; byte bpp = header[0x23];
switch (bpp) switch (bpp)
{ {
case 1: bpp = 16; break; case 1: bpp = 16; break;
case 2: bpp = 24; break; case 2: bpp = 24; break;
case 3: bpp = 32; break; case 3: bpp = 32; break;
case 4: bpp = 4; break; // 16 color case 4: bpp = 4; break; //16color
case 5: bpp = 8; break; case 5: bpp = 8; break;
default: return null; default: return null;
} }
@@ -81,62 +81,60 @@ namespace GameRes.Formats.DigitalWorks
case "RGBA": case "RGBA":
default: alpha = 8; break; default: alpha = 8; break;
} }
return new Tim2MetaData return new Tim2MetaData {
{ Width = header.ToUInt16 (0x24),
Width = header.ToUInt16(0x24), Height = header.ToUInt16 (0x26),
Height = header.ToUInt16(0x26), BPP = bpp,
BPP = bpp, PaletteSize = header.ToInt32 (0x14),
PaletteSize = header.ToInt32(0x14), HeaderSize = header.ToUInt16 (0x1C),
HeaderSize = header.ToUInt16(0x1C), Colors = header.ToUInt16 (0x1E),
Colors = header.ToUInt16(0x1E), Alpha = alpha, //header.ToUInt16(0x30) == 0?// not so sure, there will be omissions
Alpha = alpha,
}; };
} }
public override ImageData Read(IBinaryStream file, ImageMetaData info) public override ImageData Read (IBinaryStream file, ImageMetaData info)
{ {
var reader = new Tim2Reader(file, (Tim2MetaData)info); var reader = new Tim2Reader (file, (Tim2MetaData)info);
var pixels = reader.Unpack(); var pixels = reader.Unpack();
return ImageData.Create(info, reader.Format, reader.Palette, pixels); return ImageData.Create (info, reader.Format, reader.Palette, pixels);
} }
public override void Write(Stream file, ImageData image) public override void Write (Stream file, ImageData image)
{ {
throw new System.NotImplementedException("Tim2Format.Write not implemented"); throw new System.NotImplementedException ("Tim2Format.Write not implemented");
} }
} }
internal class Tim2Reader internal class Tim2Reader
{ {
IBinaryStream m_input; IBinaryStream m_input;
Tim2MetaData m_info; Tim2MetaData m_info;
public PixelFormat Format { get; private set; } public PixelFormat Format { get; private set; }
public BitmapPalette Palette { get; private set; } public BitmapPalette Palette { get; private set; }
public Tim2Reader(IBinaryStream input, Tim2MetaData info) public Tim2Reader (IBinaryStream input, Tim2MetaData info)
{ {
m_input = input; m_input = input;
m_info = info; m_info = info;
switch (info.BPP) switch (info.BPP)
{ {
case 4: Format = PixelFormats.Indexed4; break; case 4: Format = PixelFormats.Indexed4; break;
case 8: Format = PixelFormats.Indexed8; break; case 8: Format = PixelFormats.Indexed8; break;
case 16: Format = PixelFormats.Bgr555; break; case 16: Format = PixelFormats.Bgr555; break;
case 24: Format = PixelFormats.Bgr24; break; case 24: Format = PixelFormats.Bgr24; break;
case 32: Format = PixelFormats.Bgra32; break; case 32: Format = PixelFormats.Bgra32; break;
} }
} }
public byte[] Unpack() public byte[] Unpack ()
{ {
m_input.Position = 0x10 + m_info.HeaderSize; m_input.Position = 0x10 + m_info.HeaderSize;
double pixel_size = (double)m_info.BPP / 8; double pixel_size = (double)m_info.BPP / 8;
int image_size = (int)((int)m_info.Width * (int)m_info.Height * pixel_size); int image_size = (int)((int)m_info.Width * (int)m_info.Height * pixel_size);
var output = m_input.ReadBytes(image_size); var output = m_input.ReadBytes (image_size);
if (pixel_size <= 1 && m_info.Colors > 0) // Indexed images if (pixel_size <= 1 && m_info.Colors > 0) // Indexed images
Palette = ReadPalette(m_info.Colors, m_info.Alpha); Palette = ReadPalette (m_info.Colors, m_info.Alpha);
// Handle 24bpp and 32bpp (Direct Color) // Handle 24bpp and 32bpp (Direct Color)
if (pixel_size == 3 || (pixel_size == 4 && m_info.Alpha == 8)) if (pixel_size == 3 || (pixel_size == 4 && m_info.Alpha == 8))
@@ -145,8 +143,8 @@ namespace GameRes.Formats.DigitalWorks
{ {
// Swap R and B channels // Swap R and B channels
byte r = output[i]; byte r = output[i];
output[i] = output[i + 2]; output[i] = output[i+2];
output[i + 2] = r; output[i+2] = r;
// Fix Alpha for 32bpp (Double Alpha Scaling) // Fix Alpha for 32bpp (Double Alpha Scaling)
if (pixel_size == 4) if (pixel_size == 4)
@@ -187,9 +185,9 @@ namespace GameRes.Formats.DigitalWorks
return output; return output;
} }
BitmapPalette ReadPalette(int color_num, byte X_A = 8) BitmapPalette ReadPalette (int color_num, byte X_A = 8)
{ {
var source = ImageFormat.ReadColorMap(m_input.AsStream, var source = ImageFormat.ReadColorMap (m_input.AsStream,
color_num, X_A == 7 ? PaletteFormat.RgbA7 : X_A == 0 ? PaletteFormat.RgbX : PaletteFormat.RgbA); color_num, X_A == 7 ? PaletteFormat.RgbA7 : X_A == 0 ? PaletteFormat.RgbX : PaletteFormat.RgbA);
// Scaled Alpha for Indexed Palettes (KID/PS2 Fix) // Scaled Alpha for Indexed Palettes (KID/PS2 Fix)
@@ -208,8 +206,7 @@ namespace GameRes.Formats.DigitalWorks
var color_map = new Color[color_num]; var color_map = new Color[color_num];
if (color_num == 16) if (color_num == 16){
{
Array.Copy(source, 0, color_map, 0, 16); Array.Copy(source, 0, color_map, 0, 16);
return new BitmapPalette(color_map); return new BitmapPalette(color_map);
} }
@@ -225,10 +222,10 @@ namespace GameRes.Formats.DigitalWorks
for (int row = 0; row < rows; row++) for (int row = 0; row < rows; row++)
{ {
int src = (part * rows * blocks + row * rows + block) * colors; int src = (part * rows * blocks + row * rows + block) * colors;
Array.Copy(source, src, color_map, dst, colors); Array.Copy (source, src, color_map, dst, colors);
dst += colors; dst += colors;
} }
return new BitmapPalette(color_map); return new BitmapPalette (color_map);
} }
} }
} }