D2607: Switch eye dropper to use linear color space internally
[blender.git] / source / blender / compositor / operations / COM_DifferenceMatteOperation.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor: 
19  *              Jeroen Bakker 
20  *              Monique Dewanchand
21  */
22
23 #include "COM_DifferenceMatteOperation.h"
24 #include "BLI_math.h"
25
26 DifferenceMatteOperation::DifferenceMatteOperation() : NodeOperation()
27 {
28         addInputSocket(COM_DT_COLOR);
29         addInputSocket(COM_DT_COLOR);
30         addOutputSocket(COM_DT_VALUE);
31
32         this->m_inputImage1Program = NULL;
33         this->m_inputImage2Program = NULL;
34 }
35
36 void DifferenceMatteOperation::initExecution()
37 {
38         this->m_inputImage1Program = this->getInputSocketReader(0);
39         this->m_inputImage2Program = this->getInputSocketReader(1);
40 }
41 void DifferenceMatteOperation::deinitExecution()
42 {
43         this->m_inputImage1Program = NULL;
44         this->m_inputImage2Program = NULL;
45 }
46
47 void DifferenceMatteOperation::executePixelSampled(float output[4], float x, float y, PixelSampler sampler)
48 {
49         float inColor1[4];
50         float inColor2[4];
51
52         const float tolerance = this->m_settings->t1;
53         const float falloff = this->m_settings->t2;
54         float difference;
55         float alpha;
56
57         this->m_inputImage1Program->readSampled(inColor1, x, y, sampler);
58         this->m_inputImage2Program->readSampled(inColor2, x, y, sampler);
59
60         difference = (fabsf(inColor2[0] - inColor1[0]) +
61                       fabsf(inColor2[1] - inColor1[1]) +
62                       fabsf(inColor2[2] - inColor1[2]));
63
64         /* average together the distances */
65         difference = difference / 3.0f;
66
67         /* make 100% transparent */
68         if (difference <= tolerance) {
69                 output[0] = 0.0f;
70         }
71         /*in the falloff region, make partially transparent */
72         else if (difference <= falloff + tolerance) {
73                 difference = difference - tolerance;
74                 alpha = difference / falloff;
75                 /*only change if more transparent than before */
76                 if (alpha < inColor1[3]) {
77                         output[0] = alpha;
78                 }
79                 else { /* leave as before */
80                         output[0] = inColor1[3];
81                 }
82         }
83         else {
84                 /* foreground object */
85                 output[0] = inColor1[3];
86         }
87 }
88