Interoperability issue with pycairo for ImageSurface.create_for_data and numpy data
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 https://github.com/Kozea/cairocffi/blob/f1984d644bbc462ef0ec33b97782cf05733d7b53/cairocffi/surfaces.py#L725-L727 appears to generate the correct output without any error message. For this reason, it seems like the condition is superfluous or wrong.
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?