Advertisement Jump to content
Sign in to follow this  
future sandwich

Help with new blit method

This topic is 3863 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

This should be a fairly simple task but I'm Failing; SDL's software blitting methods have this annoying feature where pixels in per-pixel alpha blending assume the alpha of the destination pixel. This is obviously not so good for blitting to transparent and semi transparent surfaces. I'm a total graphics noob so I made a blitting method in C# which was easier to read for me so i could mess around with the algorithm. Figured out something that works well (just makes the colours a little muddy but I can live with that. also, requires at least one more multiplication I think than the standard SDL algo but I don't really care about that at this point either). Now I want to rebuild my SDL project with it done fast style, but it turns out I suck at working with MMX instructions and the like.
//channels of final pixel
int R, G, B, A;
//get a combination of the two alphas as the final alpha
A = (srcPixel.A + destPixel.A - (srcPixel.A * destPixel.A / 255)); 
//for each channel, FinalPixel = SrcAlpha * Src + (1-SrcAlpha)(DstAlpha * Dst)
R = (int)((int)(srcPixel.A * (srcPixel.R) / 255) + (int)((255 - srcPixel.A) * (destPixel.A * destPixel.R / 255) / 255)); 
G = (int)((int)(srcPixel.A * (srcPixel.G) / 255) + (int)((255 - srcPixel.A) * (destPixel.A * destPixel.G / 255) / 255));
B = (int)((int)(srcPixel.A * (srcPixel.B) / 255) + (int)((255 - srcPixel.A) * (destPixel.A * destPixel.B / 255) / 255));
finalPixel = Color.FromArgb(A, R, G, B);
This would seem trivial to turn into fast c++, asm or mmx instructions but my efforts to do so keep producing weird colours. I know this works, I just can't translate or optimize it without breaking it :(. Can anyone give me a hand? (I can post my attempt with MMX instructions but it's longish and I'm not sure anyone would care..) [Edited by - future sandwich on June 26, 2008 1:30:19 AM]

Share this post

Link to post
Share on other sites
no not really - the idea is to use the alpha as a rational coefficient so

(srcPixel.A * srcPixel.R / 255) + (255 - srcPixel.A) * ((destPixel.A * destPixel.R / 255) / 255);

could just as well be written

salpha * srcPixel.R + (1.0-dalpha) * dalpha * destPixel.R

assuming you turned everything into floats appropriately. However I wanted to get away from the floating points as early as possible since they have no place in an optimized version and the way to do that with ints is turn

salpha * srcPixel


srcPixel.A * srcPixel / 255

There will be rounding but that's no big deal.

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!