class Texture { PApplet applet; PImage img; Texture(PApplet applet, String filename) { this.applet = applet; img = loadImage(filename); if ((img.width!=256) || (img.height!=256)) { applet.image(img, 0, 0, 256, 256); img = applet.get(0, 0, 256, 256); } } int get(int x, int y) { return img.get(x,y); } void draw() { image(img, 0, 0); } } class Curve { PApplet applet; int [] cxs; int [] cys; int [] xs; int [] ys; int cindex; Curve(PApplet applet) { this.applet = applet; cxs = new int[4]; cys = new int[4]; xs = new int[256]; ys = new int[256]; cindex = 0; // start with a demo curve predefined int [] democxs = { 87, 192, 70, 13 }; int [] democys = { 42, 94, 209, 156 }; for (int i=0; i<4; i++) { cxs[i] = democxs[i]; cys[i] = democys[i]; } rebuild(); } void set(int x, int y) { cxs[cindex] = x; cys[cindex] = y; if (++cindex > 3) cindex = 0; rebuild(); } void rebuild() { for (int i=0; i<85; i++) { float t = (float)(i) / 85f; xs[i] = (int)curvePoint(cxs[0], cxs[0], cxs[1], cxs[2], t); ys[i] = (int)curvePoint(cys[0], cys[0], cys[1], cys[2], t); xs[85+i] = (int)curvePoint(cxs[0], cxs[1], cxs[2], cxs[3], t); ys[85+i] = (int)curvePoint(cys[0], cys[1], cys[2], cys[3], t); xs[170+i] = (int)curvePoint(cxs[1], cxs[2], cxs[3], cxs[3], t); ys[170+i] = (int)curvePoint(cys[1], cys[2], cys[3], cys[3], t); } } void draw(int xo, int yo) { fill(0,128); noStroke(); for (int i=0; i<256; i++) { int x = xo + xs[i]; int y = yo + ys[i]; rect(x,y,1,1); } for (int i=0; i<4; i++) { stroke( ((i==cindex) ? 255 : 0), 128 ); fill( ((i==cindex) ? 0 : 255), 128 ); int x = xo + cxs[i]; int y = yo + cys[i]; if (i==cindex) rect(x-4, y-4, 8, 8); else rect(x-2, y-2, 4, 4); } } } class Palette { PApplet applet; int [] colors; Palette(PApplet applet) { this.applet = applet; colors = new int[256]; } int get(int i) { return colors[i]; } void set(int i, int c) { colors[i] = c; } void draw(int x, int y, int w, int h) { noStroke(); for (int i=0; i<256; i++) { fill(colors[i]); rect(x+i*w, y, w, h); } } void load(String filename) { PImage temp = applet.loadImage(filename); for (int i=0; i<256; i++) colors[i] = temp.get(i, 0); } void save(String filename, int x, int y, int w, int h) { PImage temp = applet.get(x, y, w, h); temp.save(applet.savePath(filename)); } void scrape(Texture tex, Curve crv) { for (int i=0; i<256; i++) colors[i] = tex.get(crv.xs[i], crv.ys[i]); } void blur() { int [] old = new int[256]; for (int i=0; i<256; i++) old[i] = colors[i]; for (int i=0; i<256; i++) { // retrieve int cm2 = old[ (i<2) ? i : i-2 ]; int cm1 = old[ (i<1) ? i : i-1 ]; int c00 = old[ i ]; int cp1 = old[ (i>254) ? i : i+1 ]; int cp2 = old[ (i>253) ? i : i+2 ]; // filter int r = ((cm2>>16&0xFF) + (cm1>>16&0xFF)*3 + (c00>>16&0xFF)*8 + (cp1>>16&0xFF)*3 + (cp2>>16&0xFF)) >> 4; int g = ((cm2>>8&0xFF) + (cm1>>8&0xFF)*3 + (c00>>8&0xFF)*8 + (cp1>>8&0xFF)*3 + (cp2>>8&0xFF)) >> 4; int b = ((cm2&0xFF) + (cm1&0xFF)*3 + (c00&0xFF)*8 + (cp1&0xFF)*3 + (cp2&0xFF)) >> 4; // store colors[i] = (0xFF000000) | (r<<16) | (g<<8) | (b); } } }