Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some .ppm files trigger a ValueError exception in ppm_to_numpy() #50

Open
pklaus opened this issue Apr 4, 2019 · 3 comments
Open

Some .ppm files trigger a ValueError exception in ppm_to_numpy() #50

pklaus opened this issue Apr 4, 2019 · 3 comments

Comments

@pklaus
Copy link

pklaus commented Apr 4, 2019

I found a bug with the regex search expression in lines 36-40 of vapory/io.py:

        header, width, height, maxval = re.search(
            b"(^P\d\s(?:\s*#.*[\r\n])*"
            b"(\d+)\s(?:\s*#.*[\r\n])*"
            b"(\d+)\s(?:\s*#.*[\r\n])*"
            b"(\d+)\s(?:\s*#.*[\r\n]\s)*)", buffer).groups()

The outer group of this regex should match the header of .ppm files. Sometimes (in the case of my image) it matches not only the header but the full file. When the "header" is then stripped away using offset=len(header) in line 49, no payload is left over and reading the data fails.

So, why is this: Because the regex in its current form tries to capture comments after the last header word 'maxval'. This seems to be disallowed for the binary P6 PPM format:

Comments can only occur before the last field of the header and only one byte may appear after the last header field, normally a carriage return or line feed.

[source: http://paulbourke.net/dataformats/ppm/]

Way to reproduce the bug:

  1. Get the attached file problematic_image.ppm.zip and unpack it.
  2. Run the following code:
from vapory.io import ppm_to_numpy
with open('problematic_image.ppm', 'rb') as f:
    out = f.read()
ppm_to_numpy(buffer=out)

This will lead to the following stacktrace:

  File "/opt/python3.7/site-packages/vapory/vapory.py", line 102, in render
    quality, antialiasing, remove_temp)
  File "/opt/python3.7/site-packages/vapory/io.py", line 117, in render_povstring
    return ppm_to_numpy(buffer=out)
  File "/opt/python3.7/site-packages/vapory/io.py", line 49, in ppm_to_numpy
    offset=len(header))
ValueError: buffer is smaller than requested size
@moschmdt
Copy link

moschmdt commented Oct 1, 2020

Hi, have you solved the problem? Apperently I have the same error for all my pictures. Might look into a fix.

@pklaus
Copy link
Author

pklaus commented Oct 1, 2020

I think I replaced the regex in vapory/io.py by something like:

        header, width, height, maxval = re.search(
            b"(^P\d\s(?:\s*#.*[\r\n])*"
            b"(\d+)\s(?:\s*#.*[\r\n])*"
            b"(\d+)\s(?:\s*#.*[\r\n])*"
            b"(\d+)[\r\n])", buffer).groups()

Hope it helps.

@moschmdt
Copy link

I did not get it to work really stable with reasonable effort. Instead, I wrote a Python wrapper on my own. Thanks for your help anyway!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants