|
|
|
@ -542,7 +542,7 @@ public final class QrCode {
|
|
|
|
|
// The function modules must be marked and the codeword bits must be drawn
|
|
|
|
|
// before masking. Due to the arithmetic of XOR, calling applyMask() with
|
|
|
|
|
// the same mask value a second time will undo the mask. A final well-formed
|
|
|
|
|
// QR Code needs exactly one (not zero, two, etc.) mask applied. switch-statement refactoring
|
|
|
|
|
// QR Code needs exactly one (not zero, two, etc.) mask applied. Command pattern
|
|
|
|
|
private void applyMask(int msk) {
|
|
|
|
|
if (msk < 0 || msk > 7)
|
|
|
|
|
throw new IllegalArgumentException("Mask value out of range");
|
|
|
|
@ -571,6 +571,15 @@ public final class QrCode {
|
|
|
|
|
// This method applies and returns the actual mask chosen, from 0 to 7.
|
|
|
|
|
private int handleConstructorMasking(int msk) {
|
|
|
|
|
if (msk == -1) { // Automatically choose best mask
|
|
|
|
|
msk = chooseBestMask(msk);
|
|
|
|
|
}
|
|
|
|
|
assert 0 <= msk && msk <= 7;
|
|
|
|
|
applyMask(msk); // Apply the final choice of mask
|
|
|
|
|
drawFormatBits(msk); // Overwrite old format bits
|
|
|
|
|
return msk; // The caller shall assign this value to the final-declared field
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int chooseBestMask(int msk) {
|
|
|
|
|
int minPenalty = Integer.MAX_VALUE;
|
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
|
applyMask(i);
|
|
|
|
@ -582,17 +591,46 @@ public final class QrCode {
|
|
|
|
|
}
|
|
|
|
|
applyMask(i); // Undoes the mask due to XOR
|
|
|
|
|
}
|
|
|
|
|
return msk;
|
|
|
|
|
}
|
|
|
|
|
assert 0 <= msk && msk <= 7;
|
|
|
|
|
applyMask(msk); // Apply the final choice of mask
|
|
|
|
|
drawFormatBits(msk); // Overwrite old format bits
|
|
|
|
|
return msk; // The caller shall assign this value to the final-declared field
|
|
|
|
|
|
|
|
|
|
private int havingSameColor(int run, boolean runColor, int[] runHistory, int result, int y, int x) {
|
|
|
|
|
if (modules[y][x] == runColor) {
|
|
|
|
|
run++;
|
|
|
|
|
if (run == 5)
|
|
|
|
|
result += PENALTY_N1;
|
|
|
|
|
else if (run > 5)
|
|
|
|
|
result++;
|
|
|
|
|
} else {
|
|
|
|
|
finderPenaltyAddHistory(run, runHistory);
|
|
|
|
|
if (!runColor)
|
|
|
|
|
result += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
|
|
|
|
|
runColor = modules[y][x];
|
|
|
|
|
run = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int twobytwoHavingSameColor(boolean[][] modules) {
|
|
|
|
|
int result = 0;
|
|
|
|
|
// 2*2 blocks of modules having same color.
|
|
|
|
|
for (int y = 0; y < size - 1; y++) {
|
|
|
|
|
for (int x = 0; x < size - 1; x++) {
|
|
|
|
|
boolean color = modules[y][x];
|
|
|
|
|
if ( color == modules[y][x + 1] &&
|
|
|
|
|
color == modules[y + 1][x] &&
|
|
|
|
|
color == modules[y + 1][x + 1])
|
|
|
|
|
result += PENALTY_N2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculates and returns the penalty score based on state of this QR Code's current modules.
|
|
|
|
|
// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score. duplication code extract
|
|
|
|
|
// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
|
|
|
|
|
private int getPenaltyScore() {
|
|
|
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
|
|
// Adjacent modules in row having same color, and finder-like patterns
|
|
|
|
@ -600,21 +638,8 @@ public final class QrCode {
|
|
|
|
|
boolean runColor = false;
|
|
|
|
|
int runX = 0;
|
|
|
|
|
int[] runHistory = new int[7];
|
|
|
|
|
result += havingSameColor();
|
|
|
|
|
for (int x = 0; x < size; x++) {
|
|
|
|
|
if (modules[y][x] == runColor) {
|
|
|
|
|
runX++;
|
|
|
|
|
if (runX == 5)
|
|
|
|
|
result += PENALTY_N1;
|
|
|
|
|
else if (runX > 5)
|
|
|
|
|
result++;
|
|
|
|
|
} else {
|
|
|
|
|
finderPenaltyAddHistory(runX, runHistory);
|
|
|
|
|
if (!runColor)
|
|
|
|
|
result += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
|
|
|
|
|
runColor = modules[y][x];
|
|
|
|
|
runX = 1;
|
|
|
|
|
}
|
|
|
|
|
result += havingSameColor(runX, runColor, runHistory, result, y, x);
|
|
|
|
|
}
|
|
|
|
|
result += finderPenaltyTerminateAndCount(runColor, runX, runHistory) * PENALTY_N3;
|
|
|
|
|
}
|
|
|
|
@ -624,33 +649,13 @@ public final class QrCode {
|
|
|
|
|
int runY = 0;
|
|
|
|
|
int[] runHistory = new int[7];
|
|
|
|
|
for (int y = 0; y < size; y++) {
|
|
|
|
|
if (modules[y][x] == runColor) {
|
|
|
|
|
runY++;
|
|
|
|
|
if (runY == 5)
|
|
|
|
|
result += PENALTY_N1;
|
|
|
|
|
else if (runY > 5)
|
|
|
|
|
result++;
|
|
|
|
|
} else {
|
|
|
|
|
finderPenaltyAddHistory(runY, runHistory);
|
|
|
|
|
if (!runColor)
|
|
|
|
|
result += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
|
|
|
|
|
runColor = modules[y][x];
|
|
|
|
|
runY = 1;
|
|
|
|
|
}
|
|
|
|
|
result += havingSameColor(runY, runColor, runHistory, result, y, x);
|
|
|
|
|
}
|
|
|
|
|
result += finderPenaltyTerminateAndCount(runColor, runY, runHistory) * PENALTY_N3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2*2 blocks of modules having same color. extract method to reduce method size
|
|
|
|
|
for (int y = 0; y < size - 1; y++) {
|
|
|
|
|
for (int x = 0; x < size - 1; x++) {
|
|
|
|
|
boolean color = modules[y][x];
|
|
|
|
|
if ( color == modules[y][x + 1] &&
|
|
|
|
|
color == modules[y + 1][x] &&
|
|
|
|
|
color == modules[y + 1][x + 1])
|
|
|
|
|
result += PENALTY_N2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result += twobytwoHavingSameColor(modules);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Balance of black and white modules
|
|
|
|
|
int black = 0;
|
|
|
|
@ -667,8 +672,6 @@ public final class QrCode {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*---- Private helper functions ----*/
|
|
|
|
|
|
|
|
|
|
// Returns an ascending list of positions of alignment patterns for this version number.
|
|
|
|
@ -695,11 +698,18 @@ public final class QrCode {
|
|
|
|
|
|
|
|
|
|
// Returns the number of data bits that can be stored in a QR Code of the given version number, after
|
|
|
|
|
// all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
|
|
|
|
|
// The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table. replace integer to understandable word
|
|
|
|
|
// The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
|
|
|
|
|
private static int getNumRawDataModules(int ver) {
|
|
|
|
|
if (ver < MIN_VERSION || ver > MAX_VERSION)
|
|
|
|
|
throw new IllegalArgumentException("Version number out of range");
|
|
|
|
|
|
|
|
|
|
int result = calculateNumOfModules(ver);
|
|
|
|
|
|
|
|
|
|
assert 208 <= result && result <= 29648;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int calculateNumOfModules(int ver) {
|
|
|
|
|
int size = ver * 4 + 17;
|
|
|
|
|
int result = size * size; // Number of modules in the whole QR Code square
|
|
|
|
|
result -= 8 * 8 * 3; // Subtract the three finders with separators
|
|
|
|
@ -714,11 +724,9 @@ public final class QrCode {
|
|
|
|
|
if (ver >= 7)
|
|
|
|
|
result -= 6 * 3 * 2; // Subtract version information
|
|
|
|
|
}
|
|
|
|
|
assert 208 <= result && result <= 29648;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be
|
|
|
|
|
// implemented as a lookup table over all possible parameter values, instead of as an algorithm.
|
|
|
|
|
private static byte[] reedSolomonComputeDivisor(int degree) {
|
|
|
|
|