Skip to content

Commit c710a8d

Browse files
author
Matt Land
committed
working P6 format
1 parent bfb89b7 commit c710a8d

5 files changed

Lines changed: 39 additions & 44 deletions

File tree

adafruit_imageload/pnm/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,14 @@ def load(file, header, *, bitmap=None, palette=None):
9191

9292
return pgm.load(file, magic_number, pnm_header, bitmap=bitmap, palette=palette)
9393

94-
if magic_number.startswith(b"P3") or magic_number.startswith(b"P6"):
95-
from . import ppm
94+
if magic_number.startswith(b"P3"):
95+
from . import ppm_ascii
9696

97-
return ppm.load(file, magic_number, pnm_header, bitmap=bitmap, palette=palette)
97+
return ppm_ascii.load(file, pnm_header[0], pnm_header[1], bitmap=bitmap, palette=palette)
98+
99+
if magic_number.startswith(b"P6"):
100+
from . import ppm_binary
101+
102+
return ppm_binary.load(file, pnm_header[0], pnm_header[1], bitmap=bitmap, palette=palette)
98103

99104
raise RuntimeError("Unsupported image format")
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.ppm.ppm_ascii`
23+
`adafruit_imageload.pnm.ppm_ascii`
2424
====================================================
2525
2626
Load pixel values (indices or colors) into a bitmap and for an ascii ppm,
Lines changed: 27 additions & 38 deletions
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.ppm`
23+
`adafruit_imageload.pnm.ppm_binary`
2424
====================================================
2525
2626
Load pixel values (indices or colors) into a bitmap and for a binary ppm,
@@ -34,47 +34,36 @@
3434
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
3535

3636

37-
def load(file, magic_number, header, bitmap=None, palette=None):
37+
def load(file, width, height, bitmap=None, palette=None):
3838
"""Load pixel values (indices or colors) into a bitmap and for a binary
3939
ppm, return None for pallet."""
40-
width = header[0]
41-
height = header[1]
42-
43-
# TODO: This needs to be different most likely?
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-
)
5340

41+
data_start = file.tell()
42+
palette_colors = set()
43+
line_size = width * 3
44+
45+
for y in range(height):
46+
data_line = iter(bytes(file.read(line_size)))
47+
for red in data_line:
48+
green = next(data_line)
49+
blue = next(data_line)
50+
palette_colors.add((red, green, blue))
51+
52+
if palette:
53+
palette = palette(len(palette_colors))
54+
for counter, color in enumerate(palette_colors):
55+
palette[counter] = bytes(color)
5456
if bitmap:
55-
56-
57-
minimum_color_depth = 1
58-
while max_colors > 2 ** minimum_color_depth:
59-
minimum_color_depth *= 2
60-
61-
# This seems maybe right?
62-
line_size = width * 3
63-
64-
chunk = bytearray(line_size)
65-
57+
bitmap = bitmap(width, height, len(palette_colors))
58+
file.seek(data_start)
59+
palette_colors = list(palette_colors)
6660
for y in range(height):
67-
file.readinto(chunk)
68-
# Division by zero!
69-
pixels_per_byte = 8 // max_colors
70-
offset = y * width
71-
72-
for x in range(width):
73-
# This math is clearly wrong and was just copied in from the PR
74-
i = x // pixels_per_byte
75-
pixel = (
76-
chunk[i] >> (8 - max_colors * (x % pixels_per_byte + 1))
77-
) & ((1 << minimum_color_depth) - 1)
78-
bitmap[offset + x] = pixel
61+
x = 0
62+
data_line = iter(bytes(file.read(line_size)))
63+
for red in data_line:
64+
green = next(data_line)
65+
blue = next(data_line)
66+
bitmap[x,y] = palette_colors.index((red, green, blue))
67+
x += 1
7968

8069
return bitmap, palette

adafruit_imageload/tests/test_ppm_load.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_load_works_p6_binary(self):
4646
f, b"P6", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface
4747
)
4848
self.assertTrue(isinstance(bitmap, Bitmap_C_Interface), bitmap)
49-
self.assertEqual(16777216, bitmap.colors)
49+
self.assertEqual(6, bitmap.colors)
5050
self.assertEqual(16, bitmap.width)
5151
self.assertEqual(16, bitmap.height)
5252
bitmap.validate()

docs/pbm_test_code.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
#image = "images/netpbm_p4_mono_binary.pbm"
3131
#image = "images/netpbm_p2_ascii.pgm"
3232
#image = "images/netpbm_p5_binary.pgm"
33-
image = "images/netpbm_p3_rgb_ascii.ppm"
33+
#image = "images/netpbm_p3_rgb_ascii.ppm"
34+
image = "images/netpbm_p6_binary.ppm"
3435

3536
bitmap, palette = adafruit_imageload.load(
3637
image, bitmap=displayio.Bitmap, palette=displayio.Palette

0 commit comments

Comments
 (0)