使用Cairo旋转并保存PNG图像
我正在编写一个小型演示应用程序,它需要执行以下操作: 读入参考PNG图像文件 将PNG图像旋转x度 将新图像另存为动画帧 根据上次旋转的结果,返回步骤2,直到旋转完成。 结果应该是一系列PNG图像文件,以不同的旋转角度显示图像。这些图像将以某种方式组合成电影或动画GIF 我创建了以下代码,尝试执行一次旋转:使用Cairo旋转并保存PNG图像,c,graphics,2d,cairo,C,Graphics,2d,Cairo,我正在编写一个小型演示应用程序,它需要执行以下操作: 读入参考PNG图像文件 将PNG图像旋转x度 将新图像另存为动画帧 根据上次旋转的结果,返回步骤2,直到旋转完成。 结果应该是一系列PNG图像文件,以不同的旋转角度显示图像。这些图像将以某种方式组合成电影或动画GIF 我创建了以下代码,尝试执行一次旋转: #include <cairo.h> #include <math.h> /**** prototypes *******/ void Rotate( cairo_
#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度后,生成的保存图像会旋转,但并不完全正确。我试着重新安排开罗电话的顺序,认为这可能与表面状态或背景有关
开始和结束图像如下所示:
我错过了什么 您正在打开原始图像作为要绘制的曲面。打开原始的.png,通过cairo\u set\u source\u surface将其用作源,然后将其绘制到通过cairo\u image\u surface\u create创建的新的空图像表面上 首先替换:
cr = cairo_create(image);
w = cairo_image_surface_get_width (image);
h = cairo_image_surface_get_height (image);
与:
当然,您需要将tgt而不是图像保存到文件中,然后进行清理。您将打开原始图像作为要绘制的曲面。打开原始的.png,通过cairo\u set\u source\u surface将其用作源,然后将其绘制到通过cairo\u image\u surface\u create创建的新的空图像表面上 首先替换:
cr = cairo_create(image);
w = cairo_image_surface_get_width (image);
h = cairo_image_surface_get_height (image);
与:
当然,您需要将tgt而不是图像保存到文件中,然后进行清理。非常感谢。我感谢你的帮助,我想我明白我做错了什么。非常感谢。我感谢你的帮助,我想我明白我做错了什么。