Skip to content

Commit 3d00b42

Browse files
committed
Lint!
1 parent 6463092 commit 3d00b42

File tree

3 files changed

+45
-31
lines changed

3 files changed

+45
-31
lines changed

adafruit_imageload/__init__.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ def load(filename, *, bitmap=None, palette=None):
4141
palette is the desired pallete type. The constructor should take the number of colors and
4242
support assignment to indices via [].
4343
"""
44-
with open(filename, "rb") as f:
45-
header = f.read(3)
44+
with open(filename, "rb") as file:
45+
header = file.read(3)
4646
if header.startswith(b"BM"):
4747
from . import bmp
48-
f.seek(0)
49-
return bmp.load(f, bitmap=bitmap, palette=palette)
50-
else:
51-
raise RuntimeError("Unsupported image format")
48+
file.seek(0)
49+
return bmp.load(file, bitmap=bitmap, palette=palette)
50+
raise RuntimeError("Unsupported image format")

adafruit_imageload/bmp/__init__.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,33 @@
3232
__version__ = "0.0.0-auto.0"
3333
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
3434

35-
def load(f, *, bitmap=None, palette=None):
36-
f.seek(10)
37-
data_start = int.from_bytes(f.read(4), 'little')
35+
def load(file, *, bitmap=None, palette=None):
36+
"""Loads a bmp image from the open ``file``.
37+
38+
Returns tuple of bitmap object and palette object.
39+
40+
:param object bitmap: Type to store bitmap data. Must have API similar to `displayio.Bitmap`.
41+
Will be skipped if None
42+
:param object palette: Type to store the palette. Must have API similar to
43+
`displayio.Palette`. Will be skipped if None"""
44+
file.seek(10)
45+
data_start = int.from_bytes(file.read(4), 'little')
3846
# f.seek(14)
39-
# bmp_header_length = int.from_bytes(f.read(4), 'little')
47+
# bmp_header_length = int.from_bytes(file.read(4), 'little')
4048
# print(bmp_header_length)
41-
f.seek(18)
42-
width = int.from_bytes(f.read(4), 'little')
43-
height = int.from_bytes(f.read(4), 'little')
44-
f.seek(28)
45-
color_depth = int.from_bytes(f.read(2), 'little')
46-
f.seek(46)
47-
colors = int.from_bytes(f.read(4), 'little')
48-
49-
compute_palette = False
49+
file.seek(18)
50+
width = int.from_bytes(file.read(4), 'little')
51+
height = int.from_bytes(file.read(4), 'little')
52+
file.seek(28)
53+
color_depth = int.from_bytes(file.read(2), 'little')
54+
file.seek(46)
55+
colors = int.from_bytes(file.read(4), 'little')
56+
5057
if colors == 0 and color_depth >= 16:
5158
raise NotImplementedError("True color BMP unsupported")
5259
else:
5360
if colors == 0:
5461
colors = 2 ** color_depth
5562
from . import indexed
56-
return indexed.load(f, width, height, data_start, colors, color_depth, bitmap=bitmap, palette=palette)
63+
return indexed.load(file, width, height, data_start, colors, color_depth, bitmap=bitmap,
64+
palette=palette)

adafruit_imageload/bmp/indexed.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,45 @@
3232
__version__ = "0.0.0-auto.0"
3333
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
3434

35-
import math
35+
def load(file, width, height, data_start, colors, color_depth, *, bitmap=None, palette=None):
36+
"""Loads indexed bitmap data into bitmap and palette objects.
3637
37-
def load(f, width, height, data_start, colors, color_depth, *, bitmap=None, palette=None):
38+
:param file file: The open bmp file
39+
:param int width: Image width in pixels
40+
:param int height: Image height in pixels
41+
:param int data_start: Byte location where the data starts (after headers)
42+
:param int colors: Number of distinct colors in the image
43+
:param int color_depth: Number of bits used to store a value"""
44+
# pylint: disable=too-many-arguments,too-many-locals
3845
if palette:
3946
palette = palette(colors)
4047

41-
f.seek(data_start - colors * 4)
42-
for color in range(colors):
43-
c = f.read(4)
44-
palette[color] = c
48+
file.seek(data_start - colors * 4)
49+
for value in range(colors):
50+
palette[value] = file.read(4)
4551

4652
if bitmap:
4753
minimum_color_depth = 1
4854
while colors > 2 ** minimum_color_depth:
4955
minimum_color_depth *= 2
5056

5157
bitmap = bitmap(width, height, colors)
52-
f.seek(data_start)
58+
file.seek(data_start)
5359
line_size = width // (8 // color_depth)
5460
if line_size % 4 != 0:
5561
line_size += (4 - line_size % 4)
5662

5763
chunk = bytearray(line_size)
64+
mask = (1 << minimum_color_depth) - 1
5865

59-
for y in range(height-1,-1,-1):
60-
f.readinto(chunk)
66+
for y in range(height - 1, -1, -1):
67+
file.readinto(chunk)
6168
pixels_per_byte = 8 // color_depth
6269
offset = y * width
6370

6471
for x in range(width):
65-
ci = x // pixels_per_byte
66-
pixel = (chunk[ci] >> (8 - color_depth*(x % pixels_per_byte + 1))) & ((1 << minimum_color_depth) - 1)
72+
i = x // pixels_per_byte
73+
pixel = (chunk[i] >> (8 - color_depth*(x % pixels_per_byte + 1))) & mask
6774
bitmap[offset + x] = pixel
6875

6976
return bitmap, palette

0 commit comments

Comments
 (0)