Blender  V2.59
CMP_channelMatte.c
Go to the documentation of this file.
00001 /*
00002  * $Id: CMP_channelMatte.c 37836 2011-06-27 03:36:14Z campbellbarton $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version. 
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2006 Blender Foundation.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): Bob Holcomb
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  */
00029 
00035 #include "../CMP_util.h"
00036 
00037 
00038 /* ******************* Channel Matte Node ********************************* */
00039 static bNodeSocketType cmp_node_channel_matte_in[]={
00040         {SOCK_RGBA,1,"Image", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
00041         {-1,0,""}
00042 };
00043 
00044 static bNodeSocketType cmp_node_channel_matte_out[]={
00045         {SOCK_RGBA,0,"Image", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
00046         {SOCK_VALUE,0,"Matte",0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
00047         {-1,0,""}
00048 };
00049 
00050 static void do_normalized_rgba_to_ycca2(bNode *UNUSED(node), float *out, float *in)
00051 {
00052         /*normalize to the range 0.0 to 1.0) */
00053         rgb_to_ycc(in[0],in[1],in[2], &out[0], &out[1], &out[2], BLI_YCC_ITU_BT601);
00054         out[0]=(out[0])/255.0;
00055         out[1]=(out[1])/255.0;
00056         out[2]=(out[2])/255.0;
00057         out[3]=in[3];
00058 }
00059 
00060 static void do_normalized_ycca_to_rgba2(bNode *UNUSED(node), float *out, float *in)
00061 {
00062         /*un-normalize the normalize from above */
00063         in[0]=in[0]*255.0;
00064         in[1]=in[1]*255.0;
00065         in[2]=in[2]*255.0;
00066         ycc_to_rgb(in[0],in[1],in[2], &out[0], &out[1], &out[2], BLI_YCC_ITU_BT601);
00067         out[3]=in[3];
00068 }
00069 
00070 
00071 static void do_channel_matte(bNode *node, float *out, float *in)
00072 {
00073         NodeChroma *c=(NodeChroma *)node->storage;
00074         float alpha=0.0;        
00075 
00076         switch(c->algorithm) {
00077         case 0: { /* Alpha=key_channel-limit channel */
00078                 int key_channel=node->custom2-1;
00079                 int limit_channel=c->channel-1;
00080                 alpha=in[key_channel]-in[limit_channel];
00081                 break;
00082         }
00083         case 1: { /* Alpha=G-MAX(R, B) */
00084                 switch(node->custom2) {
00085                         case 1: {
00086                                 alpha=in[0]-MAX2(in[1],in[2]);
00087                                 break;
00088                         }
00089                         case 2: {
00090                                 alpha=in[1]-MAX2(in[0],in[2]);
00091                                 break;
00092                         }
00093                         case 3: {
00094                                 alpha=in[2]-MAX2(in[0],in[1]);
00095                                 break;
00096                         }
00097                         default:
00098                                 break;
00099                 }
00100                 break;
00101         }
00102         default:
00103                 break;
00104         }
00105 
00106         /*flip because 0.0 is transparent, not 1.0*/
00107         alpha=1-alpha;
00108         
00109         /* test range*/
00110         if(alpha>c->t1) {
00111                 alpha=in[3]; /*whatever it was prior */
00112         }
00113         else if(alpha<c->t2){
00114                 alpha=0.0;
00115         }
00116         else {/*blend */
00117                 alpha=(alpha-c->t2)/(c->t1-c->t2);
00118         }
00119 
00120         
00121         /* don't make something that was more transparent less transparent */
00122         if (alpha<in[3]) {
00123                 out[3]=alpha;
00124         }
00125         else {
00126                 out[3]=in[3];
00127         }
00128 }
00129 
00130 static void node_composit_exec_channel_matte(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
00131 {
00132         CompBuf *cbuf;
00133         CompBuf *outbuf;
00134         
00135         if(in[0]->hasinput==0) return;
00136         if(in[0]->data==NULL) return;
00137         if(out[0]->hasoutput==0 && out[1]->hasoutput==0) return;
00138         
00139         cbuf=typecheck_compbuf(in[0]->data, CB_RGBA);
00140         
00141         outbuf=dupalloc_compbuf(cbuf);
00142         
00143         /*convert to colorspace*/
00144         switch(node->custom1) {
00145         case CMP_NODE_CHANNEL_MATTE_CS_RGB:
00146                 break;
00147         case CMP_NODE_CHANNEL_MATTE_CS_HSV: /*HSV*/
00148                 composit1_pixel_processor(node, outbuf, cbuf, in[1]->vec, do_rgba_to_hsva, CB_RGBA);
00149                 break;
00150         case CMP_NODE_CHANNEL_MATTE_CS_YUV: /*YUV*/
00151                 composit1_pixel_processor(node, outbuf, cbuf, in[1]->vec, do_rgba_to_yuva, CB_RGBA);
00152                 break;
00153         case CMP_NODE_CHANNEL_MATTE_CS_YCC: /*YCC*/
00154                 composit1_pixel_processor(node, outbuf, cbuf, in[1]->vec, do_normalized_rgba_to_ycca2, CB_RGBA);
00155                 break;
00156         default:
00157                 break;
00158         }
00159 
00160         /*use the selected channel information to do the key */
00161         composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_channel_matte, CB_RGBA);
00162 
00163         /*convert back to RGB colorspace in place*/
00164         switch(node->custom1) {
00165         case CMP_NODE_CHANNEL_MATTE_CS_RGB: /*RGB*/
00166                 break;
00167         case CMP_NODE_CHANNEL_MATTE_CS_HSV: /*HSV*/
00168                 composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_hsva_to_rgba, CB_RGBA);
00169                 break;
00170         case CMP_NODE_CHANNEL_MATTE_CS_YUV: /*YUV*/
00171                 composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_yuva_to_rgba, CB_RGBA);
00172                 break;
00173         case CMP_NODE_CHANNEL_MATTE_CS_YCC: /*YCC*/
00174                 composit1_pixel_processor(node, outbuf, outbuf, in[1]->vec, do_normalized_ycca_to_rgba2, CB_RGBA);
00175                 break;
00176         default:
00177                 break;
00178         }
00179 
00180         generate_preview(data, node, outbuf);
00181         out[0]->data=outbuf;
00182         if(out[1]->hasoutput)
00183                 out[1]->data=valbuf_from_rgbabuf(outbuf, CHAN_A);
00184         
00185         if(cbuf!=in[0]->data)
00186                 free_compbuf(cbuf);
00187 
00188 }
00189 
00190 static void node_composit_init_channel_matte(bNode *node)
00191 {
00192         NodeChroma *c= MEM_callocN(sizeof(NodeChroma), "node chroma");
00193         node->storage=c;
00194         c->t1= 1.0f;
00195         c->t2= 0.0f;
00196         c->t3= 0.0f;
00197         c->fsize= 0.0f;
00198         c->fstrength= 0.0f;
00199         c->algorithm=1; /*max channel limiting */
00200         c->channel=1; /* limit by red */
00201         node->custom1= 1; /* RGB channel */
00202         node->custom2= 2; /* Green Channel */
00203 }
00204 
00205 void register_node_type_cmp_channel_matte(ListBase *lb)
00206 {
00207         static bNodeType ntype;
00208 
00209         node_type_base(&ntype, CMP_NODE_CHANNEL_MATTE, "Channel Key", NODE_CLASS_MATTE, NODE_PREVIEW|NODE_OPTIONS,
00210                 cmp_node_channel_matte_in, cmp_node_channel_matte_out);
00211         node_type_size(&ntype, 200, 80, 250);
00212         node_type_init(&ntype, node_composit_init_channel_matte);
00213         node_type_storage(&ntype, "NodeChroma", node_free_standard_storage, node_copy_standard_storage);
00214         node_type_exec(&ntype, node_composit_exec_channel_matte);
00215 
00216         nodeRegisterType(lb, &ntype);
00217 }