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