From 56062e1a3eaa264975d7c876a5f6f8c220cc355b Mon Sep 17 00:00:00 2001 From: MangaValk Date: Sun, 15 Nov 2026 23:56:58 +0100 Subject: [PATCH] Intelligent colour expansion and alpha blending, measured in the simulator Co-Authored-By: Claude Opus 5.5 --- docs/RENDER.md | 7 +++++ src/nxhmi/pictures.py | 8 +++++- src/nxhmi/render.py | 59 +++++++++++++++++++++++++------------------ tests/test_media.py | 8 ++++++ tests/test_render.py | 21 +++++++++++++++ 5 files changed, 78 insertions(+), 25 deletions(-) diff --git a/docs/RENDER.md b/docs/RENDER.md index dd70bfa..3fba936 100644 --- a/docs/RENDER.md +++ b/docs/RENDER.md @@ -38,6 +38,13 @@ Per component (dedicated test pages): ## Rules learned from the Editor +- Colours: RGB565 is expanded differently per series. Basic replicates the top bits + (`r<<3 | r>>2`); Intelligent scales (`r*255//31`, `g*255//63`), so `0xC618` shows as + (198,195,198) on Basic and (197,194,197) on Intelligent. +- Intelligent pictures with alpha (format 4, 7-bit alpha `a`): 8-bit alpha is `2*a` (127 = 255), + blended as `bg + round((src - bg) * alpha / 255)` per channel. Exact against the Intelligent + simulator (342 samples). The Editor *canvas* blends slightly differently (up to 3 levels off at + high alpha); the renderer follows the simulator, i.e. the display. - Text never starts above/left of its box: centring clamps at 0 (a 32-px font in a 30-px box starts at the top). - Text is laid out inside the border: 1 px for 3D styles, `borderw` for style "border". diff --git a/src/nxhmi/pictures.py b/src/nxhmi/pictures.py index 0ff9b5d..d7bf989 100644 --- a/src/nxhmi/pictures.py +++ b/src/nxhmi/pictures.py @@ -251,6 +251,12 @@ def to_image(blob: bytes): raise NotImplementedError("picture format %d not supported" % d["format"]) img = Image.new("RGBA" if alpha else "RGB", (w, h)) out = img.load() + if d["format"] == 4: # Intelligent: channels scaled (v*255//31), alpha 8-bit = 2 * 7-bit + for i, (x, y) in enumerate(order): + v = px[i] + col = ((v >> 11) * 255 // 31, ((v >> 5) & 63) * 255 // 63, (v & 31) * 255 // 31) + out[x, y] = col + ((255 if alpha[i] == 127 else 2 * alpha[i],) if alpha else ()) + return img for i, (x, y) in enumerate(order): - out[x, y] = _rgb(px[i]) + (((alpha[i] * 255 + 63) // 127,) if alpha else ()) + out[x, y] = _rgb(px[i]) return img diff --git a/src/nxhmi/render.py b/src/nxhmi/render.py index cc28075..e3cd8ba 100644 --- a/src/nxhmi/render.py +++ b/src/nxhmi/render.py @@ -117,8 +117,12 @@ def qr_rects(txt, w: int, dis: int): rects.append(((dis + edge[col], dis + edge[r0]), (dis + x1, dis + edge[r]))) return rects -def rgb(v: int): +def rgb(v: int, series: int = 0): + """RGB565 -> 8-bit RGB the way the Editor shows it: Basic replicates the top bits, + Intelligent scales (``v * 255 // 31``), e.g. 0xC618 is (198,195,198) vs (197,194,197).""" r, g, b = v >> 11, (v >> 5) & 63, v & 31 + if series == 3: + return (r * 255 // 31, g * 255 // 63, b * 255 // 31) return (r << 3 | r >> 2, g << 2 | g >> 4, b << 3 | b >> 2) @@ -129,12 +133,19 @@ class Renderer: self._pics = {} self._fonts = {} self.pages = {} + try: + self.series = hmi.read("main.HMI")[11] + except KeyError: + self.series = 0 for e in hmi.live(): if e.name.endswith(".pa"): p = Page.parse(hmi.read(e.name)) self.pages[p.name] = p self.pages[int(e.name[:-3])] = p + def rgb(self, v: int): + return rgb(v, self.series) + # ------------------------------------------------------------------ resources def pic(self, n: int): if n not in self._pics: @@ -161,7 +172,7 @@ class Renderer: img = Image.new("RGB", (w, h), (255, 255, 255)) sta = pc.get("sta") if sta == 1: - img.paste(rgb(pc.get("bco")), (0, 0, w, h)) + img.paste(self.rgb(pc.get("bco")), (0, 0, w, h)) elif sta == 2: pic = self.pic(pc.get("pic")) if pic: @@ -186,8 +197,8 @@ class Renderer: h = min(h, src.height - sy) if w > 0 and h > 0: part = src.crop((sx, sy, sx + w, sy + h)) - # Intelligent pictures can carry alpha: blended over what is below (blend not yet - # compared with the Editor pixel by pixel) + # Intelligent pictures carry alpha (8-bit = 2 * stored 7-bit, 127 = opaque); Pillow's + # paste rounds (s*a + b*(255-a)) / 255 like the Intelligent simulator img.paste(part, (dx, dy), part if part.mode == "RGBA" else None) def _box(self, c): @@ -201,7 +212,7 @@ class Renderer: if src: self._paste(img, src, x, y, w, h, x, y) elif sta == 1: - img.paste(rgb(c.get("bco")), (x, y, x + w, y + h)) + img.paste(self.rgb(c.get("bco")), (x, y, x + w, y + h)) elif sta == 2: src = self.pic(c.get("pic")) if src: @@ -214,7 +225,7 @@ class Renderer: if c.get("sta") in (0, 2) and c.get("type") in (98, 53): return # picture-skinned buttons have no frame if style == 1: # border - bw, bc = c.get("borderw") or 0, rgb(c.get("borderc") or 0) + bw, bc = c.get("borderw") or 0, self.rgb(c.get("borderc") or 0) for i in range(bw): d.rectangle((x + i, y + i, x + w - 1 - i, y + h - 1 - i), outline=bc) elif style in (2, 3, 4): # 3D down / up / auto(=up when released) @@ -285,7 +296,7 @@ class Renderer: on = bool(c.get("val")) sta = c.get("sta") if sta == 1: - img.paste(rgb((c.get("bco2") if on else c.get("bco")) or 0), (x, y, x + w, y + h)) + img.paste(self.rgb((c.get("bco2") if on else c.get("bco")) or 0), (x, y, x + w, y + h)) elif sta == 2: src = self.pic(c.get("pic2") if on else c.get("pic")) if src: @@ -299,7 +310,7 @@ class Renderer: else: self._border(img, c, x, y, w, h) self._text(img, c, c.get("txt") or "", *self._inner(c, x, y, w, h), - rgb((c.get("pco2") if on else c.get("pco")) or 0)) + self.rgb((c.get("pco2") if on else c.get("pco")) or 0)) def _inner(self, c, x, y, w, h): """Text area inside the border.""" @@ -311,7 +322,7 @@ class Renderer: x, y, w, h = self._box(c) self._background(img, c, x, y, w, h) self._border(img, c, x, y, w, h) - self._text(img, c, c.get("txt") or "", *self._inner(c, x, y, w, h), rgb(c.get("pco") or 0)) + self._text(img, c, c.get("txt") or "", *self._inner(c, x, y, w, h), self.rgb(c.get("pco") or 0)) def number(self, img, c): v = c.get("val") or 0 @@ -324,7 +335,7 @@ class Renderer: x, y, w, h = self._box(c) self._background(img, c, x, y, w, h) self._border(img, c, x, y, w, h) - self._text(img, c, s, *self._inner(c, x, y, w, h), rgb(c.get("pco") or 0)) + self._text(img, c, s, *self._inner(c, x, y, w, h), self.rgb(c.get("pco") or 0)) def xfloat(self, img, c): v = c.get("val") or 0 @@ -340,7 +351,7 @@ class Renderer: x, y, w, h = self._box(c) self._background(img, c, x, y, w, h) self._border(img, c, x, y, w, h) - self._text(img, c, s, *self._inner(c, x, y, w, h), rgb(c.get("pco") or 0)) + self._text(img, c, s, *self._inner(c, x, y, w, h), self.rgb(c.get("pco") or 0)) def picture(self, img, c): src = self.pic(c.get("pic")) @@ -379,12 +390,12 @@ class Renderer: cw = w if cw in (None, 255) else cw ch = w if ch in (None, 255) else ch cy = y + h - ch - int(frac * (h - ch)) # truncates, like the Editor - d.rectangle((x + (w - cw) // 2, cy, x + (w - cw) // 2 + cw - 1, cy + ch - 1), fill=rgb(c.get("pco") or 0)) + d.rectangle((x + (w - cw) // 2, cy, x + (w - cw) // 2 + cw - 1, cy + ch - 1), fill=self.rgb(c.get("pco") or 0)) else: ch = h if ch in (None, 255) else ch cw = h if cw in (None, 255) else cw cx = x + int(frac * (w - cw)) # truncates, like the Editor - d.rectangle((cx, y + (h - ch) // 2, cx + cw - 1, y + (h - ch) // 2 + ch - 1), fill=rgb(c.get("pco") or 0)) + d.rectangle((cx, y + (h - ch) // 2, cx + cw - 1, y + (h - ch) // 2 + ch - 1), fill=self.rgb(c.get("pco") or 0)) def progress(self, img, c): from PIL import ImageDraw @@ -405,24 +416,24 @@ class Renderer: if fw: self._paste(img, fg, 0, 0, fw, h, x, y) return - d.rectangle((x, y, x + w - 1, y + h - 1), fill=rgb(c.get("bco") or 0)) + d.rectangle((x, y, x + w - 1, y + h - 1), fill=self.rgb(c.get("bco") or 0)) if c.get("dez") == 1: fh = h * val // 100 if fh: - d.rectangle((x, y + h - fh, x + w - 1, y + h - 1), fill=rgb(c.get("pco") or 0)) + d.rectangle((x, y + h - fh, x + w - 1, y + h - 1), fill=self.rgb(c.get("pco") or 0)) else: fw = w * val // 100 if fw: - d.rectangle((x, y, x + fw - 1, y + h - 1), fill=rgb(c.get("pco") or 0)) + d.rectangle((x, y, x + fw - 1, y + h - 1), fill=self.rgb(c.get("pco") or 0)) def checkbox(self, img, c): from PIL import ImageDraw x, y, w, h = self._box(c) d = ImageDraw.Draw(img) - d.rectangle((x, y, x + w - 1, y + h - 1), fill=rgb(c.get("bco") or 0)) + d.rectangle((x, y, x + w - 1, y + h - 1), fill=self.rgb(c.get("bco") or 0)) self._border(img, c, x, y, w, h) if c.get("val"): # filled square, half the size, centred - d.rectangle((x + w // 4, y + h // 4, x + 3 * w // 4 - 1, y + 3 * h // 4 - 1), fill=rgb(c.get("pco") or 0)) + d.rectangle((x + w // 4, y + h // 4, x + 3 * w // 4 - 1, y + 3 * h // 4 - 1), fill=self.rgb(c.get("pco") or 0)) def radio(self, img, c): """Circle sized by the width, centred at ((w-1)//2, (w-1)//2), clipped to the box: @@ -442,16 +453,16 @@ class Renderer: for dx in range(-hw, hw + 1): put(cx + dx, cy + dy, col) - fill(r, rgb(c.get("bco") or 0)) + fill(r, self.rgb(c.get("bco") or 0)) for dx, dy in circle_points(r): put(cx + dx, cy + dy, (65, 68, 65)) if c.get("val"): - fill(r // 2, rgb(c.get("pco") or 0)) + fill(r // 2, self.rgb(c.get("pco") or 0)) def gauge(self, img, c): x, y, w, h = self._box(c) self._background(img, c, x, y, w, h) - px, col = img.load(), rgb(c.get("pco") or 0) + px, col = img.load(), self.rgb(c.get("pco") or 0) for ax, ay in gauge_needle(w, h, c.get("val") or 0, c.get("wid") or 1): if 0 <= x + ax < img.width and 0 <= y + ay < img.height: px[x + ax, y + ay] = col @@ -461,7 +472,7 @@ class Renderer: x, y, w, h = self._box(c) self._background(img, c, x, y, w, h) d = ImageDraw.Draw(img) - gw, gh, gc = c.get("gdw") or 0, c.get("gdh") or 0, rgb(c.get("gdc") or 0) + gw, gh, gc = c.get("gdw") or 0, c.get("gdh") or 0, self.rgb(c.get("gdc") or 0) if gw: for xx in range(x, x + w, gw): d.line((xx, y, xx, y + h - 1), fill=gc) @@ -472,10 +483,10 @@ class Renderer: def qrcode(self, img, c): x, y, w, h = self._box(c) bco, pco = c.get("bco"), c.get("pco") - img.paste(rgb(0xFFFF if bco is None else bco), (x, y, x + w, y + h)) + img.paste(self.rgb(0xFFFF if bco is None else bco), (x, y, x + w, y + h)) rects = qr_rects(c.get("txt") or "", w, c.get("dis") or 0) for (ax, ay), (bx, by) in rects: - img.paste(rgb(pco or 0), (x + ax, y + ay, x + bx, y + by)) # not clipped to h (like the display) + img.paste(self.rgb(pco or 0), (x + ax, y + ay, x + bx, y + by)) # not clipped to h (like the display) if rects and c.get("sta") == 1: # logo picture, centred in the box src = self.pic(c.get("pic")) if src: diff --git a/tests/test_media.py b/tests/test_media.py index 9247f37..9c136f4 100644 --- a/tests/test_media.py +++ b/tests/test_media.py @@ -87,6 +87,14 @@ class Format4(unittest.TestCase): self.assertEqual(tuple(v >> 3 for v in img.getpixel((x, y))[:3]), (r >> 3, g >> 3, b >> 3)) self.assertLess(img.getpixel((0, 3))[3], 255) + def test_intelligent_colours_and_alpha(self): + # Intelligent simulator: channels scaled v*255//31, alpha 8-bit = 2 * stored 7-bit (127 = 255) + blob = pictures.encode_format4(lambda x, y: (198, 195, 198, [255, 227, 3][x]), 3, 1, 0) + img = pictures.to_image(blob) + self.assertEqual(img.getpixel((0, 0)), (197, 194, 197, 255)) + self.assertEqual(img.getpixel((1, 0))[3], 226) # 227 * 127 // 255 = 113 -> 226 + self.assertEqual(img.getpixel((2, 0))[3], 2) + @unittest.skipUnless(HAVE_PIL, "needs Pillow") def test_image_roundtrip(self): from PIL import Image diff --git a/tests/test_render.py b/tests/test_render.py index 4cb0b22..4addfed 100644 --- a/tests/test_render.py +++ b/tests/test_render.py @@ -105,6 +105,27 @@ class Render(unittest.TestCase): self.assertEqual(len(gauge_needle(58, 58, 69, 8)), 265) self.assertEqual(gauge_needle(58, 58, 0, 2), {(x, y) for x in range(1, 30) for y in (28, 29)}) + def test_intelligent_alpha_blend(self): + # measured in the Intelligent simulator: page blue, picture (255, 0, 41) with 7-bit alpha 97 + import io + from PIL import Image + from nxhmi import project + from nxhmi.page import Page + from nxhmi.render import Renderer, rgb + self.assertEqual(rgb(0xC618, 3), (197, 194, 197)) + h = project.new_project("NX4827P043_011") + im = Image.new("RGBA", (3, 1), (255, 0, 40, 195)) # 3 px: 1-2 px pictures stay format 0 + im.putpixel((1, 0), (255, 0, 40, 255)) + b = io.BytesIO(); im.save(b, "PNG") + pid = project.add_picture(h, b.getvalue()) + p = Page.parse(h.read("0.pa")) + p.components[0].set("bco", 0x001F) + C.add(p, "picture", x=0, y=0, w=3, h=1, pic=pid) + h.write("0.pa", p.pack()) + img = Renderer(h).render("page0") + self.assertEqual(img.getpixel((0, 0)), (194, 0, 92)) + self.assertEqual(img.getpixel((1, 0)), (255, 0, 41)) + def test_demo_pages_render(self): from nxhmi.render import Renderer r = Renderer(HMIFile.load(FIXTURE))