# 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.

## 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 on other sites
Should all of those "255"'s actually be "255.0f"?

##### 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

into

srcPixel.A * srcPixel / 255

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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 11
• 9
• 9
• 42
• ### Forum Statistics

• Total Topics
634131
• Total Posts
3015715
×