Issue 489 update the port
[zxing.git] / cpp / core / src / zxing / qrcode / decoder / Mode.cpp
1 /*
2  *  Mode.cpp
3  *  zxing
4  *
5  *  Created by Christian Brunschen on 19/05/2008.
6  *  Copyright 2008 ZXing authors All rights reserved.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <zxing/qrcode/decoder/Mode.h>
22 #include <zxing/common/Counted.h>
23 #include <zxing/ReaderException.h>
24 #include <zxing/qrcode/Version.h>
25 #include <sstream>
26
27 namespace zxing {
28 namespace qrcode {
29 using namespace std;
30
31 Mode Mode::TERMINATOR(0, 0, 0);
32 Mode Mode::NUMERIC(10, 12, 14);
33 Mode Mode::ALPHANUMERIC(9, 11, 13);
34 Mode Mode::BYTE(8, 16, 16);
35 Mode Mode::KANJI(8, 10, 12);
36
37 Mode::Mode(int cbv0_9, int cbv10_26, int cbv27) :
38     characterCountBitsForVersions0To9_(cbv0_9), characterCountBitsForVersions10To26_(cbv10_26),
39     characterCountBitsForVersions27AndHigher_(cbv27) {
40 }
41
42 Mode& Mode::forBits(int bits) {
43   switch (bits) {
44   case 0x0:
45     return TERMINATOR;
46   case 0x1:
47     return NUMERIC;
48   case 0x2:
49     return ALPHANUMERIC;
50   case 0x4:
51     return BYTE;
52   case 0x8:
53     return KANJI;
54   default:
55     ostringstream s;
56     s << "Illegal mode bits: " << bits;
57     throw ReaderException(s.str().c_str());
58   }
59 }
60
61 int Mode::getCharacterCountBits(Version *version) {
62   int number = version->getVersionNumber();
63   if (number <= 9) {
64     return characterCountBitsForVersions0To9_;
65   } else if (number <= 26) {
66     return characterCountBitsForVersions10To26_;
67   } else {
68     return characterCountBitsForVersions27AndHigher_;
69   }
70 }
71
72 }
73 }