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

Bill Spitzak spitzak at gmail.com
Sat Mar 2 11:04:55 PST 2013

I may not have been very clear in my ImageMagic suggestion.

Cairo needs to provide some sample code that shows how to use a 
third-party library to read an image from disk and get it on-screen.

The existence of such code for Pango has pretty much evaporated all the 
requests to improve the toy font api to handle multiple fonts.

It would be ideal if this is an already-popular library that can be 
installed easily on a normal Linux distribution.

I'm pretty certain if there was just some example code that read a file 
using this library into a buffer and turned it into a cairo source, and 
perhaps drew it onto a destination, everybody would copy it, everybody 
would use this library, and people would stop using the png api.

On 02/27/2013 07:15 PM, Krzysztof Kosiński wrote:
> 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