// BoxFit02 (colored)
// David Bollinger, July 2006
// http://www.davebollinger.com
// for Processing 0109 beta
// (0115 has trouble loading fasa300.jpg from applet)
//
/**
BoxFit02 - "ColoredBoxFitter" (derived class)
Assigns color to areas based on an underlying texture.
Click to advance early to next pattern.
*/
ColoredBoxFitter fitter;
int currentSeed = 0;
int nextwait = 0;
PImage texture;
void setup() {
size(300,300,P3D);
texture = loadImage("fasa300.jpg");
fitter = new ColoredBoxFitter(texture,4,4,8);
framerate(30);
next();
}
void next() {
background(color(255,255,255));
fitter.make(++currentSeed);
}
void draw() {
if (nextwait > 0) {
if (--nextwait <= 0) {
next();
}
} else {
fitter.drawone();
if (fitter.at00())
nextwait = 5*30;
}
}
void keyPressed() {
if (key=='`') saveFrame("frame.tga");
}
void mousePressed() {
next();
}
class ColoredBoxFitter extends BoxFitter{
PImage texture;
ColoredBoxFitter(PImage _texture, int _divx, int _divy, int _maxsizer) {
super(_texture.width, _texture.height, _divx, _divy, _maxsizer);
texture = _texture;
}
// get the representative color from texture for specified region
int getRegionColor(int x1, int y1, int wid, int hei) {
// "easiest" method would be to just return center pixel...
// return texture.get(x1+wid/2, y1+hei/2);
//
// "best" (?) method would be to get() the entire area and average ALL pixels
// [overkill]
//
// the "compromise" adopted is to average the 4 pixels at center of quads
color q1 = texture.get(x1+wid/4, y1+hei/4);
color q2 = texture.get(x1+wid*3/4, y1+hei/4);
color q3 = texture.get(x1+wid/4, y1+hei*3/4);
color q4 = texture.get(x1+wid*3/4, y1+hei*3/4);
float r = (red(q1)+red(q2)+red(q3)+red(q4))/4.0;
float g = (green(q1)+green(q2)+green(q3)+green(q4))/4.0;
float b = (blue(q1)+blue(q2)+blue(q3)+blue(q4))/4.0;
return color(r,g,b);
}
void render(int x1, int y1, int w, int h) {
color c = getRegionColor(x1,y1,w,h);
fill(c);
stroke(#000000);
rect(x1,y1,w,h);
}
}