2 * Copyright 2007 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.qrcode.decoder;
20 * <p>Encapsulates a QR Code's format information, including the data mask used and
21 * error correction level.</p>
23 * @author srowen@google.com (Sean Owen)
25 * @see ErrorCorrectionLevel
27 final class FormatInformation {
29 private static final int FORMAT_INFO_MASK_QR = 0x5412;
32 * See ISO 18004:2006, Annex C, Table C.1
34 private static final int[][] FORMAT_INFO_DECODE_LOOKUP = new int[][]{
69 /** Offset i holds the number of 1 bits in the binary representation of i */
70 private static final int[] BITS_SET_IN_HALF_BYTE =
71 new int[]{0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
73 private final ErrorCorrectionLevel errorCorrectionLevel;
74 private final byte dataMask;
76 private FormatInformation(int formatInfo) {
78 errorCorrectionLevel = ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03);
80 dataMask = (byte) (formatInfo & 0x07);
83 static int numBitsDiffering(int a, int b) {
84 a ^= b; // a now has a 1 bit exactly where its bit differs with b's
85 // Count bits set quickly with a series of lookups:
86 return BITS_SET_IN_HALF_BYTE[a & 0x0F] +
87 BITS_SET_IN_HALF_BYTE[(a >>> 4 & 0x0F)] +
88 BITS_SET_IN_HALF_BYTE[(a >>> 8 & 0x0F)] +
89 BITS_SET_IN_HALF_BYTE[(a >>> 12 & 0x0F)] +
90 BITS_SET_IN_HALF_BYTE[(a >>> 16 & 0x0F)] +
91 BITS_SET_IN_HALF_BYTE[(a >>> 20 & 0x0F)] +
92 BITS_SET_IN_HALF_BYTE[(a >>> 24 & 0x0F)] +
93 BITS_SET_IN_HALF_BYTE[(a >>> 28 & 0x0F)];
98 * @param rawFormatInfo
101 static FormatInformation decodeFormatInformation(int rawFormatInfo) {
102 FormatInformation formatInfo = doDecodeFormatInformation(rawFormatInfo);
103 if (formatInfo != null) {
106 // Should return null, but, some QR codes apparently
107 // do not mask this info. Try again, first masking the raw bits so
108 // the function will unmask
109 return doDecodeFormatInformation(rawFormatInfo ^ FORMAT_INFO_MASK_QR);
112 private static FormatInformation doDecodeFormatInformation(int rawFormatInfo) {
114 int unmaskedFormatInfo = rawFormatInfo ^ FORMAT_INFO_MASK_QR;
115 // Find the int in FORMAT_INFO_DECODE_LOOKUP with fewest bits differing
116 int bestDifference = Integer.MAX_VALUE;
117 int bestFormatInfo = 0;
118 for (int i = 0; i < FORMAT_INFO_DECODE_LOOKUP.length; i++) {
119 int[] decodeInfo = FORMAT_INFO_DECODE_LOOKUP[i];
120 int targetInfo = decodeInfo[0];
121 if (targetInfo == unmaskedFormatInfo) {
122 // Found an exact match
123 return new FormatInformation(decodeInfo[1]);
125 int bitsDifference = numBitsDiffering(unmaskedFormatInfo, targetInfo);
126 if (bitsDifference < bestDifference) {
127 bestFormatInfo = decodeInfo[1];
128 bestDifference = bitsDifference;
131 if (bestDifference <= 3) {
132 return new FormatInformation(bestFormatInfo);
137 ErrorCorrectionLevel getErrorCorrectionLevel() {
138 return errorCorrectionLevel;
145 public int hashCode() {
146 return (errorCorrectionLevel.ordinal() << 3) | (int) dataMask;
149 public boolean equals(Object o) {
150 if (!(o instanceof FormatInformation)) {
153 FormatInformation other = (FormatInformation) o;
154 return this.errorCorrectionLevel == other.errorCorrectionLevel &&
155 this.dataMask == other.dataMask;