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

My own Alpha blending function, is it correct?


My own Alpha blending function, is it correct?

Postby Hachaso » Sep 4, 2006 @ 1:35pm

Hi!

I'm quite new at this so I was wondering if someone could please explain to me how alpha blending is done, from a programmers point of view.

Here's is what I have done. Don't know if I'm on the right track to begin with. Could you please explain to me what the errors are. I wrote this code without any optimization so that I could better understand how it works.

// Alpha is mapped to an unsigned char value of 0 - 32.

unsigned short alpha_blend(unsigned short ColorA, unsigned short ColorB, unsigned char Alpha)
{
unsigned short res = 0; // destination color
unsigned short colorAred = 0;
unsigned short colorAgreen = 0;
unsigned short colorAblue = 0;
unsigned short colorBred = 0;
unsigned short colorBgreen = 0;
unsigned short colorBblue = 0;

double step;
step = 0,03125; // is the result of 1 divided by 32. Result from the mapping

ucAlpha *= step;

// Select color bits to work with.
colorAred = uiA & 0xF800;
colorBred = uiB & 0xF800;
colorAgreen = uiA & 0x7E0;
colorBgreen = uiB & 0x7E0;
colorAblue = uiA & 0x1F;
colorBblue = uiB & 0x1F;

colorAred = colorAred * ucAlpha + colorBred * (1.0 - ucAlpha); // formula for calculating alpha blending
colorAgreen = colorAgreen * ucAlpha + colorBgreen * (1.0 - ucAlpha);
colorAblue = colorAblue * ucAlpha + colorBblue * (1.0 - ucAlpha);

res = (colorAred | colorAgreen) | colorAblue;

return res;
}


If I have it right one shoule use the alpha blending formula on each RGB color, one for Red, Green and Blue. Is this correct?
Hachaso
pm Member
 
Posts: 5
Joined: Sep 3, 2006 @ 11:38am


Postby Marcus » Sep 4, 2006 @ 2:18pm

Hello,

Given that ucAlpha is the Alpha parameter of your function, you're algorithm is correct.

However and as you probably guess, you're using floating point operations and too much multiplications.

As you are using 16bit colors (R5G6B5). You may want to have a look at the excellent algorithm provided by fast_rx .

It uses a single multiplication and only integer/bitwise operators.
- Marcus -
User avatar
Marcus
pm Member
 
Posts: 12
Joined: Aug 31, 2006 @ 7:29pm
Location: France


Postby Hachaso » Sep 4, 2006 @ 2:46pm

Hachaso
pm Member
 
Posts: 5
Joined: Sep 3, 2006 @ 11:38am


Postby Marcus » Sep 4, 2006 @ 4:49pm

The code is partial because it was posted in the PocketFrog/PocketHal forum. Readers usually have PocketFrog source code as a reference.

RED_MASK, BLUE_MASK, GREEN_MASK are the values you used in your code to separate the color components (0xF800, 0x7E0, 0x1F). So you know how to calculate "mask" now.

d = (d | (d<<16)) & mask;

This line allows changing the color representation. Why?
This new color representation and the “mask” allow you to use the alpha blending formula without splitting the color components.
Well, in your algorithm you separated the three color components (RGB) because if you apply your alpha blending formula on the color value itself, blue bits may overflow on the green ones, green ones on the red ones...

The given line allows switching the color representation from
form A: rrrr rggg gggb bbbb (16bits)
To
form B: 0000 0ggg ggg0 0000 rrrr r000 000b bbbb (32bits)
Notice that there is 5 unused bits between each component.

If you calculated "mask" you should have
0000 0111 1110 0000 1111 1000 0001 1111

So what’s the point?
If 0 <= alpha < 31, you know that multiplying alpha by any number will not “shift” its bits by more than 5 positions. This means that multiplying alpha by a color value in the B form will not allow any color component to overflow on the next one. So with the B form you can safely use your blending formula (provided that you use the mask to filter overflowing bits).

d = ((((d-s)*(m_alpha))>>5) + s) & mask;

What about this ?
The part "((((d-s)*(m_alpha))>>5) + s)" is just an algebraic simplification of your formula. I am sure you would have found it by yourself :) (if you know that >>5 means "divide by 32" :wink:)

I hope this helps...
- Marcus -
User avatar
Marcus
pm Member
 
Posts: 12
Joined: Aug 31, 2006 @ 7:29pm
Location: France


Postby Hachaso » Sep 5, 2006 @ 9:29am

Hachaso
pm Member
 
Posts: 5
Joined: Sep 3, 2006 @ 11:38am


Postby Marcus » Sep 7, 2006 @ 1:58pm

- Marcus -
User avatar
Marcus
pm Member
 
Posts: 12
Joined: Aug 31, 2006 @ 7:29pm
Location: France


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