[cairo] Issue using cairo_surface_get_data

Dominic Lachowicz domlachowicz at gmail.com
Thu Jul 30 07:36:03 PDT 2009


cairo_image_surface_get_data() returns ARGB pixels. A pixel value
could be '0', which is the same as C's end of string marker. So when
you fprintf("%s") the data, there are probably embedded NULLs in the
data, which would cause printf to stop printing things.

What you want to do is fwrite the data. Get the length from the
image's height and stride.

On Thu, Jul 30, 2009 at 9:45 AM, <emj10 at vt.edu> wrote:
> 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
> _______________________________________________
> cairo mailing list
> cairo at cairographics.org
> http://lists.cairographics.org/mailman/listinfo/cairo
>



-- 
"I like to pay taxes. With them, I buy civilization." --  Oliver Wendell Holmes


More information about the cairo mailing list