Skip to content

Commit 92421c0

Browse files
committed
Handle buffer rotation for various boards
1 parent 782f54f commit 92421c0

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

adafruit_bitmapsaver.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,43 +61,52 @@ def _write_bmp_header(output_file, filesize):
6161
output_file.write(b'\00\x00')
6262
output_file.write(struct.pack('<I', 54))
6363

64-
def _write_dib_header(output_file, pixel_source):
64+
def _write_dib_header(output_file, w, h):
6565
output_file.write(struct.pack('<I', 40))
66-
output_file.write(struct.pack('<I', pixel_source.width))
67-
output_file.write(struct.pack('<I', pixel_source.height))
66+
output_file.write(struct.pack('<I', w))
67+
output_file.write(struct.pack('<I', h))
6868
output_file.write(struct.pack('<H', 1))
6969
output_file.write(struct.pack('<H', 24))
7070
output_file.write(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
7171

72-
def _bytes_per_row(pixel_source):
73-
pixel_bytes = 3 * pixel_source.width
72+
def _bytes_per_row(source_width):
73+
pixel_bytes = 3 * source_width
7474
padding_bytes = (4 - (pixel_bytes % 4)) % 4
7575
return pixel_bytes + padding_bytes
7676

77-
def rgb565_to_bgr_tuple(color):
77+
def _rotated_height_and_width(pixel_source):
78+
w = pixel_source.width
79+
h = pixel_source.height
80+
# flip axis if the display is rotated
81+
if isinstance(pixel_source, Display) and (pixel_source.rotation % 180 != 0):
82+
return (h, w)
83+
return (w, h)
84+
85+
def _rgb565_to_bgr_tuple(color):
7886
blue = (color << 3) & 0x00F8 # extract each of the RGB tripple into it's own byte
7987
green = (color >> 3) & 0x00FC
8088
red = (color >> 8) & 0x00F8
8189
return (blue, green, red)
8290

8391
def _write_pixels(output_file, pixel_source, palette):
84-
row_buffer = bytearray(_bytes_per_row(pixel_source))
8592
saving_bitmap = isinstance(pixel_source, Bitmap)
86-
for y in range(pixel_source.height, 0, -1):
93+
w, h = _rotated_height_and_width(pixel_source)
94+
row_buffer = bytearray(_bytes_per_row(w))
95+
for y in range(h, 0, -1):
8796
buffer_index = 0
8897
if saving_bitmap:
89-
for x in range(pixel_source.width):
98+
for x in range(w):
9099
pixel = pixel_source[x, y-1]
91100
color = palette[pixel]
92101
for _ in range(3):
93102
row_buffer[buffer_index] = color & 0xFF
94103
color >>= 8
95104
buffer_index += 1
96105
else:
97-
data = pixel_source.fill_area(x=0, y=y-1, width=pixel_source.width, height=1)
98-
for i in range(pixel_source.width):
106+
data = pixel_source.fill_area(x=0, y=y-1, width=w, height=1)
107+
for i in range(w):
99108
pixel565 = (data[i * 2] << 8) + data[i * 2 + 1]
100-
for b in rgb565_to_bgr_tuple(pixel565):
109+
for b in _rgb565_to_bgr_tuple(pixel565):
101110
row_buffer[buffer_index] = b & 0xFF
102111
buffer_index += 1
103112
output_file.write(row_buffer)
@@ -123,9 +132,10 @@ def save_pixels(file_or_filename, pixel_source=board.DISPLAY, palette=None):
123132
else:
124133
output_file = file_or_filename
125134

126-
filesize = 54 + pixel_source.height * _bytes_per_row(pixel_source)
135+
w, h = _rotated_height_and_width(pixel_source)
136+
filesize = 54 + h * _bytes_per_row(w)
127137
_write_bmp_header(output_file, filesize)
128-
_write_dib_header(output_file, pixel_source)
138+
_write_dib_header(output_file, w, h)
129139
_write_pixels(output_file, pixel_source, palette)
130140
except Exception:
131141
raise

guide/pygamer_screenshot.bmp

60.1 KB
Binary file not shown.

guide/pyportal_screenshot.bmp

225 KB
Binary file not shown.

0 commit comments

Comments
 (0)