Skip to content

Ran black, updated to pylint 2.x #27

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 1 commit into from
Mar 16, 2020
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
source actions-ci/install.sh
- name: Pip install pylint, black, & Sphinx
run: |
pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme
pip install --force-reinstall pylint black==19.10b0 Sphinx sphinx-rtd-theme
- name: Library version
run: git describe --dirty --always --tags
- name: PyLint
Expand Down
5 changes: 5 additions & 0 deletions adafruit_imageload/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
* Author(s): Scott Shawcroft

"""
# pylint: disable=import-outside-toplevel

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"


def load(filename, *, bitmap=None, palette=None):
"""Load pixel values (indices or colors) into a bitmap and colors into a palette.

Expand All @@ -46,11 +48,14 @@ def load(filename, *, bitmap=None, palette=None):
file.seek(0)
if header.startswith(b"BM"):
from . import bmp

return bmp.load(file, bitmap=bitmap, palette=palette)
if header.startswith(b"P"):
from . import pnm

return pnm.load(file, header, bitmap=bitmap, palette=palette)
if header.startswith(b"GIF"):
from . import gif

return gif.load(file, bitmap=bitmap, palette=palette)
raise RuntimeError("Unsupported image format")
36 changes: 24 additions & 12 deletions adafruit_imageload/bmp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
* Author(s): Scott Shawcroft

"""
# pylint: disable=import-outside-toplevel

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"


def load(file, *, bitmap=None, palette=None):
"""Loads a bmp image from the open ``file``.

Expand All @@ -42,19 +44,19 @@ def load(file, *, bitmap=None, palette=None):
:param object palette: Type to store the palette. Must have API similar to
`displayio.Palette`. Will be skipped if None"""
file.seek(10)
data_start = int.from_bytes(file.read(4), 'little')
data_start = int.from_bytes(file.read(4), "little")
# f.seek(14)
# bmp_header_length = int.from_bytes(file.read(4), 'little')
# print(bmp_header_length)
file.seek(0x12) # Width of the bitmap in pixels
width = int.from_bytes(file.read(4), 'little')
height = int.from_bytes(file.read(4), 'little')
file.seek(0x1c) # Number of bits per pixel
color_depth = int.from_bytes(file.read(2), 'little')
file.seek(0x1e) # Compression type
compression = int.from_bytes(file.read(2), 'little')
file.seek(0x2e) # Number of colors in the color palette
colors = int.from_bytes(file.read(4), 'little')
file.seek(0x12) # Width of the bitmap in pixels
width = int.from_bytes(file.read(4), "little")
height = int.from_bytes(file.read(4), "little")
file.seek(0x1C) # Number of bits per pixel
color_depth = int.from_bytes(file.read(2), "little")
file.seek(0x1E) # Compression type
compression = int.from_bytes(file.read(2), "little")
file.seek(0x2E) # Number of colors in the color palette
colors = int.from_bytes(file.read(4), "little")

if colors == 0 and color_depth >= 16:
raise NotImplementedError("True color BMP unsupported")
Expand All @@ -65,5 +67,15 @@ def load(file, *, bitmap=None, palette=None):
if colors == 0:
colors = 2 ** color_depth
from . import indexed
return indexed.load(file, width, height, data_start, colors, color_depth,
compression, bitmap=bitmap, palette=palette)

return indexed.load(
file,
width,
height,
data_start,
colors,
color_depth,
compression,
bitmap=bitmap,
palette=palette,
)
42 changes: 26 additions & 16 deletions adafruit_imageload/bmp/indexed.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,19 @@
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"

def load(file, width, height, data_start, colors, color_depth, compression, *,
bitmap=None, palette=None):

def load(
file,
width,
height,
data_start,
colors,
color_depth,
compression,
*,
bitmap=None,
palette=None
):
"""Loads indexed bitmap data into bitmap and palette objects.

