New C# port from Suraj Supekar
[zxing.git] / csharp / qrcode / encoder / QRCode.cs
diff --git a/csharp/qrcode/encoder/QRCode.cs b/csharp/qrcode/encoder/QRCode.cs
new file mode 100755 (executable)
index 0000000..972fd02
--- /dev/null
@@ -0,0 +1,299 @@
+/*\r
+* Copyright 2008 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
+* You may obtain a copy of the License at\r
+*\r
+*      http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+*/\r
+using System;\r
+using ByteMatrix = com.google.zxing.common.ByteMatrix;\r
+using ErrorCorrectionLevel = com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;\r
+using Mode = com.google.zxing.qrcode.decoder.Mode;\r
+namespace com.google.zxing.qrcode.encoder\r
+{\r
+       \r
+       /// <author>  satorux@google.com (Satoru Takabayashi) - creator\r
+       /// </author>\r
+       /// <author>  dswitkin@google.com (Daniel Switkin) - ported from C++\r
+       /// </author>\r
+       /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
+       /// </author>\r
+       public sealed class QRCode\r
+       {\r
+               public Mode Mode\r
+               {\r
+                       // Mode of the QR Code.\r
+                       \r
+                       get\r
+                       {\r
+                               return mode;\r
+                       }\r
+                       \r
+                       set\r
+                       {\r
+                               mode = value;\r
+                       }\r
+                       \r
+               }\r
+               public ErrorCorrectionLevel ECLevel\r
+               {\r
+                       // Error correction level of the QR Code.\r
+                       \r
+                       get\r
+                       {\r
+                               return ecLevel;\r
+                       }\r
+                       \r
+                       set\r
+                       {\r
+                               ecLevel = value;\r
+                       }\r
+                       \r
+               }\r
+               public int Version\r
+               {\r
+                       // Version of the QR Code.  The bigger size, the bigger version.\r
+                       \r
+                       get\r
+                       {\r
+                               return version;\r
+                       }\r
+                       \r
+                       set\r
+                       {\r
+                               version = value;\r
+                       }\r
+                       \r
+               }\r
+               public int MatrixWidth\r
+               {\r
+                       // ByteMatrix width of the QR Code.\r
+                       \r
+                       get\r
+                       {\r
+                               return matrixWidth;\r
+                       }\r
+                       \r
+                       set\r
+                       {\r
+                               matrixWidth = value;\r
+                       }\r
+                       \r
+               }\r
+               public int MaskPattern\r
+               {\r
+                       // Mask pattern of the QR Code.\r
+                       \r
+                       get\r
+                       {\r
+                               return maskPattern;\r
+                       }\r
+                       \r
+                       set\r
+                       {\r
+                               maskPattern = value;\r
+                       }\r
+                       \r
+               }\r
+               public int NumTotalBytes\r
+               {\r
+                       // Number of total bytes in the QR Code.\r
+                       \r
+                       get\r
+                       {\r
+                               return numTotalBytes;\r
+                       }\r
+                       \r
+                       set\r
+                       {\r
+                               numTotalBytes = value;\r
+                       }\r
+                       \r
+               }\r
+               public int NumDataBytes\r
+               {\r
+                       // Number of data bytes in the QR Code.\r
+                       \r
+                       get\r
+                       {\r
+                               return numDataBytes;\r
+                       }\r
+                       \r
+                       set\r
+                       {\r
+                               numDataBytes = value;\r
+                       }\r
+                       \r
+               }\r
+               public int NumECBytes\r
+               {\r
+                       // Number of error correction bytes in the QR Code.\r
+                       \r
+                       get\r
+                       {\r
+                               return numECBytes;\r
+                       }\r
+                       \r
+                       set\r
+                       {\r
+                               numECBytes = value;\r
+                       }\r
+                       \r
+               }\r
+               public int NumRSBlocks\r
+               {\r
+                       // Number of Reedsolomon blocks in the QR Code.\r
+                       \r
+                       get\r
+                       {\r
+                               return numRSBlocks;\r
+                       }\r
+                       \r
+                       set\r
+                       {\r
+                               numRSBlocks = value;\r
+                       }\r
+                       \r
+               }\r
+               public ByteMatrix Matrix\r
+               {\r
+                       // ByteMatrix data of the QR Code.\r
+                       \r
+                       get\r
+                       {\r
+                               return matrix;\r
+                       }\r
+                       \r
+                       // This takes ownership of the 2D array.\r
+                       \r
+                       set\r
+                       {\r
+                               matrix = value;\r
+                       }\r
+                       \r
+               }\r
+               public bool Valid\r
+               {\r
+                       // Checks all the member variables are set properly. Returns true on success. Otherwise, returns\r
+                       // false.\r
+                       \r
+                       get\r
+                       {\r
+                               return mode != null && ecLevel != null && version != - 1 && matrixWidth != - 1 && maskPattern != - 1 && numTotalBytes != - 1 && numDataBytes != - 1 && numECBytes != - 1 && numRSBlocks != - 1 && isValidMaskPattern(maskPattern) && numTotalBytes == numDataBytes + numECBytes && matrix != null && matrixWidth == matrix.Width && matrix.Width == matrix.Height; // Must be square.\r
+                       }\r
+                       \r
+               }\r
+               \r
+               public const int NUM_MASK_PATTERNS = 8;\r
+               \r
+               private Mode mode;\r
+               private ErrorCorrectionLevel ecLevel;\r
+               private int version;\r
+               private int matrixWidth;\r
+               private int maskPattern;\r
+               private int numTotalBytes;\r
+               private int numDataBytes;\r
+               private int numECBytes;\r
+               private int numRSBlocks;\r
+               private ByteMatrix matrix;\r
+               \r
+               public QRCode()\r
+               {\r
+                       mode = null;\r
+                       ecLevel = null;\r
+                       version = - 1;\r
+                       matrixWidth = - 1;\r
+                       maskPattern = - 1;\r
+                       numTotalBytes = - 1;\r
+                       numDataBytes = - 1;\r
+                       numECBytes = - 1;\r
+                       numRSBlocks = - 1;\r
+                       matrix = null;\r
+               }\r
+               \r
+               \r
+               // Return the value of the module (cell) pointed by "x" and "y" in the matrix of the QR Code. They\r
+               // call cells in the matrix "modules". 1 represents a black cell, and 0 represents a white cell.\r
+               public int at(int x, int y)\r
+               {\r
+                       // The value must be zero or one.\r
+                       int value_Renamed = matrix.get_Renamed(x, y);\r
+                       if (!(value_Renamed == 0 || value_Renamed == 1))\r
+                       {\r
+                               // this is really like an assert... not sure what better exception to use?\r
+                               throw new System.SystemException("Bad value");\r
+                       }\r
+                       return value_Renamed;\r
+               }\r
+               \r
+               // Return debug String.\r
+               public override System.String ToString()\r
+               {\r
+                       System.Text.StringBuilder result = new System.Text.StringBuilder(200);\r
+                       result.Append("<<\n");\r
+                       result.Append(" mode: ");\r
+                       result.Append(mode);\r
+                       result.Append("\n ecLevel: ");\r
+                       result.Append(ecLevel);\r
+                       result.Append("\n version: ");\r
+                       result.Append(version);\r
+                       result.Append("\n matrixWidth: ");\r
+                       result.Append(matrixWidth);\r
+                       result.Append("\n maskPattern: ");\r
+                       result.Append(maskPattern);\r
+                       result.Append("\n numTotalBytes: ");\r
+                       result.Append(numTotalBytes);\r
+                       result.Append("\n numDataBytes: ");\r
+                       result.Append(numDataBytes);\r
+                       result.Append("\n numECBytes: ");\r
+                       result.Append(numECBytes);\r
+                       result.Append("\n numRSBlocks: ");\r
+                       result.Append(numRSBlocks);\r
+                       if (matrix == null)\r
+                       {\r
+                               result.Append("\n matrix: null\n");\r
+                       }\r
+                       else\r
+                       {\r
+                               result.Append("\n matrix:\n");\r
+                               result.Append(matrix.ToString());\r
+                       }\r
+                       result.Append(">>\n");\r
+                       return result.ToString();\r
+               }\r
+               \r
+               // Check if "mask_pattern" is valid.\r
+               public static bool isValidMaskPattern(int maskPattern)\r
+               {\r
+                       return maskPattern >= 0 && maskPattern < NUM_MASK_PATTERNS;\r
+               }\r
+               \r
+               // Return true if the all values in the matrix are binary numbers.\r
+               //\r
+               // JAVAPORT: This is going to be super expensive and unnecessary, we should not call this in\r
+               // production. I'm leaving it because it may be useful for testing. It should be removed entirely\r
+               // if ByteMatrix is changed never to contain a -1.\r
+               /*\r
+               private static boolean EverythingIsBinary(final ByteMatrix matrix) {\r
+               for (int y = 0; y < matrix.height(); ++y) {\r
+               for (int x = 0; x < matrix.width(); ++x) {\r
+               int value = matrix.get(y, x);\r
+               if (!(value == 0 || value == 1)) {\r
+               // Found non zero/one value.\r
+               return false;\r
+               }\r
+               }\r
+               }\r
+               return true;\r
+               }\r
+               */\r
+       }\r
+}
\ No newline at end of file