|
| 1 | +import processing.pdf.*; |
| 2 | + |
| 3 | +PImage src; |
| 4 | +PImage res; |
| 5 | + |
| 6 | + |
| 7 | +void setup() { |
| 8 | + src = loadImage("medusa.jpg"); |
| 9 | + res = createImage(src.width, src.height, RGB); |
| 10 | + size(src.width, src.height, JAVA2D); |
| 11 | + |
| 12 | + //smooth(); |
| 13 | + noLoop(); |
| 14 | + noStroke(); |
| 15 | + noSmooth(); |
| 16 | + //beginRecord(PDF, "canvas.pdf"); |
| 17 | +} |
| 18 | + |
| 19 | +void draw() { |
| 20 | + background(255,0,0); |
| 21 | + int s = 1; |
| 22 | + for (int x = 0; x < src.width; x+=s) { |
| 23 | + for (int y = 0; y < src.height; y+=s) { |
| 24 | + color oldpixel = src.get(x, y); |
| 25 | + color newpixel = findClosestColor( color ( red(oldpixel) + random(-64,64),green(oldpixel) + random(-64,64),blue(oldpixel) + random(-64,64) ) ); |
| 26 | + src.set(x, y, newpixel); |
| 27 | + |
| 28 | + stroke(newpixel); |
| 29 | + point(x,y); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +// Find closest colors in palette |
| 37 | +color findClosestColor(color in) { |
| 38 | + |
| 39 | + //Palette colors |
| 40 | + color[] palette = { |
| 41 | + color(0), |
| 42 | + color(255), |
| 43 | + color(255, 0, 0), |
| 44 | + color(0, 255, 0), |
| 45 | + color(0, 0, 255), |
| 46 | + color(255, 255, 0), |
| 47 | + color(0, 255, 255), |
| 48 | + color(255, 0, 255), |
| 49 | + color(0, 255, 255), |
| 50 | + }; |
| 51 | + |
| 52 | + |
| 53 | + PVector[] vpalette = new PVector[palette.length]; |
| 54 | + PVector vcolor = new PVector( (in >> 16 & 0xFF), (in >> 8 & 0xFF), (in & 0xFF)); |
| 55 | + int current = 0; |
| 56 | + float distance = vcolor.dist(new PVector(0, 0, 0)); |
| 57 | + |
| 58 | + for (int i=0; i<palette.length; i++) { |
| 59 | + // Using bit shifting in for loop is faster |
| 60 | + int r = (palette[i] >> 16 & 0xFF); |
| 61 | + int g = (palette[i] >> 8 & 0xFF); |
| 62 | + int b = (palette[i] & 0xFF); |
| 63 | + vpalette[i] = new PVector(r, g, b); |
| 64 | + float d = vcolor.dist(vpalette[i]); |
| 65 | + if (d < distance) { |
| 66 | + distance = d; |
| 67 | + current = i; |
| 68 | + } |
| 69 | + } |
| 70 | + return palette[current]; |
| 71 | +} |
| 72 | + |
0 commit comments