:param file file: The open bmp file
Expand All @@ -51,26 +62,25 @@ def load(file, width, height, data_start, colors, color_depth, compression, *,
for value in range(colors):
c_bytes = file.read(4)
# Need to swap red & blue bytes (bytes 0 and 2)
palette[value] = bytes(b''.join([c_bytes[2:3],
c_bytes[1:2],
c_bytes[0:1],
c_bytes[3:1]]))
palette[value] = bytes(
b"".join([c_bytes[2:3], c_bytes[1:2], c_bytes[0:1], c_bytes[3:1]])
)

if bitmap:
minimum_color_depth = 1
while colors > 2 ** minimum_color_depth:
minimum_color_depth *= 2

#convert unsigned int to signed int when height is negative
if height > 0x7fffffff:
# convert unsigned int to signed int when height is negative
if height > 0x7FFFFFFF:
height = height - 4294967296
bitmap = bitmap(width, abs(height), colors)
file.seek(data_start)
line_size = width // (8 // color_depth)
if width % (8 // color_depth) != 0:
line_size += 1
if line_size % 4 != 0:
line_size += (4 - line_size % 4)
line_size += 4 - line_size % 4

mask = (1 << minimum_color_depth) - 1
if height > 0:
Expand All @@ -91,18 +101,22 @@ def load(file, width, height, data_start, colors, color_depth, compression, *,

for x in range(width):
i = x // pixels_per_byte
pixel = (chunk[i] >> (8 - color_depth*(x % pixels_per_byte + 1))) & mask
pixel = (
chunk[i] >> (8 - color_depth * (x % pixels_per_byte + 1))
) & mask
bitmap[offset + x] = pixel
elif compression in (1, 2):
decode_rle(
bitmap=bitmap,
file=file,
compression=compression,
y_range=(range1, range2, range3),
width=width)
width=width,
)

return bitmap, palette


def decode_rle(bitmap, file, compression, y_range, width):
"""Helper to decode RLE images"""
# pylint: disable=too-many-locals,too-many-nested-blocks,too-many-branches
Expand Down Expand Up @@ -218,16 +232,12 @@ def decode_rle(bitmap, file, compression, y_range, width):
# 0xab 3 times, the output pixel values would be: 0x0a
# 0x0b 0x0a (notice how it ends at 0x0a) rather than
# 0x0a 0x0b 0x0a 0x0b 0x0a 0x0b
run_values = [
run_buf[1] >> 4,
run_buf[1] & 0x0F
]
run_values = [run_buf[1] >> 4, run_buf[1] & 0x0F]
for i in range(0, min(run_length_px, width_remaining)):
bitmap[offset + i] = run_values[i % 2]
else:
run_value = run_buf[1]
for i in range(0, min(run_length_px, width_remaining)):
bitmap[offset + i] = run_value


x = x + run_length_px
33 changes: 18 additions & 15 deletions adafruit_imageload/gif.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def load(file, *, bitmap=None, palette=None):
:param object palette: Type to store the palette. Must have API similar to
`displayio.Palette`. Will be skipped if None"""
header = file.read(6)
if header not in {b'GIF87a', b'GIF89a'}:
if header not in {b"GIF87a", b"GIF89a"}:
raise ValueError("Not a GIF file")
width, height, flags, _, _ = struct.unpack('<HHBBB', file.read(7))
width, height, flags, _, _ = struct.unpack("<HHBBB", file.read(7))
if (flags & 0x80) != 0:
palette_size = 1 << ((flags & 0x07) + 1)
palette_obj = palette(palette_size)
Expand All @@ -61,13 +61,13 @@ def load(file, *, bitmap=None, palette=None):
bitmap_obj = bitmap(width, height, (1 << color_bits) - 1)
while True:
block_type = file.read(1)[0]
if block_type == 0x2c: # frame
if block_type == 0x2C: # frame
_read_frame(file, bitmap_obj)
elif block_type == 0x21: # extension
elif block_type == 0x21: # extension
_ = file.read(1)[0]
# 0x01 = label, 0xfe = comment
_ = bytes(_read_blockstream(file))
elif block_type == 0x3b: # terminator
elif block_type == 0x3B: # terminator
break
else:
raise ValueError("Bad block type")
Expand All @@ -76,7 +76,7 @@ def load(file, *, bitmap=None, palette=None):

def _read_frame(file, bitmap):
"""Read a signle frame and apply it to the bitmap."""
ddx, ddy, width, _, flags = struct.unpack('<HHHHB', file.read(9))
ddx, ddy, width, _, flags = struct.unpack("<HHHHB", file.read(9))
if (flags & 0x40) != 0:
raise NotImplementedError("Interlacing not supported")
if (flags & 0x80) != 0:
Expand Down Expand Up @@ -111,6 +111,7 @@ class EndOfData(Exception):

class LZWDict:
"""A dictionary of LZW codes."""

def __init__(self, code_size):
self.code_size = code_size
self.clear_code = 1 << code_size
Expand All @@ -121,27 +122,29 @@ def __init__(self, code_size):

def clear(self):
"""Reset the dictionary to default codes."""
self.last = b''
self.last = b""
self.code_len = self.code_size + 1
self.codes[:] = []

def decode(self, code):
"""Decode a code."""
if code == self.clear_code:
self.clear()
return b''
elif code == self.end_code:
return b""
if code == self.end_code:
raise EndOfData()
elif code < self.clear_code:
if code < self.clear_code:
value = bytes([code])
elif code <= len(self.codes) + self.end_code:
value = self.codes[code - self.end_code - 1]
else:
value = self.last + self.last[0:1]
if self.last:
self.codes.append(self.last + value[0:1])
if (len(self.codes) + self.end_code + 1 >= 1 << self.code_len and
self.code_len < 12):
if (
len(self.codes) + self.end_code + 1 >= 1 << self.code_len
and self.code_len < 12
):
self.code_len += 1
self.last = value
return value
Expand All @@ -151,7 +154,7 @@ def lzw_decode(data, code_size):
"""Decode LZW-compressed data."""
dictionary = LZWDict(code_size)
bit = 0
byte = next(data) # pylint: disable=stop-iteration-return
byte = next(data) # pylint: disable=stop-iteration-return
try:
while True:
code = 0
Expand All @@ -160,8 +163,8 @@ def lzw_decode(data, code_size):
bit += 1
if bit >= 8:
bit = 0
byte = next(data) # pylint: disable=stop-iteration-return
byte = next(data) # pylint: disable=stop-iteration-return
yield dictionary.decode(code)
except EndOfData:
while True:
next(data) # pylint: disable=stop-iteration-return
next(data) # pylint: disable=stop-iteration-return
1 change: 1 addition & 0 deletions adafruit_imageload/pnm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* Author(s): Matt Land, Brooke Storm, Sam McGahan

"""
# pylint: disable=import-outside-toplevel

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
Expand Down
1 change: 1 addition & 0 deletions adafruit_imageload/pnm/pgm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* Author(s): Matt Land, Brooke Storm, Sam McGahan

"""
# pylint: disable=import-outside-toplevel


def load(file, magic_number, header, *, bitmap=None, palette=None):
Expand Down
Loading