Skip to content

Show 8-bit images with a ColorConverter on LVGL displays - #12025

Open
mikeysklar wants to merge 2 commits into
adafruit:mainfrom
mikeysklar:lvgl-8bit-colorconverter
Open

mikeysklar wants to merge 2 commits into
adafruit:mainfrom
mikeysklar:lvgl-8bit-colorconverter

Conversation

@mikeysklar

Copy link
Copy Markdown
Collaborator

What

Bitmaps of 8 bits or fewer with a ColorConverter now draw on LVGL displays.

Why

They raised "Format not supported", so the Qualia S3 Compass guide crashed.

Qualia ESP32-S3, 720x720, framebuffer read back Legacy Before After
8-bit, 7 colorspaces reference Fails Same as legacy
1, 2, 4-bit, 3x scale reference Fails Same as legacy
Transparent before showing reference Fails Same as legacy
Transparency changed while showing No redraw Fails Redraws
L8, 16-bit reference Same Same
Test code.py and output
# SPDX-FileCopyrightText: 2026 Mikey Sklar
# SPDX-License-Identifier: MIT
# Draw low-bit bitmaps through a ColorConverter and read the pixels back from the dot clock
# framebuffer, so legacy displayio and LVGL displayio can be compared value for value.
import binascii
import displayio
from adafruit_qualia.graphics import Displays, Graphics

graphics = Graphics(Displays.SQUARE40, default_bg=None, auto_refresh=False)
display = graphics.display
fb = memoryview(display.framebuffer).cast("B")
W = display.width
CS = displayio.Colorspace


def px(x, y):
    i = (y * W + x) * 2
    return fb[i] | (fb[i + 1] << 8)


def show(*layers, bg=None):
    g = displayio.Group()
    if bg is not None:
        p = displayio.Palette(1)
        p[0] = bg
        g.append(displayio.TileGrid(displayio.Bitmap(W, display.height, 1), pixel_shader=p))
    for layer in layers:
        g.append(layer)
    display.root_group = g
    display.refresh()


