Thursday, 22 March 2007

How to do a Divide operation in After Effects

From time to time people find that some essential stuff is missing in AE (ok to be honest something is always missing in any program).
With that i don't mean plug in stuff you can build yourself with some effects etc.
i mean basic operations that a compositor needs ( i'm not starting to rant here about the lack of 16bit and 32bit effects) .

Everyone building their own colour difference keys or despill functions are irritated by the lack of an Subtract transfer mode for example.
I know there are other ways to do this but you still lose time and make your comps more complex.

A more rare one is a Divide operation. Actually it's an essential operation for working with images with premultiplied alpha channels, where one very often needs to divide the image by it's alpha channel to revert the premultiplication.
But as AE handles that in most cases in the Interpret Footage dialog and hides most of the whole concept from the user, not everybody knows of it's importance.
Ok, besides that, there are probably not that many situations where you might need it, but when you do, you're in trouble.
An example would be an Ultimatte style screen correction.

While fiddling with AE and "The Partially Transparent Alpha Pixel Problem", as described in Steve Wright's book "Digital Compositing for Film and Video", i found a workaround for Divide in AE.
AE has an un- premultiply operation outside of the the Interpret Footage dialog, one can use that for for ones Divide needs.
It's a bit far out, but it works.
Ok, here we go....

You have two grayscale layers A and B which you want to divide: A/B

Put both in a comp.
Add a "Channel-> SetMatte" Effect to A.
For "Take Matte From Layer" choose - B.
For "Use For Matte" choose - Red Channel,Green Channel or Blue Channel.
Uncheck all the other items

Add a "Channel-> Channel Combiner" to A.
Choose "From" - Straight to Premultiplied.
check "Invert"

Add another "Channel-> SetMatte" to A.
For "Use For Matte" choose - Full
Uncheck everything.

You just divided image A by the red, blue or green channel of image B.

Ok, why does this work?
Well first you set one of the color channels of image B as the alpha of
image A.
Then you added an effect where you can multiply a image with it's alpha channel (an alpha premultiply).
By checking "Invert" we use an alpha unpremultiply instead.
That means we divide the image (layer A) by its alpha channel (a channel of layer B).
In the last step we clear layer A of its temporary alpha channel by setting alpha to full (white, 1).

I'm sure you know how to make this work with non grayscale images if needed.

As a side note: "Channel Combiner" is only an 8bit effect.