Skip to content

Commit 378e8e2

Browse files
committed
work for adafruit#59 to remove changing types of objects when reassigning
1 parent 0115251 commit 378e8e2

File tree

11 files changed

+95
-69
lines changed

11 files changed

+95
-69
lines changed

adafruit_imageload/bmp/indexed.py

Lines changed: 16 additions & 12 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,8 +43,8 @@ def load(
4343
color_depth: int,
4444
compression: int,
4545
*,
46-
bitmap: BitmapConstructor = None,
47-
palette: PaletteConstructor = None,
46+
bitmap: Optional[BitmapConstructor] = None,
47+
palette: Optional[PaletteConstructor] = None,
4848
) -> Tuple[Bitmap, Optional[Palette]]:
4949
"""Loads indexed bitmap data into bitmap and palette objects.
5050
@@ -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: Optional[Palette] = 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: Optional[Bitmap] = 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: 2 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

adafruit_imageload/png.py

Lines changed: 17 additions & 7 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
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":

adafruit_imageload/pnm/__init__.py

Lines changed: 10 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,20 @@ 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+
bitmap_obj = bitmap(pnm_header[0], pnm_header[1], 1)
96+
palette_obj: Optional[Palette] = None
9697
if palette:
97-
palette = palette(1) # type: Palette
98-
palette[0] = b"\xFF\xFF\xFF"
98+
palette_obj = palette(1)
99+
palette_obj[0] = b"\xFF\xFF\xFF"
99100
if magic_number.startswith(b"P1"):
100101
from . import pbm_ascii
101102

102103
return pbm_ascii.load(
103104
file,
104105
pnm_header[0],
105106
pnm_header[1],
106-
bitmap=bitmap,
107-
palette=palette,
107+
bitmap=bitmap_obj,
108+
palette=palette_obj,
108109
)
109110

110111
from . import pbm_binary
@@ -113,7 +114,7 @@ def load(
113114
file,
114115
pnm_header[0],
115116
pnm_header[1],
116-
bitmap=bitmap,
117+
bitmap=bitmap_obj,
117118
palette=palette,
118119
)
119120

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 & 7 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,10 +46,12 @@ def load(
4646
_palette_colors.add(int_pixel)
4747
pixel = bytearray()
4848
pixel += byte
49+
actual_palette: Optional[Palette] = None
4950
if palette:
50-
palette = build_palette(palette, _palette_colors) # type: Palette
51+
actual_palette = build_palette(palette, _palette_colors)
52+
actual_bitmap: Optional[Bitmap] = None
5153
if bitmap:
52-
bitmap = bitmap(width, height, len(_palette_colors)) # type: Bitmap
54+
actual_bitmap = bitmap(width, height, len(_palette_colors))
5355
_palette_colors = list(_palette_colors)
5456
file.seek(data_start)
5557
for y in range(height):
@@ -61,8 +63,8 @@ def load(
6163
break
6264
pixel += byte
6365
int_pixel = int("".join(["%c" % char for char in pixel]))
64-
bitmap[x, y] = _palette_colors.index(int_pixel)
65-
return bitmap, palette
66+
actual_bitmap[x, y] = _palette_colors.index(int_pixel)
67+
return actual_bitmap, actual_palette
6668

6769

6870
def build_palette(

adafruit_imageload/pnm/pgm/binary.py

Lines changed: 9 additions & 7 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 P5 format file (binary), handle PGM (greyscale)
@@ -40,17 +40,19 @@ def load(
4040
for pixel in data_line:
4141
palette_colors.add(pixel)
4242

43+
actual_palette: Optional[Palette] = None
4344
if palette:
44-
palette = build_palette(palette, palette_colors) # type: Palette
45+
actual_palette = build_palette(palette, palette_colors)
46+
actual_bitmap: Optional[Bitmap] = None
4547
if bitmap:
46-
bitmap = bitmap(width, height, len(palette_colors)) # type: Bitmap
48+
actual_bitmap = bitmap(width, height, len(palette_colors))
4749
palette_colors = list(palette_colors) # type: List[int]
4850
file.seek(data_start)
4951
for y in range(height):
5052
data_line = iter(bytes(file.read(width)))
5153
for x, pixel in enumerate(data_line):
52-
bitmap[x, y] = palette_colors.index(pixel)
53-
return bitmap, palette
54+
actual_bitmap[x, y] = palette_colors.index(pixel)
55+
return actual_bitmap, actual_palette
5456

5557

5658
def build_palette(

adafruit_imageload/pnm/ppm_ascii.py

Lines changed: 10 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
#
@@ -38,8 +38,8 @@ def load(
3838
file: BufferedReader,
3939
width: int,
4040
height: int,
41-
bitmap: BitmapConstructor = None,
42-
palette: PaletteConstructor = None,
41+
bitmap: Optional[BitmapConstructor] = None,
42+
palette: Optional[PaletteConstructor] = None,
4343
) -> Tuple[Optional[Bitmap], Optional[Palette]]:
4444
"""
4545
:param stream file: infile with the position set at start of data
@@ -55,20 +55,22 @@ def load(
5555
for triplet in read_three_colors(file):
5656
palette_colors.add(triplet)
5757

58+
actual_palette: Optional[Palette] = None
5859
if palette:
59-
palette = palette(len(palette_colors)) # type: Palette
60+
actual_palette = palette(len(palette_colors))
6061
for counter, color in enumerate(palette_colors):
61-
palette[counter] = color
62+
actual_palette[counter] = color
63+
actual_bitmap: Optional[Bitmap] = None
6264
if bitmap:
6365
file.seek(data_start)
64-
bitmap = bitmap(width, height, len(palette_colors)) # type: Bitmap
66+
actual_bitmap = bitmap(width, height, len(palette_colors))
6567
palette_colors = list(palette_colors) # type: List[bytes]
6668
for y in range(height):
6769
for x in range(width):
6870
for color in read_three_colors(file):
69-
bitmap[x, y] = palette_colors.index(color)
71+
actual_bitmap[x, y] = palette_colors.index(color)
7072
break # exit the inner generator
71-
return bitmap, palette
73+
return actual_bitmap, actual_palette
7274

7375

7476
def read_three_colors(file: BufferedReader) -> Iterator[bytes]:

0 commit comments

Comments
 (0)