applyMaskPenaltyRule4 static method

int applyMaskPenaltyRule4(
  1. ByteMatrix matrix
)

Apply mask penalty rule 4 and return the penalty. Calculate the ratio of dark cells and give penalty if the ratio is far from 50%. It gives 10 penalty for 5% distance.

Implementation

static int applyMaskPenaltyRule4(ByteMatrix matrix) {
  int numDarkCells = 0;
  final array = matrix.bytes;
  final width = matrix.width;
  final height = matrix.height;
  for (int y = 0; y < height; y++) {
    final arrayY = array[y];
    for (int x = 0; x < width; x++) {
      if (arrayY[x] == 1) {
        numDarkCells++;
      }
    }
  }
  final numTotalCells = matrix.height * matrix.width;
  final fivePercentVariances =
      (numDarkCells * 2 - numTotalCells).abs() * 10 ~/ numTotalCells;
  return fivePercentVariances * _n4;
}