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