[cairo] Get pixeldata from image

Andrew McRae amcrae at employees.org
Sun Nov 19 11:29:36 PST 2006


On Sun, 2006-11-19 at 12:57 +0100, Lobais wrote:
> Hi, it is possible to get the image data of an image, such that one
> would be able to e.g. get red, green and blue from pixel 8 row 4? 
> 
> -- 
> Best Regards, Thomas 

The easiest way (from what I know) is to use
cairo_image_surface_get_data and friends. As long
as you know it's a RGB or ARGB format (you can check
with cairo_image_surface_get_format), it's
going to be a 32 bit pixel element image.

Here's a sample code snippet:

  unsigned char *data = cairo_image_surface_get_data(surface);
  int stride = cairo_image_surface_get_stride(surface);

  uint32_t *pixelptr;
  ...
      pixelptr = (uint32_t *)(data + 4 * stride); // row 4
      my_pixel = pixel[8];                        // pixel 8
      red = (pixel[8] >> 16) & 0xFF;

Cheers,
AMc



More information about the cairo mailing list