Blender V2.61 - r43446

node_composite_dilate.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 
00036 /* **************** Dilate/Erode ******************** */
00037 
00038 static bNodeSocketTemplate cmp_node_dilateerode_in[]= {
00039     {   SOCK_FLOAT, 1, "Mask",      0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, PROP_FACTOR},
00040     {   -1, 0, ""   }
00041 };
00042 static bNodeSocketTemplate cmp_node_dilateerode_out[]= {
00043     {   SOCK_FLOAT, 0, "Mask"},
00044     {   -1, 0, ""   }
00045 };
00046 
00047 static void morpho_dilate(CompBuf *cbuf)
00048 {
00049     int x, y;
00050     float *p, *rectf = cbuf->rect;
00051     
00052     for (y=0; y < cbuf->y; y++) {
00053         for (x=0; x < cbuf->x-1; x++) {
00054             p = rectf + cbuf->x*y + x;
00055             *p = MAX2(*p, *(p + 1));
00056         }
00057     }
00058 
00059     for (y=0; y < cbuf->y; y++) {
00060         for (x=cbuf->x-1; x >= 1; x--) {
00061             p = rectf + cbuf->x*y + x;
00062             *p = MAX2(*p, *(p - 1));
00063         }
00064     }
00065 
00066     for (x=0; x < cbuf->x; x++) {
00067         for (y=0; y < cbuf->y-1; y++) {
00068             p = rectf + cbuf->x*y + x;
00069             *p = MAX2(*p, *(p + cbuf->x));
00070         }
00071     }
00072 
00073     for (x=0; x < cbuf->x; x++) {
00074         for (y=cbuf->y-1; y >= 1; y--) {
00075             p = rectf + cbuf->x*y + x;
00076             *p = MAX2(*p, *(p - cbuf->x));
00077         }
00078     }
00079 }
00080 
00081 static void morpho_erode(CompBuf *cbuf)
00082 {
00083     int x, y;
00084     float *p, *rectf = cbuf->rect;
00085     
00086     for (y=0; y < cbuf->y; y++) {
00087         for (x=0; x < cbuf->x-1; x++) {
00088             p = rectf + cbuf->x*y + x;
00089             *p = MIN2(*p, *(p + 1));
00090         }
00091     }
00092 
00093     for (y=0; y < cbuf->y; y++) {
00094         for (x=cbuf->x-1; x >= 1; x--) {
00095             p = rectf + cbuf->x*y + x;
00096             *p = MIN2(*p, *(p - 1));
00097         }
00098     }
00099 
00100     for (x=0; x < cbuf->x; x++) {
00101         for (y=0; y < cbuf->y-1; y++) {
00102             p = rectf + cbuf->x*y + x;
00103             *p = MIN2(*p, *(p + cbuf->x));
00104         }
00105     }
00106 
00107     for (x=0; x < cbuf->x; x++) {
00108         for (y=cbuf->y-1; y >= 1; y--) {
00109             p = rectf + cbuf->x*y + x;
00110             *p = MIN2(*p, *(p - cbuf->x));
00111         }
00112     }
00113     
00114 }
00115 
00116 static void node_composit_exec_dilateerode(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
00117 {
00118     /* stack order in: mask */
00119     /* stack order out: mask */
00120     if(out[0]->hasoutput==0) 
00121         return;
00122     
00123     /* input no image? then only color operation */
00124     if(in[0]->data==NULL) {
00125         out[0]->vec[0] = out[0]->vec[1] = out[0]->vec[2] = 0.0f;
00126         out[0]->vec[3] = 0.0f;
00127     }
00128     else {
00129         /* make output size of input image */
00130         CompBuf *cbuf= typecheck_compbuf(in[0]->data, CB_VAL);
00131         CompBuf *stackbuf= dupalloc_compbuf(cbuf);
00132         short i;
00133         
00134         if (node->custom2 > 0) { // positive, dilate
00135             for (i = 0; i < node->custom2; i++)
00136                 morpho_dilate(stackbuf);
00137         } else if (node->custom2 < 0) { // negative, erode
00138             for (i = 0; i > node->custom2; i--)
00139                 morpho_erode(stackbuf);
00140         }
00141         
00142         if(cbuf!=in[0]->data)
00143             free_compbuf(cbuf);
00144         
00145         out[0]->data= stackbuf;
00146     }
00147 }
00148 
00149 void register_node_type_cmp_dilateerode(bNodeTreeType *ttype)
00150 {
00151     static bNodeType ntype;
00152     
00153     node_type_base(ttype, &ntype, CMP_NODE_DILATEERODE, "Dilate/Erode", NODE_CLASS_OP_FILTER, NODE_OPTIONS);
00154     node_type_socket_templates(&ntype, cmp_node_dilateerode_in, cmp_node_dilateerode_out);
00155     node_type_size(&ntype, 130, 100, 320);
00156     node_type_exec(&ntype, node_composit_exec_dilateerode);
00157     
00158     nodeRegisterType(ttype, &ntype);
00159 }