Archived

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

bitmaskin 16bit color value

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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.

Share this post


Link to post
Share on other sites