[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [13024] trunk/blender/source/blender: Bilateral Blur Node

Juho Vepsalainen bebraw at gmail.com
Thu Dec 27 21:36:17 CET 2007


Revision: 13024
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=13024
Author:   bebraw
Date:     2007-12-27 21:36:17 +0100 (Thu, 27 Dec 2007)

Log Message:
-----------
Bilateral Blur Node
Bilateral Blur node allows the user to blur images while retaining their
sharp edges. Blurring can be controlled by following controls:
*Iterations
*Color Sigma
*Space Sigma

Also image input to blur and a determinator image is provided. The node produces
a blurred image as its output.

The more iterations are provided, the smoother the result. Use color and space
sigmas to control the amount of blur. One way to use the determinator input is
to feed a mix (add) of Z and normal passes to it.

Examples of usage:
Ambient Occlusion smoothing - http://wiki.blender.org/index.php/Image:Bilateral_blur_example_01.blend
Blurry Refraction - http://wiki.blender.org/index.php/Image:Bilateral_blur_example_02.blend
Smoothed shadows and smoothed Ambient Occlusion combined - http://wiki.blender.org/index.php/Image:Bilateral_blur_example_03.blend

If you check out the examples, render the image and alter the values to see how 
they affect.

More information about the algorithm can be found at 
http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/MANDUCHI1/Bilateral_Filtering.html .

Thanks to Vilem Novak for contributing the patch.

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_node.h
    trunk/blender/source/blender/blenkernel/intern/node.c
    trunk/blender/source/blender/makesdna/DNA_node_types.h
    trunk/blender/source/blender/nodes/CMP_node.h
    trunk/blender/source/blender/src/drawnode.c

Added Paths:
-----------
    trunk/blender/source/blender/nodes/intern/CMP_nodes/CMP_bilateralblur.c

Modified: trunk/blender/source/blender/blenkernel/BKE_node.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_node.h	2007-12-27 20:33:29 UTC (rev 13023)
+++ trunk/blender/source/blender/blenkernel/BKE_node.h	2007-12-27 20:36:17 UTC (rev 13024)
@@ -307,6 +307,7 @@
 #define CMP_NODE_NORMALIZE      252
 #define CMP_NODE_CROP		253
 #define CMP_NODE_DBLUR		254
+#define CMP_NODE_BILATERALBLUR  255
 
 #define CMP_NODE_GLARE		301
 #define CMP_NODE_TONEMAP	302

Modified: trunk/blender/source/blender/blenkernel/intern/node.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/node.c	2007-12-27 20:33:29 UTC (rev 13023)
+++ trunk/blender/source/blender/blenkernel/intern/node.c	2007-12-27 20:36:17 UTC (rev 13024)
@@ -2375,6 +2375,7 @@
 	nodeRegisterType(ntypelist, &cmp_node_filter);
 	nodeRegisterType(ntypelist, &cmp_node_blur);
 	nodeRegisterType(ntypelist, &cmp_node_dblur);
+	nodeRegisterType(ntypelist, &cmp_node_bilateralblur);
 	nodeRegisterType(ntypelist, &cmp_node_vecblur);
 	nodeRegisterType(ntypelist, &cmp_node_dilateerode);
 	nodeRegisterType(ntypelist, &cmp_node_defocus);

Modified: trunk/blender/source/blender/makesdna/DNA_node_types.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_node_types.h	2007-12-27 20:33:29 UTC (rev 13023)
+++ trunk/blender/source/blender/makesdna/DNA_node_types.h	2007-12-27 20:36:17 UTC (rev 13024)
@@ -209,6 +209,11 @@
 	char wrap, pad;
 } NodeDBlurData;
 
+typedef struct NodeBilateralBlurData {
+	float sigma_color, sigma_space;
+	short iter, pad;
+} NodeBilateralBlurData;
+
 typedef struct NodeHueSat {
 	float hue, sat, val;
 } NodeHueSat;

Modified: trunk/blender/source/blender/nodes/CMP_node.h
===================================================================
--- trunk/blender/source/blender/nodes/CMP_node.h	2007-12-27 20:33:29 UTC (rev 13023)
+++ trunk/blender/source/blender/nodes/CMP_node.h	2007-12-27 20:36:17 UTC (rev 13024)
@@ -67,6 +67,7 @@
 extern bNodeType cmp_node_filter;
 extern bNodeType cmp_node_blur;
 extern bNodeType cmp_node_dblur;
+extern bNodeType cmp_node_bilateralblur;
 extern bNodeType cmp_node_vecblur;
 extern bNodeType cmp_node_dilateerode;
 extern bNodeType cmp_node_defocus;

