33e35c3fe3bbf5b9afbd746c41983aab46e9654c
[blender.git] / source / blender / compositor / operations / COM_BrightnessOperation.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_BrightnessOperation.h"
24
25 BrightnessOperation::BrightnessOperation() : NodeOperation()
26 {
27         this->addInputSocket(COM_DT_COLOR);
28         this->addInputSocket(COM_DT_VALUE);
29         this->addInputSocket(COM_DT_VALUE);
30         this->addOutputSocket(COM_DT_COLOR);
31         this->m_inputProgram = NULL;
32 }
33 void BrightnessOperation::initExecution()
34 {
35         this->m_inputProgram = this->getInputSocketReader(0);
36         this->m_inputBrightnessProgram = this->getInputSocketReader(1);
37         this->m_inputContrastProgram = this->getInputSocketReader(2);
38 }
39
40 void BrightnessOperation::executePixelSampled(float output[4], float x, float y, PixelSampler sampler)
41 {
42         float inputValue[4];
43         float a, b;
44         float inputBrightness[4];
45         float inputContrast[4];
46         this->m_inputProgram->readSampled(inputValue, x, y, sampler);
47         this->m_inputBrightnessProgram->readSampled(inputBrightness, x, y, sampler);
48         this->m_inputContrastProgram->readSampled(inputContrast, x, y, sampler);
49         float brightness = inputBrightness[0];
50         float contrast = inputContrast[0];
51         brightness /= 100.0f;
52         float delta = contrast / 200.0f;
53         a = 1.0f - delta * 2.0f;
54         /*
55          * The algorithm is by Werner D. Streidt
56          * (http://visca.com/ffactory/archives/5-99/msg00021.html)
57          * Extracted of OpenCV demhist.c
58          */
59         if (contrast > 0) {
60                 a = 1.0f / a;
61                 b = a * (brightness - delta);
62         }
63         else {
64                 delta *= -1;
65                 b = a * (brightness + delta);
66         }
67         
68         output[0] = a * inputValue[0] + b;
69         output[1] = a * inputValue[1] + b;
70         output[2] = a * inputValue[2] + b;
71         output[3] = inputValue[3];
72 }
73
74 void BrightnessOperation::deinitExecution()
75 {
76         this->m_inputProgram = NULL;
77         this->m_inputBrightnessProgram = NULL;
78         this->m_inputContrastProgram = NULL;
79 }
80