Remove some redundant 'throws'; allocate more reasonably sized StringBuffers for...
[zxing.git] / core / src / com / google / zxing / qrcode / decoder / BitMatrixParser.java
index 27f19ad..9629385 100644 (file)
@@ -20,7 +20,7 @@ import com.google.zxing.ReaderException;
 import com.google.zxing.common.BitMatrix;\r
 \r
 /**\r
- * @author srowen@google.com (Sean Owen)\r
+ * @author Sean Owen\r
  */\r
 final class BitMatrixParser {\r
 \r
@@ -35,7 +35,7 @@ final class BitMatrixParser {
   BitMatrixParser(BitMatrix bitMatrix) throws ReaderException {\r
     int dimension = bitMatrix.getDimension();\r
     if (dimension < 21 || (dimension & 0x03) != 1) {\r
-      throw new ReaderException("Dimension must be 1 mod 4 and >= 21");\r
+      throw ReaderException.getInstance();\r
     }\r
     this.bitMatrix = bitMatrix;\r
   }\r
@@ -87,7 +87,7 @@ final class BitMatrixParser {
     if (parsedFormatInfo != null) {\r
       return parsedFormatInfo;\r
     }\r
-    throw new ReaderException("Could not decode format information");\r
+    throw ReaderException.getInstance();\r
   }\r
 \r
   /**\r
@@ -120,7 +120,7 @@ final class BitMatrixParser {
     }\r
 \r
     parsedVersion = Version.decodeVersionInformation(versionBits);\r
-    if (parsedVersion != null) {\r
+    if (parsedVersion != null && parsedVersion.getDimensionForVersion() == dimension) {\r
       return parsedVersion;\r
     }\r
 \r
@@ -128,20 +128,20 @@ final class BitMatrixParser {
     versionBits = 0;\r
     for (int j = 5; j >= 0; j--) {\r
       int iMin = dimension - 11;\r
-      for (int i = dimension - 11; i >= iMin; i--) {\r
+      for (int i = dimension - 9; i >= iMin; i--) {\r
         versionBits = copyBit(i, j, versionBits);\r
       }\r
     }\r
 \r
     parsedVersion = Version.decodeVersionInformation(versionBits);\r
-    if (parsedVersion != null) {\r
+    if (parsedVersion != null && parsedVersion.getDimensionForVersion() == dimension) {\r
       return parsedVersion;\r
     }\r
-    throw new ReaderException("Could not decode version");\r
+    throw ReaderException.getInstance();\r
   }\r
 \r
   private int copyBit(int i, int j, int versionBits) {\r
-    return bitMatrix.get(i, j) ? (versionBits << 1) | 0x1 : versionBits << 1;\r
+    return bitMatrix.get(j, i) ? (versionBits << 1) | 0x1 : versionBits << 1;\r
   }\r
 \r
   /**\r
@@ -161,7 +161,7 @@ final class BitMatrixParser {
     // some bits from reading as we wind through the bit matrix.\r
     DataMask dataMask = DataMask.forReference((int) formatInfo.getDataMask());\r
     int dimension = bitMatrix.getDimension();\r
-    dataMask.unmaskBitMatrix(bitMatrix.getBits(), dimension);\r
+    dataMask.unmaskBitMatrix(bitMatrix, dimension);\r
 \r
     BitMatrix functionPattern = version.buildFunctionPattern();\r
 \r
@@ -182,11 +182,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
@@ -198,10 +198,10 @@ final class BitMatrixParser {
           }\r
         }\r
       }\r
-      readingUp = !readingUp; // switch directions\r
+      readingUp ^= true; // readingUp = !readingUp; // switch directions\r
     }\r
     if (resultOffset != version.getTotalCodewords()) {\r
-      throw new ReaderException("Did not read all codewords");\r
+      throw ReaderException.getInstance();\r
     }\r
     return result;\r
   }\r