• Advertisement
Sign in to follow this  

Blending Function

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, earlier, i had some problems with my sprite rendering function but when i made it work, this new problem popped up. There is something wrong with the colors. The image i load is an TIFF image and i use the SDL_Image library to load it. Uint32 fh_renderer::a_blend( unsigned int bg, unsigned int fg) { int bg_r = (bg >> 0) & 0xff; int bg_g = (bg >> 8) & 0xff; int bg_b = (bg >> 16) & 0xff; int fg_r = (fg >> 0) & 0xff; int fg_g = (fg >> 8) & 0xff; int fg_b = (fg >> 16) & 0xff; float bg_a = ( ( bg >> 24 ) & 0xff ) / 255.0; float fg_a = ( ( fg >> 24 ) & 0xff ) / 255.0; bg_a = 1 - ( 1 - fg_a ) * ( 1 - bg_a ); bg_r = bg_r + ( fg_a * ( fg_r - bg_r ) ); bg_g = bg_g + ( fg_a * ( fg_g - bg_g ) ); bg_b = bg_b + ( fg_a * ( fg_b - bg_b ) ); int int_bg_a = bg_a * 255; return (bg_r << 0) | (bg_g << 8) | (bg_b << 16) | (int_bg_a << 24); } http://xs64.xs.to/pics/06033/Corrupt_colors.png.xs.jpg If i switched bg_r with bg_g it rendered in the right way. int bg_b = (bg >> 0) & 0xff; int bg_g = (bg >> 8) & 0xff; int bg_r = (bg >> 16) & 0xff; But that doesnt feel logical, does it? At least not to me... SDL_BlitSurface rendered it in the right way so it cant be that it loaded the image in a wrong way...

Share this post


Link to post
Share on other sites
Advertisement
Hi!

I am not familiar with the TIFF-Format, but I assume that the order of the
color components is "RGBA" - if this is correct, then your shifts seem to have
an error.

int bg_r = (bg >> 0) & 0xff;
int bg_g = (bg >> 8) & 0xff;
int bg_b = (bg >> 16) & 0xff;

You get the Alpha component in bg_r and the Blue in bg_b and the Red in bg_b...

Try changing (all of the occurencies in your code) to :

alpha = (color >> 0) & 0xff;
blue = (color >> 8) & 0xff;
green = (color >> 16) & 0xff;
red = (color >> 24) & 0xff;

Does it work now?

Greetings
Stefan

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement