QRCode_nuintun

Origin:https://github.com/nuintun/qrcode

このスクリプトは単体で利用できません。右のようなメタデータを含むスクリプトから、ライブラリとして読み込まれます: // @require https://update.greatest.deepsurf.us/scripts/464943/1182060/QRCode_nuintun.js

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください。
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define('qrcode', ['exports'], factory) :
  4. (global = global || self, factory(global.QRCode = {}));
  5. }(this, function (exports) { 'use strict';
  6.  
  7. /**
  8. * @module Mode
  9. * @author nuintun
  10. * @author Cosmo Wolfe
  11. * @author Kazuhiko Arase
  12. */
  13.  
  14. (function (Mode) {
  15. Mode[Mode["Terminator"] = 0] = "Terminator";
  16. Mode[Mode["Numeric"] = 1] = "Numeric";
  17. Mode[Mode["Alphanumeric"] = 2] = "Alphanumeric";
  18. Mode[Mode["StructuredAppend"] = 3] = "StructuredAppend";
  19. Mode[Mode["Byte"] = 4] = "Byte";
  20. Mode[Mode["Kanji"] = 8] = "Kanji";
  21. Mode[Mode["ECI"] = 7] = "ECI";
  22. // FNC1FirstPosition = 0x5,
  23. // FNC1SecondPosition = 0x9
  24. })(exports.Mode || (exports.Mode = {}));
  25.  
  26. /*! *****************************************************************************
  27. Copyright (c) Microsoft Corporation. All rights reserved.
  28. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  29. this file except in compliance with the License. You may obtain a copy of the
  30. License at http://www.apache.org/licenses/LICENSE-2.0
  31.  
  32. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  33. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  34. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  35. MERCHANTABLITY OR NON-INFRINGEMENT.
  36.  
  37. See the Apache Version 2.0 License for specific language governing permissions
  38. and limitations under the License.
  39. ***************************************************************************** */
  40. /* global Reflect, Promise */
  41.  
  42. var extendStatics = function(d, b) {
  43. extendStatics = Object.setPrototypeOf ||
  44. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  45. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  46. return extendStatics(d, b);
  47. };
  48.  
  49. function __extends(d, b) {
  50. extendStatics(d, b);
  51. function __() { this.constructor = d; }
  52. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  53. }
  54.  
  55. var __assign = function() {
  56. __assign = Object.assign || function __assign(t) {
  57. for (var s, i = 1, n = arguments.length; i < n; i++) {
  58. s = arguments[i];
  59. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  60. }
  61. return t;
  62. };
  63. return __assign.apply(this, arguments);
  64. };
  65.  
  66. /**
  67. * @module QRData
  68. * @author nuintun
  69. * @author Kazuhiko Arase
  70. */
  71. var QRData = /*#__PURE__*/ (function () {
  72. function QRData(mode, data) {
  73. this.mode = mode;
  74. this.data = data;
  75. }
  76. QRData.prototype.getMode = function () {
  77. return this.mode;
  78. };
  79. QRData.prototype.getLengthInBits = function (version) {
  80. var mode = this.mode;
  81. var error = "illegal mode: " + mode;
  82. if (1 <= version && version < 10) {
  83. // 1 - 9
  84. switch (mode) {
  85. case exports.Mode.Numeric:
  86. return 10;
  87. case exports.Mode.Alphanumeric:
  88. return 9;
  89. case exports.Mode.Byte:
  90. return 8;
  91. case exports.Mode.Kanji:
  92. return 8;
  93. default:
  94. throw error;
  95. }
  96. }
  97. else if (version < 27) {
  98. // 10 - 26
  99. switch (mode) {
  100. case exports.Mode.Numeric:
  101. return 12;
  102. case exports.Mode.Alphanumeric:
  103. return 11;
  104. case exports.Mode.Byte:
  105. return 16;
  106. case exports.Mode.Kanji:
  107. return 10;
  108. default:
  109. throw error;
  110. }
  111. }
  112. else if (version < 41) {
  113. // 27 - 40
  114. switch (mode) {
  115. case exports.Mode.Numeric:
  116. return 14;
  117. case exports.Mode.Alphanumeric:
  118. return 13;
  119. case exports.Mode.Byte:
  120. return 16;
  121. case exports.Mode.Kanji:
  122. return 12;
  123. default:
  124. throw error;
  125. }
  126. }
  127. else {
  128. throw "illegal version: " + version;
  129. }
  130. };
  131. return QRData;
  132. }());
  133.  
  134. /**
  135. * @module UTF8
  136. * @author nuintun
  137. */
  138. /**
  139. * @function UTF8
  140. * @param {string} str
  141. * @returns {number[]}
  142. * @see https://github.com/google/closure-library/blob/master/closure/goog/crypt/crypt.js
  143. */
  144. function UTF8(str) {
  145. var pos = 0;
  146. var bytes = [];
  147. var length = str.length;
  148. for (var i = 0; i < length; i++) {
  149. var code = str.charCodeAt(i);
  150. if (code < 128) {
  151. bytes[pos++] = code;
  152. }
  153. else if (code < 2048) {
  154. bytes[pos++] = (code >> 6) | 192;
  155. bytes[pos++] = (code & 63) | 128;
  156. }
  157. else if ((code & 0xfc00) === 0xd800 && i + 1 < length && (str.charCodeAt(i + 1) & 0xfc00) === 0xdc00) {
  158. // Surrogate Pair
  159. code = 0x10000 + ((code & 0x03ff) << 10) + (str.charCodeAt(++i) & 0x03ff);
  160. bytes[pos++] = (code >> 18) | 240;
  161. bytes[pos++] = ((code >> 12) & 63) | 128;
  162. bytes[pos++] = ((code >> 6) & 63) | 128;
  163. bytes[pos++] = (code & 63) | 128;
  164. }
  165. else {
  166. bytes[pos++] = (code >> 12) | 224;
  167. bytes[pos++] = ((code >> 6) & 63) | 128;
  168. bytes[pos++] = (code & 63) | 128;
  169. }
  170. }
  171. return bytes;
  172. }
  173.  
  174. /**
  175. * @module QR8BitByte
  176. * @author nuintun
  177. * @author Kazuhiko Arase
  178. */
  179. var QRByte = /*#__PURE__*/ (function (_super) {
  180. __extends(QRByte, _super);
  181. /**
  182. * @constructor
  183. * @param {string} data
  184. */
  185. function QRByte(data, encode) {
  186. var _this = _super.call(this, exports.Mode.Byte, data) || this;
  187. _this.encoding = -1;
  188. if (typeof encode === 'function') {
  189. var _a = encode(data), encoding = _a.encoding, bytes = _a.bytes;
  190. _this.bytes = bytes;
  191. _this.encoding = encoding;
  192. }
  193. else {
  194. _this.bytes = UTF8(data);
  195. _this.encoding = 26 /* UTF8 */;
  196. }
  197. return _this;
  198. }
  199. /**
  200. * @public
  201. * @method write
  202. * @param {BitBuffer} buffer
  203. */
  204. QRByte.prototype.write = function (buffer) {
  205. var bytes = this.bytes;
  206. var length = bytes.length;
  207. for (var i = 0; i < length; i++) {
  208. buffer.put(bytes[i], 8);
  209. }
  210. };
  211. /**
  212. * @public
  213. * @method getLength
  214. * @returns {number}
  215. */
  216. QRByte.prototype.getLength = function () {
  217. return this.bytes.length;
  218. };
  219. return QRByte;
  220. }(QRData));
  221.  
  222. /**
  223. * @module ErrorCorrectionLevel
  224. * @author nuintun
  225. * @author Cosmo Wolfe
  226. * @author Kazuhiko Arase
  227. */
  228. /**
  229. * @readonly
  230. * @enum {L, M, Q, H}
  231. */
  232.  
  233. (function (ErrorCorrectionLevel) {
  234. // 7%
  235. ErrorCorrectionLevel[ErrorCorrectionLevel["L"] = 1] = "L";
  236. // 15%
  237. ErrorCorrectionLevel[ErrorCorrectionLevel["M"] = 0] = "M";
  238. // 25%
  239. ErrorCorrectionLevel[ErrorCorrectionLevel["Q"] = 3] = "Q";
  240. // 30%
  241. ErrorCorrectionLevel[ErrorCorrectionLevel["H"] = 2] = "H";
  242. })(exports.ErrorCorrectionLevel || (exports.ErrorCorrectionLevel = {}));
  243.  
  244. /**
  245. * @module QRMath
  246. * @author nuintun
  247. * @author Kazuhiko Arase
  248. */
  249. var EXP_TABLE = [];
  250. var LOG_TABLE = [];
  251. for (var i = 0; i < 256; i++) {
  252. LOG_TABLE[i] = 0;
  253. EXP_TABLE[i] = i < 8 ? 1 << i : EXP_TABLE[i - 4] ^ EXP_TABLE[i - 5] ^ EXP_TABLE[i - 6] ^ EXP_TABLE[i - 8];
  254. }
  255. for (var i = 0; i < 255; i++) {
  256. LOG_TABLE[EXP_TABLE[i]] = i;
  257. }
  258. function glog(n) {
  259. if (n < 1) {
  260. throw "illegal log: " + n;
  261. }
  262. return LOG_TABLE[n];
  263. }
  264. function gexp(n) {
  265. while (n < 0) {
  266. n += 255;
  267. }
  268. while (n >= 256) {
  269. n -= 255;
  270. }
  271. return EXP_TABLE[n];
  272. }
  273.  
  274. /**
  275. * @module Polynomial
  276. * @author nuintun
  277. * @author Kazuhiko Arase
  278. */
  279. var Polynomial = /*#__PURE__*/ (function () {
  280. function Polynomial(num, shift) {
  281. if (shift === void 0) { shift = 0; }
  282. this.num = [];
  283. var offset = 0;
  284. var length = num.length;
  285. while (offset < length && num[offset] === 0) {
  286. offset++;
  287. }
  288. length -= offset;
  289. for (var i = 0; i < length; i++) {
  290. this.num.push(num[offset + i]);
  291. }
  292. for (var i = 0; i < shift; i++) {
  293. this.num.push(0);
  294. }
  295. }
  296. Polynomial.prototype.getAt = function (index) {
  297. return this.num[index];
  298. };
  299. Polynomial.prototype.getLength = function () {
  300. return this.num.length;
  301. };
  302. Polynomial.prototype.multiply = function (e) {
  303. var num = [];
  304. var eLength = e.getLength();
  305. var tLength = this.getLength();
  306. var dLength = tLength + eLength - 1;
  307. for (var i = 0; i < dLength; i++) {
  308. num.push(0);
  309. }
  310. for (var i = 0; i < tLength; i++) {
  311. for (var j = 0; j < eLength; j++) {
  312. num[i + j] ^= gexp(glog(this.getAt(i)) + glog(e.getAt(j)));
  313. }
  314. }
  315. return new Polynomial(num);
  316. };
  317. Polynomial.prototype.mod = function (e) {
  318. var eLength = e.getLength();
  319. var tLength = this.getLength();
  320. if (tLength - eLength < 0) {
  321. return this;
  322. }
  323. var ratio = glog(this.getAt(0)) - glog(e.getAt(0));
  324. // Create copy
  325. var num = [];
  326. for (var i = 0; i < tLength; i++) {
  327. num.push(this.getAt(i));
  328. }
  329. // Subtract and calc rest.
  330. for (var i = 0; i < eLength; i++) {
  331. num[i] ^= gexp(glog(e.getAt(i)) + ratio);
  332. }
  333. // Call recursively
  334. return new Polynomial(num).mod(e);
  335. };
  336. return Polynomial;
  337. }());
  338.  
  339. /**
  340. * @module QRUtil
  341. * @author nuintun
  342. * @author Kazuhiko Arase
  343. */
  344. var N1 = 3;
  345. var N2 = 3;
  346. var N3 = 40;
  347. var N4 = 10;
  348. var ALIGNMENT_PATTERN_TABLE = [
  349. [],
  350. [6, 18],
  351. [6, 22],
  352. [6, 26],
  353. [6, 30],
  354. [6, 34],
  355. [6, 22, 38],
  356. [6, 24, 42],
  357. [6, 26, 46],
  358. [6, 28, 50],
  359. [6, 30, 54],
  360. [6, 32, 58],
  361. [6, 34, 62],
  362. [6, 26, 46, 66],
  363. [6, 26, 48, 70],
  364. [6, 26, 50, 74],
  365. [6, 30, 54, 78],
  366. [6, 30, 56, 82],
  367. [6, 30, 58, 86],
  368. [6, 34, 62, 90],
  369. [6, 28, 50, 72, 94],
  370. [6, 26, 50, 74, 98],
  371. [6, 30, 54, 78, 102],
  372. [6, 28, 54, 80, 106],
  373. [6, 32, 58, 84, 110],
  374. [6, 30, 58, 86, 114],
  375. [6, 34, 62, 90, 118],
  376. [6, 26, 50, 74, 98, 122],
  377. [6, 30, 54, 78, 102, 126],
  378. [6, 26, 52, 78, 104, 130],
  379. [6, 30, 56, 82, 108, 134],
  380. [6, 34, 60, 86, 112, 138],
  381. [6, 30, 58, 86, 114, 142],
  382. [6, 34, 62, 90, 118, 146],
  383. [6, 30, 54, 78, 102, 126, 150],
  384. [6, 24, 50, 76, 102, 128, 154],
  385. [6, 28, 54, 80, 106, 132, 158],
  386. [6, 32, 58, 84, 110, 136, 162],
  387. [6, 26, 54, 82, 110, 138, 166],
  388. [6, 30, 58, 86, 114, 142, 170]
  389. ];
  390. var G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1);
  391. var G15 = (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0);
  392. var G18 = (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0);
  393. function getAlignmentPattern(version) {
  394. return ALIGNMENT_PATTERN_TABLE[version - 1];
  395. }
  396. function getErrorCorrectionPolynomial(errorCorrectionLength) {
  397. var e = new Polynomial([1]);
  398. for (var i = 0; i < errorCorrectionLength; i++) {
  399. e = e.multiply(new Polynomial([1, gexp(i)]));
  400. }
  401. return e;
  402. }
  403. function getBCHDigit(data) {
  404. var digit = 0;
  405. while (data !== 0) {
  406. digit++;
  407. data >>>= 1;
  408. }
  409. return digit;
  410. }
  411. var G18_BCH = getBCHDigit(G18);
  412. function getBCHVersion(data) {
  413. var offset = data << 12;
  414. while (getBCHDigit(offset) - G18_BCH >= 0) {
  415. offset ^= G18 << (getBCHDigit(offset) - G18_BCH);
  416. }
  417. return (data << 12) | offset;
  418. }
  419. var G15_BCH = getBCHDigit(G15);
  420. function getBCHVersionInfo(data) {
  421. var offset = data << 10;
  422. while (getBCHDigit(offset) - G15_BCH >= 0) {
  423. offset ^= G15 << (getBCHDigit(offset) - G15_BCH);
  424. }
  425. return ((data << 10) | offset) ^ G15_MASK;
  426. }
  427. function applyMaskPenaltyRule1Internal(qrcode, isHorizontal) {
  428. var penalty = 0;
  429. var moduleCount = qrcode.getModuleCount();
  430. for (var i = 0; i < moduleCount; i++) {
  431. var prevBit = null;
  432. var numSameBitCells = 0;
  433. for (var j = 0; j < moduleCount; j++) {
  434. var bit = isHorizontal ? qrcode.isDark(i, j) : qrcode.isDark(j, i);
  435. if (bit === prevBit) {
  436. numSameBitCells++;
  437. if (numSameBitCells === 5) {
  438. penalty += N1;
  439. }
  440. else if (numSameBitCells > 5) {
  441. penalty++;
  442. }
  443. }
  444. else {
  445. // set prev bit
  446. prevBit = bit;
  447. // include the cell itself
  448. numSameBitCells = 1;
  449. }
  450. }
  451. }
  452. return penalty;
  453. }
  454. function applyMaskPenaltyRule1(qrcode) {
  455. return applyMaskPenaltyRule1Internal(qrcode, true) + applyMaskPenaltyRule1Internal(qrcode, false);
  456. }
  457. function applyMaskPenaltyRule2(qrcode) {
  458. var penalty = 0;
  459. var moduleCount = qrcode.getModuleCount();
  460. for (var y = 0; y < moduleCount - 1; y++) {
  461. for (var x = 0; x < moduleCount - 1; x++) {
  462. var value = qrcode.isDark(y, x);
  463. if (value === qrcode.isDark(y, x + 1) && value === qrcode.isDark(y + 1, x) && value === qrcode.isDark(y + 1, x + 1)) {
  464. penalty += N2;
  465. }
  466. }
  467. }
  468. return penalty;
  469. }
  470. function isFourWhite(qrcode, rangeIndex, from, to, isHorizontal) {
  471. from = Math.max(from, 0);
  472. to = Math.min(to, qrcode.getModuleCount());
  473. for (var i = from; i < to; i++) {
  474. var value = isHorizontal ? qrcode.isDark(rangeIndex, i) : qrcode.isDark(i, rangeIndex);
  475. if (value) {
  476. return false;
  477. }
  478. }
  479. return true;
  480. }
  481. function applyMaskPenaltyRule3(qrcode) {
  482. var penalty = 0;
  483. var moduleCount = qrcode.getModuleCount();
  484. for (var y = 0; y < moduleCount; y++) {
  485. for (var x = 0; x < moduleCount; x++) {
  486. if (x + 6 < moduleCount &&
  487. qrcode.isDark(y, x) &&
  488. !qrcode.isDark(y, x + 1) &&
  489. qrcode.isDark(y, x + 2) &&
  490. qrcode.isDark(y, x + 3) &&
  491. qrcode.isDark(y, x + 4) &&
  492. !qrcode.isDark(y, x + 5) &&
  493. qrcode.isDark(y, x + 6) &&
  494. (isFourWhite(qrcode, y, x - 4, x, true) || isFourWhite(qrcode, y, x + 7, x + 11, true))) {
  495. penalty += N3;
  496. }
  497. if (y + 6 < moduleCount &&
  498. qrcode.isDark(y, x) &&
  499. !qrcode.isDark(y + 1, x) &&
  500. qrcode.isDark(y + 2, x) &&
  501. qrcode.isDark(y + 3, x) &&
  502. qrcode.isDark(y + 4, x) &&
  503. !qrcode.isDark(y + 5, x) &&
  504. qrcode.isDark(y + 6, x) &&
  505. (isFourWhite(qrcode, x, y - 4, y, false) || isFourWhite(qrcode, x, y + 7, y + 11, false))) {
  506. penalty += N3;
  507. }
  508. }
  509. }
  510. return penalty;
  511. }
  512. function applyMaskPenaltyRule4(qrcode) {
  513. var numDarkCells = 0;
  514. var moduleCount = qrcode.getModuleCount();
  515. for (var y = 0; y < moduleCount; y++) {
  516. for (var x = 0; x < moduleCount; x++) {
  517. if (qrcode.isDark(y, x)) {
  518. numDarkCells++;
  519. }
  520. }
  521. }
  522. var numTotalCells = moduleCount * moduleCount;
  523. var fivePercentVariances = Math.floor(Math.abs(numDarkCells * 20 - numTotalCells * 10) / numTotalCells);
  524. return fivePercentVariances * N4;
  525. }
  526. /**
  527. * @function calculateMaskPenalty
  528. * @param {Encoder} qrcode
  529. * @see https://www.thonky.com/qr-code-tutorial/data-masking
  530. * @see https://github.com/zxing/zxing/blob/master/core/src/main/java/com/google/zxing/qrcode/encoder/MaskUtil.java
  531. */
  532. function calculateMaskPenalty(qrcode) {
  533. return (applyMaskPenaltyRule1(qrcode) +
  534. applyMaskPenaltyRule2(qrcode) +
  535. applyMaskPenaltyRule3(qrcode) +
  536. applyMaskPenaltyRule4(qrcode));
  537. }
  538.  
  539. /**
  540. * @module RSBlock
  541. * @author nuintun
  542. * @author Kazuhiko Arase
  543. */
  544. var RSBlock = /*#__PURE__*/ (function () {
  545. function RSBlock(totalCount, dataCount) {
  546. this.dataCount = dataCount;
  547. this.totalCount = totalCount;
  548. }
  549. RSBlock.prototype.getDataCount = function () {
  550. return this.dataCount;
  551. };
  552. RSBlock.prototype.getTotalCount = function () {
  553. return this.totalCount;
  554. };
  555. RSBlock.getRSBlocks = function (version, errorCorrectionLevel) {
  556. var rsBlocks = [];
  557. var rsBlock = RSBlock.getRSBlockTable(version, errorCorrectionLevel);
  558. var length = rsBlock.length / 3;
  559. for (var i = 0; i < length; i++) {
  560. var count = rsBlock[i * 3 + 0];
  561. var totalCount = rsBlock[i * 3 + 1];
  562. var dataCount = rsBlock[i * 3 + 2];
  563. for (var j = 0; j < count; j++) {
  564. rsBlocks.push(new RSBlock(totalCount, dataCount));
  565. }
  566. }
  567. return rsBlocks;
  568. };
  569. RSBlock.getRSBlockTable = function (version, errorCorrectionLevel) {
  570. switch (errorCorrectionLevel) {
  571. case exports.ErrorCorrectionLevel.L:
  572. return RSBlock.RS_BLOCK_TABLE[(version - 1) * 4 + 0];
  573. case exports.ErrorCorrectionLevel.M:
  574. return RSBlock.RS_BLOCK_TABLE[(version - 1) * 4 + 1];
  575. case exports.ErrorCorrectionLevel.Q:
  576. return RSBlock.RS_BLOCK_TABLE[(version - 1) * 4 + 2];
  577. case exports.ErrorCorrectionLevel.H:
  578. return RSBlock.RS_BLOCK_TABLE[(version - 1) * 4 + 3];
  579. default:
  580. throw "illegal error correction level: " + errorCorrectionLevel;
  581. }
  582. };
  583. RSBlock.RS_BLOCK_TABLE = [
  584. // L
  585. // M
  586. // Q
  587. // H
  588. // 1
  589. [1, 26, 19],
  590. [1, 26, 16],
  591. [1, 26, 13],
  592. [1, 26, 9],
  593. // 2
  594. [1, 44, 34],
  595. [1, 44, 28],
  596. [1, 44, 22],
  597. [1, 44, 16],
  598. // 3
  599. [1, 70, 55],
  600. [1, 70, 44],
  601. [2, 35, 17],
  602. [2, 35, 13],
  603. // 4
  604. [1, 100, 80],
  605. [2, 50, 32],
  606. [2, 50, 24],
  607. [4, 25, 9],
  608. // 5
  609. [1, 134, 108],
  610. [2, 67, 43],
  611. [2, 33, 15, 2, 34, 16],
  612. [2, 33, 11, 2, 34, 12],
  613. // 6
  614. [2, 86, 68],
  615. [4, 43, 27],
  616. [4, 43, 19],
  617. [4, 43, 15],
  618. // 7
  619. [2, 98, 78],
  620. [4, 49, 31],
  621. [2, 32, 14, 4, 33, 15],
  622. [4, 39, 13, 1, 40, 14],
  623. // 8
  624. [2, 121, 97],
  625. [2, 60, 38, 2, 61, 39],
  626. [4, 40, 18, 2, 41, 19],
  627. [4, 40, 14, 2, 41, 15],
  628. // 9
  629. [2, 146, 116],
  630. [3, 58, 36, 2, 59, 37],
  631. [4, 36, 16, 4, 37, 17],
  632. [4, 36, 12, 4, 37, 13],
  633. // 10
  634. [2, 86, 68, 2, 87, 69],
  635. [4, 69, 43, 1, 70, 44],
  636. [6, 43, 19, 2, 44, 20],
  637. [6, 43, 15, 2, 44, 16],
  638. // 11
  639. [4, 101, 81],
  640. [1, 80, 50, 4, 81, 51],
  641. [4, 50, 22, 4, 51, 23],
  642. [3, 36, 12, 8, 37, 13],
  643. // 12
  644. [2, 116, 92, 2, 117, 93],
  645. [6, 58, 36, 2, 59, 37],
  646. [4, 46, 20, 6, 47, 21],
  647. [7, 42, 14, 4, 43, 15],
  648. // 13
  649. [4, 133, 107],
  650. [8, 59, 37, 1, 60, 38],
  651. [8, 44, 20, 4, 45, 21],
  652. [12, 33, 11, 4, 34, 12],
  653. // 14
  654. [3, 145, 115, 1, 146, 116],
  655. [4, 64, 40, 5, 65, 41],
  656. [11, 36, 16, 5, 37, 17],
  657. [11, 36, 12, 5, 37, 13],
  658. // 15
  659. [5, 109, 87, 1, 110, 88],
  660. [5, 65, 41, 5, 66, 42],
  661. [5, 54, 24, 7, 55, 25],
  662. [11, 36, 12, 7, 37, 13],
  663. // 16
  664. [5, 122, 98, 1, 123, 99],
  665. [7, 73, 45, 3, 74, 46],
  666. [15, 43, 19, 2, 44, 20],
  667. [3, 45, 15, 13, 46, 16],
  668. // 17
  669. [1, 135, 107, 5, 136, 108],
  670. [10, 74, 46, 1, 75, 47],
  671. [1, 50, 22, 15, 51, 23],
  672. [2, 42, 14, 17, 43, 15],
  673. // 18
  674. [5, 150, 120, 1, 151, 121],
  675. [9, 69, 43, 4, 70, 44],
  676. [17, 50, 22, 1, 51, 23],
  677. [2, 42, 14, 19, 43, 15],
  678. // 19
  679. [3, 141, 113, 4, 142, 114],
  680. [3, 70, 44, 11, 71, 45],
  681. [17, 47, 21, 4, 48, 22],
  682. [9, 39, 13, 16, 40, 14],
  683. // 20
  684. [3, 135, 107, 5, 136, 108],
  685. [3, 67, 41, 13, 68, 42],
  686. [15, 54, 24, 5, 55, 25],
  687. [15, 43, 15, 10, 44, 16],
  688. // 21
  689. [4, 144, 116, 4, 145, 117],
  690. [17, 68, 42],
  691. [17, 50, 22, 6, 51, 23],
  692. [19, 46, 16, 6, 47, 17],
  693. // 22
  694. [2, 139, 111, 7, 140, 112],
  695. [17, 74, 46],
  696. [7, 54, 24, 16, 55, 25],
  697. [34, 37, 13],
  698. // 23
  699. [4, 151, 121, 5, 152, 122],
  700. [4, 75, 47, 14, 76, 48],
  701. [11, 54, 24, 14, 55, 25],
  702. [16, 45, 15, 14, 46, 16],
  703. // 24
  704. [6, 147, 117, 4, 148, 118],
  705. [6, 73, 45, 14, 74, 46],
  706. [11, 54, 24, 16, 55, 25],
  707. [30, 46, 16, 2, 47, 17],
  708. // 25
  709. [8, 132, 106, 4, 133, 107],
  710. [8, 75, 47, 13, 76, 48],
  711. [7, 54, 24, 22, 55, 25],
  712. [22, 45, 15, 13, 46, 16],
  713. // 26
  714. [10, 142, 114, 2, 143, 115],
  715. [19, 74, 46, 4, 75, 47],
  716. [28, 50, 22, 6, 51, 23],
  717. [33, 46, 16, 4, 47, 17],
  718. // 27
  719. [8, 152, 122, 4, 153, 123],
  720. [22, 73, 45, 3, 74, 46],
  721. [8, 53, 23, 26, 54, 24],
  722. [12, 45, 15, 28, 46, 16],
  723. // 28
  724. [3, 147, 117, 10, 148, 118],
  725. [3, 73, 45, 23, 74, 46],
  726. [4, 54, 24, 31, 55, 25],
  727. [11, 45, 15, 31, 46, 16],
  728. // 29
  729. [7, 146, 116, 7, 147, 117],
  730. [21, 73, 45, 7, 74, 46],
  731. [1, 53, 23, 37, 54, 24],
  732. [19, 45, 15, 26, 46, 16],
  733. // 30
  734. [5, 145, 115, 10, 146, 116],
  735. [19, 75, 47, 10, 76, 48],
  736. [15, 54, 24, 25, 55, 25],
  737. [23, 45, 15, 25, 46, 16],
  738. // 31
  739. [13, 145, 115, 3, 146, 116],
  740. [2, 74, 46, 29, 75, 47],
  741. [42, 54, 24, 1, 55, 25],
  742. [23, 45, 15, 28, 46, 16],
  743. // 32
  744. [17, 145, 115],
  745. [10, 74, 46, 23, 75, 47],
  746. [10, 54, 24, 35, 55, 25],
  747. [19, 45, 15, 35, 46, 16],
  748. // 33
  749. [17, 145, 115, 1, 146, 116],
  750. [14, 74, 46, 21, 75, 47],
  751. [29, 54, 24, 19, 55, 25],
  752. [11, 45, 15, 46, 46, 16],
  753. // 34
  754. [13, 145, 115, 6, 146, 116],
  755. [14, 74, 46, 23, 75, 47],
  756. [44, 54, 24, 7, 55, 25],
  757. [59, 46, 16, 1, 47, 17],
  758. // 35
  759. [12, 151, 121, 7, 152, 122],
  760. [12, 75, 47, 26, 76, 48],
  761. [39, 54, 24, 14, 55, 25],
  762. [22, 45, 15, 41, 46, 16],
  763. // 36
  764. [6, 151, 121, 14, 152, 122],
  765. [6, 75, 47, 34, 76, 48],
  766. [46, 54, 24, 10, 55, 25],
  767. [2, 45, 15, 64, 46, 16],
  768. // 37
  769. [17, 152, 122, 4, 153, 123],
  770. [29, 74, 46, 14, 75, 47],
  771. [49, 54, 24, 10, 55, 25],
  772. [24, 45, 15, 46, 46, 16],
  773. // 38
  774. [4, 152, 122, 18, 153, 123],
  775. [13, 74, 46, 32, 75, 47],
  776. [48, 54, 24, 14, 55, 25],
  777. [42, 45, 15, 32, 46, 16],
  778. // 39
  779. [20, 147, 117, 4, 148, 118],
  780. [40, 75, 47, 7, 76, 48],
  781. [43, 54, 24, 22, 55, 25],
  782. [10, 45, 15, 67, 46, 16],
  783. // 40
  784. [19, 148, 118, 6, 149, 119],
  785. [18, 75, 47, 31, 76, 48],
  786. [34, 54, 24, 34, 55, 25],
  787. [20, 45, 15, 61, 46, 16]
  788. ];
  789. return RSBlock;
  790. }());
  791.  
  792. /**
  793. * @module BitBuffer
  794. * @author nuintun
  795. * @author Kazuhiko Arase
  796. */
  797. var BitBuffer = /*#__PURE__*/ (function () {
  798. function BitBuffer() {
  799. this.length = 0;
  800. this.buffer = [];
  801. }
  802. BitBuffer.prototype.getBuffer = function () {
  803. return this.buffer;
  804. };
  805. BitBuffer.prototype.getLengthInBits = function () {
  806. return this.length;
  807. };
  808. BitBuffer.prototype.getBit = function (index) {
  809. return ((this.buffer[(index / 8) >> 0] >>> (7 - (index % 8))) & 1) === 1;
  810. };
  811. BitBuffer.prototype.put = function (num, length) {
  812. for (var i = 0; i < length; i++) {
  813. this.putBit(((num >>> (length - i - 1)) & 1) === 1);
  814. }
  815. };
  816. BitBuffer.prototype.putBit = function (bit) {
  817. if (this.length === this.buffer.length * 8) {
  818. this.buffer.push(0);
  819. }
  820. if (bit) {
  821. this.buffer[(this.length / 8) >> 0] |= 0x80 >>> this.length % 8;
  822. }
  823. this.length++;
  824. };
  825. return BitBuffer;
  826. }());
  827.  
  828. /**
  829. * @module OutputStream
  830. * @author nuintun
  831. * @author Kazuhiko Arase
  832. */
  833. var OutputStream = /*#__PURE__*/ (function () {
  834. function OutputStream() {
  835. }
  836. OutputStream.prototype.writeBytes = function (bytes) {
  837. var length = bytes.length;
  838. for (var i = 0; i < length; i++) {
  839. this.writeByte(bytes[i]);
  840. }
  841. };
  842. OutputStream.prototype.flush = function () {
  843. // The flush method
  844. };
  845. OutputStream.prototype.close = function () {
  846. this.flush();
  847. };
  848. return OutputStream;
  849. }());
  850.  
  851. /**
  852. * @module ByteArrayOutputStream
  853. * @author nuintun
  854. * @author Kazuhiko Arase
  855. */
  856. var ByteArrayOutputStream = /*#__PURE__*/ (function (_super) {
  857. __extends(ByteArrayOutputStream, _super);
  858. function ByteArrayOutputStream() {
  859. var _this = _super !== null && _super.apply(this, arguments) || this;
  860. _this.bytes = [];
  861. return _this;
  862. }
  863. ByteArrayOutputStream.prototype.writeByte = function (byte) {
  864. this.bytes.push(byte);
  865. };
  866. ByteArrayOutputStream.prototype.toByteArray = function () {
  867. return this.bytes;
  868. };
  869. return ByteArrayOutputStream;
  870. }(OutputStream));
  871.  
  872. /**
  873. * @module Base64EncodeOutputStream
  874. * @author nuintun
  875. * @author Kazuhiko Arase
  876. */
  877. function encode(ch) {
  878. if (ch >= 0) {
  879. if (ch < 26) {
  880. // A
  881. return 0x41 + ch;
  882. }
  883. else if (ch < 52) {
  884. // a
  885. return 0x61 + (ch - 26);
  886. }
  887. else if (ch < 62) {
  888. // 0
  889. return 0x30 + (ch - 52);
  890. }
  891. else if (ch === 62) {
  892. // +
  893. return 0x2b;
  894. }
  895. else if (ch === 63) {
  896. // /
  897. return 0x2f;
  898. }
  899. }
  900. throw "illegal char: " + String.fromCharCode(ch);
  901. }
  902. var Base64EncodeOutputStream = /*#__PURE__*/ (function (_super) {
  903. __extends(Base64EncodeOutputStream, _super);
  904. function Base64EncodeOutputStream(stream) {
  905. var _this = _super.call(this) || this;
  906. _this.buffer = 0;
  907. _this.length = 0;
  908. _this.bufLength = 0;
  909. _this.stream = stream;
  910. return _this;
  911. }
  912. Base64EncodeOutputStream.prototype.writeByte = function (byte) {
  913. this.buffer = (this.buffer << 8) | (byte & 0xff);
  914. this.bufLength += 8;
  915. this.length++;
  916. while (this.bufLength >= 6) {
  917. this.writeEncoded(this.buffer >>> (this.bufLength - 6));
  918. this.bufLength -= 6;
  919. }
  920. };
  921. /**
  922. * @override
  923. */
  924. Base64EncodeOutputStream.prototype.flush = function () {
  925. if (this.bufLength > 0) {
  926. this.writeEncoded(this.buffer << (6 - this.bufLength));
  927. this.buffer = 0;
  928. this.bufLength = 0;
  929. }
  930. if (this.length % 3 != 0) {
  931. // Padding
  932. var pad = 3 - (this.length % 3);
  933. for (var i = 0; i < pad; i++) {
  934. // =
  935. this.stream.writeByte(0x3d);
  936. }
  937. }
  938. };
  939. Base64EncodeOutputStream.prototype.writeEncoded = function (byte) {
  940. this.stream.writeByte(encode(byte & 0x3f));
  941. };
  942. return Base64EncodeOutputStream;
  943. }(OutputStream));
  944.  
  945. /**
  946. * @module GIF Image (B/W)
  947. * @author nuintun
  948. * @author Kazuhiko Arase
  949. */
  950. function encodeToBase64(data) {
  951. var output = new ByteArrayOutputStream();
  952. var stream = new Base64EncodeOutputStream(output);
  953. try {
  954. stream.writeBytes(data);
  955. }
  956. finally {
  957. stream.close();
  958. }
  959. output.close();
  960. return output.toByteArray();
  961. }
  962. var LZWTable = /*#__PURE__*/ (function () {
  963. function LZWTable() {
  964. this.size = 0;
  965. this.map = {};
  966. }
  967. LZWTable.prototype.add = function (key) {
  968. if (!this.contains(key)) {
  969. this.map[key] = this.size++;
  970. }
  971. };
  972. LZWTable.prototype.getSize = function () {
  973. return this.size;
  974. };
  975. LZWTable.prototype.indexOf = function (key) {
  976. return this.map[key];
  977. };
  978. LZWTable.prototype.contains = function (key) {
  979. return this.map.hasOwnProperty(key);
  980. };
  981. return LZWTable;
  982. }());
  983. var BitOutputStream = /*#__PURE__*/ (function () {
  984. function BitOutputStream(output) {
  985. this.output = output;
  986. this.bitLength = 0;
  987. }
  988. BitOutputStream.prototype.write = function (data, length) {
  989. if (data >>> length !== 0) {
  990. throw 'length overflow';
  991. }
  992. while (this.bitLength + length >= 8) {
  993. this.output.writeByte(0xff & ((data << this.bitLength) | this.bitBuffer));
  994. length -= 8 - this.bitLength;
  995. data >>>= 8 - this.bitLength;
  996. this.bitBuffer = 0;
  997. this.bitLength = 0;
  998. }
  999. this.bitBuffer = (data << this.bitLength) | this.bitBuffer;
  1000. this.bitLength = this.bitLength + length;
  1001. };
  1002. BitOutputStream.prototype.flush = function () {
  1003. if (this.bitLength > 0) {
  1004. this.output.writeByte(this.bitBuffer);
  1005. }
  1006. this.output.flush();
  1007. };
  1008. BitOutputStream.prototype.close = function () {
  1009. this.flush();
  1010. this.output.close();
  1011. };
  1012. return BitOutputStream;
  1013. }());
  1014. var GIFImage = /*#__PURE__*/ (function () {
  1015. function GIFImage(width, height) {
  1016. this.data = [];
  1017. this.width = width;
  1018. this.height = height;
  1019. var size = width * height;
  1020. for (var i = 0; i < size; i++) {
  1021. this.data[i] = 0;
  1022. }
  1023. }
  1024. GIFImage.prototype.getLZWRaster = function (lzwMinCodeSize) {
  1025. var clearCode = 1 << lzwMinCodeSize;
  1026. var endCode = (1 << lzwMinCodeSize) + 1;
  1027. // Setup LZWTable
  1028. var table = new LZWTable();
  1029. for (var i = 0; i < clearCode; i++) {
  1030. table.add(String.fromCharCode(i));
  1031. }
  1032. table.add(String.fromCharCode(clearCode));
  1033. table.add(String.fromCharCode(endCode));
  1034. var byteOutput = new ByteArrayOutputStream();
  1035. var bitOutput = new BitOutputStream(byteOutput);
  1036. var bitLength = lzwMinCodeSize + 1;
  1037. try {
  1038. // Clear code
  1039. bitOutput.write(clearCode, bitLength);
  1040. var dataIndex = 0;
  1041. var s = String.fromCharCode(this.data[dataIndex++]);
  1042. var length_1 = this.data.length;
  1043. while (dataIndex < length_1) {
  1044. var c = String.fromCharCode(this.data[dataIndex++]);
  1045. if (table.contains(s + c)) {
  1046. s = s + c;
  1047. }
  1048. else {
  1049. bitOutput.write(table.indexOf(s), bitLength);
  1050. if (table.getSize() < 0xfff) {
  1051. if (table.getSize() === 1 << bitLength) {
  1052. bitLength++;
  1053. }
  1054. table.add(s + c);
  1055. }
  1056. s = c;
  1057. }
  1058. }
  1059. bitOutput.write(table.indexOf(s), bitLength);
  1060. // End code
  1061. bitOutput.write(endCode, bitLength);
  1062. }
  1063. finally {
  1064. bitOutput.close();
  1065. }
  1066. return byteOutput.toByteArray();
  1067. };
  1068. GIFImage.prototype.writeWord = function (output, i) {
  1069. output.writeByte(i & 0xff);
  1070. output.writeByte((i >>> 8) & 0xff);
  1071. };
  1072. GIFImage.prototype.writeBytes = function (output, bytes, off, length) {
  1073. for (var i = 0; i < length; i++) {
  1074. output.writeByte(bytes[i + off]);
  1075. }
  1076. };
  1077. GIFImage.prototype.setPixel = function (x, y, pixel) {
  1078. if (x < 0 || this.width <= x)
  1079. throw "illegal x axis: " + x;
  1080. if (y < 0 || this.height <= y)
  1081. throw "illegal y axis: " + y;
  1082. this.data[y * this.width + x] = pixel;
  1083. };
  1084. GIFImage.prototype.getPixel = function (x, y) {
  1085. if (x < 0 || this.width <= x)
  1086. throw "illegal x axis: " + x;
  1087. if (y < 0 || this.height <= y)
  1088. throw "illegal x axis: " + y;
  1089. return this.data[y * this.width + x];
  1090. };
  1091. GIFImage.prototype.write = function (output) {
  1092. // GIF Signature
  1093. output.writeByte(0x47); // G
  1094. output.writeByte(0x49); // I
  1095. output.writeByte(0x46); // F
  1096. output.writeByte(0x38); // 8
  1097. output.writeByte(0x37); // 7
  1098. output.writeByte(0x61); // a
  1099. // Screen Descriptor
  1100. this.writeWord(output, this.width);
  1101. this.writeWord(output, this.height);
  1102. output.writeByte(0x80); // 2bit
  1103. output.writeByte(0);
  1104. output.writeByte(0);
  1105. // Global Color Map
  1106. // Black
  1107. output.writeByte(0x00);
  1108. output.writeByte(0x00);
  1109. output.writeByte(0x00);
  1110. // White
  1111. output.writeByte(0xff);
  1112. output.writeByte(0xff);
  1113. output.writeByte(0xff);
  1114. // Image Descriptor
  1115. output.writeByte(0x2c); // ,
  1116. this.writeWord(output, 0);
  1117. this.writeWord(output, 0);
  1118. this.writeWord(output, this.width);
  1119. this.writeWord(output, this.height);
  1120. output.writeByte(0);
  1121. // Local Color Map
  1122. // Raster Data
  1123. var lzwMinCodeSize = 2;
  1124. var raster = this.getLZWRaster(lzwMinCodeSize);
  1125. var raLength = raster.length;
  1126. output.writeByte(lzwMinCodeSize);
  1127. var offset = 0;
  1128. while (raLength - offset > 255) {
  1129. output.writeByte(255);
  1130. this.writeBytes(output, raster, offset, 255);
  1131. offset += 255;
  1132. }
  1133. var length = raLength - offset;
  1134. output.writeByte(length);
  1135. this.writeBytes(output, raster, offset, length);
  1136. output.writeByte(0x00);
  1137. // GIF Terminator
  1138. output.writeByte(0x3b); // ;
  1139. };
  1140. GIFImage.prototype.toDataURL = function () {
  1141. var output = new ByteArrayOutputStream();
  1142. this.write(output);
  1143. var bytes = encodeToBase64(output.toByteArray());
  1144. output.close();
  1145. var url = 'data:image/gif;base64,';
  1146. var length = bytes.length;
  1147. for (var i = 0; i < length; i++) {
  1148. url += String.fromCharCode(bytes[i]);
  1149. }
  1150. return url;
  1151. };
  1152. return GIFImage;
  1153. }());
  1154.  
  1155. /**
  1156. * @module MaskPattern
  1157. * @author nuintun
  1158. * @author Cosmo Wolfe
  1159. * @author Kazuhiko Arase
  1160. */
  1161. function getMaskFunc(maskPattern) {
  1162. switch (maskPattern) {
  1163. case 0 /* PATTERN000 */:
  1164. return function (x, y) { return ((x + y) & 0x1) === 0; };
  1165. case 1 /* PATTERN001 */:
  1166. return function (x, y) { return (y & 0x1) === 0; };
  1167. case 2 /* PATTERN010 */:
  1168. return function (x, y) { return x % 3 === 0; };
  1169. case 3 /* PATTERN011 */:
  1170. return function (x, y) { return (x + y) % 3 === 0; };
  1171. case 4 /* PATTERN100 */:
  1172. return function (x, y) { return ((((x / 3) >> 0) + ((y / 2) >> 0)) & 0x1) === 0; };
  1173. case 5 /* PATTERN101 */:
  1174. return function (x, y) { return ((x * y) & 0x1) + ((x * y) % 3) === 0; };
  1175. case 6 /* PATTERN110 */:
  1176. return function (x, y) { return ((((x * y) & 0x1) + ((x * y) % 3)) & 0x1) === 0; };
  1177. case 7 /* PATTERN111 */:
  1178. return function (x, y) { return ((((x * y) % 3) + ((x + y) & 0x1)) & 0x1) === 0; };
  1179. default:
  1180. throw "illegal mask: " + maskPattern;
  1181. }
  1182. }
  1183.  
  1184. /**
  1185. * @module QRCode
  1186. * @author nuintun
  1187. * @author Kazuhiko Arase
  1188. */
  1189. var PAD0 = 0xec;
  1190. var PAD1 = 0x11;
  1191. var toString = Object.prototype.toString;
  1192. /**
  1193. * @function appendECI
  1194. * @param {number} encoding
  1195. * @param {BitBuffer} buffer
  1196. * @see https://github.com/nayuki/QR-Code-generator/blob/master/typescript/qrcodegen.ts
  1197. * @see https://github.com/zxing/zxing/blob/master/core/src/main/java/com/google/zxing/qrcode/encoder/Encoder.java
  1198. */
  1199. function appendECI(encoding, buffer) {
  1200. if (encoding < 0 || encoding >= 1000000) {
  1201. throw 'byte mode encoding hint out of range';
  1202. }
  1203. buffer.put(exports.Mode.ECI, 4);
  1204. if (encoding < 1 << 7) {
  1205. buffer.put(encoding, 8);
  1206. }
  1207. else if (encoding < 1 << 14) {
  1208. buffer.put(2, 2);
  1209. buffer.put(encoding, 14);
  1210. }
  1211. else {
  1212. buffer.put(6, 3);
  1213. buffer.put(encoding, 21);
  1214. }
  1215. }
  1216. function prepareData(version, errorCorrectionLevel, hasEncodingHint, chunks) {
  1217. var dLength = chunks.length;
  1218. var buffer = new BitBuffer();
  1219. var rsBlocks = RSBlock.getRSBlocks(version, errorCorrectionLevel);
  1220. for (var i = 0; i < dLength; i++) {
  1221. var data = chunks[i];
  1222. var mode = data.getMode();
  1223. // Default set encoding UTF-8 when has encoding hint
  1224. if (hasEncodingHint && mode === exports.Mode.Byte) {
  1225. appendECI(data.encoding, buffer);
  1226. }
  1227. buffer.put(mode, 4);
  1228. buffer.put(data.getLength(), data.getLengthInBits(version));
  1229. data.write(buffer);
  1230. }
  1231. // Calc max data count
  1232. var maxDataCount = 0;
  1233. var rLength = rsBlocks.length;
  1234. for (var i = 0; i < rLength; i++) {
  1235. maxDataCount += rsBlocks[i].getDataCount();
  1236. }
  1237. maxDataCount *= 8;
  1238. return [buffer, rsBlocks, maxDataCount];
  1239. }
  1240. function createBytes(buffer, rsBlocks) {
  1241. var offset = 0;
  1242. var maxDcCount = 0;
  1243. var maxEcCount = 0;
  1244. var dcData = [];
  1245. var ecData = [];
  1246. var rsLength = rsBlocks.length;
  1247. var bufferData = buffer.getBuffer();
  1248. for (var r = 0; r < rsLength; r++) {
  1249. var rsBlock = rsBlocks[r];
  1250. var dcCount = rsBlock.getDataCount();
  1251. var ecCount = rsBlock.getTotalCount() - dcCount;
  1252. maxDcCount = Math.max(maxDcCount, dcCount);
  1253. maxEcCount = Math.max(maxEcCount, ecCount);
  1254. dcData[r] = [];
  1255. for (var i = 0; i < dcCount; i++) {
  1256. dcData[r][i] = 0xff & bufferData[i + offset];
  1257. }
  1258. offset += dcCount;
  1259. var rsPoly = getErrorCorrectionPolynomial(ecCount);
  1260. var ecLength = rsPoly.getLength() - 1;
  1261. var rawPoly = new Polynomial(dcData[r], ecLength);
  1262. var modPoly = rawPoly.mod(rsPoly);
  1263. var mpLength = modPoly.getLength();
  1264. ecData[r] = [];
  1265. for (var i = 0; i < ecLength; i++) {
  1266. var modIndex = i + mpLength - ecLength;
  1267. ecData[r][i] = modIndex >= 0 ? modPoly.getAt(modIndex) : 0;
  1268. }
  1269. }
  1270. buffer = new BitBuffer();
  1271. for (var i = 0; i < maxDcCount; i++) {
  1272. for (var r = 0; r < rsLength; r++) {
  1273. if (i < dcData[r].length) {
  1274. buffer.put(dcData[r][i], 8);
  1275. }
  1276. }
  1277. }
  1278. for (var i = 0; i < maxEcCount; i++) {
  1279. for (var r = 0; r < rsLength; r++) {
  1280. if (i < ecData[r].length) {
  1281. buffer.put(ecData[r][i], 8);
  1282. }
  1283. }
  1284. }
  1285. return buffer;
  1286. }
  1287. function createData(buffer, rsBlocks, maxDataCount) {
  1288. if (buffer.getLengthInBits() > maxDataCount) {
  1289. throw "data overflow: " + buffer.getLengthInBits() + " > " + maxDataCount;
  1290. }
  1291. // End
  1292. if (buffer.getLengthInBits() + 4 <= maxDataCount) {
  1293. buffer.put(0, 4);
  1294. }
  1295. // Padding
  1296. while (buffer.getLengthInBits() % 8 !== 0) {
  1297. buffer.putBit(false);
  1298. }
  1299. // Padding
  1300. while (true) {
  1301. if (buffer.getLengthInBits() >= maxDataCount) {
  1302. break;
  1303. }
  1304. buffer.put(PAD0, 8);
  1305. if (buffer.getLengthInBits() >= maxDataCount) {
  1306. break;
  1307. }
  1308. buffer.put(PAD1, 8);
  1309. }
  1310. return createBytes(buffer, rsBlocks);
  1311. }
  1312. var Encoder = /*#__PURE__*/ (function () {
  1313. function Encoder() {
  1314. this.version = 0;
  1315. this.chunks = [];
  1316. this.moduleCount = 0;
  1317. this.modules = [];
  1318. this.hasEncodingHint = false;
  1319. this.autoVersion = this.version === 0;
  1320. this.errorCorrectionLevel = exports.ErrorCorrectionLevel.L;
  1321. }
  1322. /**
  1323. * @public
  1324. * @method getModules
  1325. * @returns {boolean[][]}
  1326. */
  1327. Encoder.prototype.getModules = function () {
  1328. return this.modules;
  1329. };
  1330. /**
  1331. * @public
  1332. * @method getModuleCount
  1333. */
  1334. Encoder.prototype.getModuleCount = function () {
  1335. return this.moduleCount;
  1336. };
  1337. /**
  1338. * @public
  1339. * @method getVersion
  1340. * @returns {number}
  1341. */
  1342. Encoder.prototype.getVersion = function () {
  1343. return this.version;
  1344. };
  1345. /**
  1346. * @public
  1347. * @method setVersion
  1348. * @param {number} version
  1349. */
  1350. Encoder.prototype.setVersion = function (version) {
  1351. this.version = Math.min(40, Math.max(0, version >> 0));
  1352. this.autoVersion = this.version === 0;
  1353. };
  1354. /**
  1355. * @public
  1356. * @method getErrorCorrectionLevel
  1357. * @returns {ErrorCorrectionLevel}
  1358. */
  1359. Encoder.prototype.getErrorCorrectionLevel = function () {
  1360. return this.errorCorrectionLevel;
  1361. };
  1362. /**
  1363. * @public
  1364. * @method setErrorCorrectionLevel
  1365. * @param {ErrorCorrectionLevel} errorCorrectionLevel
  1366. */
  1367. Encoder.prototype.setErrorCorrectionLevel = function (errorCorrectionLevel) {
  1368. switch (errorCorrectionLevel) {
  1369. case exports.ErrorCorrectionLevel.L:
  1370. case exports.ErrorCorrectionLevel.M:
  1371. case exports.ErrorCorrectionLevel.Q:
  1372. case exports.ErrorCorrectionLevel.H:
  1373. this.errorCorrectionLevel = errorCorrectionLevel;
  1374. }
  1375. };
  1376. /**
  1377. * @public
  1378. * @method getEncodingHint
  1379. * @returns {boolean}
  1380. */
  1381. Encoder.prototype.getEncodingHint = function () {
  1382. return this.hasEncodingHint;
  1383. };
  1384. /**
  1385. * @public
  1386. * @method setEncodingHint
  1387. * @param {boolean} hasEncodingHint
  1388. */
  1389. Encoder.prototype.setEncodingHint = function (hasEncodingHint) {
  1390. this.hasEncodingHint = hasEncodingHint;
  1391. };
  1392. /**
  1393. * @public
  1394. * @method write
  1395. * @param {QRData} data
  1396. */
  1397. Encoder.prototype.write = function (data) {
  1398. if (data instanceof QRData) {
  1399. this.chunks.push(data);
  1400. }
  1401. else {
  1402. var type = toString.call(data);
  1403. if (type === '[object String]') {
  1404. this.chunks.push(new QRByte(data));
  1405. }
  1406. else {
  1407. throw "illegal data: " + data;
  1408. }
  1409. }
  1410. };
  1411. /**
  1412. * @public
  1413. * @method isDark
  1414. * @param {number} row
  1415. * @param {number} col
  1416. * @returns {boolean}
  1417. */
  1418. Encoder.prototype.isDark = function (row, col) {
  1419. if (this.modules[row][col] !== null) {
  1420. return this.modules[row][col];
  1421. }
  1422. else {
  1423. return false;
  1424. }
  1425. };
  1426. Encoder.prototype.setupFinderPattern = function (row, col) {
  1427. var moduleCount = this.moduleCount;
  1428. for (var r = -1; r <= 7; r++) {
  1429. for (var c = -1; c <= 7; c++) {
  1430. if (row + r <= -1 || moduleCount <= row + r || col + c <= -1 || moduleCount <= col + c) {
  1431. continue;
  1432. }
  1433. if ((0 <= r && r <= 6 && (c === 0 || c === 6)) ||
  1434. (0 <= c && c <= 6 && (r === 0 || r === 6)) ||
  1435. (2 <= r && r <= 4 && 2 <= c && c <= 4)) {
  1436. this.modules[row + r][col + c] = true;
  1437. }
  1438. else {
  1439. this.modules[row + r][col + c] = false;
  1440. }
  1441. }
  1442. }
  1443. };
  1444. Encoder.prototype.setupAlignmentPattern = function () {
  1445. var pos = getAlignmentPattern(this.version);
  1446. var length = pos.length;
  1447. for (var i = 0; i < length; i++) {
  1448. for (var j = 0; j < length; j++) {
  1449. var row = pos[i];
  1450. var col = pos[j];
  1451. if (this.modules[row][col] !== null) {
  1452. continue;
  1453. }
  1454. for (var r = -2; r <= 2; r++) {
  1455. for (var c = -2; c <= 2; c++) {
  1456. if (r === -2 || r === 2 || c === -2 || c === 2 || (r === 0 && c === 0)) {
  1457. this.modules[row + r][col + c] = true;
  1458. }
  1459. else {
  1460. this.modules[row + r][col + c] = false;
  1461. }
  1462. }
  1463. }
  1464. }
  1465. }
  1466. };
  1467. Encoder.prototype.setupTimingPattern = function () {
  1468. var count = this.moduleCount - 8;
  1469. for (var i = 8; i < count; i++) {
  1470. var bit = i % 2 === 0;
  1471. // vertical
  1472. if (this.modules[i][6] === null) {
  1473. this.modules[i][6] = bit;
  1474. }
  1475. // horizontal
  1476. if (this.modules[6][i] === null) {
  1477. this.modules[6][i] = bit;
  1478. }
  1479. }
  1480. };
  1481. Encoder.prototype.setupFormatInfo = function (maskPattern) {
  1482. var data = (this.errorCorrectionLevel << 3) | maskPattern;
  1483. var bits = getBCHVersionInfo(data);
  1484. var moduleCount = this.moduleCount;
  1485. for (var i = 0; i < 15; i++) {
  1486. var bit = ((bits >> i) & 1) === 1;
  1487. // Vertical
  1488. if (i < 6) {
  1489. this.modules[i][8] = bit;
  1490. }
  1491. else if (i < 8) {
  1492. this.modules[i + 1][8] = bit;
  1493. }
  1494. else {
  1495. this.modules[moduleCount - 15 + i][8] = bit;
  1496. }
  1497. // Horizontal
  1498. if (i < 8) {
  1499. this.modules[8][moduleCount - i - 1] = bit;
  1500. }
  1501. else if (i < 9) {
  1502. this.modules[8][15 - i - 1 + 1] = bit;
  1503. }
  1504. else {
  1505. this.modules[8][15 - i - 1] = bit;
  1506. }
  1507. }
  1508. // Fixed point
  1509. this.modules[moduleCount - 8][8] = true;
  1510. };
  1511. Encoder.prototype.setupVersionInfo = function () {
  1512. if (this.version >= 7) {
  1513. var moduleCount = this.moduleCount;
  1514. var bits = getBCHVersion(this.version);
  1515. for (var i = 0; i < 18; i++) {
  1516. var bit = ((bits >> i) & 1) === 1;
  1517. this.modules[(i / 3) >> 0][(i % 3) + moduleCount - 8 - 3] = bit;
  1518. this.modules[(i % 3) + moduleCount - 8 - 3][(i / 3) >> 0] = bit;
  1519. }
  1520. }
  1521. };
  1522. Encoder.prototype.setupCodewords = function (data, maskPattern) {
  1523. // Bit index into the data
  1524. var bitIndex = 0;
  1525. var moduleCount = this.moduleCount;
  1526. var bitLength = data.getLengthInBits();
  1527. // Do the funny zigzag scan
  1528. for (var right = moduleCount - 1; right >= 1; right -= 2) {
  1529. // Index of right column in each column pair
  1530. if (right === 6) {
  1531. right = 5;
  1532. }
  1533. for (var vert = 0; vert < moduleCount; vert++) {
  1534. // Vertical counter
  1535. for (var j = 0; j < 2; j++) {
  1536. // Actual x coordinate
  1537. var x = right - j;
  1538. var upward = ((right + 1) & 2) === 0;
  1539. // Actual y coordinate
  1540. var y = upward ? moduleCount - 1 - vert : vert;
  1541. if (this.modules[y][x] !== null) {
  1542. continue;
  1543. }
  1544. var bit = false;
  1545. if (bitIndex < bitLength) {
  1546. bit = data.getBit(bitIndex++);
  1547. }
  1548. var maskFunc = getMaskFunc(maskPattern);
  1549. var invert = maskFunc(x, y);
  1550. if (invert) {
  1551. bit = !bit;
  1552. }
  1553. this.modules[y][x] = bit;
  1554. }
  1555. }
  1556. }
  1557. };
  1558. Encoder.prototype.buildMatrix = function (data, maskPattern) {
  1559. // Initialize modules
  1560. this.modules = [];
  1561. var moduleCount = this.moduleCount;
  1562. for (var row = 0; row < moduleCount; row++) {
  1563. this.modules[row] = [];
  1564. for (var col = 0; col < moduleCount; col++) {
  1565. this.modules[row][col] = null;
  1566. }
  1567. }
  1568. // Setup finder pattern
  1569. this.setupFinderPattern(0, 0);
  1570. this.setupFinderPattern(moduleCount - 7, 0);
  1571. this.setupFinderPattern(0, moduleCount - 7);
  1572. // Setup alignment pattern
  1573. this.setupAlignmentPattern();
  1574. // Setup timing pattern
  1575. this.setupTimingPattern();
  1576. // Setup format info
  1577. this.setupFormatInfo(maskPattern);
  1578. // Setup version info
  1579. this.setupVersionInfo();
  1580. // Setup codewords
  1581. this.setupCodewords(data, maskPattern);
  1582. };
  1583. /**
  1584. * @public
  1585. * @method make
  1586. */
  1587. Encoder.prototype.make = function () {
  1588. var _a, _b;
  1589. var buffer;
  1590. var rsBlocks;
  1591. var maxDataCount;
  1592. var chunks = this.chunks;
  1593. var errorCorrectionLevel = this.errorCorrectionLevel;
  1594. if (this.autoVersion) {
  1595. for (this.version = 1; this.version <= 40; this.version++) {
  1596. _a = prepareData(this.version, errorCorrectionLevel, this.hasEncodingHint, chunks), buffer = _a[0], rsBlocks = _a[1], maxDataCount = _a[2];
  1597. if (buffer.getLengthInBits() <= maxDataCount)
  1598. break;
  1599. }
  1600. }
  1601. else {
  1602. _b = prepareData(this.version, errorCorrectionLevel, this.hasEncodingHint, chunks), buffer = _b[0], rsBlocks = _b[1], maxDataCount = _b[2];
  1603. }
  1604. // Calc module count
  1605. this.moduleCount = this.version * 4 + 17;
  1606. var matrices = [];
  1607. var data = createData(buffer, rsBlocks, maxDataCount);
  1608. var bestMaskPattern = -1;
  1609. var minPenalty = Number.MAX_VALUE;
  1610. // Choose best mask pattern
  1611. for (var maskPattern = 0; maskPattern < 8; maskPattern++) {
  1612. this.buildMatrix(data, maskPattern);
  1613. matrices.push(this.modules);
  1614. var penalty = calculateMaskPenalty(this);
  1615. if (penalty < minPenalty) {
  1616. minPenalty = penalty;
  1617. bestMaskPattern = maskPattern;
  1618. }
  1619. }
  1620. this.modules = matrices[bestMaskPattern];
  1621. };
  1622. /**
  1623. * @public
  1624. * @method toDataURL
  1625. * @param {number} moduleSize
  1626. * @param {number} margin
  1627. * @returns {string}
  1628. */
  1629. Encoder.prototype.toDataURL = function (moduleSize, margin) {
  1630. if (moduleSize === void 0) { moduleSize = 2; }
  1631. if (margin === void 0) { margin = moduleSize * 4; }
  1632. moduleSize = Math.max(1, moduleSize >> 0);
  1633. margin = Math.max(0, margin >> 0);
  1634. var moduleCount = this.moduleCount;
  1635. var size = moduleSize * moduleCount + margin * 2;
  1636. var gif = new GIFImage(size, size);
  1637. for (var y = 0; y < size; y++) {
  1638. for (var x = 0; x < size; x++) {
  1639. if (margin <= x &&
  1640. x < size - margin &&
  1641. margin <= y &&
  1642. y < size - margin &&
  1643. this.isDark(((y - margin) / moduleSize) >> 0, ((x - margin) / moduleSize) >> 0)) {
  1644. gif.setPixel(x, y, 0);
  1645. }
  1646. else {
  1647. gif.setPixel(x, y, 1);
  1648. }
  1649. }
  1650. }
  1651. return gif.toDataURL();
  1652. };
  1653. return Encoder;
  1654. }());
  1655.  
  1656. /**
  1657. * @module locator
  1658. * @author nuintun
  1659. * @author Cosmo Wolfe
  1660. */
  1661. var MIN_QUAD_RATIO = 0.5;
  1662. var MAX_QUAD_RATIO = 1.5;
  1663. var MAX_FINDERPATTERNS_TO_SEARCH = 4;
  1664. function distance(a, b) {
  1665. return Math.sqrt(Math.pow((b.x - a.x), 2) + Math.pow((b.y - a.y), 2));
  1666. }
  1667. function sum(values) {
  1668. return values.reduce(function (a, b) { return a + b; });
  1669. }
  1670. // Takes three finder patterns and organizes them into topLeft, topRight, etc
  1671. function reorderFinderPatterns(pattern1, pattern2, pattern3) {
  1672. var _a, _b, _c, _d;
  1673. // Find distances between pattern centers
  1674. var oneTwoDistance = distance(pattern1, pattern2);
  1675. var twoThreeDistance = distance(pattern2, pattern3);
  1676. var oneThreeDistance = distance(pattern1, pattern3);
  1677. var topLeft;
  1678. var topRight;
  1679. var bottomLeft;
  1680. // Assume one closest to other two is B; A and C will just be guesses at first
  1681. if (twoThreeDistance >= oneTwoDistance && twoThreeDistance >= oneThreeDistance) {
  1682. _a = [pattern2, pattern1, pattern3], bottomLeft = _a[0], topLeft = _a[1], topRight = _a[2];
  1683. }
  1684. else if (oneThreeDistance >= twoThreeDistance && oneThreeDistance >= oneTwoDistance) {
  1685. _b = [pattern1, pattern2, pattern3], bottomLeft = _b[0], topLeft = _b[1], topRight = _b[2];
  1686. }
  1687. else {
  1688. _c = [pattern1, pattern3, pattern2], bottomLeft = _c[0], topLeft = _c[1], topRight = _c[2];
  1689. }
  1690. // Use cross product to figure out whether bottomLeft (A) and topRight (C) are correct or flipped in relation to topLeft (B)
  1691. // This asks whether BC x BA has a positive z component, which is the arrangement we want. If it's negative, then
  1692. // we've got it flipped around and should swap topRight and bottomLeft.
  1693. if ((topRight.x - topLeft.x) * (bottomLeft.y - topLeft.y) - (topRight.y - topLeft.y) * (bottomLeft.x - topLeft.x) < 0) {
  1694. _d = [topRight, bottomLeft], bottomLeft = _d[0], topRight = _d[1];
  1695. }
  1696. return { bottomLeft: bottomLeft, topLeft: topLeft, topRight: topRight };
  1697. }
  1698. // Computes the dimension (number of modules on a side) of the QR Code based on the position of the finder patterns
  1699. function computeDimension(topLeft, topRight, bottomLeft, matrix) {
  1700. var moduleSize = (sum(countBlackWhiteRun(topLeft, bottomLeft, matrix, 5)) / 7 + // Divide by 7 since the ratio is 1:1:3:1:1
  1701. sum(countBlackWhiteRun(topLeft, topRight, matrix, 5)) / 7 +
  1702. sum(countBlackWhiteRun(bottomLeft, topLeft, matrix, 5)) / 7 +
  1703. sum(countBlackWhiteRun(topRight, topLeft, matrix, 5)) / 7) /
  1704. 4;
  1705. if (moduleSize < 1) {
  1706. throw 'invalid module size';
  1707. }
  1708. var topDimension = Math.round(distance(topLeft, topRight) / moduleSize);
  1709. var sideDimension = Math.round(distance(topLeft, bottomLeft) / moduleSize);
  1710. var dimension = Math.floor((topDimension + sideDimension) / 2) + 7;
  1711. switch (dimension % 4) {
  1712. case 0:
  1713. dimension++;
  1714. break;
  1715. case 2:
  1716. dimension--;
  1717. break;
  1718. }
  1719. return { dimension: dimension, moduleSize: moduleSize };
  1720. }
  1721. // Takes an origin point and an end point and counts the sizes of the black white run from the origin towards the end point.
  1722. // Returns an array of elements, representing the pixel size of the black white run.
  1723. // Uses a variant of http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
  1724. function countBlackWhiteRunTowardsPoint(origin, end, matrix, length) {
  1725. var switchPoints = [{ x: Math.floor(origin.x), y: Math.floor(origin.y) }];
  1726. var steep = Math.abs(end.y - origin.y) > Math.abs(end.x - origin.x);
  1727. var fromX;
  1728. var fromY;
  1729. var toX;
  1730. var toY;
  1731. if (steep) {
  1732. fromX = Math.floor(origin.y);
  1733. fromY = Math.floor(origin.x);
  1734. toX = Math.floor(end.y);
  1735. toY = Math.floor(end.x);
  1736. }
  1737. else {
  1738. fromX = Math.floor(origin.x);
  1739. fromY = Math.floor(origin.y);
  1740. toX = Math.floor(end.x);
  1741. toY = Math.floor(end.y);
  1742. }
  1743. var dx = Math.abs(toX - fromX);
  1744. var dy = Math.abs(toY - fromY);
  1745. var error = Math.floor(-dx / 2);
  1746. var xStep = fromX < toX ? 1 : -1;
  1747. var yStep = fromY < toY ? 1 : -1;
  1748. var currentPixel = true;
  1749. // Loop up until x == toX, but not beyond
  1750. for (var x = fromX, y = fromY; x !== toX + xStep; x += xStep) {
  1751. // Does current pixel mean we have moved white to black or vice versa?
  1752. // Scanning black in state 0,2 and white in state 1, so if we find the wrong
  1753. // color, advance to next state or end if we are in state 2 already
  1754. var realX = steep ? y : x;
  1755. var realY = steep ? x : y;
  1756. if (matrix.get(realX, realY) !== currentPixel) {
  1757. currentPixel = !currentPixel;
  1758. switchPoints.push({ x: realX, y: realY });
  1759. if (switchPoints.length === length + 1) {
  1760. break;
  1761. }
  1762. }
  1763. error += dy;
  1764. if (error > 0) {
  1765. if (y === toY) {
  1766. break;
  1767. }
  1768. y += yStep;
  1769. error -= dx;
  1770. }
  1771. }
  1772. var distances = [];
  1773. for (var i = 0; i < length; i++) {
  1774. if (switchPoints[i] && switchPoints[i + 1]) {
  1775. distances.push(distance(switchPoints[i], switchPoints[i + 1]));
  1776. }
  1777. else {
  1778. distances.push(0);
  1779. }
  1780. }
  1781. return distances;
  1782. }
  1783. // Takes an origin point and an end point and counts the sizes of the black white run in the origin point
  1784. // along the line that intersects with the end point. Returns an array of elements, representing the pixel sizes
  1785. // of the black white run. Takes a length which represents the number of switches from black to white to look for.
  1786. function countBlackWhiteRun(origin, end, matrix, length) {
  1787. var _a;
  1788. var rise = end.y - origin.y;
  1789. var run = end.x - origin.x;
  1790. var towardsEnd = countBlackWhiteRunTowardsPoint(origin, end, matrix, Math.ceil(length / 2));
  1791. var awayFromEnd = countBlackWhiteRunTowardsPoint(origin, { x: origin.x - run, y: origin.y - rise }, matrix, Math.ceil(length / 2));
  1792. var middleValue = towardsEnd.shift() + awayFromEnd.shift() - 1; // Substract one so we don't double count a pixel
  1793. return (_a = awayFromEnd.concat(middleValue)).concat.apply(_a, towardsEnd);
  1794. }
  1795. // Takes in a black white run and an array of expected ratios. Returns the average size of the run as well as the "error" -
  1796. // that is the amount the run diverges from the expected ratio
  1797. function scoreBlackWhiteRun(sequence, ratios) {
  1798. var averageSize = sum(sequence) / sum(ratios);
  1799. var error = 0;
  1800. ratios.forEach(function (ratio, i) {
  1801. error += Math.pow((sequence[i] - ratio * averageSize), 2);
  1802. });
  1803. return { averageSize: averageSize, error: error };
  1804. }
  1805. // Takes an X,Y point and an array of sizes and scores the point against those ratios.
  1806. // For example for a finder pattern takes the ratio list of 1:1:3:1:1 and checks horizontal, vertical and diagonal ratios
  1807. // against that.
  1808. function scorePattern(point, ratios, matrix) {
  1809. try {
  1810. var horizontalRun = countBlackWhiteRun(point, { x: -1, y: point.y }, matrix, ratios.length);
  1811. var verticalRun = countBlackWhiteRun(point, { x: point.x, y: -1 }, matrix, ratios.length);
  1812. var topLeftPoint = {
  1813. x: Math.max(0, point.x - point.y) - 1,
  1814. y: Math.max(0, point.y - point.x) - 1
  1815. };
  1816. var topLeftBottomRightRun = countBlackWhiteRun(point, topLeftPoint, matrix, ratios.length);
  1817. var bottomLeftPoint = {
  1818. x: Math.min(matrix.width, point.x + point.y) + 1,
  1819. y: Math.min(matrix.height, point.y + point.x) + 1
  1820. };
  1821. var bottomLeftTopRightRun = countBlackWhiteRun(point, bottomLeftPoint, matrix, ratios.length);
  1822. var horzError = scoreBlackWhiteRun(horizontalRun, ratios);
  1823. var vertError = scoreBlackWhiteRun(verticalRun, ratios);
  1824. var diagDownError = scoreBlackWhiteRun(topLeftBottomRightRun, ratios);
  1825. var diagUpError = scoreBlackWhiteRun(bottomLeftTopRightRun, ratios);
  1826. var ratioError = Math.sqrt(horzError.error * horzError.error +
  1827. vertError.error * vertError.error +
  1828. diagDownError.error * diagDownError.error +
  1829. diagUpError.error * diagUpError.error);
  1830. var avgSize = (horzError.averageSize + vertError.averageSize + diagDownError.averageSize + diagUpError.averageSize) / 4;
  1831. var sizeError = (Math.pow((horzError.averageSize - avgSize), 2) +
  1832. Math.pow((vertError.averageSize - avgSize), 2) +
  1833. Math.pow((diagDownError.averageSize - avgSize), 2) +
  1834. Math.pow((diagUpError.averageSize - avgSize), 2)) /
  1835. avgSize;
  1836. return ratioError + sizeError;
  1837. }
  1838. catch (_a) {
  1839. return Infinity;
  1840. }
  1841. }
  1842. function locate(matrix) {
  1843. var _a;
  1844. var finderPatternQuads = [];
  1845. var alignmentPatternQuads = [];
  1846. var activeFinderPatternQuads = [];
  1847. var activeAlignmentPatternQuads = [];
  1848. var _loop_1 = function (y) {
  1849. var length_1 = 0;
  1850. var lastBit = false;
  1851. var scans = [0, 0, 0, 0, 0];
  1852. var _loop_2 = function (x) {
  1853. var v = matrix.get(x, y);
  1854. if (v === lastBit) {
  1855. length_1++;
  1856. }
  1857. else {
  1858. scans = [scans[1], scans[2], scans[3], scans[4], length_1];
  1859. length_1 = 1;
  1860. lastBit = v;
  1861. // Do the last 5 color changes ~ match the expected ratio for a finder pattern? 1:1:3:1:1 of b:w:b:w:b
  1862. var averageFinderPatternBlocksize = sum(scans) / 7;
  1863. var validFinderPattern = Math.abs(scans[0] - averageFinderPatternBlocksize) < averageFinderPatternBlocksize &&
  1864. Math.abs(scans[1] - averageFinderPatternBlocksize) < averageFinderPatternBlocksize &&
  1865. Math.abs(scans[2] - 3 * averageFinderPatternBlocksize) < 3 * averageFinderPatternBlocksize &&
  1866. Math.abs(scans[3] - averageFinderPatternBlocksize) < averageFinderPatternBlocksize &&
  1867. Math.abs(scans[4] - averageFinderPatternBlocksize) < averageFinderPatternBlocksize &&
  1868. !v; // And make sure the current pixel is white since finder patterns are bordered in white
  1869. // Do the last 3 color changes ~ match the expected ratio for an alignment pattern? 1:1:1 of w:b:w
  1870. var averageAlignmentPatternBlocksize = sum(scans.slice(-3)) / 3;
  1871. var validAlignmentPattern = Math.abs(scans[2] - averageAlignmentPatternBlocksize) < averageAlignmentPatternBlocksize &&
  1872. Math.abs(scans[3] - averageAlignmentPatternBlocksize) < averageAlignmentPatternBlocksize &&
  1873. Math.abs(scans[4] - averageAlignmentPatternBlocksize) < averageAlignmentPatternBlocksize &&
  1874. v; // Is the current pixel black since alignment patterns are bordered in black
  1875. if (validFinderPattern) {
  1876. // Compute the start and end x values of the large center black square
  1877. var endX_1 = x - scans[3] - scans[4];
  1878. var startX_1 = endX_1 - scans[2];
  1879. var line = { startX: startX_1, endX: endX_1, y: y };
  1880. // Is there a quad directly above the current spot? If so, extend it with the new line. Otherwise, create a new quad with
  1881. // that line as the starting point.
  1882. var matchingQuads = activeFinderPatternQuads.filter(function (q) {
  1883. return (startX_1 >= q.bottom.startX && startX_1 <= q.bottom.endX) ||
  1884. (endX_1 >= q.bottom.startX && startX_1 <= q.bottom.endX) ||
  1885. (startX_1 <= q.bottom.startX &&
  1886. endX_1 >= q.bottom.endX &&
  1887. (scans[2] / (q.bottom.endX - q.bottom.startX) < MAX_QUAD_RATIO &&
  1888. scans[2] / (q.bottom.endX - q.bottom.startX) > MIN_QUAD_RATIO));
  1889. });
  1890. if (matchingQuads.length > 0) {
  1891. matchingQuads[0].bottom = line;
  1892. }
  1893. else {
  1894. activeFinderPatternQuads.push({ top: line, bottom: line });
  1895. }
  1896. }
  1897. if (validAlignmentPattern) {
  1898. // Compute the start and end x values of the center black square
  1899. var endX_2 = x - scans[4];
  1900. var startX_2 = endX_2 - scans[3];
  1901. var line = { startX: startX_2, y: y, endX: endX_2 };
  1902. // Is there a quad directly above the current spot? If so, extend it with the new line. Otherwise, create a new quad with
  1903. // that line as the starting point.
  1904. var matchingQuads = activeAlignmentPatternQuads.filter(function (q) {
  1905. return (startX_2 >= q.bottom.startX && startX_2 <= q.bottom.endX) ||
  1906. (endX_2 >= q.bottom.startX && startX_2 <= q.bottom.endX) ||
  1907. (startX_2 <= q.bottom.startX &&
  1908. endX_2 >= q.bottom.endX &&
  1909. (scans[2] / (q.bottom.endX - q.bottom.startX) < MAX_QUAD_RATIO &&
  1910. scans[2] / (q.bottom.endX - q.bottom.startX) > MIN_QUAD_RATIO));
  1911. });
  1912. if (matchingQuads.length > 0) {
  1913. matchingQuads[0].bottom = line;
  1914. }
  1915. else {
  1916. activeAlignmentPatternQuads.push({ top: line, bottom: line });
  1917. }
  1918. }
  1919. }
  1920. };
  1921. for (var x = -1; x <= matrix.width; x++) {
  1922. _loop_2(x);
  1923. }
  1924. finderPatternQuads.push.apply(finderPatternQuads, activeFinderPatternQuads.filter(function (q) { return q.bottom.y !== y && q.bottom.y - q.top.y >= 2; }));
  1925. activeFinderPatternQuads = activeFinderPatternQuads.filter(function (q) { return q.bottom.y === y; });
  1926. alignmentPatternQuads.push.apply(alignmentPatternQuads, activeAlignmentPatternQuads.filter(function (q) { return q.bottom.y !== y; }));
  1927. activeAlignmentPatternQuads = activeAlignmentPatternQuads.filter(function (q) { return q.bottom.y === y; });
  1928. };
  1929. for (var y = 0; y <= matrix.height; y++) {
  1930. _loop_1(y);
  1931. }
  1932. finderPatternQuads.push.apply(finderPatternQuads, activeFinderPatternQuads.filter(function (q) { return q.bottom.y - q.top.y >= 2; }));
  1933. alignmentPatternQuads.push.apply(alignmentPatternQuads, activeAlignmentPatternQuads);
  1934. var finderPatternGroups = finderPatternQuads
  1935. .filter(function (q) { return q.bottom.y - q.top.y >= 2; }) // All quads must be at least 2px tall since the center square is larger than a block
  1936. .map(function (q) {
  1937. // Initial scoring of finder pattern quads by looking at their ratios, not taking into account position
  1938. var x = (q.top.startX + q.top.endX + q.bottom.startX + q.bottom.endX) / 4;
  1939. var y = (q.top.y + q.bottom.y + 1) / 2;
  1940. if (!matrix.get(Math.round(x), Math.round(y))) {
  1941. return;
  1942. }
  1943. var lengths = [q.top.endX - q.top.startX, q.bottom.endX - q.bottom.startX, q.bottom.y - q.top.y + 1];
  1944. var size = sum(lengths) / lengths.length;
  1945. var score = scorePattern({ x: Math.round(x), y: Math.round(y) }, [1, 1, 3, 1, 1], matrix);
  1946. return { score: score, x: x, y: y, size: size };
  1947. })
  1948. .filter(function (q) { return !!q; }) // Filter out any rejected quads from above
  1949. .sort(function (a, b) { return a.score - b.score; })
  1950. // Now take the top finder pattern options and try to find 2 other options with a similar size.
  1951. .map(function (point, i, finderPatterns) {
  1952. if (i > MAX_FINDERPATTERNS_TO_SEARCH) {
  1953. return null;
  1954. }
  1955. var otherPoints = finderPatterns
  1956. .filter(function (p, ii) { return i !== ii; })
  1957. .map(function (p) { return ({ x: p.x, y: p.y, score: p.score + Math.pow((p.size - point.size), 2) / point.size, size: p.size }); })
  1958. .sort(function (a, b) { return a.score - b.score; });
  1959. if (otherPoints.length < 2) {
  1960. return null;
  1961. }
  1962. var score = point.score + otherPoints[0].score + otherPoints[1].score;
  1963. return { points: [point].concat(otherPoints.slice(0, 2)), score: score };
  1964. })
  1965. .filter(function (q) { return !!q; }) // Filter out any rejected finder patterns from above
  1966. .sort(function (a, b) { return a.score - b.score; });
  1967. if (finderPatternGroups.length === 0) {
  1968. return null;
  1969. }
  1970. var _b = reorderFinderPatterns(finderPatternGroups[0].points[0], finderPatternGroups[0].points[1], finderPatternGroups[0].points[2]), topRight = _b.topRight, topLeft = _b.topLeft, bottomLeft = _b.bottomLeft;
  1971. // Now that we've found the three finder patterns we can determine the blockSize and the size of the QR code.
  1972. // We'll use these to help find the alignment pattern but also later when we do the extraction.
  1973. var dimension;
  1974. var moduleSize;
  1975. try {
  1976. (_a = computeDimension(topLeft, topRight, bottomLeft, matrix), dimension = _a.dimension, moduleSize = _a.moduleSize);
  1977. }
  1978. catch (e) {
  1979. return null;
  1980. }
  1981. // Now find the alignment pattern
  1982. var bottomRightFinderPattern = {
  1983. // Best guess at where a bottomRight finder pattern would be
  1984. x: topRight.x - topLeft.x + bottomLeft.x,
  1985. y: topRight.y - topLeft.y + bottomLeft.y
  1986. };
  1987. var modulesBetweenFinderPatterns = (distance(topLeft, bottomLeft) + distance(topLeft, topRight)) / 2 / moduleSize;
  1988. var correctionToTopLeft = 1 - 3 / modulesBetweenFinderPatterns;
  1989. var expectedAlignmentPattern = {
  1990. x: topLeft.x + correctionToTopLeft * (bottomRightFinderPattern.x - topLeft.x),
  1991. y: topLeft.y + correctionToTopLeft * (bottomRightFinderPattern.y - topLeft.y)
  1992. };
  1993. var alignmentPatterns = alignmentPatternQuads
  1994. .map(function (q) {
  1995. var x = (q.top.startX + q.top.endX + q.bottom.startX + q.bottom.endX) / 4;
  1996. var y = (q.top.y + q.bottom.y + 1) / 2;
  1997. if (!matrix.get(Math.floor(x), Math.floor(y))) {
  1998. return;
  1999. }
  2000. var sizeScore = scorePattern({ x: Math.floor(x), y: Math.floor(y) }, [1, 1, 1], matrix);
  2001. var score = sizeScore + distance({ x: x, y: y }, expectedAlignmentPattern);
  2002. return { x: x, y: y, score: score };
  2003. })
  2004. .filter(function (v) { return !!v; })
  2005. .sort(function (a, b) { return a.score - b.score; });
  2006. // If there are less than 15 modules between finder patterns it's a version 1 QR code and as such has no alignmemnt pattern
  2007. // so we can only use our best guess.
  2008. var hasAlignmentPatterns = modulesBetweenFinderPatterns >= 15 && alignmentPatterns.length;
  2009. var alignmentPattern = hasAlignmentPatterns ? alignmentPatterns[0] : expectedAlignmentPattern;
  2010. return {
  2011. dimension: dimension,
  2012. topLeft: { x: topLeft.x, y: topLeft.y },
  2013. topRight: { x: topRight.x, y: topRight.y },
  2014. bottomLeft: { x: bottomLeft.x, y: bottomLeft.y },
  2015. alignmentPattern: { x: alignmentPattern.x, y: alignmentPattern.y }
  2016. };
  2017. }
  2018.  
  2019. /**
  2020. * @module GenericGFPoly
  2021. * @author nuintun
  2022. * @author Cosmo Wolfe
  2023. */
  2024. var GenericGFPoly = /*#__PURE__*/ (function () {
  2025. function GenericGFPoly(field, coefficients) {
  2026. if (coefficients.length === 0) {
  2027. throw 'no coefficients';
  2028. }
  2029. this.field = field;
  2030. var coefficientsLength = coefficients.length;
  2031. if (coefficientsLength > 1 && coefficients[0] === 0) {
  2032. // Leading term must be non-zero for anything except the constant polynomial "0"
  2033. var firstNonZero = 1;
  2034. while (firstNonZero < coefficientsLength && coefficients[firstNonZero] === 0) {
  2035. firstNonZero++;
  2036. }
  2037. if (firstNonZero === coefficientsLength) {
  2038. this.coefficients = field.zero.coefficients;
  2039. }
  2040. else {
  2041. this.coefficients = new Uint8ClampedArray(coefficientsLength - firstNonZero);
  2042. for (var i = 0; i < this.coefficients.length; i++) {
  2043. this.coefficients[i] = coefficients[firstNonZero + i];
  2044. }
  2045. }
  2046. }
  2047. else {
  2048. this.coefficients = coefficients;
  2049. }
  2050. }
  2051. GenericGFPoly.prototype.degree = function () {
  2052. return this.coefficients.length - 1;
  2053. };
  2054. GenericGFPoly.prototype.isZero = function () {
  2055. return this.coefficients[0] === 0;
  2056. };
  2057. GenericGFPoly.prototype.getCoefficient = function (degree) {
  2058. return this.coefficients[this.coefficients.length - 1 - degree];
  2059. };
  2060. GenericGFPoly.prototype.addOrSubtract = function (other) {
  2061. var _a;
  2062. if (this.isZero()) {
  2063. return other;
  2064. }
  2065. if (other.isZero()) {
  2066. return this;
  2067. }
  2068. var smallerCoefficients = this.coefficients;
  2069. var largerCoefficients = other.coefficients;
  2070. if (smallerCoefficients.length > largerCoefficients.length) {
  2071. _a = [largerCoefficients, smallerCoefficients], smallerCoefficients = _a[0], largerCoefficients = _a[1];
  2072. }
  2073. var sumDiff = new Uint8ClampedArray(largerCoefficients.length);
  2074. var lengthDiff = largerCoefficients.length - smallerCoefficients.length;
  2075. for (var i = 0; i < lengthDiff; i++) {
  2076. sumDiff[i] = largerCoefficients[i];
  2077. }
  2078. for (var i = lengthDiff; i < largerCoefficients.length; i++) {
  2079. sumDiff[i] = addOrSubtractGF(smallerCoefficients[i - lengthDiff], largerCoefficients[i]);
  2080. }
  2081. return new GenericGFPoly(this.field, sumDiff);
  2082. };
  2083. GenericGFPoly.prototype.multiply = function (scalar) {
  2084. if (scalar === 0) {
  2085. return this.field.zero;
  2086. }
  2087. if (scalar === 1) {
  2088. return this;
  2089. }
  2090. var size = this.coefficients.length;
  2091. var product = new Uint8ClampedArray(size);
  2092. for (var i = 0; i < size; i++) {
  2093. product[i] = this.field.multiply(this.coefficients[i], scalar);
  2094. }
  2095. return new GenericGFPoly(this.field, product);
  2096. };
  2097. GenericGFPoly.prototype.multiplyPoly = function (other) {
  2098. if (this.isZero() || other.isZero()) {
  2099. return this.field.zero;
  2100. }
  2101. var aCoefficients = this.coefficients;
  2102. var aLength = aCoefficients.length;
  2103. var bCoefficients = other.coefficients;
  2104. var bLength = bCoefficients.length;
  2105. var product = new Uint8ClampedArray(aLength + bLength - 1);
  2106. for (var i = 0; i < aLength; i++) {
  2107. var aCoeff = aCoefficients[i];
  2108. for (var j = 0; j < bLength; j++) {
  2109. product[i + j] = addOrSubtractGF(product[i + j], this.field.multiply(aCoeff, bCoefficients[j]));
  2110. }
  2111. }
  2112. return new GenericGFPoly(this.field, product);
  2113. };
  2114. GenericGFPoly.prototype.multiplyByMonomial = function (degree, coefficient) {
  2115. if (degree < 0) {
  2116. throw 'invalid degree less than 0';
  2117. }
  2118. if (coefficient === 0) {
  2119. return this.field.zero;
  2120. }
  2121. var size = this.coefficients.length;
  2122. var product = new Uint8ClampedArray(size + degree);
  2123. for (var i = 0; i < size; i++) {
  2124. product[i] = this.field.multiply(this.coefficients[i], coefficient);
  2125. }
  2126. return new GenericGFPoly(this.field, product);
  2127. };
  2128. GenericGFPoly.prototype.evaluateAt = function (a) {
  2129. var result = 0;
  2130. if (a === 0) {
  2131. // Just return the x^0 coefficient
  2132. return this.getCoefficient(0);
  2133. }
  2134. var size = this.coefficients.length;
  2135. if (a === 1) {
  2136. // Just the sum of the coefficients
  2137. this.coefficients.forEach(function (coefficient) {
  2138. result = addOrSubtractGF(result, coefficient);
  2139. });
  2140. return result;
  2141. }
  2142. result = this.coefficients[0];
  2143. for (var i = 1; i < size; i++) {
  2144. result = addOrSubtractGF(this.field.multiply(a, result), this.coefficients[i]);
  2145. }
  2146. return result;
  2147. };
  2148. return GenericGFPoly;
  2149. }());
  2150.  
  2151. /**
  2152. * @module GenericGF
  2153. * @author nuintun
  2154. * @author Cosmo Wolfe
  2155. */
  2156. function addOrSubtractGF(a, b) {
  2157. return a ^ b;
  2158. }
  2159. var GenericGF = /*#__PURE__*/ (function () {
  2160. function GenericGF(primitive, size, generatorBase) {
  2161. this.primitive = primitive;
  2162. this.size = size;
  2163. this.generatorBase = generatorBase;
  2164. this.expTable = [];
  2165. this.logTable = [];
  2166. var x = 1;
  2167. for (var i = 0; i < this.size; i++) {
  2168. this.logTable[i] = 0;
  2169. this.expTable[i] = x;
  2170. x = x * 2;
  2171. if (x >= this.size) {
  2172. x = (x ^ this.primitive) & (this.size - 1);
  2173. }
  2174. }
  2175. for (var i = 0; i < this.size - 1; i++) {
  2176. this.logTable[this.expTable[i]] = i;
  2177. }
  2178. this.zero = new GenericGFPoly(this, Uint8ClampedArray.from([0]));
  2179. this.one = new GenericGFPoly(this, Uint8ClampedArray.from([1]));
  2180. }
  2181. GenericGF.prototype.multiply = function (a, b) {
  2182. if (a === 0 || b === 0) {
  2183. return 0;
  2184. }
  2185. return this.expTable[(this.logTable[a] + this.logTable[b]) % (this.size - 1)];
  2186. };
  2187. GenericGF.prototype.inverse = function (a) {
  2188. if (a === 0) {
  2189. throw "can't invert 0";
  2190. }
  2191. return this.expTable[this.size - this.logTable[a] - 1];
  2192. };
  2193. GenericGF.prototype.buildMonomial = function (degree, coefficient) {
  2194. if (degree < 0) {
  2195. throw 'invalid monomial degree less than 0';
  2196. }
  2197. if (coefficient === 0) {
  2198. return this.zero;
  2199. }
  2200. var coefficients = new Uint8ClampedArray(degree + 1);
  2201. coefficients[0] = coefficient;
  2202. return new GenericGFPoly(this, coefficients);
  2203. };
  2204. GenericGF.prototype.log = function (a) {
  2205. if (a === 0) {
  2206. throw "can't take log(0)";
  2207. }
  2208. return this.logTable[a];
  2209. };
  2210. GenericGF.prototype.exp = function (a) {
  2211. return this.expTable[a];
  2212. };
  2213. return GenericGF;
  2214. }());
  2215.  
  2216. /**
  2217. * @module index
  2218. * @author nuintun
  2219. * @author Cosmo Wolfe
  2220. */
  2221. function runEuclideanAlgorithm(field, a, b, R) {
  2222. var _a;
  2223. // Assume a's degree is >= b's
  2224. if (a.degree() < b.degree()) {
  2225. _a = [b, a], a = _a[0], b = _a[1];
  2226. }
  2227. var rLast = a;
  2228. var r = b;
  2229. var tLast = field.zero;
  2230. var t = field.one;
  2231. // Run Euclidean algorithm until r's degree is less than R/2
  2232. while (r.degree() >= R / 2) {
  2233. var rLastLast = rLast;
  2234. var tLastLast = tLast;
  2235. rLast = r;
  2236. tLast = t;
  2237. // Divide rLastLast by rLast, with quotient in q and remainder in r
  2238. if (rLast.isZero()) {
  2239. // Euclidean algorithm already terminated?
  2240. return null;
  2241. }
  2242. r = rLastLast;
  2243. var q = field.zero;
  2244. var denominatorLeadingTerm = rLast.getCoefficient(rLast.degree());
  2245. var dltInverse = field.inverse(denominatorLeadingTerm);
  2246. while (r.degree() >= rLast.degree() && !r.isZero()) {
  2247. var degreeDiff = r.degree() - rLast.degree();
  2248. var scale = field.multiply(r.getCoefficient(r.degree()), dltInverse);
  2249. q = q.addOrSubtract(field.buildMonomial(degreeDiff, scale));
  2250. r = r.addOrSubtract(rLast.multiplyByMonomial(degreeDiff, scale));
  2251. }
  2252. t = q.multiplyPoly(tLast).addOrSubtract(tLastLast);
  2253. if (r.degree() >= rLast.degree()) {
  2254. return null;
  2255. }
  2256. }
  2257. var sigmaTildeAtZero = t.getCoefficient(0);
  2258. if (sigmaTildeAtZero === 0) {
  2259. return null;
  2260. }
  2261. var inverse = field.inverse(sigmaTildeAtZero);
  2262. return [t.multiply(inverse), r.multiply(inverse)];
  2263. }
  2264. function findErrorLocations(field, errorLocator) {
  2265. // This is a direct application of Chien's search
  2266. var numErrors = errorLocator.degree();
  2267. if (numErrors === 1) {
  2268. return [errorLocator.getCoefficient(1)];
  2269. }
  2270. var errorCount = 0;
  2271. var result = new Array(numErrors);
  2272. for (var i = 1; i < field.size && errorCount < numErrors; i++) {
  2273. if (errorLocator.evaluateAt(i) === 0) {
  2274. result[errorCount] = field.inverse(i);
  2275. errorCount++;
  2276. }
  2277. }
  2278. if (errorCount !== numErrors) {
  2279. return null;
  2280. }
  2281. return result;
  2282. }
  2283. function findErrorMagnitudes(field, errorEvaluator, errorLocations) {
  2284. // This is directly applying Forney's Formula
  2285. var s = errorLocations.length;
  2286. var result = new Array(s);
  2287. for (var i = 0; i < s; i++) {
  2288. var denominator = 1;
  2289. var xiInverse = field.inverse(errorLocations[i]);
  2290. for (var j = 0; j < s; j++) {
  2291. if (i !== j) {
  2292. denominator = field.multiply(denominator, addOrSubtractGF(1, field.multiply(errorLocations[j], xiInverse)));
  2293. }
  2294. }
  2295. result[i] = field.multiply(errorEvaluator.evaluateAt(xiInverse), field.inverse(denominator));
  2296. if (field.generatorBase !== 0) {
  2297. result[i] = field.multiply(result[i], xiInverse);
  2298. }
  2299. }
  2300. return result;
  2301. }
  2302. function rsDecode(bytes, twoS) {
  2303. var outputBytes = new Uint8ClampedArray(bytes.length);
  2304. outputBytes.set(bytes);
  2305. var field = new GenericGF(0x011d, 256, 0); // x^8 + x^4 + x^3 + x^2 + 1
  2306. var poly = new GenericGFPoly(field, outputBytes);
  2307. var syndromeCoefficients = new Uint8ClampedArray(twoS);
  2308. var error = false;
  2309. for (var s = 0; s < twoS; s++) {
  2310. var evaluation = poly.evaluateAt(field.exp(s + field.generatorBase));
  2311. syndromeCoefficients[syndromeCoefficients.length - 1 - s] = evaluation;
  2312. if (evaluation !== 0) {
  2313. error = true;
  2314. }
  2315. }
  2316. if (!error) {
  2317. return outputBytes;
  2318. }
  2319. var syndrome = new GenericGFPoly(field, syndromeCoefficients);
  2320. var sigmaOmega = runEuclideanAlgorithm(field, field.buildMonomial(twoS, 1), syndrome, twoS);
  2321. if (sigmaOmega === null) {
  2322. return null;
  2323. }
  2324. var errorLocations = findErrorLocations(field, sigmaOmega[0]);
  2325. if (errorLocations == null) {
  2326. return null;
  2327. }
  2328. var errorMagnitudes = findErrorMagnitudes(field, sigmaOmega[1], errorLocations);
  2329. for (var i = 0; i < errorLocations.length; i++) {
  2330. var position = outputBytes.length - 1 - field.log(errorLocations[i]);
  2331. if (position < 0) {
  2332. return null;
  2333. }
  2334. outputBytes[position] = addOrSubtractGF(outputBytes[position], errorMagnitudes[i]);
  2335. }
  2336. return outputBytes;
  2337. }
  2338.  
  2339. /**
  2340. * @module BitMatrix
  2341. * @author nuintun
  2342. * @author Cosmo Wolfe
  2343. */
  2344. var BitMatrix = /*#__PURE__*/ (function () {
  2345. function BitMatrix(data, width) {
  2346. this.data = data;
  2347. this.width = width;
  2348. this.height = data.length / width;
  2349. }
  2350. BitMatrix.createEmpty = function (width, height) {
  2351. return new BitMatrix(new Uint8ClampedArray(width * height), width);
  2352. };
  2353. BitMatrix.prototype.get = function (x, y) {
  2354. if (x < 0 || x >= this.width || y < 0 || y >= this.height) {
  2355. return false;
  2356. }
  2357. return !!this.data[y * this.width + x];
  2358. };
  2359. BitMatrix.prototype.set = function (x, y, v) {
  2360. this.data[y * this.width + x] = v ? 1 : 0;
  2361. };
  2362. BitMatrix.prototype.setRegion = function (left, top, width, height, v) {
  2363. for (var y = top; y < top + height; y++) {
  2364. for (var x = left; x < left + width; x++) {
  2365. this.set(x, y, !!v);
  2366. }
  2367. }
  2368. };
  2369. return BitMatrix;
  2370. }());
  2371.  
  2372. /**
  2373. * @module BitStream
  2374. * @author nuintun
  2375. * @author Cosmo Wolfe
  2376. */
  2377. var BitStream = /*#__PURE__*/ (function () {
  2378. function BitStream(bytes) {
  2379. this.byteOffset = 0;
  2380. this.bitOffset = 0;
  2381. this.bytes = bytes;
  2382. }
  2383. BitStream.prototype.readBits = function (numBits) {
  2384. if (numBits < 1 || numBits > 32 || numBits > this.available()) {
  2385. throw "can't read " + numBits + " bits";
  2386. }
  2387. var result = 0;
  2388. // First, read remainder from current byte
  2389. if (this.bitOffset > 0) {
  2390. var bitsLeft = 8 - this.bitOffset;
  2391. var toRead = numBits < bitsLeft ? numBits : bitsLeft;
  2392. var bitsToNotRead = bitsLeft - toRead;
  2393. var mask = (0xff >> (8 - toRead)) << bitsToNotRead;
  2394. result = (this.bytes[this.byteOffset] & mask) >> bitsToNotRead;
  2395. numBits -= toRead;
  2396. this.bitOffset += toRead;
  2397. if (this.bitOffset === 8) {
  2398. this.bitOffset = 0;
  2399. this.byteOffset++;
  2400. }
  2401. }
  2402. // Next read whole bytes
  2403. if (numBits > 0) {
  2404. while (numBits >= 8) {
  2405. result = (result << 8) | (this.bytes[this.byteOffset] & 0xff);
  2406. this.byteOffset++;
  2407. numBits -= 8;
  2408. }
  2409. // Finally read a partial byte
  2410. if (numBits > 0) {
  2411. var bitsToNotRead = 8 - numBits;
  2412. var mask = (0xff >> bitsToNotRead) << bitsToNotRead;
  2413. result = (result << numBits) | ((this.bytes[this.byteOffset] & mask) >> bitsToNotRead);
  2414. this.bitOffset += numBits;
  2415. }
  2416. }
  2417. return result;
  2418. };
  2419. BitStream.prototype.available = function () {
  2420. return 8 * (this.bytes.length - this.byteOffset) - this.bitOffset;
  2421. };
  2422. return BitStream;
  2423. }());
  2424.  
  2425. /**
  2426. * @module SJIS
  2427. * @author nuintun
  2428. * @author soldair
  2429. * @author Kazuhiko Arase
  2430. * @see https://github.com/soldair/node-qrcode/blob/master/helper/to-sjis.js
  2431. */
  2432. // prettier-ignore
  2433. var SJIS_UTF8_TABLE = [
  2434. [0x8140, ' 、。,.・:;?!゛゜´`¨^ ̄_ヽヾゝゞ〃仝々〆〇ー―‐/\~∥|…‥‘’“”()〔〕[]{}〈〉《》「」『』【】+-±×'],
  2435. [0x8180, '÷=≠<>'],
  2436. [0x818f, '¥$¢£%#&*@§☆★'],
  2437. [0x81a6, '※〒→←↑↓〓'],
  2438. [0x81ca, '¬'],
  2439. [0x824f, '0123456789'],
  2440. [0x8260, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'],
  2441. [0x8281, 'abcdefghijklmnopqrstuvwxyz'],
  2442. [0x829f, 'ぁあぃいぅうぇえぉおかがきぎくぐけげこごさざしじすずせぜそぞただちぢっつづてでとどなにぬねのはばぱひびぴふぶぷへべぺほぼぽまみむめもゃやゅゆょよらりるれろゎわゐゑをん'],
  2443. [0x8340, 'ァアィイゥウェエォオカガキギクグケゲコゴサザシジスズセゼソゾタダチヂッツヅテデトドナニヌネノハバパヒビピフブプヘベペホボポマミ'],
  2444. [0x8380, 'ムメモャヤュユョヨラリルレロヮワヰヱヲンヴヵヶ'],
  2445. [0x839f, 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ'],
  2446. [0x83bf, 'αβγδεζηθικλμνξοπρστυφχψω'],
  2447. [0x8440, 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'],
  2448. [0x8470, 'абвгдеёжзийклмн'],
  2449. [0x8480, 'опрстуфхцчшщъыьэюя'],
  2450. [0x8780, '〝〟'],
  2451. [0x8940, '院陰隠韻吋右宇烏羽迂雨卯鵜窺丑碓臼渦嘘唄欝蔚鰻姥厩浦瓜閏噂云運雲荏餌叡営嬰影映曳栄永泳洩瑛盈穎頴英衛詠鋭液疫益駅悦謁越閲榎厭円'],
  2452. [0x8980, '園堰奄宴延怨掩援沿演炎焔煙燕猿縁艶苑薗遠鉛鴛塩於汚甥凹央奥往応押旺横欧殴王翁襖鴬鴎黄岡沖荻億屋憶臆桶牡乙俺卸恩温穏音下化仮何伽価佳加可嘉夏嫁家寡科暇果架歌河火珂禍禾稼箇花苛茄荷華菓蝦課嘩貨迦過霞蚊俄峨我牙画臥芽蛾賀雅餓駕介会解回塊壊廻快怪悔恢懐戒拐改'],
  2453. [0x8a40, '魁晦械海灰界皆絵芥蟹開階貝凱劾外咳害崖慨概涯碍蓋街該鎧骸浬馨蛙垣柿蛎鈎劃嚇各廓拡撹格核殻獲確穫覚角赫較郭閣隔革学岳楽額顎掛笠樫'],
  2454. [0x8a80, '橿梶鰍潟割喝恰括活渇滑葛褐轄且鰹叶椛樺鞄株兜竃蒲釜鎌噛鴨栢茅萱粥刈苅瓦乾侃冠寒刊勘勧巻喚堪姦完官寛干幹患感慣憾換敢柑桓棺款歓汗漢澗潅環甘監看竿管簡緩缶翰肝艦莞観諌貫還鑑間閑関陥韓館舘丸含岸巌玩癌眼岩翫贋雁頑顔願企伎危喜器基奇嬉寄岐希幾忌揮机旗既期棋棄'],
  2455. [0x8b40, '機帰毅気汽畿祈季稀紀徽規記貴起軌輝飢騎鬼亀偽儀妓宜戯技擬欺犠疑祇義蟻誼議掬菊鞠吉吃喫桔橘詰砧杵黍却客脚虐逆丘久仇休及吸宮弓急救'],
  2456. [0x8b80, '朽求汲泣灸球究窮笈級糾給旧牛去居巨拒拠挙渠虚許距鋸漁禦魚亨享京供侠僑兇競共凶協匡卿叫喬境峡強彊怯恐恭挟教橋況狂狭矯胸脅興蕎郷鏡響饗驚仰凝尭暁業局曲極玉桐粁僅勤均巾錦斤欣欽琴禁禽筋緊芹菌衿襟謹近金吟銀九倶句区狗玖矩苦躯駆駈駒具愚虞喰空偶寓遇隅串櫛釧屑屈'],
  2457. [0x8c40, '掘窟沓靴轡窪熊隈粂栗繰桑鍬勲君薫訓群軍郡卦袈祁係傾刑兄啓圭珪型契形径恵慶慧憩掲携敬景桂渓畦稽系経継繋罫茎荊蛍計詣警軽頚鶏芸迎鯨'],
  2458. [0x8c80, '劇戟撃激隙桁傑欠決潔穴結血訣月件倹倦健兼券剣喧圏堅嫌建憲懸拳捲検権牽犬献研硯絹県肩見謙賢軒遣鍵険顕験鹸元原厳幻弦減源玄現絃舷言諺限乎個古呼固姑孤己庫弧戸故枯湖狐糊袴股胡菰虎誇跨鈷雇顧鼓五互伍午呉吾娯後御悟梧檎瑚碁語誤護醐乞鯉交佼侯候倖光公功効勾厚口向'],
  2459. [0x8d40, '后喉坑垢好孔孝宏工巧巷幸広庚康弘恒慌抗拘控攻昂晃更杭校梗構江洪浩港溝甲皇硬稿糠紅紘絞綱耕考肯肱腔膏航荒行衡講貢購郊酵鉱砿鋼閤降'],
  2460. [0x8d80, '項香高鴻剛劫号合壕拷濠豪轟麹克刻告国穀酷鵠黒獄漉腰甑忽惚骨狛込此頃今困坤墾婚恨懇昏昆根梱混痕紺艮魂些佐叉唆嵯左差査沙瑳砂詐鎖裟坐座挫債催再最哉塞妻宰彩才採栽歳済災采犀砕砦祭斎細菜裁載際剤在材罪財冴坂阪堺榊肴咲崎埼碕鷺作削咋搾昨朔柵窄策索錯桜鮭笹匙冊刷'],
  2461. [0x8e40, '察拶撮擦札殺薩雑皐鯖捌錆鮫皿晒三傘参山惨撒散桟燦珊産算纂蚕讃賛酸餐斬暫残仕仔伺使刺司史嗣四士始姉姿子屍市師志思指支孜斯施旨枝止'],
  2462. [0x8e80, '死氏獅祉私糸紙紫肢脂至視詞詩試誌諮資賜雌飼歯事似侍児字寺慈持時次滋治爾璽痔磁示而耳自蒔辞汐鹿式識鴫竺軸宍雫七叱執失嫉室悉湿漆疾質実蔀篠偲柴芝屡蕊縞舎写射捨赦斜煮社紗者謝車遮蛇邪借勺尺杓灼爵酌釈錫若寂弱惹主取守手朱殊狩珠種腫趣酒首儒受呪寿授樹綬需囚収周'],
  2463. [0x8f40, '宗就州修愁拾洲秀秋終繍習臭舟蒐衆襲讐蹴輯週酋酬集醜什住充十従戎柔汁渋獣縦重銃叔夙宿淑祝縮粛塾熟出術述俊峻春瞬竣舜駿准循旬楯殉淳'],
  2464. [0x8f80, '準潤盾純巡遵醇順処初所暑曙渚庶緒署書薯藷諸助叙女序徐恕鋤除傷償勝匠升召哨商唱嘗奨妾娼宵将小少尚庄床廠彰承抄招掌捷昇昌昭晶松梢樟樵沼消渉湘焼焦照症省硝礁祥称章笑粧紹肖菖蒋蕉衝裳訟証詔詳象賞醤鉦鍾鐘障鞘上丈丞乗冗剰城場壌嬢常情擾条杖浄状畳穣蒸譲醸錠嘱埴飾'],
  2465. [0x9040, '拭植殖燭織職色触食蝕辱尻伸信侵唇娠寝審心慎振新晋森榛浸深申疹真神秦紳臣芯薪親診身辛進針震人仁刃塵壬尋甚尽腎訊迅陣靭笥諏須酢図厨'],
  2466. [0x9080, '逗吹垂帥推水炊睡粋翠衰遂酔錐錘随瑞髄崇嵩数枢趨雛据杉椙菅頗雀裾澄摺寸世瀬畝是凄制勢姓征性成政整星晴棲栖正清牲生盛精聖声製西誠誓請逝醒青静斉税脆隻席惜戚斥昔析石積籍績脊責赤跡蹟碩切拙接摂折設窃節説雪絶舌蝉仙先千占宣専尖川戦扇撰栓栴泉浅洗染潜煎煽旋穿箭線'],
  2467. [0x9140, '繊羨腺舛船薦詮賎践選遷銭銑閃鮮前善漸然全禅繕膳糎噌塑岨措曾曽楚狙疏疎礎祖租粗素組蘇訴阻遡鼠僧創双叢倉喪壮奏爽宋層匝惣想捜掃挿掻'],
  2468. [0x9180, '操早曹巣槍槽漕燥争痩相窓糟総綜聡草荘葬蒼藻装走送遭鎗霜騒像増憎臓蔵贈造促側則即息捉束測足速俗属賊族続卒袖其揃存孫尊損村遜他多太汰詑唾堕妥惰打柁舵楕陀駄騨体堆対耐岱帯待怠態戴替泰滞胎腿苔袋貸退逮隊黛鯛代台大第醍題鷹滝瀧卓啄宅托択拓沢濯琢託鐸濁諾茸凧蛸只'],
  2469. [0x9240, '叩但達辰奪脱巽竪辿棚谷狸鱈樽誰丹単嘆坦担探旦歎淡湛炭短端箪綻耽胆蛋誕鍛団壇弾断暖檀段男談値知地弛恥智池痴稚置致蜘遅馳築畜竹筑蓄'],
  2470. [0x9280, '逐秩窒茶嫡着中仲宙忠抽昼柱注虫衷註酎鋳駐樗瀦猪苧著貯丁兆凋喋寵帖帳庁弔張彫徴懲挑暢朝潮牒町眺聴脹腸蝶調諜超跳銚長頂鳥勅捗直朕沈珍賃鎮陳津墜椎槌追鎚痛通塚栂掴槻佃漬柘辻蔦綴鍔椿潰坪壷嬬紬爪吊釣鶴亭低停偵剃貞呈堤定帝底庭廷弟悌抵挺提梯汀碇禎程締艇訂諦蹄逓'],
  2471. [0x9340, '邸鄭釘鼎泥摘擢敵滴的笛適鏑溺哲徹撤轍迭鉄典填天展店添纏甜貼転顛点伝殿澱田電兎吐堵塗妬屠徒斗杜渡登菟賭途都鍍砥砺努度土奴怒倒党冬'],
  2472. [0x9380, '凍刀唐塔塘套宕島嶋悼投搭東桃梼棟盗淘湯涛灯燈当痘祷等答筒糖統到董蕩藤討謄豆踏逃透鐙陶頭騰闘働動同堂導憧撞洞瞳童胴萄道銅峠鴇匿得徳涜特督禿篤毒独読栃橡凸突椴届鳶苫寅酉瀞噸屯惇敦沌豚遁頓呑曇鈍奈那内乍凪薙謎灘捺鍋楢馴縄畷南楠軟難汝二尼弐迩匂賑肉虹廿日乳入'],
  2473. [0x9440, '如尿韮任妊忍認濡禰祢寧葱猫熱年念捻撚燃粘乃廼之埜嚢悩濃納能脳膿農覗蚤巴把播覇杷波派琶破婆罵芭馬俳廃拝排敗杯盃牌背肺輩配倍培媒梅'],
  2474. [0x9480, '楳煤狽買売賠陪這蝿秤矧萩伯剥博拍柏泊白箔粕舶薄迫曝漠爆縛莫駁麦函箱硲箸肇筈櫨幡肌畑畠八鉢溌発醗髪伐罰抜筏閥鳩噺塙蛤隼伴判半反叛帆搬斑板氾汎版犯班畔繁般藩販範釆煩頒飯挽晩番盤磐蕃蛮匪卑否妃庇彼悲扉批披斐比泌疲皮碑秘緋罷肥被誹費避非飛樋簸備尾微枇毘琵眉美'],
  2475. [0x9540, '鼻柊稗匹疋髭彦膝菱肘弼必畢筆逼桧姫媛紐百謬俵彪標氷漂瓢票表評豹廟描病秒苗錨鋲蒜蛭鰭品彬斌浜瀕貧賓頻敏瓶不付埠夫婦富冨布府怖扶敷'],
  2476. [0x9580, '斧普浮父符腐膚芙譜負賦赴阜附侮撫武舞葡蕪部封楓風葺蕗伏副復幅服福腹複覆淵弗払沸仏物鮒分吻噴墳憤扮焚奮粉糞紛雰文聞丙併兵塀幣平弊柄並蔽閉陛米頁僻壁癖碧別瞥蔑箆偏変片篇編辺返遍便勉娩弁鞭保舗鋪圃捕歩甫補輔穂募墓慕戊暮母簿菩倣俸包呆報奉宝峰峯崩庖抱捧放方朋'],
  2477. [0x9640, '法泡烹砲縫胞芳萌蓬蜂褒訪豊邦鋒飽鳳鵬乏亡傍剖坊妨帽忘忙房暴望某棒冒紡肪膨謀貌貿鉾防吠頬北僕卜墨撲朴牧睦穆釦勃没殆堀幌奔本翻凡盆'],
  2478. [0x9680, '摩磨魔麻埋妹昧枚毎哩槙幕膜枕鮪柾鱒桝亦俣又抹末沫迄侭繭麿万慢満漫蔓味未魅巳箕岬密蜜湊蓑稔脈妙粍民眠務夢無牟矛霧鵡椋婿娘冥名命明盟迷銘鳴姪牝滅免棉綿緬面麺摸模茂妄孟毛猛盲網耗蒙儲木黙目杢勿餅尤戻籾貰問悶紋門匁也冶夜爺耶野弥矢厄役約薬訳躍靖柳薮鑓愉愈油癒'],
  2479. [0x9740, '諭輸唯佑優勇友宥幽悠憂揖有柚湧涌猶猷由祐裕誘遊邑郵雄融夕予余与誉輿預傭幼妖容庸揚揺擁曜楊様洋溶熔用窯羊耀葉蓉要謡踊遥陽養慾抑欲'],
  2480. [0x9780, '沃浴翌翼淀羅螺裸来莱頼雷洛絡落酪乱卵嵐欄濫藍蘭覧利吏履李梨理璃痢裏裡里離陸律率立葎掠略劉流溜琉留硫粒隆竜龍侶慮旅虜了亮僚両凌寮料梁涼猟療瞭稜糧良諒遼量陵領力緑倫厘林淋燐琳臨輪隣鱗麟瑠塁涙累類令伶例冷励嶺怜玲礼苓鈴隷零霊麗齢暦歴列劣烈裂廉恋憐漣煉簾練聯'],
  2481. [0x9840, '蓮連錬呂魯櫓炉賂路露労婁廊弄朗楼榔浪漏牢狼篭老聾蝋郎六麓禄肋録論倭和話歪賄脇惑枠鷲亙亘鰐詫藁蕨椀湾碗腕'],
  2482. [0x989f, '弌丐丕个丱丶丼丿乂乖乘亂亅豫亊舒弍于亞亟亠亢亰亳亶从仍仄仆仂仗仞仭仟价伉佚估佛佝佗佇佶侈侏侘佻佩佰侑佯來侖儘俔俟俎俘俛俑俚俐俤俥倚倨倔倪倥倅伜俶倡倩倬俾俯們倆偃假會偕偐偈做偖偬偸傀傚傅傴傲'],
  2483. [0x9940, '僉僊傳僂僖僞僥僭僣僮價僵儉儁儂儖儕儔儚儡儺儷儼儻儿兀兒兌兔兢竸兩兪兮冀冂囘册冉冏冑冓冕冖冤冦冢冩冪冫决冱冲冰况冽凅凉凛几處凩凭'],
  2484. [0x9980, '凰凵凾刄刋刔刎刧刪刮刳刹剏剄剋剌剞剔剪剴剩剳剿剽劍劔劒剱劈劑辨辧劬劭劼劵勁勍勗勞勣勦飭勠勳勵勸勹匆匈甸匍匐匏匕匚匣匯匱匳匸區卆卅丗卉卍凖卞卩卮夘卻卷厂厖厠厦厥厮厰厶參簒雙叟曼燮叮叨叭叺吁吽呀听吭吼吮吶吩吝呎咏呵咎呟呱呷呰咒呻咀呶咄咐咆哇咢咸咥咬哄哈咨'],
  2485. [0x9a40, '咫哂咤咾咼哘哥哦唏唔哽哮哭哺哢唹啀啣啌售啜啅啖啗唸唳啝喙喀咯喊喟啻啾喘喞單啼喃喩喇喨嗚嗅嗟嗄嗜嗤嗔嘔嗷嘖嗾嗽嘛嗹噎噐營嘴嘶嘲嘸'],
  2486. [0x9a80, '噫噤嘯噬噪嚆嚀嚊嚠嚔嚏嚥嚮嚶嚴囂嚼囁囃囀囈囎囑囓囗囮囹圀囿圄圉圈國圍圓團圖嗇圜圦圷圸坎圻址坏坩埀垈坡坿垉垓垠垳垤垪垰埃埆埔埒埓堊埖埣堋堙堝塲堡塢塋塰毀塒堽塹墅墹墟墫墺壞墻墸墮壅壓壑壗壙壘壥壜壤壟壯壺壹壻壼壽夂夊夐夛梦夥夬夭夲夸夾竒奕奐奎奚奘奢奠奧奬奩'],
  2487. [0x9b40, '奸妁妝佞侫妣妲姆姨姜妍姙姚娥娟娑娜娉娚婀婬婉娵娶婢婪媚媼媾嫋嫂媽嫣嫗嫦嫩嫖嫺嫻嬌嬋嬖嬲嫐嬪嬶嬾孃孅孀孑孕孚孛孥孩孰孳孵學斈孺宀'],
  2488. [0x9b80, '它宦宸寃寇寉寔寐寤實寢寞寥寫寰寶寳尅將專對尓尠尢尨尸尹屁屆屎屓屐屏孱屬屮乢屶屹岌岑岔妛岫岻岶岼岷峅岾峇峙峩峽峺峭嶌峪崋崕崗嵜崟崛崑崔崢崚崙崘嵌嵒嵎嵋嵬嵳嵶嶇嶄嶂嶢嶝嶬嶮嶽嶐嶷嶼巉巍巓巒巖巛巫已巵帋帚帙帑帛帶帷幄幃幀幎幗幔幟幢幤幇幵并幺麼广庠廁廂廈廐廏'],
  2489. [0x9c40, '廖廣廝廚廛廢廡廨廩廬廱廳廰廴廸廾弃弉彝彜弋弑弖弩弭弸彁彈彌彎弯彑彖彗彙彡彭彳彷徃徂彿徊很徑徇從徙徘徠徨徭徼忖忻忤忸忱忝悳忿怡恠'],
  2490. [0x9c80, '怙怐怩怎怱怛怕怫怦怏怺恚恁恪恷恟恊恆恍恣恃恤恂恬恫恙悁悍惧悃悚悄悛悖悗悒悧悋惡悸惠惓悴忰悽惆悵惘慍愕愆惶惷愀惴惺愃愡惻惱愍愎慇愾愨愧慊愿愼愬愴愽慂慄慳慷慘慙慚慫慴慯慥慱慟慝慓慵憙憖憇憬憔憚憊憑憫憮懌懊應懷懈懃懆憺懋罹懍懦懣懶懺懴懿懽懼懾戀戈戉戍戌戔戛'],
  2491. [0x9d40, '戞戡截戮戰戲戳扁扎扞扣扛扠扨扼抂抉找抒抓抖拔抃抔拗拑抻拏拿拆擔拈拜拌拊拂拇抛拉挌拮拱挧挂挈拯拵捐挾捍搜捏掖掎掀掫捶掣掏掉掟掵捫'],
  2492. [0x9d80, '捩掾揩揀揆揣揉插揶揄搖搴搆搓搦搶攝搗搨搏摧摯摶摎攪撕撓撥撩撈撼據擒擅擇撻擘擂擱擧舉擠擡抬擣擯攬擶擴擲擺攀擽攘攜攅攤攣攫攴攵攷收攸畋效敖敕敍敘敞敝敲數斂斃變斛斟斫斷旃旆旁旄旌旒旛旙无旡旱杲昊昃旻杳昵昶昴昜晏晄晉晁晞晝晤晧晨晟晢晰暃暈暎暉暄暘暝曁暹曉暾暼'],
  2493. [0x9e40, '曄暸曖曚曠昿曦曩曰曵曷朏朖朞朦朧霸朮朿朶杁朸朷杆杞杠杙杣杤枉杰枩杼杪枌枋枦枡枅枷柯枴柬枳柩枸柤柞柝柢柮枹柎柆柧檜栞框栩桀桍栲桎'],
  2494. [0x9e80, '梳栫桙档桷桿梟梏梭梔條梛梃檮梹桴梵梠梺椏梍桾椁棊椈棘椢椦棡椌棍棔棧棕椶椒椄棗棣椥棹棠棯椨椪椚椣椡棆楹楷楜楸楫楔楾楮椹楴椽楙椰楡楞楝榁楪榲榮槐榿槁槓榾槎寨槊槝榻槃榧樮榑榠榜榕榴槞槨樂樛槿權槹槲槧樅榱樞槭樔槫樊樒櫁樣樓橄樌橲樶橸橇橢橙橦橈樸樢檐檍檠檄檢檣'],
  2495. [0x9f40, '檗蘗檻櫃櫂檸檳檬櫞櫑櫟檪櫚櫪櫻欅蘖櫺欒欖鬱欟欸欷盜欹飮歇歃歉歐歙歔歛歟歡歸歹歿殀殄殃殍殘殕殞殤殪殫殯殲殱殳殷殼毆毋毓毟毬毫毳毯'],
  2496. [0x9f80, '麾氈氓气氛氤氣汞汕汢汪沂沍沚沁沛汾汨汳沒沐泄泱泓沽泗泅泝沮沱沾沺泛泯泙泪洟衍洶洫洽洸洙洵洳洒洌浣涓浤浚浹浙涎涕濤涅淹渕渊涵淇淦涸淆淬淞淌淨淒淅淺淙淤淕淪淮渭湮渮渙湲湟渾渣湫渫湶湍渟湃渺湎渤滿渝游溂溪溘滉溷滓溽溯滄溲滔滕溏溥滂溟潁漑灌滬滸滾漿滲漱滯漲滌'],
  2497. [0xe040, '漾漓滷澆潺潸澁澀潯潛濳潭澂潼潘澎澑濂潦澳澣澡澤澹濆澪濟濕濬濔濘濱濮濛瀉瀋濺瀑瀁瀏濾瀛瀚潴瀝瀘瀟瀰瀾瀲灑灣炙炒炯烱炬炸炳炮烟烋烝'],
  2498. [0xe080, '烙焉烽焜焙煥煕熈煦煢煌煖煬熏燻熄熕熨熬燗熹熾燒燉燔燎燠燬燧燵燼燹燿爍爐爛爨爭爬爰爲爻爼爿牀牆牋牘牴牾犂犁犇犒犖犢犧犹犲狃狆狄狎狒狢狠狡狹狷倏猗猊猜猖猝猴猯猩猥猾獎獏默獗獪獨獰獸獵獻獺珈玳珎玻珀珥珮珞璢琅瑯琥珸琲琺瑕琿瑟瑙瑁瑜瑩瑰瑣瑪瑶瑾璋璞璧瓊瓏瓔珱'],
  2499. [0xe140, '瓠瓣瓧瓩瓮瓲瓰瓱瓸瓷甄甃甅甌甎甍甕甓甞甦甬甼畄畍畊畉畛畆畚畩畤畧畫畭畸當疆疇畴疊疉疂疔疚疝疥疣痂疳痃疵疽疸疼疱痍痊痒痙痣痞痾痿'],
  2500. [0xe180, '痼瘁痰痺痲痳瘋瘍瘉瘟瘧瘠瘡瘢瘤瘴瘰瘻癇癈癆癜癘癡癢癨癩癪癧癬癰癲癶癸發皀皃皈皋皎皖皓皙皚皰皴皸皹皺盂盍盖盒盞盡盥盧盪蘯盻眈眇眄眩眤眞眥眦眛眷眸睇睚睨睫睛睥睿睾睹瞎瞋瞑瞠瞞瞰瞶瞹瞿瞼瞽瞻矇矍矗矚矜矣矮矼砌砒礦砠礪硅碎硴碆硼碚碌碣碵碪碯磑磆磋磔碾碼磅磊磬'],
  2501. [0xe240, '磧磚磽磴礇礒礑礙礬礫祀祠祗祟祚祕祓祺祿禊禝禧齋禪禮禳禹禺秉秕秧秬秡秣稈稍稘稙稠稟禀稱稻稾稷穃穗穉穡穢穩龝穰穹穽窈窗窕窘窖窩竈窰'],
  2502. [0xe280, '窶竅竄窿邃竇竊竍竏竕竓站竚竝竡竢竦竭竰笂笏笊笆笳笘笙笞笵笨笶筐筺笄筍笋筌筅筵筥筴筧筰筱筬筮箝箘箟箍箜箚箋箒箏筝箙篋篁篌篏箴篆篝篩簑簔篦篥籠簀簇簓篳篷簗簍篶簣簧簪簟簷簫簽籌籃籔籏籀籐籘籟籤籖籥籬籵粃粐粤粭粢粫粡粨粳粲粱粮粹粽糀糅糂糘糒糜糢鬻糯糲糴糶糺紆'],
  2503. [0xe340, '紂紜紕紊絅絋紮紲紿紵絆絳絖絎絲絨絮絏絣經綉絛綏絽綛綺綮綣綵緇綽綫總綢綯緜綸綟綰緘緝緤緞緻緲緡縅縊縣縡縒縱縟縉縋縢繆繦縻縵縹繃縷'],
  2504. [0xe380, '縲縺繧繝繖繞繙繚繹繪繩繼繻纃緕繽辮繿纈纉續纒纐纓纔纖纎纛纜缸缺罅罌罍罎罐网罕罔罘罟罠罨罩罧罸羂羆羃羈羇羌羔羞羝羚羣羯羲羹羮羶羸譱翅翆翊翕翔翡翦翩翳翹飜耆耄耋耒耘耙耜耡耨耿耻聊聆聒聘聚聟聢聨聳聲聰聶聹聽聿肄肆肅肛肓肚肭冐肬胛胥胙胝胄胚胖脉胯胱脛脩脣脯腋'],
  2505. [0xe440, '隋腆脾腓腑胼腱腮腥腦腴膃膈膊膀膂膠膕膤膣腟膓膩膰膵膾膸膽臀臂膺臉臍臑臙臘臈臚臟臠臧臺臻臾舁舂舅與舊舍舐舖舩舫舸舳艀艙艘艝艚艟艤'],
  2506. [0xe480, '艢艨艪艫舮艱艷艸艾芍芒芫芟芻芬苡苣苟苒苴苳苺莓范苻苹苞茆苜茉苙茵茴茖茲茱荀茹荐荅茯茫茗茘莅莚莪莟莢莖茣莎莇莊荼莵荳荵莠莉莨菴萓菫菎菽萃菘萋菁菷萇菠菲萍萢萠莽萸蔆菻葭萪萼蕚蒄葷葫蒭葮蒂葩葆萬葯葹萵蓊葢蒹蒿蒟蓙蓍蒻蓚蓐蓁蓆蓖蒡蔡蓿蓴蔗蔘蔬蔟蔕蔔蓼蕀蕣蕘蕈'],
  2507. [0xe540, '蕁蘂蕋蕕薀薤薈薑薊薨蕭薔薛藪薇薜蕷蕾薐藉薺藏薹藐藕藝藥藜藹蘊蘓蘋藾藺蘆蘢蘚蘰蘿虍乕虔號虧虱蚓蚣蚩蚪蚋蚌蚶蚯蛄蛆蚰蛉蠣蚫蛔蛞蛩蛬'],
  2508. [0xe580, '蛟蛛蛯蜒蜆蜈蜀蜃蛻蜑蜉蜍蛹蜊蜴蜿蜷蜻蜥蜩蜚蝠蝟蝸蝌蝎蝴蝗蝨蝮蝙蝓蝣蝪蠅螢螟螂螯蟋螽蟀蟐雖螫蟄螳蟇蟆螻蟯蟲蟠蠏蠍蟾蟶蟷蠎蟒蠑蠖蠕蠢蠡蠱蠶蠹蠧蠻衄衂衒衙衞衢衫袁衾袞衵衽袵衲袂袗袒袮袙袢袍袤袰袿袱裃裄裔裘裙裝裹褂裼裴裨裲褄褌褊褓襃褞褥褪褫襁襄褻褶褸襌褝襠襞'],
  2509. [0xe640, '襦襤襭襪襯襴襷襾覃覈覊覓覘覡覩覦覬覯覲覺覽覿觀觚觜觝觧觴觸訃訖訐訌訛訝訥訶詁詛詒詆詈詼詭詬詢誅誂誄誨誡誑誥誦誚誣諄諍諂諚諫諳諧'],
  2510. [0xe680, '諤諱謔諠諢諷諞諛謌謇謚諡謖謐謗謠謳鞫謦謫謾謨譁譌譏譎證譖譛譚譫譟譬譯譴譽讀讌讎讒讓讖讙讚谺豁谿豈豌豎豐豕豢豬豸豺貂貉貅貊貍貎貔豼貘戝貭貪貽貲貳貮貶賈賁賤賣賚賽賺賻贄贅贊贇贏贍贐齎贓賍贔贖赧赭赱赳趁趙跂趾趺跏跚跖跌跛跋跪跫跟跣跼踈踉跿踝踞踐踟蹂踵踰踴蹊'],
  2511. [0xe740, '蹇蹉蹌蹐蹈蹙蹤蹠踪蹣蹕蹶蹲蹼躁躇躅躄躋躊躓躑躔躙躪躡躬躰軆躱躾軅軈軋軛軣軼軻軫軾輊輅輕輒輙輓輜輟輛輌輦輳輻輹轅轂輾轌轉轆轎轗轜'],
  2512. [0xe780, '轢轣轤辜辟辣辭辯辷迚迥迢迪迯邇迴逅迹迺逑逕逡逍逞逖逋逧逶逵逹迸遏遐遑遒逎遉逾遖遘遞遨遯遶隨遲邂遽邁邀邊邉邏邨邯邱邵郢郤扈郛鄂鄒鄙鄲鄰酊酖酘酣酥酩酳酲醋醉醂醢醫醯醪醵醴醺釀釁釉釋釐釖釟釡釛釼釵釶鈞釿鈔鈬鈕鈑鉞鉗鉅鉉鉤鉈銕鈿鉋鉐銜銖銓銛鉚鋏銹銷鋩錏鋺鍄錮'],
  2513. [0xe840, '錙錢錚錣錺錵錻鍜鍠鍼鍮鍖鎰鎬鎭鎔鎹鏖鏗鏨鏥鏘鏃鏝鏐鏈鏤鐚鐔鐓鐃鐇鐐鐶鐫鐵鐡鐺鑁鑒鑄鑛鑠鑢鑞鑪鈩鑰鑵鑷鑽鑚鑼鑾钁鑿閂閇閊閔閖閘閙'],
  2514. [0xe880, '閠閨閧閭閼閻閹閾闊濶闃闍闌闕闔闖關闡闥闢阡阨阮阯陂陌陏陋陷陜陞陝陟陦陲陬隍隘隕隗險隧隱隲隰隴隶隸隹雎雋雉雍襍雜霍雕雹霄霆霈霓霎霑霏霖霙霤霪霰霹霽霾靄靆靈靂靉靜靠靤靦靨勒靫靱靹鞅靼鞁靺鞆鞋鞏鞐鞜鞨鞦鞣鞳鞴韃韆韈韋韜韭齏韲竟韶韵頏頌頸頤頡頷頽顆顏顋顫顯顰'],
  2515. [0xe940, '顱顴顳颪颯颱颶飄飃飆飩飫餃餉餒餔餘餡餝餞餤餠餬餮餽餾饂饉饅饐饋饑饒饌饕馗馘馥馭馮馼駟駛駝駘駑駭駮駱駲駻駸騁騏騅駢騙騫騷驅驂驀驃'],
  2516. [0xe980, '騾驕驍驛驗驟驢驥驤驩驫驪骭骰骼髀髏髑髓體髞髟髢髣髦髯髫髮髴髱髷髻鬆鬘鬚鬟鬢鬣鬥鬧鬨鬩鬪鬮鬯鬲魄魃魏魍魎魑魘魴鮓鮃鮑鮖鮗鮟鮠鮨鮴鯀鯊鮹鯆鯏鯑鯒鯣鯢鯤鯔鯡鰺鯲鯱鯰鰕鰔鰉鰓鰌鰆鰈鰒鰊鰄鰮鰛鰥鰤鰡鰰鱇鰲鱆鰾鱚鱠鱧鱶鱸鳧鳬鳰鴉鴈鳫鴃鴆鴪鴦鶯鴣鴟鵄鴕鴒鵁鴿鴾鵆鵈'],
  2517. [0xea40, '鵝鵞鵤鵑鵐鵙鵲鶉鶇鶫鵯鵺鶚鶤鶩鶲鷄鷁鶻鶸鶺鷆鷏鷂鷙鷓鷸鷦鷭鷯鷽鸚鸛鸞鹵鹹鹽麁麈麋麌麒麕麑麝麥麩麸麪麭靡黌黎黏黐黔黜點黝黠黥黨黯'],
  2518. [0xea80, '黴黶黷黹黻黼黽鼇鼈皷鼕鼡鼬鼾齊齒齔齣齟齠齡齦齧齬齪齷齲齶龕龜龠堯槇遙瑤凜熙']
  2519. ];
  2520. var tables;
  2521. /**
  2522. * @function getTables
  2523. * @returns {SJISTables}
  2524. */
  2525. function getTables() {
  2526. if (!tables) {
  2527. var UTF8_TO_SJIS = {};
  2528. var SJIS_TO_UTF8 = {};
  2529. var tLength = SJIS_UTF8_TABLE.length;
  2530. for (var i = 0; i < tLength; i++) {
  2531. var mapItem = SJIS_UTF8_TABLE[i];
  2532. var kanji = mapItem[1];
  2533. var kLength = kanji.length;
  2534. for (var j = 0; j < kLength; j++) {
  2535. var kCode = mapItem[0] + j;
  2536. var uCode = kanji.charAt(j).charCodeAt(0);
  2537. UTF8_TO_SJIS[uCode] = kCode;
  2538. SJIS_TO_UTF8[kCode] = uCode;
  2539. }
  2540. }
  2541. tables = { UTF8_TO_SJIS: UTF8_TO_SJIS, SJIS_TO_UTF8: SJIS_TO_UTF8 };
  2542. }
  2543. return tables;
  2544. }
  2545. /**
  2546. * @function SJIS
  2547. * @param {string} str
  2548. * @returns {number[]}
  2549. */
  2550. function SJIS(str) {
  2551. var bytes = [];
  2552. var length = str.length;
  2553. var UTF8_TO_SJIS = getTables().UTF8_TO_SJIS;
  2554. for (var i = 0; i < length; i++) {
  2555. var code = str.charCodeAt(i);
  2556. var byte = UTF8_TO_SJIS[code];
  2557. if (byte != null) {
  2558. // 2 bytes
  2559. bytes.push(byte >> 8);
  2560. bytes.push(byte & 0xff);
  2561. }
  2562. else {
  2563. throw "illegal char: " + String.fromCharCode(code);
  2564. }
  2565. }
  2566. return bytes;
  2567. }
  2568.  
  2569. /**
  2570. * @module index
  2571. * @author nuintun
  2572. * @author Cosmo Wolfe
  2573. */
  2574. function decodeNumeric(stream, size) {
  2575. var data = '';
  2576. var bytes = [];
  2577. var characterCountSize = [10, 12, 14][size];
  2578. var length = stream.readBits(characterCountSize);
  2579. // Read digits in groups of 3
  2580. while (length >= 3) {
  2581. var num = stream.readBits(10);
  2582. if (num >= 1000) {
  2583. throw 'invalid numeric value above 999';
  2584. }
  2585. var a = Math.floor(num / 100);
  2586. var b = Math.floor(num / 10) % 10;
  2587. var c = num % 10;
  2588. bytes.push(48 + a, 48 + b, 48 + c);
  2589. data += a.toString() + b.toString() + c.toString();
  2590. length -= 3;
  2591. }
  2592. // If the number of digits aren't a multiple of 3, the remaining digits are special cased.
  2593. if (length === 2) {
  2594. var num = stream.readBits(7);
  2595. if (num >= 100) {
  2596. throw 'invalid numeric value above 99';
  2597. }
  2598. var a = Math.floor(num / 10);
  2599. var b = num % 10;
  2600. bytes.push(48 + a, 48 + b);
  2601. data += a.toString() + b.toString();
  2602. }
  2603. else if (length === 1) {
  2604. var num = stream.readBits(4);
  2605. if (num >= 10) {
  2606. throw 'invalid numeric value above 9';
  2607. }
  2608. bytes.push(48 + num);
  2609. data += num.toString();
  2610. }
  2611. return { bytes: bytes, data: data };
  2612. }
  2613. // prettier-ignore
  2614. var AlphanumericCharacterCodes = [
  2615. '0', '1', '2', '3', '4', '5', '6', '7', '8',
  2616. '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  2617. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
  2618. 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  2619. ' ', '$', '%', '*', '+', '-', '.', '/', ':'
  2620. ];
  2621. function decodeAlphanumeric(stream, size) {
  2622. var data = '';
  2623. var bytes = [];
  2624. var characterCountSize = [9, 11, 13][size];
  2625. var length = stream.readBits(characterCountSize);
  2626. while (length >= 2) {
  2627. var v = stream.readBits(11);
  2628. var a = Math.floor(v / 45);
  2629. var b = v % 45;
  2630. bytes.push(AlphanumericCharacterCodes[a].charCodeAt(0), AlphanumericCharacterCodes[b].charCodeAt(0));
  2631. data += AlphanumericCharacterCodes[a] + AlphanumericCharacterCodes[b];
  2632. length -= 2;
  2633. }
  2634. if (length === 1) {
  2635. var a = stream.readBits(6);
  2636. bytes.push(AlphanumericCharacterCodes[a].charCodeAt(0));
  2637. data += AlphanumericCharacterCodes[a];
  2638. }
  2639. return { bytes: bytes, data: data };
  2640. }
  2641. /**
  2642. * @function decodeByteAsUTF8
  2643. * @param {number[]} bytes
  2644. * @returns {string}
  2645. * @see https://github.com/google/closure-library/blob/master/closure/goog/crypt/crypt.js
  2646. */
  2647. function decodeByteAsUTF8(bytes) {
  2648. // TODO(user): Use native implementations if/when available
  2649. var pos = 0;
  2650. var output = '';
  2651. var length = bytes.length;
  2652. while (pos < length) {
  2653. var c1 = bytes[pos++];
  2654. if (c1 < 128) {
  2655. output += String.fromCharCode(c1);
  2656. }
  2657. else if (c1 > 191 && c1 < 224) {
  2658. var c2 = bytes[pos++];
  2659. output += String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
  2660. }
  2661. else if (c1 > 239 && c1 < 365) {
  2662. // Surrogate Pair
  2663. var c2 = bytes[pos++];
  2664. var c3 = bytes[pos++];
  2665. var c4 = bytes[pos++];
  2666. var u = (((c1 & 7) << 18) | ((c2 & 63) << 12) | ((c3 & 63) << 6) | (c4 & 63)) - 0x10000;
  2667. output += String.fromCharCode(0xd800 + (u >> 10));
  2668. output += String.fromCharCode(0xdc00 + (u & 1023));
  2669. }
  2670. else {
  2671. var c2 = bytes[pos++];
  2672. var c3 = bytes[pos++];
  2673. output += String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  2674. }
  2675. }
  2676. return output;
  2677. }
  2678. /**
  2679. * @function decodeByteAsUTF8
  2680. * @param {number[]} bytes
  2681. * @returns {string}
  2682. * @see https://github.com/narirou/jconv/blob/master/jconv.js
  2683. */
  2684. function decodeByteAsSJIS(bytes) {
  2685. var pos = 0;
  2686. var output = '';
  2687. var length = bytes.length;
  2688. var SJIS_TO_UTF8 = getTables().SJIS_TO_UTF8;
  2689. while (pos < length) {
  2690. var byte = bytes[pos++];
  2691. if (byte < 0x80) {
  2692. // ASCII
  2693. output += String.fromCharCode(byte);
  2694. }
  2695. else if (0xa0 <= byte && byte <= 0xdf) {
  2696. // HALFWIDTH_KATAKANA
  2697. output += String.fromCharCode(byte + 0xfec0);
  2698. }
  2699. else {
  2700. // KANJI
  2701. var code = (byte << 8) + bytes[pos++];
  2702. code = SJIS_TO_UTF8[code];
  2703. output += code != null ? String.fromCharCode(code) : '?';
  2704. }
  2705. }
  2706. return output;
  2707. }
  2708. function decodeByte(stream, size, encoding) {
  2709. var bytes = [];
  2710. var characterCountSize = [8, 16, 16][size];
  2711. var length = stream.readBits(characterCountSize);
  2712. for (var i = 0; i < length; i++) {
  2713. bytes.push(stream.readBits(8));
  2714. }
  2715. return { bytes: bytes, data: encoding === 20 /* SJIS */ ? decodeByteAsSJIS(bytes) : decodeByteAsUTF8(bytes) };
  2716. }
  2717. function decodeKanji(stream, size) {
  2718. var data = '';
  2719. var bytes = [];
  2720. var SJIS_TO_UTF8 = getTables().SJIS_TO_UTF8;
  2721. var characterCountSize = [8, 10, 12][size];
  2722. var length = stream.readBits(characterCountSize);
  2723. for (var i = 0; i < length; i++) {
  2724. var k = stream.readBits(13);
  2725. var c = (Math.floor(k / 0xc0) << 8) | k % 0xc0;
  2726. if (c < 0x1f00) {
  2727. c += 0x8140;
  2728. }
  2729. else {
  2730. c += 0xc140;
  2731. }
  2732. bytes.push(c >> 8, c & 0xff);
  2733. var b = SJIS_TO_UTF8[c];
  2734. data += String.fromCharCode(b != null ? b : c);
  2735. }
  2736. return { bytes: bytes, data: data };
  2737. }
  2738. function bytesDecode(data, version, errorCorrectionLevel) {
  2739. var _a, _b, _c, _d;
  2740. var encoding = 26 /* UTF8 */;
  2741. var stream = new BitStream(data);
  2742. // There are 3 'sizes' based on the version. 1-9 is small (0), 10-26 is medium (1) and 27-40 is large (2).
  2743. var size = version <= 9 ? 0 : version <= 26 ? 1 : 2;
  2744. var result = { data: '', bytes: [], chunks: [], version: version, errorCorrectionLevel: errorCorrectionLevel };
  2745. while (stream.available() >= 4) {
  2746. var mode = stream.readBits(4);
  2747. if (mode === exports.Mode.Terminator) {
  2748. return result;
  2749. }
  2750. else if (mode === exports.Mode.ECI) {
  2751. if (stream.readBits(1) === 0) {
  2752. encoding = stream.readBits(7);
  2753. result.chunks.push({ mode: exports.Mode.ECI, encoding: encoding });
  2754. }
  2755. else if (stream.readBits(1) === 0) {
  2756. encoding = stream.readBits(14);
  2757. result.chunks.push({ mode: exports.Mode.ECI, encoding: encoding });
  2758. }
  2759. else if (stream.readBits(1) === 0) {
  2760. encoding = stream.readBits(21);
  2761. result.chunks.push({ mode: exports.Mode.ECI, encoding: encoding });
  2762. }
  2763. else {
  2764. // ECI data seems corrupted
  2765. result.chunks.push({ mode: exports.Mode.ECI, encoding: -1 });
  2766. }
  2767. }
  2768. else if (mode === exports.Mode.Numeric) {
  2769. var numericResult = decodeNumeric(stream, size);
  2770. result.data += numericResult.data;
  2771. result.chunks.push({
  2772. mode: exports.Mode.Numeric,
  2773. data: numericResult.data,
  2774. bytes: numericResult.bytes
  2775. });
  2776. (_a = result.bytes).push.apply(_a, numericResult.bytes);
  2777. }
  2778. else if (mode === exports.Mode.Alphanumeric) {
  2779. var alphanumericResult = decodeAlphanumeric(stream, size);
  2780. result.data += alphanumericResult.data;
  2781. result.chunks.push({
  2782. mode: exports.Mode.Alphanumeric,
  2783. data: alphanumericResult.data,
  2784. bytes: alphanumericResult.bytes
  2785. });
  2786. (_b = result.bytes).push.apply(_b, alphanumericResult.bytes);
  2787. }
  2788. else if (mode === exports.Mode.StructuredAppend) {
  2789. // QR Standard section 9.2
  2790. var structuredAppend = {
  2791. // [current, total]
  2792. symbols: [stream.readBits(4), stream.readBits(4)],
  2793. parity: stream.readBits(8)
  2794. };
  2795. result.chunks.push(__assign({ mode: exports.Mode.StructuredAppend }, structuredAppend));
  2796. }
  2797. else if (mode === exports.Mode.Byte) {
  2798. var byteResult = decodeByte(stream, size, encoding);
  2799. result.data += byteResult.data;
  2800. result.chunks.push({
  2801. mode: exports.Mode.Byte,
  2802. data: byteResult.data,
  2803. bytes: byteResult.bytes
  2804. });
  2805. (_c = result.bytes).push.apply(_c, byteResult.bytes);
  2806. }
  2807. else if (mode === exports.Mode.Kanji) {
  2808. var kanjiResult = decodeKanji(stream, size);
  2809. result.data += kanjiResult.data;
  2810. result.chunks.push({
  2811. mode: exports.Mode.Kanji,
  2812. data: kanjiResult.data,
  2813. bytes: kanjiResult.bytes
  2814. });
  2815. (_d = result.bytes).push.apply(_d, kanjiResult.bytes);
  2816. }
  2817. }
  2818. // If there is no data left, or the remaining bits are all 0, then that counts as a termination marker
  2819. if (stream.available() === 0 || stream.readBits(stream.available()) === 0) {
  2820. return result;
  2821. }
  2822. }
  2823.  
  2824. /**
  2825. * @module Version
  2826. * @author nuintun
  2827. * @author Cosmo Wolfe
  2828. */
  2829. var VERSIONS = [
  2830. {
  2831. infoBits: null,
  2832. versionNumber: 1,
  2833. alignmentPatternCenters: [],
  2834. errorCorrectionLevels: [
  2835. {
  2836. ecCodewordsPerBlock: 10,
  2837. ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 16 }]
  2838. },
  2839. {
  2840. ecCodewordsPerBlock: 7,
  2841. ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 19 }]
  2842. },
  2843. {
  2844. ecCodewordsPerBlock: 17,
  2845. ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 9 }]
  2846. },
  2847. {
  2848. ecCodewordsPerBlock: 13,
  2849. ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 13 }]
  2850. }
  2851. ]
  2852. },
  2853. {
  2854. infoBits: null,
  2855. versionNumber: 2,
  2856. alignmentPatternCenters: [6, 18],
  2857. errorCorrectionLevels: [
  2858. {
  2859. ecCodewordsPerBlock: 16,
  2860. ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 28 }]
  2861. },
  2862. {
  2863. ecCodewordsPerBlock: 10,
  2864. ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 34 }]
  2865. },
  2866. {
  2867. ecCodewordsPerBlock: 28,
  2868. ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 16 }]
  2869. },
  2870. {
  2871. ecCodewordsPerBlock: 22,
  2872. ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 22 }]
  2873. }
  2874. ]
  2875. },
  2876. {
  2877. infoBits: null,
  2878. versionNumber: 3,
  2879. alignmentPatternCenters: [6, 22],
  2880. errorCorrectionLevels: [
  2881. {
  2882. ecCodewordsPerBlock: 26,
  2883. ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 44 }]
  2884. },
  2885. {
  2886. ecCodewordsPerBlock: 15,
  2887. ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 55 }]
  2888. },
  2889. {
  2890. ecCodewordsPerBlock: 22,
  2891. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 13 }]
  2892. },
  2893. {
  2894. ecCodewordsPerBlock: 18,
  2895. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 17 }]
  2896. }
  2897. ]
  2898. },
  2899. {
  2900. infoBits: null,
  2901. versionNumber: 4,
  2902. alignmentPatternCenters: [6, 26],
  2903. errorCorrectionLevels: [
  2904. {
  2905. ecCodewordsPerBlock: 18,
  2906. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 32 }]
  2907. },
  2908. {
  2909. ecCodewordsPerBlock: 20,
  2910. ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 80 }]
  2911. },
  2912. {
  2913. ecCodewordsPerBlock: 16,
  2914. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 9 }]
  2915. },
  2916. {
  2917. ecCodewordsPerBlock: 26,
  2918. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 24 }]
  2919. }
  2920. ]
  2921. },
  2922. {
  2923. infoBits: null,
  2924. versionNumber: 5,
  2925. alignmentPatternCenters: [6, 30],
  2926. errorCorrectionLevels: [
  2927. {
  2928. ecCodewordsPerBlock: 24,
  2929. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 43 }]
  2930. },
  2931. {
  2932. ecCodewordsPerBlock: 26,
  2933. ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 108 }]
  2934. },
  2935. {
  2936. ecCodewordsPerBlock: 22,
  2937. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 11 }, { numBlocks: 2, dataCodewordsPerBlock: 12 }]
  2938. },
  2939. {
  2940. ecCodewordsPerBlock: 18,
  2941. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 15 }, { numBlocks: 2, dataCodewordsPerBlock: 16 }]
  2942. }
  2943. ]
  2944. },
  2945. {
  2946. infoBits: null,
  2947. versionNumber: 6,
  2948. alignmentPatternCenters: [6, 34],
  2949. errorCorrectionLevels: [
  2950. {
  2951. ecCodewordsPerBlock: 16,
  2952. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 27 }]
  2953. },
  2954. {
  2955. ecCodewordsPerBlock: 18,
  2956. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 68 }]
  2957. },
  2958. {
  2959. ecCodewordsPerBlock: 28,
  2960. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 15 }]
  2961. },
  2962. {
  2963. ecCodewordsPerBlock: 24,
  2964. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 19 }]
  2965. }
  2966. ]
  2967. },
  2968. {
  2969. infoBits: 0x07c94,
  2970. versionNumber: 7,
  2971. alignmentPatternCenters: [6, 22, 38],
  2972. errorCorrectionLevels: [
  2973. {
  2974. ecCodewordsPerBlock: 18,
  2975. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 31 }]
  2976. },
  2977. {
  2978. ecCodewordsPerBlock: 20,
  2979. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 78 }]
  2980. },
  2981. {
  2982. ecCodewordsPerBlock: 26,
  2983. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 13 }, { numBlocks: 1, dataCodewordsPerBlock: 14 }]
  2984. },
  2985. {
  2986. ecCodewordsPerBlock: 18,
  2987. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 14 }, { numBlocks: 4, dataCodewordsPerBlock: 15 }]
  2988. }
  2989. ]
  2990. },
  2991. {
  2992. infoBits: 0x085bc,
  2993. versionNumber: 8,
  2994. alignmentPatternCenters: [6, 24, 42],
  2995. errorCorrectionLevels: [
  2996. {
  2997. ecCodewordsPerBlock: 22,
  2998. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 38 }, { numBlocks: 2, dataCodewordsPerBlock: 39 }]
  2999. },
  3000. {
  3001. ecCodewordsPerBlock: 24,
  3002. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 97 }]
  3003. },
  3004. {
  3005. ecCodewordsPerBlock: 26,
  3006. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 14 }, { numBlocks: 2, dataCodewordsPerBlock: 15 }]
  3007. },
  3008. {
  3009. ecCodewordsPerBlock: 22,
  3010. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 18 }, { numBlocks: 2, dataCodewordsPerBlock: 19 }]
  3011. }
  3012. ]
  3013. },
  3014. {
  3015. infoBits: 0x09a99,
  3016. versionNumber: 9,
  3017. alignmentPatternCenters: [6, 26, 46],
  3018. errorCorrectionLevels: [
  3019. {
  3020. ecCodewordsPerBlock: 22,
  3021. ecBlocks: [{ numBlocks: 3, dataCodewordsPerBlock: 36 }, { numBlocks: 2, dataCodewordsPerBlock: 37 }]
  3022. },
  3023. {
  3024. ecCodewordsPerBlock: 30,
  3025. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 116 }]
  3026. },
  3027. {
  3028. ecCodewordsPerBlock: 24,
  3029. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 12 }, { numBlocks: 4, dataCodewordsPerBlock: 13 }]
  3030. },
  3031. {
  3032. ecCodewordsPerBlock: 20,
  3033. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 16 }, { numBlocks: 4, dataCodewordsPerBlock: 17 }]
  3034. }
  3035. ]
  3036. },
  3037. {
  3038. infoBits: 0x0a4d3,
  3039. versionNumber: 10,
  3040. alignmentPatternCenters: [6, 28, 50],
  3041. errorCorrectionLevels: [
  3042. {
  3043. ecCodewordsPerBlock: 26,
  3044. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 43 }, { numBlocks: 1, dataCodewordsPerBlock: 44 }]
  3045. },
  3046. {
  3047. ecCodewordsPerBlock: 18,
  3048. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 68 }, { numBlocks: 2, dataCodewordsPerBlock: 69 }]
  3049. },
  3050. {
  3051. ecCodewordsPerBlock: 28,
  3052. ecBlocks: [{ numBlocks: 6, dataCodewordsPerBlock: 15 }, { numBlocks: 2, dataCodewordsPerBlock: 16 }]
  3053. },
  3054. {
  3055. ecCodewordsPerBlock: 24,
  3056. ecBlocks: [{ numBlocks: 6, dataCodewordsPerBlock: 19 }, { numBlocks: 2, dataCodewordsPerBlock: 20 }]
  3057. }
  3058. ]
  3059. },
  3060. {
  3061. infoBits: 0x0bbf6,
  3062. versionNumber: 11,
  3063. alignmentPatternCenters: [6, 30, 54],
  3064. errorCorrectionLevels: [
  3065. {
  3066. ecCodewordsPerBlock: 30,
  3067. ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 50 }, { numBlocks: 4, dataCodewordsPerBlock: 51 }]
  3068. },
  3069. {
  3070. ecCodewordsPerBlock: 20,
  3071. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 81 }]
  3072. },
  3073. {
  3074. ecCodewordsPerBlock: 24,
  3075. ecBlocks: [{ numBlocks: 3, dataCodewordsPerBlock: 12 }, { numBlocks: 8, dataCodewordsPerBlock: 13 }]
  3076. },
  3077. {
  3078. ecCodewordsPerBlock: 28,
  3079. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 22 }, { numBlocks: 4, dataCodewordsPerBlock: 23 }]
  3080. }
  3081. ]
  3082. },
  3083. {
  3084. infoBits: 0x0c762,
  3085. versionNumber: 12,
  3086. alignmentPatternCenters: [6, 32, 58],
  3087. errorCorrectionLevels: [
  3088. {
  3089. ecCodewordsPerBlock: 22,
  3090. ecBlocks: [{ numBlocks: 6, dataCodewordsPerBlock: 36 }, { numBlocks: 2, dataCodewordsPerBlock: 37 }]
  3091. },
  3092. {
  3093. ecCodewordsPerBlock: 24,
  3094. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 92 }, { numBlocks: 2, dataCodewordsPerBlock: 93 }]
  3095. },
  3096. {
  3097. ecCodewordsPerBlock: 28,
  3098. ecBlocks: [{ numBlocks: 7, dataCodewordsPerBlock: 14 }, { numBlocks: 4, dataCodewordsPerBlock: 15 }]
  3099. },
  3100. {
  3101. ecCodewordsPerBlock: 26,
  3102. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 20 }, { numBlocks: 6, dataCodewordsPerBlock: 21 }]
  3103. }
  3104. ]
  3105. },
  3106. {
  3107. infoBits: 0x0d847,
  3108. versionNumber: 13,
  3109. alignmentPatternCenters: [6, 34, 62],
  3110. errorCorrectionLevels: [
  3111. {
  3112. ecCodewordsPerBlock: 22,
  3113. ecBlocks: [{ numBlocks: 8, dataCodewordsPerBlock: 37 }, { numBlocks: 1, dataCodewordsPerBlock: 38 }]
  3114. },
  3115. {
  3116. ecCodewordsPerBlock: 26,
  3117. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 107 }]
  3118. },
  3119. {
  3120. ecCodewordsPerBlock: 22,
  3121. ecBlocks: [{ numBlocks: 12, dataCodewordsPerBlock: 11 }, { numBlocks: 4, dataCodewordsPerBlock: 12 }]
  3122. },
  3123. {
  3124. ecCodewordsPerBlock: 24,
  3125. ecBlocks: [{ numBlocks: 8, dataCodewordsPerBlock: 20 }, { numBlocks: 4, dataCodewordsPerBlock: 21 }]
  3126. }
  3127. ]
  3128. },
  3129. {
  3130. infoBits: 0x0e60d,
  3131. versionNumber: 14,
  3132. alignmentPatternCenters: [6, 26, 46, 66],
  3133. errorCorrectionLevels: [
  3134. {
  3135. ecCodewordsPerBlock: 24,
  3136. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 40 }, { numBlocks: 5, dataCodewordsPerBlock: 41 }]
  3137. },
  3138. {
  3139. ecCodewordsPerBlock: 30,
  3140. ecBlocks: [{ numBlocks: 3, dataCodewordsPerBlock: 115 }, { numBlocks: 1, dataCodewordsPerBlock: 116 }]
  3141. },
  3142. {
  3143. ecCodewordsPerBlock: 24,
  3144. ecBlocks: [{ numBlocks: 11, dataCodewordsPerBlock: 12 }, { numBlocks: 5, dataCodewordsPerBlock: 13 }]
  3145. },
  3146. {
  3147. ecCodewordsPerBlock: 20,
  3148. ecBlocks: [{ numBlocks: 11, dataCodewordsPerBlock: 16 }, { numBlocks: 5, dataCodewordsPerBlock: 17 }]
  3149. }
  3150. ]
  3151. },
  3152. {
  3153. infoBits: 0x0f928,
  3154. versionNumber: 15,
  3155. alignmentPatternCenters: [6, 26, 48, 70],
  3156. errorCorrectionLevels: [
  3157. {
  3158. ecCodewordsPerBlock: 24,
  3159. ecBlocks: [{ numBlocks: 5, dataCodewordsPerBlock: 41 }, { numBlocks: 5, dataCodewordsPerBlock: 42 }]
  3160. },
  3161. {
  3162. ecCodewordsPerBlock: 22,
  3163. ecBlocks: [{ numBlocks: 5, dataCodewordsPerBlock: 87 }, { numBlocks: 1, dataCodewordsPerBlock: 88 }]
  3164. },
  3165. {
  3166. ecCodewordsPerBlock: 24,
  3167. ecBlocks: [{ numBlocks: 11, dataCodewordsPerBlock: 12 }, { numBlocks: 7, dataCodewordsPerBlock: 13 }]
  3168. },
  3169. {
  3170. ecCodewordsPerBlock: 30,
  3171. ecBlocks: [{ numBlocks: 5, dataCodewordsPerBlock: 24 }, { numBlocks: 7, dataCodewordsPerBlock: 25 }]
  3172. }
  3173. ]
  3174. },
  3175. {
  3176. infoBits: 0x10b78,
  3177. versionNumber: 16,
  3178. alignmentPatternCenters: [6, 26, 50, 74],
  3179. errorCorrectionLevels: [
  3180. {
  3181. ecCodewordsPerBlock: 28,
  3182. ecBlocks: [{ numBlocks: 7, dataCodewordsPerBlock: 45 }, { numBlocks: 3, dataCodewordsPerBlock: 46 }]
  3183. },
  3184. {
  3185. ecCodewordsPerBlock: 24,
  3186. ecBlocks: [{ numBlocks: 5, dataCodewordsPerBlock: 98 }, { numBlocks: 1, dataCodewordsPerBlock: 99 }]
  3187. },
  3188. {
  3189. ecCodewordsPerBlock: 30,
  3190. ecBlocks: [{ numBlocks: 3, dataCodewordsPerBlock: 15 }, { numBlocks: 13, dataCodewordsPerBlock: 16 }]
  3191. },
  3192. {
  3193. ecCodewordsPerBlock: 24,
  3194. ecBlocks: [{ numBlocks: 15, dataCodewordsPerBlock: 19 }, { numBlocks: 2, dataCodewordsPerBlock: 20 }]
  3195. }
  3196. ]
  3197. },
  3198. {
  3199. infoBits: 0x1145d,
  3200. versionNumber: 17,
  3201. alignmentPatternCenters: [6, 30, 54, 78],
  3202. errorCorrectionLevels: [
  3203. {
  3204. ecCodewordsPerBlock: 28,
  3205. ecBlocks: [{ numBlocks: 10, dataCodewordsPerBlock: 46 }, { numBlocks: 1, dataCodewordsPerBlock: 47 }]
  3206. },
  3207. {
  3208. ecCodewordsPerBlock: 28,
  3209. ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 107 }, { numBlocks: 5, dataCodewordsPerBlock: 108 }]
  3210. },
  3211. {
  3212. ecCodewordsPerBlock: 28,
  3213. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 14 }, { numBlocks: 17, dataCodewordsPerBlock: 15 }]
  3214. },
  3215. {
  3216. ecCodewordsPerBlock: 28,
  3217. ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 22 }, { numBlocks: 15, dataCodewordsPerBlock: 23 }]
  3218. }
  3219. ]
  3220. },
  3221. {
  3222. infoBits: 0x12a17,
  3223. versionNumber: 18,
  3224. alignmentPatternCenters: [6, 30, 56, 82],
  3225. errorCorrectionLevels: [
  3226. {
  3227. ecCodewordsPerBlock: 26,
  3228. ecBlocks: [{ numBlocks: 9, dataCodewordsPerBlock: 43 }, { numBlocks: 4, dataCodewordsPerBlock: 44 }]
  3229. },
  3230. {
  3231. ecCodewordsPerBlock: 30,
  3232. ecBlocks: [{ numBlocks: 5, dataCodewordsPerBlock: 120 }, { numBlocks: 1, dataCodewordsPerBlock: 121 }]
  3233. },
  3234. {
  3235. ecCodewordsPerBlock: 28,
  3236. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 14 }, { numBlocks: 19, dataCodewordsPerBlock: 15 }]
  3237. },
  3238. {
  3239. ecCodewordsPerBlock: 28,
  3240. ecBlocks: [{ numBlocks: 17, dataCodewordsPerBlock: 22 }, { numBlocks: 1, dataCodewordsPerBlock: 23 }]
  3241. }
  3242. ]
  3243. },
  3244. {
  3245. infoBits: 0x13532,
  3246. versionNumber: 19,
  3247. alignmentPatternCenters: [6, 30, 58, 86],
  3248. errorCorrectionLevels: [
  3249. {
  3250. ecCodewordsPerBlock: 26,
  3251. ecBlocks: [{ numBlocks: 3, dataCodewordsPerBlock: 44 }, { numBlocks: 11, dataCodewordsPerBlock: 45 }]
  3252. },
  3253. {
  3254. ecCodewordsPerBlock: 28,
  3255. ecBlocks: [{ numBlocks: 3, dataCodewordsPerBlock: 113 }, { numBlocks: 4, dataCodewordsPerBlock: 114 }]
  3256. },
  3257. {
  3258. ecCodewordsPerBlock: 26,
  3259. ecBlocks: [{ numBlocks: 9, dataCodewordsPerBlock: 13 }, { numBlocks: 16, dataCodewordsPerBlock: 14 }]
  3260. },
  3261. {
  3262. ecCodewordsPerBlock: 26,
  3263. ecBlocks: [{ numBlocks: 17, dataCodewordsPerBlock: 21 }, { numBlocks: 4, dataCodewordsPerBlock: 22 }]
  3264. }
  3265. ]
  3266. },
  3267. {
  3268. infoBits: 0x149a6,
  3269. versionNumber: 20,
  3270. alignmentPatternCenters: [6, 34, 62, 90],
  3271. errorCorrectionLevels: [
  3272. {
  3273. ecCodewordsPerBlock: 26,
  3274. ecBlocks: [{ numBlocks: 3, dataCodewordsPerBlock: 41 }, { numBlocks: 13, dataCodewordsPerBlock: 42 }]
  3275. },
  3276. {
  3277. ecCodewordsPerBlock: 28,
  3278. ecBlocks: [{ numBlocks: 3, dataCodewordsPerBlock: 107 }, { numBlocks: 5, dataCodewordsPerBlock: 108 }]
  3279. },
  3280. {
  3281. ecCodewordsPerBlock: 28,
  3282. ecBlocks: [{ numBlocks: 15, dataCodewordsPerBlock: 15 }, { numBlocks: 10, dataCodewordsPerBlock: 16 }]
  3283. },
  3284. {
  3285. ecCodewordsPerBlock: 30,
  3286. ecBlocks: [{ numBlocks: 15, dataCodewordsPerBlock: 24 }, { numBlocks: 5, dataCodewordsPerBlock: 25 }]
  3287. }
  3288. ]
  3289. },
  3290. {
  3291. infoBits: 0x15683,
  3292. versionNumber: 21,
  3293. alignmentPatternCenters: [6, 28, 50, 72, 94],
  3294. errorCorrectionLevels: [
  3295. {
  3296. ecCodewordsPerBlock: 26,
  3297. ecBlocks: [{ numBlocks: 17, dataCodewordsPerBlock: 42 }]
  3298. },
  3299. {
  3300. ecCodewordsPerBlock: 28,
  3301. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 116 }, { numBlocks: 4, dataCodewordsPerBlock: 117 }]
  3302. },
  3303. {
  3304. ecCodewordsPerBlock: 30,
  3305. ecBlocks: [{ numBlocks: 19, dataCodewordsPerBlock: 16 }, { numBlocks: 6, dataCodewordsPerBlock: 17 }]
  3306. },
  3307. {
  3308. ecCodewordsPerBlock: 28,
  3309. ecBlocks: [{ numBlocks: 17, dataCodewordsPerBlock: 22 }, { numBlocks: 6, dataCodewordsPerBlock: 23 }]
  3310. }
  3311. ]
  3312. },
  3313. {
  3314. infoBits: 0x168c9,
  3315. versionNumber: 22,
  3316. alignmentPatternCenters: [6, 26, 50, 74, 98],
  3317. errorCorrectionLevels: [
  3318. {
  3319. ecCodewordsPerBlock: 28,
  3320. ecBlocks: [{ numBlocks: 17, dataCodewordsPerBlock: 46 }]
  3321. },
  3322. {
  3323. ecCodewordsPerBlock: 28,
  3324. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 111 }, { numBlocks: 7, dataCodewordsPerBlock: 112 }]
  3325. },
  3326. {
  3327. ecCodewordsPerBlock: 24,
  3328. ecBlocks: [{ numBlocks: 34, dataCodewordsPerBlock: 13 }]
  3329. },
  3330. {
  3331. ecCodewordsPerBlock: 30,
  3332. ecBlocks: [{ numBlocks: 7, dataCodewordsPerBlock: 24 }, { numBlocks: 16, dataCodewordsPerBlock: 25 }]
  3333. }
  3334. ]
  3335. },
  3336. {
  3337. infoBits: 0x177ec,
  3338. versionNumber: 23,
  3339. alignmentPatternCenters: [6, 30, 54, 74, 102],
  3340. errorCorrectionLevels: [
  3341. {
  3342. ecCodewordsPerBlock: 28,
  3343. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 47 }, { numBlocks: 14, dataCodewordsPerBlock: 48 }]
  3344. },
  3345. {
  3346. ecCodewordsPerBlock: 30,
  3347. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 121 }, { numBlocks: 5, dataCodewordsPerBlock: 122 }]
  3348. },
  3349. {
  3350. ecCodewordsPerBlock: 30,
  3351. ecBlocks: [{ numBlocks: 16, dataCodewordsPerBlock: 15 }, { numBlocks: 14, dataCodewordsPerBlock: 16 }]
  3352. },
  3353. {
  3354. ecCodewordsPerBlock: 30,
  3355. ecBlocks: [{ numBlocks: 11, dataCodewordsPerBlock: 24 }, { numBlocks: 14, dataCodewordsPerBlock: 25 }]
  3356. }
  3357. ]
  3358. },
  3359. {
  3360. infoBits: 0x18ec4,
  3361. versionNumber: 24,
  3362. alignmentPatternCenters: [6, 28, 54, 80, 106],
  3363. errorCorrectionLevels: [
  3364. {
  3365. ecCodewordsPerBlock: 28,
  3366. ecBlocks: [{ numBlocks: 6, dataCodewordsPerBlock: 45 }, { numBlocks: 14, dataCodewordsPerBlock: 46 }]
  3367. },
  3368. {
  3369. ecCodewordsPerBlock: 30,
  3370. ecBlocks: [{ numBlocks: 6, dataCodewordsPerBlock: 117 }, { numBlocks: 4, dataCodewordsPerBlock: 118 }]
  3371. },
  3372. {
  3373. ecCodewordsPerBlock: 30,
  3374. ecBlocks: [{ numBlocks: 30, dataCodewordsPerBlock: 16 }, { numBlocks: 2, dataCodewordsPerBlock: 17 }]
  3375. },
  3376. {
  3377. ecCodewordsPerBlock: 30,
  3378. ecBlocks: [{ numBlocks: 11, dataCodewordsPerBlock: 24 }, { numBlocks: 16, dataCodewordsPerBlock: 25 }]
  3379. }
  3380. ]
  3381. },
  3382. {
  3383. infoBits: 0x191e1,
  3384. versionNumber: 25,
  3385. alignmentPatternCenters: [6, 32, 58, 84, 110],
  3386. errorCorrectionLevels: [
  3387. {
  3388. ecCodewordsPerBlock: 28,
  3389. ecBlocks: [{ numBlocks: 8, dataCodewordsPerBlock: 47 }, { numBlocks: 13, dataCodewordsPerBlock: 48 }]
  3390. },
  3391. {
  3392. ecCodewordsPerBlock: 26,
  3393. ecBlocks: [{ numBlocks: 8, dataCodewordsPerBlock: 106 }, { numBlocks: 4, dataCodewordsPerBlock: 107 }]
  3394. },
  3395. {
  3396. ecCodewordsPerBlock: 30,
  3397. ecBlocks: [{ numBlocks: 22, dataCodewordsPerBlock: 15 }, { numBlocks: 13, dataCodewordsPerBlock: 16 }]
  3398. },
  3399. {
  3400. ecCodewordsPerBlock: 30,
  3401. ecBlocks: [{ numBlocks: 7, dataCodewordsPerBlock: 24 }, { numBlocks: 22, dataCodewordsPerBlock: 25 }]
  3402. }
  3403. ]
  3404. },
  3405. {
  3406. infoBits: 0x1afab,
  3407. versionNumber: 26,
  3408. alignmentPatternCenters: [6, 30, 58, 86, 114],
  3409. errorCorrectionLevels: [
  3410. {
  3411. ecCodewordsPerBlock: 28,
  3412. ecBlocks: [{ numBlocks: 19, dataCodewordsPerBlock: 46 }, { numBlocks: 4, dataCodewordsPerBlock: 47 }]
  3413. },
  3414. {
  3415. ecCodewordsPerBlock: 28,
  3416. ecBlocks: [{ numBlocks: 10, dataCodewordsPerBlock: 114 }, { numBlocks: 2, dataCodewordsPerBlock: 115 }]
  3417. },
  3418. {
  3419. ecCodewordsPerBlock: 30,
  3420. ecBlocks: [{ numBlocks: 33, dataCodewordsPerBlock: 16 }, { numBlocks: 4, dataCodewordsPerBlock: 17 }]
  3421. },
  3422. {
  3423. ecCodewordsPerBlock: 28,
  3424. ecBlocks: [{ numBlocks: 28, dataCodewordsPerBlock: 22 }, { numBlocks: 6, dataCodewordsPerBlock: 23 }]
  3425. }
  3426. ]
  3427. },
  3428. {
  3429. infoBits: 0x1b08e,
  3430. versionNumber: 27,
  3431. alignmentPatternCenters: [6, 34, 62, 90, 118],
  3432. errorCorrectionLevels: [
  3433. {
  3434. ecCodewordsPerBlock: 28,
  3435. ecBlocks: [{ numBlocks: 22, dataCodewordsPerBlock: 45 }, { numBlocks: 3, dataCodewordsPerBlock: 46 }]
  3436. },
  3437. {
  3438. ecCodewordsPerBlock: 30,
  3439. ecBlocks: [{ numBlocks: 8, dataCodewordsPerBlock: 122 }, { numBlocks: 4, dataCodewordsPerBlock: 123 }]
  3440. },
  3441. {
  3442. ecCodewordsPerBlock: 30,
  3443. ecBlocks: [{ numBlocks: 12, dataCodewordsPerBlock: 15 }, { numBlocks: 28, dataCodewordsPerBlock: 16 }]
  3444. },
  3445. {
  3446. ecCodewordsPerBlock: 30,
  3447. ecBlocks: [{ numBlocks: 8, dataCodewordsPerBlock: 23 }, { numBlocks: 26, dataCodewordsPerBlock: 24 }]
  3448. }
  3449. ]
  3450. },
  3451. {
  3452. infoBits: 0x1cc1a,
  3453. versionNumber: 28,
  3454. alignmentPatternCenters: [6, 26, 50, 74, 98, 122],
  3455. errorCorrectionLevels: [
  3456. {
  3457. ecCodewordsPerBlock: 28,
  3458. ecBlocks: [{ numBlocks: 3, dataCodewordsPerBlock: 45 }, { numBlocks: 23, dataCodewordsPerBlock: 46 }]
  3459. },
  3460. {
  3461. ecCodewordsPerBlock: 30,
  3462. ecBlocks: [{ numBlocks: 3, dataCodewordsPerBlock: 117 }, { numBlocks: 10, dataCodewordsPerBlock: 118 }]
  3463. },
  3464. {
  3465. ecCodewordsPerBlock: 30,
  3466. ecBlocks: [{ numBlocks: 11, dataCodewordsPerBlock: 15 }, { numBlocks: 31, dataCodewordsPerBlock: 16 }]
  3467. },
  3468. {
  3469. ecCodewordsPerBlock: 30,
  3470. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 24 }, { numBlocks: 31, dataCodewordsPerBlock: 25 }]
  3471. }
  3472. ]
  3473. },
  3474. {
  3475. infoBits: 0x1d33f,
  3476. versionNumber: 29,
  3477. alignmentPatternCenters: [6, 30, 54, 78, 102, 126],
  3478. errorCorrectionLevels: [
  3479. {
  3480. ecCodewordsPerBlock: 28,
  3481. ecBlocks: [{ numBlocks: 21, dataCodewordsPerBlock: 45 }, { numBlocks: 7, dataCodewordsPerBlock: 46 }]
  3482. },
  3483. {
  3484. ecCodewordsPerBlock: 30,
  3485. ecBlocks: [{ numBlocks: 7, dataCodewordsPerBlock: 116 }, { numBlocks: 7, dataCodewordsPerBlock: 117 }]
  3486. },
  3487. {
  3488. ecCodewordsPerBlock: 30,
  3489. ecBlocks: [{ numBlocks: 19, dataCodewordsPerBlock: 15 }, { numBlocks: 26, dataCodewordsPerBlock: 16 }]
  3490. },
  3491. {
  3492. ecCodewordsPerBlock: 30,
  3493. ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 23 }, { numBlocks: 37, dataCodewordsPerBlock: 24 }]
  3494. }
  3495. ]
  3496. },
  3497. {
  3498. infoBits: 0x1ed75,
  3499. versionNumber: 30,
  3500. alignmentPatternCenters: [6, 26, 52, 78, 104, 130],
  3501. errorCorrectionLevels: [
  3502. {
  3503. ecCodewordsPerBlock: 28,
  3504. ecBlocks: [{ numBlocks: 19, dataCodewordsPerBlock: 47 }, { numBlocks: 10, dataCodewordsPerBlock: 48 }]
  3505. },
  3506. {
  3507. ecCodewordsPerBlock: 30,
  3508. ecBlocks: [{ numBlocks: 5, dataCodewordsPerBlock: 115 }, { numBlocks: 10, dataCodewordsPerBlock: 116 }]
  3509. },
  3510. {
  3511. ecCodewordsPerBlock: 30,
  3512. ecBlocks: [{ numBlocks: 23, dataCodewordsPerBlock: 15 }, { numBlocks: 25, dataCodewordsPerBlock: 16 }]
  3513. },
  3514. {
  3515. ecCodewordsPerBlock: 30,
  3516. ecBlocks: [{ numBlocks: 15, dataCodewordsPerBlock: 24 }, { numBlocks: 25, dataCodewordsPerBlock: 25 }]
  3517. }
  3518. ]
  3519. },
  3520. {
  3521. infoBits: 0x1f250,
  3522. versionNumber: 31,
  3523. alignmentPatternCenters: [6, 30, 56, 82, 108, 134],
  3524. errorCorrectionLevels: [
  3525. {
  3526. ecCodewordsPerBlock: 28,
  3527. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 46 }, { numBlocks: 29, dataCodewordsPerBlock: 47 }]
  3528. },
  3529. {
  3530. ecCodewordsPerBlock: 30,
  3531. ecBlocks: [{ numBlocks: 13, dataCodewordsPerBlock: 115 }, { numBlocks: 3, dataCodewordsPerBlock: 116 }]
  3532. },
  3533. {
  3534. ecCodewordsPerBlock: 30,
  3535. ecBlocks: [{ numBlocks: 23, dataCodewordsPerBlock: 15 }, { numBlocks: 28, dataCodewordsPerBlock: 16 }]
  3536. },
  3537. {
  3538. ecCodewordsPerBlock: 30,
  3539. ecBlocks: [{ numBlocks: 42, dataCodewordsPerBlock: 24 }, { numBlocks: 1, dataCodewordsPerBlock: 25 }]
  3540. }
  3541. ]
  3542. },
  3543. {
  3544. infoBits: 0x209d5,
  3545. versionNumber: 32,
  3546. alignmentPatternCenters: [6, 34, 60, 86, 112, 138],
  3547. errorCorrectionLevels: [
  3548. {
  3549. ecCodewordsPerBlock: 28,
  3550. ecBlocks: [{ numBlocks: 10, dataCodewordsPerBlock: 46 }, { numBlocks: 23, dataCodewordsPerBlock: 47 }]
  3551. },
  3552. {
  3553. ecCodewordsPerBlock: 30,
  3554. ecBlocks: [{ numBlocks: 17, dataCodewordsPerBlock: 115 }]
  3555. },
  3556. {
  3557. ecCodewordsPerBlock: 30,
  3558. ecBlocks: [{ numBlocks: 19, dataCodewordsPerBlock: 15 }, { numBlocks: 35, dataCodewordsPerBlock: 16 }]
  3559. },
  3560. {
  3561. ecCodewordsPerBlock: 30,
  3562. ecBlocks: [{ numBlocks: 10, dataCodewordsPerBlock: 24 }, { numBlocks: 35, dataCodewordsPerBlock: 25 }]
  3563. }
  3564. ]
  3565. },
  3566. {
  3567. infoBits: 0x216f0,
  3568. versionNumber: 33,
  3569. alignmentPatternCenters: [6, 30, 58, 86, 114, 142],
  3570. errorCorrectionLevels: [
  3571. {
  3572. ecCodewordsPerBlock: 28,
  3573. ecBlocks: [{ numBlocks: 14, dataCodewordsPerBlock: 46 }, { numBlocks: 21, dataCodewordsPerBlock: 47 }]
  3574. },
  3575. {
  3576. ecCodewordsPerBlock: 30,
  3577. ecBlocks: [{ numBlocks: 17, dataCodewordsPerBlock: 115 }, { numBlocks: 1, dataCodewordsPerBlock: 116 }]
  3578. },
  3579. {
  3580. ecCodewordsPerBlock: 30,
  3581. ecBlocks: [{ numBlocks: 11, dataCodewordsPerBlock: 15 }, { numBlocks: 46, dataCodewordsPerBlock: 16 }]
  3582. },
  3583. {
  3584. ecCodewordsPerBlock: 30,
  3585. ecBlocks: [{ numBlocks: 29, dataCodewordsPerBlock: 24 }, { numBlocks: 19, dataCodewordsPerBlock: 25 }]
  3586. }
  3587. ]
  3588. },
  3589. {
  3590. infoBits: 0x228ba,
  3591. versionNumber: 34,
  3592. alignmentPatternCenters: [6, 34, 62, 90, 118, 146],
  3593. errorCorrectionLevels: [
  3594. {
  3595. ecCodewordsPerBlock: 28,
  3596. ecBlocks: [{ numBlocks: 14, dataCodewordsPerBlock: 46 }, { numBlocks: 23, dataCodewordsPerBlock: 47 }]
  3597. },
  3598. {
  3599. ecCodewordsPerBlock: 30,
  3600. ecBlocks: [{ numBlocks: 13, dataCodewordsPerBlock: 115 }, { numBlocks: 6, dataCodewordsPerBlock: 116 }]
  3601. },
  3602. {
  3603. ecCodewordsPerBlock: 30,
  3604. ecBlocks: [{ numBlocks: 59, dataCodewordsPerBlock: 16 }, { numBlocks: 1, dataCodewordsPerBlock: 17 }]
  3605. },
  3606. {
  3607. ecCodewordsPerBlock: 30,
  3608. ecBlocks: [{ numBlocks: 44, dataCodewordsPerBlock: 24 }, { numBlocks: 7, dataCodewordsPerBlock: 25 }]
  3609. }
  3610. ]
  3611. },
  3612. {
  3613. infoBits: 0x2379f,
  3614. versionNumber: 35,
  3615. alignmentPatternCenters: [6, 30, 54, 78, 102, 126, 150],
  3616. errorCorrectionLevels: [
  3617. {
  3618. ecCodewordsPerBlock: 28,
  3619. ecBlocks: [{ numBlocks: 12, dataCodewordsPerBlock: 47 }, { numBlocks: 26, dataCodewordsPerBlock: 48 }]
  3620. },
  3621. {
  3622. ecCodewordsPerBlock: 30,
  3623. ecBlocks: [{ numBlocks: 12, dataCodewordsPerBlock: 121 }, { numBlocks: 7, dataCodewordsPerBlock: 122 }]
  3624. },
  3625. {
  3626. ecCodewordsPerBlock: 30,
  3627. ecBlocks: [{ numBlocks: 22, dataCodewordsPerBlock: 15 }, { numBlocks: 41, dataCodewordsPerBlock: 16 }]
  3628. },
  3629. {
  3630. ecCodewordsPerBlock: 30,
  3631. ecBlocks: [{ numBlocks: 39, dataCodewordsPerBlock: 24 }, { numBlocks: 14, dataCodewordsPerBlock: 25 }]
  3632. }
  3633. ]
  3634. },
  3635. {
  3636. infoBits: 0x24b0b,
  3637. versionNumber: 36,
  3638. alignmentPatternCenters: [6, 24, 50, 76, 102, 128, 154],
  3639. errorCorrectionLevels: [
  3640. {
  3641. ecCodewordsPerBlock: 28,
  3642. ecBlocks: [{ numBlocks: 6, dataCodewordsPerBlock: 47 }, { numBlocks: 34, dataCodewordsPerBlock: 48 }]
  3643. },
  3644. {
  3645. ecCodewordsPerBlock: 30,
  3646. ecBlocks: [{ numBlocks: 6, dataCodewordsPerBlock: 121 }, { numBlocks: 14, dataCodewordsPerBlock: 122 }]
  3647. },
  3648. {
  3649. ecCodewordsPerBlock: 30,
  3650. ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 15 }, { numBlocks: 64, dataCodewordsPerBlock: 16 }]
  3651. },
  3652. {
  3653. ecCodewordsPerBlock: 30,
  3654. ecBlocks: [{ numBlocks: 46, dataCodewordsPerBlock: 24 }, { numBlocks: 10, dataCodewordsPerBlock: 25 }]
  3655. }
  3656. ]
  3657. },
  3658. {
  3659. infoBits: 0x2542e,
  3660. versionNumber: 37,
  3661. alignmentPatternCenters: [6, 28, 54, 80, 106, 132, 158],
  3662. errorCorrectionLevels: [
  3663. {
  3664. ecCodewordsPerBlock: 28,
  3665. ecBlocks: [{ numBlocks: 29, dataCodewordsPerBlock: 46 }, { numBlocks: 14, dataCodewordsPerBlock: 47 }]
  3666. },
  3667. {
  3668. ecCodewordsPerBlock: 30,
  3669. ecBlocks: [{ numBlocks: 17, dataCodewordsPerBlock: 122 }, { numBlocks: 4, dataCodewordsPerBlock: 123 }]
  3670. },
  3671. {
  3672. ecCodewordsPerBlock: 30,
  3673. ecBlocks: [{ numBlocks: 24, dataCodewordsPerBlock: 15 }, { numBlocks: 46, dataCodewordsPerBlock: 16 }]
  3674. },
  3675. {
  3676. ecCodewordsPerBlock: 30,
  3677. ecBlocks: [{ numBlocks: 49, dataCodewordsPerBlock: 24 }, { numBlocks: 10, dataCodewordsPerBlock: 25 }]
  3678. }
  3679. ]
  3680. },
  3681. {
  3682. infoBits: 0x26a64,
  3683. versionNumber: 38,
  3684. alignmentPatternCenters: [6, 32, 58, 84, 110, 136, 162],
  3685. errorCorrectionLevels: [
  3686. {
  3687. ecCodewordsPerBlock: 28,
  3688. ecBlocks: [{ numBlocks: 13, dataCodewordsPerBlock: 46 }, { numBlocks: 32, dataCodewordsPerBlock: 47 }]
  3689. },
  3690. {
  3691. ecCodewordsPerBlock: 30,
  3692. ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 122 }, { numBlocks: 18, dataCodewordsPerBlock: 123 }]
  3693. },
  3694. {
  3695. ecCodewordsPerBlock: 30,
  3696. ecBlocks: [{ numBlocks: 42, dataCodewordsPerBlock: 15 }, { numBlocks: 32, dataCodewordsPerBlock: 16 }]
  3697. },
  3698. {
  3699. ecCodewordsPerBlock: 30,
  3700. ecBlocks: [{ numBlocks: 48, dataCodewordsPerBlock: 24 }, { numBlocks: 14, dataCodewordsPerBlock: 25 }]
  3701. }
  3702. ]
  3703. },
  3704. {
  3705. infoBits: 0x27541,
  3706. versionNumber: 39,
  3707. alignmentPatternCenters: [6, 26, 54, 82, 110, 138, 166],
  3708. errorCorrectionLevels: [
  3709. {
  3710. ecCodewordsPerBlock: 28,
  3711. ecBlocks: [{ numBlocks: 40, dataCodewordsPerBlock: 47 }, { numBlocks: 7, dataCodewordsPerBlock: 48 }]
  3712. },
  3713. {
  3714. ecCodewordsPerBlock: 30,
  3715. ecBlocks: [{ numBlocks: 20, dataCodewordsPerBlock: 117 }, { numBlocks: 4, dataCodewordsPerBlock: 118 }]
  3716. },
  3717. {
  3718. ecCodewordsPerBlock: 30,
  3719. ecBlocks: [{ numBlocks: 10, dataCodewordsPerBlock: 15 }, { numBlocks: 67, dataCodewordsPerBlock: 16 }]
  3720. },
  3721. {
  3722. ecCodewordsPerBlock: 30,
  3723. ecBlocks: [{ numBlocks: 43, dataCodewordsPerBlock: 24 }, { numBlocks: 22, dataCodewordsPerBlock: 25 }]
  3724. }
  3725. ]
  3726. },
  3727. {
  3728. infoBits: 0x28c69,
  3729. versionNumber: 40,
  3730. alignmentPatternCenters: [6, 30, 58, 86, 114, 142, 170],
  3731. errorCorrectionLevels: [
  3732. {
  3733. ecCodewordsPerBlock: 28,
  3734. ecBlocks: [{ numBlocks: 18, dataCodewordsPerBlock: 47 }, { numBlocks: 31, dataCodewordsPerBlock: 48 }]
  3735. },
  3736. {
  3737. ecCodewordsPerBlock: 30,
  3738. ecBlocks: [{ numBlocks: 19, dataCodewordsPerBlock: 118 }, { numBlocks: 6, dataCodewordsPerBlock: 119 }]
  3739. },
  3740. {
  3741. ecCodewordsPerBlock: 30,
  3742. ecBlocks: [{ numBlocks: 20, dataCodewordsPerBlock: 15 }, { numBlocks: 61, dataCodewordsPerBlock: 16 }]
  3743. },
  3744. {
  3745. ecCodewordsPerBlock: 30,
  3746. ecBlocks: [{ numBlocks: 34, dataCodewordsPerBlock: 24 }, { numBlocks: 34, dataCodewordsPerBlock: 25 }]
  3747. }
  3748. ]
  3749. }
  3750. ];
  3751.  
  3752. /**
  3753. * @module index
  3754. * @author nuintun
  3755. * @author Cosmo Wolfe
  3756. */
  3757. function numBitsDiffering(x, y) {
  3758. var z = x ^ y;
  3759. var bitCount = 0;
  3760. while (z) {
  3761. bitCount++;
  3762. z &= z - 1;
  3763. }
  3764. return bitCount;
  3765. }
  3766. function pushBit(bit, byte) {
  3767. return (byte << 1) | bit;
  3768. }
  3769. var FORMAT_INFO_TABLE = [
  3770. { bits: 0x5412, formatInfo: { errorCorrectionLevel: 0, dataMask: 0 } },
  3771. { bits: 0x5125, formatInfo: { errorCorrectionLevel: 0, dataMask: 1 } },
  3772. { bits: 0x5e7c, formatInfo: { errorCorrectionLevel: 0, dataMask: 2 } },
  3773. { bits: 0x5b4b, formatInfo: { errorCorrectionLevel: 0, dataMask: 3 } },
  3774. { bits: 0x45f9, formatInfo: { errorCorrectionLevel: 0, dataMask: 4 } },
  3775. { bits: 0x40ce, formatInfo: { errorCorrectionLevel: 0, dataMask: 5 } },
  3776. { bits: 0x4f97, formatInfo: { errorCorrectionLevel: 0, dataMask: 6 } },
  3777. { bits: 0x4aa0, formatInfo: { errorCorrectionLevel: 0, dataMask: 7 } },
  3778. { bits: 0x77c4, formatInfo: { errorCorrectionLevel: 1, dataMask: 0 } },
  3779. { bits: 0x72f3, formatInfo: { errorCorrectionLevel: 1, dataMask: 1 } },
  3780. { bits: 0x7daa, formatInfo: { errorCorrectionLevel: 1, dataMask: 2 } },
  3781. { bits: 0x789d, formatInfo: { errorCorrectionLevel: 1, dataMask: 3 } },
  3782. { bits: 0x662f, formatInfo: { errorCorrectionLevel: 1, dataMask: 4 } },
  3783. { bits: 0x6318, formatInfo: { errorCorrectionLevel: 1, dataMask: 5 } },
  3784. { bits: 0x6c41, formatInfo: { errorCorrectionLevel: 1, dataMask: 6 } },
  3785. { bits: 0x6976, formatInfo: { errorCorrectionLevel: 1, dataMask: 7 } },
  3786. { bits: 0x1689, formatInfo: { errorCorrectionLevel: 2, dataMask: 0 } },
  3787. { bits: 0x13be, formatInfo: { errorCorrectionLevel: 2, dataMask: 1 } },
  3788. { bits: 0x1ce7, formatInfo: { errorCorrectionLevel: 2, dataMask: 2 } },
  3789. { bits: 0x19d0, formatInfo: { errorCorrectionLevel: 2, dataMask: 3 } },
  3790. { bits: 0x0762, formatInfo: { errorCorrectionLevel: 2, dataMask: 4 } },
  3791. { bits: 0x0255, formatInfo: { errorCorrectionLevel: 2, dataMask: 5 } },
  3792. { bits: 0x0d0c, formatInfo: { errorCorrectionLevel: 2, dataMask: 6 } },
  3793. { bits: 0x083b, formatInfo: { errorCorrectionLevel: 2, dataMask: 7 } },
  3794. { bits: 0x355f, formatInfo: { errorCorrectionLevel: 3, dataMask: 0 } },
  3795. { bits: 0x3068, formatInfo: { errorCorrectionLevel: 3, dataMask: 1 } },
  3796. { bits: 0x3f31, formatInfo: { errorCorrectionLevel: 3, dataMask: 2 } },
  3797. { bits: 0x3a06, formatInfo: { errorCorrectionLevel: 3, dataMask: 3 } },
  3798. { bits: 0x24b4, formatInfo: { errorCorrectionLevel: 3, dataMask: 4 } },
  3799. { bits: 0x2183, formatInfo: { errorCorrectionLevel: 3, dataMask: 5 } },
  3800. { bits: 0x2eda, formatInfo: { errorCorrectionLevel: 3, dataMask: 6 } },
  3801. { bits: 0x2bed, formatInfo: { errorCorrectionLevel: 3, dataMask: 7 } }
  3802. ];
  3803. function buildFunctionPatternMask(version) {
  3804. var dimension = 17 + 4 * version.versionNumber;
  3805. var matrix = BitMatrix.createEmpty(dimension, dimension);
  3806. matrix.setRegion(0, 0, 9, 9, true); // Top left finder pattern + separator + format
  3807. matrix.setRegion(dimension - 8, 0, 8, 9, true); // Top right finder pattern + separator + format
  3808. matrix.setRegion(0, dimension - 8, 9, 8, true); // Bottom left finder pattern + separator + format
  3809. // Alignment patterns
  3810. for (var _i = 0, _a = version.alignmentPatternCenters; _i < _a.length; _i++) {
  3811. var x = _a[_i];
  3812. for (var _b = 0, _c = version.alignmentPatternCenters; _b < _c.length; _b++) {
  3813. var y = _c[_b];
  3814. if (!((x === 6 && y === 6) || (x === 6 && y === dimension - 7) || (x === dimension - 7 && y === 6))) {
  3815. matrix.setRegion(x - 2, y - 2, 5, 5, true);
  3816. }
  3817. }
  3818. }
  3819. matrix.setRegion(6, 9, 1, dimension - 17, true); // Vertical timing pattern
  3820. matrix.setRegion(9, 6, dimension - 17, 1, true); // Horizontal timing pattern
  3821. if (version.versionNumber > 6) {
  3822. matrix.setRegion(dimension - 11, 0, 3, 6, true); // Version info, top right
  3823. matrix.setRegion(0, dimension - 11, 6, 3, true); // Version info, bottom left
  3824. }
  3825. return matrix;
  3826. }
  3827. function readCodewords(matrix, version, formatInfo) {
  3828. var dimension = matrix.height;
  3829. var maskFunc = getMaskFunc(formatInfo.dataMask);
  3830. var functionPatternMask = buildFunctionPatternMask(version);
  3831. var bitsRead = 0;
  3832. var currentByte = 0;
  3833. var codewords = [];
  3834. // Read columns in pairs, from right to left
  3835. var readingUp = true;
  3836. for (var columnIndex = dimension - 1; columnIndex > 0; columnIndex -= 2) {
  3837. if (columnIndex === 6) {
  3838. // Skip whole column with vertical alignment pattern;
  3839. columnIndex--;
  3840. }
  3841. for (var i = 0; i < dimension; i++) {
  3842. var y = readingUp ? dimension - 1 - i : i;
  3843. for (var columnOffset = 0; columnOffset < 2; columnOffset++) {
  3844. var x = columnIndex - columnOffset;
  3845. if (!functionPatternMask.get(x, y)) {
  3846. bitsRead++;
  3847. var bit = matrix.get(x, y);
  3848. if (maskFunc(x, y)) {
  3849. bit = !bit;
  3850. }
  3851. currentByte = pushBit(bit, currentByte);
  3852. if (bitsRead === 8) {
  3853. // Whole bytes
  3854. codewords.push(currentByte);
  3855. bitsRead = 0;
  3856. currentByte = 0;
  3857. }
  3858. }
  3859. }
  3860. }
  3861. readingUp = !readingUp;
  3862. }
  3863. return codewords;
  3864. }
  3865. function readVersion(matrix) {
  3866. var dimension = matrix.height;
  3867. var provisionalVersion = Math.floor((dimension - 17) / 4);
  3868. if (provisionalVersion <= 6) {
  3869. // 6 and under dont have version info in the QR code
  3870. return VERSIONS[provisionalVersion - 1];
  3871. }
  3872. var topRightVersionBits = 0;
  3873. for (var y = 5; y >= 0; y--) {
  3874. for (var x = dimension - 9; x >= dimension - 11; x--) {
  3875. topRightVersionBits = pushBit(matrix.get(x, y), topRightVersionBits);
  3876. }
  3877. }
  3878. var bottomLeftVersionBits = 0;
  3879. for (var x = 5; x >= 0; x--) {
  3880. for (var y = dimension - 9; y >= dimension - 11; y--) {
  3881. bottomLeftVersionBits = pushBit(matrix.get(x, y), bottomLeftVersionBits);
  3882. }
  3883. }
  3884. var bestVersion;
  3885. var bestDifference = Infinity;
  3886. for (var _i = 0, VERSIONS_1 = VERSIONS; _i < VERSIONS_1.length; _i++) {
  3887. var version = VERSIONS_1[_i];
  3888. if (version.infoBits === topRightVersionBits || version.infoBits === bottomLeftVersionBits) {
  3889. return version;
  3890. }
  3891. var difference = numBitsDiffering(topRightVersionBits, version.infoBits);
  3892. if (difference < bestDifference) {
  3893. bestVersion = version;
  3894. bestDifference = difference;
  3895. }
  3896. difference = numBitsDiffering(bottomLeftVersionBits, version.infoBits);
  3897. if (difference < bestDifference) {
  3898. bestVersion = version;
  3899. bestDifference = difference;
  3900. }
  3901. }
  3902. // We can tolerate up to 3 bits of error since no two version info codewords will
  3903. // differ in less than 8 bits.
  3904. if (bestDifference <= 3) {
  3905. return bestVersion;
  3906. }
  3907. }
  3908. function readFormatInformation(matrix) {
  3909. var topLeftFormatInfoBits = 0;
  3910. for (var x = 0; x <= 8; x++) {
  3911. if (x !== 6) {
  3912. // Skip timing pattern bit
  3913. topLeftFormatInfoBits = pushBit(matrix.get(x, 8), topLeftFormatInfoBits);
  3914. }
  3915. }
  3916. for (var y = 7; y >= 0; y--) {
  3917. if (y !== 6) {
  3918. // Skip timing pattern bit
  3919. topLeftFormatInfoBits = pushBit(matrix.get(8, y), topLeftFormatInfoBits);
  3920. }
  3921. }
  3922. var dimension = matrix.height;
  3923. var topRightBottomRightFormatInfoBits = 0;
  3924. for (var y = dimension - 1; y >= dimension - 7; y--) {
  3925. // bottom left
  3926. topRightBottomRightFormatInfoBits = pushBit(matrix.get(8, y), topRightBottomRightFormatInfoBits);
  3927. }
  3928. for (var x = dimension - 8; x < dimension; x++) {
  3929. // top right
  3930. topRightBottomRightFormatInfoBits = pushBit(matrix.get(x, 8), topRightBottomRightFormatInfoBits);
  3931. }
  3932. var bestDifference = Infinity;
  3933. var bestFormatInfo = null;
  3934. for (var _i = 0, FORMAT_INFO_TABLE_1 = FORMAT_INFO_TABLE; _i < FORMAT_INFO_TABLE_1.length; _i++) {
  3935. var _a = FORMAT_INFO_TABLE_1[_i], bits = _a.bits, formatInfo = _a.formatInfo;
  3936. if (bits === topLeftFormatInfoBits || bits === topRightBottomRightFormatInfoBits) {
  3937. return formatInfo;
  3938. }
  3939. var difference = numBitsDiffering(topLeftFormatInfoBits, bits);
  3940. if (difference < bestDifference) {
  3941. bestFormatInfo = formatInfo;
  3942. bestDifference = difference;
  3943. }
  3944. if (topLeftFormatInfoBits !== topRightBottomRightFormatInfoBits) {
  3945. // also try the other option
  3946. difference = numBitsDiffering(topRightBottomRightFormatInfoBits, bits);
  3947. if (difference < bestDifference) {
  3948. bestFormatInfo = formatInfo;
  3949. bestDifference = difference;
  3950. }
  3951. }
  3952. }
  3953. // Hamming distance of the 32 masked codes is 7, by construction, so <= 3 bits differing means we found a match
  3954. if (bestDifference <= 3) {
  3955. return bestFormatInfo;
  3956. }
  3957. return null;
  3958. }
  3959. function getDataBlocks(codewords, version, errorCorrectionLevel) {
  3960. var dataBlocks = [];
  3961. var ecInfo = version.errorCorrectionLevels[errorCorrectionLevel];
  3962. var totalCodewords = 0;
  3963. ecInfo.ecBlocks.forEach(function (block) {
  3964. for (var i = 0; i < block.numBlocks; i++) {
  3965. dataBlocks.push({ numDataCodewords: block.dataCodewordsPerBlock, codewords: [] });
  3966. totalCodewords += block.dataCodewordsPerBlock + ecInfo.ecCodewordsPerBlock;
  3967. }
  3968. });
  3969. // In some cases the QR code will be malformed enough that we pull off more or less than we should.
  3970. // If we pull off less there's nothing we can do.
  3971. // If we pull off more we can safely truncate
  3972. if (codewords.length < totalCodewords) {
  3973. return null;
  3974. }
  3975. codewords = codewords.slice(0, totalCodewords);
  3976. var shortBlockSize = ecInfo.ecBlocks[0].dataCodewordsPerBlock;
  3977. // Pull codewords to fill the blocks up to the minimum size
  3978. for (var i = 0; i < shortBlockSize; i++) {
  3979. for (var _i = 0, dataBlocks_1 = dataBlocks; _i < dataBlocks_1.length; _i++) {
  3980. var dataBlock = dataBlocks_1[_i];
  3981. dataBlock.codewords.push(codewords.shift());
  3982. }
  3983. }
  3984. // If there are any large blocks, pull codewords to fill the last element of those
  3985. if (ecInfo.ecBlocks.length > 1) {
  3986. var smallBlockCount = ecInfo.ecBlocks[0].numBlocks;
  3987. var largeBlockCount = ecInfo.ecBlocks[1].numBlocks;
  3988. for (var i = 0; i < largeBlockCount; i++) {
  3989. dataBlocks[smallBlockCount + i].codewords.push(codewords.shift());
  3990. }
  3991. }
  3992. // Add the rest of the codewords to the blocks. These are the error correction codewords.
  3993. while (codewords.length > 0) {
  3994. for (var _a = 0, dataBlocks_2 = dataBlocks; _a < dataBlocks_2.length; _a++) {
  3995. var dataBlock = dataBlocks_2[_a];
  3996. dataBlock.codewords.push(codewords.shift());
  3997. }
  3998. }
  3999. return dataBlocks;
  4000. }
  4001. function decodeMatrix(matrix) {
  4002. var version = readVersion(matrix);
  4003. if (!version) {
  4004. return null;
  4005. }
  4006. var formatInfo = readFormatInformation(matrix);
  4007. if (!formatInfo) {
  4008. return null;
  4009. }
  4010. var codewords = readCodewords(matrix, version, formatInfo);
  4011. var dataBlocks = getDataBlocks(codewords, version, formatInfo.errorCorrectionLevel);
  4012. if (!dataBlocks) {
  4013. return null;
  4014. }
  4015. // Count total number of data bytes
  4016. var totalBytes = dataBlocks.reduce(function (a, b) { return a + b.numDataCodewords; }, 0);
  4017. var resultBytes = new Uint8ClampedArray(totalBytes);
  4018. var resultIndex = 0;
  4019. for (var _i = 0, dataBlocks_3 = dataBlocks; _i < dataBlocks_3.length; _i++) {
  4020. var dataBlock = dataBlocks_3[_i];
  4021. var correctedBytes = rsDecode(dataBlock.codewords, dataBlock.codewords.length - dataBlock.numDataCodewords);
  4022. if (!correctedBytes) {
  4023. return null;
  4024. }
  4025. for (var i = 0; i < dataBlock.numDataCodewords; i++) {
  4026. resultBytes[resultIndex++] = correctedBytes[i];
  4027. }
  4028. }
  4029. try {
  4030. return bytesDecode(resultBytes, version.versionNumber, formatInfo.errorCorrectionLevel);
  4031. }
  4032. catch (_a) {
  4033. return null;
  4034. }
  4035. }
  4036. function decode(matrix) {
  4037. if (matrix == null) {
  4038. return null;
  4039. }
  4040. var result = decodeMatrix(matrix);
  4041. if (result) {
  4042. return result;
  4043. }
  4044. // Decoding didn't work, try mirroring the QR across the topLeft -> bottomRight line.
  4045. for (var x = 0; x < matrix.width; x++) {
  4046. for (var y = x + 1; y < matrix.height; y++) {
  4047. if (matrix.get(x, y) !== matrix.get(y, x)) {
  4048. matrix.set(x, y, !matrix.get(x, y));
  4049. matrix.set(y, x, !matrix.get(y, x));
  4050. }
  4051. }
  4052. }
  4053. return decodeMatrix(matrix);
  4054. }
  4055.  
  4056. /**
  4057. * @module extractor
  4058. * @author nuintun
  4059. * @author Cosmo Wolfe
  4060. */
  4061. function squareToQuadrilateral(p1, p2, p3, p4) {
  4062. var dx3 = p1.x - p2.x + p3.x - p4.x;
  4063. var dy3 = p1.y - p2.y + p3.y - p4.y;
  4064. if (dx3 === 0 && dy3 === 0) {
  4065. // Affine
  4066. return {
  4067. a11: p2.x - p1.x,
  4068. a12: p2.y - p1.y,
  4069. a13: 0,
  4070. a21: p3.x - p2.x,
  4071. a22: p3.y - p2.y,
  4072. a23: 0,
  4073. a31: p1.x,
  4074. a32: p1.y,
  4075. a33: 1
  4076. };
  4077. }
  4078. else {
  4079. var dx1 = p2.x - p3.x;
  4080. var dx2 = p4.x - p3.x;
  4081. var dy1 = p2.y - p3.y;
  4082. var dy2 = p4.y - p3.y;
  4083. var denominator = dx1 * dy2 - dx2 * dy1;
  4084. var a13 = (dx3 * dy2 - dx2 * dy3) / denominator;
  4085. var a23 = (dx1 * dy3 - dx3 * dy1) / denominator;
  4086. return {
  4087. a11: p2.x - p1.x + a13 * p2.x,
  4088. a12: p2.y - p1.y + a13 * p2.y,
  4089. a13: a13,
  4090. a21: p4.x - p1.x + a23 * p4.x,
  4091. a22: p4.y - p1.y + a23 * p4.y,
  4092. a23: a23,
  4093. a31: p1.x,
  4094. a32: p1.y,
  4095. a33: 1
  4096. };
  4097. }
  4098. }
  4099. function quadrilateralToSquare(p1, p2, p3, p4) {
  4100. // Here, the adjoint serves as the inverse:
  4101. var sToQ = squareToQuadrilateral(p1, p2, p3, p4);
  4102. return {
  4103. a11: sToQ.a22 * sToQ.a33 - sToQ.a23 * sToQ.a32,
  4104. a12: sToQ.a13 * sToQ.a32 - sToQ.a12 * sToQ.a33,
  4105. a13: sToQ.a12 * sToQ.a23 - sToQ.a13 * sToQ.a22,
  4106. a21: sToQ.a23 * sToQ.a31 - sToQ.a21 * sToQ.a33,
  4107. a22: sToQ.a11 * sToQ.a33 - sToQ.a13 * sToQ.a31,
  4108. a23: sToQ.a13 * sToQ.a21 - sToQ.a11 * sToQ.a23,
  4109. a31: sToQ.a21 * sToQ.a32 - sToQ.a22 * sToQ.a31,
  4110. a32: sToQ.a12 * sToQ.a31 - sToQ.a11 * sToQ.a32,
  4111. a33: sToQ.a11 * sToQ.a22 - sToQ.a12 * sToQ.a21
  4112. };
  4113. }
  4114. function times(a, b) {
  4115. return {
  4116. a11: a.a11 * b.a11 + a.a21 * b.a12 + a.a31 * b.a13,
  4117. a12: a.a12 * b.a11 + a.a22 * b.a12 + a.a32 * b.a13,
  4118. a13: a.a13 * b.a11 + a.a23 * b.a12 + a.a33 * b.a13,
  4119. a21: a.a11 * b.a21 + a.a21 * b.a22 + a.a31 * b.a23,
  4120. a22: a.a12 * b.a21 + a.a22 * b.a22 + a.a32 * b.a23,
  4121. a23: a.a13 * b.a21 + a.a23 * b.a22 + a.a33 * b.a23,
  4122. a31: a.a11 * b.a31 + a.a21 * b.a32 + a.a31 * b.a33,
  4123. a32: a.a12 * b.a31 + a.a22 * b.a32 + a.a32 * b.a33,
  4124. a33: a.a13 * b.a31 + a.a23 * b.a32 + a.a33 * b.a33
  4125. };
  4126. }
  4127. function extract(image, location) {
  4128. var qToS = quadrilateralToSquare({ x: 3.5, y: 3.5 }, { x: location.dimension - 3.5, y: 3.5 }, { x: location.dimension - 6.5, y: location.dimension - 6.5 }, { x: 3.5, y: location.dimension - 3.5 });
  4129. var sToQ = squareToQuadrilateral(location.topLeft, location.topRight, location.alignmentPattern, location.bottomLeft);
  4130. var transform = times(sToQ, qToS);
  4131. var matrix = BitMatrix.createEmpty(location.dimension, location.dimension);
  4132. var mappingFunction = function (x, y) {
  4133. var denominator = transform.a13 * x + transform.a23 * y + transform.a33;
  4134. return {
  4135. x: Math.max(0, (transform.a11 * x + transform.a21 * y + transform.a31) / denominator),
  4136. y: Math.max(0, (transform.a12 * x + transform.a22 * y + transform.a32) / denominator)
  4137. };
  4138. };
  4139. for (var y = 0; y < location.dimension; y++) {
  4140. for (var x = 0; x < location.dimension; x++) {
  4141. var xValue = x + 0.5;
  4142. var yValue = y + 0.5;
  4143. var sourcePixel = mappingFunction(xValue, yValue);
  4144. matrix.set(x, y, image.get(Math.floor(sourcePixel.x), Math.floor(sourcePixel.y)));
  4145. }
  4146. }
  4147. return {
  4148. matrix: matrix,
  4149. mappingFunction: mappingFunction
  4150. };
  4151. }
  4152.  
  4153. /**
  4154. * @module binarizer
  4155. * @author nuintun
  4156. * @author Cosmo Wolfe
  4157. */
  4158. var REGION_SIZE = 8;
  4159. var MIN_DYNAMIC_RANGE = 24;
  4160. function numBetween(value, min, max) {
  4161. return value < min ? min : value > max ? max : value;
  4162. }
  4163. // Like BitMatrix but accepts arbitry Uint8 values
  4164. var Matrix = /*#__PURE__*/ (function () {
  4165. function Matrix(width, height) {
  4166. this.width = width;
  4167. this.data = new Uint8ClampedArray(width * height);
  4168. }
  4169. Matrix.prototype.get = function (x, y) {
  4170. return this.data[y * this.width + x];
  4171. };
  4172. Matrix.prototype.set = function (x, y, value) {
  4173. this.data[y * this.width + x] = value;
  4174. };
  4175. return Matrix;
  4176. }());
  4177. function binarize(data, width, height, returnInverted) {
  4178. if (data.length !== width * height * 4) {
  4179. throw 'malformed data passed to binarizer';
  4180. }
  4181. // Convert image to greyscale
  4182. var greyscalePixels = new Matrix(width, height);
  4183. for (var x = 0; x < width; x++) {
  4184. for (var y = 0; y < height; y++) {
  4185. var r = data[(y * width + x) * 4 + 0];
  4186. var g = data[(y * width + x) * 4 + 1];
  4187. var b = data[(y * width + x) * 4 + 2];
  4188. greyscalePixels.set(x, y, 0.2126 * r + 0.7152 * g + 0.0722 * b);
  4189. }
  4190. }
  4191. var horizontalRegionCount = Math.ceil(width / REGION_SIZE);
  4192. var verticalRegionCount = Math.ceil(height / REGION_SIZE);
  4193. var blackPoints = new Matrix(horizontalRegionCount, verticalRegionCount);
  4194. for (var verticalRegion = 0; verticalRegion < verticalRegionCount; verticalRegion++) {
  4195. for (var hortizontalRegion = 0; hortizontalRegion < horizontalRegionCount; hortizontalRegion++) {
  4196. var sum = 0;
  4197. var min = Infinity;
  4198. var max = 0;
  4199. for (var y = 0; y < REGION_SIZE; y++) {
  4200. for (var x = 0; x < REGION_SIZE; x++) {
  4201. var pixelLumosity = greyscalePixels.get(hortizontalRegion * REGION_SIZE + x, verticalRegion * REGION_SIZE + y);
  4202. sum += pixelLumosity;
  4203. min = Math.min(min, pixelLumosity);
  4204. max = Math.max(max, pixelLumosity);
  4205. }
  4206. }
  4207. var average = sum / Math.pow(REGION_SIZE, 2);
  4208. if (max - min <= MIN_DYNAMIC_RANGE) {
  4209. // If variation within the block is low, assume this is a block with only light or only
  4210. // dark pixels. In that case we do not want to use the average, as it would divide this
  4211. // low contrast area into black and white pixels, essentially creating data out of noise.
  4212. //
  4213. // Default the blackpoint for these blocks to be half the min - effectively white them out
  4214. average = min / 2;
  4215. if (verticalRegion > 0 && hortizontalRegion > 0) {
  4216. // Correct the "white background" assumption for blocks that have neighbors by comparing
  4217. // the pixels in this block to the previously calculated black points. This is based on
  4218. // the fact that dark barcode symbology is always surrounded by some amount of light
  4219. // background for which reasonable black point estimates were made. The bp estimated at
  4220. // the boundaries is used for the interior.
  4221. // The (min < bp) is arbitrary but works better than other heuristics that were tried.
  4222. var averageNeighborBlackPoint = (blackPoints.get(hortizontalRegion, verticalRegion - 1) +
  4223. 2 * blackPoints.get(hortizontalRegion - 1, verticalRegion) +
  4224. blackPoints.get(hortizontalRegion - 1, verticalRegion - 1)) /
  4225. 4;
  4226. if (min < averageNeighborBlackPoint) {
  4227. average = averageNeighborBlackPoint;
  4228. }
  4229. }
  4230. }
  4231. blackPoints.set(hortizontalRegion, verticalRegion, average);
  4232. }
  4233. }
  4234. var inverted = null;
  4235. var binarized = BitMatrix.createEmpty(width, height);
  4236. if (returnInverted) {
  4237. inverted = BitMatrix.createEmpty(width, height);
  4238. }
  4239. for (var verticalRegion = 0; verticalRegion < verticalRegionCount; verticalRegion++) {
  4240. for (var hortizontalRegion = 0; hortizontalRegion < horizontalRegionCount; hortizontalRegion++) {
  4241. var left = numBetween(hortizontalRegion, 2, horizontalRegionCount - 3);
  4242. var top_1 = numBetween(verticalRegion, 2, verticalRegionCount - 3);
  4243. var sum = 0;
  4244. for (var xRegion = -2; xRegion <= 2; xRegion++) {
  4245. for (var yRegion = -2; yRegion <= 2; yRegion++) {
  4246. sum += blackPoints.get(left + xRegion, top_1 + yRegion);
  4247. }
  4248. }
  4249. var threshold = sum / 25;
  4250. for (var xRegion = 0; xRegion < REGION_SIZE; xRegion++) {
  4251. for (var yRegion = 0; yRegion < REGION_SIZE; yRegion++) {
  4252. var x = hortizontalRegion * REGION_SIZE + xRegion;
  4253. var y = verticalRegion * REGION_SIZE + yRegion;
  4254. var lum = greyscalePixels.get(x, y);
  4255. binarized.set(x, y, lum <= threshold);
  4256. if (returnInverted) {
  4257. inverted.set(x, y, !(lum <= threshold));
  4258. }
  4259. }
  4260. }
  4261. }
  4262. }
  4263. if (returnInverted) {
  4264. return { binarized: binarized, inverted: inverted };
  4265. }
  4266. return { binarized: binarized };
  4267. }
  4268.  
  4269. /**
  4270. * @module QRCode
  4271. * @author nuintun
  4272. * @author Cosmo Wolfe
  4273. */
  4274. function scan(matrix) {
  4275. var location = locate(matrix);
  4276. if (!location) {
  4277. return null;
  4278. }
  4279. var extracted = extract(matrix, location);
  4280. var decoded = decode(extracted.matrix);
  4281. if (!decoded) {
  4282. return null;
  4283. }
  4284. var dimension = location.dimension;
  4285. return __assign({}, decoded, { location: {
  4286. topLeft: extracted.mappingFunction(0, 0),
  4287. topRight: extracted.mappingFunction(dimension, 0),
  4288. bottomLeft: extracted.mappingFunction(0, dimension),
  4289. bottomRight: extracted.mappingFunction(dimension, dimension),
  4290. topLeftFinder: location.topLeft,
  4291. topRightFinder: location.topRight,
  4292. bottomLeftFinder: location.bottomLeft,
  4293. bottomRightAlignment: decoded.version > 1 ? location.alignmentPattern : null
  4294. } });
  4295. }
  4296. var defaultOptions = {
  4297. inversionAttempts: 'attemptBoth'
  4298. };
  4299. function disposeImageEvents(image) {
  4300. image.onload = null;
  4301. image.onerror = null;
  4302. }
  4303. var Decoder = /*#__PURE__*/ (function () {
  4304. function Decoder() {
  4305. this.options = defaultOptions;
  4306. }
  4307. /**
  4308. * @public
  4309. * @method setOptions
  4310. * @param {object} options
  4311. */
  4312. Decoder.prototype.setOptions = function (options) {
  4313. if (options === void 0) { options = {}; }
  4314. options = options || {};
  4315. this.options = __assign({}, defaultOptions, options);
  4316. };
  4317. /**
  4318. * @public
  4319. * @method decode
  4320. * @param {Uint8ClampedArray} data
  4321. * @param {number} width
  4322. * @param {number} height
  4323. * @returns {DecoderResult}
  4324. */
  4325. Decoder.prototype.decode = function (data, width, height) {
  4326. var options = this.options;
  4327. var shouldInvert = options.inversionAttempts === 'attemptBoth' || options.inversionAttempts === 'invertFirst';
  4328. var tryInvertedFirst = options.inversionAttempts === 'onlyInvert' || options.inversionAttempts === 'invertFirst';
  4329. var _a = binarize(data, width, height, shouldInvert), binarized = _a.binarized, inverted = _a.inverted;
  4330. var result = scan(tryInvertedFirst ? inverted : binarized);
  4331. if (!result && (options.inversionAttempts === 'attemptBoth' || options.inversionAttempts === 'invertFirst')) {
  4332. result = scan(tryInvertedFirst ? binarized : inverted);
  4333. }
  4334. return result;
  4335. };
  4336. /**
  4337. * @public
  4338. * @method scan
  4339. * @param {string} src
  4340. * @returns {Promise}
  4341. */
  4342. Decoder.prototype.scan = function (src) {
  4343. var _this = this;
  4344. return new Promise(function (resolve, reject) {
  4345. var image = new Image();
  4346. // Image cross origin
  4347. image.crossOrigin = 'anonymous';
  4348. image.onload = function () {
  4349. disposeImageEvents(image);
  4350. var width = image.width;
  4351. var height = image.height;
  4352. var canvas = document.createElement('canvas');
  4353. var context = canvas.getContext('2d');
  4354. canvas.width = width;
  4355. canvas.height = height;
  4356. context.drawImage(image, 0, 0);
  4357. var data = context.getImageData(0, 0, width, height).data;
  4358. var result = _this.decode(data, width, height);
  4359. if (result) {
  4360. return resolve(result);
  4361. }
  4362. return reject('failed to decode image');
  4363. };
  4364. image.onerror = function () {
  4365. disposeImageEvents(image);
  4366. reject("failed to load image: " + src);
  4367. };
  4368. image.src = src;
  4369. });
  4370. };
  4371. return Decoder;
  4372. }());
  4373.  
  4374. /**
  4375. * @module QRKanji
  4376. * @author nuintun
  4377. * @author Kazuhiko Arase
  4378. * @description SJIS only
  4379. */
  4380. var QRKanji = /*#__PURE__*/ (function (_super) {
  4381. __extends(QRKanji, _super);
  4382. /**
  4383. * @constructor
  4384. * @param {string} data
  4385. */
  4386. function QRKanji(data) {
  4387. var _this = _super.call(this, exports.Mode.Kanji, data) || this;
  4388. _this.bytes = SJIS(data);
  4389. return _this;
  4390. }
  4391. /**
  4392. * @public
  4393. * @method write
  4394. * @param {BitBuffer} buffer
  4395. */
  4396. QRKanji.prototype.write = function (buffer) {
  4397. var index = 0;
  4398. var bytes = this.bytes;
  4399. var length = bytes.length;
  4400. while (index + 1 < length) {
  4401. var code = ((0xff & bytes[index]) << 8) | (0xff & bytes[index + 1]);
  4402. if (0x8140 <= code && code <= 0x9ffc) {
  4403. code -= 0x8140;
  4404. }
  4405. else if (0xe040 <= code && code <= 0xebbf) {
  4406. code -= 0xc140;
  4407. }
  4408. code = ((code >> 8) & 0xff) * 0xc0 + (code & 0xff);
  4409. buffer.put(code, 13);
  4410. index += 2;
  4411. }
  4412. };
  4413. /**
  4414. * @public
  4415. * @method getLength
  4416. * @returns {number}
  4417. */
  4418. QRKanji.prototype.getLength = function () {
  4419. return Math.floor(this.bytes.length / 2);
  4420. };
  4421. return QRKanji;
  4422. }(QRData));
  4423.  
  4424. /**
  4425. * @module UTF16
  4426. * @author nuintun
  4427. */
  4428. function UTF16(str) {
  4429. var bytes = [];
  4430. var length = str.length;
  4431. for (var i = 0; i < length; i++) {
  4432. bytes.push(str.charCodeAt(i));
  4433. }
  4434. return bytes;
  4435. }
  4436.  
  4437. /**
  4438. * @module QRNumeric
  4439. * @author nuintun
  4440. * @author Kazuhiko Arase
  4441. */
  4442. function getCode(byte) {
  4443. // 0 - 9
  4444. if (0x30 <= byte && byte <= 0x39) {
  4445. return byte - 0x30;
  4446. }
  4447. throw "illegal char: " + String.fromCharCode(byte);
  4448. }
  4449. function getBatchCode(bytes) {
  4450. var num = 0;
  4451. var length = bytes.length;
  4452. for (var i = 0; i < length; i++) {
  4453. num = num * 10 + getCode(bytes[i]);
  4454. }
  4455. return num;
  4456. }
  4457. var QRNumeric = /*#__PURE__*/ (function (_super) {
  4458. __extends(QRNumeric, _super);
  4459. /**
  4460. * @constructor
  4461. * @param {string} data
  4462. */
  4463. function QRNumeric(data) {
  4464. var _this = _super.call(this, exports.Mode.Numeric, data) || this;
  4465. _this.bytes = UTF16(data);
  4466. return _this;
  4467. }
  4468. /**
  4469. * @public
  4470. * @method write
  4471. * @param {BitBuffer} buffer
  4472. */
  4473. QRNumeric.prototype.write = function (buffer) {
  4474. var i = 0;
  4475. var bytes = this.bytes;
  4476. var length = bytes.length;
  4477. while (i + 2 < length) {
  4478. buffer.put(getBatchCode([bytes[i], bytes[i + 1], bytes[i + 2]]), 10);
  4479. i += 3;
  4480. }
  4481. if (i < length) {
  4482. if (length - i === 1) {
  4483. buffer.put(getBatchCode([bytes[i]]), 4);
  4484. }
  4485. else if (length - i === 2) {
  4486. buffer.put(getBatchCode([bytes[i], bytes[i + 1]]), 7);
  4487. }
  4488. }
  4489. };
  4490. /**
  4491. * @public
  4492. * @method getLength
  4493. * @returns {number}
  4494. */
  4495. QRNumeric.prototype.getLength = function () {
  4496. return this.bytes.length;
  4497. };
  4498. return QRNumeric;
  4499. }(QRData));
  4500.  
  4501. /**
  4502. * @module QRAlphanumeric
  4503. * @author nuintun
  4504. * @author Kazuhiko Arase
  4505. */
  4506. function getCode$1(byte) {
  4507. if (0x30 <= byte && byte <= 0x39) {
  4508. // 0 - 9
  4509. return byte - 0x30;
  4510. }
  4511. else if (0x41 <= byte && byte <= 0x5a) {
  4512. // A - Z
  4513. return byte - 0x41 + 10;
  4514. }
  4515. else {
  4516. switch (byte) {
  4517. // space
  4518. case 0x20:
  4519. return 36;
  4520. // $
  4521. case 0x24:
  4522. return 37;
  4523. // %
  4524. case 0x25:
  4525. return 38;
  4526. // *
  4527. case 0x2a:
  4528. return 39;
  4529. // +
  4530. case 0x2b:
  4531. return 40;
  4532. // -
  4533. case 0x2d:
  4534. return 41;
  4535. // .
  4536. case 0x2e:
  4537. return 42;
  4538. // /
  4539. case 0x2f:
  4540. return 43;
  4541. // :
  4542. case 0x3a:
  4543. return 44;
  4544. default:
  4545. throw "illegal char: " + String.fromCharCode(byte);
  4546. }
  4547. }
  4548. }
  4549. var QRAlphanumeric = /*#__PURE__*/ (function (_super) {
  4550. __extends(QRAlphanumeric, _super);
  4551. /**
  4552. * @constructor
  4553. * @param {string} data
  4554. */
  4555. function QRAlphanumeric(data) {
  4556. var _this = _super.call(this, exports.Mode.Alphanumeric, data) || this;
  4557. _this.bytes = UTF16(data);
  4558. return _this;
  4559. }
  4560. /**
  4561. * @public
  4562. * @method write
  4563. * @param {BitBuffer} buffer
  4564. */
  4565. QRAlphanumeric.prototype.write = function (buffer) {
  4566. var i = 0;
  4567. var bytes = this.bytes;
  4568. var length = bytes.length;
  4569. while (i + 1 < length) {
  4570. buffer.put(getCode$1(bytes[i]) * 45 + getCode$1(bytes[i + 1]), 11);
  4571. i += 2;
  4572. }
  4573. if (i < length) {
  4574. buffer.put(getCode$1(bytes[i]), 6);
  4575. }
  4576. };
  4577. /**
  4578. * @public
  4579. * @method getLength
  4580. * @returns {number}
  4581. */
  4582. QRAlphanumeric.prototype.getLength = function () {
  4583. return this.bytes.length;
  4584. };
  4585. return QRAlphanumeric;
  4586. }(QRData));
  4587.  
  4588. /**
  4589. * @module index
  4590. * @author nuintun
  4591. */
  4592.  
  4593. exports.Decoder = Decoder;
  4594. exports.Encoder = Encoder;
  4595. exports.QRAlphanumeric = QRAlphanumeric;
  4596. exports.QRByte = QRByte;
  4597. exports.QRKanji = QRKanji;
  4598. exports.QRNumeric = QRNumeric;
  4599.  
  4600. }));