sclass BWIntegralImage { int w, h; int[] data; // 1 entry per pixel *() {} *(BufferedImage img) { this(BWImage(img)); } *(BWImage img) { w = img.w(); h = img.h(); data = new int[w*h]; int i = 0; for y to h: { int sum = 0; for x to w: { sum += img.getByte(x, y) & 0xFF; data[i] = y > 0 ? sum + data[i-w] : sum; i++; } } } // pixels outside of image are considered black int get(int x, int y) { ret x < 0 || y < 0 ? 0 : data[min(y, h-1)*w+min(x, w-1)]; } }