diff --git a/core/src/main/java/com/google/zxing/aztec/encoder/State.java b/core/src/main/java/com/google/zxing/aztec/encoder/State.java index d31b166d6..acd1ac6d8 100644 --- a/core/src/main/java/com/google/zxing/aztec/encoder/State.java +++ b/core/src/main/java/com/google/zxing/aztec/encoder/State.java @@ -42,12 +42,14 @@ final class State { private final int binaryShiftByteCount; // The total number of bits generated (including Binary Shift). private final int bitCount; + private final int binaryShiftCost; private State(Token token, int mode, int binaryBytes, int bitCount) { this.token = token; this.mode = mode; this.binaryShiftByteCount = binaryBytes; this.bitCount = bitCount; + this.binaryShiftCost = calculateBinaryShiftCost(binaryBytes); } int getMode() { @@ -155,7 +157,7 @@ final class State { int newModeBitCount = this.bitCount + (HighLevelEncoder.LATCH_TABLE[this.mode][other.mode] >> 16); if (this.binaryShiftByteCount < other.binaryShiftByteCount) { // add additional B/S encoding cost of other, if any - newModeBitCount += calculateBinaryShiftCost(other) - calculateBinaryShiftCost(this); + newModeBitCount += other.binaryShiftCost - this.binaryShiftCost; } else if (this.binaryShiftByteCount > other.binaryShiftByteCount && other.binaryShiftByteCount > 0) { // maximum possible additional cost (we end up exceeding the 31 byte boundary and other state can stay beneath it) newModeBitCount += 10; @@ -184,14 +186,14 @@ final class State { return String.format("%s bits=%d bytes=%d", HighLevelEncoder.MODE_NAMES[mode], bitCount, binaryShiftByteCount); } - private static int calculateBinaryShiftCost(State state) { - if (state.binaryShiftByteCount > 62) { + private static int calculateBinaryShiftCost(int binaryShiftByteCount) { + if (binaryShiftByteCount > 62) { return 21; // B/S with extended length } - if (state.binaryShiftByteCount > 31) { + if (binaryShiftByteCount > 31) { return 20; // two B/S } - if (state.binaryShiftByteCount > 0) { + if (binaryShiftByteCount > 0) { return 10; // one B/S } return 0;