Blender V2.61 - r43446

node_composite_normalize.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):  gsr b3d, and a very minor edit from Robert Holcomb 
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include "node_composite_util.h"
00034 
00035 
00036 /* **************** NORMALIZE single channel, useful for Z buffer ******************** */
00037 static bNodeSocketTemplate cmp_node_normalize_in[]= {
00038     {   SOCK_FLOAT, 1, "Value",         1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, PROP_NONE},
00039     {   -1, 0, ""   }
00040 };
00041 static bNodeSocketTemplate cmp_node_normalize_out[]= {
00042     {   SOCK_FLOAT, 0, "Value"},
00043     {   -1, 0, ""   }
00044 };
00045 
00046 static void do_normalize(bNode *UNUSED(node), float *out, float *src, float *min, float *mult)
00047 {
00048     float res;
00049     res = (src[0] - min[0]) * mult[0];
00050     if (res > 1.0f) {
00051         out[0] = 1.0f;
00052     }
00053     else if (res < 0.0f) {
00054         out[0] = 0.0f;
00055     }
00056     else {
00057         out[0] = res;
00058     }
00059 }
00060 
00061 /* The code below assumes all data is inside range +- this, and that input buffer is single channel */
00062 #define BLENDER_ZMAX 10000.0f
00063 
00064 static void node_composit_exec_normalize(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
00065 {
00066     /* stack order in: valbuf */
00067     /* stack order out: valbuf */
00068     if(out[0]->hasoutput==0) return;
00069 
00070     /* Input has no image buffer? Then pass the value */
00071     if(in[0]->data==NULL) {
00072         copy_v4_v4(out[0]->vec, in[0]->vec);
00073     }
00074     else {
00075         float min = 1.0f+BLENDER_ZMAX;
00076         float max = -1.0f-BLENDER_ZMAX;
00077         float mult = 1.0f;
00078         float *val;
00079         /* make output size of input image */
00080         CompBuf *cbuf= in[0]->data;
00081         int tot= cbuf->x*cbuf->y;
00082         CompBuf *stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_VAL, 1); /* allocs */
00083 
00084         for (val = cbuf->rect; tot; tot--, val++) {
00085             if ((*val > max) && (*val <= BLENDER_ZMAX)) {
00086                 max = *val;
00087             }
00088             if ((*val < min) && (*val >= -BLENDER_ZMAX)) {
00089                 min = *val;
00090             }
00091         }
00092         /* In the rare case of flat buffer, which would cause a divide by 0, just pass the input to the output */
00093         if ((max-min) != 0.0f) {
00094             mult = 1.0f/(max-min);
00095             composit3_pixel_processor(node, stackbuf, in[0]->data, in[0]->vec, NULL, &min, NULL, &mult, do_normalize, CB_VAL, CB_VAL, CB_VAL);
00096         } else {
00097             memcpy(stackbuf->rect, cbuf->rect, sizeof(float) * cbuf->x * cbuf->y);
00098         }
00099 
00100         out[0]->data= stackbuf;
00101     }
00102 }
00103 
00104 void register_node_type_cmp_normalize(bNodeTreeType *ttype)
00105 {
00106     static bNodeType ntype;
00107     
00108     node_type_base(ttype, &ntype, CMP_NODE_NORMALIZE, "Normalize", NODE_CLASS_OP_VECTOR, NODE_OPTIONS);
00109     node_type_socket_templates(&ntype, cmp_node_normalize_in, cmp_node_normalize_out);
00110     node_type_size(&ntype, 100, 60, 150);
00111     node_type_exec(&ntype, node_composit_exec_normalize);
00112     
00113     nodeRegisterType(ttype, &ntype);
00114 }