From dd7f1cc9ec70726dce8494a2b6ffb8fe7f2c6134 Mon Sep 17 00:00:00 2001 From: ManicSteiner Date: Mon, 20 Jan 2025 12:28:55 +0800 Subject: [PATCH] fix support of 4 BPP --- ArcFormats/DigitalWorks/ImageTM2.cs | 21 ++++++++++++++------- GameRes/Image.cs | 7 ++++--- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/ArcFormats/DigitalWorks/ImageTM2.cs b/ArcFormats/DigitalWorks/ImageTM2.cs index 0be8eab1..b5ce73f4 100644 --- a/ArcFormats/DigitalWorks/ImageTM2.cs +++ b/ArcFormats/DigitalWorks/ImageTM2.cs @@ -69,6 +69,7 @@ namespace GameRes.Formats.DigitalWorks case 1: bpp = 16; break; case 2: bpp = 24; break; case 3: bpp = 32; break; + case 4: bpp = 4; break; //16color case 5: bpp = 8; break; default: return null; } @@ -118,25 +119,26 @@ namespace GameRes.Formats.DigitalWorks m_info = info; switch (info.BPP) { - case 8: Format = PixelFormats.Indexed8; break; - case 16: Format = PixelFormats.Bgr555; break; - case 24: Format = PixelFormats.Bgr24; break; - case 32: Format = PixelFormats.Bgra32; break; + case 4: Format = PixelFormats.Indexed4; break; + case 8: Format = PixelFormats.Indexed8; break; + case 16: Format = PixelFormats.Bgr555; break; + case 24: Format = PixelFormats.Bgr24; break; + case 32: Format = PixelFormats.Bgra32; break; } } public byte[] Unpack () { m_input.Position = 0x10 + m_info.HeaderSize; - int pixel_size = m_info.BPP / 8; - int image_size = (int)m_info.Width * (int)m_info.Height * pixel_size; + 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) Palette = ReadPalette (m_info.Colors, m_info.Alpha); if (pixel_size == 3 || pixel_size == 4 && m_info.Alpha == 8) { - for (int i = 0; i < image_size; i += pixel_size) + for (int i = 0; i < image_size; i += (int)pixel_size) { byte r = output[i]; output[i] = output[i+2]; @@ -175,6 +177,11 @@ namespace GameRes.Formats.DigitalWorks color_num, X_A == 7 ? PaletteFormat.RgbA7 : X_A == 0 ? PaletteFormat.RgbX : PaletteFormat.RgbA); var color_map = new Color[color_num]; + if (color_num == 16){ + Array.Copy(source, 0, color_map, 0, 16); + return new BitmapPalette(color_map); + } + int parts = color_num / 32; const int blocks = 2; const int rows = 2; diff --git a/GameRes/Image.cs b/GameRes/Image.cs index f3680ea4..da5e4105 100644 --- a/GameRes/Image.cs +++ b/GameRes/Image.cs @@ -71,8 +71,8 @@ namespace GameRes BgrX = 6, RgbA = 9, BgrA = 10, - RgbA7 = 55, - BgrA7 = 66, + RgbA7 = 55, + BgrA7 = 66, } public class ImageData @@ -126,7 +126,8 @@ namespace GameRes public static ImageData Create (ImageMetaData info, PixelFormat format, BitmapPalette palette, Array pixel_data) { - return Create (info, format, palette, pixel_data, (int)info.Width*((format.BitsPerPixel+7)/8)); + return Create (info, format, palette, pixel_data, + format.BitsPerPixel == 4 ? (int)info.Width*format.BitsPerPixel/8 : (int)info.Width*((format.BitsPerPixel+7)/8)); } public static ImageData CreateFlipped (ImageMetaData info, PixelFormat format, BitmapPalette palette,