Skip to content

Commit efeff7a

Browse files
committed
Merge branch 'feature-tests' of https://github.com/crookedstorm/Adafruit_CircuitPython_ImageLoad into Riddlerat/pgm-work
2 parents 0a2e93c + 79b9f6b commit efeff7a

12 files changed

Lines changed: 398 additions & 60 deletions

File tree

adafruit_imageload/__init__.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,15 @@ def load(filename, *, bitmap=None, palette=None):
4242
palette is the desired pallete type. The constructor should take the number of colors and
4343
support assignment to indices via [].
4444
"""
45-
with open(filename, "rb") as f:
46-
header = f.read(3)
47-
f.seek(0)
45+
with open(filename, "rb") as file:
46+
header = file.read(3)
47+
file.seek(0)
4848
if header.startswith(b"BM"):
4949
from . import bmp
50-
51-
return bmp.load(f, bitmap=bitmap, palette=palette)
50+
return bmp.load(file, bitmap=bitmap, palette=palette)
5251
elif header.startswith(b"P"):
5352
from . import pnm
5453

55-
return pnm.load(f, header, bitmap=bitmap, palette=palette)
54+
return pnm.load(file, header, bitmap=bitmap, palette=palette)
5655
else:
5756
raise RuntimeError("Unsupported image format")

adafruit_imageload/pnm/__init__.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,39 @@
3333
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
3434

3535

36-
def load(f, header, *, bitmap=None, palette=None):
36+
def load(file, header, *, bitmap=None, palette=None):
3737
# Read the header
3838
magic_number = header[:2]
39-
f.seek(2)
39+
file.seek(2)
4040
pnm_header = []
4141
while True:
4242
# We have all we need at length 3
4343
if len(pnm_header) == 3:
4444
break
45-
if magic_number.startswith(b"P1") or magic_number.startswith(b"P4"):
46-
if len(pnm_header) == 2:
47-
from . import pbm
45+
if len(pnm_header) == 2 and (
46+
magic_number.startswith(b"P1") or magic_number.startswith(b"P4")
47+
):
48+
bitmap = bitmap(pnm_header[0], pnm_header[1], 1)
49+
if palette:
50+
palette = palette(1)
51+
palette[0] = 0xFFFFFF
52+
if magic_number.startswith(b"P1"):
53+
from . import pbm_ascii
4854

49-
return pbm.load(
50-
f, magic_number, pnm_header, bitmap=bitmap, palette=palette
55+
return pbm_ascii.load(
56+
file, pnm_header[0], pnm_header[1], bitmap=bitmap, palette=palette
5157
)
5258

53-
next_byte = f.read(1)
59+
from . import pbm_binary
60+
61+
return pbm_binary.load(
62+
file, pnm_header[0], pnm_header[1], bitmap=bitmap, palette=palette
63+
)
64+
65+
next_byte = file.read(1)
5466
if next_byte == b"#":
5567
while True:
56-
next_byte = f.read(1)
68+
next_byte = file.read(1)
5769
if not next_byte:
5870
raise RuntimeError("Unsupported image format")
5971
if next_byte == b"\n":
@@ -64,7 +76,7 @@ def load(f, header, *, bitmap=None, palette=None):
6476
if not next_byte.isdigit():
6577
break
6678
value += next_byte
67-
next_byte = f.read(1)
79+
next_byte = file.read(1)
6880
if not next_byte:
6981
raise RuntimeError("Unsupported image format")
7082

@@ -77,15 +89,11 @@ def load(f, header, *, bitmap=None, palette=None):
7789
if magic_number.startswith(b"P2") or magic_number.startswith(b"P5"):
7890
from . import pgm
7991

80-
return pgm.load(
81-
f, magic_number, pnm_header, bitmap=bitmap, palette=palette
82-
)
92+
return pgm.load(file, magic_number, pnm_header, bitmap=bitmap, palette=palette)
8393

8494
if magic_number.startswith(b"P3") or magic_number.startswith(b"P6"):
8595
from . import ppm
8696

87-
return ppm.load(
88-
f, magic_number, pnm_header, bitmap=bitmap, palette=palette
89-
)
97+
return ppm.load(file, magic_number, pnm_header, bitmap=bitmap, palette=palette)
9098

9199
raise RuntimeError("Unsupported image format")

adafruit_imageload/pnm/pbm/__init__.py

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.pbm.ascii`
24+
====================================================
25+
26+
Load pixel values (indices or colors) into a bitmap and for an ascii ppm,
27+
return None for pallet.
28+
29+
* Author(s): Matt Land, Brooke Storm, Sam McGahan
30+
31+
"""
32+
33+
__version__ = "0.0.0-auto.0"
34+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
35+
36+
37+
def load(file, width, height, bitmap=None, palette=None):
38+
"""
39+
Load a P1 'PBM' ascii image into the displayio.Bitmap
40+
"""
41+
next_byte = True
42+
for y in range(height):
43+
x = 0
44+
while next_byte:
45+
next_byte = file.read(1)
46+
if not next_byte.isdigit():
47+
continue
48+
bitmap[x, y] = 1 if next_byte == b"1" else 0
49+
if x == width - 1:
50+
break
51+
x += 1
52+
return bitmap, palette
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.pbm.binary`
24+
====================================================
25+
26+
Load pixel values (indices or colors) into a bitmap and for an ascii ppm,
27+
return None for pallet.
28+
29+
* Author(s): Matt Land, Brooke Storm, Sam McGahan
30+
31+
"""
32+
33+
__version__ = "0.0.0-auto.0"
34+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
35+
36+
37+
def load(file, width, height, bitmap=None, palette=None):
38+
"""
39+
Load a P4 'PBM' binary image into the displayio.Bitmap
40+
"""
41+
x = 0
42+
y = 0
43+
while True:
44+
next_byte = file.read(1)
45+
if not next_byte:
46+
break # out of bits
47+
for bit in iterbits(int.from_bytes(next_byte, "little")):
48+
bitmap[x, y] = bit
49+
x += 1
50+
if x > width - 1:
51+
y += 1
52+
x = 0
53+
if y > height - 1:
54+
break
55+
return bitmap, palette
56+
57+
58+
def iterbits(b):
59+
for i in range(8):
60+
yield (b >> i) & 1

0 commit comments

Comments
 (0)