converted more mixed tab/space indentations to tabs. only whitespace changes.
[blender.git] / source / blender / nodes / intern / CMP_nodes / CMP_channelMatte.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_channelMatte.c
31  *  \ingroup cmpnodes
32  */
33
34
35 #include "../CMP_util.h"
36
37
38 /* ******************* Channel Matte Node ********************************* */
39 static bNodeSocketType cmp_node_channel_matte_in[]={
40         {SOCK_RGBA,1,"Image", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
41         {-1,0,""}
42 };
43
44 static bNodeSocketType cmp_node_channel_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_normalized_rgba_to_ycca2(bNode *UNUSED(node), float *out, float *in)
51 {
52         /*normalize to the range 0.0 to 1.0) */
53         rgb_to_ycc(in[0],in[1],in[2], &out[0], &out[1], &out[2], BLI_YCC_ITU_BT601);
54         out[0]=(out[0])/255.0;
55         out[1]=(out[1])/255.0;
56         out[2]=(out[2])/255.0;
57         out[3]=in[3];
58 }
59
60 static void do_normalized_ycca_to_rgba2(bNode *UNUSED(node), float *out, float *in)
61 {
62         /*un-normalize the normalize from above */
63         in[0]=in[0]*255.0;
64         in[1]=in[1]*255.0;
65         in[2]=in[2]*255.0;
66         ycc_to_rgb(in[0],in[1],in[2], &out[0], &out[1], &out[2], BLI_YCC_ITU_BT601);
67         out[3]=in[3];
68 }
69
70
71 static void do_channel_matte(bNode *node, float *out, float *in)
72 {
73         NodeChroma *c=(NodeChroma *)node->storage;
74         float alpha=0.0;        
75
76         switch(c->algorithm) {
77         case 0: { /* Alpha=key_channel-limit channel */
78                 int key_channel=node->custom2-1;
79                 int limit_channel=c->channel-1;
80                 alpha=in[key_channel]-in[limit_channel];
81                 break;
82         }
83         case 1: { /* Alpha=G-MAX(R, B) */
84                 switch(node->custom2) {
85                         case 1: {
86                                 alpha=in[0]-MAX2(in[1],in[2]);
87                                 break;
88                         }
89                         case 2: {
90                                 alpha=in[1]-MAX2(in[0],in[2]);
91                                 break;
92                         }
93                         case 3: {
94                                 alpha=in[2]-MAX2(in[0],in[1]);
95                                 break;
96                         }
97                         default:
98                                 break;
99                 }
100         }
101         default:
102                 break;
103         }
104
105         /*flip because 0.0 is transparent, not 1.0*/
106         alpha=1-alpha;
107         
108         /* test range*/
109         if(alpha>c->t1) {
110                 alpha=in[3]; /*whatever it was prior */
111         }
112         else if(alpha<c->t2){
113                 alpha=0.0;
114         }
115         else {/*blend */
116                 alpha=(alpha-c->t2)/(c->t1-c->t2);
117         }
118
119         
120         /* don't make something that was more transparent less transparent */
121         if (alpha<in[3]) {
122                 out[3]=alpha;
123         }
124         else {
125                 out[3]=in[3];
126         }
127 }
128
129 static void node_composit_exec_channel_matte(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
130 {
131         CompBuf *cbuf;
132         CompBuf *outbuf;
133         
134         if(in[0]->hasinput==0) return;
135         if(in[0]->data==NULL) return;
136         if(out[0]->hasoutput==0 && out[1]->hasoutput==0) return;
137         
138         cbuf=typecheck_compbuf(in[0]->data, CB_RGBA);
139         
140         outbuf=dupalloc_compbuf(cbuf);
141         
142         /*convert to colorspace*/
143         switch(node->custom1) {
144         case CMP_NODE_CHANNEL_MATTE_CS_RGB:
145                 break;
146         case CMP_NODE_CHANNEL_MATTE_CS_HSV: /*HSV*/
147                 composit1_pixel_processor(node, outbuf, cbuf, in[1]->vec, do_rgba_to_hsva, CB_RGBA);
148                 break;
149         case CMP_NODE_CHANNEL_MATTE_CS_YUV: /*YUV*/
150                 composit1_pixel_processor(node, outbuf, cbuf, in[1]->vec, do_rgba_to_yuva, CB_RGBA);
151                 break;
152         case CMP_NODE_CHANNEL_MATTE_CS_YCC: /*YCC*/
153                 composit1_pixel_processor(node, outbuf, cbuf, in[1]->vec, do_normalized_rgba_to_ycca2, CB_RGBA);
154                 break;
155         default:
156                 break;
157         }
158
159         /*use the selected channel information to do the key */
160         composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_channel_matte, CB_RGBA);
161
162         /*convert back to RGB colorspace in place*/
163         switch(node->custom1) {
164         case CMP_NODE_CHANNEL_MATTE_CS_RGB: /*RGB*/
165                 break;
166         case CMP_NODE_CHANNEL_MATTE_CS_HSV: /*HSV*/
167                 composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_hsva_to_rgba, CB_RGBA);
168                 break;
169         case CMP_NODE_CHANNEL_MATTE_CS_YUV: /*YUV*/
170                 composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_yuva_to_rgba, CB_RGBA);
171                 break;
172         case CMP_NODE_CHANNEL_MATTE_CS_YCC: /*YCC*/
173                 composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_normalized_ycca_to_rgba2, CB_RGBA);
174                 break;
175         default:
176                 break;
177         }
178
179         generate_preview(data, node, outbuf);
180         out[0]->data=outbuf;
181         if(out[1]->hasoutput)
182                 out[1]->data=valbuf_from_rgbabuf(outbuf, CHAN_A);
183         
184         if(cbuf!=in[0]->data)
185                 free_compbuf(cbuf);
186
187 }
188
189 static void node_composit_init_channel_matte(bNode *node)
190 {
191         NodeChroma *c= MEM_callocN(sizeof(NodeChroma), "node chroma");
192         node->storage=c;
193         c->t1= 1.0f;
194         c->t2= 0.0f;
195         c->t3= 0.0f;
196         c->fsize= 0.0f;
197         c->fstrength= 0.0f;
198         c->algorithm=1; /*max channel limiting */
199         c->channel=1; /* limit by red */
200         node->custom1= 1; /* RGB channel */
201         node->custom2= 2; /* Green Channel */
202 }
203
204 void register_node_type_cmp_channel_matte(ListBase *lb)
205 {
206         static bNodeType ntype;
207
208         node_type_base(&ntype, CMP_NODE_CHANNEL_MATTE, "Channel Key", NODE_CLASS_MATTE, NODE_PREVIEW|NODE_OPTIONS,
209                 cmp_node_channel_matte_in, cmp_node_channel_matte_out);
210         node_type_size(&ntype, 200, 80, 250);
211         node_type_init(&ntype, node_composit_init_channel_matte);
212         node_type_storage(&ntype, "NodeChroma", node_free_standard_storage, node_copy_standard_storage);
213         node_type_exec(&ntype, node_composit_exec_channel_matte);
214
215         nodeRegisterType(lb, &ntype);
216 }