52
52
__version__ = "0.0.0-auto.0"
53
53
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BitmapSaver.git"
54
54
55
- #pylint:disable=line-too-long,broad-except,redefined-outer-name,invalid-name
56
55
57
56
def _write_bmp_header (output_file , filesize ):
58
57
output_file .write (bytes ('BM' , 'ascii' ))
@@ -61,26 +60,25 @@ def _write_bmp_header(output_file, filesize):
61
60
output_file .write (b'\00 \x00 ' )
62
61
output_file .write (struct .pack ('<I' , 54 ))
63
62
64
- def _write_dib_header (output_file , w , h ):
63
+ def _write_dib_header (output_file , width , height ):
65
64
output_file .write (struct .pack ('<I' , 40 ))
66
- output_file .write (struct .pack ('<I' , w ))
67
- output_file .write (struct .pack ('<I' , h ))
65
+ output_file .write (struct .pack ('<I' , width ))
66
+ output_file .write (struct .pack ('<I' , height ))
68
67
output_file .write (struct .pack ('<H' , 1 ))
69
68
output_file .write (struct .pack ('<H' , 24 ))
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 ' )
69
+ for _ in range (24 ):
70
+ output_file .write (b'\x00 ' )
71
71
72
72
def _bytes_per_row (source_width ):
73
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
77
def _rotated_height_and_width (pixel_source ):
78
- w = pixel_source .width
79
- h = pixel_source .height
80
78
# flip axis if the display is rotated
81
79
if isinstance (pixel_source , Display ) and (pixel_source .rotation % 180 != 0 ):
82
- return (h , w )
83
- return (w , h )
80
+ return (pixel_source . height , pixel_source . width )
81
+ return (pixel_source . width , pixel_source . height )
84
82
85
83
def _rgb565_to_bgr_tuple (color ):
86
84
blue = (color << 3 ) & 0x00F8 # extract each of the RGB tripple into it's own byte
@@ -91,21 +89,21 @@ def _rgb565_to_bgr_tuple(color):
91
89
#pylint:disable=too-many-locals
92
90
def _write_pixels (output_file , pixel_source , palette ):
93
91
saving_bitmap = isinstance (pixel_source , Bitmap )
94
- w , h = _rotated_height_and_width (pixel_source )
95
- row_buffer = bytearray (_bytes_per_row (w ))
96
- for y in range (h , 0 , - 1 ):
92
+ width , height = _rotated_height_and_width (pixel_source )
93
+ row_buffer = bytearray (_bytes_per_row (width ))
94
+ for y in range (height , 0 , - 1 ):
97
95
buffer_index = 0
98
96
if saving_bitmap :
99
- for x in range (w ):
97
+ for x in range (width ):
100
98
pixel = pixel_source [x , y - 1 ]
101
99
color = palette [pixel ]
102
100
for _ in range (3 ):
103
101
row_buffer [buffer_index ] = color & 0xFF
104
102
color >>= 8
105
103
buffer_index += 1
106
104
else :
107
- data = pixel_source .fill_area (x = 0 , y = y - 1 , width = w , height = 1 )
108
- for i in range (w ):
105
+ data = pixel_source .fill_area (x = 0 , y = y - 1 , width = width , height = 1 )
106
+ for i in range (width ):
109
107
pixel565 = (data [i * 2 ] << 8 ) + data [i * 2 + 1 ]
110
108
for b in _rgb565_to_bgr_tuple (pixel565 ):
111
109
row_buffer [buffer_index ] = b & 0xFF
@@ -134,10 +132,10 @@ def save_pixels(file_or_filename, pixel_source=board.DISPLAY, palette=None):
134
132
else :
135
133
output_file = file_or_filename
136
134
137
- w , h = _rotated_height_and_width (pixel_source )
138
- filesize = 54 + h * _bytes_per_row (w )
135
+ width , height = _rotated_height_and_width (pixel_source )
136
+ filesize = 54 + height * _bytes_per_row (width )
139
137
_write_bmp_header (output_file , filesize )
140
- _write_dib_header (output_file , w , h )
138
+ _write_dib_header (output_file , width , height )
141
139
_write_pixels (output_file , pixel_source , palette )
142
140
except Exception :
143
141
raise
0 commit comments