Cleanup: warning & whitespace
[blender.git] / source / blender / compositor / operations / COM_EllipseMaskOperation.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_EllipseMaskOperation.h"
24 #include "BLI_math.h"
25 #include "DNA_node_types.h"
26
27 EllipseMaskOperation::EllipseMaskOperation() : 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 EllipseMaskOperation::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 EllipseMaskOperation::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         const float halfHeight = (this->m_data->height) / 2.0f;
64         const float halfWidth = this->m_data->width / 2.0f;
65         float sx = rx - this->m_data->x;
66         sx *= sx;
67         const float tx = halfWidth * halfWidth;
68         float sy = ry - this->m_data->y;
69         sy *= sy;
70         const float ty = halfHeight * halfHeight;
71         
72         bool inside = ((sx / tx) + (sy / ty)) < 1.0f;
73         
74         switch (this->m_maskType) {
75                 case CMP_NODE_MASKTYPE_ADD:
76                         if (inside) {
77                                 output[0] = max(inputMask[0], inputValue[0]);
78                         }
79                         else {
80                                 output[0] = inputMask[0];
81                         }
82                         break;
83                 case CMP_NODE_MASKTYPE_SUBTRACT:
84                         if (inside) {
85                                 output[0] = inputMask[0] - inputValue[0];
86                                 CLAMP(output[0], 0, 1);
87                         }
88                         else {
89                                 output[0] = inputMask[0];
90                         }
91                         break;
92                 case CMP_NODE_MASKTYPE_MULTIPLY:
93                         if (inside) {
94                                 output[0] = inputMask[0] * inputValue[0];
95                         }
96                         else {
97                                 output[0] = 0;
98                         }
99                         break;
100                 case CMP_NODE_MASKTYPE_NOT:
101                         if (inside) {
102                                 if (inputMask[0] > 0.0f) {
103                                         output[0] = 0;
104                                 }
105                                 else {
106                                         output[0] = inputValue[0];
107                                 }
108                         }
109                         else {
110                                 output[0] = inputMask[0];
111                         }
112                         break;
113         }
114 }
115
116 void EllipseMaskOperation::deinitExecution()
117 {
118         this->m_inputMask = NULL;
119         this->m_inputValue = NULL;
120 }
121