Added: trunk/blender/source/blender/nodes/intern/CMP_nodes/CMP_bilateralblur.c
===================================================================
--- trunk/blender/source/blender/nodes/intern/CMP_nodes/CMP_bilateralblur.c	                        (rev 0)
+++ trunk/blender/source/blender/nodes/intern/CMP_nodes/CMP_bilateralblur.c	2007-12-27 20:36:17 UTC (rev 13024)
@@ -0,0 +1,273 @@
+/**
+ *
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. 
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2006 Blender Foundation.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): Vilem Novak
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+#include "../CMP_util.h"
+
+/* **************** BILATERALBLUR ******************** */
+static bNodeSocketType cmp_node_bilateralblur_in[]= {
+	{ SOCK_RGBA, 1, "Image", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f}, 
+	{ SOCK_RGBA, 1, "Determinator", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f}, 
+	{ -1, 0, "" } 
+};
+
+static bNodeSocketType cmp_node_bilateralblur_out[]= { 
+	{ SOCK_RGBA, 0, "Image", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f}, 
+	{ -1, 0, "" } 
+};
+
+#define INIT_C3\
+	mean0 = 1; mean1[0] = src[0];mean1[1] = src[1];mean1[2] = src[2];mean1[3] = src[3];
+
+/* finds color distances */
+#define COLOR_DISTANCE_C3(c1, c2)\
+	((c1[0] - c2[0])*(c1[0] - c2[0]) + \
+	(c1[1] - c2[1])*(c1[1] - c2[1]) + \
+	(c1[2] - c2[2])*(c1[2] - c2[2]) + \
+	(c1[3] - c2[3])*(c1[3] - c2[3]))
+
+/* this is the main kernel function for comparing color distances
+ and adding them weighted to the final color */
+#define KERNEL_ELEMENT_C3(k)\
+	temp_color = src + deltas[k];\
+	ref_color = ref + deltas[k];\
+	w = weight_tab[k] + COLOR_DISTANCE_C3(ref, ref_color )*i2sigma_color;\
+	w = 1./(w*w + 1); \
+	mean0 += w;\
+	mean1[0] += temp_color[0]*w; \
+	mean1[1] += temp_color[1]*w; \
+	mean1[2] += temp_color[2]*w; \
+	mean1[3] += temp_color[3]*w;
+
+/* write blurred values to image */
+#define UPDATE_OUTPUT_C3\
+	mean0 = 1./mean0;\
+	dest[x*pix + 0] = mean1[0]*mean0; \
+	dest[x*pix + 1] = mean1[1]*mean0; \
+	dest[x*pix + 2] = mean1[2]*mean0; \
+	dest[x*pix + 3] = mean1[3]*mean0;
+
+/* initializes deltas for fast access to neighbour pixels */
+#define INIT_3X3_DELTAS( deltas, step, nch )            \
+	((deltas)[0] =  (nch),  (deltas)[1] = -(step) + (nch),  \
+	(deltas)[2] = -(step), (deltas)[3] = -(step) - (nch),  \
+	(deltas)[4] = -(nch),  (deltas)[5] =  (step) - (nch),  \
+	(deltas)[6] =  (step), (deltas)[7] =  (step) + (nch));
+
+
+/* code of this node was heavily inspired by the smooth function of opencv library.
+The main change is an optional image input */
+static void node_composit_exec_bilateralblur(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
+{
+	NodeBilateralBlurData *nbbd= node->storage;
+	CompBuf *new, *source, *img= in[0]->data , *refimg= in[1]->data;
+	double mean0, w, i2sigma_color, i2sigma_space;
+	double mean1[4];
+	double weight_tab[8];
+	float *src, *dest, *ref, *temp_color, *ref_color;
+	float sigma_color, sigma_space;
+	int imgx, imgy, x, y, pix, i, step;
+	int deltas[8];
+	short found_determinator= 0;
+
+	if(img == NULL || out[0]->hasoutput == 0)
+		return;
+
+	if(img->type != CB_RGBA) {
+		img= typecheck_compbuf(in[0]->data, CB_RGBA);
+	}
+
+	imgx= img->x;
+	imgy= img->y;
+	pix= img->type;
+	step= pix * imgx;
+
+	if(refimg) {
+		refimg= img;
+		if(refimg->x == imgx && refimg->y == imgy) {
+			if(refimg->type == CB_VEC2 || refimg->type == CB_VEC3) {
+				refimg= typecheck_compbuf(in[1]->data, CB_RGBA);
+				found_determinator= 1;
+			}
+		}
+	}
+	else {
+		refimg= img;
+	}
+
+	/* allocs */
+	source= dupalloc_compbuf(img);
+	new= alloc_compbuf(imgx, imgy, pix, 1);
+	
+	/* accept image offsets from other nodes */
+	new->xof= img->xof;
+	new->yof= img->yof;
+
+	/* bilateral code properties */
+	sigma_color= nbbd->sigma_color;
+	sigma_space= nbbd->sigma_space;
+	
+	i2sigma_color= 1. / (sigma_color * sigma_color);
+	i2sigma_space= 1. / (sigma_space * sigma_space);
+
+	INIT_3X3_DELTAS(deltas, step, pix);
+
+	weight_tab[0] = weight_tab[2] = weight_tab[4] = weight_tab[6] = i2sigma_space;
+	weight_tab[1] = weight_tab[3] = weight_tab[5] = weight_tab[7] = i2sigma_space * 2;
+
+	/* iterations */
+	for(i= 0; i < nbbd->iter; i++) {
+		src= source->rect;
+		ref= refimg->rect;
+		dest= new->rect;
+		/*goes through image, there are more loops for 1st/last line and all other lines*/
+		/*kernel element accumulates surrounding colors, which are then written with the update_output function*/
+		for(x= 0; x < imgx; x++, src+= pix, ref+= pix) {
+			INIT_C3;
+
+			KERNEL_ELEMENT_C3(6);
+
+			if(x > 0) {
+				KERNEL_ELEMENT_C3(5);
+				KERNEL_ELEMENT_C3(4);
+			}
+
+			if(x < imgx - 1) {
+				KERNEL_ELEMENT_C3(7);
+				KERNEL_ELEMENT_C3(0);
+			}
+
+			UPDATE_OUTPUT_C3;
+		}
+
+		dest+= step;
+
+		for(y= 1; y < imgy - 1; y++, dest+= step, src+= pix, ref+= pix) {
+			x= 0;
+
+			INIT_C3;
+
+			KERNEL_ELEMENT_C3(0);
+			KERNEL_ELEMENT_C3(1);
+			KERNEL_ELEMENT_C3(2);
+			KERNEL_ELEMENT_C3(6);
+			KERNEL_ELEMENT_C3(7);
+
+			UPDATE_OUTPUT_C3;
+
+			src+= pix;
+			ref+= pix;
+
+			for(x= 1; x < imgx - 1; x++, src+= pix, ref+= pix) {
+				INIT_C3;
+
+				KERNEL_ELEMENT_C3(0);
+				KERNEL_ELEMENT_C3(1);
+				KERNEL_ELEMENT_C3(2);
+				KERNEL_ELEMENT_C3(3);
+				KERNEL_ELEMENT_C3(4);
+				KERNEL_ELEMENT_C3(5);
+				KERNEL_ELEMENT_C3(6);
+				KERNEL_ELEMENT_C3(7);
+
+				UPDATE_OUTPUT_C3;
+			}
+
+			INIT_C3;
+
+			KERNEL_ELEMENT_C3(2);
+			KERNEL_ELEMENT_C3(3);
+			KERNEL_ELEMENT_C3(4);
+			KERNEL_ELEMENT_C3(5);
+			KERNEL_ELEMENT_C3(6);
+
+			UPDATE_OUTPUT_C3;
+		}
+
+		for(x= 0; x < imgx; x++, src+= pix, ref+= pix) {
+			INIT_C3;
+
+			KERNEL_ELEMENT_C3(2);
+
+			if(x > 0) {
+				KERNEL_ELEMENT_C3(3);
+				KERNEL_ELEMENT_C3(4);
+			}
+			if(x < imgx - 1) {
+				KERNEL_ELEMENT_C3(1);
+				KERNEL_ELEMENT_C3(0);
+			}
+
+			UPDATE_OUTPUT_C3;
+		}
+
+		if(node->exec & NODE_BREAK) break;
+
+		SWAP(CompBuf, *source, *new);
+	}
+
+	if(node->exec & NODE_BREAK)
+		free_compbuf(source);
+
+	if(img != in[0]->data)
+		free_compbuf(img);
+
+	if(found_determinator == 1) {
+		if(refimg != in[1]->data)
+			free_compbuf(refimg);
+	}
+
+	out[0]->data= source;
+
+	free_compbuf(new);
+}
+
+static void node_composit_init_bilateralblur(bNode* node)
+{
+	NodeBilateralBlurData *nbbd= MEM_callocN(sizeof(NodeBilateralBlurData), "node bilateral blur data");
+	node->storage= nbbd;
+	nbbd->sigma_color= 0.3;
+	nbbd->sigma_space= 5.0;
+}
+
+bNodeType cmp_node_bilateralblur= {
+	/* *next,*prev */	NULL, NULL,
+	/* type code   */	CMP_NODE_BILATERALBLUR,
+	/* name        */	"Bilateral Blur",
+	/* width+range */	150, 120, 200,
+	/* class+opts  */	NODE_CLASS_OP_FILTER, NODE_OPTIONS,
+	/* input sock  */	cmp_node_bilateralblur_in,
+	/* output sock */	cmp_node_bilateralblur_out,
+	/* storage     */	"NodeBilateralBlurData",
+	/* execfunc    */	node_composit_exec_bilateralblur,
+	/* butfunc     */	NULL,
+	/* initfunc    */	node_composit_init_bilateralblur,
+	/* freestoragefunc    */	node_free_standard_storage,
+	/* copystoragefunc    */	node_copy_standard_storage,
+	/* id          */	NULL
+	
+};

Modified: trunk/blender/source/blender/src/drawnode.c
===================================================================
--- trunk/blender/source/blender/src/drawnode.c	2007-12-27 20:33:29 UTC (rev 13023)
+++ trunk/blender/source/blender/src/drawnode.c	2007-12-27 20:36:17 UTC (rev 13024)
@@ -1142,6 +1142,30 @@
 	return 190;
 }
 

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list