4e9569053110e98b88fe2605b478f0ef35b35e4d
[blender.git] / source / blender / compositor / operations / COM_BoxMaskOperation.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_BoxMaskOperation.h"
24 #include "BLI_math.h"
25 #include "DNA_node_types.h"
26
27 BoxMaskOperation::BoxMaskOperation() : NodeOperation()
28 {
29         this->addInputSocket(COM_DT_VALUE);
30         this->addInputSocket(COM_DT_VALUE);
31         this->addOutputSocket(COM_DT_VALUE);
32         this->m_inputMask = NULL;
33         this->m_inputValue = NULL;
34         this->m_cosine = 0.0f;
35         this->m_sine = 0.0f;
36 }
37 void BoxMaskOperation::initExecution()
38 {
39         this->m_inputMask = this->getInputSocketReader(0);
40         this->m_inputValue = this->getInputSocketReader(1);
41         const double rad = (double)this->m_data->rotation;
42         this->m_cosine = cos(rad);
43         this->m_sine = sin(rad);
44         this->m_aspectRatio = ((float)this->getWidth()) / this->getHeight();
45 }
46
47 void BoxMaskOperation::executePixelSampled(float output[4], float x, float y, PixelSampler sampler)
48 {
49         float inputMask[4];
50         float inputValue[4];
51         
52         float rx = x / this->getWidth();
53         float ry = y / this->getHeight();
54         
55         const float dy = (ry - this->m_data->y) / this->m_aspectRatio;
56         const float dx = rx - this->m_data->x;
57         rx = this->m_data->x + (this->m_cosine * dx + this->m_sine * dy);
58         ry = this->m_data->y + (-this->m_sine * dx + this->m_cosine * dy);
59         
60         this->m_inputMask->readSampled(inputMask, x, y, sampler);
61         this->m_inputValue->readSampled(inputValue, x, y, sampler);
62         
63         float halfHeight = this->m_data->height / 2.0f;
64         float halfWidth = this->m_data->width / 2.0f;
65         bool inside = (rx > this->m_data->x - halfWidth &&
66                        rx < this->m_data->x + halfWidth &&
67                        ry > this->m_data->y - halfHeight &&
68                        ry < this->m_data->y + halfHeight);
69         
70         switch (this->m_maskType) {
71                 case CMP_NODE_MASKTYPE_ADD:
72                         if (inside) {
73                                 output[0] = max(inputMask[0], inputValue[0]);
74                         }
75                         else {
76                                 output[0] = inputMask[0];
77                         }
78                         break;
79                 case CMP_NODE_MASKTYPE_SUBTRACT:
80                         if (inside) {
81                                 output[0] = inputMask[0] - inputValue[0];
82                                 CLAMP(output[0], 0, 1);
83                         }
84                         else {
85                                 output[0] = inputMask[0];
86                         }
87                         break;
88                 case CMP_NODE_MASKTYPE_MULTIPLY:
89                         if (inside) {
90                                 output[0] = inputMask[0] * inputValue[0];
91                         }
92                         else {
93                                 output[0] = 0;
94                         }
95                         break;
96                 case CMP_NODE_MASKTYPE_NOT:
97                         if (inside) {
98                                 if (inputMask[0] > 0.0f) {
99                                         output[0] = 0;
100                                 }
101                                 else {
102                                         output[0] = inputValue[0];
103                                 }
104                         }
105                         else {
106                                 output[0] = inputMask[0];
107                         }
108                         break;
109         }
110
111
112 }
113
114 void BoxMaskOperation::deinitExecution()
115 {
116         this->m_inputMask = NULL;
117         this->m_inputValue = NULL;
118 }
119