From 349529978c607bc4added21642594a98d96babe6 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Sat, 3 Aug 2024 16:35:02 +0200 Subject: [PATCH] Fix #74 Add a workaround for bug https://github.com/adafruit/circuitpython/issues/6675 and use bitmap[x, y] instead of putting data into the bitmap directly through the memoryview for bit depths < 8. --- adafruit_imageload/png.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/adafruit_imageload/png.py b/adafruit_imageload/png.py index 2c302f1..404d503 100644 --- a/adafruit_imageload/png.py +++ b/adafruit_imageload/png.py @@ -112,7 +112,19 @@ def load( for y in range(height): dst = y * scanline src = y * (scanline + 1) + 1 - mem[dst : dst + scanline] = data_bytes[src : src + scanline] + if depth < 8: + # Work around the bug in displayio.Bitmap + # https://github.com/adafruit/circuitpython/issues/6675 + pixels_per_byte = 8 // depth + for x in range(0, width, pixels_per_byte): + byte = data_bytes[src] + for pixel in range(pixels_per_byte): + bmp[x + pixel, y] = ( + byte >> ((pixels_per_byte - pixel - 1) * depth) + ) & ((1 << depth) - 1) + src += 1 + else: + mem[dst : dst + scanline] = data_bytes[src : src + scanline] return bmp, pal # RGB, RGBA or Grayscale import displayio