#### Archived

This topic is now archived and is closed to further replies.

# Alpha function

This topic is 5745 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi there, Ive been trying to convert an ARGB 32 bit image file to a XRGB 32bit bmp but with no success. the thing that Im having a hard time with is the alpha blending, when using a background as i''ve defined myself. Im trying to run my decalph function on the different colors like this. Do you guys know what''s wrong with my decalph function
char source[4]; //ARGB
char dest[4]; //XRGB
char background[4]; //XRGB
for(int i=1;i!=3;i++) // Copy Just RGB
{
dest=decalph(source[0],source[i],background[i]);
}
double decalph(double a,double fg,double bg)
{

if(a>255||a<0)
printf("error");
if(fg>255||fg<0)
printf("error");
if(bg>255||bg<0)
printf("error");

a=a/255.0f;
fg=fg/255.0f;
bg=bg/255.0f;

double ret=(a * fg + (1-a) * bg) * 255.0f;
if(ret>255||ret<0)
printf("error");
return ret;

}

##### Share on other sites
Try this:

for(int i = 1; i < 4;i++) // Copy Just RGB

{
dest[i] = decalph(source[0], source[i], background[i]);
}
dest[0] = source[0]; //you can spare that if you don't care

//what's in the alpha part of the final

//color

Also, this will only work if you aren't using pre-multiplied alpha.

Ciao, ¡muh!

[edited by - thebigMuh on October 23, 2002 8:58:11 AM]

##### Share on other sites
what Im not sure of is the decalph function, if it works properly.
the code I pasted above was just demostration code to show the usage of the decalph function.
-Keen

##### Share on other sites
It seems ok, however I would do it more like this:

static inline void decAlpha(float *dest, const float *source, const float *back)
{
dest[0] = 1.0f - source[0];

dest[1] = source[0] * source[1] + dest[0] * back[1];
dest[2] = source[0] * source[2] + dest[0] * back[2];
dest[3] = source[0] * source[3] + dest[0] * back[3];
}

int main()
{
float dest[4], source[4], back[4];

//...

decAlpha(dest, source, back);

//...

}

This should be fast & working.

I would generally advise you to keep your colors around as floats as long as possible, and only convert them to the 0-255 range when needed.

Don''t output errors when you encounter out of range values, instead simply clamp them.

Ciao, ¡muh!

1. 1
2. 2
Rutin
19
3. 3
JoeJ
16
4. 4
5. 5

• 30
• 22
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001800
×