PocketMatrix forums

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

Alpha Blending help.

Hi!

I need some help with alpha blending.
What I have done, is to write 2 different functions that are supposed to return the same value.

The problem is that something is different and I can't seem to figure out what.
I have done it by hand to see that the calculation is correct and still nothing.

Please tell me if I'm on the right track.
Is that the way to do alpha blending?
And also what is the difference between the two functions?

Here's the code:

Alpha is mapped to a value between 0 - 32.
If I understand correct, alpha originally is a float containing a value between 0.0 - 1.0. So now I have mapped alpha to an unsigned char value between 0 - 32.

unsigned short rgb565Blender1(unsigned short ColorA, unsigned short ColorB, unsigned char Alpha)
{
unsigned short res = 0;

unsigned short ColorAred = 0;
unsigned short ColorAgreen = 0;
unsigned short ColorAblue = 0;
unsigned short ColorBred = 0;
unsigned short ColorBgreen = 0;
unsigned short ColorBblue = 0;

unsigned short resR = 0;
unsigned short resG = 0;
unsigned short resB = 0;

ColorAred = ColorA & (0x1f << 11);
ColorBred = ColorB & (0x1f << 11);
ColorAgreen = ColorA & (0x3f << 5);
ColorBgreen = ColorB & (0x3f << 5);
ColorAblue = ColorA & 0x1f;
ColorBblue = ColorB & 0x1f;


resR = ((Alpha*(ColorAred - ColorBred)) >> 5) + ColorBred;
resG = ((Alpha*(ColorAgreen - ColorBgreen)) >> 5) + ColorBgreen;
resB = ((Alpha*(ColorAblue - ColorBblue)) >> 5) + ColorBblue;


res = resR | resG | resB;

return res;
}


Next function here with some minor differences.


unsigned short rgb565Blender2(unsigned short ColorA , unsigned short ColorB , unsigned char Alpha)
{
unsigned short res = 0;

unsigned short ColorAred = 0;
unsigned short ColorAgreen = 0;
unsigned short ColorAblue = 0;
unsigned short ColorBred = 0;
unsigned short ColorBgreen = 0;
unsigned short ColorBblue = 0;

unsigned short resR = 0;
unsigned short resG = 0;
unsigned short resB = 0;

ColorAred = (ColorA >> 11) & 0x1f;
ColorBred = (ColorB >> 11) & 0x1f;
ColorAgreen = (ColorA >> 5) & 0x3f;
ColorBgreen = (ColorB >> 5) & 0x3f;
ColorAblue = ColorA & 0x1f;
ColorBblue = ColorB & 0x1f;

resR = (Alpha*(ColorAred - ColorBred) >> 5) + ColorBred;
resG = (Alpha*(ColorAgreen - ColorBgreen) >> 5) + ColorBgreen;
resB = (Alpha*(ColorAblue - ColorBblue) >> 5) + ColorBblue;

resG = resG << 5;
resR = resR << 11;

res = resB | resG | resR;

return res;
}



Thanks for the help!
close, but both versions have some major problems...

In rgb565Blender1...
ColorAred = ColorA & (0x1f << 11);
ColorBred = ColorB & (0x1f << 11);
ColorAgreen = ColorA & (0x3f << 5);
ColorBgreen = ColorB & (0x3f << 5);
ColorAblue = ColorA & 0x1f;
ColorBblue = ColorB & 0x1f;

You are separating out the components but leaving them 'in-place'. So, for example, the red could be 11111 000000 00000 in binary. What do you expect to happen when you add or multiply this number? Your max multiplier is 32 (the same as 5 bits, right) so you need at least 5 bit above your number to hold the value before the rest of the calculation. IF you use a 32 bit int to hold it, it will work - otherwise you may be getting overflow (maybe not since you're using the differece method? Maybe you can check the extremes to see if 16 bits is enough?)

The other problem is that you want to get rid of any other bits except for the 5 or 6 for each component. You aren't doing this and so the residual bits from the red get into the green and blue. For instance, after the calc resR might be something like 10101 001100 00110. The last bits are garbage and you need to mask them off.

That will fix the first function.


The second one has a major problem in the line
res = resB | resG | resR;

Think about it... you have components shifted down to the 0-31 or 0-63 range...

00000000000rrrrr
0000000000gggggg
00000000000bbbbb
===OR'd=========
0000000000g????? will be a bunch of blue garbage.

you want
rrrrr00000000000
00000gggggg00000
00000000000bbbbb
===OR'd=========
rrrrrggggggbbbbb right!