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