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?