Fix T51308: Bright/Contrast Doesn't respect Pre-multiplied Alpha
[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         this->m_use_premultiply = false;
33 }
34
35 void BrightnessOperation::setUsePremultiply(bool use_premultiply)
36 {
37         this->m_use_premultiply = use_premultiply;
38 }
39
40 void BrightnessOperation::initExecution()
41 {
42         this->m_inputProgram = this->getInputSocketReader(0);
43         this->m_inputBrightnessProgram = this->getInputSocketReader(1);
44         this->m_inputContrastProgram = this->getInputSocketReader(2);
45 }
46
47 void BrightnessOperation::executePixelSampled(float output[4], float x, float y, PixelSampler sampler)
48 {
49         float inputValue[4];
50         float a, b;
51         float inputBrightness[4];
52         float inputContrast[4];
53         this->m_inputProgram->readSampled(inputValue, x, y, sampler);
54         this->m_inputBrightnessProgram->readSampled(inputBrightness, x, y, sampler);
55         this->m_inputContrastProgram->readSampled(inputContrast, x, y, sampler);
56         float brightness = inputBrightness[0];
57         float contrast = inputContrast[0];
58         brightness /= 100.0f;
59         float delta = contrast / 200.0f;
60         a = 1.0f - delta * 2.0f;
61         /*
62          * The algorithm is by Werner D. Streidt
63          * (http://visca.com/ffactory/archives/5-99/msg00021.html)
64          * Extracted of OpenCV demhist.c
65          */
66         if (contrast > 0) {
67                 a = 1.0f / a;
68                 b = a * (brightness - delta);
69         }
70         else {
71                 delta *= -1;
72                 b = a * (brightness + delta);
73         }
74         if (this->m_use_premultiply) {
75                 premul_to_straight_v4(inputValue);
76         }
77         output[0] = a * inputValue[0] + b;
78         output[1] = a * inputValue[1] + b;
79         output[2] = a * inputValue[2] + b;
80         output[3] = inputValue[3];
81         if (this->m_use_premultiply) {
82                 straight_to_premul_v4(output);
83         }
84 }
85
86 void BrightnessOperation::deinitExecution()
87 {
88         this->m_inputProgram = NULL;
89         this->m_inputBrightnessProgram = NULL;
90         this->m_inputContrastProgram = NULL;
91 }
92