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