Skip to content

Commit bfb89b7

Browse files
author
Matt Land
committed
p3 format (RGB ascii) working
1 parent f6a63e3 commit bfb89b7

4 files changed

Lines changed: 62 additions & 38 deletions

File tree

adafruit_imageload/pnm/ppm/__init__.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,26 @@
3333
__version__ = "0.0.0-auto.0"
3434
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
3535

36-
import math
37-
3836

3937
def load(file, magic_number, header, bitmap=None, palette=None):
4038
"""Load pixel values (indices or colors) into a bitmap and for a binary
4139
ppm, return None for pallet."""
4240
width = header[0]
4341
height = header[1]
44-
max_colors = (header[2] + 1) ** 3
42+
4543
# TODO: This needs to be different most likely?
46-
colors = math.log(header[2], 2)
47-
bitmap = bitmap(width, height, int(colors))
48-
palette = None
44+
#colors = math.log(header[2], 2)
45+
#bitmap = bitmap(width, height, int(colors))
46+
if magic_number == b"P3":
47+
# This is ascii
48+
from . import ppm_ascii
49+
50+
return ppm_ascii.load(
51+
file, width, height, bitmap=bitmap, palette=palette
52+
)
4953

5054
if bitmap:
51-
if magic_number == b"P3":
52-
# This is ascii
53-
from . import ppm_ascii
5455

55-
return ppm_ascii.load(
56-
file, width, height, max_colors, bitmap=bitmap, palette=None
57-
)
5856

5957
minimum_color_depth = 1
6058
while max_colors > 2 ** minimum_color_depth:

adafruit_imageload/pnm/ppm/ppm_ascii.py

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,37 +33,58 @@
3333
__version__ = "0.0.0-auto.0"
3434
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
3535

36-
import struct
3736

37+
def load(file, width, height, bitmap=None, palette=None):
38+
"""
39+
40+
:param stream file: infile with the position set at start of data
41+
:param int width:
42+
:param int height:
43+
:param int max_colors: color space of file
44+
:param bitmap: displayio.Bitmap class
45+
:param palette: displayio.Palette class
46+
:return:
47+
"""
48+
palette_colors = set()
49+
data_start = file.tell()
50+
triplet = []
51+
color = bytearray()
52+
while True: # scan for all colors present in the file
53+
# read values from file, values can be string len 1-3 for values 0 - 255
54+
this_byte = file.read(1)
55+
if this_byte == b'':
56+
break
57+
if not this_byte.isdigit(): # completed one number
58+
triplet.append(int("".join(["%c" % char for char in color])))
59+
color = bytearray()
60+
if len(triplet) == 3:
61+
palette_colors.add(tuple(triplet))
62+
triplet = []
63+
continue
64+
color += this_byte
65+
if palette:
66+
palette = palette(len(palette_colors))
67+
for counter, color in enumerate(palette_colors):
68+
palette[counter] = bytes(color)
3869

39-
def load(file, width, height, max_colors, bitmap=None, palette=None):
40-
"""Load an ascii ppm into the Bitmap object"""
4170
if bitmap:
71+
file.seek(data_start)
72+
bitmap = bitmap(width, height, len(palette_colors))
73+
palette_colors = list(palette_colors)
4274
for y in range(height):
43-
offset = y * width
4475
for x in range(width):
4576
triplet = []
4677
color = bytearray()
4778
while True:
48-
if len(triplet) == 3:
49-
break
5079
this_byte = file.read(1)
51-
if this_byte.isdigit():
52-
color += this_byte
53-
else:
54-
triplet.append(color)
80+
81+
if not this_byte.isdigit(): # completed one number
82+
triplet.append(int("".join(["%c" % char for char in color])))
5583
color = bytearray()
84+
if len(triplet) == 3: # completed one pixel
85+
bitmap[x, y] = palette_colors.index(tuple(triplet))
86+
break
5687
continue
57-
pixel = bytearray(3)
58-
# This just became 8-bit only...
59-
struct.pack_into(
60-
"BBB",
61-
pixel,
62-
0,
63-
int("".join(["%c" % char for char in triplet[0]])),
64-
int("".join(["%c" % char for char in triplet[1]])),
65-
int("".join(["%c" % char for char in triplet[2]])),
66-
)
67-
bitmap[offset + x] = int.from_bytes(pixel, "little")
88+
color += this_byte
6889

6990
return bitmap, palette

adafruit_imageload/tests/test_ppm_load.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ def test_load_works_p3_ascii(self):
1717
"images",
1818
"netpbm_p3_rgb_ascii.ppm",
1919
)
20-
with open(test_file, "rb") as f:
20+
with open(test_file, "rb") as file:
2121
bitmap, palette = pnm.load(
22-
f, b"P3", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface
22+
file, b"P3", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface
2323
)
24+
25+
self.assertTrue(isinstance(palette, Palette_C_Interface))
26+
self.assertEqual(6, palette.num_colors)
27+
#self.fail(str(palette))
2428
self.assertTrue(isinstance(bitmap, Bitmap_C_Interface), bitmap)
25-
self.assertEqual(16777216, bitmap.colors)
29+
self.assertEqual(6, bitmap.colors)
2630
self.assertEqual(16, bitmap.width)
2731
self.assertEqual(16, bitmap.height)
2832
bitmap.validate()
29-
str(bitmap)
3033
palette.validate()
3134

3235
def test_load_works_p6_binary(self):

docs/pbm_test_code.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
display.show(splash)
2929
#image = "images/netpbm_p1_mono_ascii.pbm"
3030
#image = "images/netpbm_p4_mono_binary.pbm"
31-
image = "images/netpbm_p2_ascii.pgm"
31+
#image = "images/netpbm_p2_ascii.pgm"
32+
#image = "images/netpbm_p5_binary.pgm"
33+
image = "images/netpbm_p3_rgb_ascii.ppm"
3234

3335
bitmap, palette = adafruit_imageload.load(
3436
image, bitmap=displayio.Bitmap, palette=displayio.Palette

0 commit comments

Comments
 (0)