Bumped Barcode Scanner version to 3.5 final.
[zxing.git] / csharp / datamatrix / decoder / Version.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 ReaderException = com.google.zxing.ReaderException;\r
18 namespace com.google.zxing.datamatrix.decoder\r
19 {\r
20         \r
21         /// <summary> The Version object encapsulates attributes about a particular\r
22         /// size Data Matrix Code.\r
23         /// \r
24         /// </summary>\r
25         /// <author>  bbrown@google.com (Brian Brown)\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 Version\r
30         {\r
31                 public int VersionNumber\r
32                 {\r
33                         get\r
34                         {\r
35                                 return versionNumber;\r
36                         }\r
37                         \r
38                 }\r
39                 public int SymbolSizeRows\r
40                 {\r
41                         get\r
42                         {\r
43                                 return symbolSizeRows;\r
44                         }\r
45                         \r
46                 }\r
47                 public int SymbolSizeColumns\r
48                 {\r
49                         get\r
50                         {\r
51                                 return symbolSizeColumns;\r
52                         }\r
53                         \r
54                 }\r
55                 public int DataRegionSizeRows\r
56                 {\r
57                         get\r
58                         {\r
59                                 return dataRegionSizeRows;\r
60                         }\r
61                         \r
62                 }\r
63                 public int DataRegionSizeColumns\r
64                 {\r
65                         get\r
66                         {\r
67                                 return dataRegionSizeColumns;\r
68                         }\r
69                         \r
70                 }\r
71                 public int TotalCodewords\r
72                 {\r
73                         get\r
74                         {\r
75                                 return totalCodewords;\r
76                         }\r
77                         \r
78                 }\r
79                 \r
80                 //UPGRADE_NOTE: Final was removed from the declaration of 'VERSIONS '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
81                 private static readonly Version[] VERSIONS = buildVersions();\r
82                 \r
83                 //UPGRADE_NOTE: Final was removed from the declaration of 'versionNumber '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
84                 private int versionNumber;\r
85                 //UPGRADE_NOTE: Final was removed from the declaration of 'symbolSizeRows '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
86                 private int symbolSizeRows;\r
87                 //UPGRADE_NOTE: Final was removed from the declaration of 'symbolSizeColumns '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
88                 private int symbolSizeColumns;\r
89                 //UPGRADE_NOTE: Final was removed from the declaration of 'dataRegionSizeRows '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
90                 private int dataRegionSizeRows;\r
91                 //UPGRADE_NOTE: Final was removed from the declaration of 'dataRegionSizeColumns '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
92                 private int dataRegionSizeColumns;\r
93                 //UPGRADE_NOTE: Final was removed from the declaration of 'ecBlocks '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
94                 private ECBlocks ecBlocks;\r
95                 //UPGRADE_NOTE: Final was removed from the declaration of 'totalCodewords '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
96                 private int totalCodewords;\r
97                 \r
98                 private Version(int versionNumber, int symbolSizeRows, int symbolSizeColumns, int dataRegionSizeRows, int dataRegionSizeColumns, ECBlocks ecBlocks)\r
99                 {\r
100                         this.versionNumber = versionNumber;\r
101                         this.symbolSizeRows = symbolSizeRows;\r
102                         this.symbolSizeColumns = symbolSizeColumns;\r
103                         this.dataRegionSizeRows = dataRegionSizeRows;\r
104                         this.dataRegionSizeColumns = dataRegionSizeColumns;\r
105                         this.ecBlocks = ecBlocks;\r
106                         \r
107                         // Calculate the total number of codewords\r
108                         int total = 0;\r
109                         int ecCodewords = ecBlocks.ECCodewords;\r
110                         ECB[] ecbArray = ecBlocks.getECBlocks();\r
111                         for (int i = 0; i < ecbArray.Length; i++)\r
112                         {\r
113                                 ECB ecBlock = ecbArray[i];\r
114                                 total += ecBlock.Count * (ecBlock.DataCodewords + ecCodewords);\r
115                         }\r
116                         this.totalCodewords = total;\r
117                 }\r
118                 \r
119                 internal ECBlocks getECBlocks()\r
120                 {\r
121                         return ecBlocks;\r
122                 }\r
123                 \r
124                 /// <summary> <p>Deduces version information from Data Matrix dimensions.</p>\r
125                 /// \r
126                 /// </summary>\r
127                 /// <param name="numRows">Number of rows in modules\r
128                 /// </param>\r
129                 /// <param name="numColumns">Number of columns in modules\r
130                 /// </param>\r
131                 /// <returns> {@link Version} for a Data Matrix Code of those dimensions\r
132                 /// </returns>\r
133                 /// <throws>  ReaderException if dimensions do correspond to a valid Data Matrix size </throws>\r
134                 public static Version getVersionForDimensions(int numRows, int numColumns)\r
135                 {\r
136                         if ((numRows & 0x01) != 0 || (numColumns & 0x01) != 0)\r
137                         {\r
138                                 throw ReaderException.Instance;\r
139                         }\r
140                         \r
141                         // TODO(bbrown): This is doing a linear search through the array of versions.\r
142                         // If we interleave the rectangular versions with the square versions we could\r
143                         // do a binary search.\r
144                         int numVersions = VERSIONS.Length;\r
145                         for (int i = 0; i < numVersions; ++i)\r
146                         {\r
147                                 Version version = VERSIONS[i];\r
148                                 if (version.symbolSizeRows == numRows && version.symbolSizeColumns == numColumns)\r
149                                 {\r
150                                         return version;\r
151                                 }\r
152                         }\r
153                         \r
154                         throw ReaderException.Instance;\r
155                 }\r
156                 \r
157                 /// <summary> <p>Encapsulates a set of error-correction blocks in one symbol version. Most versions will\r
158                 /// use blocks of differing sizes within one version, so, this encapsulates the parameters for\r
159                 /// each set of blocks. It also holds the number of error-correction codewords per block since it\r
160                 /// will be the same across all blocks within one version.</p>\r
161                 /// </summary>\r
162                 internal sealed class ECBlocks\r
163                 {\r
164                         internal int ECCodewords\r
165                         {\r
166                                 get\r
167                                 {\r
168                                         return ecCodewords;\r
169                                 }\r
170                                 \r
171                         }\r
172                         //UPGRADE_NOTE: Final was removed from the declaration of 'ecCodewords '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
173                         private int ecCodewords;\r
174                         //UPGRADE_NOTE: Final was removed from the declaration of 'ecBlocks '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
175                         private ECB[] ecBlocks;\r
176                         \r
177                         internal ECBlocks(int ecCodewords, ECB ecBlocks)\r
178                         {\r
179                                 this.ecCodewords = ecCodewords;\r
180                                 this.ecBlocks = new ECB[]{ecBlocks};\r
181                         }\r
182                         \r
183                         internal ECBlocks(int ecCodewords, ECB ecBlocks1, ECB ecBlocks2)\r
184                         {\r
185                                 this.ecCodewords = ecCodewords;\r
186                                 this.ecBlocks = new ECB[]{ecBlocks1, ecBlocks2};\r
187                         }\r
188                         \r
189                         internal ECB[] getECBlocks()\r
190                         {\r
191                                 return ecBlocks;\r
192                         }\r
193                 }\r
194                 \r
195                 /// <summary> <p>Encapsualtes the parameters for one error-correction block in one symbol version.\r
196                 /// This includes the number of data codewords, and the number of times a block with these\r
197                 /// parameters is used consecutively in the Data Matrix code version's format.</p>\r
198                 /// </summary>\r
199                 internal sealed class ECB\r
200                 {\r
201                         internal int Count\r
202                         {\r
203                                 get\r
204                                 {\r
205                                         return count;\r
206                                 }\r
207                                 \r
208                         }\r
209                         internal int DataCodewords\r
210                         {\r
211                                 get\r
212                                 {\r
213                                         return dataCodewords;\r
214                                 }\r
215                                 \r
216                         }\r
217                         //UPGRADE_NOTE: Final was removed from the declaration of 'count '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
218                         private int count;\r
219                         //UPGRADE_NOTE: Final was removed from the declaration of 'dataCodewords '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
220                         private int dataCodewords;\r
221                         \r
222                         internal ECB(int count, int dataCodewords)\r
223                         {\r
224                                 this.count = count;\r
225                                 this.dataCodewords = dataCodewords;\r
226                         }\r
227                 }\r
228                 \r
229                 public override System.String ToString()\r
230                 {\r
231                         return System.Convert.ToString(versionNumber);\r
232                 }\r
233                 \r
234                 /// <summary> See ISO 16022:2006 5.5.1 Table 7</summary>\r
235                 private static Version[] buildVersions()\r
236                 {\r
237                         return new Version[]{new Version(1, 10, 10, 8, 8, new ECBlocks(5, new ECB(1, 3))), new Version(2, 12, 12, 10, 10, new ECBlocks(7, new ECB(1, 5))), new Version(3, 14, 14, 12, 12, new ECBlocks(10, new ECB(1, 8))), new Version(4, 16, 16, 14, 14, new ECBlocks(12, new ECB(1, 12))), new Version(5, 18, 18, 16, 16, new ECBlocks(14, new ECB(1, 18))), new Version(6, 20, 20, 18, 18, new ECBlocks(18, new ECB(1, 22))), new Version(7, 22, 22, 20, 20, new ECBlocks(20, new ECB(1, 30))), new Version(8, 24, 24, 22, 22, new ECBlocks(24, new ECB(1, 36))), new Version(9, 26, 26, 24, 24, new ECBlocks(28, new ECB(1, 44))), new Version(10, 32, 32, 14, 14, new ECBlocks(36, new ECB(1, 62))), new Version(11, 36, 36, 16, 16, new ECBlocks(42, new ECB(1, 86))), new Version(12, 40, 40, 18, 18, new ECBlocks(48, new ECB(1, 114))), new Version(13, 44, 44, 20, 20, new ECBlocks(56, new ECB(1, 144))), new Version(14, 48, 48, 22, 22, new ECBlocks(68, new ECB(1, 174))), new Version(15, 52, 52, 24, 24, new ECBlocks(42, new ECB(2, 102))), new Version(16, 64, 64, 14, 14, new ECBlocks(56, new ECB(2, 140))), new Version(17, 72, 72, 16, 16, new ECBlocks(36, new ECB(4, 92))), new Version(18, 80, 80, 18, 18, new ECBlocks(48, new ECB(4, 114))), new Version(19, 88, 88, 20, 20, new ECBlocks(56, new ECB(4, 144))), new Version(20, 96, 96, 22, 22, new ECBlocks(68, new ECB(4, 174))), new Version(21, 104, 104, 24, 24, new ECBlocks(56, new ECB(6, 136))), new Version(22, 120, 120, 18, 18, new ECBlocks(68, new ECB(6, 175))), new Version(23, 132, 132, 20, 20, new ECBlocks(62, new ECB(8, 163))), new Version(24, 144, 144, 22, 22, new ECBlocks(62, new ECB(8, 156), new ECB(2, 155))), new Version(25, 8, 18, 6, 16, new ECBlocks(7, new ECB(1, 5))), new Version(26, 8, 32, 6, 14, new ECBlocks(11, new ECB(1, 10))), new Version(27, 12, 26, 10, 24, new ECBlocks(14, new ECB(1, 16))), new Version(28, 12, 36, 10, 16, new ECBlocks(18, new ECB(1, 22))), new Version(29, 16, 36, 10, 16, new ECBlocks(24, new ECB(1, 32))), new Version(30, 16, 48, 14, 22, new ECBlocks(28, \r
238                                 new ECB(1, 49)))};\r
239                 }\r
240         }\r
241 }