Monday, September 24, 2007

Blending Mode Math - Part 2

Screen Blending Mode (Alt+Shft+S)
Screen, part of the Lighten group, can be used to quickly lighten a dark image. Often times, there is a tremendous amount of detail that is obscured by shadow. A Screen self blend can be used to brighten the image or a selected area of the image.

Screen: X = 1- ((255-U)*(255-L))/255

Screen mode inverts the values of each of the visible pixels in the two layers of the image. (That is, it subtracts each of them from 255.) Then it multiplies them together, inverts this value again and divides by 255. The resulting image is usually brighter and sometimes “washed out” in appearance. The exceptions to this are a black layer, which does not change the other layer, and a white layer, which results in a white image. The mode is commutative; the order of the two layers doesn't matter.

Soft Light Blending Mode (Alt+Shft+F)
A Soft Light self blend dodges the light areas and burns the dark areas of an image. This can be helpful when used on an image with a “back lit” suspect. It has been one of my personal favorites. When trying to accomplish non-destructively what Shadows and Highlights can do, Soft Light is my tool of choice. Shadow detail can be found by using an inverted, desaturated, and blurred Soft Light self-blend.

Soft Light: X = (((255-L)*U*L)+ (L*R_s))/255

The Soft Light equation is complicated. It needs R_s, the result of the Screen mode equation (shown above). This one treats the Upper image as if it were the light source (a Soft Light) for the Lower image. So if the Upper Image, or blend colour, is brighter than 50% (has a numerical value greater than 127.5, which is half of 255), then the Lower image, or base colour, gets lightened subtly. If the Upper image's colour is darker than 50% (has a numerical value less than 127.5) then the Lower image's colour gets darkened a little. As stated above, it dodges the light areas and burns the dark areas.

Difference Blending Mode (Alt+Shft+E)

Difference is great for aligning images or stitching images together. It is also helpful in quickly comparing images of a scene to see what is missing or has been added. If there is no difference between the layers, the result is black.

Difference: X =|L-M|

Difference mode subtracts the pixel value of the upper layer from that of the lower layer and then takes the absolute value of the result. The mode is commutative; the order of the two layers doesn't matter. Because the order of the layers doesn’t matter, Difference examines the numerical values for colour in both the base and blend images, and subtracts the lower number (darker colour) from the higher number (brighter colour). The result colour has a numerical value which is the difference between the two values.

Well, there you have it. A quick but detailed look at the most common blending modes used in forensic video analysis. I certainly hope that this bit of information spurs thought and discussion on the issue of the science of forensic video analysis and the use of Adobe’s Photoshop within the analyst’s work flow. I have found Photoshop to be one of the most potent tools in my toolbox. I love finding new ways to employ my old and trusted friend. If there is some process that you use, take a second, put it down on paper, and send it in. Have a question? Ask. 

I try to attend any Photoshop class, whatever the level, because there is always something more to learn … or a different way of looking at the same problem. I always encourage Photoshop users of all levels to do the same. Look for opportunities to further your knowledge. Read the new Photoshop books as they come out. See if and how the information conveyed in the book can apply to your workflow. If you find something new and great, don’t be afraid to share. For my part, I will be teaching a 2 day Forensic Photoshop class at the 2007 LEVA Conference. I’d love to see you there.

Beardsworth, J. (2005). Photoshop Blending Modes Cookbook. O'Reilly.
French, N. (2006). Adobe Photoshop Unmasked: The Art and Science of Selections, Layers, and Paths. Adobe Press.
GIMP Documentation Team. (2007). GNU Image Manipulation Program User Manual. Free Software Foundation.
Harrington, R. (2006). Understanding: Adobe® Photoshop®: Digital Imaging Concepts and Techniques. Peachpit Press.
Lawler, B. (2005). Official Adobe Print Publishing Guide, Second Edition: The Essential Resource for Design, Production, and Prepress. Adobe Press.
Long, B. (2007). Adobe Photoshop CS3 Beta First Look with Adobe Bridge and Camera Raw. Peachpit Press.
Pankala, B. (2006). Layer blending modes explained. Retrieved from TeamPhotoshop:
Reding, E. (2006). Adobe® Photoshop® CS2 Revealed. Course Technology.


Anonymous said...

Totally awesome reference. Saved me a few hours investigation! Thanks.

xeption12 said...

2 things:
2. for Screen Effect the formula is actualy 255 - ((255-U)*(255-L))/255

Jim Hoerricks said...

plug the numbers in and show your work. In your formula, the top and bottom 255 would cancel each other out. Try it.

xeption12 said...

