[Bf-committers] DOF patch

Johnny Matthews johnny.matthews at gmail.com
Mon Apr 18 15:37:05 CEST 2005


Got it applied, I had to move that declaration of  "

+		Camera *ca;

in render/intern/readfile.c up a little bit to the top of the block to get it to compile. Also had to move the DOF button to make it visable :). I was able to blur my whole image, but when I turn autofocus on, it doesn't do anything. Perhaps I have missed an important step. I await your docs :)


Alexander Ewering wrote:

>
> Finally, here it is... instinctive-blender's DOF (Depth of Field) using
> strubi's zblur.c plugin, integrated into the render pipeline, with
> additional features Autofocus and Focus Lag, and some funky UI.
>
> Will write / provide existing docs shortly.
>
> zblur.c goes into source/blender/render/intern/source/
> zblur.h goes into source/blender/render/intern/include/
>
> Didn't know how to make cvs diff accept those...
>
> Ton, please review and commit. Don't forget Unified Renderer :)
>
> | alexander ewering              instinctive mediaworks
> | ae[@]instinctive[.]de   http://www[.]instinctive[.]de
>
>------------------------------------------------------------------------
>
>? cscope.out
>? dof.patch
>? run
>? t
>? obj/linux-glibc2.3.2-i386
>? source/blender/render/intern/include/zblur.h
>? source/blender/render/intern/source/zblur.c
>Index: source/blender/blenloader/intern/readfile.c
>===================================================================
>RCS file: /cvsroot/bf-blender/blender/source/blender/blenloader/intern/readfile.c,v
>retrieving revision 1.113
>diff -u -r1.113 readfile.c
>--- source/blender/blenloader/intern/readfile.c	17 Apr 2005 17:43:06 -0000	1.113
>+++ source/blender/blenloader/intern/readfile.c	17 Apr 2005 20:17:39 -0000
>@@ -4699,6 +4699,15 @@
> 				}
> 			}
> 		}
>+		Camera *ca;
>+		
>+		for (ca= main->camera.first; ca; ca= ca->id.next) {
>+			ca->focus= 0.9;
>+			ca->zgamma= 1.0;
>+			ca->zblur= 10.0;
>+			ca->zmin= 0.8;
>+			ca->flag |= CAM_AUTOFOCUS;
>+		}		
> 	}
> 	
> 	/* don't forget to set version number in blender.c! */
>Index: source/blender/include/butspace.h
>===================================================================
>RCS file: /cvsroot/bf-blender/blender/source/blender/include/butspace.h,v
>retrieving revision 1.36
>diff -u -r1.36 butspace.h
>--- source/blender/include/butspace.h	16 Apr 2005 14:01:49 -0000	1.36
>+++ source/blender/include/butspace.h	17 Apr 2005 20:17:41 -0000
>@@ -398,6 +398,7 @@
> 
> /* *********************** */
> #define B_CAMBUTS		2500
>+#define B_ZMIN			2401
> 
> /* *********************** */
> #define B_MBALLBUTS		2600
>Index: source/blender/makesdna/DNA_camera_types.h
>===================================================================
>RCS file: /cvsroot/bf-blender/blender/source/blender/makesdna/DNA_camera_types.h,v
>retrieving revision 1.10
>diff -u -r1.10 DNA_camera_types.h
>--- source/blender/makesdna/DNA_camera_types.h	30 Jan 2005 11:25:27 -0000	1.10
>+++ source/blender/makesdna/DNA_camera_types.h	17 Apr 2005 20:17:41 -0000
>@@ -54,6 +54,11 @@
> 	float YF_dofdist, YF_aperture;
> 	short YF_bkhtype, YF_bkhbias;
> 	float YF_bkhrot;
>+
>+	/* integrated DOF */
>+	float zmin, focus, focusx, focusy, zgamma, zsigma, zblur;
>+	float zlag;
>+	int oldzfocus, pad;
> 	
> 	struct Ipo *ipo;
> 	
>@@ -71,6 +76,10 @@
> #define CAM_SHOWMIST	2
> /* yafray: dof sampling switch */
> #define CAM_YF_NO_QMC	4
>+#define CAM_SHOWFOCUS   8
>+
>+#define CAM_DOF			16
>+#define CAM_AUTOFOCUS	32
> 
> #ifdef __cplusplus
> }
>Index: source/blender/makesdna/DNA_scene_types.h
>===================================================================
>RCS file: /cvsroot/bf-blender/blender/source/blender/makesdna/DNA_scene_types.h,v
>retrieving revision 1.37
>diff -u -r1.37 DNA_scene_types.h
>--- source/blender/makesdna/DNA_scene_types.h	17 Mar 2005 21:31:48 -0000	1.37
>+++ source/blender/makesdna/DNA_scene_types.h	17 Apr 2005 20:17:42 -0000
>@@ -303,6 +303,7 @@
> #define R_GAUSS      	0x20000
> #define R_FBUF			0x40000
> #define R_THREADS		0x80000
>+#define R_DOF			0x100000
> 
> /* yafray: renderer flag (not only exclusive to yafray) */
> #define R_INTERN	0
>Index: source/blender/render/intern/source/initrender.c
>===================================================================
>RCS file: /cvsroot/bf-blender/blender/source/blender/render/intern/source/initrender.c,v
>retrieving revision 1.54
>diff -u -r1.54 initrender.c
>--- source/blender/render/intern/source/initrender.c	11 Apr 2005 17:55:20 -0000	1.54
>+++ source/blender/render/intern/source/initrender.c	17 Apr 2005 20:17:47 -0000
>@@ -1065,6 +1065,10 @@
> 				if(RE_local_test_break()==0) add_halo_flare();
> 			}
> 
>+			if( (R.r.mode & R_DOF)) {
>+				if(RE_local_test_break()==0) add_zblur();
>+			}
>+
> 			if(R.r.mode & R_MBLUR) {
> 				add_to_blurbuf(blur);
> 			}
>Index: source/blender/src/butspace.c
>===================================================================
>RCS file: /cvsroot/bf-blender/blender/source/blender/src/butspace.c,v
>retrieving revision 1.33
>diff -u -r1.33 butspace.c
>--- source/blender/src/butspace.c	21 Mar 2005 00:47:21 -0000	1.33
>+++ source/blender/src/butspace.c	17 Apr 2005 20:17:48 -0000
>@@ -288,7 +288,7 @@
> 		do_fontbuts(event);
> 	}
> 	else if(event<=B_CAMBUTS) {
>-		;
>+		do_cambuts(event);
> 	}
> 	else if(event<=B_MBALLBUTS) {
> 		do_mballbuts(event);
>Index: source/blender/src/buttons_editing.c
>===================================================================
>RCS file: /cvsroot/bf-blender/blender/source/blender/src/buttons_editing.c,v
>retrieving revision 1.107
>diff -u -r1.107 buttons_editing.c
>--- source/blender/src/buttons_editing.c	17 Apr 2005 18:00:32 -0000	1.107
>+++ source/blender/src/buttons_editing.c	17 Apr 2005 20:17:54 -0000
>@@ -1343,10 +1343,11 @@
> 
> 	uiDefButF(block, NUM,REDRAWVIEW3D, "DrawSize:", 470,90,160,20, &cam->drawsize, 0.1*grid, 10.0, 10, 0, "Specify the drawsize of the camera");
> 
>-	uiDefButS(block, TOG, REDRAWVIEW3D, "Ortho", 470,49,61,40, &cam->type, 0, 0, 0, 0, "Render orthogonally");
>+	uiDefButS(block, TOG, REDRAWVIEW3D, "Ortho", 470,29,61,60, &cam->type, 0, 0, 0, 0, "Render orthogonally");
> 	uiBlockBeginAlign(block);
> 	uiDefButS(block, TOG|BIT|0,REDRAWVIEW3D, "ShowLimits", 533,69,97,20, &cam->flag, 0, 0, 0, 0, "Draw the field of view");
> 	uiDefButS(block, TOG|BIT|1,REDRAWVIEW3D, "Show Mist", 533,49,97,20, &cam->flag, 0, 0, 0, 0, "Draw a line that indicates the mist area");
>+	uiDefButS(block, TOG|BIT|3,REDRAWVIEW3D, "ShowFocus", 533,29,97,20, &cam->flag, 0, 0, 0, 0, "Draw a crosshair at the autofocus point in camera view");	
> 	uiBlockEndAlign(block);
> }
> 
>@@ -1378,6 +1379,46 @@
> 
> }
> 
>+static void editing_panel_camera_dof(Object *ob, Camera *cam)
>+{
>+	uiBlock *block;
>+
>+	block= uiNewBlock(&curarea->uiblocks, "editing_panel_camera_dof", UI_EMBOSS, UI_HELV, curarea->win);
>+	if(uiNewPanel(curarea, block, "Depth of Field", "DOF", 320, 0, 318, 204)==0) return;
>+
>+	uiDefButS(block, TOG|BIT|4,REDRAWVIEW3D, "DOF", 340,178,110,20, &cam->flag, 0, 0, 0, 0, "Use depth of field");		
>+
>+	uiDefButF(block, NUM,REDRAWVIEW3D, "Focus:", 340,147,110,20, &cam->focus, cam->zmin, 1.0, 0, 0, "Specify the focus distance");
>+	uiDefButF(block, NUM,B_ZMIN, "ZMin:", 340,125,110,20, &cam->zmin, 0.0, 1.0, 0, 0, "Specify the minimum distance that can be focussed");				
>+	uiDefButF(block, NUM,REDRAWVIEW3D, "Blur:", 340,103,110,20, &cam->zblur, 1.0, 100.0, 0, 0, "Specify the maximum blur radius");		
>+	uiDefButF(block, NUM,REDRAWVIEW3D, "BG Gamma:", 340,81,110,20, &cam->zgamma, 0.05, 2.0, 0, 0, "");
>+	uiDefButF(block, NUM,REDRAWVIEW3D, "FG Sigma:", 340,59,110,20, &cam->zsigma, 0.0, 20.0, 0, 0, "");
>+
>+	uiDefButS(block, TOG|BIT|5,REDRAWVIEW3D, "AutoFocus", 460,178,110,20, &cam->flag, 0, 0, 0, 0, "Use autofocus");				
>+
>+	uiDefButF(block, NUM,REDRAWVIEW3D, "Focus X:", 460,147,110,20, &cam->focusx, -1.0, 1.0, 0, 0, "Specify the focus point in the field of view");
>+	uiDefButF(block, NUM,REDRAWVIEW3D, "Focus Y:", 460,125,110,20, &cam->focusy, -1.0, 1.0, 0, 0, "Specify the focus point in the field of view");
>+	uiDefButF(block, NUM,REDRAWVIEW3D, "Focus Lag:", 460,103,110,20, &cam->zlag, 0.0, 50.0, 0, 0, "Number of frames focus motor lags behind");			
>+}
>+
>+/* **************************** CAMERA *************************** */
>+
>+void do_cambuts(unsigned short event)
>+{
>+	Object *ob;
>+	Camera *cam;
>+	
>+	ob= OBACT;
>+	if (ob==0) return;
>+	cam= ob->data;
>+
>+	switch(event) {
>+	case B_ZMIN:
>+		if (cam->focus < cam->zmin) cam->focus= cam->zmin;
>+		allqueue(REDRAWBUTSEDIT, 0);
>+		break;
>+	}
>+}
> 
> /* *************************** MBALL ******************************** */
> 
>@@ -2786,6 +2827,7 @@
> 		cam= ob->data;
> 		editing_panel_links(ob); // no editmode!
> 		editing_panel_camera_type(ob, cam);
>+		editing_panel_camera_dof(ob, cam);				
> 		/* yafray: extra panel for dof parameters */
> 		if (G.scene->r.renderer==R_YAFRAY) editing_panel_camera_yafraydof(ob, cam);
> 		break;
>Index: source/blender/src/buttons_scene.c
>===================================================================
>RCS file: /cvsroot/bf-blender/blender/source/blender/src/buttons_scene.c,v
>retrieving revision 1.79
>diff -u -r1.79 buttons_scene.c
>--- source/blender/src/buttons_scene.c	31 Mar 2005 18:49:52 -0000	1.79
>+++ source/blender/src/buttons_scene.c	17 Apr 2005 20:17:57 -0000
>@@ -1093,6 +1093,8 @@
> 	uiDefButS(block, ROW,800,"Premul",	405,13,50,20,&G.scene->r.alphamode,3.0,1.0, 0, 0, "Multiply alpha in advance");
> 	uiDefButS(block, ROW,800,"Key",		456,13,35,20,&G.scene->r.alphamode,3.0,2.0, 0, 0, "Alpha and colour values remain unchanged");
> 	uiBlockEndAlign(block);
>+
>+	uiDefButI(block, TOG|BIT|20,800,"DOF",		500,13,35,20,&G.scene->r.mode,0,0, 0, 0, "Apply Depth of Field (Ton, move elsewhere!)");
> 	
> 	if(G.scene->r.mode & R_RAYTRACE)
> 		uiDefButS(block, MENU, B_DIFF,"Octree resolution %t|64 %x64|128 %x128|256 %x256|512 %x512",	496,13,64,20,&G.scene->r.ocres,0.0,0.0, 0, 0, "Octree resolution for ray tracing");
>Index: source/blender/src/drawview.c
>===================================================================
>RCS file: /cvsroot/bf-blender/blender/source/blender/src/drawview.c,v
>retrieving revision 1.124
>diff -u -r1.124 drawview.c
>--- source/blender/src/drawview.c	11 Apr 2005 17:55:20 -0000	1.124
>+++ source/blender/src/drawview.c	17 Apr 2005 20:18:02 -0000
>@@ -56,6 +56,7 @@
> 
> #include "DNA_action_types.h"
> #include "DNA_armature_types.h"
>+#include "DNA_camera_types.h"
> #include "DNA_constraint_types.h"
> #include "DNA_curve_types.h"
> #include "DNA_group_types.h"
>@@ -842,12 +843,37 @@
> 	float x1, x2, y1, y2;
> 	float x3, y3, x4, y4;
> 	rcti viewborder;
>+	Camera *cam;
>+	float fx, fy;
> 	
> 	calc_viewborder(G.vd, &viewborder);
> 	x1= viewborder.xmin;
> 	y1= viewborder.ymin;
> 	x2= viewborder.xmax;
> 	y2= viewborder.ymax;
>+
>+	if (G.scene->camera) {
>+		cam= G.scene->camera->data;	
>+		if ((cam->flag & CAM_SHOWFOCUS) && (cam->flag & CAM_DOF) && (cam->flag & CAM_AUTOFOCUS)) {
>+			setlinestyle(0);
>+			cpack(0xFFFFFF);
>+			fx= (x2+x1)/2.0 + cam->focusx*((x2-x1)/2.0);
>+			fy= (y2+y1)/2.0 + cam->focusy*((y2-y1)/2.0);
>+			glBegin(GL_LINES);
>+			glVertex2f(fx-2, fy);
>+			glVertex2f(fx-5, fy);
>+			
>+			glVertex2f(fx+2, fy);
>+			glVertex2f(fx+5, fy);
>+			
>+			glVertex2f(fx, fy-2);
>+			glVertex2f(fx, fy-5);		
>+			
>+			glVertex2f(fx, fy+2);
>+			glVertex2f(fx, fy+5);		
>+			glEnd();
>+		}
>+	}
> 
> 	/* passepartout, in color of backdrop minus 50 */
> 	if(G.scene->r.scemode & R_PASSEPARTOUT) {
>Index: source/blender/src/editview.c
>===================================================================
>RCS file: /cvsroot/bf-blender/blender/source/blender/src/editview.c,v
>retrieving revision 1.58
>diff -u -r1.58 editview.c
>--- source/blender/src/editview.c	11 Apr 2005 19:31:05 -0000	1.58
>+++ source/blender/src/editview.c	17 Apr 2005 20:18:04 -0000
>@@ -46,6 +46,7 @@
> #include "DNA_armature_types.h"
> #include "DNA_meta_types.h"
> #include "DNA_mesh_types.h"
>+#include "DNA_camera_types.h"
> #include "DNA_curve_types.h"
> #include "DNA_lattice_types.h"
> #include "DNA_object_types.h"
>@@ -772,6 +773,24 @@
> 		VECCOPY(fp, oldcurs);
> 	}
> 	
>+}
>+
>+void set_autofocus(Camera *cam)
>+{
>+	rcti viewborder;
>+	short mval[2];
>+	
>+	calc_viewborder(G.vd, &viewborder);
>+	getmouseco_areawin(mval);	
>+	
>+	cam->focusx= (-1.0+(((float)mval[0]-(float)viewborder.xmin)/((float)viewborder.xmax-(float)viewborder.xmin))*2);
>+	cam->focusy= (-1.0+(((float)mval[1]-(float)viewborder.ymin)/((float)viewborder.ymax-(float)viewborder.ymin))*2);	
>+	
>+	CLAMP(cam->focusx, -1.0, 1.0);
>+	CLAMP(cam->focusy, -1.0, 1.0);	
>+	
>+	allqueue(REDRAWBUTSEDIT, 0);
>+	allqueue(REDRAWVIEW3D, 0);
> }
> 
> void deselectall(void)	/* is toggle */
>Index: source/blender/src/space.c
>===================================================================
>RCS file: /cvsroot/bf-blender/blender/source/blender/src/space.c,v
>retrieving revision 1.247
>diff -u -r1.247 space.c
>--- source/blender/src/space.c	17 Apr 2005 15:50:52 -0000	1.247
>+++ source/blender/src/space.c	17 Apr 2005 20:18:13 -0000
>@@ -1211,9 +1211,11 @@
> 				else if((G.qual==LR_SHIFTKEY))
> 					fly();
> 				else {
>-						set_faceselect();
>-					}
>-				
>+					if ( (G.vd->persp==2) &&
>+					     OBACT && (OBACT == G.vd->camera) ) {
>+						set_autofocus(G.vd->camera->data);
>+					} else set_faceselect();
>+				}				
> 				break;
> 			case GKEY:
> 				/* RMGRP if(G.qual & LR_CTRLKEY) add_selected_to_group();
>  
>
>------------------------------------------------------------------------
>
>/**
> * $Id: zblur.c,v 1.0 2004/01/18 15:06:07 intrr Exp $
> *
> * ***** BEGIN GPL/BL DUAL 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. The Blender
> * Foundation also sells licenses for use in proprietary software under
> * the Blender License.  See http://www.blender.org/BL/ for information
> * about this.
> *
> * 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.
> *
> * ***** END GPL/BL DUAL LICENSE BLOCK *****
> */
>
>/* 
> * This file is largely based on the focal blur plugin by onk, 8.99
> *
> */
>
>#include <math.h>
>#include <limits.h>
>#include <stdio.h>
>
>#include "IMB_imbuf_types.h"
>#include "MEM_guardedalloc.h"
>
>#include "DNA_camera_types.h"
>#include "DNA_scene_types.h"
>
>#include "BKE_global.h"
>#include "BKE_utildefines.h"
>
>#include "render.h"
>#include "renderHelp.h"
>#include "blendef.h"
>
>/* -------------------------------------------------
> * defines, protos */
>
>#define MINGAMMA 0.05
>#define MAXGAMMA 2.0
>#define MAXSIGMA 20.0
>#define IMG_COMPLEX Complex
>#define IMG_FLOAT float
>#define IMG_BYTE unsigned char
>#define sigmo(s, x, c) (tanh(c * (x - s)))
>
>typedef struct {
>	IMG_FLOAT re;
>	IMG_FLOAT im;
>} Complex;
>
>
>typedef enum { I_RGB, I_RGBA, I_GRAY, I_LUT, I_FLOAT, I_FLOAT3, I_COMPLEX } IMGTYPE;
>
>typedef struct {
>	int x, y;
>	int size, el_size;
>	IMGTYPE type;
>	unsigned char *data;
>} Image;
>
>typedef struct {			/* blur mask struct */
>	int size;
>	float fac;
>	float *val;
>} Mask;
>
>typedef Mask* Maskarray;
>
>/* don't change these */
>#define NMASKS_SHIFT 2			
>#define NMASKS 64
>
>/* -------------------------------------------------
> * globals */
>
>unsigned int g_imagemem = 0;
>
>Image *alloc_img(int x, int y, IMGTYPE type)
>{
>	Image *ret;
>	int size, typesize;
>
>	switch (type)
>	{
>	case I_RGB:
>		typesize = 3;
>		break;
>	case I_RGBA:
>		typesize = 4;
>		break;
>	case I_GRAY:
>		typesize = 1;
>		break;
>	case I_FLOAT:
>		typesize = sizeof(IMG_FLOAT);
>		break;
>	case I_FLOAT3:
>		typesize = 3 * sizeof(IMG_FLOAT);
>		break;
>	case I_COMPLEX:
>		typesize = sizeof(IMG_COMPLEX);
>		break;
>	default:
>		return 0;
>	}
>
>	size = x * y;
>
>	
>	ret = (Image *) MEM_mallocN(sizeof(Image) + size*typesize, "zblur_img");
>	if (ret)
>	{
>		g_imagemem += sizeof(Image) + size*typesize;
>		ret->x = x;
>		ret->y = y;
>		ret->size = size;
>		ret->el_size = typesize;
>		ret->type = type;
>		ret->data = (unsigned char *) (ret + 1);
>		size *= typesize;
>		memset(ret->data, 0, size);
>	}
>
>	return ret;
>}
>
>int free_img(Image *img)
>{
>	g_imagemem -= sizeof(Image) + img->size*img->el_size;
>	MEM_freeN(img);
>	return 1;
>}
>
>void g2rgb(IMG_BYTE *g, IMG_BYTE *rgb, int len)
>{
>	while (len--)
>	{
>		*rgb++ = *g;
>		*rgb++ = *g;
>		*rgb++ = *g;
>		g++;
>	}
>}
>
>/* --------------------------------------------------------------------- */
>/* mask routines */
>
>Mask *alloc_mask(int size)
>{
>	Mask *m;
>	int memsize;
>
>	memsize = (sizeof(Mask) + (2 * size +1) * (2 * size +1) * sizeof(float));
>	g_imagemem += memsize;
>
>	m = (Mask*) MEM_mallocN(memsize, "zblur_mask");
>	m->size = size;
>	m->val = (float *) (m + 1);
>
>	return m;
>}
>
>void free_mask(Mask *m)
>{
>	int memsize;
>
>	memsize = 2 * m->size + 1;
>	memsize *= memsize * sizeof(float);
>	memsize += sizeof(Mask);
>	
>	g_imagemem -= memsize;
>	MEM_freeN(m);
>}
>
>/* normalize mask to 1 */
>
>void norm_mask(Mask *m)
>{
>	float fac;
>	int size;
>	float *v;
>
>	fac = m->fac;
>	size = (2 * m->size +1)*(2 * m->size +1);
>
>	v = m->val;
>	while(size--)
>	{
>		*v++ *= fac;
>	}
>	m->fac = 1.0;
>}
>
>/* sinc removed */
>
>float zigma(float x)
>{
>	return 1/(1+pow(x, 8));
>}
>			
>/* filters a grayvalue image with a gaussian IIR filter with blur radius "rad" 
> * For large blurs, it's more efficient to call the routine several times
> * instead of using big blur radii.
> * The original image is changed */
>
>
>void gauss_blur(Image *img, float rad)
>{
>	Image *new;
>	unsigned char *src, *dest;
>	int r, n, m;
>	int x, y;
>	int i;
>	int step, bigstep;
>
>	register float sum, val;
>	float gval;
>	float *gausstab, *v;
>
>	r = (1.5 * rad + 1.5);
>	n = 2 * r + 1;
>	/* ugly : */
>	if ((img->x <= n) || (img->y <= n))
>	{
>		return;
>	}
>	gausstab = (float *) MEM_mallocN(n * sizeof(float), "zblur_gauss");
>	if (!gausstab)
>	{
>		return;
>	}
>	sum = 0.0;
>	v = gausstab;
>	for (x = -r; x <= r; x++)
>	{
>
>		val = exp(-4*(float ) (x*x)/ (float) (r*r));
>		sum += val;
>		*v++ = val;
>	}
>
>	i = n;
>	v = gausstab;
>	while (i--)
>	{
>		*v++ /= sum;
>	}
>
>	new = alloc_img(img->x, img->y, I_GRAY);
>	if (!new)
>	{
>		return;
>	}
>
>	/* horizontal */
>
>	step = (n - 1);
>
>	for (y = 0; y < img->y; y++)
>	{
>		src = (unsigned char *)img->data + (y * img->x);
>		dest = (unsigned char *)new->data + (y * img->x);
>
>		for (x = r; x > 0 ; x--)
>		{
>			m = n - x;
>			gval = 0.0;
>			sum = 0.0;
>			v = gausstab + x;
>			for (i = 0; i < m; i++)
>			{
>				val = *v++;
>				sum += val;
>				gval += val * (*src++);
>			}
>			*dest++ = gval / sum;
>			src -= m;
>		}
>
>		for (x = 0; x <= (img->x - n); x++)
>		{
>			gval = 0.0;
>			v = gausstab;
>
>			for (i = 0; i < n; i++)
>			{
>				val = *v++;
>				gval += val * (*src++);
>			}
>			*dest++ = gval;
>			src -= step;
>		}	
>
>		for (x = 1; x <= r ; x++)
>		{
>			m = n - x;
>			gval = 0.0;
>			sum = 0.0;
>			v = gausstab;
>			for (i = 0; i < m; i++)
>			{
>				val = *v++;
>				sum += val;
>				gval += val * (*src++);
>			}
>			*dest++ = gval / sum;
>			src -= (m - 1);
>		}
>	}
>
>	/* vertikal */
>
>	step = img->x;
>	bigstep = (n - 1) * step;
>	for (x = 0; x < step  ; x++)
>	{
>		src = new->data + x;
>		dest = img->data + x;
>
>		for (y = r; y > 0; y--)
>		{
>			m = n - y;
>			gval = 0.0;
>			sum = 0.0;
>			v = gausstab + y;
>			for (i = 0; i < m; i++)
>			{
>				val = *v++;
>				sum += val;
>				gval += val * src[0];
>				src += step;
>			}
>			dest[0] = gval / sum;
>			src -= m * step;
>			dest+= step;
>		}
>		for (y = 0; y <= (img->y - n); y++)
>		{
>			gval = 0.0;
>			v = gausstab;
>			for (i = 0; i < n; i++)
>			{
>				val = *v++;
>				gval += val * src[0];
>				src += step;
>			}
>			dest[0] = gval;
>			dest += step;
>			src -= bigstep;
>		}
>		for (y = 1; y <= r ; y++)
>		{
>			m = n - y;
>			gval = 0.0;
>			sum = 0.0;
>			v = gausstab;
>			for (i = 0; i < m; i++)
>			{
>				val = *v++;
>				sum += val;
>				gval += val * src[0];
>				src += step;
>			}
>			dest[0] = gval / sum;
>			dest += step;
>			src -= (m - 1) * step;
>		}
>	}
>	MEM_freeN(gausstab);
>	free_img(new);
>}
>
>
>Mask *gauss_mask(float rad)
>{
>	int r;
>	int ix, iy;
>	Mask *m;
>	float sum, val, *v;
>
>	
>	r = (1.0 * rad + 1.0);
>	m = alloc_mask(r);
>	v = m->val;
>	sum = 0.0;
>	for (iy = -r; iy <= r; iy++)
>	{
>		for (ix = -r; ix <= r; ix++)
>		{
>			val = zigma((float) (ix*ix + iy*iy)/(rad*rad));
>/*			val = exp(-(float) (ix*ix + iy*iy)/(rad * rad)); */
>			sum += val;
>			*v++ = val;
>		}
>	}
>	m->fac = 1.0 / sum;
>	norm_mask(m);
>	return m;
>}
>
>/* generates #num masks with the maximal blur radius 'rad' 
> * */
>Maskarray *init_masks(int num, float rad)
>{
>	int i;
>	float r, step;
>	Maskarray *maskarray;
>
>	maskarray = (Maskarray*) MEM_mallocN(num * sizeof (Maskarray), "zblur_masks");
>	step = rad / num;
>	r = 0.1;
>	for (i = 0; i < num; i++)
>	{
>		maskarray[i] = gauss_mask(r);
>		r += step;
>	}
>	return maskarray;
>}
>
>/* gamma correction, z-weighted */
>void zgamma(Image *img, Image *zbuf, float gamma)
>{
>	int i;
>	unsigned char *zptr, *iptr;
>	float f, g;
>
>
>	gamma = 1.0 - gamma;
>
>	zptr = zbuf->data;
>	iptr = img->data;
>	i = img->size;
>	while (i--)
>	{
>		g = 1.0 - gamma * (float) (*zptr++) / 255.0;
>		f = *iptr / 255.0 ;
>		f = pow(f,g);
>		*iptr++ = 255.0 * f;
>		f = *iptr / 255.0 ;
>		f = pow(f,g);
>		*iptr++ = 255.0 * f;
>		f = *iptr / 255.0 ;
>		f = pow(f,g);
>		*iptr++ = 255.0 * f;
>	}
>}
>
>/* lights warp function (like gamma correction only after value s)
> * -1.0 < c < 3.0 : warp parameter 
> *  0.0 < x < 1.0 : intensity value
> *  0.0 < s < 1.0 : warp start
> */
>
>float hiwarp(float c, float x, float s)
>{
>	float a, b, e;
>	float y, z;
>	
>	if ((x <= s) || (s == 1.0))
>		return x;
>
>	e = 1.0 / (1.0 - s);
>
>	x -= s;
>	a = c - 2.0;
>	b = - (1.0 + a + c);
>	y = e * x - 1.0;
>	z = y*y;
>	y = 1 + a*z; z *= z; y += b*z; z *= z; y+= c*z;		/* y = 1 + a * z + b*z^2 + c*z^3 */
>	e *= -0.5; z = e*x*x;
>	e = -0.25 / e;
>	z += s + x + e*y;
>	return z;
>}
>	
>
>Image *zblur2(Image *src, Image *zbuf, float radius, float sigma)
>{
>	Image *dest, *fdest;
>	Maskarray *mar;
>	float *mval;
>	float rval, gval, bval;
>	float fac;		
>	Image *norm;
>	unsigned char *sptr, *dptr, *zptr;	/* source, dest */
>	float *nptr, *fptr; 			/* norm image, float-rgb-image */
>	int zval;
>	int size;
>	int row;
>	int mrow;
>	int x, y;
>	int sx, sy, ex, ey;
>	int mx, my;
>	Mask *m;
>
>	/* test vars */
>	float f, g;
>
>
>	if(src->type != I_RGB)
>		return 0;
>
>	dest = alloc_img(src->x, src->y, I_RGB);
>	norm = alloc_img(src->x, src->y, I_FLOAT);
>	fdest = alloc_img(src->x, src->y, I_FLOAT3);
>
>	
>	if (!norm || !fdest || !dest)
>	{
>		perror("zblur2");
>		return 0;
>	}
>	row = src->x * src->el_size;
>	sptr = src->data;
>	dptr = dest->data;
>
>	mar = init_masks(NMASKS, radius);
>
>	for (y = 0; y < src->y; y++)
>	{
>		
>		for (x = 0; x < src->x; x++)
>		{
>			zptr = zbuf->data + (y * src->x + x);
>			zval = *zptr;
>			m = mar[zval >> NMASKS_SHIFT];
>			size = m->size;
>
>
>
>			fptr = ((float *) fdest->data) + (y * src->x + x) * 3;
>			sptr = (unsigned char *) src->data + ((y * src->x + x) * 3);
>			nptr = ((float *) norm->data) + (y * src->x + x);
>
>			rval = (float) sptr[0];
>			gval = (float) sptr[1];
>			bval = (float) sptr[2];
>
>			if (size == 0)
>			{
>				fptr[0] = rval;
>				fptr[1] = gval;
>				fptr[2] = bval;
>				*nptr += 1.0;
>				nptr++;
>				continue;
>			} 
>
>			/* dealing with the edges */
>			ex = src->x - x;
>			ey = src->y - y;
>
>			sx = (x < size) ? x : size;
>			sy = (y < size) ? y : size;
>			ex = (ex <= size) ? ex - 1 : size;
>			ey = (ey <= size) ? ey - 1 : size;
>
>			mrow = (size << 1) + 1;
>
>			mval = m->val + (size - sy) * mrow + size;
>
>
>			fptr -= sy * src->x * 3;
>			nptr -= sy * src->x;
>			/* test gamma */
>					g = sigma * (float) zval / 255.0;
>			if (g > 0.001)
>			{
>#define sig(x) (sigmo(0.3, x, g))
>					f = sig(0);
>					fac = 255.0 /(sig(1) - f);
>
>					rval = (sig(rval/255.0) - f) * fac;
>
>					gval = (sig(gval/255.0) - f) * fac;
>
>					bval = (sig(bval/255.0) - f) * fac;
>/*
>#undef sig(x)
>*/
>			}
>
>			/* end test */
>
>			for (my = -sy; my <= ey; my++)
>			{
>				for (mx = -sx; mx <= ex; mx++)
>				{
>					fac = mval[mx];
>					nptr[mx] += fac;
>					fptr[3 * mx] += fac * rval;
>					fptr[3 * mx + 1] += fac * gval;
>					fptr[3 * mx + 2] += fac * bval;
>				}
>				fptr += row;
>				nptr += src->x;
>				mval += mrow;
>			}
>		}
>		if(!(y%4)&&RE_local_test_break()) break;				
>	}
>
>	/* now renorm and convert the image back */
>	x = dest->size;
>	dptr = dest->data;
>	nptr = (float *) norm->data;
>	fptr = (float *) fdest->data;
>	while (x--)
>	{
>		fac = *nptr++;
>		if (fac != 0.0)
>		{
>			*dptr++ = *fptr++ / fac;
>			*dptr++ = *fptr++ / fac;
>			*dptr++ = *fptr++ / fac;
>		}
>		else
>		{	
>			*dptr++ = 0;
>			*dptr++ = 0;
>			*dptr++ = 0;
>			fptr +=3;
>		}
>	}
>	free_img(norm); free_img(fdest);
>	for (x= 0; x < NMASKS; x++)
>	{
>		free_mask(mar[x]);
>	}
>	MEM_freeN(mar);
>	return dest;
>}
>
>Image *zblur(Image *src, Image *zbuf, float radius)
>{
>	Image *dest;
>	Maskarray *mar;
>	float *mval;				/* mask value pointer */
>	float rval, gval, bval;			
>	float norm, fac;			
>	int tmp;
>	unsigned char *sptr, *dptr, *zptr;
>	int zval;
>	int size;
>	int row;
>	int mrow;
>	int x, y;
>	int i;
>	int sx, sy, ex, ey;
>	int mx, my;
>	Mask *m;
>
>	if(src->type != I_RGB)
>		return 0;
>
>	dest = alloc_img(src->x, src->y, I_RGB);
>	row = src->x * src->el_size;
>
>	mar = init_masks(NMASKS, radius);
>
>	for (y = 0; y < src->y  ; y++)
>	{
>		for (x = 0; x < src->x; x++)
>		{
>			dptr = (unsigned char *) dest->data + ((y * src->x + x) * src->el_size);
>			zptr = ((unsigned char *) zbuf->data) + (y * src->x + x);
>			zval = *zptr;
>			sptr = (unsigned char *) src->data + ((y *src->x + x )* src->el_size);
>
>			m = mar[zval >> NMASKS_SHIFT];
>
>			size = m->size;
>
>			if (zval == 0)
>			{
>				dptr[0] = sptr[0];
>				dptr[1] = sptr[1];
>				dptr[2] = sptr[2];
>				continue;
>			}
>
>			ex = src->x - x;
>			ey = src->y - y;
>
>			sx = (x < size) ? x : size;
>			sy = (y < size) ? y : size;
>			ex = (ex <= size) ? ex - 1: size;
>			ey = (ey <= size) ? ey - 1: size;
>
>			sptr -= sy *src->x * src->el_size;
>			zptr -= sy * src->x;
>			mrow = (size << 1) + 1;
>			mval = m->val + (size - sy) * mrow + size;
>
>			norm = rval = gval = bval = 0.0;
>	
>			for (my = -sy; my <= ey; my++)
>			{
>				for (mx = -sx; mx <= ex; mx++)
>				{
>					tmp = 3 * mx;
>					fac = mval[mx] * (float) zptr[mx] /255.0 ;
>					norm += fac;
>					rval += fac * sptr[tmp];
>					gval += fac * sptr[tmp + 1];
>					bval += fac * sptr[tmp + 2];
>				}
>				mval += mrow;
>				sptr += row;
>				zptr += src->x;
>			}
>			dptr[0] = rval / norm;
>			dptr[1] = gval / norm;
>			dptr[2] = bval / norm;
>		}
>		if(!(y%4)&&RE_local_test_break()) break;		
>	}
>	for (i= 0; i < NMASKS; i++)
>	{
>		free_mask(mar[i]);
>	}
>	MEM_freeN(mar);
>	return dest;
>}
>
>/* this splits the z-buffer into 2 gray-images (background, foreground)
> * which are used for the weighted blur */
>
>void zsplit(int *zptr, Image *fg, Image *bg, int zfocus, int zmax, int zmin, int x, int y)
>{
>	IMG_BYTE *p, *q;
>	int i, ix, iy;
>	float fdist;
>	float fgnorm, bgnorm;
>
>	p = fg->data;
>	q = bg->data;
>	bgnorm = 255.0 / ((float) zmax - (float) zmin);
>	fgnorm = 255.0 / ((float) zfocus - (float) zmin);
>
>	if (R.r.mode & R_FIELDS) {
>		for (iy=0; iy<y; iy++) {
>			for (ix=0; ix<x; ix++) {
>				fdist = (float) (*zptr++);
>				if (fdist < zmin) fdist = zmin;
>					
>				fdist -= zfocus;
>		
>				if (fdist < 0)
>				{
>					*p = (IMG_BYTE) (-fdist * fgnorm);
>					*q = 0;
>				}
>				else
>				{
>					*q = (IMG_BYTE) (fdist * bgnorm);
>					*p = 0;
>				}
>				p++, q++;			
>			}
>			iy++;
>			p+= x;
>			q+= x;
>		}
>	}
>	else
>	{
>		i = x * y;
>		while(i--)
>		{
>			fdist = (float) (*zptr++);
>			if (fdist < zmin) fdist = zmin;
>				
>			fdist -= zfocus;
>	
>			if (fdist < 0)
>			{
>				*p = (IMG_BYTE) (-fdist * fgnorm);
>				*q = 0;
>			}
>			else
>			{
>				*q = (IMG_BYTE) (fdist * bgnorm);
>				*p = 0;
>			}
>			p++, q++;
>		}
>	}
>}
>
>
>void rect2img(int *src, Image *dest, int x, int y)
>{
>	char *from;
>	IMG_BYTE *to;
>	int i, ix, iy;
>
>	from = (char *) src;
>	to = (IMG_BYTE *) dest->data;		
>
>	if (R.r.mode & R_FIELDS) {	/* double each scanline */
>		for (iy=0; iy<y; iy++) {
>			for (ix=0; ix<x; ix++) {
>				*to++ = from[0];
>				*to++ = from[1];
>				*to++ = from[2];
>				from += 4;
>			}
>			memcpy(to, to-3*x, 3*x);
>			to+= 3*x;
>			iy++;
>		}
>	}
>	else
>	{
>		i = x * y;
>		while(i--)
>		{
>			*to++ = from[0];
>			*to++ = from[1];
>			*to++ = from[2];
>			from += 4;
>		}
>	}
>}
>
>
>void img2rect(Image *src, int *dest)
>{
>	IMG_BYTE *from;
>	char *to;
>	int i, ix, iy;
>
>	from = (IMG_BYTE *) src->data;
>	to = (char *) dest;
>	
>	if (R.r.mode & R_FIELDS) {
>		for (iy=0; iy<src->y; iy++) {
>			for (ix=0; ix<src->x; ix++) {
>				*to++ = from[0];
>				*to++ = from[1];
>				*to++ = from[2];
>				*to++ = 255;
>				from += 3;
>			}
>			iy++;
>			from+= 3*src->x;
>		}	
>	}
>	else 
>	{
>		i = src->x * src->y;
>		while(i--)
>		{
>			*to++ = from[0];
>			*to++ = from[1];
>			*to++ = from[2];
>			*to++ = 255;
>			from += 3;
>		}
>	}
>}
>
>
>void add_zblur(void)
>{
>	Image *orig, *work, *zfront, *zback;
>	float fields;
>	int zfocus, newzfocus;
>	float zblurr;
>	int mode, x, y, fx, fy, zmin;
>	Camera *cam;
>
>	cam= G.scene->camera->data;
>	if ((cam->flag & CAM_DOF)==0) return;
>	
>	if (R.rectz == NULL) { printf("add_zblur: no rectz!\n"); return; }
>	
>	mode= R.r.mode;
>	R.r.mode &= ~R_PANORAMA;
>	R.xstart= -R.afmx; 
>	R.ystart= -R.afmy;
>	R.xend= R.xstart+R.rectx-1;
>	R.yend= R.ystart+R.recty-1;
>	
>	x= R.rectx;
>	y= R.recty;
>
>	zblurr= (cam->zblur*R.r.size)/100;
>	
>	if (R.r.mode & R_FIELDS) {
>		y *= 2;
>		zblurr *= 2;
>		fields= 0.5; 
>	} 
>	else 
>	{
>		fields= 1.0;	
>	}
>
>	zmin = (cam->zmin * (float) UINT_MAX + (float) INT_MIN) ;
>
>	RE_setwindowclip(1,-1); /*  geen jit:(-1) */
>	setzbufvlaggen(RE_projectverto);
>
>	zfront = alloc_img(x, y, I_GRAY);
>	zback = alloc_img(x, y, I_GRAY);
>	orig = alloc_img(x, y, I_RGB);
>
>	rect2img(R.rectot, orig, x, y);
>
>	if (cam->flag & CAM_AUTOFOCUS) {
>		fx= ((float)R.rectx + cam->focusx * (float)R.rectx)/2.0;
>		fy= ((float)R.recty + cam->focusy * (float)R.recty)/2.0;
>		CLAMP(fx, 0, R.rectx-1);
>		CLAMP(fy, 0, R.recty-1);
>		newzfocus= R.rectz[fy*R.rectx+fx];
>		if ((R.flag & R_ANIMRENDER) && (cam->zlag > 1.0)) {
>			zfocus= (int)((float)cam->oldzfocus + ((((float)newzfocus - (float)cam->oldzfocus)/cam->zlag) * fields));
>			cam->oldzfocus= zfocus;
>		} else {
>			zfocus= cam->oldzfocus= newzfocus;
>		}
>	}
>	else {
>		zfocus = (cam->focus * (float) UINT_MAX + (float) INT_MIN);
>	}
>
>	/* split up z buffer into 2 gray images	*/
>	zsplit(R.rectz, zfront, zback, zfocus, INT_MAX, zmin, x, y);	
>	gauss_blur(zback, 1.0);
>	gauss_blur(zfront, zblurr);
>	work = zblur(orig, zback, zblurr);
>	free_img(orig);
>	orig = zblur2(work, zfront, zblurr, cam->zsigma);
>	
>	if (cam->zgamma != 1.0)
>	{
>		zgamma(orig, zback, cam->zgamma);
>/*		zlight(orig, zfront, -1.0); */
>	}
>
>	img2rect(orig, R.rectot);
>	free_img(orig);
>	free_img(work); free_img(zfront); free_img(zback);
>
>	R.r.mode= mode;	
>}
>
>
>  
>
>------------------------------------------------------------------------
>
>/*
> * $Id: zblur.h,v 1.1.1.1 2003/01/18 05:40:38 intrr Exp $
> *
> * ***** BEGIN GPL/BL DUAL 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. The Blender
> * Foundation also sells licenses for use in proprietary software under
> * the Blender License.  See http://www.blender.org/BL/ for information
> * about this.
> *
> * 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.
> *
> * ***** END GPL/BL DUAL LICENSE BLOCK *****
> */
>
>#ifndef ZBLUR_H
>#define ZBLUR_H
>
>#ifdef __cplusplus
>extern "C" { 
>#endif
>
>/*-----------------------------------------------------------*/ 
>/* Includes                                                  */
>/*-----------------------------------------------------------*/ 
>
>/*-----------------------------------------------------------*/ 
>/* Function                                                  */
>/*-----------------------------------------------------------*/ 
>
>void add_zblur(void) 
>
>#ifdef __cplusplus
>}
>#endif
>
>#endif
>
>  
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Bf-committers mailing list
>Bf-committers at projects.blender.org
>http://projects.blender.org/mailman/listinfo/bf-committers
>  
>

-- 
Johnny Matthews
Network Administrator
Bradford National Bank
Greenville, IL



More information about the Bf-committers mailing list