From 23f7866f2e69fbf9af54a0b9094ee3db53611b38 Mon Sep 17 00:00:00 2001 From: Karolis Stasaitis Date: Wed, 9 Apr 2025 22:22:41 +0200 Subject: [PATCH] fix padded pbm binary loading --- adafruit_imageload/pnm/pbm_binary.py | 6 +++-- examples/images/netpbm_p4_mono_width.pbm | 3 +++ .../images/netpbm_p4_mono_width.pbm.license | 2 ++ tests/test_pbm_load.py | 23 ++++++++++++++++++- 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 examples/images/netpbm_p4_mono_width.pbm create mode 100644 examples/images/netpbm_p4_mono_width.pbm.license diff --git a/adafruit_imageload/pnm/pbm_binary.py b/adafruit_imageload/pnm/pbm_binary.py index cecdba5..53d92df 100644 --- a/adafruit_imageload/pnm/pbm_binary.py +++ b/adafruit_imageload/pnm/pbm_binary.py @@ -38,6 +38,7 @@ def load( """ Load a P4 'PBM' binary image into the Bitmap """ + padded_width = (width + 7) // 8 * 8 x = 0 y = 0 while True: @@ -45,9 +46,10 @@ def load( 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: diff --git a/examples/images/netpbm_p4_mono_width.pbm b/examples/images/netpbm_p4_mono_width.pbm new file mode 100644 index 0000000..ef6a9e6 --- /dev/null +++ b/examples/images/netpbm_p4_mono_width.pbm @@ -0,0 +1,3 @@ +P4 +14 20 +ÏÌ·´·´·´·´·´·´·´·´{x|øøg˜Cøø{x¼ôßìà \ No newline at end of file diff --git a/examples/images/netpbm_p4_mono_width.pbm.license b/examples/images/netpbm_p4_mono_width.pbm.license new file mode 100644 index 0000000..5e64ae4 --- /dev/null +++ b/examples/images/netpbm_p4_mono_width.pbm.license @@ -0,0 +1,2 @@ +# SPDX-FileCopyrightText: 2025 Karolis Stasaitis +# SPDX-License-Identifier: MIT diff --git a/tests/test_pbm_load.py b/tests/test_pbm_load.py index f1ba849..e999657 100644 --- a/tests/test_pbm_load.py +++ b/tests/test_pbm_load.py @@ -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 ) @@ -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__),