[cairo] Trouble resizing (scaling) an XImage

Justin Anderson justin at rayonix.com
Mon Jun 27 11:27:28 PDT 2011

Thanks for your response, Uli.

Ugh...I was trying to clean up my code when putting in the email but made a
couple of mistakes.  The XPutImage is actually commented out in my running
code.  Please disregard it.  Also, the video feed is initially 8 bit but I
have another program grabbing the frames via V4L2 and piping them out as 24
bits.  Therefore, the xImage which cairo uses as a source is indeed 24 bits.
 Sorry for the confusion.

I am not waiting for an Expose event but I don't see why it would matter
since the images are drawn fine when I don't call the scale function.  It's
only when I call scale that the image is no longer displayed in the window.
 This is a stand alone window who's only purpose right now is to have these
images drawn in it (for testing).  If I can get this working, I will then
draw them in a Widget which is part of a larger application.

I have tried some other operations with cairo which aren't working for me
either (right before the call to set_source_surface).  I tried writing text
on the image:

cairo_select_font_face (cr_dest, "serif", CAIRO_FONT_SLANT_NORMAL,
cairo_set_font_size (cr_dest, 20.0);
cairo_move_to(cr_dest, 50, 50);
cairo_show_text(cr_dest, "Hello");

and I tried rotating the image 180 degrees:

cairo_rotate(cr_dest, M_PI);

Writing text and rotating don't do anything but the image is still
displayed, unaltered.  This is in contrast to the attempt to scale which
results in no image being displayed.

The corrected code is below:

//Create a simple X11 window for displaying the XImage...
   window = XCreateSimpleWindow(
      0, // x
      0, // y
      0,                          // border width
      BlackPixelOfScreen(screen), // border
      BlackPixelOfScreen(screen)  // background
   XMapRaised(display, window);

   //cairo stuff...
   cairo_surface_t *surface_dest = cairo_xlib_surface_create(display,
window, XDefaultVisualOfScreen(screen), width, height);
   cairo_t *cr_dest = cairo_create(surface_dest);

   cairo_surface_t *surface_source =
cairo_image_surface_create_for_data((unsigned char*)(xImage->data),
CAIRO_FORMAT_RGB24, xImage->width, xImage->height, xImage->bytes_per_line);

   cairo_scale(cr_dest, (double)width / xImage->width, (double)height /

   cairo_set_source_surface(cr_dest, surface_source, 0, 0);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cairographics.org/archives/cairo/attachments/20110627/0c25cec5/attachment.htm>

More information about the cairo mailing list