Replace Math.round() with simple workalike -- faster, and needed to work in JavaME
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Mon, 13 Sep 2010 09:05:15 +0000 (09:05 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Mon, 13 Sep 2010 09:05:15 +0000 (09:05 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@1585 59b500cc-1b3d-0410-9834-0bbf25fbcc57

core/src/com/google/zxing/common/detector/WhiteRectangleDetector.java
core/src/com/google/zxing/datamatrix/detector/Detector.java

index ce2e8a9..38adbaa 100644 (file)
@@ -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);
 
   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++) {
     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);
       }
       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;
   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));
   }
 
   /**
   }
 
   /**
index ca1918f..59ffbad 100644 (file)
@@ -214,9 +214,17 @@ public final class Detector {
          return (p.getX() >= 0 && p.getX() < image.width && p.getY() > 0 && p.getY() < image.height);
   }
 
          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) {
 // 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())));
   }
         * (a.getX() - b.getX()) + (a.getY() - b.getY())
         * (a.getY() - b.getY())));
   }