Tip: An Optimized Formula for Alpha Blending Pixels

When you have a formula, you could see if some of the operations could be optimized and you could see if the integer to float conversions could be simplifying for the formula. I will demonstrate this by using the alphablend formula in MSDN. This may be useful if you are writing your own custom-optimized alphablend function.

Note in the formula that S is one of the source primary colors(RGB), D is one of the destination primary colors(RGB) and A is the alpha channel. S, D and A are byte integers (that is, value is from 0 to 255).

First, you should attempt to remove the expensive integer to float conversion through rearrangement. Below is the original BLENDFUNCTION formula in MSDN.

As shown below, the subtraction is executed to invert the alpha first, before the division by 255.0.

Next, the multiplications are done before the divisions. If the multiplications are done first, the results of the divisions will not be 0.0 to 1.0 but 0 to 255, thus you can eliminate the float values and the integer to float conversions. You may ask, since S, D and A are byte integers, will the byte integer overflow if the multiplications execute first? The answer is no. Because in C/C++, byte and short integers are promoted to full integers before any computation begins.

Since both divisions are the same (both are divided by 255), they can be grouped together as shown below. In that case, one division has been eliminated.

As a further optimization, since 255 is close to 256, the division could be replaced by 255 with shift to the right by 8 as shown below. Of course, this optimization would sacrifice some accuracy, for alphablend operations, this is okay.

I have made a benchmark application to benchmark the alphablending of each formula for 1000 times. As shown in the screenshot below, the "Unoptimized" option is the original MSDN formula, the "Optimized" option is the new improved formula and the "Very Optimized" option is the new improved formula with right shifting to replace the division.

The results for a release build on my machine, are as follows

Unoptimized: 10030 milliseconds
Optimized: 3790 milliseconds
Very Optimized: 2821 milliseconds



About the Author

Wong Shao Voon

I guess I'll write here what I does in my free time, than to write an accolade of skills which I currently possess. I believe the things I does in my free time, say more about me.

When I am not working, I like to watch Japanese anime. I am also writing some movie script, hoping to see my own movie on the big screen one day.

I like to jog because it makes me feel good, having done something meaningful in the morning before the day starts.

I also writes articles for CodeGuru; I have a few ideas to write about but never get around writing because of hectic schedule.

Downloads

Comments

  • wronge?

    Posted by kiba on 06/06/2012 07:19am

    i dont know but its that wronge x 8 = x / 256 but thats must be x / 255 e.g. srcColor (0,0,0,0), dstColor (128,128,128,128) ((0 * 0) + (128 * (255-0))) / 256 = 127, wronge ((0 * 0) + (128 * (255-0))) / 255 = 128, right

    Reply
  • Can do faster I think...

    Posted by legendh6 on 07/06/2009 07:06am

    You can directly rewrite the equation as D+A*(S-D)/255 You save a multiplication if I am correct... You can even use your /255 tip on A*(S-D) and loose maybe even less by approximating only one term of the addition.

    • Alphablend is not correct

      Posted by CBasicNet on 07/12/2009 10:25pm

      Hi legendh6,
      
      I have tried your formula but the alphablend is not correct when I checked "show graphics" option. If you want my edited source (for trying your formula) to tweak your formula, you can email me cbasicnet[at]yahoo[.]com[.]sg
      
      Thanks!!

      Reply
    Reply
  • a bit faster please?

    Posted by vlad_tepesch on 04/03/2009 08:24am

    it would be possibly slightly faster if replacing (255-A) with (~A) (bitwise not) So no 'mov' of a constant is required. so: (S*A + D* ~A)>>8

    Reply
  • Am happy!

    Posted by Alok Govil on 04/02/2009 03:39pm

    2.5X improvement just by rearranging the formula! The inaccurate one would result in issues if the blending is done on a part of the image since boundary may appear.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • A majority of organizations are operating under the assumption that their network has already been compromised, or will be, according to a survey conducted by the SANS Institute. With many high profile breaches in 2013 occurring on endpoints, interest in improving endpoint security is top-of-mind for many information security professionals. The full results of the inaugural SANS Endpoint Security Survey are summarized in this white paper to help information security professionals track trends in endpoint …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds