项目中需要插入图片,并旋转任意角度。我用的GTK版本为windows32bit 2.24,是从官网下的,这版的API中关于rotate pixbuf的api只有一个简单的旋转90°倍数的函数:gdk_pixbuf_rotate_simple (),没有旋转任意角度的函数。google了一天,终于解决了。方式如下:
STEP 1
搜到了这个bug,他的附件实现了旋转任意角度,但是这个附件文件不全,我添加了一个gdk-pixbuf-private.h文件(从这里找的)。并修改了这几个文件的#include语句,因为他提供的patch原本是编译gtk时使用的,但是我用的gtk是已经编译好了的,无法自己重新编译,因此必须把#include语句改了,这样把这个patch当成我自己项目的代码用就可以了。
STEP 2
把patch中的那几个.c.h文件放到同一个make目录下,编译运行patch自带的demo,测试效果如下图:
上图是运行demo菜单的image的结果,it works!
patch API说明:
gdk_pixbuf_rotate_matrix_开头的都是一些矩阵工具方法,不重要。
最重要的是这个函数
void gdk_pixbuf_rotate (GdkPixbuf* dst, gdouble dst_x, gdouble dst_y, gdouble* matrix, GdkPixbuf* src, gdouble src_center_x, gdouble src_center_y, gint* result_rect_x, gint* result_rect_y, gint* result_rect_width, gint* result_rect_height, GdkInterpType interp_type);
这个函数把src pixbuf通过rotate matrix转换后写到dst pixbuf中,
dst: 目标pixbuf
dst_x: 目标pixbuf的中心
dst_y: 目标pixbuf的中心
matrix: 转换矩阵
s r c: 源pixbuf
src_center_x: 源pixbuf的中心
src_center_y: 源pixbuf的中心
result_rect_开头的四个参数: 旋转后图形的坐标,以目标pixbuf坐上角为坐标系原点。宽高
调用方法可以参考demo,或我的代码片
段:
MyPicture *picture = MY_PICTURE(self); MyPicturePrivate* priv = MY_PICTURE_GET_PRIVATE (picture); GdkRectangle rect; if (priv->pixbuf) g_object_unref (priv->pixbuf); priv->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, picture->rotateSideLen, picture->rotateSideLen); gdk_pixbuf_fill (priv->pixbuf, 0); gdouble m[4]; gdk_pixbuf_rotate_martix_fill_for_rotation(m, picture->angle); gdk_pixbuf_rotate (priv->pixbuf, picture->rotateSideLen/2, picture->rotateSideLen/2, m, priv->orignPixbuf, picture->orignWidth/2, picture->orignHeight/2, &rect.x, &rect.y, &rect.width, &rect.height, GDK_INTERP_NEAREST); gdk_draw_pixbuf (appState->pixmap, appState->drawingArea->style->white_gc, priv->pixbuf, 0, 0, picture->rotateX + appState->orignX, picture->rotateY + appState->orignY, -1, -1, GDK_RGB_DITHER_NORMAL, 0, 0);
由于这个patch只支持GDK_INTERP_NEAREST,所以旋转后精度下降的比较厉害,变糙了。哪位同学发现更好的解决方法,请留言,谢谢~
PS: 没有用cairo处理图片,是因为cairo在处理png时,在我的项目中透明的地方都变成黑色了(原因未知,我是用cairo往pixmap上画的),而且cairo只支持png,不支持jpg等等,于是索性不用cairo画图片了。