diff --git a/java/src/main/java/io/nayuki/qrcodegen/BitBuffer.java b/java/src/main/java/io/nayuki/qrcodegen/BitBuffer.java index 624d129..4ff3e42 100644 --- a/java/src/main/java/io/nayuki/qrcodegen/BitBuffer.java +++ b/java/src/main/java/io/nayuki/qrcodegen/BitBuffer.java @@ -125,5 +125,29 @@ public final class BitBuffer implements Cloneable { throw new AssertionError(e); } } + + + // Pad with alternating bytes until data capacity is reached + public void addPad(int dataCapacityBits) { + for (int padByte = 0xEC; bitLength() < dataCapacityBits; padByte ^= 0xEC ^ 0x11) + appendBits(padByte, 8); + } + + + // Pack bits into bytes in big endian + public byte[] toCodewords() { + byte[] dataCodewords = new byte[bitLength() / 8]; + for (int i = 0; i < bitLength(); i++) + dataCodewords[i >>> 3] |= getBit(i) << (7 - (i & 7)); + return dataCodewords; + } + + + // Add terminator and pad up to a byte if applicable + public void addTerminator(int dataCapacityBits) { + appendBits(0, Math.min(4, dataCapacityBits - bitLength())); + appendBits(0, (8 - bitLength() % 8) % 8); + assert bitLength() % 8 == 0; + } -} +} \ No newline at end of file diff --git a/java/src/main/java/io/nayuki/qrcodegen/Button.java b/java/src/main/java/io/nayuki/qrcodegen/Button.java new file mode 100644 index 0000000..66fca4e --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/Button.java @@ -0,0 +1,17 @@ +package io.nayuki.qrcodegen; + +public class Button { + public Command theCommand; + + public Button(Command theCommand) { + setCommand(theCommand); + } + + public void setCommand(Command newCommand) { + this.theCommand = newCommand; + } + + public boolean pressed(int y, int x, int msk) { + return theCommand.excute(y, x, msk); + } +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/Command.java b/java/src/main/java/io/nayuki/qrcodegen/Command.java new file mode 100644 index 0000000..b12845e --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/Command.java @@ -0,0 +1,5 @@ +package io.nayuki.qrcodegen; + +public interface Command { + public abstract boolean excute(int y, int x, int msk); +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/Msk0.java b/java/src/main/java/io/nayuki/qrcodegen/Msk0.java new file mode 100644 index 0000000..a125577 --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/Msk0.java @@ -0,0 +1,7 @@ +package io.nayuki.qrcodegen; + +public class Msk0 { + public boolean operation(int y, int x, int msk) { + return ((x + y) % 2 == 0); + } +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/Msk1.java b/java/src/main/java/io/nayuki/qrcodegen/Msk1.java new file mode 100644 index 0000000..f9f92e3 --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/Msk1.java @@ -0,0 +1,7 @@ +package io.nayuki.qrcodegen; + +public class Msk1 { + public boolean operation(int y, int x, int msk) { + return (y % 2 == 0); + } +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/Msk2.java b/java/src/main/java/io/nayuki/qrcodegen/Msk2.java new file mode 100644 index 0000000..42c145e --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/Msk2.java @@ -0,0 +1,7 @@ +package io.nayuki.qrcodegen; + +public class Msk2 { + public boolean operation(int y, int x, int msk) { + return (x % 3 == 0); + } +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/Msk3.java b/java/src/main/java/io/nayuki/qrcodegen/Msk3.java new file mode 100644 index 0000000..45064c0 --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/Msk3.java @@ -0,0 +1,7 @@ +package io.nayuki.qrcodegen; + +public class Msk3 { + public boolean operation(int y, int x, int msk) { + return ((x + y) % 3 == 0); + } +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/Msk4.java b/java/src/main/java/io/nayuki/qrcodegen/Msk4.java new file mode 100644 index 0000000..d0b13a1 --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/Msk4.java @@ -0,0 +1,7 @@ +package io.nayuki.qrcodegen; + +public class Msk4 { + public boolean operation(int y, int x, int msk) { + return ((x / 3 + y / 2) % 2 == 0); + } +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/Msk5.java b/java/src/main/java/io/nayuki/qrcodegen/Msk5.java new file mode 100644 index 0000000..decc5df --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/Msk5.java @@ -0,0 +1,7 @@ +package io.nayuki.qrcodegen; + +public class Msk5 { + public boolean operation(int y, int x, int msk) { + return (x * y % 2 + x * y % 3 == 0); + } +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/Msk6.java b/java/src/main/java/io/nayuki/qrcodegen/Msk6.java new file mode 100644 index 0000000..555fb64 --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/Msk6.java @@ -0,0 +1,7 @@ +package io.nayuki.qrcodegen; + +public class Msk6 { + public boolean operation(int y, int x, int msk) { + return ((x * y % 2 + x * y % 3) % 2 == 0); + } +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/Msk7.java b/java/src/main/java/io/nayuki/qrcodegen/Msk7.java new file mode 100644 index 0000000..7d98190 --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/Msk7.java @@ -0,0 +1,7 @@ +package io.nayuki.qrcodegen; + +public class Msk7 { + public boolean operation(int y, int x, int msk) { + return (((x + y) % 2 + x * y % 3) % 2 == 0); + } +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/MskCommandFactory.java b/java/src/main/java/io/nayuki/qrcodegen/MskCommandFactory.java new file mode 100644 index 0000000..93bc177 --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/MskCommandFactory.java @@ -0,0 +1,46 @@ +package io.nayuki.qrcodegen; + +public class MskCommandFactory { + public static Command getCommand(int msk) { + Command theCommand = null; + + Msk0 msk0 = new Msk0(); + Msk1 msk1 = new Msk1(); + Msk2 msk2 = new Msk2(); + Msk3 msk3 = new Msk3(); + Msk4 msk4 = new Msk4(); + Msk5 msk5 = new Msk5(); + Msk6 msk6 = new Msk6(); + Msk7 msk7 = new Msk7(); + + switch (msk) { + case 0: + theCommand = new msk0Command(msk0); + break; + case 1: + theCommand = new msk1Command(msk1); + break; + case 2: + theCommand = new msk2Command(msk2); + break; + case 3: + theCommand = new msk3Command(msk3); + break; + case 4: + theCommand = new msk4Command(msk4); + break; + case 5: + theCommand = new msk5Command(msk5); + break; + case 6: + theCommand = new msk6Command(msk6); + break; + case 7: + theCommand = new msk7Command(msk7); + break; + default: + throw new AssertionError(); + } + return theCommand; + } +} \ No newline at end of file diff --git a/java/src/main/java/io/nayuki/qrcodegen/QrCode.java b/java/src/main/java/io/nayuki/qrcodegen/QrCode.java index bf3a11a..dcb8b6b 100644 --- a/java/src/main/java/io/nayuki/qrcodegen/QrCode.java +++ b/java/src/main/java/io/nayuki/qrcodegen/QrCode.java @@ -151,11 +151,67 @@ public final class QrCode { Objects.requireNonNull(segments); Objects.requireNonNull(errorCorrectionLevel); final boolean isVersionInRange = MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= MAX_VERSION; - final boolean isMaskValid = mask < -1 || mask > 7; - if (!isVersionInRange || isMaskValid) + final boolean isMaskOutOfRange = mask < -1 || mask > 7; + if (!isVersionInRange || isMaskOutOfRange) throw new IllegalArgumentException("Invalid value"); - // Find the minimal version number to use + + int version = findMinimalVersion(segments, errorCorrectionLevel, minVersion, maxVersion); + + int dataUsedBits = QrSegment.getTotalBits(segments, version); + + errorCorrectionLevel = findMaximalErrorCorrectionLevel(errorCorrectionLevel, boostEcl, version, dataUsedBits); + + + BitBuffer bitBuffer = segmentsToBitBuffer(segments, version); + assert bitBuffer.bitLength() == dataUsedBits; + + + int dataCapacityBits = getNumDataCodewords(version, errorCorrectionLevel) * 8; + assert bitBuffer.bitLength() <= dataCapacityBits; + bitBuffer.addTerminator(dataCapacityBits); + + + bitBuffer.addPad(dataCapacityBits); + + + byte[] dataCodewords = bitBuffer.toCodewords(); + + // Create the QR Code object + return new QrCode(version, errorCorrectionLevel, dataCodewords, mask); + } + + /*---- Private helper methods for encodeSegments ----*/ + + + + // Concatenate all segments to create the data bit string + private static BitBuffer segmentsToBitBuffer(List segments, int version) { + BitBuffer bitBuffer = new BitBuffer(); + for (QrSegment segment : segments) { + bitBuffer.appendBits(segment.mode.modeBits, 4); + bitBuffer.appendBits(segment.numChars, segment.mode.numCharCountBits(version)); + bitBuffer.appendData(segment.data); + } + return bitBuffer; + } + + + // Increase the error correction level while the data still fits in the current version number + private static Ecc findMaximalErrorCorrectionLevel(Ecc errorCorrectionLevel, boolean boostEcl, int version, + int dataUsedBits) { + for (Ecc newEcl : Ecc.values()) { // From low to high + final boolean canIncreaseErrorCorrectionLevel = dataUsedBits <= getNumDataCodewords(version, newEcl) * 8; + if (boostEcl && canIncreaseErrorCorrectionLevel) + errorCorrectionLevel = newEcl; + } + return errorCorrectionLevel; + } + + + //Returns the minimal version number to use + private static int findMinimalVersion(List segments, Ecc errorCorrectionLevel, int minVersion, + int maxVersion) { int version, dataUsedBits; for (version = minVersion; ; version++) { int dataCapacityBits = getNumDataCodewords(version, errorCorrectionLevel) * 8; // Number of data bits available @@ -170,41 +226,7 @@ public final class QrCode { } } assert dataUsedBits != -1; - - // Increase the error correction level while the data still fits in the current version number - for (Ecc newEcl : Ecc.values()) { // From low to high - final boolean canIncreaseErrorCorrectionLevel = dataUsedBits <= getNumDataCodewords(version, newEcl) * 8; - if (boostEcl && canIncreaseErrorCorrectionLevel) - errorCorrectionLevel = newEcl; - } - - // Concatenate all segments to create the data bit string - BitBuffer bitBuffer = new BitBuffer(); - for (QrSegment segment : segments) { - bitBuffer.appendBits(segment.mode.modeBits, 4); - bitBuffer.appendBits(segment.numChars, segment.mode.numCharCountBits(version)); - bitBuffer.appendData(segment.data); - } - assert bitBuffer.bitLength() == dataUsedBits; - - // Add terminator and pad up to a byte if applicable - int dataCapacityBits = getNumDataCodewords(version, errorCorrectionLevel) * 8; - assert bitBuffer.bitLength() <= dataCapacityBits; - bitBuffer.appendBits(0, Math.min(4, dataCapacityBits - bitBuffer.bitLength())); - bitBuffer.appendBits(0, (8 - bitBuffer.bitLength() % 8) % 8); - assert bitBuffer.bitLength() % 8 == 0; - - // Pad with alternating bytes until data capacity is reached - for (int padByte = 0xEC; bitBuffer.bitLength() < dataCapacityBits; padByte ^= 0xEC ^ 0x11) - bitBuffer.appendBits(padByte, 8); - - // Pack bits into bytes in big endian - byte[] dataCodewords = new byte[bitBuffer.bitLength() / 8]; - for (int i = 0; i < bitBuffer.bitLength(); i++) - dataCodewords[i >>> 3] |= bitBuffer.getBit(i) << (7 - (i & 7)); - - // Create the QR Code object - return new QrCode(version, errorCorrectionLevel, dataCodewords, mask); + return version; } /*---- Instance fields ----*/ @@ -370,7 +392,16 @@ public final class QrCode { drawFinderPattern(size - 1 - FINDER_SIZE, FINDER_SIZE); drawFinderPattern(FINDER_SIZE, size - 1 - FINDER_SIZE); - // Draw numerous alignment patterns + drawAlignmentsPatterns(); + + // Draw configuration data + drawFormatBits(0); // Dummy mask value; overwritten later in the constructor + drawVersion(); + } + + + // Draw numerous alignment patterns + private void drawAlignmentsPatterns() { int[] alignPatPos = getAlignmentPatternPositions(); int numAlign = alignPatPos.length; for (int i = 0; i < numAlign; i++) { @@ -383,10 +414,6 @@ public final class QrCode { drawAlignmentPattern(alignPatPos[i], alignPatPos[j]); } } - - // Draw configuration data - drawFormatBits(0); // Dummy mask value; overwritten later in the constructor - drawVersion(); } @@ -553,23 +580,20 @@ public final class QrCode { // 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. - private void applyMask(int msk) { + + private void applyMask(int msk) { + + if (msk < 0 || msk > 7) throw new IllegalArgumentException("Mask value out of range"); for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { boolean invert; - switch (msk) { - case 0: invert = (x + y) % 2 == 0; break; - case 1: invert = y % 2 == 0; break; - case 2: invert = x % 3 == 0; break; - case 3: invert = (x + y) % 3 == 0; break; - case 4: invert = (x / 3 + y / 2) % 2 == 0; break; - case 5: invert = x * y % 2 + x * y % 3 == 0; break; - case 6: invert = (x * y % 2 + x * y % 3) % 2 == 0; break; - case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break; - default: throw new AssertionError(); - } + + Command mskCommand = MskCommandFactory.getCommand(msk); + Button button = new Button(mskCommand); + invert = button.pressed(y, x, msk); + modules[y][x] ^= invert & !isFunction[y][x]; } } @@ -579,51 +603,69 @@ public final class QrCode { // A messy helper function for the constructor. This QR Code must be in an unmasked state when this // method is called. The given argument is the requested mask, which is -1 for auto or 0 to 7 for fixed. // 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 - int minPenalty = Integer.MAX_VALUE; - for (int i = 0; i < 8; i++) { - applyMask(i); - drawFormatBits(i); - int penalty = getPenaltyScore(); - if (penalty < minPenalty) { - msk = i; - minPenalty = penalty; - } - applyMask(i); // Undoes the mask due to XOR + private int handleConstructorMasking(int mask) { + if (mask == -1) { + mask = findBestMask(); + } + assert 0 <= mask && mask <= 7; + applyMask(mask); // Apply the final choice of mask + drawFormatBits(mask); // Overwrite old format bits + return mask; // The caller shall assign this value to the final-declared field + } + + + // Automatically choose best mask + private int findBestMask() { + int mask = -1; + int minPenalty = Integer.MAX_VALUE; + for (int i = 0; i < 8; i++) { + applyMask(i); + drawFormatBits(i); + int penalty = getPenaltyScore(); + if (penalty < minPenalty) { + mask = i; + minPenalty = penalty; } + applyMask(i); // Undoes the mask due to XOR } - 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 + return mask; } + 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; + } + // 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. - private int getPenaltyScore() { - int result = 0; + + private int getPenaltyScore() { + + + int result = 0; + // Adjacent modules in row having same color, and finder-like patterns for (int y = 0; y < size; y++) { boolean runColor = false; int runX = 0; int[] runHistory = new int[7]; 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; } @@ -633,34 +675,14 @@ 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 - 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; for (boolean[] row : modules) { @@ -675,11 +697,24 @@ public final class QrCode { result += k * PENALTY_N4; 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; + } + /*---- Private helper functions ----*/ - + // Returns an ascending list of positions of alignment patterns for this version number. // Each position is in the range [0,177), and are used on both the x and y axes. // This could be implemented as lookup table of 40 variable-length lists of unsigned bytes. @@ -700,15 +735,22 @@ public final class QrCode { return result; } } - - + + // 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. 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 @@ -723,11 +765,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) { @@ -737,7 +777,7 @@ public final class QrCode { // For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array {255, 8, 93}. byte[] result = new byte[degree]; result[degree - 1] = 1; // Start off with the monomial x^0 - + // Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}), // and drop the highest monomial term which is always 1x^degree. // Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D). @@ -753,8 +793,8 @@ public final class QrCode { } return result; } - - + + // Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials. private static byte[] reedSolomonComputeRemainder(byte[] data, byte[] divisor) { Objects.requireNonNull(data); @@ -769,33 +809,33 @@ public final class QrCode { } return result; } - - + + // Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result // are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8. private static int reedSolomonMultiply(int x, int y) { assert x >> 8 == 0 && y >> 8 == 0; - // Russian peasant multiplication - int z = 0; - for (int i = 7; i >= 0; i--) { - z = (z << 1) ^ ((z >>> 7) * 0x11D); - z ^= ((y >>> i) & 1) * x; - } - assert z >>> 8 == 0; - return z; + // Russian peasant multiplication + int z = 0; + for (int i = 7; i >= 0; i--) { + z = (z << 1) ^ ((z >>> 7) * 0x11D); + z ^= ((y >>> i) & 1) * x; + } + assert z >>> 8 == 0; + return z; } - - + + // Returns the number of 8-bit data (i.e. not error correction) codewords contained in any // QR Code of the given version number and error correction level, with remainder bits discarded. // This stateless pure function could be implemented as a (40*4)-cell lookup table. static int getNumDataCodewords(int ver, Ecc ecl) { return getNumRawDataModules(ver) / 8 - - ECC_CODEWORDS_PER_BLOCK [ecl.ordinal()][ver] - * NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal()][ver]; + - ECC_CODEWORDS_PER_BLOCK [ecl.ordinal()][ver] + * NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal()][ver]; } - - + + // Can only be called immediately after a white run is added, and // returns either 0, 1, or 2. A helper function for getPenaltyScore(). private int finderPenaltyCountPatterns(int[] runHistory) { @@ -803,10 +843,10 @@ public final class QrCode { assert n <= size * 3; boolean core = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n; return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0) - + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0); + + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0); } - - + + // Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore(). private int finderPenaltyTerminateAndCount(boolean currentRunColor, int currentRunLength, int[] runHistory) { if (currentRunColor) { // Terminate black run @@ -817,8 +857,8 @@ public final class QrCode { finderPenaltyAddHistory(currentRunLength, runHistory); return finderPenaltyCountPatterns(runHistory); } - - + + // Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore(). private void finderPenaltyAddHistory(int currentRunLength, int[] runHistory) { if (runHistory[0] == 0) @@ -826,52 +866,52 @@ public final class QrCode { System.arraycopy(runHistory, 0, runHistory, 1, runHistory.length - 1); runHistory[0] = currentRunLength; } - - + + // Returns true iff the i'th bit of x is set to 1. static boolean getBit(int x, int i) { return ((x >>> i) & 1) != 0; } - - + + /*---- Constants and tables ----*/ - + /** The minimum version number (1) supported in the QR Code Model 2 standard. */ public static final int MIN_VERSION = 1; - + /** The maximum version number (40) supported in the QR Code Model 2 standard. */ public static final int MAX_VERSION = 40; - - + + // For use in getPenaltyScore(), when evaluating which mask is best. private static final int PENALTY_N1 = 3; private static final int PENALTY_N2 = 3; private static final int PENALTY_N3 = 40; private static final int PENALTY_N4 = 10; - - + + private static final byte[][] ECC_CODEWORDS_PER_BLOCK = { - // Version: (note that index 0 is for padding, and is set to an illegal value) - //0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level - {-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Low - {-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28}, // Medium - {-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Quartile - {-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // High + // Version: (note that index 0 is for padding, and is set to an illegal value) + //0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level + {-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Low + {-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28}, // Medium + {-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Quartile + {-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // High }; - + private static final byte[][] NUM_ERROR_CORRECTION_BLOCKS = { - // Version: (note that index 0 is for padding, and is set to an illegal value) - //0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level - {-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25}, // Low - {-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49}, // Medium - {-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68}, // Quartile - {-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81}, // High + // Version: (note that index 0 is for padding, and is set to an illegal value) + //0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level + {-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25}, // Low + {-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49}, // Medium + {-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68}, // Quartile + {-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81}, // High }; - - - + + + /*---- Public helper enumeration ----*/ - + /** * The error correction level in a QR Code symbol. */ @@ -882,14 +922,14 @@ public final class QrCode { /** The QR Code can tolerate about 15% erroneous codewords. */ MEDIUM(0), /** The QR Code can tolerate about 25% erroneous codewords. */ QUARTILE(3), /** The QR Code can tolerate about 30% erroneous codewords. */ HIGH(2); - + // In the range 0 to 3 (unsigned 2-bit integer). final int formatBits; - + // Constructor. private Ecc(int fb) { formatBits = fb; } } - -} + +} \ No newline at end of file diff --git a/java/src/main/java/io/nayuki/qrcodegen/QrSegmentAdvanced.java b/java/src/main/java/io/nayuki/qrcodegen/QrSegmentAdvanced.java index a4f039c..b125f18 100644 --- a/java/src/main/java/io/nayuki/qrcodegen/QrSegmentAdvanced.java +++ b/java/src/main/java/io/nayuki/qrcodegen/QrSegmentAdvanced.java @@ -359,4 +359,4 @@ public final class QrSegmentAdvanced { private QrSegmentAdvanced() {} // Not instantiable -} +} \ No newline at end of file diff --git a/java/src/main/java/io/nayuki/qrcodegen/msk0Command.java b/java/src/main/java/io/nayuki/qrcodegen/msk0Command.java new file mode 100644 index 0000000..357bcdc --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/msk0Command.java @@ -0,0 +1,13 @@ +package io.nayuki.qrcodegen; + +public class msk0Command implements Command{ + private Msk0 theMsk0; + + public msk0Command(Msk0 theMsk0) { + this.theMsk0 = theMsk0; + } + + public boolean excute(int y, int x, int msk) { + return theMsk0.operation(y, x, msk); + } +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/msk1Command.java b/java/src/main/java/io/nayuki/qrcodegen/msk1Command.java new file mode 100644 index 0000000..a503249 --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/msk1Command.java @@ -0,0 +1,13 @@ +package io.nayuki.qrcodegen; + +public class msk1Command implements Command { + private Msk1 theMsk1; + + public msk1Command(Msk1 theMsk1) { + this.theMsk1 = theMsk1; + } + + public boolean excute(int y, int x, int msk) { + return theMsk1.operation(y, x, msk); + } +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/msk2Command.java b/java/src/main/java/io/nayuki/qrcodegen/msk2Command.java new file mode 100644 index 0000000..d62f58e --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/msk2Command.java @@ -0,0 +1,13 @@ +package io.nayuki.qrcodegen; + +public class msk2Command implements Command { + private Msk2 theMsk2; + + public msk2Command(Msk2 theMsk2) { + this.theMsk2 = theMsk2; + } + + public boolean excute(int y, int x, int msk) { + return theMsk2.operation(y, x, msk); + } +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/msk3Command.java b/java/src/main/java/io/nayuki/qrcodegen/msk3Command.java new file mode 100644 index 0000000..402f2a2 --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/msk3Command.java @@ -0,0 +1,13 @@ +package io.nayuki.qrcodegen; + +public class msk3Command implements Command { + private Msk3 theMsk3; + + public msk3Command(Msk3 theMsk3) { + this.theMsk3 = theMsk3; + } + + public boolean excute(int y, int x, int msk) { + return theMsk3.operation(y, x, msk); + } +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/msk4Command.java b/java/src/main/java/io/nayuki/qrcodegen/msk4Command.java new file mode 100644 index 0000000..ca755fc --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/msk4Command.java @@ -0,0 +1,13 @@ +package io.nayuki.qrcodegen; + +public class msk4Command implements Command { + private Msk4 theMsk4; + + public msk4Command(Msk4 theMsk4) { + this.theMsk4 = theMsk4; + } + + public boolean excute(int y, int x, int msk) { + return theMsk4.operation(y, x, msk); + } +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/msk5Command.java b/java/src/main/java/io/nayuki/qrcodegen/msk5Command.java new file mode 100644 index 0000000..7a6f4b1 --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/msk5Command.java @@ -0,0 +1,13 @@ +package io.nayuki.qrcodegen; + +public class msk5Command implements Command { + private Msk5 theMsk5; + + public msk5Command(Msk5 theMsk5) { + this.theMsk5 = theMsk5; + } + + public boolean excute(int y, int x, int msk) { + return theMsk5.operation(y, x, msk); + } +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/msk6Command.java b/java/src/main/java/io/nayuki/qrcodegen/msk6Command.java new file mode 100644 index 0000000..078c270 --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/msk6Command.java @@ -0,0 +1,13 @@ +package io.nayuki.qrcodegen; + +public class msk6Command implements Command { + private Msk6 theMsk6; + + public msk6Command(Msk6 theMsk6) { + this.theMsk6 = theMsk6; + } + + public boolean excute(int y, int x, int msk) { + return theMsk6.operation(y, x, msk); + } +} diff --git a/java/src/main/java/io/nayuki/qrcodegen/msk7Command.java b/java/src/main/java/io/nayuki/qrcodegen/msk7Command.java new file mode 100644 index 0000000..b5c9e2c --- /dev/null +++ b/java/src/main/java/io/nayuki/qrcodegen/msk7Command.java @@ -0,0 +1,13 @@ +package io.nayuki.qrcodegen; + +public class msk7Command implements Command { + private Msk7 theMsk7; + + public msk7Command(Msk7 theMsk7) { + this.theMsk7 = theMsk7; + } + + public boolean excute(int y, int x, int msk) { + return theMsk7.operation(y, x, msk); + } +}