Merging r50470 through r50477 from trunk into soc-2011-tomato
[blender.git] / source / blender / nodes / composite / nodes / node_composite_channelMatte.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2006 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Bob Holcomb
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/nodes/composite/nodes/node_composite_channelMatte.c
29  *  \ingroup cmpnodes
30  */
31
32
33 #include "node_composite_util.h"
34
35
36 /* ******************* Channel Matte Node ********************************* */
37 static bNodeSocketTemplate cmp_node_channel_matte_in[]={
38         {SOCK_RGBA, 1, N_("Image"), 1.0f, 1.0f, 1.0f, 1.0f},
39         {-1, 0, ""}
40 };
41
42 static bNodeSocketTemplate cmp_node_channel_matte_out[]={
43         {SOCK_RGBA, 0, N_("Image")},
44         {SOCK_FLOAT, 0, N_("Matte")},
45         {-1, 0, ""}
46 };
47
48 #ifdef WITH_COMPOSITOR_LEGACY
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.0f;
55         out[1]=(out[1])/255.0f;
56         out[2]=(out[2])/255.0f;
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.0f;
64         in[1]=in[1]*255.0f;
65         in[2]=in[2]*255.0f;
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                         {
87                                 alpha=in[0]-MAX2(in[1], in[2]);
88                                 break;
89                         }
90                         case 2:
91                         {
92                                 alpha=in[1]-MAX2(in[0], in[2]);
93                                 break;
94                         }
95                         case 3:
96                         {
97                                 alpha=in[2]-MAX2(in[0], in[1]);
98                                 break;
99                         }
100                         default:
101                                 break;
102                 }
103                 break;
104         }
105         default:
106                 break;
107         }
108
109         /*flip because 0.0 is transparent, not 1.0*/
110         alpha=1-alpha;
111         
112         /* test range*/
113         if (alpha>c->t1) {
114                 alpha=in[3]; /*whatever it was prior */
115         }
116         else if (alpha<c->t2) {
117                 alpha=0.0;
118         }
119         else {/*blend */
120                 alpha=(alpha-c->t2)/(c->t1-c->t2);
121         }
122
123         
124         /* don't make something that was more transparent less transparent */
125         if (alpha<in[3]) {
126                 out[3]=alpha;
127         }
128         else {
129                 out[3]=in[3];
130         }
131 }
132
133 static void node_composit_exec_channel_matte(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
134 {
135         CompBuf *cbuf;
136         CompBuf *outbuf;
137         
138         if (in[0]->hasinput==0) return;
139         if (in[0]->data==NULL) return;
140         if (out[0]->hasoutput==0 && out[1]->hasoutput==0) return;
141         
142         cbuf=typecheck_compbuf(in[0]->data, CB_RGBA);
143         
144         outbuf=dupalloc_compbuf(cbuf);
145         
146         /*convert to colorspace*/
147         switch (node->custom1) {
148         case CMP_NODE_CHANNEL_MATTE_CS_RGB:
149                 break;
150         case CMP_NODE_CHANNEL_MATTE_CS_HSV: /*HSV*/
151                 composit1_pixel_processor(node, outbuf, cbuf, in[1]->vec, do_rgba_to_hsva, CB_RGBA);
152                 break;
153         case CMP_NODE_CHANNEL_MATTE_CS_YUV: /*YUV*/
154                 composit1_pixel_processor(node, outbuf, cbuf, in[1]->vec, do_rgba_to_yuva, CB_RGBA);
155                 break;
156         case CMP_NODE_CHANNEL_MATTE_CS_YCC: /*YCC*/
157                 composit1_pixel_processor(node, outbuf, cbuf, in[1]->vec, do_normalized_rgba_to_ycca2, CB_RGBA);
158                 break;
159         default:
160                 break;
161         }
162
163         /*use the selected channel information to do the key */
164         composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_channel_matte, CB_RGBA);
165
166         /*convert back to RGB colorspace in place*/
167         switch (node->custom1) {
168         case CMP_NODE_CHANNEL_MATTE_CS_RGB: /*RGB*/
169                 break;
170         case CMP_NODE_CHANNEL_MATTE_CS_HSV: /*HSV*/
171                 composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_hsva_to_rgba, CB_RGBA);
172                 break;
173         case CMP_NODE_CHANNEL_MATTE_CS_YUV: /*YUV*/
174                 composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_yuva_to_rgba, CB_RGBA);
175                 break;
176         case CMP_NODE_CHANNEL_MATTE_CS_YCC: /*YCC*/
177                 composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_normalized_ycca_to_rgba2, CB_RGBA);
178                 break;
179         default:
180                 break;
181         }
182
183         generate_preview(data, node, outbuf);
184         out[0]->data=outbuf;
185         if (out[1]->hasoutput)
186                 out[1]->data=valbuf_from_rgbabuf(outbuf, CHAN_A);
187
188         if (cbuf!=in[0]->data)
189                 free_compbuf(cbuf);
190
191 }
192
193 #endif  /* WITH_COMPOSITOR_LEGACY */
194
195 static void node_composit_init_channel_matte(bNodeTree *UNUSED(ntree), bNode *node, bNodeTemplate *UNUSED(ntemp))
196 {
197         NodeChroma *c= MEM_callocN(sizeof(NodeChroma), "node chroma");
198         node->storage=c;
199         c->t1= 1.0f;
200         c->t2= 0.0f;
201         c->t3= 0.0f;
202         c->fsize= 0.0f;
203         c->fstrength= 0.0f;
204         c->algorithm=1; /*max channel limiting */
205         c->channel=1; /* limit by red */
206         node->custom1= 1; /* RGB channel */
207         node->custom2= 2; /* Green Channel */
208 }
209
210 void register_node_type_cmp_channel_matte(bNodeTreeType *ttype)
211 {
212         static bNodeType ntype;
213
214         node_type_base(ttype, &ntype, CMP_NODE_CHANNEL_MATTE, "Channel Key", NODE_CLASS_MATTE, NODE_PREVIEW|NODE_OPTIONS);
215         node_type_socket_templates(&ntype, cmp_node_channel_matte_in, cmp_node_channel_matte_out);
216         node_type_size(&ntype, 200, 80, 250);
217         node_type_init(&ntype, node_composit_init_channel_matte);
218         node_type_storage(&ntype, "NodeChroma", node_free_standard_storage, node_copy_standard_storage);
219 #ifdef WITH_COMPOSITOR_LEGACY
220         node_type_exec(&ntype, node_composit_exec_channel_matte);
221 #endif
222
223         nodeRegisterType(ttype, &ntype);
224 }