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