Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions adafruit_imageload/pnm/pbm_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ def load(
"""
Load a P4 'PBM' binary image into the Bitmap
"""
padded_width = (width + 7) // 8 * 8
x = 0
y = 0
while True:
next_byte = file.read(1)
if not next_byte:
break # out of bits
for bit in iterbits(next_byte):
bitmap[x, y] = bit
if x < width:
bitmap[x, y] = bit
x += 1
if x > width - 1:
if x > padded_width - 1:
y += 1
x = 0
if y > height - 1:
Expand Down
3 changes: 3 additions & 0 deletions examples/images/netpbm_p4_mono_width.pbm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
P4
14 20
�̷���������������{x|��g�C��{x�����
2 changes: 2 additions & 0 deletions examples/images/netpbm_p4_mono_width.pbm.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# SPDX-FileCopyrightText: 2025 Karolis Stasaitis
# SPDX-License-Identifier: MIT
23 changes: 22 additions & 1 deletion tests/test_pbm_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_load_works_p1_ascii(self):
palette.validate()

def test_load_works_p4_in_mem(self):
file = BytesIO(b"P4\n4 2\n\x55")
file = BytesIO(b"P4\n4 2\n\x5f\x5f")
bitmap, palette = pnm.load(
file, b"P4", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface
)
Expand Down Expand Up @@ -103,6 +103,27 @@ def test_load_works_p4_binary(self):
self.assertEqual(15, bitmap.height)
bitmap.validate()

def test_load_works_p4_binary_padded_width(self):
test_file = os.path.join(
os.path.dirname(__file__),
"..",
"examples",
"images",
"netpbm_p4_mono_width.pbm",
)
with open(test_file, "rb") as file:
bitmap, palette = pnm.load(
file, b"P4", bitmap=Bitmap_C_Interface, palette=Palette_C_Interface
)
self.assertEqual(1, palette.num_colors)
palette.validate()
self.assertEqual(b"\xff\xff\xff", palette[0])
self.assertTrue(isinstance(bitmap, Bitmap_C_Interface))
self.assertEqual(1, bitmap.colors)
self.assertEqual(14, bitmap.width)
self.assertEqual(20, bitmap.height)
bitmap.validate()

def test_load_works_p4_binary_high_res(self):
test_file = os.path.join(
os.path.dirname(__file__),
Expand Down