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

Interoperability issue with pycairo for ImageSurface.create_for_data and numpy data #223

Open
FriedrichFroebel opened this issue Jan 24, 2024 · 1 comment

Comments

@FriedrichFroebel
Copy link

I am currently trying to understand why my code works with pycairo, but fails for cairocffi.

Let's take the script at https://github.com/FriedrichFroebel/python-djvulibre/blob/b6c68a3496a244403488034b30c8de4b935b02b5/examples/djvu2png and call it with djvu2png --foreground ../tests/images/test1.djvu out.png. With a regular pycairo installation, everything works fine. But as soon as I replace import cairo with import cairocffi as cairo, I get an error:

ValueError: Got a 3072 bytes buffer, needs at least 12288.

Simply removing the offending lines from the cairocffi source code at

if length < stride * height:
raise ValueError('Got a %d bytes buffer, needs at least %d.'
% (length, stride * height))
appears to generate the correct output without any error message. For this reason, it seems like the condition is superfluous or wrong.

@FriedrichFroebel FriedrichFroebel changed the title Interoperability issue with pycairo for ImageSurface.create_for_data Interoperability issue with pycairo for ImageSurface.create_for_data and numpy data Jan 26, 2024
@liZe
Copy link
Member

liZe commented Jan 28, 2024

Hi!

Thanks for the report.

Instead of trying to find wrong data lengths and stride values, we should rely on Cairo. Removing the condition is not enough, as we want to raise an error if anything goes wrong. The "right" way to do this would be to detect when cairo_image_surface_create_for_data returns a pointer to a nil surface and raise a ValueError then.

Interested in opening a pull request?

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