Blender  V2.59
CMP_colorMatte.c
Go to the documentation of this file.
00001 /*
00002  * $Id: CMP_colorMatte.c 37051 2011-05-31 14:06:29Z 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 /* ******************* Color Key ********************************************************** */
00038 static bNodeSocketType cmp_node_color_in[]={
00039         {SOCK_RGBA,1,"Image", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
00040         {SOCK_RGBA,1,"Key Color", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
00041         {-1,0,""}
00042 };
00043 
00044 static bNodeSocketType cmp_node_color_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_color_key(bNode *node, float *out, float *in)
00051 {
00052         float h_wrap;
00053         NodeChroma *c;
00054         c=node->storage;
00055 
00056 
00057         VECCOPY(out, in);
00058 
00059         if(
00060         /* do hue last because it needs to wrap, and does some more checks  */
00061 
00062         /* sat */       (fabs(in[1]-c->key[1]) < c->t2) &&
00063         /* val */       (fabs(in[2]-c->key[2]) < c->t3) &&
00064 
00065         /* multiply by 2 because it wraps on both sides of the hue,
00066          * otherwise 0.5 would key all hue's */
00067 
00068         /* hue */       ((h_wrap= 2.0f * fabs(in[0]-c->key[0])) < c->t1 || (2.0f - h_wrap) < c->t1)
00069         ) {
00070                 out[3]=0.0; /*make transparent*/
00071         }
00072 
00073         else { /*pixel is outside key color */
00074                 out[3]=in[3]; /* make pixel just as transparent as it was before */
00075         }
00076 }
00077 
00078 static void node_composit_exec_color_matte(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
00079 {
00080         CompBuf *cbuf;
00081         CompBuf *colorbuf;
00082         NodeChroma *c;
00083         
00084         if(in[0]->hasinput==0) return;
00085         if(in[0]->data==NULL) return;
00086         if(out[0]->hasoutput==0 && out[1]->hasoutput==0) return;
00087         
00088         cbuf= typecheck_compbuf(in[0]->data, CB_RGBA);
00089         
00090         colorbuf= dupalloc_compbuf(cbuf);
00091         
00092         c=node->storage;
00093         
00094         /*convert rgbbuf to hsv*/
00095         composit1_pixel_processor(node, colorbuf, cbuf, in[0]->vec, do_rgba_to_hsva, CB_RGBA);
00096         
00097    /*convert key to hsv*/
00098         do_rgba_to_hsva(node, c->key, in[1]->vec);
00099         
00100 
00101         /*per pixel color key*/
00102         composit1_pixel_processor(node, colorbuf, colorbuf, in[0]->vec, do_color_key, CB_RGBA);
00103         
00104         /*convert back*/
00105         composit1_pixel_processor(node, colorbuf, colorbuf, in[0]->vec, do_hsva_to_rgba, CB_RGBA);
00106         
00107         out[0]->data= colorbuf;
00108         if(out[1]->hasoutput)
00109                 out[1]->data= valbuf_from_rgbabuf(colorbuf, CHAN_A);
00110         
00111         generate_preview(data, node, colorbuf);
00112 
00113         if(cbuf!=in[0]->data)
00114                 free_compbuf(cbuf);
00115 }
00116 
00117 static void node_composit_init_color_matte(bNode *node)
00118 {
00119         NodeChroma *c= MEM_callocN(sizeof(NodeChroma), "node color");
00120         node->storage= c;
00121         c->t1= 0.01f;
00122         c->t2= 0.1f;
00123         c->t3= 0.1f;
00124         c->fsize= 0.0f;
00125         c->fstrength= 1.0f;
00126 }
00127 
00128 void register_node_type_cmp_color_matte(ListBase *lb)
00129 {
00130         static bNodeType ntype;
00131 
00132         node_type_base(&ntype, CMP_NODE_COLOR_MATTE, "Color Key", NODE_CLASS_MATTE, NODE_PREVIEW|NODE_OPTIONS,
00133                 cmp_node_color_in, cmp_node_color_out);
00134         node_type_size(&ntype, 200, 80, 300);
00135         node_type_init(&ntype, node_composit_init_color_matte);
00136         node_type_storage(&ntype, "NodeChroma", node_free_standard_storage, node_copy_standard_storage);
00137         node_type_exec(&ntype, node_composit_exec_color_matte);
00138 
00139         nodeRegisterType(lb, &ntype);
00140 }
00141 
00142 
00143