Skip to content

Commit 0269c39

Browse files
author
Matt Land
committed
optimize
1 parent 6038440 commit 0269c39

9 files changed

Lines changed: 35 additions & 27 deletions

File tree

adafruit_imageload/pnm/__init__.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@ def load(file, header, *, bitmap=None, palette=None):
3939
file.seek(2)
4040
pnm_header = []
4141
while True:
42-
# We have all we need at length 3
42+
# We have all we need at length 3 for formats P2, P3, P5, P6
4343
if len(pnm_header) == 3:
4444
break
45-
if len(pnm_header) == 2 and (
46-
magic_number.startswith(b"P1") or magic_number.startswith(b"P4")
47-
):
45+
if len(pnm_header) == 2 and magic_number in [b"P1", b"P4"]:
4846
bitmap = bitmap(pnm_header[0], pnm_header[1], 1)
4947
if palette:
5048
palette = palette(1)
@@ -86,19 +84,23 @@ def load(file, header, *, bitmap=None, palette=None):
8684
if not next_byte:
8785
raise RuntimeError("Unsupported image format")
8886

89-
if magic_number.startswith(b"P2") or magic_number.startswith(b"P5"):
87+
if magic_number in [b"P2", b"P5"]:
9088
from . import pgm
9189

9290
return pgm.load(file, magic_number, pnm_header, bitmap=bitmap, palette=palette)
9391

94-
if magic_number.startswith(b"P3"):
92+
if magic_number == b"P3":
9593
from . import ppm_ascii
9694

97-
return ppm_ascii.load(file, pnm_header[0], pnm_header[1], bitmap=bitmap, palette=palette)
95+
return ppm_ascii.load(
96+
file, pnm_header[0], pnm_header[1], bitmap=bitmap, palette=palette
97+
)
9898

99-
if magic_number.startswith(b"P6"):
99+
if magic_number == b"P6":
100100
from . import ppm_binary
101101

102-
return ppm_binary.load(file, pnm_header[0], pnm_header[1], bitmap=bitmap, palette=palette)
102+
return ppm_binary.load(
103+
file, pnm_header[0], pnm_header[1], bitmap=bitmap, palette=palette
104+
)
103105

104106
raise RuntimeError("Unsupported image format {}".format(magic_number))

adafruit_imageload/pnm/pbm_ascii.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
"""
23-
`adafruit_imageload.pnm.pbm.ascii`
23+
`adafruit_imageload.pnm.pbm_ascii`
2424
====================================================
2525
2626
Load pixel values (indices or colors) into a bitmap and for an ascii ppm,

adafruit_imageload/pnm/pbm_binary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
"""
23-
`adafruit_imageload.pnm.pbm.binary`
23+
`adafruit_imageload.pnm.pbm_binary`
2424
====================================================
2525
2626
Load pixel values (indices or colors) into a bitmap and for an ascii ppm,

