Added private helper function to reduce occurrences of low-level bit arithmetic.

pull/134/head
Project Nayuki 6 years ago
parent 1027ad5b65
commit b0a7a4240a

@ -162,7 +162,7 @@ public final class QrCode {
public boolean getModule(int x, int y) { public boolean getModule(int x, int y) {
if (0 <= x && x < size && 0 <= y && y < size) { if (0 <= x && x < size && 0 <= y && y < size) {
int i = y * size + x; int i = y * size + x;
return ((modules[i >>> 5] >>> i) & 1) != 0; return getBit(modules[i >>> 5], i) != 0;
} else } else
return false; return false;
} }
@ -246,18 +246,18 @@ public final class QrCode {
// Draw first copy // Draw first copy
for (int i = 0; i <= 5; i++) for (int i = 0; i <= 5; i++)
setModule(8, i, (bits >>> i) & 1); setModule(8, i, getBit(bits, i));
setModule(8, 7, (bits >>> 6) & 1); setModule(8, 7, getBit(bits, 6));
setModule(8, 8, (bits >>> 7) & 1); setModule(8, 8, getBit(bits, 7));
setModule(7, 8, (bits >>> 8) & 1); setModule(7, 8, getBit(bits, 8));
for (int i = 9; i < 15; i++) for (int i = 9; i < 15; i++)
setModule(14 - i, 8, (bits >>> i) & 1); setModule(14 - i, 8, getBit(bits, i));
// Draw second copy // Draw second copy
for (int i = 0; i <= 7; i++) for (int i = 0; i <= 7; i++)
setModule(size - 1 - i, 8, (bits >>> i) & 1); setModule(size - 1 - i, 8, getBit(bits, i));
for (int i = 8; i < 15; i++) for (int i = 8; i < 15; i++)
setModule(8, size - 15 + i, (bits >>> i) & 1); setModule(8, size - 15 + i, getBit(bits, i));
setModule(8, size - 8, 1); // Always black setModule(8, size - 8, 1); // Always black
} }
@ -319,7 +319,7 @@ public final class QrCode {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
for (int i = 0; i < dataOutputBitIndexes.length; i++) { for (int i = 0; i < dataOutputBitIndexes.length; i++) {
int j = dataOutputBitIndexes[i]; int j = dataOutputBitIndexes[i];
int bit = (allCodewords[i >>> 3] >>> (~i & 7)) & 1; int bit = getBit(allCodewords[i >>> 3], ~i & 7);
modules[j >>> 5] |= bit << j; modules[j >>> 5] |= bit << j;
} }
} }
@ -377,7 +377,7 @@ public final class QrCode {
for (int x = 0; x < size; x++, index++, downIndex++) { for (int x = 0; x < size; x++, index++, downIndex++) {
// Adjacent modules having same color // Adjacent modules having same color
int bit = (modules[index >>> 5] >>> index) & 1; int bit = getBit(modules[index >>> 5], index);
if (bit != runColor) { if (bit != runColor) {
runColor = bit; runColor = bit;
runLen = 1; runLen = 1;
@ -392,7 +392,7 @@ public final class QrCode {
black += bit; black += bit;
bits = ((bits & 0b1111111111) << 1) | bit; bits = ((bits & 0b1111111111) << 1) | bit;
if (downIndex < end) { if (downIndex < end) {
downBits = ((downBits & 1) << 1) | ((modules[downIndex >>> 5] >>> downIndex) & 1); downBits = ((downBits & 1) << 1) | getBit(modules[downIndex >>> 5], downIndex);
// 2*2 blocks of modules having same color // 2*2 blocks of modules having same color
if (x >= 1 && (downBits == 0 || downBits == 3) && downBits == (bits & 3)) if (x >= 1 && (downBits == 0 || downBits == 3) && downBits == (bits & 3))
result += PENALTY_N2; result += PENALTY_N2;
@ -412,7 +412,7 @@ public final class QrCode {
for (int y = 0, index = x; y < size; y++, index += size) { for (int y = 0, index = x; y < size; y++, index += size) {
// Adjacent modules having same color // Adjacent modules having same color
int bit = (modules[index >>> 5] >>> index) & 1; int bit = getBit(modules[index >>> 5], index);
if (bit != runColor) { if (bit != runColor) {
runColor = bit; runColor = bit;
runLen = 1; runLen = 1;
@ -453,6 +453,12 @@ public final class QrCode {
} }
// Returns 0 or 1 based on the i'th bit of x.
private static int getBit(int x, int i) {
return (x >>> i) & 1;
}
/*---- Private tables of constants ----*/ /*---- Private tables of constants ----*/
// For use in getPenaltyScore(), when evaluating which mask is best. // For use in getPenaltyScore(), when evaluating which mask is best.

Loading…
Cancel
Save