C++ changes for r2294

git-svn-id: https://zxing.googlecode.com/svn/trunk@2332 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
smparkes@smparkes.net 2012-06-26 18:14:23 +00:00
parent 7890d40026
commit afb81b1625
8 changed files with 100 additions and 63 deletions

View file

@ -1,3 +1,4 @@
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
/* /*
* ResultPoint.cpp * ResultPoint.cpp
* zxing * zxing
@ -19,7 +20,9 @@
*/ */
#include <zxing/ResultPoint.h> #include <zxing/ResultPoint.h>
#include <math.h> #include <zxing/common/detector/math_utils.h>
namespace math_utils = zxing::common::detector::math_utils;
namespace zxing { namespace zxing {
@ -82,8 +85,11 @@ void ResultPoint::orderBestPatterns(std::vector<Ref<ResultPoint> > &patterns) {
patterns[2] = pointC; patterns[2] = pointC;
} }
float ResultPoint::distance(Ref<ResultPoint> point1, Ref<ResultPoint> point2) { float ResultPoint::distance(Ref<ResultPoint> pattern1, Ref<ResultPoint> pattern2) {
return distance(point1->getX(), point1->getY(), point2->getX(), point2->getY()); return math_utils::distance(pattern1->posX_,
pattern1->posY_,
pattern2->posX_,
pattern2->posY_);
} }
float ResultPoint::distance(float x1, float x2, float y1, float y2) { float ResultPoint::distance(float x1, float x2, float y1, float y2) {

View file

@ -26,7 +26,7 @@
#include <zxing/common/reedsolomon/ReedSolomonException.h> #include <zxing/common/reedsolomon/ReedSolomonException.h>
#include <zxing/common/reedsolomon/GenericGF.h> #include <zxing/common/reedsolomon/GenericGF.h>
#include <iostream> #include <iostream>
#include <cmath> #include <zxing/common/detector/math_utils.h>
#include <zxing/NotFoundException.h> #include <zxing/NotFoundException.h>
using zxing::aztec::Detector; using zxing::aztec::Detector;
@ -36,6 +36,7 @@ using zxing::Ref;
using zxing::ResultPoint; using zxing::ResultPoint;
using zxing::BitArray; using zxing::BitArray;
using zxing::BitMatrix; using zxing::BitMatrix;
namespace math_utils = zxing::common::detector::math_utils;
Detector::Detector(Ref<BitMatrix> image): Detector::Detector(Ref<BitMatrix> image):
image_(image), image_(image),
@ -136,21 +137,21 @@ std::vector<Ref<ResultPoint> > Detector::getMatrixCornerPoints(std::vector<Ref<P
int dy = bullEyeCornerPoints[0]->y - bullEyeCornerPoints[2]->y; int dy = bullEyeCornerPoints[0]->y - bullEyeCornerPoints[2]->y;
dy += dy > 0 ? 1 : -1; dy += dy > 0 ? 1 : -1;
int targetcx = ROUND(bullEyeCornerPoints[2]->x - ratio * dx); int targetcx = math_utils::round(bullEyeCornerPoints[2]->x - ratio * dx);
int targetcy = ROUND(bullEyeCornerPoints[2]->y - ratio * dy); int targetcy = math_utils::round(bullEyeCornerPoints[2]->y - ratio * dy);
int targetax = ROUND(bullEyeCornerPoints[0]->x + ratio * dx); int targetax = math_utils::round(bullEyeCornerPoints[0]->x + ratio * dx);
int targetay = ROUND(bullEyeCornerPoints[0]->y + ratio * dy); int targetay = math_utils::round(bullEyeCornerPoints[0]->y + ratio * dy);
dx = bullEyeCornerPoints[1]->x - bullEyeCornerPoints[3]->x; dx = bullEyeCornerPoints[1]->x - bullEyeCornerPoints[3]->x;
dx += dx > 0 ? 1 : -1; dx += dx > 0 ? 1 : -1;
dy = bullEyeCornerPoints[1]->y - bullEyeCornerPoints[3]->y; dy = bullEyeCornerPoints[1]->y - bullEyeCornerPoints[3]->y;
dy += dy > 0 ? 1 : -1; dy += dy > 0 ? 1 : -1;
int targetdx = ROUND(bullEyeCornerPoints[3]->x - ratio * dx); int targetdx = math_utils::round(bullEyeCornerPoints[3]->x - ratio * dx);
int targetdy = ROUND(bullEyeCornerPoints[3]->y - ratio * dy); int targetdy = math_utils::round(bullEyeCornerPoints[3]->y - ratio * dy);
int targetbx = ROUND(bullEyeCornerPoints[1]->x + ratio * dx); int targetbx = math_utils::round(bullEyeCornerPoints[1]->x + ratio * dx);
int targetby = ROUND(bullEyeCornerPoints[1]->y + ratio * dy); int targetby = math_utils::round(bullEyeCornerPoints[1]->y + ratio * dy);
if (!isValid(targetax, targetay) || if (!isValid(targetax, targetay) ||
!isValid(targetbx, targetby) || !isValid(targetbx, targetby) ||
@ -262,18 +263,18 @@ std::vector<Ref<Point> > Detector::getBullEyeCornerPoints(Ref<zxing::aztec::Poin
int dx = pina->x - pind->x; int dx = pina->x - pind->x;
int dy = pina->y - pinc->y; int dy = pina->y - pinc->y;
int targetcx = ROUND(pinc->x - ratio * dx); int targetcx = math_utils::round(pinc->x - ratio * dx);
int targetcy = ROUND(pinc->y - ratio * dy); int targetcy = math_utils::round(pinc->y - ratio * dy);
int targetax = ROUND(pina->x + ratio * dx); int targetax = math_utils::round(pina->x + ratio * dx);
int targetay = ROUND(pina->y + ratio * dy); int targetay = math_utils::round(pina->y + ratio * dy);
dx = pinb->x - pind->x; dx = pinb->x - pind->x;
dy = pinb->y - pind->y; dy = pinb->y - pind->y;
int targetdx = ROUND(pind->x - ratio * dx); int targetdx = math_utils::round(pind->x - ratio * dx);
int targetdy = ROUND(pind->y - ratio * dy); int targetdy = math_utils::round(pind->y - ratio * dy);
int targetbx = ROUND(pinb->x + ratio * dx); int targetbx = math_utils::round(pinb->x + ratio * dx);
int targetby = ROUND(pinb->y + ratio * dy); int targetby = math_utils::round(pinb->y + ratio * dy);
if (!isValid(targetax, targetay) || if (!isValid(targetax, targetay) ||
!isValid(targetbx, targetby) || !isValid(targetbx, targetby) ||
@ -314,8 +315,8 @@ Ref<Point> Detector::getMatrixCenter() {
} }
int cx = ROUND((pointA->getX() + pointD->getX() + pointB->getX() + pointC->getX()) / 4); int cx = math_utils::round((pointA->getX() + pointD->getX() + pointB->getX() + pointC->getX()) / 4);
int cy = ROUND((pointA->getY() + pointD->getY() + pointB->getY() + pointC->getY()) / 4); int cy = math_utils::round((pointA->getY() + pointD->getY() + pointB->getY() + pointC->getY()) / 4);
try { try {
@ -334,8 +335,8 @@ Ref<Point> Detector::getMatrixCenter() {
} }
cx = ROUND((pointA->getX() + pointD->getX() + pointB->getX() + pointC->getX()) / 4); cx = math_utils::round((pointA->getX() + pointD->getX() + pointB->getX() + pointC->getX()) / 4);
cy = ROUND((pointA->getY() + pointD->getY() + pointB->getY() + pointC->getY()) / 4); cy = math_utils::round((pointA->getY() + pointD->getY() + pointB->getY() + pointC->getY()) / 4);
return Ref<Point>(new Point(cx, cy)); return Ref<Point>(new Point(cx, cy));
@ -424,7 +425,7 @@ Ref<BitArray> Detector::sampleLine(Ref<zxing::aztec::Point> p1, Ref<zxing::aztec
float py = p1->y; float py = p1->y;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
if (image_->get(ROUND(px), ROUND(py))) res->set(i); if (image_->get(math_utils::round(px), math_utils::round(py))) res->set(i);
px += dx; px += dx;
py += dy; py += dy;
} }
@ -486,7 +487,7 @@ int Detector::getColor(Ref<zxing::aztec::Point> p1, Ref<zxing::aztec::Point> p2)
for (int i = 0; i < d; i++) { for (int i = 0; i < d; i++) {
px += dx; px += dx;
py += dy; py += dy;
if (image_->get(ROUND(px), ROUND(py)) != colorModel) { if (image_->get(math_utils::round(px), math_utils::round(py)) != colorModel) {
error ++; error ++;
} }
} }

View file

@ -28,8 +28,6 @@
#include <zxing/DecodeHints.h> #include <zxing/DecodeHints.h>
#include <zxing/aztec/AztecDetectorResult.h> #include <zxing/aztec/AztecDetectorResult.h>
#define ROUND(a) ((int)(a + 0.5f))
namespace zxing { namespace zxing {
namespace aztec { namespace aztec {

View file

@ -1,3 +1,4 @@
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
/* /*
* WhiteRectangleDetector.cpp * WhiteRectangleDetector.cpp
* y_wmk * y_wmk
@ -20,9 +21,11 @@
#include <zxing/NotFoundException.h> #include <zxing/NotFoundException.h>
#include <zxing/common/detector/WhiteRectangleDetector.h> #include <zxing/common/detector/WhiteRectangleDetector.h>
#include <math.h> #include <zxing/common/detector/math_utils.h>
#include <sstream> #include <sstream>
namespace math_utils = zxing::common::detector::math_utils;
namespace zxing { namespace zxing {
using namespace std; using namespace std;
@ -222,21 +225,14 @@ std::vector<Ref<ResultPoint> > WhiteRectangleDetector::detect() {
} }
} }
/**
* Ends up being a bit faster than Math.round(). This merely rounds its
* argument to the nearest int, where x.5 rounds up.
*/
int WhiteRectangleDetector::round(float d) {
return (int) (d + 0.5f);
}
Ref<ResultPoint> WhiteRectangleDetector::getBlackPointOnSegment(float aX, float aY, float bX, float bY) { Ref<ResultPoint> WhiteRectangleDetector::getBlackPointOnSegment(float aX, float aY, float bX, float bY) {
int dist = distanceL2(aX, aY, bX, bY); int dist = math_utils::round(math_utils::distance(aX, aY, bX, bY));
float xStep = (bX - aX) / dist; float xStep = (bX - aX) / dist;
float yStep = (bY - aY) / dist; float yStep = (bY - aY) / dist;
for (int i = 0; i < dist; i++) { for (int i = 0; i < dist; i++) {
int x = round(aX + i * xStep); int x = math_utils::round(aX + i * xStep);
int y = round(aY + i * yStep); int y = math_utils::round(aY + i * yStep);
if (image_->get(x, y)) { if (image_->get(x, y)) {
Ref<ResultPoint> point(new ResultPoint(x, y)); Ref<ResultPoint> point(new ResultPoint(x, y));
return point; return point;
@ -246,12 +242,6 @@ Ref<ResultPoint> WhiteRectangleDetector::getBlackPointOnSegment(float aX, float
return point; return point;
} }
int WhiteRectangleDetector::distanceL2(float aX, float aY, float bX, float bY) {
float xDiff = aX - bX;
float yDiff = aY - bY;
return round((float)sqrt(xDiff * xDiff + yDiff * yDiff));
}
/** /**
* recenters the points of a constant distance towards the center * recenters the points of a constant distance towards the center
* *

View file

@ -49,9 +49,7 @@ class WhiteRectangleDetector : public Counted {
std::vector<Ref<ResultPoint> > detect(); std::vector<Ref<ResultPoint> > detect();
private: private:
int round(float a);
Ref<ResultPoint> getBlackPointOnSegment(float aX, float aY, float bX, float bY); Ref<ResultPoint> getBlackPointOnSegment(float aX, float aY, float bX, float bY);
int distanceL2(float aX, float aY, float bX, float bY);
std::vector<Ref<ResultPoint> > centerEdges(Ref<ResultPoint> y, Ref<ResultPoint> z, std::vector<Ref<ResultPoint> > centerEdges(Ref<ResultPoint> y, Ref<ResultPoint> z,
Ref<ResultPoint> x, Ref<ResultPoint> t); Ref<ResultPoint> x, Ref<ResultPoint> t);
bool containsBlackPoint(int a, int b, int fixed, bool horizontal); bool containsBlackPoint(int a, int b, int fixed, bool horizontal);

View file

@ -0,0 +1,46 @@
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
#ifndef __ZXING_COMMON_DETECTOR_MATHUTILS_H__
#define __ZXING_COMMON_DETECTOR_MATHUTILS_H__
/*
* Copyright 2012 ZXing authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <math.h>
namespace zxing { namespace common { namespace detector { namespace math_utils {
/**
* Ends up being a bit faster than {@link Math#round(float)}. This merely rounds its
* argument to the nearest int, where x.5 rounds up to x+1.
*/
inline int round(float d) {
return (int) (d + 0.5f);
}
inline float distance(float aX, float aY, float bX, float bY) {
float xDiff = aX - bX;
float yDiff = aY - bY;
return (float) sqrt(xDiff * xDiff + yDiff * yDiff);
}
inline float distance(int aX, int aY, int bX, int bY) {
int xDiff = aX - bX;
int yDiff = aY - bY;
return (float) sqrt(xDiff * xDiff + yDiff * yDiff);
}
}}}}
#endif

View file

@ -22,10 +22,12 @@
#include <zxing/ResultPoint.h> #include <zxing/ResultPoint.h>
#include <zxing/common/GridSampler.h> #include <zxing/common/GridSampler.h>
#include <zxing/datamatrix/detector/Detector.h> #include <zxing/datamatrix/detector/Detector.h>
#include <cmath> #include <zxing/common/detector/math_utils.h>
#include <sstream> #include <sstream>
#include <cstdlib> #include <cstdlib>
namespace math_utils = zxing::common::detector::math_utils;
namespace zxing { namespace zxing {
namespace datamatrix { namespace datamatrix {
@ -327,12 +329,8 @@ bool Detector::isValid(Ref<ResultPoint> p) {
&& p->getY() < image_->getHeight(); && p->getY() < image_->getHeight();
} }
// L2 distance
int Detector::distance(Ref<ResultPoint> a, Ref<ResultPoint> b) { int Detector::distance(Ref<ResultPoint> a, Ref<ResultPoint> b) {
return round( return math_utils::round(ResultPoint::distance(a, b));
(float) sqrt(
(double) (a->getX() - b->getX()) * (a->getX() - b->getX())
+ (a->getY() - b->getY()) * (a->getY() - b->getY())));
} }
Ref<ResultPointsAndTransitions> Detector::transitionsBetween(Ref<ResultPoint> from, Ref<ResultPointsAndTransitions> Detector::transitionsBetween(Ref<ResultPoint> from,

View file

@ -27,10 +27,12 @@
#include <zxing/qrcode/Version.h> #include <zxing/qrcode/Version.h>
#include <zxing/common/GridSampler.h> #include <zxing/common/GridSampler.h>
#include <zxing/DecodeHints.h> #include <zxing/DecodeHints.h>
#include <cmath> #include <zxing/common/detector/math_utils.h>
#include <sstream> #include <sstream>
#include <cstdlib> #include <cstdlib>
namespace math_utils = zxing::common::detector::math_utils;
namespace zxing { namespace zxing {
namespace qrcode { namespace qrcode {
@ -145,8 +147,10 @@ Ref<BitMatrix> Detector::sampleGrid(Ref<BitMatrix> image, int dimension, Ref<Per
int Detector::computeDimension(Ref<ResultPoint> topLeft, Ref<ResultPoint> topRight, Ref<ResultPoint> bottomLeft, int Detector::computeDimension(Ref<ResultPoint> topLeft, Ref<ResultPoint> topRight, Ref<ResultPoint> bottomLeft,
float moduleSize) { float moduleSize) {
int tltrCentersDimension = int(FinderPatternFinder::distance(topLeft, topRight) / moduleSize + 0.5f); int tltrCentersDimension =
int tlblCentersDimension = int(FinderPatternFinder::distance(topLeft, bottomLeft) / moduleSize + 0.5f); math_utils::round(ResultPoint::distance(topLeft, topRight) / moduleSize);
int tlblCentersDimension =
math_utils::round(ResultPoint::distance(topLeft, bottomLeft) / moduleSize);
int dimension = ((tltrCentersDimension + tlblCentersDimension) >> 1) + 7; int dimension = ((tltrCentersDimension + tlblCentersDimension) >> 1) + 7;
switch (dimension & 0x03) { // mod 4 switch (dimension & 0x03) { // mod 4
case 0: case 0:
@ -247,9 +251,7 @@ float Detector::sizeOfBlackWhiteBlackRun(int fromX, int fromY, int toX, int toY)
// Does current pixel mean we have moved white to black or vice versa? // Does current pixel mean we have moved white to black or vice versa?
if (!((state == 1) ^ image_->get(realX, realY))) { if (!((state == 1) ^ image_->get(realX, realY))) {
if (state == 2) { if (state == 2) {
int diffX = x - fromX; return math_utils::distance(x, y, fromX, fromY);
int diffY = y - fromY;
return (float) sqrt((double) (diffX * diffX + diffY * diffY));
} }
state++; state++;
} }
@ -267,9 +269,7 @@ float Detector::sizeOfBlackWhiteBlackRun(int fromX, int fromY, int toX, int toY)
// is "white" so this last point at (toX+xStep,toY) is the right ending. This is really a // is "white" so this last point at (toX+xStep,toY) is the right ending. This is really a
// small approximation; (toX+xStep,toY+yStep) might be really correct. Ignore this. // small approximation; (toX+xStep,toY+yStep) might be really correct. Ignore this.
if (state == 2) { if (state == 2) {
int diffX = toX + xstep - fromX; return math_utils::distance(toX + xstep, toY, fromX, fromY);
int diffY = toY - fromY;
return (float) sqrt((double) (diffX * diffX + diffY * diffY));
} }
// else we didn't find even black-white-black; no estimate is really possible // else we didn't find even black-white-black; no estimate is really possible
return NAN; return NAN;