Related to Issue 205, but not the direct issue: read both copies of the format info...
[zxing.git] / core / src / com / google / zxing / qrcode / decoder / BitMatrixParser.java
index 59d558f..2a820ec 100644 (file)
@@ -16,7 +16,7 @@
 \r
 package com.google.zxing.qrcode.decoder;\r
 \r
-import com.google.zxing.ReaderException;\r
+import com.google.zxing.FormatException;\r
 import com.google.zxing.common.BitMatrix;\r
 \r
 /**\r
@@ -30,12 +30,12 @@ final class BitMatrixParser {
 \r
   /**\r
    * @param bitMatrix {@link BitMatrix} to parse\r
-   * @throws ReaderException if dimension is not >= 21 and 1 mod 4\r
+   * @throws FormatException if dimension is not >= 21 and 1 mod 4\r
    */\r
-  BitMatrixParser(BitMatrix bitMatrix) throws ReaderException {\r
+  BitMatrixParser(BitMatrix bitMatrix) throws FormatException {\r
     int dimension = bitMatrix.getDimension();\r
     if (dimension < 21 || (dimension & 0x03) != 1) {\r
-      throw ReaderException.getInstance();\r
+      throw FormatException.getFormatInstance();\r
     }\r
     this.bitMatrix = bitMatrix;\r
   }\r
@@ -44,60 +44,55 @@ final class BitMatrixParser {
    * <p>Reads format information from one of its two locations within the QR Code.</p>\r
    *\r
    * @return {@link FormatInformation} encapsulating the QR Code's format info\r
-   * @throws ReaderException if both format information locations cannot be parsed as\r
+   * @throws FormatException if both format information locations cannot be parsed as\r
    * the valid encoding of format information\r
    */\r
-  FormatInformation readFormatInformation() throws ReaderException {\r
+  FormatInformation readFormatInformation() throws FormatException {\r
 \r
     if (parsedFormatInfo != null) {\r
       return parsedFormatInfo;\r
     }\r
 \r
     // Read top-left format info bits\r
-    int formatInfoBits = 0;\r
-    for (int j = 0; j < 6; j++) {\r
-      formatInfoBits = copyBit(8, j, formatInfoBits);\r
+    int formatInfoBits1 = 0;\r
+    for (int i = 0; i < 6; i++) {\r
+      formatInfoBits1 = copyBit(i, 8, formatInfoBits1);\r
     }\r
     // .. and skip a bit in the timing pattern ...\r
-    formatInfoBits = copyBit(8, 7, formatInfoBits);\r
-    formatInfoBits = copyBit(8, 8, formatInfoBits);\r
-    formatInfoBits = copyBit(7, 8, formatInfoBits);\r
+    formatInfoBits1 = copyBit(7, 8, formatInfoBits1);\r
+    formatInfoBits1 = copyBit(8, 8, formatInfoBits1);\r
+    formatInfoBits1 = copyBit(8, 7, formatInfoBits1);\r
     // .. and skip a bit in the timing pattern ...\r
-    for (int i = 5; i >= 0; i--) {\r
-      formatInfoBits = copyBit(i, 8, formatInfoBits);\r
-    }\r
-\r
-    parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits);\r
-    if (parsedFormatInfo != null) {\r
-      return parsedFormatInfo;\r
+    for (int j = 5; j >= 0; j--) {\r
+      formatInfoBits1 = copyBit(8, j, formatInfoBits1);\r
     }\r
 \r
-    // Hmm, failed. Try the top-right/bottom-left pattern\r
+    // Read the top-right/bottom-left pattern too\r
     int dimension = bitMatrix.getDimension();\r
-    formatInfoBits = 0;\r
+    int formatInfoBits2 = 0;\r
     int iMin = dimension - 8;\r
     for (int i = dimension - 1; i >= iMin; i--) {\r
-      formatInfoBits = copyBit(i, 8, formatInfoBits);\r
+      formatInfoBits2 = copyBit(i, 8, formatInfoBits2);\r
     }\r
     for (int j = dimension - 7; j < dimension; j++) {\r
-      formatInfoBits = copyBit(8, j, formatInfoBits);\r
+      formatInfoBits2 = copyBit(8, j, formatInfoBits2);\r
     }\r
 \r
-    parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits);\r
+    parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits1, formatInfoBits2);\r
     if (parsedFormatInfo != null) {\r
       return parsedFormatInfo;\r
     }\r
-    throw ReaderException.getInstance();\r
+    throw FormatException.getFormatInstance();\r
   }\r
 \r
   /**\r
    * <p>Reads version information from one of its two locations within the QR Code.</p>\r
    *\r
    * @return {@link Version} encapsulating the QR Code's version\r
-   * @throws ReaderException if both version information locations cannot be parsed as\r
+   * @throws FormatException if both version information locations cannot be parsed as\r
    * the valid encoding of version information\r
    */\r
-  Version readVersion() throws ReaderException {\r
+  Version readVersion() throws FormatException {\r
 \r
     if (parsedVersion != null) {\r
       return parsedVersion;\r
@@ -112,9 +107,9 @@ final class BitMatrixParser {
 \r
     // Read top-right version info: 3 wide by 6 tall\r
     int versionBits = 0;\r
-    for (int i = 5; i >= 0; i--) {\r
-      int jMin = dimension - 11;\r
-      for (int j = dimension - 9; j >= jMin; j--) {\r
+    int ijMin = dimension - 11;\r
+    for (int j = 5; j >= 0; j--) {\r
+      for (int i = dimension - 9; i >= ijMin; i--) {\r
         versionBits = copyBit(i, j, versionBits);\r
       }\r
     }\r
@@ -126,9 +121,8 @@ final class BitMatrixParser {
 \r
     // Hmm, failed. Try bottom left: 6 wide by 3 tall\r
     versionBits = 0;\r
-    for (int j = 5; j >= 0; j--) {\r
-      int iMin = dimension - 11;\r
-      for (int i = dimension - 9; i >= iMin; i--) {\r
+    for (int i = 5; i >= 0; i--) {\r
+      for (int j = dimension - 9; j >= ijMin; j--) {\r
         versionBits = copyBit(i, j, versionBits);\r
       }\r
     }\r
@@ -137,7 +131,7 @@ final class BitMatrixParser {
     if (parsedVersion != null && parsedVersion.getDimensionForVersion() == dimension) {\r
       return parsedVersion;\r
     }\r
-    throw ReaderException.getInstance();\r
+    throw FormatException.getFormatInstance();\r
   }\r
 \r
   private int copyBit(int i, int j, int versionBits) {\r
@@ -150,9 +144,9 @@ final class BitMatrixParser {
    * QR Code.</p>\r
    *\r
    * @return bytes encoded within the QR Code\r
-   * @throws ReaderException if the exact number of bytes expected is not read\r
+   * @throws FormatException if the exact number of bytes expected is not read\r
    */\r
-  byte[] readCodewords() throws ReaderException {\r
+  byte[] readCodewords() throws FormatException {\r
 \r
     FormatInformation formatInfo = readFormatInformation();\r
     Version version = readVersion();\r
@@ -182,11 +176,11 @@ final class BitMatrixParser {
         int i = readingUp ? dimension - 1 - count : count;\r
         for (int col = 0; col < 2; col++) {\r
           // Ignore bits covered by the function pattern\r
-          if (!functionPattern.get(i, j - col)) {\r
+          if (!functionPattern.get(j - col, i)) {\r
             // Read a bit\r
             bitsRead++;\r
             currentByte <<= 1;\r
-            if (bitMatrix.get(i, j - col)) {\r
+            if (bitMatrix.get(j - col, i)) {\r
               currentByte |= 1;\r
             }\r
             // If we've made a whole byte, save it off\r
@@ -201,7 +195,7 @@ final class BitMatrixParser {
       readingUp ^= true; // readingUp = !readingUp; // switch directions\r
     }\r
     if (resultOffset != version.getTotalCodewords()) {\r
-      throw ReaderException.getInstance();\r
+      throw FormatException.getFormatInstance();\r
     }\r
     return result;\r
   }\r