• Advertisement
Sign in to follow this  

Breaking down a 32bit pixel into colors[SOLVED]

This topic is 3704 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 know using the macro
_RGB32BIT(a,r,g,b) ((b) + ((g) << 8) + ((r) << 16) + ((a) << 24))// this builds a 32 bit color value in A.8.8.8 format (8-bit alpha mode)
Builds up a 32 bit pixel, I have used this macro to do this and it works great, what I can't seem to figure out now is how to break a 32 pixel down into the color values again... I can get it stored I guess I just don't understand bit shifting :/ I have no idea how to break this down anyone have a link with information on this? here is what i'm trying to do
UINT OriginalPixel; //Our Original pixel
OriginalPixel = video_buffer[x + y*lpitch32]; //Copy the pixel
//add something here to separate the pixel into alpha, blue, green, red
//add something here to lower those values
//update pixel with new colors
[Edited by - yewbie on January 4, 2008 11:38:43 AM]

Share this post

Link to post
Share on other sites
I would do it like this:

#define ALPHA_MASK 0xff000000
#define RED_MASK 0x00ff0000
#define GREEN_MASK 0x0000ff00
#define BLUE_MASK 0x000000ff

a = colour & ALPHA_MASK;
r = colour & RED_MASK;
g = colour & GREEN_MASK;
b = colour & BLUE_MASK;

using the masks you can extract the colours whenever you need them.

Share this post

Link to post
Share on other sites
You could use something like this:
void ARGB_to_element(DWORD argb,unsigned int& a,unsigned int& r,unsigned int& g,unsigned int& b)
a = argb & (0xff << 24);
r = argb & (0xff << 16);
g = argb & (0xff << 8);
b = argb & 0xff;

See this article for a detailed explanation.

Share this post

Link to post
Share on other sites
Your 32-bit pixel has 4 channels. These 4 channels each have 8 bits each, for a total of 32-bits.

The top 8 bits are alpha, the next 8 are red, the next 8 are green, and the final 8 are blue.

This is the layout of your pixel:

|8 bits||8 bits||8 bits||8 bits|

You can use simple bitshifts to extract certain channels. Let's say, you wanted the red channel. First, get rid of everything below the red channel:

OriginalPixel >> 16 // Shift everything across to the right by 16 bits:

Now you want to get rid of the Alpha channel. Use a bitwise AND:

OriginalPixel & 0xFF // 0xFF in binary is: 00000000000000000000000011111111

The bitwise AND operation works as follows:
1 & 1 = 1,
1 & 0 = 0,
0 & 1 = 0,
0 & 0 = 0.

So we have:
& 00000000000000000000000011111111

Which is the red channel, on its own. You can apply the same thing, (but with different bitshifts) to extract the other 3 channels.

Share this post

Link to post
Share on other sites
Awesome you guys all rock, I was able to get it working like so, I also learned alot about bit shifting and bitwise operations, thanks for all the help, you have no idea how long I have been trying to get this to work =p

DWORD dwBBitMask= OriginalPixel & 0xff; //this works!!
DWORD dwGBitMask = OriginalPixel >> 8 & 0xff;
DWORD dwRBitMask = OriginalPixel >> 16 & 0xff;
DWORD dwAlphaBitMask = OriginalPixel >> 24 & 0xff;//this works

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement