Blender V2.61 - r43446

node_composite_channelMatte.c

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