sclass IIPureQuad extends Meta implements IIntegralImage { int imageSizeInBits; // e.g. 8 = 256*256 int[] pixelSumsByChannel; // average color of whole image stored as pixel sum IIntegralImage[] quadrants = new[4]; // in scan line order int imageSize() { ret 1 << imageSizeInBits; } int halfSize() { ret 1 << (imageSizeInBits-1); } public int getWidth() { ret imageSize(); } public int getHeight() { ret imageSize(); } int pixelCount() { ret 1 << (imageSizeInBits*2); } public double getIntegralValue(int x, int y, int channel) { int quadX = x >> (imageSizeInBits-1); // 0 or 1 int quadY = y >> (imageSizeInBits-1); // 0 or 1 int quadrant = quadX | (quadY << 1); // 0 to 3 int xLo = x & ~halfSize(); // clear topmost bit int yLo = y & ~halfSize(); // clear topmost bit // first query quadrant we landed in double pixelSum = quadrants[quadrant].getIntegralValue(xLo, yLo, channel); // add quadrant to the left if (quadX != 0) pixelSum += quadrants[quadrant-1].getIntegralValue(halfSize(), yLo, channel); // add quadrant above if (quadY != 0) pixelSum += quadrants[quadrant-2].getIntegralValue(xLo, halfSize(), channel); // add full top left quadrant if (quadrant == 3) pixelSum += quadrants[0].getIntegralValue(halfSize(), halfSize(), channel); ret pixelSum; } }