Saturday, June 6, 2009

Photoshop-like Alpha Blending

I have written about simple blending equation in previous blog. Now I want to describe more complex blending equation. That equation is used by graphics applications like Adobe Photoshop or Corel Paint Shop. Adobe “basic” blending equation in its simplified form is presented below: Let’s adapt to HLSL. We will use premultiplied alpha to simplify and optimize equation (see my previous post):

// Performs advanced "over"-type blending
// (blended is the result of advanced blending 
// (for ex, overlying.rgb * underlying.rgb))
float4 blend(float3 blended, 
             float4 overlying, 
             float4 underlying)
      // Convert to premultiplied format 
 overlying.rgb *= overlying.a;
 underlying.rgb *= underlying.a;

 float4 result = 
       ((1-underlying.a)*overlying) + 
       ((1-overlying.a)*underlying) + 
       (overlying.a * underlying.a) 
               * float4(blended, 1);
      return float4(result.rgb / result.a, result.a);
* I used the same equation for rgb-values and alpha

No comments: