@@ -61,43 +61,52 @@ def _write_bmp_header(output_file, filesize):
61
61
output_file .write (b'\00 \x00 ' )
62
62
output_file .write (struct .pack ('<I' , 54 ))
63
63
64
- def _write_dib_header (output_file , pixel_source ):
64
+ def _write_dib_header (output_file , w , h ):
65
65
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 ))
68
68
output_file .write (struct .pack ('<H' , 1 ))
69
69
output_file .write (struct .pack ('<H' , 24 ))
70
70
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 ' )
71
71
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
74
74
padding_bytes = (4 - (pixel_bytes % 4 )) % 4
75
75
return pixel_bytes + padding_bytes
76
76
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 ):
78
86
blue = (color << 3 ) & 0x00F8 # extract each of the RGB tripple into it's own byte
79
87
green = (color >> 3 ) & 0x00FC
80
88
red = (color >> 8 ) & 0x00F8
81
89
return (blue , green , red )
82
90
83
91
def _write_pixels (output_file , pixel_source , palette ):
84
- row_buffer = bytearray (_bytes_per_row (pixel_source ))
85
92
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 ):
87
96
buffer_index = 0
88
97
if saving_bitmap :
89
- for x in range (pixel_source . width ):
98
+ for x in range (w ):
90
99
pixel = pixel_source [x , y - 1 ]
91
100
color = palette [pixel ]
92
101
for _ in range (3 ):
93
102
row_buffer [buffer_index ] = color & 0xFF
94
103
color >>= 8
95
104
buffer_index += 1
96
105
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 ):
99
108
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 ):
101
110
row_buffer [buffer_index ] = b & 0xFF
102
111
buffer_index += 1
103
112
output_file .write (row_buffer )
@@ -123,9 +132,10 @@ def save_pixels(file_or_filename, pixel_source=board.DISPLAY, palette=None):
123
132
else :
124
133
output_file = file_or_filename
125
134
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 )
127
137
_write_bmp_header (output_file , filesize )
128
- _write_dib_header (output_file , pixel_source )
138
+ _write_dib_header (output_file , w , h )
129
139
_write_pixels (output_file , pixel_source , palette )
130
140
except Exception :
131
141
raise
0 commit comments