fix various wrong abs; other cleanup

git-svn-id: https://zxing.googlecode.com/svn/trunk@2675 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
smparkes@smparkes.net 2013-04-21 20:01:55 +00:00
parent 64a5f4a664
commit c8a818982f
9 changed files with 105 additions and 101 deletions

View file

@ -28,7 +28,7 @@
#include <sstream> #include <sstream>
#include <cstdlib> #include <cstdlib>
using std::abs;
using zxing::Ref; using zxing::Ref;
using zxing::BitMatrix; using zxing::BitMatrix;
using zxing::ResultPoint; using zxing::ResultPoint;

View file

@ -16,21 +16,27 @@
*/ */
#include <cmath> #include <cmath>
#include <algorithm>
#include <zxing/multi/qrcode/detector/MultiFinderPatternFinder.h> #include <zxing/multi/qrcode/detector/MultiFinderPatternFinder.h>
#include <zxing/DecodeHints.h> #include <zxing/DecodeHints.h>
#include <zxing/ReaderException.h> #include <zxing/ReaderException.h>
/*
#include <algorithm>
#include <stdlib.h>
*/
using std::abs; using std::abs;
using zxing::multi::MultiFinderPatternFinder; using std::min;
using std::sort;
using std::vector;
using zxing::Ref; using zxing::Ref;
using zxing::BitMatrix;
using zxing::ReaderException;
using zxing::qrcode::FinderPattern; using zxing::qrcode::FinderPattern;
using zxing::qrcode::FinderPatternInfo; using zxing::qrcode::FinderPatternInfo;
using zxing::ReaderException; using zxing::multi::MultiFinderPatternFinder;
// VC++
using zxing::BitMatrix;
using zxing::ResultPointCallback;
using zxing::DecodeHints;
const float MultiFinderPatternFinder::MAX_MODULE_COUNT_PER_EDGE = 180; const float MultiFinderPatternFinder::MAX_MODULE_COUNT_PER_EDGE = 180;
const float MultiFinderPatternFinder::MIN_MODULE_COUNT_PER_EDGE = 9; const float MultiFinderPatternFinder::MIN_MODULE_COUNT_PER_EDGE = 9;
@ -54,7 +60,7 @@ MultiFinderPatternFinder::MultiFinderPatternFinder(Ref<BitMatrix> image,
MultiFinderPatternFinder::~MultiFinderPatternFinder(){} MultiFinderPatternFinder::~MultiFinderPatternFinder(){}
std::vector<Ref<FinderPatternInfo> > MultiFinderPatternFinder::findMulti(DecodeHints const& hints){ vector<Ref<FinderPatternInfo> > MultiFinderPatternFinder::findMulti(DecodeHints const& hints){
bool tryHarder = hints.getTryHarder(); bool tryHarder = hints.getTryHarder();
Ref<BitMatrix> image = image_; // Protected member Ref<BitMatrix> image = image_; // Protected member
int maxI = image->getHeight(); int maxI = image->getHeight();
@ -119,18 +125,18 @@ std::vector<Ref<FinderPatternInfo> > MultiFinderPatternFinder::findMulti(DecodeH
handlePossibleCenter(stateCount, i, maxJ); handlePossibleCenter(stateCount, i, maxJ);
} // end if foundPatternCross } // end if foundPatternCross
} // for i=iSkip-1 ... } // for i=iSkip-1 ...
std::vector<std::vector<Ref<FinderPattern> > > patternInfo = selectBestPatterns(); vector<vector<Ref<FinderPattern> > > patternInfo = selectBestPatterns();
std::vector<Ref<FinderPatternInfo> > result; vector<Ref<FinderPatternInfo> > result;
for (unsigned int i = 0; i < patternInfo.size(); i++) { for (unsigned int i = 0; i < patternInfo.size(); i++) {
std::vector<Ref<FinderPattern> > pattern = patternInfo[i]; vector<Ref<FinderPattern> > pattern = patternInfo[i];
pattern = FinderPatternFinder::orderBestPatterns(pattern); pattern = FinderPatternFinder::orderBestPatterns(pattern);
result.push_back(Ref<FinderPatternInfo>(new FinderPatternInfo(pattern))); result.push_back(Ref<FinderPatternInfo>(new FinderPatternInfo(pattern)));
} }
return result; return result;
} }
std::vector<std::vector<Ref<FinderPattern> > > MultiFinderPatternFinder::selectBestPatterns(){ vector<vector<Ref<FinderPattern> > > MultiFinderPatternFinder::selectBestPatterns(){
std::vector<Ref<FinderPattern> > possibleCenters = possibleCenters_; vector<Ref<FinderPattern> > possibleCenters = possibleCenters_;
int size = possibleCenters.size(); int size = possibleCenters.size();
@ -139,7 +145,7 @@ std::vector<std::vector<Ref<FinderPattern> > > MultiFinderPatternFinder::selectB
throw ReaderException("No code detected"); throw ReaderException("No code detected");
} }
std::vector<std::vector<Ref<FinderPattern> > > results; vector<vector<Ref<FinderPattern> > > results;
/* /*
* Begin HE modifications to safely detect multiple codes of equal size * Begin HE modifications to safely detect multiple codes of equal size
@ -151,7 +157,7 @@ std::vector<std::vector<Ref<FinderPattern> > > MultiFinderPatternFinder::selectB
// Sort by estimated module size to speed up the upcoming checks // Sort by estimated module size to speed up the upcoming checks
//TODO do a sort based on module size //TODO do a sort based on module size
std::sort(possibleCenters.begin(), possibleCenters.end(), compareModuleSize); sort(possibleCenters.begin(), possibleCenters.end(), compareModuleSize);
/* /*
* Now lets start: build a list of tuples of three finder locations that * Now lets start: build a list of tuples of three finder locations that
@ -173,7 +179,7 @@ std::vector<std::vector<Ref<FinderPattern> > > MultiFinderPatternFinder::selectB
for (int i2 = i1 + 1; i2 < (size - 1); i2++) { for (int i2 = i1 + 1; i2 < (size - 1); i2++) {
Ref<FinderPattern> p2 = possibleCenters[i2]; Ref<FinderPattern> p2 = possibleCenters[i2];
// Compare the expected module sizes; if they are really off, skip // Compare the expected module sizes; if they are really off, skip
float vModSize12 = (p1->getEstimatedModuleSize() - p2->getEstimatedModuleSize()) / std::min(p1->getEstimatedModuleSize(), p2->getEstimatedModuleSize()); float vModSize12 = (p1->getEstimatedModuleSize() - p2->getEstimatedModuleSize()) / min(p1->getEstimatedModuleSize(), p2->getEstimatedModuleSize());
float vModSize12A = abs(p1->getEstimatedModuleSize() - p2->getEstimatedModuleSize()); float vModSize12A = abs(p1->getEstimatedModuleSize() - p2->getEstimatedModuleSize());
if (vModSize12A > DIFF_MODSIZE_CUTOFF && vModSize12 >= DIFF_MODSIZE_CUTOFF_PERCENT) { if (vModSize12A > DIFF_MODSIZE_CUTOFF && vModSize12 >= DIFF_MODSIZE_CUTOFF_PERCENT) {
// break, since elements are ordered by the module size deviation there cannot be // break, since elements are ordered by the module size deviation there cannot be
@ -183,14 +189,14 @@ std::vector<std::vector<Ref<FinderPattern> > > MultiFinderPatternFinder::selectB
for (int i3 = i2 + 1; i3 < size; i3++) { for (int i3 = i2 + 1; i3 < size; i3++) {
Ref<FinderPattern> p3 = possibleCenters[i3]; Ref<FinderPattern> p3 = possibleCenters[i3];
// Compare the expected module sizes; if they are really off, skip // Compare the expected module sizes; if they are really off, skip
float vModSize23 = (p2->getEstimatedModuleSize() - p3->getEstimatedModuleSize()) / std::min(p2->getEstimatedModuleSize(), p3->getEstimatedModuleSize()); float vModSize23 = (p2->getEstimatedModuleSize() - p3->getEstimatedModuleSize()) / min(p2->getEstimatedModuleSize(), p3->getEstimatedModuleSize());
float vModSize23A = abs(p2->getEstimatedModuleSize() - p3->getEstimatedModuleSize()); float vModSize23A = abs(p2->getEstimatedModuleSize() - p3->getEstimatedModuleSize());
if (vModSize23A > DIFF_MODSIZE_CUTOFF && vModSize23 >= DIFF_MODSIZE_CUTOFF_PERCENT) { if (vModSize23A > DIFF_MODSIZE_CUTOFF && vModSize23 >= DIFF_MODSIZE_CUTOFF_PERCENT) {
// break, since elements are ordered by the module size deviation there cannot be // break, since elements are ordered by the module size deviation there cannot be
// any more interesting elements for the given p1. // any more interesting elements for the given p1.
break; break;
} }
std::vector<Ref<FinderPattern> > test; vector<Ref<FinderPattern> > test;
test.push_back(p1); test.push_back(p1);
test.push_back(p2); test.push_back(p2);
test.push_back(p3); test.push_back(p3);
@ -206,14 +212,14 @@ std::vector<std::vector<Ref<FinderPattern> > > MultiFinderPatternFinder::selectB
continue; continue;
} }
// Calculate the difference of the edge lengths in percent // Calculate the difference of the edge lengths in percent
float vABBC = abs((dA - dB) / std::min(dA, dB)); float vABBC = abs((dA - dB) / min(dA, dB));
if (vABBC >= 0.1f) { if (vABBC >= 0.1f) {
continue; continue;
} }
// Calculate the diagonal length by assuming a 90° angle at topleft // Calculate the diagonal length by assuming a 90° angle at topleft
float dCpy = (float) sqrt(dA * dA + dB * dB); float dCpy = (float) sqrt(dA * dA + dB * dB);
// Compare to the real distance in % // Compare to the real distance in %
float vPyC = abs((dC - dCpy) / std::min(dC, dCpy)); float vPyC = abs((dC - dCpy) / min(dC, dCpy));
if (vPyC >= 0.1f) { if (vPyC >= 0.1f) {
continue; continue;
} }

View file

@ -23,6 +23,7 @@
#include <zxing/common/detector/MathUtils.h> #include <zxing/common/detector/MathUtils.h>
using std::max; using std::max;
using std::abs;
using std::numeric_limits; using std::numeric_limits;
using zxing::pdf417::detector::Detector; using zxing::pdf417::detector::Detector;
using zxing::common::detector::Math; using zxing::common::detector::Math;

View file

@ -24,6 +24,7 @@
using std::map; using std::map;
using std::vector; using std::vector;
using std::min; using std::min;
using std::abs;
using zxing::pdf417::detector::LinesSampler; using zxing::pdf417::detector::LinesSampler;
using zxing::pdf417::decoder::BitMatrixParser; using zxing::pdf417::decoder::BitMatrixParser;
using zxing::Ref; using zxing::Ref;

View file

@ -21,10 +21,9 @@
#include <zxing/qrcode/detector/AlignmentPattern.h> #include <zxing/qrcode/detector/AlignmentPattern.h>
namespace zxing { using std::abs;
namespace qrcode { using zxing::Ref;
using zxing::qrcode::AlignmentPattern;
using namespace std;
AlignmentPattern::AlignmentPattern(float posX, float posY, float estimatedModuleSize) : AlignmentPattern::AlignmentPattern(float posX, float posY, float estimatedModuleSize) :
ResultPoint(posX,posY), estimatedModuleSize_(estimatedModuleSize) { ResultPoint(posX,posY), estimatedModuleSize_(estimatedModuleSize) {
@ -43,9 +42,6 @@ Ref<AlignmentPattern> AlignmentPattern::combineEstimate(float i, float j, float
float combinedY = (getY() + i) / 2.0f; float combinedY = (getY() + i) / 2.0f;
float combinedModuleSize = (estimatedModuleSize_ + newModuleSize) / 2.0f; float combinedModuleSize = (estimatedModuleSize_ + newModuleSize) / 2.0f;
Ref<AlignmentPattern> result Ref<AlignmentPattern> result
(new AlignmentPattern(combinedX, combinedY, combinedModuleSize)); (new AlignmentPattern(combinedX, combinedY, combinedModuleSize));
return result; return result;
} }
}
}

View file

@ -1,9 +1,5 @@
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*- // -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
/* /*
* AlignmentPatternFinder.cpp
* zxing
*
* Created by Christian Brunschen on 14/05/2008.
* Copyright 2008 ZXing authors All rights reserved. * Copyright 2008 ZXing authors All rights reserved.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
@ -19,19 +15,25 @@
* limitations under the License. * limitations under the License.
*/ */
#include "AlignmentPatternFinder.h" #include <zxing/qrcode/detector/AlignmentPatternFinder.h>
#include <zxing/ReaderException.h> #include <zxing/ReaderException.h>
#include <zxing/common/BitArray.h> #include <zxing/common/BitArray.h>
#include <vector> #include <vector>
#include <cmath> #include <cmath>
#include <cstdlib> #include <cstdlib>
namespace zxing { using std::abs;
namespace qrcode { using std::vector;
using zxing::Ref;
using zxing::qrcode::AlignmentPatternFinder;
using zxing::qrcode::AlignmentPattern;
using namespace std; // VC++
float AlignmentPatternFinder::centerFromEnd(vector<int> &stateCount, int end) { using zxing::BitMatrix;
using zxing::ResultPointCallback;
float AlignmentPatternFinder::centerFromEnd(vector<int>& stateCount, int end) {
return (float)(end - stateCount[2]) - stateCount[1] / 2.0f; return (float)(end - stateCount[2]) - stateCount[1] / 2.0f;
} }
@ -46,7 +48,7 @@ bool AlignmentPatternFinder::foundPatternCross(vector<int> &stateCount) {
} }
float AlignmentPatternFinder::crossCheckVertical(int startI, int centerJ, int maxCount, float AlignmentPatternFinder::crossCheckVertical(int startI, int centerJ, int maxCount,
int originalStateCountTotal) { int originalStateCountTotal) {
int maxI = image_->getHeight(); int maxI = image_->getHeight();
vector<int> stateCount(3, 0); vector<int> stateCount(3, 0);
@ -204,6 +206,3 @@ Ref<AlignmentPattern> AlignmentPatternFinder::find() {
throw zxing::ReaderException("Could not find alignment pattern"); throw zxing::ReaderException("Could not find alignment pattern");
} }
}
}

