PDA

View Full Version : Image filters

 Do3d10-06-2009, 10:25 AMHey I am looking for more information about the algorithm used in the standard imaging filters such as multiply , burn, dodge etc... Can anyonme suggest me some programming, math or books on this topic.
tmr232
10-06-2009, 11:40 AM
The basic blend modes (multiply, burn, dodge, lighten, darken etc.) are all pretty simple local operations on two layers of color. So basically, all you need is a blend function, and to use it on all pixels in your resulting image.

Basic C code with all the blend modes can be found here: LINK (http://www.nathanm.com/photoshop-blending-math/)

If you have a basic understanding of C, this should really be enough.
Feel free to ask if you need further guidance.

Tamir.

Do3d
10-06-2009, 11:48 AM
Cool Thanks. Thats what I was looking for.

ndeboar
10-06-2009, 01:35 PM
multiply = a x b
darken = min (a,b)
lighting = max (a,b)
linear dodge = a + b

wobagi
10-18-2009, 03:22 PM
PDF reference describes this.

ScEngMan
10-24-2009, 02:49 PM
When blending two layers with a Blending Mode and Opacity, there are two steps:

1. Generate the Blending Layer using a Blending Mode (Multiply, Divide, etc).
This is done by mixing the Background Layer with the Foreground Layer using a Blending function.

2. Combine the Blending Layer with the Background Layer using a Combine function (most usually MixOpacity).

I implemented Blending functions and Combine functions for colors defined by 4 unsigned chars from many resources (including the links above), and I created a single header -mix.h (http://sceneengine.svn.sourceforge.net/viewvc/sceneengine/trunk/SceneEngine/ScEngLibs/include/ScEng/math/mix.h?revision=1249&view=markup) - that can be used anywhere.

Here is an example of how to combine two colors to get the same result as PhotoShop with two layers with alpha, a Multiply blending mode and an Opacity of 50.0.

#include "mix.h"

unsigned char bg[4] = { 180, 70, 70, 255 };
unsigned char fg[4] = { 80, 70, 175, 128 };

unsigned char opacity = 128; (50%)

unsigned char dest[4] = { 0, 0, 0, 0 };

Multiply( dest, bg, fg ); // Blend bg and fg -> dest
MixOpacity( dest, bg, dest, opacity ); // Combint bg and dest -> dest

All this stuff is implemented by the Texture Painting tool for CrackArt (http://www.crackart.org), so you can download the full source code and check it out.

MDuffy
10-27-2009, 10:46 PM
I know others have already answered the question, but I've always found the following site to be a good description of each blending mode:

http://www.pegtop.net/delphi/articles/blendmodes/

Cheers,
Michael

mtartist
11-08-2009, 08:16 PM
I have a question on the related subject. Given two RGBA colours c1 = {r1, g1, b1, a1} (top) and c2 = {r2, g2, b2, a2} (bottom), I want to find the resulting colour cr = {r, g, b, a} when both colours are composited using the 'Normal' mode algorithm.

The algorithms I managed to find, seem to assume that the bottom colour has an opacity of 255 (fully opaque), but what if it doesn't?

After some experimentation, I figured that the resultant opacity can be calculated by:

a = (255 - a2) / 255 * a1 + a2;

Could anyone please tell me how I can find the other three colour components?

Thank you

wobagi
11-08-2009, 10:22 PM
From what I find in PDF spec:

a_r * C_r = ((1-a_1) * a_2 * C_2) + ((1-a_2) * a_1 * C_1) +
(a_2 * a_1 * B(C_2, C_1))

where
a_? - alpha for resulting (r), top (1) and bottom (2) layers
C_? - color, accordingly
B() - blend function

in this case (normal mode): B(C_2, C_1) = C_1, so
a_r * C_r = ((1-a_1)*a_2*C_2) + ((1-a_2)*a_1*C_1) + (a_2*a_1*C_1) =
((1-a_1)*a_2*C_2) + a_1*C_1

mtartist
11-09-2009, 09:21 AM
Thank you wobagi, it worked fine. Also, after having a look at that reference PDF, it seems to be very useful.

CGTalk Moderation
11-09-2009, 09:21 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.