I have code below to blend alpha color in ARGB format:
a, b, o are integers, but should always fall in [0x00ffffff,0xffffffff]
o = ( ( ( a >> 24 ) * ( b >> 24 ) ) >> 8 ) << 24;
Is there anyway to optimize it?
Thanks!
I have code below to blend alpha color in ARGB format:
a, b, o are integers, but should always fall in [0x00ffffff,0xffffffff]
o = ( ( ( a >> 24 ) * ( b >> 24 ) ) >> 8 ) << 24;
Is there anyway to optimize it?
Thanks!
That doesn't perform the alpha blending. That just combines the alpha component of two pixels. It's also probably about as efficient as you're going to get without going directly to assembly. If you can use pointers, you might get a little faster, though:
byte* pa = (byte*)&a;
byte* pb = (byte*)&b;
byte* po = (byte*)&o;
po[3] = pa[3] * pb[3] >> 8;
This saves most of the shifting. Note that this assumes a little-endian machine. If you're running on a big-endian processor, it becomes even better with:
*po = *pa * *pb >> 8;
In either case, you can turn this into a one-liner by doing all the pointer casting inline, but it gets a bit hard to read.
((byte*)&o)[3] = ((byte*)&a)[3] * ((byte*)&b)[3] >> 8;
or
*((byte*)&o) = *((byte*)&a) * *((byte*)&b) >> 8;
unsigned int blend(unsigned int a, unsigned int b)
{
return (((a >> 24) * (b >> 24)) << 16) & 0xFF000000;
}
Is this what you are trying to do? I don't think this is any faster though, but more accuracte according to what you stated.
If you don't need to use bitwise operators you can use pointers too, as shown by other solutions.