Tighten up some non-private fields in DM encoder

git-svn-id: https://zxing.googlecode.com/svn/trunk@2800 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen@gmail.com 2013-05-17 15:50:51 +00:00
parent c608a08978
commit 5214bf1840
11 changed files with 62 additions and 44 deletions

View file

@ -26,14 +26,14 @@ final class ASCIIEncoder implements Encoder {
@Override @Override
public void encode(EncoderContext context) { public void encode(EncoderContext context) {
//step B //step B
int n = HighLevelEncoder.determineConsecutiveDigitCount(context.msg, context.pos); int n = HighLevelEncoder.determineConsecutiveDigitCount(context.getMessage(), context.pos);
if (n >= 2) { if (n >= 2) {
context.writeCodeword(encodeASCIIDigits(context.msg.charAt(context.pos), context.writeCodeword(encodeASCIIDigits(context.getMessage().charAt(context.pos),
context.msg.charAt(context.pos + 1))); context.getMessage().charAt(context.pos + 1)));
context.pos += 2; context.pos += 2;
} else { } else {
char c = context.getCurrentChar(); char c = context.getCurrentChar();
int newMode = HighLevelEncoder.lookAheadTest(context.msg, context.pos, getEncodingMode()); int newMode = HighLevelEncoder.lookAheadTest(context.getMessage(), context.pos, getEncodingMode());
if (newMode != getEncodingMode()) { if (newMode != getEncodingMode()) {
switch (newMode) { switch (newMode) {
case HighLevelEncoder.BASE256_ENCODATION: case HighLevelEncoder.BASE256_ENCODATION:

View file

@ -33,7 +33,7 @@ final class Base256Encoder implements Encoder {
context.pos++; context.pos++;
int newMode = HighLevelEncoder.lookAheadTest(context.msg, context.pos, getEncodingMode()); int newMode = HighLevelEncoder.lookAheadTest(context.getMessage(), context.pos, getEncodingMode());
if (newMode != getEncodingMode()) { if (newMode != getEncodingMode()) {
context.signalEncoderChange(newMode); context.signalEncoderChange(newMode);
break; break;
@ -43,7 +43,7 @@ final class Base256Encoder implements Encoder {
int lengthFieldSize = 1; int lengthFieldSize = 1;
int currentSize = context.getCodewordCount() + dataCount + lengthFieldSize; int currentSize = context.getCodewordCount() + dataCount + lengthFieldSize;
context.updateSymbolInfo(currentSize); context.updateSymbolInfo(currentSize);
boolean mustPad = (context.symbolInfo.dataCapacity - currentSize) > 0; boolean mustPad = (context.getSymbolInfo().getDataCapacity() - currentSize) > 0;
if (context.hasMoreCharacters() || mustPad) { if (context.hasMoreCharacters() || mustPad) {
if (dataCount <= 249) { if (dataCount <= 249) {
buffer.setCharAt(0, (char) dataCount); buffer.setCharAt(0, (char) dataCount);

View file

@ -37,7 +37,7 @@ class C40Encoder implements Encoder {
int curCodewordCount = context.getCodewordCount() + unwritten; int curCodewordCount = context.getCodewordCount() + unwritten;
context.updateSymbolInfo(curCodewordCount); context.updateSymbolInfo(curCodewordCount);
int available = context.symbolInfo.dataCapacity - curCodewordCount; int available = context.getSymbolInfo().getDataCapacity() - curCodewordCount;
if (!context.hasMoreCharacters()) { if (!context.hasMoreCharacters()) {
//Avoid having a single C40 value in the last triplet //Avoid having a single C40 value in the last triplet
@ -57,7 +57,7 @@ class C40Encoder implements Encoder {
int count = buffer.length(); int count = buffer.length();
if ((count % 3) == 0) { if ((count % 3) == 0) {
int newMode = HighLevelEncoder.lookAheadTest(context.msg, context.pos, getEncodingMode()); int newMode = HighLevelEncoder.lookAheadTest(context.getMessage(), context.pos, getEncodingMode());
if (newMode != getEncodingMode()) { if (newMode != getEncodingMode()) {
context.signalEncoderChange(newMode); context.signalEncoderChange(newMode);
break; break;
@ -95,7 +95,7 @@ class C40Encoder implements Encoder {
int curCodewordCount = context.getCodewordCount() + unwritten; int curCodewordCount = context.getCodewordCount() + unwritten;
context.updateSymbolInfo(curCodewordCount); context.updateSymbolInfo(curCodewordCount);
int available = context.symbolInfo.dataCapacity - curCodewordCount; int available = context.getSymbolInfo().getDataCapacity() - curCodewordCount;
if (rest == 2) { if (rest == 2) {
buffer.append('\0'); //Shift 1 buffer.append('\0'); //Shift 1

View file

@ -19,9 +19,7 @@ package com.google.zxing.datamatrix.encoder;
final class DataMatrixSymbolInfo144 extends SymbolInfo { final class DataMatrixSymbolInfo144 extends SymbolInfo {
DataMatrixSymbolInfo144() { DataMatrixSymbolInfo144() {
super(false, 1558, 620, 22, 22, 36); super(false, 1558, 620, 22, 22, 36, -1, 62);
this.rsBlockData = -1; //special! see below
this.rsBlockError = 62;
} }
@Override @Override

View file

@ -37,7 +37,7 @@ final class EdifactEncoder implements Encoder {
context.writeCodewords(encodeToCodewords(buffer, 0)); context.writeCodewords(encodeToCodewords(buffer, 0));
buffer.delete(0, 4); buffer.delete(0, 4);
int newMode = HighLevelEncoder.lookAheadTest(context.msg, context.pos, getEncodingMode()); int newMode = HighLevelEncoder.lookAheadTest(context.getMessage(), context.pos, getEncodingMode());
if (newMode != getEncodingMode()) { if (newMode != getEncodingMode()) {
context.signalEncoderChange(HighLevelEncoder.ASCII_ENCODATION); context.signalEncoderChange(HighLevelEncoder.ASCII_ENCODATION);
break; break;
@ -63,7 +63,7 @@ final class EdifactEncoder implements Encoder {
if (count == 1) { if (count == 1) {
//Only an unlatch at the end //Only an unlatch at the end
context.updateSymbolInfo(); context.updateSymbolInfo();
int available = context.symbolInfo.dataCapacity - context.getCodewordCount(); int available = context.getSymbolInfo().getDataCapacity() - context.getCodewordCount();
int remaining = context.getRemainingCharacters(); int remaining = context.getRemainingCharacters();
if (remaining == 0 && available <= 2) { if (remaining == 0 && available <= 2) {
return; //No unlatch return; //No unlatch
@ -80,7 +80,7 @@ final class EdifactEncoder implements Encoder {
if (restChars <= 2) { if (restChars <= 2) {
context.updateSymbolInfo(context.getCodewordCount() + restChars); context.updateSymbolInfo(context.getCodewordCount() + restChars);
int available = context.symbolInfo.dataCapacity - context.getCodewordCount(); int available = context.getSymbolInfo().getDataCapacity() - context.getCodewordCount();
if (available >= 3) { if (available >= 3) {
restInAscii = false; restInAscii = false;
context.updateSymbolInfo(context.getCodewordCount() + encoded.length()); context.updateSymbolInfo(context.getCodewordCount() + encoded.length());

View file

@ -22,14 +22,14 @@ import java.nio.charset.Charset;
final class EncoderContext { final class EncoderContext {
String msg; private final String msg;
private SymbolShapeHint shape; private SymbolShapeHint shape;
private Dimension minSize; private Dimension minSize;
private Dimension maxSize; private Dimension maxSize;
StringBuilder codewords; private final StringBuilder codewords;
int pos; int pos;
int newEncoding; private int newEncoding;
SymbolInfo symbolInfo; private SymbolInfo symbolInfo;
private int skipAtEnd; private int skipAtEnd;
EncoderContext(String msg) { EncoderContext(String msg) {
@ -74,6 +74,10 @@ final class EncoderContext {
return msg.charAt(pos); return msg.charAt(pos);
} }
public StringBuilder getCodewords() {
return codewords;
}
public void writeCodewords(String codewords) { public void writeCodewords(String codewords) {
this.codewords.append(codewords); this.codewords.append(codewords);
} }
@ -86,6 +90,10 @@ final class EncoderContext {
return this.codewords.length(); return this.codewords.length();
} }
public int getNewEncoding() {
return newEncoding;
}
public void signalEncoderChange(int encoding) { public void signalEncoderChange(int encoding) {
this.newEncoding = encoding; this.newEncoding = encoding;
} }
@ -106,12 +114,16 @@ final class EncoderContext {
return getTotalMessageCharCount() - pos; return getTotalMessageCharCount() - pos;
} }
public SymbolInfo getSymbolInfo() {
return symbolInfo;
}
public void updateSymbolInfo() { public void updateSymbolInfo() {
updateSymbolInfo(getCodewordCount()); updateSymbolInfo(getCodewordCount());
} }
public void updateSymbolInfo(int len) { public void updateSymbolInfo(int len) {
if (this.symbolInfo == null || len > this.symbolInfo.dataCapacity) { if (this.symbolInfo == null || len > this.symbolInfo.getDataCapacity()) {
this.symbolInfo = SymbolInfo.lookup(len, shape, minSize, maxSize, true); this.symbolInfo = SymbolInfo.lookup(len, shape, minSize, maxSize, true);
} }
} }

View file

@ -99,15 +99,15 @@ public final class ErrorCorrection {
* @return the codewords with interleaved error correction. * @return the codewords with interleaved error correction.
*/ */
public static String encodeECC200(String codewords, SymbolInfo symbolInfo) { public static String encodeECC200(String codewords, SymbolInfo symbolInfo) {
if (codewords.length() != symbolInfo.dataCapacity) { if (codewords.length() != symbolInfo.getDataCapacity()) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"The number of codewords does not match the selected symbol"); "The number of codewords does not match the selected symbol");
} }
StringBuilder sb = new StringBuilder(symbolInfo.dataCapacity + symbolInfo.errorCodewords); StringBuilder sb = new StringBuilder(symbolInfo.getDataCapacity() + symbolInfo.getErrorCodewords());
sb.append(codewords); sb.append(codewords);
int blockCount = symbolInfo.getInterleavedBlockCount(); int blockCount = symbolInfo.getInterleavedBlockCount();
if (blockCount == 1) { if (blockCount == 1) {
String ecc = createECCBlock(codewords, symbolInfo.errorCodewords); String ecc = createECCBlock(codewords, symbolInfo.getErrorCodewords());
sb.append(ecc); sb.append(ecc);
} else { } else {
sb.setLength(sb.capacity()); sb.setLength(sb.capacity());
@ -124,13 +124,13 @@ public final class ErrorCorrection {
} }
for (int block = 0; block < blockCount; block++) { for (int block = 0; block < blockCount; block++) {
StringBuilder temp = new StringBuilder(dataSizes[block]); StringBuilder temp = new StringBuilder(dataSizes[block]);
for (int d = block; d < symbolInfo.dataCapacity; d += blockCount) { for (int d = block; d < symbolInfo.getDataCapacity(); d += blockCount) {
temp.append(codewords.charAt(d)); temp.append(codewords.charAt(d));
} }
String ecc = createECCBlock(temp.toString(), errorSizes[block]); String ecc = createECCBlock(temp.toString(), errorSizes[block]);
int pos = 0; int pos = 0;
for (int e = block; e < errorSizes[block] * blockCount; e += blockCount) { for (int e = block; e < errorSizes[block] * blockCount; e += blockCount) {
sb.setCharAt(symbolInfo.dataCapacity + e, ecc.charAt(pos++)); sb.setCharAt(symbolInfo.getDataCapacity() + e, ecc.charAt(pos++));
} }
} }
} }

View file

@ -178,21 +178,21 @@ public final class HighLevelEncoder {
int encodingMode = ASCII_ENCODATION; //Default mode int encodingMode = ASCII_ENCODATION; //Default mode
while (context.hasMoreCharacters()) { while (context.hasMoreCharacters()) {
encoders[encodingMode].encode(context); encoders[encodingMode].encode(context);
if (context.newEncoding >= 0) { if (context.getNewEncoding() >= 0) {
encodingMode = context.newEncoding; encodingMode = context.getNewEncoding();
context.resetEncoderSignal(); context.resetEncoderSignal();
} }
} }
int len = context.codewords.length(); int len = context.getCodewordCount();
context.updateSymbolInfo(); context.updateSymbolInfo();
int capacity = context.symbolInfo.dataCapacity; int capacity = context.getSymbolInfo().getDataCapacity();
if (len < capacity) { if (len < capacity) {
if (encodingMode != ASCII_ENCODATION && encodingMode != BASE256_ENCODATION) { if (encodingMode != ASCII_ENCODATION && encodingMode != BASE256_ENCODATION) {
context.writeCodeword('\u00fe'); //Unlatch (254) context.writeCodeword('\u00fe'); //Unlatch (254)
} }
} }
//Padding //Padding
StringBuilder codewords = context.codewords; StringBuilder codewords = context.getCodewords();
if (codewords.length() < capacity) { if (codewords.length() < capacity) {
codewords.append(PAD); codewords.append(PAD);
} }
@ -200,7 +200,7 @@ public final class HighLevelEncoder {
codewords.append(randomize253State(PAD, codewords.length() + 1)); codewords.append(randomize253State(PAD, codewords.length() + 1));
} }
return context.codewords.toString(); return context.getCodewords().toString();
} }
static int lookAheadTest(CharSequence msg, int startpos, int currentMode) { static int lookAheadTest(CharSequence msg, int startpos, int currentMode) {

View file

@ -73,13 +73,13 @@ public class SymbolInfo {
} }
private final boolean rectangular; private final boolean rectangular;
final int dataCapacity; private final int dataCapacity;
final int errorCodewords; private final int errorCodewords;
public final int matrixWidth; public final int matrixWidth;
public final int matrixHeight; public final int matrixHeight;
private final int dataRegions; private final int dataRegions;
int rsBlockData; private int rsBlockData;
int rsBlockError; private int rsBlockError;
public SymbolInfo(boolean rectangular, int dataCapacity, int errorCodewords, public SymbolInfo(boolean rectangular, int dataCapacity, int errorCodewords,
int matrixWidth, int matrixHeight, int dataRegions) { int matrixWidth, int matrixHeight, int dataRegions) {
@ -87,7 +87,7 @@ public class SymbolInfo {
dataCapacity, errorCodewords); dataCapacity, errorCodewords);
} }
private SymbolInfo(boolean rectangular, int dataCapacity, int errorCodewords, SymbolInfo(boolean rectangular, int dataCapacity, int errorCodewords,
int matrixWidth, int matrixHeight, int dataRegions, int matrixWidth, int matrixHeight, int dataRegions,
int rsBlockData, int rsBlockError) { int rsBlockData, int rsBlockError) {
this.rectangular = rectangular; this.rectangular = rectangular;
@ -210,6 +210,14 @@ public class SymbolInfo {
return dataCapacity / rsBlockData; return dataCapacity / rsBlockData;
} }
public int getDataCapacity() {
return dataCapacity;
}
public int getErrorCodewords() {
return errorCodewords;
}
public int getDataLengthForInterleavedBlock(int index) { public int getDataLengthForInterleavedBlock(int index) {
return rsBlockData; return rsBlockData;
} }

View file

@ -37,7 +37,7 @@ final class X12Encoder extends C40Encoder {
if ((count % 3) == 0) { if ((count % 3) == 0) {
writeNextTriplet(context, buffer); writeNextTriplet(context, buffer);
int newMode = HighLevelEncoder.lookAheadTest(context.msg, context.pos, getEncodingMode()); int newMode = HighLevelEncoder.lookAheadTest(context.getMessage(), context.pos, getEncodingMode());
if (newMode != getEncodingMode()) { if (newMode != getEncodingMode()) {
context.signalEncoderChange(newMode); context.signalEncoderChange(newMode);
break; break;
@ -70,7 +70,7 @@ final class X12Encoder extends C40Encoder {
@Override @Override
void handleEOD(EncoderContext context, StringBuilder buffer) { void handleEOD(EncoderContext context, StringBuilder buffer) {
context.updateSymbolInfo(); context.updateSymbolInfo();
int available = context.symbolInfo.dataCapacity - context.getCodewordCount(); int available = context.getSymbolInfo().getDataCapacity() - context.getCodewordCount();
int count = buffer.length(); int count = buffer.length();
if (count == 2) { if (count == 2) {
context.writeCodeword(HighLevelEncoder.X12_UNLATCH); context.writeCodeword(HighLevelEncoder.X12_UNLATCH);

View file

@ -28,28 +28,28 @@ public final class SymbolInfoTestCase extends Assert {
@Test @Test
public void testSymbolInfo() { public void testSymbolInfo() {
SymbolInfo info = SymbolInfo.lookup(3); SymbolInfo info = SymbolInfo.lookup(3);
assertEquals(5, info.errorCodewords); assertEquals(5, info.getErrorCodewords());
assertEquals(8, info.matrixWidth); assertEquals(8, info.matrixWidth);
assertEquals(8, info.matrixHeight); assertEquals(8, info.matrixHeight);
assertEquals(10, info.getSymbolWidth()); assertEquals(10, info.getSymbolWidth());
assertEquals(10, info.getSymbolHeight()); assertEquals(10, info.getSymbolHeight());
info = SymbolInfo.lookup(3, SymbolShapeHint.FORCE_RECTANGLE); info = SymbolInfo.lookup(3, SymbolShapeHint.FORCE_RECTANGLE);
assertEquals(7, info.errorCodewords); assertEquals(7, info.getErrorCodewords());
assertEquals(16, info.matrixWidth); assertEquals(16, info.matrixWidth);
assertEquals(6, info.matrixHeight); assertEquals(6, info.matrixHeight);
assertEquals(18, info.getSymbolWidth()); assertEquals(18, info.getSymbolWidth());
assertEquals(8, info.getSymbolHeight()); assertEquals(8, info.getSymbolHeight());
info = SymbolInfo.lookup(9); info = SymbolInfo.lookup(9);
assertEquals(11, info.errorCodewords); assertEquals(11, info.getErrorCodewords());
assertEquals(14, info.matrixWidth); assertEquals(14, info.matrixWidth);
assertEquals(6, info.matrixHeight); assertEquals(6, info.matrixHeight);
assertEquals(32, info.getSymbolWidth()); assertEquals(32, info.getSymbolWidth());
assertEquals(8, info.getSymbolHeight()); assertEquals(8, info.getSymbolHeight());
info = SymbolInfo.lookup(9, SymbolShapeHint.FORCE_SQUARE); info = SymbolInfo.lookup(9, SymbolShapeHint.FORCE_SQUARE);
assertEquals(12, info.errorCodewords); assertEquals(12, info.getErrorCodewords());
assertEquals(14, info.matrixWidth); assertEquals(14, info.matrixWidth);
assertEquals(14, info.matrixHeight); assertEquals(14, info.matrixHeight);
assertEquals(16, info.getSymbolWidth()); assertEquals(16, info.getSymbolWidth());