001/*
002 * Copyright 2007 ZXing authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.itextpdf.text.pdf.qrcode;
018
019/**
020 * <p>Encapsulates a QR Code's format information, including the data mask used and
021 * error correction level.</p>
022 *
023 * @author Sean Owen
024 * @see ErrorCorrectionLevel
025 * @since 5.0.2
026 */
027final class FormatInformation {
028
029  private static final int FORMAT_INFO_MASK_QR = 0x5412;
030
031  /**
032   * See ISO 18004:2006, Annex C, Table C.1
033   */
034  private static final int[][] FORMAT_INFO_DECODE_LOOKUP = {
035      {0x5412, 0x00},
036      {0x5125, 0x01},
037      {0x5E7C, 0x02},
038      {0x5B4B, 0x03},
039      {0x45F9, 0x04},
040      {0x40CE, 0x05},
041      {0x4F97, 0x06},
042      {0x4AA0, 0x07},
043      {0x77C4, 0x08},
044      {0x72F3, 0x09},
045      {0x7DAA, 0x0A},
046      {0x789D, 0x0B},
047      {0x662F, 0x0C},
048      {0x6318, 0x0D},
049      {0x6C41, 0x0E},
050      {0x6976, 0x0F},
051      {0x1689, 0x10},
052      {0x13BE, 0x11},
053      {0x1CE7, 0x12},
054      {0x19D0, 0x13},
055      {0x0762, 0x14},
056      {0x0255, 0x15},
057      {0x0D0C, 0x16},
058      {0x083B, 0x17},
059      {0x355F, 0x18},
060      {0x3068, 0x19},
061      {0x3F31, 0x1A},
062      {0x3A06, 0x1B},
063      {0x24B4, 0x1C},
064      {0x2183, 0x1D},
065      {0x2EDA, 0x1E},
066      {0x2BED, 0x1F},
067  };
068
069  /**
070   * Offset i holds the number of 1 bits in the binary representation of i
071   */
072  private static final int[] BITS_SET_IN_HALF_BYTE =
073      {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
074
075  private final ErrorCorrectionLevel errorCorrectionLevel;
076  private final byte dataMask;
077
078  private FormatInformation(int formatInfo) {
079    // Bits 3,4
080    errorCorrectionLevel = ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03);
081    // Bottom 3 bits
082    dataMask = (byte) (formatInfo & 0x07);
083  }
084
085  static int numBitsDiffering(int a, int b) {
086    a ^= b; // a now has a 1 bit exactly where its bit differs with b's
087    // Count bits set quickly with a series of lookups:
088    return BITS_SET_IN_HALF_BYTE[a & 0x0F] +
089        BITS_SET_IN_HALF_BYTE[(a >>> 4 & 0x0F)] +
090        BITS_SET_IN_HALF_BYTE[(a >>> 8 & 0x0F)] +
091        BITS_SET_IN_HALF_BYTE[(a >>> 12 & 0x0F)] +
092        BITS_SET_IN_HALF_BYTE[(a >>> 16 & 0x0F)] +
093        BITS_SET_IN_HALF_BYTE[(a >>> 20 & 0x0F)] +
094        BITS_SET_IN_HALF_BYTE[(a >>> 24 & 0x0F)] +
095        BITS_SET_IN_HALF_BYTE[(a >>> 28 & 0x0F)];
096  }
097
098  /**
099   * @param maskedFormatInfo1 format info indicator, with mask still applied
100   * @param maskedFormatInfo2 second copy of same info; both are checked at the same time
101   *  to establish best match
102   * @return information about the format it specifies, or <code>null</code>
103   *  if doesn't seem to match any known pattern
104   */
105  static FormatInformation decodeFormatInformation(int maskedFormatInfo1, int maskedFormatInfo2) {
106    FormatInformation formatInfo = doDecodeFormatInformation(maskedFormatInfo1, maskedFormatInfo2);
107    if (formatInfo != null) {
108      return formatInfo;
109    }
110    // Should return null, but, some QR codes apparently
111    // do not mask this info. Try again by actually masking the pattern
112    // first
113    return doDecodeFormatInformation(maskedFormatInfo1 ^ FORMAT_INFO_MASK_QR,
114                                     maskedFormatInfo2 ^ FORMAT_INFO_MASK_QR);
115  }
116
117  private static FormatInformation doDecodeFormatInformation(int maskedFormatInfo1, int maskedFormatInfo2) {
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 == maskedFormatInfo1 || targetInfo == maskedFormatInfo2) {
125        // Found an exact match
126        return new FormatInformation(decodeInfo[1]);
127      }
128      int bitsDifference = numBitsDiffering(maskedFormatInfo1, targetInfo);
129      if (bitsDifference < bestDifference) {
130        bestFormatInfo = decodeInfo[1];
131        bestDifference = bitsDifference;
132      }
133      if (maskedFormatInfo1 != maskedFormatInfo2) {
134        // also try the other option
135        bitsDifference = numBitsDiffering(maskedFormatInfo2, targetInfo);
136        if (bitsDifference < bestDifference) {
137          bestFormatInfo = decodeInfo[1];
138          bestDifference = bitsDifference;
139        }
140      }
141    }
142    // Hamming distance of the 32 masked codes is 7, by construction, so <= 3 bits
143    // differing means we found a match
144    if (bestDifference <= 3) {
145      return new FormatInformation(bestFormatInfo);
146    }
147    return null;
148  }
149
150  ErrorCorrectionLevel getErrorCorrectionLevel() {
151    return errorCorrectionLevel;
152  }
153
154  byte getDataMask() {
155    return dataMask;
156  }
157
158  public int hashCode() {
159    return (errorCorrectionLevel.ordinal() << 3) | (int) dataMask;
160  }
161
162  public boolean equals(Object o) {
163    if (!(o instanceof FormatInformation)) {
164      return false;
165    }
166    FormatInformation other = (FormatInformation) o;
167    return this.errorCorrectionLevel == other.errorCorrectionLevel &&
168        this.dataMask == other.dataMask;
169  }
170
171}