From afb81b1625b5f9ede33f3106b42ecc9da944c38d Mon Sep 17 00:00:00 2001 From: "smparkes@smparkes.net" Date: Tue, 26 Jun 2012 18:14:23 +0000 Subject: [PATCH] C++ changes for r2294 git-svn-id: https://zxing.googlecode.com/svn/trunk@2332 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- cpp/core/src/zxing/ResultPoint.cpp | 12 +++-- .../src/zxing/aztec/detector/Detector.cpp | 47 ++++++++++--------- cpp/core/src/zxing/aztec/detector/Detector.h | 2 - .../detector/WhiteRectangleDetector.cpp | 26 ++++------ .../common/detector/WhiteRectangleDetector.h | 2 - .../src/zxing/common/detector/math_utils.h | 46 ++++++++++++++++++ .../zxing/datamatrix/detector/Detector.cpp | 10 ++-- .../src/zxing/qrcode/detector/Detector.cpp | 18 +++---- 8 files changed, 100 insertions(+), 63 deletions(-) create mode 100644 cpp/core/src/zxing/common/detector/math_utils.h diff --git a/cpp/core/src/zxing/ResultPoint.cpp b/cpp/core/src/zxing/ResultPoint.cpp index fa31588a1..53d2d1db7 100755 --- a/cpp/core/src/zxing/ResultPoint.cpp +++ b/cpp/core/src/zxing/ResultPoint.cpp @@ -1,3 +1,4 @@ +// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*- /* * ResultPoint.cpp * zxing @@ -19,7 +20,9 @@ */ #include -#include +#include + +namespace math_utils = zxing::common::detector::math_utils; namespace zxing { @@ -82,8 +85,11 @@ void ResultPoint::orderBestPatterns(std::vector > &patterns) { patterns[2] = pointC; } -float ResultPoint::distance(Ref point1, Ref point2) { - return distance(point1->getX(), point1->getY(), point2->getX(), point2->getY()); + float ResultPoint::distance(Ref pattern1, Ref pattern2) { + return math_utils::distance(pattern1->posX_, + pattern1->posY_, + pattern2->posX_, + pattern2->posY_); } float ResultPoint::distance(float x1, float x2, float y1, float y2) { diff --git a/cpp/core/src/zxing/aztec/detector/Detector.cpp b/cpp/core/src/zxing/aztec/detector/Detector.cpp index 8d51ff773..6808ededc 100644 --- a/cpp/core/src/zxing/aztec/detector/Detector.cpp +++ b/cpp/core/src/zxing/aztec/detector/Detector.cpp @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include using zxing::aztec::Detector; @@ -36,6 +36,7 @@ using zxing::Ref; using zxing::ResultPoint; using zxing::BitArray; using zxing::BitMatrix; +namespace math_utils = zxing::common::detector::math_utils; Detector::Detector(Ref image): image_(image), @@ -136,21 +137,21 @@ std::vector > Detector::getMatrixCornerPoints(std::vectory - bullEyeCornerPoints[2]->y; dy += dy > 0 ? 1 : -1; - int targetcx = ROUND(bullEyeCornerPoints[2]->x - ratio * dx); - int targetcy = ROUND(bullEyeCornerPoints[2]->y - ratio * dy); + int targetcx = math_utils::round(bullEyeCornerPoints[2]->x - ratio * dx); + int targetcy = math_utils::round(bullEyeCornerPoints[2]->y - ratio * dy); - int targetax = ROUND(bullEyeCornerPoints[0]->x + ratio * dx); - int targetay = ROUND(bullEyeCornerPoints[0]->y + ratio * dy); + int targetax = math_utils::round(bullEyeCornerPoints[0]->x + ratio * dx); + int targetay = math_utils::round(bullEyeCornerPoints[0]->y + ratio * dy); dx = bullEyeCornerPoints[1]->x - bullEyeCornerPoints[3]->x; dx += dx > 0 ? 1 : -1; dy = bullEyeCornerPoints[1]->y - bullEyeCornerPoints[3]->y; dy += dy > 0 ? 1 : -1; - int targetdx = ROUND(bullEyeCornerPoints[3]->x - ratio * dx); - int targetdy = ROUND(bullEyeCornerPoints[3]->y - ratio * dy); - int targetbx = ROUND(bullEyeCornerPoints[1]->x + ratio * dx); - int targetby = ROUND(bullEyeCornerPoints[1]->y + ratio * dy); + int targetdx = math_utils::round(bullEyeCornerPoints[3]->x - ratio * dx); + int targetdy = math_utils::round(bullEyeCornerPoints[3]->y - ratio * dy); + int targetbx = math_utils::round(bullEyeCornerPoints[1]->x + ratio * dx); + int targetby = math_utils::round(bullEyeCornerPoints[1]->y + ratio * dy); if (!isValid(targetax, targetay) || !isValid(targetbx, targetby) || @@ -262,18 +263,18 @@ std::vector > Detector::getBullEyeCornerPoints(Refx - pind->x; int dy = pina->y - pinc->y; - int targetcx = ROUND(pinc->x - ratio * dx); - int targetcy = ROUND(pinc->y - ratio * dy); - int targetax = ROUND(pina->x + ratio * dx); - int targetay = ROUND(pina->y + ratio * dy); + int targetcx = math_utils::round(pinc->x - ratio * dx); + int targetcy = math_utils::round(pinc->y - ratio * dy); + int targetax = math_utils::round(pina->x + ratio * dx); + int targetay = math_utils::round(pina->y + ratio * dy); dx = pinb->x - pind->x; dy = pinb->y - pind->y; - int targetdx = ROUND(pind->x - ratio * dx); - int targetdy = ROUND(pind->y - ratio * dy); - int targetbx = ROUND(pinb->x + ratio * dx); - int targetby = ROUND(pinb->y + ratio * dy); + int targetdx = math_utils::round(pind->x - ratio * dx); + int targetdy = math_utils::round(pind->y - ratio * dy); + int targetbx = math_utils::round(pinb->x + ratio * dx); + int targetby = math_utils::round(pinb->y + ratio * dy); if (!isValid(targetax, targetay) || !isValid(targetbx, targetby) || @@ -314,8 +315,8 @@ Ref Detector::getMatrixCenter() { } - int cx = ROUND((pointA->getX() + pointD->getX() + pointB->getX() + pointC->getX()) / 4); - int cy = ROUND((pointA->getY() + pointD->getY() + pointB->getY() + pointC->getY()) / 4); + int cx = math_utils::round((pointA->getX() + pointD->getX() + pointB->getX() + pointC->getX()) / 4); + int cy = math_utils::round((pointA->getY() + pointD->getY() + pointB->getY() + pointC->getY()) / 4); try { @@ -334,8 +335,8 @@ Ref Detector::getMatrixCenter() { } - cx = ROUND((pointA->getX() + pointD->getX() + pointB->getX() + pointC->getX()) / 4); - cy = ROUND((pointA->getY() + pointD->getY() + pointB->getY() + pointC->getY()) / 4); + cx = math_utils::round((pointA->getX() + pointD->getX() + pointB->getX() + pointC->getX()) / 4); + cy = math_utils::round((pointA->getY() + pointD->getY() + pointB->getY() + pointC->getY()) / 4); return Ref(new Point(cx, cy)); @@ -424,7 +425,7 @@ Ref Detector::sampleLine(Ref p1, Refy; 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; py += dy; } @@ -486,7 +487,7 @@ int Detector::getColor(Ref p1, Ref p2) for (int i = 0; i < d; i++) { px += dx; py += dy; - if (image_->get(ROUND(px), ROUND(py)) != colorModel) { + if (image_->get(math_utils::round(px), math_utils::round(py)) != colorModel) { error ++; } } diff --git a/cpp/core/src/zxing/aztec/detector/Detector.h b/cpp/core/src/zxing/aztec/detector/Detector.h index 8b10853a1..d265446d8 100644 --- a/cpp/core/src/zxing/aztec/detector/Detector.h +++ b/cpp/core/src/zxing/aztec/detector/Detector.h @@ -28,8 +28,6 @@ #include #include -#define ROUND(a) ((int)(a + 0.5f)) - namespace zxing { namespace aztec { diff --git a/cpp/core/src/zxing/common/detector/WhiteRectangleDetector.cpp b/cpp/core/src/zxing/common/detector/WhiteRectangleDetector.cpp index 4decd31ab..03cb6cba2 100644 --- a/cpp/core/src/zxing/common/detector/WhiteRectangleDetector.cpp +++ b/cpp/core/src/zxing/common/detector/WhiteRectangleDetector.cpp @@ -1,3 +1,4 @@ +// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*- /* * WhiteRectangleDetector.cpp * y_wmk @@ -20,9 +21,11 @@ #include #include -#include +#include #include +namespace math_utils = zxing::common::detector::math_utils; + namespace zxing { using namespace std; @@ -222,21 +225,14 @@ std::vector > 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 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 yStep = (bY - aY) / dist; + for (int i = 0; i < dist; i++) { - int x = round(aX + i * xStep); - int y = round(aY + i * yStep); + int x = math_utils::round(aX + i * xStep); + int y = math_utils::round(aY + i * yStep); if (image_->get(x, y)) { Ref point(new ResultPoint(x, y)); return point; @@ -246,12 +242,6 @@ Ref WhiteRectangleDetector::getBlackPointOnSegment(float aX, float 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 * diff --git a/cpp/core/src/zxing/common/detector/WhiteRectangleDetector.h b/cpp/core/src/zxing/common/detector/WhiteRectangleDetector.h index 2a8b8a4ff..eb9d36f99 100644 --- a/cpp/core/src/zxing/common/detector/WhiteRectangleDetector.h +++ b/cpp/core/src/zxing/common/detector/WhiteRectangleDetector.h @@ -49,9 +49,7 @@ class WhiteRectangleDetector : public Counted { std::vector > detect(); private: - int round(float a); Ref getBlackPointOnSegment(float aX, float aY, float bX, float bY); - int distanceL2(float aX, float aY, float bX, float bY); std::vector > centerEdges(Ref y, Ref z, Ref x, Ref t); bool containsBlackPoint(int a, int b, int fixed, bool horizontal); diff --git a/cpp/core/src/zxing/common/detector/math_utils.h b/cpp/core/src/zxing/common/detector/math_utils.h new file mode 100644 index 000000000..2cd0c74f3 --- /dev/null +++ b/cpp/core/src/zxing/common/detector/math_utils.h @@ -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 + +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 diff --git a/cpp/core/src/zxing/datamatrix/detector/Detector.cpp b/cpp/core/src/zxing/datamatrix/detector/Detector.cpp index 4e5c4343f..72d40d605 100644 --- a/cpp/core/src/zxing/datamatrix/detector/Detector.cpp +++ b/cpp/core/src/zxing/datamatrix/detector/Detector.cpp @@ -22,10 +22,12 @@ #include #include #include -#include +#include #include #include +namespace math_utils = zxing::common::detector::math_utils; + namespace zxing { namespace datamatrix { @@ -327,12 +329,8 @@ bool Detector::isValid(Ref p) { && p->getY() < image_->getHeight(); } -// L2 distance int Detector::distance(Ref a, Ref b) { - return round( - (float) sqrt( - (double) (a->getX() - b->getX()) * (a->getX() - b->getX()) - + (a->getY() - b->getY()) * (a->getY() - b->getY()))); + return math_utils::round(ResultPoint::distance(a, b)); } Ref Detector::transitionsBetween(Ref from, diff --git a/cpp/core/src/zxing/qrcode/detector/Detector.cpp b/cpp/core/src/zxing/qrcode/detector/Detector.cpp index 405354b07..53c9403c7 100644 --- a/cpp/core/src/zxing/qrcode/detector/Detector.cpp +++ b/cpp/core/src/zxing/qrcode/detector/Detector.cpp @@ -27,10 +27,12 @@ #include #include #include -#include +#include #include #include +namespace math_utils = zxing::common::detector::math_utils; + namespace zxing { namespace qrcode { @@ -145,8 +147,10 @@ Ref Detector::sampleGrid(Ref image, int dimension, Ref topLeft, Ref topRight, Ref bottomLeft, float moduleSize) { - int tltrCentersDimension = int(FinderPatternFinder::distance(topLeft, topRight) / moduleSize + 0.5f); - int tlblCentersDimension = int(FinderPatternFinder::distance(topLeft, bottomLeft) / moduleSize + 0.5f); + int tltrCentersDimension = + math_utils::round(ResultPoint::distance(topLeft, topRight) / moduleSize); + int tlblCentersDimension = + math_utils::round(ResultPoint::distance(topLeft, bottomLeft) / moduleSize); int dimension = ((tltrCentersDimension + tlblCentersDimension) >> 1) + 7; switch (dimension & 0x03) { // mod 4 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? if (!((state == 1) ^ image_->get(realX, realY))) { if (state == 2) { - int diffX = x - fromX; - int diffY = y - fromY; - return (float) sqrt((double) (diffX * diffX + diffY * diffY)); + return math_utils::distance(x, y, fromX, fromY); } 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 // small approximation; (toX+xStep,toY+yStep) might be really correct. Ignore this. if (state == 2) { - int diffX = toX + xstep - fromX; - int diffY = toY - fromY; - return (float) sqrt((double) (diffX * diffX + diffY * diffY)); + return math_utils::distance(toX + xstep, toY, fromX, fromY); } // else we didn't find even black-white-black; no estimate is really possible return NAN;