Skip to content

Commit 6463092

Browse files
committed
Simplify load and use [] on the bitmap
1 parent 31ba93a commit 6463092

File tree

1 file changed

+9
-23
lines changed

1 file changed

+9
-23
lines changed

adafruit_imageload/bmp/indexed.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,16 @@ def load(f, width, height, data_start, colors, color_depth, *, bitmap=None, pale
5454
if line_size % 4 != 0:
5555
line_size += (4 - line_size % 4)
5656

57-
packed_pixels = None
58-
if color_depth != minimum_color_depth and minimum_color_depth == 2:
59-
target_line_size = width // 4
60-
if target_line_size % 4 != 0:
61-
target_line_size += (4 - target_line_size % 4)
57+
chunk = bytearray(line_size)
6258

63-
packed_pixels = bytearray(target_line_size)
59+
for y in range(height-1,-1,-1):
60+
f.readinto(chunk)
61+
pixels_per_byte = 8 // color_depth
62+
offset = y * width
6463

65-
for line in range(height-1,-1,-1):
66-
chunk = f.read(line_size)
67-
if packed_pixels:
68-
original_pixels_per_byte = 8 // color_depth
69-
packed_pixels_per_byte = 8 // minimum_color_depth
70-
71-
for i in range(width // packed_pixels_per_byte):
72-
packed_pixels[i] = 0
73-
74-
for i in range(width):
75-
pi = i // packed_pixels_per_byte
76-
ci = i // original_pixels_per_byte
77-
packed_pixels[pi] |= ((chunk[ci] >> (8 - color_depth*(i % original_pixels_per_byte + 1))) & 0x3) << (8 - minimum_color_depth*(i % packed_pixels_per_byte + 1))
78-
79-
bitmap._load_row(line, packed_pixels)
80-
else:
81-
bitmap._load_row(line, chunk)
64+
for x in range(width):
65+
ci = x // pixels_per_byte
66+
pixel = (chunk[ci] >> (8 - color_depth*(x % pixels_per_byte + 1))) & ((1 << minimum_color_depth) - 1)
67+
bitmap[offset + x] = pixel
8268

8369
return bitmap, palette

0 commit comments

Comments
 (0)