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 shown below:
What am I missing?