Small style stuff
[zxing.git] / core / src / com / google / zxing / datamatrix / decoder / BitMatrixParser.java
index f54e9c1..45ffd2d 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
 /*\r
- * Copyright 2007 Google Inc.\r
+ * Copyright 2007 ZXing authors\r
  *\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  *\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
@@ -16,7 +16,7 @@
 \r
 package com.google.zxing.datamatrix.decoder;\r
 \r
 \r
 package com.google.zxing.datamatrix.decoder;\r
 \r
-import com.google.zxing.ReaderException;\r
+import com.google.zxing.FormatException;\r
 import com.google.zxing.common.BitMatrix;\r
 \r
 /**\r
 import com.google.zxing.common.BitMatrix;\r
 \r
 /**\r
@@ -26,23 +26,25 @@ final class BitMatrixParser {
 \r
   private final BitMatrix mappingBitMatrix;\r
   private final BitMatrix readMappingMatrix;\r
 \r
   private final BitMatrix mappingBitMatrix;\r
   private final BitMatrix readMappingMatrix;\r
-  private Version version;\r
-//  private FormatInformation parsedFormatInfo;\r
+  private final Version version;\r
 \r
   /**\r
    * @param bitMatrix {@link BitMatrix} to parse\r
 \r
   /**\r
    * @param bitMatrix {@link BitMatrix} to parse\r
-   * @throws ReaderException if dimension is < 10 or > 144 or not 0 mod 2\r
+   * @throws FormatException if dimension is < 8 or > 144 or not 0 mod 2\r
    */\r
    */\r
-  BitMatrixParser(BitMatrix bitMatrix) throws ReaderException {\r
-    int dimension = bitMatrix.getDimension();\r
-    if (dimension < 10 || dimension > 144 || (dimension & 0x01) != 0) {\r
-      throw new ReaderException("Invalid dimension (" + dimension + ")  Must be 0 mod 2 and >= 10 and <= 144");\r
+  BitMatrixParser(BitMatrix bitMatrix) throws FormatException {\r
+    int dimension = bitMatrix.getHeight();\r
+    if (dimension < 8 || dimension > 144 || (dimension & 0x01) != 0) {\r
+      throw FormatException.getFormatInstance();\r
     }\r
     \r
     version = readVersion(bitMatrix);\r
     this.mappingBitMatrix = extractDataRegion(bitMatrix);\r
     }\r
     \r
     version = readVersion(bitMatrix);\r
     this.mappingBitMatrix = extractDataRegion(bitMatrix);\r
-    // TODO(bbrown): Make this work for rectangular symbols\r
-    this.readMappingMatrix = new BitMatrix(this.mappingBitMatrix.getDimension());\r
+    this.readMappingMatrix = new BitMatrix(this.mappingBitMatrix.getWidth(), this.mappingBitMatrix.getHeight());\r
+  }\r
+\r
+  Version getVersion() {\r
+    return version;\r
   }\r
 \r
   /**\r
   }\r
 \r
   /**\r
@@ -53,19 +55,12 @@ final class BitMatrixParser {
    * \r
    * @param bitMatrix Original {@link BitMatrix} including alignment patterns\r
    * @return {@link Version} encapsulating the Data Matrix Code's "version"\r
    * \r
    * @param bitMatrix Original {@link BitMatrix} including alignment patterns\r
    * @return {@link Version} encapsulating the Data Matrix Code's "version"\r
-   * @throws ReaderException if the dimensions of the mapping matrix are not valid\r
+   * @throws FormatException if the dimensions of the mapping matrix are not valid\r
    * Data Matrix dimensions.\r
    */\r
    * Data Matrix dimensions.\r
    */\r
-  Version readVersion(BitMatrix bitMatrix) throws ReaderException {\r
-\r
-    if (version != null) {\r
-      return version;\r
-    }\r
-\r
-    // TODO(bbrown): make this work for rectangular dimensions as well.\r
-    int numRows = bitMatrix.getDimension();\r
-    int numColumns = numRows;\r
-    \r
+  private static Version readVersion(BitMatrix bitMatrix) throws FormatException {\r
+    int numRows = bitMatrix.getHeight();\r
+    int numColumns = bitMatrix.getWidth();\r
     return Version.getVersionForDimensions(numRows, numColumns);\r
   }\r
 \r
     return Version.getVersionForDimensions(numRows, numColumns);\r
   }\r
 \r
@@ -75,18 +70,18 @@ final class BitMatrixParser {
    * Data Matrix Code.</p>\r
    *\r
    * @return bytes encoded within the Data Matrix Code\r
    * Data Matrix Code.</p>\r
    *\r
    * @return bytes encoded within the Data Matrix 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
    */\r
-  byte[] readCodewords() throws ReaderException {\r
+  byte[] readCodewords() throws FormatException {\r
 \r
     byte[] result = new byte[version.getTotalCodewords()];\r
     int resultOffset = 0;\r
     \r
     int row = 4;\r
     int column = 0;\r
 \r
     byte[] result = new byte[version.getTotalCodewords()];\r
     int resultOffset = 0;\r
     \r
     int row = 4;\r
     int column = 0;\r
-    // TODO(bbrown): Data Matrix can be rectangular, assuming square for now\r
-    int numRows = mappingBitMatrix.getDimension();\r
-    int numColumns = numRows;\r
+\r
+    int numRows = mappingBitMatrix.getHeight();\r
+    int numColumns = mappingBitMatrix.getWidth();\r
     \r
     boolean corner1Read = false;\r
     boolean corner2Read = false;\r
     \r
     boolean corner1Read = false;\r
     boolean corner2Read = false;\r
@@ -98,49 +93,57 @@ final class BitMatrixParser {
       // Check the four corner cases\r
       if ((row == numRows) && (column == 0) && !corner1Read) {\r
         result[resultOffset++] = (byte) readCorner1(numRows, numColumns);\r
       // Check the four corner cases\r
       if ((row == numRows) && (column == 0) && !corner1Read) {\r
         result[resultOffset++] = (byte) readCorner1(numRows, numColumns);\r
-        row -= 2; column +=2;\r
+        row -= 2;\r
+        column +=2;\r
         corner1Read = true;\r
       } else if ((row == numRows-2) && (column == 0) && ((numColumns & 0x03) != 0) && !corner2Read) {\r
         result[resultOffset++] = (byte) readCorner2(numRows, numColumns);\r
         corner1Read = true;\r
       } else if ((row == numRows-2) && (column == 0) && ((numColumns & 0x03) != 0) && !corner2Read) {\r
         result[resultOffset++] = (byte) readCorner2(numRows, numColumns);\r
-        row -= 2; column +=2;\r
+        row -= 2;\r
+        column +=2;\r
         corner2Read = true;\r
       } else if ((row == numRows+4) && (column == 2) && ((numColumns & 0x07) == 0) && !corner3Read) {\r
         result[resultOffset++] = (byte) readCorner3(numRows, numColumns);\r
         corner2Read = true;\r
       } else if ((row == numRows+4) && (column == 2) && ((numColumns & 0x07) == 0) && !corner3Read) {\r
         result[resultOffset++] = (byte) readCorner3(numRows, numColumns);\r
-        row -= 2; column +=2;\r
+        row -= 2;\r
+        column +=2;\r
         corner3Read = true;\r
       } else if ((row == numRows-2) && (column == 0) && ((numColumns & 0x07) == 4) && !corner4Read) {\r
         result[resultOffset++] = (byte) readCorner4(numRows, numColumns);\r
         corner3Read = true;\r
       } else if ((row == numRows-2) && (column == 0) && ((numColumns & 0x07) == 4) && !corner4Read) {\r
         result[resultOffset++] = (byte) readCorner4(numRows, numColumns);\r
-        row -= 2; column +=2;\r
+        row -= 2;\r
+        column +=2;\r
         corner4Read = true;\r
       } else {\r
         // Sweep upward diagonally to the right\r
         do {\r
         corner4Read = true;\r
       } else {\r
         // Sweep upward diagonally to the right\r
         do {\r
-          if ((row < numRows) && (column >= 0) && !readMappingMatrix.get(row, column)) {\r
+          if ((row < numRows) && (column >= 0) && !readMappingMatrix.get(column, row)) {\r
             result[resultOffset++] = (byte) readUtah(row, column, numRows, numColumns);\r
           }\r
             result[resultOffset++] = (byte) readUtah(row, column, numRows, numColumns);\r
           }\r
-          row -= 2; column +=2;\r
+          row -= 2;\r
+          column +=2;\r
         } while ((row >= 0) && (column < numColumns));\r
         } while ((row >= 0) && (column < numColumns));\r
-        row += 1; column +=3;\r
+        row += 1;\r
+        column +=3;\r
         \r
         \r
-        // Sweep downward giagonally to the left\r
+        // Sweep downward diagonally to the left\r
         do {\r
         do {\r
-          if ((row >= 0) && (column < numColumns) && !readMappingMatrix.get(row, column)) {\r
+          if ((row >= 0) && (column < numColumns) && !readMappingMatrix.get(column, row)) {\r
              result[resultOffset++] = (byte) readUtah(row, column, numRows, numColumns);\r
           }\r
              result[resultOffset++] = (byte) readUtah(row, column, numRows, numColumns);\r
           }\r
-          row += 2; column -=2;\r
+          row += 2;\r
+          column -=2;\r
         } while ((row < numRows) && (column >= 0));\r
         } while ((row < numRows) && (column >= 0));\r
-        row += 3; column +=1;\r
+        row += 3;\r
+        column +=1;\r
       }\r
     } while ((row < numRows) || (column < numColumns));\r
 \r
     if (resultOffset != version.getTotalCodewords()) {\r
       }\r
     } while ((row < numRows) || (column < numColumns));\r
 \r
     if (resultOffset != version.getTotalCodewords()) {\r
-      throw new ReaderException("Did not read all codewords");\r
+      throw FormatException.getFormatInstance();\r
     }\r
     return result;\r
   }\r
   \r
   /**\r
     }\r
     return result;\r
   }\r
   \r
   /**\r
-   * <p>Reads a bit of the mapping matrix accounting for boundry wrapping.</p>\r
+   * <p>Reads a bit of the mapping matrix accounting for boundary wrapping.</p>\r
    * \r
    * @param row Row to read in the mapping matrix\r
    * @param column Column to read in the mapping matrix\r
    * \r
    * @param row Row to read in the mapping matrix\r
    * @param column Column to read in the mapping matrix\r
@@ -149,7 +152,7 @@ final class BitMatrixParser {
    * @return value of the given bit in the mapping matrix\r
    */\r
   boolean readModule(int row, int column, int numRows, int numColumns) {\r
    * @return value of the given bit in the mapping matrix\r
    */\r
   boolean readModule(int row, int column, int numRows, int numColumns) {\r
-    // Adjust the row and column indicies based on boundry wrapping\r
+    // Adjust the row and column indices based on boundary wrapping\r
     if (row < 0) {\r
       row += numRows;\r
       column += 4 - ((numRows + 4) & 0x07);\r
     if (row < 0) {\r
       row += numRows;\r
       column += 4 - ((numRows + 4) & 0x07);\r
@@ -158,12 +161,12 @@ final class BitMatrixParser {
       column += numColumns;\r
       row += 4 - ((numColumns + 4) & 0x07);\r
     }\r
       column += numColumns;\r
       row += 4 - ((numColumns + 4) & 0x07);\r
     }\r
-    readMappingMatrix.set(row, column);\r
-    return mappingBitMatrix.get(row, column);\r
+    readMappingMatrix.set(column, row);\r
+    return mappingBitMatrix.get(column, row);\r
   }\r
   \r
   /**\r
   }\r
   \r
   /**\r
-   * <p>Reads the 8 bits of the standard utah shaped pattern.</p>\r
+   * <p>Reads the 8 bits of the standard Utah-shaped pattern.</p>\r
    * \r
    * <p>See ISO 16022:2006, 5.8.1 Figure 6</p>\r
    * \r
    * \r
    * <p>See ISO 16022:2006, 5.8.1 Figure 6</p>\r
    * \r
@@ -400,8 +403,7 @@ final class BitMatrixParser {
     int symbolSizeRows = version.getSymbolSizeRows();\r
     int symbolSizeColumns = version.getSymbolSizeColumns();\r
     \r
     int symbolSizeRows = version.getSymbolSizeRows();\r
     int symbolSizeColumns = version.getSymbolSizeColumns();\r
     \r
-    // TODO(bbrown): Make this work with rectangular codes\r
-    if (bitMatrix.getDimension() != symbolSizeRows) {\r
+    if (bitMatrix.getHeight() != symbolSizeRows) {\r
       throw new IllegalArgumentException("Dimension of bitMarix must match the version size");\r
     }\r
     \r
       throw new IllegalArgumentException("Dimension of bitMarix must match the version size");\r
     }\r
     \r
@@ -412,27 +414,26 @@ final class BitMatrixParser {
     int numDataRegionsColumn = symbolSizeColumns / dataRegionSizeColumns;\r
     \r
     int sizeDataRegionRow = numDataRegionsRow * dataRegionSizeRows;\r
     int numDataRegionsColumn = symbolSizeColumns / dataRegionSizeColumns;\r
     \r
     int sizeDataRegionRow = numDataRegionsRow * dataRegionSizeRows;\r
-    //int sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns;\r
+    int sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns;\r
     \r
     \r
-    // TODO(bbrown): Make this work with rectangular codes\r
-    BitMatrix bitMatrixWithoutAlignment = new BitMatrix(sizeDataRegionRow);\r
+    BitMatrix bitMatrixWithoutAlignment = new BitMatrix(sizeDataRegionColumn, sizeDataRegionRow);\r
     for (int dataRegionRow = 0; dataRegionRow < numDataRegionsRow; ++dataRegionRow) {\r
     for (int dataRegionRow = 0; dataRegionRow < numDataRegionsRow; ++dataRegionRow) {\r
+      int dataRegionRowOffset = dataRegionRow * dataRegionSizeRows;\r
       for (int dataRegionColumn = 0; dataRegionColumn < numDataRegionsColumn; ++dataRegionColumn) {\r
       for (int dataRegionColumn = 0; dataRegionColumn < numDataRegionsColumn; ++dataRegionColumn) {\r
+        int dataRegionColumnOffset = dataRegionColumn * dataRegionSizeColumns;\r
         for (int i = 0; i < dataRegionSizeRows; ++i) {\r
         for (int i = 0; i < dataRegionSizeRows; ++i) {\r
+          int readRowOffset = dataRegionRow * (dataRegionSizeRows + 2) + 1 + i;\r
+          int writeRowOffset = dataRegionRowOffset + i;\r
           for (int j = 0; j < dataRegionSizeColumns; ++j) {\r
           for (int j = 0; j < dataRegionSizeColumns; ++j) {\r
-            int readRowOffset = dataRegionRow * (dataRegionSizeRows + 2) + 1 + i;\r
             int readColumnOffset = dataRegionColumn * (dataRegionSizeColumns + 2) + 1 + j;\r
             int readColumnOffset = dataRegionColumn * (dataRegionSizeColumns + 2) + 1 + j;\r
-            int writeRowOffset = dataRegionRow * dataRegionSizeRows + i;\r
-            int writeColumnOffset = dataRegionColumn * dataRegionSizeColumns + j;\r
-\r
-            if (bitMatrix.get(readRowOffset, readColumnOffset)) {\r
-              bitMatrixWithoutAlignment.set(writeRowOffset, writeColumnOffset);\r
+            if (bitMatrix.get(readColumnOffset, readRowOffset)) {\r
+              int writeColumnOffset = dataRegionColumnOffset + j;\r
+              bitMatrixWithoutAlignment.set(writeColumnOffset, writeRowOffset);\r
             }\r
           }\r
         }\r
       }\r
     }\r
             }\r
           }\r
         }\r
       }\r
     }\r
-    \r
     return bitMatrixWithoutAlignment;\r
   }\r
 \r
     return bitMatrixWithoutAlignment;\r
   }\r
 \r