Skip to content

Commit 456d9ca

Browse files
author
bstorm
committed
Merge remote-tracking branch 'origin/master' into ppm
2 parents 1822cb3 + 8294e85 commit 456d9ca

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

adafruit_imageload/pnm/__init__.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,53 @@
2828
* Author(s): Matt Land, Brooke Storm, Sam McGahan
2929
3030
"""
31-
# spec https://en.wikipedia.org/wiki/Netpbm_format
31+
32+
__version__ = "0.0.0-auto.0"
33+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
34+
35+
import re
3236

3337

3438
def load(f, header, *, bitmap=None, palette=None):
3539
# Read the header
3640
magic_number = header[:2]
37-
if magic_number.startswith(b"P1") or magic_number.startswith(b"P4"):
38-
from . import pbm
41+
f.seek(2)
42+
pnm_header = []
43+
while True:
44+
# We have all we need at length 3
45+
if len(pnm_header) == 3:
46+
break
47+
if magic_number.startswith(b"P1") or magic_number.startswith(b"P4"):
48+
if len(pnm_header) == 2:
49+
from . import pbm
50+
51+
return pbm.load(
52+
f, magic_number, pnm_header, bitmap=bitmap, palette=palette
53+
)
54+
55+
next_byte = f.read(1)
56+
if next_byte == b"#":
57+
if next_byte != b"\n":
58+
continue
59+
dec_re = re.compile(r"\d")
60+
if dec_re.match(next_byte):
61+
pnm_header.append(next_byte)
62+
continue
63+
64+
if not next_byte:
65+
raise RuntimeError("Unsupported image format")
3966

40-
return pbm.load(f, magic_number, bitmap=bitmap, palette=palette)
41-
elif magic_number.startswith(b"P2") or magic_number.startswith(b"P5"):
67+
if magic_number.startswith(b"P2") or magic_number.startswith(b"P5"):
4268
from . import pgm
4369

44-
return pgm.load(f, magic_number, bitmap=bitmap, palette=palette)
70+
return pgm.load(
71+
f, magic_number, pnm_header, bitmap=bitmap, palette=palette
72+
)
4573
elif magic_number.startswith(b"P3") or magic_number.startswith(b"P6"):
4674
from . import ppm
4775

48-
return ppm.load(f, magic_number, bitmap=bitmap, palette=palette)
76+
return ppm.load(
77+
f, magic_number, pnm_header, bitmap=bitmap, palette=palette
78+
)
4979
else:
5080
raise RuntimeError("Unsupported image format")

0 commit comments

Comments
 (0)