From 6788a7b5fe8c93973600b4941066b2637350c723 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Wed, 4 May 2022 17:16:08 +0100 Subject: [PATCH] fxsdk: round RGB16 images to even widths This allows 4-alignment on the input to be preserved from line to line, which is very valuable for rendering optimizations. --- fxconv/fxconv.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/fxconv/fxconv.py b/fxconv/fxconv.py index c7030dc..b3add0c 100644 --- a/fxconv/fxconv.py +++ b/fxconv/fxconv.py @@ -1096,14 +1096,20 @@ def r5g6b5(img, color_count=0, trim_palette=False, palette_base=0, alpha=None): # Create byte arrays with pixel data and palette data #--- - pixel_count = img.width * img.height + stride = 0 if not color_count: - size = pixel_count * 2 + # In RGB16, preserve alignment between rows + stride = (img.width + 1) // 2 * 2 + size = stride * img.height * 2 elif color_count == 256: - size = pixel_count + # No constraint in P8 + size = img.width * img.height + stride = img.width elif color_count == 16: - size = ((img.width + 1) // 2) * img.height + # In P4, pad whole bytes + stride = (img.width + 1) // 2 + size = stride * img.height # Result of encoding encoded = bytearray(size) @@ -1116,8 +1122,8 @@ def r5g6b5(img, color_count=0, trim_palette=False, palette_base=0, alpha=None): if not color_count: c = alpha_encoding(rgb24to16(pixels[x, y]), a) + offset = (stride * y + x) * 2 encoded[offset:offset+2] = u16(c) - offset += 2 elif color_count == 16: c = palette_remap[pixels[x, y]][0] if a > 0 else alpha