applyMaskPenaltyRule2 static method

int applyMaskPenaltyRule2(
  1. ByteMatrix matrix
)

Apply mask penalty rule 2 and return the penalty. Find 2x2 blocks with the same color and give penalty to them. This is actually equivalent to the spec's rule, which is to find MxN blocks and give a penalty proportional to (M-1)x(N-1), because this is the number of 2x2 blocks inside such a block.

Implementation

static int applyMaskPenaltyRule2(ByteMatrix matrix) {
  int penalty = 0;
  final array = matrix.bytes;
  final width = matrix.width;
  final height = matrix.height;
  for (int y = 0; y < height - 1; y++) {
    final arrayY = array[y];
    for (int x = 0; x < width - 1; x++) {
      final value = arrayY[x];
      if (value == arrayY[x + 1] &&
          value == array[y + 1][x] &&
          value == array[y + 1][x + 1]) {
        penalty++;
      }
    }
  }
  return _n2 * penalty;
}