whitespace only, no functional change mixed tabs/spaces --> tabs.
[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
130 static void node_composit_exec_channel_matte(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
131 {
132         CompBuf *cbuf;
133         CompBuf *outbuf;
134         
135         if(in[0]->hasinput==0)  return;
136         if(in[0]->data==NULL) return;
137         if(out[0]->hasoutput==0 && out[1]->hasoutput==0) return;
138         
139         cbuf=typecheck_compbuf(in[0]->data, CB_RGBA);
140         
141         outbuf=dupalloc_compbuf(cbuf);
142         
143         /*convert to colorspace*/
144         switch(node->custom1) {
145            case CMP_NODE_CHANNEL_MATTE_CS_RGB:
146                    break;
147            case CMP_NODE_CHANNEL_MATTE_CS_HSV: /*HSV*/
148                    composit1_pixel_processor(node, outbuf, cbuf, in[1]->vec, do_rgba_to_hsva, CB_RGBA);
149                    break;
150            case CMP_NODE_CHANNEL_MATTE_CS_YUV: /*YUV*/
151                    composit1_pixel_processor(node, outbuf, cbuf, in[1]->vec, do_rgba_to_yuva, CB_RGBA);
152                    break;
153            case CMP_NODE_CHANNEL_MATTE_CS_YCC: /*YCC*/
154                    composit1_pixel_processor(node, outbuf, cbuf, in[1]->vec, do_normalized_rgba_to_ycca2, CB_RGBA);
155                    break;
156            default:
157                    break;
158         }
159
160         /*use the selected channel information to do the key */
161         composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_channel_matte, CB_RGBA);
162    
163         /*convert back to RGB colorspace in place*/
164         switch(node->custom1) {
165         case CMP_NODE_CHANNEL_MATTE_CS_RGB: /*RGB*/
166                 break;
167         case CMP_NODE_CHANNEL_MATTE_CS_HSV: /*HSV*/
168                 composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_hsva_to_rgba, CB_RGBA);
169                 break;
170         case CMP_NODE_CHANNEL_MATTE_CS_YUV: /*YUV*/
171                 composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_yuva_to_rgba, CB_RGBA);
172                 break;
173         case CMP_NODE_CHANNEL_MATTE_CS_YCC: /*YCC*/
174                 composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_normalized_ycca_to_rgba2, CB_RGBA);
175                 break;
176         default:
177                 break;
178         }
179
180         generate_preview(data, node, outbuf);
181         out[0]->data=outbuf;
182         if(out[1]->hasoutput)
183                 out[1]->data=valbuf_from_rgbabuf(outbuf, CHAN_A);
184         
185         if(cbuf!=in[0]->data)
186                 free_compbuf(cbuf);
187
188 }
189
190 static void node_composit_init_channel_matte(bNode *node)
191 {
192    NodeChroma *c= MEM_callocN(sizeof(NodeChroma), "node chroma");
193    node->storage=c;
194    c->t1= 1.0f;
195    c->t2= 0.0f;
196    c->t3= 0.0f;
197    c->fsize= 0.0f;
198    c->fstrength= 0.0f;
199    c->algorithm=1; /*max channel limiting */
200    c->channel=1; /* limit by red */
201    node->custom1= 1; /* RGB channel */
202    node->custom2= 2; /* Green Channel */
203 }
204
205 void register_node_type_cmp_channel_matte(ListBase *lb)
206 {
207         static bNodeType ntype;
208
209         node_type_base(&ntype, CMP_NODE_CHANNEL_MATTE, "Channel Key", NODE_CLASS_MATTE, NODE_PREVIEW|NODE_OPTIONS,
210                 cmp_node_channel_matte_in, cmp_node_channel_matte_out);
211         node_type_size(&ntype, 200, 80, 250);
212         node_type_init(&ntype, node_composit_init_channel_matte);
213         node_type_storage(&ntype, "NodeChroma", node_free_standard_storage, node_copy_standard_storage);
214         node_type_exec(&ntype, node_composit_exec_channel_matte);
215
216         nodeRegisterType(lb, &ntype);
217 }
218