We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Syntax update (gaussian blur)
Page Index Toggle Pages: 1
Syntax update (gaussian blur) (Read 595 times)
Syntax update (gaussian blur)
Nov 12th, 2007, 7:04pm
 
Hi,

I need a little help with getting some old code to work. I solved the errors quite easily, now it gives none, but I also get a blank screen.

I'm trying to build and edge detection thingie that I can later use to build an image out of boxes in a close to optimal way. Anyway, here's the blur part.

Code:


// Fast Gaussian Blur v1.3
// by Mario Klingemann <http://incubator.quasimondo.com>

Convolver c;
PImage a;
PImage b;


void setup(){
a=loadImage("trees.jpg");
size(a.width, a.height);
b=createImage(a.width, a.height, RGB);
image(b, 0, 0);
noStroke();
frameRate(30);
c=new Convolver(1);
}

void draw(){
c.setRadius(mouseY>>4);
System.arraycopy(a.pixels,0,b.pixels,0,a.pixels.length);
c.blur(b,mouseX-30,0,60,height);
image(b, 0, 0);
}



class Convolver{
int radius;
int kernelSize;
int[] kernel;
int[][] mult;

Convolver(int sz){
this.setRadius(sz);
}

void setRadius(int sz){
int i,j;
sz=min(max(1,sz),248);
if (radius==sz) return;
kernelSize=1+sz*2;
radius=sz;
kernel= new int[1+sz*2];
mult=new int[1+sz*2][256];

int sum=0;
for (i=1;i<sz;i++){
int szi=sz-i;
kernel[sz+i]=kernel[szi]=szi*szi;
sum+=kernel[szi]+kernel[szi];
for (j=0;j<256;j++){
mult[sz+i][j]=mult[szi][j]=kernel[szi]*j;
}
}
kernel[sz]=sz*sz;
sum+=kernel[sz];
for (j=0;j<256;j++){
mult[sz][j]=kernel[sz]*j;
}
}



void blur(PImage img, int x, int y, int w, int h){
int sum,cr,cg,cb,k;
int pixel,read,i,ri,xl,yl,yi,ym,riw;
int[] pix=img.pixels;
int iw=img.width;

int wh=iw*img.height;

int r[]=new int[wh];
int g[]=new int[wh];
int b[]=new int[wh];

for (i=0;i<wh;i++){
ri=pix[i];
r[i]=(ri&0xff0000)>>16;
g[i]=(ri&0x00ff00)>>8;
b[i]=(ri&0x0000ff);
}

int r2[]=new int[wh];
int g2[]=new int[wh];
int b2[]=new int[wh];

x=max(0,x);
y=max(0,y);
w=x+w-max(0,(x+w)-iw);
h=y+h-max(0,(y+h)-img.height);
yi=y*iw;

for (yl=y;yl<h;yl++){
for (xl=x;xl<w;xl++){
cb=cg=cr=sum=0;
ri=xl-radius;
for (i=0;i<kernelSize;i++){
read=ri+i;
if (read>=x && read<w){
read+=yi;
cr+=mult[i][r[read]];
cg+=mult[i][g[read]];
cb+=mult[i][b[read]];
sum+=kernel[i];
}
}
ri=yi+xl;
r2[ri]=cr/sum;
g2[ri]=cg/sum;
b2[ri]=cb/sum;
}
yi+=iw;
}
yi=y*iw;

for (yl=y;yl<h;yl++){
ym=yl-radius;
riw=ym*iw;
for (xl=x;xl<w;xl++){
cb=cg=cr=sum=0;
ri=ym;
read=xl+riw;
for (i=0;i<kernelSize;i++){
if (ri<h && ri>=y){
cr+=mult[i][r2[read]];
cg+=mult[i][g2[read]];
cb+=mult[i][b2[read]];
sum+=kernel[i];
}
ri++;
read+=iw;
}
pix[xl+yi]=0xff000000 | (cr/sum)<<16 | (cg/sum)<<8 | (cb/sum);
}
yi+=iw;
}
}
}


Re: Syntax update (gaussian blur)
Reply #1 - Nov 15th, 2007, 10:54pm
 
purty please ?

love, cosmin
Re: Syntax update (gaussian blur)
Reply #2 - Nov 15th, 2007, 11:04pm
 
You probably need some img.loadpixels() and img.updatePixels() in there in various places.
Page Index Toggle Pages: 1