I've used this formula to achieve the overlay effect (which is actually screen effect over multiply effect) in PHP through GD library, and if the formula is with 1- then all the values I get are negative and then they're automaticly set to zero (as you recommanded a few lines back). At first I tried to multiply those values with -1 and see what i get but the result was something like inverting the imagine which wasn't what i was expecting. I really don't know why it worked when i've used "255 - " instead of "1 -". I was a little confused since at first you said 'inverting pixel values' is equivalent to 'substracting them from 255', and then you said "inverts this value again" (the value you was poiting here was (255-U)*(255-L)) and i see no 255-(255-U)*(255-L)... i'm still confused about that and i'm sorry if im wrong somehow :) you did a very great job... but for me worked with 255 - ((255-U)*(255-L))/255 and goddamit if i know why :)

xeption12 said...

"In your formula, the top and bottom 255 would cancel each other out." what do you mean they would cancel each other out ?! cancelling each other out would happen if there was 255 * ((255-U)*(255-L))/255 no? but that is 255 (minus) ((255-U)*(255-L))/255 .... sorry for being a newbie

Anonymous said...

Soft Light and Overlay -

These are the results of my experiments. Not sure if they check out mathematically, but they look approximately right.

David said...

This note concerns the formulae for Screen and Soft Light. Both are in error!

Screen: A comment by "xeption" shows the formula can not be right.

The way to think about this is via decimal fractions. If L and U are tone values, between 0 and 255, let U% = U/255 and L% = L/255 denote the "fraction of white" in a tone.

The product L%*U% always darkens producing a value between 0 and 1 but no more than L% or U%. To "Screen" we need to make tones lighter. U% and L% describe the "distance to black", so 1-U% and 1-L% measure the "distance to white." Their product (1-U%)(1-L%) would as above decrease the distance from white. The complement
1 - (1-U%)(1-L%)
is the distance from black which has been increased --- the resulting tone is lighter.

Writing decimal fractions in terms of tone values requires back substitutions
1 - (1-U/255)(1-L/255)
which is
1 - (255-U)/255 *(255-L)/255
As this equation represents the fraction of change, we must multiply by 255 to get the desired tone, finally achieving, after cancellation
255 - (255-U)*(255-L)/255 ***

This is the correct Screen formula.

Soft Light. Soft Light uses the formula for Screen so, as it stands, Soft Light at least needs this correction. However, there are actually two further errors.
The given Soft Light formula is wrong independent of the "screen error" in the first term alone.
The formula given is
X = ( ((255-L)*U*L) + (L*R_s) )/255
where the trailing 255 divides both terms. The first term is a triple product of tones so, for example, could have the value 127*128*128/255. Beside the obvious problem that the value is out of range, the units appear to be tone*tone (the 255 cancels one of them.) With incorrect units the formula cannot be correct.

Attempt at a FIX??? Let's eliminate the units problem by dividing by another 255; the divisor will be placed carefully in the formula to get
X = ( ((255-L)*(U/255)*L)+ (L*R_s) )/255

Putting this together with the corrected Screen formula (see *** above)
X = ( ((255-L)*(U/255)*L)+ (L* (255 - (255-U)*(255-L)/255) ) )/255
Carrying out the last division by 255,
X = ( ((255-L)/255*(U/255)*L)+ (L* (255/255 - (255-U)/255*(255-L)/255) ) )
Substitutions again
X = ( ((1-L%)*(U%)*L)+ (L* (1 - (1 - U%*)(1-L%) ) )
L may be factored and we can collect terms
X = L* ( (1-L%)*U% + 1 - (1 - U%)(1-L%) ) )
X = L* ( U% - L%U% + 1 - 1 + U% + L% - L%U% )
At last:
X = L * ( 2U% + L% - 2*L%U% ) ****
If one insists on tone units,
X = L * ( 2U + L - 2*L*U/255 )/255

The formula **** requires some tests to see if in fact it agrees with what occurs in Photoshop. There are two cases in its favor, and others not.

It's good that when the upper layer is middle gray (U% = 1/2) Soft Light returns L for no net change. It appears after checking that if U% < 0.5 the correct result is achieved, but when U% is above 0.5 there are discrepancies. For example when U = 255 and L = 25, this formula returns tone 48 while Photoshop returns 78. The percentage error is 30/255 or about 12%.

It is common for blend formulae to have cases, one for U% < 0.5 and another for U% above 0.5. This appears to be needed here! In summary, the screen formula is corrected; the Soft Light formula is correct for low U% but seriously off for large U%.

What to do.

There is another formula for the SOFT LIGHT fraction (see that agrees in the THEN clause

If U% <.5 THEN 2U%L% + L%^2 (1-2U%) ELSE sqrt(L%) (2U% - 1) + 2 L%(1 - U%)