开发者

Getting a coordinate's pixel color from a GtkDrawingArea

T开发者_StackOverflow中文版he question pretty much sums my question. I've a GtkDrawingArea and its surface (cairo_surface_t) has format CAIRO_FORMAT_INVALID (by default), i.e. "no such format exists or is supported" for that surface's data.

Is there a way to create a copy of my GtkDrawingArea surface with any format I choose? Then I'd be able to query the data knowing its format.


Since the goal was to get the color pixel, the following worked for me:

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

Since the type of the drawing area is xlib (not image surface) it wasn't necessary to create a copy of the latter type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