Somewhat simplified black/white balance penalty calculation in all language versions.

This commit is contained in:
Project Nayuki
2018-08-26 03:20:12 +00:00
parent 7d7a9b4820
commit 5073db4487
7 changed files with 28 additions and 26 deletions
+4 -3
View File
@@ -485,10 +485,11 @@ long QrCode::getPenaltyScore() const {
black++;
}
}
// Note that size is odd, so black/total != 1/2
int total = size * size;
// Find smallest k such that (45-5k)% <= dark/total <= (55+5k)%
for (int k = 0; black*20L < (9L-k)*total || black*20L > (11L+k)*total; k++)
result += PENALTY_N4;
// Compute the smallest integer k >= 0 such that (45-5k)% <= black/total <= (55+5k)%
int k = static_cast<int>((std::abs(black * 20L - total * 10L) + total - 1) / total) - 1;
result += k * PENALTY_N4;
return result;
}