From fabda50cb848df1cc5d54b630c660dc75377bd2b Mon Sep 17 00:00:00 2001 From: Stefan <96178532+stefan6419846@users.noreply.github.com> Date: Mon, 9 Oct 2023 16:39:00 +0200 Subject: [PATCH 1/3] BUG: Correctly handle image mode 1 with FlateDecode --- pypdf/_xobj_image_helpers.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pypdf/_xobj_image_helpers.py b/pypdf/_xobj_image_helpers.py index bf3c869f7..ebc0d1cea 100644 --- a/pypdf/_xobj_image_helpers.py +++ b/pypdf/_xobj_image_helpers.py @@ -167,7 +167,9 @@ def bits2byte(data: bytes, size: Tuple[int, int], bits: int) -> bytes: lookup = None else: if img.mode == "1": - colors_arr = [lookup[x - nb : x] for x in range(nb, len(lookup), nb)] + # Two values ("high" and "low"). + assert len(lookup) == 2 * nb, len(lookup) + colors_arr = [lookup[:nb], lookup[nb:]] arr = b"".join( [ b"".join( From b672d3379a3f81cb1b842cf79bd313666150f3e5 Mon Sep 17 00:00:00 2001 From: Stefan <96178532+stefan6419846@users.noreply.github.com> Date: Mon, 9 Oct 2023 16:42:43 +0200 Subject: [PATCH 2/3] add test --- tests/test_filters.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_filters.py b/tests/test_filters.py index 12819c43b..bcbd7f54c 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -628,3 +628,13 @@ def test_nested_device_n_color_space(): name = "issue2240.pdf" reader = PdfReader(BytesIO(get_data_from_url(url, name=name))) reader.pages[0].images[0] + + +@pytest.mark.enable_socket() +def test_flate_decode_with_image_mode_1(): + """From #2248""" + url = "https://github.com/py-pdf/pypdf/files/12847339/Prototype-Declaration-VDE4110-HYD-5000-20000-ZSS-DE.pdf" + name = "issue2248.pdf" + reader = PdfReader(BytesIO(get_data_from_url(url, name=name))) + for image in reader.pages[7].images: + _ = image From 5b7bbe3fd1243a138b81246872a14d9507033530 Mon Sep 17 00:00:00 2001 From: Stefan <96178532+stefan6419846@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:31:06 +0200 Subject: [PATCH 3/3] skip test when HAS_AES is not active --- tests/test_filters.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_filters.py b/tests/test_filters.py index bcbd7f54c..bcb71e88e 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -22,6 +22,7 @@ from pypdf.generic import ArrayObject, DictionaryObject, NameObject, NumberObject from . import get_data_from_url +from .test_encryption import HAS_AES from .test_images import image_similarity filter_inputs = ( @@ -631,6 +632,7 @@ def test_nested_device_n_color_space(): @pytest.mark.enable_socket() +@pytest.mark.skipif(not HAS_AES, reason="No AES implementation") def test_flate_decode_with_image_mode_1(): """From #2248""" url = "https://github.com/py-pdf/pypdf/files/12847339/Prototype-Declaration-VDE4110-HYD-5000-20000-ZSS-DE.pdf"