[cairo] Issue using cairo_surface_get_data

emj10 at vt.edu emj10 at vt.edu
Thu Jul 30 06:45:03 PDT 2009


I've been trying a simple test program with cairo (1.2.4) to grab the data from
a cairo_surfce_t after displaying some text.  I also output a png to visually
verify that indeed data is written to the surface.  Yet there is no output in a
text file when I try to write the surface data using cairo_surface_get_data(). 
Did I overlook something obvious?

Below is the source code:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cairo.h>

static void show_usage(char *name)
{
    printf("%s <file-name>\n",name);
}

int main(int argc, char *argv[])
{
    cairo_surface_t *surface;
    cairo_t         *cr;
    unsigned char   *data;
    FILE            *file;
    char            *file_png;
    if (argc < 2)
    {
        show_usage(argv[0]);
        return 1;
    }
    surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,400,400);
    cr = cairo_create(surface);
    cairo_set_source_rgb(cr,0.1,0.1,0.1);
    cairo_select_font_face(cr,"Arial",CAIRO_FONT_SLANT_NORMAL,
        CAIRO_FONT_WEIGHT_BOLD);
    cairo_set_font_size(cr,13);
    cairo_move_to(cr,20,30);
    cairo_show_text(cr,"Display some text");
    cairo_destroy(cr);
    file_png = (char *) malloc(strlen(argv[1])+50);
    sprintf(file_png,"%s-surface_png.png",argv[1]);
    cairo_surface_write_to_png(surface,file_png);
    free(file_png);
    data = cairo_image_surface_get_data(surface);
    file = fopen(argv[1],"w");
    if (!file){
        fprintf(stderr,"ERROR: could not open %s\n",argv[1]);
        return 1;
    }
    fprintf(file,"Data: \n%s\n",data);
    fclose(file);
    cairo_surface_destroy(surface);
    return 0;
}

Thanks in advance,
Eric


More information about the cairo mailing list