Merge Tool Maths

This is a brief breakdown of the maths behind Fusion’s Merge Tool.

The Operator modes are well documented. But, I’ve done some digging around to uncover the maths behind the Apply mode, Alpha Gain and Burn In.

Eyeon Fusion’s Merge tool allows for additive and subractive compositing.

Additive assumes that images are pre-multiplied. This is the default for most compositing in Fusion.

Subtractive assumes that  foreground image is NOT pre-multiplied. The Merge Tool will pre-multiply the foreground image by it’s alpha then do an additive merge.

Generally images should be pre-mutliplied with black. If I’m working with 3d rendered images this is normally the default. If I’m working with green screen or rotoscoped footage I may need to do some work to remove colour from the edges.

The Subtractive Additive slider is a quick and easy way to darken edges of the foreground image.

The missing additive blend mode?

The merge tool DOES do a straight additive blend but ‘Add’ is not listed as an apply mode in the drop down list. You need to know your composite maths to solve the puzzle…

Set ‘Apply Mode’ to ‘Normal’ and ‘Alpha Gain’ to zero for the ADDATIVE BLEND.

This mathematically the same as an additive blend.

The general maths for merging pixel colours

With an Additive merge the formula used to merge a pixel is

( A * FA ) + ( B * FB )

A – RGBA Foreground
B – RGBA Background

If a Subtractive merge is used the foreground ( A ) is pre-multiplied in the merge operation.

( A * αAFA ) + ( B * FB )

αA – Alpha Foreground
αB – Alpha Background

The Merge Operator determines  FA and FB

The various operator options control where the foreground is merged with the background.

The table below shows how the different merge operations determine  FA and FB .

of 1 pixel
Clear 0 0
A 1 0 A = RGBA Foreground
B 0 1 B = RGBA Background
A Over B 1 1-αA
A in B B 0
A Held Out B 1-αB 0
A ATop B B 1-αA
A XOr B 1-αB 1-αA

This table is based on the 1984 SIGGRAPH paper, Compositing Digital Images, Porter and Duff.

Normal mode is A over B.

The basic maths.

Substituting the formula from the table above we get these simple functions for merging one image over another.

Normal Additive ( A over B )

A  +  B * ( 1 –  αA )

Normal Subtractive ( A over B )

 A *  α+  B * ( 1 – αA )

B – background color
A – foreground color
αA – foreground alpha

Alpha Gain and Burn In sliders.

Alpha gain and Burn In sliders scale the foreground alpha to create screen like effects.
When G = 1 and U = 0 these equations can be simplified to the maths above.

Normal Additive ( A over B )

A  +  B * ( 1 – ( αA * G ) * ( 1 – U ) )

Normal Subtractive ( A over B )

 A * ( α* G ) +  B * ( 1 – ( αA * G ) * ( 1 – U ) )

G – Alpha Gain
U – Burn In

It’s worth noting that using Burn In with floating point maths can give alpha values above 1.0. The foreground alpha is essentially doubled where it crosses the background.

Other Apply Modes.

The Merge tool offers a host of Apply modes many of which emulate photoshop layers. The Subtractive Additive slider is disabled in these modes which makes the maths a bit simpler.

Screen Mode is not effected by alpha at all. Otherwise images should be pre-multiplied before using these blend modes.

In all these apply modes image alphas are combined using this equation.

αA  +  αB * ( 1 –  αA )

Gringo’s apply modes comp demonstrates the maths in Fusion using custom tools. I’ve summarised most of the maths in the table below.

Uses alpha
Screen 1 – (1-A) (1-B) no
Multiply AB yes
Overlay if B < 0.5 then 2AB else 1 – 2(1-A) (1-B) yes
Hard light if A < 0.5 then 2AB else 1 – 2(1-A) (1-B) yes
Soft light if A < 0.5 then 2AB + B2( 1 – 2A ) else 2B(1 – A) + (2A – 1)√B yes
Lighten if A > B then B else A yes
Darken if A < B then B else A yes
Difference abs(A-B) yes
Exclusion A + B – 2AB yes
Colour Dodge B / (1 – A) yes
Colour Burn 1 – ( (1 – B) / A ) yes

This is based on Delphi’s photoshop maths. Note: I’ve flipped the notation in my article… A = b, B = a.

I’ve also used some maths shorthand and removed the * sign. So; 2AB = 2*A*B, 2(1-A) = 2*(1-A), etc.