meta-for IBWIntegralImage in IBWIntegralImage, BWIntegralImage { // clip is in output coordinates static BufferedImage scaledGrayImageFromBWIntegralImage_clip(IBWIntegralImage img, double scalingFactor, Rect clip) { int w = clip.w, h = clip.h; int w1 = img.getWidth(), h1 = img.getHeight(); byte[] pixels = new[w*h]; int i = 0; int xx2 = clip.x2(), yy2 = clip.y2(); for (int y = clip.y; y < yy2; y++) for (int x = clip.x; x < xx2; x++) { // TODO: optimize int x1 = ifloor(x/scalingFactor), x2 = ifloor(max(x1+1, (x+1)/scalingFactor)); int y1 = ifloor(y/scalingFactor), y2 = ifloor(max(y1+1, (y+1)/scalingFactor)); int pixel = iround(img.getPixelAverage(x1, y1, x2, y2)); pixels[i++] = clampToUByte(pixel); } ret newGrayBufferedImage(w, h, pixels); } }