[cairo] [cairomm] inline Images

Daniel Amelang daniel.amelang at gmail.com
Wed Apr 4 23:15:00 PDT 2007

On 4/4/07, Carl Worth <cworth at cworth.org> wrote:
> On Wed, 28 Mar 2007 05:30:38 -0700 (PDT), The-Kenny wrote:
> > Is there a way to store images (Png) in the sourcecode?
> For reference, cairo_image_surface_create_from_png_stream can be used
> with an array that contains actual PNG data, (that is, precisely the
> contents of a PNG file). This takes a very little bit of effort,
> (writing the trivial read function).

I just did this a couple weeks ago when I wanted to use the cairo logo
in a benchmark/demo program. First, I used bin2c to embed the png data
as an array in my C source. Then I used this code to load it into a
cairo image surface:

static cairo_status_t
read_cairo_logo_as_png_from_memory (void *closure, unsigned char *data,
                                    unsigned int length)
    int *offset = (int *)closure;

    if ((*offset + length) > sizeof (cairo_logo_as_png))

    memcpy (data, cairo_logo_as_png + *offset, length);
    *offset = *offset + length;


/*...in some other function... */
int offset = 0;
cairo_surface_t *image_surface;

image_surface = cairo_image_surface_create_from_png_stream (
    read_cairo_logo_as_png_from_memory, &offset

You could probably generalize it for loading arbitrary pngs, but I'll
leave that as an exercise to the reader :)


More information about the cairo mailing list