Thread: Help with Cairo graphics from data

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    30

    Help with Cairo graphics from data

    Hi folks,

    I have been trying to create an image from pixel data using cairo, but it is not working.

    Here is a simple code that generates a .png file using cairo:

    Code:
    //cc -o hello $(pkg-config --cflags --libs cairo) hello.c
    
    #include <cairo.h>
    
    #define ROWS 240
    #define COLS 80
    
    
    
    int
    main (int argc, char *argv[])
    {
            cairo_surface_t *surface =
                cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 240, 80);
            cairo_t *cr =
                cairo_create (surface);
    
    
            cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
            cairo_set_font_size (cr, 32.0);
            cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
            cairo_move_to (cr, 10.0, 50.0);
            cairo_show_text (cr, "Hello, world");
    
    
            cairo_destroy (cr);
            cairo_surface_write_to_png (surface, "hello.png");
            cairo_surface_destroy (surface);
            return 0;
    }
    Here is the pixel data array bw that I want to use in the code above:
    Code:
     unsigned char bw[ROWS * COLS] = {0};
      int r, c;
     
      for (r = 0; r < ROWS; r++)
        for (c = 0; c < COLS; c++)
          if (r / 20 % 2 && c / 20 % 2)
            bw[r*COLS + c] = 255;
    Is this the function I should use to plot the array bw: cairo_image_surface_create_for_data (https://www.cairographics.org/manual-1.0.2/cairo-Image-Surfaces.html#cairo-image-surface-create-for-data)?

    If so what is the value I should use for int stride?

    Should cairo_format_t format be assigned CAIRO_FORMAT_ARGB32?
    Last edited by hikerFreak; 04-02-2017 at 02:53 PM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    The code you've posted just creates a png file. It doesn't render to the screen. Do you just want a png file of the grid pattern? Easy enough.

    You need to pass 32-bit rgba values to cairo. You can tell the surface to ignore the alpha value by specifying the RGB24 format, but pixel data still needs to be 32-bit (4 byte) values.
    Code:
    //gcc -o cairo cairo.c $(pkg-config --cflags --libs cairo)
    
    #include <cairo.h>
    #include <stdlib.h>
    
    #define ROWS 200
    #define COLS 300
    
    typedef unsigned char uchar;
    
    int main (int argc, char *argv[])
    {
        cairo_format_t format = CAIRO_FORMAT_RGB24;
        int stride = cairo_format_stride_for_width(format, COLS);
        uchar *pix = calloc(stride * ROWS, 1);
    
        for (int r = 0; r < ROWS; r++)
            for (int c = 0; c < COLS; c++)
                if (r / 20 % 2 && c / 20 % 2) {
                    uchar *p = &pix[r*stride + c*4];
                    p[0] = p[1] = p[2] = p[3] = 255;
                    // p[3] is the alpha, which is unused with RGB24
                }
    
        cairo_surface_t *surface =
            cairo_image_surface_create_for_data(
                pix, format, COLS, ROWS, stride);
    
        cairo_surface_write_to_png (surface, "grid.png");
        cairo_surface_destroy (surface);
        free(pix);
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    30
    Once again algorism, thank you!

    You've been very helpful, both for this question and for my previous one on plotting images based on data arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Move an object in cairo
    By tvaz in forum C Programming
    Replies: 1
    Last Post: 01-14-2016, 04:36 PM
  2. 3D Graphics with quaternion data
    By mrm63 in forum C++ Programming
    Replies: 1
    Last Post: 07-09-2014, 02:58 AM
  3. CAIRO - error when converting txt to pdf
    By leonardoadoado in forum C++ Programming
    Replies: 17
    Last Post: 12-05-2012, 01:38 PM
  4. cairo, rasterizing text
    By javaeyes in forum C Programming
    Replies: 1
    Last Post: 03-06-2012, 08:51 PM
  5. Cairo and GTK, still need a pixmap?
    By TriKri in forum Linux Programming
    Replies: 0
    Last Post: 03-18-2008, 04:18 PM

Tags for this Thread