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

Andrea Canciani ranma42 at gmail.com
Mon Aug 6 20:06:32 PDT 2012


On Mon, Aug 6, 2012 at 12:10 PM, Jim Norton <jimnorton at jimnorton.org> wrote:
> 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);

You're using the same surface as destination and source.
This causes undefined behavior.

Andrea

>     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.
>
> --
> cairo mailing list
> cairo at cairographics.org
> http://lists.cairographics.org/mailman/listinfo/cairo


More information about the cairo mailing list