/* * Engraver * by Matt Katz * Simulates a verrrrry basic engraving based on brightness values. * click with the mouse on the canvas to try engraving * mouse x controls the cuttoff after which we stop engraving * mouse y controls the thickness of our engraving blade */ PImage source; int lineSpace; int fiddle; float darkValLimit; boolean render; String fileName; int maxLineSpace; void setup(){ fileName = "smilinsam"; //size(500,375); source = loadImage(fileName+".jpg"); //comment out the previous size and uncomment this //if you are playing with various photos in //processing ide size(source.width, source.height); background(source); //background(255); smooth(); //lineSpace = 10; maxLineSpace = 40; //fiddle = lineSpace/2; stroke(100); darkValLimit = 0; render = false; } void keyPressed(){ println("pressed " + key); if(key == 'g'){ render = !render; if(render){background(255);}else{background(source);} println("render is " + render); } } color randomDark(){ return randomColor(0,125); } color randomLight(){ return randomColor(126,255); } color randomColor(){ return randomColor(0,255); } color randomColor(int low, int high){ int seed = high -low; int r = low + int(random(seed)); int g = low + int(random(seed)); int b = low + int(random(seed)); //int a = 200; return color(r,g,b); } void draw(){ if(mousePressed){ //background(255); darkValLimit = float(mouseX)/width; lineSpace = max(1,int(maxLineSpace*float(mouseY)/height)); fiddle = lineSpace/2; println("mouse pressed, x = " + mouseX + ", y = "+ mouseY); println("lineSpace = " + lineSpace); println("darkValLimit = " + darkValLimit); render = true; } if(render){ //background(255); int h = height; int w = width; PGraphics buf = createGraphics(w,h,P3D); PImage i; float darkVal = 0; buf.beginDraw(); buf.background(randomLight()); buf.stroke(randomDark()); for(int y = lineSpace/2; y < h; y = y + lineSpace){ for(int x = 0; x < w; x++){ //println("x = " + x + ", y =" + y); //get the brightness at this pixel in the picture darkVal = getAverageAmbientBrightness(x,y);   // float darkVal = 255 - brightVal; if(darkVal > darkValLimit){ buf.line(x,y + int( lineSpace * darkVal)/2 ,x, y - int( lineSpace * darkVal)/2); } } } buf.endDraw(); i = buf.get(0,0,w,h); background(0); image(i,0,0); render = false; //this is good to uncomment if you are in a non-web environment saveFrame(fileName+"-####.jpg"); } } float getAverageAmbientBrightness(int x, int y){ float bright = 0; //don't ever optimize unless you notice slowdown //I noticed slowdown int lim = y + fiddle; for(int curY = y - fiddle; curY < lim; curY++){ bright += brightness(source.get(x,curY)); } //actually, let's return darkness... return ( 255 -(bright/lineSpace))/255; }