This site is no longer active and is available for archival purposes only. Registration and login is disabled.

Are my Alpha Blending functions correct?


Are my Alpha Blending functions correct?

Postby Hachaso » Sep 12, 2006 @ 11:07am

Hi!

For the last couple of weeks I have tried to figure out how this works. Going from a simple calculation to a optimized one.
The problem I always encounter is that the result is not the same.

Here's the non optimized function.
The function takes to Colors A and B and an Alpha value the goes from 0.0 - 1.0.
Is this correctly written?

#define RGB(r,g,b) ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8 ))|(((DWORD)(BYTE)(b))<<16 )))

#define GetR(pixel) (((pixel) & 0x00ff0000) >> 16)
#define GetG(pixel) (((pixel) & 0x0000ff00) >> 8 )
#define GetB(pixel) ((pixel) & 0x000000ff)

unsigned double blend_rgb2( unsigned int ColorA, unsigned int ColorB, double Alpha)
{
unsigned int res = 0;

int abR = (int) (GetR(ColorA) * Alpha + (1-Alpha) * GetR(ColorB));
int abG = (int) (GetG(ColorA) * Alpha + (1-Alpha) * GetG(ColorB));
int abB = (int) (GetB(ColorA) * Alpha + (1-Alpha) * GetB(ColorB));

res = RGB(abB, abG, abR);

return res;
}


Here's the optimized function that results in another value.
Alpha is now mapped to an unsigned char of value 0 - 32

unsigned int blend_rgb3( unsigned int ColorA, unsigned int ColorB, unsigned char ALPHA)
{
unsigned long res = 0;
DWORD sr,sg,sb,dr,dg,db;
DWORD sTemp,dTemp;

sTemp = ColorA;

dTemp = ColorB;
sb = sTemp & 0xff;
db = dTemp & 0xff;
sg = (sTemp >> 8 ) & 0xff;
dg = (dTemp >> 8 ) & 0xff;
sr = (sTemp >> 16) & 0xff;
dr = (dTemp >> 16) & 0xff;

res = (WORD)((ALPHA * (sb - db) >> 5 ) + db | ((ALPHA * (sg - dg) >> 5) + dg) << 8 | ((ALPHA * (sr - dr) >> 8 ) + dr) << 16);

return res;
}


What am I doing wrong?


The part that I don't understand is the bit shift.

For instance if I in the first function send in Alpha = 0.75 and do the calculation I get one result

If I then use the same values for ColorA and ColorB but now I have Alpha = 24 (Meaning that I multiply by 32)
And in turn I divide by 32 in the equation.
Here's the part I get lost in the equation.

Alpha / 32 is the same as Alpha >> 5, since 2^5 is 32.
Doesn't this mean that the result of the bit shift of 5, should be 0.75 ?


Thanks for you help!
Hachaso
pm Member
 
Posts: 5
Joined: Sep 3, 2006 @ 11:38am


Postby drgoldie » Sep 12, 2006 @ 2:15pm

Hi Hachaso,

this is probably not what you want to hear but:

- your code is far from being optimized (just look into the forum. you can do the complete blend with a single multiplication).

- since you have a working reference function: where is the problem to step through your code and check where the numbers go wrong?

bye,
Daniel
drgoldie
pm Member
 
Posts: 330
Joined: Jan 10, 2003 @ 10:46am
Location: Vienna


Re: Are my Alpha Blending functions correct?

Postby pappaxray » May 3, 2007 @ 8:55pm

pappaxray
pm Member
 
Posts: 23
Joined: Jul 28, 2004 @ 4:57pm
Location: UK


Postby j.edwards » May 5, 2007 @ 2:10am

Have a look at Tala's post on this thread:
User avatar
j.edwards
pm Member
 
Posts: 240
Joined: Oct 29, 2003 @ 11:09am
Location: Australia


Postby pappaxray » May 6, 2007 @ 2:34pm

pappaxray
pm Member
 
Posts: 23
Joined: Jul 28, 2004 @ 4:57pm
Location: UK


Return to Windows Mobile


Sort


Forum Description

A discussion forum for mobile device developers on the Windows Mobile platform. Any platform specific topics are welcome.

Moderators:

Dan East, sponge, Digby, David Horn, Kevin Gelso, RICoder

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

cron