private static void calculateThresholdForBlock(byte[] luminances, int subWidth, int subHeight, int stride, int[][] blackPoints, BitMatrix matrix) { for (int y = 0; y < subHeight; y++) { for (int x = 0; x < subWidth; x++) { 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, x << 3, y << 3, average, stride, matrix); } } }