Bring C++ datamatrix to parity w/Java wrt to the blackbox test

Plus a little whitespace cleanup and make resultpoint immutable.

git-svn-id: https://zxing.googlecode.com/svn/trunk@2490 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
smparkes@smparkes.net 2012-10-30 21:03:31 +00:00
parent 7a82d685a3
commit c8e2721f7d
4 changed files with 155 additions and 138 deletions

View file

@ -19,6 +19,8 @@
namespace zxing {
NotFoundException::NotFoundException() {}
NotFoundException::NotFoundException(const char *msg)
: ReaderException(msg) {}

View file

@ -25,6 +25,7 @@ namespace zxing {
class NotFoundException : public ReaderException {
public:
NotFoundException();
NotFoundException(const char *msg);
~NotFoundException() throw();
};

View file

@ -28,8 +28,8 @@ namespace zxing {
class ResultPoint : public Counted {
protected:
float posX_;
float posY_;
const float posX_;
const float posY_;
public:
ResultPoint();

View file

@ -19,19 +19,33 @@
* limitations under the License.
*/
#include <map>
#include <zxing/ResultPoint.h>
#include <zxing/common/GridSampler.h>
#include <zxing/datamatrix/detector/Detector.h>
#include <zxing/common/detector/math_utils.h>
#include <zxing/NotFoundException.h>
#include <sstream>
#include <cstdlib>
namespace math_utils = zxing::common::detector::math_utils;
namespace zxing {
namespace datamatrix {
using zxing::Ref;
using zxing::BitMatrix;
using zxing::ResultPoint;
using zxing::DetectorResult;
using zxing::PerspectiveTransform;
using zxing::NotFoundException;
using zxing::datamatrix::Detector;
using zxing::datamatrix::ResultPointsAndTransitions;
using namespace std;
namespace {
typedef std::map<Ref<ResultPoint>, int> PointMap;
void increment(PointMap& table, Ref<ResultPoint> const& key) {
int& value = table[key];
value += 1;
}
}
ResultPointsAndTransitions::ResultPointsAndTransitions() {
Ref<ResultPoint> ref(new ResultPoint(0, 0));
@ -88,35 +102,37 @@ Ref<DetectorResult> Detector::detect() {
Ref<ResultPointsAndTransitions> lSideOne(transitions[0]);
Ref<ResultPointsAndTransitions> lSideTwo(transitions[1]);
// Figure out which point is their intersection by tallying up the number of times we see the
// endpoints in the four endpoints. One will show up twice.
typedef std::map<Ref<ResultPoint>, int> PointMap;
PointMap pointCount;
increment(pointCount, lSideOne->getFrom());
increment(pointCount, lSideOne->getTo());
increment(pointCount, lSideTwo->getFrom());
increment(pointCount, lSideTwo->getTo());
// Figure out which point is their intersection by tallying up the number of times we see the
// endpoints in the four endpoints. One will show up twice.
Ref<ResultPoint> maybeTopLeft;
Ref<ResultPoint> bottomLeft;
Ref<ResultPoint> maybeBottomRight;
if (lSideOne->getFrom()->equals(lSideOne->getTo())) {
bottomLeft = lSideOne->getFrom();
maybeTopLeft = lSideTwo->getFrom();
maybeBottomRight = lSideTwo->getTo();
} else if (lSideOne->getFrom()->equals(lSideTwo->getFrom())) {
bottomLeft = lSideOne->getFrom();
maybeTopLeft = lSideOne->getTo();
maybeBottomRight = lSideTwo->getTo();
} else if (lSideOne->getFrom()->equals(lSideTwo->getTo())) {
bottomLeft = lSideOne->getFrom();
maybeTopLeft = lSideOne->getTo();
maybeBottomRight = lSideTwo->getFrom();
} else if (lSideOne->getTo()->equals(lSideTwo->getFrom())) {
bottomLeft = lSideOne->getTo();
maybeTopLeft = lSideOne->getFrom();
maybeBottomRight = lSideTwo->getTo();
} else if (lSideOne->getTo()->equals(lSideTwo->getTo())) {
bottomLeft = lSideOne->getTo();
maybeTopLeft = lSideOne->getFrom();
maybeBottomRight = lSideTwo->getFrom();
for (PointMap::const_iterator entry = pointCount.begin(), end = pointCount.end(); entry != end; ++entry) {
Ref<ResultPoint> const& point = entry->first;
int value = entry->second;
if (value == 2) {
bottomLeft = point; // this is definitely the bottom left, then -- end of two L sides
} else {
bottomLeft = lSideTwo->getFrom();
maybeTopLeft = lSideOne->getTo();
maybeBottomRight = lSideOne->getFrom();
// Otherwise it's either top left or bottom right -- just assign the two arbitrarily now
if (maybeTopLeft == 0) {
maybeTopLeft = point;
} else {
maybeBottomRight = point;
}
}
}
if (maybeTopLeft == 0 || bottomLeft == 0 || maybeBottomRight == 0) {
throw NotFoundException();
}
// Bottom left is correct but top left and bottom right might be switched
@ -217,7 +233,7 @@ Ref<DetectorResult> Detector::detect() {
}
// Redetermine the dimension using the corrected top right point
int dimensionCorrected = max(transitionsBetween(topLeft, correctedTopRight)->getTransitions(),
int dimensionCorrected = std::max(transitionsBetween(topLeft, correctedTopRight)->getTransitions(),
transitionsBetween(bottomRight, correctedTopRight)->getTransitions());
dimensionCorrected++;
if ((dimensionCorrected & 0x01) == 1) {
@ -428,5 +444,3 @@ void Detector::insertionSort(std::vector<Ref<ResultPointsAndTransitions> > &vect
int Detector::compare(Ref<ResultPointsAndTransitions> a, Ref<ResultPointsAndTransitions> b) {
return a->getTransitions() - b->getTransitions();
}
}
}