bitcoin-atm
bitcoin atm for pyc inc.
git clone https://9o.is/git/bitcoin-atm.git
formatinf.js
(4237B)
1 /*
2 Ported to JavaScript by Lazar Laszlo 2011
3
4 lazarsoft@gmail.com, www.lazarsoft.info
5
6 */
7
8 /*
9 *
10 * Copyright 2007 ZXing authors
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 */
24
25
26 var FORMAT_INFO_MASK_QR = 0x5412;
27 var FORMAT_INFO_DECODE_LOOKUP = new Array(new Array(0x5412, 0x00), new Array(0x5125, 0x01), new Array(0x5E7C, 0x02), new Array(0x5B4B, 0x03), new Array(0x45F9, 0x04), new Array(0x40CE, 0x05), new Array(0x4F97, 0x06), new Array(0x4AA0, 0x07), new Array(0x77C4, 0x08), new Array(0x72F3, 0x09), new Array(0x7DAA, 0x0A), new Array(0x789D, 0x0B), new Array(0x662F, 0x0C), new Array(0x6318, 0x0D), new Array(0x6C41, 0x0E), new Array(0x6976, 0x0F), new Array(0x1689, 0x10), new Array(0x13BE, 0x11), new Array(0x1CE7, 0x12), new Array(0x19D0, 0x13), new Array(0x0762, 0x14), new Array(0x0255, 0x15), new Array(0x0D0C, 0x16), new Array(0x083B, 0x17), new Array(0x355F, 0x18), new Array(0x3068, 0x19), new Array(0x3F31, 0x1A), new Array(0x3A06, 0x1B), new Array(0x24B4, 0x1C), new Array(0x2183, 0x1D), new Array(0x2EDA, 0x1E), new Array(0x2BED, 0x1F));
28 var BITS_SET_IN_HALF_BYTE = new Array(0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4);
29
30
31 function FormatInformation(formatInfo)
32 {
33 this.errorCorrectionLevel = ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03);
34 this.dataMask = (formatInfo & 0x07);
35
36 this.__defineGetter__("ErrorCorrectionLevel", function()
37 {
38 return this.errorCorrectionLevel;
39 });
40 this.__defineGetter__("DataMask", function()
41 {
42 return this.dataMask;
43 });
44 this.GetHashCode=function()
45 {
46 return (this.errorCorrectionLevel.ordinal() << 3) | dataMask;
47 }
48 this.Equals=function( o)
49 {
50 var other = o;
51 return this.errorCorrectionLevel == other.errorCorrectionLevel && this.dataMask == other.dataMask;
52 }
53 }
54
55 FormatInformation.numBitsDiffering=function( a, b)
56 {
57 a ^= b; // a now has a 1 bit exactly where its bit differs with b's
58 // Count bits set quickly with a series of lookups:
59 return BITS_SET_IN_HALF_BYTE[a & 0x0F] + BITS_SET_IN_HALF_BYTE[(URShift(a, 4) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 8) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 12) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 16) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 20) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 24) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 28) & 0x0F)];
60 }
61
62 FormatInformation.decodeFormatInformation=function( maskedFormatInfo)
63 {
64 var formatInfo = FormatInformation.doDecodeFormatInformation(maskedFormatInfo);
65 if (formatInfo != null)
66 {
67 return formatInfo;
68 }
69 // Should return null, but, some QR codes apparently
70 // do not mask this info. Try again by actually masking the pattern
71 // first
72 return FormatInformation.doDecodeFormatInformation(maskedFormatInfo ^ FORMAT_INFO_MASK_QR);
73 }
74 FormatInformation.doDecodeFormatInformation=function( maskedFormatInfo)
75 {
76 // Find the int in FORMAT_INFO_DECODE_LOOKUP with fewest bits differing
77 var bestDifference = 0xffffffff;
78 var bestFormatInfo = 0;
79 for (var i = 0; i < FORMAT_INFO_DECODE_LOOKUP.length; i++)
80 {
81 var decodeInfo = FORMAT_INFO_DECODE_LOOKUP[i];
82 var targetInfo = decodeInfo[0];
83 if (targetInfo == maskedFormatInfo)
84 {
85 // Found an exact match
86 return new FormatInformation(decodeInfo[1]);
87 }
88 var bitsDifference = this.numBitsDiffering(maskedFormatInfo, targetInfo);
89 if (bitsDifference < bestDifference)
90 {
91 bestFormatInfo = decodeInfo[1];
92 bestDifference = bitsDifference;
93 }
94 }
95 // Hamming distance of the 32 masked codes is 7, by construction, so <= 3 bits
96 // differing means we found a match
97 if (bestDifference <= 3)
98 {
99 return new FormatInformation(bestFormatInfo);
100 }
101 return null;
102 }
103
104