0

Okay bit shifting is still a bit weird to me.

I've got a 16bit value. The first 15 bits are colors and the last bit is alpha.

I have done this with 24 and 32 bit colors no problem as they are nice byte size's to deal with, but I cant seem to get this to work with 15/16 bits.

This is what I've done in the past, with 24/32 bit colors

(m_colorValue >> RED_CHANNEL) & 0xFF;

I'm trying to split the value into 4 values. 3 5 bit color values and 1 alpha value. I don't know what mask I should be using.

Thanks.

  • 4
    What exactly isn't working? All I can tell from your question is that you have a 16 bit number and "something" isn't working. Code would be nice. – Chris Eberle Jun 01 '11 at 15:46
  • 2
    You forgot the question... what do you want to accomplish? What have you tried? – Kleist Jun 01 '11 at 15:46
  • Duplicate of http://stackoverflow.com/questions/6194449/converting-bitmap-from-argb1555-to-rgb8888 ? – Mark Ransom Jun 01 '11 at 16:01
  • 1
    @Mark I think extracting the channels and converting to 24 bit is similar, but not the same problem. So I wouldn't call it a duplicate. – CodesInChaos Jun 01 '11 at 16:05
  • @CodeInChaos, good point. The code in the answer would be a good starting point though. – Mark Ransom Jun 01 '11 at 16:15

3 Answers3

3

If I understand correctly:

red = (packed >> 0) & 0x1F;
green = (packed >> 5) & 0x1F;
blue = (packed >> 10) & 0x1F;
alpha = (packed >> 15) & 0x01;

packed should better be an unsigned and I'm probably off for the order.

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
1

use bit shifting.

ushort s;
ushort b= s&0x1F // first five bits
ushort g= (s>>5)&0x1F // second five bits

etc...

Raiv
  • 5,731
  • 1
  • 33
  • 51
1

Your question is pretty vague, but if you're trying to extract the individual color components from an RGB1555 this should do the trick:

unsigned short color;

const unsigned int 
    a = color & 0x8000, 
    r = color & 0x7C00, 
    g = color & 0x03E0, 
    b = color & 0x1F; 
Brandon Moretz
  • 7,512
  • 3
  • 33
  • 43
  • Anding isn't enough, you have to shift too. – Mark Ransom Jun 01 '11 at 16:15
  • @Mark Um, yeah. I would think that OP would know the basics given that he's done with 24bpp/32bpp. I just provided the masks to get the components as his question doesn't even explicitly state what he's trying to achieve. If he states the full problem I suppose I could write a full solution for him... – Brandon Moretz Jun 01 '11 at 16:19