Small style stuff
[zxing.git] / csharp / qrcode / encoder / QRCode.cs
1 /*\r
2 * Copyright 2008 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 ByteMatrix = com.google.zxing.common.ByteMatrix;\r
18 using ErrorCorrectionLevel = com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;\r
19 using Mode = com.google.zxing.qrcode.decoder.Mode;\r
20 namespace com.google.zxing.qrcode.encoder\r
21 {\r
22         \r
23         /// <author>  satorux@google.com (Satoru Takabayashi) - creator\r
24         /// </author>\r
25         /// <author>  dswitkin@google.com (Daniel Switkin) - ported from C++\r
26         /// </author>\r
27         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
28         /// </author>\r
29         public sealed class QRCode\r
30         {\r
31                 public Mode Mode\r
32                 {\r
33                         // Mode of the QR Code.\r
34                         \r
35                         get\r
36                         {\r
37                                 return mode;\r
38                         }\r
39                         \r
40                         set\r
41                         {\r
42                                 mode = value;\r
43                         }\r
44                         \r
45                 }\r
46                 public ErrorCorrectionLevel ECLevel\r
47                 {\r
48                         // Error correction level of the QR Code.\r
49                         \r
50                         get\r
51                         {\r
52                                 return ecLevel;\r
53                         }\r
54                         \r
55                         set\r
56                         {\r
57                                 ecLevel = value;\r
58                         }\r
59                         \r
60                 }\r
61                 public int Version\r
62                 {\r
63                         // Version of the QR Code.  The bigger size, the bigger version.\r
64                         \r
65                         get\r
66                         {\r
67                                 return version;\r
68                         }\r
69                         \r
70                         set\r
71                         {\r
72                                 version = value;\r
73                         }\r
74                         \r
75                 }\r
76                 public int MatrixWidth\r
77                 {\r
78                         // ByteMatrix width of the QR Code.\r
79                         \r
80                         get\r
81                         {\r
82                                 return matrixWidth;\r
83                         }\r
84                         \r
85                         set\r
86                         {\r
87                                 matrixWidth = value;\r
88                         }\r
89                         \r
90                 }\r
91                 public int MaskPattern\r
92                 {\r
93                         // Mask pattern of the QR Code.\r
94                         \r
95                         get\r
96                         {\r
97                                 return maskPattern;\r
98                         }\r
99                         \r
100                         set\r
101                         {\r
102                                 maskPattern = value;\r
103                         }\r
104                         \r
105                 }\r
106                 public int NumTotalBytes\r
107                 {\r
108                         // Number of total bytes in the QR Code.\r
109                         \r
110                         get\r
111                         {\r
112                                 return numTotalBytes;\r
113                         }\r
114                         \r
115                         set\r
116                         {\r
117                                 numTotalBytes = value;\r
118                         }\r
119                         \r
120                 }\r
121                 public int NumDataBytes\r
122                 {\r
123                         // Number of data bytes in the QR Code.\r
124                         \r
125                         get\r
126                         {\r
127                                 return numDataBytes;\r
128                         }\r
129                         \r
130                         set\r
131                         {\r
132                                 numDataBytes = value;\r
133                         }\r
134                         \r
135                 }\r
136                 public int NumECBytes\r
137                 {\r
138                         // Number of error correction bytes in the QR Code.\r
139                         \r
140                         get\r
141                         {\r
142                                 return numECBytes;\r
143                         }\r
144                         \r
145                         set\r
146                         {\r
147                                 numECBytes = value;\r
148                         }\r
149                         \r
150                 }\r
151                 public int NumRSBlocks\r
152                 {\r
153                         // Number of Reedsolomon blocks in the QR Code.\r
154                         \r
155                         get\r
156                         {\r
157                                 return numRSBlocks;\r
158                         }\r
159                         \r
160                         set\r
161                         {\r
162                                 numRSBlocks = value;\r
163                         }\r
164                         \r
165                 }\r
166                 public ByteMatrix Matrix\r
167                 {\r
168                         // ByteMatrix data of the QR Code.\r
169                         \r
170                         get\r
171                         {\r
172                                 return matrix;\r
173                         }\r
174                         \r
175                         // This takes ownership of the 2D array.\r
176                         \r
177                         set\r
178                         {\r
179                                 matrix = value;\r
180                         }\r
181                         \r
182                 }\r
183                 public bool Valid\r
184                 {\r
185                         // Checks all the member variables are set properly. Returns true on success. Otherwise, returns\r
186                         // false.\r
187                         \r
188                         get\r
189                         {\r
190                                 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
191                         }\r
192                         \r
193                 }\r
194                 \r
195                 public const int NUM_MASK_PATTERNS = 8;\r
196                 \r
197                 private Mode mode;\r
198                 private ErrorCorrectionLevel ecLevel;\r
199                 private int version;\r
200                 private int matrixWidth;\r
201                 private int maskPattern;\r
202                 private int numTotalBytes;\r
203                 private int numDataBytes;\r
204                 private int numECBytes;\r
205                 private int numRSBlocks;\r
206                 private ByteMatrix matrix;\r
207                 \r
208                 public QRCode()\r
209                 {\r
210                         mode = null;\r
211                         ecLevel = null;\r
212                         version = - 1;\r
213                         matrixWidth = - 1;\r
214                         maskPattern = - 1;\r
215                         numTotalBytes = - 1;\r
216                         numDataBytes = - 1;\r
217                         numECBytes = - 1;\r
218                         numRSBlocks = - 1;\r
219                         matrix = null;\r
220                 }\r
221                 \r
222                 \r
223                 // Return the value of the module (cell) pointed by "x" and "y" in the matrix of the QR Code. They\r
224                 // call cells in the matrix "modules". 1 represents a black cell, and 0 represents a white cell.\r
225                 public int at(int x, int y)\r
226                 {\r
227                         // The value must be zero or one.\r
228                         int value_Renamed = matrix.get_Renamed(x, y);\r
229                         if (!(value_Renamed == 0 || value_Renamed == 1))\r
230                         {\r
231                                 // this is really like an assert... not sure what better exception to use?\r
232                                 throw new System.SystemException("Bad value");\r
233                         }\r
234                         return value_Renamed;\r
235                 }\r
236                 \r
237                 // Return debug String.\r
238                 public override System.String ToString()\r
239                 {\r
240                         System.Text.StringBuilder result = new System.Text.StringBuilder(200);\r
241                         result.Append("<<\n");\r
242                         result.Append(" mode: ");\r
243                         result.Append(mode);\r
244                         result.Append("\n ecLevel: ");\r
245                         result.Append(ecLevel);\r
246                         result.Append("\n version: ");\r
247                         result.Append(version);\r
248                         result.Append("\n matrixWidth: ");\r
249                         result.Append(matrixWidth);\r
250                         result.Append("\n maskPattern: ");\r
251                         result.Append(maskPattern);\r
252                         result.Append("\n numTotalBytes: ");\r
253                         result.Append(numTotalBytes);\r
254                         result.Append("\n numDataBytes: ");\r
255                         result.Append(numDataBytes);\r
256                         result.Append("\n numECBytes: ");\r
257                         result.Append(numECBytes);\r
258                         result.Append("\n numRSBlocks: ");\r
259                         result.Append(numRSBlocks);\r
260                         if (matrix == null)\r
261                         {\r
262                                 result.Append("\n matrix: null\n");\r
263                         }\r
264                         else\r
265                         {\r
266                                 result.Append("\n matrix:\n");\r
267                                 result.Append(matrix.ToString());\r
268                         }\r
269                         result.Append(">>\n");\r
270                         return result.ToString();\r
271                 }\r
272                 \r
273                 // Check if "mask_pattern" is valid.\r
274                 public static bool isValidMaskPattern(int maskPattern)\r
275                 {\r
276                         return maskPattern >= 0 && maskPattern < NUM_MASK_PATTERNS;\r
277                 }\r
278                 \r
279                 // Return true if the all values in the matrix are binary numbers.\r
280                 //\r
281                 // JAVAPORT: This is going to be super expensive and unnecessary, we should not call this in\r
282                 // production. I'm leaving it because it may be useful for testing. It should be removed entirely\r
283                 // if ByteMatrix is changed never to contain a -1.\r
284                 /*\r
285                 private static boolean EverythingIsBinary(final ByteMatrix matrix) {\r
286                 for (int y = 0; y < matrix.height(); ++y) {\r
287                 for (int x = 0; x < matrix.width(); ++x) {\r
288                 int value = matrix.get(y, x);\r
289                 if (!(value == 0 || value == 1)) {\r
290                 // Found non zero/one value.\r
291                 return false;\r
292                 }\r
293                 }\r
294                 }\r
295                 return true;\r
296                 }\r
297                 */\r
298         }\r
299 }