bitcoin-atm
bitcoin atm for pyc inc.
git clone https://9o.is/git/bitcoin-atm.git
bmparser.js
(5780B)
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 function BitMatrixParser(bitMatrix)
27 {
28 var dimension = bitMatrix.Dimension;
29 if (dimension < 21 || (dimension & 0x03) != 1)
30 {
31 throw "Error BitMatrixParser";
32 }
33 this.bitMatrix = bitMatrix;
34 this.parsedVersion = null;
35 this.parsedFormatInfo = null;
36
37 this.copyBit=function( i, j, versionBits)
38 {
39 return this.bitMatrix.get_Renamed(i, j)?(versionBits << 1) | 0x1:versionBits << 1;
40 }
41
42 this.readFormatInformation=function()
43 {
44 if (this.parsedFormatInfo != null)
45 {
46 return this.parsedFormatInfo;
47 }
48
49 // Read top-left format info bits
50 var formatInfoBits = 0;
51 for (var i = 0; i < 6; i++)
52 {
53 formatInfoBits = this.copyBit(i, 8, formatInfoBits);
54 }
55 // .. and skip a bit in the timing pattern ...
56 formatInfoBits = this.copyBit(7, 8, formatInfoBits);
57 formatInfoBits = this.copyBit(8, 8, formatInfoBits);
58 formatInfoBits = this.copyBit(8, 7, formatInfoBits);
59 // .. and skip a bit in the timing pattern ...
60 for (var j = 5; j >= 0; j--)
61 {
62 formatInfoBits = this.copyBit(8, j, formatInfoBits);
63 }
64
65 this.parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits);
66 if (this.parsedFormatInfo != null)
67 {
68 return this.parsedFormatInfo;
69 }
70
71 // Hmm, failed. Try the top-right/bottom-left pattern
72 var dimension = this.bitMatrix.Dimension;
73 formatInfoBits = 0;
74 var iMin = dimension - 8;
75 for (var i = dimension - 1; i >= iMin; i--)
76 {
77 formatInfoBits = this.copyBit(i, 8, formatInfoBits);
78 }
79 for (var j = dimension - 7; j < dimension; j++)
80 {
81 formatInfoBits = this.copyBit(8, j, formatInfoBits);
82 }
83
84 this.parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits);
85 if (this.parsedFormatInfo != null)
86 {
87 return this.parsedFormatInfo;
88 }
89 throw "Error readFormatInformation";
90 }
91 this.readVersion=function()
92 {
93
94 if (this.parsedVersion != null)
95 {
96 return this.parsedVersion;
97 }
98
99 var dimension = this.bitMatrix.Dimension;
100
101 var provisionalVersion = (dimension - 17) >> 2;
102 if (provisionalVersion <= 6)
103 {
104 return Version.getVersionForNumber(provisionalVersion);
105 }
106
107 // Read top-right version info: 3 wide by 6 tall
108 var versionBits = 0;
109 var ijMin = dimension - 11;
110 for (var j = 5; j >= 0; j--)
111 {
112 for (var i = dimension - 9; i >= ijMin; i--)
113 {
114 versionBits = this.copyBit(i, j, versionBits);
115 }
116 }
117
118 this.parsedVersion = Version.decodeVersionInformation(versionBits);
119 if (this.parsedVersion != null && this.parsedVersion.DimensionForVersion == dimension)
120 {
121 return this.parsedVersion;
122 }
123
124 // Hmm, failed. Try bottom left: 6 wide by 3 tall
125 versionBits = 0;
126 for (var i = 5; i >= 0; i--)
127 {
128 for (var j = dimension - 9; j >= ijMin; j--)
129 {
130 versionBits = this.copyBit(i, j, versionBits);
131 }
132 }
133
134 this.parsedVersion = Version.decodeVersionInformation(versionBits);
135 if (this.parsedVersion != null && this.parsedVersion.DimensionForVersion == dimension)
136 {
137 return this.parsedVersion;
138 }
139 throw "Error readVersion";
140 }
141 this.readCodewords=function()
142 {
143
144 var formatInfo = this.readFormatInformation();
145 var version = this.readVersion();
146
147 // Get the data mask for the format used in this QR Code. This will exclude
148 // some bits from reading as we wind through the bit matrix.
149 var dataMask = DataMask.forReference( formatInfo.DataMask);
150 var dimension = this.bitMatrix.Dimension;
151 dataMask.unmaskBitMatrix(this.bitMatrix, dimension);
152
153 var functionPattern = version.buildFunctionPattern();
154
155 var readingUp = true;
156 var result = new Array(version.TotalCodewords);
157 var resultOffset = 0;
158 var currentByte = 0;
159 var bitsRead = 0;
160 // Read columns in pairs, from right to left
161 for (var j = dimension - 1; j > 0; j -= 2)
162 {
163 if (j == 6)
164 {
165 // Skip whole column with vertical alignment pattern;
166 // saves time and makes the other code proceed more cleanly
167 j--;
168 }
169 // Read alternatingly from bottom to top then top to bottom
170 for (var count = 0; count < dimension; count++)
171 {
172 var i = readingUp?dimension - 1 - count:count;
173 for (var col = 0; col < 2; col++)
174 {
175 // Ignore bits covered by the function pattern
176 if (!functionPattern.get_Renamed(j - col, i))
177 {
178 // Read a bit
179 bitsRead++;
180 currentByte <<= 1;
181 if (this.bitMatrix.get_Renamed(j - col, i))
182 {
183 currentByte |= 1;
184 }
185 // If we've made a whole byte, save it off
186 if (bitsRead == 8)
187 {
188 result[resultOffset++] = currentByte;
189 bitsRead = 0;
190 currentByte = 0;
191 }
192 }
193 }
194 }
195 readingUp ^= true; // readingUp = !readingUp; // switch directions
196 }
197 if (resultOffset != version.TotalCodewords)
198 {
199 throw "Error readCodewords";
200 }
201 return result;
202 }
203 }