def ramp(bits):
    n = 1 << bits
    side = 16 if n == 256 else n
    b = displayio.Bitmap(side, max(1, n // side), n)
    for v in range(n):
        b[v % side, v // side] = v
    return b, side


def sample(bits, x0=0, y0=0, step=1):
    n = 1 << bits
    side = 16 if n == 256 else n
    return [px(x0 + (v % side) * step, y0 + (v // side) * step) for v in range(n)]


def report(name, vals):
    raw = bytes(b for v in vals for b in (v & 0xFF, v >> 8))
    print("CC8 %-26s crc=%08x %s" % (name, binascii.crc32(raw), " ".join("%04x" % v for v in vals[:6])))


for cs_name in ("RGB888", "RGB565", "RGB565_SWAPPED", "RGB555", "RGB555_SWAPPED",
                "BGR565", "BGR565_SWAPPED", "L8"):
    b, _ = ramp(8)
    cc = displayio.ColorConverter(input_colorspace=getattr(CS, cs_name))
    try:
        show(displayio.TileGrid(b, pixel_shader=cc, x=40, y=40))
        report("8bit " + cs_name, sample(8, 40, 40))
    except Exception as e:
        print("CC8 %-26s FAIL %r" % ("8bit " + cs_name, e))

for bits in (1, 2, 4):
    b, _ = ramp(bits)
    cc = displayio.ColorConverter(input_colorspace=CS.RGB565_SWAPPED)
    show(displayio.TileGrid(b, pixel_shader=cc, x=40, y=40))
    report("%dbit RGB565_SWAPPED" % bits, sample(bits, 40, 40))

# Scaled 3x inside a group, read at each cell's center.
b, _ = ramp(8)
g = displayio.Group(scale=3, x=40, y=40)
g.append(displayio.TileGrid(b, pixel_shader=displayio.ColorConverter(input_colorspace=CS.RGB565_SWAPPED)))
show(g)
report("8bit scale3", sample(8, 41, 41, 3))

# Transparency over a red background: set before showing, set after, then cleared.
b, _ = ramp(8)
cc = displayio.ColorConverter(input_colorspace=CS.RGB565_SWAPPED)
cc.make_transparent(5)
tg = displayio.TileGrid(b, pixel_shader=cc, x=40, y=40)
show(tg, bg=0xFF0000)
report("transparent 5 at start", sample(8, 40, 40))
cc.make_opaque(5)
display.refresh()
report("opaque again", sample(8, 40, 40))
cc.make_transparent(9)
display.refresh()
report("transparent 9 after show", sample(8, 40, 40))

# L8 with a transparent value keeps its old behavior.
b, _ = ramp(8)
cc = displayio.ColorConverter(input_colorspace=CS.L8)
cc.make_transparent(5)
try:
    show(displayio.TileGrid(b, pixel_shader=cc, x=40, y=40), bg=0xFF0000)
    report("L8 transparent 5", sample(8, 40, 40))
except Exception as e:
    print("CC8 %-26s FAIL %r" % ("L8 transparent 5", e))

# 16-bit path unchanged.
b16 = displayio.Bitmap(16, 16, 65536)
for v in range(256):
    b16[v % 16, v // 16] = v * 257
show(displayio.TileGrid(b16, pixel_shader=displayio.ColorConverter(input_colorspace=CS.RGB565_SWAPPED), x=40, y=40))
report("16bit RGB565_SWAPPED", sample(8, 40, 40))
print("BENCH done")

Legacy displayio (run before the L8 transparency case was added):

CC8 8bit RGB888                crc=e1da1c45 0000 0000 0000 0000 0000 0000
CC8 8bit RGB565                crc=f8eaab81 0000 0001 0002 0003 0004 0005
CC8 8bit RGB565_SWAPPED        crc=2dd1cd1c 0000 0100 0200 0300 0400 0500
CC8 8bit RGB555                crc=061d1184 0000 0001 0002 0003 0004 0005
CC8 8bit RGB555_SWAPPED        crc=589fc13e 0000 0200 0420 0620 0800 0a00
CC8 8bit BGR565                crc=85d543a2 0000 0800 1000 1800 2000 2800
CC8 8bit BGR565_SWAPPED        crc=d7c4d4f4 0000 0100 0200 0300 0400 0500
CC8 8bit L8                    crc=ca08b824 0000 0000 0000 0000 0020 0020
CC8 1bit RGB565_SWAPPED        crc=5643ef8a 0000 0100
CC8 2bit RGB565_SWAPPED        crc=c2cf730d 0000 0100 0200 0300
CC8 4bit RGB565_SWAPPED        crc=2f3ec3de 0000 0100 0200 0300 0400 0500
CC8 8bit scale3                crc=2dd1cd1c 0000 0100 0200 0300 0400 0500
CC8 transparent 5 at start     crc=349560e1 0000 0100 0200 0300 0400 f800
CC8 opaque again               crc=349560e1 0000 0100 0200 0300 0400 f800
CC8 transparent 9 after show   crc=349560e1 0000 0100 0200 0300 0400 f800
CC8 16bit RGB565_SWAPPED       crc=679113e5 0000 0101 0202 0303 0404 0505

LVGL with this PR:

CC8 8bit RGB888                crc=e1da1c45 0000 0000 0000 0000 0000 0000
CC8 8bit RGB565                crc=f8eaab81 0000 0001 0002 0003 0004 0005
CC8 8bit RGB565_SWAPPED        crc=2dd1cd1c 0000 0100 0200 0300 0400 0500
CC8 8bit RGB555                crc=061d1184 0000 0001 0002 0003 0004 0005
CC8 8bit RGB555_SWAPPED        crc=589fc13e 0000 0200 0420 0620 0800 0a00
CC8 8bit BGR565                crc=85d543a2 0000 0800 1000 1800 2000 2800
CC8 8bit BGR565_SWAPPED        crc=d7c4d4f4 0000 0100 0200 0300 0400 0500
CC8 8bit L8                    crc=ca08b824 0000 0000 0000 0000 0020 0020
CC8 1bit RGB565_SWAPPED        crc=5643ef8a 0000 0100
CC8 2bit RGB565_SWAPPED        crc=c2cf730d 0000 0100 0200 0300
CC8 4bit RGB565_SWAPPED        crc=2f3ec3de 0000 0100 0200 0300 0400 0500
CC8 8bit scale3                crc=2dd1cd1c 0000 0100 0200 0300 0400 0500
CC8 transparent 5 at start     crc=349560e1 0000 0100 0200 0300 0400 f800
CC8 opaque again               crc=2dd1cd1c 0000 0100 0200 0300 0400 0500
CC8 transparent 9 after show   crc=70e4458c 0000 0100 0200 0300 0400 0500
CC8 L8 transparent 5           FAIL NotImplementedError('Format not supported',)
CC8 16bit RGB565_SWAPPED       crc=679113e5 0000 0101 0202 0303 0404 0505

Not covered: a converter shared with a 16-bit TileGrid ignores later transparency, as before.

Written with Claude Code; I checked the pixel results and photos on the board.

🤖 Generated with Claude Code

A TileGrid with a 1, 2, 4 or 8 bit Bitmap and a ColorConverter raised
"Format not supported" with LVGL displayio, except 8-bit L8. Draw it as an
indexed image instead, with a 256 entry table of the colors the converter
gives each value, the same way a Palette is drawn. The pixels match legacy
displayio.

make_transparent() and make_opaque() now tell LVGL, so the table and any
shapes using the converter redraw.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@tannewt tannewt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some native_sim based tests for this.

Draws 1, 2, 4 and 8 bit bitmaps through a ColorConverter on both
displayio compositors and compares the frame pixel for pixel, including
transparency and a scaled group. On LVGL, also checks that
make_transparent() and make_opaque() redraw a TileGrid that is shown.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mikeysklar

Copy link
Copy Markdown
Collaborator Author

Added native_sim tests for both compositors. They fail on main with "Format not supported", and the redraw test also fails without the make_transparent()/make_opaque() change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants