Skip to content

Remove reassigning variables with new types, pass Mypy #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion adafruit_imageload/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
# SPDX-FileCopyrightText: 2022 Matt Land
# SPDX-FileCopyrightText: 2022-2023 Matt Land
#
# SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -77,10 +77,15 @@ def load(

return pnm.load(file, header, bitmap=bitmap, palette=palette)
if header.startswith(b"GIF"):
if not bitmap:
raise RuntimeError("bitmap argument required")

from . import gif

return gif.load(file, bitmap=bitmap, palette=palette)
if header.startswith(b"\x89PN"):
if not bitmap:
raise RuntimeError("bitmap argument required")
from . import png

return png.load(file, bitmap=bitmap, palette=palette)
Expand Down
8 changes: 4 additions & 4 deletions adafruit_imageload/bmp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
# SPDX-FileCopyrightText: 2022 Matt Land
# SPDX-FileCopyrightText: 2022-2023 Matt Land
#
# SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -29,9 +29,9 @@
def load(
file: BufferedReader,
*,
bitmap: BitmapConstructor = None,
palette: PaletteConstructor = None
) -> Tuple[Bitmap, Optional[Palette]]:
bitmap: Optional[BitmapConstructor] = None,
palette: Optional[PaletteConstructor] = None
) -> Tuple[Optional[Bitmap], Optional[Palette]]:
"""Loads a bmp image from the open ``file``.

Returns tuple of bitmap object and palette object.
Expand Down
30 changes: 17 additions & 13 deletions adafruit_imageload/bmp/indexed.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
# SPDX-FileCopyrightText: 2022 Matt Land
# SPDX-FileCopyrightText: 2022-2023 Matt Land
#
# SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -43,9 +43,9 @@ def load(
color_depth: int,
compression: int,
*,
bitmap: BitmapConstructor = None,
palette: PaletteConstructor = None,
) -> Tuple[Bitmap, Optional[Palette]]:
bitmap: Optional[BitmapConstructor] = None,
palette: Optional[PaletteConstructor] = None,
) -> Tuple[Optional[Bitmap], Optional[Palette]]:
"""Loads indexed bitmap data into bitmap and palette objects.

:param file file: The open bmp file
Expand All @@ -54,19 +54,24 @@ def load(
:param int data_start: Byte location where the data starts (after headers)
:param int colors: Number of distinct colors in the image
:param int color_depth: Number of bits used to store a value
:param int compression: 0 - none, 1 - 8bit RLE, 2 - 4bit RLE"""
:param int compression: 0 - none, 1 - 8bit RLE, 2 - 4bit RLE
:param BitmapConstructor bitmap: a function that returns a displayio.Bitmap
:param PaletteConstructor palette: a function that returns a displayio.Palette
"""
# pylint: disable=too-many-arguments,too-many-locals,too-many-branches
palette_obj = None
if palette:
palette = palette(colors) # type: Palette
palette_obj = palette(colors)

file.seek(data_start - colors * 4)
for value in range(colors):
c_bytes = file.read(4)
# Need to swap red & blue bytes (bytes 0 and 2)
palette[value] = bytes(
palette_obj[value] = bytes(
b"".join([c_bytes[2:3], c_bytes[1:2], c_bytes[0:1], c_bytes[3:1]])
)

bitmap_obj = None
if bitmap:
minimum_color_depth = 1
while colors > 2**minimum_color_depth:
Expand All @@ -78,7 +83,7 @@ def load(

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

if compression == 0:

if _bitmap_readinto:
_bitmap_readinto(
bitmap,
bitmap_obj,
file,
bits_per_pixel=color_depth,
element_size=4,
Expand All @@ -120,17 +124,17 @@ def load(
pixel = (
chunk[i] >> (8 - color_depth * (x % pixels_per_byte + 1))
) & mask
bitmap[offset + x] = pixel
bitmap_obj[offset + x] = pixel
elif compression in (1, 2):
decode_rle(
bitmap=bitmap,
bitmap=bitmap_obj,
file=file,
compression=compression,
y_range=(range1, range2, range3),
width=width,
)

return bitmap, palette
return bitmap_obj, palette_obj


def decode_rle(
Expand Down
6 changes: 4 additions & 2 deletions adafruit_imageload/gif.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-FileCopyrightText: 2019 Radomir Dopieralski for Adafruit Industries
# SPDX-FileCopyrightText: 2022 Matt Land
# SPDX-FileCopyrightText: 2022-2023 Matt Land
#
# SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -32,7 +32,7 @@ def load(
file: BufferedReader,
*,
bitmap: BitmapConstructor,
palette: PaletteConstructor = None
palette: Optional[PaletteConstructor] = None
) -> Tuple[Bitmap, Optional[Palette]]:
"""Loads a GIF image from the open ``file``.

Expand All @@ -50,6 +50,8 @@ def load(
"<HHBBB", file.read(7)
)
if (flags & 0x80) != 0:
if not palette:
raise RuntimeError("palette argument required")
palette_size = 1 << ((flags & 0x07) + 1)
palette_obj = palette(palette_size)
for i in range(palette_size):
Expand Down
32 changes: 21 additions & 11 deletions adafruit_imageload/png.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# SPDX-FileCopyrightText: 2022 Radomir Dopieralski
# SPDX-FileCopyrightText: 2023 Matt Land
#
# SPDX-License-Identifier: MIT

Expand All @@ -9,13 +10,14 @@
Load pixel values (indices or colors) into a bitmap and colors into a palette
from a PNG file.

* Author(s): Radomir Dopieralski
* Author(s): Radomir Dopieralski, Matt Land

"""

try:
# pylint: disable=unused-import
import typing
from io import BufferedReader
from typing import Optional, Tuple
from displayio import Palette, Bitmap
from .displayio_types import PaletteConstructor, BitmapConstructor
except ImportError:
pass
Expand All @@ -28,25 +30,33 @@


def load(
file: str, *, bitmap: BitmapConstructor, palette: PaletteConstructor = None
): # pylint: disable=too-many-locals,too-many-branches
"""Loads a PNG image from the open ``file``.
file: BufferedReader,
*,
bitmap: BitmapConstructor,
palette: Optional[PaletteConstructor] = None
) -> Tuple[Bitmap, Optional[Palette]]:
"""
Loads a PNG image from the open ``file``.

Returns tuple of bitmap object and palette object.

:param file: The *.png file being loaded
:param object bitmap: Type to store bitmap data. Must have API similar to
`displayio.Bitmap`.
:param object palette: Type to store the palette. Must have API similar to
`displayio.Palette`. Will be skipped if None"""
`displayio.Palette`. Will be skipped if None
"""
# pylint: disable=too-many-locals,too-many-branches
header = file.read(8)
if header != b"\x89PNG\r\n\x1a\n":
raise ValueError("Not a PNG file")
del header
data = bytearray()
pal = None
mode = None
depth = None
depth = 0
width = 0
height = 0
while True:
size, chunk = struct.unpack(">I4s", file.read(8))
if chunk == b"IHDR":
Expand Down Expand Up @@ -81,16 +91,16 @@ def load(
else:
file.seek(size, 1) # skip unknown chunks
file.seek(4, 1) # skip CRC
data = zlib.decompress(data)
data_bytes = zlib.decompress(data)
bmp = bitmap(width, height, 1 << depth)
scanline = (width * depth + 7) // 8
mem = memoryview(bmp)
for y in range(height):
dst = y * scanline
src = y * (scanline + 1) + 1
filter_ = data[src - 1]
filter_ = data_bytes[src - 1]
if filter_ == 0:
mem[dst : dst + scanline] = data[src : src + scanline]
mem[dst : dst + scanline] = data_bytes[src : src + scanline]
else:
raise NotImplementedError("Filters not supported")
return bmp, pal
25 changes: 15 additions & 10 deletions adafruit_imageload/pnm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
# SPDX-FileCopyrightText: 2022 Matt Land
# SPDX-FileCopyrightText: 2022-2023 Matt Land
# SPDX-FileCopyrightText: Brooke Storm
# SPDX-FileCopyrightText: Sam McGahan
#
Expand Down Expand Up @@ -40,8 +40,8 @@ def load(
file: BufferedReader,
header: bytes,
*,
bitmap: BitmapConstructor = None,
palette: PaletteConstructor = None
bitmap: Optional[BitmapConstructor] = None,
palette: Optional[PaletteConstructor] = None
) -> Tuple[Optional[Bitmap], Optional[Palette]]:
"""
Scan for netpbm format info, skip over comments, and delegate to a submodule
Expand Down Expand Up @@ -92,19 +92,24 @@ def load(
)

if len(pnm_header) == 2 and magic_number in [b"P1", b"P4"]:
bitmap = bitmap(pnm_header[0], pnm_header[1], 1) # type: Bitmap
if not bitmap:
raise RuntimeError(
"A bitmap constructor is required for this type of pnm format file"
)
bitmap_obj = bitmap(pnm_header[0], pnm_header[1], 1)
palette_obj = None
if palette:
palette = palette(1) # type: Palette
palette[0] = b"\xFF\xFF\xFF"
palette_obj = palette(1)
palette_obj[0] = b"\xFF\xFF\xFF"
if magic_number.startswith(b"P1"):
from . import pbm_ascii

return pbm_ascii.load(
file,
pnm_header[0],
pnm_header[1],
bitmap=bitmap,
palette=palette,
bitmap=bitmap_obj,
palette=palette_obj,
)

from . import pbm_binary
Expand All @@ -113,8 +118,8 @@ def load(
file,
pnm_header[0],
pnm_header[1],
bitmap=bitmap,
palette=palette,
bitmap=bitmap_obj,
palette=palette_obj,
)

next_byte = file.read(1)
Expand Down
4 changes: 2 additions & 2 deletions adafruit_imageload/pnm/pbm_ascii.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
# SPDX-FileCopyrightText: 2022 Matt Land
# SPDX-FileCopyrightText: 2022-2023 Matt Land
# SPDX-FileCopyrightText: Brooke Storm
# SPDX-FileCopyrightText: Sam McGahan
#
Expand Down Expand Up @@ -32,7 +32,7 @@ def load(
width: int,
height: int,
bitmap: Bitmap,
palette: Palette = None,
palette: Optional[Palette] = None,
) -> Tuple[Bitmap, Optional[Palette]]:
"""
Load a P1 'PBM' ascii image into the displayio.Bitmap
Expand Down
4 changes: 2 additions & 2 deletions adafruit_imageload/pnm/pbm_binary.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
# SPDX-FileCopyrightText: 2022 Matt Land
# SPDX-FileCopyrightText: 2022-2023 Matt Land
# SPDX-FileCopyrightText: Brooke Storm
# SPDX-FileCopyrightText: Sam McGahan
#
Expand Down Expand Up @@ -31,7 +31,7 @@ def load(
width: int,
height: int,
bitmap: Bitmap,
palette: Palette = None,
palette: Optional[Palette] = None,
) -> Tuple[Bitmap, Optional[Palette]]:
"""
Load a P4 'PBM' binary image into the Bitmap
Expand Down
6 changes: 3 additions & 3 deletions adafruit_imageload/pnm/pgm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
# SPDX-FileCopyrightText: 2022 Matt Land
# SPDX-FileCopyrightText: 2022-2023 Matt Land
# SPDX-FileCopyrightText: Brooke Storm
# SPDX-FileCopyrightText: Sam McGahan
#
Expand Down Expand Up @@ -29,8 +29,8 @@ def load(
magic_number: bytes,
header: List[int],
*,
bitmap: BitmapConstructor = None,
palette: PaletteConstructor = None
bitmap: Optional[BitmapConstructor] = None,
palette: Optional[PaletteConstructor] = None
) -> Tuple[Optional[Bitmap], Optional[Palette]]:
"""
Perform the load of Netpbm greyscale images (P2, P5)
Expand Down
17 changes: 9 additions & 8 deletions adafruit_imageload/pnm/pgm/ascii.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
# SPDX-FileCopyrightText: 2022 Matt Land
# SPDX-FileCopyrightText: 2022-2023 Matt Land
# SPDX-FileCopyrightText: Brooke Storm
# SPDX-FileCopyrightText: Sam McGahan
#
Expand Down Expand Up @@ -27,8 +27,8 @@ def load(
file: BufferedReader,
width: int,
height: int,
bitmap: BitmapConstructor = None,
palette: PaletteConstructor = None,
bitmap: Optional[BitmapConstructor] = None,
palette: Optional[PaletteConstructor] = None,
) -> Tuple[Optional[Bitmap], Optional[Palette]]:
"""
Load a PGM ascii file (P2)
Expand All @@ -46,11 +46,12 @@ def load(
_palette_colors.add(int_pixel)
pixel = bytearray()
pixel += byte
palette_obj = None
if palette:
palette = build_palette(palette, _palette_colors) # type: Palette
palette_obj = build_palette(palette, _palette_colors)
bitmap_obj = None
if bitmap:
bitmap = bitmap(width, height, len(_palette_colors)) # type: Bitmap
_palette_colors = list(_palette_colors)
bitmap_obj = bitmap(width, height, len(_palette_colors))
file.seek(data_start)
for y in range(height):
for x in range(width):
Expand All @@ -61,8 +62,8 @@ def load(
break
pixel += byte
int_pixel = int("".join(["%c" % char for char in pixel]))
bitmap[x, y] = _palette_colors.index(int_pixel)
return bitmap, palette
bitmap_obj[x, y] = list(_palette_colors).index(int_pixel)
return bitmap_obj, palette_obj


def build_palette(
Expand Down
Loading