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