Search code examples
x11pixmap

How to get pixel color from a Pixmap


I can use XGetPixel() to get a pixel from an XImage. What do I use to get a pixel from a Pixmap?


Solution

  • It would be nice if there was at least one function to pull a pixel from the server's drawable. Sadly, there isn't one. However the following might be of some use.

    Since Pixmap is a Drawable you can pass XGetImage() the Pixmap which will return a pointer to an XImage. Now that you have an XImage you can use XGetPixel().

    XGetImage Parameters:

    XImage *XGetImage(display, d, x, y, width, height, plane_mask, format)
            Display *display;
            Drawable d;
            int x, y;
            unsigned int width, height;
            unsigned long plane_mask;
            int format;
    

    Better yet you could have a pre-created XImage and pass it, along with the Pixmap to XGetSubImage(). You can grab a single pixel by passing a width and height both set to 1, and then use XGetPixel() on your XImage.

    XGetSubImage Parameters:

    XImage *XGetSubImage(display, d, x, y, width, height, plane_mask, format, dest_image, dest_x, 
                         dest_y)
          Display *display;
          Drawable d;
          int x, y;
          unsigned int width, height;
          unsigned long plane_mask;
          int format;
          XImage *dest_image;
          int dest_x, dest_y;
    

    Note: XGetSubImage() returns a pointer to the same XImage structure specified by dest_image.