Committed C# port from Mohamad
[zxing.git] / csharp / qrcode / encoder / QRCode.cs
1 /*\r
2 * Copyright 2007 ZXing authors\r
3 *\r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5 * you may not use this file except in compliance with the License.\r
6 * You may obtain a copy of the License at\r
7 *\r
8 *      http://www.apache.org/licenses/LICENSE-2.0\r
9 *\r
10 * Unless required by applicable law or agreed to in writing, software\r
11 * distributed under the License is distributed on an "AS IS" BASIS,\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 * See the License for the specific language governing permissions and\r
14 * limitations under the License.\r
15 */\r
16 using System;\r
17 using System.Text;\r
18 using com.google.zxing;\r
19 using com.google.zxing.common;\r
20 using com.google.zxing.qrcode.decoder;\r
21 using com.google.zxing.qrcode;\r
22 \r
23 namespace com.google.zxing.qrcode.encoder\r
24 {\r
25 \r
26     public sealed class QRCode \r
27     { \r
28     \r
29           public static int NUM_MASK_PATTERNS = 8;\r
30 \r
31           private Mode mode;\r
32           private ErrorCorrectionLevel ecLevel;\r
33           private int version;\r
34           private int matrixWidth;\r
35           private int maskPattern;\r
36           private int numTotalBytes;\r
37           private int numDataBytes;\r
38           private int numECBytes;\r
39           private int numRSBlocks;\r
40           private ByteMatrix matrix;\r
41 \r
42           public QRCode() {\r
43             mode = null;\r
44             ecLevel = null;\r
45             version = -1;\r
46             matrixWidth = -1;\r
47             maskPattern = -1;\r
48             numTotalBytes = -1;\r
49             numDataBytes = -1;\r
50             numECBytes = -1;\r
51             numRSBlocks = -1;\r
52             matrix = null;\r
53           }\r
54 \r
55           // Mode of the QR Code.\r
56           public Mode getMode() {\r
57             return mode;\r
58           }\r
59 \r
60           // Error correction level of the QR Code.\r
61           public ErrorCorrectionLevel getECLevel() {\r
62             return ecLevel;\r
63           }\r
64 \r
65           // Version of the QR Code.  The bigger size, the bigger version.\r
66           public int getVersion() {\r
67             return version;\r
68           }\r
69 \r
70           // ByteMatrix width of the QR Code.\r
71           public int getMatrixWidth() {\r
72             return matrixWidth;\r
73           }\r
74 \r
75           // Mask pattern of the QR Code.\r
76           public int getMaskPattern() {\r
77             return maskPattern;\r
78           }\r
79 \r
80           // Number of total bytes in the QR Code.\r
81           public int getNumTotalBytes() {\r
82             return numTotalBytes;\r
83           }\r
84 \r
85           // Number of data bytes in the QR Code.\r
86           public int getNumDataBytes() {\r
87             return numDataBytes;\r
88           }\r
89 \r
90           // Number of error correction bytes in the QR Code.\r
91           public int getNumECBytes() {\r
92             return numECBytes;\r
93           }\r
94 \r
95           // Number of Reedsolomon blocks in the QR Code.\r
96           public int getNumRSBlocks() {\r
97             return numRSBlocks;\r
98           }\r
99 \r
100           // ByteMatrix data of the QR Code.\r
101           public ByteMatrix getMatrix() {\r
102             return matrix;\r
103           }\r
104           \r
105 \r
106           // Return the value of the module (cell) pointed by "x" and "y" in the matrix of the QR Code. They\r
107           // call cells in the matrix "modules". 1 represents a black cell, and 0 represents a white cell.\r
108           public int at(int x, int y) {\r
109             // The value must be zero or one.\r
110             int value = matrix.get(y, x);\r
111             if (!(value == 0 || value == 1)) {\r
112               // this is really like an assert... not sure what better exception to use?\r
113               throw new Exception("Bad value");\r
114             }\r
115             return value;\r
116           }\r
117 \r
118           // Checks all the member variables are set properly. Returns true on success. Otherwise, returns\r
119           // false.\r
120           public bool isValid() {\r
121             return\r
122                 // First check if all version are not uninitialized.\r
123                 mode != null &&\r
124                 ecLevel != null &&\r
125                 version != -1 &&\r
126                 matrixWidth != -1 &&\r
127                 maskPattern != -1 &&\r
128                 numTotalBytes != -1 &&\r
129                 numDataBytes != -1 &&\r
130                 numECBytes != -1 &&\r
131                 numRSBlocks != -1 &&\r
132                 // Then check them in other ways..\r
133                 isValidMaskPattern(maskPattern) &&\r
134                 numTotalBytes == numDataBytes + numECBytes &&\r
135                 // ByteMatrix stuff.\r
136                 matrix != null &&\r
137                 matrixWidth == matrix.width() &&\r
138                 // See 7.3.1 of JISX0510:2004 (p.5).\r
139                 matrix.width() == matrix.height(); // Must be square.\r
140           }\r
141 \r
142           // Return debug String.\r
143           public String toString() {\r
144                 StringBuilder result = new StringBuilder(200);\r
145                 result.Append("<<\n");\r
146                 result.Append(" mode: ");\r
147                 result.Append(mode);\r
148                 result.Append("\n ecLevel: ");\r
149                 result.Append(ecLevel);\r
150                 result.Append("\n version: ");\r
151                 result.Append(version);\r
152                 result.Append("\n matrixWidth: ");\r
153                 result.Append(matrixWidth);\r
154                 result.Append("\n maskPattern: ");\r
155                 result.Append(maskPattern);\r
156                 result.Append("\n numTotalBytes: ");\r
157                 result.Append(numTotalBytes);\r
158                 result.Append("\n numDataBytes: ");\r
159                 result.Append(numDataBytes);\r
160                 result.Append("\n numECBytes: ");\r
161                 result.Append(numECBytes);\r
162                 result.Append("\n numRSBlocks: ");\r
163                 result.Append(numRSBlocks);\r
164                 if (matrix == null) {\r
165                   result.Append("\n matrix: null\n");\r
166                 } else {\r
167                   result.Append("\n matrix:\n");\r
168                   result.Append(matrix.toString());\r
169                 }\r
170                 result.Append(">>\n");\r
171                 return result.ToString();\r
172           }\r
173 \r
174           public void setMode(Mode value) {\r
175             mode = value;\r
176           }\r
177 \r
178           public void setECLevel(ErrorCorrectionLevel value) {\r
179             ecLevel = value;\r
180           }\r
181 \r
182           public void setVersion(int value) {\r
183             version = value;\r
184           }\r
185 \r
186           public void setMatrixWidth(int value) {\r
187             matrixWidth = value;\r
188           }\r
189 \r
190           public void setMaskPattern(int value) {\r
191             maskPattern = value;\r
192           }\r
193 \r
194           public void setNumTotalBytes(int value) {\r
195             numTotalBytes = value;\r
196           }\r
197 \r
198           public void setNumDataBytes(int value) {\r
199             numDataBytes = value;\r
200           }\r
201 \r
202           public void setNumECBytes(int value) {\r
203             numECBytes = value;\r
204           }\r
205 \r
206           public void setNumRSBlocks(int value) {\r
207             numRSBlocks = value;\r
208           }\r
209 \r
210           // This takes ownership of the 2D array.\r
211           public void setMatrix(ByteMatrix value) {\r
212             matrix = value;\r
213           }\r
214 \r
215           // Check if "mask_pattern" is valid.\r
216           public static bool isValidMaskPattern(int maskPattern) {\r
217             return maskPattern >= 0 && maskPattern < NUM_MASK_PATTERNS;\r
218           }\r
219 \r
220           // Return true if the all values in the matrix are binary numbers.\r
221           //\r
222           // JAVAPORT: This is going to be super expensive and unnecessary, we should not call this in\r
223           // production. I'm leaving it because it may be useful for testing. It should be removed entirely\r
224           // if ByteMatrix is changed never to contain a -1.\r
225           /*\r
226           private static boolean EverythingIsBinary(final ByteMatrix matrix) {\r
227             for (int y = 0; y < matrix.height(); ++y) {\r
228               for (int x = 0; x < matrix.width(); ++x) {\r
229                 int value = matrix.get(y, x);\r
230                 if (!(value == 0 || value == 1)) {\r
231                   // Found non zero/one value.\r
232                   return false;\r
233                 }\r
234               }\r
235             }\r
236             return true;\r
237           }\r
238             */\r
239     \r
240     }\r
241 \r
242 }