More small changes from code inspection
[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 import com.google.zxing.ReaderException;
20
21 /**
22  * <p>Encapsulates a QR Code's format information, including the data mask used and
23  * error correction level.</p>
24  *
25  * @author Sean Owen
26  * @see DataMask
27  * @see ErrorCorrectionLevel
28  */
29 final class FormatInformation {
30
31   private static final int FORMAT_INFO_MASK_QR = 0x5412;
32
33   /**
34    * See ISO 18004:2006, Annex C, Table C.1
35    */
36   private static final int[][] FORMAT_INFO_DECODE_LOOKUP = {
37       {0x5412, 0x00},
38       {0x5125, 0x01},
39       {0x5E7C, 0x02},
40       {0x5B4B, 0x03},
41       {0x45F9, 0x04},
42       {0x40CE, 0x05},
43       {0x4F97, 0x06},
44       {0x4AA0, 0x07},
45       {0x77C4, 0x08},
46       {0x72F3, 0x09},
47       {0x7DAA, 0x0A},
48       {0x789D, 0x0B},
49       {0x662F, 0x0C},
50       {0x6318, 0x0D},
51       {0x6C41, 0x0E},
52       {0x6976, 0x0F},
53       {0x1689, 0x10},
54       {0x13BE, 0x11},
55       {0x1CE7, 0x12},
56       {0x19D0, 0x13},
57       {0x0762, 0x14},
58       {0x0255, 0x15},
59       {0x0D0C, 0x16},
60       {0x083B, 0x17},
61       {0x355F, 0x18},
62       {0x3068, 0x19},
63       {0x3F31, 0x1A},
64       {0x3A06, 0x1B},
65       {0x24B4, 0x1C},
66       {0x2183, 0x1D},
67       {0x2EDA, 0x1E},
68       {0x2BED, 0x1F},
69   };
70
71   /**
72    * Offset i holds the number of 1 bits in the binary representation of i
73    */
74   private static final int[] BITS_SET_IN_HALF_BYTE =
75       {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
76
77   private final ErrorCorrectionLevel errorCorrectionLevel;
78   private final byte dataMask;
79
80   private FormatInformation(int formatInfo) {
81     // Bits 3,4
82     errorCorrectionLevel = ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03);
83     // Bottom 3 bits
84     dataMask = (byte) (formatInfo & 0x07);
85   }
86
87   static int numBitsDiffering(int a, int b) {
88     a ^= b; // a now has a 1 bit exactly where its bit differs with b's
89     // Count bits set quickly with a series of lookups:
90     return BITS_SET_IN_HALF_BYTE[a & 0x0F] +
91         BITS_SET_IN_HALF_BYTE[(a >>> 4 & 0x0F)] +
92         BITS_SET_IN_HALF_BYTE[(a >>> 8 & 0x0F)] +
93         BITS_SET_IN_HALF_BYTE[(a >>> 12 & 0x0F)] +
94         BITS_SET_IN_HALF_BYTE[(a >>> 16 & 0x0F)] +
95         BITS_SET_IN_HALF_BYTE[(a >>> 20 & 0x0F)] +
96         BITS_SET_IN_HALF_BYTE[(a >>> 24 & 0x0F)] +
97         BITS_SET_IN_HALF_BYTE[(a >>> 28 & 0x0F)];
98   }
99
100   /**
101    * @param rawFormatInfo
102    * @return
103    */
104   static FormatInformation decodeFormatInformation(int rawFormatInfo) throws ReaderException {
105     FormatInformation formatInfo = doDecodeFormatInformation(rawFormatInfo);
106     if (formatInfo != null) {
107       return formatInfo;
108     }
109     // Should return null, but, some QR codes apparently
110     // do not mask this info. Try again, first masking the raw bits so
111     // the function will unmask
112     return doDecodeFormatInformation(rawFormatInfo ^ FORMAT_INFO_MASK_QR);
113   }
114
115   private static FormatInformation doDecodeFormatInformation(int rawFormatInfo) throws ReaderException {
116     // Unmask:
117     int unmaskedFormatInfo = rawFormatInfo ^ FORMAT_INFO_MASK_QR;
118     // Find the int in FORMAT_INFO_DECODE_LOOKUP with fewest bits differing
119     int bestDifference = Integer.MAX_VALUE;
120     int bestFormatInfo = 0;
121     for (int i = 0; i < FORMAT_INFO_DECODE_LOOKUP.length; i++) {
122       int[] decodeInfo = FORMAT_INFO_DECODE_LOOKUP[i];
123       int targetInfo = decodeInfo[0];
124       if (targetInfo == unmaskedFormatInfo) {
125         // Found an exact match
126         return new FormatInformation(decodeInfo[1]);
127       }
128       int bitsDifference = numBitsDiffering(unmaskedFormatInfo, targetInfo);
129       if (bitsDifference < bestDifference) {
130         bestFormatInfo = decodeInfo[1];
131         bestDifference = bitsDifference;
132       }
133     }
134     if (bestDifference <= 3) {
135       return new FormatInformation(bestFormatInfo);
136     }
137     return null;
138   }
139
140   ErrorCorrectionLevel getErrorCorrectionLevel() {
141     return errorCorrectionLevel;
142   }
143
144   byte getDataMask() {
145     return dataMask;
146   }
147
148   public int hashCode() {
149     return (errorCorrectionLevel.ordinal() << 3) | (int) dataMask;
150   }
151
152   public boolean equals(Object o) {
153     if (!(o instanceof FormatInformation)) {
154       return false;
155     }
156     FormatInformation other = (FormatInformation) o;
157     return this.errorCorrectionLevel == other.errorCorrectionLevel &&
158         this.dataMask == other.dataMask;
159   }
160
161 }