33
33
import board
34
34
from displayio import Bitmap , Palette , Display
35
35
36
+ try :
37
+ from typing import Tuple , Optional , Union
38
+ from io import BufferedWriter
39
+ except ImportError :
40
+ pass
41
+
36
42
__version__ = "0.0.0-auto.0"
37
43
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BitmapSaver.git"
38
44
39
45
40
- def _write_bmp_header (output_file , filesize ) :
46
+ def _write_bmp_header (output_file : BufferedWriter , filesize : int ) -> None :
41
47
output_file .write (bytes ("BM" , "ascii" ))
42
48
output_file .write (struct .pack ("<I" , filesize ))
43
49
output_file .write (b"\00 \x00 " )
44
50
output_file .write (b"\00 \x00 " )
45
51
output_file .write (struct .pack ("<I" , 54 ))
46
52
47
53
48
- def _write_dib_header (output_file , width , height ) :
54
+ def _write_dib_header (output_file : BufferedWriter , width : int , height : int ) -> None :
49
55
output_file .write (struct .pack ("<I" , 40 ))
50
56
output_file .write (struct .pack ("<I" , width ))
51
57
output_file .write (struct .pack ("<I" , height ))
@@ -55,43 +61,49 @@ def _write_dib_header(output_file, width, height):
55
61
output_file .write (b"\x00 " )
56
62
57
63
58
- def _bytes_per_row (source_width ) :
64
+ def _bytes_per_row (source_width : int ) -> int :
59
65
pixel_bytes = 3 * source_width
60
66
padding_bytes = (4 - (pixel_bytes % 4 )) % 4
61
67
return pixel_bytes + padding_bytes
62
68
63
69
64
- def _rotated_height_and_width (pixel_source ) :
70
+ def _rotated_height_and_width (pixel_source : Union [ Bitmap , Display ]) -> Tuple [ int , int ] :
65
71
# flip axis if the display is rotated
66
72
if isinstance (pixel_source , Display ) and (pixel_source .rotation % 180 != 0 ):
67
- return ( pixel_source .height , pixel_source .width )
68
- return ( pixel_source .width , pixel_source .height )
73
+ return pixel_source .height , pixel_source .width
74
+ return pixel_source .width , pixel_source .height
69
75
70
76
71
- def _rgb565_to_bgr_tuple (color ) :
72
- blue = (color << 3 ) & 0x00F8 # extract each of the RGB tripple into it's own byte
77
+ def _rgb565_to_bgr_tuple (color : int ) -> Tuple [ int , int , int ] :
78
+ blue = (color << 3 ) & 0x00F8 # extract each of the RGB triple into it's own byte
73
79
green = (color >> 3 ) & 0x00FC
74
80
red = (color >> 8 ) & 0x00F8
75
- return ( blue , green , red )
81
+ return blue , green , red
76
82
77
83
78
84
# pylint:disable=too-many-locals
79
- def _write_pixels (output_file , pixel_source , palette ):
85
+ def _write_pixels (
86
+ output_file : BufferedWriter ,
87
+ pixel_source : Union [Bitmap , Display ],
88
+ palette : Optional [Palette ],
89
+ ) -> None :
80
90
saving_bitmap = isinstance (pixel_source , Bitmap )
81
91
width , height = _rotated_height_and_width (pixel_source )
82
92
row_buffer = bytearray (_bytes_per_row (width ))
83
93
result_buffer = False
84
94
for y in range (height , 0 , - 1 ):
85
95
buffer_index = 0
86
96
if saving_bitmap :
97
+ # pixel_source: Bitmap
87
98
for x in range (width ):
88
99
pixel = pixel_source [x , y - 1 ]
89
- color = palette [pixel ]
100
+ color = palette [pixel ] # handled by save_pixel's guardians
90
101
for _ in range (3 ):
91
102
row_buffer [buffer_index ] = color & 0xFF
92
103
color >>= 8
93
104
buffer_index += 1
94
105
else :
106
+ # pixel_source: Display
95
107
result_buffer = bytearray (2048 )
96
108
data = pixel_source .fill_row (y - 1 , result_buffer )
97
109
for i in range (width ):
@@ -109,7 +121,11 @@ def _write_pixels(output_file, pixel_source, palette):
109
121
# pylint:enable=too-many-locals
110
122
111
123
112
- def save_pixels (file_or_filename , pixel_source = None , palette = None ):
124
+ def save_pixels (
125
+ file_or_filename : Union [str , BufferedWriter ],
126
+ pixel_source : Union [Display , Bitmap ] = None ,
127
+ palette : Optional [Palette ] = None ,
128
+ ) -> None :
113
129
"""Save pixels to a 24 bit per pixel BMP file.
114
130
If pixel_source if a displayio.Bitmap, save it's pixels through palette.
115
131
If it's a displayio.Display, a palette isn't required.
@@ -119,10 +135,10 @@ def save_pixels(file_or_filename, pixel_source=None, palette=None):
119
135
:param palette: the Palette to use for looking up colors in the bitmap
120
136
"""
121
137
if not pixel_source :
122
- if "DISPLAY" in dir (board ):
123
- pixel_source = board .DISPLAY
124
- else :
138
+ if not hasattr (board , "DISPLAY" ):
125
139
raise ValueError ("Second argument must be a Bitmap or Display" )
140
+ pixel_source = board .DISPLAY
141
+
126
142
if isinstance (pixel_source , Bitmap ):
127
143
if not isinstance (palette , Palette ):
128
144
raise ValueError ("Third argument must be a Palette for a Bitmap save" )
0 commit comments