Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Xlib question - efficient way to get pixel value?

709 views
Skip to first unread message

bolta...@yahoo.co.uk

unread,
Oct 15, 2009, 6:09:01 AM10/15/09
to
Hi

I need to constantly get the pixel values in a drawable. The only way I can
see to do this is call XGetImage() then XGetPixel() , but calling XGetImage()
every iteration seems horribly inefficient. Is there a way I can get a pixel
value directly instead of having to copy the entire window contents each time?

Thanks for any help

B2003


TR Oltrogge

unread,
Oct 15, 2009, 11:09:45 AM10/15/09
to

<bolta...@yahoo.co.uk> wrote in message news:hb6sbt$rbf$1...@aioe.org...
If the drawable is not changing then you only need to execute XGetImage()
once and the entire
window contents will now be in your local memory space (instead of the
server's) and you
now use XGetPixel() in a loop to pull out any individual pixels you want.
XGetPixel()
runs quickly since it does not involve communication with the server.


bolta...@yahoo.co.uk

unread,
Oct 15, 2009, 11:19:46 AM10/15/09
to
On Thu, 15 Oct 2009 11:09:45 -0400
"TR Oltrogge" <trolt...@verizon.net> wrote:
><bolta...@yahoo.co.uk> wrote in message news:hb6sbt$rbf$1...@aioe.org...
>> Hi
>>
>> I need to constantly get the pixel values in a drawable. The only way I
>> can
>> see to do this is call XGetImage() then XGetPixel() , but calling
>> XGetImage()
>> every iteration seems horribly inefficient. Is there a way I can get a
>> pixel
>> value directly instead of having to copy the entire window contents each
>> time?
>>
>> Thanks for any help
>>
>> B2003
>>
>If the drawable is not changing then you only need to execute XGetImage()
>once and the entire

Thats the problem though - it is changing in quite a complex way because its
a game. Trying to work out what exact pixels will be where from the drawing
commands sent would be a thankless task.

>window contents will now be in your local memory space (instead of the
>server's) and you
>now use XGetPixel() in a loop to pull out any individual pixels you want.
>XGetPixel()
>runs quickly since it does not involve communication with the server.

But calling XGetImage() all the time I imagine would not be quick.

B2003

TR Oltrogge

unread,
Oct 15, 2009, 11:33:26 AM10/15/09
to

<bolta...@yahoo.co.uk> wrote in message news:hb7eii$kv5$1...@aioe.org...

> On Thu, 15 Oct 2009 11:09:45 -0400
> "TR Oltrogge" <trolt...@verizon.net> wrote:
>><bolta...@yahoo.co.uk> wrote in message news:hb6sbt$rbf$1...@aioe.org...
>>> Hi
>>>
<snip>

>>>
>>If the drawable is not changing then you only need to execute XGetImage()
>>once and the entire
>
> Thats the problem though - it is changing in quite a complex way because
> its
> a game. Trying to work out what exact pixels will be where from the
> drawing
> commands sent would be a thankless task.
>
>>window contents will now be in your local memory space (instead of the
>>server's) and you
>>now use XGetPixel() in a loop to pull out any individual pixels you want.
>>XGetPixel()
>>runs quickly since it does not involve communication with the server.
>
> But calling XGetImage() all the time I imagine would not be quick.
>
> B2003
>
Correct, XGetImage is an expensive operation.
If you only want a portion (even a single pixel) of a drawable you can use
XCopyArea()
with width and height equal to one to read a single pixel from the server
into your code.
This is sort of the opposite of XDrawPoint(). I don't know why the
implementors of
XWindow didn't include an XReadPoint() but XCopyArea() with dimensions of
one will
accomplish the same thing.

Tim


bolta...@yahoo.co.uk

unread,
Oct 15, 2009, 11:59:47 AM10/15/09
to

Ah ok , so I guess I'd create a drawable 1x1 pixel , copy into that then
do XGetImage and XGetPoint on that 1x1 drawable?

You'd think that something like XReadPoint() would be an obvious thing
to include since many other graphics systems had it but... oh well.

Thanks for the info!

B2003

TR Oltrogge

unread,
Oct 15, 2009, 3:58:01 PM10/15/09
to

<bolta...@yahoo.co.uk> wrote in message news:hb7gtj$ocd$1...@aioe.org...

> On Thu, 15 Oct 2009 11:33:26 -0400
<snip>

> Ah ok , so I guess I'd create a drawable 1x1 pixel , copy into that then
> do XGetImage and XGetPoint on that 1x1 drawable?
>
> You'd think that something like XReadPoint() would be an obvious thing
> to include since many other graphics systems had it but... oh well.
>
> Thanks for the info!
>
> B2003
>
BTW, I'm not an XWindow expert. I've written maybe half-a-dozen programs in
C (not C++) that call the Xlib routines
to write to the screen. I've never tried to read back into my program the
contents of a window since my program logic
wrote it in the first place and I know what it is (heh, heh). I run under
Linux, BTW, when I use Xlib because the development tools are
free and non-proprietary. I feel your frustration at not having "simple"
tools to read a window. But this is not a Commodore-64
world anymore! (Am I dating myself? I'm 62!)

Part of the complexity of XWindow is its power: the server for your XWindow
protocol requests *can* be running on a machine half way around the world
with display characteristics quite foreign to the
machine your client application is running on. The pattern of bits you pack
into a 'long' variable to define the 3 RGB colors
of a line, text, or pixel going into a drawable residing in the server's
memory can be rearranged by the server to best fit the
physical screen memory. But you'd like to think there was at least one
function to pull a pixel from the server's drawable and
then rearrange it to fit the usual pattern you originally packed into your
'long' variable. But I don't see any such function in my
O'Reilly & Associates "Xlib Programming Manual" Copyright 1992 book that I
have.

However, it does seem to have a
slightly simpler function that would eliminate your idea of having to create
a 1x1 pixel drawable and XCopy() your source
window into. Look at XGetSubImage(). This will permit you to pull a 1x1 (or
any size you want) sub-portion of your
game window into your pre-created XImage structure. Then you use XGetPixel()
to pull the pixel's color in client-side
'long' variable format. So, instead of XCopy(), XGetImage(), XGetPoint() you
would use XGetSubImage(), XGetPoint().

HTH

Tim


bolta...@yahoo.co.uk

unread,
Oct 16, 2009, 4:28:21 AM10/16/09
to
On Thu, 15 Oct 2009 15:58:01 -0400
"TR Oltrogge" <trolt...@verizon.net> wrote:
>window into. Look at XGetSubImage(). This will permit you to pull a 1x1 (or
>any size you want) sub-portion of your
>game window into your pre-created XImage structure. Then you use XGetPixel()
>to pull the pixel's color in client-side
>'long' variable format. So, instead of XCopy(), XGetImage(), XGetPoint() you
>would use XGetSubImage(), XGetPoint().

Even better, thanks for that.

B2003

0 new messages