80989feaf2649e11de458634c7cce67f8cc06fa7
[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->custom1 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         if(*z1 <= *z2) {
51                 QUATCOPY(out, src1);
52         }
53         else {
54                 QUATCOPY(out, src2);
55                 
56                 if(node->custom1)
57                         *z1= *z2;
58         }
59 }
60
61 static void do_zcombine_mask(bNode *node, float *out, float *z1, float *z2)
62 {
63         if(*z1 > *z2) {
64                 *out= 1.0f;
65                 if(node->custom1)
66                         *z1= *z2;
67         }
68 }
69
70 static void do_zcombine_add(bNode *UNUSED(node), float *out, float *col1, float *col2, float *acol)
71 {
72         float alpha= *acol;
73         float malpha= 1.0f - alpha;
74         
75         out[0]= malpha*col1[0] + alpha*col2[0];
76         out[1]= malpha*col1[1] + alpha*col2[1];
77         out[2]= malpha*col1[2] + alpha*col2[2];
78         out[3]= malpha*col1[3] + alpha*col2[3];
79 }
80
81 static void node_composit_exec_zcombine(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
82 {
83         RenderData *rd= data;
84         CompBuf *cbuf= in[0]->data;
85         CompBuf *zbuf;
86
87         /* stack order in: col z col z */
88         /* stack order out: col z */
89         if(out[0]->hasoutput==0 && out[1]->hasoutput==0) 
90                 return;
91         
92         /* no input image; do nothing now */
93         if(in[0]->data==NULL) {
94                 return;
95         }
96         
97         if(out[1]->hasoutput) {
98                 /* copy or make a buffer for for the first z value, here we write result in */
99                 if(in[1]->data)
100                         zbuf= dupalloc_compbuf(in[1]->data);
101                 else {
102                         float *zval;
103                         int tot= cbuf->x*cbuf->y;
104                         
105                         zbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_VAL, 1);
106                         for(zval= zbuf->rect; tot; tot--, zval++)
107                                 *zval= in[1]->vec[0];
108                 }
109                 /* lazy coder hack */
110                 node->custom1= 1;
111                 out[1]->data= zbuf;
112         }
113         else {
114                 node->custom1= 0;
115                 zbuf= in[1]->data;
116         }
117         
118         if(rd->scemode & R_FULL_SAMPLE) {
119                 /* make output size of first input image */
120                 CompBuf *stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 1); // allocs
121                 
122                 composit4_pixel_processor(node, stackbuf, in[0]->data, in[0]->vec, zbuf, in[1]->vec, in[2]->data, in[2]->vec, 
123                                                                   in[3]->data, in[3]->vec, do_zcombine, CB_RGBA, CB_VAL, CB_RGBA, CB_VAL);
124                 
125                 out[0]->data= stackbuf;
126         }
127         else {
128                 /* make output size of first input image */
129                 CompBuf *stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 1); /* allocs */
130                 CompBuf *mbuf;
131                 float *fp;
132                 int x;
133                 char *aabuf;
134                 
135                 
136                 /* make a mask based on comparison, optionally write zvalue */
137                 mbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_VAL, 1);
138                 composit2_pixel_processor(node, mbuf, zbuf, in[1]->vec, in[3]->data, in[3]->vec, do_zcombine_mask, CB_VAL, CB_VAL);
139                 
140                 /* convert to char */
141                 aabuf= MEM_mallocN(cbuf->x*cbuf->y, "aa buf");
142                 fp= mbuf->rect;
143                 for(x= cbuf->x*cbuf->y-1; x>=0; x--)
144                         if(fp[x]==0.0f) aabuf[x]= 0;
145                         else aabuf[x]= 255;
146                 
147                 antialias_tagbuf(cbuf->x, cbuf->y, aabuf);
148                 
149                 /* convert to float */
150                 fp= mbuf->rect;
151                 for(x= cbuf->x*cbuf->y-1; x>=0; x--)
152                         if(aabuf[x]>1)
153                                 fp[x]= (1.0f/255.0f)*(float)aabuf[x];
154                 
155                 composit3_pixel_processor(node, stackbuf, in[0]->data, in[0]->vec, in[2]->data, in[2]->vec, mbuf, NULL, 
156                                                                   do_zcombine_add, CB_RGBA, CB_RGBA, CB_VAL);
157                 /* free */
158                 free_compbuf(mbuf);
159                 MEM_freeN(aabuf);
160                 
161                 out[0]->data= stackbuf;
162         }
163
164 }
165
166 bNodeType cmp_node_zcombine= {
167         /* *next,*prev */       NULL, NULL,
168         /* type code   */       CMP_NODE_ZCOMBINE,
169         /* name        */       "Z Combine",
170         /* width+range */       80, 40, 120,
171         /* class+opts  */       NODE_CLASS_OP_COLOR, NODE_OPTIONS,
172         /* input sock  */       cmp_node_zcombine_in,
173         /* output sock */       cmp_node_zcombine_out,
174         /* storage     */       "",
175         /* execfunc    */       node_composit_exec_zcombine,
176         /* butfunc     */       NULL,
177         /* initfunc    */       NULL,
178         /* freestoragefunc    */        NULL,
179         /* copystoragefunc    */        NULL,
180         /* id          */       NULL
181         
182 };