PocketMatrix forums

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

Getting pixel color

Why is it that this doesn't work in GameLoop:

Code:

DisplayDevice* display = GetDisplay();
display->Clear(Color(23, 245, 167));
Pixel myPixel = display->GetPixel(100, 100);
register int R = myPixel & RED_MASK;
register int G = myPixel & GREEN_MASK;
register int B = myPixel & BLUE_MASK;
R = R >> RED_SHIFT;
G = G >> (GREEN_SHIFT + 1);
B = B >> BLUE_SHIFT;
display->Update();

Shouldn't the values of R, G and B be 23, 245 and 167 at the end of this code? It's not, but I don't get why. Help?!?
Both Pixel (myPixel) and the screen are 16-bit, so you're chopping your 24bpp colour down to a 16bpp 565 representation when you clear the screen with it/pick it up again.

I.e. Your R G B should come out as:

R=23>>3 = 2
G=245>>3= 30
B=167>>3= 20

G is "special" because you shift it down to 5 bits in your code.
A bit unrelated, but who actually invented 565 color? Is the extra green bit actually ever useful? Or is it just a way to round each pixel to a nice 16-bit integer? If so, how come there's no 655 or 556 color?
It's there to maximise the usage of 16-bits, really. The human eye is more sensitive to green than blue and red, so it makes sense to use the extra bit for more green hues.

However, a lot of display hardware will use an equal number of bits per colour anyway so it doesn't actually gain much (or anything) on devices like Pocket PCs (especially when the screens on devices like the earlier iPaqs are 12-bit in a 4bpp format).
So it's not a bad programming practice to lose that green bit in calculations?
It's not a bad practice IMHO. Just be sure and make it always 0 or always 1. I've seen a lot of code that doesn't mask it properly and that can leave the bit "unstable" (again, not that it will actually matter or even be noticable on a lot of devices, but it's careless).
Thanks for the explanation ref, I now understand perfectly.

However, it leaves an important question in my mind: how can I gain access to the real RGB values of a surface? It seems obvious now that you can't get that once things are blitted to the screen, or the back buffer, but I assume there must be a way if you just have a surface with an image on it, right?

For things like fire effects and lens effects and the like, that is obviously required to properly do the interpolation required for those effects. Is there a way to get access to a pixel that returns a 24-bit value on a surface? Then I should just have to mask out each byte to get my RGB values, right?

Or am I missing the obvious again somewhere and screwing this all up?
I'm guessing here, again (I've not played with pocketfrog), but if the surfaces are 16bpp then your 24-bit data is lost (as you said). The only way you're going to get it back is to not lose it in the first place (i.e. work in 24-bit in the code and down-convert onto the screen buffer)... but...

Why do you particularly want to use 24-bit? Why not just work in 16bpp to begin with (using 5 bits per colour rather than 8 )?
Well, I guess that my understanding of some basic concepts here is lacking...

When I have my images in Paint Shop Pro or Photoshop or whatever, my colors are specified as RGB values where R, G and B can each have a value of 0-255. Ok, this is all obvious. That of course is 8 bits per color channel, yielding 24-bit color. Ok, now stop saying "duh, what a moron!" and let me continue... :)

If I use 16-bit color as you say, does this mean that, assuming 565 format where red gets 5 bits, green 7 abd blue 5, that my red channel cannot have a value greater than 31. green no greater than 63 and blue no greater than 31? This is the part I obviously don't get because if that was the case, nothing would work right!

The ironic thing is that all my images are paletized to 256 colors, but the colors themselves are still specified as 24-bit colors I think.

So obviously my brain ain't working quite right here...
Sorry if this is patronising; I'm not sure which bit you don't understand.

If I use 16-bit color as you say, does this mean that, assuming 565 format where red gets 5 bits, green 7 abd blue 5, that my red channel cannot have a value greater than 31. green no greater than 63 and blue no greater than 31?


Yes. Spot on (except the 7 typo).

So, white would be (in binary):
<pre>11111 111111 11111
(red)(green) (blue)</pre>

This is the part I obviously don't get because if that was the case, nothing would work right!


Why not? You need to think of it as a fraction, maybe?

In 8bits/colour, the maximum value is 255. So, your ratio is (colour intensity)/255.

For 5bits/colour the max. value is 31, so your value is (colour intensity)/31.

With 8bits/colour 255 in the red channel is full red.
With 5bits/colour 31 in the red channel is also full red.
I guess what I'm still having trouble with is RGB values above 31. What I mean is, the bit pattern of 31 is of course 00011111. However, if I set a pixel to an RGB value of (32,0,0), the bit pattern of 32 is 00100000, so why doesn't it show up as black, since the least-significant bits are all 0's?

