#include #include #include #include /* #undef CAIRO_HAS_QUARTZ_SURFACE */ cairo_surface_t * create_surface (int width, int height, int channels) { #ifdef CAIRO_HAS_QUARTZ_SURFACE return cairo_quartz_surface_create (CAIRO_FORMAT_ARGB32, width, height); #else /* create cairo-surface/context to act as Open_gL-texture source */ unsigned char *buffer = calloc (channels * width * height, sizeof (unsigned char)); return cairo_image_surface_create_for_data (buffer, CAIRO_FORMAT_ARGB32, width, height, channels * width); #endif } void destroy_surface (cairo_surface_t *surface) { #ifndef CAIRO_HAS_QUARTZ_SURFACE free (cairo_image_surface_get_data (surface)); #endif cairo_surface_destroy (surface); } void render (cairo_t *ctx, cairo_pattern_t *gradient) { cairo_save (ctx); cairo_set_operator (ctx, CAIRO_OPERATOR_OVER); cairo_set_source (ctx, gradient); cairo_paint (ctx); cairo_restore (ctx); cairo_surface_flush (cairo_get_target (ctx)); } #define WIDTH 256 #define HEIGHT 256 int main () { cairo_surface_t *surface = create_surface (WIDTH, HEIGHT, 4); cairo_t *ctx = cairo_create (surface); cairo_pattern_t *gradient = cairo_pattern_create_linear (0, 1, 1, 0); cairo_pattern_add_color_stop_rgb (gradient, 0, 1, 0, 0); cairo_pattern_add_color_stop_rgb (gradient, 0.5, 0, 1, 0); cairo_pattern_add_color_stop_rgb (gradient, 1, 0, 0, 1); cairo_scale (ctx, WIDTH, HEIGHT); render (ctx, gradient); render (ctx, gradient); cairo_surface_write_to_png (surface, "frame.png"); cairo_destroy (ctx); destroy_surface (surface); }