#include <zxing/ZXing.h>
#include <zxing/oned/Code128Reader.h>
#include <zxing/oned/OneDResultPoint.h>
#include <zxing/common/Array.h>
#include <zxing/ReaderException.h>
#include <zxing/NotFoundException.h>
#include <zxing/FormatException.h>
#include <zxing/ChecksumException.h>
#include <zxing/DecodeHints.h>
#include <zxing/BarcodeFormat.h>
#include <math.h>
#include <string.h>
#include <sstream>
#include <algorithm>
using std::vector;
using std::string;
using zxing::NotFoundException;
using zxing::FormatException;
using zxing::ChecksumException;
using zxing::Ref;
using zxing::Result;
using zxing::oned::Code128Reader;
using zxing::BitArray;
const int Code128Reader::MAX_AVG_VARIANCE = int(PATTERN_MATCH_RESULT_SCALE_FACTOR * 250/1000);
const int Code128Reader::MAX_INDIVIDUAL_VARIANCE = int(PATTERN_MATCH_RESULT_SCALE_FACTOR * 700/1000);
namespace {
const int CODE_SHIFT = 98;
const int CODE_CODE_C = 99;
const int CODE_CODE_B = 100;
const int CODE_CODE_A = 101;
const int CODE_FNC_1 = 102;
const int CODE_FNC_2 = 97;
const int CODE_FNC_3 = 96;
const int CODE_FNC_4_A = 101;
const int CODE_FNC_4_B = 100;
const int CODE_START_A = 103;
const int CODE_START_B = 104;
const int CODE_START_C = 105;
const int CODE_STOP = 106;
const int CODE_PATTERNS_LENGTH = 107;
const int CODE_PATTERNS[CODE_PATTERNS_LENGTH][6] = {
{2, 1, 2, 2, 2, 2},
{2, 2, 2, 1, 2, 2},
{2, 2, 2, 2, 2, 1},
{1, 2, 1, 2, 2, 3},
{1, 2, 1, 3, 2, 2},
{1, 3, 1, 2, 2, 2},
{1, 2, 2, 2, 1, 3},
{1, 2, 2, 3, 1, 2},
{1, 3, 2, 2, 1, 2},
{2, 2, 1, 2, 1, 3},
{2, 2, 1, 3, 1, 2},
{2, 3, 1, 2, 1, 2},
{1, 1, 2, 2, 3, 2},
{1, 2, 2, 1, 3, 2},
{1, 2, 2, 2, 3, 1},
{1, 1, 3, 2, 2, 2},
{1, 2, 3, 1, 2, 2},
{1,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1, 2, 2, 1, 1, 4},
{1, 2, 2, 4, 1, 1},
{1, 4, 2, 1, 1, 2},
{1, 4, 2, 2, 1, 1},
{2, 4, 1, 2, 1, 1},
{2, 2, 1, 1, 1, 4},
{4, 1, 3, 1, 1, 1},
{2, 4, 1, 1, 1, 2},
{1, 3, 4, 1, 1, 1},
{1, 1, 1, 2, 4, 2},
{1, 2, 1, 1, 4, 2},
{1, 2, 1, 2, 4, 1},
{1, 1, 4, 2, 1, 2},
{1, 2, 4, 1, 1, 2},
{1, 2, 4, 2, 1, 1},
{4, 1, 1, 2, 1, 2},
{4, 2, 1, 1, 1, 2},
{4, 2, 1, 2, 1, 1},
{2, 1, 2, 1, 4, 1},
{2, 1, 4, 1, 2, 1},
{4, 1, 2, 1, 2, 1},
{1, 1, 1, 1, 4, 3},
{1, 1, 1, 3, 4, 1},
{1, 3, 1, 1, 4, 1},
{1, 1, 4, 1, 1, 3},
{1, 1, 4, 3, 1, 1},
{4, 1, 1, 1, 1, 3},
{4, 1, 1, 3, 1, 1},
{1, 1, 3, 1, 4, 1},
{1, 1, 4, 1, 3, 1},
{3, 1, 1, 1, 4, 1},
{4, 1, 1, 1, 3, 1},
{2, 1, 1, 4, 1, 2},
{2, 1, 1, 2, 1, 4},
{2, 1, 1, 2, 3, 2},
{2, 3, 3, 1, 1, 1}
};
}
Code128Reader::Code128Reader(){}
vector<int> Code128Reader::findStartPattern(Ref<BitArray> row){
int width = row->getSize();
int rowOffset = row->getNextSet(0);
int counterPosition = 0;
vector<int> counters (6, 0);
int patternStart = rowOffset;
bool isWhite = false;
int patternLength = counters.size();
for (int i = rowOffset; i < width; i++) {
if (row->get(i) ^ isWhite) {
counters[counterPosition]++;
} else {
if (counterPosition == patternLength - 1) {
int bestVariance = MAX_AVG_VARIANCE;
int bestMatch = -1;
for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) {
int variance = patternMatchVariance(counters, CODE_PATTERNS[startCode], MAX_INDIVIDUAL_VARIANCE);
if (variance < bestVariance) {
bestVariance = variance;
bestMatch = startCode;
}
}
if (bestMatch >= 0 &&
row->isRange(std::max(0, patternStart - (i - patternStart) / 2), patternStart, false)) {
vector<int> resultValue (3, 0);
resultValue[0] = patternStart;
resultValue[1] = i;
resultValue[2] = bestMatch;
return resultValue;
}
patternStart += counters[0] + counters[1];
for (int y = 2; y < patternLength; y++) {
counters[y - 2] = counters[y];
}
counters[patternLength - 2] = 0;
counters[patternLength - 1] = 0;
counterPosition--;
} else {
counterPosition++;
}
counters[counterPosition] = 1;
isWhite = !isWhite;
}
}
throw NotFoundException();
}
int Code128Reader::decodeCode(Ref<BitArray> row, vector<int>& counters, int rowOffset) {
recordPattern(row, rowOffset, counters);
int bestVariance = MAX_AVG_VARIANCE;
int bestMatch = -1;
for (int d = 0; d < CODE_PATTERNS_LENGTH; d++) {
int const* const pattern = CODE_PATTERNS[d];
int variance = patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE);
if (variance < bestVariance) {
bestVariance = variance;
bestMatch = d;
}
}
if (bestMatch >= 0) {
return bestMatch;
} else {
throw NotFoundException();
}
}
Ref<Result> Code128Reader::decodeRow(int rowNumber, Ref<BitArray> row, zxing::DecodeHints hints) {
bool convertFNC1 = hints.containsFormat(zxing::BarcodeFormat(zxing::BarcodeFormat::Value::ASSUME_GS1));
vector<int> startPatternInfo (findStartPattern(row));
int startCode = startPatternInfo[2];
int codeSet;
switch (startCode) {
case CODE_START_A:
codeSet = CODE_CODE_A;
break;
case CODE_START_B:
codeSet = CODE_CODE_B;
break;
case CODE_START_C:
codeSet = CODE_CODE_C;
break;
default:
throw FormatException();
}
bool done = false;
bool isNextShifted = false;
string result;
vector<byte> rawCodes(20, 0);
int lastStart = startPatternInfo[0];
int nextStart = startPatternInfo[1];
vector<int> counters (6, 0);
int lastCode = 0;
int code = 0;
int checksumTotal = startCode;
int multiplier = 0;
bool lastCharacterWasPrintable = true;
bool upperMode = false;
bool shiftUpperMode = false;
std::ostringstream oss;
while (!done) {
bool unshift = isNextShifted;
isNextShifted = false;
lastCode = code;
code = decodeCode(row, counters, nextStart);
if (code != CODE_STOP) {
lastCharacterWasPrintable = true;
}
if (code != CODE_STOP) {
multiplier++;
checksumTotal += multiplier * code;
}
lastStart = nextStart;
for (int i = 0, e = counters.size(); i < e; i++) {
nextStart += counters[i];
}
switch (code) {
case CODE_START_A:
case CODE_START_B:
case CODE_START_C:
throw FormatException();
}
switch (codeSet) {
case CODE_CODE_A:
if (code < 64) {
if (shiftUpperMode == upperMode) {
result.append(1,(byte) (' ' + code));
} else {
result.append(1,(byte) (' ' + code + 128));
}
shiftUpperMode = false;
} else if (code < 96) {
if (shiftUpperMode == upperMode) {
result.append(1, (byte) (code - 64));
} else {
result.append(1, (byte) (code + 64));
}
shiftUpperMode = false;
} else {
if (code != CODE_STOP) {
lastCharacterWasPrintable = false;
}
switch (code) {
case CODE_FNC_1:
if (convertFNC1) {
if (result.length() == 0){
result.append("]C1");
} else {
result.append(1, (byte) 29);
}
}
break;
case CODE_FNC_2:
case CODE_FNC_3:
break;
case CODE_FNC_4_A:
if (!upperMode && shiftUpperMode) {
upperMode = true;
shiftUpperMode = false;
} else if (upperMode && shiftUpperMode) {
upperMode = false;
shiftUpperMode = false;
} else {
shiftUpperMode = true;
}
break;
case CODE_SHIFT:
isNextShifted = true;
codeSet = CODE_CODE_B;
break;
case CODE_CODE_B:
codeSet = CODE_CODE_B;
break;
case CODE_CODE_C:
codeSet = CODE_CODE_C;
break;
case CODE_STOP:
done = true;
break;
}
}
break;
case CODE_CODE_B:
if (code < 96) {
if (shiftUpperMode == upperMode) {
result.append(1, (byte) (' ' + code));
} else {
result.append(1, (byte) (' ' + code + 128));
}
shiftUpperMode = false;
} else {
if (code != CODE_STOP) {
lastCharacterWasPrintable = false;
}
switch (code) {
case CODE_FNC_1:
if (convertFNC1) {
if (result.length() == 0) {
result.append("]C1");
} else {
result.append(1, (byte) 29);
}
}
break;
case CODE_FNC_2:
case CODE_FNC_3:
break;
case CODE_FNC_4_B:
if (!upperMode && shiftUpperMode) {
upperMode = true;
shiftUpperMode = false;
} else if (upperMode && shiftUpperMode) {
upperMode = false;
shiftUpperMode = false;
} else {
shiftUpperMode = true;
}
break;
case CODE_SHIFT:
isNextShifted = true;
codeSet = CODE_CODE_A;
break;
case CODE_CODE_A:
codeSet = CODE_CODE_A;
break;
case CODE_CODE_C:
codeSet = CODE_CODE_C;
break;
case CODE_STOP:
done = true;
break;
}
}
break;
case CODE_CODE_C:
if (code < 100) {
if (code < 10) {
result.append(1, '0');
}
oss.clear();
oss.str("");
oss << code;
result.append(oss.str());
} else {
if (code != CODE_STOP) {
lastCharacterWasPrintable = false;
}
switch (code) {
case CODE_FNC_1:
if (convertFNC1) {
if (result.length() == 0) {
result.append("]C1");
} else {
result.append(1, (byte) 29);
}
}
break;
case CODE_CODE_A:
codeSet = CODE_CODE_A;
break;
case CODE_CODE_B:
codeSet = CODE_CODE_B;
break;
case CODE_STOP:
done = true;
break;
}
}
break;
}
if (unshift) {
codeSet = codeSet == CODE_CODE_A ? CODE_CODE_B : CODE_CODE_A;
}
}
int lastPatternSize = nextStart - lastStart;
nextStart = row->getNextUnset(nextStart);
if (!row->isRange(nextStart,
std::min(row->getSize(), nextStart + (nextStart - lastStart) / 2),
false)) {
throw NotFoundException();
}
checksumTotal -= multiplier * lastCode;
if (checksumTotal % 103 != lastCode) {
throw ChecksumException();
}
int resultLength = result.length();
if (resultLength == 0) {
throw NotFoundException();
}
if (resultLength > 0 && lastCharacterWasPrintable) {
if (codeSet == CODE_CODE_C) {
result.erase(resultLength - 2, resultLength);
} else {
result.erase(resultLength - 1, resultLength);
}
}
float left = (float) (startPatternInfo[1] + startPatternInfo[0]) / 2.0f;
float right = lastStart + lastPatternSize / 2.0f;
int rawCodesSize = rawCodes.size();
ArrayRef<byte> rawBytes (rawCodesSize);
for (int i = 0; i < rawCodesSize; i++) {
rawBytes[i] = rawCodes[i];
}
ArrayRef< Ref<ResultPoint> > resultPoints(2);
resultPoints[0] =
Ref<OneDResultPoint>(new OneDResultPoint(left, (float) rowNumber));
resultPoints[1] =
Ref<OneDResultPoint>(new OneDResultPoint(right, (float) rowNumber));
return Ref<Result>(new Result(Ref<String>(new String(result)), rawBytes, resultPoints,
BarcodeFormat::CODE_128));
}
Code128Reader::~Code128Reader(){}
zxing::BarcodeFormat Code128Reader::getBarcodeFormat(){
return BarcodeFormat::CODE_128;
}