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