From e9241ac795cea9b93f82741c57b1036de456d330 Mon Sep 17 00:00:00 2001 From: Stefan <96178532+stefan6419846@users.noreply.github.com> Date: Sat, 28 Oct 2023 12:42:16 +0200 Subject: [PATCH] BUG: Correctly handle image mode 1 with FlateDecode (#2249) Fixes #2248 --- pypdf/_xobj_image_helpers.py | 4 +++- tests/test_filters.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pypdf/_xobj_image_helpers.py b/pypdf/_xobj_image_helpers.py index f41b772b0..87bff19b9 100644 --- a/pypdf/_xobj_image_helpers.py +++ b/pypdf/_xobj_image_helpers.py @@ -186,7 +186,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( diff --git a/tests/test_filters.py b/tests/test_filters.py index 9cfea57b5..00a548ab0 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 = ( @@ -637,3 +638,14 @@ 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() +@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" + name = "issue2248.pdf" + reader = PdfReader(BytesIO(get_data_from_url(url, name=name))) + for image in reader.pages[7].images: + _ = image