static int bwContrastQuadtree_minOfArea(BWContrastQuadTree qt, int originalSize, Rect rect) { ret bwContrastQuadtree_minOfArea(qt, Rect(0, 0, originalSize, originalSize), rect); } static int bwContrastQuadtree_minOfArea(BWContrastQuadTree qt, Rect block, Rect rect) { Rect r2 = intersectRects(rect, block); if (r2.empty()) ret 255; // simple case 1: no intersection print(rect + " => " + r2); // simple case 2: whole block if (eq(r2, block)) ret qt.min(); // simple case 3: block is one color if (!(qt instanceof BWContrastQuadTree.Composite)) ret qt.min(); // complex case: process subregions BWContrastQuadTree.Composite qt2 = cast qt; int w = block.w, r = w/2; ret intMin( bwContrastQuadtree_minOfArea(qt2.a, Rect(block.x, block.y, r, r), r2), bwContrastQuadtree_minOfArea(qt2.b, Rect(block.x+r, block.y, r, r), r2), bwContrastQuadtree_minOfArea(qt2.c, Rect(block.x, block.y+r, r, r), r2), bwContrastQuadtree_minOfArea(qt2.d, Rect(block.x+r, block.y+r, r, r), r2)); }