hi all
i am trying to convert rgba pixel value to argb..
i have started like this..
int RGBAtoARGB(int rgba) {
int argb;
return argb;
}
can anyone help to proceed with that code..
RGBA to ARGB
You might want to choose unsigned integers for pixel values as they won't go below zero
Something like this I'd imagine, don't know if it works wrote this from my mobile phone.
If you want I can explain how this works but it isn't that difficult. Work it out yourself and if you really don't understand it just ask me.
Something like this I'd imagine, don't know if it works wrote this from my mobile phone.
unsigned int RGBAtoARGB( unsigned int rgba )
{
unsigned int argb = rgba &0xFFFFFF00;
argb += (rgba &0x000000FF) << 24;
return argb;
}
If you want I can explain how this works but it isn't that difficult. Work it out yourself and if you really don't understand it just ask me.
Here's a hint:
int integer =...;
int numberOfBytesInAnInteger = sizeof(int);
assert( numberOfBytesInAnInteger == 4 )
char* bytes = reinterpret_cast<char*>( &integer );
can explain how bitwise operators are working here..
You might want to choose unsigned integers for pixel values as they won't go below zero
Something like this I'd imagine, don't know if it works wrote this from my mobile phone.
unsigned int RGBAtoARGB( unsigned int rgba )
{
unsigned int argb = rgba &0xFFFFFF00;
argb += (rgba &0x000000FF) << 24;
return argb;
}
If you want I can explain how this works but it isn't that difficult. Work it out yourself and if you really don't understand it just ask me.
You might want to choose unsigned integers for pixel values as they won't go below zero
Something like this I'd imagine, don't know if it works wrote this from my mobile phone.
unsigned int RGBAtoARGB( unsigned int rgba )
{
unsigned int argb = rgba &0xFFFFFF00;
argb += (rgba &0x000000FF) << 24;
return argb;
}
If you want I can explain how this works but it isn't that difficult. Work it out yourself and if you really don't understand it just ask me.
You sure thats right. Its different to how ive always done it, and I got wrong results running through your way
rgba = 0x11223344
argb = rgba & 0xFFFFFF00;// = 0x11223300
//argb += (rgba &0x000000FF) << 24
t = rgba & 0x000000FF;// = 0x00000044
t2 = t << 24;// = 0x44000000
argb += t2;// = 0x55223300
//expected = 0x44112233
Anyway my way:
unsigned rgbaToArgb(unsigned rgba)
{
//take just the alpha component in the most significant 8 bits
//this works since by shifting it from the least to most significant 8 bits,
//all the other components get pushed out
//ie RRGGBBAA << 24 = AA000000
unsigned alpha = rgba << 24;
//move the rgb coponents to the least signicant 24 bits
unsigned argb = rgba >> 8;
//add alpha back in
argb |= alpha;
//done
return argb;
}
rgba = 0x11223344;
unsigned alpha = rgba << 24; // = 0x44000000
unsigned argb = rgba >> 8; // = 0x00112233
argb |= alpha; // = 0x44112233
[quote name='0x3a' timestamp='1303284675' post='4800679']
You might want to choose unsigned integers for pixel values as they won't go below zero
Something like this I'd imagine, don't know if it works wrote this from my mobile phone.
unsigned int RGBAtoARGB( unsigned int rgba )
{
unsigned int argb = rgba &0xFFFFFF00;
argb += (rgba &0x000000FF) << 24;
return argb;
}
If you want I can explain how this works but it isn't that difficult. Work it out yourself and if you really don't understand it just ask me.
You sure thats right. Its different to how ive always done it, and I got wrong results running through your way
rgba = 0x11223344
argb = rgba & 0xFFFFFF00;// = 0x11223300
//argb += (rgba &0x000000FF) << 24
t = rgba & 0x000000FF;// = 0x00000044
t2 = t << 24;// = 0x44000000
argb += t2;// = 0x55223300
//expected = 0x44112233
Anyway my way:
unsigned rgbaToArgb(unsigned rgba)
{
//take just the alpha component in the most significant 8 bits
//this works since by shifting it from the least to most significant 8 bits,
//all the other components get pushed out
//ie RRGGBBAA << 24 = AA000000
unsigned alpha = rgba << 24;
//move the rgb coponents to the least signicant 24 bits
unsigned argb = rgba >> 8;
//add alpha back in
argb |= alpha;
//done
return argb;
}
rgba = 0x11223344;
unsigned alpha = rgba << 24; // = 0x44000000
unsigned argb = rgba >> 8; // = 0x00112233
argb |= alpha; // = 0x44112233
[/quote]
will you explain "argb |= alpha;" i am noob at bitwise operators
Well, 0x3a's solution is wrong, since the rgb part needs to be shifted too.
OP: if you are noob at bitwise operations, look into them.
The | operator is an "or" operator. If one OR the other element (or both) is 1, the result is 1. Otherwise it's zero (otherwise means NEITHER of them is 1). It's basic logic, I don't think it's possible to explain further. Bitwise means you have to do it for every corresponding bits of a data (unsigned int in this case).
In this special case the | and the + operators give the same result.
OP: if you are noob at bitwise operations, look into them.
The | operator is an "or" operator. If one OR the other element (or both) is 1, the result is 1. Otherwise it's zero (otherwise means NEITHER of them is 1). It's basic logic, I don't think it's possible to explain further. Bitwise means you have to do it for every corresponding bits of a data (unsigned int in this case).
In this special case the | and the + operators give the same result.
will you explain "argb |= alpha;" i am noob at bitwise operators
(That explanation by SyncViews was great btw imo. +1 rep).
To explain that particular operator:
It's like the += operator but with | instead of +. | is called the bitwise OR operator.
EDIT: What | does is operate on two numbers to get another one, for example 0x0011 | 0x1122 = 0x1133.
EDIT: So we end up with an equivalency between a |= b; and a = a | b; where | has been explained.
EDIT: oops, missed that szecs had already explained the operator. Oh well, can't hurt.
[quote name='rays' timestamp='1303302188' post='4800732']
will you explain "argb |= alpha;" i am noob at bitwise operators
(That explanation by SyncViews was great btw imo. +1 rep).
To explain that particular operator:
It's like the += operator but with | instead of +.
What | does is bitwise add two numbers. Bitwise addition is for example adding 0x0011 and 0x1122 to get 0x1133.
So we end up with an equivalency between a |= b; and a = a | b; where | is bitwise addition.
EDIT: oops, missed that szecs had already explained the operator. Oh well, can't hurt.
[/quote]
Well, | (OR) operator is NOT bitwise addition. ^ (XOR) is bitwise addition, because by definition (and reason) bitwise addition is this:
0+0 = 0
0+1 = 1
1+0 = 1
1+1 = 0 (and 1 "overflows" because 1+1 = 10)
And that is exactly what XOR does by definition.
OR does the following:
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1
Which is NOT addition.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement