Issue 508
[zxing.git] / core / src / com / google / zxing / common / BitMatrix.java
index 1088db2..d1e6974 100755 (executable)
@@ -50,11 +50,7 @@ public final class BitMatrix {
     }\r
     this.width = width;\r
     this.height = height;\r
-    int rowSize = width >> 5;\r
-    if ((width & 0x1f) != 0) {\r
-      rowSize++;\r
-    }\r
-    this.rowSize = rowSize;\r
+    this.rowSize = (width + 31) >> 5;\r
     bits = new int[rowSize * height];\r
   }\r
 \r
@@ -149,6 +145,31 @@ public final class BitMatrix {
     return row;\r
   }\r
 \r
+  /**\r
+   * This is useful in detecting a corner of a 'pure' barcode.\r
+   * \r
+   * @return {x,y} coordinate of top-left-most 1 bit, or null if it is all white\r
+   */\r
+  public int[] getTopLeftOnBit() {\r
+    int bitsOffset = 0;\r
+    while (bitsOffset < bits.length && bits[bitsOffset] == 0) {\r
+      bitsOffset++;\r
+    }\r
+    if (bitsOffset == bits.length) {\r
+      return null;\r
+    }\r
+    int y = bitsOffset / rowSize;\r
+    int x = (bitsOffset % rowSize) << 5;\r
+    \r
+    int theBits = bits[bitsOffset];\r
+    int bit = 0;\r
+    while ((theBits << (31-bit)) == 0) {\r
+      bit++;\r
+    }\r
+    x += bit;\r
+    return new int[] {x, y};\r
+  }\r
+\r
   /**\r
    * @return The width of the matrix\r
    */\r
@@ -163,17 +184,32 @@ public final class BitMatrix {
     return height;\r
   }\r
 \r
-  /**\r
-   * This method is for compatibility with older code. It's only logical to call if the matrix\r
-   * is square, so I'm throwing if that's not the case.\r
-   *\r
-   * @return row/column dimension of this matrix\r
-   */\r
-  public int getDimension() {\r
-    if (width != height) {\r
-      throw new RuntimeException("Can't call getDimension() on a non-square matrix");\r
+  public boolean equals(Object o) {\r
+    if (!(o instanceof BitMatrix)) {\r
+      return false;\r
     }\r
-    return width;\r
+    BitMatrix other = (BitMatrix) o;\r
+    if (width != other.width || height != other.height ||\r
+        rowSize != other.rowSize || bits.length != other.bits.length) {\r
+      return false;\r
+    }\r
+    for (int i = 0; i < bits.length; i++) {\r
+      if (bits[i] != other.bits[i]) {\r
+        return false;\r
+      }\r
+    }\r
+    return true;\r
+  }\r
+\r
+  public int hashCode() {\r
+    int hash = width;\r
+    hash = 31 * hash + width;\r
+    hash = 31 * hash + height;\r
+    hash = 31 * hash + rowSize;\r
+    for (int i = 0; i < bits.length; i++) {\r
+      hash = 31 * hash + bits[i];\r
+    }\r
+    return hash;\r
   }\r
 \r
   public String toString() {\r