PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Determing a pixel's RGB components with GAPI

Frank W. Zammetti · 23 May 2001, 15:08 UTC
Quick question that I suspect is simple, it's just elluding me...

If I want to determine the Red, Green and Blue components of a pixel on the screen with GAPI, how do I do it?

I have the following so far:

unsigned short* pScrBuf = NULL;
pScrBuf = (unsigned short*)GXBeginDraw();
unsigned short pixelColor = 0;
unsigned short* pPointAddress = NULL;
pPointAddress = pScrBuf + (x * (g_gxdp.cbxPitch >> 1)) + (y * (g_gxdp.cbyPitch >> 1));
pixelColor = *pPointAddress;
GXEndDraw();

(assuming a 16-bit display obvously).  That all seems obvious and easy enough.  But, how can I get the R, G and B components out of pixelColor (into separate unsigned char's).  Thanks all, I appreciate any help you can offer!
That depends on the type of RGB encoding (555, 565, 888 ). Check the ffFormat member of the the display properties to determine the actual encoding. AFAIK, all devices use 565 at this time. This is off the top of my head, so it may need tweaking:
[pre]
//RGB 565 -> 24 bit RGB
int red=(pixelColor&0xf800)>>8;
int green=(pixelColor&0x7e0)>>3;
int blue=pixelColor&0x001f<<3;
[/pre]
Note that with 565 encoding green can have twice as many shades as red or blue.

Dan East

Last modification: Dan East - 05/23/01 at 13:30:59
Back in the early DDraw days, drivers would be notorious for reporting the wrong pixel format for surfaces.  What we'd do is draw a red, green, and blue pixel on the screen using GDI's SetPixel(), then go read what was actually written to video memory using DDraw.  That way you absolutely knew which bits were being set for each color component.  

You shouldn't have to do that with the PocketPC though, since the display modes don't change and you can't swap out the video display hardware.
That depends on the type of RGB encoding (555, 565, 888 ). Check the ffFormat member of the the display properties to determine the actual encoding. AFAIK, all devices use 565 at this time. This is off the top of my head, so it may need tweaking:

//RGB 565 -> 24 bit RGB
int red=(pixelColor&0xf800)>>8;
int green=(pixelColor&0x7e0)>>3;
int blue=pixelColor&0x001f<<3;

Note that with 565 encoding green can have twice as many shades as red or blue.

Dan East

Last modification: Dan East - 05/23/01 at 13:30:59

Oh my god! That makes anybody look like a newbie - or is that just me?
:-(
Frank W. Zammetti · 23 May 2001, 22:37 UTC
Dan, I appreciate your help greatly!  Unfortunately, I'm still not having any luck, hoping you can get me just a little bit further...

What I'm getting is weird and I've been banging my head for about 4 hours now...

I am simply filling the screen with a sold color and checking a pixel as a test...

Using color R=100, G=150, B=200, I get back R=96, G=148 and B=200.

Using color R=12, G=80, B=230, I get back R=8, G=80, B=224.

Using color R=213, G=42, B=74, I get back R=208, G=40, B=72.

As you can see, sometimes one or two of them come back right, most times not, and they aren't off by much.  I tried reasoning it out, I'm guessing it's an incorrect bit shift, but I truthfully haven't been able to figure it out.  I tried some random changes hoping my intuition would serve me well, but not this time.

Any further idea?  I thank you again!

Frank
Actually it is more easy than that. You will always get those numbers. Because you tell the system to save your colors with 256 levels per channel. That is 24-bit. The display uses 16-bit, which doesn't have as many colors (32 levels per channel). That's why the values you get back aren't "perfect". There is no workaround. It is a hardware issue.

// Fredrik
To expand on what Fredrik said, you are starting with colors that are 8 bits per color component. Those are converted to 5 bits for red and blue, and 6 bits for green. So you lose 3 bits of information from red and blue and 2 bits from green.  3 bits is equal to 8 decimal, while 2 bits is 4 decimal. So red and green may be equal to, or up to 8 less than, your original value, while green may be up to 4 less than the original color value.

Original 8 bit Red in binary:
RRRRRRRR
After converted to / from 5 bits:
RRRRR000
The lowest three bits will always be zero, hence your lost precision. If it so happened that in the original value the lowest 3 bits were already zero, then there was no information to lose, so the result will match. In the values you gave, 200 matched. 200 in binary is 11001000. Clear as mud? :)

Dan East

Last modification: Dan East - 05/24/01 at 05:58:29
Frank W. Zammetti · 24 May 2001, 08:12 UTC
Grr... It IS clear as mud, it's just that mud sucks!

I guess the work-around is to pre-decrease the values when I plot them.  In other words, zero out the lowest three bits of R and B and two bits of G and use those as my color components.  Then when I read a pixel from the VFP, I would expect to get the same values I put there then.  

Fortunately, in my application I think it's not a big deal because I was actually writing a function to dim a portion of the screen, so basically this means that my colors will start out dimmed slightly already.  I was expecting this to lead to a sprite collision detection algorithm, so I would have had to have dealt with this anyway.

Does the pre-decreasing idea sound right though?  I'll test it later, but it seems to make sense at the moment (it IS 9 in the morning though, way too early for MY brain!)  Thanks again guys, you've all been a great help!
That sounds good. If you pre-translate all your bitmaps to the RGB encoding used by the device then it will save a lot of processor time when you blit the screen out. The disadvantage is that some processing, depending exactly on what your software does, may take longer because of the extra work to extract individual color component values.

Dan East