[cairo] Call to cairo_paint triggers an assertion
Tim Corio
tcorio at rochester.rr.com
Wed Jan 6 11:31:53 PST 2010
Success!
I chanced upon a function called gdk_cairo_set_source_pixbuf(...). This
let me get rid of the Cairo Surface. Now, this code works.
For review: This demo program loads an image file called orig.jpg,
rotates it, then draws it to a GtkImage that had previously been created
by loading an image called blank.jpg.
Both these test images are 200x200 pixels. I'm assuming the handling of
images of arbitrary size is a solved problem in GTK.
I appreciate the help I've gotten on this list. You've been patient and
helpful with my newbie questions.
Thank You,
Tim Corio
#include <gtk/gtk.h>
#define WIDTH 200
#define HEIGHT 200
static void destroy( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}
int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *box_image;
GdkPixbuf *pxb_orig;
GdkPixmap *pxm_dest;
GtkImage *img_orig;
GtkImage *img_dest;
GError *gerror = NULL;
cairo_t *cr = NULL;
GdkColor blue;
GdkColor black;
GdkColor white;
gtk_init (&argc, &argv);
blue.red =0x0000; blue.green =0x0000; blue.blue =0xffff;
black.red =0x0000; black.green =0x0000; black.blue =0x0000;
white.red =0xffff; white.green =0xffff; white.blue =0xffff;
gdk_color_alloc(gdk_colormap_get_system(),&blue);
gdk_color_alloc(gdk_colormap_get_system(),&black);
gdk_color_alloc(gdk_colormap_get_system(),&white);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (G_OBJECT (window),
"destroy",
G_CALLBACK (destroy),
NULL);
gtk_window_set_title (GTK_WINDOW (window), "GtkScrolledWindow
example");
gtk_container_set_border_width (GTK_CONTAINER (window), 5);
gtk_widget_set_size_request (window, 700, 300);
box_image = gtk_hbox_new (TRUE, 5);
pxb_orig = gdk_pixbuf_new_from_file("orig.jpg", &gerror);
img_orig = (GtkImage *)gtk_image_new_from_pixbuf (pxb_orig);
gtk_container_add (GTK_CONTAINER (window), box_image);
gtk_box_pack_start (GTK_BOX (box_image), GTK_WIDGET(img_orig), FALSE,
FALSE, 0);
img_dest = (GtkImage *)gtk_image_new_from_file ("blank.jpg");
gtk_box_pack_start (GTK_BOX (box_image), GTK_WIDGET(img_dest), FALSE,
FALSE, 0);
gtk_widget_show ((GtkWidget *)img_dest);
gtk_widget_show ((GtkWidget *)img_orig);
gtk_widget_show (box_image);
gtk_widget_show (window);
/* ************************************************** */
/* Draw to the output image. */
guchar *data;
int datasize;
datasize = WIDTH * HEIGHT * 4;
data = g_malloc0 (datasize);
pxm_dest = gdk_pixmap_create_from_data (window->window, /* drawable
*/
data, /* data */
WIDTH, /* width */
HEIGHT, /* height */
24, /* depth */
&black, /*
forground */
&white); /*
background */
gdk_drawable_set_colormap (pxm_dest, gdk_colormap_get_system());
cr = gdk_cairo_create (pxm_dest);
cairo_rotate (cr, 0.5);
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
gdk_cairo_set_source_pixbuf (cr, pxb_orig, 0, 0);
cairo_paint(cr);
gtk_image_set_from_pixmap (GTK_IMAGE(img_dest), pxm_dest, NULL);
/* ************************************************** */
gtk_main();
return 0;
}
More information about the cairo
mailing list