2012-08-06 56 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