"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "core/src/main/java/com/google/zxing/aztec/encoder/State.java" between
zxing-zxing-3.4.1.tar.gz and zxing-zxing-3.5.0.tar.gz

About: ZXing ("zebra crossing") is a multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages. Info: Project is in maintenance mode (no active development).

State.java  (zxing-zxing-3.4.1):State.java  (zxing-zxing-3.5.0)
skipping to change at line 19 skipping to change at line 19
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.google.zxing.aztec.encoder; package com.google.zxing.aztec.encoder;
import java.util.Deque; import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
import com.google.zxing.common.BitArray; import com.google.zxing.common.BitArray;
/** /**
* State represents all information about a sequence necessary to generate the c urrent output. * State represents all information about a sequence necessary to generate the c urrent output.
* Note that a state is immutable. * Note that a state is immutable.
*/ */
final class State { final class State {
static final State INITIAL_STATE = new State(Token.EMPTY, HighLevelEncoder.MOD E_UPPER, 0, 0); static final State INITIAL_STATE = new State(Token.EMPTY, HighLevelEncoder.MOD E_UPPER, 0, 0);
skipping to change at line 43 skipping to change at line 45
// we're in Binary Shift mode. // we're in Binary Shift mode.
private final int mode; private final int mode;
// The list of tokens that we output. If we are in Binary Shift mode, this // The list of tokens that we output. If we are in Binary Shift mode, this
// token list does *not* yet included the token for those bytes // token list does *not* yet included the token for those bytes
private final Token token; private final Token token;
// If non-zero, the number of most recent bytes that should be output // If non-zero, the number of most recent bytes that should be output
// in Binary Shift mode. // in Binary Shift mode.
private final int binaryShiftByteCount; private final int binaryShiftByteCount;
// The total number of bits generated (including Binary Shift). // The total number of bits generated (including Binary Shift).
private final int bitCount; private final int bitCount;
private final int binaryShiftCost;
private State(Token token, int mode, int binaryBytes, int bitCount) { private State(Token token, int mode, int binaryBytes, int bitCount) {
this.token = token; this.token = token;
this.mode = mode; this.mode = mode;
this.binaryShiftByteCount = binaryBytes; this.binaryShiftByteCount = binaryBytes;
this.bitCount = bitCount; this.bitCount = bitCount;
// Make sure we match the token this.binaryShiftCost = calculateBinaryShiftCost(binaryBytes);
//int binaryShiftBitCount = (binaryShiftByteCount * 8) +
// (binaryShiftByteCount == 0 ? 0 :
// binaryShiftByteCount <= 31 ? 10 :
// binaryShiftByteCount <= 62 ? 20 : 21);
//assert this.bitCount == token.getTotalBitCount() + binaryShiftBitCount;
} }
int getMode() { int getMode() {
return mode; return mode;
} }
Token getToken() { Token getToken() {
return token; return token;
} }
int getBinaryShiftByteCount() { int getBinaryShiftByteCount() {
return binaryShiftByteCount; return binaryShiftByteCount;
} }
int getBitCount() { int getBitCount() {
return bitCount; return bitCount;
} }
State appendFLGn(int eci) {
State result = shiftAndAppend(HighLevelEncoder.MODE_PUNCT, 0); // 0: FLG(n)
Token token = result.token;
int bitsAdded = 3;
if (eci < 0) {
token = token.add(0, 3); // 0: FNC1
} else if (eci > 999999) {
throw new IllegalArgumentException("ECI code must be between 0 and 999999"
);
} else {
byte[] eciDigits = Integer.toString(eci).getBytes(StandardCharsets.ISO_885
9_1);
token = token.add(eciDigits.length, 3); // 1-6: number of ECI digits
for (byte eciDigit : eciDigits) {
token = token.add(eciDigit - '0' + 2, 4);
}
bitsAdded += eciDigits.length * 4;
}
return new State(token, mode, 0, bitCount + bitsAdded);
}
// Create a new state representing this state with a latch to a (not // Create a new state representing this state with a latch to a (not
// necessary different) mode, and then a code. // necessary different) mode, and then a code.
State latchAndAppend(int mode, int value) { State latchAndAppend(int mode, int value) {
//assert binaryShiftByteCount == 0;
int bitCount = this.bitCount; int bitCount = this.bitCount;
Token token = this.token; Token token = this.token;
if (mode != this.mode) { if (mode != this.mode) {
int latch = HighLevelEncoder.LATCH_TABLE[this.mode][mode]; int latch = HighLevelEncoder.LATCH_TABLE[this.mode][mode];
token = token.add(latch & 0xFFFF, latch >> 16); token = token.add(latch & 0xFFFF, latch >> 16);
bitCount += latch >> 16; bitCount += latch >> 16;
} }
int latchModeBitCount = mode == HighLevelEncoder.MODE_DIGIT ? 4 : 5; int latchModeBitCount = mode == HighLevelEncoder.MODE_DIGIT ? 4 : 5;
token = token.add(value, latchModeBitCount); token = token.add(value, latchModeBitCount);
return new State(token, mode, 0, bitCount + latchModeBitCount); return new State(token, mode, 0, bitCount + latchModeBitCount);
} }
// Create a new state representing this state, with a temporary shift // Create a new state representing this state, with a temporary shift
// to a different mode to output a single value. // to a different mode to output a single value.
State shiftAndAppend(int mode, int value) { State shiftAndAppend(int mode, int value) {
//assert binaryShiftByteCount == 0 && this.mode != mode;
Token token = this.token; Token token = this.token;
int thisModeBitCount = this.mode == HighLevelEncoder.MODE_DIGIT ? 4 : 5; int thisModeBitCount = this.mode == HighLevelEncoder.MODE_DIGIT ? 4 : 5;
// Shifts exist only to UPPER and PUNCT, both with tokens size 5. // Shifts exist only to UPPER and PUNCT, both with tokens size 5.
token = token.add(HighLevelEncoder.SHIFT_TABLE[this.mode][mode], thisModeBit Count); token = token.add(HighLevelEncoder.SHIFT_TABLE[this.mode][mode], thisModeBit Count);
token = token.add(value, 5); token = token.add(value, 5);
return new State(token, this.mode, 0, this.bitCount + thisModeBitCount + 5); return new State(token, this.mode, 0, this.bitCount + thisModeBitCount + 5);
} }
// Create a new state representing this state, but an additional character // Create a new state representing this state, but an additional character
// output in Binary Shift mode. // output in Binary Shift mode.
State addBinaryShiftChar(int index) { State addBinaryShiftChar(int index) {
Token token = this.token; Token token = this.token;
int mode = this.mode; int mode = this.mode;
int bitCount = this.bitCount; int bitCount = this.bitCount;
if (this.mode == HighLevelEncoder.MODE_PUNCT || this.mode == HighLevelEncode r.MODE_DIGIT) { if (this.mode == HighLevelEncoder.MODE_PUNCT || this.mode == HighLevelEncode r.MODE_DIGIT) {
//assert binaryShiftByteCount == 0;
int latch = HighLevelEncoder.LATCH_TABLE[mode][HighLevelEncoder.MODE_UPPER ]; int latch = HighLevelEncoder.LATCH_TABLE[mode][HighLevelEncoder.MODE_UPPER ];
token = token.add(latch & 0xFFFF, latch >> 16); token = token.add(latch & 0xFFFF, latch >> 16);
bitCount += latch >> 16; bitCount += latch >> 16;
mode = HighLevelEncoder.MODE_UPPER; mode = HighLevelEncoder.MODE_UPPER;
} }
int deltaBitCount = int deltaBitCount =
(binaryShiftByteCount == 0 || binaryShiftByteCount == 31) ? 18 : (binaryShiftByteCount == 0 || binaryShiftByteCount == 31) ? 18 :
(binaryShiftByteCount == 62) ? 9 : 8; (binaryShiftByteCount == 62) ? 9 : 8;
State result = new State(token, mode, binaryShiftByteCount + 1, bitCount + d eltaBitCount); State result = new State(token, mode, binaryShiftByteCount + 1, bitCount + d eltaBitCount);
if (result.binaryShiftByteCount == 2047 + 31) { if (result.binaryShiftByteCount == 2047 + 31) {
// The string is as long as it's allowed to be. We should end it. // The string is as long as it's allowed to be. We should end it.
result = result.endBinaryShift(index + 1); result = result.endBinaryShift(index + 1);
} }
return result; return result;
} }
// Create the state identical to this one, but we are no longer in // Create the state identical to this one, but we are no longer in
// Binary Shift mode. // Binary Shift mode.
State endBinaryShift(int index) { State endBinaryShift(int index) {
if (binaryShiftByteCount == 0) { if (binaryShiftByteCount == 0) {
return this; return this;
} }
Token token = this.token; Token token = this.token;
token = token.addBinaryShift(index - binaryShiftByteCount, binaryShiftByteCo unt); token = token.addBinaryShift(index - binaryShiftByteCount, binaryShiftByteCo unt);
//assert token.getTotalBitCount() == this.bitCount;
return new State(token, mode, 0, this.bitCount); return new State(token, mode, 0, this.bitCount);
} }
// Returns true if "this" state is better (or equal) to be in than "that" // Returns true if "this" state is better (or equal) to be in than "that"
// state under all possible circumstances. // state under all possible circumstances.
boolean isBetterThanOrEqualTo(State other) { boolean isBetterThanOrEqualTo(State other) {
int newModeBitCount = this.bitCount + (HighLevelEncoder.LATCH_TABLE[this.mod e][other.mode] >> 16); int newModeBitCount = this.bitCount + (HighLevelEncoder.LATCH_TABLE[this.mod e][other.mode] >> 16);
if (this.binaryShiftByteCount < other.binaryShiftByteCount) { if (this.binaryShiftByteCount < other.binaryShiftByteCount) {
// add additional B/S encoding cost of other, if any // add additional B/S encoding cost of other, if any
newModeBitCount += calculateBinaryShiftCost(other) - calculateBinaryShiftC ost(this); newModeBitCount += other.binaryShiftCost - this.binaryShiftCost;
} else if (this.binaryShiftByteCount > other.binaryShiftByteCount && other.b inaryShiftByteCount > 0) { } else if (this.binaryShiftByteCount > other.binaryShiftByteCount && other.b inaryShiftByteCount > 0) {
// maximum possible additional cost (we end up exceeding the 31 byte bound ary and other state can stay beneath it) // maximum possible additional cost (we end up exceeding the 31 byte bound ary and other state can stay beneath it)
newModeBitCount += 10; newModeBitCount += 10;
} }
return newModeBitCount <= other.bitCount; return newModeBitCount <= other.bitCount;
} }
BitArray toBitArray(byte[] text) { BitArray toBitArray(byte[] text) {
// Reverse the tokens, so that they are in the order that they should List<Token> symbols = new ArrayList<>();
// be output
Deque<Token> symbols = new LinkedList<>();
for (Token token = endBinaryShift(text.length).token; token != null; token = token.getPrevious()) { for (Token token = endBinaryShift(text.length).token; token != null; token = token.getPrevious()) {
symbols.addFirst(token); symbols.add(token);
} }
BitArray bitArray = new BitArray(); BitArray bitArray = new BitArray();
// Add each token to the result. // Add each token to the result in forward order
for (Token symbol : symbols) { for (int i = symbols.size() - 1; i >= 0; i--) {
symbol.appendTo(bitArray, text); symbols.get(i).appendTo(bitArray, text);
} }
//assert bitArray.getSize() == this.bitCount;
return bitArray; return bitArray;
} }
@Override @Override
public String toString() { public String toString() {
return String.format("%s bits=%d bytes=%d", HighLevelEncoder.MODE_NAMES[mode ], bitCount, binaryShiftByteCount); return String.format("%s bits=%d bytes=%d", HighLevelEncoder.MODE_NAMES[mode ], bitCount, binaryShiftByteCount);
} }
private static int calculateBinaryShiftCost(State state) { private static int calculateBinaryShiftCost(int binaryShiftByteCount) {
if (state.binaryShiftByteCount > 62) { if (binaryShiftByteCount > 62) {
return 21; // B/S with extended length return 21; // B/S with extended length
} }
if (state.binaryShiftByteCount > 31) { if (binaryShiftByteCount > 31) {
return 20; // two B/S return 20; // two B/S
} }
if (state.binaryShiftByteCount > 0) { if (binaryShiftByteCount > 0) {
return 10; // one B/S return 10; // one B/S
} }
return 0; return 0;
} }
} }
 End of changes. 17 change blocks. 
27 lines changed or deleted 39 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)