If I use the code:
#define RGB32BIT(a, r, g, b) ((b) + (g << 8) + (r << 16) + (a << 24)
UINT pixel = RGB32BIT(0, 0, 255, 0);
How do I get back the alpha, red, green, and blue values if I just have the pixel?
Printable View
If I use the code:
#define RGB32BIT(a, r, g, b) ((b) + (g << 8) + (r << 16) + (a << 24)
UINT pixel = RGB32BIT(0, 0, 255, 0);
How do I get back the alpha, red, green, and blue values if I just have the pixel?
alpha = (pixel >> 24) & 0xff;
red = (pixel >> 16) & 0xff;
green = (pixel >> 8) & 0xff;
blue = pixel & 0xff;
Congratulations! You have proven yourself to be among the ranks of the programmers that know too much. I knew the answer. I just wanted to see if anyone had a clue about it. I guess you do. I prefer:
TheCode:UCHAR alpha = pixel >> 24;
UCHAR red = pixel >> 16;
UCHAR green = pixel >> 8;
UCHAR blue = pixel;
automaticaly drops off the extra so you don't need theCode:UCHAR
. I put that up at the game programmers message board and I got all kinds of junk.Code:& 0xFF
How about:
Code:b = pixel & 0x000000ff;
g = (pixel >> 8) & 0x000000ff;
r = (pixel >> 16) & 0x000000ff;
a = pixel >> 24;
But in your original code a, g, b, and r aren't UCHAR's. So now you're changing their original types.Quote:
Originally Posted by Brad0407
Asked and answered in your previous post (which you claim you answered)
http://cboard.cprogramming.com/showthread.php?t=65502
You're a genious.. :oQuote:
Originally Posted by Brad0407
How about something like:
Code:union pixel_data
{
uint32_t pixel;
struct
{
uint8_t alpha;
uint8_t red;
uint8_t green;
uint8_t blue;
} part;
};
Maybe? Not sure if word-alignment or padding would screw it up.Code:{
union pixel_data pix1;
pix1.pixel = 0x12345678;
printf("red: %d\n", pix1.part.red);
printf("green: %d\n", pix1.part.green);
printf("blue: %d\n", pix1.part.blue);
}
Or an array. Unions are definately fun.Code:enum { cAlpha, cRed, cGreen, cBlue };
union foo
{
uint32_t pixel;
uint8_t part[ sizeof( uint32_t ) ];
};
union foo bar;
printf("Alpha: %d\n", bar.part[ cAlpha ] );
Quzah.
That depends on the order of the variables in the definition, and correct usage of #pragma pack(...)Quote:
Originally Posted by itsme86