private static void calculateThresholdForBlock(byte[] luminances, int subWidth, int subHeight,
      int width, int height, int[][] blackPoints, BitMatrix matrix) {
    for (int y = 0; y < subHeight; y++) {
      int yoffset = y << 3;
      if ((yoffset + 8) >= height) {
        yoffset = height - 8;
      }
      for (int x = 0; x < subWidth; x++) {
        int xoffset = x << 3;
        if ((xoffset + 8) >= width) {
            xoffset = width - 8;
        }
        int left = (x > 1) ? x : 2;
        left = (left < subWidth - 2) ? left : subWidth - 3;
        int top = (y > 1) ? y : 2;
        top = (top < subHeight - 2) ? top : subHeight - 3;
        int sum = 0;
        for (int z = -2; z <= 2; z++) {
          int[] blackRow = blackPoints[top + z];
          sum += blackRow[left - 2];
          sum += blackRow[left - 1];
          sum += blackRow[left];
          sum += blackRow[left + 1];
          sum += blackRow[left + 2];
        }
        int average = sum / 25;
        threshold8x8Block(luminances, xoffset, yoffset, average, width, matrix);
      }
    }
  }