Skip to content

Commit 11da972

Browse files
author
Matt Land
committed
ignore test folder for pylint
1 parent 1f0afd6 commit 11da972

6 files changed

Lines changed: 156 additions & 71 deletions

File tree

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extension-pkg-whitelist=
77

88
# Add files or directories to the blacklist. They should be base names, not
99
# paths.
10-
ignore=CVS
10+
ignore=CVS,adafruit_imageload/tests
1111

1212
# Add files or directories matching the regex patterns to the blacklist. The
1313
# regex matches against base names, not paths.

adafruit_imageload/pnm/pbm_binary.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,8 @@ def load(file, width, height, bitmap=None, palette=None):
5656

5757

5858
def iterbits(b):
59+
"""
60+
generator to iterate over the bits in a byte (character)
61+
"""
5962
for i in range(8):
6063
yield (b >> i) & 1

adafruit_imageload/pnm/pgm/__init__.py

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -32,78 +32,19 @@
3232

3333
def load(file, magic_number, header, *, bitmap=None, palette=None):
3434
"""
35-
Perform the load of Netpbm greyscale images
36-
:param file: stream resource with the pointer at the start of data
37-
:param magic_number: string P2 or P6
38-
:param header: list of width, height, max color value
39-
:param bitmap: displayio.Bitmap class object
40-
:param palette: displayio.Palette class object
41-
:return:
35+
Perform the load of Netpbm greyscale images (P2, P5)
4236
"""
4337
if header[2] > 256:
4438
raise NotImplementedError("16 bit files are not supported")
4539
width = header[0]
4640
height = header[1]
4741

48-
data_start = file.tell() # keep this so we can rewind
49-
palette_colors = set()
50-
5142
if magic_number == b"P2": # To handle ascii PGM files.
52-
pixel = bytearray()
53-
# build a set of all colors present in the file, so palette and bitmap can be constructed
54-
while True:
55-
byte = file.read(1)
56-
if byte == b"":
57-
break
58-
if not byte.isdigit():
59-
int_pixel = int("".join(["%c" % char for char in pixel]))
60-
palette_colors.add(int_pixel)
61-
pixel = bytearray()
62-
pixel += byte
63-
if palette:
64-
palette = build_palette(palette, palette_colors)
65-
if bitmap:
66-
bitmap = bitmap(width, height, len(palette_colors))
67-
palette_colors = list(palette_colors)
68-
file.seek(data_start)
69-
for y in range(height):
70-
for x in range(width):
71-
pixel = bytearray()
72-
while True:
73-
byte = file.read(1)
74-
if not byte.isdigit():
75-
break
76-
pixel += byte
77-
int_pixel = int("".join(["%c" % char for char in pixel]))
78-
bitmap[x, y] = palette_colors.index(int_pixel)
79-
return bitmap, palette
43+
from . import ascii as pgm_ascii
44+
return pgm_ascii.load(file, width, height, bitmap=bitmap, palette=palette)
8045

8146
if magic_number == b"P5": # To handle binary PGM files.
82-
for y in range(height):
83-
data_line = iter(bytes(file.read(width)))
84-
for pixel in data_line:
85-
palette_colors.add(pixel)
86-
87-
if palette:
88-
palette = build_palette(palette, palette_colors)
89-
if bitmap:
90-
bitmap = bitmap(width, height, len(palette_colors))
91-
palette_colors = list(palette_colors)
92-
file.seek(data_start)
93-
for y in range(height):
94-
data_line = iter(bytes(file.read(width)))
95-
for x, pixel in enumerate(data_line):
96-
bitmap[x, y] = palette_colors.index(pixel)
97-
return bitmap, palette
47+
from . import binary
48+
return binary.load(file, width, height, bitmap=bitmap, palette=palette)
9849

