From 6d2dcea900c538cc139bc1e1b7c8bb825e810678 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Sat, 7 May 2022 12:46:30 +0100 Subject: [PATCH] fxconv: "p4" and "p8" now select best new format Instead of p8 being the old p8 (which now doesn't display anymore!) and p4 being p4_rgb565a. --- fxconv/fxconv.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/fxconv/fxconv.py b/fxconv/fxconv.py index b3add0c..78265b6 100644 --- a/fxconv/fxconv.py +++ b/fxconv/fxconv.py @@ -114,20 +114,16 @@ CG_PROFILES = [ # 16-bit RGB565 and RGB565 with alpha CgProfile(0x0, "rgb565", False), CgProfile(0x1, "rgb565a", True), - # 8-bit palette for RGB565 and RGB565A (supported by Azur only) + # 8-bit palette for RGB565 and RGB565A CgProfile(0x4, "p8_rgb565", False), CgProfile(0x5, "p8_rgb565a", True), - # 4-bit palette for RGB565 and RGB565A (supported by Azur only) + # 4-bit palette for RGB565 and RGB565A CgProfile(0x6, "p4_rgb565", False), CgProfile(0x3, "p4_rgb565a", True), # Original names for RGB565 and RGB565A CgProfile(0x0, "r5g6b5", False), CgProfile(0x1, "r5g6b5a", True), - # The original 8-bit palette mode of bopti (inferior to the other P8 modes) - CgProfile(0x2, "p8", True), - # The original 4-bit palette mode of bopti (same as P4_RGB565A) - CgProfile(0x3, "p4", True), ] # Libimg flags @@ -541,15 +537,18 @@ def convert_bopti_cg(input, params): profile = CgProfile.find(name) elif name.startswith("p"): - if name in ["p8_rgb565", "p8_rgb565a"]: + if name.startswith("p8"): trim_palette = True palette_base = 0x80 - else: + color_count = 256 + elif name.startswith("p4"): trim_palette = False palette_base = 0x00 + color_count = 16 + else: + raise FxconvError(f"unknown palette format {profile}") - # Encoded the image into 16-bit with a palette of 16 or 256 entries - color_count = 1 << int(name[1]) + # Encode the image into 16-bit with a palette of 16 or 256 entries encoded, palette, alpha = r5g6b5(img, color_count=color_count, trim_palette=trim_palette, palette_base=palette_base) @@ -559,8 +558,17 @@ def convert_bopti_cg(input, params): else: raise FxconvError(f"unknown color profile '{name}'") + # Resolve "p8" and "p4" to their optimal variation + if name == "p8": + name = "p8_rgb565" if alpha is None else "p8_rgb565a" + profile = CgProfile.find(name) + elif name == "p4": + name = "p4_rgb565" if alpha is None else "p4_rgb565a" + profile = CgProfile.find(name) + if alpha is not None and not profile.supports_alpha: - raise FxconvError(f"'{input}' has transparency; use rgb565a, p8 or p4") + raise FxconvError(f"'{input}' has transparency; use rgb565a, " + + "p8_rgb565a or p4_rgb565a") header = bytes() header += u16(profile.id)