From 1cd49dbcb1241402916d45000b870ca35386d65b Mon Sep 17 00:00:00 2001 From: "smparkes@smparkes.net" Date: Mon, 12 Dec 2011 22:38:50 +0000 Subject: [PATCH] Update C++ code from Java implementation, binarizer and ReedSolomon. Closes issue 1099. git-svn-id: https://zxing.googlecode.com/svn/trunk@2082 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../src/zxing/common/GlobalHistogramBinarizer.cpp | 8 ++++---- cpp/core/src/zxing/common/HybridBinarizer.cpp | 4 ---- .../zxing/common/reedsolomon/ReedSolomonDecoder.cpp | 11 ++++++++--- .../src/zxing/common/reedsolomon/ReedSolomonDecoder.h | 2 +- cpp/core/tests/src/common/BlackPointEstimatorTest.cpp | 2 +- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/cpp/core/src/zxing/common/GlobalHistogramBinarizer.cpp b/cpp/core/src/zxing/common/GlobalHistogramBinarizer.cpp index 2a1467331..ac5460f5d 100644 --- a/cpp/core/src/zxing/common/GlobalHistogramBinarizer.cpp +++ b/cpp/core/src/zxing/common/GlobalHistogramBinarizer.cpp @@ -64,7 +64,7 @@ Ref GlobalHistogramBinarizer::getBlackRow(int y, Ref row) { for (int x = 0; x < width; x++) { histogram[row_pixels[x] >> LUMINANCE_SHIFT]++; } - int blackPoint = estimate(histogram) << LUMINANCE_SHIFT; + int blackPoint = estimate(histogram); BitArray& array = *row; int left = row_pixels[0]; @@ -118,14 +118,14 @@ Ref GlobalHistogramBinarizer::getBlackMatrix() { } } - int blackPoint = estimate(histogram) << LUMINANCE_SHIFT; + int blackPoint = estimate(histogram); Ref matrix_ref(new BitMatrix(width, height)); BitMatrix& matrix = *matrix_ref; for (int y = 0; y < height; y++) { row = source.getRow(y, row); for (int x = 0; x < width; x++) { - if (row[x] <= blackPoint) + if (row[x] < blackPoint) matrix.set(x, y); } } @@ -199,7 +199,7 @@ int GlobalHistogramBinarizer::estimate(vector &histogram) { } } - return bestValley; + return bestValley << LUMINANCE_SHIFT; } Ref GlobalHistogramBinarizer::createBinarizer(Ref source) { diff --git a/cpp/core/src/zxing/common/HybridBinarizer.cpp b/cpp/core/src/zxing/common/HybridBinarizer.cpp index dc912c8ff..f30fc5be2 100644 --- a/cpp/core/src/zxing/common/HybridBinarizer.cpp +++ b/cpp/core/src/zxing/common/HybridBinarizer.cpp @@ -26,10 +26,6 @@ using namespace std; using namespace zxing; namespace { - const int LUMINANCE_BITS = 5; - const int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS; - const int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS; - const int BLOCK_SIZE_POWER = 3; const int BLOCK_SIZE = 1 << BLOCK_SIZE_POWER; const int BLOCK_SIZE_MASK = BLOCK_SIZE - 1; diff --git a/cpp/core/src/zxing/common/reedsolomon/ReedSolomonDecoder.cpp b/cpp/core/src/zxing/common/reedsolomon/ReedSolomonDecoder.cpp index 58a95a8e1..0a2974814 100644 --- a/cpp/core/src/zxing/common/reedsolomon/ReedSolomonDecoder.cpp +++ b/cpp/core/src/zxing/common/reedsolomon/ReedSolomonDecoder.cpp @@ -55,9 +55,10 @@ void ReedSolomonDecoder::decode(ArrayRef received, int twoS) { syndromeCoefficients.array_ << "\n"; #endif + bool dataMatrix = (&field == &GF256::DATA_MATRIX_FIELD); bool noError = true; for (int i = 0; i < twoS; i++) { - int eval = poly->evaluateAt(field.exp(i)); + int eval = poly->evaluateAt(field.exp(dataMatrix ? i + 1 : i)); syndromeCoefficients[syndromeCoefficients->size() - 1 - i] = eval; if (eval != 0) { noError = false; @@ -71,7 +72,7 @@ void ReedSolomonDecoder::decode(ArrayRef received, int twoS) { Ref monomial(field.buildMonomial(twoS, 1)); vector > sigmaOmega(runEuclideanAlgorithm(monomial, syndrome, twoS)); ArrayRef errorLocations = findErrorLocations(sigmaOmega[0]); - ArrayRef errorMagitudes = findErrorMagnitudes(sigmaOmega[1], errorLocations); + ArrayRef errorMagitudes = findErrorMagnitudes(sigmaOmega[1], errorLocations, dataMatrix); for (unsigned i = 0; i < errorLocations->size(); i++) { int position = received->size() - 1 - field.log(errorLocations[i]); //TODO: check why the position would be invalid @@ -173,7 +174,7 @@ ArrayRef ReedSolomonDecoder::findErrorLocations(Ref errorLocator return result; } -ArrayRef ReedSolomonDecoder::findErrorMagnitudes(Ref errorEvaluator, ArrayRef errorLocations) { +ArrayRef ReedSolomonDecoder::findErrorMagnitudes(Ref errorEvaluator, ArrayRef errorLocations, bool dataMatrix) { // This is directly applying Forney's Formula int s = errorLocations.size(); ArrayRef result(s); @@ -187,6 +188,10 @@ ArrayRef ReedSolomonDecoder::findErrorMagnitudes(Ref errorEvalua } } result[i] = field.multiply(errorEvaluator->evaluateAt(xiInverse), field.inverse(denominator)); + + if (dataMatrix) { + result[i] = field.multiply(result[i], xiInverse); + } } return result; } diff --git a/cpp/core/src/zxing/common/reedsolomon/ReedSolomonDecoder.h b/cpp/core/src/zxing/common/reedsolomon/ReedSolomonDecoder.h index f7ad98a8f..f6c7681f8 100644 --- a/cpp/core/src/zxing/common/reedsolomon/ReedSolomonDecoder.h +++ b/cpp/core/src/zxing/common/reedsolomon/ReedSolomonDecoder.h @@ -39,7 +39,7 @@ public: private: std::vector > runEuclideanAlgorithm(Ref a, Ref b, int R); ArrayRef findErrorLocations(Ref errorLocator); - ArrayRef findErrorMagnitudes(Ref errorEvaluator, ArrayRef errorLocations); + ArrayRef findErrorMagnitudes(Ref errorEvaluator, ArrayRef errorLocations, bool dataMatrix); }; } diff --git a/cpp/core/tests/src/common/BlackPointEstimatorTest.cpp b/cpp/core/tests/src/common/BlackPointEstimatorTest.cpp index f79b28a9e..1f44cf59a 100644 --- a/cpp/core/tests/src/common/BlackPointEstimatorTest.cpp +++ b/cpp/core/tests/src/common/BlackPointEstimatorTest.cpp @@ -30,7 +30,7 @@ void BlackPointEstimatorTest::testBasic() { int histogramRaw[] = { 0, 0, 11, 43, 37, 18, 3, 1, 0, 0, 13, 36, 24, 0, 11, 2 }; vector histogram(histogramRaw, histogramRaw+16); size_t point = GlobalHistogramBinarizer::estimate(histogram); - CPPUNIT_ASSERT_EQUAL((size_t)8, point); + CPPUNIT_ASSERT_EQUAL((size_t)64, point); } void BlackPointEstimatorTest::testTooLittleRange() {