/* * 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 */ String fileName = "beth_dog_small"; String fileExt = ".jpg"; PImage source; int lineSpace; int fiddle; float bright; boolean render; HScrollbar[] scrolls; HScrollbar lines; float[][] palette = { {.15, #01253D}, {.25, #E7000A}, {.5, #4F919F}, {1,#FBDE8E} }; //for speed later, so we don't have to do lots of conversions color[] colors;// = { #01253D, #E7000A, #4F919F,#FBDE8E}; void setup(){ //size(500,375); source = loadImage(fileName + fileExt); //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); smooth(); lineSpace = 10; fiddle = lineSpace/2; render = false; debug("plength = " + palette.length); colors = new color[palette.length]; scrolls = new HScrollbar[palette.length]; for(int i = 0; i < palette.length; i++){ colors[i] = color(int(palette[i][1])); scrolls[i] = new HScrollbar(0,40 +20 * i, width, 10, 3*5+1); scrolls[i].spos = palette[i][0] * width; scrolls[i].newspos = scrolls[i].spos; scrolls[i].overColor = colors[i]; debug("color " + i + ": " + hex(colors[i])); } noStroke(); lines = new HScrollbar(0, 20, width, 10, 3*5+1); } void keyPressed(){ debug("pressed " + key); } color randomColor(){ int r = int(random(255)); int g = int(random(255)); int b = int(random(255)); int a = 200; return color(r,g,b,a); } void updateScrolls(){ lines.update(); lineSpace = max(1, int(20* lines.getPos()/width)); fiddle = lineSpace/2; for(int i = 0; i < scrolls.length; i++){ scrolls[i].update(); palette[i][0] = scrolls[i].spos/width; } } void displayScrolls(){ lines.display(); for(int i = 0; i < scrolls.length; i++){ scrolls[i].display(); } } boolean overScrolls(){ if(lines.over){ return true; } for(int i = 0; i < scrolls.length; i++){ if(scrolls[i].over){return true;} } return false; } void draw(){ updateScrolls(); if(overScrolls()){ displayScrolls(); } else if(mousePressed ){ //darkValLimit = float(mouseX)/width; //lineSpace = max(1,int(20*float(mouseY)/height)); debug("mouse pressed, x = " + mouseX + ", y = "+ mouseY); debug("lineSpace = " + lineSpace); //debug("darkValLimit = " + darkValLimit); render = true; } if(render){ int h = height; int w = width; for(int y = lineSpace/2; y < h; y = y + lineSpace){ for(int x = 0; x < w; x++){ drawLine(x,y); } } render = false; //this is good to uncomment if you are in a non-web environment saveFrame(fileName + "-####.jpg"); } } void drawLine(int x, int y){ bright = getAverageAmbientBrightness(x,y); int plength = palette.length; float brightLimit; for(int i = 0; i < plength; i++){ brightLimit = palette[i][0]; if( bright <= brightLimit){ stroke(colors[i]); line(x, y + fiddle, x, y - fiddle); return; } } } void drawLine(int x, int y, color top, color bottom){ stroke(bottom); line(x, y, x, int(y+ lineSpace/2)); stroke(top); line(x,y,x,int(y-lineSpace/2)); } 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)); } return ( (bright/lineSpace))/255; }