[cairo] Call to cairo_paint triggers an assertion

Tim Corio tcorio at rochester.rr.com
Sat Jan 2 19:15:05 PST 2010


I'm working on a small project that will aid in comparing two images.
As part of this application the user will be able to rotate the images,
but I'm having trouble just getting the attached demo application to
rotate an image.

I plan on using cairo_rotate to do the rotation part of this
application, but I'm having trouble just copying an image through ciaro.
The demo application below demonstrates the problem I'm having.

I'm new to both GTK and Cairo.  I think I'm going about this in a
reasonable way, but if there's a better solution than using cairo I'm
happy ditch it.

When the program runs the cairo_paint command I get the following error
at the command line and the application aborts:

g4: cairo-xlib-surface.c:934: _draw_image_surface: Assertion `ret != 0'
failed.
Aborted

Thank You,
Tim



#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;

  gtk_init (&argc, &argv);

  blue.red=0x0000; blue.green=0x0000; blue.blue=0xffff;

  black.red  =0x0000; black.green  =0x0000; black.blue  =0x0000;
 
  gdk_color_alloc(gdk_colormap_get_system(),&blue);
  gdk_color_alloc(gdk_colormap_get_system(),&black);

  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), "Rotate");
  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;
  GdkColorspace    colorspace;
  cairo_surface_t *surface_source;

  datasize   = WIDTH * HEIGHT * 4;
  data       = g_malloc0 (datasize);
  colorspace = gdk_pixbuf_get_colorspace (pxb_orig);

  surface_source = cairo_image_surface_create_for_data (
                         gdk_pixbuf_get_pixels(pxb_orig),
                         CAIRO_FORMAT_RGB24,
                         WIDTH,
                         HEIGHT,
                         gdk_pixbuf_get_rowstride(pxb_orig));

  pxm_dest = gdk_pixmap_create_from_data (window->window, /* drawable */
                                          data,           /* data */
                                          WIDTH,   /* width */
                                          HEIGHT,  /* height */
                                          24,      /* depth */
                                          &black,  /* forground */
                                          &blue);  /* background */

  gdk_drawable_set_colormap(pxm_dest, gdk_rgb_get_colormap());
  cr = gdk_cairo_create (pxm_dest);
  cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
  cairo_set_source_surface (cr, surface_source, 0, 0);
  cairo_paint(cr);
  gtk_image_set_from_pixmap (GTK_IMAGE(img_dest), pxm_dest, NULL);

  /* ************************************************** */

  gtk_main();
}




More information about the cairo mailing list