blur having no effect
in
Contributed Library Questions
•
1 year ago
If you run the script now then blur works,
however, look for
and delete that and uncomment the stuf below it.
Why has blur no effect? The rectangles should get smaller in the blurred area.
however, look for
- // here it blurs
image(img, 0, 0);
and delete that and uncomment the stuf below it.
Why has blur no effect? The rectangles should get smaller in the blurred area.
- import processing.pdf.*;
import controlP5.*;
ControlP5 cp5;
Accordion accordion;
PFont font;
String textValue = "";
int fontSize = 50;
int blur = 0;
int posterize;
int threshold = 50;
int step = 8;
int maxStrokeW = 5;
PGraphics img;
// - - - - - - - - - - - - - - - - - -
void setup() {
size(800, 600);
frame.setResizable(true);
font = createFont("arial", 20);
gui();
img = createGraphics(width, height, JAVA2D);
}
// - - - - - - - - - - - - - - - - - -
void gui() {
cp5 = new ControlP5(this);
Group g1 = cp5.addGroup("settings")
.setBackgroundColor(color(0, 64))
.setBackgroundHeight(200)
;
int y = 10;
cp5.addTextfield("input")
.setPosition(10, y)
.setSize(300, 40)
.setFont(font)
.setFocus(true)
.moveTo(g1)
;
cp5.addSlider("fontSize")
.setPosition(10, y+=60)
.setSize(100, 10)
.setRange(10, 500)
.setValue(50)
.moveTo(g1)
;
cp5.addSlider("blur")
.setPosition(10, y+=20)
.setSize(100, 10)
.setRange(0, 100)
.setValue(0)
.moveTo(g1)
;
cp5.addSlider("posterize")
.setPosition(10, y+=20)
.setSize(100, 10)
.setRange(2, 255)
.setValue(128)
.moveTo(g1)
;
cp5.addSlider("threshold")
.setPosition(10, y+=20)
.setSize(100, 10)
.setRange(0, 100)
.setValue(50)
.moveTo(g1)
;
cp5.addSlider("step")
.setPosition(10, y+=20)
.setSize(100, 10)
.setRange(1, 20)
.setValue(5)
.moveTo(g1)
;
cp5.addSlider("maxStrokeW")
.setPosition(10, y+=20)
.setSize(100, 10)
.setRange(1, 20)
.setValue(5)
.moveTo(g1)
;
accordion = cp5.addAccordion("acc")
.setPosition(0, 0)
.setWidth(350)
.addItem(g1)
;
cp5.mapKeyFor(new ControlKey() {public void keyEvent() {accordion.open(0);}}, 'o');
cp5.mapKeyFor(new ControlKey() {public void keyEvent() {accordion.close(0);}}, 'c');
accordion.setCollapseMode(ControlP5.ALL);
accordion.open(0);
}
// - - - - - - - - - - - - - - - - - -
/* broke? */
//public void input(String theText) {
// // automatically receives results from controller input
// println("a textfield event for controller 'input' : "+theText);
//}
// - - - - - - - - - - - - - - - - - -
void draw() {
background(255);
img.beginDraw();
img.background(255);
img.textFont(font);
img.textSize(fontSize);
img.fill(0);
img.textAlign(CENTER, CENTER);
img.text(cp5.get(Textfield.class,"input").getText(), width/2, height/2);
img.endDraw();
img.loadPixels();
int blurRadius = int(map(blur, 0, 100, 0, min(img.width, img.height)-1));
println(blurRadius);
fastblur(img, blurRadius);
// here it blurs
image(img, 0, 0);
// delete above line and uncomment from here
/*
img.filter(POSTERIZE, posterize);
float thresholdVal = norm(threshold, 0, 100);
img.filter(THRESHOLD, thresholdVal);
img.loadPixels();
//squareStyle(img);
img.updatePixels();
image(img, 0, 0);
*/
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void squareStyle(PImage img) {
fill(0);
noStroke();
for(int y=0; y < img.height; y += step) {
for(int x=0; x < img.width; x += step) {
color col= img.get(x,y);
float r = red(col);
float g = green(col);
float b = blue(col);
float bright = brightness(col);
float strokeW = map(bright, 0, 255, maxStrokeW, 0);
if(strokeW > 0) {
// fill(r,g,b);
//strokeWeight(strokeW);
//line(x, y, x,y+step+1);
rect(x, y, strokeW, strokeW);
}
}
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void fastblur(PImage img,int radius) {
if (radius<1) {
return;
}
int w=img.width;
int h=img.height;
int wm=w-1;
int hm=h-1;
int wh=w*h;
int div=radius+radius+1;
int r[]=new int[wh];
int g[]=new int[wh];
int b[]=new int[wh];
int rsum,gsum,bsum,x,y,i,p,p1,p2,yp,yi,yw;
int vmin[] = new int[max(w,h)];
int vmax[] = new int[max(w,h)];
int[] pix=img.pixels;
int dv[]=new int[256*div];
for (i=0;i<256*div;i++) {
dv[i]=(i/div);
}
yw=yi=0;
for (y=0;y<h;y++) {
rsum=gsum=bsum=0;
for(i=-radius;i<=radius;i++) {
p=pix[yi+min(wm,max(i,0))];
rsum+=(p & 0xff0000)>>16;
gsum+=(p & 0x00ff00)>>8;
bsum+= p & 0x0000ff;
}
for (x=0;x<w;x++) {
r[yi]=dv[rsum];
g[yi]=dv[gsum];
b[yi]=dv[bsum];
if(y==0) {
vmin[x]=min(x+radius+1,wm);
vmax[x]=max(x-radius,0);
}
p1=pix[yw+vmin[x]];
p2=pix[yw+vmax[x]];
rsum+=((p1 & 0xff0000)-(p2 & 0xff0000))>>16;
gsum+=((p1 & 0x00ff00)-(p2 & 0x00ff00))>>8;
bsum+= (p1 & 0x0000ff)-(p2 & 0x0000ff);
yi++;
}
yw+=w;
}
for (x=0;x<w;x++) {
rsum=gsum=bsum=0;
yp=-radius*w;
for(i=-radius;i<=radius;i++) {
yi=max(0,yp)+x;
rsum+=r[yi];
gsum+=g[yi];
bsum+=b[yi];
yp+=w;
}
yi=x;
for (y=0;y<h;y++) {
pix[yi]=0xff000000 | (dv[rsum]<<16) | (dv[gsum]<<8) | dv[bsum];
if(x==0) {
vmin[y]=min(y+radius+1,hm)*w;
vmax[y]=max(y-radius,0)*w;
}
p1=x+vmin[y];
p2=x+vmax[y];
rsum+=r[p1]-r[p2];
gsum+=g[p1]-g[p2];
bsum+=b[p1]-b[p2];
yi+=w;
}
}
}
1