Merge with -r 22620:23107.
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 *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]);
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 *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]);
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         /* Alpha=G-MAX(R, B) */
72         
73         switch(node->custom2)
74         {
75         case 1:
76                 {
77                         alpha=in[0]-MAX2(in[1],in[2]);
78                         break;
79                 }
80         case 2:
81                 {
82                         alpha=in[1]-MAX2(in[0],in[2]);
83                         break;
84                 }
85         case 3:
86                 {
87                         alpha=in[2]-MAX2(in[0],in[1]);
88                         break;
89                 }
90         default:
91                 break;
92         }
93         
94         /*flip because 0.0 is transparent, not 1.0*/
95         alpha=1-alpha;
96         
97         /* test range*/
98         if(alpha>c->t1) {
99                 alpha=in[3]; /*whatever it was prior */
100         }
101         else if(alpha<c->t2){
102                 alpha=0.0;
103         }
104         else {/*blend */
105                 alpha=(alpha-c->t2)/(c->t1-c->t2);
106         }
107
108         
109         /* don't make something that was more transparent less transparent */
110         if (alpha<in[3]) {
111                 out[3]=alpha;
112         }
113         else {
114                 out[3]=in[3];
115         }
116
117 }
118
119 static void node_composit_exec_channel_matte(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
120 {
121         CompBuf *cbuf;
122         CompBuf *outbuf;
123         
124         if(in[0]->hasinput==0)  return;
125         if(in[0]->data==NULL) return;
126         if(out[0]->hasoutput==0 && out[1]->hasoutput==0) return;
127         
128         cbuf=typecheck_compbuf(in[0]->data, CB_RGBA);
129         
130         outbuf=dupalloc_compbuf(cbuf);
131         
132         /*convert to colorspace*/
133         switch(node->custom1) {
134         case 1: /*RGB */
135                 break;
136         case 2: /*HSV*/
137                 composit1_pixel_processor(node, outbuf, cbuf, in[1]->vec, do_rgba_to_hsva, CB_RGBA);
138                 break;
139         case 3: /*YUV*/
140                 composit1_pixel_processor(node, outbuf, cbuf, in[1]->vec, do_rgba_to_yuva, CB_RGBA);
141                 break;
142         case 4: /*YCC*/
143                 composit1_pixel_processor(node, outbuf, cbuf, in[1]->vec, do_normalized_rgba_to_ycca2, CB_RGBA);
144                 break;
145         default:
146                 break;
147         }
148
149         /*use the selected channel information to do the key */
150         composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_channel_matte, CB_RGBA);
151
152         /*convert back to RGB colorspace in place*/
153         switch(node->custom1) {
154         case 1: /*RGB*/
155                 break;
156         case 2: /*HSV*/
157                 composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_hsva_to_rgba, CB_RGBA);
158                 break;
159         case 3: /*YUV*/
160                 composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_yuva_to_rgba, CB_RGBA);
161                 break;
162         case 4: /*YCC*/
163                 composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_normalized_ycca_to_rgba2, CB_RGBA);
164                 break;
165         default:
166                 break;
167         }
168
169         generate_preview(node, outbuf);
170         out[0]->data=outbuf;
171         if(out[1]->hasoutput)
172                 out[1]->data=valbuf_from_rgbabuf(outbuf, CHAN_A);
173         
174         if(cbuf!=in[0]->data)
175                 free_compbuf(cbuf);
176
177 }
178
179 static void node_composit_init_channel_matte(bNode *node)
180 {
181    NodeChroma *c= MEM_callocN(sizeof(NodeChroma), "node chroma");
182    node->storage=c;
183    c->t1= 1.0f;
184    c->t2= 0.0f;
185    c->t3= 0.0f;
186    c->fsize= 0.0f;
187    c->fstrength= 0.0f;
188    node->custom1= 1; /* RGB channel */
189    node->custom2= 2; /* Green Channel */
190 }
191
192 bNodeType cmp_node_channel_matte={
193         /* *next,*prev */       NULL, NULL,
194         /* type code   */       CMP_NODE_CHANNEL_MATTE,
195         /* name        */       "Channel Key",
196         /* width+range */       200, 80, 250,
197         /* class+opts  */       NODE_CLASS_MATTE, NODE_PREVIEW|NODE_OPTIONS,
198         /* input sock  */       cmp_node_channel_matte_in,
199         /* output sock */       cmp_node_channel_matte_out,
200         /* storage     */       "NodeChroma",
201         /* execfunc    */       node_composit_exec_channel_matte,
202         /* butfunc     */       NULL,
203         /* initfunc    */       node_composit_init_channel_matte,
204         /* freestoragefunc    */        node_free_standard_storage,
205         /* copystoragefunc    */        node_copy_standard_storage,
206         /* id          */       NULL
207 };