[cairo] cairo_surface_write_to_png_stream example

Daniel Amelang daniel.amelang at gmail.com
Sat May 19 17:42:57 PDT 2007


On 5/18/07, ken carlino <ken.carlino at gmail.com> wrote:
> Hi,
>
> Can you please tell me where can I find an example of how to use
> cairo_surface_write_to_png_stream? For example, I want to write the
> result png in a memory buffer.

Hi Ken, this should get you on the right track:

typedef struct
{
    unsigned char *current_position;
    unsigned char *end_of_array;
} png_stream_to_byte_array_closure_t;

static cairo_status_t
write_png_stream_to_byte_array (void *in_closure, const unsigned char *data,
                                                unsigned int length)
{
    png_stream_to_byte_array_closure_t *closure =
        (png_stream_to_byte_array_closure_t *) in_closure;

    if ((closure->current_position + length) > (closure->end_of_array))
        return CAIRO_STATUS_WRITE_ERROR;

    memcpy (closure->current_position, data, length);
    closure->current_position += length;

    return CAIRO_STATUS_SUCCESS;
}

...somewhere in your code...

closure.current_position = byte_array;
closure.end_of_array = byte_array + sizeof (byte_array);
cairo_surface_write_to_png_stream (surface, write_png_stream_to_byte_array,
                                                    &closure);

Dan


More information about the cairo mailing list