|
Blender
V2.59
|
00001 /* 00002 * $Id: CMP_alphaOver.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 /* **************** ALPHAOVER ******************** */ 00038 static bNodeSocketType cmp_node_alphaover_in[]= { 00039 { SOCK_VALUE, 1, "Fac", 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f}, 00040 { SOCK_RGBA, 1, "Image", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f}, 00041 { SOCK_RGBA, 1, "Image", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f}, 00042 { -1, 0, "" } 00043 }; 00044 static bNodeSocketType cmp_node_alphaover_out[]= { 00045 { SOCK_RGBA, 0, "Image", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f}, 00046 { -1, 0, "" } 00047 }; 00048 00049 static void do_alphaover_premul(bNode *UNUSED(node), float *out, float *src, float *over, float *fac) 00050 { 00051 00052 if(over[3]<=0.0f) { 00053 QUATCOPY(out, src); 00054 } 00055 else if(fac[0]==1.0f && over[3]>=1.0f) { 00056 QUATCOPY(out, over); 00057 } 00058 else { 00059 float mul= 1.0f - fac[0]*over[3]; 00060 00061 out[0]= (mul*src[0]) + fac[0]*over[0]; 00062 out[1]= (mul*src[1]) + fac[0]*over[1]; 00063 out[2]= (mul*src[2]) + fac[0]*over[2]; 00064 out[3]= (mul*src[3]) + fac[0]*over[3]; 00065 } 00066 } 00067 00068 /* result will be still premul, but the over part is premulled */ 00069 static void do_alphaover_key(bNode *UNUSED(node), float *out, float *src, float *over, float *fac) 00070 { 00071 00072 if(over[3]<=0.0f) { 00073 QUATCOPY(out, src); 00074 } 00075 else if(fac[0]==1.0f && over[3]>=1.0f) { 00076 QUATCOPY(out, over); 00077 } 00078 else { 00079 float premul= fac[0]*over[3]; 00080 float mul= 1.0f - premul; 00081 00082 out[0]= (mul*src[0]) + premul*over[0]; 00083 out[1]= (mul*src[1]) + premul*over[1]; 00084 out[2]= (mul*src[2]) + premul*over[2]; 00085 out[3]= (mul*src[3]) + fac[0]*over[3]; 00086 } 00087 } 00088 00089 /* result will be still premul, but the over part is premulled */ 00090 static void do_alphaover_mixed(bNode *node, float *out, float *src, float *over, float *fac) 00091 { 00092 00093 if(over[3]<=0.0f) { 00094 QUATCOPY(out, src); 00095 } 00096 else if(fac[0]==1.0f && over[3]>=1.0f) { 00097 QUATCOPY(out, over); 00098 } 00099 else { 00100 NodeTwoFloats *ntf= node->storage; 00101 float addfac= 1.0f - ntf->x + over[3]*ntf->x; 00102 float premul= fac[0]*addfac; 00103 float mul= 1.0f - fac[0]*over[3]; 00104 00105 out[0]= (mul*src[0]) + premul*over[0]; 00106 out[1]= (mul*src[1]) + premul*over[1]; 00107 out[2]= (mul*src[2]) + premul*over[2]; 00108 out[3]= (mul*src[3]) + fac[0]*over[3]; 00109 } 00110 } 00111 00112 00113 00114 00115 static void node_composit_exec_alphaover(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out) 00116 { 00117 /* stack order in: col col */ 00118 /* stack order out: col */ 00119 if(out[0]->hasoutput==0) 00120 return; 00121 00122 /* input no image? then only color operation */ 00123 if(in[1]->data==NULL && in[2]->data==NULL) { 00124 do_alphaover_premul(node, out[0]->vec, in[1]->vec, in[2]->vec, in[0]->vec); 00125 } 00126 else { 00127 /* make output size of input image */ 00128 CompBuf *cbuf= in[1]->data?in[1]->data:in[2]->data; 00129 CompBuf *stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 1); /* allocs */ 00130 NodeTwoFloats *ntf= node->storage; 00131 00132 if(ntf->x != 0.0f) 00133 composit3_pixel_processor(node, stackbuf, in[1]->data, in[1]->vec, in[2]->data, in[2]->vec, in[0]->data, in[0]->vec, do_alphaover_mixed, CB_RGBA, CB_RGBA, CB_VAL); 00134 else if(node->custom1) 00135 composit3_pixel_processor(node, stackbuf, in[1]->data, in[1]->vec, in[2]->data, in[2]->vec, in[0]->data, in[0]->vec, do_alphaover_key, CB_RGBA, CB_RGBA, CB_VAL); 00136 else 00137 composit3_pixel_processor(node, stackbuf, in[1]->data, in[1]->vec, in[2]->data, in[2]->vec, in[0]->data, in[0]->vec, do_alphaover_premul, CB_RGBA, CB_RGBA, CB_VAL); 00138 00139 out[0]->data= stackbuf; 00140 } 00141 } 00142 00143 static void node_alphaover_init(bNode* node) 00144 { 00145 node->storage= MEM_callocN(sizeof(NodeTwoFloats), "NodeTwoFloats"); 00146 } 00147 00148 void register_node_type_cmp_alphaover(ListBase *lb) 00149 { 00150 static bNodeType ntype; 00151 00152 node_type_base(&ntype, CMP_NODE_ALPHAOVER, "AlphaOver", NODE_CLASS_OP_COLOR, NODE_OPTIONS, 00153 cmp_node_alphaover_in, cmp_node_alphaover_out); 00154 node_type_size(&ntype, 80, 40, 120); 00155 node_type_init(&ntype, node_alphaover_init); 00156 node_type_storage(&ntype, "NodeTwoFloats", node_free_standard_storage, node_copy_standard_storage); 00157 node_type_exec(&ntype, node_composit_exec_alphaover); 00158 00159 nodeRegisterType(lb, &ntype); 00160 } 00161 00162 00163