This would of course mean that any RGB value above 31 (or 63 for green) would not display as I expect, they would in essence "mirror" the first 31 colors, but that's not the case.

For instance, setting a pixel to RGB(255,0,0) I would expect, given what I've read here, to give me the same result on the screen as doing RGB(31,0,0) because the red bits in both cases are all 1's... but that's not the case, they show up as different colors on the screen (well, ok, same COLOR technically, just a different intensity).

Don't worry about patronizing me by the way... I feel like any moment I'm going to have a moment of clarity and this will all make sense, at which case I'll feel like an idiot for not seeing it sooner, but until then I appreciate your efforts to explain it!
fzammetti wrote:
For instance, setting a pixel to RGB(255,0,0) I would expect, given what I've read here, to give me the same result on the screen as doing RGB(31,0,0) because the red bits in both cases are all 1's... but that's not the case, they show up as different colors on the screen (well, ok, same COLOR technically, just a different intensity).


Here's the code for the Color() function from PocketFrog:

Code:

//////////////////////////////////////////////////////////////////////////////
//
// Color() - Helpers to create Pixels
//
//////////////////////////////////////////////////////////////////////////////

// Return a "Pixel" from the specified color components.
// Each component value must be between 0 and 255
inline Pixel Color( int red, int green, int blue )
{
return ((red << 8) & RED_MASK) | ((green << 3) & GREEN_MASK) | (blue >> 3);
}


The assumption is that you are always going to specifiy the color values as 0-255. Look at what happens to the blue value for example. If you pass in a value of 255, it is shifted down to 31. If you pass in a value of 31, it is shifted down to 3. Obviously, you are losing some color information since colors 248-255 will all shift to 31.

You could easily modify this function to assume you passed in values of 0-31 for red and blue and 0-63 for green.

Pam
Ok, that certainly helps. Let me ask this question then: if I load an image in PF, I of course have a surface. Is that surface 24-bit internally? If so, how can I get at the pixels in non-565 format?

For instance, if I have a surface with an image loaded in it, I know that my pixels are specified in RGB values with each component ranging from 0-255. As is now clear, GetPixel will return the pixel in 565 format, which is not the same as the values of the pixels in my image. If I wanted to do a lens effect for instance, the code I've previously written back in the DOS days works against full 8-bit per channel colors, so I think I'd need the same here?

Is that possible, or are surfaces not internally 24-bit to begin with?
PocketFrog surfaces are always 16-bit. When you load the image it is converted from 24-bit to 16-bit behind the scenes.

Pam
so why doesn't it show up as black, since the least-significant bits are all 0's?


Because the values you're giving it are right-shifted by 2 or 3 bits before they're plotted. The RGB function you use takes the most significant bits, not the least.

For instance, setting a pixel to RGB(255,0,0) I would expect, given what I've read here, to give me the same result on the screen as doing RGB(31,0,0) because the red bits in both cases are all 1's... but that's not the case, they show up as different colors on the screen (well, ok, same COLOR technically, just a different intensity).


Your RGB function is expecting 8-bit inputs. To build the colour with the 0-31 and 0-63 inputs you'd want something like (the function Pam mentioned):

Code:
inline Pixel Color565( int red, int green, int blue )
{
return ((red << 11) & RED_MASK) | ((green << 5) & GREEN_MASK) | blue;
}


Or you could be a bit clumsier and call the RGB function like this:

Code:
RGB(R<<3,G<<2,B<<3);


(and have the code simply undo the shifts you did on the parameters).

Essentially, you're just shifting the 5 or 6 bit values into the correct place.

If I wanted to do a lens effect for instance, the code I've previously written back in the DOS days works against full 8-bit per channel colors, so I think I'd need the same here?


It shouldn't be all that hard to modify it to run on a 16-bit 565 system. You're only losing 2 or 3 bits per channel - it's much easier than trying to look up colours in a palettised system anyway :).
Code:
register int G = myPixel & GREEN_MASK;


Err... I'm bored, so I'll just ask a stupid semi-related question: how many registers does the ARM have?
Depends whether you want the detailed, "real" answer or not. The undetailed answer is:

Unless you play dirty tricks with mode switching (which may gain you something but probably costs too much to gain a lot), within your programs you have access to:

R0-R12 as general purpose registers
R13 as the stack pointer
R14 as the link register
R15 (AKA PC) as the program counter

R13 and R14 can be used and abused if you're ("very" and "a bit", respectively) careful about what you do.

The more detailed answer would explain that the processor has different modes of operation and has a banked register set (there are several R13s, for example) that gets used depending on the mode... but if you're really interested in that, go read the ARM ARM (ARM Architecture Reference Manual). I can dig out a link if you want me to (it's a 12MB PDF).