I can use XGetPixel()
to get a pixel from an XImage
. What do I use to get a pixel from a Pixmap
?
2 Answers
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
.

- 1
- 1

- 4,023
- 4
- 26
- 43
Generally it's a bad idea from performance point of view: never read from screen, only push to it. If you need to get pixel state, maintain local buffer of screen. If you can't modify program you are using, then +1 to Jonny Henly's answer: do a GetImage request first do download a region containing your pixel first, then read locally. If you want to access multiple pixels in a loop it's better to grab them all in one request

- 24,905
- 4
- 62
- 75
-
My program is reading from the root window (the desktop) at the current pointer's position and displaying it, modified, in a window. So I think I have no choice but to "read from screen", right? – Harvey Jan 17 '15 at 02:41
-
yeah, probably no choice – Andrey Sidorov Jan 17 '15 at 12:10