All pastes #981528 Raw Edit

example

public c v1 · immutable
·published 2008-04-11 21:16 UTC
rendered paste body
/* create an in-memory/image-surface and generate a cairo-context from that */cairo_t*create_cairo_context (int               width,   /* 16 in your case         */		      int               height,  /* 16 in your case         */		      int               channels,/* only 3 or 4 makes sense */		      cairo_surface_t** surface, /* pass valid pointer      */		      unsigned char**   buffer)  /* pass valid pointer      */{	cairo_t* context = NULL;	*buffer = calloc (channels * width * height, sizeof (unsigned char));	if (!*buffer)	{		printf ("create_cairo_context() - Couldn't allocate buffer\n");		return NULL;	}	*surface = cairo_image_surface_create_for_data (*buffer,							CAIRO_FORMAT_ARGB32,							width,							height,							channels * width);	if (cairo_surface_status (*surface) != CAIRO_STATUS_SUCCESS)	{		free (*buffer);		printf ("create_cairo_context() - Couldn't create surface\n");		return NULL;	}	context = cairo_create (*surface);	if (cairo_status (context) != CAIRO_STATUS_SUCCESS)	{		free (*buffer);		printf ("create_cairo_context() - Couldn't create context\n");		return NULL;	}	return context;}GdkPixbuf* get_pixbuf_from_context (guchar* buffer,				    gint    channels,				    gint    width,				    gint    height){	GdkPixbuf* pixbuf = NULL;	pixbuf = gdk_pixbuf_new_from_data (buffer,					   GDK_COLORSPACE_RGB,					   channels == 4? TRUE : FALSE,					   8, /* usually save to assume :) */					   width,					   height,					   channels * width,                                           NULL,					   NULL);	return pixbuf;}cairo_t*         context = NULL;cairo_surface_t* surface = NULL;guchar*          buffer  = NULL;GdkPixbuf*       pixbuf  = NULL;context = create_cairo_context (16, 16, 3, &surface, &buffer);/* do your drawing here with cairo */pixbuf = get_pixbuf_from_buffer (buffer, 3, 16, 16);cairo_destroy (context);