Skip to content

Commit 276466e

Browse files
committed
work for adafruit#59 to remove changing types of objects when reassigning
rename, remove type overrides more implicit types, and non-matching return types mypy caught errors
1 parent 0115251 commit 276466e

File tree

14 files changed

+121
-85
lines changed

14 files changed

+121
-85
lines changed

adafruit_imageload/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
2-
# SPDX-FileCopyrightText: 2022 Matt Land
2+
# SPDX-FileCopyrightText: 2022-2023 Matt Land
33
#
44
# SPDX-License-Identifier: MIT
55

@@ -77,10 +77,15 @@ def load(
7777

7878
return pnm.load(file, header, bitmap=bitmap, palette=palette)
7979
if header.startswith(b"GIF"):
80+
if not bitmap:
81+
raise RuntimeError("bitmap argument required")
82+
8083
from . import gif
8184

8285
return gif.load(file, bitmap=bitmap, palette=palette)
8386
if header.startswith(b"\x89PN"):
87+
if not bitmap:
88+
raise RuntimeError("bitmap argument required")
8489
from . import png
8590

8691
return png.load(file, bitmap=bitmap, palette=palette)

adafruit_imageload/bmp/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
2-
# SPDX-FileCopyrightText: 2022 Matt Land
2+
# SPDX-FileCopyrightText: 2022-2023 Matt Land
33
#
44
# SPDX-License-Identifier: MIT
55

@@ -29,9 +29,9 @@
2929
def load(
3030
file: BufferedReader,
3131
*,
32-
bitmap: BitmapConstructor = None,
33-
palette: PaletteConstructor = None
34-
) -> Tuple[Bitmap, Optional[Palette]]:
32+
bitmap: Optional[BitmapConstructor] = None,
33+
palette: Optional[PaletteConstructor] = None
34+
) -> Tuple[Optional[Bitmap], Optional[Palette]]:
3535
"""Loads a bmp image from the open ``file``.
3636
3737
Returns tuple of bitmap object and palette object.

adafruit_imageload/bmp/indexed.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
2-
# SPDX-FileCopyrightText: 2022 Matt Land
2+
# SPDX-FileCopyrightText: 2022-2023 Matt Land
33
#
44
# SPDX-License-Identifier: MIT
55

@@ -43,9 +43,9 @@ def load(
4343
color_depth: int,
4444
compression: int,
4545
*,
46-
bitmap: BitmapConstructor = None,
47-
palette: PaletteConstructor = None,
48-
) -> Tuple[Bitmap, Optional[Palette]]:
46+
bitmap: Optional[BitmapConstructor] = None,
47+
palette: Optional[PaletteConstructor] = None,
48+
) -> Tuple[Optional[Bitmap], Optional[Palette]]:
4949
"""Loads indexed bitmap data into bitmap and palette objects.
5050
5151
:param file file: The open bmp file
@@ -54,19 +54,24 @@ def load(
5454
:param int data_start: Byte location where the data starts (after headers)
5555
:param int colors: Number of distinct colors in the image
5656
:param int color_depth: Number of bits used to store a value
57-
:param int compression: 0 - none, 1 - 8bit RLE, 2 - 4bit RLE"""
57+
:param int compression: 0 - none, 1 - 8bit RLE, 2 - 4bit RLE
58+
:param BitmapConstructor bitmap: a function that returns a displayio.Bitmap
59+
:param PaletteConstructor palette: a function that returns a displayio.Palette
60+
"""
5861
# pylint: disable=too-many-arguments,too-many-locals,too-many-branches
62+
palette_obj = None
5963
if palette:
60-
palette = palette(colors) # type: Palette
64+
palette_obj = palette(colors)
6165

6266
file.seek(data_start - colors * 4)
6367
for value in range(colors):
6468
c_bytes = file.read(4)
6569
# Need to swap red & blue bytes (bytes 0 and 2)
66-
palette[value] = bytes(
70+
palette_obj[value] = bytes(
6771
b"".join([c_bytes[2:3], c_bytes[1:2], c_bytes[0:1], c_bytes[3:1]])
6872
)
6973

74+
bitmap_obj = None
7075
if bitmap:
7176
minimum_color_depth = 1
7277
while colors > 2**minimum_color_depth:
@@ -78,7 +83,7 @@ def load(
7883

7984
# convert unsigned int to signed int when height is negative
8085
height = negative_height_check(height)
81-
bitmap = bitmap(width, abs(height), colors) # type: Bitmap
86+
bitmap_obj = bitmap(width, abs(height), colors)
8287
file.seek(data_start)
8388
line_size = width // (8 // color_depth)
8489
if width % (8 // color_depth) != 0:
@@ -97,10 +102,9 @@ def load(
97102
range3 = 1
98103

99104
if compression == 0:
100-
101105
if _bitmap_readinto:
102106
_bitmap_readinto(
103-
bitmap,
107+
bitmap_obj,
104108
file,
105109
bits_per_pixel=color_depth,
106110
element_size=4,
@@ -120,17 +124,17 @@ def load(
120124
pixel = (
121125
chunk[i] >> (8 - color_depth * (x % pixels_per_byte + 1))
122126
) & mask
123-
bitmap[offset + x] = pixel
127+
bitmap_obj[offset + x] = pixel
124128
elif compression in (1, 2):
125129
decode_rle(
126-
bitmap=bitmap,
130+
bitmap=bitmap_obj,
127131
file=file,
128132
compression=compression,
129133
y_range=(range1, range2, range3),
130134
width=width,
131135
)
132136

133-
return bitmap, palette
137+
return bitmap_obj, palette_obj
134138

135139

136140
def decode_rle(

adafruit_imageload/gif.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-FileCopyrightText: 2019 Radomir Dopieralski for Adafruit Industries
2-
# SPDX-FileCopyrightText: 2022 Matt Land
2+
# SPDX-FileCopyrightText: 2022-2023 Matt Land
33
#
44
# SPDX-License-Identifier: MIT
55

@@ -32,7 +32,7 @@ def load(
3232
file: BufferedReader,
3333
*,
3434
bitmap: BitmapConstructor,
35-
palette: PaletteConstructor = None
35+
palette: Optional[PaletteConstructor] = None
3636
) -> Tuple[Bitmap, Optional[Palette]]:
3737
"""Loads a GIF image from the open ``file``.
3838
@@ -50,6 +50,8 @@ def load(
5050
"<HHBBB", file.read(7)
5151
)
5252
if (flags & 0x80) != 0:
53+
if not palette:
54+
raise RuntimeError("palette argument required")
5355
palette_size = 1 << ((flags & 0x07) + 1)
5456
palette_obj = palette(palette_size)
5557
for i in range(palette_size):

adafruit_imageload/png.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-FileCopyrightText: 2022 Radomir Dopieralski
2+
# SPDX-FileCopyrightText: 2023 Matt Land
23
#
34
# SPDX-License-Identifier: MIT
45

@@ -9,13 +10,14 @@
910
Load pixel values (indices or colors) into a bitmap and colors into a palette
1011
from a PNG file.
1112
12-
* Author(s): Radomir Dopieralski
13+
* Author(s): Radomir Dopieralski, Matt Land
1314
1415
"""
1516

1617
try:
17-
# pylint: disable=unused-import
18-
import typing
18+
from io import BufferedReader
19+
from typing import Optional, Tuple
20+
from displayio import Palette, Bitmap
1921
from .displayio_types import PaletteConstructor, BitmapConstructor
2022
except ImportError:
2123
pass
@@ -28,17 +30,23 @@
2830

2931

3032
def load(
31-
file: str, *, bitmap: BitmapConstructor, palette: PaletteConstructor = None
32-
): # pylint: disable=too-many-locals,too-many-branches
33-
"""Loads a PNG image from the open ``file``.
33+
file: BufferedReader,
34+
*,
35+
bitmap: BitmapConstructor,
36+
palette: Optional[PaletteConstructor] = None
37+
) -> Tuple[Bitmap, Optional[Palette]]:
38+
"""
39+
Loads a PNG image from the open ``file``.
3440
3541
Returns tuple of bitmap object and palette object.
3642
3743
:param file: The *.png file being loaded
3844
:param object bitmap: Type to store bitmap data. Must have API similar to
3945
`displayio.Bitmap`.
4046
:param object palette: Type to store the palette. Must have API similar to
41-
`displayio.Palette`. Will be skipped if None"""
47+
`displayio.Palette`. Will be skipped if None
48+
"""
49+
# pylint: disable=too-many-locals,too-many-branches
4250
header = file.read(8)
4351
if header != b"\x89PNG\r\n\x1a\n":
4452
raise ValueError("Not a PNG file")
@@ -47,6 +55,8 @@ def load(
4755
pal = None
4856
mode = None
4957
depth = None
58+
width = 0
59+
height = 0
5060
while True:
5161
size, chunk = struct.unpack(">I4s", file.read(8))
5262
if chunk == b"IHDR":
@@ -81,16 +91,16 @@ def load(
8191
else:
8292
file.seek(size, 1) # skip unknown chunks
8393
file.seek(4, 1) # skip CRC
84-
data = zlib.decompress(data)
94+
data_bytes = zlib.decompress(data)
8595
bmp = bitmap(width, height, 1 << depth)
8696
scanline = (width * depth + 7) // 8
8797
mem = memoryview(bmp)
8898
for y in range(height):
8999
dst = y * scanline
90100
src = y * (scanline + 1) + 1
91-
filter_ = data[src - 1]
101+
filter_ = data_bytes[src - 1]
92102
if filter_ == 0:
93-
mem[dst : dst + scanline] = data[src : src + scanline]
103+
mem[dst : dst + scanline] = data_bytes[src : src + scanline]
94104
else:
95105
raise NotImplementedError("Filters not supported")
96106
return bmp, pal

adafruit_imageload/pnm/__init__.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
2-
# SPDX-FileCopyrightText: 2022 Matt Land
2+
# SPDX-FileCopyrightText: 2022-2023 Matt Land
33
# SPDX-FileCopyrightText: Brooke Storm
44
# SPDX-FileCopyrightText: Sam McGahan
55
#
@@ -40,8 +40,8 @@ def load(
4040
file: BufferedReader,
4141
header: bytes,
4242
*,
43-
bitmap: BitmapConstructor = None,
44-
palette: PaletteConstructor = None
43+
bitmap: Optional[BitmapConstructor] = None,
44+
palette: Optional[PaletteConstructor] = None
4545
) -> Tuple[Optional[Bitmap], Optional[Palette]]:
4646
"""
4747
Scan for netpbm format info, skip over comments, and delegate to a submodule
@@ -92,19 +92,24 @@ def load(
9292
)
9393

9494
if len(pnm_header) == 2 and magic_number in [b"P1", b"P4"]:
95-
bitmap = bitmap(pnm_header[0], pnm_header[1], 1) # type: Bitmap
95+
if not bitmap:
96+
raise RuntimeError(
97+
"A bitmap constructor is required for this type of pnm format file"
98+
)
99+
bitmap_obj = bitmap(pnm_header[0], pnm_header[1], 1)
100+
palette_obj = None
96101
if palette:
97-
palette = palette(1) # type: Palette
98-
palette[0] = b"\xFF\xFF\xFF"
102+
palette_obj = palette(1)
103+
palette_obj[0] = b"\xFF\xFF\xFF"
99104
if magic_number.startswith(b"P1"):
100105
from . import pbm_ascii
101106

102107
return pbm_ascii.load(
103108
file,
104109
pnm_header[0],
105110
pnm_header[1],
106-
bitmap=bitmap,
107-
palette=palette,
111+
bitmap=bitmap_obj,
112+
palette=palette_obj,
108113
)
109114

110115
from . import pbm_binary
@@ -113,7 +118,7 @@ def load(
113118
file,
114119
pnm_header[0],
115120
pnm_header[1],
116-
bitmap=bitmap,
121+
bitmap=bitmap_obj,
117122
palette=palette,
118123
)
119124

adafruit_imageload/pnm/pbm_ascii.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
2-
# SPDX-FileCopyrightText: 2022 Matt Land
2+
# SPDX-FileCopyrightText: 2022-2023 Matt Land
33
# SPDX-FileCopyrightText: Brooke Storm
44
# SPDX-FileCopyrightText: Sam McGahan
55
#
@@ -32,7 +32,7 @@ def load(
3232
width: int,
3333
height: int,
3434
bitmap: Bitmap,
35-
palette: Palette = None,
35+
palette: Optional[Palette] = None,
3636
) -> Tuple[Bitmap, Optional[Palette]]:
3737
"""
3838
Load a P1 'PBM' ascii image into the displayio.Bitmap

adafruit_imageload/pnm/pbm_binary.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
2-
# SPDX-FileCopyrightText: 2022 Matt Land
2+
# SPDX-FileCopyrightText: 2022-2023 Matt Land
33
# SPDX-FileCopyrightText: Brooke Storm
44
# SPDX-FileCopyrightText: Sam McGahan
55
#
@@ -31,7 +31,7 @@ def load(
3131
width: int,
3232
height: int,
3333
bitmap: Bitmap,
34-
palette: Palette = None,
34+
palette: Optional[Palette] = None,
3535
) -> Tuple[Bitmap, Optional[Palette]]:
3636
"""
3737
Load a P4 'PBM' binary image into the Bitmap

adafruit_imageload/pnm/pgm/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
2-
# SPDX-FileCopyrightText: 2022 Matt Land
2+
# SPDX-FileCopyrightText: 2022-2023 Matt Land
33
# SPDX-FileCopyrightText: Brooke Storm
44
# SPDX-FileCopyrightText: Sam McGahan
55
#
@@ -29,8 +29,8 @@ def load(
2929
magic_number: bytes,
3030
header: List[int],
3131
*,
32-
bitmap: BitmapConstructor = None,
33-
palette: PaletteConstructor = None
32+
bitmap: Optional[BitmapConstructor] = None,
33+
palette: Optional[PaletteConstructor] = None
3434
) -> Tuple[Optional[Bitmap], Optional[Palette]]:
3535
"""
3636
Perform the load of Netpbm greyscale images (P2, P5)

adafruit_imageload/pnm/pgm/ascii.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
2-
# SPDX-FileCopyrightText: 2022 Matt Land
2+
# SPDX-FileCopyrightText: 2022-2023 Matt Land
33
# SPDX-FileCopyrightText: Brooke Storm
44
# SPDX-FileCopyrightText: Sam McGahan
55
#
@@ -27,8 +27,8 @@ def load(
2727
file: BufferedReader,
2828
width: int,
2929
height: int,
30-
bitmap: BitmapConstructor = None,
31-
palette: PaletteConstructor = None,
30+
bitmap: Optional[BitmapConstructor] = None,
31+
palette: Optional[PaletteConstructor] = None,
3232
) -> Tuple[Optional[Bitmap], Optional[Palette]]:
3333
"""
3434
Load a PGM ascii file (P2)
@@ -46,11 +46,12 @@ def load(
4646
_palette_colors.add(int_pixel)
4747
pixel = bytearray()
4848
pixel += byte
49+
palette_obj = None
4950
if palette:
50-
palette = build_palette(palette, _palette_colors) # type: Palette
51+
palette_obj = build_palette(palette, _palette_colors)
52+
bitmap_obj = None
5153
if bitmap:
52-
bitmap = bitmap(width, height, len(_palette_colors)) # type: Bitmap
53-
_palette_colors = list(_palette_colors)
54+
bitmap_obj = bitmap(width, height, len(_palette_colors))
5455
file.seek(data_start)
5556
for y in range(height):
5657
for x in range(width):
@@ -61,8 +62,8 @@ def load(
6162
break
6263
pixel += byte
6364
int_pixel = int("".join(["%c" % char for char in pixel]))
64-
bitmap[x, y] = _palette_colors.index(int_pixel)
65-
return bitmap, palette
65+
bitmap_obj[x, y] = list(_palette_colors).index(int_pixel)
66+
return bitmap_obj, palette_obj
6667

6768

6869
def build_palette(

0 commit comments

Comments
 (0)