!7 static int levels = 8; static Color bgColor = Color.red; sbool showBorders = true; sclass Element {} Element > SingleColor { int color; } Element > Quad { Element[] subs; } Element > Abort {} p { RGBImage img = rgbShootScreen(); Element e = split(img, 0, 0, img.w(), img.h(), 1, levels); //printStructure(e); showImage(reconstruct(img.w(), img.h(), e)); } static Element split(RGBImage img, int x1, int y1, int w, int h, int level, int maxLevel) { if (rgbAllOneColor(img, x1, y1, w, h)) { int color = img.getInt(x1, y1); //print("one color: " + color); ret nu SingleColor(+color; } if (level >= maxLevel) { //print("abort"); ret nu Abort; } else { //print("split"); int x = x1+(w+1)/2, y = y1+(h+1)/2; Element[] subs = { split(img, x1, y1, x-x1, y-y1, level+1, maxLevel), split(img, x, y1, x1+w-x, y-y1, level+1, maxLevel), split(img, x1, y, x-x1, y1+h-y, level+1, maxLevel), split(img, x, y, x1+w-x, y1+h-y, level+1, maxLevel) }; ret nu Quad(+subs; } } static RGBImage reconstruct(int w, int h, Element e) { RGBImage img = new RGBImage(w, h, bgColor); reconstruct(img, 0, 0, w, h, e); ret img; } svoid reconstruct(RGBImage img, int x1, int y1, int w, int h, Element e) { if (w <= 0 || h <= 0) ret; if (e << SingleColor) { if (showBorders) rgbFill(img, x1, y1, w-1, h-1, e/SingleColor.color); else rgbFill(img, x1, y1, w, h, e/SingleColor.color); ret; } if (e << Quad) { Element[] subs = e/Quad.subs; int x = x1+(w+1)/2, y = y1+(h+1)/2; reconstruct(img, x1, y1, x-x1, y-y1, subs[0]); reconstruct(img, x, y1, x1+w-x, y-y1, subs[1]); reconstruct(img, x1, y, x-x1, y1+h-y, subs[2]); reconstruct(img, x, y, x1+w-x, y1+h-y, subs[3]); } }