Warning: session_start(): open(/var/lib/php/sessions/sess_aqlea4gd4kov33407q7smoa3f7, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
sclass BWImage_FastRegions {
BWImage image;
int w, h, size, runner;
new IntBuffer stack; // locations as y*w+x
int[] regionMatrix;
int regionCounter;
bool verbose;
double regionStep = .1; // for rendering in regionsImage
int x(int pos) { ret pos % w; }
int y(int pos) { ret pos / w; }
int getColor(int pos) { ret image.getInt(x(pos), y(pos)); }
run {
w = image.getWidth(); h = image.getHeight();
size = w*h;
while (runner < size) {
if (regionMatrix[runner] == 0) {
// make a new region, get color
int region = ++regionCounter;
stack.add(runner);
int color = getColor(runner);
// flood-fill region
while (nempty(stack)) {
int pos = stack.popLast();
if (regionMatrix[pos] != 0) continue; // touching myself (or someone else)
if (getColor(pos) != color) continue; // wrong color
// new pixel found, mark as ours
regionMatrix[pos] = region;
// explore neighborhood
int x = x(pos), y = y(pos);
if (x > 0) stack.add(pos-1);
if (x < w) stack.add(pos+1);
if (y > 0) stack.add(pos-w);
if (y < h) stack.add(pos+w);
}
}
++runner;
}
}
IBWImage regionsImage() {
ret iBWImageFromFunction((x, y) -> {
var region = regionMatrix[x, y];
ret ((region-1)*regionStep) % (1.0+regionStep-0.0001);
}, w, h);
}
}