e483ebd69f29729ac4f0328256b8a0dfcec3a2ec
[zxing.git] / core / src / com / google / zxing / qrcode / decoder / FormatInformation.java
1 /*
2  * Copyright 2007 Google Inc.
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  * @author srowen@google.com (Sean Owen)
21  */
22 final class FormatInformation {
23
24   private static final int FORMAT_INFO_MASK_QR = 0x5412;
25
26   /**
27    * See ISO 18004:2006, Annex C, Table C.1
28    */
29   private static final int[][] FORMAT_INFO_DECODE_LOOKUP = new int[][]{
30       {0x5412, 0x00},
31       {0x5125, 0x01},
32       {0x5E7C, 0x02},
33       {0x5B4B, 0x03},
34       {0x45F9, 0x04},
35       {0x40CE, 0x05},
36       {0x4F97, 0x06},
37       {0x4AA0, 0x07},
38       {0x77C4, 0x08},
39       {0x72F3, 0x09},
40       {0x7DAA, 0x0A},
41       {0x789D, 0x0B},
42       {0x662F, 0x0C},
43       {0x6318, 0x0D},
44       {0x6C41, 0x0E},
45       {0x6976, 0x0F},
46       {0x1689, 0x10},
47       {0x13BE, 0x11},
48       {0x1CE7, 0x12},
49       {0x19D0, 0x13},
50       {0x0762, 0x14},
51       {0x0255, 0x15},
52       {0x0D0C, 0x16},
53       {0x083B, 0x17},
54       {0x355F, 0x18},
55       {0x3068, 0x19},
56       {0x3F31, 0x1A},
57       {0x3A06, 0x1B},
58       {0x24B4, 0x1C},
59       {0x2183, 0x1D},
60       {0x2EDA, 0x1E},
61       {0x2BED, 0x1F},
62   };
63
64   private static final int[] BITS_SET_IN_HALF_BYTE =
65       new int[]{0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
66
67   private final ErrorCorrectionLevel errorCorrectionLevel;
68   private final byte dataMask;
69
70   private FormatInformation(int formatInfo) {
71     // Bits 3,4
72     errorCorrectionLevel =
73         ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03);
74     // Bottom 3 bits
75     dataMask = (byte) (formatInfo & 0x07);
76   }
77
78   static int numBitsDiffering(int a, int b) {
79     a ^= b;
80     return
81         BITS_SET_IN_HALF_BYTE[a & 0x0F] +
82             BITS_SET_IN_HALF_BYTE[(a >>> 4 & 0x0F)] +
83             BITS_SET_IN_HALF_BYTE[(a >>> 8 & 0x0F)] +
84             BITS_SET_IN_HALF_BYTE[(a >>> 12 & 0x0F)] +
85             BITS_SET_IN_HALF_BYTE[(a >>> 16 & 0x0F)] +
86             BITS_SET_IN_HALF_BYTE[(a >>> 20 & 0x0F)] +
87             BITS_SET_IN_HALF_BYTE[(a >>> 24 & 0x0F)] +
88             BITS_SET_IN_HALF_BYTE[(a >>> 28 & 0x0F)];
89   }
90
91   static FormatInformation decodeFormatInformation(int rawFormatInfo) {
92     FormatInformation formatInfo = doDecodeFormatInformation(rawFormatInfo);
93     if (formatInfo != null) {
94       return formatInfo;
95     }
96     // Should return null, but, some QR codes apparently
97     // do not mask this info. Try again, first masking the raw bits so
98     // the function will unmask
99     return doDecodeFormatInformation(rawFormatInfo ^ FORMAT_INFO_MASK_QR);
100   }
101
102   private static FormatInformation doDecodeFormatInformation(
103       int rawFormatInfo) {
104     // Unmask:
105     int unmaskedFormatInfo = rawFormatInfo ^ FORMAT_INFO_MASK_QR;
106     int bestDifference = Integer.MAX_VALUE;
107     int bestFormatInfo = 0;
108     for (int i = 0; i < FORMAT_INFO_DECODE_LOOKUP.length; i++) {
109       int[] decodeInfo = FORMAT_INFO_DECODE_LOOKUP[i];
110       int targetInfo = decodeInfo[0];
111       if (targetInfo == unmaskedFormatInfo) {
112         return new FormatInformation(decodeInfo[1]);
113       }
114       int bitsDifference = numBitsDiffering(unmaskedFormatInfo, targetInfo);
115       if (bitsDifference < bestDifference) {
116         bestFormatInfo = decodeInfo[1];
117         bestDifference = bitsDifference;
118       }
119     }
120     if (bestDifference <= 3) {
121       return new FormatInformation(bestFormatInfo);
122     }
123     return null;
124   }
125
126   ErrorCorrectionLevel getErrorCorrectionLevel() {
127     return errorCorrectionLevel;
128   }
129
130   byte getDataMask() {
131     return dataMask;
132   }
133
134   public int hashCode() {
135     return (errorCorrectionLevel.ordinal() << 3) | (int) dataMask;
136   }
137
138   public boolean equals(Object o) {
139     if (!(o instanceof FormatInformation)) {
140       return false;
141     }
142     FormatInformation other = (FormatInformation) o;
143     return this.errorCorrectionLevel == other.errorCorrectionLevel &&
144         this.dataMask == other.dataMask;
145   }
146
147 }