Skip to content

Memoryview copy to Displayio.Bitmap not working for most display widths #9707

@RetiredWizard

Description

@RetiredWizard

CircuitPython version

Adafruit CircuitPython 9.2.0-beta.0-35-g6f6b8b03a6 on 2024-10-09; ESP32-S3-DevKitC-1-N8R8-with-HACKTABLET with ESP32S3
Adafruit CircuitPython 9.2.0-beta.0-31-g14d3f2e839 on 2024-10-08; Raspberry Pi Pico 2 with rp2350a (using dvi sock)

Code/REPL

import displayio

databytes = bytearray(64)
for i in range(64):
    databytes[i] = i+1

for i in range(2,64):
    bitmap = displayio.Bitmap(i,i,255)
    mem = memoryview(bitmap)
    mem[i:i+i] = databytes[0:i]
    
    passed = 0
    for j in range(i):
        if bitmap[j,1] != databytes[j]:
            passed += 1
            
    if passed == 0:
        print(f'memoryview copy for bitmap dimension of {i} passed')

Behavior

memoryview copy for bitmap dimension of 4 passed
memoryview copy for bitmap dimension of 8 passed
memoryview copy for bitmap dimension of 12 passed
memoryview copy for bitmap dimension of 16 passed
memoryview copy for bitmap dimension of 20 passed
memoryview copy for bitmap dimension of 24 passed
memoryview copy for bitmap dimension of 28 passed
memoryview copy for bitmap dimension of 32 passed
memoryview copy for bitmap dimension of 36 passed
memoryview copy for bitmap dimension of 40 passed
memoryview copy for bitmap dimension of 44 passed
memoryview copy for bitmap dimension of 48 passed
memoryview copy for bitmap dimension of 52 passed
memoryview copy for bitmap dimension of 56 passed
memoryview copy for bitmap dimension of 60 passed

Description

The memoryview copy into the second line of the bitmap matrix gets corrupted for any bitmap with a width which isn't an even multiple of 4. This is causing image display issues in the adafruit_imageload library as discussed in adafruit/Adafruit_CircuitPython_ImageLoad#88

Additional information

Running the following with values of 8 and 10 shows a working and a corrupted matrix:

import displayio

databytes = bytearray(100)
for i in range(100):
    databytes[i] = i+1
dm = 8
bitmap = displayio.Bitmap(dm,dm,255)
mem = memoryview(bitmap)

print('Memoryview copies into bitmap of width',dm)

for i in range(dm):
    mem[dm*i:(dm*i)+dm] = databytes[dm*i:(dm*i)+dm]

    for j in range(dm):
        print(f'{bitmap[j,i]}:',end="")
    print()
Memoryview copies into bitmap of width 8
1:2:3:4:5:6:7:8:
9:10:11:12:13:14:15:16:
17:18:19:20:21:22:23:24:
25:26:27:28:29:30:31:32:
33:34:35:36:37:38:39:40:
41:42:43:44:45:46:47:48:
49:50:51:52:53:54:55:56:
57:58:59:60:61:62:63:64:
Memoryview copies into bitmap of width 10
1:2:3:4:5:6:7:8:9:10:
13:14:15:16:17:18:19:20:0:0:
25:26:27:28:29:30:0:0:0:0:
37:38:39:40:0:0:0:0:0:0:
49:50:0:0:0:0:0:0:0:0:
0:0:0:0:0:0:0:0:0:0:
0:0:0:0:0:0:0:0:0:0:
0:0:0:0:0:0:0:0:0:0:
0:0:0:0:0:0:0:0:0:0:
0:0:0:0:0:0:0:0:0:0:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions