static Rect selectRandomPartContaining(RGBImage img, Rect r, int w, int h) { int maxX = min(r.x, img.w()-w), maxY = min(r.y, img.h()-h); int minX = max(r.x2()-w, 0), minY = max(r.y2()-h, 0); if (minX > maxX || minY > maxY) fail("selectRandomPartContaining fail " + struct(ll(r, img.w(), img.h(), w, h, minX, maxX, minY, maxY))); ret new Rect(random(minX, maxX+1), random(minY, maxY+1), w, h); }