35
35
36
36
try :
37
37
from typing import Tuple , Optional , Union
38
- from io import BufferedWriter
38
+ from io import WriteableBuffer
39
39
except ImportError :
40
40
pass
41
41
42
42
__version__ = "0.0.0-auto.0"
43
43
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BitmapSaver.git"
44
44
45
45
46
- def _write_bmp_header (output_file : BufferedWriter , filesize : int ) -> None :
46
+ def _write_bmp_header (output_file : WriteableBuffer , filesize : int ) -> None :
47
47
output_file .write (bytes ("BM" , "ascii" ))
48
48
output_file .write (struct .pack ("<I" , filesize ))
49
49
output_file .write (b"\00 \x00 " )
50
50
output_file .write (b"\00 \x00 " )
51
51
output_file .write (struct .pack ("<I" , 54 ))
52
52
53
53
54
- def _write_dib_header (output_file : BufferedWriter , width : int , height : int ) -> None :
54
+ def _write_dib_header (output_file : WriteableBuffer , width : int , height : int ) -> None :
55
55
output_file .write (struct .pack ("<I" , 40 ))
56
56
output_file .write (struct .pack ("<I" , width ))
57
57
output_file .write (struct .pack ("<I" , height ))
@@ -83,7 +83,7 @@ def _rgb565_to_bgr_tuple(color: int) -> Tuple[int, int, int]:
83
83
84
84
# pylint:disable=too-many-locals
85
85
def _write_pixels (
86
- output_file : BufferedWriter ,
86
+ output_file : WriteableBuffer ,
87
87
pixel_source : Union [Bitmap , Display ],
88
88
palette : Optional [Palette ],
89
89
) -> None :
@@ -95,16 +95,16 @@ def _write_pixels(
95
95
if saving_bitmap :
96
96
# pixel_source: Bitmap
97
97
for x in range (width ):
98
- pixel = pixel_source [x , y - 1 ] # type: ignore
99
- color = palette [pixel ] # type: ignore # handled by save_pixel's guardians
98
+ pixel = pixel_source [x , y - 1 ]
99
+ color = palette [pixel ] # handled by save_pixel's guardians
100
100
for _ in range (3 ):
101
101
row_buffer [buffer_index ] = color & 0xFF
102
102
color >>= 8
103
103
buffer_index += 1
104
104
else :
105
105
# pixel_source: Display
106
106
result_buffer = bytearray (2048 )
107
- data = pixel_source .fill_row (y - 1 , result_buffer ) # type: ignore
107
+ data = pixel_source .fill_row (y - 1 , result_buffer )
108
108
for i in range (width ):
109
109
pixel565 = (data [i * 2 ] << 8 ) + data [i * 2 + 1 ]
110
110
for b in _rgb565_to_bgr_tuple (pixel565 ):
@@ -120,7 +120,7 @@ def _write_pixels(
120
120
121
121
122
122
def save_pixels (
123
- file_or_filename : Union [str , BufferedWriter ],
123
+ file_or_filename : Union [str , WriteableBuffer ],
124
124
pixel_source : Union [Display , Bitmap ] = None ,
125
125
palette : Palette = None ,
126
126
) -> None :
@@ -133,7 +133,7 @@ def save_pixels(
133
133
:param palette: the Palette to use for looking up colors in the bitmap
134
134
"""
135
135
if not pixel_source :
136
- if "DISPLAY" not in dir (board ):
136
+ if not hasattr (board , "DISPLAY" ):
137
137
raise ValueError ("Second argument must be a Bitmap or Display" )
138
138
pixel_source = board .DISPLAY
139
139
0 commit comments