From: srowen Date: Mon, 13 Sep 2010 09:05:15 +0000 (+0000) Subject: Replace Math.round() with simple workalike -- faster, and needed to work in JavaME X-Git-Url: http://git.rot13.org/?p=zxing.git;a=commitdiff_plain;h=3cd96673d9f7a87ebbf6a3ee5d16bbf4d8bb4f0c Replace Math.round() with simple workalike -- faster, and needed to work in JavaME git-svn-id: http://zxing.googlecode.com/svn/trunk@1585 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- diff --git a/core/src/com/google/zxing/common/detector/WhiteRectangleDetector.java b/core/src/com/google/zxing/common/detector/WhiteRectangleDetector.java index ce2e8a9d..38adbaa7 100644 --- a/core/src/com/google/zxing/common/detector/WhiteRectangleDetector.java +++ b/core/src/com/google/zxing/common/detector/WhiteRectangleDetector.java @@ -206,6 +206,13 @@ public final class WhiteRectangleDetector { } } + /** + * Ends up being a bit faster than Math.round(). This merely rounds its + * argument to the nearest int, where x.5 rounds up. + */ + private static int round(float d) { + return (int) (d + 0.5f); + } private ResultPoint getBlackPointOnSegment(float aX, float aY, float bX, float bY) { int dist = distanceL2(aX, aY, bX, bY); @@ -213,8 +220,8 @@ public final class WhiteRectangleDetector { float yStep = (bY - aY) / dist; for (int i = 0; i < dist; i++) { - int x = Math.round(aX + i * xStep); - int y = Math.round(aY + i * yStep); + int x = round(aX + i * xStep); + int y = round(aY + i * yStep); if (image.get(x, y)) { return new ResultPoint(x, y); } @@ -225,7 +232,7 @@ public final class WhiteRectangleDetector { private static int distanceL2(float aX, float aY, float bX, float bY) { float xDiff = aX - bX; float yDiff = aY - bY; - return (int) Math.round(Math.sqrt(xDiff * xDiff + yDiff * yDiff)); + return round((float) Math.sqrt(xDiff * xDiff + yDiff * yDiff)); } /** diff --git a/core/src/com/google/zxing/datamatrix/detector/Detector.java b/core/src/com/google/zxing/datamatrix/detector/Detector.java index ca1918f7..59ffbada 100644 --- a/core/src/com/google/zxing/datamatrix/detector/Detector.java +++ b/core/src/com/google/zxing/datamatrix/detector/Detector.java @@ -214,9 +214,17 @@ public final class Detector { return (p.getX() >= 0 && p.getX() < image.width && p.getY() > 0 && p.getY() < image.height); } + /** + * Ends up being a bit faster than Math.round(). This merely rounds its + * argument to the nearest int, where x.5 rounds up. + */ + private static int round(float d) { + return (int) (d + 0.5f); + } + // L2 distance private static int distance(ResultPoint a, ResultPoint b) { - return (int) Math.round(Math.sqrt((a.getX() - b.getX()) + return round((float) Math.sqrt((a.getX() - b.getX()) * (a.getX() - b.getX()) + (a.getY() - b.getY()) * (a.getY() - b.getY()))); }