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?
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?
Don't quote me on that... ...seriously
alpha = (pixel >> 24) & 0xff;
red = (pixel >> 16) & 0xff;
green = (pixel >> 8) & 0xff;
blue = pixel & 0xff;
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
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
Don't quote me on that... ...seriously
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.Originally Posted by Brad0407
Asked and answered in your previous post (which you claim you answered)
http://cboard.cprogramming.com/showthread.php?t=65502
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
You're a genious..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); }
If you understand what you're doing, you're not learning anything.
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.
Hope is the first step on the road to disappointment.
That depends on the order of the variables in the definition, and correct usage of #pragma pack(...)Originally Posted by itsme86