PocketMatrix forums

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

Transparency

I'm looking for a quick method of doing variable levels of transparency.

My current scheme is to do shifts and adds. I can get a few levels this way but I'm wondering if there is an easier way to get finer control and still be fast.

i.e.
if (blend = 75) {
a = (*source >> 2) & 2MASK;
*dest = a + a + a + ((*dest >> 2) & 2MASK);
}

In this scheme I have to specifically test for each ratio I want and run a little bit different alg. I'm looking for an easy way to just give it a 0 - 255 number and do it in a loop while maintaining fixed point math.

Any help?
Have you considered using palettized images? It's not an option for all apps of course, but when it is, you can get very high 'alpha' speed. Suppose you want 16 alpha levels (effectively, a 4-bit alpha channel) then you simply calculate 16 intensities of the palette. When you want to mix e.g. 25%/75%, you use palette 4 for the first layer, and (15-4) for the second layer. The colors in these arrays can never exceed pure white when added (since they are pre-scaled) and so the 'alpha' calculations are merely a matter of two lookups in the palette arrays and an addition. Won't get much faster than that. More than two layers are also possible of course.
I'll give that a look. I'm not very familiar with your code base yet. I was just thinking that when handling 8bit images the first task would be to dither them to 16 bit and keep them there for faster blitting to the framebuffer. Thanks for the prompt reply.
Did you test this method ?

*dest = (*src * blend + *dest * (0xFF - blend)) >> 8;

With only 16 alpha levels, maybe a lookups table can be used to avoid multiplication ?
Try this:
Code:

static __inline PIXEL blend(PIXEL c1, PIXEL c2, int a)
{
PIXEL rb1,g1,rb2,g2;
PIXEL rb,g;
rb1 = (c1&0xf81f);
g1 = (c1&0x07e0);
rb2 = (c2&0xf81f);
g2 = (c2&0x07e0);
rb = rb1+(((rb2-rb1)*(a>>3))>>5);
rb &= 0xf81f;
g = g1+(((g2-g1)*(a>>2))>>6);
g &= 0x07e0;
return rb|g;
}


"a" is in the range [0...255]. It doesn't check for overflow, but I haven't found any occurrences of that anyway

By changing "b*alpha + a*(1-alpha)" to "a+((b-a)*alpha)" (which would use fixed point of course) we can get rid of one of the multiplies. By operating on red and blue at the same time we can reduce the work even more (since red and blue are seperated by 6-bits, as long as the alpha is shifted to be less than 64, the color values won't be mixed when we shift it back. The masking operation is to get rid of the overlapping bits where green is.

update:
Fixed the explanation to account for Digby's correction
billcow,

If you start with this:
a * alpha + b * (1 - alpha)

You'll end up with this:
b + (a - b) * alpha

Not this:
a + (b - a) * alpha
Digby:
You wrote "a" as the foreground and "b" as the background. My code uses the arguments in the reverse order. ("c1" would be the background)

As for my explanation, you're right, it was wrong. I've just corrected the post.
Thanks guys, this is working nice.
laurnet · 05 Jun 2002, 22:29 UTC — trouble using blend
hello
How to use this blend code
I tried with these declarations


unsigned short* buff1 = (unsigned short*)getbuffer();
PIXEL* flare = new PIXEL[128 * 128];


blend(flare,buff1,5);


i get this :
cannot convert parameter 1 from 'unsigned short *' to 'unsigned short'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast[/code]
This function takes two single pixels, blends them, and returns the result. You are trying to apply the function to an entire array.

You would have to create a function that copies the first buffer to the second, blend along the way.

This function would have to know stuff like the sizes of the surfaces.
laurnet · 06 Jun 2002, 19:39 UTC — blend
ôops !!
I was tired after a long day work..

So I do something like this :


Code:

int i,j;

for (j=0; j<480; j++)
{
for (i=0; i<640; i++)
{

buff1[ i ]=blend(buff1[ i ],buff2[ i ],150);

}
}
Exactly (Provided your surfaces are both 640x480 :)).
laurnet · 09 Jun 2002, 14:09 UTC — blend
thanks billcowwwww!