quiet all -Wshadow warnings in the compositor.
[blender.git] / source / blender / compositor / intern / COM_Converter.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 <string.h>
24
25 #include "BKE_node.h"
26
27 #include "COM_AlphaOverNode.h"
28 #include "COM_BilateralBlurNode.h"
29 #include "COM_BlurNode.h"
30 #include "COM_BokehBlurNode.h"
31 #include "COM_BokehImageNode.h"
32 #include "COM_BoxMaskNode.h"
33 #include "COM_BrightnessNode.h"
34 #include "COM_ChannelMatteNode.h"
35 #include "COM_ChromaMatteNode.h"
36 #include "COM_ColorBalanceNode.h"
37 #include "COM_ColorCorrectionNode.h"
38 #include "COM_ColorCurveNode.h"
39 #include "COM_ColorMatteNode.h"
40 #include "COM_ColorNode.h"
41 #include "COM_ColorRampNode.h"
42 #include "COM_ColorSpillNode.h"
43 #include "COM_ColorToBWNode.h"
44 #include "COM_CombineHSVANode.h"
45 #include "COM_CombineRGBANode.h"
46 #include "COM_CombineYCCANode.h"
47 #include "COM_CombineYUVANode.h"
48 #include "COM_CompositorNode.h"
49 #include "COM_ConvertAlphaNode.h"
50 #include "COM_ConvertColorToVectorOperation.h"
51 #include "COM_ConvertColourToValueProg.h"
52 #include "COM_ConvertValueToColourProg.h"
53 #include "COM_ConvertValueToVectorOperation.h"
54 #include "COM_ConvertVectorToColorOperation.h"
55 #include "COM_ConvertVectorToValueOperation.h"
56 #include "COM_Converter.h"
57 #include "COM_CropNode.h"
58 #include "COM_DefocusNode.h"
59 #include "COM_DifferenceMatteNode.h"
60 #include "COM_DilateErodeNode.h"
61 #include "COM_DirectionalBlurNode.h"
62 #include "COM_DisplaceNode.h"
63 #include "COM_DistanceMatteNode.h"
64 #include "COM_DoubleEdgeMaskNode.h"
65 #include "COM_EllipseMaskNode.h"
66 #include "COM_ExecutionSystem.h"
67 #include "COM_ExecutionSystemHelper.h"
68 #include "COM_FilterNode.h"
69 #include "COM_FlipNode.h"
70 #include "COM_GammaNode.h"
71 #include "COM_GlareNode.h"
72 #include "COM_GroupNode.h"
73 #include "COM_HueSaturationValueCorrectNode.h"
74 #include "COM_HueSaturationValueNode.h"
75 #include "COM_IDMaskNode.h"
76 #include "COM_ImageNode.h"
77 #include "COM_InvertNode.h"
78 #include "COM_KeyingNode.h"
79 #include "COM_KeyingScreenNode.h"
80 #include "COM_LensDistortionNode.h"
81 #include "COM_LuminanceMatteNode.h"
82 #include "COM_MapUVNode.h"
83 #include "COM_MapValueNode.h"
84 #include "COM_MaskNode.h"
85 #include "COM_MathNode.h"
86 #include "COM_MixNode.h"
87 #include "COM_MovieClipNode.h"
88 #include "COM_MovieDistortionNode.h"
89 #include "COM_MuteNode.h"
90 #include "COM_NormalNode.h"
91 #include "COM_NormalizeNode.h"
92 #include "COM_OutputFileNode.h"
93 #include "COM_RenderLayersNode.h"
94 #include "COM_RotateNode.h"
95 #include "COM_ScaleNode.h"
96 #include "COM_ScaleOperation.h"
97 #include "COM_SeparateHSVANode.h"
98 #include "COM_SeparateRGBANode.h"
99 #include "COM_SeparateYCCANode.h"
100 #include "COM_SeparateYUVANode.h"
101 #include "COM_SetAlphaNode.h"
102 #include "COM_SetValueOperation.h"
103 #include "COM_SocketConnection.h"
104 #include "COM_SplitViewerNode.h"
105 #include "COM_Stabilize2dNode.h"
106 #include "COM_SwitchNode.h"
107 #include "COM_TextureNode.h"
108 #include "COM_TimeNode.h"
109 #include "COM_TonemapNode.h"
110 #include "COM_TransformNode.h"
111 #include "COM_TranslateNode.h"
112 #include "COM_TranslateOperation.h"
113 #include "COM_ValueNode.h"
114 #include "COM_VectorBlurNode.h"
115 #include "COM_VectorCurveNode.h"
116 #include "COM_ViewLevelsNode.h"
117 #include "COM_ViewerNode.h"
118 #include "COM_ZCombineNode.h"
119
120 Node *Converter::convert(bNode *b_node)
121 {
122         Node *node;
123
124         if (b_node->flag & NODE_MUTED) {
125                 node = new MuteNode(b_node);
126                 return node;
127         }
128
129         switch (b_node->type) {
130                 case CMP_NODE_COMPOSITE:
131                         node = new CompositorNode(b_node);
132                         break;
133                 case CMP_NODE_R_LAYERS:
134                         node = new RenderLayersNode(b_node);
135                         break;
136                 case CMP_NODE_TEXTURE:
137                         node = new TextureNode(b_node);
138                         break;
139                 case CMP_NODE_RGBTOBW:
140                         node = new ColourToBWNode(b_node);
141                         break;
142                 case CMP_NODE_MIX_RGB:
143                         node = new MixNode(b_node);
144                         break;
145                 case CMP_NODE_TRANSLATE:
146                         node = new TranslateNode(b_node);
147                         break;
148                 case CMP_NODE_SCALE:
149                         node = new ScaleNode(b_node);
150                         break;
151                 case CMP_NODE_ROTATE:
152                         node = new RotateNode(b_node);
153                         break;
154                 case CMP_NODE_FLIP:
155                         node = new FlipNode(b_node);
156                         break;
157                 case CMP_NODE_FILTER:
158                         node = new FilterNode(b_node);
159                         break;
160                 case CMP_NODE_ID_MASK:
161                         node = new IDMaskNode(b_node);
162                         break;
163                 case CMP_NODE_BRIGHTCONTRAST:
164                         node = new BrightnessNode(b_node);
165                         break;
166                 case CMP_NODE_SEPRGBA:
167                         node = new SeparateRGBANode(b_node);
168                         break;
169                 case CMP_NODE_COMBRGBA:
170                         node = new CombineRGBANode(b_node);
171                         break;
172                 case CMP_NODE_SEPHSVA:
173                         node = new SeparateHSVANode(b_node);
174                         break;
175                 case CMP_NODE_COMBHSVA:
176                         node = new CombineHSVANode(b_node);
177                         break;
178                 case CMP_NODE_SEPYUVA:
179                         node = new SeparateYUVANode(b_node);
180                         break;
181                 case CMP_NODE_COMBYUVA:
182                         node = new CombineYUVANode(b_node);
183                         break;
184                 case CMP_NODE_SEPYCCA:
185                         node = new SeparateYCCANode(b_node);
186                         break;
187                 case CMP_NODE_COMBYCCA:
188                         node = new CombineYCCANode(b_node);
189                         break;
190                 case CMP_NODE_ALPHAOVER:
191                         node = new AlphaOverNode(b_node);
192                         break;
193                 case CMP_NODE_COLORBALANCE:
194                         node = new ColorBalanceNode(b_node);
195                         break;
196                 case CMP_NODE_VIEWER:
197                         node = new ViewerNode(b_node);
198                         break;
199                 case CMP_NODE_SPLITVIEWER:
200                         node = new SplitViewerNode(b_node);
201                         break;
202                 case CMP_NODE_INVERT:
203                         node = new InvertNode(b_node);
204                         break;
205                 case NODE_GROUP:
206                         node = new GroupNode(b_node);
207                         break;
208                 case CMP_NODE_NORMAL:
209                         node = new NormalNode(b_node);
210                         break;
211                 case CMP_NODE_NORMALIZE:
212                         node = new NormalizeNode(b_node);
213                         break;
214                 case CMP_NODE_IMAGE:
215                         node = new ImageNode(b_node);
216                         break;
217                 case CMP_NODE_SETALPHA:
218                         node = new SetAlphaNode(b_node);
219                         break;
220                 case CMP_NODE_PREMULKEY:
221                         node = new ConvertAlphaNode(b_node);
222                         break;
223                 case CMP_NODE_MATH:
224                         node = new MathNode(b_node);
225                         break;
226                 case CMP_NODE_HUE_SAT:
227                         node = new HueSaturationValueNode(b_node);
228                         break;
229                 case CMP_NODE_COLORCORRECTION:
230                         node = new ColorCorrectionNode(b_node);
231                         break;
232                 case CMP_NODE_MASK_BOX:
233                         node = new BoxMaskNode(b_node);
234                         break;
235                 case CMP_NODE_MASK_ELLIPSE:
236                         node = new EllipseMaskNode(b_node);
237                         break;
238                 case CMP_NODE_GAMMA:
239                         node = new GammaNode(b_node);
240                         break;
241                 case CMP_NODE_CURVE_RGB:
242                         node = new ColorCurveNode(b_node);
243                         break;
244                 case CMP_NODE_CURVE_VEC:
245                         node = new VectorCurveNode(b_node);
246                         break;
247                 case CMP_NODE_HUECORRECT:
248                         node = new HueSaturationValueCorrectNode(b_node);
249                         break;
250                 case CMP_NODE_MAP_UV:
251                         node = new MapUVNode(b_node);
252                         break;
253                 case CMP_NODE_DISPLACE:
254                         node = new DisplaceNode(b_node);
255                         break;
256                 case CMP_NODE_VALTORGB:
257                         node = new ColorRampNode(b_node);
258                         break;
259                 case CMP_NODE_DIFF_MATTE:
260                         node = new DifferenceMatteNode(b_node);
261                         break;
262                 case CMP_NODE_LUMA_MATTE:
263                         node = new LuminanceMatteNode(b_node);
264                         break;
265                 case CMP_NODE_DIST_MATTE:
266                         node = new DistanceMatteNode(b_node);
267                         break;
268                 case CMP_NODE_CHROMA_MATTE:
269                         node = new ChromaMatteNode(b_node);
270                         break;
271                 case CMP_NODE_COLOR_MATTE:
272                         node = new ColorMatteNode(b_node);
273                         break;
274                 case CMP_NODE_CHANNEL_MATTE:
275                         node = new ChannelMatteNode(b_node);
276                         break;
277                 case CMP_NODE_BLUR:
278                         node = new BlurNode(b_node);
279                         break;
280                 case CMP_NODE_BOKEHIMAGE:
281                         node = new BokehImageNode(b_node);
282                         break;
283                 case CMP_NODE_BOKEHBLUR:
284                         node = new BokehBlurNode(b_node);
285                         break;
286                 case CMP_NODE_DILATEERODE:
287                         node = new DilateErodeNode(b_node);
288                         break;
289                 case CMP_NODE_LENSDIST:
290                         node = new LensDistortionNode(b_node);
291                         break;
292                 case CMP_NODE_RGB:
293                         node = new ColorNode(b_node);
294                         break;
295                 case CMP_NODE_VALUE:
296                         node = new ValueNode(b_node);
297                         break;
298                 case CMP_NODE_TIME:
299                         node = new TimeNode(b_node);
300                         break;
301                 case CMP_NODE_DBLUR:
302                         node = new DirectionalBlurNode(b_node);
303                         break;
304                 case CMP_NODE_ZCOMBINE:
305                         node = new ZCombineNode(b_node);
306                         break;
307                 case CMP_NODE_TONEMAP:
308                         node = new TonemapNode(b_node);
309                         break;
310                 case CMP_NODE_SWITCH:
311                         node = new SwitchNode(b_node);
312                         break;
313                 case CMP_NODE_GLARE:
314                         node = new GlareNode(b_node);
315                         break;
316                 case CMP_NODE_MOVIECLIP:
317                         node = new MovieClipNode(b_node);
318                         break;
319                 case CMP_NODE_COLOR_SPILL:
320                         node = new ColorSpillNode(b_node);
321                         break;
322                 case CMP_NODE_OUTPUT_FILE:
323                         node = new OutputFileNode(b_node);
324                         break;
325                 case CMP_NODE_MAP_VALUE:
326                         node = new MapValueNode(b_node);
327                         break;
328                 case CMP_NODE_TRANSFORM:
329                         node = new TransformNode(b_node);
330                         break;
331                 case CMP_NODE_STABILIZE2D:
332                         node = new Stabilize2dNode(b_node);
333                         break;
334                 case CMP_NODE_BILATERALBLUR:
335                         node = new BilateralBlurNode(b_node);
336                         break;
337                 case CMP_NODE_VECBLUR:
338                         node = new VectorBlurNode(b_node);
339                         break;
340                 case CMP_NODE_MOVIEDISTORTION:
341                         node = new MovieDistortionNode(b_node);
342                         break;
343                 case CMP_NODE_VIEW_LEVELS:
344                         node = new ViewLevelsNode(b_node);
345                         break;
346                 case CMP_NODE_DEFOCUS:
347                         node = new DefocusNode(b_node);
348                         break;
349                 case CMP_NODE_DOUBLEEDGEMASK:
350                         node = new DoubleEdgeMaskNode(b_node);
351                         break;
352                 case CMP_NODE_CROP:
353                         node = new CropNode(b_node);
354                         break;
355                 case CMP_NODE_MASK:
356                         node = new MaskNode(b_node);
357                         break;
358                 case CMP_NODE_KEYINGSCREEN:
359                         node = new KeyingScreenNode(b_node);
360                         break;
361                 case CMP_NODE_KEYING:
362                         node = new KeyingNode(b_node);
363                         break;
364                 /* not inplemented yet */
365                 default:
366                         node = new MuteNode(b_node);
367                         break;
368         }
369         return node;
370 }
371 void Converter::convertDataType(SocketConnection *connection, ExecutionSystem *system)
372 {
373         OutputSocket *outputSocket = connection->getFromSocket();
374         InputSocket *inputSocket = connection->getToSocket();
375         DataType fromDatatype = outputSocket->getDataType();
376         DataType toDatatype = inputSocket->getDataType();
377         NodeOperation *converter = NULL;
378         if (fromDatatype == COM_DT_VALUE && toDatatype == COM_DT_COLOR) {
379                 converter = new ConvertValueToColourProg();
380         }
381         else if (fromDatatype == COM_DT_VALUE && toDatatype == COM_DT_VECTOR) {
382                 converter = new ConvertValueToVectorOperation();
383         }
384         else if (fromDatatype == COM_DT_COLOR && toDatatype == COM_DT_VALUE) {
385                 converter = new ConvertColourToValueProg();
386         }
387         else if (fromDatatype == COM_DT_COLOR && toDatatype == COM_DT_VECTOR) {
388                 converter = new ConvertColorToVectorOperation();
389         }
390         else if (fromDatatype == COM_DT_VECTOR && toDatatype == COM_DT_VALUE) {
391                 converter = new ConvertVectorToValueOperation();
392         }
393         else if (fromDatatype == COM_DT_VECTOR && toDatatype == COM_DT_COLOR) {
394                 converter = new ConvertVectorToColorOperation();
395         }
396         if (converter != NULL) {
397                 inputSocket->relinkConnections(converter->getInputSocket(0));
398                 ExecutionSystemHelper::addLink(system->getConnections(), converter->getOutputSocket(), inputSocket);
399                 system->addOperation(converter);
400         }
401 }
402
403 void Converter::convertResolution(SocketConnection *connection, ExecutionSystem *system)
404 {
405         InputSocketResizeMode mode = connection->getToSocket()->getResizeMode();
406
407         NodeOperation *toOperation = (NodeOperation *)connection->getToNode();
408         const float toWidth = toOperation->getWidth();
409         const float toHeight = toOperation->getHeight();
410         NodeOperation *fromOperation = (NodeOperation *)connection->getFromNode();
411         const float fromWidth = fromOperation->getWidth();
412         const float fromHeight = fromOperation->getHeight();
413         bool doCenter = false;
414         bool doScale = false;
415         float addX = (toWidth - fromWidth) / 2.0f;
416         float addY = (toHeight - fromHeight) / 2.0f;
417         float scaleX = 0;
418         float scaleY = 0;
419
420         switch (mode) {
421                 case COM_SC_NO_RESIZE:
422                         break;
423                 case COM_SC_CENTER:
424                         doCenter = true;
425                         break;
426                 case COM_SC_FIT_WIDTH:
427                         doCenter = true;
428                         doScale = true;
429                         scaleX = scaleY = toWidth / fromWidth;
430                         break;
431                 case COM_SC_FIT_HEIGHT:
432                         doCenter = true;
433                         doScale = true;
434                         scaleX = scaleY = toHeight / fromHeight;
435                         break;
436                 case COM_SC_FIT:
437                         doCenter = true;
438                         doScale = true;
439                         scaleX = toWidth / fromWidth;
440                         scaleY = toHeight / fromHeight;
441                         if (scaleX < scaleY) {
442                                 scaleX = scaleY;
443                         }
444                         else {
445                                 scaleY = scaleX;
446                         }
447                         break;
448                 case COM_SC_STRETCH:
449                         doCenter = true;
450                         doScale = true;
451                         scaleX = toWidth / fromWidth;
452                         scaleY = toHeight / fromHeight;
453                         break;
454
455         }
456
457         if (doCenter) {
458                 NodeOperation *first = NULL;
459                 SocketConnection *c;
460                 ScaleOperation *scaleOperation = NULL;
461                 if (doScale) {
462                         scaleOperation = new ScaleOperation();
463                         first = scaleOperation;
464                         SetValueOperation *sxop = new SetValueOperation();
465                         sxop->setValue(scaleX);
466                         c = ExecutionSystemHelper::addLink(system->getConnections(), sxop->getOutputSocket(), scaleOperation->getInputSocket(1));
467                         c->setIgnoreResizeCheck(true);
468                         SetValueOperation *syop = new SetValueOperation();
469                         syop->setValue(scaleY);
470                         c = ExecutionSystemHelper::addLink(system->getConnections(), syop->getOutputSocket(), scaleOperation->getInputSocket(2));
471                         c->setIgnoreResizeCheck(true);
472                         system->addOperation(sxop);
473                         system->addOperation(syop);
474
475                         unsigned int resolution[2] = {fromWidth, fromHeight};
476                         scaleOperation->setResolution(resolution);
477                         sxop->setResolution(resolution);
478                         syop->setResolution(resolution);
479                         system->addOperation(scaleOperation);
480
481                         c->setIgnoreResizeCheck(true);
482                 }
483
484                 TranslateOperation *translateOperation = new TranslateOperation();
485                 if (!first) first = translateOperation;
486                 SetValueOperation *xop = new SetValueOperation();
487                 xop->setValue(addX);
488                 c = ExecutionSystemHelper::addLink(system->getConnections(), xop->getOutputSocket(), translateOperation->getInputSocket(1));
489                 c->setIgnoreResizeCheck(true);
490                 SetValueOperation *yop = new SetValueOperation();
491                 yop->setValue(addY);
492                 c = ExecutionSystemHelper::addLink(system->getConnections(), yop->getOutputSocket(), translateOperation->getInputSocket(2));
493                 c->setIgnoreResizeCheck(true);
494                 system->addOperation(xop);
495                 system->addOperation(yop);
496
497                 unsigned int resolution[2] = {toWidth, toHeight};
498                 translateOperation->setResolution(resolution);
499                 xop->setResolution(resolution);
500                 yop->setResolution(resolution);
501                 system->addOperation(translateOperation);
502
503                 if (doScale) {
504                         c = ExecutionSystemHelper::addLink(system->getConnections(), scaleOperation->getOutputSocket(), translateOperation->getInputSocket(0));
505                         c->setIgnoreResizeCheck(true);
506                 }
507
508                 InputSocket *inputSocket = connection->getToSocket();
509                 inputSocket->relinkConnections(first->getInputSocket(0));
510                 c = ExecutionSystemHelper::addLink(system->getConnections(), translateOperation->getOutputSocket(), inputSocket);
511                 c->setIgnoreResizeCheck(true);
512         }
513
514         connection->setIgnoreResizeCheck(true);
515 }