ded686bb5de0ed8477da07b09ee10ed9a7b9b061
[blender.git] / source / blender / compositor / operations / COM_CombineChannelsOperation.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_CombineChannelsOperation.h"
24 #include "BLI_utildefines.h"
25
26 CombineChannelsOperation::CombineChannelsOperation() : NodeOperation()
27 {
28         this->addInputSocket(COM_DT_VALUE);
29         this->addInputSocket(COM_DT_VALUE);
30         this->addInputSocket(COM_DT_VALUE);
31         this->addInputSocket(COM_DT_VALUE);
32         this->addOutputSocket(COM_DT_COLOR);
33         this->setResolutionInputSocketIndex(0);
34         this->m_inputChannel1Operation = NULL;
35         this->m_inputChannel2Operation = NULL;
36         this->m_inputChannel3Operation = NULL;
37         this->m_inputChannel4Operation = NULL;
38 }
39
40 bool CombineChannelsOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output) 
41 {
42         rcti tempOutput;
43         bool first = true;
44         for (int i = 0 ; i < 4 ; i ++) {
45                 NodeOperation * inputOperation = this->getInputOperation(i);
46                 if (inputOperation->determineDependingAreaOfInterest(input, readOperation, &tempOutput)) {
47                         if (first) {
48                                 output->xmin = tempOutput.xmin;
49                                 output->ymin = tempOutput.ymin;
50                                 output->xmax = tempOutput.xmax;
51                                 output->ymax = tempOutput.ymax;
52                                 first = false;
53                         }
54                         else {
55                                 output->xmin = MIN2(output->xmin, tempOutput.xmin);
56                                 output->ymin = MIN2(output->ymin, tempOutput.ymin);
57                                 output->xmax = MAX2(output->xmax, tempOutput.xmax);
58                                 output->ymax = MAX2(output->ymax, tempOutput.ymax);
59                         }
60                 }
61         }
62         return !first;
63 }
64
65 void CombineChannelsOperation::initExecution()
66 {
67         this->m_inputChannel1Operation = this->getInputSocketReader(0);
68         this->m_inputChannel2Operation = this->getInputSocketReader(1);
69         this->m_inputChannel3Operation = this->getInputSocketReader(2);
70         this->m_inputChannel4Operation = this->getInputSocketReader(3);
71 }
72
73 void CombineChannelsOperation::deinitExecution()
74 {
75         this->m_inputChannel1Operation = NULL;
76         this->m_inputChannel2Operation = NULL;
77         this->m_inputChannel3Operation = NULL;
78         this->m_inputChannel4Operation = NULL;
79 }
80
81
82 void CombineChannelsOperation::executePixel(float output[4], float x, float y, PixelSampler sampler)
83 {
84         float input[4];
85         /// @todo: remove if statements
86         if (this->m_inputChannel1Operation) {
87                 this->m_inputChannel1Operation->read(input, x, y, sampler);
88                 output[0] = input[0];
89         }
90         if (this->m_inputChannel2Operation) {
91                 this->m_inputChannel2Operation->read(input, x, y, sampler);
92                 output[1] = input[0];
93         }
94         if (this->m_inputChannel3Operation) {
95                 this->m_inputChannel3Operation->read(input, x, y, sampler);
96                 output[2] = input[0];
97         }
98         if (this->m_inputChannel4Operation) {
99                 this->m_inputChannel4Operation->read(input, x, y, sampler);
100                 output[3] = input[0];
101         }
102 }