Cleanup: comment line length (editors)
[blender.git] / source / blender / compositor / nodes / COM_DilateErodeNode.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_DilateErodeNode.h"
24 #include "COM_ExecutionSystem.h"
25 #include "COM_DilateErodeOperation.h"
26 #include "COM_AntiAliasOperation.h"
27 #include "COM_GaussianAlphaXBlurOperation.h"
28 #include "COM_GaussianAlphaYBlurOperation.h"
29 #include "BLI_math.h"
30
31 DilateErodeNode::DilateErodeNode(bNode *editorNode) : Node(editorNode)
32 {
33         /* initialize node data */
34         NodeBlurData *data = &m_alpha_blur;
35         memset(data, 0, sizeof(NodeBlurData));
36         data->filtertype = R_FILTER_GAUSS;
37
38         if (editorNode->custom2 > 0) {
39                 data->sizex = data->sizey = editorNode->custom2;
40         }
41         else {
42                 data->sizex = data->sizey = -editorNode->custom2;
43         }
44 }
45
46 void DilateErodeNode::convertToOperations(NodeConverter &converter, const CompositorContext &context) const
47 {
48
49         bNode *editorNode = this->getbNode();
50         if (editorNode->custom1 == CMP_NODE_DILATEERODE_DISTANCE_THRESH) {
51                 DilateErodeThresholdOperation *operation = new DilateErodeThresholdOperation();
52                 operation->setDistance(editorNode->custom2);
53                 operation->setInset(editorNode->custom3);
54                 converter.addOperation(operation);
55
56                 converter.mapInputSocket(getInputSocket(0), operation->getInputSocket(0));
57
58                 if (editorNode->custom3 < 2.0f) {
59                         AntiAliasOperation *antiAlias = new AntiAliasOperation();
60                         converter.addOperation(antiAlias);
61
62                         converter.addLink(operation->getOutputSocket(), antiAlias->getInputSocket(0));
63                         converter.mapOutputSocket(getOutputSocket(0), antiAlias->getOutputSocket(0));
64                 }
65                 else {
66                         converter.mapOutputSocket(getOutputSocket(0), operation->getOutputSocket(0));
67                 }
68         }
69         else if (editorNode->custom1 == CMP_NODE_DILATEERODE_DISTANCE) {
70                 if (editorNode->custom2 > 0) {
71                         DilateDistanceOperation *operation = new DilateDistanceOperation();
72                         operation->setDistance(editorNode->custom2);
73                         converter.addOperation(operation);
74
75                         converter.mapInputSocket(getInputSocket(0), operation->getInputSocket(0));
76                         converter.mapOutputSocket(getOutputSocket(0), operation->getOutputSocket(0));
77                 }
78                 else {
79                         ErodeDistanceOperation *operation = new ErodeDistanceOperation();
80                         operation->setDistance(-editorNode->custom2);
81                         converter.addOperation(operation);
82
83                         converter.mapInputSocket(getInputSocket(0), operation->getInputSocket(0));
84                         converter.mapOutputSocket(getOutputSocket(0), operation->getOutputSocket(0));
85                 }
86         }
87         else if (editorNode->custom1 == CMP_NODE_DILATEERODE_DISTANCE_FEATHER) {
88                 /* this uses a modified gaussian blur function otherwise its far too slow */
89                 CompositorQuality quality = context.getQuality();
90
91                 GaussianAlphaXBlurOperation *operationx = new GaussianAlphaXBlurOperation();
92                 operationx->setData(&m_alpha_blur);
93                 operationx->setQuality(quality);
94                 operationx->setFalloff(PROP_SMOOTH);
95                 converter.addOperation(operationx);
96
97                 converter.mapInputSocket(getInputSocket(0), operationx->getInputSocket(0));
98                 // converter.mapInputSocket(getInputSocket(1), operationx->getInputSocket(1)); // no size input yet
99
100                 GaussianAlphaYBlurOperation *operationy = new GaussianAlphaYBlurOperation();
101                 operationy->setData(&m_alpha_blur);
102                 operationy->setQuality(quality);
103                 operationy->setFalloff(PROP_SMOOTH);
104                 converter.addOperation(operationy);
105
106                 converter.addLink(operationx->getOutputSocket(), operationy->getInputSocket(0));
107                 // converter.mapInputSocket(getInputSocket(1), operationy->getInputSocket(1)); // no size input yet
108                 converter.mapOutputSocket(getOutputSocket(0), operationy->getOutputSocket());
109
110                 converter.addPreview(operationy->getOutputSocket());
111
112                 /* TODO? */
113                 /* see gaussian blue node for original usage */
114 #if 0
115                 if (!connectedSizeSocket) {
116                         operationx->setSize(size);
117                         operationy->setSize(size);
118                 }
119 #else
120                 operationx->setSize(1.0f);
121                 operationy->setSize(1.0f);
122 #endif
123                 operationx->setSubtract(editorNode->custom2 < 0);
124                 operationy->setSubtract(editorNode->custom2 < 0);
125
126                 if (editorNode->storage) {
127                         NodeDilateErode *data_storage = (NodeDilateErode *)editorNode->storage;
128                         operationx->setFalloff(data_storage->falloff);
129                         operationy->setFalloff(data_storage->falloff);
130                 }
131         }
132         else {
133                 if (editorNode->custom2 > 0) {
134                         DilateStepOperation *operation = new DilateStepOperation();
135                         operation->setIterations(editorNode->custom2);
136                         converter.addOperation(operation);
137
138                         converter.mapInputSocket(getInputSocket(0), operation->getInputSocket(0));
139                         converter.mapOutputSocket(getOutputSocket(0), operation->getOutputSocket(0));
140                 }
141                 else {
142                         ErodeStepOperation *operation = new ErodeStepOperation();
143                         operation->setIterations(-editorNode->custom2);
144                         converter.addOperation(operation);
145
146                         converter.mapInputSocket(getInputSocket(0), operation->getInputSocket(0));
147                         converter.mapOutputSocket(getOutputSocket(0), operation->getOutputSocket(0));
148                 }
149         }
150 }