Skip to content

Commit 392007c

Browse files
committed
speed optimizations
1 parent 12f4cdc commit 392007c

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

adafruit_imageload/png.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,28 +108,32 @@ def load( # noqa: PLR0912, PLR0915, Too many branches, Too many statements
108108
if mode == 3: # indexed
109109
bmp = bitmap(width, height, colors)
110110
mem = memoryview(bmp)
111+
pixels_per_byte = 8 // depth
112+
# Adjust for Displayio.Bitmap filler to scanline at 4-byte boundry
113+
filladj = (4 - (scanline % 4)) % 4
114+
dst = 0
115+
src = 1
116+
src_b = 1
111117
for y in range(height):
112-
src = y * (scanline + 1) + 1
113118
if (
114119
(implementation[1][0] == 9 and implementation[1][1] < 2) or implementation[1][0] < 9
115120
) and (depth < 8 or width % 4 != 0):
116121
# Work around the bug in displayio.Bitmap
117122
# remove once CircuitPython 9.1 is no longer supported
118123
# https://github.com/adafruit/circuitpython/issues/6675
119124
# https://github.com/adafruit/circuitpython/issues/9707
120-
pixels_per_byte = 8 // depth
121125
for x in range(0, width, pixels_per_byte):
122-
byte = data_bytes[src]
126+
byte = data_bytes[src_b]
123127
for pixel in range(pixels_per_byte):
124128
bmp[x + pixel, y] = (byte >> ((pixels_per_byte - pixel - 1) * depth)) & (
125129
(1 << depth) - 1
126130
)
127-
src += 1
131+
src_b += 1
128132
else:
129-
# Adjust for Displayio.Bitmap filler to scanline at 4-byte boundry
130-
filladj = y * ((4 - (scanline % 4)) % 4)
131-
dst = y * scanline + filladj
132133
mem[dst : dst + scanline] = data_bytes[src : src + scanline]
134+
dst += scanline + filladj
135+
src += scanline + 1
136+
src_b = src
133137
return bmp, pal
134138
# RGB, RGBA or Grayscale
135139
import displayio

0 commit comments

Comments
 (0)