converted more mixed tab/space indentations to tabs. only whitespace changes.
[blender.git] / source / blender / nodes / intern / CMP_nodes / CMP_diffMatte.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): Bob Holcomb
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/nodes/intern/CMP_nodes/CMP_diffMatte.c
31  *  \ingroup cmpnodes
32  */
33
34
35 #include "../CMP_util.h"
36
37 /* ******************* channel Difference Matte ********************************* */
38 static bNodeSocketType cmp_node_diff_matte_in[]={
39         {SOCK_RGBA,1,"Image 1", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
40         {SOCK_RGBA,1,"Image 2", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
41         {-1,0,""}
42 };
43
44 static bNodeSocketType cmp_node_diff_matte_out[]={
45         {SOCK_RGBA,0,"Image", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
46         {SOCK_VALUE,0,"Matte",0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
47         {-1,0,""}
48 };
49
50 static void do_diff_matte(bNode *node, float *outColor, float *inColor1, float *inColor2)
51 {
52         NodeChroma *c= (NodeChroma *)node->storage;
53         float tolerence=c->t1;
54         float falloff=c->t2;
55         float difference;
56         float alpha;
57
58         difference= fabs(inColor2[0]-inColor1[0])+
59                            fabs(inColor2[1]-inColor1[1])+
60                            fabs(inColor2[2]-inColor1[2]);
61
62         /*average together the distances*/
63         difference=difference/3.0;
64
65         VECCOPY(outColor, inColor1);
66
67         /*make 100% transparent*/
68         if(difference < tolerence) {
69                 outColor[3]=0.0;
70         }
71         /*in the falloff region, make partially transparent */
72         else if(difference < falloff+tolerence) {
73                 difference=difference-tolerence;
74                 alpha=difference/falloff;
75                 /*only change if more transparent than before */
76                 if(alpha < inColor1[3]) {
77                         outColor[3]=alpha;
78                 }
79                 else { /* leave as before */
80                         outColor[3]=inColor1[3];
81                 }
82         }
83         else {
84                 /*foreground object*/
85                 outColor[3]= inColor1[3];
86         }
87 }
88
89 static void node_composit_exec_diff_matte(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
90 {
91         CompBuf *outbuf=0;
92         CompBuf *imbuf1=0;
93         CompBuf *imbuf2=0;
94         NodeChroma *c;
95
96         /*is anything connected?*/
97         if(out[0]->hasoutput==0 && out[1]->hasoutput==0) return;
98
99         /*must have an image imput*/
100         if(in[0]->data==NULL) return;
101
102
103         imbuf1=typecheck_compbuf(in[0]->data, CB_RGBA);
104
105         /* if there's an image, use that, if not use the color */
106         if(in[1]->data) {
107                 imbuf2=typecheck_compbuf(in[1]->data, CB_RGBA);
108         }
109
110         c=node->storage;
111         outbuf=dupalloc_compbuf(imbuf1);
112
113         /* note, processor gets a keyvals array passed on as buffer constant */
114         composit2_pixel_processor(node, outbuf, imbuf1, in[0]->vec, imbuf2, in[1]->vec, do_diff_matte, CB_RGBA, CB_RGBA);
115
116         out[0]->data=outbuf;
117         if(out[1]->hasoutput)
118                 out[1]->data=valbuf_from_rgbabuf(outbuf, CHAN_A);
119         generate_preview(data, node, outbuf);
120
121         if(imbuf1!=in[0]->data)
122                 free_compbuf(imbuf1);
123
124         if(imbuf2!=in[1]->data)
125                 free_compbuf(imbuf2);
126 }
127
128 static void node_composit_init_diff_matte(bNode *node)
129 {
130         NodeChroma *c= MEM_callocN(sizeof(NodeChroma), "node chroma");
131         node->storage= c;
132         c->t1= 0.1f;
133         c->t2= 0.1f;
134 }
135
136 void register_node_type_cmp_diff_matte(ListBase *lb)
137 {
138         static bNodeType ntype;
139
140         node_type_base(&ntype, CMP_NODE_DIFF_MATTE, "Difference Key", NODE_CLASS_MATTE, NODE_PREVIEW|NODE_OPTIONS,
141                 cmp_node_diff_matte_in, cmp_node_diff_matte_out);
142         node_type_size(&ntype, 200, 80, 250);
143         node_type_init(&ntype, node_composit_init_diff_matte);
144         node_type_storage(&ntype, "NodeChroma", node_free_standard_storage, node_copy_standard_storage);
145         node_type_exec(&ntype, node_composit_exec_diff_matte);
146
147         nodeRegisterType(lb, &ntype);
148 }
149
150
151