Fix for bug #8582: compositor math node did not output any result
[plumiferos.git] / source / blender / nodes / intern / CMP_nodes / CMP_math.c
blob543d34c53becf01a4a02f74692f36a4650faa773
1 /**
2 * $Id: CMP_math.c 10456 2007-04-04 13:58:12Z jesterking $
4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2006 Blender Foundation.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
30 #include "../CMP_util.h"
33 /* **************** SCALAR MATH ******************** */
34 static bNodeSocketType cmp_node_math_in[]= {
35 { SOCK_VALUE, 1, "Value", 0.5f, 0.5f, 0.5f, 1.0f, -10000.0f, 10000.0f},
36 { SOCK_VALUE, 1, "Value", 0.5f, 0.5f, 0.5f, 1.0f, -10000.0f, 10000.0f},
37 { -1, 0, "" }
40 static bNodeSocketType cmp_node_math_out[]= {
41 { SOCK_VALUE, 0, "Value", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
42 { -1, 0, "" }
45 static void do_math(bNode *node, float *out, float *in, float *in2)
47 switch(node->custom1)
49 case 0: /* Add */
50 out[0]= in[0] + in2[0];
51 break;
52 case 1: /* Subtract */
53 out[0]= in[0] - in2[0];
54 break;
55 case 2: /* Multiply */
56 out[0]= in[0] * in2[0];
57 break;
58 case 3: /* Divide */
60 if(in[1]==0) /* We don't want to divide by zero. */
61 out[0]= 0.0;
62 else
63 out[0]= in[0] / in2[0];
65 break;
66 case 4: /* Sine */
67 out[0]= sin(in[0]);
68 break;
69 case 5: /* Cosine */
70 out[0]= cos(in[0]);
71 break;
72 case 6: /* Tangent */
73 out[0]= tan(in[0]);
74 break;
75 case 7: /* Arc-Sine */
77 /* Can't do the impossible... */
78 if(in[0] <= 1 && in[0] >= -1 )
79 out[0]= asin(in[0]);
80 else
81 out[0]= 0.0;
83 break;
84 case 8: /* Arc-Cosine */
86 /* Can't do the impossible... */
87 if( in[0] <= 1 && in[0] >= -1 )
88 out[0]= acos(in[0]);
89 else
90 out[0]= 0.0;
92 break;
93 case 9: /* Arc-Tangent */
94 out[0]= atan(in[0]);
95 break;
96 case 10: /* Power */
98 /* Don't want any imaginary numbers... */
99 if( in[0] >= 0 )
100 out[0]= pow(in[0], in2[0]);
101 else
102 out[0]= 0.0;
104 break;
105 case 11: /* Logarithm */
107 /* Don't want any imaginary numbers... */
108 if( in[0] > 0 && in2[0] > 0 )
109 out[0]= log(in[0]) / log(in2[0]);
110 else
111 out[0]= 0.0;
113 break;
114 case 12: /* Minimum */
116 if( in[0] < in2[0] )
117 out[0]= in[0];
118 else
119 out[0]= in2[0];
121 break;
122 case 13: /* Maximum */
124 if( in[0] > in2[0] )
125 out[0]= in[0];
126 else
127 out[0]= in2[0];
129 break;
130 case 14: /* Round */
132 out[0]= (int)(in[0] + 0.5f);
134 break;
138 static void node_composit_exec_math(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
140 CompBuf *cbuf=in[0]->data;
141 CompBuf *cbuf2=in[1]->data;
142 CompBuf *stackbuf;
144 /* check for inputs and outputs for early out*/
145 if(out[0]->hasoutput==0) return;
147 /* no image-color operation */
148 if(in[0]->data==NULL && in[1]->data==NULL) {
149 do_math(node, out[0]->vec, in[0]->vec, in[1]->vec);
150 return;
153 /*create output based on first input */
154 if(cbuf) {
155 stackbuf=alloc_compbuf(cbuf->x, cbuf->y, CB_VAL, 1);
157 /* and if it doesn't exist use the second input since we
158 know that one of them must exist at this point*/
159 else {
160 stackbuf=alloc_compbuf(cbuf2->x, cbuf2->y, CB_VAL, 1);
163 /* operate in case there's valid size */
164 composit2_pixel_processor(node, stackbuf, in[0]->data, in[0]->vec, in[1]->data, in[1]->vec, do_math, CB_VAL, CB_VAL);
165 out[0]->data= stackbuf;
168 bNodeType cmp_node_math= {
169 /* *next,*prev */ NULL, NULL,
170 /* type code */ CMP_NODE_MATH,
171 /* name */ "Math",
172 /* width+range */ 120, 110, 160,
173 /* class+opts */ NODE_CLASS_CONVERTOR, NODE_OPTIONS,
174 /* input sock */ cmp_node_math_in,
175 /* output sock */ cmp_node_math_out,
176 /* storage */ "",
177 /* execfunc */ node_composit_exec_math,
178 /* butfunc */ NULL,
179 /* initfunc */ NULL,
180 /* freestoragefunc */ NULL,
181 /* copystoragefunc */ NULL,
182 /* id */ NULL