2012-08-06 93 views
2

我正在写需要做下面的一个小演示应用程序保存PNG图像:由度 x个旋转和使用开罗

  1. 阅读参考PNG图像文件
  2. 旋转PNG图像
  3. 将新图像保存为动画的框架
  4. 随着上次旋转的结果返回到步骤2,直到完成旋转。

结果应该是一系列PNG图像文件,以不同的旋转角度显示图像。这些图像然后将以某种方式组合成电影或动画GIF

我创建了下面的代码,它试图做一个旋转:

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

的问题是原始图像的90度旋转后,所产生的保存的图像旋转,但并不完全正确。我试过重新排列cairo的调用顺序,认为它可能与表面状态或环境有关。

的开始和结束图片如下:

Results

我缺少什么?

回答

4

您正在打开原始图像作为要绘制的曲面。打开您的原始.png并将其作为通过cairo_set_source_surface,并将其绘制到通过cairo_image_surface_create创建的新的图像表面。

开始通过更换:

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

有:

w = cairo_image_surface_get_width (image); 
h = cairo_image_surface_get_height (image); 
cairo_surface_t* tgt = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h); 

cr = cairo_create(tgt); 

那当然,你要救出来tgt,不image,到文件,并做好清理工作。

+0

非常感谢。我感谢帮助,并认为我明白我做错了什么。 – Chimera 2012-08-06 21:12:52