adafruit_imageload/pnm/pgm/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def load(file, magic_number, header, *, bitmap=None, palette=None):
3737
:param magic_number: string P2 or P6
3838
:param header: list of width, height, max color value
3939
:param bitmap: displayio.Bitmap class object
40-
:param palette: displayio.Paletta class object
40+
:param palette: displayio.Palette class object
4141
:return:
4242
"""
4343
if header[2] > 256:
@@ -53,7 +53,7 @@ def load(file, magic_number, header, *, bitmap=None, palette=None):
5353
# build a set of all colors present in the file, so palette and bitmap can be constructed
5454
while True:
5555
byte = file.read(1)
56-
if byte == b'':
56+
if byte == b"":
5757
break
5858
if not byte.isdigit():
5959
int_pixel = int("".join(["%c" % char for char in pixel]))
@@ -81,7 +81,7 @@ def load(file, magic_number, header, *, bitmap=None, palette=None):
8181
if magic_number == b"P5": # To handle binary PGM files.
8282
while True:
8383
byte = file.read(1)
84-
if byte == b'':
84+
if byte == b"":
8585
break
8686
int_pixel = int.from_bytes(byte, "little")
8787
palette_colors.add(int_pixel)
@@ -108,4 +108,4 @@ def build_palette(palette_class, palette_colors):
108108
palette = palette_class(len(palette_colors))
109109
for counter, color in enumerate(palette_colors):
110110
palette[counter] = bytes([color, color, color])
111-
return palette
111+
return palette

adafruit_imageload/pnm/ppm_ascii.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def load(file, width, height, bitmap=None, palette=None):
5252
while True: # scan for all colors present in the file
5353
# read values from file, values can be string len 1-3 for values 0 - 255
5454
this_byte = file.read(1)
55-
if this_byte == b'':
55+
if this_byte == b"":
5656
break
5757
if not this_byte.isdigit(): # completed one number
5858
triplet.append(int("".join(["%c" % char for char in color])))

adafruit_imageload/pnm/ppm_binary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def load(file, width, height, bitmap=None, palette=None):
6363
for red in data_line:
6464
green = next(data_line)
6565
blue = next(data_line)
66-
bitmap[x,y] = palette_colors.index((red, green, blue))
66+
bitmap[x, y] = palette_colors.index((red, green, blue))
6767
x += 1
6868

6969
return bitmap, palette

adafruit_imageload/tests/test_bitmap_c_interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from .displayio_shared_bindings import Bitmap_C_Interface
33

44

5-
class TestBitmap_C(TestCase):
5+
class TestBitmap_C_Interface(TestCase):
66
def test_init(self):
77
b = Bitmap_C_Interface(2, 4, 1)
88
self.assertEqual(2, b.width)

adafruit_imageload/tests/test_pbm_load.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,16 @@ def test_load_works_p4_binary(self):
5858
"netpbm_p4_mono_binary.pbm",
5959
)
6060
with open(test_file, "rb") as f:
61-
bitmap, palette = pnm.load(f, b"P4", bitmap=Bitmap_C_Interface)
61+
bitmap, palette = pnm.load(
62+
f, b"P4", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface
63+
)
6264
self.assertTrue(isinstance(bitmap, Bitmap_C_Interface))
6365
self.assertEqual(1, bitmap.colors)
6466
self.assertEqual(32, bitmap.width)
6567
self.assertEqual(15, bitmap.height)
6668
bitmap.validate()
69+
self.assertEqual(1, palette.num_colors)
70+
palette.validate()
6771

6872
def test_load_works_p4_binary_high_res(self):
6973
test_file = os.path.join(
@@ -75,8 +79,13 @@ def test_load_works_p4_binary_high_res(self):
7579
"netpbm_p4_mono_large.pbm",
7680
)
7781
with open(test_file, "rb") as f:
78-
bitmap, palette = pnm.load(f, b"P4", bitmap=Bitmap_C_Interface)
82+
bitmap, palette = pnm.load(
83+
f, b"P4", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface
84+
)
7985
self.assertTrue(isinstance(bitmap, Bitmap_C_Interface))
8086
self.assertEqual(1, bitmap.colors)
8187
self.assertEqual(320, bitmap.width)
8288
self.assertEqual(240, bitmap.height)
89+
bitmap.validate()
90+
self.assertEqual(1, palette.num_colors)
91+
palette.validate()

adafruit_imageload/tests/test_ppm_load.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import os
2-
import logging
32
from unittest import TestCase
43
from adafruit_imageload import pnm
54
from .displayio_shared_bindings import Bitmap_C_Interface, Palette_C_Interface
65

7-
logging.getLogger().setLevel(logging.INFO)
8-
96

107
class TestPpmLoad(TestCase):
118
def test_load_works_p3_ascii(self):
@@ -24,13 +21,13 @@ def test_load_works_p3_ascii(self):
2421

2522
self.assertTrue(isinstance(palette, Palette_C_Interface))
2623
self.assertEqual(6, palette.num_colors)
27-
#self.fail(str(palette))
24+
palette.validate()
25+
# self.fail(str(palette))
2826
self.assertTrue(isinstance(bitmap, Bitmap_C_Interface), bitmap)
2927
self.assertEqual(6, bitmap.colors)
3028
self.assertEqual(16, bitmap.width)
3129
self.assertEqual(16, bitmap.height)
3230
bitmap.validate()
33-
palette.validate()
3431

3532
def test_load_works_p6_binary(self):
3633
test_file = os.path.join(
@@ -45,10 +42,10 @@ def test_load_works_p6_binary(self):
4542
bitmap, palette = pnm.load(
4643
f, b"P6", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface
4744
)
45+
self.assertEqual(6, palette.num_colors)
46+
palette.validate()
4847
self.assertTrue(isinstance(bitmap, Bitmap_C_Interface), bitmap)
4948
self.assertEqual(6, bitmap.colors)
5049
self.assertEqual(16, bitmap.width)
5150
self.assertEqual(16, bitmap.height)
5251
bitmap.validate()
53-
str(bitmap)
54-
palette.validate()

0 commit comments

Comments
 (0)