[cairo] Need help with creating a series of rotated PNG images

Jim Norton jimnorton at jimnorton.org
Mon Aug 6 12:10:29 PDT 2012


Hello,

I've read my examples of Cairo code and used the search engines to get  
this problem resolved, but for some reason I'm not understanding what  
I'm doing wrong.

I'm writing a small demo application that needs to do the following:

     Read in reference PNG image file
     Rotate PNG image by x number of degrees
     Save new image as a frame of an animation
     With result of last rotation go back to step 2 until done rotating.

The result should be a series of PNG image files showing the image at  
various degrees of rotation. These images will then somehow be  
combined into a movie or animated GIF.

I've created the following code which attempts to do one rotation:

#include <cairo.h>
#include <math.h>

/**** prototypes *******/
void Rotate( cairo_surface_t *image, int degress, const char *fileName );
double DegreesToRadians( double degrees );
/***********************/

double DegreesToRadians( double degrees )
{
     return((double)((double)degrees * ( (double)M_PI/(double)180.0 )));
}

void Rotate( cairo_surface_t *image, int degrees, const char *fileName )
{
     int w, h;
     cairo_t *cr;

     cr = cairo_create(image);
     w = cairo_image_surface_get_width (image);
     h = cairo_image_surface_get_height (image);

     cairo_translate(cr, w/2.0, h/2.0);
     cairo_rotate(cr, DegreesToRadians( degrees ));
     cairo_translate(cr, - w/2.0, -h/2.0);

     cairo_set_source_surface(cr, image,  0, 0);
     cairo_paint (cr);


     cairo_surface_write_to_png(image, fileName );
     cairo_surface_destroy (image);
     cairo_destroy(cr);
}

int main()
{
     cairo_surface_t *image = cairo_image_surface_create_from_png  
("images/begin.png");
     Rotate(image, 90, "images/end.png");
     return( 0 );
}

The problem is that after the rotation of the original image by 90  
degrees, the resulting saved image is rotated but not quite correctly.  
I've tried rearranging the order of the cairo calls thinking maybe it  
has to do with the state of the surface or the context.

The begin and end images are here: http://i.stack.imgur.com/s34l2.png
What am I missing?

I've also posted this question to stackoverflow.com if that makes it easier.
http://stackoverflow.com/questions/11834243/rotate-and-save-png-image-using-cairo

Any pointers will be greatly appreciated. If somebody would help me  
out I'd give you my first born... well ok maybe not... but perhaps a  
donation of some sort?

Thank you.



More information about the cairo mailing list