Add parser error handler.
[blender-staging.git] / source / blender / nodes / intern / CMP_nodes / CMP_zcombine.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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. 
10  *
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.
15  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2006 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/nodes/intern/CMP_nodes/CMP_zcombine.c
31  *  \ingroup cmpnodes
32  */
33
34
35 #include "../CMP_util.h"
36
37
38 /* **************** Z COMBINE ******************** */
39         /* lazy coder note: node->custom2 is abused to send signal */
40 static bNodeSocketType cmp_node_zcombine_in[]= {
41         {       SOCK_RGBA, 1, "Image",          0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
42         {       SOCK_VALUE, 1, "Z",                     0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 10000.0f},
43         {       SOCK_RGBA, 1, "Image",          0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
44         {       SOCK_VALUE, 1, "Z",                     0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 10000.0f},
45         {       -1, 0, ""       }
46 };
47 static bNodeSocketType cmp_node_zcombine_out[]= {
48         {       SOCK_RGBA, 0, "Image",          0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
49         {       SOCK_VALUE, 0, "Z",                     0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 10000.0f},
50         {       -1, 0, ""       }
51 };
52
53 static void do_zcombine(bNode *node, float *out, float *src1, float *z1, float *src2, float *z2)
54 {
55         float alpha;
56         float malpha;
57         
58         if(*z1 <= *z2) {
59                 if (node->custom1) {
60                         // use alpha in combine operation
61                         alpha= src1[3];
62                         malpha= 1.0f - alpha;
63                         out[0]= malpha*src2[0] + alpha*src1[0];
64                         out[1]= malpha*src2[1] + alpha*src1[1];
65                         out[2]= malpha*src2[2] + alpha*src1[2];
66                         out[3]= malpha*src2[3] + alpha*src1[3];
67                 }
68                 else {
69                         // do combination based solely on z value
70                         QUATCOPY(out, src1);
71                 }
72         }
73         else {
74                 if (node->custom1) {
75                         // use alpha in combine operation
76                         alpha= src2[3];
77                         malpha= 1.0f - alpha;
78                         out[0]= malpha*src1[0] + alpha*src2[0];
79                         out[1]= malpha*src1[1] + alpha*src2[1];
80                         out[2]= malpha*src1[2] + alpha*src2[2];
81                         out[3]= malpha*src1[3] + alpha*src2[3];
82                 }
83                 else {
84                         // do combination based solely on z value
85                         QUATCOPY(out, src1);
86                 }
87                 
88                 if(node->custom2)
89                         *z1= *z2;
90         }
91 }
92
93 static void do_zcombine_mask(bNode *node, float *out, float *z1, float *z2)
94 {
95         if(*z1 > *z2) {
96                 *out= 1.0f;
97                 if(node->custom2)
98                         *z1= *z2;
99         }
100 }
101
102 static void do_zcombine_add(bNode *node, float *out, float *col1, float *col2, float *acol)
103 {
104         float alpha;
105         float malpha;
106
107         if (node->custom1) {
108                 // use alpha in combine operation, antialiased mask in used here just as hint for the z value
109                 if (*acol>0.0f) {
110                         alpha= col2[3];
111                         malpha= 1.0f - alpha;
112                 
113                 
114                         out[0]= malpha*col1[0] + alpha*col2[0];
115                         out[1]= malpha*col1[1] + alpha*col2[1];
116                         out[2]= malpha*col1[2] + alpha*col2[2];
117                         out[3]= malpha*col1[3] + alpha*col2[3];
118                 }
119                 else {
120                         alpha= col1[3];
121                         malpha= 1.0f - alpha;
122                 
123                 
124                         out[0]= malpha*col2[0] + alpha*col1[0];
125                         out[1]= malpha*col2[1] + alpha*col1[1];
126                         out[2]= malpha*col2[2] + alpha*col1[2];
127                         out[3]= malpha*col2[3] + alpha*col1[3];
128                 }
129         }
130         else {
131                 // do combination based solely on z value but with antialiased mask
132                 alpha = *acol;
133                 malpha= 1.0f - alpha;
134                 
135                 out[0]= malpha*col1[0] + alpha*col2[0];
136                 out[1]= malpha*col1[1] + alpha*col2[1];
137                 out[2]= malpha*col1[2] + alpha*col2[2];
138                 out[3]= malpha*col1[3] + alpha*col2[3];
139         }
140 }
141
142 static void node_composit_exec_zcombine(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
143 {
144         RenderData *rd= data;
145         CompBuf *cbuf= in[0]->data;
146         CompBuf *zbuf;
147
148         /* stack order in: col z col z */
149         /* stack order out: col z */
150         if(out[0]->hasoutput==0 && out[1]->hasoutput==0) 
151                 return;
152         
153         /* no input image; do nothing now */
154         if(in[0]->data==NULL) {
155                 return;
156         }
157         
158         if(out[1]->hasoutput) {
159                 /* copy or make a buffer for for the first z value, here we write result in */
160                 if(in[1]->data)
161                         zbuf= dupalloc_compbuf(in[1]->data);
162                 else {
163                         float *zval;
164                         int tot= cbuf->x*cbuf->y;
165                         
166                         zbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_VAL, 1);
167                         for(zval= zbuf->rect; tot; tot--, zval++)
168                                 *zval= in[1]->vec[0];
169                 }
170                 /* lazy coder hack */
171                 node->custom2= 1;
172                 out[1]->data= zbuf;
173         }
174         else {
175                 node->custom2= 0;
176                 zbuf= in[1]->data;
177         }
178         
179         if(rd->scemode & R_FULL_SAMPLE) {
180                 /* make output size of first input image */
181                 CompBuf *stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 1); // allocs
182                 
183                 composit4_pixel_processor(node, stackbuf, in[0]->data, in[0]->vec, zbuf, in[1]->vec, in[2]->data, in[2]->vec, 
184                                                                   in[3]->data, in[3]->vec, do_zcombine, CB_RGBA, CB_VAL, CB_RGBA, CB_VAL);
185                 
186                 out[0]->data= stackbuf;
187         }
188         else {
189                 /* make output size of first input image */
190                 CompBuf *stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 1); /* allocs */
191                 CompBuf *mbuf;
192                 float *fp;
193                 int x;
194                 char *aabuf;
195                 
196                 
197                 /* make a mask based on comparison, optionally write zvalue */
198                 mbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_VAL, 1);
199                 composit2_pixel_processor(node, mbuf, zbuf, in[1]->vec, in[3]->data, in[3]->vec, do_zcombine_mask, CB_VAL, CB_VAL);
200                 
201                 /* convert to char */
202                 aabuf= MEM_mallocN(cbuf->x*cbuf->y, "aa buf");
203                 fp= mbuf->rect;
204                 for(x= cbuf->x*cbuf->y-1; x>=0; x--)
205                         if(fp[x]==0.0f) aabuf[x]= 0;
206                         else aabuf[x]= 255;
207                 
208                 antialias_tagbuf(cbuf->x, cbuf->y, aabuf);
209                 
210                 /* convert to float */
211                 fp= mbuf->rect;
212                 for(x= cbuf->x*cbuf->y-1; x>=0; x--)
213                         if(aabuf[x]>1)
214                                 fp[x]= (1.0f/255.0f)*(float)aabuf[x];
215                 
216                 composit3_pixel_processor(node, stackbuf, in[0]->data, in[0]->vec, in[2]->data, in[2]->vec, mbuf, NULL, 
217                                                                   do_zcombine_add, CB_RGBA, CB_RGBA, CB_VAL);
218                 /* free */
219                 free_compbuf(mbuf);
220                 MEM_freeN(aabuf);
221                 
222                 out[0]->data= stackbuf;
223         }
224
225 }
226
227 void register_node_type_cmp_zcombine(ListBase *lb)
228 {
229         static bNodeType ntype;
230
231         node_type_base(&ntype, CMP_NODE_ZCOMBINE, "Z Combine", NODE_CLASS_OP_COLOR, NODE_OPTIONS,
232                 cmp_node_zcombine_in, cmp_node_zcombine_out);
233         node_type_size(&ntype, 80, 40, 120);
234         node_type_exec(&ntype, node_composit_exec_zcombine);
235
236         nodeRegisterType(lb, &ntype);
237 }
238