Guard against exception case discovered in new DM code, clean up its formatting a bit
[zxing.git] / core / src / com / google / zxing / datamatrix / detector / Detector.java
index 6c43ef7..08c9319 100644 (file)
@@ -158,13 +158,14 @@ public final class Detector {
       }
       dimensionRight += 2;
 
-    BitMatrix bits = null;
-    ResultPoint correctedTopRight = null;
+    BitMatrix bits;
+    ResultPoint correctedTopRight;
       
     if (dimensionTop >= dimensionRight * 2 || dimensionRight >= dimensionTop * 2){
        //The matrix is rectangular
        
-        correctedTopRight = correctTopRightRectangular(bottomLeft, bottomRight, topLeft, topRight, dimensionTop, dimensionRight);
+        correctedTopRight =
+            correctTopRightRectangular(bottomLeft, bottomRight, topLeft, topRight, dimensionTop, dimensionRight);
         if (correctedTopRight == null){
                correctedTopRight = topRight;
         }
@@ -178,11 +179,11 @@ public final class Detector {
         }
         
         if ((dimensionRight & 0x01) == 1) {
-            // it can't be odd, so, round... up?
-            dimensionRight++;
-          }
-        
-          bits = sampleGrid(image, topLeft, bottomLeft, bottomRight, correctedTopRight, dimensionTop, dimensionRight);
+          // it can't be odd, so, round... up?
+          dimensionRight++;
+        }
+
+        bits = sampleGrid(image, topLeft, bottomLeft, bottomRight, correctedTopRight, dimensionTop, dimensionRight);
           
     } else {
        //The matrix is square
@@ -202,7 +203,13 @@ public final class Detector {
           dimensionCorrected++;
         }
 
-        bits = sampleGrid(image, topLeft, bottomLeft, bottomRight, correctedTopRight, dimensionCorrected, dimensionCorrected);
+        bits = sampleGrid(image,
+                          topLeft,
+                          bottomLeft,
+                          bottomRight,
+                          correctedTopRight,
+                          dimensionCorrected,
+                          dimensionCorrected);
     }
 
 
@@ -210,7 +217,8 @@ public final class Detector {
   }
 
   /**
-   * Calculates the position of the white top right module using the output of the rectangle detector for a rectangular matrix
+   * Calculates the position of the white top right module using the output of the rectangle detector
+   * for a rectangular matrix
    */
   private ResultPoint correctTopRightRectangular(ResultPoint bottomLeft,
                ResultPoint bottomRight, ResultPoint topLeft, ResultPoint topRight,
@@ -249,10 +257,11 @@ public final class Detector {
                }
                
                return c2;
-}
+  }
 
-/**
-   * Calculates the position of the white top right module using the output of the rectangle detector for a square matrix
+  /**
+   * Calculates the position of the white top right module using the output of the rectangle detector
+   * for a square matrix
    */
   private ResultPoint correctTopRight(ResultPoint bottomLeft,
                                       ResultPoint bottomRight,
@@ -260,37 +269,35 @@ public final class Detector {
                                       ResultPoint topRight,
                                       int dimension) {
                
-               float corr = distance(bottomLeft, bottomRight) / (float)dimension;
+               float corr = distance(bottomLeft, bottomRight) / (float) dimension;
                int norm = distance(topLeft, topRight);
                float cos = (topRight.getX() - topLeft.getX()) / norm;
                float sin = (topRight.getY() - topLeft.getY()) / norm;
                
-               ResultPoint c1 = new ResultPoint(topRight.getX()+corr*cos, topRight.getY()+corr*sin);
+               ResultPoint c1 = new ResultPoint(topRight.getX() + corr * cos, topRight.getY() + corr * sin);
        
-               corr = distance(bottomLeft, bottomRight) / (float)dimension;
+               corr = distance(bottomLeft, bottomRight) / (float) dimension;
                norm = distance(bottomRight, topRight);
                cos = (topRight.getX() - bottomRight.getX()) / norm;
                sin = (topRight.getY() - bottomRight.getY()) / norm;
                
-               ResultPoint c2 = new ResultPoint(topRight.getX()+corr*cos, topRight.getY()+corr*sin);
+               ResultPoint c2 = new ResultPoint(topRight.getX() + corr * cos, topRight.getY() + corr * sin);
 
-               if (!isValid(c1)){
-                       if (isValid(c2)){
+               if (!isValid(c1)) {
+                       if (isValid(c2)) {
                                return c2;
                        }
                        return null;
-               } else if (!isValid(c2)){
-                       return c1;
-               }
-               
-               int l1 = Math.abs(transitionsBetween(topLeft, c1).getTransitions() - transitionsBetween(bottomRight, c1).getTransitions());
-               int l2 = Math.abs(transitionsBetween(topLeft, c2).getTransitions() - transitionsBetween(bottomRight, c2).getTransitions());
-               
-               if (l1 <= l2){
+               } else if (!isValid(c2)) {
                        return c1;
                }
                
-               return c2;
+               int l1 = Math.abs(transitionsBetween(topLeft, c1).getTransitions() -
+                      transitionsBetween(bottomRight, c1).getTransitions());
+               int l2 = Math.abs(transitionsBetween(topLeft, c2).getTransitions() -
+                      transitionsBetween(bottomRight, c2).getTransitions());
+
+    return l1 <= l2 ? c1 : c2;
   }
 
   private boolean isValid(ResultPoint p) {