Blender V2.61 - r43446

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