/*
- * Copyright 2007 Google Inc.
+ * Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package com.google.zxing.qrcode.decoder;
+import com.google.zxing.ReaderException;
+
/**
- * @author srowen@google.com (Sean Owen)
+ * <p>Encapsulates a QR Code's format information, including the data mask used and
+ * error correction level.</p>
+ *
+ * @author Sean Owen
+ * @see DataMask
+ * @see ErrorCorrectionLevel
*/
final class FormatInformation {
/**
* See ISO 18004:2006, Annex C, Table C.1
*/
- private static final int[][] FORMAT_INFO_DECODE_LOOKUP = new int[][]{
+ private static final int[][] FORMAT_INFO_DECODE_LOOKUP = {
{0x5412, 0x00},
{0x5125, 0x01},
{0x5E7C, 0x02},
{0x2BED, 0x1F},
};
+ /**
+ * Offset i holds the number of 1 bits in the binary representation of i
+ */
private static final int[] BITS_SET_IN_HALF_BYTE =
- new int[]{0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
+ {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
private final ErrorCorrectionLevel errorCorrectionLevel;
private final byte dataMask;
private FormatInformation(int formatInfo) {
// Bits 3,4
- errorCorrectionLevel =
- ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03);
+ errorCorrectionLevel = ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03);
// Bottom 3 bits
dataMask = (byte) (formatInfo & 0x07);
}
static int numBitsDiffering(int a, int b) {
- a ^= b;
- return
- BITS_SET_IN_HALF_BYTE[a & 0x0F] +
- BITS_SET_IN_HALF_BYTE[(a >>> 4 & 0x0F)] +
- BITS_SET_IN_HALF_BYTE[(a >>> 8 & 0x0F)] +
- BITS_SET_IN_HALF_BYTE[(a >>> 12 & 0x0F)] +
- BITS_SET_IN_HALF_BYTE[(a >>> 16 & 0x0F)] +
- BITS_SET_IN_HALF_BYTE[(a >>> 20 & 0x0F)] +
- BITS_SET_IN_HALF_BYTE[(a >>> 24 & 0x0F)] +
- BITS_SET_IN_HALF_BYTE[(a >>> 28 & 0x0F)];
+ a ^= b; // a now has a 1 bit exactly where its bit differs with b's
+ // Count bits set quickly with a series of lookups:
+ return BITS_SET_IN_HALF_BYTE[a & 0x0F] +
+ BITS_SET_IN_HALF_BYTE[(a >>> 4 & 0x0F)] +
+ BITS_SET_IN_HALF_BYTE[(a >>> 8 & 0x0F)] +
+ BITS_SET_IN_HALF_BYTE[(a >>> 12 & 0x0F)] +
+ BITS_SET_IN_HALF_BYTE[(a >>> 16 & 0x0F)] +
+ BITS_SET_IN_HALF_BYTE[(a >>> 20 & 0x0F)] +
+ BITS_SET_IN_HALF_BYTE[(a >>> 24 & 0x0F)] +
+ BITS_SET_IN_HALF_BYTE[(a >>> 28 & 0x0F)];
}
- static FormatInformation decodeFormatInformation(int rawFormatInfo) {
+ /**
+ * @param rawFormatInfo
+ * @return
+ */
+ static FormatInformation decodeFormatInformation(int rawFormatInfo) throws ReaderException {
FormatInformation formatInfo = doDecodeFormatInformation(rawFormatInfo);
if (formatInfo != null) {
return formatInfo;
return doDecodeFormatInformation(rawFormatInfo ^ FORMAT_INFO_MASK_QR);
}
- private static FormatInformation doDecodeFormatInformation(
- int rawFormatInfo) {
+ private static FormatInformation doDecodeFormatInformation(int rawFormatInfo) throws ReaderException {
// Unmask:
int unmaskedFormatInfo = rawFormatInfo ^ FORMAT_INFO_MASK_QR;
+ // Find the int in FORMAT_INFO_DECODE_LOOKUP with fewest bits differing
int bestDifference = Integer.MAX_VALUE;
int bestFormatInfo = 0;
for (int i = 0; i < FORMAT_INFO_DECODE_LOOKUP.length; i++) {
int[] decodeInfo = FORMAT_INFO_DECODE_LOOKUP[i];
int targetInfo = decodeInfo[0];
if (targetInfo == unmaskedFormatInfo) {
+ // Found an exact match
return new FormatInformation(decodeInfo[1]);
}
int bitsDifference = numBitsDiffering(unmaskedFormatInfo, targetInfo);