Issue 165
[zxing.git] / cpp / core / src / qrcode / decoder / FormatInformation.cpp
1 /*
2  *  FormatInformation.cpp
3  *  zxing
4  *
5  *  Created by Christian Brunschen on 18/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 "FormatInformation.h"
22 #include <limits>
23
24 namespace qrcode {
25   namespace decoder {
26     int FormatInformation::FORMAT_INFO_MASK_QR = 0x5412;
27     int FormatInformation::FORMAT_INFO_DECODE_LOOKUP[][2] = {
28     {0x5412, 0x00},
29     {0x5125, 0x01},
30     {0x5E7C, 0x02},
31     {0x5B4B, 0x03},
32     {0x45F9, 0x04},
33     {0x40CE, 0x05},
34     {0x4F97, 0x06},
35     {0x4AA0, 0x07},
36     {0x77C4, 0x08},
37     {0x72F3, 0x09},
38     {0x7DAA, 0x0A},
39     {0x789D, 0x0B},
40     {0x662F, 0x0C},
41     {0x6318, 0x0D},
42     {0x6C41, 0x0E},
43     {0x6976, 0x0F},
44     {0x1689, 0x10},
45     {0x13BE, 0x11},
46     {0x1CE7, 0x12},
47     {0x19D0, 0x13},
48     {0x0762, 0x14},
49     {0x0255, 0x15},
50     {0x0D0C, 0x16},
51     {0x083B, 0x17},
52     {0x355F, 0x18},
53     {0x3068, 0x19},
54     {0x3F31, 0x1A},
55     {0x3A06, 0x1B},
56     {0x24B4, 0x1C},
57     {0x2183, 0x1D},
58     {0x2EDA, 0x1E},
59     {0x2BED, 0x1F},
60     };
61     int FormatInformation::N_FORMAT_INFO_DECODE_LOOKUPS = 32;
62
63     int FormatInformation::BITS_SET_IN_HALF_BYTE[] = { 
64       0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
65     };
66     
67     int FormatInformation::numBitsDiffering(unsigned int a, unsigned int b) {
68       a ^= b;
69       return BITS_SET_IN_HALF_BYTE[a & 0x0F] +
70       BITS_SET_IN_HALF_BYTE[(a >> 4 & 0x0F)] +
71       BITS_SET_IN_HALF_BYTE[(a >> 8 & 0x0F)] +
72       BITS_SET_IN_HALF_BYTE[(a >> 12 & 0x0F)] +
73       BITS_SET_IN_HALF_BYTE[(a >> 16 & 0x0F)] +
74       BITS_SET_IN_HALF_BYTE[(a >> 20 & 0x0F)] +
75       BITS_SET_IN_HALF_BYTE[(a >> 24 & 0x0F)] +
76       BITS_SET_IN_HALF_BYTE[(a >> 28 & 0x0F)];
77     }
78
79     Ref<FormatInformation> 
80     FormatInformation::decodeFormatInformation(int rawFormatInfo) {
81       Ref<FormatInformation> result(doDecodeFormatInformation(rawFormatInfo));
82       if (result != 0) {
83         return result;
84       }
85       return doDecodeFormatInformation(rawFormatInfo ^ FORMAT_INFO_MASK_QR);
86     }
87     Ref<FormatInformation> 
88     FormatInformation::doDecodeFormatInformation(int rawFormatInfo) {
89       // Unmask:
90       int unmaskedFormatInfo = rawFormatInfo ^ FORMAT_INFO_MASK_QR;
91       // Find the int in FORMAT_INFO_DECODE_LOOKUP with fewest bits differing
92       int bestDifference = numeric_limits<int>::max();
93       int bestFormatInfo = 0;
94       for (int i = 0; i < N_FORMAT_INFO_DECODE_LOOKUPS; i++) {
95         int* decodeInfo = FORMAT_INFO_DECODE_LOOKUP[i];
96         int targetInfo = decodeInfo[0];
97         if (targetInfo == unmaskedFormatInfo) {
98           // Found an exact match
99           Ref<FormatInformation> result(new FormatInformation(decodeInfo[1]));
100           return result;
101         }
102         int bitsDifference = numBitsDiffering(unmaskedFormatInfo, targetInfo);
103         if (bitsDifference < bestDifference) {
104           bestFormatInfo = decodeInfo[1];
105           bestDifference = bitsDifference;
106         }
107       }
108       if (bestDifference <= 3) {
109         Ref<FormatInformation> result(new FormatInformation(bestFormatInfo));
110         return result;
111       }
112       Ref<FormatInformation> result;
113       return result;
114     }
115     
116     bool operator==(const FormatInformation &a,
117                     const FormatInformation &b) {
118       return &(a.errorCorrectionLevel_) == &(b.errorCorrectionLevel_) &&
119       a.dataMask_ == b.dataMask_;
120     }
121     
122     ostream& operator<<(ostream& out, const FormatInformation& fi) {
123       const FormatInformation *fip = &fi;
124       out << "FormatInformation @ " << (unsigned int)fip;
125       return out;
126     }
127   }
128 }