From 0ccb52ab372ecda4a58ea5e075768d974872c893 Mon Sep 17 00:00:00 2001 From: "smparkes@smparkes.net" Date: Mon, 1 Apr 2013 06:01:12 +0000 Subject: [PATCH] c++ changes for r2517 git-svn-id: https://zxing.googlecode.com/svn/trunk@2611 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- cpp/core/src/zxing/oned/Code39Reader.cpp | 43 ++++++++++----------- cpp/core/src/zxing/oned/Code93Reader.cpp | 48 ++++++++++++++++-------- cpp/core/src/zxing/oned/Code93Reader.h | 7 +++- 3 files changed, 60 insertions(+), 38 deletions(-) diff --git a/cpp/core/src/zxing/oned/Code39Reader.cpp b/cpp/core/src/zxing/oned/Code39Reader.cpp index 18fbf111f..6cba557e8 100644 --- a/cpp/core/src/zxing/oned/Code39Reader.cpp +++ b/cpp/core/src/zxing/oned/Code39Reader.cpp @@ -89,41 +89,42 @@ Code39Reader::Code39Reader(bool usingCheckDigit_, bool extendedMode_) { } Ref Code39Reader::decodeRow(int rowNumber, Ref row) { + std::vector& theCounters (counters); { // Arrays.fill(counters, 0); - int size = counters.size(); - counters.resize(0); - counters.resize(size); } - decodeRowResult.clear(); + int size = theCounters.size(); + theCounters.resize(0); + theCounters.resize(size); } + std::string& result (decodeRowResult); + result.clear(); - vector start (findAsteriskPattern(row, counters)); - // Read off white space + vector start (findAsteriskPattern(row, theCounters)); + // Read off white space int nextStart = row->getNextSet(start[1]); int end = row->getSize(); char decodedChar; int lastStart; do { - recordPattern(row, nextStart, counters); - int pattern = toNarrowWidePattern(counters); + recordPattern(row, nextStart, theCounters); + int pattern = toNarrowWidePattern(theCounters); if (pattern < 0) { throw NotFoundException();; } decodedChar = patternToChar(pattern); - decodeRowResult.append(1, decodedChar); + result.append(1, decodedChar); lastStart = nextStart; - for (int i = 0, end=counters.size(); i < end; i++) { - nextStart += counters[i]; + for (int i = 0, end=theCounters.size(); i < end; i++) { + nextStart += theCounters[i]; } // Read off white space - nextStart = row->getNextSet(nextStart); } while (decodedChar != '*'); - decodeRowResult.resize(decodeRowResult.length()-1);// remove asterisk + result.resize(decodeRowResult.length()-1);// remove asterisk // Look for whitespace after pattern: int lastPatternSize = 0; - for (int i = 0, e = counters.size(); i < e; i++) { - lastPatternSize += counters[i]; + for (int i = 0, e = theCounters.size(); i < e; i++) { + lastPatternSize += theCounters[i]; } int whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize; // If 50% of last pattern size, following last pattern, is not whitespace, @@ -133,27 +134,27 @@ Ref Code39Reader::decodeRow(int rowNumber, Ref row) { } if (usingCheckDigit) { - int max = decodeRowResult.length() - 1; + int max = result.length() - 1; int total = 0; for (int i = 0; i < max; i++) { total += alphabet_string.find_first_of(decodeRowResult[i], 0); } - if (decodeRowResult[max] != ALPHABET[total % 43]) { + if (result[max] != ALPHABET[total % 43]) { throw ChecksumException(); } - decodeRowResult.resize(max); + result.resize(max); } - if (decodeRowResult.length() == 0) { + if (result.length() == 0) { // Almost false positive throw NotFoundException(); } Ref resultString; if (extendedMode) { - resultString = decodeExtended(decodeRowResult); + resultString = decodeExtended(result); } else { - resultString = Ref(new String(decodeRowResult)); + resultString = Ref(new String(result)); } float left = (float) (start[1] + start[0]) / 2.0f; diff --git a/cpp/core/src/zxing/oned/Code93Reader.cpp b/cpp/core/src/zxing/oned/Code93Reader.cpp index d174c32ec..8d43d3f66 100644 --- a/cpp/core/src/zxing/oned/Code93Reader.cpp +++ b/cpp/core/src/zxing/oned/Code93Reader.cpp @@ -56,27 +56,38 @@ namespace { const int ASTERISK_ENCODING = CHARACTER_ENCODINGS[47]; } +Code93Reader::Code93Reader() { + decodeRowResult.reserve(20); + counters.resize(6); +} + Ref Code93Reader::decodeRow(int rowNumber, Ref row) { Range start (findAsteriskPattern(row)); // Read off white space int nextStart = row->getNextSet(start[1]); int end = row->getSize(); - string result; - vector counters (6, 0); + vector& theCounters (counters); + { // Arrays.fill(counters, 0); + int size = theCounters.size(); + theCounters.resize(0); + theCounters.resize(size); } + string& result (decodeRowResult); + result.clear(); + char decodedChar; int lastStart; do { - recordPattern(row, nextStart, counters); - int pattern = toPattern(counters); + recordPattern(row, nextStart, theCounters); + int pattern = toPattern(theCounters); if (pattern < 0) { throw NotFoundException(); } decodedChar = patternToChar(pattern); result.append(1, decodedChar); lastStart = nextStart; - for(int i=0, e=counters.size(); i < e; ++i) { - nextStart += counters[i]; + for(int i=0, e=theCounters.size(); i < e; ++i) { + nextStart += theCounters[i]; } // Read off white space nextStart = row->getNextSet(nextStart); @@ -119,31 +130,36 @@ Code93Reader::Range Code93Reader::findAsteriskPattern(Ref row) { int width = row->getSize(); int rowOffset = row->getNextSet(0); - int counterPosition = 0; - vector counters (6, 0); + { // Arrays.fill(counters, 0); + int size = counters.size(); + counters.resize(0); + counters.resize(size); } + vector& theCounters (counters); + int patternStart = rowOffset; bool isWhite = false; - int patternLength = counters.size(); + int patternLength = theCounters.size(); + int counterPosition = 0; for (int i = rowOffset; i < width; i++) { if (row->get(i) ^ isWhite) { - counters[counterPosition]++; + theCounters[counterPosition]++; } else { if (counterPosition == patternLength - 1) { - if (toPattern(counters) == ASTERISK_ENCODING) { + if (toPattern(theCounters) == ASTERISK_ENCODING) { return Range(patternStart, i); } - patternStart += counters[0] + counters[1]; + patternStart += theCounters[0] + theCounters[1]; for (int y = 2; y < patternLength; y++) { - counters[y - 2] = counters[y]; + theCounters[y - 2] = theCounters[y]; } - counters[patternLength - 2] = 0; - counters[patternLength - 1] = 0; + theCounters[patternLength - 2] = 0; + theCounters[patternLength - 1] = 0; counterPosition--; } else { counterPosition++; } - counters[counterPosition] = 1; + theCounters[counterPosition] = 1; isWhite = !isWhite; } } diff --git a/cpp/core/src/zxing/oned/Code93Reader.h b/cpp/core/src/zxing/oned/Code93Reader.h index e5e0e62cc..a78c2c78d 100644 --- a/cpp/core/src/zxing/oned/Code93Reader.h +++ b/cpp/core/src/zxing/oned/Code93Reader.h @@ -37,10 +37,15 @@ namespace zxing { */ class zxing::oned::Code93Reader : public OneDReader { public: + Code93Reader(); Ref decodeRow(int rowNumber, Ref row); private: - static Range findAsteriskPattern(Ref row); + std::string decodeRowResult; + std::vector counters; + + Range findAsteriskPattern(Ref row); + static int toPattern(std::vector& counters); static char patternToChar(int pattern); static Ref decodeExtended(std::string const& encoded);