// Fast Gaussian Blur w/ Javascript Communication // by Mario Klingemann // First this is a pretty fast implementation of a Gaussian Blur. // Second it's a demonstration of Javascript to P5 communication. // In this demonstration the slider that controls the blur // radius sits in a Flash file on the same webpage. // Created 22 February 2003 Convolver c; BImage a; BImage b; boolean changed; boolean processing; void setup() { processing=true; a=loadImage("mastermind_small.jpg"); size(a.width, a.height); b=new BImage(a.width, a.height); noFill(); stroke(255); framerate(15); c=new Convolver(1); changed=false; c.setRadius(0); process(); } void loop(){ if (changed){ image(b, 0, 0); changed=false; processing=false; } } void process() { System.arraycopy(a.pixels,0,b.pixels,0,a.pixels.length); c.blur(b,0,0,width,height); changed=true; } public boolean setBlurRadius(int r){ if (!processing){ processing=true; c.setRadius(r); process(); return true; } else { return false; } } 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>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=x && read=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; } } }