View file

@ -31,8 +31,8 @@
#include <sstream> #include <sstream>
#include <cstdlib> #include <cstdlib>
using std::ostringstream; using std::ostringstream;
using std::abs;
using std::min; using std::min;
using std::max; using std::max;
using zxing::isnan; using zxing::isnan;

View file

@ -21,57 +21,49 @@
#include <zxing/qrcode/detector/FinderPattern.h> #include <zxing/qrcode/detector/FinderPattern.h>
namespace zxing { using std::abs;
namespace qrcode { using zxing::Ref;
using zxing::qrcode::FinderPattern;
using namespace std; FinderPattern::FinderPattern(float posX, float posY, float estimatedModuleSize)
: ResultPoint(posX,posY), estimatedModuleSize_(estimatedModuleSize), count_(1) {}
FinderPattern::FinderPattern(float posX, float posY, float estimatedModuleSize) : FinderPattern::FinderPattern(float posX, float posY, float estimatedModuleSize, int count)
ResultPoint(posX,posY), estimatedModuleSize_(estimatedModuleSize), count_(1) { : ResultPoint(posX,posY), estimatedModuleSize_(estimatedModuleSize), count_(count) {}
// cerr << "fpc " << getX() << " " << getY() << " " << count_ << endl;
// cerr << "fp " << getX() << " " << getY() << endl;
}
FinderPattern::FinderPattern(float posX, float posY, float estimatedModuleSize, int count) : int FinderPattern::getCount() const {
ResultPoint(posX,posY), estimatedModuleSize_(estimatedModuleSize), count_(count) { return count_;
// cerr << "fpc " << getX() << " " << getY() << " " << count << endl; }
}
int FinderPattern::getCount() const { float FinderPattern::getEstimatedModuleSize() const {
return count_; return estimatedModuleSize_;
} }
float FinderPattern::getEstimatedModuleSize() const { void FinderPattern::incrementCount() {
return estimatedModuleSize_; count_++;
} // cerr << "ic " << getX() << " " << getY() << " " << count_ << endl;
}
void FinderPattern::incrementCount() {
count_++;
// cerr << "ic " << getX() << " " << getY() << " " << count_ << endl;
}
/* /*
bool FinderPattern::aboutEquals(float moduleSize, float i, float j) const { bool FinderPattern::aboutEquals(float moduleSize, float i, float j) const {
return abs(i - posY_) <= moduleSize && abs(j - posX_) <= moduleSize && (abs(moduleSize - estimatedModuleSize_) return abs(i - posY_) <= moduleSize && abs(j - posX_) <= moduleSize && (abs(moduleSize - estimatedModuleSize_)
<= 1.0f || abs(moduleSize - estimatedModuleSize_) / estimatedModuleSize_ <= 0.1f); <= 1.0f || abs(moduleSize - estimatedModuleSize_) / estimatedModuleSize_ <= 0.1f);
} }
*/ */
bool FinderPattern::aboutEquals(float moduleSize, float i, float j) const { bool FinderPattern::aboutEquals(float moduleSize, float i, float j) const {
if (abs(i - getY()) <= moduleSize && abs(j - getX()) <= moduleSize) { if (abs(i - getY()) <= moduleSize && abs(j - getX()) <= moduleSize) {
float moduleSizeDiff = abs(moduleSize - estimatedModuleSize_); float moduleSizeDiff = abs(moduleSize - estimatedModuleSize_);
return moduleSizeDiff <= 1.0f || moduleSizeDiff <= estimatedModuleSize_; return moduleSizeDiff <= 1.0f || moduleSizeDiff <= estimatedModuleSize_;
} }
return false; return false;
} }
Ref<FinderPattern> FinderPattern::combineEstimate(float i, float j, float newModuleSize) const { Ref<FinderPattern> FinderPattern::combineEstimate(float i, float j, float newModuleSize) const {
// fprintf(stderr, "ce %f %f %f\n", i, j, newModuleSize); // fprintf(stderr, "ce %f %f %f\n", i, j, newModuleSize);
int combinedCount = count_ + 1; int combinedCount = count_ + 1;
float combinedX = (count_ * getX() + j) / combinedCount; float combinedX = (count_ * getX() + j) / combinedCount;
float combinedY = (count_ * getY() + i) / combinedCount; float combinedY = (count_ * getY() + i) / combinedCount;
float combinedModuleSize = (count_ * getEstimatedModuleSize() + newModuleSize) / combinedCount; float combinedModuleSize = (count_ * getEstimatedModuleSize() + newModuleSize) / combinedCount;
return Ref<FinderPattern>(new FinderPattern(combinedX, combinedY, combinedModuleSize, combinedCount)); return Ref<FinderPattern>(new FinderPattern(combinedX, combinedY, combinedModuleSize, combinedCount));
}
}
} }

View file

@ -19,18 +19,28 @@
* limitations under the License. * limitations under the License.
*/ */
#include <algorithm>
#include <zxing/qrcode/detector/FinderPatternFinder.h> #include <zxing/qrcode/detector/FinderPatternFinder.h>
#include <zxing/ReaderException.h> #include <zxing/ReaderException.h>
#include <zxing/DecodeHints.h> #include <zxing/DecodeHints.h>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <algorithm>
namespace zxing { using std::sort;
namespace qrcode { using std::max;
using std::abs;
using std::vector;
using zxing::Ref;
using zxing::qrcode::FinderPatternFinder;
using zxing::qrcode::FinderPattern;
using zxing::qrcode::FinderPatternInfo;
using namespace std; // VC++
using zxing::BitMatrix;
using zxing::ResultPointCallback;
using zxing::ResultPoint;
using zxing::DecodeHints;
namespace {
class FurthestFromAverageComparator { class FurthestFromAverageComparator {
private: private:
@ -64,6 +74,8 @@ public:
} }
}; };
}
int FinderPatternFinder::CENTER_QUORUM = 2; int FinderPatternFinder::CENTER_QUORUM = 2;
int FinderPatternFinder::MIN_SKIP = 3; int FinderPatternFinder::MIN_SKIP = 3;
int FinderPatternFinder::MAX_MODULES = 57; int FinderPatternFinder::MAX_MODULES = 57;
@ -308,7 +320,7 @@ bool FinderPatternFinder::haveMultiplyConfirmedCenters() {
return totalDeviation <= 0.05f * totalModuleSize; return totalDeviation <= 0.05f * totalModuleSize;
} }
vector<Ref<FinderPattern> > FinderPatternFinder::selectBestPatterns() { vector< Ref<FinderPattern> > FinderPatternFinder::selectBestPatterns() {
size_t startSize = possibleCenters_.size(); size_t startSize = possibleCenters_.size();
if (startSize < 3) { if (startSize < 3) {
@ -542,9 +554,6 @@ Ref<BitMatrix> FinderPatternFinder::getImage() {
return image_; return image_;
} }
std::vector<Ref<FinderPattern> >& FinderPatternFinder::getPossibleCenters() { vector<Ref<FinderPattern> >& FinderPatternFinder::getPossibleCenters() {
return possibleCenters_; return possibleCenters_;
} }
}
}