[cairo] cairocffi and GDK-PixBuf for decoding JPEG and other image formats

Krzysztof Kosiński tweenk.pl at gmail.com
Wed Feb 27 19:15:36 PST 2013


2013/2/26 Simon Sapin <simon.sapin at exyr.org>:
> Hi,
>
> A long-standing feature request on cairo has been the ability to decode JPEG
> and other image formats, just like cairo_image_surface_create_from_png()
> does for PNG.
>
> The response has been that this does not belong in cairo itself.
>
> GDK-PixBuf can load many image formats, but uses a different internal format
> for pixel data. I just added some integration code in cairocffi, to wrap
> GDK-PixBuf and do the pixel conversion:
>
> http://pythonhosted.org/cairocffi/pixbuf.html
>
> (cairocffi is a drop-in alternative to pycairo.)

In Inkscape I wanted to avoid using gdk_cairo_set_source_pixbuf(),
since it creates a copy and therefore unnecessarily wastes memory.
This can be a problem when the SVG file contains several full-size
photos. I used a hack that involves shuffling around the components
and premultiplying alpha in the pixbuf data, then calling
cairo_image_surface_create_for_data(). This is followed by calling
cairo_set_user_data() with g_object_unref() as a destroy function to
store a reference to the GdkPixbuf in the image surface. Naturally, if
the pixbuf is not reverted to its original state before e.g. saving
it, the output will be garbled.

This hack could be avoided if there was a way to specify image formats
other than Cairo's default of native-endian ARGB with premultiplied
alpha, so that the data in a GdkPixbuf could be used to create a Cairo
image surface without modifications.

Regards, Krzysztof


More information about the cairo mailing list