9950
raise NotImplementedError("Was not able to send image")
100-
101-
102-
def build_palette(palette_class, palette_colors):
103-
"""
104-
construct the Palette, and populate it with the set of palette_colors
105-
"""
106-
palette = palette_class(len(palette_colors))
107-
for counter, color in enumerate(palette_colors):
108-
palette[counter] = bytes([color, color, color])
109-
return palette
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2018 Scott Shawcroft for Adafruit Industries LLC
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
`adafruit_imageload.pnm.pgm.ascii`
24+
====================================================
25+
26+
Load pixel values (indices or colors) into a bitmap and colors into a palette.
27+
28+
* Author(s): Matt Land, Brooke Storm, Sam McGahan
29+
30+
"""
31+
32+
33+
def load(file, width, height, bitmap=None, palette=None):
34+
"""
35+
Load a PGM ascii file (P2)
36+
"""
37+
data_start = file.tell() # keep this so we can rewind
38+
palette_colors = set()
39+
pixel = bytearray()
40+
# build a set of all colors present in the file, so palette and bitmap can be constructed
41+
while True:
42+
byte = file.read(1)
43+
if byte == b"":
44+
break
45+
if not byte.isdigit():
46+
int_pixel = int("".join(["%c" % char for char in pixel]))
47+
palette_colors.add(int_pixel)
48+
pixel = bytearray()
49+
pixel += byte
50+
if palette:
51+
palette = build_palette(palette, palette_colors)
52+
if bitmap:
53+
bitmap = bitmap(width, height, len(palette_colors))
54+
palette_colors = list(palette_colors)
55+
file.seek(data_start)
56+
for y in range(height):
57+
for x in range(width):
58+
pixel = bytearray()
59+
while True:
60+
byte = file.read(1)
61+
if not byte.isdigit():
62+
break
63+
pixel += byte
64+
int_pixel = int("".join(["%c" % char for char in pixel]))
65+
bitmap[x, y] = palette_colors.index(int_pixel)
66+
return bitmap, palette
67+
68+
69+
def build_palette(palette_class, palette_colors):
70+
"""
71+
construct the Palette, and populate it with the set of palette_colors
72+
"""
73+
palette = palette_class(len(palette_colors))
74+
for counter, color in enumerate(palette_colors):
75+
palette[counter] = bytes([color, color, color])
76+
return palette
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2018 Scott Shawcroft for Adafruit Industries LLC
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
`adafruit_imageload.pnm.pgm.binary`
24+
====================================================
25+
26+
Load pixel values (indices or colors) into a bitmap and colors into a palette.
27+
28+
* Author(s): Matt Land, Brooke Storm, Sam McGahan
29+
30+
"""
31+
32+
33+
def load(file, width, height, bitmap=None, palette=None):
34+
"""
35+
Load a P5 format file (binary), handle PGM (greyscale)
36+
"""
37+
palette_colors = set()
38+
data_start = file.tell()
39+
for y in range(height):
40+
data_line = iter(bytes(file.read(width)))
41+
for pixel in data_line:
42+
palette_colors.add(pixel)
43+
44+
if palette:
45+
palette = build_palette(palette, palette_colors)
46+
if bitmap:
47+
bitmap = bitmap(width, height, len(palette_colors))
48+
palette_colors = list(palette_colors)
49+
file.seek(data_start)
50+
for y in range(height):
51+
data_line = iter(bytes(file.read(width)))
52+
for x, pixel in enumerate(data_line):
53+
bitmap[x, y] = palette_colors.index(pixel)
54+
return bitmap, palette
55+
56+
57+
def build_palette(palette_class, palette_colors):
58+
"""
59+
construct the Palette, and populate it with the set of palette_colors
60+
"""
61+
palette = palette_class(len(palette_colors))
62+
for counter, color in enumerate(palette_colors):
63+
palette[counter] = bytes([color, color, color])
64+
return palette

adafruit_imageload/tests/test_pbm_load.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
class TestPnmLoad(TestCase):
99
def test_load_fails_with_no_header_data(self):
10-
f = BytesIO(b"some initial binary data: \x00\x01")
10+
file = BytesIO(b"some initial binary data: \x00\x01")
1111
try:
12-
pnm.load(f, b"P1", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface)
12+
pnm.load(file, b"P1", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface)
1313
self.fail("should have failed")
14-
except Exception as e:
15-
if "Unsupported image format" not in str(e):
14+
except Exception as caught_exception:
15+
if "Unsupported image format" not in str(caught_exception):
1616
raise
1717

1818
def test_load_works_p1_ascii(self):
@@ -41,12 +41,13 @@ def test_load_works_p1_ascii(self):
4141
palette.validate()
4242

4343
def test_load_works_p4_in_mem(self):
44-
f = BytesIO(b"P4\n4 2\n\x55")
45-
bitmap, palette = pnm.load(f, b"P4", bitmap=Bitmap_C_Interface)
44+
file = BytesIO(b"P4\n4 2\n\x55")
45+
bitmap, palette = pnm.load(file, b"P4", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface)
4646
self.assertEqual(4, bitmap.width)
4747
self.assertEqual(2, bitmap.height)
4848
bitmap.validate()
4949
self.assertEqual("\n 1 0 1 0\n 1 0 1 0\n", str(bitmap))
50+
palette.validate()
5051

5152
def test_load_works_p4_binary(self):
5253
test_file = os.path.join(

0 commit comments

Comments
 (0)