how to extract r,g,b components out of DWORD
Bit mask then bit shifts.
Say color == 0xFFFFFFFF. To extract red:
Do something like that:
int red = (color & 0xFF000000) << 96; // 96 == 6*16
int blue = (color & 0x00FF0000) << 64; // 64 == 4*16
etc..
Just experiment with the bit shift operator I''m not sure about it.
ph0ng^_^wh0ng
Say color == 0xFFFFFFFF. To extract red:
Do something like that:
int red = (color & 0xFF000000) << 96; // 96 == 6*16
int blue = (color & 0x00FF0000) << 64; // 64 == 4*16
etc..
Just experiment with the bit shift operator I''m not sure about it.
ph0ng^_^wh0ng
If, for example, your components are stored this way:
0x00RRGGBB
You would have to, in C, do this:
Color = 0x00F080F0;
Red = (Color >> 16);
Green = (Color >> 8) & 0xFF;
Blue = Color & 0xFF;
In fact, what I just did was shift the wanted bits and mask out the unwanted ones.
0x00RRGGBB
You would have to, in C, do this:
Color = 0x00F080F0;
Red = (Color >> 16);
Green = (Color >> 8) & 0xFF;
Blue = Color & 0xFF;
In fact, what I just did was shift the wanted bits and mask out the unwanted ones.
If you can, maybe represent your RGBA data like this in the first place:
Then it''s simple enough. Otherwise I guess you could use bitshifts and the life to get the data:
This assumes format is RGBA, you should be able to modify it to your needs.
struct RGBA{ union { struct { BYTE r, g, b, a; }; DWORD c; };}
Then it''s simple enough. Otherwise I guess you could use bitshifts and the life to get the data:
void DWORD_To_RGBA(DWORD color, BYTE& r, BYTE& g, BYTE& b, BYTE& a){ r = (color & 0xFF000000) >> 24; g = (color & 0x00FF0000) >> 16; b = (color & 0x0000FF00) >> 8; a = (color & 0x000000FF);}void RGBA_To_DWORD(DWORD& color, BYTE r, BYTE g, BYTE b, BYTE a){ color = (r << 24) | (g << 16) | (b << 8) | a;}
This assumes format is RGBA, you should be able to modify it to your needs.
quote:Original post by porthios
struct RGBA
{
union
{
struct { BYTE r, g, b, a; };
DWORD c;
};
}
K so if I understand you right, c and r,g,b,a all pint to the same memory???
[edited by - deinesh on March 6, 2004 7:16:02 PM]
No, the union is between the dword and the struct, heres what it looks like, a D is a bye of the dword A R G B all are parts of the struct, the top and bottom are the same...
DDDD
RGBA so the first D is the same as the R, the G the same as the second D and so on
hope that helps
-Dan
DDDD
RGBA so the first D is the same as the R, the G the same as the second D and so on
hope that helps
-Dan
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement