Improvement to Shift_JIS encoding detector to avoid detecting some UTF-8 strings...
[zxing.git] / core / src / com / google / zxing / qrcode / decoder / FormatInformation.java
1 /*
2  * Copyright 2007 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.qrcode.decoder;
18
19 /**
20  * <p>Encapsulates a QR Code's format information, including the data mask used and
21  * error correction level.</p>
22  *
23  * @author Sean Owen
24  * @see DataMask
25  * @see ErrorCorrectionLevel
26  */
27 final class FormatInformation {
28
29   private static final int FORMAT_INFO_MASK_QR = 0x5412;
30
31   /**
32    * See ISO 18004:2006, Annex C, Table C.1
33    */
34   private static final int[][] FORMAT_INFO_DECODE_LOOKUP = {
35       {0x5412, 0x00},
36       {0x5125, 0x01},
37       {0x5E7C, 0x02},
38       {0x5B4B, 0x03},
39       {0x45F9, 0x04},
40       {0x40CE, 0x05},
41       {0x4F97, 0x06},
42       {0x4AA0, 0x07},
43       {0x77C4, 0x08},
44       {0x72F3, 0x09},
45       {0x7DAA, 0x0A},
46       {0x789D, 0x0B},
47       {0x662F, 0x0C},
48       {0x6318, 0x0D},
49       {0x6C41, 0x0E},
50       {0x6976, 0x0F},
51       {0x1689, 0x10},
52       {0x13BE, 0x11},
53       {0x1CE7, 0x12},
54       {0x19D0, 0x13},
55       {0x0762, 0x14},
56       {0x0255, 0x15},
57       {0x0D0C, 0x16},
58       {0x083B, 0x17},
59       {0x355F, 0x18},
60       {0x3068, 0x19},
61       {0x3F31, 0x1A},
62       {0x3A06, 0x1B},
63       {0x24B4, 0x1C},
64       {0x2183, 0x1D},
65       {0x2EDA, 0x1E},
66       {0x2BED, 0x1F},
67   };
68
69   /**
70    * Offset i holds the number of 1 bits in the binary representation of i
71    */
72   private static final int[] BITS_SET_IN_HALF_BYTE =
73       {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
74
75   private final ErrorCorrectionLevel errorCorrectionLevel;
76   private final byte dataMask;
77
78   private FormatInformation(int formatInfo) {
79     // Bits 3,4
80     errorCorrectionLevel = ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03);
81     // Bottom 3 bits
82     dataMask = (byte) (formatInfo & 0x07);
83   }
84
85   static int numBitsDiffering(int a, int b) {
86     a ^= b; // a now has a 1 bit exactly where its bit differs with b's
87     // Count bits set quickly with a series of lookups:
88     return BITS_SET_IN_HALF_BYTE[a & 0x0F] +
89         BITS_SET_IN_HALF_BYTE[(a >>> 4 & 0x0F)] +
90         BITS_SET_IN_HALF_BYTE[(a >>> 8 & 0x0F)] +
91         BITS_SET_IN_HALF_BYTE[(a >>> 12 & 0x0F)] +
92         BITS_SET_IN_HALF_BYTE[(a >>> 16 & 0x0F)] +
93         BITS_SET_IN_HALF_BYTE[(a >>> 20 & 0x0F)] +
94         BITS_SET_IN_HALF_BYTE[(a >>> 24 & 0x0F)] +
95         BITS_SET_IN_HALF_BYTE[(a >>> 28 & 0x0F)];
96   }
97
98   /**
99    * @param rawFormatInfo
100    * @return
101    */
102   static FormatInformation decodeFormatInformation(int rawFormatInfo) {
103     FormatInformation formatInfo = doDecodeFormatInformation(rawFormatInfo);
104     if (formatInfo != null) {
105       return formatInfo;
106     }
107     // Should return null, but, some QR codes apparently
108     // do not mask this info. Try again, first masking the raw bits so
109     // the function will unmask
110     return doDecodeFormatInformation(rawFormatInfo ^ FORMAT_INFO_MASK_QR);
111   }
112
113   private static FormatInformation doDecodeFormatInformation(int rawFormatInfo) {
114     // Unmask:
115     int unmaskedFormatInfo = rawFormatInfo ^ FORMAT_INFO_MASK_QR;
116     // Find the int in FORMAT_INFO_DECODE_LOOKUP with fewest bits differing
117     int bestDifference = Integer.MAX_VALUE;
118     int bestFormatInfo = 0;
119     for (int i = 0; i < FORMAT_INFO_DECODE_LOOKUP.length; i++) {
120       int[] decodeInfo = FORMAT_INFO_DECODE_LOOKUP[i];
121       int targetInfo = decodeInfo[0];
122       if (targetInfo == unmaskedFormatInfo) {
123         // Found an exact match
124         return new FormatInformation(decodeInfo[1]);
125       }
126       int bitsDifference = numBitsDiffering(unmaskedFormatInfo, targetInfo);
127       if (bitsDifference < bestDifference) {
128         bestFormatInfo = decodeInfo[1];
129         bestDifference = bitsDifference;
130       }
131     }
132     if (bestDifference <= 3) {
133       return new FormatInformation(bestFormatInfo);
134     }
135     return null;
136   }
137
138   ErrorCorrectionLevel getErrorCorrectionLevel() {
139     return errorCorrectionLevel;
140   }
141
142   byte getDataMask() {
143     return dataMask;
144   }
145
146   public int hashCode() {
147     return (errorCorrectionLevel.ordinal() << 3) | (int) dataMask;
148   }
149
150   public boolean equals(Object o) {
151     if (!(o instanceof FormatInformation)) {
152       return false;
153     }
154     FormatInformation other = (FormatInformation) o;
155     return this.errorCorrectionLevel == other.errorCorrectionLevel &&
156         this.dataMask == other.dataMask;
157   }
158
159 }