2011-08-05 62 views
1

這個問題幾乎總結了我的問題。我有一個GtkDrawingArea,其表面(cairo_surface_t)的格式爲CAIRO_FORMAT_INVALID(由默認爲),即該表面的數據爲「不存在或不支持此格式」。從GtkDrawingArea獲取座標的像素顏色

有沒有辦法用我選擇的任何格式創建我的GtkDrawingArea表面的副本?然後,我可以查詢知道其格式的數據。

回答

0

既然目標是獲得彩色像素,以下爲我工作:

typedef struct{ 
guint16 red; 
guint16 green; 
guint16 blue; 
guint16 alpha; 
} color_struct; 

color_struct get_pixel_pixbuf(double x,double y,GdkPixbuf *pixbuf,unsigned char *pixels){ 
    color_struct color; 
    guchar *p; 
    p = pixels + ((int)y) * gdk_pixbuf_get_rowstride (pixbuf) + ((int)x) * gdk_pixbuf_get_n_channels(pixbuf); 

    color.red = p[0]; 
    color.green = p[1]; 
    color.blue = p[2]; 
    color.alpha = p[3]; 

    return color; 
} 

GtkWidget *drawingarea; 
GdkPixbuf *surface_pixbuf = gdk_pixbuf_get_from_drawable(NULL,GDK_DRAWABLE(drawingarea->window),gdk_colormap_get_system(),0,0,0,0,drawingarea->allocation.width,drawingarea->allocation.height); 
pixbuf_pixels = gdk_pixbuf_get_pixels (surface_pixbuf); 


color_struct *pixel_color = get_pixel_pixbuf(someX,someY,surface_pixbuf,pixbuf_pixels); 

由於繪圖區域的類型是Xlib的(不是像面),這是沒有必要創建一個副本後一種類型。