|
Blender
V2.59
|
00001 /* 00002 * $Id: CMP_chromaMatte.c 36276 2011-04-21 15:53:30Z 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): none yet. 00026 * 00027 * ***** END GPL LICENSE BLOCK ***** 00028 */ 00029 00035 #include "../CMP_util.h" 00036 00037 /* ******************* Chroma Key ********************************************************** */ 00038 static bNodeSocketType cmp_node_chroma_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_chroma_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_rgba_to_ycca_normalized(bNode *UNUSED(node), float *out, float *in) 00051 { 00052 rgb_to_ycc(in[0],in[1],in[2], &out[0], &out[1], &out[2], BLI_YCC_ITU_BT601); 00053 00054 //normalize to 0..1.0 00055 out[0]=out[0]/255.0; 00056 out[1]=out[1]/255.0; 00057 out[2]=out[2]/255.0; 00058 00059 //rescale to -1.0..1.0 00060 out[0]=(out[0]*2.0)-1.0; 00061 out[1]=(out[1]*2.0)-1.0; 00062 out[2]=(out[2]*2.0)-1.0; 00063 00064 // out[0]=((out[0])-16)/255.0; 00065 // out[1]=((out[1])-128)/255.0; 00066 // out[2]=((out[2])-128)/255.0; 00067 out[3]=in[3]; 00068 } 00069 00070 static void do_ycca_to_rgba_normalized(bNode *UNUSED(node), float *out, float *in) 00071 { 00072 /*un-normalize the normalize from above */ 00073 in[0]=(in[0]+1.0)/2.0; 00074 in[1]=(in[1]+1.0)/2.0; 00075 in[2]=(in[2]+1.0)/2.0; 00076 00077 in[0]=(in[0]*255.0); 00078 in[1]=(in[1]*255.0); 00079 in[2]=(in[2]*255.0); 00080 00081 // in[0]=(in[0]*255.0)+16; 00082 // in[1]=(in[1]*255.0)+128; 00083 // in[2]=(in[2]*255.0)+128; 00084 ycc_to_rgb(in[0],in[1],in[2], &out[0], &out[1], &out[2], BLI_YCC_ITU_BT601); 00085 out[3]=in[3]; 00086 } 00087 00088 static void do_chroma_key(bNode *node, float *out, float *in) 00089 { 00090 NodeChroma *c; 00091 float x, z, alpha; 00092 float theta, beta, angle, angle2; 00093 float kfg; 00094 00095 c=node->storage; 00096 00097 /* Algorithm from book "Video Demistified," does not include the spill reduction part */ 00098 00099 /* find theta, the angle that the color space should be rotated based on key*/ 00100 theta=atan2(c->key[2], c->key[1]); 00101 00102 /*rotate the cb and cr into x/z space */ 00103 x=in[1]*cos(theta)+in[2]*sin(theta); 00104 z=in[2]*cos(theta)-in[1]*sin(theta); 00105 00106 /*if within the acceptance angle */ 00107 angle=c->t1*M_PI/180.0; /* convert to radians */ 00108 00109 /* if kfg is <0 then the pixel is outside of the key color */ 00110 kfg=x-(fabs(z)/tan(angle/2.0)); 00111 00112 out[0]=in[0]; 00113 out[1]=in[1]; 00114 out[2]=in[2]; 00115 00116 if(kfg>0.0) { /* found a pixel that is within key color */ 00117 alpha=(1.0-kfg)*(c->fstrength); 00118 00119 beta=atan2(z,x); 00120 angle2=c->t2*M_PI/180.0; 00121 00122 /* if beta is within the cutoff angle */ 00123 if(fabs(beta)<(angle2/2.0)) { 00124 alpha=0.0; 00125 } 00126 00127 /* don't make something that was more transparent less transparent */ 00128 if (alpha<in[3]) { 00129 out[3]=alpha; 00130 } 00131 else { 00132 out[3]=in[3]; 00133 } 00134 } 00135 else { /*pixel is outside key color */ 00136 out[0]=in[0]; 00137 out[1]=in[1]; 00138 out[2]=in[2]; 00139 out[3]=in[3]; /* make pixel just as transparent as it was before */ 00140 } 00141 } 00142 00143 static void node_composit_exec_chroma_matte(void *data, bNode *node, bNodeStack **in, bNodeStack **out) 00144 { 00145 CompBuf *cbuf; 00146 CompBuf *chromabuf; 00147 NodeChroma *c; 00148 00149 if(in[0]->hasinput==0) return; 00150 if(in[0]->data==NULL) return; 00151 if(out[0]->hasoutput==0 && out[1]->hasoutput==0) return; 00152 00153 cbuf= typecheck_compbuf(in[0]->data, CB_RGBA); 00154 00155 chromabuf= dupalloc_compbuf(cbuf); 00156 00157 c=node->storage; 00158 00159 /*convert rgbbuf to normalized chroma space*/ 00160 composit1_pixel_processor(node, chromabuf, cbuf, in[0]->vec, do_rgba_to_ycca_normalized, CB_RGBA); 00161 /*convert key to normalized chroma color space */ 00162 do_rgba_to_ycca_normalized(node, c->key, in[1]->vec); 00163 00164 /*per pixel chroma key*/ 00165 composit1_pixel_processor(node, chromabuf, chromabuf, in[0]->vec, do_chroma_key, CB_RGBA); 00166 00167 /*convert back*/ 00168 composit1_pixel_processor(node, chromabuf, chromabuf, in[0]->vec, do_ycca_to_rgba_normalized, CB_RGBA); 00169 00170 out[0]->data= chromabuf; 00171 if(out[1]->hasoutput) 00172 out[1]->data= valbuf_from_rgbabuf(chromabuf, CHAN_A); 00173 00174 generate_preview(data, node, chromabuf); 00175 00176 if(cbuf!=in[0]->data) 00177 free_compbuf(cbuf); 00178 } 00179 00180 00181 static void node_composit_init_chroma_matte(bNode *node) 00182 { 00183 NodeChroma *c= MEM_callocN(sizeof(NodeChroma), "node chroma"); 00184 node->storage= c; 00185 c->t1= 30.0f; 00186 c->t2= 10.0f; 00187 c->t3= 0.0f; 00188 c->fsize= 0.0f; 00189 c->fstrength= 1.0f; 00190 } 00191 00192 void register_node_type_cmp_chroma_matte(ListBase *lb) 00193 { 00194 static bNodeType ntype; 00195 00196 node_type_base(&ntype, CMP_NODE_CHROMA_MATTE, "Chroma Key", NODE_CLASS_MATTE, NODE_PREVIEW|NODE_OPTIONS, 00197 cmp_node_chroma_in, cmp_node_chroma_out); 00198 node_type_size(&ntype, 200, 80, 300); 00199 node_type_init(&ntype, node_composit_init_chroma_matte); 00200 node_type_storage(&ntype, "NodeChroma", node_free_standard_storage, node_copy_standard_storage); 00201 node_type_exec(&ntype, node_composit_exec_chroma_matte); 00202 00203 nodeRegisterType(lb, &ntype); 00204 } 00205 00206 00207