svn merge -r39834:40222 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / nodes / composite / nodes / node_composite_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/composite/nodes/node_composite_channelMatte.c
31  *  \ingroup cmpnodes
32  */
33
34
35 #include "node_composite_util.h"
36
37
38 /* ******************* Channel Matte Node ********************************* */
39 static bNodeSocketTemplate cmp_node_channel_matte_in[]={
40         {SOCK_RGBA,1,"Image", 0.8f, 0.8f, 0.8f, 1.0f},
41         {-1,0,""}
42 };
43
44 static bNodeSocketTemplate cmp_node_channel_matte_out[]={
45         {SOCK_RGBA,0,"Image"},
46         {SOCK_FLOAT,0,"Matte"},
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                 break;
101         }
102         default:
103                 break;
104         }
105
106         /*flip because 0.0 is transparent, not 1.0*/
107         alpha=1-alpha;
108         
109         /* test range*/
110         if(alpha>c->t1) {
111                 alpha=in[3]; /*whatever it was prior */
112         }
113         else if(alpha<c->t2){
114                 alpha=0.0;
115         }
116         else {/*blend */
117                 alpha=(alpha-c->t2)/(c->t1-c->t2);
118         }
119
120         
121         /* don't make something that was more transparent less transparent */
122         if (alpha<in[3]) {
123                 out[3]=alpha;
124         }
125         else {
126                 out[3]=in[3];
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(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
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         node_type_socket_templates(&ntype, 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 }