converted more mixed tab/space indentations to tabs. only whitespace changes.
[blender.git] / source / blender / nodes / intern / CMP_nodes / CMP_distanceMatte.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_distanceMatte.c
31  *  \ingroup cmpnodes
32  */
33
34
35 #include "../CMP_util.h"
36
37 /* ******************* channel Distance Matte ********************************* */
38 static bNodeSocketType cmp_node_distance_matte_in[]={
39         {SOCK_RGBA,1,"Image", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
40         {SOCK_RGBA,1,"Key Color", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
41         {-1,0,""}
42 };
43
44 static bNodeSocketType cmp_node_distance_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 /* note, keyvals is passed on from caller as stack array */
51 /* might have been nicer as temp struct though... */
52 static void do_distance_matte(bNode *node, float *out, float *in)
53 {
54         NodeChroma *c= (NodeChroma *)node->storage;
55         float tolerence=c->t1;
56         float falloff=c->t2;
57         float distance;
58         float alpha;
59
60         distance=sqrt((c->key[0]-in[0])*(c->key[0]-in[0]) +
61                                   (c->key[1]-in[1])*(c->key[1]-in[1]) +
62                                   (c->key[2]-in[2])*(c->key[2]-in[2]));
63
64         VECCOPY(out, in);
65
66         /*make 100% transparent */
67         if(distance < tolerence) {
68                 out[3]=0.0;
69         }
70         /*in the falloff region, make partially transparent */
71         else if(distance < falloff+tolerence){
72                 distance=distance-tolerence;
73                 alpha=distance/falloff;
74                 /*only change if more transparent than before */
75                 if(alpha < in[3]) {
76                         out[3]=alpha;
77                 }
78                 else { /* leave as before */
79                         out[3]=in[3];
80                 }
81         }
82         else {
83                 out[3]=in[3];
84         }
85 }
86
87 static void node_composit_exec_distance_matte(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
88 {
89         /*
90         Loosely based on the Sequencer chroma key plug-in, but enhanced to work in other color spaces and
91         uses a different difference function (suggested in forums of vfxtalk.com).
92         */
93         CompBuf *workbuf;
94         CompBuf *inbuf;
95         NodeChroma *c;
96         
97         /*is anything connected?*/
98         if(out[0]->hasoutput==0 && out[1]->hasoutput==0) return;
99         /*must have an image imput*/
100         if(in[0]->data==NULL) return;
101         
102         inbuf=typecheck_compbuf(in[0]->data, CB_RGBA);
103         
104         c=node->storage;
105         workbuf=dupalloc_compbuf(inbuf);
106         
107         /*use the input color*/
108         c->key[0]= in[1]->vec[0];
109         c->key[1]= in[1]->vec[1];
110         c->key[2]= in[1]->vec[2];
111         
112         /* note, processor gets a keyvals array passed on as buffer constant */
113         composit1_pixel_processor(node, workbuf, workbuf, in[0]->vec, do_distance_matte, CB_RGBA);
114         
115         
116         out[0]->data=workbuf;
117         if(out[1]->hasoutput)
118                 out[1]->data=valbuf_from_rgbabuf(workbuf, CHAN_A);
119         generate_preview(data, node, workbuf);
120
121         if(inbuf!=in[0]->data)
122                 free_compbuf(inbuf);
123 }
124
125 static void node_composit_init_distance_matte(bNode *node)
126 {
127         NodeChroma *c= MEM_callocN(sizeof(NodeChroma), "node chroma");
128         node->storage= c;
129         c->t1= 0.1f;
130         c->t2= 0.1f;
131 }
132
133 void register_node_type_cmp_distance_matte(ListBase *lb)
134 {
135         static bNodeType ntype;
136
137         node_type_base(&ntype, CMP_NODE_DIST_MATTE, "Distance Key", NODE_CLASS_MATTE, NODE_PREVIEW|NODE_OPTIONS,
138                 cmp_node_distance_matte_in, cmp_node_distance_matte_out);
139         node_type_size(&ntype, 200, 80, 250);
140         node_type_init(&ntype, node_composit_init_distance_matte);
141         node_type_storage(&ntype, "NodeChroma", node_free_standard_storage, node_copy_standard_storage);
142         node_type_exec(&ntype, node_composit_exec_distance_matte);
143
144         nodeRegisterType(lb, &ntype);
145 }
146
147
148