Skip to content

Incorrect handling of FlateDecode with image mode "1" #2248

@stefan6419846

Description

@stefan6419846

Extracting /FlateDecode images with an image mode of 1 currently fails, probably due to an invalid range loop.

Let's consider the lookup of b'\x00p\xc0\xff\xff\xff' in RGB mode. This should always create two RGB values for further processing ("high" and "low" for image mode 1), but currently only creates one.

Environment

Which environment were you using when you encountered the problem?

$ python -m platform
Linux-5.14.21-150400.24.84-default-x86_64-with-glibc2.31

$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==3.16.3, crypt_provider=('pycryptodome', '3.18.0'), PIL=10.0.0

Code + PDF

This is a minimal, complete example that shows the issue:

from pypdf import PdfReader

path = 'Prototype-Declaration-VDE4110-HYD-5000-20000-ZSS-DE.pdf'
for index, page in enumerate(PdfReader(path).pages):
    for key in page.images.keys():
        print(key)
        print(page.images[key].indirect_reference)

An example file is this

Traceback

This is the complete traceback I see:

Traceback (most recent call last):
  File "/home/stefan/temp/run.py", line 15, in <module>
    print(page.images[key].indirect_reference)
  File "/home/stefan/temp/venv/lib/python3.9/site-packages/pypdf/_page.py", line 2709, in __getitem__
    return self.get_function(index)
  File "/home/stefan/temp/venv/lib/python3.9/site-packages/pypdf/_page.py", line 558, in _get_image
    return self._get_image(ids, cast(DictionaryObject, xobjs[id[0]]))
  File "/home/stefan/temp/venv/lib/python3.9/site-packages/pypdf/_page.py", line 547, in _get_image
    imgd = _xobj_to_image(cast(DictionaryObject, xobjs[id]))
  File "/home/stefan/temp/venv/lib/python3.9/site-packages/pypdf/filters.py", line 781, in _xobj_to_image
    img, image_format, extension, invert_color = _handle_flate(
  File "/home/stefan/temp/venv/lib/python3.9/site-packages/pypdf/_xobj_image_helpers.py", line 177, in _handle_flate
    [
  File "/home/stefan/temp/venv/lib/python3.9/site-packages/pypdf/_xobj_image_helpers.py", line 179, in <listcomp>
    [
  File "/home/stefan/temp/venv/lib/python3.9/site-packages/pypdf/_xobj_image_helpers.py", line 180, in <listcomp>
    colors_arr[1 if img.getpixel((x, y)) > 127 else 0]
IndexError: list index out of range

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions