Skip to content

Commit ff9113d

Browse files
committed
🤖👽
1 parent efeff7a commit ff9113d

5 files changed

Lines changed: 59 additions & 12 deletions

File tree

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": "/Users/Lyra/Documents/GitHub/Adafruit_CircuitPython_ImageLoad/venv/bin/python3.7"
3+
}

adafruit_imageload/pnm/pgm/__init__.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,49 @@
2929
3030
"""
3131

32-
def load(f, magic_number, header, *, bitmap=None, palette=None):
32+
33+
def load(file, magic_number, header, *, bitmap=None, palette=None):
3334
# TODO: remove unused variables later
3435
width = header[0]
3536
height = header[1]
36-
max_colors = header[2] + 1
37+
max_colors = header[2]
3738
min_color = 1 # probably don't need this
3839
columns = height
39-
line_length = width //(8 // max_colors)
4040
bitmap = bitmap(width, height, max_colors)
4141

4242

43-
if max_color > 256:
43+
if max_colors > 256:
4444
# raise exception
4545
raise NotImplementedError("16 bit grayscale not supported")
4646

47-
if magic_number == "P2":
47+
if magic_number == b'P2':
4848
# Handle ascii
49-
for x in height:
50-
for y in line_length:
49+
colors = set()
50+
for y in range(height):
51+
for x in range(width):
5152
# Takes int and converts to an 8 bit
52-
53-
bitmap[x,y] = format(inttoformat,'08b')
53+
pixel = bytearray()
54+
55+
while True:
56+
bit = file.read(1) # type: byte
57+
if not bit.isdigit():
58+
break
59+
pixel += bit
60+
61+
bitmap[x, y] = int(pixel)
62+
colors.add(int(pixel))
63+
if palette:
64+
palette = palette(len(colors))
65+
for counter, color in enumerate(colors):
66+
color_int = int(f'{hex(color)}{hex(color)[2:]}{hex(color)[2:]}', 16) # HACK: is there a better way?
67+
palette[counter] = color_int
68+
return bitmap, palette
5469

5570

71+
if magic_number == b'P5':
72+
raise NotImplementedError("Nope")
5673

57-
if magic_number == "P5":
74+
raise NotImplementedError("no")
5875

5976
# Assign bits to bitmap
6077

adafruit_imageload/tests/test_pnm_load.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,17 @@ def __str__(self):
5757
for y in range(self.height):
5858
for x in range(self.width):
5959
data = self[x, y]
60-
out += f"{data}"
60+
out += f"{data} "
6161
out += "\n"
6262
return out
6363

64+
class Palette_C_Interface(object):
65+
def __init__(self, num_colors):
66+
self.num_colors = num_colors
67+
self.colors = {}
68+
69+
def __setitem__(self, key, value):
70+
self.colors[key] = value
6471

6572
logging.getLogger().setLevel(logging.INFO)
6673

@@ -136,6 +143,26 @@ def test_decode(self):
136143
encoded = b._abs_pos(3, 3)
137144
self.assertEqual((3, 3), b._decode(encoded))
138145

146+
class testPgmLoad(TestCase):
147+
def test_load_works_p1_ascii(self):
148+
test_file = os.path.join(
149+
os.path.dirname(__file__),
150+
"..",
151+
"..",
152+
"examples",
153+
"images",
154+
"netpbm_p2_ascii.pgm",
155+
)
156+
with open(test_file, "rb") as f:
157+
bitmap, palette = pnm.load(f, b"P2", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface)
158+
self.assertTrue(isinstance(bitmap, Bitmap_C_Interface), bitmap)
159+
self.assertEqual(255, bitmap.colors)
160+
self.assertEqual(8, bitmap.width)
161+
self.assertEqual(8, bitmap.height)
162+
bitmap.validate()
163+
self.fail(str(bitmap))
164+
165+
139166

140167
class TestPnmLoad(TestCase):
141168
def test_load_fails_with_no_header_data(self):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ P2
77
203 0 255 0 0 62 0 128
88
0 84 0 62 255 0 84 0
99
255 0 84 0 0 84 0 128
10-
0 128 0 128 203 0 255 0
10+
0 128 0 128 203 0 255 0

0 commit comments

Comments
 (0)