#### Archived

This topic is now archived and is closed to further replies.

This topic is 5747 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I though i knew how to use bitmask until i tryed this code:

#define RGB16(red, green, blue) ( ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3))

USHORT col = RGB16(255,255,255);

USHORT r,g,b;

r = ((col & 0x1F)<<3);
g = ((col>>5 & 0x3F)<<2);
b = (((col>>11) & 0x1F)<<3);

The results of r g and be are bad they arent 255 like they should, what do i do wrong? [edited by - Craazer on October 22, 2002 6:09:19 PM]

##### Share on other sites
Shouldn''t the second line be
USHORT col = RGB16(255,255,255); ?

That will use the macro you defined.

##### Share on other sites
quote:
Original post by Prototype
Shouldn''t the second line be
USHORT col = RGB16(255,255,255); ?

That will use the macro you defined.

Oh that was just typo, and it wasnt the real problem.

##### Share on other sites
I can see a couple of possible problems there. First, you seem to have red and blue the wrong way round in the last three lines. The macro does ((red >> 3) << 11), so the corresponding "unpack" expression would be (((col>>11) & 0x1F)<<3) rather than ((col & 0x1F)<<3).

The second problem is that you are packing three bytes into a word - 24 bits into 16. The macro drops the 3 least significant bits of red and blue and the two least significant bits of green. That being the case, I''d expect you got:

r = 0b11111000 = 0xF8 = 248
g = 0b11111100 = 0xFC = 252
b = 0b11111000 = 0xF8 = 248

In other words, the missing bits are just set to zero. I''m not sure if this is the correct way to fix this, but I seem to remember seeing the upper bits of a component used to fill the missing ones, something like this:

r = ((col & 0xF800) >> 8) | ((col & 0xF800) >> 13);
g = ((col & 0x07E0) >> 5) | ((col & 0x07E0) >> 8);
b = ((col & 0x001F) << 3) | ((col & 0x001F) >> 2);

It doesn''t get back the lost bits, but it means that RGB16(255, 255, 255) gives pure white if you unpack the components.

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 22
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631733
• Total Posts
3001927
×