PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

My own Alpha blending function, is it correct?

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?
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 here.

It uses a single multiplication and only integer/bitwise operators.
Thanks for the fast reply.

good to know that I'm on the right track.

I looked at the algorithm you provided but there are things I don't understand. Maybe you have the time to explain it to me.

First what's the value of GREEN_MASK?

What are the function parameters here?

Seems like some parts of the code are missing.

Maybe this is simple but looking at the code I can't seem to make it right. I want to understand it and not just use it.

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

Why does he do this?

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

This part seems so complicated. ?

If you have the time please explain.
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...
Hi Marcus!

Reviewing the code again raized some more questions.
If I got it right, the GREEN_MASK is my 0x7E0.
What about the GREEN_SHIF? What's the value of that?
And why is Alpha calculated this way?

int m_alpha = ((*AlphaPixel & GREEN_MASK)>>(GREEN_SHIFT-2+3 )) ;


Is this done to be able to calculate the blend for the three colors at once. Instead of doing it one for every color like I did in my code?

const unsigned long mask = RED_MASK | BLUE_MASK | (GREEN_MASK << 16);


Thanks again Marcus!
Sorry I don't have as much time I would like to answer quickly.

Hachaso wrote:
If I got it right, the GREEN_MASK is my 0x7E0.

Yes

Hachaso wrote:
What about the GREEN_SHIF? What's the value of that?

The GREEN_SHIFT is the position of the fist bit of the green component (in the A form shown above). Here is the 3 different components “shift”:
RED_SHIFT = 11
GREEN_SHIFT = 5
BLUE_SHIFT = 0

Hachaso wrote:
And why is Alpha calculated this way?

int m_alpha = ((*AlphaPixel & GREEN_MASK)>>(GREEN_SHIFT-2+3 )) ;

This question would be better answered by fast_rx. I assume he stores the alpha value in the green component of a separate “picture” (surface). The green component being 6 bits, he right-shifts the green component by 1 to have it on 5 bits (and then being able to use the blending formula).

In you case, you seem to have already your alpha as a 5 bits value (0->31) so you don’t need this line.

Hachaso wrote:
Is this done to be able to calculate the blend for the three colors at once. Instead of doing it one for every color like I did in my code?

const unsigned long mask = RED_MASK | BLUE_MASK | (GREEN_MASK << 16);

It’s mainly the B form (shown above) that allow to calculate the blend for the three colors at once. The “mask” value is only a “helper mask” that allows cleaning the color in it’s B form.

The best advice I can give you is to take a color and a blending value and then replay (on a paper!) each step done in this algorithm (expect the m_alpha processing that you don’t need).
Write all in binary form and things will, I hope, become clear :)

Alpha blending is really costly, so it worth spending some time on this algorithm (which, I bet, is at least 4 times faster that yours).