Skip to content

Commit ab5ea17

Browse files
authored
Merge pull request #23 from matt-land/add-typing
add type annotations #18
2 parents f3052b8 + d1d8905 commit ab5ea17

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

adafruit_bitmapsaver.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,25 @@
3333
import board
3434
from displayio import Bitmap, Palette, Display
3535

36+
try:
37+
from typing import Tuple, Optional, Union
38+
from io import BufferedWriter
39+
except ImportError:
40+
pass
41+
3642
__version__ = "0.0.0-auto.0"
3743
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BitmapSaver.git"
3844

3945

40-
def _write_bmp_header(output_file, filesize):
46+
def _write_bmp_header(output_file: BufferedWriter, filesize: int) -> None:
4147
output_file.write(bytes("BM", "ascii"))
4248
output_file.write(struct.pack("<I", filesize))
4349
output_file.write(b"\00\x00")
4450
output_file.write(b"\00\x00")
4551
output_file.write(struct.pack("<I", 54))
4652

4753

48-
def _write_dib_header(output_file, width, height):
54+
def _write_dib_header(output_file: BufferedWriter, width: int, height: int) -> None:
4955
output_file.write(struct.pack("<I", 40))
5056
output_file.write(struct.pack("<I", width))
5157
output_file.write(struct.pack("<I", height))
@@ -55,43 +61,49 @@ def _write_dib_header(output_file, width, height):
5561
output_file.write(b"\x00")
5662

5763

58-
def _bytes_per_row(source_width):
64+
def _bytes_per_row(source_width: int) -> int:
5965
pixel_bytes = 3 * source_width
6066
padding_bytes = (4 - (pixel_bytes % 4)) % 4
6167
return pixel_bytes + padding_bytes
6268

6369

64-
def _rotated_height_and_width(pixel_source):
70+
def _rotated_height_and_width(pixel_source: Union[Bitmap, Display]) -> Tuple[int, int]:
6571
# flip axis if the display is rotated
6672
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
6975

7076

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
7379
green = (color >> 3) & 0x00FC
7480
red = (color >> 8) & 0x00F8
75-
return (blue, green, red)
81+
return blue, green, red
7682

7783

7884
# 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:
8090
saving_bitmap = isinstance(pixel_source, Bitmap)
8191
width, height = _rotated_height_and_width(pixel_source)
8292
row_buffer = bytearray(_bytes_per_row(width))
8393
result_buffer = False
8494
for y in range(height, 0, -1):
8595
buffer_index = 0
8696
if saving_bitmap:
97+
# pixel_source: Bitmap
8798
for x in range(width):
8899
pixel = pixel_source[x, y - 1]
89-
color = palette[pixel]
100+
color = palette[pixel] # handled by save_pixel's guardians
90101
for _ in range(3):
91102
row_buffer[buffer_index] = color & 0xFF
92103
color >>= 8
93104
buffer_index += 1
94105
else:
106+
# pixel_source: Display
95107
result_buffer = bytearray(2048)
96108
data = pixel_source.fill_row(y - 1, result_buffer)
97109
for i in range(width):
@@ -109,7 +121,11 @@ def _write_pixels(output_file, pixel_source, palette):
109121
# pylint:enable=too-many-locals
110122

111123

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:
113129
"""Save pixels to a 24 bit per pixel BMP file.
114130
If pixel_source if a displayio.Bitmap, save it's pixels through palette.
115131
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):
119135
:param palette: the Palette to use for looking up colors in the bitmap
120136
"""
121137
if not pixel_source:
122-
if "DISPLAY" in dir(board):
123-
pixel_source = board.DISPLAY
124-
else:
138+
if not hasattr(board, "DISPLAY"):
125139
raise ValueError("Second argument must be a Bitmap or Display")
140+
pixel_source = board.DISPLAY
141+
126142
if isinstance(pixel_source, Bitmap):
127143
if not isinstance(palette, Palette):
128144
raise ValueError("Third argument must be a Palette for a Bitmap save")

mypy.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-FileCopyrightText: 2022 Matt Land
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
[mypy]
5+
python_version = 3.7
6+
disallow_untyped_defs = True
7+
disable_error_code = no-redef
8+
exclude = (examples|tests|setup.py|docs)

0 commit comments

Comments
 (0)