var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
// node_modules/qrcode/lib/can-promise.js
var require_can_promise = __commonJS({
"node_modules/qrcode/lib/can-promise.js"(exports, module) {
module.exports = function() {
return typeof Promise === "function" && Promise.prototype && Promise.prototype.then;
};
}
});
// node_modules/qrcode/lib/core/utils.js
var require_utils = __commonJS({
"node_modules/qrcode/lib/core/utils.js"(exports) {
var toSJISFunction;
var CODEWORDS_COUNT = [
0,
// Not used
26,
44,
70,
100,
134,
172,
196,
242,
292,
346,
404,
466,
532,
581,
655,
733,
815,
901,
991,
1085,
1156,
1258,
1364,
1474,
1588,
1706,
1828,
1921,
2051,
2185,
2323,
2465,
2611,
2761,
2876,
3034,
3196,
3362,
3532,
3706
];
exports.getSymbolSize = function getSymbolSize(version) {
if (!version) throw new Error('"version" cannot be null or undefined');
if (version < 1 || version > 40) throw new Error('"version" should be in range from 1 to 40');
return version * 4 + 17;
};
exports.getSymbolTotalCodewords = function getSymbolTotalCodewords(version) {
return CODEWORDS_COUNT[version];
};
exports.getBCHDigit = function(data) {
let digit = 0;
while (data !== 0) {
digit++;
data >>>= 1;
}
return digit;
};
exports.setToSJISFunction = function setToSJISFunction(f) {
if (typeof f !== "function") {
throw new Error('"toSJISFunc" is not a valid function.');
}
toSJISFunction = f;
};
exports.isKanjiModeEnabled = function() {
return typeof toSJISFunction !== "undefined";
};
exports.toSJIS = function toSJIS(kanji) {
return toSJISFunction(kanji);
};
}
});
// node_modules/qrcode/lib/core/error-correction-level.js
var require_error_correction_level = __commonJS({
"node_modules/qrcode/lib/core/error-correction-level.js"(exports) {
exports.L = { bit: 1 };
exports.M = { bit: 0 };
exports.Q = { bit: 3 };
exports.H = { bit: 2 };
function fromString(string) {
if (typeof string !== "string") {
throw new Error("Param is not a string");
}
const lcStr = string.toLowerCase();
switch (lcStr) {
case "l":
case "low":
return exports.L;
case "m":
case "medium":
return exports.M;
case "q":
case "quartile":
return exports.Q;
case "h":
case "high":
return exports.H;
default:
throw new Error("Unknown EC Level: " + string);
}
}
exports.isValid = function isValid(level) {
return level && typeof level.bit !== "undefined" && level.bit >= 0 && level.bit < 4;
};
exports.from = function from(value, defaultValue) {
if (exports.isValid(value)) {
return value;
}
try {
return fromString(value);
} catch (e) {
return defaultValue;
}
};
}
});
// node_modules/qrcode/lib/core/bit-buffer.js
var require_bit_buffer = __commonJS({
"node_modules/qrcode/lib/core/bit-buffer.js"(exports, module) {
function BitBuffer() {
this.buffer = [];
this.length = 0;
}
BitBuffer.prototype = {
get: function(index) {
const bufIndex = Math.floor(index / 8);
return (this.buffer[bufIndex] >>> 7 - index % 8 & 1) === 1;
},
put: function(num, length) {
for (let i = 0; i < length; i++) {
this.putBit((num >>> length - i - 1 & 1) === 1);
}
},
getLengthInBits: function() {
return this.length;
},
putBit: function(bit) {
const bufIndex = Math.floor(this.length / 8);
if (this.buffer.length <= bufIndex) {
this.buffer.push(0);
}
if (bit) {
this.buffer[bufIndex] |= 128 >>> this.length % 8;
}
this.length++;
}
};
module.exports = BitBuffer;
}
});
// node_modules/qrcode/lib/core/bit-matrix.js
var require_bit_matrix = __commonJS({
"node_modules/qrcode/lib/core/bit-matrix.js"(exports, module) {
function BitMatrix(size) {
if (!size || size < 1) {
throw new Error("BitMatrix size must be defined and greater than 0");
}
this.size = size;
this.data = new Uint8Array(size * size);
this.reservedBit = new Uint8Array(size * size);
}
BitMatrix.prototype.set = function(row, col, value, reserved) {
const index = row * this.size + col;
this.data[index] = value;
if (reserved) this.reservedBit[index] = true;
};
BitMatrix.prototype.get = function(row, col) {
return this.data[row * this.size + col];
};
BitMatrix.prototype.xor = function(row, col, value) {
this.data[row * this.size + col] ^= value;
};
BitMatrix.prototype.isReserved = function(row, col) {
return this.reservedBit[row * this.size + col];
};
module.exports = BitMatrix;
}
});
// node_modules/qrcode/lib/core/alignment-pattern.js
var require_alignment_pattern = __commonJS({
"node_modules/qrcode/lib/core/alignment-pattern.js"(exports) {
var getSymbolSize = require_utils().getSymbolSize;
exports.getRowColCoords = function getRowColCoords(version) {
if (version === 1) return [];
const posCount = Math.floor(version / 7) + 2;
const size = getSymbolSize(version);
const intervals = size === 145 ? 26 : Math.ceil((size - 13) / (2 * posCount - 2)) * 2;
const positions = [size - 7];
for (let i = 1; i < posCount - 1; i++) {
positions[i] = positions[i - 1] - intervals;
}
positions.push(6);
return positions.reverse();
};
exports.getPositions = function getPositions(version) {
const coords = [];
const pos = exports.getRowColCoords(version);
const posLength = pos.length;
for (let i = 0; i < posLength; i++) {
for (let j = 0; j < posLength; j++) {
if (i === 0 && j === 0 || // top-left
i === 0 && j === posLength - 1 || // bottom-left
i === posLength - 1 && j === 0) {
continue;
}
coords.push([pos[i], pos[j]]);
}
}
return coords;
};
}
});
// node_modules/qrcode/lib/core/finder-pattern.js
var require_finder_pattern = __commonJS({
"node_modules/qrcode/lib/core/finder-pattern.js"(exports) {
var getSymbolSize = require_utils().getSymbolSize;
var FINDER_PATTERN_SIZE = 7;
exports.getPositions = function getPositions(version) {
const size = getSymbolSize(version);
return [
// top-left
[0, 0],
// top-right
[size - FINDER_PATTERN_SIZE, 0],
// bottom-left
[0, size - FINDER_PATTERN_SIZE]
];
};
}
});
// node_modules/qrcode/lib/core/mask-pattern.js
var require_mask_pattern = __commonJS({
"node_modules/qrcode/lib/core/mask-pattern.js"(exports) {
exports.Patterns = {
PATTERN000: 0,
PATTERN001: 1,
PATTERN010: 2,
PATTERN011: 3,
PATTERN100: 4,
PATTERN101: 5,
PATTERN110: 6,
PATTERN111: 7
};
var PenaltyScores = {
N1: 3,
N2: 3,
N3: 40,
N4: 10
};
exports.isValid = function isValid(mask) {
return mask != null && mask !== "" && !isNaN(mask) && mask >= 0 && mask <= 7;
};
exports.from = function from(value) {
return exports.isValid(value) ? parseInt(value, 10) : void 0;
};
exports.getPenaltyN1 = function getPenaltyN1(data) {
const size = data.size;
let points = 0;
let sameCountCol = 0;
let sameCountRow = 0;
let lastCol = null;
let lastRow = null;
for (let row = 0; row < size; row++) {
sameCountCol = sameCountRow = 0;
lastCol = lastRow = null;
for (let col = 0; col < size; col++) {
let module2 = data.get(row, col);
if (module2 === lastCol) {
sameCountCol++;
} else {
if (sameCountCol >= 5) points += PenaltyScores.N1 + (sameCountCol - 5);
lastCol = module2;
sameCountCol = 1;
}
module2 = data.get(col, row);
if (module2 === lastRow) {
sameCountRow++;
} else {
if (sameCountRow >= 5) points += PenaltyScores.N1 + (sameCountRow - 5);
lastRow = module2;
sameCountRow = 1;
}
}
if (sameCountCol >= 5) points += PenaltyScores.N1 + (sameCountCol - 5);
if (sameCountRow >= 5) points += PenaltyScores.N1 + (sameCountRow - 5);
}
return points;
};
exports.getPenaltyN2 = function getPenaltyN2(data) {
const size = data.size;
let points = 0;
for (let row = 0; row < size - 1; row++) {
for (let col = 0; col < size - 1; col++) {
const last = data.get(row, col) + data.get(row, col + 1) + data.get(row + 1, col) + data.get(row + 1, col + 1);
if (last === 4 || last === 0) points++;
}
}
return points * PenaltyScores.N2;
};
exports.getPenaltyN3 = function getPenaltyN3(data) {
const size = data.size;
let points = 0;
let bitsCol = 0;
let bitsRow = 0;
for (let row = 0; row < size; row++) {
bitsCol = bitsRow = 0;
for (let col = 0; col < size; col++) {
bitsCol = bitsCol << 1 & 2047 | data.get(row, col);
if (col >= 10 && (bitsCol === 1488 || bitsCol === 93)) points++;
bitsRow = bitsRow << 1 & 2047 | data.get(col, row);
if (col >= 10 && (bitsRow === 1488 || bitsRow === 93)) points++;
}
}
return points * PenaltyScores.N3;
};
exports.getPenaltyN4 = function getPenaltyN4(data) {
let darkCount = 0;
const modulesCount = data.data.length;
for (let i = 0; i < modulesCount; i++) darkCount += data.data[i];
const k = Math.abs(Math.ceil(darkCount * 100 / modulesCount / 5) - 10);
return k * PenaltyScores.N4;
};
function getMaskAt(maskPattern, i, j) {
switch (maskPattern) {
case exports.Patterns.PATTERN000:
return (i + j) % 2 === 0;
case exports.Patterns.PATTERN001:
return i % 2 === 0;
case exports.Patterns.PATTERN010:
return j % 3 === 0;
case exports.Patterns.PATTERN011:
return (i + j) % 3 === 0;
case exports.Patterns.PATTERN100:
return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 === 0;
case exports.Patterns.PATTERN101:
return i * j % 2 + i * j % 3 === 0;
case exports.Patterns.PATTERN110:
return (i * j % 2 + i * j % 3) % 2 === 0;
case exports.Patterns.PATTERN111:
return (i * j % 3 + (i + j) % 2) % 2 === 0;
default:
throw new Error("bad maskPattern:" + maskPattern);
}
}
exports.applyMask = function applyMask(pattern, data) {
const size = data.size;
for (let col = 0; col < size; col++) {
for (let row = 0; row < size; row++) {
if (data.isReserved(row, col)) continue;
data.xor(row, col, getMaskAt(pattern, row, col));
}
}
};
exports.getBestMask = function getBestMask(data, setupFormatFunc) {
const numPatterns = Object.keys(exports.Patterns).length;
let bestPattern = 0;
let lowerPenalty = Infinity;
for (let p = 0; p < numPatterns; p++) {
setupFormatFunc(p);
exports.applyMask(p, data);
const penalty = exports.getPenaltyN1(data) + exports.getPenaltyN2(data) + exports.getPenaltyN3(data) + exports.getPenaltyN4(data);
exports.applyMask(p, data);
if (penalty < lowerPenalty) {
lowerPenalty = penalty;
bestPattern = p;
}
}
return bestPattern;
};
}
});
// node_modules/qrcode/lib/core/error-correction-code.js
var require_error_correction_code = __commonJS({
"node_modules/qrcode/lib/core/error-correction-code.js"(exports) {
var ECLevel = require_error_correction_level();
var EC_BLOCKS_TABLE = [
// L M Q H
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
2,
2,
1,
2,
2,
4,
1,
2,
4,
4,
2,
4,
4,
4,
2,
4,
6,
5,
2,
4,
6,
6,
2,
5,
8,
8,
4,
5,
8,
8,
4,
5,
8,
11,
4,
8,
10,
11,
4,
9,
12,
16,
4,
9,
16,
16,
6,
10,
12,
18,
6,
10,
17,
16,
6,
11,
16,
19,
6,
13,
18,
21,
7,
14,
21,
25,
8,
16,
20,
25,
8,
17,
23,
25,
9,
17,
23,
34,
9,
18,
25,
30,
10,
20,
27,
32,
12,
21,
29,
35,
12,
23,
34,
37,
12,
25,
34,
40,
13,
26,
35,
42,
14,
28,
38,
45,
15,
29,
40,
48,
16,
31,
43,
51,
17,
33,
45,
54,
18,
35,
48,
57,
19,
37,
51,
60,
19,
38,
53,
63,
20,
40,
56,
66,
21,
43,
59,
70,
22,
45,
62,
74,
24,
47,
65,
77,
25,
49,
68,
81
];
var EC_CODEWORDS_TABLE = [
// L M Q H
7,
10,
13,
17,
10,
16,
22,
28,
15,
26,
36,
44,
20,
36,
52,
64,
26,
48,
72,
88,
36,
64,
96,
112,
40,
72,
108,
130,
48,
88,
132,
156,
60,
110,
160,
192,
72,
130,
192,
224,
80,
150,
224,
264,
96,
176,
260,
308,
104,
198,
288,
352,
120,
216,
320,
384,
132,
240,
360,
432,
144,
280,
408,
480,
168,
308,
448,
532,
180,
338,
504,
588,
196,
364,
546,
650,
224,
416,
600,
700,
224,
442,
644,
750,
252,
476,
690,
816,
270,
504,
750,
900,
300,
560,
810,
960,
312,
588,
870,
1050,
336,
644,
952,
1110,
360,
700,
1020,
1200,
390,
728,
1050,
1260,
420,
784,
1140,
1350,
450,
812,
1200,
1440,
480,
868,
1290,
1530,
510,
924,
1350,
1620,
540,
980,
1440,
1710,
570,
1036,
1530,
1800,
570,
1064,
1590,
1890,
600,
1120,
1680,
1980,
630,
1204,
1770,
2100,
660,
1260,
1860,
2220,
720,
1316,
1950,
2310,
750,
1372,
2040,
2430
];
exports.getBlocksCount = function getBlocksCount(version, errorCorrectionLevel) {
switch (errorCorrectionLevel) {
case ECLevel.L:
return EC_BLOCKS_TABLE[(version - 1) * 4 + 0];
case ECLevel.M:
return EC_BLOCKS_TABLE[(version - 1) * 4 + 1];
case ECLevel.Q:
return EC_BLOCKS_TABLE[(version - 1) * 4 + 2];
case ECLevel.H:
return EC_BLOCKS_TABLE[(version - 1) * 4 + 3];
default:
return void 0;
}
};
exports.getTotalCodewordsCount = function getTotalCodewordsCount(version, errorCorrectionLevel) {
switch (errorCorrectionLevel) {
case ECLevel.L:
return EC_CODEWORDS_TABLE[(version - 1) * 4 + 0];
case ECLevel.M:
return EC_CODEWORDS_TABLE[(version - 1) * 4 + 1];
case ECLevel.Q:
return EC_CODEWORDS_TABLE[(version - 1) * 4 + 2];
case ECLevel.H:
return EC_CODEWORDS_TABLE[(version - 1) * 4 + 3];
default:
return void 0;
}
};
}
});
// node_modules/qrcode/lib/core/galois-field.js
var require_galois_field = __commonJS({
"node_modules/qrcode/lib/core/galois-field.js"(exports) {
var EXP_TABLE = new Uint8Array(512);
var LOG_TABLE = new Uint8Array(256);
(function initTables() {
let x = 1;
for (let i = 0; i < 255; i++) {
EXP_TABLE[i] = x;
LOG_TABLE[x] = i;
x <<= 1;
if (x & 256) {
x ^= 285;
}
}
for (let i = 255; i < 512; i++) {
EXP_TABLE[i] = EXP_TABLE[i - 255];
}
})();
exports.log = function log(n) {
if (n < 1) throw new Error("log(" + n + ")");
return LOG_TABLE[n];
};
exports.exp = function exp(n) {
return EXP_TABLE[n];
};
exports.mul = function mul(x, y) {
if (x === 0 || y === 0) return 0;
return EXP_TABLE[LOG_TABLE[x] + LOG_TABLE[y]];
};
}
});
// node_modules/qrcode/lib/core/polynomial.js
var require_polynomial = __commonJS({
"node_modules/qrcode/lib/core/polynomial.js"(exports) {
var GF = require_galois_field();
exports.mul = function mul(p1, p2) {
const coeff = new Uint8Array(p1.length + p2.length - 1);
for (let i = 0; i < p1.length; i++) {
for (let j = 0; j < p2.length; j++) {
coeff[i + j] ^= GF.mul(p1[i], p2[j]);
}
}
return coeff;
};
exports.mod = function mod(divident, divisor) {
let result = new Uint8Array(divident);
while (result.length - divisor.length >= 0) {
const coeff = result[0];
for (let i = 0; i < divisor.length; i++) {
result[i] ^= GF.mul(divisor[i], coeff);
}
let offset = 0;
while (offset < result.length && result[offset] === 0) offset++;
result = result.slice(offset);
}
return result;
};
exports.generateECPolynomial = function generateECPolynomial(degree) {
let poly = new Uint8Array([1]);
for (let i = 0; i < degree; i++) {
poly = exports.mul(poly, new Uint8Array([1, GF.exp(i)]));
}
return poly;
};
}
});
// node_modules/qrcode/lib/core/reed-solomon-encoder.js
var require_reed_solomon_encoder = __commonJS({
"node_modules/qrcode/lib/core/reed-solomon-encoder.js"(exports, module) {
var Polynomial = require_polynomial();
function ReedSolomonEncoder(degree) {
this.genPoly = void 0;
this.degree = degree;
if (this.degree) this.initialize(this.degree);
}
ReedSolomonEncoder.prototype.initialize = function initialize(degree) {
this.degree = degree;
this.genPoly = Polynomial.generateECPolynomial(this.degree);
};
ReedSolomonEncoder.prototype.encode = function encode2(data) {
if (!this.genPoly) {
throw new Error("Encoder not initialized");
}
const paddedData = new Uint8Array(data.length + this.degree);
paddedData.set(data);
const remainder = Polynomial.mod(paddedData, this.genPoly);
const start = this.degree - remainder.length;
if (start > 0) {
const buff = new Uint8Array(this.degree);
buff.set(remainder, start);
return buff;
}
return remainder;
};
module.exports = ReedSolomonEncoder;
}
});
// node_modules/qrcode/lib/core/version-check.js
var require_version_check = __commonJS({
"node_modules/qrcode/lib/core/version-check.js"(exports) {
exports.isValid = function isValid(version) {
return !isNaN(version) && version >= 1 && version <= 40;
};
}
});
// node_modules/qrcode/lib/core/regex.js
var require_regex = __commonJS({
"node_modules/qrcode/lib/core/regex.js"(exports) {
var numeric = "[0-9]+";
var alphanumeric = "[A-Z $%*+\\-./:]+";
var kanji = "(?:[u3000-u303F]|[u3040-u309F]|[u30A0-u30FF]|[uFF00-uFFEF]|[u4E00-u9FAF]|[u2605-u2606]|[u2190-u2195]|u203B|[u2010u2015u2018u2019u2025u2026u201Cu201Du2225u2260]|[u0391-u0451]|[u00A7u00A8u00B1u00B4u00D7u00F7])+";
kanji = kanji.replace(/u/g, "\\u");
var byte = "(?:(?![A-Z0-9 $%*+\\-./:]|" + kanji + ")(?:.|[\r\n]))+";
exports.KANJI = new RegExp(kanji, "g");
exports.BYTE_KANJI = new RegExp("[^A-Z0-9 $%*+\\-./:]+", "g");
exports.BYTE = new RegExp(byte, "g");
exports.NUMERIC = new RegExp(numeric, "g");
exports.ALPHANUMERIC = new RegExp(alphanumeric, "g");
var TEST_KANJI = new RegExp("^" + kanji + "$");
var TEST_NUMERIC = new RegExp("^" + numeric + "$");
var TEST_ALPHANUMERIC = new RegExp("^[A-Z0-9 $%*+\\-./:]+$");
exports.testKanji = function testKanji(str) {
return TEST_KANJI.test(str);
};
exports.testNumeric = function testNumeric(str) {
return TEST_NUMERIC.test(str);
};
exports.testAlphanumeric = function testAlphanumeric(str) {
return TEST_ALPHANUMERIC.test(str);
};
}
});
// node_modules/qrcode/lib/core/mode.js
var require_mode = __commonJS({
"node_modules/qrcode/lib/core/mode.js"(exports) {
var VersionCheck = require_version_check();
var Regex = require_regex();
exports.NUMERIC = {
id: "Numeric",
bit: 1 << 0,
ccBits: [10, 12, 14]
};
exports.ALPHANUMERIC = {
id: "Alphanumeric",
bit: 1 << 1,
ccBits: [9, 11, 13]
};
exports.BYTE = {
id: "Byte",
bit: 1 << 2,
ccBits: [8, 16, 16]
};
exports.KANJI = {
id: "Kanji",
bit: 1 << 3,
ccBits: [8, 10, 12]
};
exports.MIXED = {
bit: -1
};
exports.getCharCountIndicator = function getCharCountIndicator(mode, version) {
if (!mode.ccBits) throw new Error("Invalid mode: " + mode);
if (!VersionCheck.isValid(version)) {
throw new Error("Invalid version: " + version);
}
if (version >= 1 && version < 10) return mode.ccBits[0];
else if (version < 27) return mode.ccBits[1];
return mode.ccBits[2];
};
exports.getBestModeForData = function getBestModeForData(dataStr) {
if (Regex.testNumeric(dataStr)) return exports.NUMERIC;
else if (Regex.testAlphanumeric(dataStr)) return exports.ALPHANUMERIC;
else if (Regex.testKanji(dataStr)) return exports.KANJI;
else return exports.BYTE;
};
exports.toString = function toString2(mode) {
if (mode && mode.id) return mode.id;
throw new Error("Invalid mode");
};
exports.isValid = function isValid(mode) {
return mode && mode.bit && mode.ccBits;
};
function fromString(string) {
if (typeof string !== "string") {
throw new Error("Param is not a string");
}
const lcStr = string.toLowerCase();
switch (lcStr) {
case "numeric":
return exports.NUMERIC;
case "alphanumeric":
return exports.ALPHANUMERIC;
case "kanji":
return exports.KANJI;
case "byte":
return exports.BYTE;
default:
throw new Error("Unknown mode: " + string);
}
}
exports.from = function from(value, defaultValue) {
if (exports.isValid(value)) {
return value;
}
try {
return fromString(value);
} catch (e) {
return defaultValue;
}
};
}
});
// node_modules/qrcode/lib/core/version.js
var require_version = __commonJS({
"node_modules/qrcode/lib/core/version.js"(exports) {
var Utils = require_utils();
var ECCode = require_error_correction_code();
var ECLevel = require_error_correction_level();
var Mode = require_mode();
var VersionCheck = require_version_check();
var G18 = 1 << 12 | 1 << 11 | 1 << 10 | 1 << 9 | 1 << 8 | 1 << 5 | 1 << 2 | 1 << 0;
var G18_BCH = Utils.getBCHDigit(G18);
function getBestVersionForDataLength(mode, length, errorCorrectionLevel) {
for (let currentVersion = 1; currentVersion <= 40; currentVersion++) {
if (length <= exports.getCapacity(currentVersion, errorCorrectionLevel, mode)) {
return currentVersion;
}
}
return void 0;
}
function getReservedBitsCount(mode, version) {
return Mode.getCharCountIndicator(mode, version) + 4;
}
function getTotalBitsFromDataArray(segments, version) {
let totalBits = 0;
segments.forEach(function(data) {
const reservedBits = getReservedBitsCount(data.mode, version);
totalBits += reservedBits + data.getBitsLength();
});
return totalBits;
}
function getBestVersionForMixedData(segments, errorCorrectionLevel) {
for (let currentVersion = 1; currentVersion <= 40; currentVersion++) {
const length = getTotalBitsFromDataArray(segments, currentVersion);
if (length <= exports.getCapacity(currentVersion, errorCorrectionLevel, Mode.MIXED)) {
return currentVersion;
}
}
return void 0;
}
exports.from = function from(value, defaultValue) {
if (VersionCheck.isValid(value)) {
return parseInt(value, 10);
}
return defaultValue;
};
exports.getCapacity = function getCapacity(version, errorCorrectionLevel, mode) {
if (!VersionCheck.isValid(version)) {
throw new Error("Invalid QR Code version");
}
if (typeof mode === "undefined") mode = Mode.BYTE;
const totalCodewords = Utils.getSymbolTotalCodewords(version);
const ecTotalCodewords = ECCode.getTotalCodewordsCount(version, errorCorrectionLevel);
const dataTotalCodewordsBits = (totalCodewords - ecTotalCodewords) * 8;
if (mode === Mode.MIXED) return dataTotalCodewordsBits;
const usableBits = dataTotalCodewordsBits - getReservedBitsCount(mode, version);
switch (mode) {
case Mode.NUMERIC:
return Math.floor(usableBits / 10 * 3);
case Mode.ALPHANUMERIC:
return Math.floor(usableBits / 11 * 2);
case Mode.KANJI:
return Math.floor(usableBits / 13);
case Mode.BYTE:
default:
return Math.floor(usableBits / 8);
}
};
exports.getBestVersionForData = function getBestVersionForData(data, errorCorrectionLevel) {
let seg;
const ecl = ECLevel.from(errorCorrectionLevel, ECLevel.M);
if (Array.isArray(data)) {
if (data.length > 1) {
return getBestVersionForMixedData(data, ecl);
}
if (data.length === 0) {
return 1;
}
seg = data[0];
} else {
seg = data;
}
return getBestVersionForDataLength(seg.mode, seg.getLength(), ecl);
};
exports.getEncodedBits = function getEncodedBits(version) {
if (!VersionCheck.isValid(version) || version < 7) {
throw new Error("Invalid QR Code version");
}
let d = version << 12;
while (Utils.getBCHDigit(d) - G18_BCH >= 0) {
d ^= G18 << Utils.getBCHDigit(d) - G18_BCH;
}
return version << 12 | d;
};
}
});
// node_modules/qrcode/lib/core/format-info.js
var require_format_info = __commonJS({
"node_modules/qrcode/lib/core/format-info.js"(exports) {
var Utils = require_utils();
var G15 = 1 << 10 | 1 << 8 | 1 << 5 | 1 << 4 | 1 << 2 | 1 << 1 | 1 << 0;
var G15_MASK = 1 << 14 | 1 << 12 | 1 << 10 | 1 << 4 | 1 << 1;
var G15_BCH = Utils.getBCHDigit(G15);
exports.getEncodedBits = function getEncodedBits(errorCorrectionLevel, mask) {
const data = errorCorrectionLevel.bit << 3 | mask;
let d = data << 10;
while (Utils.getBCHDigit(d) - G15_BCH >= 0) {
d ^= G15 << Utils.getBCHDigit(d) - G15_BCH;
}
return (data << 10 | d) ^ G15_MASK;
};
}
});
// node_modules/qrcode/lib/core/numeric-data.js
var require_numeric_data = __commonJS({
"node_modules/qrcode/lib/core/numeric-data.js"(exports, module) {
var Mode = require_mode();
function NumericData(data) {
this.mode = Mode.NUMERIC;
this.data = data.toString();
}
NumericData.getBitsLength = function getBitsLength(length) {
return 10 * Math.floor(length / 3) + (length % 3 ? length % 3 * 3 + 1 : 0);
};
NumericData.prototype.getLength = function getLength() {
return this.data.length;
};
NumericData.prototype.getBitsLength = function getBitsLength() {
return NumericData.getBitsLength(this.data.length);
};
NumericData.prototype.write = function write(bitBuffer) {
let i, group, value;
for (i = 0; i + 3 <= this.data.length; i += 3) {
group = this.data.substr(i, 3);
value = parseInt(group, 10);
bitBuffer.put(value, 10);
}
const remainingNum = this.data.length - i;
if (remainingNum > 0) {
group = this.data.substr(i);
value = parseInt(group, 10);
bitBuffer.put(value, remainingNum * 3 + 1);
}
};
module.exports = NumericData;
}
});
// node_modules/qrcode/lib/core/alphanumeric-data.js
var require_alphanumeric_data = __commonJS({
"node_modules/qrcode/lib/core/alphanumeric-data.js"(exports, module) {
var Mode = require_mode();
var ALPHA_NUM_CHARS = [
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
" ",
"$",
"%",
"*",
"+",
"-",
".",
"/",
":"
];
function AlphanumericData(data) {
this.mode = Mode.ALPHANUMERIC;
this.data = data;
}
AlphanumericData.getBitsLength = function getBitsLength(length) {
return 11 * Math.floor(length / 2) + 6 * (length % 2);
};
AlphanumericData.prototype.getLength = function getLength() {
return this.data.length;
};
AlphanumericData.prototype.getBitsLength = function getBitsLength() {
return AlphanumericData.getBitsLength(this.data.length);
};
AlphanumericData.prototype.write = function write(bitBuffer) {
let i;
for (i = 0; i + 2 <= this.data.length; i += 2) {
let value = ALPHA_NUM_CHARS.indexOf(this.data[i]) * 45;
value += ALPHA_NUM_CHARS.indexOf(this.data[i + 1]);
bitBuffer.put(value, 11);
}
if (this.data.length % 2) {
bitBuffer.put(ALPHA_NUM_CHARS.indexOf(this.data[i]), 6);
}
};
module.exports = AlphanumericData;
}
});
// node_modules/qrcode/lib/core/byte-data.js
var require_byte_data = __commonJS({
"node_modules/qrcode/lib/core/byte-data.js"(exports, module) {
var Mode = require_mode();
function ByteData(data) {
this.mode = Mode.BYTE;
if (typeof data === "string") {
this.data = new TextEncoder().encode(data);
} else {
this.data = new Uint8Array(data);
}
}
ByteData.getBitsLength = function getBitsLength(length) {
return length * 8;
};
ByteData.prototype.getLength = function getLength() {
return this.data.length;
};
ByteData.prototype.getBitsLength = function getBitsLength() {
return ByteData.getBitsLength(this.data.length);
};
ByteData.prototype.write = function(bitBuffer) {
for (let i = 0, l = this.data.length; i < l; i++) {
bitBuffer.put(this.data[i], 8);
}
};
module.exports = ByteData;
}
});
// node_modules/qrcode/lib/core/kanji-data.js
var require_kanji_data = __commonJS({
"node_modules/qrcode/lib/core/kanji-data.js"(exports, module) {
var Mode = require_mode();
var Utils = require_utils();
function KanjiData(data) {
this.mode = Mode.KANJI;
this.data = data;
}
KanjiData.getBitsLength = function getBitsLength(length) {
return length * 13;
};
KanjiData.prototype.getLength = function getLength() {
return this.data.length;
};
KanjiData.prototype.getBitsLength = function getBitsLength() {
return KanjiData.getBitsLength(this.data.length);
};
KanjiData.prototype.write = function(bitBuffer) {
let i;
for (i = 0; i < this.data.length; i++) {
let value = Utils.toSJIS(this.data[i]);
if (value >= 33088 && value <= 40956) {
value -= 33088;
} else if (value >= 57408 && value <= 60351) {
value -= 49472;
} else {
throw new Error(
"Invalid SJIS character: " + this.data[i] + "\nMake sure your charset is UTF-8"
);
}
value = (value >>> 8 & 255) * 192 + (value & 255);
bitBuffer.put(value, 13);
}
};
module.exports = KanjiData;
}
});
// node_modules/dijkstrajs/dijkstra.js
var require_dijkstra = __commonJS({
"node_modules/dijkstrajs/dijkstra.js"(exports, module) {
"use strict";
var dijkstra = {
single_source_shortest_paths: function(graph, s, d) {
var predecessors = {};
var costs = {};
costs[s] = 0;
var open = dijkstra.PriorityQueue.make();
open.push(s, 0);
var closest, u, v, cost_of_s_to_u, adjacent_nodes, cost_of_e, cost_of_s_to_u_plus_cost_of_e, cost_of_s_to_v, first_visit;
while (!open.empty()) {
closest = open.pop();
u = closest.value;
cost_of_s_to_u = closest.cost;
adjacent_nodes = graph[u] || {};
for (v in adjacent_nodes) {
if (adjacent_nodes.hasOwnProperty(v)) {
cost_of_e = adjacent_nodes[v];
cost_of_s_to_u_plus_cost_of_e = cost_of_s_to_u + cost_of_e;
cost_of_s_to_v = costs[v];
first_visit = typeof costs[v] === "undefined";
if (first_visit || cost_of_s_to_v > cost_of_s_to_u_plus_cost_of_e) {
costs[v] = cost_of_s_to_u_plus_cost_of_e;
open.push(v, cost_of_s_to_u_plus_cost_of_e);
predecessors[v] = u;
}
}
}
}
if (typeof d !== "undefined" && typeof costs[d] === "undefined") {
var msg = ["Could not find a path from ", s, " to ", d, "."].join("");
throw new Error(msg);
}
return predecessors;
},
extract_shortest_path_from_predecessor_list: function(predecessors, d) {
var nodes = [];
var u = d;
var predecessor;
while (u) {
nodes.push(u);
predecessor = predecessors[u];
u = predecessors[u];
}
nodes.reverse();
return nodes;
},
find_path: function(graph, s, d) {
var predecessors = dijkstra.single_source_shortest_paths(graph, s, d);
return dijkstra.extract_shortest_path_from_predecessor_list(
predecessors,
d
);
},
/**
* A very naive priority queue implementation.
*/
PriorityQueue: {
make: function(opts) {
var T = dijkstra.PriorityQueue, t = {}, key;
opts = opts || {};
for (key in T) {
if (T.hasOwnProperty(key)) {
t[key] = T[key];
}
}
t.queue = [];
t.sorter = opts.sorter || T.default_sorter;
return t;
},
default_sorter: function(a, b) {
return a.cost - b.cost;
},
/**
* Add a new item to the queue and ensure the highest priority element
* is at the front of the queue.
*/
push: function(value, cost) {
var item = { value, cost };
this.queue.push(item);
this.queue.sort(this.sorter);
},
/**
* Return the highest priority element in the queue.
*/
pop: function() {
return this.queue.shift();
},
empty: function() {
return this.queue.length === 0;
}
}
};
if (typeof module !== "undefined") {
module.exports = dijkstra;
}
}
});
// node_modules/qrcode/lib/core/segments.js
var require_segments = __commonJS({
"node_modules/qrcode/lib/core/segments.js"(exports) {
var Mode = require_mode();
var NumericData = require_numeric_data();
var AlphanumericData = require_alphanumeric_data();
var ByteData = require_byte_data();
var KanjiData = require_kanji_data();
var Regex = require_regex();
var Utils = require_utils();
var dijkstra = require_dijkstra();
function getStringByteLength(str) {
return unescape(encodeURIComponent(str)).length;
}
function getSegments(regex, mode, str) {
const segments = [];
let result;
while ((result = regex.exec(str)) !== null) {
segments.push({
data: result[0],
index: result.index,
mode,
length: result[0].length
});
}
return segments;
}
function getSegmentsFromString(dataStr) {
const numSegs = getSegments(Regex.NUMERIC, Mode.NUMERIC, dataStr);
const alphaNumSegs = getSegments(Regex.ALPHANUMERIC, Mode.ALPHANUMERIC, dataStr);
let byteSegs;
let kanjiSegs;
if (Utils.isKanjiModeEnabled()) {
byteSegs = getSegments(Regex.BYTE, Mode.BYTE, dataStr);
kanjiSegs = getSegments(Regex.KANJI, Mode.KANJI, dataStr);
} else {
byteSegs = getSegments(Regex.BYTE_KANJI, Mode.BYTE, dataStr);
kanjiSegs = [];
}
const segs = numSegs.concat(alphaNumSegs, byteSegs, kanjiSegs);
return segs.sort(function(s1, s2) {
return s1.index - s2.index;
}).map(function(obj) {
return {
data: obj.data,
mode: obj.mode,
length: obj.length
};
});
}
function getSegmentBitsLength(length, mode) {
switch (mode) {
case Mode.NUMERIC:
return NumericData.getBitsLength(length);
case Mode.ALPHANUMERIC:
return AlphanumericData.getBitsLength(length);
case Mode.KANJI:
return KanjiData.getBitsLength(length);
case Mode.BYTE:
return ByteData.getBitsLength(length);
}
}
function mergeSegments(segs) {
return segs.reduce(function(acc, curr) {
const prevSeg = acc.length - 1 >= 0 ? acc[acc.length - 1] : null;
if (prevSeg && prevSeg.mode === curr.mode) {
acc[acc.length - 1].data += curr.data;
return acc;
}
acc.push(curr);
return acc;
}, []);
}
function buildNodes(segs) {
const nodes = [];
for (let i = 0; i < segs.length; i++) {
const seg = segs[i];
switch (seg.mode) {
case Mode.NUMERIC:
nodes.push([
seg,
{ data: seg.data, mode: Mode.ALPHANUMERIC, length: seg.length },
{ data: seg.data, mode: Mode.BYTE, length: seg.length }
]);
break;
case Mode.ALPHANUMERIC:
nodes.push([
seg,
{ data: seg.data, mode: Mode.BYTE, length: seg.length }
]);
break;
case Mode.KANJI:
nodes.push([
seg,
{ data: seg.data, mode: Mode.BYTE, length: getStringByteLength(seg.data) }
]);
break;
case Mode.BYTE:
nodes.push([
{ data: seg.data, mode: Mode.BYTE, length: getStringByteLength(seg.data) }
]);
}
}
return nodes;
}
function buildGraph(nodes, version) {
const table = {};
const graph = { start: {} };
let prevNodeIds = ["start"];
for (let i = 0; i < nodes.length; i++) {
const nodeGroup = nodes[i];
const currentNodeIds = [];
for (let j = 0; j < nodeGroup.length; j++) {
const node = nodeGroup[j];
const key = "" + i + j;
currentNodeIds.push(key);
table[key] = { node, lastCount: 0 };
graph[key] = {};
for (let n = 0; n < prevNodeIds.length; n++) {
const prevNodeId = prevNodeIds[n];
if (table[prevNodeId] && table[prevNodeId].node.mode === node.mode) {
graph[prevNodeId][key] = getSegmentBitsLength(table[prevNodeId].lastCount + node.length, node.mode) - getSegmentBitsLength(table[prevNodeId].lastCount, node.mode);
table[prevNodeId].lastCount += node.length;
} else {
if (table[prevNodeId]) table[prevNodeId].lastCount = node.length;
graph[prevNodeId][key] = getSegmentBitsLength(node.length, node.mode) + 4 + Mode.getCharCountIndicator(node.mode, version);
}
}
}
prevNodeIds = currentNodeIds;
}
for (let n = 0; n < prevNodeIds.length; n++) {
graph[prevNodeIds[n]].end = 0;
}
return { map: graph, table };
}
function buildSingleSegment(data, modesHint) {
let mode;
const bestMode = Mode.getBestModeForData(data);
mode = Mode.from(modesHint, bestMode);
if (mode !== Mode.BYTE && mode.bit < bestMode.bit) {
throw new Error('"' + data + '" cannot be encoded with mode ' + Mode.toString(mode) + ".\n Suggested mode is: " + Mode.toString(bestMode));
}
if (mode === Mode.KANJI && !Utils.isKanjiModeEnabled()) {
mode = Mode.BYTE;
}
switch (mode) {
case Mode.NUMERIC:
return new NumericData(data);
case Mode.ALPHANUMERIC:
return new AlphanumericData(data);
case Mode.KANJI:
return new KanjiData(data);
case Mode.BYTE:
return new ByteData(data);
}
}
exports.fromArray = function fromArray(array) {
return array.reduce(function(acc, seg) {
if (typeof seg === "string") {
acc.push(buildSingleSegment(seg, null));
} else if (seg.data) {
acc.push(buildSingleSegment(seg.data, seg.mode));
}
return acc;
}, []);
};
exports.fromString = function fromString(data, version) {
const segs = getSegmentsFromString(data, Utils.isKanjiModeEnabled());
const nodes = buildNodes(segs);
const graph = buildGraph(nodes, version);
const path = dijkstra.find_path(graph.map, "start", "end");
const optimizedSegs = [];
for (let i = 1; i < path.length - 1; i++) {
optimizedSegs.push(graph.table[path[i]].node);
}
return exports.fromArray(mergeSegments(optimizedSegs));
};
exports.rawSplit = function rawSplit(data) {
return exports.fromArray(
getSegmentsFromString(data, Utils.isKanjiModeEnabled())
);
};
}
});
// node_modules/qrcode/lib/core/qrcode.js
var require_qrcode = __commonJS({
"node_modules/qrcode/lib/core/qrcode.js"(exports) {
var Utils = require_utils();
var ECLevel = require_error_correction_level();
var BitBuffer = require_bit_buffer();
var BitMatrix = require_bit_matrix();
var AlignmentPattern = require_alignment_pattern();
var FinderPattern = require_finder_pattern();
var MaskPattern = require_mask_pattern();
var ECCode = require_error_correction_code();
var ReedSolomonEncoder = require_reed_solomon_encoder();
var Version = require_version();
var FormatInfo = require_format_info();
var Mode = require_mode();
var Segments = require_segments();
function setupFinderPattern(matrix, version) {
const size = matrix.size;
const pos = FinderPattern.getPositions(version);
for (let i = 0; i < pos.length; i++) {
const row = pos[i][0];
const col = pos[i][1];
for (let r = -1; r <= 7; r++) {
if (row + r <= -1 || size <= row + r) continue;
for (let c = -1; c <= 7; c++) {
if (col + c <= -1 || size <= col + c) continue;
if (r >= 0 && r <= 6 && (c === 0 || c === 6) || c >= 0 && c <= 6 && (r === 0 || r === 6) || r >= 2 && r <= 4 && c >= 2 && c <= 4) {
matrix.set(row + r, col + c, true, true);
} else {
matrix.set(row + r, col + c, false, true);
}
}
}
}
}
function setupTimingPattern(matrix) {
const size = matrix.size;
for (let r = 8; r < size - 8; r++) {
const value = r % 2 === 0;
matrix.set(r, 6, value, true);
matrix.set(6, r, value, true);
}
}
function setupAlignmentPattern(matrix, version) {
const pos = AlignmentPattern.getPositions(version);
for (let i = 0; i < pos.length; i++) {
const row = pos[i][0];
const col = pos[i][1];
for (let r = -2; r <= 2; r++) {
for (let c = -2; c <= 2; c++) {
if (r === -2 || r === 2 || c === -2 || c === 2 || r === 0 && c === 0) {
matrix.set(row + r, col + c, true, true);
} else {
matrix.set(row + r, col + c, false, true);
}
}
}
}
}
function setupVersionInfo(matrix, version) {
const size = matrix.size;
const bits = Version.getEncodedBits(version);
let row, col, mod;
for (let i = 0; i < 18; i++) {
row = Math.floor(i / 3);
col = i % 3 + size - 8 - 3;
mod = (bits >> i & 1) === 1;
matrix.set(row, col, mod, true);
matrix.set(col, row, mod, true);
}
}
function setupFormatInfo(matrix, errorCorrectionLevel, maskPattern) {
const size = matrix.size;
const bits = FormatInfo.getEncodedBits(errorCorrectionLevel, maskPattern);
let i, mod;
for (i = 0; i < 15; i++) {
mod = (bits >> i & 1) === 1;
if (i < 6) {
matrix.set(i, 8, mod, true);
} else if (i < 8) {
matrix.set(i + 1, 8, mod, true);
} else {
matrix.set(size - 15 + i, 8, mod, true);
}
if (i < 8) {
matrix.set(8, size - i - 1, mod, true);
} else if (i < 9) {
matrix.set(8, 15 - i - 1 + 1, mod, true);
} else {
matrix.set(8, 15 - i - 1, mod, true);
}
}
matrix.set(size - 8, 8, 1, true);
}
function setupData(matrix, data) {
const size = matrix.size;
let inc = -1;
let row = size - 1;
let bitIndex = 7;
let byteIndex = 0;
for (let col = size - 1; col > 0; col -= 2) {
if (col === 6) col--;
while (true) {
for (let c = 0; c < 2; c++) {
if (!matrix.isReserved(row, col - c)) {
let dark = false;
if (byteIndex < data.length) {
dark = (data[byteIndex] >>> bitIndex & 1) === 1;
}
matrix.set(row, col - c, dark);
bitIndex--;
if (bitIndex === -1) {
byteIndex++;
bitIndex = 7;
}
}
}
row += inc;
if (row < 0 || size <= row) {
row -= inc;
inc = -inc;
break;
}
}
}
}
function createData(version, errorCorrectionLevel, segments) {
const buffer = new BitBuffer();
segments.forEach(function(data) {
buffer.put(data.mode.bit, 4);
buffer.put(data.getLength(), Mode.getCharCountIndicator(data.mode, version));
data.write(buffer);
});
const totalCodewords = Utils.getSymbolTotalCodewords(version);
const ecTotalCodewords = ECCode.getTotalCodewordsCount(version, errorCorrectionLevel);
const dataTotalCodewordsBits = (totalCodewords - ecTotalCodewords) * 8;
if (buffer.getLengthInBits() + 4 <= dataTotalCodewordsBits) {
buffer.put(0, 4);
}
while (buffer.getLengthInBits() % 8 !== 0) {
buffer.putBit(0);
}
const remainingByte = (dataTotalCodewordsBits - buffer.getLengthInBits()) / 8;
for (let i = 0; i < remainingByte; i++) {
buffer.put(i % 2 ? 17 : 236, 8);
}
return createCodewords(buffer, version, errorCorrectionLevel);
}
function createCodewords(bitBuffer, version, errorCorrectionLevel) {
const totalCodewords = Utils.getSymbolTotalCodewords(version);
const ecTotalCodewords = ECCode.getTotalCodewordsCount(version, errorCorrectionLevel);
const dataTotalCodewords = totalCodewords - ecTotalCodewords;
const ecTotalBlocks = ECCode.getBlocksCount(version, errorCorrectionLevel);
const blocksInGroup2 = totalCodewords % ecTotalBlocks;
const blocksInGroup1 = ecTotalBlocks - blocksInGroup2;
const totalCodewordsInGroup1 = Math.floor(totalCodewords / ecTotalBlocks);
const dataCodewordsInGroup1 = Math.floor(dataTotalCodewords / ecTotalBlocks);
const dataCodewordsInGroup2 = dataCodewordsInGroup1 + 1;
const ecCount = totalCodewordsInGroup1 - dataCodewordsInGroup1;
const rs = new ReedSolomonEncoder(ecCount);
let offset = 0;
const dcData = new Array(ecTotalBlocks);
const ecData = new Array(ecTotalBlocks);
let maxDataSize = 0;
const buffer = new Uint8Array(bitBuffer.buffer);
for (let b = 0; b < ecTotalBlocks; b++) {
const dataSize = b < blocksInGroup1 ? dataCodewordsInGroup1 : dataCodewordsInGroup2;
dcData[b] = buffer.slice(offset, offset + dataSize);
ecData[b] = rs.encode(dcData[b]);
offset += dataSize;
maxDataSize = Math.max(maxDataSize, dataSize);
}
const data = new Uint8Array(totalCodewords);
let index = 0;
let i, r;
for (i = 0; i < maxDataSize; i++) {
for (r = 0; r < ecTotalBlocks; r++) {
if (i < dcData[r].length) {
data[index++] = dcData[r][i];
}
}
}
for (i = 0; i < ecCount; i++) {
for (r = 0; r < ecTotalBlocks; r++) {
data[index++] = ecData[r][i];
}
}
return data;
}
function createSymbol(data, version, errorCorrectionLevel, maskPattern) {
let segments;
if (Array.isArray(data)) {
segments = Segments.fromArray(data);
} else if (typeof data === "string") {
let estimatedVersion = version;
if (!estimatedVersion) {
const rawSegments = Segments.rawSplit(data);
estimatedVersion = Version.getBestVersionForData(rawSegments, errorCorrectionLevel);
}
segments = Segments.fromString(data, estimatedVersion || 40);
} else {
throw new Error("Invalid data");
}
const bestVersion = Version.getBestVersionForData(segments, errorCorrectionLevel);
if (!bestVersion) {
throw new Error("The amount of data is too big to be stored in a QR Code");
}
if (!version) {
version = bestVersion;
} else if (version < bestVersion) {
throw new Error(
"\nThe chosen QR Code version cannot contain this amount of data.\nMinimum version required to store current data is: " + bestVersion + ".\n"
);
}
const dataBits = createData(version, errorCorrectionLevel, segments);
const moduleCount = Utils.getSymbolSize(version);
const modules = new BitMatrix(moduleCount);
setupFinderPattern(modules, version);
setupTimingPattern(modules);
setupAlignmentPattern(modules, version);
setupFormatInfo(modules, errorCorrectionLevel, 0);
if (version >= 7) {
setupVersionInfo(modules, version);
}
setupData(modules, dataBits);
if (isNaN(maskPattern)) {
maskPattern = MaskPattern.getBestMask(
modules,
setupFormatInfo.bind(null, modules, errorCorrectionLevel)
);
}
MaskPattern.applyMask(maskPattern, modules);
setupFormatInfo(modules, errorCorrectionLevel, maskPattern);
return {
modules,
version,
errorCorrectionLevel,
maskPattern,
segments
};
}
exports.create = function create(data, options) {
if (typeof data === "undefined" || data === "") {
throw new Error("No input text");
}
let errorCorrectionLevel = ECLevel.M;
let version;
let mask;
if (typeof options !== "undefined") {
errorCorrectionLevel = ECLevel.from(options.errorCorrectionLevel, ECLevel.M);
version = Version.from(options.version);
mask = MaskPattern.from(options.maskPattern);
if (options.toSJISFunc) {
Utils.setToSJISFunction(options.toSJISFunc);
}
}
return createSymbol(data, version, errorCorrectionLevel, mask);
};
}
});
// node_modules/qrcode/lib/renderer/utils.js
var require_utils2 = __commonJS({
"node_modules/qrcode/lib/renderer/utils.js"(exports) {
function hex2rgba(hex) {
if (typeof hex === "number") {
hex = hex.toString();
}
if (typeof hex !== "string") {
throw new Error("Color should be defined as hex string");
}
let hexCode = hex.slice().replace("#", "").split("");
if (hexCode.length < 3 || hexCode.length === 5 || hexCode.length > 8) {
throw new Error("Invalid hex color: " + hex);
}
if (hexCode.length === 3 || hexCode.length === 4) {
hexCode = Array.prototype.concat.apply([], hexCode.map(function(c) {
return [c, c];
}));
}
if (hexCode.length === 6) hexCode.push("F", "F");
const hexValue = parseInt(hexCode.join(""), 16);
return {
r: hexValue >> 24 & 255,
g: hexValue >> 16 & 255,
b: hexValue >> 8 & 255,
a: hexValue & 255,
hex: "#" + hexCode.slice(0, 6).join("")
};
}
exports.getOptions = function getOptions(options) {
if (!options) options = {};
if (!options.color) options.color = {};
const margin = typeof options.margin === "undefined" || options.margin === null || options.margin < 0 ? 4 : options.margin;
const width = options.width && options.width >= 21 ? options.width : void 0;
const scale = options.scale || 4;
return {
width,
scale: width ? 4 : scale,
margin,
color: {
dark: hex2rgba(options.color.dark || "#000000ff"),
light: hex2rgba(options.color.light || "#ffffffff")
},
type: options.type,
rendererOpts: options.rendererOpts || {}
};
};
exports.getScale = function getScale(qrSize, opts) {
return opts.width && opts.width >= qrSize + opts.margin * 2 ? opts.width / (qrSize + opts.margin * 2) : opts.scale;
};
exports.getImageWidth = function getImageWidth(qrSize, opts) {
const scale = exports.getScale(qrSize, opts);
return Math.floor((qrSize + opts.margin * 2) * scale);
};
exports.qrToImageData = function qrToImageData(imgData, qr, opts) {
const size = qr.modules.size;
const data = qr.modules.data;
const scale = exports.getScale(size, opts);
const symbolSize = Math.floor((size + opts.margin * 2) * scale);
const scaledMargin = opts.margin * scale;
const palette = [opts.color.light, opts.color.dark];
for (let i = 0; i < symbolSize; i++) {
for (let j = 0; j < symbolSize; j++) {
let posDst = (i * symbolSize + j) * 4;
let pxColor = opts.color.light;
if (i >= scaledMargin && j >= scaledMargin && i < symbolSize - scaledMargin && j < symbolSize - scaledMargin) {
const iSrc = Math.floor((i - scaledMargin) / scale);
const jSrc = Math.floor((j - scaledMargin) / scale);
pxColor = palette[data[iSrc * size + jSrc] ? 1 : 0];
}
imgData[posDst++] = pxColor.r;
imgData[posDst++] = pxColor.g;
imgData[posDst++] = pxColor.b;
imgData[posDst] = pxColor.a;
}
}
};
}
});
// node_modules/qrcode/lib/renderer/canvas.js
var require_canvas = __commonJS({
"node_modules/qrcode/lib/renderer/canvas.js"(exports) {
var Utils = require_utils2();
function clearCanvas(ctx, canvas, size) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (!canvas.style) canvas.style = {};
canvas.height = size;
canvas.width = size;
canvas.style.height = size + "px";
canvas.style.width = size + "px";
}
function getCanvasElement() {
try {
return document.createElement("canvas");
} catch (e) {
throw new Error("You need to specify a canvas element");
}
}
exports.render = function render(qrData, canvas, options) {
let opts = options;
let canvasEl = canvas;
if (typeof opts === "undefined" && (!canvas || !canvas.getContext)) {
opts = canvas;
canvas = void 0;
}
if (!canvas) {
canvasEl = getCanvasElement();
}
opts = Utils.getOptions(opts);
const size = Utils.getImageWidth(qrData.modules.size, opts);
const ctx = canvasEl.getContext("2d");
const image = ctx.createImageData(size, size);
Utils.qrToImageData(image.data, qrData, opts);
clearCanvas(ctx, canvasEl, size);
ctx.putImageData(image, 0, 0);
return canvasEl;
};
exports.renderToDataURL = function renderToDataURL(qrData, canvas, options) {
let opts = options;
if (typeof opts === "undefined" && (!canvas || !canvas.getContext)) {
opts = canvas;
canvas = void 0;
}
if (!opts) opts = {};
const canvasEl = exports.render(qrData, canvas, opts);
const type = opts.type || "image/png";
const rendererOpts = opts.rendererOpts || {};
return canvasEl.toDataURL(type, rendererOpts.quality);
};
}
});
// node_modules/qrcode/lib/renderer/svg-tag.js
var require_svg_tag = __commonJS({
"node_modules/qrcode/lib/renderer/svg-tag.js"(exports) {
var Utils = require_utils2();
function getColorAttrib(color, attrib) {
const alpha = color.a / 255;
const str = attrib + '="' + color.hex + '"';
return alpha < 1 ? str + " " + attrib + '-opacity="' + alpha.toFixed(2).slice(1) + '"' : str;
}
function svgCmd(cmd, x, y) {
let str = cmd + x;
if (typeof y !== "undefined") str += " " + y;
return str;
}
function qrToPath(data, size, margin) {
let path = "";
let moveBy = 0;
let newRow = false;
let lineLength = 0;
for (let i = 0; i < data.length; i++) {
const col = Math.floor(i % size);
const row = Math.floor(i / size);
if (!col && !newRow) newRow = true;
if (data[i]) {
lineLength++;
if (!(i > 0 && col > 0 && data[i - 1])) {
path += newRow ? svgCmd("M", col + margin, 0.5 + row + margin) : svgCmd("m", moveBy, 0);
moveBy = 0;
newRow = false;
}
if (!(col + 1 < size && data[i + 1])) {
path += svgCmd("h", lineLength);
lineLength = 0;
}
} else {
moveBy++;
}
}
return path;
}
exports.render = function render(qrData, options, cb) {
const opts = Utils.getOptions(options);
const size = qrData.modules.size;
const data = qrData.modules.data;
const qrcodesize = size + opts.margin * 2;
const bg = !opts.color.light.a ? "" : "
Gets the requested bit, where true means black.
* * @param x The horizontal component (i.e. which column) * @param y The vertical component (i.e. which row) * @return value of given bit in matrix */ get(x, y) { const offset = y * this.rowSize + Math.floor(x / 32); return (this.bits[offset] >>> (x & 31) & 1) !== 0; } /** *Sets the given bit to true.
* * @param x The horizontal component (i.e. which column) * @param y The vertical component (i.e. which row) */ set(x, y) { const offset = y * this.rowSize + Math.floor(x / 32); this.bits[offset] |= 1 << (x & 31) & 4294967295; } unset(x, y) { const offset = y * this.rowSize + Math.floor(x / 32); this.bits[offset] &= ~(1 << (x & 31) & 4294967295); } /** *Flips the given bit.
* * @param x The horizontal component (i.e. which column) * @param y The vertical component (i.e. which row) */ flip(x, y) { const offset = y * this.rowSize + Math.floor(x / 32); this.bits[offset] ^= 1 << (x & 31) & 4294967295; } /** * Exclusive-or (XOR): Flip the bit in this {@code BitMatrix} if the corresponding * mask bit is set. * * @param mask XOR mask */ xor(mask) { if (this.width !== mask.getWidth() || this.height !== mask.getHeight() || this.rowSize !== mask.getRowSize()) { throw new IllegalArgumentException("input matrix dimensions do not match"); } const rowArray = new BitArray(Math.floor(this.width / 32) + 1); const rowSize = this.rowSize; const bits = this.bits; for (let y = 0, height = this.height; y < height; y++) { const offset = y * rowSize; const row = mask.getRow(y, rowArray).getBitArray(); for (let x = 0; x < rowSize; x++) { bits[offset + x] ^= row[x]; } } } /** * Clears all bits (sets to false). */ clear() { const bits = this.bits; const max = bits.length; for (let i = 0; i < max; i++) { bits[i] = 0; } } /** *Sets a square region of the bit matrix to true.
* * @param left The horizontal position to begin at (inclusive) * @param top The vertical position to begin at (inclusive) * @param width The width of the region * @param height The height of the region */ setRegion(left, top, width, height) { if (top < 0 || left < 0) { throw new IllegalArgumentException("Left and top must be nonnegative"); } if (height < 1 || width < 1) { throw new IllegalArgumentException("Height and width must be at least 1"); } const right = left + width; const bottom = top + height; if (bottom > this.height || right > this.width) { throw new IllegalArgumentException("The region must fit inside the matrix"); } const rowSize = this.rowSize; const bits = this.bits; for (let y = top; y < bottom; y++) { const offset = y * rowSize; for (let x = left; x < right; x++) { bits[offset + Math.floor(x / 32)] |= 1 << (x & 31) & 4294967295; } } } /** * A fast method to retrieve one row of data from the matrix as a BitArray. * * @param y The row to retrieve * @param row An optional caller-allocated BitArray, will be allocated if null or too small * @return The resulting BitArray - this reference should always be used even when passing * your own row */ getRow(y, row) { if (row === null || row === void 0 || row.getSize() < this.width) { row = new BitArray(this.width); } else { row.clear(); } const rowSize = this.rowSize; const bits = this.bits; const offset = y * rowSize; for (let x = 0; x < rowSize; x++) { row.setBulk(x * 32, bits[offset + x]); } return row; } /** * @param y row to set * @param row {@link BitArray} to copy from */ setRow(y, row) { System.arraycopy(row.getBitArray(), 0, this.bits, y * this.rowSize, this.rowSize); } /** * Modifies this {@code BitMatrix} to represent the same but rotated 180 degrees */ rotate180() { const width = this.getWidth(); const height = this.getHeight(); let topRow = new BitArray(width); let bottomRow = new BitArray(width); for (let i = 0, length = Math.floor((height + 1) / 2); i < length; i++) { topRow = this.getRow(i, topRow); bottomRow = this.getRow(height - 1 - i, bottomRow); topRow.reverse(); bottomRow.reverse(); this.setRow(i, bottomRow); this.setRow(height - 1 - i, topRow); } } /** * This is useful in detecting the enclosing rectangle of a 'pure' barcode. * * @return {@code left,top,width,height} enclosing rectangle of all 1 bits, or null if it is all white */ getEnclosingRectangle() { const width = this.width; const height = this.height; const rowSize = this.rowSize; const bits = this.bits; let left = width; let top = height; let right = -1; let bottom = -1; for (let y = 0; y < height; y++) { for (let x32 = 0; x32 < rowSize; x32++) { const theBits = bits[y * rowSize + x32]; if (theBits !== 0) { if (y < top) { top = y; } if (y > bottom) { bottom = y; } if (x32 * 32 < left) { let bit = 0; while ((theBits << 31 - bit & 4294967295) === 0) { bit++; } if (x32 * 32 + bit < left) { left = x32 * 32 + bit; } } if (x32 * 32 + 31 > right) { let bit = 31; while (theBits >>> bit === 0) { bit--; } if (x32 * 32 + bit > right) { right = x32 * 32 + bit; } } } } } if (right < left || bottom < top) { return null; } return Int32Array.from([left, top, right - left + 1, bottom - top + 1]); } /** * This is useful in detecting a corner of a 'pure' barcode. * * @return {@code x,y} coordinate of top-left-most 1 bit, or null if it is all white */ getTopLeftOnBit() { const rowSize = this.rowSize; const bits = this.bits; let bitsOffset = 0; while (bitsOffset < bits.length && bits[bitsOffset] === 0) { bitsOffset++; } if (bitsOffset === bits.length) { return null; } const y = bitsOffset / rowSize; let x = bitsOffset % rowSize * 32; const theBits = bits[bitsOffset]; let bit = 0; while ((theBits << 31 - bit & 4294967295) === 0) { bit++; } x += bit; return Int32Array.from([x, y]); } getBottomRightOnBit() { const rowSize = this.rowSize; const bits = this.bits; let bitsOffset = bits.length - 1; while (bitsOffset >= 0 && bits[bitsOffset] === 0) { bitsOffset--; } if (bitsOffset < 0) { return null; } const y = Math.floor(bitsOffset / rowSize); let x = Math.floor(bitsOffset % rowSize) * 32; const theBits = bits[bitsOffset]; let bit = 31; while (theBits >>> bit === 0) { bit--; } x += bit; return Int32Array.from([x, y]); } /** * @return The width of the matrix */ getWidth() { return this.width; } /** * @return The height of the matrix */ getHeight() { return this.height; } /** * @return The row size of the matrix */ getRowSize() { return this.rowSize; } /*@Override*/ equals(o) { if (!(o instanceof BitMatrix)) { return false; } const other = o; return this.width === other.width && this.height === other.height && this.rowSize === other.rowSize && Arrays.equals(this.bits, other.bits); } /*@Override*/ hashCode() { let hash = this.width; hash = 31 * hash + this.width; hash = 31 * hash + this.height; hash = 31 * hash + this.rowSize; hash = 31 * hash + Arrays.hashCode(this.bits); return hash; } /** * @return string representation using "X" for set and " " for unset bits */ /*@Override*/ // public toString(): string { // return toString(": "X, " ") // } /** * @param setString representation of a set bit * @param unsetString representation of an unset bit * @return string representation of entire matrix utilizing given strings */ // public toString(setString: string = "X ", unsetString: string = " "): string { // return this.buildToString(setString, unsetString, "\n") // } /** * @param setString representation of a set bit * @param unsetString representation of an unset bit * @param lineSeparator newline character in string representation * @return string representation of entire matrix utilizing given strings and line separator * @deprecated call {@link #toString(String,String)} only, which uses \n line separator always */ // @Deprecated toString(setString = "X ", unsetString = " ", lineSeparator = "\n") { return this.buildToString(setString, unsetString, lineSeparator); } buildToString(setString, unsetString, lineSeparator) { let result = new StringBuilder(); for (let y = 0, height = this.height; y < height; y++) { for (let x = 0, width = this.width; x < width; x++) { result.append(this.get(x, y) ? setString : unsetString); } result.append(lineSeparator); } return result.toString(); } /*@Override*/ clone() { return new BitMatrix(this.width, this.height, this.rowSize, this.bits.slice()); } } class NotFoundException extends Exception { static getNotFoundInstance() { return new NotFoundException(); } } NotFoundException.kind = "NotFoundException"; class GlobalHistogramBinarizer extends Binarizer { constructor(source) { super(source); this.luminances = GlobalHistogramBinarizer.EMPTY; this.buckets = new Int32Array(GlobalHistogramBinarizer.LUMINANCE_BUCKETS); } // Applies simple sharpening to the row data to improve performance of the 1D Readers. /*@Override*/ getBlackRow(y, row) { const source = this.getLuminanceSource(); const width = source.getWidth(); if (row === void 0 || row === null || row.getSize() < width) { row = new BitArray(width); } else { row.clear(); } this.initArrays(width); const localLuminances = source.getRow(y, this.luminances); const localBuckets = this.buckets; for (let x = 0; x < width; x++) { localBuckets[(localLuminances[x] & 255) >> GlobalHistogramBinarizer.LUMINANCE_SHIFT]++; } const blackPoint = GlobalHistogramBinarizer.estimateBlackPoint(localBuckets); if (width < 3) { for (let x = 0; x < width; x++) { if ((localLuminances[x] & 255) < blackPoint) { row.set(x); } } } else { let left = localLuminances[0] & 255; let center = localLuminances[1] & 255; for (let x = 1; x < width - 1; x++) { const right = localLuminances[x + 1] & 255; if ((center * 4 - left - right) / 2 < blackPoint) { row.set(x); } left = center; center = right; } } return row; } // Does not sharpen the data, as this call is intended to only be used by 2D Readers. /*@Override*/ getBlackMatrix() { const source = this.getLuminanceSource(); const width = source.getWidth(); const height = source.getHeight(); const matrix = new BitMatrix(width, height); this.initArrays(width); const localBuckets = this.buckets; for (let y = 1; y < 5; y++) { const row = Math.floor(height * y / 5); const localLuminances2 = source.getRow(row, this.luminances); const right = Math.floor(width * 4 / 5); for (let x = Math.floor(width / 5); x < right; x++) { const pixel = localLuminances2[x] & 255; localBuckets[pixel >> GlobalHistogramBinarizer.LUMINANCE_SHIFT]++; } } const blackPoint = GlobalHistogramBinarizer.estimateBlackPoint(localBuckets); const localLuminances = source.getMatrix(); for (let y = 0; y < height; y++) { const offset = y * width; for (let x = 0; x < width; x++) { const pixel = localLuminances[offset + x] & 255; if (pixel < blackPoint) { matrix.set(x, y); } } } return matrix; } /*@Override*/ createBinarizer(source) { return new GlobalHistogramBinarizer(source); } initArrays(luminanceSize) { if (this.luminances.length < luminanceSize) { this.luminances = new Uint8ClampedArray(luminanceSize); } const buckets = this.buckets; for (let x = 0; x < GlobalHistogramBinarizer.LUMINANCE_BUCKETS; x++) { buckets[x] = 0; } } static estimateBlackPoint(buckets) { const numBuckets = buckets.length; let maxBucketCount = 0; let firstPeak = 0; let firstPeakSize = 0; for (let x = 0; x < numBuckets; x++) { if (buckets[x] > firstPeakSize) { firstPeak = x; firstPeakSize = buckets[x]; } if (buckets[x] > maxBucketCount) { maxBucketCount = buckets[x]; } } let secondPeak = 0; let secondPeakScore = 0; for (let x = 0; x < numBuckets; x++) { const distanceToBiggest = x - firstPeak; const score = buckets[x] * distanceToBiggest * distanceToBiggest; if (score > secondPeakScore) { secondPeak = x; secondPeakScore = score; } } if (firstPeak > secondPeak) { const temp = firstPeak; firstPeak = secondPeak; secondPeak = temp; } if (secondPeak - firstPeak <= numBuckets / 16) { throw new NotFoundException(); } let bestValley = secondPeak - 1; let bestValleyScore = -1; for (let x = secondPeak - 1; x > firstPeak; x--) { const fromFirst = x - firstPeak; const score = fromFirst * fromFirst * (secondPeak - x) * (maxBucketCount - buckets[x]); if (score > bestValleyScore) { bestValley = x; bestValleyScore = score; } } return bestValley << GlobalHistogramBinarizer.LUMINANCE_SHIFT; } } GlobalHistogramBinarizer.LUMINANCE_BITS = 5; GlobalHistogramBinarizer.LUMINANCE_SHIFT = 8 - GlobalHistogramBinarizer.LUMINANCE_BITS; GlobalHistogramBinarizer.LUMINANCE_BUCKETS = 1 << GlobalHistogramBinarizer.LUMINANCE_BITS; GlobalHistogramBinarizer.EMPTY = Uint8ClampedArray.from([0]); class HybridBinarizer2 extends GlobalHistogramBinarizer { constructor(source) { super(source); this.matrix = null; } /** * Calculates the final BitMatrix once for all requests. This could be called once from the * constructor instead, but there are some advantages to doing it lazily, such as making * profiling easier, and not doing heavy lifting when callers don't expect it. */ /*@Override*/ getBlackMatrix() { if (this.matrix !== null) { return this.matrix; } const source = this.getLuminanceSource(); const width = source.getWidth(); const height = source.getHeight(); if (width >= HybridBinarizer2.MINIMUM_DIMENSION && height >= HybridBinarizer2.MINIMUM_DIMENSION) { const luminances = source.getMatrix(); let subWidth = width >> HybridBinarizer2.BLOCK_SIZE_POWER; if ((width & HybridBinarizer2.BLOCK_SIZE_MASK) !== 0) { subWidth++; } let subHeight = height >> HybridBinarizer2.BLOCK_SIZE_POWER; if ((height & HybridBinarizer2.BLOCK_SIZE_MASK) !== 0) { subHeight++; } const blackPoints = HybridBinarizer2.calculateBlackPoints(luminances, subWidth, subHeight, width, height); const newMatrix = new BitMatrix(width, height); HybridBinarizer2.calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints, newMatrix); this.matrix = newMatrix; } else { this.matrix = super.getBlackMatrix(); } return this.matrix; } /*@Override*/ createBinarizer(source) { return new HybridBinarizer2(source); } /** * For each block in the image, calculate the average black point using a 5x5 grid * of the blocks around it. Also handles the corner cases (fractional blocks are computed based * on the last pixels in the row/column which are also used in the previous block). */ static calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints, matrix) { const maxYOffset = height - HybridBinarizer2.BLOCK_SIZE; const maxXOffset = width - HybridBinarizer2.BLOCK_SIZE; for (let y = 0; y < subHeight; y++) { let yoffset = y << HybridBinarizer2.BLOCK_SIZE_POWER; if (yoffset > maxYOffset) { yoffset = maxYOffset; } const top = HybridBinarizer2.cap(y, 2, subHeight - 3); for (let x = 0; x < subWidth; x++) { let xoffset = x << HybridBinarizer2.BLOCK_SIZE_POWER; if (xoffset > maxXOffset) { xoffset = maxXOffset; } const left = HybridBinarizer2.cap(x, 2, subWidth - 3); let sum = 0; for (let z = -2; z <= 2; z++) { const blackRow = blackPoints[top + z]; sum += blackRow[left - 2] + blackRow[left - 1] + blackRow[left] + blackRow[left + 1] + blackRow[left + 2]; } const average = sum / 25; HybridBinarizer2.thresholdBlock(luminances, xoffset, yoffset, average, width, matrix); } } } static cap(value, min, max) { return value < min ? min : value > max ? max : value; } /** * Applies a single threshold to a block of pixels. */ static thresholdBlock(luminances, xoffset, yoffset, threshold, stride, matrix) { for (let y = 0, offset = yoffset * stride + xoffset; y < HybridBinarizer2.BLOCK_SIZE; y++, offset += stride) { for (let x = 0; x < HybridBinarizer2.BLOCK_SIZE; x++) { if ((luminances[offset + x] & 255) <= threshold) { matrix.set(xoffset + x, yoffset + y); } } } } /** * Calculates a single black point for each block of pixels and saves it away. * See the following thread for a discussion of this algorithm: * http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0 */ static calculateBlackPoints(luminances, subWidth, subHeight, width, height) { const maxYOffset = height - HybridBinarizer2.BLOCK_SIZE; const maxXOffset = width - HybridBinarizer2.BLOCK_SIZE; const blackPoints = new Array(subHeight); for (let y = 0; y < subHeight; y++) { blackPoints[y] = new Int32Array(subWidth); let yoffset = y << HybridBinarizer2.BLOCK_SIZE_POWER; if (yoffset > maxYOffset) { yoffset = maxYOffset; } for (let x = 0; x < subWidth; x++) { let xoffset = x << HybridBinarizer2.BLOCK_SIZE_POWER; if (xoffset > maxXOffset) { xoffset = maxXOffset; } let sum = 0; let min = 255; let max = 0; for (let yy = 0, offset = yoffset * width + xoffset; yy < HybridBinarizer2.BLOCK_SIZE; yy++, offset += width) { for (let xx = 0; xx < HybridBinarizer2.BLOCK_SIZE; xx++) { const pixel = luminances[offset + xx] & 255; sum += pixel; if (pixel < min) { min = pixel; } if (pixel > max) { max = pixel; } } if (max - min > HybridBinarizer2.MIN_DYNAMIC_RANGE) { for (yy++, offset += width; yy < HybridBinarizer2.BLOCK_SIZE; yy++, offset += width) { for (let xx = 0; xx < HybridBinarizer2.BLOCK_SIZE; xx++) { sum += luminances[offset + xx] & 255; } } } } let average = sum >> HybridBinarizer2.BLOCK_SIZE_POWER * 2; if (max - min <= HybridBinarizer2.MIN_DYNAMIC_RANGE) { average = min / 2; if (y > 0 && x > 0) { const averageNeighborBlackPoint = (blackPoints[y - 1][x] + 2 * blackPoints[y][x - 1] + blackPoints[y - 1][x - 1]) / 4; if (min < averageNeighborBlackPoint) { average = averageNeighborBlackPoint; } } } blackPoints[y][x] = average; } } return blackPoints; } } HybridBinarizer2.BLOCK_SIZE_POWER = 3; HybridBinarizer2.BLOCK_SIZE = 1 << HybridBinarizer2.BLOCK_SIZE_POWER; HybridBinarizer2.BLOCK_SIZE_MASK = HybridBinarizer2.BLOCK_SIZE - 1; HybridBinarizer2.MINIMUM_DIMENSION = HybridBinarizer2.BLOCK_SIZE * 5; HybridBinarizer2.MIN_DYNAMIC_RANGE = 24; class LuminanceSource { constructor(width, height) { this.width = width; this.height = height; } /** * @return The width of the bitmap. */ getWidth() { return this.width; } /** * @return The height of the bitmap. */ getHeight() { return this.height; } /** * @return Whether this subclass supports cropping. */ isCropSupported() { return false; } /** * Returns a new object with cropped image data. Implementations may keep a reference to the * original data rather than a copy. Only callable if isCropSupported() is true. * * @param left The left coordinate, which must be in [0,getWidth()) * @param top The top coordinate, which must be in [0,getHeight()) * @param width The width of the rectangle to crop. * @param height The height of the rectangle to crop. * @return A cropped version of this object. */ crop(left, top, width, height) { throw new UnsupportedOperationException("This luminance source does not support cropping."); } /** * @return Whether this subclass supports counter-clockwise rotation. */ isRotateSupported() { return false; } /** * Returns a new object with rotated image data by 90 degrees counterclockwise. * Only callable if {@link #isRotateSupported()} is true. * * @return A rotated version of this object. */ rotateCounterClockwise() { throw new UnsupportedOperationException("This luminance source does not support rotation by 90 degrees."); } /** * Returns a new object with rotated image data by 45 degrees counterclockwise. * Only callable if {@link #isRotateSupported()} is true. * * @return A rotated version of this object. */ rotateCounterClockwise45() { throw new UnsupportedOperationException("This luminance source does not support rotation by 45 degrees."); } /*@Override*/ toString() { const row = new Uint8ClampedArray(this.width); let result = new StringBuilder(); for (let y = 0; y < this.height; y++) { const sourceRow = this.getRow(y, row); for (let x = 0; x < this.width; x++) { const luminance = sourceRow[x] & 255; let c; if (luminance < 64) { c = "#"; } else if (luminance < 128) { c = "+"; } else if (luminance < 192) { c = "."; } else { c = " "; } result.append(c); } result.append("\n"); } return result.toString(); } } class InvertedLuminanceSource extends LuminanceSource { constructor(delegate) { super(delegate.getWidth(), delegate.getHeight()); this.delegate = delegate; } /*@Override*/ getRow(y, row) { const sourceRow = this.delegate.getRow(y, row); const width = this.getWidth(); for (let i = 0; i < width; i++) { sourceRow[i] = /*(byte)*/ 255 - (sourceRow[i] & 255); } return sourceRow; } /*@Override*/ getMatrix() { const matrix = this.delegate.getMatrix(); const length = this.getWidth() * this.getHeight(); const invertedMatrix = new Uint8ClampedArray(length); for (let i = 0; i < length; i++) { invertedMatrix[i] = /*(byte)*/ 255 - (matrix[i] & 255); } return invertedMatrix; } /*@Override*/ isCropSupported() { return this.delegate.isCropSupported(); } /*@Override*/ crop(left, top, width, height) { return new InvertedLuminanceSource(this.delegate.crop(left, top, width, height)); } /*@Override*/ isRotateSupported() { return this.delegate.isRotateSupported(); } /** * @return original delegate {@link LuminanceSource} since invert undoes itself */ /*@Override*/ invert() { return this.delegate; } /*@Override*/ rotateCounterClockwise() { return new InvertedLuminanceSource(this.delegate.rotateCounterClockwise()); } /*@Override*/ rotateCounterClockwise45() { return new InvertedLuminanceSource(this.delegate.rotateCounterClockwise45()); } } class HTMLCanvasElementLuminanceSource2 extends LuminanceSource { constructor(canvas) { super(canvas.width, canvas.height); this.canvas = canvas; this.tempCanvasElement = null; this.buffer = HTMLCanvasElementLuminanceSource2.makeBufferFromCanvasImageData(canvas); } static makeBufferFromCanvasImageData(canvas) { const imageData = canvas.getContext("2d").getImageData(0, 0, canvas.width, canvas.height); return HTMLCanvasElementLuminanceSource2.toGrayscaleBuffer(imageData.data, canvas.width, canvas.height); } static toGrayscaleBuffer(imageBuffer, width, height) { const grayscaleBuffer = new Uint8ClampedArray(width * height); for (let i = 0, j = 0, length = imageBuffer.length; i < length; i += 4, j++) { let gray; const alpha = imageBuffer[i + 3]; if (alpha === 0) { gray = 255; } else { const pixelR = imageBuffer[i]; const pixelG = imageBuffer[i + 1]; const pixelB = imageBuffer[i + 2]; gray = 306 * pixelR + 601 * pixelG + 117 * pixelB + 512 >> 10; } grayscaleBuffer[j] = gray; } return grayscaleBuffer; } getRow(y, row) { if (y < 0 || y >= this.getHeight()) { throw new IllegalArgumentException("Requested row is outside the image: " + y); } const width = this.getWidth(); const start = y * width; if (row === null) { row = this.buffer.slice(start, start + width); } else { if (row.length < width) { row = new Uint8ClampedArray(width); } row.set(this.buffer.slice(start, start + width)); } return row; } getMatrix() { return this.buffer; } isCropSupported() { return true; } crop(left, top, width, height) { super.crop(left, top, width, height); return this; } /** * This is always true, since the image is a gray-scale image. * * @return true */ isRotateSupported() { return true; } rotateCounterClockwise() { this.rotate(-90); return this; } rotateCounterClockwise45() { this.rotate(-45); return this; } getTempCanvasElement() { if (null === this.tempCanvasElement) { const tempCanvasElement = this.canvas.ownerDocument.createElement("canvas"); tempCanvasElement.width = this.canvas.width; tempCanvasElement.height = this.canvas.height; this.tempCanvasElement = tempCanvasElement; } return this.tempCanvasElement; } rotate(angle) { const tempCanvasElement = this.getTempCanvasElement(); const tempContext = tempCanvasElement.getContext("2d"); const angleRadians = angle * HTMLCanvasElementLuminanceSource2.DEGREE_TO_RADIANS; const width = this.canvas.width; const height = this.canvas.height; const newWidth = Math.ceil(Math.abs(Math.cos(angleRadians)) * width + Math.abs(Math.sin(angleRadians)) * height); const newHeight = Math.ceil(Math.abs(Math.sin(angleRadians)) * width + Math.abs(Math.cos(angleRadians)) * height); tempCanvasElement.width = newWidth; tempCanvasElement.height = newHeight; tempContext.translate(newWidth / 2, newHeight / 2); tempContext.rotate(angleRadians); tempContext.drawImage(this.canvas, width / -2, height / -2); this.buffer = HTMLCanvasElementLuminanceSource2.makeBufferFromCanvasImageData(tempCanvasElement); return this; } invert() { return new InvertedLuminanceSource(this); } } HTMLCanvasElementLuminanceSource2.DEGREE_TO_RADIANS = Math.PI / 180; class VideoInputDevice { /** * Creates an instance of VideoInputDevice. * * @param {string} deviceId the video input device id * @param {string} label the label of the device if available */ constructor(deviceId, label, groupId) { this.deviceId = deviceId; this.label = label; this.kind = "videoinput"; this.groupId = groupId || void 0; } /** @inheritdoc */ toJSON() { return { kind: this.kind, groupId: this.groupId, deviceId: this.deviceId, label: this.label }; } } var __awaiter8 = (globalThis || global || self || window || void 0) && (globalThis || global || self || window || void 0).__awaiter || function(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function(resolve) { resolve(value); }); } return new (P || (P = Promise))(function(resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; class BrowserCodeReader { /** * Creates an instance of BrowserCodeReader. * @param {Reader} reader The reader instance to decode the barcode * @param {number} [timeBetweenScansMillis=500] the time delay between subsequent successful decode tries * * @memberOf BrowserCodeReader */ constructor(reader, timeBetweenScansMillis = 500, _hints) { this.reader = reader; this.timeBetweenScansMillis = timeBetweenScansMillis; this._hints = _hints; this._stopContinuousDecode = false; this._stopAsyncDecode = false; this._timeBetweenDecodingAttempts = 0; } /** * If navigator is present. */ get hasNavigator() { return typeof navigator !== "undefined"; } /** * If mediaDevices under navigator is supported. */ get isMediaDevicesSuported() { return this.hasNavigator && !!navigator.mediaDevices; } /** * If enumerateDevices under navigator is supported. */ get canEnumerateDevices() { return !!(this.isMediaDevicesSuported && navigator.mediaDevices.enumerateDevices); } /** Time between two decoding tries in milli seconds. */ get timeBetweenDecodingAttempts() { return this._timeBetweenDecodingAttempts; } /** * Change the time span the decoder waits between two decoding tries. * * @param {number} millis Time between two decoding tries in milli seconds. */ set timeBetweenDecodingAttempts(millis) { this._timeBetweenDecodingAttempts = millis < 0 ? 0 : millis; } /** * Sets the hints. */ set hints(hints) { this._hints = hints || null; } /** * Sets the hints. */ get hints() { return this._hints; } /** * Lists all the available video input devices. */ listVideoInputDevices() { return __awaiter8(this, void 0, void 0, function* () { if (!this.hasNavigator) { throw new Error("Can't enumerate devices, navigator is not present."); } if (!this.canEnumerateDevices) { throw new Error("Can't enumerate devices, method not supported."); } const devices = yield navigator.mediaDevices.enumerateDevices(); const videoDevices = []; for (const device of devices) { const kind = device.kind === "video" ? "videoinput" : device.kind; if (kind !== "videoinput") { continue; } const deviceId = device.deviceId || device.id; const label = device.label || `Video device ${videoDevices.length + 1}`; const groupId = device.groupId; const videoDevice = { deviceId, label, kind, groupId }; videoDevices.push(videoDevice); } return videoDevices; }); } /** * Obtain the list of available devices with type 'videoinput'. * * @returns {PromiseDecodes given set of received codewords, which include both data and error-correction * codewords. Really, this means it uses Reed-Solomon to detect and correct errors, in-place, * in the input.
* * @param received data and error-correction codewords * @param twoS number of error-correction codewords available * @throws ReedSolomonException if decoding fails for any reason */ decode(received, twoS) { const field = this.field; const poly = new GenericGFPoly(field, received); const syndromeCoefficients = new Int32Array(twoS); let noError = true; for (let i = 0; i < twoS; i++) { const evalResult = poly.evaluateAt(field.exp(i + field.getGeneratorBase())); syndromeCoefficients[syndromeCoefficients.length - 1 - i] = evalResult; if (evalResult !== 0) { noError = false; } } if (noError) { return; } const syndrome = new GenericGFPoly(field, syndromeCoefficients); const sigmaOmega = this.runEuclideanAlgorithm(field.buildMonomial(twoS, 1), syndrome, twoS); const sigma = sigmaOmega[0]; const omega = sigmaOmega[1]; const errorLocations = this.findErrorLocations(sigma); const errorMagnitudes = this.findErrorMagnitudes(omega, errorLocations); for (let i = 0; i < errorLocations.length; i++) { const position = received.length - 1 - field.log(errorLocations[i]); if (position < 0) { throw new ReedSolomonException("Bad error location"); } received[position] = GenericGF.addOrSubtract(received[position], errorMagnitudes[i]); } } runEuclideanAlgorithm(a, b, R) { if (a.getDegree() < b.getDegree()) { const temp = a; a = b; b = temp; } const field = this.field; let rLast = a; let r = b; let tLast = field.getZero(); let t = field.getOne(); while (r.getDegree() >= (R / 2 | 0)) { let rLastLast = rLast; let tLastLast = tLast; rLast = r; tLast = t; if (rLast.isZero()) { throw new ReedSolomonException("r_{i-1} was zero"); } r = rLastLast; let q = field.getZero(); const denominatorLeadingTerm = rLast.getCoefficient(rLast.getDegree()); const dltInverse = field.inverse(denominatorLeadingTerm); while (r.getDegree() >= rLast.getDegree() && !r.isZero()) { const degreeDiff = r.getDegree() - rLast.getDegree(); const scale = field.multiply(r.getCoefficient(r.getDegree()), dltInverse); q = q.addOrSubtract(field.buildMonomial(degreeDiff, scale)); r = r.addOrSubtract(rLast.multiplyByMonomial(degreeDiff, scale)); } t = q.multiply(tLast).addOrSubtract(tLastLast); if (r.getDegree() >= rLast.getDegree()) { throw new IllegalStateException("Division algorithm failed to reduce polynomial?"); } } const sigmaTildeAtZero = t.getCoefficient(0); if (sigmaTildeAtZero === 0) { throw new ReedSolomonException("sigmaTilde(0) was zero"); } const inverse = field.inverse(sigmaTildeAtZero); const sigma = t.multiplyScalar(inverse); const omega = r.multiplyScalar(inverse); return [sigma, omega]; } findErrorLocations(errorLocator) { const numErrors = errorLocator.getDegree(); if (numErrors === 1) { return Int32Array.from([errorLocator.getCoefficient(1)]); } const result = new Int32Array(numErrors); let e = 0; const field = this.field; for (let i = 1; i < field.getSize() && e < numErrors; i++) { if (errorLocator.evaluateAt(i) === 0) { result[e] = field.inverse(i); e++; } } if (e !== numErrors) { throw new ReedSolomonException("Error locator degree does not match number of roots"); } return result; } findErrorMagnitudes(errorEvaluator, errorLocations) { const s = errorLocations.length; const result = new Int32Array(s); const field = this.field; for (let i = 0; i < s; i++) { const xiInverse = field.inverse(errorLocations[i]); let denominator = 1; for (let j = 0; j < s; j++) { if (i !== j) { const term = field.multiply(errorLocations[j], xiInverse); const termPlus1 = (term & 1) === 0 ? term | 1 : term & ~1; denominator = field.multiply(denominator, termPlus1); } } result[i] = field.multiply(errorEvaluator.evaluateAt(xiInverse), field.inverse(denominator)); if (field.getGeneratorBase() !== 0) { result[i] = field.multiply(result[i], xiInverse); } } return result; } } var Table; (function(Table2) { Table2[Table2["UPPER"] = 0] = "UPPER"; Table2[Table2["LOWER"] = 1] = "LOWER"; Table2[Table2["MIXED"] = 2] = "MIXED"; Table2[Table2["DIGIT"] = 3] = "DIGIT"; Table2[Table2["PUNCT"] = 4] = "PUNCT"; Table2[Table2["BINARY"] = 5] = "BINARY"; })(Table || (Table = {})); class Decoder { decode(detectorResult) { this.ddata = detectorResult; let matrix = detectorResult.getBits(); let rawbits = this.extractBits(matrix); let correctedBits = this.correctBits(rawbits); let rawBytes = Decoder.convertBoolArrayToByteArray(correctedBits); let result = Decoder.getEncodedData(correctedBits); let decoderResult = new DecoderResult(rawBytes, result, null, null); decoderResult.setNumBits(correctedBits.length); return decoderResult; } // This method is used for testing the high-level encoder static highLevelDecode(correctedBits) { return this.getEncodedData(correctedBits); } /** * Gets the string encoded in the aztec code bits * * @return the decoded string */ static getEncodedData(correctedBits) { let endIndex = correctedBits.length; let latchTable = Table.UPPER; let shiftTable = Table.UPPER; let result = ""; let index = 0; while (index < endIndex) { if (shiftTable === Table.BINARY) { if (endIndex - index < 5) { break; } let length = Decoder.readCode(correctedBits, index, 5); index += 5; if (length === 0) { if (endIndex - index < 11) { break; } length = Decoder.readCode(correctedBits, index, 11) + 31; index += 11; } for (let charCount = 0; charCount < length; charCount++) { if (endIndex - index < 8) { index = endIndex; break; } const code = Decoder.readCode(correctedBits, index, 8); result += /*(char)*/ StringUtils.castAsNonUtf8Char(code); index += 8; } shiftTable = latchTable; } else { let size = shiftTable === Table.DIGIT ? 4 : 5; if (endIndex - index < size) { break; } let code = Decoder.readCode(correctedBits, index, size); index += size; let str = Decoder.getCharacter(shiftTable, code); if (str.startsWith("CTRL_")) { latchTable = shiftTable; shiftTable = Decoder.getTable(str.charAt(5)); if (str.charAt(6) === "L") { latchTable = shiftTable; } } else { result += str; shiftTable = latchTable; } } } return result; } /** * gets the table corresponding to the char passed */ static getTable(t) { switch (t) { case "L": return Table.LOWER; case "P": return Table.PUNCT; case "M": return Table.MIXED; case "D": return Table.DIGIT; case "B": return Table.BINARY; case "U": default: return Table.UPPER; } } /** * Gets the character (or string) corresponding to the passed code in the given table * * @param table the table used * @param code the code of the character */ static getCharacter(table, code) { switch (table) { case Table.UPPER: return Decoder.UPPER_TABLE[code]; case Table.LOWER: return Decoder.LOWER_TABLE[code]; case Table.MIXED: return Decoder.MIXED_TABLE[code]; case Table.PUNCT: return Decoder.PUNCT_TABLE[code]; case Table.DIGIT: return Decoder.DIGIT_TABLE[code]; default: throw new IllegalStateException("Bad table"); } } /** *Performs RS error correction on an array of bits.
* * @return the corrected array * @throws FormatException if the input contains too many errors */ correctBits(rawbits) { let gf; let codewordSize; if (this.ddata.getNbLayers() <= 2) { codewordSize = 6; gf = GenericGF.AZTEC_DATA_6; } else if (this.ddata.getNbLayers() <= 8) { codewordSize = 8; gf = GenericGF.AZTEC_DATA_8; } else if (this.ddata.getNbLayers() <= 22) { codewordSize = 10; gf = GenericGF.AZTEC_DATA_10; } else { codewordSize = 12; gf = GenericGF.AZTEC_DATA_12; } let numDataCodewords = this.ddata.getNbDatablocks(); let numCodewords = rawbits.length / codewordSize; if (numCodewords < numDataCodewords) { throw new FormatException(); } let offset = rawbits.length % codewordSize; let dataWords = new Int32Array(numCodewords); for (let i = 0; i < numCodewords; i++, offset += codewordSize) { dataWords[i] = Decoder.readCode(rawbits, offset, codewordSize); } try { let rsDecoder = new ReedSolomonDecoder(gf); rsDecoder.decode(dataWords, numCodewords - numDataCodewords); } catch (ex) { throw new FormatException(ex); } let mask = (1 << codewordSize) - 1; let stuffedBits = 0; for (let i = 0; i < numDataCodewords; i++) { let dataWord = dataWords[i]; if (dataWord === 0 || dataWord === mask) { throw new FormatException(); } else if (dataWord === 1 || dataWord === mask - 1) { stuffedBits++; } } let correctedBits = new Array(numDataCodewords * codewordSize - stuffedBits); let index = 0; for (let i = 0; i < numDataCodewords; i++) { let dataWord = dataWords[i]; if (dataWord === 1 || dataWord === mask - 1) { correctedBits.fill(dataWord > 1, index, index + codewordSize - 1); index += codewordSize - 1; } else { for (let bit = codewordSize - 1; bit >= 0; --bit) { correctedBits[index++] = (dataWord & 1 << bit) !== 0; } } } return correctedBits; } /** * Gets the array of bits from an Aztec Code matrix * * @return the array of bits */ extractBits(matrix) { let compact = this.ddata.isCompact(); let layers = this.ddata.getNbLayers(); let baseMatrixSize = (compact ? 11 : 14) + layers * 4; let alignmentMap = new Int32Array(baseMatrixSize); let rawbits = new Array(this.totalBitsInLayer(layers, compact)); if (compact) { for (let i = 0; i < alignmentMap.length; i++) { alignmentMap[i] = i; } } else { let matrixSize = baseMatrixSize + 1 + 2 * Integer.truncDivision(Integer.truncDivision(baseMatrixSize, 2) - 1, 15); let origCenter = baseMatrixSize / 2; let center = Integer.truncDivision(matrixSize, 2); for (let i = 0; i < origCenter; i++) { let newOffset = i + Integer.truncDivision(i, 15); alignmentMap[origCenter - i - 1] = center - newOffset - 1; alignmentMap[origCenter + i] = center + newOffset + 1; } } for (let i = 0, rowOffset = 0; i < layers; i++) { let rowSize = (layers - i) * 4 + (compact ? 9 : 12); let low = i * 2; let high = baseMatrixSize - 1 - low; for (let j = 0; j < rowSize; j++) { let columnOffset = j * 2; for (let k = 0; k < 2; k++) { rawbits[rowOffset + columnOffset + k] = matrix.get(alignmentMap[low + k], alignmentMap[low + j]); rawbits[rowOffset + 2 * rowSize + columnOffset + k] = matrix.get(alignmentMap[low + j], alignmentMap[high - k]); rawbits[rowOffset + 4 * rowSize + columnOffset + k] = matrix.get(alignmentMap[high - k], alignmentMap[high - j]); rawbits[rowOffset + 6 * rowSize + columnOffset + k] = matrix.get(alignmentMap[high - j], alignmentMap[low + k]); } } rowOffset += rowSize * 8; } return rawbits; } /** * Reads a code of given length and at given index in an array of bits */ static readCode(rawbits, startIndex, length) { let res = 0; for (let i = startIndex; i < startIndex + length; i++) { res <<= 1; if (rawbits[i]) { res |= 1; } } return res; } /** * Reads a code of length 8 in an array of bits, padding with zeros */ static readByte(rawbits, startIndex) { let n = rawbits.length - startIndex; if (n >= 8) { return Decoder.readCode(rawbits, startIndex, 8); } return Decoder.readCode(rawbits, startIndex, n) << 8 - n; } /** * Packs a bit array into bytes, most significant bit first */ static convertBoolArrayToByteArray(boolArr) { let byteArr = new Uint8Array((boolArr.length + 7) / 8); for (let i = 0; i < byteArr.length; i++) { byteArr[i] = Decoder.readByte(boolArr, 8 * i); } return byteArr; } totalBitsInLayer(layers, compact) { return ((compact ? 88 : 112) + 16 * layers) * layers; } } Decoder.UPPER_TABLE = [ "CTRL_PS", " ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "CTRL_LL", "CTRL_ML", "CTRL_DL", "CTRL_BS" ]; Decoder.LOWER_TABLE = [ "CTRL_PS", " ", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "CTRL_US", "CTRL_ML", "CTRL_DL", "CTRL_BS" ]; Decoder.MIXED_TABLE = [ // Module parse failed: Octal literal in strict mode (50:29) // so number string were scaped "CTRL_PS", " ", "\\1", "\\2", "\\3", "\\4", "\\5", "\\6", "\\7", "\b", " ", "\n", "\\13", "\f", "\r", "\\33", "\\34", "\\35", "\\36", "\\37", "@", "\\", "^", "_", "`", "|", "~", "\\177", "CTRL_LL", "CTRL_UL", "CTRL_PL", "CTRL_BS" ]; Decoder.PUNCT_TABLE = [ "", "\r", "\r\n", ". ", ", ", ": ", "!", '"', "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "<", "=", ">", "?", "[", "]", "{", "}", "CTRL_UL" ]; Decoder.DIGIT_TABLE = [ "CTRL_PS", " ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ",", ".", "CTRL_UL", "CTRL_US" ]; class MathUtils { constructor() { } /** * Ends up being a bit faster than {@link Math#round(float)}. This merely rounds its * argument to the nearest int, where x.5 rounds up to x+1. Semantics of this shortcut * differ slightly from {@link Math#round(float)} in that half rounds down for negative * values. -2.5 rounds to -3, not -2. For purposes here it makes no difference. * * @param d real value to round * @return nearest {@code int} */ static round(d) { if (NaN === d) return 0; if (d <= Number.MIN_SAFE_INTEGER) return Number.MIN_SAFE_INTEGER; if (d >= Number.MAX_SAFE_INTEGER) return Number.MAX_SAFE_INTEGER; return ( /*(int) */ d + (d < 0 ? -0.5 : 0.5) | 0 ); } // TYPESCRIPTPORT: maybe remove round method and call directly Math.round, it looks like it doesn't make sense for js /** * @param aX point A x coordinate * @param aY point A y coordinate * @param bX point B x coordinate * @param bY point B y coordinate * @return Euclidean distance between points A and B */ static distance(aX, aY, bX, bY) { const xDiff = aX - bX; const yDiff = aY - bY; return ( /*(float) */ Math.sqrt(xDiff * xDiff + yDiff * yDiff) ); } /** * @param aX point A x coordinate * @param aY point A y coordinate * @param bX point B x coordinate * @param bY point B y coordinate * @return Euclidean distance between points A and B */ // public static distance(aX: number /*int*/, aY: number /*int*/, bX: number /*int*/, bY: number /*int*/): float { // const xDiff = aX - bX // const yDiff = aY - bY // return (float) Math.sqrt(xDiff * xDiff + yDiff * yDiff); // } /** * @param array values to sum * @return sum of values in array */ static sum(array) { let count = 0; for (let i = 0, length = array.length; i !== length; i++) { const a = array[i]; count += a; } return count; } } class Float { /** * SincTS has no difference between int and float, there's all numbers, * this is used only to polyfill Java code. */ static floatToIntBits(f) { return f; } } Float.MAX_VALUE = Number.MAX_SAFE_INTEGER; class ResultPoint { constructor(x, y) { this.x = x; this.y = y; } getX() { return this.x; } getY() { return this.y; } /*@Override*/ equals(other) { if (other instanceof ResultPoint) { const otherPoint = other; return this.x === otherPoint.x && this.y === otherPoint.y; } return false; } /*@Override*/ hashCode() { return 31 * Float.floatToIntBits(this.x) + Float.floatToIntBits(this.y); } /*@Override*/ toString() { return "(" + this.x + "," + this.y + ")"; } /** * Orders an array of three ResultPoints in an order [A,B,C] such that AB is less than AC * and BC is less than AC, and the angle between BC and BA is less than 180 degrees. * * @param patterns array of three {@code ResultPoint} to order */ static orderBestPatterns(patterns) { const zeroOneDistance = this.distance(patterns[0], patterns[1]); const oneTwoDistance = this.distance(patterns[1], patterns[2]); const zeroTwoDistance = this.distance(patterns[0], patterns[2]); let pointA; let pointB; let pointC; if (oneTwoDistance >= zeroOneDistance && oneTwoDistance >= zeroTwoDistance) { pointB = patterns[0]; pointA = patterns[1]; pointC = patterns[2]; } else if (zeroTwoDistance >= oneTwoDistance && zeroTwoDistance >= zeroOneDistance) { pointB = patterns[1]; pointA = patterns[0]; pointC = patterns[2]; } else { pointB = patterns[2]; pointA = patterns[0]; pointC = patterns[1]; } if (this.crossProductZ(pointA, pointB, pointC) < 0) { const temp = pointA; pointA = pointC; pointC = temp; } patterns[0] = pointA; patterns[1] = pointB; patterns[2] = pointC; } /** * @param pattern1 first pattern * @param pattern2 second pattern * @return distance between two points */ static distance(pattern1, pattern2) { return MathUtils.distance(pattern1.x, pattern1.y, pattern2.x, pattern2.y); } /** * Returns the z component of the cross product between vectors BC and BA. */ static crossProductZ(pointA, pointB, pointC) { const bX = pointB.x; const bY = pointB.y; return (pointC.x - bX) * (pointA.y - bY) - (pointC.y - bY) * (pointA.x - bX); } } class DetectorResult { constructor(bits, points) { this.bits = bits; this.points = points; } getBits() { return this.bits; } getPoints() { return this.points; } } class AztecDetectorResult extends DetectorResult { constructor(bits, points, compact, nbDatablocks, nbLayers) { super(bits, points); this.compact = compact; this.nbDatablocks = nbDatablocks; this.nbLayers = nbLayers; } getNbLayers() { return this.nbLayers; } getNbDatablocks() { return this.nbDatablocks; } isCompact() { return this.compact; } } class WhiteRectangleDetector { // public constructor(private image: BitMatrix) /*throws NotFoundException*/ { // this(image, INIT_SIZE, image.getWidth() / 2, image.getHeight() / 2) // } /** * @param image barcode image to find a rectangle in * @param initSize initial size of search area around center * @param x x position of search center * @param y y position of search center * @throws NotFoundException if image is too small to accommodate {@code initSize} */ constructor(image, initSize, x, y) { this.image = image; this.height = image.getHeight(); this.width = image.getWidth(); if (void 0 === initSize || null === initSize) { initSize = WhiteRectangleDetector.INIT_SIZE; } if (void 0 === x || null === x) { x = image.getWidth() / 2 | 0; } if (void 0 === y || null === y) { y = image.getHeight() / 2 | 0; } const halfsize = initSize / 2 | 0; this.leftInit = x - halfsize; this.rightInit = x + halfsize; this.upInit = y - halfsize; this.downInit = y + halfsize; if (this.upInit < 0 || this.leftInit < 0 || this.downInit >= this.height || this.rightInit >= this.width) { throw new NotFoundException(); } } /** ** Detects a candidate barcode-like rectangular region within an image. It * starts around the center of the image, increases the size of the candidate * region until it finds a white rectangular region. *
* * @return {@link ResultPoint}[] describing the corners of the rectangular * region. The first and last points are opposed on the diagonal, as * are the second and third. The first point will be the topmost * point and the last, the bottommost. The second point will be * leftmost and the third, the rightmost * @throws NotFoundException if no Data Matrix Code can be found */ detect() { let left = this.leftInit; let right = this.rightInit; let up = this.upInit; let down = this.downInit; let sizeExceeded = false; let aBlackPointFoundOnBorder = true; let atLeastOneBlackPointFoundOnBorder = false; let atLeastOneBlackPointFoundOnRight = false; let atLeastOneBlackPointFoundOnBottom = false; let atLeastOneBlackPointFoundOnLeft = false; let atLeastOneBlackPointFoundOnTop = false; const width = this.width; const height = this.height; while (aBlackPointFoundOnBorder) { aBlackPointFoundOnBorder = false; let rightBorderNotWhite = true; while ((rightBorderNotWhite || !atLeastOneBlackPointFoundOnRight) && right < width) { rightBorderNotWhite = this.containsBlackPoint(up, down, right, false); if (rightBorderNotWhite) { right++; aBlackPointFoundOnBorder = true; atLeastOneBlackPointFoundOnRight = true; } else if (!atLeastOneBlackPointFoundOnRight) { right++; } } if (right >= width) { sizeExceeded = true; break; } let bottomBorderNotWhite = true; while ((bottomBorderNotWhite || !atLeastOneBlackPointFoundOnBottom) && down < height) { bottomBorderNotWhite = this.containsBlackPoint(left, right, down, true); if (bottomBorderNotWhite) { down++; aBlackPointFoundOnBorder = true; atLeastOneBlackPointFoundOnBottom = true; } else if (!atLeastOneBlackPointFoundOnBottom) { down++; } } if (down >= height) { sizeExceeded = true; break; } let leftBorderNotWhite = true; while ((leftBorderNotWhite || !atLeastOneBlackPointFoundOnLeft) && left >= 0) { leftBorderNotWhite = this.containsBlackPoint(up, down, left, false); if (leftBorderNotWhite) { left--; aBlackPointFoundOnBorder = true; atLeastOneBlackPointFoundOnLeft = true; } else if (!atLeastOneBlackPointFoundOnLeft) { left--; } } if (left < 0) { sizeExceeded = true; break; } let topBorderNotWhite = true; while ((topBorderNotWhite || !atLeastOneBlackPointFoundOnTop) && up >= 0) { topBorderNotWhite = this.containsBlackPoint(left, right, up, true); if (topBorderNotWhite) { up--; aBlackPointFoundOnBorder = true; atLeastOneBlackPointFoundOnTop = true; } else if (!atLeastOneBlackPointFoundOnTop) { up--; } } if (up < 0) { sizeExceeded = true; break; } if (aBlackPointFoundOnBorder) { atLeastOneBlackPointFoundOnBorder = true; } } if (!sizeExceeded && atLeastOneBlackPointFoundOnBorder) { const maxSize = right - left; let z = null; for (let i = 1; z === null && i < maxSize; i++) { z = this.getBlackPointOnSegment(left, down - i, left + i, down); } if (z == null) { throw new NotFoundException(); } let t = null; for (let i = 1; t === null && i < maxSize; i++) { t = this.getBlackPointOnSegment(left, up + i, left + i, up); } if (t == null) { throw new NotFoundException(); } let x = null; for (let i = 1; x === null && i < maxSize; i++) { x = this.getBlackPointOnSegment(right, up + i, right - i, up); } if (x == null) { throw new NotFoundException(); } let y = null; for (let i = 1; y === null && i < maxSize; i++) { y = this.getBlackPointOnSegment(right, down - i, right - i, down); } if (y == null) { throw new NotFoundException(); } return this.centerEdges(y, z, x, t); } else { throw new NotFoundException(); } } getBlackPointOnSegment(aX, aY, bX, bY) { const dist = MathUtils.round(MathUtils.distance(aX, aY, bX, bY)); const xStep = (bX - aX) / dist; const yStep = (bY - aY) / dist; const image = this.image; for (let i = 0; i < dist; i++) { const x = MathUtils.round(aX + i * xStep); const y = MathUtils.round(aY + i * yStep); if (image.get(x, y)) { return new ResultPoint(x, y); } } return null; } /** * recenters the points of a constant distance towards the center * * @param y bottom most point * @param z left most point * @param x right most point * @param t top most point * @return {@link ResultPoint}[] describing the corners of the rectangular * region. The first and last points are opposed on the diagonal, as * are the second and third. The first point will be the topmost * point and the last, the bottommost. The second point will be * leftmost and the third, the rightmost */ centerEdges(y, z, x, t) { const yi = y.getX(); const yj = y.getY(); const zi = z.getX(); const zj = z.getY(); const xi = x.getX(); const xj = x.getY(); const ti = t.getX(); const tj = t.getY(); const CORR = WhiteRectangleDetector.CORR; if (yi < this.width / 2) { return [ new ResultPoint(ti - CORR, tj + CORR), new ResultPoint(zi + CORR, zj + CORR), new ResultPoint(xi - CORR, xj - CORR), new ResultPoint(yi + CORR, yj - CORR) ]; } else { return [ new ResultPoint(ti + CORR, tj + CORR), new ResultPoint(zi + CORR, zj - CORR), new ResultPoint(xi - CORR, xj + CORR), new ResultPoint(yi - CORR, yj - CORR) ]; } } /** * Determines whether a segment contains a black point * * @param a min value of the scanned coordinate * @param b max value of the scanned coordinate * @param fixed value of fixed coordinate * @param horizontal set to true if scan must be horizontal, false if vertical * @return true if a black point has been found, else false. */ containsBlackPoint(a, b, fixed, horizontal) { const image = this.image; if (horizontal) { for (let x = a; x <= b; x++) { if (image.get(x, fixed)) { return true; } } } else { for (let y = a; y <= b; y++) { if (image.get(fixed, y)) { return true; } } } return false; } } WhiteRectangleDetector.INIT_SIZE = 10; WhiteRectangleDetector.CORR = 1; class GridSampler { /** *Checks a set of points that have been transformed to sample points on an image against * the image's dimensions to see if the point are even within the image.
* *This method will actually "nudge" the endpoints back onto the image if they are found to be * barely (less than 1 pixel) off the image. This accounts for imperfect detection of finder * patterns in an image where the QR Code runs all the way to the image border.
* *For efficiency, the method will check points from either end of the line until one is found * to be within the image. Because the set of points are assumed to be linear, this is valid.
* * @param image image into which the points should map * @param points actual points in x1,y1,...,xn,yn form * @throws NotFoundException if an endpoint is lies outside the image boundaries */ static checkAndNudgePoints(image, points) { const width = image.getWidth(); const height = image.getHeight(); let nudged = true; for (let offset = 0; offset < points.length && nudged; offset += 2) { const x = Math.floor(points[offset]); const y = Math.floor(points[offset + 1]); if (x < -1 || x > width || y < -1 || y > height) { throw new NotFoundException(); } nudged = false; if (x === -1) { points[offset] = 0; nudged = true; } else if (x === width) { points[offset] = width - 1; nudged = true; } if (y === -1) { points[offset + 1] = 0; nudged = true; } else if (y === height) { points[offset + 1] = height - 1; nudged = true; } } nudged = true; for (let offset = points.length - 2; offset >= 0 && nudged; offset -= 2) { const x = Math.floor(points[offset]); const y = Math.floor(points[offset + 1]); if (x < -1 || x > width || y < -1 || y > height) { throw new NotFoundException(); } nudged = false; if (x === -1) { points[offset] = 0; nudged = true; } else if (x === width) { points[offset] = width - 1; nudged = true; } if (y === -1) { points[offset + 1] = 0; nudged = true; } else if (y === height) { points[offset + 1] = height - 1; nudged = true; } } } } class PerspectiveTransform { constructor(a11, a21, a31, a12, a22, a32, a13, a23, a33) { this.a11 = a11; this.a21 = a21; this.a31 = a31; this.a12 = a12; this.a22 = a22; this.a32 = a32; this.a13 = a13; this.a23 = a23; this.a33 = a33; } static quadrilateralToQuadrilateral(x0, y0, x1, y1, x2, y2, x3, y3, x0p, y0p, x1p, y1p, x2p, y2p, x3p, y3p) { const qToS = PerspectiveTransform.quadrilateralToSquare(x0, y0, x1, y1, x2, y2, x3, y3); const sToQ = PerspectiveTransform.squareToQuadrilateral(x0p, y0p, x1p, y1p, x2p, y2p, x3p, y3p); return sToQ.times(qToS); } transformPoints(points) { const max = points.length; const a11 = this.a11; const a12 = this.a12; const a13 = this.a13; const a21 = this.a21; const a22 = this.a22; const a23 = this.a23; const a31 = this.a31; const a32 = this.a32; const a33 = this.a33; for (let i = 0; i < max; i += 2) { const x = points[i]; const y = points[i + 1]; const denominator = a13 * x + a23 * y + a33; points[i] = (a11 * x + a21 * y + a31) / denominator; points[i + 1] = (a12 * x + a22 * y + a32) / denominator; } } transformPointsWithValues(xValues, yValues) { const a11 = this.a11; const a12 = this.a12; const a13 = this.a13; const a21 = this.a21; const a22 = this.a22; const a23 = this.a23; const a31 = this.a31; const a32 = this.a32; const a33 = this.a33; const n = xValues.length; for (let i = 0; i < n; i++) { const x = xValues[i]; const y = yValues[i]; const denominator = a13 * x + a23 * y + a33; xValues[i] = (a11 * x + a21 * y + a31) / denominator; yValues[i] = (a12 * x + a22 * y + a32) / denominator; } } static squareToQuadrilateral(x0, y0, x1, y1, x2, y2, x3, y3) { const dx3 = x0 - x1 + x2 - x3; const dy3 = y0 - y1 + y2 - y3; if (dx3 === 0 && dy3 === 0) { return new PerspectiveTransform(x1 - x0, x2 - x1, x0, y1 - y0, y2 - y1, y0, 0, 0, 1); } else { const dx1 = x1 - x2; const dx2 = x3 - x2; const dy1 = y1 - y2; const dy2 = y3 - y2; const denominator = dx1 * dy2 - dx2 * dy1; const a13 = (dx3 * dy2 - dx2 * dy3) / denominator; const a23 = (dx1 * dy3 - dx3 * dy1) / denominator; return new PerspectiveTransform(x1 - x0 + a13 * x1, x3 - x0 + a23 * x3, x0, y1 - y0 + a13 * y1, y3 - y0 + a23 * y3, y0, a13, a23, 1); } } static quadrilateralToSquare(x0, y0, x1, y1, x2, y2, x3, y3) { return PerspectiveTransform.squareToQuadrilateral(x0, y0, x1, y1, x2, y2, x3, y3).buildAdjoint(); } buildAdjoint() { return new PerspectiveTransform(this.a22 * this.a33 - this.a23 * this.a32, this.a23 * this.a31 - this.a21 * this.a33, this.a21 * this.a32 - this.a22 * this.a31, this.a13 * this.a32 - this.a12 * this.a33, this.a11 * this.a33 - this.a13 * this.a31, this.a12 * this.a31 - this.a11 * this.a32, this.a12 * this.a23 - this.a13 * this.a22, this.a13 * this.a21 - this.a11 * this.a23, this.a11 * this.a22 - this.a12 * this.a21); } times(other) { return new PerspectiveTransform(this.a11 * other.a11 + this.a21 * other.a12 + this.a31 * other.a13, this.a11 * other.a21 + this.a21 * other.a22 + this.a31 * other.a23, this.a11 * other.a31 + this.a21 * other.a32 + this.a31 * other.a33, this.a12 * other.a11 + this.a22 * other.a12 + this.a32 * other.a13, this.a12 * other.a21 + this.a22 * other.a22 + this.a32 * other.a23, this.a12 * other.a31 + this.a22 * other.a32 + this.a32 * other.a33, this.a13 * other.a11 + this.a23 * other.a12 + this.a33 * other.a13, this.a13 * other.a21 + this.a23 * other.a22 + this.a33 * other.a23, this.a13 * other.a31 + this.a23 * other.a32 + this.a33 * other.a33); } } class DefaultGridSampler extends GridSampler { /*@Override*/ sampleGrid(image, dimensionX, dimensionY, p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY, p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY) { const transform = PerspectiveTransform.quadrilateralToQuadrilateral(p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY, p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY); return this.sampleGridWithTransform(image, dimensionX, dimensionY, transform); } /*@Override*/ sampleGridWithTransform(image, dimensionX, dimensionY, transform) { if (dimensionX <= 0 || dimensionY <= 0) { throw new NotFoundException(); } const bits = new BitMatrix(dimensionX, dimensionY); const points = new Float32Array(2 * dimensionX); for (let y = 0; y < dimensionY; y++) { const max = points.length; const iValue = y + 0.5; for (let x = 0; x < max; x += 2) { points[x] = x / 2 + 0.5; points[x + 1] = iValue; } transform.transformPoints(points); GridSampler.checkAndNudgePoints(image, points); try { for (let x = 0; x < max; x += 2) { if (image.get(Math.floor(points[x]), Math.floor(points[x + 1]))) { bits.set(x / 2, y); } } } catch (aioobe) { throw new NotFoundException(); } } return bits; } } class GridSamplerInstance { /** * Sets the implementation of GridSampler used by the library. One global * instance is stored, which may sound problematic. But, the implementation provided * ought to be appropriate for the entire platform, and all uses of this library * in the whole lifetime of the JVM. For instance, an Android activity can swap in * an implementation that takes advantage of native platform libraries. * * @param newGridSampler The platform-specific object to install. */ static setGridSampler(newGridSampler) { GridSamplerInstance.gridSampler = newGridSampler; } /** * @return the current implementation of GridSampler */ static getInstance() { return GridSamplerInstance.gridSampler; } } GridSamplerInstance.gridSampler = new DefaultGridSampler(); class Point { constructor(x, y) { this.x = x; this.y = y; } toResultPoint() { return new ResultPoint(this.getX(), this.getY()); } getX() { return this.x; } getY() { return this.y; } } class Detector { constructor(image) { this.EXPECTED_CORNER_BITS = new Int32Array([ 3808, 476, 2107, 1799 ]); this.image = image; } detect() { return this.detectMirror(false); } /** * Detects an Aztec Code in an image. * * @param isMirror if true, image is a mirror-image of original * @return {@link AztecDetectorResult} encapsulating results of detecting an Aztec Code * @throws NotFoundException if no Aztec Code can be found */ detectMirror(isMirror) { let pCenter = this.getMatrixCenter(); let bullsEyeCorners = this.getBullsEyeCorners(pCenter); if (isMirror) { let temp = bullsEyeCorners[0]; bullsEyeCorners[0] = bullsEyeCorners[2]; bullsEyeCorners[2] = temp; } this.extractParameters(bullsEyeCorners); let bits = this.sampleGrid(this.image, bullsEyeCorners[this.shift % 4], bullsEyeCorners[(this.shift + 1) % 4], bullsEyeCorners[(this.shift + 2) % 4], bullsEyeCorners[(this.shift + 3) % 4]); let corners = this.getMatrixCornerPoints(bullsEyeCorners); return new AztecDetectorResult(bits, corners, this.compact, this.nbDataBlocks, this.nbLayers); } /** * Extracts the number of data layers and data blocks from the layer around the bull's eye. * * @param bullsEyeCorners the array of bull's eye corners * @throws NotFoundException in case of too many errors or invalid parameters */ extractParameters(bullsEyeCorners) { if (!this.isValidPoint(bullsEyeCorners[0]) || !this.isValidPoint(bullsEyeCorners[1]) || !this.isValidPoint(bullsEyeCorners[2]) || !this.isValidPoint(bullsEyeCorners[3])) { throw new NotFoundException(); } let length = 2 * this.nbCenterLayers; let sides = new Int32Array([ this.sampleLine(bullsEyeCorners[0], bullsEyeCorners[1], length), this.sampleLine(bullsEyeCorners[1], bullsEyeCorners[2], length), this.sampleLine(bullsEyeCorners[2], bullsEyeCorners[3], length), this.sampleLine(bullsEyeCorners[3], bullsEyeCorners[0], length) // Top ]); this.shift = this.getRotation(sides, length); let parameterData = 0; for (let i = 0; i < 4; i++) { let side = sides[(this.shift + i) % 4]; if (this.compact) { parameterData <<= 7; parameterData += side >> 1 & 127; } else { parameterData <<= 10; parameterData += (side >> 2 & 31 << 5) + (side >> 1 & 31); } } let correctedData = this.getCorrectedParameterData(parameterData, this.compact); if (this.compact) { this.nbLayers = (correctedData >> 6) + 1; this.nbDataBlocks = (correctedData & 63) + 1; } else { this.nbLayers = (correctedData >> 11) + 1; this.nbDataBlocks = (correctedData & 2047) + 1; } } getRotation(sides, length) { let cornerBits = 0; sides.forEach((side, idx, arr) => { let t = (side >> length - 2 << 1) + (side & 1); cornerBits = (cornerBits << 3) + t; }); cornerBits = ((cornerBits & 1) << 11) + (cornerBits >> 1); for (let shift = 0; shift < 4; shift++) { if (Integer.bitCount(cornerBits ^ this.EXPECTED_CORNER_BITS[shift]) <= 2) { return shift; } } throw new NotFoundException(); } /** * Corrects the parameter bits using Reed-Solomon algorithm. * * @param parameterData parameter bits * @param compact true if this is a compact Aztec code * @throws NotFoundException if the array contains too many errors */ getCorrectedParameterData(parameterData, compact) { let numCodewords; let numDataCodewords; if (compact) { numCodewords = 7; numDataCodewords = 2; } else { numCodewords = 10; numDataCodewords = 4; } let numECCodewords = numCodewords - numDataCodewords; let parameterWords = new Int32Array(numCodewords); for (let i = numCodewords - 1; i >= 0; --i) { parameterWords[i] = parameterData & 15; parameterData >>= 4; } try { let rsDecoder = new ReedSolomonDecoder(GenericGF.AZTEC_PARAM); rsDecoder.decode(parameterWords, numECCodewords); } catch (ignored) { throw new NotFoundException(); } let result = 0; for (let i = 0; i < numDataCodewords; i++) { result = (result << 4) + parameterWords[i]; } return result; } /** * Finds the corners of a bull-eye centered on the passed point. * This returns the centers of the diagonal points just outside the bull's eye * Returns [topRight, bottomRight, bottomLeft, topLeft] * * @param pCenter Center point * @return The corners of the bull-eye * @throws NotFoundException If no valid bull-eye can be found */ getBullsEyeCorners(pCenter) { let pina = pCenter; let pinb = pCenter; let pinc = pCenter; let pind = pCenter; let color = true; for (this.nbCenterLayers = 1; this.nbCenterLayers < 9; this.nbCenterLayers++) { let pouta = this.getFirstDifferent(pina, color, 1, -1); let poutb = this.getFirstDifferent(pinb, color, 1, 1); let poutc = this.getFirstDifferent(pinc, color, -1, 1); let poutd = this.getFirstDifferent(pind, color, -1, -1); if (this.nbCenterLayers > 2) { let q = this.distancePoint(poutd, pouta) * this.nbCenterLayers / (this.distancePoint(pind, pina) * (this.nbCenterLayers + 2)); if (q < 0.75 || q > 1.25 || !this.isWhiteOrBlackRectangle(pouta, poutb, poutc, poutd)) { break; } } pina = pouta; pinb = poutb; pinc = poutc; pind = poutd; color = !color; } if (this.nbCenterLayers !== 5 && this.nbCenterLayers !== 7) { throw new NotFoundException(); } this.compact = this.nbCenterLayers === 5; let pinax = new ResultPoint(pina.getX() + 0.5, pina.getY() - 0.5); let pinbx = new ResultPoint(pinb.getX() + 0.5, pinb.getY() + 0.5); let pincx = new ResultPoint(pinc.getX() - 0.5, pinc.getY() + 0.5); let pindx = new ResultPoint(pind.getX() - 0.5, pind.getY() - 0.5); return this.expandSquare([pinax, pinbx, pincx, pindx], 2 * this.nbCenterLayers - 3, 2 * this.nbCenterLayers); } /** * Finds a candidate center point of an Aztec code from an image * * @return the center point */ getMatrixCenter() { let pointA; let pointB; let pointC; let pointD; try { let cornerPoints = new WhiteRectangleDetector(this.image).detect(); pointA = cornerPoints[0]; pointB = cornerPoints[1]; pointC = cornerPoints[2]; pointD = cornerPoints[3]; } catch (e) { let cx2 = this.image.getWidth() / 2; let cy2 = this.image.getHeight() / 2; pointA = this.getFirstDifferent(new Point(cx2 + 7, cy2 - 7), false, 1, -1).toResultPoint(); pointB = this.getFirstDifferent(new Point(cx2 + 7, cy2 + 7), false, 1, 1).toResultPoint(); pointC = this.getFirstDifferent(new Point(cx2 - 7, cy2 + 7), false, -1, 1).toResultPoint(); pointD = this.getFirstDifferent(new Point(cx2 - 7, cy2 - 7), false, -1, -1).toResultPoint(); } let cx = MathUtils.round((pointA.getX() + pointD.getX() + pointB.getX() + pointC.getX()) / 4); let cy = MathUtils.round((pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY()) / 4); try { let cornerPoints = new WhiteRectangleDetector(this.image, 15, cx, cy).detect(); pointA = cornerPoints[0]; pointB = cornerPoints[1]; pointC = cornerPoints[2]; pointD = cornerPoints[3]; } catch (e) { pointA = this.getFirstDifferent(new Point(cx + 7, cy - 7), false, 1, -1).toResultPoint(); pointB = this.getFirstDifferent(new Point(cx + 7, cy + 7), false, 1, 1).toResultPoint(); pointC = this.getFirstDifferent(new Point(cx - 7, cy + 7), false, -1, 1).toResultPoint(); pointD = this.getFirstDifferent(new Point(cx - 7, cy - 7), false, -1, -1).toResultPoint(); } cx = MathUtils.round((pointA.getX() + pointD.getX() + pointB.getX() + pointC.getX()) / 4); cy = MathUtils.round((pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY()) / 4); return new Point(cx, cy); } /** * Gets the Aztec code corners from the bull's eye corners and the parameters. * * @param bullsEyeCorners the array of bull's eye corners * @return the array of aztec code corners */ getMatrixCornerPoints(bullsEyeCorners) { return this.expandSquare(bullsEyeCorners, 2 * this.nbCenterLayers, this.getDimension()); } /** * Creates a BitMatrix by sampling the provided image. * topLeft, topRight, bottomRight, and bottomLeft are the centers of the squares on the * diagonal just outside the bull's eye. */ sampleGrid(image, topLeft, topRight, bottomRight, bottomLeft) { let sampler = GridSamplerInstance.getInstance(); let dimension = this.getDimension(); let low = dimension / 2 - this.nbCenterLayers; let high = dimension / 2 + this.nbCenterLayers; return sampler.sampleGrid( image, dimension, dimension, low, low, // topleft high, low, // topright high, high, // bottomright low, high, // bottomleft topLeft.getX(), topLeft.getY(), topRight.getX(), topRight.getY(), bottomRight.getX(), bottomRight.getY(), bottomLeft.getX(), bottomLeft.getY() ); } /** * Samples a line. * * @param p1 start point (inclusive) * @param p2 end point (exclusive) * @param size number of bits * @return the array of bits as an int (first bit is high-order bit of result) */ sampleLine(p1, p2, size) { let result = 0; let d = this.distanceResultPoint(p1, p2); let moduleSize = d / size; let px = p1.getX(); let py = p1.getY(); let dx = moduleSize * (p2.getX() - p1.getX()) / d; let dy = moduleSize * (p2.getY() - p1.getY()) / d; for (let i = 0; i < size; i++) { if (this.image.get(MathUtils.round(px + i * dx), MathUtils.round(py + i * dy))) { result |= 1 << size - i - 1; } } return result; } /** * @return true if the border of the rectangle passed in parameter is compound of white points only * or black points only */ isWhiteOrBlackRectangle(p1, p2, p3, p4) { let corr = 3; p1 = new Point(p1.getX() - corr, p1.getY() + corr); p2 = new Point(p2.getX() - corr, p2.getY() - corr); p3 = new Point(p3.getX() + corr, p3.getY() - corr); p4 = new Point(p4.getX() + corr, p4.getY() + corr); let cInit = this.getColor(p4, p1); if (cInit === 0) { return false; } let c = this.getColor(p1, p2); if (c !== cInit) { return false; } c = this.getColor(p2, p3); if (c !== cInit) { return false; } c = this.getColor(p3, p4); return c === cInit; } /** * Gets the color of a segment * * @return 1 if segment more than 90% black, -1 if segment is more than 90% white, 0 else */ getColor(p1, p2) { let d = this.distancePoint(p1, p2); let dx = (p2.getX() - p1.getX()) / d; let dy = (p2.getY() - p1.getY()) / d; let error = 0; let px = p1.getX(); let py = p1.getY(); let colorModel = this.image.get(p1.getX(), p1.getY()); let iMax = Math.ceil(d); for (let i = 0; i < iMax; i++) { px += dx; py += dy; if (this.image.get(MathUtils.round(px), MathUtils.round(py)) !== colorModel) { error++; } } let errRatio = error / d; if (errRatio > 0.1 && errRatio < 0.9) { return 0; } return errRatio <= 0.1 === colorModel ? 1 : -1; } /** * Gets the coordinate of the first point with a different color in the given direction */ getFirstDifferent(init, color, dx, dy) { let x = init.getX() + dx; let y = init.getY() + dy; while (this.isValid(x, y) && this.image.get(x, y) === color) { x += dx; y += dy; } x -= dx; y -= dy; while (this.isValid(x, y) && this.image.get(x, y) === color) { x += dx; } x -= dx; while (this.isValid(x, y) && this.image.get(x, y) === color) { y += dy; } y -= dy; return new Point(x, y); } /** * Expand the square represented by the corner points by pushing out equally in all directions * * @param cornerPoints the corners of the square, which has the bull's eye at its center * @param oldSide the original length of the side of the square in the target bit matrix * @param newSide the new length of the size of the square in the target bit matrix * @return the corners of the expanded square */ expandSquare(cornerPoints, oldSide, newSide) { let ratio = newSide / (2 * oldSide); let dx = cornerPoints[0].getX() - cornerPoints[2].getX(); let dy = cornerPoints[0].getY() - cornerPoints[2].getY(); let centerx = (cornerPoints[0].getX() + cornerPoints[2].getX()) / 2; let centery = (cornerPoints[0].getY() + cornerPoints[2].getY()) / 2; let result0 = new ResultPoint(centerx + ratio * dx, centery + ratio * dy); let result2 = new ResultPoint(centerx - ratio * dx, centery - ratio * dy); dx = cornerPoints[1].getX() - cornerPoints[3].getX(); dy = cornerPoints[1].getY() - cornerPoints[3].getY(); centerx = (cornerPoints[1].getX() + cornerPoints[3].getX()) / 2; centery = (cornerPoints[1].getY() + cornerPoints[3].getY()) / 2; let result1 = new ResultPoint(centerx + ratio * dx, centery + ratio * dy); let result3 = new ResultPoint(centerx - ratio * dx, centery - ratio * dy); let results = [result0, result1, result2, result3]; return results; } isValid(x, y) { return x >= 0 && x < this.image.getWidth() && y > 0 && y < this.image.getHeight(); } isValidPoint(point) { let x = MathUtils.round(point.getX()); let y = MathUtils.round(point.getY()); return this.isValid(x, y); } distancePoint(a, b) { return MathUtils.distance(a.getX(), a.getY(), b.getX(), b.getY()); } distanceResultPoint(a, b) { return MathUtils.distance(a.getX(), a.getY(), b.getX(), b.getY()); } getDimension() { if (this.compact) { return 4 * this.nbLayers + 11; } if (this.nbLayers <= 4) { return 4 * this.nbLayers + 15; } return 4 * this.nbLayers + 2 * (Integer.truncDivision(this.nbLayers - 4, 8) + 1) + 15; } } class AztecReader { /** * Locates and decodes a Data Matrix code in an image. * * @return a String representing the content encoded by the Data Matrix code * @throws NotFoundException if a Data Matrix code cannot be found * @throws FormatException if a Data Matrix code cannot be decoded */ decode(image, hints = null) { let exception = null; let detector = new Detector(image.getBlackMatrix()); let points = null; let decoderResult = null; try { let detectorResult = detector.detectMirror(false); points = detectorResult.getPoints(); this.reportFoundResultPoints(hints, points); decoderResult = new Decoder().decode(detectorResult); } catch (e) { exception = e; } if (decoderResult == null) { try { let detectorResult = detector.detectMirror(true); points = detectorResult.getPoints(); this.reportFoundResultPoints(hints, points); decoderResult = new Decoder().decode(detectorResult); } catch (e) { if (exception != null) { throw exception; } throw e; } } let result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), decoderResult.getNumBits(), points, BarcodeFormat$1.AZTEC, System.currentTimeMillis()); let byteSegments = decoderResult.getByteSegments(); if (byteSegments != null) { result.putMetadata(ResultMetadataType$1.BYTE_SEGMENTS, byteSegments); } let ecLevel = decoderResult.getECLevel(); if (ecLevel != null) { result.putMetadata(ResultMetadataType$1.ERROR_CORRECTION_LEVEL, ecLevel); } return result; } reportFoundResultPoints(hints, points) { if (hints != null) { let rpcb = hints.get(DecodeHintType$1.NEED_RESULT_POINT_CALLBACK); if (rpcb != null) { points.forEach((point, idx, arr) => { rpcb.foundPossibleResultPoint(point); }); } } } // @Override reset() { } } class BrowserAztecCodeReader extends BrowserCodeReader { /** * Creates an instance of BrowserAztecCodeReader. * @param {number} [timeBetweenScansMillis=500] the time delay between subsequent decode tries * * @memberOf BrowserAztecCodeReader */ constructor(timeBetweenScansMillis = 500) { super(new AztecReader(), timeBetweenScansMillis); } } class OneDReader { /* @Override public Result decode(BinaryBitmap image) throws NotFoundException, FormatException { return decode(image, null); } */ // Note that we don't try rotation without the try harder flag, even if rotation was supported. // @Override decode(image, hints) { try { return this.doDecode(image, hints); } catch (nfe) { const tryHarder = hints && hints.get(DecodeHintType$1.TRY_HARDER) === true; if (tryHarder && image.isRotateSupported()) { const rotatedImage = image.rotateCounterClockwise(); const result = this.doDecode(rotatedImage, hints); const metadata = result.getResultMetadata(); let orientation = 270; if (metadata !== null && metadata.get(ResultMetadataType$1.ORIENTATION) === true) { orientation = orientation + metadata.get(ResultMetadataType$1.ORIENTATION) % 360; } result.putMetadata(ResultMetadataType$1.ORIENTATION, orientation); const points = result.getResultPoints(); if (points !== null) { const height = rotatedImage.getHeight(); for (let i = 0; i < points.length; i++) { points[i] = new ResultPoint(height - points[i].getY() - 1, points[i].getX()); } } return result; } else { throw new NotFoundException(); } } } // @Override reset() { } /** * We're going to examine rows from the middle outward, searching alternately above and below the * middle, and farther out each time. rowStep is the number of rows between each successive * attempt above and below the middle. So we'd scan row middle, then middle - rowStep, then * middle + rowStep, then middle - (2 * rowStep), etc. * rowStep is bigger as the image is taller, but is always at least 1. We've somewhat arbitrarily * decided that moving up and down by about 1/16 of the image is pretty good; we try more of the * image if "trying harder". * * @param image The image to decode * @param hints Any hints that were requested * @return The contents of the decoded barcode * @throws NotFoundException Any spontaneous errors which occur */ doDecode(image, hints) { const width = image.getWidth(); const height = image.getHeight(); let row = new BitArray(width); const tryHarder = hints && hints.get(DecodeHintType$1.TRY_HARDER) === true; const rowStep = Math.max(1, height >> (tryHarder ? 8 : 5)); let maxLines; if (tryHarder) { maxLines = height; } else { maxLines = 15; } const middle = Math.trunc(height / 2); for (let x = 0; x < maxLines; x++) { const rowStepsAboveOrBelow = Math.trunc((x + 1) / 2); const isAbove = (x & 1) === 0; const rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow); if (rowNumber < 0 || rowNumber >= height) { break; } try { row = image.getBlackRow(rowNumber, row); } catch (ignored) { continue; } for (let attempt = 0; attempt < 2; attempt++) { if (attempt === 1) { row.reverse(); if (hints && hints.get(DecodeHintType$1.NEED_RESULT_POINT_CALLBACK) === true) { const newHints = /* @__PURE__ */ new Map(); hints.forEach((hint, key) => newHints.set(key, hint)); newHints.delete(DecodeHintType$1.NEED_RESULT_POINT_CALLBACK); hints = newHints; } } try { const result = this.decodeRow(rowNumber, row, hints); if (attempt === 1) { result.putMetadata(ResultMetadataType$1.ORIENTATION, 180); const points = result.getResultPoints(); if (points !== null) { points[0] = new ResultPoint(width - points[0].getX() - 1, points[0].getY()); points[1] = new ResultPoint(width - points[1].getX() - 1, points[1].getY()); } } return result; } catch (re) { } } } throw new NotFoundException(); } /** * Records the size of successive runs of white and black pixels in a row, starting at a given point. * The values are recorded in the given array, and the number of runs recorded is equal to the size * of the array. If the row starts on a white pixel at the given start point, then the first count * recorded is the run of white pixels starting from that point; likewise it is the count of a run * of black pixels if the row begin on a black pixels at that point. * * @param row row to count from * @param start offset into row to start at * @param counters array into which to record counts * @throws NotFoundException if counters cannot be filled entirely from row before running out * of pixels */ static recordPattern(row, start, counters) { const numCounters = counters.length; for (let index = 0; index < numCounters; index++) counters[index] = 0; const end = row.getSize(); if (start >= end) { throw new NotFoundException(); } let isWhite = !row.get(start); let counterPosition = 0; let i = start; while (i < end) { if (row.get(i) !== isWhite) { counters[counterPosition]++; } else { if (++counterPosition === numCounters) { break; } else { counters[counterPosition] = 1; isWhite = !isWhite; } } i++; } if (!(counterPosition === numCounters || counterPosition === numCounters - 1 && i === end)) { throw new NotFoundException(); } } static recordPatternInReverse(row, start, counters) { let numTransitionsLeft = counters.length; let last = row.get(start); while (start > 0 && numTransitionsLeft >= 0) { if (row.get(--start) !== last) { numTransitionsLeft--; last = !last; } } if (numTransitionsLeft >= 0) { throw new NotFoundException(); } OneDReader.recordPattern(row, start + 1, counters); } /** * Determines how closely a set of observed counts of runs of black/white values matches a given * target pattern. This is reported as the ratio of the total variance from the expected pattern * proportions across all pattern elements, to the length of the pattern. * * @param counters observed counters * @param pattern expected pattern * @param maxIndividualVariance The most any counter can differ before we give up * @return ratio of total variance between counters and pattern compared to total pattern size */ static patternMatchVariance(counters, pattern, maxIndividualVariance) { const numCounters = counters.length; let total = 0; let patternLength = 0; for (let i = 0; i < numCounters; i++) { total += counters[i]; patternLength += pattern[i]; } if (total < patternLength) { return Number.POSITIVE_INFINITY; } const unitBarWidth = total / patternLength; maxIndividualVariance *= unitBarWidth; let totalVariance = 0; for (let x = 0; x < numCounters; x++) { const counter = counters[x]; const scaledPattern = pattern[x] * unitBarWidth; const variance = counter > scaledPattern ? counter - scaledPattern : scaledPattern - counter; if (variance > maxIndividualVariance) { return Number.POSITIVE_INFINITY; } totalVariance += variance; } return totalVariance / total; } } class Code128Reader extends OneDReader { static findStartPattern(row) { const width = row.getSize(); const rowOffset = row.getNextSet(0); let counterPosition = 0; let counters = Int32Array.from([0, 0, 0, 0, 0, 0]); let patternStart = rowOffset; let isWhite = false; const patternLength = 6; for (let i = rowOffset; i < width; i++) { if (row.get(i) !== isWhite) { counters[counterPosition]++; } else { if (counterPosition === patternLength - 1) { let bestVariance = Code128Reader.MAX_AVG_VARIANCE; let bestMatch = -1; for (let startCode = Code128Reader.CODE_START_A; startCode <= Code128Reader.CODE_START_C; startCode++) { const variance = OneDReader.patternMatchVariance(counters, Code128Reader.CODE_PATTERNS[startCode], Code128Reader.MAX_INDIVIDUAL_VARIANCE); if (variance < bestVariance) { bestVariance = variance; bestMatch = startCode; } } if (bestMatch >= 0 && row.isRange(Math.max(0, patternStart - (i - patternStart) / 2), patternStart, false)) { return Int32Array.from([patternStart, i, bestMatch]); } patternStart += counters[0] + counters[1]; counters = counters.slice(2, counters.length - 1); counters[counterPosition - 1] = 0; counters[counterPosition] = 0; counterPosition--; } else { counterPosition++; } counters[counterPosition] = 1; isWhite = !isWhite; } } throw new NotFoundException(); } static decodeCode(row, counters, rowOffset) { OneDReader.recordPattern(row, rowOffset, counters); let bestVariance = Code128Reader.MAX_AVG_VARIANCE; let bestMatch = -1; for (let d = 0; d < Code128Reader.CODE_PATTERNS.length; d++) { const pattern = Code128Reader.CODE_PATTERNS[d]; const variance = this.patternMatchVariance(counters, pattern, Code128Reader.MAX_INDIVIDUAL_VARIANCE); if (variance < bestVariance) { bestVariance = variance; bestMatch = d; } } if (bestMatch >= 0) { return bestMatch; } else { throw new NotFoundException(); } } decodeRow(rowNumber, row, hints) { const convertFNC1 = hints && hints.get(DecodeHintType$1.ASSUME_GS1) === true; const startPatternInfo = Code128Reader.findStartPattern(row); const startCode = startPatternInfo[2]; let currentRawCodesIndex = 0; const rawCodes = new Uint8Array(20); rawCodes[currentRawCodesIndex++] = startCode; let codeSet; switch (startCode) { case Code128Reader.CODE_START_A: codeSet = Code128Reader.CODE_CODE_A; break; case Code128Reader.CODE_START_B: codeSet = Code128Reader.CODE_CODE_B; break; case Code128Reader.CODE_START_C: codeSet = Code128Reader.CODE_CODE_C; break; default: throw new FormatException(); } let done = false; let isNextShifted = false; let result = ""; let lastStart = startPatternInfo[0]; let nextStart = startPatternInfo[1]; const counters = Int32Array.from([0, 0, 0, 0, 0, 0]); let lastCode = 0; let code = 0; let checksumTotal = startCode; let multiplier = 0; let lastCharacterWasPrintable = true; let upperMode = false; let shiftUpperMode = false; while (!done) { const unshift = isNextShifted; isNextShifted = false; lastCode = code; code = Code128Reader.decodeCode(row, counters, nextStart); rawCodes[currentRawCodesIndex++] = code; if (code !== Code128Reader.CODE_STOP) { lastCharacterWasPrintable = true; } if (code !== Code128Reader.CODE_STOP) { multiplier++; checksumTotal += multiplier * code; } lastStart = nextStart; nextStart += counters.reduce((previous, current) => previous + current, 0); switch (code) { case Code128Reader.CODE_START_A: case Code128Reader.CODE_START_B: case Code128Reader.CODE_START_C: throw new FormatException(); } switch (codeSet) { case Code128Reader.CODE_CODE_A: if (code < 64) { if (shiftUpperMode === upperMode) { result += String.fromCharCode(" ".charCodeAt(0) + code); } else { result += String.fromCharCode(" ".charCodeAt(0) + code + 128); } shiftUpperMode = false; } else if (code < 96) { if (shiftUpperMode === upperMode) { result += String.fromCharCode(code - 64); } else { result += String.fromCharCode(code + 64); } shiftUpperMode = false; } else { if (code !== Code128Reader.CODE_STOP) { lastCharacterWasPrintable = false; } switch (code) { case Code128Reader.CODE_FNC_1: if (convertFNC1) { if (result.length === 0) { result += "]C1"; } else { result += String.fromCharCode(29); } } break; case Code128Reader.CODE_FNC_2: case Code128Reader.CODE_FNC_3: break; case Code128Reader.CODE_FNC_4_A: if (!upperMode && shiftUpperMode) { upperMode = true; shiftUpperMode = false; } else if (upperMode && shiftUpperMode) { upperMode = false; shiftUpperMode = false; } else { shiftUpperMode = true; } break; case Code128Reader.CODE_SHIFT: isNextShifted = true; codeSet = Code128Reader.CODE_CODE_B; break; case Code128Reader.CODE_CODE_B: codeSet = Code128Reader.CODE_CODE_B; break; case Code128Reader.CODE_CODE_C: codeSet = Code128Reader.CODE_CODE_C; break; case Code128Reader.CODE_STOP: done = true; break; } } break; case Code128Reader.CODE_CODE_B: if (code < 96) { if (shiftUpperMode === upperMode) { result += String.fromCharCode(" ".charCodeAt(0) + code); } else { result += String.fromCharCode(" ".charCodeAt(0) + code + 128); } shiftUpperMode = false; } else { if (code !== Code128Reader.CODE_STOP) { lastCharacterWasPrintable = false; } switch (code) { case Code128Reader.CODE_FNC_1: if (convertFNC1) { if (result.length === 0) { result += "]C1"; } else { result += String.fromCharCode(29); } } break; case Code128Reader.CODE_FNC_2: case Code128Reader.CODE_FNC_3: break; case Code128Reader.CODE_FNC_4_B: if (!upperMode && shiftUpperMode) { upperMode = true; shiftUpperMode = false; } else if (upperMode && shiftUpperMode) { upperMode = false; shiftUpperMode = false; } else { shiftUpperMode = true; } break; case Code128Reader.CODE_SHIFT: isNextShifted = true; codeSet = Code128Reader.CODE_CODE_A; break; case Code128Reader.CODE_CODE_A: codeSet = Code128Reader.CODE_CODE_A; break; case Code128Reader.CODE_CODE_C: codeSet = Code128Reader.CODE_CODE_C; break; case Code128Reader.CODE_STOP: done = true; break; } } break; case Code128Reader.CODE_CODE_C: if (code < 100) { if (code < 10) { result += "0"; } result += code; } else { if (code !== Code128Reader.CODE_STOP) { lastCharacterWasPrintable = false; } switch (code) { case Code128Reader.CODE_FNC_1: if (convertFNC1) { if (result.length === 0) { result += "]C1"; } else { result += String.fromCharCode(29); } } break; case Code128Reader.CODE_CODE_A: codeSet = Code128Reader.CODE_CODE_A; break; case Code128Reader.CODE_CODE_B: codeSet = Code128Reader.CODE_CODE_B; break; case Code128Reader.CODE_STOP: done = true; break; } } break; } if (unshift) { codeSet = codeSet === Code128Reader.CODE_CODE_A ? Code128Reader.CODE_CODE_B : Code128Reader.CODE_CODE_A; } } const lastPatternSize = nextStart - lastStart; nextStart = row.getNextUnset(nextStart); if (!row.isRange(nextStart, Math.min(row.getSize(), nextStart + (nextStart - lastStart) / 2), false)) { throw new NotFoundException(); } checksumTotal -= multiplier * lastCode; if (checksumTotal % 103 !== lastCode) { throw new ChecksumException(); } const resultLength = result.length; if (resultLength === 0) { throw new NotFoundException(); } if (resultLength > 0 && lastCharacterWasPrintable) { if (codeSet === Code128Reader.CODE_CODE_C) { result = result.substring(0, resultLength - 2); } else { result = result.substring(0, resultLength - 1); } } const left = (startPatternInfo[1] + startPatternInfo[0]) / 2; const right = lastStart + lastPatternSize / 2; const rawCodesSize = rawCodes.length; const rawBytes = new Uint8Array(rawCodesSize); for (let i = 0; i < rawCodesSize; i++) { rawBytes[i] = rawCodes[i]; } const points = [new ResultPoint(left, rowNumber), new ResultPoint(right, rowNumber)]; return new Result(result, rawBytes, 0, points, BarcodeFormat$1.CODE_128, (/* @__PURE__ */ new Date()).getTime()); } } Code128Reader.CODE_PATTERNS = [ Int32Array.from([2, 1, 2, 2, 2, 2]), Int32Array.from([2, 2, 2, 1, 2, 2]), Int32Array.from([2, 2, 2, 2, 2, 1]), Int32Array.from([1, 2, 1, 2, 2, 3]), Int32Array.from([1, 2, 1, 3, 2, 2]), Int32Array.from([1, 3, 1, 2, 2, 2]), Int32Array.from([1, 2, 2, 2, 1, 3]), Int32Array.from([1, 2, 2, 3, 1, 2]), Int32Array.from([1, 3, 2, 2, 1, 2]), Int32Array.from([2, 2, 1, 2, 1, 3]), Int32Array.from([2, 2, 1, 3, 1, 2]), Int32Array.from([2, 3, 1, 2, 1, 2]), Int32Array.from([1, 1, 2, 2, 3, 2]), Int32Array.from([1, 2, 2, 1, 3, 2]), Int32Array.from([1, 2, 2, 2, 3, 1]), Int32Array.from([1, 1, 3, 2, 2, 2]), Int32Array.from([1, 2, 3, 1, 2, 2]), Int32Array.from([1, 2, 3, 2, 2, 1]), Int32Array.from([2, 2, 3, 2, 1, 1]), Int32Array.from([2, 2, 1, 1, 3, 2]), Int32Array.from([2, 2, 1, 2, 3, 1]), Int32Array.from([2, 1, 3, 2, 1, 2]), Int32Array.from([2, 2, 3, 1, 1, 2]), Int32Array.from([3, 1, 2, 1, 3, 1]), Int32Array.from([3, 1, 1, 2, 2, 2]), Int32Array.from([3, 2, 1, 1, 2, 2]), Int32Array.from([3, 2, 1, 2, 2, 1]), Int32Array.from([3, 1, 2, 2, 1, 2]), Int32Array.from([3, 2, 2, 1, 1, 2]), Int32Array.from([3, 2, 2, 2, 1, 1]), Int32Array.from([2, 1, 2, 1, 2, 3]), Int32Array.from([2, 1, 2, 3, 2, 1]), Int32Array.from([2, 3, 2, 1, 2, 1]), Int32Array.from([1, 1, 1, 3, 2, 3]), Int32Array.from([1, 3, 1, 1, 2, 3]), Int32Array.from([1, 3, 1, 3, 2, 1]), Int32Array.from([1, 1, 2, 3, 1, 3]), Int32Array.from([1, 3, 2, 1, 1, 3]), Int32Array.from([1, 3, 2, 3, 1, 1]), Int32Array.from([2, 1, 1, 3, 1, 3]), Int32Array.from([2, 3, 1, 1, 1, 3]), Int32Array.from([2, 3, 1, 3, 1, 1]), Int32Array.from([1, 1, 2, 1, 3, 3]), Int32Array.from([1, 1, 2, 3, 3, 1]), Int32Array.from([1, 3, 2, 1, 3, 1]), Int32Array.from([1, 1, 3, 1, 2, 3]), Int32Array.from([1, 1, 3, 3, 2, 1]), Int32Array.from([1, 3, 3, 1, 2, 1]), Int32Array.from([3, 1, 3, 1, 2, 1]), Int32Array.from([2, 1, 1, 3, 3, 1]), Int32Array.from([2, 3, 1, 1, 3, 1]), Int32Array.from([2, 1, 3, 1, 1, 3]), Int32Array.from([2, 1, 3, 3, 1, 1]), Int32Array.from([2, 1, 3, 1, 3, 1]), Int32Array.from([3, 1, 1, 1, 2, 3]), Int32Array.from([3, 1, 1, 3, 2, 1]), Int32Array.from([3, 3, 1, 1, 2, 1]), Int32Array.from([3, 1, 2, 1, 1, 3]), Int32Array.from([3, 1, 2, 3, 1, 1]), Int32Array.from([3, 3, 2, 1, 1, 1]), Int32Array.from([3, 1, 4, 1, 1, 1]), Int32Array.from([2, 2, 1, 4, 1, 1]), Int32Array.from([4, 3, 1, 1, 1, 1]), Int32Array.from([1, 1, 1, 2, 2, 4]), Int32Array.from([1, 1, 1, 4, 2, 2]), Int32Array.from([1, 2, 1, 1, 2, 4]), Int32Array.from([1, 2, 1, 4, 2, 1]), Int32Array.from([1, 4, 1, 1, 2, 2]), Int32Array.from([1, 4, 1, 2, 2, 1]), Int32Array.from([1, 1, 2, 2, 1, 4]), Int32Array.from([1, 1, 2, 4, 1, 2]), Int32Array.from([1, 2, 2, 1, 1, 4]), Int32Array.from([1, 2, 2, 4, 1, 1]), Int32Array.from([1, 4, 2, 1, 1, 2]), Int32Array.from([1, 4, 2, 2, 1, 1]), Int32Array.from([2, 4, 1, 2, 1, 1]), Int32Array.from([2, 2, 1, 1, 1, 4]), Int32Array.from([4, 1, 3, 1, 1, 1]), Int32Array.from([2, 4, 1, 1, 1, 2]), Int32Array.from([1, 3, 4, 1, 1, 1]), Int32Array.from([1, 1, 1, 2, 4, 2]), Int32Array.from([1, 2, 1, 1, 4, 2]), Int32Array.from([1, 2, 1, 2, 4, 1]), Int32Array.from([1, 1, 4, 2, 1, 2]), Int32Array.from([1, 2, 4, 1, 1, 2]), Int32Array.from([1, 2, 4, 2, 1, 1]), Int32Array.from([4, 1, 1, 2, 1, 2]), Int32Array.from([4, 2, 1, 1, 1, 2]), Int32Array.from([4, 2, 1, 2, 1, 1]), Int32Array.from([2, 1, 2, 1, 4, 1]), Int32Array.from([2, 1, 4, 1, 2, 1]), Int32Array.from([4, 1, 2, 1, 2, 1]), Int32Array.from([1, 1, 1, 1, 4, 3]), Int32Array.from([1, 1, 1, 3, 4, 1]), Int32Array.from([1, 3, 1, 1, 4, 1]), Int32Array.from([1, 1, 4, 1, 1, 3]), Int32Array.from([1, 1, 4, 3, 1, 1]), Int32Array.from([4, 1, 1, 1, 1, 3]), Int32Array.from([4, 1, 1, 3, 1, 1]), Int32Array.from([1, 1, 3, 1, 4, 1]), Int32Array.from([1, 1, 4, 1, 3, 1]), Int32Array.from([3, 1, 1, 1, 4, 1]), Int32Array.from([4, 1, 1, 1, 3, 1]), Int32Array.from([2, 1, 1, 4, 1, 2]), Int32Array.from([2, 1, 1, 2, 1, 4]), Int32Array.from([2, 1, 1, 2, 3, 2]), Int32Array.from([2, 3, 3, 1, 1, 1, 2]) ]; Code128Reader.MAX_AVG_VARIANCE = 0.25; Code128Reader.MAX_INDIVIDUAL_VARIANCE = 0.7; Code128Reader.CODE_SHIFT = 98; Code128Reader.CODE_CODE_C = 99; Code128Reader.CODE_CODE_B = 100; Code128Reader.CODE_CODE_A = 101; Code128Reader.CODE_FNC_1 = 102; Code128Reader.CODE_FNC_2 = 97; Code128Reader.CODE_FNC_3 = 96; Code128Reader.CODE_FNC_4_A = 101; Code128Reader.CODE_FNC_4_B = 100; Code128Reader.CODE_START_A = 103; Code128Reader.CODE_START_B = 104; Code128Reader.CODE_START_C = 105; Code128Reader.CODE_STOP = 106; class Code39Reader extends OneDReader { /** * Creates a reader that assumes all encoded data is data, and does not treat the final * character as a check digit. It will not decoded "extended Code 39" sequences. */ // public Code39Reader() { // this(false); // } /** * Creates a reader that can be configured to check the last character as a check digit. * It will not decoded "extended Code 39" sequences. * * @param usingCheckDigit if true, treat the last data character as a check digit, not * data, and verify that the checksum passes. */ // public Code39Reader(boolean usingCheckDigit) { // this(usingCheckDigit, false); // } /** * Creates a reader that can be configured to check the last character as a check digit, * or optionally attempt to decode "extended Code 39" sequences that are used to encode * the full ASCII character set. * * @param usingCheckDigit if true, treat the last data character as a check digit, not * data, and verify that the checksum passes. * @param extendedMode if true, will attempt to decode extended Code 39 sequences in the * text. */ constructor(usingCheckDigit = false, extendedMode = false) { super(); this.usingCheckDigit = usingCheckDigit; this.extendedMode = extendedMode; this.decodeRowResult = ""; this.counters = new Int32Array(9); } decodeRow(rowNumber, row, hints) { let theCounters = this.counters; theCounters.fill(0); this.decodeRowResult = ""; let start = Code39Reader.findAsteriskPattern(row, theCounters); let nextStart = row.getNextSet(start[1]); let end = row.getSize(); let decodedChar; let lastStart; do { Code39Reader.recordPattern(row, nextStart, theCounters); let pattern = Code39Reader.toNarrowWidePattern(theCounters); if (pattern < 0) { throw new NotFoundException(); } decodedChar = Code39Reader.patternToChar(pattern); this.decodeRowResult += decodedChar; lastStart = nextStart; for (let counter of theCounters) { nextStart += counter; } nextStart = row.getNextSet(nextStart); } while (decodedChar !== "*"); this.decodeRowResult = this.decodeRowResult.substring(0, this.decodeRowResult.length - 1); let lastPatternSize = 0; for (let counter of theCounters) { lastPatternSize += counter; } let whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize; if (nextStart !== end && whiteSpaceAfterEnd * 2 < lastPatternSize) { throw new NotFoundException(); } if (this.usingCheckDigit) { let max = this.decodeRowResult.length - 1; let total = 0; for (let i = 0; i < max; i++) { total += Code39Reader.ALPHABET_STRING.indexOf(this.decodeRowResult.charAt(i)); } if (this.decodeRowResult.charAt(max) !== Code39Reader.ALPHABET_STRING.charAt(total % 43)) { throw new ChecksumException(); } this.decodeRowResult = this.decodeRowResult.substring(0, max); } if (this.decodeRowResult.length === 0) { throw new NotFoundException(); } let resultString; if (this.extendedMode) { resultString = Code39Reader.decodeExtended(this.decodeRowResult); } else { resultString = this.decodeRowResult; } let left = (start[1] + start[0]) / 2; let right = lastStart + lastPatternSize / 2; return new Result(resultString, null, 0, [new ResultPoint(left, rowNumber), new ResultPoint(right, rowNumber)], BarcodeFormat$1.CODE_39, (/* @__PURE__ */ new Date()).getTime()); } static findAsteriskPattern(row, counters) { let width = row.getSize(); let rowOffset = row.getNextSet(0); let counterPosition = 0; let patternStart = rowOffset; let isWhite = false; let patternLength = counters.length; for (let i = rowOffset; i < width; i++) { if (row.get(i) !== isWhite) { counters[counterPosition]++; } else { if (counterPosition === patternLength - 1) { if (this.toNarrowWidePattern(counters) === Code39Reader.ASTERISK_ENCODING && row.isRange(Math.max(0, patternStart - Math.floor((i - patternStart) / 2)), patternStart, false)) { return [patternStart, i]; } patternStart += counters[0] + counters[1]; counters.copyWithin(0, 2, 2 + counterPosition - 1); counters[counterPosition - 1] = 0; counters[counterPosition] = 0; counterPosition--; } else { counterPosition++; } counters[counterPosition] = 1; isWhite = !isWhite; } } throw new NotFoundException(); } // For efficiency, returns -1 on failure. Not throwing here saved as many as 700 exceptions // per image when using some of our blackbox images. static toNarrowWidePattern(counters) { let numCounters = counters.length; let maxNarrowCounter = 0; let wideCounters; do { let minCounter = 2147483647; for (let counter of counters) { if (counter < minCounter && counter > maxNarrowCounter) { minCounter = counter; } } maxNarrowCounter = minCounter; wideCounters = 0; let totalWideCountersWidth = 0; let pattern = 0; for (let i = 0; i < numCounters; i++) { let counter = counters[i]; if (counter > maxNarrowCounter) { pattern |= 1 << numCounters - 1 - i; wideCounters++; totalWideCountersWidth += counter; } } if (wideCounters === 3) { for (let i = 0; i < numCounters && wideCounters > 0; i++) { let counter = counters[i]; if (counter > maxNarrowCounter) { wideCounters--; if (counter * 2 >= totalWideCountersWidth) { return -1; } } } return pattern; } } while (wideCounters > 3); return -1; } static patternToChar(pattern) { for (let i = 0; i < Code39Reader.CHARACTER_ENCODINGS.length; i++) { if (Code39Reader.CHARACTER_ENCODINGS[i] === pattern) { return Code39Reader.ALPHABET_STRING.charAt(i); } } if (pattern === Code39Reader.ASTERISK_ENCODING) { return "*"; } throw new NotFoundException(); } static decodeExtended(encoded) { let length = encoded.length; let decoded = ""; for (let i = 0; i < length; i++) { let c = encoded.charAt(i); if (c === "+" || c === "$" || c === "%" || c === "/") { let next = encoded.charAt(i + 1); let decodedChar = "\0"; switch (c) { case "+": if (next >= "A" && next <= "Z") { decodedChar = String.fromCharCode(next.charCodeAt(0) + 32); } else { throw new FormatException(); } break; case "$": if (next >= "A" && next <= "Z") { decodedChar = String.fromCharCode(next.charCodeAt(0) - 64); } else { throw new FormatException(); } break; case "%": if (next >= "A" && next <= "E") { decodedChar = String.fromCharCode(next.charCodeAt(0) - 38); } else if (next >= "F" && next <= "J") { decodedChar = String.fromCharCode(next.charCodeAt(0) - 11); } else if (next >= "K" && next <= "O") { decodedChar = String.fromCharCode(next.charCodeAt(0) + 16); } else if (next >= "P" && next <= "T") { decodedChar = String.fromCharCode(next.charCodeAt(0) + 43); } else if (next === "U") { decodedChar = "\0"; } else if (next === "V") { decodedChar = "@"; } else if (next === "W") { decodedChar = "`"; } else if (next === "X" || next === "Y" || next === "Z") { decodedChar = "\x7F"; } else { throw new FormatException(); } break; case "/": if (next >= "A" && next <= "O") { decodedChar = String.fromCharCode(next.charCodeAt(0) - 32); } else if (next === "Z") { decodedChar = ":"; } else { throw new FormatException(); } break; } decoded += decodedChar; i++; } else { decoded += c; } } return decoded; } } Code39Reader.ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"; Code39Reader.CHARACTER_ENCODINGS = [ 52, 289, 97, 352, 49, 304, 112, 37, 292, 100, 265, 73, 328, 25, 280, 88, 13, 268, 76, 28, 259, 67, 322, 19, 274, 82, 7, 262, 70, 22, 385, 193, 448, 145, 400, 208, 133, 388, 196, 168, 162, 138, 42 // /-% ]; Code39Reader.ASTERISK_ENCODING = 148; class ITFReader extends OneDReader { constructor() { super(...arguments); this.narrowLineWidth = -1; } // See ITFWriter.PATTERNS /* /!** * Patterns of Wide / Narrow lines to indicate each digit *!/ */ decodeRow(rowNumber, row, hints) { let startRange = this.decodeStart(row); let endRange = this.decodeEnd(row); let result = new StringBuilder(); ITFReader.decodeMiddle(row, startRange[1], endRange[0], result); let resultString = result.toString(); let allowedLengths = null; if (hints != null) { allowedLengths = hints.get(DecodeHintType$1.ALLOWED_LENGTHS); } if (allowedLengths == null) { allowedLengths = ITFReader.DEFAULT_ALLOWED_LENGTHS; } let length = resultString.length; let lengthOK = false; let maxAllowedLength = 0; for (let value of allowedLengths) { if (length === value) { lengthOK = true; break; } if (value > maxAllowedLength) { maxAllowedLength = value; } } if (!lengthOK && length > maxAllowedLength) { lengthOK = true; } if (!lengthOK) { throw new FormatException(); } const points = [new ResultPoint(startRange[1], rowNumber), new ResultPoint(endRange[0], rowNumber)]; let resultReturn = new Result( resultString, null, // no natural byte representation for these barcodes 0, points, BarcodeFormat$1.ITF, (/* @__PURE__ */ new Date()).getTime() ); return resultReturn; } /* /!** * @param row row of black/white values to search * @param payloadStart offset of start pattern * @param resultString {@link StringBuilder} to append decoded chars to * @throws NotFoundException if decoding could not complete successfully *!/*/ static decodeMiddle(row, payloadStart, payloadEnd, resultString) { let counterDigitPair = new Int32Array(10); let counterBlack = new Int32Array(5); let counterWhite = new Int32Array(5); counterDigitPair.fill(0); counterBlack.fill(0); counterWhite.fill(0); while (payloadStart < payloadEnd) { OneDReader.recordPattern(row, payloadStart, counterDigitPair); for (let k = 0; k < 5; k++) { let twoK = 2 * k; counterBlack[k] = counterDigitPair[twoK]; counterWhite[k] = counterDigitPair[twoK + 1]; } let bestMatch = ITFReader.decodeDigit(counterBlack); resultString.append(bestMatch.toString()); bestMatch = this.decodeDigit(counterWhite); resultString.append(bestMatch.toString()); counterDigitPair.forEach(function(counterDigit) { payloadStart += counterDigit; }); } } /*/!** * Identify where the start of the middle / payload section starts. * * @param row row of black/white values to search * @return Array, containing index of start of 'start block' and end of * 'start block' *!/*/ decodeStart(row) { let endStart = ITFReader.skipWhiteSpace(row); let startPattern = ITFReader.findGuardPattern(row, endStart, ITFReader.START_PATTERN); this.narrowLineWidth = (startPattern[1] - startPattern[0]) / 4; this.validateQuietZone(row, startPattern[0]); return startPattern; } /*/!** * The start & end patterns must be pre/post fixed by a quiet zone. This * zone must be at least 10 times the width of a narrow line. Scan back until * we either get to the start of the barcode or match the necessary number of * quiet zone pixels. * * Note: Its assumed the row is reversed when using this method to find * quiet zone after the end pattern. * * ref: http://www.barcode-1.net/i25code.html * * @param row bit array representing the scanned barcode. * @param startPattern index into row of the start or end pattern. * @throws NotFoundException if the quiet zone cannot be found *!/*/ validateQuietZone(row, startPattern) { let quietCount = this.narrowLineWidth * 10; quietCount = quietCount < startPattern ? quietCount : startPattern; for (let i = startPattern - 1; quietCount > 0 && i >= 0; i--) { if (row.get(i)) { break; } quietCount--; } if (quietCount !== 0) { throw new NotFoundException(); } } /* /!** * Skip all whitespace until we get to the first black line. * * @param row row of black/white values to search * @return index of the first black line. * @throws NotFoundException Throws exception if no black lines are found in the row *!/*/ static skipWhiteSpace(row) { const width = row.getSize(); const endStart = row.getNextSet(0); if (endStart === width) { throw new NotFoundException(); } return endStart; } /*/!** * Identify where the end of the middle / payload section ends. * * @param row row of black/white values to search * @return Array, containing index of start of 'end block' and end of 'end * block' *!/*/ decodeEnd(row) { row.reverse(); try { let endStart = ITFReader.skipWhiteSpace(row); let endPattern; try { endPattern = ITFReader.findGuardPattern(row, endStart, ITFReader.END_PATTERN_REVERSED[0]); } catch (error) { if (error instanceof NotFoundException) { endPattern = ITFReader.findGuardPattern(row, endStart, ITFReader.END_PATTERN_REVERSED[1]); } } this.validateQuietZone(row, endPattern[0]); let temp = endPattern[0]; endPattern[0] = row.getSize() - endPattern[1]; endPattern[1] = row.getSize() - temp; return endPattern; } finally { row.reverse(); } } /* /!** * @param row row of black/white values to search * @param rowOffset position to start search * @param pattern pattern of counts of number of black and white pixels that are * being searched for as a pattern * @return start/end horizontal offset of guard pattern, as an array of two * ints * @throws NotFoundException if pattern is not found *!/*/ static findGuardPattern(row, rowOffset, pattern) { let patternLength = pattern.length; let counters = new Int32Array(patternLength); let width = row.getSize(); let isWhite = false; let counterPosition = 0; let patternStart = rowOffset; counters.fill(0); for (let x = rowOffset; x < width; x++) { if (row.get(x) !== isWhite) { counters[counterPosition]++; } else { if (counterPosition === patternLength - 1) { if (OneDReader.patternMatchVariance(counters, pattern, ITFReader.MAX_INDIVIDUAL_VARIANCE) < ITFReader.MAX_AVG_VARIANCE) { return [patternStart, x]; } patternStart += counters[0] + counters[1]; System.arraycopy(counters, 2, counters, 0, counterPosition - 1); counters[counterPosition - 1] = 0; counters[counterPosition] = 0; counterPosition--; } else { counterPosition++; } counters[counterPosition] = 1; isWhite = !isWhite; } } throw new NotFoundException(); } /*/!** * Attempts to decode a sequence of ITF black/white lines into single * digit. * * @param counters the counts of runs of observed black/white/black/... values * @return The decoded digit * @throws NotFoundException if digit cannot be decoded *!/*/ static decodeDigit(counters) { let bestVariance = ITFReader.MAX_AVG_VARIANCE; let bestMatch = -1; let max = ITFReader.PATTERNS.length; for (let i = 0; i < max; i++) { let pattern = ITFReader.PATTERNS[i]; let variance = OneDReader.patternMatchVariance(counters, pattern, ITFReader.MAX_INDIVIDUAL_VARIANCE); if (variance < bestVariance) { bestVariance = variance; bestMatch = i; } else if (variance === bestVariance) { bestMatch = -1; } } if (bestMatch >= 0) { return bestMatch % 10; } else { throw new NotFoundException(); } } } ITFReader.PATTERNS = [ Int32Array.from([1, 1, 2, 2, 1]), Int32Array.from([2, 1, 1, 1, 2]), Int32Array.from([1, 2, 1, 1, 2]), Int32Array.from([2, 2, 1, 1, 1]), Int32Array.from([1, 1, 2, 1, 2]), Int32Array.from([2, 1, 2, 1, 1]), Int32Array.from([1, 2, 2, 1, 1]), Int32Array.from([1, 1, 1, 2, 2]), Int32Array.from([2, 1, 1, 2, 1]), Int32Array.from([1, 2, 1, 2, 1]), Int32Array.from([1, 1, 3, 3, 1]), Int32Array.from([3, 1, 1, 1, 3]), Int32Array.from([1, 3, 1, 1, 3]), Int32Array.from([3, 3, 1, 1, 1]), Int32Array.from([1, 1, 3, 1, 3]), Int32Array.from([3, 1, 3, 1, 1]), Int32Array.from([1, 3, 3, 1, 1]), Int32Array.from([1, 1, 1, 3, 3]), Int32Array.from([3, 1, 1, 3, 1]), Int32Array.from([1, 3, 1, 3, 1]) // 9 ]; ITFReader.MAX_AVG_VARIANCE = 0.38; ITFReader.MAX_INDIVIDUAL_VARIANCE = 0.5; ITFReader.DEFAULT_ALLOWED_LENGTHS = [6, 8, 10, 12, 14]; ITFReader.START_PATTERN = Int32Array.from([1, 1, 1, 1]); ITFReader.END_PATTERN_REVERSED = [ Int32Array.from([1, 1, 2]), Int32Array.from([1, 1, 3]) // 3x ]; class AbstractUPCEANReader extends OneDReader { constructor() { super(...arguments); this.decodeRowStringBuffer = ""; } static findStartGuardPattern(row) { let foundStart = false; let startRange; let nextStart = 0; let counters = Int32Array.from([0, 0, 0]); while (!foundStart) { counters = Int32Array.from([0, 0, 0]); startRange = AbstractUPCEANReader.findGuardPattern(row, nextStart, false, this.START_END_PATTERN, counters); let start = startRange[0]; nextStart = startRange[1]; let quietStart = start - (nextStart - start); if (quietStart >= 0) { foundStart = row.isRange(quietStart, start, false); } } return startRange; } static checkChecksum(s) { return AbstractUPCEANReader.checkStandardUPCEANChecksum(s); } static checkStandardUPCEANChecksum(s) { let length = s.length; if (length === 0) return false; let check = parseInt(s.charAt(length - 1), 10); return AbstractUPCEANReader.getStandardUPCEANChecksum(s.substring(0, length - 1)) === check; } static getStandardUPCEANChecksum(s) { let length = s.length; let sum = 0; for (let i = length - 1; i >= 0; i -= 2) { let digit = s.charAt(i).charCodeAt(0) - "0".charCodeAt(0); if (digit < 0 || digit > 9) { throw new FormatException(); } sum += digit; } sum *= 3; for (let i = length - 2; i >= 0; i -= 2) { let digit = s.charAt(i).charCodeAt(0) - "0".charCodeAt(0); if (digit < 0 || digit > 9) { throw new FormatException(); } sum += digit; } return (1e3 - sum) % 10; } static decodeEnd(row, endStart) { return AbstractUPCEANReader.findGuardPattern(row, endStart, false, AbstractUPCEANReader.START_END_PATTERN, new Int32Array(AbstractUPCEANReader.START_END_PATTERN.length).fill(0)); } /** * @throws NotFoundException */ static findGuardPatternWithoutCounters(row, rowOffset, whiteFirst, pattern) { return this.findGuardPattern(row, rowOffset, whiteFirst, pattern, new Int32Array(pattern.length)); } /** * @param row row of black/white values to search * @param rowOffset position to start search * @param whiteFirst if true, indicates that the pattern specifies white/black/white/... * pixel counts, otherwise, it is interpreted as black/white/black/... * @param pattern pattern of counts of number of black and white pixels that are being * searched for as a pattern * @param counters array of counters, as long as pattern, to re-use * @return start/end horizontal offset of guard pattern, as an array of two ints * @throws NotFoundException if pattern is not found */ static findGuardPattern(row, rowOffset, whiteFirst, pattern, counters) { let width = row.getSize(); rowOffset = whiteFirst ? row.getNextUnset(rowOffset) : row.getNextSet(rowOffset); let counterPosition = 0; let patternStart = rowOffset; let patternLength = pattern.length; let isWhite = whiteFirst; for (let x = rowOffset; x < width; x++) { if (row.get(x) !== isWhite) { counters[counterPosition]++; } else { if (counterPosition === patternLength - 1) { if (OneDReader.patternMatchVariance(counters, pattern, AbstractUPCEANReader.MAX_INDIVIDUAL_VARIANCE) < AbstractUPCEANReader.MAX_AVG_VARIANCE) { return Int32Array.from([patternStart, x]); } patternStart += counters[0] + counters[1]; let slice = counters.slice(2, counters.length - 1); for (let i = 0; i < counterPosition - 1; i++) { counters[i] = slice[i]; } counters[counterPosition - 1] = 0; counters[counterPosition] = 0; counterPosition--; } else { counterPosition++; } counters[counterPosition] = 1; isWhite = !isWhite; } } throw new NotFoundException(); } static decodeDigit(row, counters, rowOffset, patterns) { this.recordPattern(row, rowOffset, counters); let bestVariance = this.MAX_AVG_VARIANCE; let bestMatch = -1; let max = patterns.length; for (let i = 0; i < max; i++) { let pattern = patterns[i]; let variance = OneDReader.patternMatchVariance(counters, pattern, AbstractUPCEANReader.MAX_INDIVIDUAL_VARIANCE); if (variance < bestVariance) { bestVariance = variance; bestMatch = i; } } if (bestMatch >= 0) { return bestMatch; } else { throw new NotFoundException(); } } } AbstractUPCEANReader.MAX_AVG_VARIANCE = 0.48; AbstractUPCEANReader.MAX_INDIVIDUAL_VARIANCE = 0.7; AbstractUPCEANReader.START_END_PATTERN = Int32Array.from([1, 1, 1]); AbstractUPCEANReader.MIDDLE_PATTERN = Int32Array.from([1, 1, 1, 1, 1]); AbstractUPCEANReader.END_PATTERN = Int32Array.from([1, 1, 1, 1, 1, 1]); AbstractUPCEANReader.L_PATTERNS = [ Int32Array.from([3, 2, 1, 1]), Int32Array.from([2, 2, 2, 1]), Int32Array.from([2, 1, 2, 2]), Int32Array.from([1, 4, 1, 1]), Int32Array.from([1, 1, 3, 2]), Int32Array.from([1, 2, 3, 1]), Int32Array.from([1, 1, 1, 4]), Int32Array.from([1, 3, 1, 2]), Int32Array.from([1, 2, 1, 3]), Int32Array.from([3, 1, 1, 2]) ]; class UPCEANExtension5Support { constructor() { this.CHECK_DIGIT_ENCODINGS = [24, 20, 18, 17, 12, 6, 3, 10, 9, 5]; this.decodeMiddleCounters = Int32Array.from([0, 0, 0, 0]); this.decodeRowStringBuffer = ""; } decodeRow(rowNumber, row, extensionStartRange) { let result = this.decodeRowStringBuffer; let end = this.decodeMiddle(row, extensionStartRange, result); let resultString = result.toString(); let extensionData = UPCEANExtension5Support.parseExtensionString(resultString); let resultPoints = [ new ResultPoint((extensionStartRange[0] + extensionStartRange[1]) / 2, rowNumber), new ResultPoint(end, rowNumber) ]; let extensionResult = new Result(resultString, null, 0, resultPoints, BarcodeFormat$1.UPC_EAN_EXTENSION, (/* @__PURE__ */ new Date()).getTime()); if (extensionData != null) { extensionResult.putAllMetadata(extensionData); } return extensionResult; } decodeMiddle(row, startRange, resultString) { let counters = this.decodeMiddleCounters; counters[0] = 0; counters[1] = 0; counters[2] = 0; counters[3] = 0; let end = row.getSize(); let rowOffset = startRange[1]; let lgPatternFound = 0; for (let x = 0; x < 5 && rowOffset < end; x++) { let bestMatch = AbstractUPCEANReader.decodeDigit( row, counters, rowOffset, AbstractUPCEANReader.L_AND_G_PATTERNS ); resultString += String.fromCharCode("0".charCodeAt(0) + bestMatch % 10); for (let counter of counters) { rowOffset += counter; } if (bestMatch >= 10) { lgPatternFound |= 1 << 4 - x; } if (x !== 4) { rowOffset = row.getNextSet(rowOffset); rowOffset = row.getNextUnset(rowOffset); } } if (resultString.length !== 5) { throw new NotFoundException(); } let checkDigit = this.determineCheckDigit(lgPatternFound); if (UPCEANExtension5Support.extensionChecksum(resultString.toString()) !== checkDigit) { throw new NotFoundException(); } return rowOffset; } static extensionChecksum(s) { let length = s.length; let sum = 0; for (let i = length - 2; i >= 0; i -= 2) { sum += s.charAt(i).charCodeAt(0) - "0".charCodeAt(0); } sum *= 3; for (let i = length - 1; i >= 0; i -= 2) { sum += s.charAt(i).charCodeAt(0) - "0".charCodeAt(0); } sum *= 3; return sum % 10; } determineCheckDigit(lgPatternFound) { for (let d = 0; d < 10; d++) { if (lgPatternFound === this.CHECK_DIGIT_ENCODINGS[d]) { return d; } } throw new NotFoundException(); } static parseExtensionString(raw) { if (raw.length !== 5) { return null; } let value = UPCEANExtension5Support.parseExtension5String(raw); if (value == null) { return null; } return /* @__PURE__ */ new Map([[ResultMetadataType$1.SUGGESTED_PRICE, value]]); } static parseExtension5String(raw) { let currency; switch (raw.charAt(0)) { case "0": currency = "\xA3"; break; case "5": currency = "$"; break; case "9": switch (raw) { case "90000": return null; case "99991": return "0.00"; case "99990": return "Used"; } currency = ""; break; default: currency = ""; break; } let rawAmount = parseInt(raw.substring(1)); let unitsString = (rawAmount / 100).toString(); let hundredths = rawAmount % 100; let hundredthsString = hundredths < 10 ? "0" + hundredths : hundredths.toString(); return currency + unitsString + "." + hundredthsString; } } class UPCEANExtension2Support { constructor() { this.decodeMiddleCounters = Int32Array.from([0, 0, 0, 0]); this.decodeRowStringBuffer = ""; } decodeRow(rowNumber, row, extensionStartRange) { let result = this.decodeRowStringBuffer; let end = this.decodeMiddle(row, extensionStartRange, result); let resultString = result.toString(); let extensionData = UPCEANExtension2Support.parseExtensionString(resultString); let resultPoints = [ new ResultPoint((extensionStartRange[0] + extensionStartRange[1]) / 2, rowNumber), new ResultPoint(end, rowNumber) ]; let extensionResult = new Result(resultString, null, 0, resultPoints, BarcodeFormat$1.UPC_EAN_EXTENSION, (/* @__PURE__ */ new Date()).getTime()); if (extensionData != null) { extensionResult.putAllMetadata(extensionData); } return extensionResult; } decodeMiddle(row, startRange, resultString) { let counters = this.decodeMiddleCounters; counters[0] = 0; counters[1] = 0; counters[2] = 0; counters[3] = 0; let end = row.getSize(); let rowOffset = startRange[1]; let checkParity = 0; for (let x = 0; x < 2 && rowOffset < end; x++) { let bestMatch = AbstractUPCEANReader.decodeDigit(row, counters, rowOffset, AbstractUPCEANReader.L_AND_G_PATTERNS); resultString += String.fromCharCode("0".charCodeAt(0) + bestMatch % 10); for (let counter of counters) { rowOffset += counter; } if (bestMatch >= 10) { checkParity |= 1 << 1 - x; } if (x !== 1) { rowOffset = row.getNextSet(rowOffset); rowOffset = row.getNextUnset(rowOffset); } } if (resultString.length !== 2) { throw new NotFoundException(); } if (parseInt(resultString.toString()) % 4 !== checkParity) { throw new NotFoundException(); } return rowOffset; } static parseExtensionString(raw) { if (raw.length !== 2) { return null; } return /* @__PURE__ */ new Map([[ResultMetadataType$1.ISSUE_NUMBER, parseInt(raw)]]); } } class UPCEANExtensionSupport { static decodeRow(rowNumber, row, rowOffset) { let extensionStartRange = AbstractUPCEANReader.findGuardPattern( row, rowOffset, false, this.EXTENSION_START_PATTERN, new Int32Array(this.EXTENSION_START_PATTERN.length).fill(0) ); try { let fiveSupport = new UPCEANExtension5Support(); return fiveSupport.decodeRow(rowNumber, row, extensionStartRange); } catch (err2) { let twoSupport = new UPCEANExtension2Support(); return twoSupport.decodeRow(rowNumber, row, extensionStartRange); } } } UPCEANExtensionSupport.EXTENSION_START_PATTERN = Int32Array.from([1, 1, 2]); class UPCEANReader extends AbstractUPCEANReader { constructor() { super(); this.decodeRowStringBuffer = ""; UPCEANReader.L_AND_G_PATTERNS = UPCEANReader.L_PATTERNS.map((arr) => Int32Array.from(arr)); for (let i = 10; i < 20; i++) { let widths = UPCEANReader.L_PATTERNS[i - 10]; let reversedWidths = new Int32Array(widths.length); for (let j = 0; j < widths.length; j++) { reversedWidths[j] = widths[widths.length - j - 1]; } UPCEANReader.L_AND_G_PATTERNS[i] = reversedWidths; } } decodeRow(rowNumber, row, hints) { let startGuardRange = UPCEANReader.findStartGuardPattern(row); let resultPointCallback = hints == null ? null : hints.get(DecodeHintType$1.NEED_RESULT_POINT_CALLBACK); if (resultPointCallback != null) { const resultPoint2 = new ResultPoint((startGuardRange[0] + startGuardRange[1]) / 2, rowNumber); resultPointCallback.foundPossibleResultPoint(resultPoint2); } let budello = this.decodeMiddle(row, startGuardRange, this.decodeRowStringBuffer); let endStart = budello.rowOffset; let result = budello.resultString; if (resultPointCallback != null) { const resultPoint2 = new ResultPoint(endStart, rowNumber); resultPointCallback.foundPossibleResultPoint(resultPoint2); } let endRange = this.decodeEnd(row, endStart); if (resultPointCallback != null) { const resultPoint2 = new ResultPoint((endRange[0] + endRange[1]) / 2, rowNumber); resultPointCallback.foundPossibleResultPoint(resultPoint2); } let end = endRange[1]; let quietEnd = end + (end - endRange[0]); if (quietEnd >= row.getSize() || !row.isRange(end, quietEnd, false)) { throw new NotFoundException(); } let resultString = result.toString(); if (resultString.length < 8) { throw new FormatException(); } if (!UPCEANReader.checkChecksum(resultString)) { throw new ChecksumException(); } let left = (startGuardRange[1] + startGuardRange[0]) / 2; let right = (endRange[1] + endRange[0]) / 2; let format = this.getBarcodeFormat(); let resultPoint = [new ResultPoint(left, rowNumber), new ResultPoint(right, rowNumber)]; let decodeResult = new Result(resultString, null, 0, resultPoint, format, (/* @__PURE__ */ new Date()).getTime()); let extensionLength = 0; try { let extensionResult = UPCEANExtensionSupport.decodeRow(rowNumber, row, endRange[1]); decodeResult.putMetadata(ResultMetadataType$1.UPC_EAN_EXTENSION, extensionResult.getText()); decodeResult.putAllMetadata(extensionResult.getResultMetadata()); decodeResult.addResultPoints(extensionResult.getResultPoints()); extensionLength = extensionResult.getText().length; } catch (ignoreError) { } let allowedExtensions = hints == null ? null : hints.get(DecodeHintType$1.ALLOWED_EAN_EXTENSIONS); if (allowedExtensions != null) { let valid = false; for (let length in allowedExtensions) { if (extensionLength.toString() === length) { valid = true; break; } } if (!valid) { throw new NotFoundException(); } } return decodeResult; } decodeEnd(row, endStart) { return UPCEANReader.findGuardPattern( row, endStart, false, UPCEANReader.START_END_PATTERN, new Int32Array(UPCEANReader.START_END_PATTERN.length).fill(0) ); } static checkChecksum(s) { return UPCEANReader.checkStandardUPCEANChecksum(s); } static checkStandardUPCEANChecksum(s) { let length = s.length; if (length === 0) return false; let check = parseInt(s.charAt(length - 1), 10); return UPCEANReader.getStandardUPCEANChecksum(s.substring(0, length - 1)) === check; } static getStandardUPCEANChecksum(s) { let length = s.length; let sum = 0; for (let i = length - 1; i >= 0; i -= 2) { let digit = s.charAt(i).charCodeAt(0) - "0".charCodeAt(0); if (digit < 0 || digit > 9) { throw new FormatException(); } sum += digit; } sum *= 3; for (let i = length - 2; i >= 0; i -= 2) { let digit = s.charAt(i).charCodeAt(0) - "0".charCodeAt(0); if (digit < 0 || digit > 9) { throw new FormatException(); } sum += digit; } return (1e3 - sum) % 10; } } class EAN13Reader extends UPCEANReader { constructor() { super(); this.decodeMiddleCounters = Int32Array.from([0, 0, 0, 0]); } decodeMiddle(row, startRange, resultString) { let counters = this.decodeMiddleCounters; counters[0] = 0; counters[1] = 0; counters[2] = 0; counters[3] = 0; let end = row.getSize(); let rowOffset = startRange[1]; let lgPatternFound = 0; for (let x = 0; x < 6 && rowOffset < end; x++) { let bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS); resultString += String.fromCharCode("0".charCodeAt(0) + bestMatch % 10); for (let counter of counters) { rowOffset += counter; } if (bestMatch >= 10) { lgPatternFound |= 1 << 5 - x; } } resultString = EAN13Reader.determineFirstDigit(resultString, lgPatternFound); let middleRange = UPCEANReader.findGuardPattern( row, rowOffset, true, UPCEANReader.MIDDLE_PATTERN, new Int32Array(UPCEANReader.MIDDLE_PATTERN.length).fill(0) ); rowOffset = middleRange[1]; for (let x = 0; x < 6 && rowOffset < end; x++) { let bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_PATTERNS); resultString += String.fromCharCode("0".charCodeAt(0) + bestMatch); for (let counter of counters) { rowOffset += counter; } } return { rowOffset, resultString }; } getBarcodeFormat() { return BarcodeFormat$1.EAN_13; } static determineFirstDigit(resultString, lgPatternFound) { for (let d = 0; d < 10; d++) { if (lgPatternFound === this.FIRST_DIGIT_ENCODINGS[d]) { resultString = String.fromCharCode("0".charCodeAt(0) + d) + resultString; return resultString; } } throw new NotFoundException(); } } EAN13Reader.FIRST_DIGIT_ENCODINGS = [0, 11, 13, 14, 19, 25, 28, 21, 22, 26]; class EAN8Reader extends UPCEANReader { constructor() { super(); this.decodeMiddleCounters = Int32Array.from([0, 0, 0, 0]); } decodeMiddle(row, startRange, resultString) { const counters = this.decodeMiddleCounters; counters[0] = 0; counters[1] = 0; counters[2] = 0; counters[3] = 0; let end = row.getSize(); let rowOffset = startRange[1]; for (let x = 0; x < 4 && rowOffset < end; x++) { let bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_PATTERNS); resultString += String.fromCharCode("0".charCodeAt(0) + bestMatch); for (let counter of counters) { rowOffset += counter; } } let middleRange = UPCEANReader.findGuardPattern(row, rowOffset, true, UPCEANReader.MIDDLE_PATTERN, new Int32Array(UPCEANReader.MIDDLE_PATTERN.length).fill(0)); rowOffset = middleRange[1]; for (let x = 0; x < 4 && rowOffset < end; x++) { let bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_PATTERNS); resultString += String.fromCharCode("0".charCodeAt(0) + bestMatch); for (let counter of counters) { rowOffset += counter; } } return { rowOffset, resultString }; } getBarcodeFormat() { return BarcodeFormat$1.EAN_8; } } class UPCAReader extends UPCEANReader { constructor() { super(...arguments); this.ean13Reader = new EAN13Reader(); } // @Override getBarcodeFormat() { return BarcodeFormat$1.UPC_A; } // Note that we don't try rotation without the try harder flag, even if rotation was supported. // @Override decode(image, hints) { return this.maybeReturnResult(this.ean13Reader.decode(image)); } // @Override decodeRow(rowNumber, row, hints) { return this.maybeReturnResult(this.ean13Reader.decodeRow(rowNumber, row, hints)); } // @Override decodeMiddle(row, startRange, resultString) { return this.ean13Reader.decodeMiddle(row, startRange, resultString); } maybeReturnResult(result) { let text = result.getText(); if (text.charAt(0) === "0") { let upcaResult = new Result(text.substring(1), null, null, result.getResultPoints(), BarcodeFormat$1.UPC_A); if (result.getResultMetadata() != null) { upcaResult.putAllMetadata(result.getResultMetadata()); } return upcaResult; } else { throw new NotFoundException(); } } reset() { this.ean13Reader.reset(); } } class UPCEReader extends UPCEANReader { constructor() { super(); this.decodeMiddleCounters = new Int32Array(4); } /** * @throws NotFoundException */ // @Override decodeMiddle(row, startRange, result) { const counters = this.decodeMiddleCounters.map((x) => x); counters[0] = 0; counters[1] = 0; counters[2] = 0; counters[3] = 0; const end = row.getSize(); let rowOffset = startRange[1]; let lgPatternFound = 0; for (let x = 0; x < 6 && rowOffset < end; x++) { const bestMatch = UPCEReader.decodeDigit( row, counters, rowOffset, UPCEReader.L_AND_G_PATTERNS ); result += String.fromCharCode("0".charCodeAt(0) + bestMatch % 10); for (let counter of counters) { rowOffset += counter; } if (bestMatch >= 10) { lgPatternFound |= 1 << 5 - x; } } let resultString = UPCEReader.determineNumSysAndCheckDigit( result, lgPatternFound ); return { rowOffset, resultString }; } /** * @throws NotFoundException */ // @Override decodeEnd(row, endStart) { return UPCEReader.findGuardPatternWithoutCounters( row, endStart, true, UPCEReader.MIDDLE_END_PATTERN ); } /** * @throws FormatException */ // @Override checkChecksum(s) { return UPCEANReader.checkChecksum(UPCEReader.convertUPCEtoUPCA(s)); } /** * @throws NotFoundException */ static determineNumSysAndCheckDigit(resultString, lgPatternFound) { for (let numSys = 0; numSys <= 1; numSys++) { for (let d = 0; d < 10; d++) { if (lgPatternFound === this.NUMSYS_AND_CHECK_DIGIT_PATTERNS[numSys][d]) { let prefix = String.fromCharCode("0".charCodeAt(0) + numSys); let suffix = String.fromCharCode("0".charCodeAt(0) + d); return prefix + resultString + suffix; } } } throw NotFoundException.getNotFoundInstance(); } // @Override getBarcodeFormat() { return BarcodeFormat$1.UPC_E; } /** * Expands a UPC-E value back into its full, equivalent UPC-A code value. * * @param upce UPC-E code as string of digits * @return equivalent UPC-A code as string of digits */ static convertUPCEtoUPCA(upce) { const upceChars = upce.slice(1, 7).split("").map((x) => x.charCodeAt(0)); const result = new StringBuilder( /*12*/ ); result.append(upce.charAt(0)); let lastChar = upceChars[5]; switch (lastChar) { case 0: case 1: case 2: result.appendChars(upceChars, 0, 2); result.append(lastChar); result.append("0000"); result.appendChars(upceChars, 2, 3); break; case 3: result.appendChars(upceChars, 0, 3); result.append("00000"); result.appendChars(upceChars, 3, 2); break; case 4: result.appendChars(upceChars, 0, 4); result.append("00000"); result.append(upceChars[4]); break; default: result.appendChars(upceChars, 0, 5); result.append("0000"); result.append(lastChar); break; } if (upce.length >= 8) { result.append(upce.charAt(7)); } return result.toString(); } } UPCEReader.MIDDLE_END_PATTERN = Int32Array.from([1, 1, 1, 1, 1, 1]); UPCEReader.NUMSYS_AND_CHECK_DIGIT_PATTERNS = [ Int32Array.from([56, 52, 50, 49, 44, 38, 35, 42, 41, 37]), Int32Array.from([7, 11, 13, 14, 19, 25, 28, 21, 22, 26]) ]; class MultiFormatUPCEANReader extends OneDReader { constructor(hints) { super(); let possibleFormats = hints == null ? null : hints.get(DecodeHintType$1.POSSIBLE_FORMATS); let readers = []; if (!isNullOrUndefined2(possibleFormats)) { if (possibleFormats.indexOf(BarcodeFormat$1.EAN_13) > -1) { readers.push(new EAN13Reader()); } if (possibleFormats.indexOf(BarcodeFormat$1.UPC_A) > -1) { readers.push(new UPCAReader()); } if (possibleFormats.indexOf(BarcodeFormat$1.EAN_8) > -1) { readers.push(new EAN8Reader()); } if (possibleFormats.indexOf(BarcodeFormat$1.UPC_E) > -1) { readers.push(new UPCEReader()); } } else { readers.push(new EAN13Reader()); readers.push(new UPCAReader()); readers.push(new EAN8Reader()); readers.push(new UPCEReader()); } this.readers = readers; } decodeRow(rowNumber, row, hints) { for (let reader of this.readers) { try { const result = reader.decodeRow(rowNumber, row, hints); const ean13MayBeUPCA = result.getBarcodeFormat() === BarcodeFormat$1.EAN_13 && result.getText().charAt(0) === "0"; const possibleFormats = hints == null ? null : hints.get(DecodeHintType$1.POSSIBLE_FORMATS); const canReturnUPCA = possibleFormats == null || possibleFormats.includes(BarcodeFormat$1.UPC_A); if (ean13MayBeUPCA && canReturnUPCA) { const rawBytes = result.getRawBytes(); const resultUPCA = new Result( result.getText().substring(1), rawBytes, rawBytes ? rawBytes.length : null, result.getResultPoints(), BarcodeFormat$1.UPC_A ); resultUPCA.putAllMetadata(result.getResultMetadata()); return resultUPCA; } return result; } catch (err2) { } } throw new NotFoundException(); } reset() { for (let reader of this.readers) { reader.reset(); } } } class AbstractRSSReader extends OneDReader { constructor() { super(); this.decodeFinderCounters = new Int32Array(4); this.dataCharacterCounters = new Int32Array(8); this.oddRoundingErrors = new Array(4); this.evenRoundingErrors = new Array(4); this.oddCounts = new Array(this.dataCharacterCounters.length / 2); this.evenCounts = new Array(this.dataCharacterCounters.length / 2); } getDecodeFinderCounters() { return this.decodeFinderCounters; } getDataCharacterCounters() { return this.dataCharacterCounters; } getOddRoundingErrors() { return this.oddRoundingErrors; } getEvenRoundingErrors() { return this.evenRoundingErrors; } getOddCounts() { return this.oddCounts; } getEvenCounts() { return this.evenCounts; } parseFinderValue(counters, finderPatterns) { for (let value = 0; value < finderPatterns.length; value++) { if (OneDReader.patternMatchVariance(counters, finderPatterns[value], AbstractRSSReader.MAX_INDIVIDUAL_VARIANCE) < AbstractRSSReader.MAX_AVG_VARIANCE) { return value; } } throw new NotFoundException(); } /** * @param array values to sum * @return sum of values * @deprecated call {@link MathUtils#sum(int[])} */ static count(array) { return MathUtils.sum(new Int32Array(array)); } static increment(array, errors) { let index = 0; let biggestError = errors[0]; for (let i = 1; i < array.length; i++) { if (errors[i] > biggestError) { biggestError = errors[i]; index = i; } } array[index]++; } static decrement(array, errors) { let index = 0; let biggestError = errors[0]; for (let i = 1; i < array.length; i++) { if (errors[i] < biggestError) { biggestError = errors[i]; index = i; } } array[index]--; } static isFinderPattern(counters) { let firstTwoSum = counters[0] + counters[1]; let sum = firstTwoSum + counters[2] + counters[3]; let ratio = firstTwoSum / sum; if (ratio >= AbstractRSSReader.MIN_FINDER_PATTERN_RATIO && ratio <= AbstractRSSReader.MAX_FINDER_PATTERN_RATIO) { let minCounter = Number.MAX_SAFE_INTEGER; let maxCounter = Number.MIN_SAFE_INTEGER; for (let counter of counters) { if (counter > maxCounter) { maxCounter = counter; } if (counter < minCounter) { minCounter = counter; } } return maxCounter < 10 * minCounter; } return false; } } AbstractRSSReader.MAX_AVG_VARIANCE = 0.2; AbstractRSSReader.MAX_INDIVIDUAL_VARIANCE = 0.45; AbstractRSSReader.MIN_FINDER_PATTERN_RATIO = 9.5 / 12; AbstractRSSReader.MAX_FINDER_PATTERN_RATIO = 12.5 / 14; class DataCharacter { constructor(value, checksumPortion) { this.value = value; this.checksumPortion = checksumPortion; } getValue() { return this.value; } getChecksumPortion() { return this.checksumPortion; } toString() { return this.value + "(" + this.checksumPortion + ")"; } equals(o) { if (!(o instanceof DataCharacter)) { return false; } const that = o; return this.value === that.value && this.checksumPortion === that.checksumPortion; } hashCode() { return this.value ^ this.checksumPortion; } } class FinderPattern { constructor(value, startEnd, start, end, rowNumber) { this.value = value; this.startEnd = startEnd; this.value = value; this.startEnd = startEnd; this.resultPoints = new Array(); this.resultPoints.push(new ResultPoint(start, rowNumber)); this.resultPoints.push(new ResultPoint(end, rowNumber)); } getValue() { return this.value; } getStartEnd() { return this.startEnd; } getResultPoints() { return this.resultPoints; } equals(o) { if (!(o instanceof FinderPattern)) { return false; } const that = o; return this.value === that.value; } hashCode() { return this.value; } } class RSSUtils { constructor() { } static getRSSvalue(widths, maxWidth, noNarrow) { let n = 0; for (let width of widths) { n += width; } let val = 0; let narrowMask = 0; let elements = widths.length; for (let bar = 0; bar < elements - 1; bar++) { let elmWidth; for (elmWidth = 1, narrowMask |= 1 << bar; elmWidth < widths[bar]; elmWidth++, narrowMask &= ~(1 << bar)) { let subVal = RSSUtils.combins(n - elmWidth - 1, elements - bar - 2); if (noNarrow && narrowMask === 0 && n - elmWidth - (elements - bar - 1) >= elements - bar - 1) { subVal -= RSSUtils.combins(n - elmWidth - (elements - bar), elements - bar - 2); } if (elements - bar - 1 > 1) { let lessVal = 0; for (let mxwElement = n - elmWidth - (elements - bar - 2); mxwElement > maxWidth; mxwElement--) { lessVal += RSSUtils.combins(n - elmWidth - mxwElement - 1, elements - bar - 3); } subVal -= lessVal * (elements - 1 - bar); } else if (n - elmWidth > maxWidth) { subVal--; } val += subVal; } n -= elmWidth; } return val; } static combins(n, r) { let maxDenom; let minDenom; if (n - r > r) { minDenom = r; maxDenom = n - r; } else { minDenom = n - r; maxDenom = r; } let val = 1; let j = 1; for (let i = n; i > maxDenom; i--) { val *= i; if (j <= minDenom) { val /= j; j++; } } while (j <= minDenom) { val /= j; j++; } return val; } } class BitArrayBuilder { static buildBitArray(pairs) { let charNumber = pairs.length * 2 - 1; if (pairs[pairs.length - 1].getRightChar() == null) { charNumber -= 1; } let size = 12 * charNumber; let binary = new BitArray(size); let accPos = 0; let firstPair = pairs[0]; let firstValue = firstPair.getRightChar().getValue(); for (let i = 11; i >= 0; --i) { if ((firstValue & 1 << i) != 0) { binary.set(accPos); } accPos++; } for (let i = 1; i < pairs.length; ++i) { let currentPair = pairs[i]; let leftValue = currentPair.getLeftChar().getValue(); for (let j = 11; j >= 0; --j) { if ((leftValue & 1 << j) != 0) { binary.set(accPos); } accPos++; } if (currentPair.getRightChar() != null) { let rightValue = currentPair.getRightChar().getValue(); for (let j = 11; j >= 0; --j) { if ((rightValue & 1 << j) != 0) { binary.set(accPos); } accPos++; } } } return binary; } } class BlockParsedResult { constructor(finished, decodedInformation) { if (decodedInformation) { this.decodedInformation = null; } else { this.finished = finished; this.decodedInformation = decodedInformation; } } getDecodedInformation() { return this.decodedInformation; } isFinished() { return this.finished; } } class DecodedObject { constructor(newPosition) { this.newPosition = newPosition; } getNewPosition() { return this.newPosition; } } class DecodedChar extends DecodedObject { constructor(newPosition, value) { super(newPosition); this.value = value; } getValue() { return this.value; } isFNC1() { return this.value === DecodedChar.FNC1; } } DecodedChar.FNC1 = "$"; class DecodedInformation extends DecodedObject { constructor(newPosition, newString, remainingValue) { super(newPosition); if (remainingValue) { this.remaining = true; this.remainingValue = this.remainingValue; } else { this.remaining = false; this.remainingValue = 0; } this.newString = newString; } getNewString() { return this.newString; } isRemaining() { return this.remaining; } getRemainingValue() { return this.remainingValue; } } class DecodedNumeric extends DecodedObject { constructor(newPosition, firstDigit, secondDigit) { super(newPosition); if (firstDigit < 0 || firstDigit > 10 || secondDigit < 0 || secondDigit > 10) { throw new FormatException(); } this.firstDigit = firstDigit; this.secondDigit = secondDigit; } getFirstDigit() { return this.firstDigit; } getSecondDigit() { return this.secondDigit; } getValue() { return this.firstDigit * 10 + this.secondDigit; } isFirstDigitFNC1() { return this.firstDigit === DecodedNumeric.FNC1; } isSecondDigitFNC1() { return this.secondDigit === DecodedNumeric.FNC1; } isAnyFNC1() { return this.firstDigit === DecodedNumeric.FNC1 || this.secondDigit === DecodedNumeric.FNC1; } } DecodedNumeric.FNC1 = 10; class FieldParser { constructor() { } static parseFieldsInGeneralPurpose(rawInformation) { if (!rawInformation) { return null; } if (rawInformation.length < 2) { throw new NotFoundException(); } let firstTwoDigits = rawInformation.substring(0, 2); for (let dataLength of FieldParser.TWO_DIGIT_DATA_LENGTH) { if (dataLength[0] === firstTwoDigits) { if (dataLength[1] === FieldParser.VARIABLE_LENGTH) { return FieldParser.processVariableAI(2, dataLength[2], rawInformation); } return FieldParser.processFixedAI(2, dataLength[1], rawInformation); } } if (rawInformation.length < 3) { throw new NotFoundException(); } let firstThreeDigits = rawInformation.substring(0, 3); for (let dataLength of FieldParser.THREE_DIGIT_DATA_LENGTH) { if (dataLength[0] === firstThreeDigits) { if (dataLength[1] === FieldParser.VARIABLE_LENGTH) { return FieldParser.processVariableAI(3, dataLength[2], rawInformation); } return FieldParser.processFixedAI(3, dataLength[1], rawInformation); } } for (let dataLength of FieldParser.THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH) { if (dataLength[0] === firstThreeDigits) { if (dataLength[1] === FieldParser.VARIABLE_LENGTH) { return FieldParser.processVariableAI(4, dataLength[2], rawInformation); } return FieldParser.processFixedAI(4, dataLength[1], rawInformation); } } if (rawInformation.length < 4) { throw new NotFoundException(); } let firstFourDigits = rawInformation.substring(0, 4); for (let dataLength of FieldParser.FOUR_DIGIT_DATA_LENGTH) { if (dataLength[0] === firstFourDigits) { if (dataLength[1] === FieldParser.VARIABLE_LENGTH) { return FieldParser.processVariableAI(4, dataLength[2], rawInformation); } return FieldParser.processFixedAI(4, dataLength[1], rawInformation); } } throw new NotFoundException(); } static processFixedAI(aiSize, fieldSize, rawInformation) { if (rawInformation.length < aiSize) { throw new NotFoundException(); } let ai = rawInformation.substring(0, aiSize); if (rawInformation.length < aiSize + fieldSize) { throw new NotFoundException(); } let field = rawInformation.substring(aiSize, aiSize + fieldSize); let remaining = rawInformation.substring(aiSize + fieldSize); let result = "(" + ai + ")" + field; let parsedAI = FieldParser.parseFieldsInGeneralPurpose(remaining); return parsedAI == null ? result : result + parsedAI; } static processVariableAI(aiSize, variableFieldSize, rawInformation) { let ai = rawInformation.substring(0, aiSize); let maxSize; if (rawInformation.length < aiSize + variableFieldSize) { maxSize = rawInformation.length; } else { maxSize = aiSize + variableFieldSize; } let field = rawInformation.substring(aiSize, maxSize); let remaining = rawInformation.substring(maxSize); let result = "(" + ai + ")" + field; let parsedAI = FieldParser.parseFieldsInGeneralPurpose(remaining); return parsedAI == null ? result : result + parsedAI; } } FieldParser.VARIABLE_LENGTH = []; FieldParser.TWO_DIGIT_DATA_LENGTH = [ ["00", 18], ["01", 14], ["02", 14], ["10", FieldParser.VARIABLE_LENGTH, 20], ["11", 6], ["12", 6], ["13", 6], ["15", 6], ["17", 6], ["20", 2], ["21", FieldParser.VARIABLE_LENGTH, 20], ["22", FieldParser.VARIABLE_LENGTH, 29], ["30", FieldParser.VARIABLE_LENGTH, 8], ["37", FieldParser.VARIABLE_LENGTH, 8], // internal company codes ["90", FieldParser.VARIABLE_LENGTH, 30], ["91", FieldParser.VARIABLE_LENGTH, 30], ["92", FieldParser.VARIABLE_LENGTH, 30], ["93", FieldParser.VARIABLE_LENGTH, 30], ["94", FieldParser.VARIABLE_LENGTH, 30], ["95", FieldParser.VARIABLE_LENGTH, 30], ["96", FieldParser.VARIABLE_LENGTH, 30], ["97", FieldParser.VARIABLE_LENGTH, 3], ["98", FieldParser.VARIABLE_LENGTH, 30], ["99", FieldParser.VARIABLE_LENGTH, 30] ]; FieldParser.THREE_DIGIT_DATA_LENGTH = [ // Same format as above ["240", FieldParser.VARIABLE_LENGTH, 30], ["241", FieldParser.VARIABLE_LENGTH, 30], ["242", FieldParser.VARIABLE_LENGTH, 6], ["250", FieldParser.VARIABLE_LENGTH, 30], ["251", FieldParser.VARIABLE_LENGTH, 30], ["253", FieldParser.VARIABLE_LENGTH, 17], ["254", FieldParser.VARIABLE_LENGTH, 20], ["400", FieldParser.VARIABLE_LENGTH, 30], ["401", FieldParser.VARIABLE_LENGTH, 30], ["402", 17], ["403", FieldParser.VARIABLE_LENGTH, 30], ["410", 13], ["411", 13], ["412", 13], ["413", 13], ["414", 13], ["420", FieldParser.VARIABLE_LENGTH, 20], ["421", FieldParser.VARIABLE_LENGTH, 15], ["422", 3], ["423", FieldParser.VARIABLE_LENGTH, 15], ["424", 3], ["425", 3], ["426", 3] ]; FieldParser.THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH = [ // Same format as above ["310", 6], ["311", 6], ["312", 6], ["313", 6], ["314", 6], ["315", 6], ["316", 6], ["320", 6], ["321", 6], ["322", 6], ["323", 6], ["324", 6], ["325", 6], ["326", 6], ["327", 6], ["328", 6], ["329", 6], ["330", 6], ["331", 6], ["332", 6], ["333", 6], ["334", 6], ["335", 6], ["336", 6], ["340", 6], ["341", 6], ["342", 6], ["343", 6], ["344", 6], ["345", 6], ["346", 6], ["347", 6], ["348", 6], ["349", 6], ["350", 6], ["351", 6], ["352", 6], ["353", 6], ["354", 6], ["355", 6], ["356", 6], ["357", 6], ["360", 6], ["361", 6], ["362", 6], ["363", 6], ["364", 6], ["365", 6], ["366", 6], ["367", 6], ["368", 6], ["369", 6], ["390", FieldParser.VARIABLE_LENGTH, 15], ["391", FieldParser.VARIABLE_LENGTH, 18], ["392", FieldParser.VARIABLE_LENGTH, 15], ["393", FieldParser.VARIABLE_LENGTH, 18], ["703", FieldParser.VARIABLE_LENGTH, 30] ]; FieldParser.FOUR_DIGIT_DATA_LENGTH = [ // Same format as above ["7001", 13], ["7002", FieldParser.VARIABLE_LENGTH, 30], ["7003", 10], ["8001", 14], ["8002", FieldParser.VARIABLE_LENGTH, 20], ["8003", FieldParser.VARIABLE_LENGTH, 30], ["8004", FieldParser.VARIABLE_LENGTH, 30], ["8005", 6], ["8006", 18], ["8007", FieldParser.VARIABLE_LENGTH, 30], ["8008", FieldParser.VARIABLE_LENGTH, 12], ["8018", 18], ["8020", FieldParser.VARIABLE_LENGTH, 25], ["8100", 6], ["8101", 10], ["8102", 2], ["8110", FieldParser.VARIABLE_LENGTH, 70], ["8200", FieldParser.VARIABLE_LENGTH, 70] ]; class GeneralAppIdDecoder { constructor(information) { this.buffer = new StringBuilder(); this.information = information; } decodeAllCodes(buff, initialPosition) { let currentPosition = initialPosition; let remaining = null; do { let info = this.decodeGeneralPurposeField(currentPosition, remaining); let parsedFields = FieldParser.parseFieldsInGeneralPurpose(info.getNewString()); if (parsedFields != null) { buff.append(parsedFields); } if (info.isRemaining()) { remaining = "" + info.getRemainingValue(); } else { remaining = null; } if (currentPosition === info.getNewPosition()) { break; } currentPosition = info.getNewPosition(); } while (true); return buff.toString(); } isStillNumeric(pos) { if (pos + 7 > this.information.getSize()) { return pos + 4 <= this.information.getSize(); } for (let i = pos; i < pos + 3; ++i) { if (this.information.get(i)) { return true; } } return this.information.get(pos + 3); } decodeNumeric(pos) { if (pos + 7 > this.information.getSize()) { let numeric2 = this.extractNumericValueFromBitArray(pos, 4); if (numeric2 === 0) { return new DecodedNumeric(this.information.getSize(), DecodedNumeric.FNC1, DecodedNumeric.FNC1); } return new DecodedNumeric(this.information.getSize(), numeric2 - 1, DecodedNumeric.FNC1); } let numeric = this.extractNumericValueFromBitArray(pos, 7); let digit1 = (numeric - 8) / 11; let digit2 = (numeric - 8) % 11; return new DecodedNumeric(pos + 7, digit1, digit2); } extractNumericValueFromBitArray(pos, bits) { return GeneralAppIdDecoder.extractNumericValueFromBitArray(this.information, pos, bits); } static extractNumericValueFromBitArray(information, pos, bits) { let value = 0; for (let i = 0; i < bits; ++i) { if (information.get(pos + i)) { value |= 1 << bits - i - 1; } } return value; } decodeGeneralPurposeField(pos, remaining) { this.buffer.setLengthToZero(); if (remaining != null) { this.buffer.append(remaining); } this.current.setPosition(pos); let lastDecoded = this.parseBlocks(); if (lastDecoded != null && lastDecoded.isRemaining()) { return new DecodedInformation(this.current.getPosition(), this.buffer.toString(), lastDecoded.getRemainingValue()); } return new DecodedInformation(this.current.getPosition(), this.buffer.toString()); } parseBlocks() { let isFinished; let result; do { let initialPosition = this.current.getPosition(); if (this.current.isAlpha()) { result = this.parseAlphaBlock(); isFinished = result.isFinished(); } else if (this.current.isIsoIec646()) { result = this.parseIsoIec646Block(); isFinished = result.isFinished(); } else { result = this.parseNumericBlock(); isFinished = result.isFinished(); } let positionChanged = initialPosition !== this.current.getPosition(); if (!positionChanged && !isFinished) { break; } } while (!isFinished); return result.getDecodedInformation(); } parseNumericBlock() { while (this.isStillNumeric(this.current.getPosition())) { let numeric = this.decodeNumeric(this.current.getPosition()); this.current.setPosition(numeric.getNewPosition()); if (numeric.isFirstDigitFNC1()) { let information; if (numeric.isSecondDigitFNC1()) { information = new DecodedInformation(this.current.getPosition(), this.buffer.toString()); } else { information = new DecodedInformation(this.current.getPosition(), this.buffer.toString(), numeric.getSecondDigit()); } return new BlockParsedResult(true, information); } this.buffer.append(numeric.getFirstDigit()); if (numeric.isSecondDigitFNC1()) { let information = new DecodedInformation(this.current.getPosition(), this.buffer.toString()); return new BlockParsedResult(true, information); } this.buffer.append(numeric.getSecondDigit()); } if (this.isNumericToAlphaNumericLatch(this.current.getPosition())) { this.current.setAlpha(); this.current.incrementPosition(4); } return new BlockParsedResult(false); } parseIsoIec646Block() { while (this.isStillIsoIec646(this.current.getPosition())) { let iso = this.decodeIsoIec646(this.current.getPosition()); this.current.setPosition(iso.getNewPosition()); if (iso.isFNC1()) { let information = new DecodedInformation(this.current.getPosition(), this.buffer.toString()); return new BlockParsedResult(true, information); } this.buffer.append(iso.getValue()); } if (this.isAlphaOr646ToNumericLatch(this.current.getPosition())) { this.current.incrementPosition(3); this.current.setNumeric(); } else if (this.isAlphaTo646ToAlphaLatch(this.current.getPosition())) { if (this.current.getPosition() + 5 < this.information.getSize()) { this.current.incrementPosition(5); } else { this.current.setPosition(this.information.getSize()); } this.current.setAlpha(); } return new BlockParsedResult(false); } parseAlphaBlock() { while (this.isStillAlpha(this.current.getPosition())) { let alpha = this.decodeAlphanumeric(this.current.getPosition()); this.current.setPosition(alpha.getNewPosition()); if (alpha.isFNC1()) { let information = new DecodedInformation(this.current.getPosition(), this.buffer.toString()); return new BlockParsedResult(true, information); } this.buffer.append(alpha.getValue()); } if (this.isAlphaOr646ToNumericLatch(this.current.getPosition())) { this.current.incrementPosition(3); this.current.setNumeric(); } else if (this.isAlphaTo646ToAlphaLatch(this.current.getPosition())) { if (this.current.getPosition() + 5 < this.information.getSize()) { this.current.incrementPosition(5); } else { this.current.setPosition(this.information.getSize()); } this.current.setIsoIec646(); } return new BlockParsedResult(false); } isStillIsoIec646(pos) { if (pos + 5 > this.information.getSize()) { return false; } let fiveBitValue = this.extractNumericValueFromBitArray(pos, 5); if (fiveBitValue >= 5 && fiveBitValue < 16) { return true; } if (pos + 7 > this.information.getSize()) { return false; } let sevenBitValue = this.extractNumericValueFromBitArray(pos, 7); if (sevenBitValue >= 64 && sevenBitValue < 116) { return true; } if (pos + 8 > this.information.getSize()) { return false; } let eightBitValue = this.extractNumericValueFromBitArray(pos, 8); return eightBitValue >= 232 && eightBitValue < 253; } decodeIsoIec646(pos) { let fiveBitValue = this.extractNumericValueFromBitArray(pos, 5); if (fiveBitValue === 15) { return new DecodedChar(pos + 5, DecodedChar.FNC1); } if (fiveBitValue >= 5 && fiveBitValue < 15) { return new DecodedChar(pos + 5, "0" + (fiveBitValue - 5)); } let sevenBitValue = this.extractNumericValueFromBitArray(pos, 7); if (sevenBitValue >= 64 && sevenBitValue < 90) { return new DecodedChar(pos + 7, "" + (sevenBitValue + 1)); } if (sevenBitValue >= 90 && sevenBitValue < 116) { return new DecodedChar(pos + 7, "" + (sevenBitValue + 7)); } let eightBitValue = this.extractNumericValueFromBitArray(pos, 8); let c; switch (eightBitValue) { case 232: c = "!"; break; case 233: c = '"'; break; case 234: c = "%"; break; case 235: c = "&"; break; case 236: c = "'"; break; case 237: c = "("; break; case 238: c = ")"; break; case 239: c = "*"; break; case 240: c = "+"; break; case 241: c = ","; break; case 242: c = "-"; break; case 243: c = "."; break; case 244: c = "/"; break; case 245: c = ":"; break; case 246: c = ";"; break; case 247: c = "<"; break; case 248: c = "="; break; case 249: c = ">"; break; case 250: c = "?"; break; case 251: c = "_"; break; case 252: c = " "; break; default: throw new FormatException(); } return new DecodedChar(pos + 8, c); } isStillAlpha(pos) { if (pos + 5 > this.information.getSize()) { return false; } let fiveBitValue = this.extractNumericValueFromBitArray(pos, 5); if (fiveBitValue >= 5 && fiveBitValue < 16) { return true; } if (pos + 6 > this.information.getSize()) { return false; } let sixBitValue = this.extractNumericValueFromBitArray(pos, 6); return sixBitValue >= 16 && sixBitValue < 63; } decodeAlphanumeric(pos) { let fiveBitValue = this.extractNumericValueFromBitArray(pos, 5); if (fiveBitValue === 15) { return new DecodedChar(pos + 5, DecodedChar.FNC1); } if (fiveBitValue >= 5 && fiveBitValue < 15) { return new DecodedChar(pos + 5, "0" + (fiveBitValue - 5)); } let sixBitValue = this.extractNumericValueFromBitArray(pos, 6); if (sixBitValue >= 32 && sixBitValue < 58) { return new DecodedChar(pos + 6, "" + (sixBitValue + 33)); } let c; switch (sixBitValue) { case 58: c = "*"; break; case 59: c = ","; break; case 60: c = "-"; break; case 61: c = "."; break; case 62: c = "/"; break; default: throw new IllegalStateException("Decoding invalid alphanumeric value: " + sixBitValue); } return new DecodedChar(pos + 6, c); } isAlphaTo646ToAlphaLatch(pos) { if (pos + 1 > this.information.getSize()) { return false; } for (let i = 0; i < 5 && i + pos < this.information.getSize(); ++i) { if (i === 2) { if (!this.information.get(pos + 2)) { return false; } } else if (this.information.get(pos + i)) { return false; } } return true; } isAlphaOr646ToNumericLatch(pos) { if (pos + 3 > this.information.getSize()) { return false; } for (let i = pos; i < pos + 3; ++i) { if (this.information.get(i)) { return false; } } return true; } isNumericToAlphaNumericLatch(pos) { if (pos + 1 > this.information.getSize()) { return false; } for (let i = 0; i < 4 && i + pos < this.information.getSize(); ++i) { if (this.information.get(pos + i)) { return false; } } return true; } } class AbstractExpandedDecoder { constructor(information) { this.information = information; this.generalDecoder = new GeneralAppIdDecoder(information); } getInformation() { return this.information; } getGeneralDecoder() { return this.generalDecoder; } } class AI01decoder extends AbstractExpandedDecoder { constructor(information) { super(information); } encodeCompressedGtin(buf, currentPos) { buf.append("(01)"); let initialPosition = buf.length(); buf.append("9"); this.encodeCompressedGtinWithoutAI(buf, currentPos, initialPosition); } encodeCompressedGtinWithoutAI(buf, currentPos, initialBufferPosition) { for (let i = 0; i < 4; ++i) { let currentBlock = this.getGeneralDecoder().extractNumericValueFromBitArray(currentPos + 10 * i, 10); if (currentBlock / 100 === 0) { buf.append("0"); } if (currentBlock / 10 === 0) { buf.append("0"); } buf.append(currentBlock); } AI01decoder.appendCheckDigit(buf, initialBufferPosition); } static appendCheckDigit(buf, currentPos) { let checkDigit = 0; for (let i = 0; i < 13; i++) { let digit = buf.charAt(i + currentPos).charCodeAt(0) - "0".charCodeAt(0); checkDigit += (i & 1) === 0 ? 3 * digit : digit; } checkDigit = 10 - checkDigit % 10; if (checkDigit === 10) { checkDigit = 0; } buf.append(checkDigit); } } AI01decoder.GTIN_SIZE = 40; class AI01AndOtherAIs extends AI01decoder { // the second one is the encodation method, and the other two are for the variable length constructor(information) { super(information); } parseInformation() { let buff = new StringBuilder(); buff.append("(01)"); let initialGtinPosition = buff.length(); let firstGtinDigit = this.getGeneralDecoder().extractNumericValueFromBitArray(AI01AndOtherAIs.HEADER_SIZE, 4); buff.append(firstGtinDigit); this.encodeCompressedGtinWithoutAI(buff, AI01AndOtherAIs.HEADER_SIZE + 4, initialGtinPosition); return this.getGeneralDecoder().decodeAllCodes(buff, AI01AndOtherAIs.HEADER_SIZE + 44); } } AI01AndOtherAIs.HEADER_SIZE = 1 + 1 + 2; class AnyAIDecoder extends AbstractExpandedDecoder { constructor(information) { super(information); } parseInformation() { let buf = new StringBuilder(); return this.getGeneralDecoder().decodeAllCodes(buf, AnyAIDecoder.HEADER_SIZE); } } AnyAIDecoder.HEADER_SIZE = 2 + 1 + 2; class AI01weightDecoder extends AI01decoder { constructor(information) { super(information); } encodeCompressedWeight(buf, currentPos, weightSize) { let originalWeightNumeric = this.getGeneralDecoder().extractNumericValueFromBitArray(currentPos, weightSize); this.addWeightCode(buf, originalWeightNumeric); let weightNumeric = this.checkWeight(originalWeightNumeric); let currentDivisor = 1e5; for (let i = 0; i < 5; ++i) { if (weightNumeric / currentDivisor === 0) { buf.append("0"); } currentDivisor /= 10; } buf.append(weightNumeric); } } class AI013x0xDecoder extends AI01weightDecoder { constructor(information) { super(information); } parseInformation() { if (this.getInformation().getSize() != AI013x0xDecoder.HEADER_SIZE + AI01weightDecoder.GTIN_SIZE + AI013x0xDecoder.WEIGHT_SIZE) { throw new NotFoundException(); } let buf = new StringBuilder(); this.encodeCompressedGtin(buf, AI013x0xDecoder.HEADER_SIZE); this.encodeCompressedWeight(buf, AI013x0xDecoder.HEADER_SIZE + AI01weightDecoder.GTIN_SIZE, AI013x0xDecoder.WEIGHT_SIZE); return buf.toString(); } } AI013x0xDecoder.HEADER_SIZE = 4 + 1; AI013x0xDecoder.WEIGHT_SIZE = 15; class AI013103decoder extends AI013x0xDecoder { constructor(information) { super(information); } addWeightCode(buf, weight) { buf.append("(3103)"); } checkWeight(weight) { return weight; } } class AI01320xDecoder extends AI013x0xDecoder { constructor(information) { super(information); } addWeightCode(buf, weight) { if (weight < 1e4) { buf.append("(3202)"); } else { buf.append("(3203)"); } } checkWeight(weight) { if (weight < 1e4) { return weight; } return weight - 1e4; } } class AI01392xDecoder extends AI01decoder { constructor(information) { super(information); } parseInformation() { if (this.getInformation().getSize() < AI01392xDecoder.HEADER_SIZE + AI01decoder.GTIN_SIZE) { throw new NotFoundException(); } let buf = new StringBuilder(); this.encodeCompressedGtin(buf, AI01392xDecoder.HEADER_SIZE); let lastAIdigit = this.getGeneralDecoder().extractNumericValueFromBitArray(AI01392xDecoder.HEADER_SIZE + AI01decoder.GTIN_SIZE, AI01392xDecoder.LAST_DIGIT_SIZE); buf.append("(392"); buf.append(lastAIdigit); buf.append(")"); let decodedInformation = this.getGeneralDecoder().decodeGeneralPurposeField(AI01392xDecoder.HEADER_SIZE + AI01decoder.GTIN_SIZE + AI01392xDecoder.LAST_DIGIT_SIZE, null); buf.append(decodedInformation.getNewString()); return buf.toString(); } } AI01392xDecoder.HEADER_SIZE = 5 + 1 + 2; AI01392xDecoder.LAST_DIGIT_SIZE = 2; class AI01393xDecoder extends AI01decoder { constructor(information) { super(information); } parseInformation() { if (this.getInformation().getSize() < AI01393xDecoder.HEADER_SIZE + AI01decoder.GTIN_SIZE) { throw new NotFoundException(); } let buf = new StringBuilder(); this.encodeCompressedGtin(buf, AI01393xDecoder.HEADER_SIZE); let lastAIdigit = this.getGeneralDecoder().extractNumericValueFromBitArray(AI01393xDecoder.HEADER_SIZE + AI01decoder.GTIN_SIZE, AI01393xDecoder.LAST_DIGIT_SIZE); buf.append("(393"); buf.append(lastAIdigit); buf.append(")"); let firstThreeDigits = this.getGeneralDecoder().extractNumericValueFromBitArray(AI01393xDecoder.HEADER_SIZE + AI01decoder.GTIN_SIZE + AI01393xDecoder.LAST_DIGIT_SIZE, AI01393xDecoder.FIRST_THREE_DIGITS_SIZE); if (firstThreeDigits / 100 == 0) { buf.append("0"); } if (firstThreeDigits / 10 == 0) { buf.append("0"); } buf.append(firstThreeDigits); let generalInformation = this.getGeneralDecoder().decodeGeneralPurposeField(AI01393xDecoder.HEADER_SIZE + AI01decoder.GTIN_SIZE + AI01393xDecoder.LAST_DIGIT_SIZE + AI01393xDecoder.FIRST_THREE_DIGITS_SIZE, null); buf.append(generalInformation.getNewString()); return buf.toString(); } } AI01393xDecoder.HEADER_SIZE = 5 + 1 + 2; AI01393xDecoder.LAST_DIGIT_SIZE = 2; AI01393xDecoder.FIRST_THREE_DIGITS_SIZE = 10; class AI013x0x1xDecoder extends AI01weightDecoder { constructor(information, firstAIdigits, dateCode) { super(information); this.dateCode = dateCode; this.firstAIdigits = firstAIdigits; } parseInformation() { if (this.getInformation().getSize() != AI013x0x1xDecoder.HEADER_SIZE + AI013x0x1xDecoder.GTIN_SIZE + AI013x0x1xDecoder.WEIGHT_SIZE + AI013x0x1xDecoder.DATE_SIZE) { throw new NotFoundException(); } let buf = new StringBuilder(); this.encodeCompressedGtin(buf, AI013x0x1xDecoder.HEADER_SIZE); this.encodeCompressedWeight(buf, AI013x0x1xDecoder.HEADER_SIZE + AI013x0x1xDecoder.GTIN_SIZE, AI013x0x1xDecoder.WEIGHT_SIZE); this.encodeCompressedDate(buf, AI013x0x1xDecoder.HEADER_SIZE + AI013x0x1xDecoder.GTIN_SIZE + AI013x0x1xDecoder.WEIGHT_SIZE); return buf.toString(); } encodeCompressedDate(buf, currentPos) { let numericDate = this.getGeneralDecoder().extractNumericValueFromBitArray(currentPos, AI013x0x1xDecoder.DATE_SIZE); if (numericDate == 38400) { return; } buf.append("("); buf.append(this.dateCode); buf.append(")"); let day = numericDate % 32; numericDate /= 32; let month = numericDate % 12 + 1; numericDate /= 12; let year = numericDate; if (year / 10 == 0) { buf.append("0"); } buf.append(year); if (month / 10 == 0) { buf.append("0"); } buf.append(month); if (day / 10 == 0) { buf.append("0"); } buf.append(day); } addWeightCode(buf, weight) { buf.append("("); buf.append(this.firstAIdigits); buf.append(weight / 1e5); buf.append(")"); } checkWeight(weight) { return weight % 1e5; } } AI013x0x1xDecoder.HEADER_SIZE = 7 + 1; AI013x0x1xDecoder.WEIGHT_SIZE = 20; AI013x0x1xDecoder.DATE_SIZE = 16; function createDecoder(information) { try { if (information.get(1)) { return new AI01AndOtherAIs(information); } if (!information.get(2)) { return new AnyAIDecoder(information); } let fourBitEncodationMethod = GeneralAppIdDecoder.extractNumericValueFromBitArray(information, 1, 4); switch (fourBitEncodationMethod) { case 4: return new AI013103decoder(information); case 5: return new AI01320xDecoder(information); } let fiveBitEncodationMethod = GeneralAppIdDecoder.extractNumericValueFromBitArray(information, 1, 5); switch (fiveBitEncodationMethod) { case 12: return new AI01392xDecoder(information); case 13: return new AI01393xDecoder(information); } let sevenBitEncodationMethod = GeneralAppIdDecoder.extractNumericValueFromBitArray(information, 1, 7); switch (sevenBitEncodationMethod) { case 56: return new AI013x0x1xDecoder(information, "310", "11"); case 57: return new AI013x0x1xDecoder(information, "320", "11"); case 58: return new AI013x0x1xDecoder(information, "310", "13"); case 59: return new AI013x0x1xDecoder(information, "320", "13"); case 60: return new AI013x0x1xDecoder(information, "310", "15"); case 61: return new AI013x0x1xDecoder(information, "320", "15"); case 62: return new AI013x0x1xDecoder(information, "310", "17"); case 63: return new AI013x0x1xDecoder(information, "320", "17"); } } catch (e) { console.log(e); throw new IllegalStateException("unknown decoder: " + information); } } class ExpandedPair { constructor(leftChar, rightChar, finderPatter, mayBeLast) { this.leftchar = leftChar; this.rightchar = rightChar; this.finderpattern = finderPatter; this.maybeLast = mayBeLast; } mayBeLast() { return this.maybeLast; } getLeftChar() { return this.leftchar; } getRightChar() { return this.rightchar; } getFinderPattern() { return this.finderpattern; } mustBeLast() { return this.rightchar == null; } toString() { return "[ " + this.leftchar + ", " + this.rightchar + " : " + (this.finderpattern == null ? "null" : this.finderpattern.getValue()) + " ]"; } static equals(o1, o2) { if (!(o1 instanceof ExpandedPair)) { return false; } return ExpandedPair.equalsOrNull(o1.leftchar, o2.leftchar) && ExpandedPair.equalsOrNull(o1.rightchar, o2.rightchar) && ExpandedPair.equalsOrNull(o1.finderpattern, o2.finderpattern); } static equalsOrNull(o1, o2) { return o1 === null ? o2 === null : ExpandedPair.equals(o1, o2); } hashCode() { let value = this.leftchar.getValue() ^ this.rightchar.getValue() ^ this.finderpattern.getValue(); return value; } } class ExpandedRow { constructor(pairs, rowNumber, wasReversed) { this.pairs = pairs; this.rowNumber = rowNumber; this.wasReversed = wasReversed; } getPairs() { return this.pairs; } getRowNumber() { return this.rowNumber; } isReversed() { return this.wasReversed; } // check implementation isEquivalent(otherPairs) { return this.checkEqualitity(this, otherPairs); } // @Override toString() { return "{ " + this.pairs + " }"; } /** * Two rows are equal if they contain the same pairs in the same order. */ // @Override // check implementation equals(o1, o2) { if (!(o1 instanceof ExpandedRow)) { return false; } return this.checkEqualitity(o1, o2) && o1.wasReversed === o2.wasReversed; } checkEqualitity(pair1, pair2) { if (!pair1 || !pair2) return; let result; pair1.forEach((e1, i) => { pair2.forEach((e2) => { if (e1.getLeftChar().getValue() === e2.getLeftChar().getValue() && e1.getRightChar().getValue() === e2.getRightChar().getValue() && e1.getFinderPatter().getValue() === e2.getFinderPatter().getValue()) { result = true; } }); }); return result; } } class RSSExpandedReader extends AbstractRSSReader { constructor(verbose) { super(...arguments); this.pairs = new Array(RSSExpandedReader.MAX_PAIRS); this.rows = new Array(); this.startEnd = [2]; this.verbose = verbose === true; } decodeRow(rowNumber, row, hints) { this.pairs.length = 0; this.startFromEven = false; try { return RSSExpandedReader.constructResult(this.decodeRow2pairs(rowNumber, row)); } catch (e) { if (this.verbose) { console.log(e); } } this.pairs.length = 0; this.startFromEven = true; return RSSExpandedReader.constructResult(this.decodeRow2pairs(rowNumber, row)); } reset() { this.pairs.length = 0; this.rows.length = 0; } // Not private for testing decodeRow2pairs(rowNumber, row) { let done = false; while (!done) { try { this.pairs.push(this.retrieveNextPair(row, this.pairs, rowNumber)); } catch (error) { if (error instanceof NotFoundException) { if (!this.pairs.length) { throw new NotFoundException(); } done = true; } } } if (this.checkChecksum()) { return this.pairs; } let tryStackedDecode; if (this.rows.length) { tryStackedDecode = true; } else { tryStackedDecode = false; } this.storeRow(rowNumber, false); if (tryStackedDecode) { let ps = this.checkRowsBoolean(false); if (ps != null) { return ps; } ps = this.checkRowsBoolean(true); if (ps != null) { return ps; } } throw new NotFoundException(); } // Need to Verify checkRowsBoolean(reverse) { if (this.rows.length > 25) { this.rows.length = 0; return null; } this.pairs.length = 0; if (reverse) { this.rows = this.rows.reverse(); } let ps = null; try { ps = this.checkRows(new Array(), 0); } catch (e) { if (this.verbose) { console.log(e); } } if (reverse) { this.rows = this.rows.reverse(); } return ps; } // Try to construct a valid rows sequence // Recursion is used to implement backtracking checkRows(collectedRows, currentRow) { for (let i = currentRow; i < this.rows.length; i++) { let row = this.rows[i]; this.pairs.length = 0; for (let collectedRow of collectedRows) { this.pairs.push(collectedRow.getPairs()); } this.pairs.push(row.getPairs()); if (!RSSExpandedReader.isValidSequence(this.pairs)) { continue; } if (this.checkChecksum()) { return this.pairs; } let rs = new Array(collectedRows); rs.push(row); try { return this.checkRows(rs, i + 1); } catch (e) { if (this.verbose) { console.log(e); } } } throw new NotFoundException(); } // Whether the pairs form a valid find pattern sequence, // either complete or a prefix static isValidSequence(pairs) { for (let sequence of RSSExpandedReader.FINDER_PATTERN_SEQUENCES) { if (pairs.length > sequence.length) { continue; } let stop = true; for (let j = 0; j < pairs.length; j++) { if (pairs[j].getFinderPattern().getValue() != sequence[j]) { stop = false; break; } } if (stop) { return true; } } return false; } storeRow(rowNumber, wasReversed) { let insertPos = 0; let prevIsSame = false; let nextIsSame = false; while (insertPos < this.rows.length) { let erow = this.rows[insertPos]; if (erow.getRowNumber() > rowNumber) { nextIsSame = erow.isEquivalent(this.pairs); break; } prevIsSame = erow.isEquivalent(this.pairs); insertPos++; } if (nextIsSame || prevIsSame) { return; } if (RSSExpandedReader.isPartialRow(this.pairs, this.rows)) { return; } this.rows.push(insertPos, new ExpandedRow(this.pairs, rowNumber, wasReversed)); this.removePartialRows(this.pairs, this.rows); } // Remove all the rows that contains only specified pairs removePartialRows(pairs, rows) { for (let row of rows) { if (row.getPairs().length === pairs.length) { continue; } for (let p of row.getPairs()) { for (let pp of pairs) { if (ExpandedPair.equals(p, pp)) { break; } } } } } // Returns true when one of the rows already contains all the pairs static isPartialRow(pairs, rows) { for (let r of rows) { let allFound = true; for (let p of pairs) { let found = false; for (let pp of r.getPairs()) { if (p.equals(pp)) { found = true; break; } } if (!found) { allFound = false; break; } } if (allFound) { return true; } } return false; } // Only used for unit testing getRows() { return this.rows; } // Not private for unit testing static constructResult(pairs) { let binary = BitArrayBuilder.buildBitArray(pairs); let decoder = createDecoder(binary); let resultingString = decoder.parseInformation(); let firstPoints = pairs[0].getFinderPattern().getResultPoints(); let lastPoints = pairs[pairs.length - 1].getFinderPattern().getResultPoints(); let points = [firstPoints[0], firstPoints[1], lastPoints[0], lastPoints[1]]; return new Result(resultingString, null, null, points, BarcodeFormat$1.RSS_EXPANDED, null); } checkChecksum() { let firstPair = this.pairs.get(0); let checkCharacter = firstPair.getLeftChar(); let firstCharacter = firstPair.getRightChar(); if (firstCharacter == null) { return false; } let checksum = firstCharacter.getChecksumPortion(); let s = 2; for (let i = 1; i < this.pairs.size(); ++i) { let currentPair = this.pairs.get(i); checksum += currentPair.getLeftChar().getChecksumPortion(); s++; let currentRightChar = currentPair.getRightChar(); if (currentRightChar != null) { checksum += currentRightChar.getChecksumPortion(); s++; } } checksum %= 211; let checkCharacterValue = 211 * (s - 4) + checksum; return checkCharacterValue == checkCharacter.getValue(); } static getNextSecondBar(row, initialPos) { let currentPos; if (row.get(initialPos)) { currentPos = row.getNextUnset(initialPos); currentPos = row.getNextSet(currentPos); } else { currentPos = row.getNextSet(initialPos); currentPos = row.getNextUnset(currentPos); } return currentPos; } // not private for testing retrieveNextPair(row, previousPairs, rowNumber) { let isOddPattern = previousPairs.length % 2 == 0; if (this.startFromEven) { isOddPattern = !isOddPattern; } let pattern; let keepFinding = true; let forcedOffset = -1; do { this.findNextPair(row, previousPairs, forcedOffset); pattern = this.parseFoundFinderPattern(row, rowNumber, isOddPattern); if (pattern == null) { forcedOffset = RSSExpandedReader.getNextSecondBar(row, this.startEnd[0]); } else { keepFinding = false; } } while (keepFinding); let leftChar = this.decodeDataCharacter(row, pattern, isOddPattern, true); if (!this.isEmptyPair(previousPairs) && previousPairs[previousPairs.length - 1].mustBeLast()) { throw new NotFoundException(); } let rightChar; try { rightChar = this.decodeDataCharacter(row, pattern, isOddPattern, false); } catch (e) { rightChar = null; if (this.verbose) { console.log(e); } } return new ExpandedPair(leftChar, rightChar, pattern, true); } isEmptyPair(pairs) { if (pairs.length === 0) { return true; } return false; } findNextPair(row, previousPairs, forcedOffset) { let counters = this.getDecodeFinderCounters(); counters[0] = 0; counters[1] = 0; counters[2] = 0; counters[3] = 0; let width = row.getSize(); let rowOffset; if (forcedOffset >= 0) { rowOffset = forcedOffset; } else if (this.isEmptyPair(previousPairs)) { rowOffset = 0; } else { let lastPair = previousPairs[previousPairs.length - 1]; rowOffset = lastPair.getFinderPattern().getStartEnd()[1]; } let searchingEvenPair = previousPairs.length % 2 != 0; if (this.startFromEven) { searchingEvenPair = !searchingEvenPair; } let isWhite = false; while (rowOffset < width) { isWhite = !row.get(rowOffset); if (!isWhite) { break; } rowOffset++; } let counterPosition = 0; let patternStart = rowOffset; for (let x = rowOffset; x < width; x++) { if (row.get(x) != isWhite) { counters[counterPosition]++; } else { if (counterPosition == 3) { if (searchingEvenPair) { RSSExpandedReader.reverseCounters(counters); } if (RSSExpandedReader.isFinderPattern(counters)) { this.startEnd[0] = patternStart; this.startEnd[1] = x; return; } if (searchingEvenPair) { RSSExpandedReader.reverseCounters(counters); } patternStart += counters[0] + counters[1]; counters[0] = counters[2]; counters[1] = counters[3]; counters[2] = 0; counters[3] = 0; counterPosition--; } else { counterPosition++; } counters[counterPosition] = 1; isWhite = !isWhite; } } throw new NotFoundException(); } static reverseCounters(counters) { let length = counters.length; for (let i = 0; i < length / 2; ++i) { let tmp = counters[i]; counters[i] = counters[length - i - 1]; counters[length - i - 1] = tmp; } } parseFoundFinderPattern(row, rowNumber, oddPattern) { let firstCounter; let start; let end; if (oddPattern) { let firstElementStart = this.startEnd[0] - 1; while (firstElementStart >= 0 && !row.get(firstElementStart)) { firstElementStart--; } firstElementStart++; firstCounter = this.startEnd[0] - firstElementStart; start = firstElementStart; end = this.startEnd[1]; } else { start = this.startEnd[0]; end = row.getNextUnset(this.startEnd[1] + 1); firstCounter = end - this.startEnd[1]; } let counters = this.getDecodeFinderCounters(); System.arraycopy(counters, 0, counters, 1, counters.length - 1); counters[0] = firstCounter; let value; try { value = this.parseFinderValue(counters, RSSExpandedReader.FINDER_PATTERNS); } catch (e) { return null; } return new FinderPattern(value, [start, end], start, end, rowNumber); } decodeDataCharacter(row, pattern, isOddPattern, leftChar) { let counters = this.getDataCharacterCounters(); for (let x = 0; x < counters.length; x++) { counters[x] = 0; } if (leftChar) { RSSExpandedReader.recordPatternInReverse(row, pattern.getStartEnd()[0], counters); } else { RSSExpandedReader.recordPattern(row, pattern.getStartEnd()[1], counters); for (let i = 0, j = counters.length - 1; i < j; i++, j--) { let temp = counters[i]; counters[i] = counters[j]; counters[j] = temp; } } let numModules = 17; let elementWidth = MathUtils.sum(new Int32Array(counters)) / numModules; let expectedElementWidth = (pattern.getStartEnd()[1] - pattern.getStartEnd()[0]) / 15; if (Math.abs(elementWidth - expectedElementWidth) / expectedElementWidth > 0.3) { throw new NotFoundException(); } let oddCounts = this.getOddCounts(); let evenCounts = this.getEvenCounts(); let oddRoundingErrors = this.getOddRoundingErrors(); let evenRoundingErrors = this.getEvenRoundingErrors(); for (let i = 0; i < counters.length; i++) { let value2 = 1 * counters[i] / elementWidth; let count = value2 + 0.5; if (count < 1) { if (value2 < 0.3) { throw new NotFoundException(); } count = 1; } else if (count > 8) { if (value2 > 8.7) { throw new NotFoundException(); } count = 8; } let offset = i / 2; if ((i & 1) == 0) { oddCounts[offset] = count; oddRoundingErrors[offset] = value2 - count; } else { evenCounts[offset] = count; evenRoundingErrors[offset] = value2 - count; } } this.adjustOddEvenCounts(numModules); let weightRowNumber = 4 * pattern.getValue() + (isOddPattern ? 0 : 2) + (leftChar ? 0 : 1) - 1; let oddSum = 0; let oddChecksumPortion = 0; for (let i = oddCounts.length - 1; i >= 0; i--) { if (RSSExpandedReader.isNotA1left(pattern, isOddPattern, leftChar)) { let weight = RSSExpandedReader.WEIGHTS[weightRowNumber][2 * i]; oddChecksumPortion += oddCounts[i] * weight; } oddSum += oddCounts[i]; } let evenChecksumPortion = 0; for (let i = evenCounts.length - 1; i >= 0; i--) { if (RSSExpandedReader.isNotA1left(pattern, isOddPattern, leftChar)) { let weight = RSSExpandedReader.WEIGHTS[weightRowNumber][2 * i + 1]; evenChecksumPortion += evenCounts[i] * weight; } } let checksumPortion = oddChecksumPortion + evenChecksumPortion; if ((oddSum & 1) != 0 || oddSum > 13 || oddSum < 4) { throw new NotFoundException(); } let group = (13 - oddSum) / 2; let oddWidest = RSSExpandedReader.SYMBOL_WIDEST[group]; let evenWidest = 9 - oddWidest; let vOdd = RSSUtils.getRSSvalue(oddCounts, oddWidest, true); let vEven = RSSUtils.getRSSvalue(evenCounts, evenWidest, false); let tEven = RSSExpandedReader.EVEN_TOTAL_SUBSET[group]; let gSum = RSSExpandedReader.GSUM[group]; let value = vOdd * tEven + vEven + gSum; return new DataCharacter(value, checksumPortion); } static isNotA1left(pattern, isOddPattern, leftChar) { return !(pattern.getValue() == 0 && isOddPattern && leftChar); } adjustOddEvenCounts(numModules) { let oddSum = MathUtils.sum(new Int32Array(this.getOddCounts())); let evenSum = MathUtils.sum(new Int32Array(this.getEvenCounts())); let incrementOdd = false; let decrementOdd = false; if (oddSum > 13) { decrementOdd = true; } else if (oddSum < 4) { incrementOdd = true; } let incrementEven = false; let decrementEven = false; if (evenSum > 13) { decrementEven = true; } else if (evenSum < 4) { incrementEven = true; } let mismatch = oddSum + evenSum - numModules; let oddParityBad = (oddSum & 1) == 1; let evenParityBad = (evenSum & 1) == 0; if (mismatch == 1) { if (oddParityBad) { if (evenParityBad) { throw new NotFoundException(); } decrementOdd = true; } else { if (!evenParityBad) { throw new NotFoundException(); } decrementEven = true; } } else if (mismatch == -1) { if (oddParityBad) { if (evenParityBad) { throw new NotFoundException(); } incrementOdd = true; } else { if (!evenParityBad) { throw new NotFoundException(); } incrementEven = true; } } else if (mismatch == 0) { if (oddParityBad) { if (!evenParityBad) { throw new NotFoundException(); } if (oddSum < evenSum) { incrementOdd = true; decrementEven = true; } else { decrementOdd = true; incrementEven = true; } } else { if (evenParityBad) { throw new NotFoundException(); } } } else { throw new NotFoundException(); } if (incrementOdd) { if (decrementOdd) { throw new NotFoundException(); } RSSExpandedReader.increment(this.getOddCounts(), this.getOddRoundingErrors()); } if (decrementOdd) { RSSExpandedReader.decrement(this.getOddCounts(), this.getOddRoundingErrors()); } if (incrementEven) { if (decrementEven) { throw new NotFoundException(); } RSSExpandedReader.increment(this.getEvenCounts(), this.getOddRoundingErrors()); } if (decrementEven) { RSSExpandedReader.decrement(this.getEvenCounts(), this.getEvenRoundingErrors()); } } } RSSExpandedReader.SYMBOL_WIDEST = [7, 5, 4, 3, 1]; RSSExpandedReader.EVEN_TOTAL_SUBSET = [4, 20, 52, 104, 204]; RSSExpandedReader.GSUM = [0, 348, 1388, 2948, 3988]; RSSExpandedReader.FINDER_PATTERNS = [ Int32Array.from([1, 8, 4, 1]), Int32Array.from([3, 6, 4, 1]), Int32Array.from([3, 4, 6, 1]), Int32Array.from([3, 2, 8, 1]), Int32Array.from([2, 6, 5, 1]), Int32Array.from([2, 2, 9, 1]) // F ]; RSSExpandedReader.WEIGHTS = [ [1, 3, 9, 27, 81, 32, 96, 77], [20, 60, 180, 118, 143, 7, 21, 63], [189, 145, 13, 39, 117, 140, 209, 205], [193, 157, 49, 147, 19, 57, 171, 91], [62, 186, 136, 197, 169, 85, 44, 132], [185, 133, 188, 142, 4, 12, 36, 108], [113, 128, 173, 97, 80, 29, 87, 50], [150, 28, 84, 41, 123, 158, 52, 156], [46, 138, 203, 187, 139, 206, 196, 166], [76, 17, 51, 153, 37, 111, 122, 155], [43, 129, 176, 106, 107, 110, 119, 146], [16, 48, 144, 10, 30, 90, 59, 177], [109, 116, 137, 200, 178, 112, 125, 164], [70, 210, 208, 202, 184, 130, 179, 115], [134, 191, 151, 31, 93, 68, 204, 190], [148, 22, 66, 198, 172, 94, 71, 2], [6, 18, 54, 162, 64, 192, 154, 40], [120, 149, 25, 75, 14, 42, 126, 167], [79, 26, 78, 23, 69, 207, 199, 175], [103, 98, 83, 38, 114, 131, 182, 124], [161, 61, 183, 127, 170, 88, 53, 159], [55, 165, 73, 8, 24, 72, 5, 15], [45, 135, 194, 160, 58, 174, 100, 89] ]; RSSExpandedReader.FINDER_PAT_A = 0; RSSExpandedReader.FINDER_PAT_B = 1; RSSExpandedReader.FINDER_PAT_C = 2; RSSExpandedReader.FINDER_PAT_D = 3; RSSExpandedReader.FINDER_PAT_E = 4; RSSExpandedReader.FINDER_PAT_F = 5; RSSExpandedReader.FINDER_PATTERN_SEQUENCES = [ [RSSExpandedReader.FINDER_PAT_A, RSSExpandedReader.FINDER_PAT_A], [RSSExpandedReader.FINDER_PAT_A, RSSExpandedReader.FINDER_PAT_B, RSSExpandedReader.FINDER_PAT_B], [RSSExpandedReader.FINDER_PAT_A, RSSExpandedReader.FINDER_PAT_C, RSSExpandedReader.FINDER_PAT_B, RSSExpandedReader.FINDER_PAT_D], [RSSExpandedReader.FINDER_PAT_A, RSSExpandedReader.FINDER_PAT_E, RSSExpandedReader.FINDER_PAT_B, RSSExpandedReader.FINDER_PAT_D, RSSExpandedReader.FINDER_PAT_C], [RSSExpandedReader.FINDER_PAT_A, RSSExpandedReader.FINDER_PAT_E, RSSExpandedReader.FINDER_PAT_B, RSSExpandedReader.FINDER_PAT_D, RSSExpandedReader.FINDER_PAT_D, RSSExpandedReader.FINDER_PAT_F], [RSSExpandedReader.FINDER_PAT_A, RSSExpandedReader.FINDER_PAT_E, RSSExpandedReader.FINDER_PAT_B, RSSExpandedReader.FINDER_PAT_D, RSSExpandedReader.FINDER_PAT_E, RSSExpandedReader.FINDER_PAT_F, RSSExpandedReader.FINDER_PAT_F], [RSSExpandedReader.FINDER_PAT_A, RSSExpandedReader.FINDER_PAT_A, RSSExpandedReader.FINDER_PAT_B, RSSExpandedReader.FINDER_PAT_B, RSSExpandedReader.FINDER_PAT_C, RSSExpandedReader.FINDER_PAT_C, RSSExpandedReader.FINDER_PAT_D, RSSExpandedReader.FINDER_PAT_D], [RSSExpandedReader.FINDER_PAT_A, RSSExpandedReader.FINDER_PAT_A, RSSExpandedReader.FINDER_PAT_B, RSSExpandedReader.FINDER_PAT_B, RSSExpandedReader.FINDER_PAT_C, RSSExpandedReader.FINDER_PAT_C, RSSExpandedReader.FINDER_PAT_D, RSSExpandedReader.FINDER_PAT_E, RSSExpandedReader.FINDER_PAT_E], [RSSExpandedReader.FINDER_PAT_A, RSSExpandedReader.FINDER_PAT_A, RSSExpandedReader.FINDER_PAT_B, RSSExpandedReader.FINDER_PAT_B, RSSExpandedReader.FINDER_PAT_C, RSSExpandedReader.FINDER_PAT_C, RSSExpandedReader.FINDER_PAT_D, RSSExpandedReader.FINDER_PAT_E, RSSExpandedReader.FINDER_PAT_F, RSSExpandedReader.FINDER_PAT_F], [RSSExpandedReader.FINDER_PAT_A, RSSExpandedReader.FINDER_PAT_A, RSSExpandedReader.FINDER_PAT_B, RSSExpandedReader.FINDER_PAT_B, RSSExpandedReader.FINDER_PAT_C, RSSExpandedReader.FINDER_PAT_D, RSSExpandedReader.FINDER_PAT_D, RSSExpandedReader.FINDER_PAT_E, RSSExpandedReader.FINDER_PAT_E, RSSExpandedReader.FINDER_PAT_F, RSSExpandedReader.FINDER_PAT_F] ]; RSSExpandedReader.MAX_PAIRS = 11; class Pair extends DataCharacter { constructor(value, checksumPortion, finderPattern) { super(value, checksumPortion); this.count = 0; this.finderPattern = finderPattern; } getFinderPattern() { return this.finderPattern; } getCount() { return this.count; } incrementCount() { this.count++; } } class RSS14Reader extends AbstractRSSReader { constructor() { super(...arguments); this.possibleLeftPairs = []; this.possibleRightPairs = []; } decodeRow(rowNumber, row, hints) { const leftPair = this.decodePair(row, false, rowNumber, hints); RSS14Reader.addOrTally(this.possibleLeftPairs, leftPair); row.reverse(); let rightPair = this.decodePair(row, true, rowNumber, hints); RSS14Reader.addOrTally(this.possibleRightPairs, rightPair); row.reverse(); for (let left of this.possibleLeftPairs) { if (left.getCount() > 1) { for (let right of this.possibleRightPairs) { if (right.getCount() > 1 && RSS14Reader.checkChecksum(left, right)) { return RSS14Reader.constructResult(left, right); } } } } throw new NotFoundException(); } static addOrTally(possiblePairs, pair) { if (pair == null) { return; } let found = false; for (let other of possiblePairs) { if (other.getValue() === pair.getValue()) { other.incrementCount(); found = true; break; } } if (!found) { possiblePairs.push(pair); } } reset() { this.possibleLeftPairs.length = 0; this.possibleRightPairs.length = 0; } static constructResult(leftPair, rightPair) { let symbolValue = 4537077 * leftPair.getValue() + rightPair.getValue(); let text = new String(symbolValue).toString(); let buffer = new StringBuilder(); for (let i = 13 - text.length; i > 0; i--) { buffer.append("0"); } buffer.append(text); let checkDigit = 0; for (let i = 0; i < 13; i++) { let digit = buffer.charAt(i).charCodeAt(0) - "0".charCodeAt(0); checkDigit += (i & 1) === 0 ? 3 * digit : digit; } checkDigit = 10 - checkDigit % 10; if (checkDigit === 10) { checkDigit = 0; } buffer.append(checkDigit.toString()); let leftPoints = leftPair.getFinderPattern().getResultPoints(); let rightPoints = rightPair.getFinderPattern().getResultPoints(); return new Result(buffer.toString(), null, 0, [leftPoints[0], leftPoints[1], rightPoints[0], rightPoints[1]], BarcodeFormat$1.RSS_14, (/* @__PURE__ */ new Date()).getTime()); } static checkChecksum(leftPair, rightPair) { let checkValue = (leftPair.getChecksumPortion() + 16 * rightPair.getChecksumPortion()) % 79; let targetCheckValue = 9 * leftPair.getFinderPattern().getValue() + rightPair.getFinderPattern().getValue(); if (targetCheckValue > 72) { targetCheckValue--; } if (targetCheckValue > 8) { targetCheckValue--; } return checkValue === targetCheckValue; } decodePair(row, right, rowNumber, hints) { try { let startEnd = this.findFinderPattern(row, right); let pattern = this.parseFoundFinderPattern(row, rowNumber, right, startEnd); let resultPointCallback = hints == null ? null : hints.get(DecodeHintType$1.NEED_RESULT_POINT_CALLBACK); if (resultPointCallback != null) { let center = (startEnd[0] + startEnd[1]) / 2; if (right) { center = row.getSize() - 1 - center; } resultPointCallback.foundPossibleResultPoint(new ResultPoint(center, rowNumber)); } let outside = this.decodeDataCharacter(row, pattern, true); let inside = this.decodeDataCharacter(row, pattern, false); return new Pair(1597 * outside.getValue() + inside.getValue(), outside.getChecksumPortion() + 4 * inside.getChecksumPortion(), pattern); } catch (err2) { return null; } } decodeDataCharacter(row, pattern, outsideChar) { let counters = this.getDataCharacterCounters(); for (let x = 0; x < counters.length; x++) { counters[x] = 0; } if (outsideChar) { OneDReader.recordPatternInReverse(row, pattern.getStartEnd()[0], counters); } else { OneDReader.recordPattern(row, pattern.getStartEnd()[1] + 1, counters); for (let i = 0, j = counters.length - 1; i < j; i++, j--) { let temp = counters[i]; counters[i] = counters[j]; counters[j] = temp; } } let numModules = outsideChar ? 16 : 15; let elementWidth = MathUtils.sum(new Int32Array(counters)) / numModules; let oddCounts = this.getOddCounts(); let evenCounts = this.getEvenCounts(); let oddRoundingErrors = this.getOddRoundingErrors(); let evenRoundingErrors = this.getEvenRoundingErrors(); for (let i = 0; i < counters.length; i++) { let value = counters[i] / elementWidth; let count = Math.floor(value + 0.5); if (count < 1) { count = 1; } else if (count > 8) { count = 8; } let offset = Math.floor(i / 2); if ((i & 1) === 0) { oddCounts[offset] = count; oddRoundingErrors[offset] = value - count; } else { evenCounts[offset] = count; evenRoundingErrors[offset] = value - count; } } this.adjustOddEvenCounts(outsideChar, numModules); let oddSum = 0; let oddChecksumPortion = 0; for (let i = oddCounts.length - 1; i >= 0; i--) { oddChecksumPortion *= 9; oddChecksumPortion += oddCounts[i]; oddSum += oddCounts[i]; } let evenChecksumPortion = 0; let evenSum = 0; for (let i = evenCounts.length - 1; i >= 0; i--) { evenChecksumPortion *= 9; evenChecksumPortion += evenCounts[i]; evenSum += evenCounts[i]; } let checksumPortion = oddChecksumPortion + 3 * evenChecksumPortion; if (outsideChar) { if ((oddSum & 1) !== 0 || oddSum > 12 || oddSum < 4) { throw new NotFoundException(); } let group = (12 - oddSum) / 2; let oddWidest = RSS14Reader.OUTSIDE_ODD_WIDEST[group]; let evenWidest = 9 - oddWidest; let vOdd = RSSUtils.getRSSvalue(oddCounts, oddWidest, false); let vEven = RSSUtils.getRSSvalue(evenCounts, evenWidest, true); let tEven = RSS14Reader.OUTSIDE_EVEN_TOTAL_SUBSET[group]; let gSum = RSS14Reader.OUTSIDE_GSUM[group]; return new DataCharacter(vOdd * tEven + vEven + gSum, checksumPortion); } else { if ((evenSum & 1) !== 0 || evenSum > 10 || evenSum < 4) { throw new NotFoundException(); } let group = (10 - evenSum) / 2; let oddWidest = RSS14Reader.INSIDE_ODD_WIDEST[group]; let evenWidest = 9 - oddWidest; let vOdd = RSSUtils.getRSSvalue(oddCounts, oddWidest, true); let vEven = RSSUtils.getRSSvalue(evenCounts, evenWidest, false); let tOdd = RSS14Reader.INSIDE_ODD_TOTAL_SUBSET[group]; let gSum = RSS14Reader.INSIDE_GSUM[group]; return new DataCharacter(vEven * tOdd + vOdd + gSum, checksumPortion); } } findFinderPattern(row, rightFinderPattern) { let counters = this.getDecodeFinderCounters(); counters[0] = 0; counters[1] = 0; counters[2] = 0; counters[3] = 0; let width = row.getSize(); let isWhite = false; let rowOffset = 0; while (rowOffset < width) { isWhite = !row.get(rowOffset); if (rightFinderPattern === isWhite) { break; } rowOffset++; } let counterPosition = 0; let patternStart = rowOffset; for (let x = rowOffset; x < width; x++) { if (row.get(x) !== isWhite) { counters[counterPosition]++; } else { if (counterPosition === 3) { if (AbstractRSSReader.isFinderPattern(counters)) { return [patternStart, x]; } patternStart += counters[0] + counters[1]; counters[0] = counters[2]; counters[1] = counters[3]; counters[2] = 0; counters[3] = 0; counterPosition--; } else { counterPosition++; } counters[counterPosition] = 1; isWhite = !isWhite; } } throw new NotFoundException(); } parseFoundFinderPattern(row, rowNumber, right, startEnd) { let firstIsBlack = row.get(startEnd[0]); let firstElementStart = startEnd[0] - 1; while (firstElementStart >= 0 && firstIsBlack !== row.get(firstElementStart)) { firstElementStart--; } firstElementStart++; const firstCounter = startEnd[0] - firstElementStart; const counters = this.getDecodeFinderCounters(); const copy = new Int32Array(counters.length); System.arraycopy(counters, 0, copy, 1, counters.length - 1); copy[0] = firstCounter; const value = this.parseFinderValue(copy, RSS14Reader.FINDER_PATTERNS); let start = firstElementStart; let end = startEnd[1]; if (right) { start = row.getSize() - 1 - start; end = row.getSize() - 1 - end; } return new FinderPattern(value, [firstElementStart, startEnd[1]], start, end, rowNumber); } adjustOddEvenCounts(outsideChar, numModules) { let oddSum = MathUtils.sum(new Int32Array(this.getOddCounts())); let evenSum = MathUtils.sum(new Int32Array(this.getEvenCounts())); let incrementOdd = false; let decrementOdd = false; let incrementEven = false; let decrementEven = false; if (outsideChar) { if (oddSum > 12) { decrementOdd = true; } else if (oddSum < 4) { incrementOdd = true; } if (evenSum > 12) { decrementEven = true; } else if (evenSum < 4) { incrementEven = true; } } else { if (oddSum > 11) { decrementOdd = true; } else if (oddSum < 5) { incrementOdd = true; } if (evenSum > 10) { decrementEven = true; } else if (evenSum < 4) { incrementEven = true; } } let mismatch = oddSum + evenSum - numModules; let oddParityBad = (oddSum & 1) === (outsideChar ? 1 : 0); let evenParityBad = (evenSum & 1) === 1; if (mismatch === 1) { if (oddParityBad) { if (evenParityBad) { throw new NotFoundException(); } decrementOdd = true; } else { if (!evenParityBad) { throw new NotFoundException(); } decrementEven = true; } } else if (mismatch === -1) { if (oddParityBad) { if (evenParityBad) { throw new NotFoundException(); } incrementOdd = true; } else { if (!evenParityBad) { throw new NotFoundException(); } incrementEven = true; } } else if (mismatch === 0) { if (oddParityBad) { if (!evenParityBad) { throw new NotFoundException(); } if (oddSum < evenSum) { incrementOdd = true; decrementEven = true; } else { decrementOdd = true; incrementEven = true; } } else { if (evenParityBad) { throw new NotFoundException(); } } } else { throw new NotFoundException(); } if (incrementOdd) { if (decrementOdd) { throw new NotFoundException(); } AbstractRSSReader.increment(this.getOddCounts(), this.getOddRoundingErrors()); } if (decrementOdd) { AbstractRSSReader.decrement(this.getOddCounts(), this.getOddRoundingErrors()); } if (incrementEven) { if (decrementEven) { throw new NotFoundException(); } AbstractRSSReader.increment(this.getEvenCounts(), this.getOddRoundingErrors()); } if (decrementEven) { AbstractRSSReader.decrement(this.getEvenCounts(), this.getEvenRoundingErrors()); } } } RSS14Reader.OUTSIDE_EVEN_TOTAL_SUBSET = [1, 10, 34, 70, 126]; RSS14Reader.INSIDE_ODD_TOTAL_SUBSET = [4, 20, 48, 81]; RSS14Reader.OUTSIDE_GSUM = [0, 161, 961, 2015, 2715]; RSS14Reader.INSIDE_GSUM = [0, 336, 1036, 1516]; RSS14Reader.OUTSIDE_ODD_WIDEST = [8, 6, 4, 3, 1]; RSS14Reader.INSIDE_ODD_WIDEST = [2, 4, 6, 8]; RSS14Reader.FINDER_PATTERNS = [ Int32Array.from([3, 8, 2, 1]), Int32Array.from([3, 5, 5, 1]), Int32Array.from([3, 3, 7, 1]), Int32Array.from([3, 1, 9, 1]), Int32Array.from([2, 7, 4, 1]), Int32Array.from([2, 5, 6, 1]), Int32Array.from([2, 3, 8, 1]), Int32Array.from([1, 5, 7, 1]), Int32Array.from([1, 3, 9, 1]) ]; class MultiFormatOneDReader extends OneDReader { constructor(hints, verbose) { super(); this.readers = []; this.verbose = verbose === true; const possibleFormats = !hints ? null : hints.get(DecodeHintType$1.POSSIBLE_FORMATS); const useCode39CheckDigit = hints && hints.get(DecodeHintType$1.ASSUME_CODE_39_CHECK_DIGIT) !== void 0; if (possibleFormats) { if (possibleFormats.includes(BarcodeFormat$1.EAN_13) || possibleFormats.includes(BarcodeFormat$1.UPC_A) || possibleFormats.includes(BarcodeFormat$1.EAN_8) || possibleFormats.includes(BarcodeFormat$1.UPC_E)) { this.readers.push(new MultiFormatUPCEANReader(hints)); } if (possibleFormats.includes(BarcodeFormat$1.CODE_39)) { this.readers.push(new Code39Reader(useCode39CheckDigit)); } if (possibleFormats.includes(BarcodeFormat$1.CODE_128)) { this.readers.push(new Code128Reader()); } if (possibleFormats.includes(BarcodeFormat$1.ITF)) { this.readers.push(new ITFReader()); } if (possibleFormats.includes(BarcodeFormat$1.RSS_14)) { this.readers.push(new RSS14Reader()); } if (possibleFormats.includes(BarcodeFormat$1.RSS_EXPANDED)) { this.readers.push(new RSSExpandedReader(this.verbose)); } } else { this.readers.push(new MultiFormatUPCEANReader(hints)); this.readers.push(new Code39Reader()); this.readers.push(new MultiFormatUPCEANReader(hints)); this.readers.push(new Code128Reader()); this.readers.push(new ITFReader()); this.readers.push(new RSS14Reader()); this.readers.push(new RSSExpandedReader(this.verbose)); } } // @Override decodeRow(rowNumber, row, hints) { for (let i = 0; i < this.readers.length; i++) { try { return this.readers[i].decodeRow(rowNumber, row, hints); } catch (re) { } } throw new NotFoundException(); } // @Override reset() { this.readers.forEach((reader) => reader.reset()); } } class BrowserBarcodeReader extends BrowserCodeReader { /** * Creates an instance of BrowserBarcodeReader. * @param {number} [timeBetweenScansMillis=500] the time delay between subsequent decode tries * @param {MapDeduces version information from Data Matrix dimensions.
* * @param numRows Number of rows in modules * @param numColumns Number of columns in modules * @return Version for a Data Matrix Code of those dimensions * @throws FormatException if dimensions do correspond to a valid Data Matrix size */ static getVersionForDimensions(numRows, numColumns) { if ((numRows & 1) !== 0 || (numColumns & 1) !== 0) { throw new FormatException(); } for (let version of Version.VERSIONS) { if (version.symbolSizeRows === numRows && version.symbolSizeColumns === numColumns) { return version; } } throw new FormatException(); } // @Override toString() { return "" + this.versionNumber; } /** * See ISO 16022:2006 5.5.1 Table 7 */ static buildVersions() { return [ new Version(1, 10, 10, 8, 8, new ECBlocks(5, new ECB(1, 3))), new Version(2, 12, 12, 10, 10, new ECBlocks(7, new ECB(1, 5))), new Version(3, 14, 14, 12, 12, new ECBlocks(10, new ECB(1, 8))), new Version(4, 16, 16, 14, 14, new ECBlocks(12, new ECB(1, 12))), new Version(5, 18, 18, 16, 16, new ECBlocks(14, new ECB(1, 18))), new Version(6, 20, 20, 18, 18, new ECBlocks(18, new ECB(1, 22))), new Version(7, 22, 22, 20, 20, new ECBlocks(20, new ECB(1, 30))), new Version(8, 24, 24, 22, 22, new ECBlocks(24, new ECB(1, 36))), new Version(9, 26, 26, 24, 24, new ECBlocks(28, new ECB(1, 44))), new Version(10, 32, 32, 14, 14, new ECBlocks(36, new ECB(1, 62))), new Version(11, 36, 36, 16, 16, new ECBlocks(42, new ECB(1, 86))), new Version(12, 40, 40, 18, 18, new ECBlocks(48, new ECB(1, 114))), new Version(13, 44, 44, 20, 20, new ECBlocks(56, new ECB(1, 144))), new Version(14, 48, 48, 22, 22, new ECBlocks(68, new ECB(1, 174))), new Version(15, 52, 52, 24, 24, new ECBlocks(42, new ECB(2, 102))), new Version(16, 64, 64, 14, 14, new ECBlocks(56, new ECB(2, 140))), new Version(17, 72, 72, 16, 16, new ECBlocks(36, new ECB(4, 92))), new Version(18, 80, 80, 18, 18, new ECBlocks(48, new ECB(4, 114))), new Version(19, 88, 88, 20, 20, new ECBlocks(56, new ECB(4, 144))), new Version(20, 96, 96, 22, 22, new ECBlocks(68, new ECB(4, 174))), new Version(21, 104, 104, 24, 24, new ECBlocks(56, new ECB(6, 136))), new Version(22, 120, 120, 18, 18, new ECBlocks(68, new ECB(6, 175))), new Version(23, 132, 132, 20, 20, new ECBlocks(62, new ECB(8, 163))), new Version(24, 144, 144, 22, 22, new ECBlocks(62, new ECB(8, 156), new ECB(2, 155))), new Version(25, 8, 18, 6, 16, new ECBlocks(7, new ECB(1, 5))), new Version(26, 8, 32, 6, 14, new ECBlocks(11, new ECB(1, 10))), new Version(27, 12, 26, 10, 24, new ECBlocks(14, new ECB(1, 16))), new Version(28, 12, 36, 10, 16, new ECBlocks(18, new ECB(1, 22))), new Version(29, 16, 36, 14, 16, new ECBlocks(24, new ECB(1, 32))), new Version(30, 16, 48, 14, 22, new ECBlocks(28, new ECB(1, 49))) ]; } } Version.VERSIONS = Version.buildVersions(); class BitMatrixParser { /** * @param bitMatrix {@link BitMatrix} to parse * @throws FormatException if dimension is < 8 or > 144 or not 0 mod 2 */ constructor(bitMatrix) { const dimension = bitMatrix.getHeight(); if (dimension < 8 || dimension > 144 || (dimension & 1) !== 0) { throw new FormatException(); } this.version = BitMatrixParser.readVersion(bitMatrix); this.mappingBitMatrix = this.extractDataRegion(bitMatrix); this.readMappingMatrix = new BitMatrix(this.mappingBitMatrix.getWidth(), this.mappingBitMatrix.getHeight()); } getVersion() { return this.version; } /** *Creates the version object based on the dimension of the original bit matrix from * the datamatrix code.
* *See ISO 16022:2006 Table 7 - ECC 200 symbol attributes
* * @param bitMatrix Original {@link BitMatrix} including alignment patterns * @return {@link Version} encapsulating the Data Matrix Code's "version" * @throws FormatException if the dimensions of the mapping matrix are not valid * Data Matrix dimensions. */ static readVersion(bitMatrix) { const numRows = bitMatrix.getHeight(); const numColumns = bitMatrix.getWidth(); return Version.getVersionForDimensions(numRows, numColumns); } /** *Reads the bits in the {@link BitMatrix} representing the mapping matrix (No alignment patterns) * in the correct order in order to reconstitute the codewords bytes contained within the * Data Matrix Code.
* * @return bytes encoded within the Data Matrix Code * @throws FormatException if the exact number of bytes expected is not read */ readCodewords() { const result = new Int8Array(this.version.getTotalCodewords()); let resultOffset = 0; let row = 4; let column = 0; const numRows = this.mappingBitMatrix.getHeight(); const numColumns = this.mappingBitMatrix.getWidth(); let corner1Read = false; let corner2Read = false; let corner3Read = false; let corner4Read = false; do { if (row === numRows && column === 0 && !corner1Read) { result[resultOffset++] = this.readCorner1(numRows, numColumns) & 255; row -= 2; column += 2; corner1Read = true; } else if (row === numRows - 2 && column === 0 && (numColumns & 3) !== 0 && !corner2Read) { result[resultOffset++] = this.readCorner2(numRows, numColumns) & 255; row -= 2; column += 2; corner2Read = true; } else if (row === numRows + 4 && column === 2 && (numColumns & 7) === 0 && !corner3Read) { result[resultOffset++] = this.readCorner3(numRows, numColumns) & 255; row -= 2; column += 2; corner3Read = true; } else if (row === numRows - 2 && column === 0 && (numColumns & 7) === 4 && !corner4Read) { result[resultOffset++] = this.readCorner4(numRows, numColumns) & 255; row -= 2; column += 2; corner4Read = true; } else { do { if (row < numRows && column >= 0 && !this.readMappingMatrix.get(column, row)) { result[resultOffset++] = this.readUtah(row, column, numRows, numColumns) & 255; } row -= 2; column += 2; } while (row >= 0 && column < numColumns); row += 1; column += 3; do { if (row >= 0 && column < numColumns && !this.readMappingMatrix.get(column, row)) { result[resultOffset++] = this.readUtah(row, column, numRows, numColumns) & 255; } row += 2; column -= 2; } while (row < numRows && column >= 0); row += 3; column += 1; } } while (row < numRows || column < numColumns); if (resultOffset !== this.version.getTotalCodewords()) { throw new FormatException(); } return result; } /** *Reads a bit of the mapping matrix accounting for boundary wrapping.
* * @param row Row to read in the mapping matrix * @param column Column to read in the mapping matrix * @param numRows Number of rows in the mapping matrix * @param numColumns Number of columns in the mapping matrix * @return value of the given bit in the mapping matrix */ readModule(row, column, numRows, numColumns) { if (row < 0) { row += numRows; column += 4 - (numRows + 4 & 7); } if (column < 0) { column += numColumns; row += 4 - (numColumns + 4 & 7); } this.readMappingMatrix.set(column, row); return this.mappingBitMatrix.get(column, row); } /** *Reads the 8 bits of the standard Utah-shaped pattern.
* *See ISO 16022:2006, 5.8.1 Figure 6
* * @param row Current row in the mapping matrix, anchored at the 8th bit (LSB) of the pattern * @param column Current column in the mapping matrix, anchored at the 8th bit (LSB) of the pattern * @param numRows Number of rows in the mapping matrix * @param numColumns Number of columns in the mapping matrix * @return byte from the utah shape */ readUtah(row, column, numRows, numColumns) { let currentByte = 0; if (this.readModule(row - 2, column - 2, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(row - 2, column - 1, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(row - 1, column - 2, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(row - 1, column - 1, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(row - 1, column, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(row, column - 2, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(row, column - 1, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(row, column, numRows, numColumns)) { currentByte |= 1; } return currentByte; } /** *Reads the 8 bits of the special corner condition 1.
* *See ISO 16022:2006, Figure F.3
* * @param numRows Number of rows in the mapping matrix * @param numColumns Number of columns in the mapping matrix * @return byte from the Corner condition 1 */ readCorner1(numRows, numColumns) { let currentByte = 0; if (this.readModule(numRows - 1, 0, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(numRows - 1, 1, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(numRows - 1, 2, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(0, numColumns - 2, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(0, numColumns - 1, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(1, numColumns - 1, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(2, numColumns - 1, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(3, numColumns - 1, numRows, numColumns)) { currentByte |= 1; } return currentByte; } /** *Reads the 8 bits of the special corner condition 2.
* *See ISO 16022:2006, Figure F.4
* * @param numRows Number of rows in the mapping matrix * @param numColumns Number of columns in the mapping matrix * @return byte from the Corner condition 2 */ readCorner2(numRows, numColumns) { let currentByte = 0; if (this.readModule(numRows - 3, 0, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(numRows - 2, 0, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(numRows - 1, 0, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(0, numColumns - 4, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(0, numColumns - 3, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(0, numColumns - 2, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(0, numColumns - 1, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(1, numColumns - 1, numRows, numColumns)) { currentByte |= 1; } return currentByte; } /** *Reads the 8 bits of the special corner condition 3.
* *See ISO 16022:2006, Figure F.5
* * @param numRows Number of rows in the mapping matrix * @param numColumns Number of columns in the mapping matrix * @return byte from the Corner condition 3 */ readCorner3(numRows, numColumns) { let currentByte = 0; if (this.readModule(numRows - 1, 0, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(numRows - 1, numColumns - 1, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(0, numColumns - 3, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(0, numColumns - 2, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(0, numColumns - 1, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(1, numColumns - 3, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(1, numColumns - 2, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(1, numColumns - 1, numRows, numColumns)) { currentByte |= 1; } return currentByte; } /** *Reads the 8 bits of the special corner condition 4.
* *See ISO 16022:2006, Figure F.6
* * @param numRows Number of rows in the mapping matrix * @param numColumns Number of columns in the mapping matrix * @return byte from the Corner condition 4 */ readCorner4(numRows, numColumns) { let currentByte = 0; if (this.readModule(numRows - 3, 0, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(numRows - 2, 0, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(numRows - 1, 0, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(0, numColumns - 2, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(0, numColumns - 1, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(1, numColumns - 1, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(2, numColumns - 1, numRows, numColumns)) { currentByte |= 1; } currentByte <<= 1; if (this.readModule(3, numColumns - 1, numRows, numColumns)) { currentByte |= 1; } return currentByte; } /** *Extracts the data region from a {@link BitMatrix} that contains * alignment patterns.
* * @param bitMatrix Original {@link BitMatrix} with alignment patterns * @return BitMatrix that has the alignment patterns removed */ extractDataRegion(bitMatrix) { const symbolSizeRows = this.version.getSymbolSizeRows(); const symbolSizeColumns = this.version.getSymbolSizeColumns(); if (bitMatrix.getHeight() !== symbolSizeRows) { throw new IllegalArgumentException("Dimension of bitMatrix must match the version size"); } const dataRegionSizeRows = this.version.getDataRegionSizeRows(); const dataRegionSizeColumns = this.version.getDataRegionSizeColumns(); const numDataRegionsRow = symbolSizeRows / dataRegionSizeRows | 0; const numDataRegionsColumn = symbolSizeColumns / dataRegionSizeColumns | 0; const sizeDataRegionRow = numDataRegionsRow * dataRegionSizeRows; const sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns; const bitMatrixWithoutAlignment = new BitMatrix(sizeDataRegionColumn, sizeDataRegionRow); for (let dataRegionRow = 0; dataRegionRow < numDataRegionsRow; ++dataRegionRow) { const dataRegionRowOffset = dataRegionRow * dataRegionSizeRows; for (let dataRegionColumn = 0; dataRegionColumn < numDataRegionsColumn; ++dataRegionColumn) { const dataRegionColumnOffset = dataRegionColumn * dataRegionSizeColumns; for (let i = 0; i < dataRegionSizeRows; ++i) { const readRowOffset = dataRegionRow * (dataRegionSizeRows + 2) + 1 + i; const writeRowOffset = dataRegionRowOffset + i; for (let j = 0; j < dataRegionSizeColumns; ++j) { const readColumnOffset = dataRegionColumn * (dataRegionSizeColumns + 2) + 1 + j; if (bitMatrix.get(readColumnOffset, readRowOffset)) { const writeColumnOffset = dataRegionColumnOffset + j; bitMatrixWithoutAlignment.set(writeColumnOffset, writeRowOffset); } } } } } return bitMatrixWithoutAlignment; } } class DataBlock { constructor(numDataCodewords, codewords) { this.numDataCodewords = numDataCodewords; this.codewords = codewords; } /** *When Data Matrix Codes use multiple data blocks, they actually interleave the bytes of each of them. * That is, the first byte of data block 1 to n is written, then the second bytes, and so on. This * method will separate the data into original blocks.
* * @param rawCodewords bytes as read directly from the Data Matrix Code * @param version version of the Data Matrix Code * @return DataBlocks containing original bytes, "de-interleaved" from representation in the * Data Matrix Code */ static getDataBlocks(rawCodewords, version) { const ecBlocks = version.getECBlocks(); let totalBlocks = 0; const ecBlockArray = ecBlocks.getECBlocks(); for (let ecBlock of ecBlockArray) { totalBlocks += ecBlock.getCount(); } const result = new Array(totalBlocks); let numResultBlocks = 0; for (let ecBlock of ecBlockArray) { for (let i = 0; i < ecBlock.getCount(); i++) { const numDataCodewords = ecBlock.getDataCodewords(); const numBlockCodewords = ecBlocks.getECCodewords() + numDataCodewords; result[numResultBlocks++] = new DataBlock(numDataCodewords, new Uint8Array(numBlockCodewords)); } } const longerBlocksTotalCodewords = result[0].codewords.length; const longerBlocksNumDataCodewords = longerBlocksTotalCodewords - ecBlocks.getECCodewords(); const shorterBlocksNumDataCodewords = longerBlocksNumDataCodewords - 1; let rawCodewordsOffset = 0; for (let i = 0; i < shorterBlocksNumDataCodewords; i++) { for (let j = 0; j < numResultBlocks; j++) { result[j].codewords[i] = rawCodewords[rawCodewordsOffset++]; } } const specialVersion = version.getVersionNumber() === 24; const numLongerBlocks = specialVersion ? 8 : numResultBlocks; for (let j = 0; j < numLongerBlocks; j++) { result[j].codewords[longerBlocksNumDataCodewords - 1] = rawCodewords[rawCodewordsOffset++]; } const max = result[0].codewords.length; for (let i = longerBlocksNumDataCodewords; i < max; i++) { for (let j = 0; j < numResultBlocks; j++) { const jOffset = specialVersion ? (j + 8) % numResultBlocks : j; const iOffset = specialVersion && jOffset > 7 ? i - 1 : i; result[jOffset].codewords[iOffset] = rawCodewords[rawCodewordsOffset++]; } } if (rawCodewordsOffset !== rawCodewords.length) { throw new IllegalArgumentException(); } return result; } getNumDataCodewords() { return this.numDataCodewords; } getCodewords() { return this.codewords; } } class BitSource { /** * @param bytes bytes from which this will read bits. Bits will be read from the first byte first. * Bits are read within a byte from most-significant to least-significant bit. */ constructor(bytes) { this.bytes = bytes; this.byteOffset = 0; this.bitOffset = 0; } /** * @return index of next bit in current byte which would be read by the next call to {@link #readBits(int)}. */ getBitOffset() { return this.bitOffset; } /** * @return index of next byte in input byte array which would be read by the next call to {@link #readBits(int)}. */ getByteOffset() { return this.byteOffset; } /** * @param numBits number of bits to read * @return int representing the bits read. The bits will appear as the least-significant * bits of the int * @throws IllegalArgumentException if numBits isn't in [1,32] or more than is available */ readBits(numBits) { if (numBits < 1 || numBits > 32 || numBits > this.available()) { throw new IllegalArgumentException("" + numBits); } let result = 0; let bitOffset = this.bitOffset; let byteOffset = this.byteOffset; const bytes = this.bytes; if (bitOffset > 0) { const bitsLeft = 8 - bitOffset; const toRead = numBits < bitsLeft ? numBits : bitsLeft; const bitsToNotRead = bitsLeft - toRead; const mask = 255 >> 8 - toRead << bitsToNotRead; result = (bytes[byteOffset] & mask) >> bitsToNotRead; numBits -= toRead; bitOffset += toRead; if (bitOffset === 8) { bitOffset = 0; byteOffset++; } } if (numBits > 0) { while (numBits >= 8) { result = result << 8 | bytes[byteOffset] & 255; byteOffset++; numBits -= 8; } if (numBits > 0) { const bitsToNotRead = 8 - numBits; const mask = 255 >> bitsToNotRead << bitsToNotRead; result = result << numBits | (bytes[byteOffset] & mask) >> bitsToNotRead; bitOffset += numBits; } } this.bitOffset = bitOffset; this.byteOffset = byteOffset; return result; } /** * @return number of bits that can be read successfully */ available() { return 8 * (this.bytes.length - this.byteOffset) - this.bitOffset; } } var Mode; (function(Mode2) { Mode2[Mode2["PAD_ENCODE"] = 0] = "PAD_ENCODE"; Mode2[Mode2["ASCII_ENCODE"] = 1] = "ASCII_ENCODE"; Mode2[Mode2["C40_ENCODE"] = 2] = "C40_ENCODE"; Mode2[Mode2["TEXT_ENCODE"] = 3] = "TEXT_ENCODE"; Mode2[Mode2["ANSIX12_ENCODE"] = 4] = "ANSIX12_ENCODE"; Mode2[Mode2["EDIFACT_ENCODE"] = 5] = "EDIFACT_ENCODE"; Mode2[Mode2["BASE256_ENCODE"] = 6] = "BASE256_ENCODE"; })(Mode || (Mode = {})); class DecodedBitStreamParser { static decode(bytes) { const bits = new BitSource(bytes); const result = new StringBuilder(); const resultTrailer = new StringBuilder(); const byteSegments = new Array(); let mode = Mode.ASCII_ENCODE; do { if (mode === Mode.ASCII_ENCODE) { mode = this.decodeAsciiSegment(bits, result, resultTrailer); } else { switch (mode) { case Mode.C40_ENCODE: this.decodeC40Segment(bits, result); break; case Mode.TEXT_ENCODE: this.decodeTextSegment(bits, result); break; case Mode.ANSIX12_ENCODE: this.decodeAnsiX12Segment(bits, result); break; case Mode.EDIFACT_ENCODE: this.decodeEdifactSegment(bits, result); break; case Mode.BASE256_ENCODE: this.decodeBase256Segment(bits, result, byteSegments); break; default: throw new FormatException(); } mode = Mode.ASCII_ENCODE; } } while (mode !== Mode.PAD_ENCODE && bits.available() > 0); if (resultTrailer.length() > 0) { result.append(resultTrailer.toString()); } return new DecoderResult(bytes, result.toString(), byteSegments.length === 0 ? null : byteSegments, null); } /** * See ISO 16022:2006, 5.2.3 and Annex C, Table C.2 */ static decodeAsciiSegment(bits, result, resultTrailer) { let upperShift = false; do { let oneByte = bits.readBits(8); if (oneByte === 0) { throw new FormatException(); } else if (oneByte <= 128) { if (upperShift) { oneByte += 128; } result.append(String.fromCharCode(oneByte - 1)); return Mode.ASCII_ENCODE; } else if (oneByte === 129) { return Mode.PAD_ENCODE; } else if (oneByte <= 229) { const value = oneByte - 130; if (value < 10) { result.append("0"); } result.append("" + value); } else { switch (oneByte) { case 230: return Mode.C40_ENCODE; case 231: return Mode.BASE256_ENCODE; case 232: result.append(String.fromCharCode(29)); break; case 233: // Structured Append case 234: break; case 235: upperShift = true; break; case 236: result.append("[)>05"); resultTrailer.insert(0, ""); break; case 237: result.append("[)>06"); resultTrailer.insert(0, ""); break; case 238: return Mode.ANSIX12_ENCODE; case 239: return Mode.TEXT_ENCODE; case 240: return Mode.EDIFACT_ENCODE; case 241: break; default: if (oneByte !== 254 || bits.available() !== 0) { throw new FormatException(); } break; } } } while (bits.available() > 0); return Mode.ASCII_ENCODE; } /** * See ISO 16022:2006, 5.2.5 and Annex C, Table C.1 */ static decodeC40Segment(bits, result) { let upperShift = false; const cValues = []; let shift = 0; do { if (bits.available() === 8) { return; } const firstByte = bits.readBits(8); if (firstByte === 254) { return; } this.parseTwoBytes(firstByte, bits.readBits(8), cValues); for (let i = 0; i < 3; i++) { const cValue = cValues[i]; switch (shift) { case 0: if (cValue < 3) { shift = cValue + 1; } else if (cValue < this.C40_BASIC_SET_CHARS.length) { const c40char = this.C40_BASIC_SET_CHARS[cValue]; if (upperShift) { result.append(String.fromCharCode(c40char.charCodeAt(0) + 128)); upperShift = false; } else { result.append(c40char); } } else { throw new FormatException(); } break; case 1: if (upperShift) { result.append(String.fromCharCode(cValue + 128)); upperShift = false; } else { result.append(String.fromCharCode(cValue)); } shift = 0; break; case 2: if (cValue < this.C40_SHIFT2_SET_CHARS.length) { const c40char = this.C40_SHIFT2_SET_CHARS[cValue]; if (upperShift) { result.append(String.fromCharCode(c40char.charCodeAt(0) + 128)); upperShift = false; } else { result.append(c40char); } } else { switch (cValue) { case 27: result.append(String.fromCharCode(29)); break; case 30: upperShift = true; break; default: throw new FormatException(); } } shift = 0; break; case 3: if (upperShift) { result.append(String.fromCharCode(cValue + 224)); upperShift = false; } else { result.append(String.fromCharCode(cValue + 96)); } shift = 0; break; default: throw new FormatException(); } } } while (bits.available() > 0); } /** * See ISO 16022:2006, 5.2.6 and Annex C, Table C.2 */ static decodeTextSegment(bits, result) { let upperShift = false; let cValues = []; let shift = 0; do { if (bits.available() === 8) { return; } const firstByte = bits.readBits(8); if (firstByte === 254) { return; } this.parseTwoBytes(firstByte, bits.readBits(8), cValues); for (let i = 0; i < 3; i++) { const cValue = cValues[i]; switch (shift) { case 0: if (cValue < 3) { shift = cValue + 1; } else if (cValue < this.TEXT_BASIC_SET_CHARS.length) { const textChar = this.TEXT_BASIC_SET_CHARS[cValue]; if (upperShift) { result.append(String.fromCharCode(textChar.charCodeAt(0) + 128)); upperShift = false; } else { result.append(textChar); } } else { throw new FormatException(); } break; case 1: if (upperShift) { result.append(String.fromCharCode(cValue + 128)); upperShift = false; } else { result.append(String.fromCharCode(cValue)); } shift = 0; break; case 2: if (cValue < this.TEXT_SHIFT2_SET_CHARS.length) { const textChar = this.TEXT_SHIFT2_SET_CHARS[cValue]; if (upperShift) { result.append(String.fromCharCode(textChar.charCodeAt(0) + 128)); upperShift = false; } else { result.append(textChar); } } else { switch (cValue) { case 27: result.append(String.fromCharCode(29)); break; case 30: upperShift = true; break; default: throw new FormatException(); } } shift = 0; break; case 3: if (cValue < this.TEXT_SHIFT3_SET_CHARS.length) { const textChar = this.TEXT_SHIFT3_SET_CHARS[cValue]; if (upperShift) { result.append(String.fromCharCode(textChar.charCodeAt(0) + 128)); upperShift = false; } else { result.append(textChar); } shift = 0; } else { throw new FormatException(); } break; default: throw new FormatException(); } } } while (bits.available() > 0); } /** * See ISO 16022:2006, 5.2.7 */ static decodeAnsiX12Segment(bits, result) { const cValues = []; do { if (bits.available() === 8) { return; } const firstByte = bits.readBits(8); if (firstByte === 254) { return; } this.parseTwoBytes(firstByte, bits.readBits(8), cValues); for (let i = 0; i < 3; i++) { const cValue = cValues[i]; switch (cValue) { case 0: result.append("\r"); break; case 1: result.append("*"); break; case 2: result.append(">"); break; case 3: result.append(" "); break; default: if (cValue < 14) { result.append(String.fromCharCode(cValue + 44)); } else if (cValue < 40) { result.append(String.fromCharCode(cValue + 51)); } else { throw new FormatException(); } break; } } } while (bits.available() > 0); } static parseTwoBytes(firstByte, secondByte, result) { let fullBitValue = (firstByte << 8) + secondByte - 1; let temp = Math.floor(fullBitValue / 1600); result[0] = temp; fullBitValue -= temp * 1600; temp = Math.floor(fullBitValue / 40); result[1] = temp; result[2] = fullBitValue - temp * 40; } /** * See ISO 16022:2006, 5.2.8 and Annex C Table C.3 */ static decodeEdifactSegment(bits, result) { do { if (bits.available() <= 16) { return; } for (let i = 0; i < 4; i++) { let edifactValue = bits.readBits(6); if (edifactValue === 31) { const bitsLeft = 8 - bits.getBitOffset(); if (bitsLeft !== 8) { bits.readBits(bitsLeft); } return; } if ((edifactValue & 32) === 0) { edifactValue |= 64; } result.append(String.fromCharCode(edifactValue)); } } while (bits.available() > 0); } /** * See ISO 16022:2006, 5.2.9 and Annex B, B.2 */ static decodeBase256Segment(bits, result, byteSegments) { let codewordPosition = 1 + bits.getByteOffset(); const d1 = this.unrandomize255State(bits.readBits(8), codewordPosition++); let count; if (d1 === 0) { count = bits.available() / 8 | 0; } else if (d1 < 250) { count = d1; } else { count = 250 * (d1 - 249) + this.unrandomize255State(bits.readBits(8), codewordPosition++); } if (count < 0) { throw new FormatException(); } const bytes = new Uint8Array(count); for (let i = 0; i < count; i++) { if (bits.available() < 8) { throw new FormatException(); } bytes[i] = this.unrandomize255State(bits.readBits(8), codewordPosition++); } byteSegments.push(bytes); try { result.append(StringEncoding.decode(bytes, StringUtils.ISO88591)); } catch (uee) { throw new IllegalStateException("Platform does not support required encoding: " + uee.message); } } /** * See ISO 16022:2006, Annex B, B.2 */ static unrandomize255State(randomizedBase256Codeword, base256CodewordPosition) { const pseudoRandomNumber = 149 * base256CodewordPosition % 255 + 1; const tempVariable = randomizedBase256Codeword - pseudoRandomNumber; return tempVariable >= 0 ? tempVariable : tempVariable + 256; } } DecodedBitStreamParser.C40_BASIC_SET_CHARS = [ "*", "*", "*", " ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ]; DecodedBitStreamParser.C40_SHIFT2_SET_CHARS = [ "!", '"', "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "<", "=", ">", "?", "@", "[", "\\", "]", "^", "_" ]; DecodedBitStreamParser.TEXT_BASIC_SET_CHARS = [ "*", "*", "*", " ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" ]; DecodedBitStreamParser.TEXT_SHIFT2_SET_CHARS = DecodedBitStreamParser.C40_SHIFT2_SET_CHARS; DecodedBitStreamParser.TEXT_SHIFT3_SET_CHARS = [ "`", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "{", "|", "}", "~", String.fromCharCode(127) ]; class Decoder$1 { constructor() { this.rsDecoder = new ReedSolomonDecoder(GenericGF.DATA_MATRIX_FIELD_256); } /** *Decodes a Data Matrix Code represented as a {@link BitMatrix}. A 1 or "true" is taken * to mean a black module.
* * @param bits booleans representing white/black Data Matrix Code modules * @return text and bytes encoded within the Data Matrix Code * @throws FormatException if the Data Matrix Code cannot be decoded * @throws ChecksumException if error correction fails */ decode(bits) { const parser = new BitMatrixParser(bits); const version = parser.getVersion(); const codewords = parser.readCodewords(); const dataBlocks = DataBlock.getDataBlocks(codewords, version); let totalBytes = 0; for (let db of dataBlocks) { totalBytes += db.getNumDataCodewords(); } const resultBytes = new Uint8Array(totalBytes); const dataBlocksCount = dataBlocks.length; for (let j = 0; j < dataBlocksCount; j++) { const dataBlock = dataBlocks[j]; const codewordBytes = dataBlock.getCodewords(); const numDataCodewords = dataBlock.getNumDataCodewords(); this.correctErrors(codewordBytes, numDataCodewords); for (let i = 0; i < numDataCodewords; i++) { resultBytes[i * dataBlocksCount + j] = codewordBytes[i]; } } return DecodedBitStreamParser.decode(resultBytes); } /** *Given data and error-correction codewords received, possibly corrupted by errors, attempts to * correct the errors in-place using Reed-Solomon error correction.
* * @param codewordBytes data and error correction codewords * @param numDataCodewords number of codewords that are data bytes * @throws ChecksumException if error correction fails */ correctErrors(codewordBytes, numDataCodewords) { const codewordsInts = new Int32Array(codewordBytes); try { this.rsDecoder.decode(codewordsInts, codewordBytes.length - numDataCodewords); } catch (ignored) { throw new ChecksumException(); } for (let i = 0; i < numDataCodewords; i++) { codewordBytes[i] = codewordsInts[i]; } } } class Detector$1 { constructor(image) { this.image = image; this.rectangleDetector = new WhiteRectangleDetector(this.image); } /** *Detects a Data Matrix Code in an image.
* * @return {@link DetectorResult} encapsulating results of detecting a Data Matrix Code * @throws NotFoundException if no Data Matrix Code can be found */ detect() { const cornerPoints = this.rectangleDetector.detect(); let points = this.detectSolid1(cornerPoints); points = this.detectSolid2(points); points[3] = this.correctTopRight(points); if (!points[3]) { throw new NotFoundException(); } points = this.shiftToModuleCenter(points); const topLeft = points[0]; const bottomLeft = points[1]; const bottomRight = points[2]; const topRight = points[3]; let dimensionTop = this.transitionsBetween(topLeft, topRight) + 1; let dimensionRight = this.transitionsBetween(bottomRight, topRight) + 1; if ((dimensionTop & 1) === 1) { dimensionTop += 1; } if ((dimensionRight & 1) === 1) { dimensionRight += 1; } if (4 * dimensionTop < 7 * dimensionRight && 4 * dimensionRight < 7 * dimensionTop) { dimensionTop = dimensionRight = Math.max(dimensionTop, dimensionRight); } let bits = Detector$1.sampleGrid(this.image, topLeft, bottomLeft, bottomRight, topRight, dimensionTop, dimensionRight); return new DetectorResult(bits, [topLeft, bottomLeft, bottomRight, topRight]); } static shiftPoint(point, to, div) { let x = (to.getX() - point.getX()) / (div + 1); let y = (to.getY() - point.getY()) / (div + 1); return new ResultPoint(point.getX() + x, point.getY() + y); } static moveAway(point, fromX, fromY) { let x = point.getX(); let y = point.getY(); if (x < fromX) { x -= 1; } else { x += 1; } if (y < fromY) { y -= 1; } else { y += 1; } return new ResultPoint(x, y); } /** * Detect a solid side which has minimum transition. */ detectSolid1(cornerPoints) { let pointA = cornerPoints[0]; let pointB = cornerPoints[1]; let pointC = cornerPoints[3]; let pointD = cornerPoints[2]; let trAB = this.transitionsBetween(pointA, pointB); let trBC = this.transitionsBetween(pointB, pointC); let trCD = this.transitionsBetween(pointC, pointD); let trDA = this.transitionsBetween(pointD, pointA); let min = trAB; let points = [pointD, pointA, pointB, pointC]; if (min > trBC) { min = trBC; points[0] = pointA; points[1] = pointB; points[2] = pointC; points[3] = pointD; } if (min > trCD) { min = trCD; points[0] = pointB; points[1] = pointC; points[2] = pointD; points[3] = pointA; } if (min > trDA) { points[0] = pointC; points[1] = pointD; points[2] = pointA; points[3] = pointB; } return points; } /** * Detect a second solid side next to first solid side. */ detectSolid2(points) { let pointA = points[0]; let pointB = points[1]; let pointC = points[2]; let pointD = points[3]; let tr = this.transitionsBetween(pointA, pointD); let pointBs = Detector$1.shiftPoint(pointB, pointC, (tr + 1) * 4); let pointCs = Detector$1.shiftPoint(pointC, pointB, (tr + 1) * 4); let trBA = this.transitionsBetween(pointBs, pointA); let trCD = this.transitionsBetween(pointCs, pointD); if (trBA < trCD) { points[0] = pointA; points[1] = pointB; points[2] = pointC; points[3] = pointD; } else { points[0] = pointB; points[1] = pointC; points[2] = pointD; points[3] = pointA; } return points; } /** * Calculates the corner position of the white top right module. */ correctTopRight(points) { let pointA = points[0]; let pointB = points[1]; let pointC = points[2]; let pointD = points[3]; let trTop = this.transitionsBetween(pointA, pointD); let trRight = this.transitionsBetween(pointB, pointD); let pointAs = Detector$1.shiftPoint(pointA, pointB, (trRight + 1) * 4); let pointCs = Detector$1.shiftPoint(pointC, pointB, (trTop + 1) * 4); trTop = this.transitionsBetween(pointAs, pointD); trRight = this.transitionsBetween(pointCs, pointD); let candidate1 = new ResultPoint(pointD.getX() + (pointC.getX() - pointB.getX()) / (trTop + 1), pointD.getY() + (pointC.getY() - pointB.getY()) / (trTop + 1)); let candidate2 = new ResultPoint(pointD.getX() + (pointA.getX() - pointB.getX()) / (trRight + 1), pointD.getY() + (pointA.getY() - pointB.getY()) / (trRight + 1)); if (!this.isValid(candidate1)) { if (this.isValid(candidate2)) { return candidate2; } return null; } if (!this.isValid(candidate2)) { return candidate1; } let sumc1 = this.transitionsBetween(pointAs, candidate1) + this.transitionsBetween(pointCs, candidate1); let sumc2 = this.transitionsBetween(pointAs, candidate2) + this.transitionsBetween(pointCs, candidate2); if (sumc1 > sumc2) { return candidate1; } else { return candidate2; } } /** * Shift the edge points to the module center. */ shiftToModuleCenter(points) { let pointA = points[0]; let pointB = points[1]; let pointC = points[2]; let pointD = points[3]; let dimH = this.transitionsBetween(pointA, pointD) + 1; let dimV = this.transitionsBetween(pointC, pointD) + 1; let pointAs = Detector$1.shiftPoint(pointA, pointB, dimV * 4); let pointCs = Detector$1.shiftPoint(pointC, pointB, dimH * 4); dimH = this.transitionsBetween(pointAs, pointD) + 1; dimV = this.transitionsBetween(pointCs, pointD) + 1; if ((dimH & 1) === 1) { dimH += 1; } if ((dimV & 1) === 1) { dimV += 1; } let centerX = (pointA.getX() + pointB.getX() + pointC.getX() + pointD.getX()) / 4; let centerY = (pointA.getY() + pointB.getY() + pointC.getY() + pointD.getY()) / 4; pointA = Detector$1.moveAway(pointA, centerX, centerY); pointB = Detector$1.moveAway(pointB, centerX, centerY); pointC = Detector$1.moveAway(pointC, centerX, centerY); pointD = Detector$1.moveAway(pointD, centerX, centerY); let pointBs; let pointDs; pointAs = Detector$1.shiftPoint(pointA, pointB, dimV * 4); pointAs = Detector$1.shiftPoint(pointAs, pointD, dimH * 4); pointBs = Detector$1.shiftPoint(pointB, pointA, dimV * 4); pointBs = Detector$1.shiftPoint(pointBs, pointC, dimH * 4); pointCs = Detector$1.shiftPoint(pointC, pointD, dimV * 4); pointCs = Detector$1.shiftPoint(pointCs, pointB, dimH * 4); pointDs = Detector$1.shiftPoint(pointD, pointC, dimV * 4); pointDs = Detector$1.shiftPoint(pointDs, pointA, dimH * 4); return [pointAs, pointBs, pointCs, pointDs]; } isValid(p) { return p.getX() >= 0 && p.getX() < this.image.getWidth() && p.getY() > 0 && p.getY() < this.image.getHeight(); } static sampleGrid(image, topLeft, bottomLeft, bottomRight, topRight, dimensionX, dimensionY) { const sampler = GridSamplerInstance.getInstance(); return sampler.sampleGrid(image, dimensionX, dimensionY, 0.5, 0.5, dimensionX - 0.5, 0.5, dimensionX - 0.5, dimensionY - 0.5, 0.5, dimensionY - 0.5, topLeft.getX(), topLeft.getY(), topRight.getX(), topRight.getY(), bottomRight.getX(), bottomRight.getY(), bottomLeft.getX(), bottomLeft.getY()); } /** * Counts the number of black/white transitions between two points, using something like Bresenham's algorithm. */ transitionsBetween(from, to) { let fromX = Math.trunc(from.getX()); let fromY = Math.trunc(from.getY()); let toX = Math.trunc(to.getX()); let toY = Math.trunc(to.getY()); let steep = Math.abs(toY - fromY) > Math.abs(toX - fromX); if (steep) { let temp = fromX; fromX = fromY; fromY = temp; temp = toX; toX = toY; toY = temp; } let dx = Math.abs(toX - fromX); let dy = Math.abs(toY - fromY); let error = -dx / 2; let ystep = fromY < toY ? 1 : -1; let xstep = fromX < toX ? 1 : -1; let transitions = 0; let inBlack = this.image.get(steep ? fromY : fromX, steep ? fromX : fromY); for (let x = fromX, y = fromY; x !== toX; x += xstep) { let isBlack = this.image.get(steep ? y : x, steep ? x : y); if (isBlack !== inBlack) { transitions++; inBlack = isBlack; } error += dy; if (error > 0) { if (y === toY) { break; } y += ystep; error -= dx; } } return transitions; } } class DataMatrixReader { constructor() { this.decoder = new Decoder$1(); } /** * Locates and decodes a Data Matrix code in an image. * * @return a String representing the content encoded by the Data Matrix code * @throws NotFoundException if a Data Matrix code cannot be found * @throws FormatException if a Data Matrix code cannot be decoded * @throws ChecksumException if error correction fails */ // @Override // public Result decode(BinaryBitmap image) throws NotFoundException, ChecksumException, FormatException { // return decode(image, null); // } // @Override decode(image, hints = null) { let decoderResult; let points; if (hints != null && hints.has(DecodeHintType$1.PURE_BARCODE)) { const bits = DataMatrixReader.extractPureBits(image.getBlackMatrix()); decoderResult = this.decoder.decode(bits); points = DataMatrixReader.NO_POINTS; } else { const detectorResult = new Detector$1(image.getBlackMatrix()).detect(); decoderResult = this.decoder.decode(detectorResult.getBits()); points = detectorResult.getPoints(); } const rawBytes = decoderResult.getRawBytes(); const result = new Result(decoderResult.getText(), rawBytes, 8 * rawBytes.length, points, BarcodeFormat$1.DATA_MATRIX, System.currentTimeMillis()); const byteSegments = decoderResult.getByteSegments(); if (byteSegments != null) { result.putMetadata(ResultMetadataType$1.BYTE_SEGMENTS, byteSegments); } const ecLevel = decoderResult.getECLevel(); if (ecLevel != null) { result.putMetadata(ResultMetadataType$1.ERROR_CORRECTION_LEVEL, ecLevel); } return result; } // @Override reset() { } /** * This method detects a code in a "pure" image -- that is, pure monochrome image * which contains only an unrotated, unskewed, image of a code, with some white border * around it. This is a specialized method that works exceptionally fast in this special * case. * * @see com.google.zxing.qrcode.QRCodeReader#extractPureBits(BitMatrix) */ static extractPureBits(image) { const leftTopBlack = image.getTopLeftOnBit(); const rightBottomBlack = image.getBottomRightOnBit(); if (leftTopBlack == null || rightBottomBlack == null) { throw new NotFoundException(); } const moduleSize = this.moduleSize(leftTopBlack, image); let top = leftTopBlack[1]; const bottom = rightBottomBlack[1]; let left = leftTopBlack[0]; const right = rightBottomBlack[0]; const matrixWidth = (right - left + 1) / moduleSize; const matrixHeight = (bottom - top + 1) / moduleSize; if (matrixWidth <= 0 || matrixHeight <= 0) { throw new NotFoundException(); } const nudge = moduleSize / 2; top += nudge; left += nudge; const bits = new BitMatrix(matrixWidth, matrixHeight); for (let y = 0; y < matrixHeight; y++) { const iOffset = top + y * moduleSize; for (let x = 0; x < matrixWidth; x++) { if (image.get(left + x * moduleSize, iOffset)) { bits.set(x, y); } } } return bits; } static moduleSize(leftTopBlack, image) { const width = image.getWidth(); let x = leftTopBlack[0]; const y = leftTopBlack[1]; while (x < width && image.get(x, y)) { x++; } if (x === width) { throw new NotFoundException(); } const moduleSize = x - leftTopBlack[0]; if (moduleSize === 0) { throw new NotFoundException(); } return moduleSize; } } DataMatrixReader.NO_POINTS = []; class BrowserDatamatrixCodeReader extends BrowserCodeReader { /** * Creates an instance of BrowserQRCodeReader. * @param {number} [timeBetweenScansMillis=500] the time delay between subsequent decode tries */ constructor(timeBetweenScansMillis = 500) { super(new DataMatrixReader(), timeBetweenScansMillis); } } var ErrorCorrectionLevelValues; (function(ErrorCorrectionLevelValues2) { ErrorCorrectionLevelValues2[ErrorCorrectionLevelValues2["L"] = 0] = "L"; ErrorCorrectionLevelValues2[ErrorCorrectionLevelValues2["M"] = 1] = "M"; ErrorCorrectionLevelValues2[ErrorCorrectionLevelValues2["Q"] = 2] = "Q"; ErrorCorrectionLevelValues2[ErrorCorrectionLevelValues2["H"] = 3] = "H"; })(ErrorCorrectionLevelValues || (ErrorCorrectionLevelValues = {})); class ErrorCorrectionLevel { constructor(value, stringValue, bits) { this.value = value; this.stringValue = stringValue; this.bits = bits; ErrorCorrectionLevel.FOR_BITS.set(bits, this); ErrorCorrectionLevel.FOR_VALUE.set(value, this); } getValue() { return this.value; } getBits() { return this.bits; } static fromString(s) { switch (s) { case "L": return ErrorCorrectionLevel.L; case "M": return ErrorCorrectionLevel.M; case "Q": return ErrorCorrectionLevel.Q; case "H": return ErrorCorrectionLevel.H; default: throw new ArgumentException(s + "not available"); } } toString() { return this.stringValue; } equals(o) { if (!(o instanceof ErrorCorrectionLevel)) { return false; } const other = o; return this.value === other.value; } /** * @param bits int containing the two bits encoding a QR Code's error correction level * @return ErrorCorrectionLevel representing the encoded error correction level */ static forBits(bits) { if (bits < 0 || bits >= ErrorCorrectionLevel.FOR_BITS.size) { throw new IllegalArgumentException(); } return ErrorCorrectionLevel.FOR_BITS.get(bits); } } ErrorCorrectionLevel.FOR_BITS = /* @__PURE__ */ new Map(); ErrorCorrectionLevel.FOR_VALUE = /* @__PURE__ */ new Map(); ErrorCorrectionLevel.L = new ErrorCorrectionLevel(ErrorCorrectionLevelValues.L, "L", 1); ErrorCorrectionLevel.M = new ErrorCorrectionLevel(ErrorCorrectionLevelValues.M, "M", 0); ErrorCorrectionLevel.Q = new ErrorCorrectionLevel(ErrorCorrectionLevelValues.Q, "Q", 3); ErrorCorrectionLevel.H = new ErrorCorrectionLevel(ErrorCorrectionLevelValues.H, "H", 2); class FormatInformation { constructor(formatInfo) { this.errorCorrectionLevel = ErrorCorrectionLevel.forBits(formatInfo >> 3 & 3); this.dataMask = /*(byte) */ formatInfo & 7; } static numBitsDiffering(a, b) { return Integer.bitCount(a ^ b); } /** * @param maskedFormatInfo1 format info indicator, with mask still applied * @param maskedFormatInfo2 second copy of same info; both are checked at the same time * to establish best match * @return information about the format it specifies, or {@code null} * if doesn't seem to match any known pattern */ static decodeFormatInformation(maskedFormatInfo1, maskedFormatInfo2) { const formatInfo = FormatInformation.doDecodeFormatInformation(maskedFormatInfo1, maskedFormatInfo2); if (formatInfo !== null) { return formatInfo; } return FormatInformation.doDecodeFormatInformation(maskedFormatInfo1 ^ FormatInformation.FORMAT_INFO_MASK_QR, maskedFormatInfo2 ^ FormatInformation.FORMAT_INFO_MASK_QR); } static doDecodeFormatInformation(maskedFormatInfo1, maskedFormatInfo2) { let bestDifference = Number.MAX_SAFE_INTEGER; let bestFormatInfo = 0; for (const decodeInfo of FormatInformation.FORMAT_INFO_DECODE_LOOKUP) { const targetInfo = decodeInfo[0]; if (targetInfo === maskedFormatInfo1 || targetInfo === maskedFormatInfo2) { return new FormatInformation(decodeInfo[1]); } let bitsDifference = FormatInformation.numBitsDiffering(maskedFormatInfo1, targetInfo); if (bitsDifference < bestDifference) { bestFormatInfo = decodeInfo[1]; bestDifference = bitsDifference; } if (maskedFormatInfo1 !== maskedFormatInfo2) { bitsDifference = FormatInformation.numBitsDiffering(maskedFormatInfo2, targetInfo); if (bitsDifference < bestDifference) { bestFormatInfo = decodeInfo[1]; bestDifference = bitsDifference; } } } if (bestDifference <= 3) { return new FormatInformation(bestFormatInfo); } return null; } getErrorCorrectionLevel() { return this.errorCorrectionLevel; } getDataMask() { return this.dataMask; } /*@Override*/ hashCode() { return this.errorCorrectionLevel.getBits() << 3 | this.dataMask; } /*@Override*/ equals(o) { if (!(o instanceof FormatInformation)) { return false; } const other = o; return this.errorCorrectionLevel === other.errorCorrectionLevel && this.dataMask === other.dataMask; } } FormatInformation.FORMAT_INFO_MASK_QR = 21522; FormatInformation.FORMAT_INFO_DECODE_LOOKUP = [ Int32Array.from([21522, 0]), Int32Array.from([20773, 1]), Int32Array.from([24188, 2]), Int32Array.from([23371, 3]), Int32Array.from([17913, 4]), Int32Array.from([16590, 5]), Int32Array.from([20375, 6]), Int32Array.from([19104, 7]), Int32Array.from([30660, 8]), Int32Array.from([29427, 9]), Int32Array.from([32170, 10]), Int32Array.from([30877, 11]), Int32Array.from([26159, 12]), Int32Array.from([25368, 13]), Int32Array.from([27713, 14]), Int32Array.from([26998, 15]), Int32Array.from([5769, 16]), Int32Array.from([5054, 17]), Int32Array.from([7399, 18]), Int32Array.from([6608, 19]), Int32Array.from([1890, 20]), Int32Array.from([597, 21]), Int32Array.from([3340, 22]), Int32Array.from([2107, 23]), Int32Array.from([13663, 24]), Int32Array.from([12392, 25]), Int32Array.from([16177, 26]), Int32Array.from([14854, 27]), Int32Array.from([9396, 28]), Int32Array.from([8579, 29]), Int32Array.from([11994, 30]), Int32Array.from([11245, 31]) ]; class ECBlocks$1 { constructor(ecCodewordsPerBlock, ...ecBlocks) { this.ecCodewordsPerBlock = ecCodewordsPerBlock; this.ecBlocks = ecBlocks; } getECCodewordsPerBlock() { return this.ecCodewordsPerBlock; } getNumBlocks() { let total = 0; const ecBlocks = this.ecBlocks; for (const ecBlock of ecBlocks) { total += ecBlock.getCount(); } return total; } getTotalECCodewords() { return this.ecCodewordsPerBlock * this.getNumBlocks(); } getECBlocks() { return this.ecBlocks; } } class ECB$1 { constructor(count, dataCodewords) { this.count = count; this.dataCodewords = dataCodewords; } getCount() { return this.count; } getDataCodewords() { return this.dataCodewords; } } class Version$1 { constructor(versionNumber, alignmentPatternCenters, ...ecBlocks) { this.versionNumber = versionNumber; this.alignmentPatternCenters = alignmentPatternCenters; this.ecBlocks = ecBlocks; let total = 0; const ecCodewords = ecBlocks[0].getECCodewordsPerBlock(); const ecbArray = ecBlocks[0].getECBlocks(); for (const ecBlock of ecbArray) { total += ecBlock.getCount() * (ecBlock.getDataCodewords() + ecCodewords); } this.totalCodewords = total; } getVersionNumber() { return this.versionNumber; } getAlignmentPatternCenters() { return this.alignmentPatternCenters; } getTotalCodewords() { return this.totalCodewords; } getDimensionForVersion() { return 17 + 4 * this.versionNumber; } getECBlocksForLevel(ecLevel) { return this.ecBlocks[ecLevel.getValue()]; } /** *Deduces version information purely from QR Code dimensions.
* * @param dimension dimension in modules * @return Version for a QR Code of that dimension * @throws FormatException if dimension is not 1 mod 4 */ static getProvisionalVersionForDimension(dimension) { if (dimension % 4 !== 1) { throw new FormatException(); } try { return this.getVersionForNumber((dimension - 17) / 4); } catch (ignored) { throw new FormatException(); } } static getVersionForNumber(versionNumber) { if (versionNumber < 1 || versionNumber > 40) { throw new IllegalArgumentException(); } return Version$1.VERSIONS[versionNumber - 1]; } static decodeVersionInformation(versionBits) { let bestDifference = Number.MAX_SAFE_INTEGER; let bestVersion = 0; for (let i = 0; i < Version$1.VERSION_DECODE_INFO.length; i++) { const targetVersion = Version$1.VERSION_DECODE_INFO[i]; if (targetVersion === versionBits) { return Version$1.getVersionForNumber(i + 7); } const bitsDifference = FormatInformation.numBitsDiffering(versionBits, targetVersion); if (bitsDifference < bestDifference) { bestVersion = i + 7; bestDifference = bitsDifference; } } if (bestDifference <= 3) { return Version$1.getVersionForNumber(bestVersion); } return null; } /** * See ISO 18004:2006 Annex E */ buildFunctionPattern() { const dimension = this.getDimensionForVersion(); const bitMatrix = new BitMatrix(dimension); bitMatrix.setRegion(0, 0, 9, 9); bitMatrix.setRegion(dimension - 8, 0, 8, 9); bitMatrix.setRegion(0, dimension - 8, 9, 8); const max = this.alignmentPatternCenters.length; for (let x = 0; x < max; x++) { const i = this.alignmentPatternCenters[x] - 2; for (let y = 0; y < max; y++) { if (x === 0 && (y === 0 || y === max - 1) || x === max - 1 && y === 0) { continue; } bitMatrix.setRegion(this.alignmentPatternCenters[y] - 2, i, 5, 5); } } bitMatrix.setRegion(6, 9, 1, dimension - 17); bitMatrix.setRegion(9, 6, dimension - 17, 1); if (this.versionNumber > 6) { bitMatrix.setRegion(dimension - 11, 0, 3, 6); bitMatrix.setRegion(0, dimension - 11, 6, 3); } return bitMatrix; } /*@Override*/ toString() { return "" + this.versionNumber; } } Version$1.VERSION_DECODE_INFO = Int32Array.from([ 31892, 34236, 39577, 42195, 48118, 51042, 55367, 58893, 63784, 68472, 70749, 76311, 79154, 84390, 87683, 92361, 96236, 102084, 102881, 110507, 110734, 117786, 119615, 126325, 127568, 133589, 136944, 141498, 145311, 150283, 152622, 158308, 161089, 167017 ]); Version$1.VERSIONS = [ new Version$1(1, new Int32Array(0), new ECBlocks$1(7, new ECB$1(1, 19)), new ECBlocks$1(10, new ECB$1(1, 16)), new ECBlocks$1(13, new ECB$1(1, 13)), new ECBlocks$1(17, new ECB$1(1, 9))), new Version$1(2, Int32Array.from([6, 18]), new ECBlocks$1(10, new ECB$1(1, 34)), new ECBlocks$1(16, new ECB$1(1, 28)), new ECBlocks$1(22, new ECB$1(1, 22)), new ECBlocks$1(28, new ECB$1(1, 16))), new Version$1(3, Int32Array.from([6, 22]), new ECBlocks$1(15, new ECB$1(1, 55)), new ECBlocks$1(26, new ECB$1(1, 44)), new ECBlocks$1(18, new ECB$1(2, 17)), new ECBlocks$1(22, new ECB$1(2, 13))), new Version$1(4, Int32Array.from([6, 26]), new ECBlocks$1(20, new ECB$1(1, 80)), new ECBlocks$1(18, new ECB$1(2, 32)), new ECBlocks$1(26, new ECB$1(2, 24)), new ECBlocks$1(16, new ECB$1(4, 9))), new Version$1(5, Int32Array.from([6, 30]), new ECBlocks$1(26, new ECB$1(1, 108)), new ECBlocks$1(24, new ECB$1(2, 43)), new ECBlocks$1(18, new ECB$1(2, 15), new ECB$1(2, 16)), new ECBlocks$1(22, new ECB$1(2, 11), new ECB$1(2, 12))), new Version$1(6, Int32Array.from([6, 34]), new ECBlocks$1(18, new ECB$1(2, 68)), new ECBlocks$1(16, new ECB$1(4, 27)), new ECBlocks$1(24, new ECB$1(4, 19)), new ECBlocks$1(28, new ECB$1(4, 15))), new Version$1(7, Int32Array.from([6, 22, 38]), new ECBlocks$1(20, new ECB$1(2, 78)), new ECBlocks$1(18, new ECB$1(4, 31)), new ECBlocks$1(18, new ECB$1(2, 14), new ECB$1(4, 15)), new ECBlocks$1(26, new ECB$1(4, 13), new ECB$1(1, 14))), new Version$1(8, Int32Array.from([6, 24, 42]), new ECBlocks$1(24, new ECB$1(2, 97)), new ECBlocks$1(22, new ECB$1(2, 38), new ECB$1(2, 39)), new ECBlocks$1(22, new ECB$1(4, 18), new ECB$1(2, 19)), new ECBlocks$1(26, new ECB$1(4, 14), new ECB$1(2, 15))), new Version$1(9, Int32Array.from([6, 26, 46]), new ECBlocks$1(30, new ECB$1(2, 116)), new ECBlocks$1(22, new ECB$1(3, 36), new ECB$1(2, 37)), new ECBlocks$1(20, new ECB$1(4, 16), new ECB$1(4, 17)), new ECBlocks$1(24, new ECB$1(4, 12), new ECB$1(4, 13))), new Version$1(10, Int32Array.from([6, 28, 50]), new ECBlocks$1(18, new ECB$1(2, 68), new ECB$1(2, 69)), new ECBlocks$1(26, new ECB$1(4, 43), new ECB$1(1, 44)), new ECBlocks$1(24, new ECB$1(6, 19), new ECB$1(2, 20)), new ECBlocks$1(28, new ECB$1(6, 15), new ECB$1(2, 16))), new Version$1(11, Int32Array.from([6, 30, 54]), new ECBlocks$1(20, new ECB$1(4, 81)), new ECBlocks$1(30, new ECB$1(1, 50), new ECB$1(4, 51)), new ECBlocks$1(28, new ECB$1(4, 22), new ECB$1(4, 23)), new ECBlocks$1(24, new ECB$1(3, 12), new ECB$1(8, 13))), new Version$1(12, Int32Array.from([6, 32, 58]), new ECBlocks$1(24, new ECB$1(2, 92), new ECB$1(2, 93)), new ECBlocks$1(22, new ECB$1(6, 36), new ECB$1(2, 37)), new ECBlocks$1(26, new ECB$1(4, 20), new ECB$1(6, 21)), new ECBlocks$1(28, new ECB$1(7, 14), new ECB$1(4, 15))), new Version$1(13, Int32Array.from([6, 34, 62]), new ECBlocks$1(26, new ECB$1(4, 107)), new ECBlocks$1(22, new ECB$1(8, 37), new ECB$1(1, 38)), new ECBlocks$1(24, new ECB$1(8, 20), new ECB$1(4, 21)), new ECBlocks$1(22, new ECB$1(12, 11), new ECB$1(4, 12))), new Version$1(14, Int32Array.from([6, 26, 46, 66]), new ECBlocks$1(30, new ECB$1(3, 115), new ECB$1(1, 116)), new ECBlocks$1(24, new ECB$1(4, 40), new ECB$1(5, 41)), new ECBlocks$1(20, new ECB$1(11, 16), new ECB$1(5, 17)), new ECBlocks$1(24, new ECB$1(11, 12), new ECB$1(5, 13))), new Version$1(15, Int32Array.from([6, 26, 48, 70]), new ECBlocks$1(22, new ECB$1(5, 87), new ECB$1(1, 88)), new ECBlocks$1(24, new ECB$1(5, 41), new ECB$1(5, 42)), new ECBlocks$1(30, new ECB$1(5, 24), new ECB$1(7, 25)), new ECBlocks$1(24, new ECB$1(11, 12), new ECB$1(7, 13))), new Version$1(16, Int32Array.from([6, 26, 50, 74]), new ECBlocks$1(24, new ECB$1(5, 98), new ECB$1(1, 99)), new ECBlocks$1(28, new ECB$1(7, 45), new ECB$1(3, 46)), new ECBlocks$1(24, new ECB$1(15, 19), new ECB$1(2, 20)), new ECBlocks$1(30, new ECB$1(3, 15), new ECB$1(13, 16))), new Version$1(17, Int32Array.from([6, 30, 54, 78]), new ECBlocks$1(28, new ECB$1(1, 107), new ECB$1(5, 108)), new ECBlocks$1(28, new ECB$1(10, 46), new ECB$1(1, 47)), new ECBlocks$1(28, new ECB$1(1, 22), new ECB$1(15, 23)), new ECBlocks$1(28, new ECB$1(2, 14), new ECB$1(17, 15))), new Version$1(18, Int32Array.from([6, 30, 56, 82]), new ECBlocks$1(30, new ECB$1(5, 120), new ECB$1(1, 121)), new ECBlocks$1(26, new ECB$1(9, 43), new ECB$1(4, 44)), new ECBlocks$1(28, new ECB$1(17, 22), new ECB$1(1, 23)), new ECBlocks$1(28, new ECB$1(2, 14), new ECB$1(19, 15))), new Version$1(19, Int32Array.from([6, 30, 58, 86]), new ECBlocks$1(28, new ECB$1(3, 113), new ECB$1(4, 114)), new ECBlocks$1(26, new ECB$1(3, 44), new ECB$1(11, 45)), new ECBlocks$1(26, new ECB$1(17, 21), new ECB$1(4, 22)), new ECBlocks$1(26, new ECB$1(9, 13), new ECB$1(16, 14))), new Version$1(20, Int32Array.from([6, 34, 62, 90]), new ECBlocks$1(28, new ECB$1(3, 107), new ECB$1(5, 108)), new ECBlocks$1(26, new ECB$1(3, 41), new ECB$1(13, 42)), new ECBlocks$1(30, new ECB$1(15, 24), new ECB$1(5, 25)), new ECBlocks$1(28, new ECB$1(15, 15), new ECB$1(10, 16))), new Version$1(21, Int32Array.from([6, 28, 50, 72, 94]), new ECBlocks$1(28, new ECB$1(4, 116), new ECB$1(4, 117)), new ECBlocks$1(26, new ECB$1(17, 42)), new ECBlocks$1(28, new ECB$1(17, 22), new ECB$1(6, 23)), new ECBlocks$1(30, new ECB$1(19, 16), new ECB$1(6, 17))), new Version$1(22, Int32Array.from([6, 26, 50, 74, 98]), new ECBlocks$1(28, new ECB$1(2, 111), new ECB$1(7, 112)), new ECBlocks$1(28, new ECB$1(17, 46)), new ECBlocks$1(30, new ECB$1(7, 24), new ECB$1(16, 25)), new ECBlocks$1(24, new ECB$1(34, 13))), new Version$1(23, Int32Array.from([6, 30, 54, 78, 102]), new ECBlocks$1(30, new ECB$1(4, 121), new ECB$1(5, 122)), new ECBlocks$1(28, new ECB$1(4, 47), new ECB$1(14, 48)), new ECBlocks$1(30, new ECB$1(11, 24), new ECB$1(14, 25)), new ECBlocks$1(30, new ECB$1(16, 15), new ECB$1(14, 16))), new Version$1(24, Int32Array.from([6, 28, 54, 80, 106]), new ECBlocks$1(30, new ECB$1(6, 117), new ECB$1(4, 118)), new ECBlocks$1(28, new ECB$1(6, 45), new ECB$1(14, 46)), new ECBlocks$1(30, new ECB$1(11, 24), new ECB$1(16, 25)), new ECBlocks$1(30, new ECB$1(30, 16), new ECB$1(2, 17))), new Version$1(25, Int32Array.from([6, 32, 58, 84, 110]), new ECBlocks$1(26, new ECB$1(8, 106), new ECB$1(4, 107)), new ECBlocks$1(28, new ECB$1(8, 47), new ECB$1(13, 48)), new ECBlocks$1(30, new ECB$1(7, 24), new ECB$1(22, 25)), new ECBlocks$1(30, new ECB$1(22, 15), new ECB$1(13, 16))), new Version$1(26, Int32Array.from([6, 30, 58, 86, 114]), new ECBlocks$1(28, new ECB$1(10, 114), new ECB$1(2, 115)), new ECBlocks$1(28, new ECB$1(19, 46), new ECB$1(4, 47)), new ECBlocks$1(28, new ECB$1(28, 22), new ECB$1(6, 23)), new ECBlocks$1(30, new ECB$1(33, 16), new ECB$1(4, 17))), new Version$1(27, Int32Array.from([6, 34, 62, 90, 118]), new ECBlocks$1(30, new ECB$1(8, 122), new ECB$1(4, 123)), new ECBlocks$1(28, new ECB$1(22, 45), new ECB$1(3, 46)), new ECBlocks$1(30, new ECB$1(8, 23), new ECB$1(26, 24)), new ECBlocks$1(30, new ECB$1(12, 15), new ECB$1(28, 16))), new Version$1(28, Int32Array.from([6, 26, 50, 74, 98, 122]), new ECBlocks$1(30, new ECB$1(3, 117), new ECB$1(10, 118)), new ECBlocks$1(28, new ECB$1(3, 45), new ECB$1(23, 46)), new ECBlocks$1(30, new ECB$1(4, 24), new ECB$1(31, 25)), new ECBlocks$1(30, new ECB$1(11, 15), new ECB$1(31, 16))), new Version$1(29, Int32Array.from([6, 30, 54, 78, 102, 126]), new ECBlocks$1(30, new ECB$1(7, 116), new ECB$1(7, 117)), new ECBlocks$1(28, new ECB$1(21, 45), new ECB$1(7, 46)), new ECBlocks$1(30, new ECB$1(1, 23), new ECB$1(37, 24)), new ECBlocks$1(30, new ECB$1(19, 15), new ECB$1(26, 16))), new Version$1(30, Int32Array.from([6, 26, 52, 78, 104, 130]), new ECBlocks$1(30, new ECB$1(5, 115), new ECB$1(10, 116)), new ECBlocks$1(28, new ECB$1(19, 47), new ECB$1(10, 48)), new ECBlocks$1(30, new ECB$1(15, 24), new ECB$1(25, 25)), new ECBlocks$1(30, new ECB$1(23, 15), new ECB$1(25, 16))), new Version$1(31, Int32Array.from([6, 30, 56, 82, 108, 134]), new ECBlocks$1(30, new ECB$1(13, 115), new ECB$1(3, 116)), new ECBlocks$1(28, new ECB$1(2, 46), new ECB$1(29, 47)), new ECBlocks$1(30, new ECB$1(42, 24), new ECB$1(1, 25)), new ECBlocks$1(30, new ECB$1(23, 15), new ECB$1(28, 16))), new Version$1(32, Int32Array.from([6, 34, 60, 86, 112, 138]), new ECBlocks$1(30, new ECB$1(17, 115)), new ECBlocks$1(28, new ECB$1(10, 46), new ECB$1(23, 47)), new ECBlocks$1(30, new ECB$1(10, 24), new ECB$1(35, 25)), new ECBlocks$1(30, new ECB$1(19, 15), new ECB$1(35, 16))), new Version$1(33, Int32Array.from([6, 30, 58, 86, 114, 142]), new ECBlocks$1(30, new ECB$1(17, 115), new ECB$1(1, 116)), new ECBlocks$1(28, new ECB$1(14, 46), new ECB$1(21, 47)), new ECBlocks$1(30, new ECB$1(29, 24), new ECB$1(19, 25)), new ECBlocks$1(30, new ECB$1(11, 15), new ECB$1(46, 16))), new Version$1(34, Int32Array.from([6, 34, 62, 90, 118, 146]), new ECBlocks$1(30, new ECB$1(13, 115), new ECB$1(6, 116)), new ECBlocks$1(28, new ECB$1(14, 46), new ECB$1(23, 47)), new ECBlocks$1(30, new ECB$1(44, 24), new ECB$1(7, 25)), new ECBlocks$1(30, new ECB$1(59, 16), new ECB$1(1, 17))), new Version$1(35, Int32Array.from([6, 30, 54, 78, 102, 126, 150]), new ECBlocks$1(30, new ECB$1(12, 121), new ECB$1(7, 122)), new ECBlocks$1(28, new ECB$1(12, 47), new ECB$1(26, 48)), new ECBlocks$1(30, new ECB$1(39, 24), new ECB$1(14, 25)), new ECBlocks$1(30, new ECB$1(22, 15), new ECB$1(41, 16))), new Version$1(36, Int32Array.from([6, 24, 50, 76, 102, 128, 154]), new ECBlocks$1(30, new ECB$1(6, 121), new ECB$1(14, 122)), new ECBlocks$1(28, new ECB$1(6, 47), new ECB$1(34, 48)), new ECBlocks$1(30, new ECB$1(46, 24), new ECB$1(10, 25)), new ECBlocks$1(30, new ECB$1(2, 15), new ECB$1(64, 16))), new Version$1(37, Int32Array.from([6, 28, 54, 80, 106, 132, 158]), new ECBlocks$1(30, new ECB$1(17, 122), new ECB$1(4, 123)), new ECBlocks$1(28, new ECB$1(29, 46), new ECB$1(14, 47)), new ECBlocks$1(30, new ECB$1(49, 24), new ECB$1(10, 25)), new ECBlocks$1(30, new ECB$1(24, 15), new ECB$1(46, 16))), new Version$1(38, Int32Array.from([6, 32, 58, 84, 110, 136, 162]), new ECBlocks$1(30, new ECB$1(4, 122), new ECB$1(18, 123)), new ECBlocks$1(28, new ECB$1(13, 46), new ECB$1(32, 47)), new ECBlocks$1(30, new ECB$1(48, 24), new ECB$1(14, 25)), new ECBlocks$1(30, new ECB$1(42, 15), new ECB$1(32, 16))), new Version$1(39, Int32Array.from([6, 26, 54, 82, 110, 138, 166]), new ECBlocks$1(30, new ECB$1(20, 117), new ECB$1(4, 118)), new ECBlocks$1(28, new ECB$1(40, 47), new ECB$1(7, 48)), new ECBlocks$1(30, new ECB$1(43, 24), new ECB$1(22, 25)), new ECBlocks$1(30, new ECB$1(10, 15), new ECB$1(67, 16))), new Version$1(40, Int32Array.from([6, 30, 58, 86, 114, 142, 170]), new ECBlocks$1(30, new ECB$1(19, 118), new ECB$1(6, 119)), new ECBlocks$1(28, new ECB$1(18, 47), new ECB$1(31, 48)), new ECBlocks$1(30, new ECB$1(34, 24), new ECB$1(34, 25)), new ECBlocks$1(30, new ECB$1(20, 15), new ECB$1(61, 16))) ]; var DataMaskValues; (function(DataMaskValues2) { DataMaskValues2[DataMaskValues2["DATA_MASK_000"] = 0] = "DATA_MASK_000"; DataMaskValues2[DataMaskValues2["DATA_MASK_001"] = 1] = "DATA_MASK_001"; DataMaskValues2[DataMaskValues2["DATA_MASK_010"] = 2] = "DATA_MASK_010"; DataMaskValues2[DataMaskValues2["DATA_MASK_011"] = 3] = "DATA_MASK_011"; DataMaskValues2[DataMaskValues2["DATA_MASK_100"] = 4] = "DATA_MASK_100"; DataMaskValues2[DataMaskValues2["DATA_MASK_101"] = 5] = "DATA_MASK_101"; DataMaskValues2[DataMaskValues2["DATA_MASK_110"] = 6] = "DATA_MASK_110"; DataMaskValues2[DataMaskValues2["DATA_MASK_111"] = 7] = "DATA_MASK_111"; })(DataMaskValues || (DataMaskValues = {})); class DataMask { // See ISO 18004:2006 6.8.1 constructor(value, isMasked) { this.value = value; this.isMasked = isMasked; } // End of enum constants. /** *Implementations of this method reverse the data masking process applied to a QR Code and * make its bits ready to read.
* * @param bits representation of QR Code bits * @param dimension dimension of QR Code, represented by bits, being unmasked */ unmaskBitMatrix(bits, dimension) { for (let i = 0; i < dimension; i++) { for (let j = 0; j < dimension; j++) { if (this.isMasked(i, j)) { bits.flip(j, i); } } } } } DataMask.values = /* @__PURE__ */ new Map([ /** * 000: mask bits for which (x + y) mod 2 == 0 */ [DataMaskValues.DATA_MASK_000, new DataMask(DataMaskValues.DATA_MASK_000, (i, j) => { return (i + j & 1) === 0; })], /** * 001: mask bits for which x mod 2 == 0 */ [DataMaskValues.DATA_MASK_001, new DataMask(DataMaskValues.DATA_MASK_001, (i, j) => { return (i & 1) === 0; })], /** * 010: mask bits for which y mod 3 == 0 */ [DataMaskValues.DATA_MASK_010, new DataMask(DataMaskValues.DATA_MASK_010, (i, j) => { return j % 3 === 0; })], /** * 011: mask bits for which (x + y) mod 3 == 0 */ [DataMaskValues.DATA_MASK_011, new DataMask(DataMaskValues.DATA_MASK_011, (i, j) => { return (i + j) % 3 === 0; })], /** * 100: mask bits for which (x/2 + y/3) mod 2 == 0 */ [DataMaskValues.DATA_MASK_100, new DataMask(DataMaskValues.DATA_MASK_100, (i, j) => { return (Math.floor(i / 2) + Math.floor(j / 3) & 1) === 0; })], /** * 101: mask bits for which xy mod 2 + xy mod 3 == 0 * equivalently, such that xy mod 6 == 0 */ [DataMaskValues.DATA_MASK_101, new DataMask(DataMaskValues.DATA_MASK_101, (i, j) => { return i * j % 6 === 0; })], /** * 110: mask bits for which (xy mod 2 + xy mod 3) mod 2 == 0 * equivalently, such that xy mod 6 < 3 */ [DataMaskValues.DATA_MASK_110, new DataMask(DataMaskValues.DATA_MASK_110, (i, j) => { return i * j % 6 < 3; })], /** * 111: mask bits for which ((x+y)mod 2 + xy mod 3) mod 2 == 0 * equivalently, such that (x + y + xy mod 3) mod 2 == 0 */ [DataMaskValues.DATA_MASK_111, new DataMask(DataMaskValues.DATA_MASK_111, (i, j) => { return (i + j + i * j % 3 & 1) === 0; })] ]); class BitMatrixParser$1 { /** * @param bitMatrix {@link BitMatrix} to parse * @throws FormatException if dimension is not >= 21 and 1 mod 4 */ constructor(bitMatrix) { const dimension = bitMatrix.getHeight(); if (dimension < 21 || (dimension & 3) !== 1) { throw new FormatException(); } this.bitMatrix = bitMatrix; } /** *Reads format information from one of its two locations within the QR Code.
* * @return {@link FormatInformation} encapsulating the QR Code's format info * @throws FormatException if both format information locations cannot be parsed as * the valid encoding of format information */ readFormatInformation() { if (this.parsedFormatInfo !== null && this.parsedFormatInfo !== void 0) { return this.parsedFormatInfo; } let formatInfoBits1 = 0; for (let i = 0; i < 6; i++) { formatInfoBits1 = this.copyBit(i, 8, formatInfoBits1); } formatInfoBits1 = this.copyBit(7, 8, formatInfoBits1); formatInfoBits1 = this.copyBit(8, 8, formatInfoBits1); formatInfoBits1 = this.copyBit(8, 7, formatInfoBits1); for (let j = 5; j >= 0; j--) { formatInfoBits1 = this.copyBit(8, j, formatInfoBits1); } const dimension = this.bitMatrix.getHeight(); let formatInfoBits2 = 0; const jMin = dimension - 7; for (let j = dimension - 1; j >= jMin; j--) { formatInfoBits2 = this.copyBit(8, j, formatInfoBits2); } for (let i = dimension - 8; i < dimension; i++) { formatInfoBits2 = this.copyBit(i, 8, formatInfoBits2); } this.parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits1, formatInfoBits2); if (this.parsedFormatInfo !== null) { return this.parsedFormatInfo; } throw new FormatException(); } /** *Reads version information from one of its two locations within the QR Code.
* * @return {@link Version} encapsulating the QR Code's version * @throws FormatException if both version information locations cannot be parsed as * the valid encoding of version information */ readVersion() { if (this.parsedVersion !== null && this.parsedVersion !== void 0) { return this.parsedVersion; } const dimension = this.bitMatrix.getHeight(); const provisionalVersion = Math.floor((dimension - 17) / 4); if (provisionalVersion <= 6) { return Version$1.getVersionForNumber(provisionalVersion); } let versionBits = 0; const ijMin = dimension - 11; for (let j = 5; j >= 0; j--) { for (let i = dimension - 9; i >= ijMin; i--) { versionBits = this.copyBit(i, j, versionBits); } } let theParsedVersion = Version$1.decodeVersionInformation(versionBits); if (theParsedVersion !== null && theParsedVersion.getDimensionForVersion() === dimension) { this.parsedVersion = theParsedVersion; return theParsedVersion; } versionBits = 0; for (let i = 5; i >= 0; i--) { for (let j = dimension - 9; j >= ijMin; j--) { versionBits = this.copyBit(i, j, versionBits); } } theParsedVersion = Version$1.decodeVersionInformation(versionBits); if (theParsedVersion !== null && theParsedVersion.getDimensionForVersion() === dimension) { this.parsedVersion = theParsedVersion; return theParsedVersion; } throw new FormatException(); } copyBit(i, j, versionBits) { const bit = this.isMirror ? this.bitMatrix.get(j, i) : this.bitMatrix.get(i, j); return bit ? versionBits << 1 | 1 : versionBits << 1; } /** *Reads the bits in the {@link BitMatrix} representing the finder pattern in the * correct order in order to reconstruct the codewords bytes contained within the * QR Code.
* * @return bytes encoded within the QR Code * @throws FormatException if the exact number of bytes expected is not read */ readCodewords() { const formatInfo = this.readFormatInformation(); const version = this.readVersion(); const dataMask = DataMask.values.get(formatInfo.getDataMask()); const dimension = this.bitMatrix.getHeight(); dataMask.unmaskBitMatrix(this.bitMatrix, dimension); const functionPattern = version.buildFunctionPattern(); let readingUp = true; const result = new Uint8Array(version.getTotalCodewords()); let resultOffset = 0; let currentByte = 0; let bitsRead = 0; for (let j = dimension - 1; j > 0; j -= 2) { if (j === 6) { j--; } for (let count = 0; count < dimension; count++) { const i = readingUp ? dimension - 1 - count : count; for (let col = 0; col < 2; col++) { if (!functionPattern.get(j - col, i)) { bitsRead++; currentByte <<= 1; if (this.bitMatrix.get(j - col, i)) { currentByte |= 1; } if (bitsRead === 8) { result[resultOffset++] = /*(byte) */ currentByte; bitsRead = 0; currentByte = 0; } } } } readingUp = !readingUp; } if (resultOffset !== version.getTotalCodewords()) { throw new FormatException(); } return result; } /** * Revert the mask removal done while reading the code words. The bit matrix should revert to its original state. */ remask() { if (this.parsedFormatInfo === null) { return; } const dataMask = DataMask.values[this.parsedFormatInfo.getDataMask()]; const dimension = this.bitMatrix.getHeight(); dataMask.unmaskBitMatrix(this.bitMatrix, dimension); } /** * Prepare the parser for a mirrored operation. * This flag has effect only on the {@link #readFormatInformation()} and the * {@link #readVersion()}. Before proceeding with {@link #readCodewords()} the * {@link #mirror()} method should be called. * * @param mirror Whether to read version and format information mirrored. */ setMirror(isMirror) { this.parsedVersion = null; this.parsedFormatInfo = null; this.isMirror = isMirror; } /** Mirror the bit matrix in order to attempt a second reading. */ mirror() { const bitMatrix = this.bitMatrix; for (let x = 0, width = bitMatrix.getWidth(); x < width; x++) { for (let y = x + 1, height = bitMatrix.getHeight(); y < height; y++) { if (bitMatrix.get(x, y) !== bitMatrix.get(y, x)) { bitMatrix.flip(y, x); bitMatrix.flip(x, y); } } } } } class DataBlock$1 { constructor(numDataCodewords, codewords) { this.numDataCodewords = numDataCodewords; this.codewords = codewords; } /** *When QR Codes use multiple data blocks, they are actually interleaved. * That is, the first byte of data block 1 to n is written, then the second bytes, and so on. This * method will separate the data into original blocks.
* * @param rawCodewords bytes as read directly from the QR Code * @param version version of the QR Code * @param ecLevel error-correction level of the QR Code * @return DataBlocks containing original bytes, "de-interleaved" from representation in the * QR Code */ static getDataBlocks(rawCodewords, version, ecLevel) { if (rawCodewords.length !== version.getTotalCodewords()) { throw new IllegalArgumentException(); } const ecBlocks = version.getECBlocksForLevel(ecLevel); let totalBlocks = 0; const ecBlockArray = ecBlocks.getECBlocks(); for (const ecBlock of ecBlockArray) { totalBlocks += ecBlock.getCount(); } const result = new Array(totalBlocks); let numResultBlocks = 0; for (const ecBlock of ecBlockArray) { for (let i = 0; i < ecBlock.getCount(); i++) { const numDataCodewords = ecBlock.getDataCodewords(); const numBlockCodewords = ecBlocks.getECCodewordsPerBlock() + numDataCodewords; result[numResultBlocks++] = new DataBlock$1(numDataCodewords, new Uint8Array(numBlockCodewords)); } } const shorterBlocksTotalCodewords = result[0].codewords.length; let longerBlocksStartAt = result.length - 1; while (longerBlocksStartAt >= 0) { const numCodewords = result[longerBlocksStartAt].codewords.length; if (numCodewords === shorterBlocksTotalCodewords) { break; } longerBlocksStartAt--; } longerBlocksStartAt++; const shorterBlocksNumDataCodewords = shorterBlocksTotalCodewords - ecBlocks.getECCodewordsPerBlock(); let rawCodewordsOffset = 0; for (let i = 0; i < shorterBlocksNumDataCodewords; i++) { for (let j = 0; j < numResultBlocks; j++) { result[j].codewords[i] = rawCodewords[rawCodewordsOffset++]; } } for (let j = longerBlocksStartAt; j < numResultBlocks; j++) { result[j].codewords[shorterBlocksNumDataCodewords] = rawCodewords[rawCodewordsOffset++]; } const max = result[0].codewords.length; for (let i = shorterBlocksNumDataCodewords; i < max; i++) { for (let j = 0; j < numResultBlocks; j++) { const iOffset = j < longerBlocksStartAt ? i : i + 1; result[j].codewords[iOffset] = rawCodewords[rawCodewordsOffset++]; } } return result; } getNumDataCodewords() { return this.numDataCodewords; } getCodewords() { return this.codewords; } } var ModeValues; (function(ModeValues2) { ModeValues2[ModeValues2["TERMINATOR"] = 0] = "TERMINATOR"; ModeValues2[ModeValues2["NUMERIC"] = 1] = "NUMERIC"; ModeValues2[ModeValues2["ALPHANUMERIC"] = 2] = "ALPHANUMERIC"; ModeValues2[ModeValues2["STRUCTURED_APPEND"] = 3] = "STRUCTURED_APPEND"; ModeValues2[ModeValues2["BYTE"] = 4] = "BYTE"; ModeValues2[ModeValues2["ECI"] = 5] = "ECI"; ModeValues2[ModeValues2["KANJI"] = 6] = "KANJI"; ModeValues2[ModeValues2["FNC1_FIRST_POSITION"] = 7] = "FNC1_FIRST_POSITION"; ModeValues2[ModeValues2["FNC1_SECOND_POSITION"] = 8] = "FNC1_SECOND_POSITION"; ModeValues2[ModeValues2["HANZI"] = 9] = "HANZI"; })(ModeValues || (ModeValues = {})); class Mode$1 { constructor(value, stringValue, characterCountBitsForVersions, bits) { this.value = value; this.stringValue = stringValue; this.characterCountBitsForVersions = characterCountBitsForVersions; this.bits = bits; Mode$1.FOR_BITS.set(bits, this); Mode$1.FOR_VALUE.set(value, this); } /** * @param bits four bits encoding a QR Code data mode * @return Mode encoded by these bits * @throws IllegalArgumentException if bits do not correspond to a known mode */ static forBits(bits) { const mode = Mode$1.FOR_BITS.get(bits); if (void 0 === mode) { throw new IllegalArgumentException(); } return mode; } /** * @param version version in question * @return number of bits used, in this QR Code symbol {@link Version}, to encode the * count of characters that will follow encoded in this Mode */ getCharacterCountBits(version) { const versionNumber = version.getVersionNumber(); let offset; if (versionNumber <= 9) { offset = 0; } else if (versionNumber <= 26) { offset = 1; } else { offset = 2; } return this.characterCountBitsForVersions[offset]; } getValue() { return this.value; } getBits() { return this.bits; } equals(o) { if (!(o instanceof Mode$1)) { return false; } const other = o; return this.value === other.value; } toString() { return this.stringValue; } } Mode$1.FOR_BITS = /* @__PURE__ */ new Map(); Mode$1.FOR_VALUE = /* @__PURE__ */ new Map(); Mode$1.TERMINATOR = new Mode$1(ModeValues.TERMINATOR, "TERMINATOR", Int32Array.from([0, 0, 0]), 0); Mode$1.NUMERIC = new Mode$1(ModeValues.NUMERIC, "NUMERIC", Int32Array.from([10, 12, 14]), 1); Mode$1.ALPHANUMERIC = new Mode$1(ModeValues.ALPHANUMERIC, "ALPHANUMERIC", Int32Array.from([9, 11, 13]), 2); Mode$1.STRUCTURED_APPEND = new Mode$1(ModeValues.STRUCTURED_APPEND, "STRUCTURED_APPEND", Int32Array.from([0, 0, 0]), 3); Mode$1.BYTE = new Mode$1(ModeValues.BYTE, "BYTE", Int32Array.from([8, 16, 16]), 4); Mode$1.ECI = new Mode$1(ModeValues.ECI, "ECI", Int32Array.from([0, 0, 0]), 7); Mode$1.KANJI = new Mode$1(ModeValues.KANJI, "KANJI", Int32Array.from([8, 10, 12]), 8); Mode$1.FNC1_FIRST_POSITION = new Mode$1(ModeValues.FNC1_FIRST_POSITION, "FNC1_FIRST_POSITION", Int32Array.from([0, 0, 0]), 5); Mode$1.FNC1_SECOND_POSITION = new Mode$1(ModeValues.FNC1_SECOND_POSITION, "FNC1_SECOND_POSITION", Int32Array.from([0, 0, 0]), 9); Mode$1.HANZI = new Mode$1(ModeValues.HANZI, "HANZI", Int32Array.from([8, 10, 12]), 13); class DecodedBitStreamParser$1 { static decode(bytes, version, ecLevel, hints) { const bits = new BitSource(bytes); let result = new StringBuilder(); const byteSegments = new Array(); let symbolSequence = -1; let parityData = -1; try { let currentCharacterSetECI = null; let fc1InEffect = false; let mode; do { if (bits.available() < 4) { mode = Mode$1.TERMINATOR; } else { const modeBits = bits.readBits(4); mode = Mode$1.forBits(modeBits); } switch (mode) { case Mode$1.TERMINATOR: break; case Mode$1.FNC1_FIRST_POSITION: case Mode$1.FNC1_SECOND_POSITION: fc1InEffect = true; break; case Mode$1.STRUCTURED_APPEND: if (bits.available() < 16) { throw new FormatException(); } symbolSequence = bits.readBits(8); parityData = bits.readBits(8); break; case Mode$1.ECI: const value = DecodedBitStreamParser$1.parseECIValue(bits); currentCharacterSetECI = CharacterSetECI.getCharacterSetECIByValue(value); if (currentCharacterSetECI === null) { throw new FormatException(); } break; case Mode$1.HANZI: const subset = bits.readBits(4); const countHanzi = bits.readBits(mode.getCharacterCountBits(version)); if (subset === DecodedBitStreamParser$1.GB2312_SUBSET) { DecodedBitStreamParser$1.decodeHanziSegment(bits, result, countHanzi); } break; default: const count = bits.readBits(mode.getCharacterCountBits(version)); switch (mode) { case Mode$1.NUMERIC: DecodedBitStreamParser$1.decodeNumericSegment(bits, result, count); break; case Mode$1.ALPHANUMERIC: DecodedBitStreamParser$1.decodeAlphanumericSegment(bits, result, count, fc1InEffect); break; case Mode$1.BYTE: DecodedBitStreamParser$1.decodeByteSegment(bits, result, count, currentCharacterSetECI, byteSegments, hints); break; case Mode$1.KANJI: DecodedBitStreamParser$1.decodeKanjiSegment(bits, result, count); break; default: throw new FormatException(); } break; } } while (mode !== Mode$1.TERMINATOR); } catch (iae) { throw new FormatException(); } return new DecoderResult(bytes, result.toString(), byteSegments.length === 0 ? null : byteSegments, ecLevel === null ? null : ecLevel.toString(), symbolSequence, parityData); } /** * See specification GBT 18284-2000 */ static decodeHanziSegment(bits, result, count) { if (count * 13 > bits.available()) { throw new FormatException(); } const buffer = new Uint8Array(2 * count); let offset = 0; while (count > 0) { const twoBytes = bits.readBits(13); let assembledTwoBytes = twoBytes / 96 << 8 & 4294967295 | twoBytes % 96; if (assembledTwoBytes < 959) { assembledTwoBytes += 41377; } else { assembledTwoBytes += 42657; } buffer[offset] = /*(byte) */ assembledTwoBytes >> 8 & 255; buffer[offset + 1] = /*(byte) */ assembledTwoBytes & 255; offset += 2; count--; } try { result.append(StringEncoding.decode(buffer, StringUtils.GB2312)); } catch (ignored) { throw new FormatException(ignored); } } static decodeKanjiSegment(bits, result, count) { if (count * 13 > bits.available()) { throw new FormatException(); } const buffer = new Uint8Array(2 * count); let offset = 0; while (count > 0) { const twoBytes = bits.readBits(13); let assembledTwoBytes = twoBytes / 192 << 8 & 4294967295 | twoBytes % 192; if (assembledTwoBytes < 7936) { assembledTwoBytes += 33088; } else { assembledTwoBytes += 49472; } buffer[offset] = /*(byte) */ assembledTwoBytes >> 8; buffer[offset + 1] = /*(byte) */ assembledTwoBytes; offset += 2; count--; } try { result.append(StringEncoding.decode(buffer, StringUtils.SHIFT_JIS)); } catch (ignored) { throw new FormatException(ignored); } } static decodeByteSegment(bits, result, count, currentCharacterSetECI, byteSegments, hints) { if (8 * count > bits.available()) { throw new FormatException(); } const readBytes = new Uint8Array(count); for (let i = 0; i < count; i++) { readBytes[i] = /*(byte) */ bits.readBits(8); } let encoding; if (currentCharacterSetECI === null) { encoding = StringUtils.guessEncoding(readBytes, hints); } else { encoding = currentCharacterSetECI.getName(); } try { result.append(StringEncoding.decode(readBytes, encoding)); } catch (ignored) { throw new FormatException(ignored); } byteSegments.push(readBytes); } static toAlphaNumericChar(value) { if (value >= DecodedBitStreamParser$1.ALPHANUMERIC_CHARS.length) { throw new FormatException(); } return DecodedBitStreamParser$1.ALPHANUMERIC_CHARS[value]; } static decodeAlphanumericSegment(bits, result, count, fc1InEffect) { const start = result.length(); while (count > 1) { if (bits.available() < 11) { throw new FormatException(); } const nextTwoCharsBits = bits.readBits(11); result.append(DecodedBitStreamParser$1.toAlphaNumericChar(Math.floor(nextTwoCharsBits / 45))); result.append(DecodedBitStreamParser$1.toAlphaNumericChar(nextTwoCharsBits % 45)); count -= 2; } if (count === 1) { if (bits.available() < 6) { throw new FormatException(); } result.append(DecodedBitStreamParser$1.toAlphaNumericChar(bits.readBits(6))); } if (fc1InEffect) { for (let i = start; i < result.length(); i++) { if (result.charAt(i) === "%") { if (i < result.length() - 1 && result.charAt(i + 1) === "%") { result.deleteCharAt(i + 1); } else { result.setCharAt(i, String.fromCharCode(29)); } } } } } static decodeNumericSegment(bits, result, count) { while (count >= 3) { if (bits.available() < 10) { throw new FormatException(); } const threeDigitsBits = bits.readBits(10); if (threeDigitsBits >= 1e3) { throw new FormatException(); } result.append(DecodedBitStreamParser$1.toAlphaNumericChar(Math.floor(threeDigitsBits / 100))); result.append(DecodedBitStreamParser$1.toAlphaNumericChar(Math.floor(threeDigitsBits / 10) % 10)); result.append(DecodedBitStreamParser$1.toAlphaNumericChar(threeDigitsBits % 10)); count -= 3; } if (count === 2) { if (bits.available() < 7) { throw new FormatException(); } const twoDigitsBits = bits.readBits(7); if (twoDigitsBits >= 100) { throw new FormatException(); } result.append(DecodedBitStreamParser$1.toAlphaNumericChar(Math.floor(twoDigitsBits / 10))); result.append(DecodedBitStreamParser$1.toAlphaNumericChar(twoDigitsBits % 10)); } else if (count === 1) { if (bits.available() < 4) { throw new FormatException(); } const digitBits = bits.readBits(4); if (digitBits >= 10) { throw new FormatException(); } result.append(DecodedBitStreamParser$1.toAlphaNumericChar(digitBits)); } } static parseECIValue(bits) { const firstByte = bits.readBits(8); if ((firstByte & 128) === 0) { return firstByte & 127; } if ((firstByte & 192) === 128) { const secondByte = bits.readBits(8); return (firstByte & 63) << 8 & 4294967295 | secondByte; } if ((firstByte & 224) === 192) { const secondThirdBytes = bits.readBits(16); return (firstByte & 31) << 16 & 4294967295 | secondThirdBytes; } throw new FormatException(); } } DecodedBitStreamParser$1.ALPHANUMERIC_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; DecodedBitStreamParser$1.GB2312_SUBSET = 1; class QRCodeDecoderMetaData { constructor(mirrored) { this.mirrored = mirrored; } /** * @return true if the QR Code was mirrored. */ isMirrored() { return this.mirrored; } /** * Apply the result points' order correction due to mirroring. * * @param points Array of points to apply mirror correction to. */ applyMirroredCorrection(points) { if (!this.mirrored || points === null || points.length < 3) { return; } const bottomLeft = points[0]; points[0] = points[2]; points[2] = bottomLeft; } } class Decoder$2 { constructor() { this.rsDecoder = new ReedSolomonDecoder(GenericGF.QR_CODE_FIELD_256); } // public decode(image: boolean[][]): DecoderResult /*throws ChecksumException, FormatException*/ { // return decode(image, null) // } /** *Convenience method that can decode a QR Code represented as a 2D array of booleans. * "true" is taken to mean a black module.
* * @param image booleans representing white/black QR Code modules * @param hints decoding hints that should be used to influence decoding * @return text and bytes encoded within the QR Code * @throws FormatException if the QR Code cannot be decoded * @throws ChecksumException if error correction fails */ decodeBooleanArray(image, hints) { return this.decodeBitMatrix(BitMatrix.parseFromBooleanArray(image), hints); } // public decodeBitMatrix(bits: BitMatrix): DecoderResult /*throws ChecksumException, FormatException*/ { // return decode(bits, null) // } /** *Decodes a QR Code represented as a {@link BitMatrix}. A 1 or "true" is taken to mean a black module.
* * @param bits booleans representing white/black QR Code modules * @param hints decoding hints that should be used to influence decoding * @return text and bytes encoded within the QR Code * @throws FormatException if the QR Code cannot be decoded * @throws ChecksumException if error correction fails */ decodeBitMatrix(bits, hints) { const parser = new BitMatrixParser$1(bits); let ex = null; try { return this.decodeBitMatrixParser(parser, hints); } catch (e) { ex = e; } try { parser.remask(); parser.setMirror(true); parser.readVersion(); parser.readFormatInformation(); parser.mirror(); const result = this.decodeBitMatrixParser(parser, hints); result.setOther(new QRCodeDecoderMetaData(true)); return result; } catch (e) { if (ex !== null) { throw ex; } throw e; } } decodeBitMatrixParser(parser, hints) { const version = parser.readVersion(); const ecLevel = parser.readFormatInformation().getErrorCorrectionLevel(); const codewords = parser.readCodewords(); const dataBlocks = DataBlock$1.getDataBlocks(codewords, version, ecLevel); let totalBytes = 0; for (const dataBlock of dataBlocks) { totalBytes += dataBlock.getNumDataCodewords(); } const resultBytes = new Uint8Array(totalBytes); let resultOffset = 0; for (const dataBlock of dataBlocks) { const codewordBytes = dataBlock.getCodewords(); const numDataCodewords = dataBlock.getNumDataCodewords(); this.correctErrors(codewordBytes, numDataCodewords); for (let i = 0; i < numDataCodewords; i++) { resultBytes[resultOffset++] = codewordBytes[i]; } } return DecodedBitStreamParser$1.decode(resultBytes, version, ecLevel, hints); } /** *Given data and error-correction codewords received, possibly corrupted by errors, attempts to * correct the errors in-place using Reed-Solomon error correction.
* * @param codewordBytes data and error correction codewords * @param numDataCodewords number of codewords that are data bytes * @throws ChecksumException if error correction fails */ correctErrors(codewordBytes, numDataCodewords) { const codewordsInts = new Int32Array(codewordBytes); try { this.rsDecoder.decode(codewordsInts, codewordBytes.length - numDataCodewords); } catch (ignored) { throw new ChecksumException(); } for (let i = 0; i < numDataCodewords; i++) { codewordBytes[i] = /*(byte) */ codewordsInts[i]; } } } class AlignmentPattern extends ResultPoint { constructor(posX, posY, estimatedModuleSize) { super(posX, posY); this.estimatedModuleSize = estimatedModuleSize; } /** *Determines if this alignment pattern "about equals" an alignment pattern at the stated * position and size -- meaning, it is at nearly the same center with nearly the same size.
*/ aboutEquals(moduleSize, i, j) { if (Math.abs(i - this.getY()) <= moduleSize && Math.abs(j - this.getX()) <= moduleSize) { const moduleSizeDiff = Math.abs(moduleSize - this.estimatedModuleSize); return moduleSizeDiff <= 1 || moduleSizeDiff <= this.estimatedModuleSize; } return false; } /** * Combines this object's current estimate of a finder pattern position and module size * with a new estimate. It returns a new {@code FinderPattern} containing an average of the two. */ combineEstimate(i, j, newModuleSize) { const combinedX = (this.getX() + j) / 2; const combinedY = (this.getY() + i) / 2; const combinedModuleSize = (this.estimatedModuleSize + newModuleSize) / 2; return new AlignmentPattern(combinedX, combinedY, combinedModuleSize); } } class AlignmentPatternFinder { /** *Creates a finder that will look in a portion of the whole image.
* * @param image image to search * @param startX left column from which to start searching * @param startY top row from which to start searching * @param width width of region to search * @param height height of region to search * @param moduleSize estimated module size so far */ constructor(image, startX, startY, width, height, moduleSize, resultPointCallback) { this.image = image; this.startX = startX; this.startY = startY; this.width = width; this.height = height; this.moduleSize = moduleSize; this.resultPointCallback = resultPointCallback; this.possibleCenters = []; this.crossCheckStateCount = new Int32Array(3); } /** *This method attempts to find the bottom-right alignment pattern in the image. It is a bit messy since * it's pretty performance-critical and so is written to be fast foremost.
* * @return {@link AlignmentPattern} if found * @throws NotFoundException if not found */ find() { const startX = this.startX; const height = this.height; const width = this.width; const maxJ = startX + width; const middleI = this.startY + height / 2; const stateCount = new Int32Array(3); const image = this.image; for (let iGen = 0; iGen < height; iGen++) { const i = middleI + ((iGen & 1) === 0 ? Math.floor((iGen + 1) / 2) : -Math.floor((iGen + 1) / 2)); stateCount[0] = 0; stateCount[1] = 0; stateCount[2] = 0; let j = startX; while (j < maxJ && !image.get(j, i)) { j++; } let currentState = 0; while (j < maxJ) { if (image.get(j, i)) { if (currentState === 1) { stateCount[1]++; } else { if (currentState === 2) { if (this.foundPatternCross(stateCount)) { const confirmed = this.handlePossibleCenter(stateCount, i, j); if (confirmed !== null) { return confirmed; } } stateCount[0] = stateCount[2]; stateCount[1] = 1; stateCount[2] = 0; currentState = 1; } else { stateCount[++currentState]++; } } } else { if (currentState === 1) { currentState++; } stateCount[currentState]++; } j++; } if (this.foundPatternCross(stateCount)) { const confirmed = this.handlePossibleCenter(stateCount, i, maxJ); if (confirmed !== null) { return confirmed; } } } if (this.possibleCenters.length !== 0) { return this.possibleCenters[0]; } throw new NotFoundException(); } /** * Given a count of black/white/black pixels just seen and an end position, * figures the location of the center of this black/white/black run. */ static centerFromEnd(stateCount, end) { return end - stateCount[2] - stateCount[1] / 2; } /** * @param stateCount count of black/white/black pixels just read * @return true iff the proportions of the counts is close enough to the 1/1/1 ratios * used by alignment patterns to be considered a match */ foundPatternCross(stateCount) { const moduleSize = this.moduleSize; const maxVariance = moduleSize / 2; for (let i = 0; i < 3; i++) { if (Math.abs(moduleSize - stateCount[i]) >= maxVariance) { return false; } } return true; } /** *After a horizontal scan finds a potential alignment pattern, this method * "cross-checks" by scanning down vertically through the center of the possible * alignment pattern to see if the same proportion is detected.
* * @param startI row where an alignment pattern was detected * @param centerJ center of the section that appears to cross an alignment pattern * @param maxCount maximum reasonable number of modules that should be * observed in any reading state, based on the results of the horizontal scan * @return vertical center of alignment pattern, or {@link Float#NaN} if not found */ crossCheckVertical(startI, centerJ, maxCount, originalStateCountTotal) { const image = this.image; const maxI = image.getHeight(); const stateCount = this.crossCheckStateCount; stateCount[0] = 0; stateCount[1] = 0; stateCount[2] = 0; let i = startI; while (i >= 0 && image.get(centerJ, i) && stateCount[1] <= maxCount) { stateCount[1]++; i--; } if (i < 0 || stateCount[1] > maxCount) { return NaN; } while (i >= 0 && !image.get(centerJ, i) && stateCount[0] <= maxCount) { stateCount[0]++; i--; } if (stateCount[0] > maxCount) { return NaN; } i = startI + 1; while (i < maxI && image.get(centerJ, i) && stateCount[1] <= maxCount) { stateCount[1]++; i++; } if (i === maxI || stateCount[1] > maxCount) { return NaN; } while (i < maxI && !image.get(centerJ, i) && stateCount[2] <= maxCount) { stateCount[2]++; i++; } if (stateCount[2] > maxCount) { return NaN; } const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2]; if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal) { return NaN; } return this.foundPatternCross(stateCount) ? AlignmentPatternFinder.centerFromEnd(stateCount, i) : NaN; } /** *This is called when a horizontal scan finds a possible alignment pattern. It will * cross check with a vertical scan, and if successful, will see if this pattern had been * found on a previous horizontal scan. If so, we consider it confirmed and conclude we have * found the alignment pattern.
* * @param stateCount reading state module counts from horizontal scan * @param i row where alignment pattern may be found * @param j end of possible alignment pattern in row * @return {@link AlignmentPattern} if we have found the same pattern twice, or null if not */ handlePossibleCenter(stateCount, i, j) { const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2]; const centerJ = AlignmentPatternFinder.centerFromEnd(stateCount, j); const centerI = this.crossCheckVertical( i, /*(int) */ centerJ, 2 * stateCount[1], stateCountTotal ); if (!isNaN(centerI)) { const estimatedModuleSize = (stateCount[0] + stateCount[1] + stateCount[2]) / 3; for (const center of this.possibleCenters) { if (center.aboutEquals(estimatedModuleSize, centerI, centerJ)) { return center.combineEstimate(centerI, centerJ, estimatedModuleSize); } } const point = new AlignmentPattern(centerJ, centerI, estimatedModuleSize); this.possibleCenters.push(point); if (this.resultPointCallback !== null && this.resultPointCallback !== void 0) { this.resultPointCallback.foundPossibleResultPoint(point); } } return null; } } class FinderPattern$1 extends ResultPoint { // FinderPattern(posX: number/*float*/, posY: number/*float*/, estimatedModuleSize: number/*float*/) { // this(posX, posY, estimatedModuleSize, 1) // } constructor(posX, posY, estimatedModuleSize, count) { super(posX, posY); this.estimatedModuleSize = estimatedModuleSize; this.count = count; if (void 0 === count) { this.count = 1; } } getEstimatedModuleSize() { return this.estimatedModuleSize; } getCount() { return this.count; } /* void incrementCount() { this.count++ } */ /** *Determines if this finder pattern "about equals" a finder pattern at the stated * position and size -- meaning, it is at nearly the same center with nearly the same size.
*/ aboutEquals(moduleSize, i, j) { if (Math.abs(i - this.getY()) <= moduleSize && Math.abs(j - this.getX()) <= moduleSize) { const moduleSizeDiff = Math.abs(moduleSize - this.estimatedModuleSize); return moduleSizeDiff <= 1 || moduleSizeDiff <= this.estimatedModuleSize; } return false; } /** * Combines this object's current estimate of a finder pattern position and module size * with a new estimate. It returns a new {@code FinderPattern} containing a weighted average * based on count. */ combineEstimate(i, j, newModuleSize) { const combinedCount = this.count + 1; const combinedX = (this.count * this.getX() + j) / combinedCount; const combinedY = (this.count * this.getY() + i) / combinedCount; const combinedModuleSize = (this.count * this.estimatedModuleSize + newModuleSize) / combinedCount; return new FinderPattern$1(combinedX, combinedY, combinedModuleSize, combinedCount); } } class FinderPatternInfo { constructor(patternCenters) { this.bottomLeft = patternCenters[0]; this.topLeft = patternCenters[1]; this.topRight = patternCenters[2]; } getBottomLeft() { return this.bottomLeft; } getTopLeft() { return this.topLeft; } getTopRight() { return this.topRight; } } class FinderPatternFinder { /** *Creates a finder that will search the image for three finder patterns.
* * @param image image to search */ // public constructor(image: BitMatrix) { // this(image, null) // } constructor(image, resultPointCallback) { this.image = image; this.resultPointCallback = resultPointCallback; this.possibleCenters = []; this.crossCheckStateCount = new Int32Array(5); this.resultPointCallback = resultPointCallback; } getImage() { return this.image; } getPossibleCenters() { return this.possibleCenters; } find(hints) { const tryHarder = hints !== null && hints !== void 0 && void 0 !== hints.get(DecodeHintType$1.TRY_HARDER); const pureBarcode = hints !== null && hints !== void 0 && void 0 !== hints.get(DecodeHintType$1.PURE_BARCODE); const image = this.image; const maxI = image.getHeight(); const maxJ = image.getWidth(); let iSkip = Math.floor(3 * maxI / (4 * FinderPatternFinder.MAX_MODULES)); if (iSkip < FinderPatternFinder.MIN_SKIP || tryHarder) { iSkip = FinderPatternFinder.MIN_SKIP; } let done = false; const stateCount = new Int32Array(5); for (let i = iSkip - 1; i < maxI && !done; i += iSkip) { stateCount[0] = 0; stateCount[1] = 0; stateCount[2] = 0; stateCount[3] = 0; stateCount[4] = 0; let currentState = 0; for (let j = 0; j < maxJ; j++) { if (image.get(j, i)) { if ((currentState & 1) === 1) { currentState++; } stateCount[currentState]++; } else { if ((currentState & 1) === 0) { if (currentState === 4) { if (FinderPatternFinder.foundPatternCross(stateCount)) { const confirmed = this.handlePossibleCenter(stateCount, i, j, pureBarcode); if (confirmed === true) { iSkip = 2; if (this.hasSkipped === true) { done = this.haveMultiplyConfirmedCenters(); } else { const rowSkip = this.findRowSkip(); if (rowSkip > stateCount[2]) { i += rowSkip - stateCount[2] - iSkip; j = maxJ - 1; } } } else { stateCount[0] = stateCount[2]; stateCount[1] = stateCount[3]; stateCount[2] = stateCount[4]; stateCount[3] = 1; stateCount[4] = 0; currentState = 3; continue; } currentState = 0; stateCount[0] = 0; stateCount[1] = 0; stateCount[2] = 0; stateCount[3] = 0; stateCount[4] = 0; } else { stateCount[0] = stateCount[2]; stateCount[1] = stateCount[3]; stateCount[2] = stateCount[4]; stateCount[3] = 1; stateCount[4] = 0; currentState = 3; } } else { stateCount[++currentState]++; } } else { stateCount[currentState]++; } } } if (FinderPatternFinder.foundPatternCross(stateCount)) { const confirmed = this.handlePossibleCenter(stateCount, i, maxJ, pureBarcode); if (confirmed === true) { iSkip = stateCount[0]; if (this.hasSkipped) { done = this.haveMultiplyConfirmedCenters(); } } } } const patternInfo = this.selectBestPatterns(); ResultPoint.orderBestPatterns(patternInfo); return new FinderPatternInfo(patternInfo); } /** * Given a count of black/white/black/white/black pixels just seen and an end position, * figures the location of the center of this run. */ static centerFromEnd(stateCount, end) { return end - stateCount[4] - stateCount[3] - stateCount[2] / 2; } /** * @param stateCount count of black/white/black/white/black pixels just read * @return true iff the proportions of the counts is close enough to the 1/1/3/1/1 ratios * used by finder patterns to be considered a match */ static foundPatternCross(stateCount) { let totalModuleSize = 0; for (let i = 0; i < 5; i++) { const count = stateCount[i]; if (count === 0) { return false; } totalModuleSize += count; } if (totalModuleSize < 7) { return false; } const moduleSize = totalModuleSize / 7; const maxVariance = moduleSize / 2; return Math.abs(moduleSize - stateCount[0]) < maxVariance && Math.abs(moduleSize - stateCount[1]) < maxVariance && Math.abs(3 * moduleSize - stateCount[2]) < 3 * maxVariance && Math.abs(moduleSize - stateCount[3]) < maxVariance && Math.abs(moduleSize - stateCount[4]) < maxVariance; } getCrossCheckStateCount() { const crossCheckStateCount = this.crossCheckStateCount; crossCheckStateCount[0] = 0; crossCheckStateCount[1] = 0; crossCheckStateCount[2] = 0; crossCheckStateCount[3] = 0; crossCheckStateCount[4] = 0; return crossCheckStateCount; } /** * After a vertical and horizontal scan finds a potential finder pattern, this method * "cross-cross-cross-checks" by scanning down diagonally through the center of the possible * finder pattern to see if the same proportion is detected. * * @param startI row where a finder pattern was detected * @param centerJ center of the section that appears to cross a finder pattern * @param maxCount maximum reasonable number of modules that should be * observed in any reading state, based on the results of the horizontal scan * @param originalStateCountTotal The original state count total. * @return true if proportions are withing expected limits */ crossCheckDiagonal(startI, centerJ, maxCount, originalStateCountTotal) { const stateCount = this.getCrossCheckStateCount(); let i = 0; const image = this.image; while (startI >= i && centerJ >= i && image.get(centerJ - i, startI - i)) { stateCount[2]++; i++; } if (startI < i || centerJ < i) { return false; } while (startI >= i && centerJ >= i && !image.get(centerJ - i, startI - i) && stateCount[1] <= maxCount) { stateCount[1]++; i++; } if (startI < i || centerJ < i || stateCount[1] > maxCount) { return false; } while (startI >= i && centerJ >= i && image.get(centerJ - i, startI - i) && stateCount[0] <= maxCount) { stateCount[0]++; i++; } if (stateCount[0] > maxCount) { return false; } const maxI = image.getHeight(); const maxJ = image.getWidth(); i = 1; while (startI + i < maxI && centerJ + i < maxJ && image.get(centerJ + i, startI + i)) { stateCount[2]++; i++; } if (startI + i >= maxI || centerJ + i >= maxJ) { return false; } while (startI + i < maxI && centerJ + i < maxJ && !image.get(centerJ + i, startI + i) && stateCount[3] < maxCount) { stateCount[3]++; i++; } if (startI + i >= maxI || centerJ + i >= maxJ || stateCount[3] >= maxCount) { return false; } while (startI + i < maxI && centerJ + i < maxJ && image.get(centerJ + i, startI + i) && stateCount[4] < maxCount) { stateCount[4]++; i++; } if (stateCount[4] >= maxCount) { return false; } const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4]; return Math.abs(stateCountTotal - originalStateCountTotal) < 2 * originalStateCountTotal && FinderPatternFinder.foundPatternCross(stateCount); } /** *After a horizontal scan finds a potential finder pattern, this method * "cross-checks" by scanning down vertically through the center of the possible * finder pattern to see if the same proportion is detected.
* * @param startI row where a finder pattern was detected * @param centerJ center of the section that appears to cross a finder pattern * @param maxCount maximum reasonable number of modules that should be * observed in any reading state, based on the results of the horizontal scan * @return vertical center of finder pattern, or {@link Float#NaN} if not found */ crossCheckVertical(startI, centerJ, maxCount, originalStateCountTotal) { const image = this.image; const maxI = image.getHeight(); const stateCount = this.getCrossCheckStateCount(); let i = startI; while (i >= 0 && image.get(centerJ, i)) { stateCount[2]++; i--; } if (i < 0) { return NaN; } while (i >= 0 && !image.get(centerJ, i) && stateCount[1] <= maxCount) { stateCount[1]++; i--; } if (i < 0 || stateCount[1] > maxCount) { return NaN; } while (i >= 0 && image.get(centerJ, i) && stateCount[0] <= maxCount) { stateCount[0]++; i--; } if (stateCount[0] > maxCount) { return NaN; } i = startI + 1; while (i < maxI && image.get(centerJ, i)) { stateCount[2]++; i++; } if (i === maxI) { return NaN; } while (i < maxI && !image.get(centerJ, i) && stateCount[3] < maxCount) { stateCount[3]++; i++; } if (i === maxI || stateCount[3] >= maxCount) { return NaN; } while (i < maxI && image.get(centerJ, i) && stateCount[4] < maxCount) { stateCount[4]++; i++; } if (stateCount[4] >= maxCount) { return NaN; } const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4]; if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal) { return NaN; } return FinderPatternFinder.foundPatternCross(stateCount) ? FinderPatternFinder.centerFromEnd(stateCount, i) : NaN; } /** *Like {@link #crossCheckVertical(int, int, int, int)}, and in fact is basically identical, * except it reads horizontally instead of vertically. This is used to cross-cross * check a vertical cross check and locate the real center of the alignment pattern.
*/ crossCheckHorizontal(startJ, centerI, maxCount, originalStateCountTotal) { const image = this.image; const maxJ = image.getWidth(); const stateCount = this.getCrossCheckStateCount(); let j = startJ; while (j >= 0 && image.get(j, centerI)) { stateCount[2]++; j--; } if (j < 0) { return NaN; } while (j >= 0 && !image.get(j, centerI) && stateCount[1] <= maxCount) { stateCount[1]++; j--; } if (j < 0 || stateCount[1] > maxCount) { return NaN; } while (j >= 0 && image.get(j, centerI) && stateCount[0] <= maxCount) { stateCount[0]++; j--; } if (stateCount[0] > maxCount) { return NaN; } j = startJ + 1; while (j < maxJ && image.get(j, centerI)) { stateCount[2]++; j++; } if (j === maxJ) { return NaN; } while (j < maxJ && !image.get(j, centerI) && stateCount[3] < maxCount) { stateCount[3]++; j++; } if (j === maxJ || stateCount[3] >= maxCount) { return NaN; } while (j < maxJ && image.get(j, centerI) && stateCount[4] < maxCount) { stateCount[4]++; j++; } if (stateCount[4] >= maxCount) { return NaN; } const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4]; if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= originalStateCountTotal) { return NaN; } return FinderPatternFinder.foundPatternCross(stateCount) ? FinderPatternFinder.centerFromEnd(stateCount, j) : NaN; } /** *This is called when a horizontal scan finds a possible alignment pattern. It will * cross check with a vertical scan, and if successful, will, ah, cross-cross-check * with another horizontal scan. This is needed primarily to locate the real horizontal * center of the pattern in cases of extreme skew. * And then we cross-cross-cross check with another diagonal scan.
* *If that succeeds the finder pattern location is added to a list that tracks * the number of times each location has been nearly-matched as a finder pattern. * Each additional find is more evidence that the location is in fact a finder * pattern center * * @param stateCount reading state module counts from horizontal scan * @param i row where finder pattern may be found * @param j end of possible finder pattern in row * @param pureBarcode true if in "pure barcode" mode * @return true if a finder pattern candidate was found this time */ handlePossibleCenter(stateCount, i, j, pureBarcode) { const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4]; let centerJ = FinderPatternFinder.centerFromEnd(stateCount, j); let centerI = this.crossCheckVertical( i, /*(int) */ Math.floor(centerJ), stateCount[2], stateCountTotal ); if (!isNaN(centerI)) { centerJ = this.crossCheckHorizontal( /*(int) */ Math.floor(centerJ), /*(int) */ Math.floor(centerI), stateCount[2], stateCountTotal ); if (!isNaN(centerJ) && (!pureBarcode || this.crossCheckDiagonal( /*(int) */ Math.floor(centerI), /*(int) */ Math.floor(centerJ), stateCount[2], stateCountTotal ))) { const estimatedModuleSize = stateCountTotal / 7; let found = false; const possibleCenters = this.possibleCenters; for (let index = 0, length = possibleCenters.length; index < length; index++) { const center = possibleCenters[index]; if (center.aboutEquals(estimatedModuleSize, centerI, centerJ)) { possibleCenters[index] = center.combineEstimate(centerI, centerJ, estimatedModuleSize); found = true; break; } } if (!found) { const point = new FinderPattern$1(centerJ, centerI, estimatedModuleSize); possibleCenters.push(point); if (this.resultPointCallback !== null && this.resultPointCallback !== void 0) { this.resultPointCallback.foundPossibleResultPoint(point); } } return true; } } return false; } /** * @return number of rows we could safely skip during scanning, based on the first * two finder patterns that have been located. In some cases their position will * allow us to infer that the third pattern must lie below a certain point farther * down in the image. */ findRowSkip() { const max = this.possibleCenters.length; if (max <= 1) { return 0; } let firstConfirmedCenter = null; for (const center of this.possibleCenters) { if (center.getCount() >= FinderPatternFinder.CENTER_QUORUM) { if (firstConfirmedCenter == null) { firstConfirmedCenter = center; } else { this.hasSkipped = true; return ( /*(int) */ Math.floor((Math.abs(firstConfirmedCenter.getX() - center.getX()) - Math.abs(firstConfirmedCenter.getY() - center.getY())) / 2) ); } } } return 0; } /** * @return true iff we have found at least 3 finder patterns that have been detected * at least {@link #CENTER_QUORUM} times each, and, the estimated module size of the * candidates is "pretty similar" */ haveMultiplyConfirmedCenters() { let confirmedCount = 0; let totalModuleSize = 0; const max = this.possibleCenters.length; for (const pattern of this.possibleCenters) { if (pattern.getCount() >= FinderPatternFinder.CENTER_QUORUM) { confirmedCount++; totalModuleSize += pattern.getEstimatedModuleSize(); } } if (confirmedCount < 3) { return false; } const average = totalModuleSize / max; let totalDeviation = 0; for (const pattern of this.possibleCenters) { totalDeviation += Math.abs(pattern.getEstimatedModuleSize() - average); } return totalDeviation <= 0.05 * totalModuleSize; } /** * @return the 3 best {@link FinderPattern}s from our list of candidates. The "best" are * those that have been detected at least {@link #CENTER_QUORUM} times, and whose module * size differs from the average among those patterns the least * @throws NotFoundException if 3 such finder patterns do not exist */ selectBestPatterns() { const startSize = this.possibleCenters.length; if (startSize < 3) { throw new NotFoundException(); } const possibleCenters = this.possibleCenters; let average; if (startSize > 3) { let totalModuleSize = 0; let square = 0; for (const center of this.possibleCenters) { const size = center.getEstimatedModuleSize(); totalModuleSize += size; square += size * size; } average = totalModuleSize / startSize; let stdDev = Math.sqrt(square / startSize - average * average); possibleCenters.sort( /** *
Orders by furthest from average
*/ // FurthestFromAverageComparator implements ComparatorOrders by {@link FinderPattern#getCount()}, descending.
*/ // CenterComparator implements ComparatorDetects a QR Code in an image.
* * @return {@link DetectorResult} encapsulating results of detecting a QR Code * @throws NotFoundException if QR Code cannot be found * @throws FormatException if a QR Code cannot be decoded */ // public detect(): DetectorResult /*throws NotFoundException, FormatException*/ { // return detect(null) // } /** *Detects a QR Code in an image.
* * @param hints optional hints to detector * @return {@link DetectorResult} encapsulating results of detecting a QR Code * @throws NotFoundException if QR Code cannot be found * @throws FormatException if a QR Code cannot be decoded */ detect(hints) { this.resultPointCallback = hints === null || hints === void 0 ? null : ( /*(ResultPointCallback) */ hints.get(DecodeHintType$1.NEED_RESULT_POINT_CALLBACK) ); const finder = new FinderPatternFinder(this.image, this.resultPointCallback); const info = finder.find(hints); return this.processFinderPatternInfo(info); } processFinderPatternInfo(info) { const topLeft = info.getTopLeft(); const topRight = info.getTopRight(); const bottomLeft = info.getBottomLeft(); const moduleSize = this.calculateModuleSize(topLeft, topRight, bottomLeft); if (moduleSize < 1) { throw new NotFoundException("No pattern found in proccess finder."); } const dimension = Detector$2.computeDimension(topLeft, topRight, bottomLeft, moduleSize); const provisionalVersion = Version$1.getProvisionalVersionForDimension(dimension); const modulesBetweenFPCenters = provisionalVersion.getDimensionForVersion() - 7; let alignmentPattern = null; if (provisionalVersion.getAlignmentPatternCenters().length > 0) { const bottomRightX = topRight.getX() - topLeft.getX() + bottomLeft.getX(); const bottomRightY = topRight.getY() - topLeft.getY() + bottomLeft.getY(); const correctionToTopLeft = 1 - 3 / modulesBetweenFPCenters; const estAlignmentX = ( /*(int) */ Math.floor(topLeft.getX() + correctionToTopLeft * (bottomRightX - topLeft.getX())) ); const estAlignmentY = ( /*(int) */ Math.floor(topLeft.getY() + correctionToTopLeft * (bottomRightY - topLeft.getY())) ); for (let i = 4; i <= 16; i <<= 1) { try { alignmentPattern = this.findAlignmentInRegion(moduleSize, estAlignmentX, estAlignmentY, i); break; } catch (re) { if (!(re instanceof NotFoundException)) { throw re; } } } } const transform = Detector$2.createTransform(topLeft, topRight, bottomLeft, alignmentPattern, dimension); const bits = Detector$2.sampleGrid(this.image, transform, dimension); let points; if (alignmentPattern === null) { points = [bottomLeft, topLeft, topRight]; } else { points = [bottomLeft, topLeft, topRight, alignmentPattern]; } return new DetectorResult(bits, points); } static createTransform(topLeft, topRight, bottomLeft, alignmentPattern, dimension) { const dimMinusThree = dimension - 3.5; let bottomRightX; let bottomRightY; let sourceBottomRightX; let sourceBottomRightY; if (alignmentPattern !== null) { bottomRightX = alignmentPattern.getX(); bottomRightY = alignmentPattern.getY(); sourceBottomRightX = dimMinusThree - 3; sourceBottomRightY = sourceBottomRightX; } else { bottomRightX = topRight.getX() - topLeft.getX() + bottomLeft.getX(); bottomRightY = topRight.getY() - topLeft.getY() + bottomLeft.getY(); sourceBottomRightX = dimMinusThree; sourceBottomRightY = dimMinusThree; } return PerspectiveTransform.quadrilateralToQuadrilateral(3.5, 3.5, dimMinusThree, 3.5, sourceBottomRightX, sourceBottomRightY, 3.5, dimMinusThree, topLeft.getX(), topLeft.getY(), topRight.getX(), topRight.getY(), bottomRightX, bottomRightY, bottomLeft.getX(), bottomLeft.getY()); } static sampleGrid(image, transform, dimension) { const sampler = GridSamplerInstance.getInstance(); return sampler.sampleGridWithTransform(image, dimension, dimension, transform); } /** *Computes the dimension (number of modules on a size) of the QR Code based on the position * of the finder patterns and estimated module size.
*/ static computeDimension(topLeft, topRight, bottomLeft, moduleSize) { const tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize); const tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize); let dimension = Math.floor((tltrCentersDimension + tlblCentersDimension) / 2) + 7; switch (dimension & 3) { // mod 4 case 0: dimension++; break; // 1? do nothing case 2: dimension--; break; case 3: throw new NotFoundException("Dimensions could be not found."); } return dimension; } /** *Computes an average estimated module size based on estimated derived from the positions * of the three finder patterns.
* * @param topLeft detected top-left finder pattern center * @param topRight detected top-right finder pattern center * @param bottomLeft detected bottom-left finder pattern center * @return estimated module size */ calculateModuleSize(topLeft, topRight, bottomLeft) { return (this.calculateModuleSizeOneWay(topLeft, topRight) + this.calculateModuleSizeOneWay(topLeft, bottomLeft)) / 2; } /** *Estimates module size based on two finder patterns -- it uses * {@link #sizeOfBlackWhiteBlackRunBothWays(int, int, int, int)} to figure the * width of each, measuring along the axis between their centers.
*/ calculateModuleSizeOneWay(pattern, otherPattern) { const moduleSizeEst1 = this.sizeOfBlackWhiteBlackRunBothWays( /*(int) */ Math.floor(pattern.getX()), /*(int) */ Math.floor(pattern.getY()), /*(int) */ Math.floor(otherPattern.getX()), /*(int) */ Math.floor(otherPattern.getY()) ); const moduleSizeEst2 = this.sizeOfBlackWhiteBlackRunBothWays( /*(int) */ Math.floor(otherPattern.getX()), /*(int) */ Math.floor(otherPattern.getY()), /*(int) */ Math.floor(pattern.getX()), /*(int) */ Math.floor(pattern.getY()) ); if (isNaN(moduleSizeEst1)) { return moduleSizeEst2 / 7; } if (isNaN(moduleSizeEst2)) { return moduleSizeEst1 / 7; } return (moduleSizeEst1 + moduleSizeEst2) / 14; } /** * See {@link #sizeOfBlackWhiteBlackRun(int, int, int, int)}; computes the total width of * a finder pattern by looking for a black-white-black run from the center in the direction * of another point (another finder pattern center), and in the opposite direction too. */ sizeOfBlackWhiteBlackRunBothWays(fromX, fromY, toX, toY) { let result = this.sizeOfBlackWhiteBlackRun(fromX, fromY, toX, toY); let scale = 1; let otherToX = fromX - (toX - fromX); if (otherToX < 0) { scale = fromX / /*(float) */ (fromX - otherToX); otherToX = 0; } else if (otherToX >= this.image.getWidth()) { scale = (this.image.getWidth() - 1 - fromX) / /*(float) */ (otherToX - fromX); otherToX = this.image.getWidth() - 1; } let otherToY = ( /*(int) */ Math.floor(fromY - (toY - fromY) * scale) ); scale = 1; if (otherToY < 0) { scale = fromY / /*(float) */ (fromY - otherToY); otherToY = 0; } else if (otherToY >= this.image.getHeight()) { scale = (this.image.getHeight() - 1 - fromY) / /*(float) */ (otherToY - fromY); otherToY = this.image.getHeight() - 1; } otherToX = /*(int) */ Math.floor(fromX + (otherToX - fromX) * scale); result += this.sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY); return result - 1; } /** *This method traces a line from a point in the image, in the direction towards another point. * It begins in a black region, and keeps going until it finds white, then black, then white again. * It reports the distance from the start to this point.
* *This is used when figuring out how wide a finder pattern is, when the finder pattern * may be skewed or rotated.
*/ sizeOfBlackWhiteBlackRun(fromX, fromY, toX, toY) { const steep = Math.abs(toY - fromY) > Math.abs(toX - fromX); if (steep) { let temp = fromX; fromX = fromY; fromY = temp; temp = toX; toX = toY; toY = temp; } const dx = Math.abs(toX - fromX); const dy = Math.abs(toY - fromY); let error = -dx / 2; const xstep = fromX < toX ? 1 : -1; const ystep = fromY < toY ? 1 : -1; let state = 0; const xLimit = toX + xstep; for (let x = fromX, y = fromY; x !== xLimit; x += xstep) { const realX = steep ? y : x; const realY = steep ? x : y; if (state === 1 === this.image.get(realX, realY)) { if (state === 2) { return MathUtils.distance(x, y, fromX, fromY); } state++; } error += dy; if (error > 0) { if (y === toY) { break; } y += ystep; error -= dx; } } if (state === 2) { return MathUtils.distance(toX + xstep, toY, fromX, fromY); } return NaN; } /** *Attempts to locate an alignment pattern in a limited region of the image, which is * guessed to contain it. This method uses {@link AlignmentPattern}.
* * @param overallEstModuleSize estimated module size so far * @param estAlignmentX x coordinate of center of area probably containing alignment pattern * @param estAlignmentY y coordinate of above * @param allowanceFactor number of pixels in all directions to search from the center * @return {@link AlignmentPattern} if found, or null otherwise * @throws NotFoundException if an unexpected error occurs during detection */ findAlignmentInRegion(overallEstModuleSize, estAlignmentX, estAlignmentY, allowanceFactor) { const allowance = ( /*(int) */ Math.floor(allowanceFactor * overallEstModuleSize) ); const alignmentAreaLeftX = Math.max(0, estAlignmentX - allowance); const alignmentAreaRightX = Math.min(this.image.getWidth() - 1, estAlignmentX + allowance); if (alignmentAreaRightX - alignmentAreaLeftX < overallEstModuleSize * 3) { throw new NotFoundException("Alignment top exceeds estimated module size."); } const alignmentAreaTopY = Math.max(0, estAlignmentY - allowance); const alignmentAreaBottomY = Math.min(this.image.getHeight() - 1, estAlignmentY + allowance); if (alignmentAreaBottomY - alignmentAreaTopY < overallEstModuleSize * 3) { throw new NotFoundException("Alignment bottom exceeds estimated module size."); } const alignmentFinder = new AlignmentPatternFinder(this.image, alignmentAreaLeftX, alignmentAreaTopY, alignmentAreaRightX - alignmentAreaLeftX, alignmentAreaBottomY - alignmentAreaTopY, overallEstModuleSize, this.resultPointCallback); return alignmentFinder.find(); } } class QRCodeReader { constructor() { this.decoder = new Decoder$2(); } getDecoder() { return this.decoder; } /** * Locates and decodes a QR code in an image. * * @return a representing: string the content encoded by the QR code * @throws NotFoundException if a QR code cannot be found * @throws FormatException if a QR code cannot be decoded * @throws ChecksumException if error correction fails */ /*@Override*/ // public decode(image: BinaryBitmap): Result /*throws NotFoundException, ChecksumException, FormatException */ { // return this.decode(image, null) // } /*@Override*/ decode(image, hints) { let decoderResult; let points; if (hints !== void 0 && hints !== null && void 0 !== hints.get(DecodeHintType$1.PURE_BARCODE)) { const bits = QRCodeReader.extractPureBits(image.getBlackMatrix()); decoderResult = this.decoder.decodeBitMatrix(bits, hints); points = QRCodeReader.NO_POINTS; } else { const detectorResult = new Detector$2(image.getBlackMatrix()).detect(hints); decoderResult = this.decoder.decodeBitMatrix(detectorResult.getBits(), hints); points = detectorResult.getPoints(); } if (decoderResult.getOther() instanceof QRCodeDecoderMetaData) { decoderResult.getOther().applyMirroredCorrection(points); } const result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), void 0, points, BarcodeFormat$1.QR_CODE, void 0); const byteSegments = decoderResult.getByteSegments(); if (byteSegments !== null) { result.putMetadata(ResultMetadataType$1.BYTE_SEGMENTS, byteSegments); } const ecLevel = decoderResult.getECLevel(); if (ecLevel !== null) { result.putMetadata(ResultMetadataType$1.ERROR_CORRECTION_LEVEL, ecLevel); } if (decoderResult.hasStructuredAppend()) { result.putMetadata(ResultMetadataType$1.STRUCTURED_APPEND_SEQUENCE, decoderResult.getStructuredAppendSequenceNumber()); result.putMetadata(ResultMetadataType$1.STRUCTURED_APPEND_PARITY, decoderResult.getStructuredAppendParity()); } return result; } /*@Override*/ reset() { } /** * This method detects a code in a "pure" image -- that is, pure monochrome image * which contains only an unrotated, unskewed, image of a code, with some white border * around it. This is a specialized method that works exceptionally fast in this special * case. * * @see com.google.zxing.datamatrix.DataMatrixReader#extractPureBits(BitMatrix) */ static extractPureBits(image) { const leftTopBlack = image.getTopLeftOnBit(); const rightBottomBlack = image.getBottomRightOnBit(); if (leftTopBlack === null || rightBottomBlack === null) { throw new NotFoundException(); } const moduleSize = this.moduleSize(leftTopBlack, image); let top = leftTopBlack[1]; let bottom = rightBottomBlack[1]; let left = leftTopBlack[0]; let right = rightBottomBlack[0]; if (left >= right || top >= bottom) { throw new NotFoundException(); } if (bottom - top !== right - left) { right = left + (bottom - top); if (right >= image.getWidth()) { throw new NotFoundException(); } } const matrixWidth = Math.round((right - left + 1) / moduleSize); const matrixHeight = Math.round((bottom - top + 1) / moduleSize); if (matrixWidth <= 0 || matrixHeight <= 0) { throw new NotFoundException(); } if (matrixHeight !== matrixWidth) { throw new NotFoundException(); } const nudge = ( /*(int) */ Math.floor(moduleSize / 2) ); top += nudge; left += nudge; const nudgedTooFarRight = left + /*(int) */ Math.floor((matrixWidth - 1) * moduleSize) - right; if (nudgedTooFarRight > 0) { if (nudgedTooFarRight > nudge) { throw new NotFoundException(); } left -= nudgedTooFarRight; } const nudgedTooFarDown = top + /*(int) */ Math.floor((matrixHeight - 1) * moduleSize) - bottom; if (nudgedTooFarDown > 0) { if (nudgedTooFarDown > nudge) { throw new NotFoundException(); } top -= nudgedTooFarDown; } const bits = new BitMatrix(matrixWidth, matrixHeight); for (let y = 0; y < matrixHeight; y++) { const iOffset = top + /*(int) */ Math.floor(y * moduleSize); for (let x = 0; x < matrixWidth; x++) { if (image.get(left + /*(int) */ Math.floor(x * moduleSize), iOffset)) { bits.set(x, y); } } } return bits; } static moduleSize(leftTopBlack, image) { const height = image.getHeight(); const width = image.getWidth(); let x = leftTopBlack[0]; let y = leftTopBlack[1]; let inBlack = true; let transitions = 0; while (x < width && y < height) { if (inBlack !== image.get(x, y)) { if (++transitions === 5) { break; } inBlack = !inBlack; } x++; y++; } if (x === width || y === height) { throw new NotFoundException(); } return (x - leftTopBlack[0]) / 7; } } QRCodeReader.NO_POINTS = new Array(); class PDF417Common { PDF417Common() { } /** * @param moduleBitCount values to sum * @return sum of values * @deprecated call {@link MathUtils#sum(int[])} */ // @Deprecated static getBitCountSum(moduleBitCount) { return MathUtils.sum(moduleBitCount); } static toIntArray(list) { if (list == null || !list.length) { return PDF417Common.EMPTY_INT_ARRAY; } const result = new Int32Array(list.length); let i = 0; for (const integer of list) { result[i++] = integer; } return result; } /** * @param symbol encoded symbol to translate to a codeword * @return the codeword corresponding to the symbol. */ static getCodeword(symbol) { const i = Arrays.binarySearch(PDF417Common.SYMBOL_TABLE, symbol & 262143); if (i < 0) { return -1; } return (PDF417Common.CODEWORD_TABLE[i] - 1) % PDF417Common.NUMBER_OF_CODEWORDS; } } PDF417Common.NUMBER_OF_CODEWORDS = 929; PDF417Common.MAX_CODEWORDS_IN_BARCODE = PDF417Common.NUMBER_OF_CODEWORDS - 1; PDF417Common.MIN_ROWS_IN_BARCODE = 3; PDF417Common.MAX_ROWS_IN_BARCODE = 90; PDF417Common.MODULES_IN_CODEWORD = 17; PDF417Common.MODULES_IN_STOP_PATTERN = 18; PDF417Common.BARS_IN_MODULE = 8; PDF417Common.EMPTY_INT_ARRAY = new Int32Array([]); PDF417Common.SYMBOL_TABLE = Int32Array.from([ 66142, 66170, 66206, 66236, 66290, 66292, 66350, 66382, 66396, 66454, 66470, 66476, 66594, 66600, 66614, 66626, 66628, 66632, 66640, 66654, 66662, 66668, 66682, 66690, 66718, 66720, 66748, 66758, 66776, 66798, 66802, 66804, 66820, 66824, 66832, 66846, 66848, 66876, 66880, 66936, 66950, 66956, 66968, 66992, 67006, 67022, 67036, 67042, 67044, 67048, 67062, 67118, 67150, 67164, 67214, 67228, 67256, 67294, 67322, 67350, 67366, 67372, 67398, 67404, 67416, 67438, 67474, 67476, 67490, 67492, 67496, 67510, 67618, 67624, 67650, 67656, 67664, 67678, 67686, 67692, 67706, 67714, 67716, 67728, 67742, 67744, 67772, 67782, 67788, 67800, 67822, 67826, 67828, 67842, 67848, 67870, 67872, 67900, 67904, 67960, 67974, 67992, 68016, 68030, 68046, 68060, 68066, 68068, 68072, 68086, 68104, 68112, 68126, 68128, 68156, 68160, 68216, 68336, 68358, 68364, 68376, 68400, 68414, 68448, 68476, 68494, 68508, 68536, 68546, 68548, 68552, 68560, 68574, 68582, 68588, 68654, 68686, 68700, 68706, 68708, 68712, 68726, 68750, 68764, 68792, 68802, 68804, 68808, 68816, 68830, 68838, 68844, 68858, 68878, 68892, 68920, 68976, 68990, 68994, 68996, 69e3, 69008, 69022, 69024, 69052, 69062, 69068, 69080, 69102, 69106, 69108, 69142, 69158, 69164, 69190, 69208, 69230, 69254, 69260, 69272, 69296, 69310, 69326, 69340, 69386, 69394, 69396, 69410, 69416, 69430, 69442, 69444, 69448, 69456, 69470, 69478, 69484, 69554, 69556, 69666, 69672, 69698, 69704, 69712, 69726, 69754, 69762, 69764, 69776, 69790, 69792, 69820, 69830, 69836, 69848, 69870, 69874, 69876, 69890, 69918, 69920, 69948, 69952, 70008, 70022, 70040, 70064, 70078, 70094, 70108, 70114, 70116, 70120, 70134, 70152, 70174, 70176, 70264, 70384, 70412, 70448, 70462, 70496, 70524, 70542, 70556, 70584, 70594, 70600, 70608, 70622, 70630, 70636, 70664, 70672, 70686, 70688, 70716, 70720, 70776, 70896, 71136, 71180, 71192, 71216, 71230, 71264, 71292, 71360, 71416, 71452, 71480, 71536, 71550, 71554, 71556, 71560, 71568, 71582, 71584, 71612, 71622, 71628, 71640, 71662, 71726, 71732, 71758, 71772, 71778, 71780, 71784, 71798, 71822, 71836, 71864, 71874, 71880, 71888, 71902, 71910, 71916, 71930, 71950, 71964, 71992, 72048, 72062, 72066, 72068, 72080, 72094, 72096, 72124, 72134, 72140, 72152, 72174, 72178, 72180, 72206, 72220, 72248, 72304, 72318, 72416, 72444, 72456, 72464, 72478, 72480, 72508, 72512, 72568, 72588, 72600, 72624, 72638, 72654, 72668, 72674, 72676, 72680, 72694, 72726, 72742, 72748, 72774, 72780, 72792, 72814, 72838, 72856, 72880, 72894, 72910, 72924, 72930, 72932, 72936, 72950, 72966, 72972, 72984, 73008, 73022, 73056, 73084, 73102, 73116, 73144, 73156, 73160, 73168, 73182, 73190, 73196, 73210, 73226, 73234, 73236, 73250, 73252, 73256, 73270, 73282, 73284, 73296, 73310, 73318, 73324, 73346, 73348, 73352, 73360, 73374, 73376, 73404, 73414, 73420, 73432, 73454, 73498, 73518, 73522, 73524, 73550, 73564, 73570, 73572, 73576, 73590, 73800, 73822, 73858, 73860, 73872, 73886, 73888, 73916, 73944, 73970, 73972, 73992, 74014, 74016, 74044, 74048, 74104, 74118, 74136, 74160, 74174, 74210, 74212, 74216, 74230, 74244, 74256, 74270, 74272, 74360, 74480, 74502, 74508, 74544, 74558, 74592, 74620, 74638, 74652, 74680, 74690, 74696, 74704, 74726, 74732, 74782, 74784, 74812, 74992, 75232, 75288, 75326, 75360, 75388, 75456, 75512, 75576, 75632, 75646, 75650, 75652, 75664, 75678, 75680, 75708, 75718, 75724, 75736, 75758, 75808, 75836, 75840, 75896, 76016, 76256, 76736, 76824, 76848, 76862, 76896, 76924, 76992, 77048, 77296, 77340, 77368, 77424, 77438, 77536, 77564, 77572, 77576, 77584, 77600, 77628, 77632, 77688, 77702, 77708, 77720, 77744, 77758, 77774, 77788, 77870, 77902, 77916, 77922, 77928, 77966, 77980, 78008, 78018, 78024, 78032, 78046, 78060, 78074, 78094, 78136, 78192, 78206, 78210, 78212, 78224, 78238, 78240, 78268, 78278, 78284, 78296, 78322, 78324, 78350, 78364, 78448, 78462, 78560, 78588, 78600, 78622, 78624, 78652, 78656, 78712, 78726, 78744, 78768, 78782, 78798, 78812, 78818, 78820, 78824, 78838, 78862, 78876, 78904, 78960, 78974, 79072, 79100, 79296, 79352, 79368, 79376, 79390, 79392, 79420, 79424, 79480, 79600, 79628, 79640, 79664, 79678, 79712, 79740, 79772, 79800, 79810, 79812, 79816, 79824, 79838, 79846, 79852, 79894, 79910, 79916, 79942, 79948, 79960, 79982, 79988, 80006, 80024, 80048, 80062, 80078, 80092, 80098, 80100, 80104, 80134, 80140, 80176, 80190, 80224, 80252, 80270, 80284, 80312, 80328, 80336, 80350, 80358, 80364, 80378, 80390, 80396, 80408, 80432, 80446, 80480, 80508, 80576, 80632, 80654, 80668, 80696, 80752, 80766, 80776, 80784, 80798, 80800, 80828, 80844, 80856, 80878, 80882, 80884, 80914, 80916, 80930, 80932, 80936, 80950, 80962, 80968, 80976, 80990, 80998, 81004, 81026, 81028, 81040, 81054, 81056, 81084, 81094, 81100, 81112, 81134, 81154, 81156, 81160, 81168, 81182, 81184, 81212, 81216, 81272, 81286, 81292, 81304, 81328, 81342, 81358, 81372, 81380, 81384, 81398, 81434, 81454, 81458, 81460, 81486, 81500, 81506, 81508, 81512, 81526, 81550, 81564, 81592, 81602, 81604, 81608, 81616, 81630, 81638, 81644, 81702, 81708, 81722, 81734, 81740, 81752, 81774, 81778, 81780, 82050, 82078, 82080, 82108, 82180, 82184, 82192, 82206, 82208, 82236, 82240, 82296, 82316, 82328, 82352, 82366, 82402, 82404, 82408, 82440, 82448, 82462, 82464, 82492, 82496, 82552, 82672, 82694, 82700, 82712, 82736, 82750, 82784, 82812, 82830, 82882, 82884, 82888, 82896, 82918, 82924, 82952, 82960, 82974, 82976, 83004, 83008, 83064, 83184, 83424, 83468, 83480, 83504, 83518, 83552, 83580, 83648, 83704, 83740, 83768, 83824, 83838, 83842, 83844, 83848, 83856, 83872, 83900, 83910, 83916, 83928, 83950, 83984, 84e3, 84028, 84032, 84088, 84208, 84448, 84928, 85040, 85054, 85088, 85116, 85184, 85240, 85488, 85560, 85616, 85630, 85728, 85756, 85764, 85768, 85776, 85790, 85792, 85820, 85824, 85880, 85894, 85900, 85912, 85936, 85966, 85980, 86048, 86080, 86136, 86256, 86496, 86976, 88160, 88188, 88256, 88312, 88560, 89056, 89200, 89214, 89312, 89340, 89536, 89592, 89608, 89616, 89632, 89664, 89720, 89840, 89868, 89880, 89904, 89952, 89980, 89998, 90012, 90040, 90190, 90204, 90254, 90268, 90296, 90306, 90308, 90312, 90334, 90382, 90396, 90424, 90480, 90494, 90500, 90504, 90512, 90526, 90528, 90556, 90566, 90572, 90584, 90610, 90612, 90638, 90652, 90680, 90736, 90750, 90848, 90876, 90884, 90888, 90896, 90910, 90912, 90940, 90944, 91e3, 91014, 91020, 91032, 91056, 91070, 91086, 91100, 91106, 91108, 91112, 91126, 91150, 91164, 91192, 91248, 91262, 91360, 91388, 91584, 91640, 91664, 91678, 91680, 91708, 91712, 91768, 91888, 91928, 91952, 91966, 92e3, 92028, 92046, 92060, 92088, 92098, 92100, 92104, 92112, 92126, 92134, 92140, 92188, 92216, 92272, 92384, 92412, 92608, 92664, 93168, 93200, 93214, 93216, 93244, 93248, 93304, 93424, 93664, 93720, 93744, 93758, 93792, 93820, 93888, 93944, 93980, 94008, 94064, 94078, 94084, 94088, 94096, 94110, 94112, 94140, 94150, 94156, 94168, 94246, 94252, 94278, 94284, 94296, 94318, 94342, 94348, 94360, 94384, 94398, 94414, 94428, 94440, 94470, 94476, 94488, 94512, 94526, 94560, 94588, 94606, 94620, 94648, 94658, 94660, 94664, 94672, 94686, 94694, 94700, 94714, 94726, 94732, 94744, 94768, 94782, 94816, 94844, 94912, 94968, 94990, 95004, 95032, 95088, 95102, 95112, 95120, 95134, 95136, 95164, 95180, 95192, 95214, 95218, 95220, 95244, 95256, 95280, 95294, 95328, 95356, 95424, 95480, 95728, 95758, 95772, 95800, 95856, 95870, 95968, 95996, 96008, 96016, 96030, 96032, 96060, 96064, 96120, 96152, 96176, 96190, 96220, 96226, 96228, 96232, 96290, 96292, 96296, 96310, 96322, 96324, 96328, 96336, 96350, 96358, 96364, 96386, 96388, 96392, 96400, 96414, 96416, 96444, 96454, 96460, 96472, 96494, 96498, 96500, 96514, 96516, 96520, 96528, 96542, 96544, 96572, 96576, 96632, 96646, 96652, 96664, 96688, 96702, 96718, 96732, 96738, 96740, 96744, 96758, 96772, 96776, 96784, 96798, 96800, 96828, 96832, 96888, 97008, 97030, 97036, 97048, 97072, 97086, 97120, 97148, 97166, 97180, 97208, 97220, 97224, 97232, 97246, 97254, 97260, 97326, 97330, 97332, 97358, 97372, 97378, 97380, 97384, 97398, 97422, 97436, 97464, 97474, 97476, 97480, 97488, 97502, 97510, 97516, 97550, 97564, 97592, 97648, 97666, 97668, 97672, 97680, 97694, 97696, 97724, 97734, 97740, 97752, 97774, 97830, 97836, 97850, 97862, 97868, 97880, 97902, 97906, 97908, 97926, 97932, 97944, 97968, 97998, 98012, 98018, 98020, 98024, 98038, 98618, 98674, 98676, 98838, 98854, 98874, 98892, 98904, 98926, 98930, 98932, 98968, 99006, 99042, 99044, 99048, 99062, 99166, 99194, 99246, 99286, 99350, 99366, 99372, 99386, 99398, 99416, 99438, 99442, 99444, 99462, 99504, 99518, 99534, 99548, 99554, 99556, 99560, 99574, 99590, 99596, 99608, 99632, 99646, 99680, 99708, 99726, 99740, 99768, 99778, 99780, 99784, 99792, 99806, 99814, 99820, 99834, 99858, 99860, 99874, 99880, 99894, 99906, 99920, 99934, 99962, 99970, 99972, 99976, 99984, 99998, 1e5, 100028, 100038, 100044, 100056, 100078, 100082, 100084, 100142, 100174, 100188, 100246, 100262, 100268, 100306, 100308, 100390, 100396, 100410, 100422, 100428, 100440, 100462, 100466, 100468, 100486, 100504, 100528, 100542, 100558, 100572, 100578, 100580, 100584, 100598, 100620, 100656, 100670, 100704, 100732, 100750, 100792, 100802, 100808, 100816, 100830, 100838, 100844, 100858, 100888, 100912, 100926, 100960, 100988, 101056, 101112, 101148, 101176, 101232, 101246, 101250, 101252, 101256, 101264, 101278, 101280, 101308, 101318, 101324, 101336, 101358, 101362, 101364, 101410, 101412, 101416, 101430, 101442, 101448, 101456, 101470, 101478, 101498, 101506, 101508, 101520, 101534, 101536, 101564, 101580, 101618, 101620, 101636, 101640, 101648, 101662, 101664, 101692, 101696, 101752, 101766, 101784, 101838, 101858, 101860, 101864, 101934, 101938, 101940, 101966, 101980, 101986, 101988, 101992, 102030, 102044, 102072, 102082, 102084, 102088, 102096, 102138, 102166, 102182, 102188, 102214, 102220, 102232, 102254, 102282, 102290, 102292, 102306, 102308, 102312, 102326, 102444, 102458, 102470, 102476, 102488, 102514, 102516, 102534, 102552, 102576, 102590, 102606, 102620, 102626, 102632, 102646, 102662, 102668, 102704, 102718, 102752, 102780, 102798, 102812, 102840, 102850, 102856, 102864, 102878, 102886, 102892, 102906, 102936, 102974, 103008, 103036, 103104, 103160, 103224, 103280, 103294, 103298, 103300, 103312, 103326, 103328, 103356, 103366, 103372, 103384, 103406, 103410, 103412, 103472, 103486, 103520, 103548, 103616, 103672, 103920, 103992, 104048, 104062, 104160, 104188, 104194, 104196, 104200, 104208, 104224, 104252, 104256, 104312, 104326, 104332, 104344, 104368, 104382, 104398, 104412, 104418, 104420, 104424, 104482, 104484, 104514, 104520, 104528, 104542, 104550, 104570, 104578, 104580, 104592, 104606, 104608, 104636, 104652, 104690, 104692, 104706, 104712, 104734, 104736, 104764, 104768, 104824, 104838, 104856, 104910, 104930, 104932, 104936, 104968, 104976, 104990, 104992, 105020, 105024, 105080, 105200, 105240, 105278, 105312, 105372, 105410, 105412, 105416, 105424, 105446, 105518, 105524, 105550, 105564, 105570, 105572, 105576, 105614, 105628, 105656, 105666, 105672, 105680, 105702, 105722, 105742, 105756, 105784, 105840, 105854, 105858, 105860, 105864, 105872, 105888, 105932, 105970, 105972, 106006, 106022, 106028, 106054, 106060, 106072, 106100, 106118, 106124, 106136, 106160, 106174, 106190, 106210, 106212, 106216, 106250, 106258, 106260, 106274, 106276, 106280, 106306, 106308, 106312, 106320, 106334, 106348, 106394, 106414, 106418, 106420, 106566, 106572, 106610, 106612, 106630, 106636, 106648, 106672, 106686, 106722, 106724, 106728, 106742, 106758, 106764, 106776, 106800, 106814, 106848, 106876, 106894, 106908, 106936, 106946, 106948, 106952, 106960, 106974, 106982, 106988, 107032, 107056, 107070, 107104, 107132, 107200, 107256, 107292, 107320, 107376, 107390, 107394, 107396, 107400, 107408, 107422, 107424, 107452, 107462, 107468, 107480, 107502, 107506, 107508, 107544, 107568, 107582, 107616, 107644, 107712, 107768, 108016, 108060, 108088, 108144, 108158, 108256, 108284, 108290, 108292, 108296, 108304, 108318, 108320, 108348, 108352, 108408, 108422, 108428, 108440, 108464, 108478, 108494, 108508, 108514, 108516, 108520, 108592, 108640, 108668, 108736, 108792, 109040, 109536, 109680, 109694, 109792, 109820, 110016, 110072, 110084, 110088, 110096, 110112, 110140, 110144, 110200, 110320, 110342, 110348, 110360, 110384, 110398, 110432, 110460, 110478, 110492, 110520, 110532, 110536, 110544, 110558, 110658, 110686, 110714, 110722, 110724, 110728, 110736, 110750, 110752, 110780, 110796, 110834, 110836, 110850, 110852, 110856, 110864, 110878, 110880, 110908, 110912, 110968, 110982, 111e3, 111054, 111074, 111076, 111080, 111108, 111112, 111120, 111134, 111136, 111164, 111168, 111224, 111344, 111372, 111422, 111456, 111516, 111554, 111556, 111560, 111568, 111590, 111632, 111646, 111648, 111676, 111680, 111736, 111856, 112096, 112152, 112224, 112252, 112320, 112440, 112514, 112516, 112520, 112528, 112542, 112544, 112588, 112686, 112718, 112732, 112782, 112796, 112824, 112834, 112836, 112840, 112848, 112870, 112890, 112910, 112924, 112952, 113008, 113022, 113026, 113028, 113032, 113040, 113054, 113056, 113100, 113138, 113140, 113166, 113180, 113208, 113264, 113278, 113376, 113404, 113416, 113424, 113440, 113468, 113472, 113560, 113614, 113634, 113636, 113640, 113686, 113702, 113708, 113734, 113740, 113752, 113778, 113780, 113798, 113804, 113816, 113840, 113854, 113870, 113890, 113892, 113896, 113926, 113932, 113944, 113968, 113982, 114016, 114044, 114076, 114114, 114116, 114120, 114128, 114150, 114170, 114194, 114196, 114210, 114212, 114216, 114242, 114244, 114248, 114256, 114270, 114278, 114306, 114308, 114312, 114320, 114334, 114336, 114364, 114380, 114420, 114458, 114478, 114482, 114484, 114510, 114524, 114530, 114532, 114536, 114842, 114866, 114868, 114970, 114994, 114996, 115042, 115044, 115048, 115062, 115130, 115226, 115250, 115252, 115278, 115292, 115298, 115300, 115304, 115318, 115342, 115394, 115396, 115400, 115408, 115422, 115430, 115436, 115450, 115478, 115494, 115514, 115526, 115532, 115570, 115572, 115738, 115758, 115762, 115764, 115790, 115804, 115810, 115812, 115816, 115830, 115854, 115868, 115896, 115906, 115912, 115920, 115934, 115942, 115948, 115962, 115996, 116024, 116080, 116094, 116098, 116100, 116104, 116112, 116126, 116128, 116156, 116166, 116172, 116184, 116206, 116210, 116212, 116246, 116262, 116268, 116282, 116294, 116300, 116312, 116334, 116338, 116340, 116358, 116364, 116376, 116400, 116414, 116430, 116444, 116450, 116452, 116456, 116498, 116500, 116514, 116520, 116534, 116546, 116548, 116552, 116560, 116574, 116582, 116588, 116602, 116654, 116694, 116714, 116762, 116782, 116786, 116788, 116814, 116828, 116834, 116836, 116840, 116854, 116878, 116892, 116920, 116930, 116936, 116944, 116958, 116966, 116972, 116986, 117006, 117048, 117104, 117118, 117122, 117124, 117136, 117150, 117152, 117180, 117190, 117196, 117208, 117230, 117234, 117236, 117304, 117360, 117374, 117472, 117500, 117506, 117508, 117512, 117520, 117536, 117564, 117568, 117624, 117638, 117644, 117656, 117680, 117694, 117710, 117724, 117730, 117732, 117736, 117750, 117782, 117798, 117804, 117818, 117830, 117848, 117874, 117876, 117894, 117936, 117950, 117966, 117986, 117988, 117992, 118022, 118028, 118040, 118064, 118078, 118112, 118140, 118172, 118210, 118212, 118216, 118224, 118238, 118246, 118266, 118306, 118312, 118338, 118352, 118366, 118374, 118394, 118402, 118404, 118408, 118416, 118430, 118432, 118460, 118476, 118514, 118516, 118574, 118578, 118580, 118606, 118620, 118626, 118628, 118632, 118678, 118694, 118700, 118730, 118738, 118740, 118830, 118834, 118836, 118862, 118876, 118882, 118884, 118888, 118902, 118926, 118940, 118968, 118978, 118980, 118984, 118992, 119006, 119014, 119020, 119034, 119068, 119096, 119152, 119166, 119170, 119172, 119176, 119184, 119198, 119200, 119228, 119238, 119244, 119256, 119278, 119282, 119284, 119324, 119352, 119408, 119422, 119520, 119548, 119554, 119556, 119560, 119568, 119582, 119584, 119612, 119616, 119672, 119686, 119692, 119704, 119728, 119742, 119758, 119772, 119778, 119780, 119784, 119798, 119920, 119934, 120032, 120060, 120256, 120312, 120324, 120328, 120336, 120352, 120384, 120440, 120560, 120582, 120588, 120600, 120624, 120638, 120672, 120700, 120718, 120732, 120760, 120770, 120772, 120776, 120784, 120798, 120806, 120812, 120870, 120876, 120890, 120902, 120908, 120920, 120946, 120948, 120966, 120972, 120984, 121008, 121022, 121038, 121058, 121060, 121064, 121078, 121100, 121112, 121136, 121150, 121184, 121212, 121244, 121282, 121284, 121288, 121296, 121318, 121338, 121356, 121368, 121392, 121406, 121440, 121468, 121536, 121592, 121656, 121730, 121732, 121736, 121744, 121758, 121760, 121804, 121842, 121844, 121890, 121922, 121924, 121928, 121936, 121950, 121958, 121978, 121986, 121988, 121992, 122e3, 122014, 122016, 122044, 122060, 122098, 122100, 122116, 122120, 122128, 122142, 122144, 122172, 122176, 122232, 122246, 122264, 122318, 122338, 122340, 122344, 122414, 122418, 122420, 122446, 122460, 122466, 122468, 122472, 122510, 122524, 122552, 122562, 122564, 122568, 122576, 122598, 122618, 122646, 122662, 122668, 122694, 122700, 122712, 122738, 122740, 122762, 122770, 122772, 122786, 122788, 122792, 123018, 123026, 123028, 123042, 123044, 123048, 123062, 123098, 123146, 123154, 123156, 123170, 123172, 123176, 123190, 123202, 123204, 123208, 123216, 123238, 123244, 123258, 123290, 123314, 123316, 123402, 123410, 123412, 123426, 123428, 123432, 123446, 123458, 123464, 123472, 123486, 123494, 123500, 123514, 123522, 123524, 123528, 123536, 123552, 123580, 123590, 123596, 123608, 123630, 123634, 123636, 123674, 123698, 123700, 123740, 123746, 123748, 123752, 123834, 123914, 123922, 123924, 123938, 123944, 123958, 123970, 123976, 123984, 123998, 124006, 124012, 124026, 124034, 124036, 124048, 124062, 124064, 124092, 124102, 124108, 124120, 124142, 124146, 124148, 124162, 124164, 124168, 124176, 124190, 124192, 124220, 124224, 124280, 124294, 124300, 124312, 124336, 124350, 124366, 124380, 124386, 124388, 124392, 124406, 124442, 124462, 124466, 124468, 124494, 124508, 124514, 124520, 124558, 124572, 124600, 124610, 124612, 124616, 124624, 124646, 124666, 124694, 124710, 124716, 124730, 124742, 124748, 124760, 124786, 124788, 124818, 124820, 124834, 124836, 124840, 124854, 124946, 124948, 124962, 124964, 124968, 124982, 124994, 124996, 125e3, 125008, 125022, 125030, 125036, 125050, 125058, 125060, 125064, 125072, 125086, 125088, 125116, 125126, 125132, 125144, 125166, 125170, 125172, 125186, 125188, 125192, 125200, 125216, 125244, 125248, 125304, 125318, 125324, 125336, 125360, 125374, 125390, 125404, 125410, 125412, 125416, 125430, 125444, 125448, 125456, 125472, 125504, 125560, 125680, 125702, 125708, 125720, 125744, 125758, 125792, 125820, 125838, 125852, 125880, 125890, 125892, 125896, 125904, 125918, 125926, 125932, 125978, 125998, 126002, 126004, 126030, 126044, 126050, 126052, 126056, 126094, 126108, 126136, 126146, 126148, 126152, 126160, 126182, 126202, 126222, 126236, 126264, 126320, 126334, 126338, 126340, 126344, 126352, 126366, 126368, 126412, 126450, 126452, 126486, 126502, 126508, 126522, 126534, 126540, 126552, 126574, 126578, 126580, 126598, 126604, 126616, 126640, 126654, 126670, 126684, 126690, 126692, 126696, 126738, 126754, 126756, 126760, 126774, 126786, 126788, 126792, 126800, 126814, 126822, 126828, 126842, 126894, 126898, 126900, 126934, 127126, 127142, 127148, 127162, 127178, 127186, 127188, 127254, 127270, 127276, 127290, 127302, 127308, 127320, 127342, 127346, 127348, 127370, 127378, 127380, 127394, 127396, 127400, 127450, 127510, 127526, 127532, 127546, 127558, 127576, 127598, 127602, 127604, 127622, 127628, 127640, 127664, 127678, 127694, 127708, 127714, 127716, 127720, 127734, 127754, 127762, 127764, 127778, 127784, 127810, 127812, 127816, 127824, 127838, 127846, 127866, 127898, 127918, 127922, 127924, 128022, 128038, 128044, 128058, 128070, 128076, 128088, 128110, 128114, 128116, 128134, 128140, 128152, 128176, 128190, 128206, 128220, 128226, 128228, 128232, 128246, 128262, 128268, 128280, 128304, 128318, 128352, 128380, 128398, 128412, 128440, 128450, 128452, 128456, 128464, 128478, 128486, 128492, 128506, 128522, 128530, 128532, 128546, 128548, 128552, 128566, 128578, 128580, 128584, 128592, 128606, 128614, 128634, 128642, 128644, 128648, 128656, 128670, 128672, 128700, 128716, 128754, 128756, 128794, 128814, 128818, 128820, 128846, 128860, 128866, 128868, 128872, 128886, 128918, 128934, 128940, 128954, 128978, 128980, 129178, 129198, 129202, 129204, 129238, 129258, 129306, 129326, 129330, 129332, 129358, 129372, 129378, 129380, 129384, 129398, 129430, 129446, 129452, 129466, 129482, 129490, 129492, 129562, 129582, 129586, 129588, 129614, 129628, 129634, 129636, 129640, 129654, 129678, 129692, 129720, 129730, 129732, 129736, 129744, 129758, 129766, 129772, 129814, 129830, 129836, 129850, 129862, 129868, 129880, 129902, 129906, 129908, 129930, 129938, 129940, 129954, 129956, 129960, 129974, 130010 ]); PDF417Common.CODEWORD_TABLE = Int32Array.from([ 2627, 1819, 2622, 2621, 1813, 1812, 2729, 2724, 2723, 2779, 2774, 2773, 902, 896, 908, 868, 865, 861, 859, 2511, 873, 871, 1780, 835, 2493, 825, 2491, 842, 837, 844, 1764, 1762, 811, 810, 809, 2483, 807, 2482, 806, 2480, 815, 814, 813, 812, 2484, 817, 816, 1745, 1744, 1742, 1746, 2655, 2637, 2635, 2626, 2625, 2623, 2628, 1820, 2752, 2739, 2737, 2728, 2727, 2725, 2730, 2785, 2783, 2778, 2777, 2775, 2780, 787, 781, 747, 739, 736, 2413, 754, 752, 1719, 692, 689, 681, 2371, 678, 2369, 700, 697, 694, 703, 1688, 1686, 642, 638, 2343, 631, 2341, 627, 2338, 651, 646, 643, 2345, 654, 652, 1652, 1650, 1647, 1654, 601, 599, 2322, 596, 2321, 594, 2319, 2317, 611, 610, 608, 606, 2324, 603, 2323, 615, 614, 612, 1617, 1616, 1614, 1612, 616, 1619, 1618, 2575, 2538, 2536, 905, 901, 898, 909, 2509, 2507, 2504, 870, 867, 864, 860, 2512, 875, 872, 1781, 2490, 2489, 2487, 2485, 1748, 836, 834, 832, 830, 2494, 827, 2492, 843, 841, 839, 845, 1765, 1763, 2701, 2676, 2674, 2653, 2648, 2656, 2634, 2633, 2631, 2629, 1821, 2638, 2636, 2770, 2763, 2761, 2750, 2745, 2753, 2736, 2735, 2733, 2731, 1848, 2740, 2738, 2786, 2784, 591, 588, 576, 569, 566, 2296, 1590, 537, 534, 526, 2276, 522, 2274, 545, 542, 539, 548, 1572, 1570, 481, 2245, 466, 2242, 462, 2239, 492, 485, 482, 2249, 496, 494, 1534, 1531, 1528, 1538, 413, 2196, 406, 2191, 2188, 425, 419, 2202, 415, 2199, 432, 430, 427, 1472, 1467, 1464, 433, 1476, 1474, 368, 367, 2160, 365, 2159, 362, 2157, 2155, 2152, 378, 377, 375, 2166, 372, 2165, 369, 2162, 383, 381, 379, 2168, 1419, 1418, 1416, 1414, 385, 1411, 384, 1423, 1422, 1420, 1424, 2461, 802, 2441, 2439, 790, 786, 783, 794, 2409, 2406, 2403, 750, 742, 738, 2414, 756, 753, 1720, 2367, 2365, 2362, 2359, 1663, 693, 691, 684, 2373, 680, 2370, 702, 699, 696, 704, 1690, 1687, 2337, 2336, 2334, 2332, 1624, 2329, 1622, 640, 637, 2344, 634, 2342, 630, 2340, 650, 648, 645, 2346, 655, 653, 1653, 1651, 1649, 1655, 2612, 2597, 2595, 2571, 2568, 2565, 2576, 2534, 2529, 2526, 1787, 2540, 2537, 907, 904, 900, 910, 2503, 2502, 2500, 2498, 1768, 2495, 1767, 2510, 2508, 2506, 869, 866, 863, 2513, 876, 874, 1782, 2720, 2713, 2711, 2697, 2694, 2691, 2702, 2672, 2670, 2664, 1828, 2678, 2675, 2647, 2646, 2644, 2642, 1823, 2639, 1822, 2654, 2652, 2650, 2657, 2771, 1855, 2765, 2762, 1850, 1849, 2751, 2749, 2747, 2754, 353, 2148, 344, 342, 336, 2142, 332, 2140, 345, 1375, 1373, 306, 2130, 299, 2128, 295, 2125, 319, 314, 311, 2132, 1354, 1352, 1349, 1356, 262, 257, 2101, 253, 2096, 2093, 274, 273, 267, 2107, 263, 2104, 280, 278, 275, 1316, 1311, 1308, 1320, 1318, 2052, 202, 2050, 2044, 2040, 219, 2063, 212, 2060, 208, 2055, 224, 221, 2066, 1260, 1258, 1252, 231, 1248, 229, 1266, 1264, 1261, 1268, 155, 1998, 153, 1996, 1994, 1991, 1988, 165, 164, 2007, 162, 2006, 159, 2003, 2e3, 172, 171, 169, 2012, 166, 2010, 1186, 1184, 1182, 1179, 175, 1176, 173, 1192, 1191, 1189, 1187, 176, 1194, 1193, 2313, 2307, 2305, 592, 589, 2294, 2292, 2289, 578, 572, 568, 2297, 580, 1591, 2272, 2267, 2264, 1547, 538, 536, 529, 2278, 525, 2275, 547, 544, 541, 1574, 1571, 2237, 2235, 2229, 1493, 2225, 1489, 478, 2247, 470, 2244, 465, 2241, 493, 488, 484, 2250, 498, 495, 1536, 1533, 1530, 1539, 2187, 2186, 2184, 2182, 1432, 2179, 1430, 2176, 1427, 414, 412, 2197, 409, 2195, 405, 2193, 2190, 426, 424, 421, 2203, 418, 2201, 431, 429, 1473, 1471, 1469, 1466, 434, 1477, 1475, 2478, 2472, 2470, 2459, 2457, 2454, 2462, 803, 2437, 2432, 2429, 1726, 2443, 2440, 792, 789, 785, 2401, 2399, 2393, 1702, 2389, 1699, 2411, 2408, 2405, 745, 741, 2415, 758, 755, 1721, 2358, 2357, 2355, 2353, 1661, 2350, 1660, 2347, 1657, 2368, 2366, 2364, 2361, 1666, 690, 687, 2374, 683, 2372, 701, 698, 705, 1691, 1689, 2619, 2617, 2610, 2608, 2605, 2613, 2593, 2588, 2585, 1803, 2599, 2596, 2563, 2561, 2555, 1797, 2551, 1795, 2573, 2570, 2567, 2577, 2525, 2524, 2522, 2520, 1786, 2517, 1785, 2514, 1783, 2535, 2533, 2531, 2528, 1788, 2541, 2539, 906, 903, 911, 2721, 1844, 2715, 2712, 1838, 1836, 2699, 2696, 2693, 2703, 1827, 1826, 1824, 2673, 2671, 2669, 2666, 1829, 2679, 2677, 1858, 1857, 2772, 1854, 1853, 1851, 1856, 2766, 2764, 143, 1987, 139, 1986, 135, 133, 131, 1984, 128, 1983, 125, 1981, 138, 137, 136, 1985, 1133, 1132, 1130, 112, 110, 1974, 107, 1973, 104, 1971, 1969, 122, 121, 119, 117, 1977, 114, 1976, 124, 1115, 1114, 1112, 1110, 1117, 1116, 84, 83, 1953, 81, 1952, 78, 1950, 1948, 1945, 94, 93, 91, 1959, 88, 1958, 85, 1955, 99, 97, 95, 1961, 1086, 1085, 1083, 1081, 1078, 100, 1090, 1089, 1087, 1091, 49, 47, 1917, 44, 1915, 1913, 1910, 1907, 59, 1926, 56, 1925, 53, 1922, 1919, 66, 64, 1931, 61, 1929, 1042, 1040, 1038, 71, 1035, 70, 1032, 68, 1048, 1047, 1045, 1043, 1050, 1049, 12, 10, 1869, 1867, 1864, 1861, 21, 1880, 19, 1877, 1874, 1871, 28, 1888, 25, 1886, 22, 1883, 982, 980, 977, 974, 32, 30, 991, 989, 987, 984, 34, 995, 994, 992, 2151, 2150, 2147, 2146, 2144, 356, 355, 354, 2149, 2139, 2138, 2136, 2134, 1359, 343, 341, 338, 2143, 335, 2141, 348, 347, 346, 1376, 1374, 2124, 2123, 2121, 2119, 1326, 2116, 1324, 310, 308, 305, 2131, 302, 2129, 298, 2127, 320, 318, 316, 313, 2133, 322, 321, 1355, 1353, 1351, 1357, 2092, 2091, 2089, 2087, 1276, 2084, 1274, 2081, 1271, 259, 2102, 256, 2100, 252, 2098, 2095, 272, 269, 2108, 266, 2106, 281, 279, 277, 1317, 1315, 1313, 1310, 282, 1321, 1319, 2039, 2037, 2035, 2032, 1203, 2029, 1200, 1197, 207, 2053, 205, 2051, 201, 2049, 2046, 2043, 220, 218, 2064, 215, 2062, 211, 2059, 228, 226, 223, 2069, 1259, 1257, 1254, 232, 1251, 230, 1267, 1265, 1263, 2316, 2315, 2312, 2311, 2309, 2314, 2304, 2303, 2301, 2299, 1593, 2308, 2306, 590, 2288, 2287, 2285, 2283, 1578, 2280, 1577, 2295, 2293, 2291, 579, 577, 574, 571, 2298, 582, 581, 1592, 2263, 2262, 2260, 2258, 1545, 2255, 1544, 2252, 1541, 2273, 2271, 2269, 2266, 1550, 535, 532, 2279, 528, 2277, 546, 543, 549, 1575, 1573, 2224, 2222, 2220, 1486, 2217, 1485, 2214, 1482, 1479, 2238, 2236, 2234, 2231, 1496, 2228, 1492, 480, 477, 2248, 473, 2246, 469, 2243, 490, 487, 2251, 497, 1537, 1535, 1532, 2477, 2476, 2474, 2479, 2469, 2468, 2466, 2464, 1730, 2473, 2471, 2453, 2452, 2450, 2448, 1729, 2445, 1728, 2460, 2458, 2456, 2463, 805, 804, 2428, 2427, 2425, 2423, 1725, 2420, 1724, 2417, 1722, 2438, 2436, 2434, 2431, 1727, 2444, 2442, 793, 791, 788, 795, 2388, 2386, 2384, 1697, 2381, 1696, 2378, 1694, 1692, 2402, 2400, 2398, 2395, 1703, 2392, 1701, 2412, 2410, 2407, 751, 748, 744, 2416, 759, 757, 1807, 2620, 2618, 1806, 1805, 2611, 2609, 2607, 2614, 1802, 1801, 1799, 2594, 2592, 2590, 2587, 1804, 2600, 2598, 1794, 1793, 1791, 1789, 2564, 2562, 2560, 2557, 1798, 2554, 1796, 2574, 2572, 2569, 2578, 1847, 1846, 2722, 1843, 1842, 1840, 1845, 2716, 2714, 1835, 1834, 1832, 1830, 1839, 1837, 2700, 2698, 2695, 2704, 1817, 1811, 1810, 897, 862, 1777, 829, 826, 838, 1760, 1758, 808, 2481, 1741, 1740, 1738, 1743, 2624, 1818, 2726, 2776, 782, 740, 737, 1715, 686, 679, 695, 1682, 1680, 639, 628, 2339, 647, 644, 1645, 1643, 1640, 1648, 602, 600, 597, 595, 2320, 593, 2318, 609, 607, 604, 1611, 1610, 1608, 1606, 613, 1615, 1613, 2328, 926, 924, 892, 886, 899, 857, 850, 2505, 1778, 824, 823, 821, 819, 2488, 818, 2486, 833, 831, 828, 840, 1761, 1759, 2649, 2632, 2630, 2746, 2734, 2732, 2782, 2781, 570, 567, 1587, 531, 527, 523, 540, 1566, 1564, 476, 467, 463, 2240, 486, 483, 1524, 1521, 1518, 1529, 411, 403, 2192, 399, 2189, 423, 416, 1462, 1457, 1454, 428, 1468, 1465, 2210, 366, 363, 2158, 360, 2156, 357, 2153, 376, 373, 370, 2163, 1410, 1409, 1407, 1405, 382, 1402, 380, 1417, 1415, 1412, 1421, 2175, 2174, 777, 774, 771, 784, 732, 725, 722, 2404, 743, 1716, 676, 674, 668, 2363, 665, 2360, 685, 1684, 1681, 626, 624, 622, 2335, 620, 2333, 617, 2330, 641, 635, 649, 1646, 1644, 1642, 2566, 928, 925, 2530, 2527, 894, 891, 888, 2501, 2499, 2496, 858, 856, 854, 851, 1779, 2692, 2668, 2665, 2645, 2643, 2640, 2651, 2768, 2759, 2757, 2744, 2743, 2741, 2748, 352, 1382, 340, 337, 333, 1371, 1369, 307, 300, 296, 2126, 315, 312, 1347, 1342, 1350, 261, 258, 250, 2097, 246, 2094, 271, 268, 264, 1306, 1301, 1298, 276, 1312, 1309, 2115, 203, 2048, 195, 2045, 191, 2041, 213, 209, 2056, 1246, 1244, 1238, 225, 1234, 222, 1256, 1253, 1249, 1262, 2080, 2079, 154, 1997, 150, 1995, 147, 1992, 1989, 163, 160, 2004, 156, 2001, 1175, 1174, 1172, 1170, 1167, 170, 1164, 167, 1185, 1183, 1180, 1177, 174, 1190, 1188, 2025, 2024, 2022, 587, 586, 564, 559, 556, 2290, 573, 1588, 520, 518, 512, 2268, 508, 2265, 530, 1568, 1565, 461, 457, 2233, 450, 2230, 446, 2226, 479, 471, 489, 1526, 1523, 1520, 397, 395, 2185, 392, 2183, 389, 2180, 2177, 410, 2194, 402, 422, 1463, 1461, 1459, 1456, 1470, 2455, 799, 2433, 2430, 779, 776, 773, 2397, 2394, 2390, 734, 728, 724, 746, 1717, 2356, 2354, 2351, 2348, 1658, 677, 675, 673, 670, 667, 688, 1685, 1683, 2606, 2589, 2586, 2559, 2556, 2552, 927, 2523, 2521, 2518, 2515, 1784, 2532, 895, 893, 890, 2718, 2709, 2707, 2689, 2687, 2684, 2663, 2662, 2660, 2658, 1825, 2667, 2769, 1852, 2760, 2758, 142, 141, 1139, 1138, 134, 132, 129, 126, 1982, 1129, 1128, 1126, 1131, 113, 111, 108, 105, 1972, 101, 1970, 120, 118, 115, 1109, 1108, 1106, 1104, 123, 1113, 1111, 82, 79, 1951, 75, 1949, 72, 1946, 92, 89, 86, 1956, 1077, 1076, 1074, 1072, 98, 1069, 96, 1084, 1082, 1079, 1088, 1968, 1967, 48, 45, 1916, 42, 1914, 39, 1911, 1908, 60, 57, 54, 1923, 50, 1920, 1031, 1030, 1028, 1026, 67, 1023, 65, 1020, 62, 1041, 1039, 1036, 1033, 69, 1046, 1044, 1944, 1943, 1941, 11, 9, 1868, 7, 1865, 1862, 1859, 20, 1878, 16, 1875, 13, 1872, 970, 968, 966, 963, 29, 960, 26, 23, 983, 981, 978, 975, 33, 971, 31, 990, 988, 985, 1906, 1904, 1902, 993, 351, 2145, 1383, 331, 330, 328, 326, 2137, 323, 2135, 339, 1372, 1370, 294, 293, 291, 289, 2122, 286, 2120, 283, 2117, 309, 303, 317, 1348, 1346, 1344, 245, 244, 242, 2090, 239, 2088, 236, 2085, 2082, 260, 2099, 249, 270, 1307, 1305, 1303, 1300, 1314, 189, 2038, 186, 2036, 183, 2033, 2030, 2026, 206, 198, 2047, 194, 216, 1247, 1245, 1243, 1240, 227, 1237, 1255, 2310, 2302, 2300, 2286, 2284, 2281, 565, 563, 561, 558, 575, 1589, 2261, 2259, 2256, 2253, 1542, 521, 519, 517, 514, 2270, 511, 533, 1569, 1567, 2223, 2221, 2218, 2215, 1483, 2211, 1480, 459, 456, 453, 2232, 449, 474, 491, 1527, 1525, 1522, 2475, 2467, 2465, 2451, 2449, 2446, 801, 800, 2426, 2424, 2421, 2418, 1723, 2435, 780, 778, 775, 2387, 2385, 2382, 2379, 1695, 2375, 1693, 2396, 735, 733, 730, 727, 749, 1718, 2616, 2615, 2604, 2603, 2601, 2584, 2583, 2581, 2579, 1800, 2591, 2550, 2549, 2547, 2545, 1792, 2542, 1790, 2558, 929, 2719, 1841, 2710, 2708, 1833, 1831, 2690, 2688, 2686, 1815, 1809, 1808, 1774, 1756, 1754, 1737, 1736, 1734, 1739, 1816, 1711, 1676, 1674, 633, 629, 1638, 1636, 1633, 1641, 598, 1605, 1604, 1602, 1600, 605, 1609, 1607, 2327, 887, 853, 1775, 822, 820, 1757, 1755, 1584, 524, 1560, 1558, 468, 464, 1514, 1511, 1508, 1519, 408, 404, 400, 1452, 1447, 1444, 417, 1458, 1455, 2208, 364, 361, 358, 2154, 1401, 1400, 1398, 1396, 374, 1393, 371, 1408, 1406, 1403, 1413, 2173, 2172, 772, 726, 723, 1712, 672, 669, 666, 682, 1678, 1675, 625, 623, 621, 618, 2331, 636, 632, 1639, 1637, 1635, 920, 918, 884, 880, 889, 849, 848, 847, 846, 2497, 855, 852, 1776, 2641, 2742, 2787, 1380, 334, 1367, 1365, 301, 297, 1340, 1338, 1335, 1343, 255, 251, 247, 1296, 1291, 1288, 265, 1302, 1299, 2113, 204, 196, 192, 2042, 1232, 1230, 1224, 214, 1220, 210, 1242, 1239, 1235, 1250, 2077, 2075, 151, 148, 1993, 144, 1990, 1163, 1162, 1160, 1158, 1155, 161, 1152, 157, 1173, 1171, 1168, 1165, 168, 1181, 1178, 2021, 2020, 2018, 2023, 585, 560, 557, 1585, 516, 509, 1562, 1559, 458, 447, 2227, 472, 1516, 1513, 1510, 398, 396, 393, 390, 2181, 386, 2178, 407, 1453, 1451, 1449, 1446, 420, 1460, 2209, 769, 764, 720, 712, 2391, 729, 1713, 664, 663, 661, 659, 2352, 656, 2349, 671, 1679, 1677, 2553, 922, 919, 2519, 2516, 885, 883, 881, 2685, 2661, 2659, 2767, 2756, 2755, 140, 1137, 1136, 130, 127, 1125, 1124, 1122, 1127, 109, 106, 102, 1103, 1102, 1100, 1098, 116, 1107, 1105, 1980, 80, 76, 73, 1947, 1068, 1067, 1065, 1063, 90, 1060, 87, 1075, 1073, 1070, 1080, 1966, 1965, 46, 43, 40, 1912, 36, 1909, 1019, 1018, 1016, 1014, 58, 1011, 55, 1008, 51, 1029, 1027, 1024, 1021, 63, 1037, 1034, 1940, 1939, 1937, 1942, 8, 1866, 4, 1863, 1, 1860, 956, 954, 952, 949, 946, 17, 14, 969, 967, 964, 961, 27, 957, 24, 979, 976, 972, 1901, 1900, 1898, 1896, 986, 1905, 1903, 350, 349, 1381, 329, 327, 324, 1368, 1366, 292, 290, 287, 284, 2118, 304, 1341, 1339, 1337, 1345, 243, 240, 237, 2086, 233, 2083, 254, 1297, 1295, 1293, 1290, 1304, 2114, 190, 187, 184, 2034, 180, 2031, 177, 2027, 199, 1233, 1231, 1229, 1226, 217, 1223, 1241, 2078, 2076, 584, 555, 554, 552, 550, 2282, 562, 1586, 507, 506, 504, 502, 2257, 499, 2254, 515, 1563, 1561, 445, 443, 441, 2219, 438, 2216, 435, 2212, 460, 454, 475, 1517, 1515, 1512, 2447, 798, 797, 2422, 2419, 770, 768, 766, 2383, 2380, 2376, 721, 719, 717, 714, 731, 1714, 2602, 2582, 2580, 2548, 2546, 2543, 923, 921, 2717, 2706, 2705, 2683, 2682, 2680, 1771, 1752, 1750, 1733, 1732, 1731, 1735, 1814, 1707, 1670, 1668, 1631, 1629, 1626, 1634, 1599, 1598, 1596, 1594, 1603, 1601, 2326, 1772, 1753, 1751, 1581, 1554, 1552, 1504, 1501, 1498, 1509, 1442, 1437, 1434, 401, 1448, 1445, 2206, 1392, 1391, 1389, 1387, 1384, 359, 1399, 1397, 1394, 1404, 2171, 2170, 1708, 1672, 1669, 619, 1632, 1630, 1628, 1773, 1378, 1363, 1361, 1333, 1328, 1336, 1286, 1281, 1278, 248, 1292, 1289, 2111, 1218, 1216, 1210, 197, 1206, 193, 1228, 1225, 1221, 1236, 2073, 2071, 1151, 1150, 1148, 1146, 152, 1143, 149, 1140, 145, 1161, 1159, 1156, 1153, 158, 1169, 1166, 2017, 2016, 2014, 2019, 1582, 510, 1556, 1553, 452, 448, 1506, 1500, 394, 391, 387, 1443, 1441, 1439, 1436, 1450, 2207, 765, 716, 713, 1709, 662, 660, 657, 1673, 1671, 916, 914, 879, 878, 877, 882, 1135, 1134, 1121, 1120, 1118, 1123, 1097, 1096, 1094, 1092, 103, 1101, 1099, 1979, 1059, 1058, 1056, 1054, 77, 1051, 74, 1066, 1064, 1061, 1071, 1964, 1963, 1007, 1006, 1004, 1002, 999, 41, 996, 37, 1017, 1015, 1012, 1009, 52, 1025, 1022, 1936, 1935, 1933, 1938, 942, 940, 938, 935, 932, 5, 2, 955, 953, 950, 947, 18, 943, 15, 965, 962, 958, 1895, 1894, 1892, 1890, 973, 1899, 1897, 1379, 325, 1364, 1362, 288, 285, 1334, 1332, 1330, 241, 238, 234, 1287, 1285, 1283, 1280, 1294, 2112, 188, 185, 181, 178, 2028, 1219, 1217, 1215, 1212, 200, 1209, 1227, 2074, 2072, 583, 553, 551, 1583, 505, 503, 500, 513, 1557, 1555, 444, 442, 439, 436, 2213, 455, 451, 1507, 1505, 1502, 796, 763, 762, 760, 767, 711, 710, 708, 706, 2377, 718, 715, 1710, 2544, 917, 915, 2681, 1627, 1597, 1595, 2325, 1769, 1749, 1747, 1499, 1438, 1435, 2204, 1390, 1388, 1385, 1395, 2169, 2167, 1704, 1665, 1662, 1625, 1623, 1620, 1770, 1329, 1282, 1279, 2109, 1214, 1207, 1222, 2068, 2065, 1149, 1147, 1144, 1141, 146, 1157, 1154, 2013, 2011, 2008, 2015, 1579, 1549, 1546, 1495, 1487, 1433, 1431, 1428, 1425, 388, 1440, 2205, 1705, 658, 1667, 1664, 1119, 1095, 1093, 1978, 1057, 1055, 1052, 1062, 1962, 1960, 1005, 1003, 1e3, 997, 38, 1013, 1010, 1932, 1930, 1927, 1934, 941, 939, 936, 933, 6, 930, 3, 951, 948, 944, 1889, 1887, 1884, 1881, 959, 1893, 1891, 35, 1377, 1360, 1358, 1327, 1325, 1322, 1331, 1277, 1275, 1272, 1269, 235, 1284, 2110, 1205, 1204, 1201, 1198, 182, 1195, 179, 1213, 2070, 2067, 1580, 501, 1551, 1548, 440, 437, 1497, 1494, 1490, 1503, 761, 709, 707, 1706, 913, 912, 2198, 1386, 2164, 2161, 1621, 1766, 2103, 1208, 2058, 2054, 1145, 1142, 2005, 2002, 1999, 2009, 1488, 1429, 1426, 2200, 1698, 1659, 1656, 1975, 1053, 1957, 1954, 1001, 998, 1924, 1921, 1918, 1928, 937, 934, 931, 1879, 1876, 1873, 1870, 945, 1885, 1882, 1323, 1273, 1270, 2105, 1202, 1199, 1196, 1211, 2061, 2057, 1576, 1543, 1540, 1484, 1481, 1478, 1491, 1700 ]); class PDF417DetectorResult { constructor(bits, points) { this.bits = bits; this.points = points; } getBits() { return this.bits; } getPoints() { return this.points; } } class Detector$3 { /** *Detects a PDF417 Code in an image. Only checks 0 and 180 degree rotations.
* * @param image barcode image to decode * @param hints optional hints to detector * @param multiple if true, then the image is searched for multiple codes. If false, then at most one code will * be found and returned * @return {@link PDF417DetectorResult} encapsulating results of detecting a PDF417 code * @throws NotFoundException if no PDF417 Code can be found */ static detectMultiple(image, hints, multiple) { let bitMatrix = image.getBlackMatrix(); let barcodeCoordinates = Detector$3.detect(multiple, bitMatrix); if (!barcodeCoordinates.length) { bitMatrix = bitMatrix.clone(); bitMatrix.rotate180(); barcodeCoordinates = Detector$3.detect(multiple, bitMatrix); } return new PDF417DetectorResult(bitMatrix, barcodeCoordinates); } /** * Detects PDF417 codes in an image. Only checks 0 degree rotation * @param multiple if true, then the image is searched for multiple codes. If false, then at most one code will * be found and returned * @param bitMatrix bit matrix to detect barcodes in * @return List of ResultPoint arrays containing the coordinates of found barcodes */ static detect(multiple, bitMatrix) { const barcodeCoordinates = new Array(); let row = 0; let column = 0; let foundBarcodeInRow = false; while (row < bitMatrix.getHeight()) { const vertices = Detector$3.findVertices(bitMatrix, row, column); if (vertices[0] == null && vertices[3] == null) { if (!foundBarcodeInRow) { break; } foundBarcodeInRow = false; column = 0; for (const barcodeCoordinate of barcodeCoordinates) { if (barcodeCoordinate[1] != null) { row = Math.trunc(Math.max(row, barcodeCoordinate[1].getY())); } if (barcodeCoordinate[3] != null) { row = Math.max(row, Math.trunc(barcodeCoordinate[3].getY())); } } row += Detector$3.ROW_STEP; continue; } foundBarcodeInRow = true; barcodeCoordinates.push(vertices); if (!multiple) { break; } if (vertices[2] != null) { column = Math.trunc(vertices[2].getX()); row = Math.trunc(vertices[2].getY()); } else { column = Math.trunc(vertices[4].getX()); row = Math.trunc(vertices[4].getY()); } } return barcodeCoordinates; } /** * Locate the vertices and the codewords area of a black blob using the Start * and Stop patterns as locators. * * @param matrix the scanned barcode image. * @return an array containing the vertices: * vertices[0] x, y top left barcode * vertices[1] x, y bottom left barcode * vertices[2] x, y top right barcode * vertices[3] x, y bottom right barcode * vertices[4] x, y top left codeword area * vertices[5] x, y bottom left codeword area * vertices[6] x, y top right codeword area * vertices[7] x, y bottom right codeword area */ static findVertices(matrix, startRow, startColumn) { const height = matrix.getHeight(); const width = matrix.getWidth(); const result = new Array(8); Detector$3.copyToResult(result, Detector$3.findRowsWithPattern(matrix, height, width, startRow, startColumn, Detector$3.START_PATTERN), Detector$3.INDEXES_START_PATTERN); if (result[4] != null) { startColumn = Math.trunc(result[4].getX()); startRow = Math.trunc(result[4].getY()); } Detector$3.copyToResult(result, Detector$3.findRowsWithPattern(matrix, height, width, startRow, startColumn, Detector$3.STOP_PATTERN), Detector$3.INDEXES_STOP_PATTERN); return result; } static copyToResult(result, tmpResult, destinationIndexes) { for (let i = 0; i < destinationIndexes.length; i++) { result[destinationIndexes[i]] = tmpResult[i]; } } static findRowsWithPattern(matrix, height, width, startRow, startColumn, pattern) { const result = new Array(4); let found = false; const counters = new Int32Array(pattern.length); for (; startRow < height; startRow += Detector$3.ROW_STEP) { let loc = Detector$3.findGuardPattern(matrix, startColumn, startRow, width, false, pattern, counters); if (loc != null) { while (startRow > 0) { const previousRowLoc = Detector$3.findGuardPattern(matrix, startColumn, --startRow, width, false, pattern, counters); if (previousRowLoc != null) { loc = previousRowLoc; } else { startRow++; break; } } result[0] = new ResultPoint(loc[0], startRow); result[1] = new ResultPoint(loc[1], startRow); found = true; break; } } let stopRow = startRow + 1; if (found) { let skippedRowCount = 0; let previousRowLoc = Int32Array.from([Math.trunc(result[0].getX()), Math.trunc(result[1].getX())]); for (; stopRow < height; stopRow++) { const loc = Detector$3.findGuardPattern(matrix, previousRowLoc[0], stopRow, width, false, pattern, counters); if (loc != null && Math.abs(previousRowLoc[0] - loc[0]) < Detector$3.MAX_PATTERN_DRIFT && Math.abs(previousRowLoc[1] - loc[1]) < Detector$3.MAX_PATTERN_DRIFT) { previousRowLoc = loc; skippedRowCount = 0; } else { if (skippedRowCount > Detector$3.SKIPPED_ROW_COUNT_MAX) { break; } else { skippedRowCount++; } } } stopRow -= skippedRowCount + 1; result[2] = new ResultPoint(previousRowLoc[0], stopRow); result[3] = new ResultPoint(previousRowLoc[1], stopRow); } if (stopRow - startRow < Detector$3.BARCODE_MIN_HEIGHT) { Arrays.fill(result, null); } return result; } /** * @param matrix row of black/white values to search * @param column x position to start search * @param row y position to start search * @param width the number of pixels to search on this row * @param pattern pattern of counts of number of black and white pixels that are * being searched for as a pattern * @param counters array of counters, as long as pattern, to re-use * @return start/end horizontal offset of guard pattern, as an array of two ints. */ static findGuardPattern(matrix, column, row, width, whiteFirst, pattern, counters) { Arrays.fillWithin(counters, 0, counters.length, 0); let patternStart = column; let pixelDrift = 0; while (matrix.get(patternStart, row) && patternStart > 0 && pixelDrift++ < Detector$3.MAX_PIXEL_DRIFT) { patternStart--; } let x = patternStart; let counterPosition = 0; let patternLength = pattern.length; for (let isWhite = whiteFirst; x < width; x++) { let pixel = matrix.get(x, row); if (pixel !== isWhite) { counters[counterPosition]++; } else { if (counterPosition === patternLength - 1) { if (Detector$3.patternMatchVariance(counters, pattern, Detector$3.MAX_INDIVIDUAL_VARIANCE) < Detector$3.MAX_AVG_VARIANCE) { return new Int32Array([patternStart, x]); } patternStart += counters[0] + counters[1]; System.arraycopy(counters, 2, counters, 0, counterPosition - 1); counters[counterPosition - 1] = 0; counters[counterPosition] = 0; counterPosition--; } else { counterPosition++; } counters[counterPosition] = 1; isWhite = !isWhite; } } if (counterPosition === patternLength - 1 && Detector$3.patternMatchVariance(counters, pattern, Detector$3.MAX_INDIVIDUAL_VARIANCE) < Detector$3.MAX_AVG_VARIANCE) { return new Int32Array([patternStart, x - 1]); } return null; } /** * Determines how closely a set of observed counts of runs of black/white * values matches a given target pattern. This is reported as the ratio of * the total variance from the expected pattern proportions across all * pattern elements, to the length of the pattern. * * @param counters observed counters * @param pattern expected pattern * @param maxIndividualVariance The most any counter can differ before we give up * @return ratio of total variance between counters and pattern compared to total pattern size */ static patternMatchVariance(counters, pattern, maxIndividualVariance) { let numCounters = counters.length; let total = 0; let patternLength = 0; for (let i = 0; i < numCounters; i++) { total += counters[i]; patternLength += pattern[i]; } if (total < patternLength) { return ( /*Float.POSITIVE_INFINITY*/ Infinity ); } let unitBarWidth = total / patternLength; maxIndividualVariance *= unitBarWidth; let totalVariance = 0; for (let x = 0; x < numCounters; x++) { let counter = counters[x]; let scaledPattern = pattern[x] * unitBarWidth; let variance = counter > scaledPattern ? counter - scaledPattern : scaledPattern - counter; if (variance > maxIndividualVariance) { return ( /*Float.POSITIVE_INFINITY*/ Infinity ); } totalVariance += variance; } return totalVariance / total; } } Detector$3.INDEXES_START_PATTERN = Int32Array.from([0, 4, 1, 5]); Detector$3.INDEXES_STOP_PATTERN = Int32Array.from([6, 2, 7, 3]); Detector$3.MAX_AVG_VARIANCE = 0.42; Detector$3.MAX_INDIVIDUAL_VARIANCE = 0.8; Detector$3.START_PATTERN = Int32Array.from([8, 1, 1, 1, 1, 1, 1, 3]); Detector$3.STOP_PATTERN = Int32Array.from([7, 1, 1, 3, 1, 1, 1, 2, 1]); Detector$3.MAX_PIXEL_DRIFT = 3; Detector$3.MAX_PATTERN_DRIFT = 5; Detector$3.SKIPPED_ROW_COUNT_MAX = 25; Detector$3.ROW_STEP = 5; Detector$3.BARCODE_MIN_HEIGHT = 10; class ModulusPoly { constructor(field, coefficients) { if (coefficients.length === 0) { throw new IllegalArgumentException(); } this.field = field; let coefficientsLength = ( /*int*/ coefficients.length ); if (coefficientsLength > 1 && coefficients[0] === 0) { let firstNonZero = ( /*int*/ 1 ); while (firstNonZero < coefficientsLength && coefficients[firstNonZero] === 0) { firstNonZero++; } if (firstNonZero === coefficientsLength) { this.coefficients = new Int32Array([0]); } else { this.coefficients = new Int32Array(coefficientsLength - firstNonZero); System.arraycopy(coefficients, firstNonZero, this.coefficients, 0, this.coefficients.length); } } else { this.coefficients = coefficients; } } getCoefficients() { return this.coefficients; } /** * @return degree of this polynomial */ getDegree() { return this.coefficients.length - 1; } /** * @return true iff this polynomial is the monomial "0" */ isZero() { return this.coefficients[0] === 0; } /** * @return coefficient of x^degree term in this polynomial */ getCoefficient(degree) { return this.coefficients[this.coefficients.length - 1 - degree]; } /** * @return evaluation of this polynomial at a given point */ evaluateAt(a) { if (a === 0) { return this.getCoefficient(0); } if (a === 1) { let sum = ( /*int*/ 0 ); for (let coefficient of this.coefficients) { sum = this.field.add(sum, coefficient); } return sum; } let result = ( /*int*/ this.coefficients[0] ); let size = ( /*int*/ this.coefficients.length ); for (let i = 1; i < size; i++) { result = this.field.add(this.field.multiply(a, result), this.coefficients[i]); } return result; } add(other) { if (!this.field.equals(other.field)) { throw new IllegalArgumentException("ModulusPolys do not have same ModulusGF field"); } if (this.isZero()) { return other; } if (other.isZero()) { return this; } let smallerCoefficients = this.coefficients; let largerCoefficients = other.coefficients; if (smallerCoefficients.length > largerCoefficients.length) { let temp = smallerCoefficients; smallerCoefficients = largerCoefficients; largerCoefficients = temp; } let sumDiff = new Int32Array(largerCoefficients.length); let lengthDiff = ( /*int*/ largerCoefficients.length - smallerCoefficients.length ); System.arraycopy(largerCoefficients, 0, sumDiff, 0, lengthDiff); for (let i = lengthDiff; i < largerCoefficients.length; i++) { sumDiff[i] = this.field.add(smallerCoefficients[i - lengthDiff], largerCoefficients[i]); } return new ModulusPoly(this.field, sumDiff); } subtract(other) { if (!this.field.equals(other.field)) { throw new IllegalArgumentException("ModulusPolys do not have same ModulusGF field"); } if (other.isZero()) { return this; } return this.add(other.negative()); } multiply(other) { if (other instanceof ModulusPoly) { return this.multiplyOther(other); } return this.multiplyScalar(other); } multiplyOther(other) { if (!this.field.equals(other.field)) { throw new IllegalArgumentException("ModulusPolys do not have same ModulusGF field"); } if (this.isZero() || other.isZero()) { return new ModulusPoly(this.field, new Int32Array([0])); } let aCoefficients = this.coefficients; let aLength = ( /*int*/ aCoefficients.length ); let bCoefficients = other.coefficients; let bLength = ( /*int*/ bCoefficients.length ); let product = new Int32Array(aLength + bLength - 1); for (let i = 0; i < aLength; i++) { let aCoeff = ( /*int*/ aCoefficients[i] ); for (let j = 0; j < bLength; j++) { product[i + j] = this.field.add(product[i + j], this.field.multiply(aCoeff, bCoefficients[j])); } } return new ModulusPoly(this.field, product); } negative() { let size = ( /*int*/ this.coefficients.length ); let negativeCoefficients = new Int32Array(size); for (let i = 0; i < size; i++) { negativeCoefficients[i] = this.field.subtract(0, this.coefficients[i]); } return new ModulusPoly(this.field, negativeCoefficients); } multiplyScalar(scalar) { if (scalar === 0) { return new ModulusPoly(this.field, new Int32Array([0])); } if (scalar === 1) { return this; } let size = ( /*int*/ this.coefficients.length ); let product = new Int32Array(size); for (let i = 0; i < size; i++) { product[i] = this.field.multiply(this.coefficients[i], scalar); } return new ModulusPoly(this.field, product); } multiplyByMonomial(degree, coefficient) { if (degree < 0) { throw new IllegalArgumentException(); } if (coefficient === 0) { return new ModulusPoly(this.field, new Int32Array([0])); } let size = ( /*int*/ this.coefficients.length ); let product = new Int32Array(size + degree); for (let i = 0; i < size; i++) { product[i] = this.field.multiply(this.coefficients[i], coefficient); } return new ModulusPoly(this.field, product); } /* ModulusPoly[] divide(other: ModulusPoly) { if (!field.equals(other.field)) { throw new IllegalArgumentException("ModulusPolys do not have same ModulusGF field"); } if (other.isZero()) { throw new IllegalArgumentException("Divide by 0"); } let quotient: ModulusPoly = field.getZero(); let remainder: ModulusPoly = this; let denominatorLeadingTerm: /*int/ number = other.getCoefficient(other.getDegree()); let inverseDenominatorLeadingTerm: /*int/ number = field.inverse(denominatorLeadingTerm); while (remainder.getDegree() >= other.getDegree() && !remainder.isZero()) { let degreeDifference: /*int/ number = remainder.getDegree() - other.getDegree(); let scale: /*int/ number = field.multiply(remainder.getCoefficient(remainder.getDegree()), inverseDenominatorLeadingTerm); let term: ModulusPoly = other.multiplyByMonomial(degreeDifference, scale); let iterationQuotient: ModulusPoly = field.buildMonomial(degreeDifference, scale); quotient = quotient.add(iterationQuotient); remainder = remainder.subtract(term); } return new ModulusPoly[] { quotient, remainder }; } */ // @Override toString() { let result = new StringBuilder( /*8 * this.getDegree()*/ ); for (let degree = this.getDegree(); degree >= 0; degree--) { let coefficient = ( /*int*/ this.getCoefficient(degree) ); if (coefficient !== 0) { if (coefficient < 0) { result.append(" - "); coefficient = -coefficient; } else { if (result.length() > 0) { result.append(" + "); } } if (degree === 0 || coefficient !== 1) { result.append(coefficient); } if (degree !== 0) { if (degree === 1) { result.append("x"); } else { result.append("x^"); result.append(degree); } } } } return result.toString(); } } class ModulusBase { add(a, b) { return (a + b) % this.modulus; } subtract(a, b) { return (this.modulus + a - b) % this.modulus; } exp(a) { return this.expTable[a]; } log(a) { if (a === 0) { throw new IllegalArgumentException(); } return this.logTable[a]; } inverse(a) { if (a === 0) { throw new ArithmeticException(); } return this.expTable[this.modulus - this.logTable[a] - 1]; } multiply(a, b) { if (a === 0 || b === 0) { return 0; } return this.expTable[(this.logTable[a] + this.logTable[b]) % (this.modulus - 1)]; } getSize() { return this.modulus; } equals(o) { return o === this; } } class ModulusGF extends ModulusBase { // private /*final*/ modulus: /*int*/ number; constructor(modulus, generator) { super(); this.modulus = modulus; this.expTable = new Int32Array(modulus); this.logTable = new Int32Array(modulus); let x = ( /*int*/ 1 ); for (let i = 0; i < modulus; i++) { this.expTable[i] = x; x = x * generator % modulus; } for (let i = 0; i < modulus - 1; i++) { this.logTable[this.expTable[i]] = i; } this.zero = new ModulusPoly(this, new Int32Array([0])); this.one = new ModulusPoly(this, new Int32Array([1])); } getZero() { return this.zero; } getOne() { return this.one; } buildMonomial(degree, coefficient) { if (degree < 0) { throw new IllegalArgumentException(); } if (coefficient === 0) { return this.zero; } let coefficients = new Int32Array(degree + 1); coefficients[0] = coefficient; return new ModulusPoly(this, coefficients); } } ModulusGF.PDF417_GF = new ModulusGF(PDF417Common.NUMBER_OF_CODEWORDS, 3); class ErrorCorrection { constructor() { this.field = ModulusGF.PDF417_GF; } /** * @param received received codewords * @param numECCodewords number of those codewords used for EC * @param erasures location of erasures * @return number of errors * @throws ChecksumException if errors cannot be corrected, maybe because of too many errors */ decode(received, numECCodewords, erasures) { let poly = new ModulusPoly(this.field, received); let S = new Int32Array(numECCodewords); let error = false; for (let i = numECCodewords; i > 0; i--) { let evaluation = poly.evaluateAt(this.field.exp(i)); S[numECCodewords - i] = evaluation; if (evaluation !== 0) { error = true; } } if (!error) { return 0; } let knownErrors = this.field.getOne(); if (erasures != null) { for (const erasure of erasures) { let b = this.field.exp(received.length - 1 - erasure); let term = new ModulusPoly(this.field, new Int32Array([this.field.subtract(0, b), 1])); knownErrors = knownErrors.multiply(term); } } let syndrome = new ModulusPoly(this.field, S); let sigmaOmega = this.runEuclideanAlgorithm(this.field.buildMonomial(numECCodewords, 1), syndrome, numECCodewords); let sigma = sigmaOmega[0]; let omega = sigmaOmega[1]; let errorLocations = this.findErrorLocations(sigma); let errorMagnitudes = this.findErrorMagnitudes(omega, sigma, errorLocations); for (let i = 0; i < errorLocations.length; i++) { let position = received.length - 1 - this.field.log(errorLocations[i]); if (position < 0) { throw ChecksumException.getChecksumInstance(); } received[position] = this.field.subtract(received[position], errorMagnitudes[i]); } return errorLocations.length; } /** * * @param ModulusPoly * @param a * @param ModulusPoly * @param b * @param int * @param R * @throws ChecksumException */ runEuclideanAlgorithm(a, b, R) { if (a.getDegree() < b.getDegree()) { let temp = a; a = b; b = temp; } let rLast = a; let r = b; let tLast = this.field.getZero(); let t = this.field.getOne(); while (r.getDegree() >= Math.round(R / 2)) { let rLastLast = rLast; let tLastLast = tLast; rLast = r; tLast = t; if (rLast.isZero()) { throw ChecksumException.getChecksumInstance(); } r = rLastLast; let q = this.field.getZero(); let denominatorLeadingTerm = rLast.getCoefficient(rLast.getDegree()); let dltInverse = this.field.inverse(denominatorLeadingTerm); while (r.getDegree() >= rLast.getDegree() && !r.isZero()) { let degreeDiff = r.getDegree() - rLast.getDegree(); let scale = this.field.multiply(r.getCoefficient(r.getDegree()), dltInverse); q = q.add(this.field.buildMonomial(degreeDiff, scale)); r = r.subtract(rLast.multiplyByMonomial(degreeDiff, scale)); } t = q.multiply(tLast).subtract(tLastLast).negative(); } let sigmaTildeAtZero = t.getCoefficient(0); if (sigmaTildeAtZero === 0) { throw ChecksumException.getChecksumInstance(); } let inverse = this.field.inverse(sigmaTildeAtZero); let sigma = t.multiply(inverse); let omega = r.multiply(inverse); return [sigma, omega]; } /** * * @param errorLocator * @throws ChecksumException */ findErrorLocations(errorLocator) { let numErrors = errorLocator.getDegree(); let result = new Int32Array(numErrors); let e = 0; for (let i = 1; i < this.field.getSize() && e < numErrors; i++) { if (errorLocator.evaluateAt(i) === 0) { result[e] = this.field.inverse(i); e++; } } if (e !== numErrors) { throw ChecksumException.getChecksumInstance(); } return result; } findErrorMagnitudes(errorEvaluator, errorLocator, errorLocations) { let errorLocatorDegree = errorLocator.getDegree(); let formalDerivativeCoefficients = new Int32Array(errorLocatorDegree); for (let i = 1; i <= errorLocatorDegree; i++) { formalDerivativeCoefficients[errorLocatorDegree - i] = this.field.multiply(i, errorLocator.getCoefficient(i)); } let formalDerivative = new ModulusPoly(this.field, formalDerivativeCoefficients); let s = errorLocations.length; let result = new Int32Array(s); for (let i = 0; i < s; i++) { let xiInverse = this.field.inverse(errorLocations[i]); let numerator = this.field.subtract(0, errorEvaluator.evaluateAt(xiInverse)); let denominator = this.field.inverse(formalDerivative.evaluateAt(xiInverse)); result[i] = this.field.multiply(numerator, denominator); } return result; } } class BoundingBox { constructor(image, topLeft, bottomLeft, topRight, bottomRight) { if (image instanceof BoundingBox) { this.constructor_2(image); } else { this.constructor_1(image, topLeft, bottomLeft, topRight, bottomRight); } } /** * * @param image * @param topLeft * @param bottomLeft * @param topRight * @param bottomRight * * @throws NotFoundException */ constructor_1(image, topLeft, bottomLeft, topRight, bottomRight) { const leftUnspecified = topLeft == null || bottomLeft == null; const rightUnspecified = topRight == null || bottomRight == null; if (leftUnspecified && rightUnspecified) { throw new NotFoundException(); } if (leftUnspecified) { topLeft = new ResultPoint(0, topRight.getY()); bottomLeft = new ResultPoint(0, bottomRight.getY()); } else if (rightUnspecified) { topRight = new ResultPoint(image.getWidth() - 1, topLeft.getY()); bottomRight = new ResultPoint(image.getWidth() - 1, bottomLeft.getY()); } this.image = image; this.topLeft = topLeft; this.bottomLeft = bottomLeft; this.topRight = topRight; this.bottomRight = bottomRight; this.minX = Math.trunc(Math.min(topLeft.getX(), bottomLeft.getX())); this.maxX = Math.trunc(Math.max(topRight.getX(), bottomRight.getX())); this.minY = Math.trunc(Math.min(topLeft.getY(), topRight.getY())); this.maxY = Math.trunc(Math.max(bottomLeft.getY(), bottomRight.getY())); } constructor_2(boundingBox) { this.image = boundingBox.image; this.topLeft = boundingBox.getTopLeft(); this.bottomLeft = boundingBox.getBottomLeft(); this.topRight = boundingBox.getTopRight(); this.bottomRight = boundingBox.getBottomRight(); this.minX = boundingBox.getMinX(); this.maxX = boundingBox.getMaxX(); this.minY = boundingBox.getMinY(); this.maxY = boundingBox.getMaxY(); } /** * @throws NotFoundException */ static merge(leftBox, rightBox) { if (leftBox == null) { return rightBox; } if (rightBox == null) { return leftBox; } return new BoundingBox(leftBox.image, leftBox.topLeft, leftBox.bottomLeft, rightBox.topRight, rightBox.bottomRight); } /** * @throws NotFoundException */ addMissingRows(missingStartRows, missingEndRows, isLeft) { let newTopLeft = this.topLeft; let newBottomLeft = this.bottomLeft; let newTopRight = this.topRight; let newBottomRight = this.bottomRight; if (missingStartRows > 0) { let top = isLeft ? this.topLeft : this.topRight; let newMinY = Math.trunc(top.getY() - missingStartRows); if (newMinY < 0) { newMinY = 0; } let newTop = new ResultPoint(top.getX(), newMinY); if (isLeft) { newTopLeft = newTop; } else { newTopRight = newTop; } } if (missingEndRows > 0) { let bottom = isLeft ? this.bottomLeft : this.bottomRight; let newMaxY = Math.trunc(bottom.getY() + missingEndRows); if (newMaxY >= this.image.getHeight()) { newMaxY = this.image.getHeight() - 1; } let newBottom = new ResultPoint(bottom.getX(), newMaxY); if (isLeft) { newBottomLeft = newBottom; } else { newBottomRight = newBottom; } } return new BoundingBox(this.image, newTopLeft, newBottomLeft, newTopRight, newBottomRight); } getMinX() { return this.minX; } getMaxX() { return this.maxX; } getMinY() { return this.minY; } getMaxY() { return this.maxY; } getTopLeft() { return this.topLeft; } getTopRight() { return this.topRight; } getBottomLeft() { return this.bottomLeft; } getBottomRight() { return this.bottomRight; } } class BarcodeMetadata { constructor(columnCount, rowCountUpperPart, rowCountLowerPart, errorCorrectionLevel) { this.columnCount = columnCount; this.errorCorrectionLevel = errorCorrectionLevel; this.rowCountUpperPart = rowCountUpperPart; this.rowCountLowerPart = rowCountLowerPart; this.rowCount = rowCountUpperPart + rowCountLowerPart; } getColumnCount() { return this.columnCount; } getErrorCorrectionLevel() { return this.errorCorrectionLevel; } getRowCount() { return this.rowCount; } getRowCountUpperPart() { return this.rowCountUpperPart; } getRowCountLowerPart() { return this.rowCountLowerPart; } } class Formatter { constructor() { this.buffer = ""; } /** * * @see https://stackoverflow.com/a/13439711/4367683 * * @param str * @param arr */ static form(str, arr) { let i = -1; function callback(exp, p0, p1, p2, p3, p4) { if (exp === "%%") return "%"; if (arr[++i] === void 0) return void 0; exp = p2 ? parseInt(p2.substr(1)) : void 0; let base = p3 ? parseInt(p3.substr(1)) : void 0; let val; switch (p4) { case "s": val = arr[i]; break; case "c": val = arr[i][0]; break; case "f": val = parseFloat(arr[i]).toFixed(exp); break; case "p": val = parseFloat(arr[i]).toPrecision(exp); break; case "e": val = parseFloat(arr[i]).toExponential(exp); break; case "x": val = parseInt(arr[i]).toString(base ? base : 16); break; case "d": val = parseFloat(parseInt(arr[i], base ? base : 10).toPrecision(exp)).toFixed(0); break; } val = typeof val === "object" ? JSON.stringify(val) : (+val).toString(base); let size = parseInt(p1); let ch = p1 && p1[0] + "" === "0" ? "0" : " "; while (val.length < size) val = p0 !== void 0 ? val + ch : ch + val; return val; } let regex = /%(-)?(0?[0-9]+)?([.][0-9]+)?([#][0-9]+)?([scfpexd%])/g; return str.replace(regex, callback); } /** * * @param append The new string to append. * @param args Argumets values to be formated. */ format(append, ...args) { this.buffer += Formatter.form(append, args); } /** * Returns the Formatter string value. */ toString() { return this.buffer; } } class DetectionResultColumn { constructor(boundingBox) { this.boundingBox = new BoundingBox(boundingBox); this.codewords = new Array(boundingBox.getMaxY() - boundingBox.getMinY() + 1); } /*final*/ getCodewordNearby(imageRow) { let codeword = this.getCodeword(imageRow); if (codeword != null) { return codeword; } for (let i = 1; i < DetectionResultColumn.MAX_NEARBY_DISTANCE; i++) { let nearImageRow = this.imageRowToCodewordIndex(imageRow) - i; if (nearImageRow >= 0) { codeword = this.codewords[nearImageRow]; if (codeword != null) { return codeword; } } nearImageRow = this.imageRowToCodewordIndex(imageRow) + i; if (nearImageRow < this.codewords.length) { codeword = this.codewords[nearImageRow]; if (codeword != null) { return codeword; } } } return null; } /*final int*/ imageRowToCodewordIndex(imageRow) { return imageRow - this.boundingBox.getMinY(); } /*final void*/ setCodeword(imageRow, codeword) { this.codewords[this.imageRowToCodewordIndex(imageRow)] = codeword; } /*final*/ getCodeword(imageRow) { return this.codewords[this.imageRowToCodewordIndex(imageRow)]; } /*final*/ getBoundingBox() { return this.boundingBox; } /*final*/ getCodewords() { return this.codewords; } // @Override toString() { const formatter = new Formatter(); let row = 0; for (const codeword of this.codewords) { if (codeword == null) { formatter.format("%3d: | %n", row++); continue; } formatter.format("%3d: %3d|%3d%n", row++, codeword.getRowNumber(), codeword.getValue()); } return formatter.toString(); } } DetectionResultColumn.MAX_NEARBY_DISTANCE = 5; class BarcodeValue { constructor() { this.values = /* @__PURE__ */ new Map(); } /** * Add an occurrence of a value */ setValue(value) { value = Math.trunc(value); let confidence = this.values.get(value); if (confidence == null) { confidence = 0; } confidence++; this.values.set(value, confidence); } /** * Determines the maximum occurrence of a set value and returns all values which were set with this occurrence. * @return an array of int, containing the values with the highest occurrence, or null, if no value was set */ getValue() { let maxConfidence = -1; let result = new Array(); for (const [key, value] of this.values.entries()) { const entry = { getKey: () => key, getValue: () => value }; if (entry.getValue() > maxConfidence) { maxConfidence = entry.getValue(); result = []; result.push(entry.getKey()); } else if (entry.getValue() === maxConfidence) { result.push(entry.getKey()); } } return PDF417Common.toIntArray(result); } getConfidence(value) { return this.values.get(value); } } class DetectionResultRowIndicatorColumn extends DetectionResultColumn { constructor(boundingBox, isLeft) { super(boundingBox); this._isLeft = isLeft; } setRowNumbers() { for (let codeword of this.getCodewords()) { if (codeword != null) { codeword.setRowNumberAsRowIndicatorColumn(); } } } // TODO implement properly // TODO maybe we should add missing codewords to store the correct row number to make // finding row numbers for other columns easier // use row height count to make detection of invalid row numbers more reliable adjustCompleteIndicatorColumnRowNumbers(barcodeMetadata) { let codewords = this.getCodewords(); this.setRowNumbers(); this.removeIncorrectCodewords(codewords, barcodeMetadata); let boundingBox = this.getBoundingBox(); let top = this._isLeft ? boundingBox.getTopLeft() : boundingBox.getTopRight(); let bottom = this._isLeft ? boundingBox.getBottomLeft() : boundingBox.getBottomRight(); let firstRow = this.imageRowToCodewordIndex(Math.trunc(top.getY())); let lastRow = this.imageRowToCodewordIndex(Math.trunc(bottom.getY())); let barcodeRow = -1; let maxRowHeight = 1; let currentRowHeight = 0; for (let codewordsRow = firstRow; codewordsRow < lastRow; codewordsRow++) { if (codewords[codewordsRow] == null) { continue; } let codeword = codewords[codewordsRow]; let rowDifference = codeword.getRowNumber() - barcodeRow; if (rowDifference === 0) { currentRowHeight++; } else if (rowDifference === 1) { maxRowHeight = Math.max(maxRowHeight, currentRowHeight); currentRowHeight = 1; barcodeRow = codeword.getRowNumber(); } else if (rowDifference < 0 || codeword.getRowNumber() >= barcodeMetadata.getRowCount() || rowDifference > codewordsRow) { codewords[codewordsRow] = null; } else { let checkedRows; if (maxRowHeight > 2) { checkedRows = (maxRowHeight - 2) * rowDifference; } else { checkedRows = rowDifference; } let closePreviousCodewordFound = checkedRows >= codewordsRow; for (let i = 1; i <= checkedRows && !closePreviousCodewordFound; i++) { closePreviousCodewordFound = codewords[codewordsRow - i] != null; } if (closePreviousCodewordFound) { codewords[codewordsRow] = null; } else { barcodeRow = codeword.getRowNumber(); currentRowHeight = 1; } } } } getRowHeights() { let barcodeMetadata = this.getBarcodeMetadata(); if (barcodeMetadata == null) { return null; } this.adjustIncompleteIndicatorColumnRowNumbers(barcodeMetadata); let result = new Int32Array(barcodeMetadata.getRowCount()); for (let codeword of this.getCodewords()) { if (codeword != null) { let rowNumber = codeword.getRowNumber(); if (rowNumber >= result.length) { continue; } result[rowNumber]++; } } return result; } // TODO maybe we should add missing codewords to store the correct row number to make // finding row numbers for other columns easier // use row height count to make detection of invalid row numbers more reliable adjustIncompleteIndicatorColumnRowNumbers(barcodeMetadata) { let boundingBox = this.getBoundingBox(); let top = this._isLeft ? boundingBox.getTopLeft() : boundingBox.getTopRight(); let bottom = this._isLeft ? boundingBox.getBottomLeft() : boundingBox.getBottomRight(); let firstRow = this.imageRowToCodewordIndex(Math.trunc(top.getY())); let lastRow = this.imageRowToCodewordIndex(Math.trunc(bottom.getY())); let codewords = this.getCodewords(); let barcodeRow = -1; for (let codewordsRow = firstRow; codewordsRow < lastRow; codewordsRow++) { if (codewords[codewordsRow] == null) { continue; } let codeword = codewords[codewordsRow]; codeword.setRowNumberAsRowIndicatorColumn(); let rowDifference = codeword.getRowNumber() - barcodeRow; if (rowDifference === 0) ; else if (rowDifference === 1) { barcodeRow = codeword.getRowNumber(); } else if (codeword.getRowNumber() >= barcodeMetadata.getRowCount()) { codewords[codewordsRow] = null; } else { barcodeRow = codeword.getRowNumber(); } } } getBarcodeMetadata() { let codewords = this.getCodewords(); let barcodeColumnCount = new BarcodeValue(); let barcodeRowCountUpperPart = new BarcodeValue(); let barcodeRowCountLowerPart = new BarcodeValue(); let barcodeECLevel = new BarcodeValue(); for (let codeword of codewords) { if (codeword == null) { continue; } codeword.setRowNumberAsRowIndicatorColumn(); let rowIndicatorValue = codeword.getValue() % 30; let codewordRowNumber = codeword.getRowNumber(); if (!this._isLeft) { codewordRowNumber += 2; } switch (codewordRowNumber % 3) { case 0: barcodeRowCountUpperPart.setValue(rowIndicatorValue * 3 + 1); break; case 1: barcodeECLevel.setValue(rowIndicatorValue / 3); barcodeRowCountLowerPart.setValue(rowIndicatorValue % 3); break; case 2: barcodeColumnCount.setValue(rowIndicatorValue + 1); break; } } if (barcodeColumnCount.getValue().length === 0 || barcodeRowCountUpperPart.getValue().length === 0 || barcodeRowCountLowerPart.getValue().length === 0 || barcodeECLevel.getValue().length === 0 || barcodeColumnCount.getValue()[0] < 1 || barcodeRowCountUpperPart.getValue()[0] + barcodeRowCountLowerPart.getValue()[0] < PDF417Common.MIN_ROWS_IN_BARCODE || barcodeRowCountUpperPart.getValue()[0] + barcodeRowCountLowerPart.getValue()[0] > PDF417Common.MAX_ROWS_IN_BARCODE) { return null; } let barcodeMetadata = new BarcodeMetadata(barcodeColumnCount.getValue()[0], barcodeRowCountUpperPart.getValue()[0], barcodeRowCountLowerPart.getValue()[0], barcodeECLevel.getValue()[0]); this.removeIncorrectCodewords(codewords, barcodeMetadata); return barcodeMetadata; } removeIncorrectCodewords(codewords, barcodeMetadata) { for (let codewordRow = 0; codewordRow < codewords.length; codewordRow++) { let codeword = codewords[codewordRow]; if (codewords[codewordRow] == null) { continue; } let rowIndicatorValue = codeword.getValue() % 30; let codewordRowNumber = codeword.getRowNumber(); if (codewordRowNumber > barcodeMetadata.getRowCount()) { codewords[codewordRow] = null; continue; } if (!this._isLeft) { codewordRowNumber += 2; } switch (codewordRowNumber % 3) { case 0: if (rowIndicatorValue * 3 + 1 !== barcodeMetadata.getRowCountUpperPart()) { codewords[codewordRow] = null; } break; case 1: if (Math.trunc(rowIndicatorValue / 3) !== barcodeMetadata.getErrorCorrectionLevel() || rowIndicatorValue % 3 !== barcodeMetadata.getRowCountLowerPart()) { codewords[codewordRow] = null; } break; case 2: if (rowIndicatorValue + 1 !== barcodeMetadata.getColumnCount()) { codewords[codewordRow] = null; } break; } } } isLeft() { return this._isLeft; } // @Override toString() { return "IsLeft: " + this._isLeft + "\n" + super.toString(); } } class DetectionResult { constructor(barcodeMetadata, boundingBox) { this.ADJUST_ROW_NUMBER_SKIP = 2; this.barcodeMetadata = barcodeMetadata; this.barcodeColumnCount = barcodeMetadata.getColumnCount(); this.boundingBox = boundingBox; this.detectionResultColumns = new Array(this.barcodeColumnCount + 2); } getDetectionResultColumns() { this.adjustIndicatorColumnRowNumbers(this.detectionResultColumns[0]); this.adjustIndicatorColumnRowNumbers(this.detectionResultColumns[this.barcodeColumnCount + 1]); let unadjustedCodewordCount = PDF417Common.MAX_CODEWORDS_IN_BARCODE; let previousUnadjustedCount; do { previousUnadjustedCount = unadjustedCodewordCount; unadjustedCodewordCount = this.adjustRowNumbersAndGetCount(); } while (unadjustedCodewordCount > 0 && unadjustedCodewordCount < previousUnadjustedCount); return this.detectionResultColumns; } adjustIndicatorColumnRowNumbers(detectionResultColumn) { if (detectionResultColumn != null) { detectionResultColumn.adjustCompleteIndicatorColumnRowNumbers(this.barcodeMetadata); } } // TODO ensure that no detected codewords with unknown row number are left // we should be able to estimate the row height and use it as a hint for the row number // we should also fill the rows top to bottom and bottom to top /** * @return number of codewords which don't have a valid row number. Note that the count is not accurate as codewords * will be counted several times. It just serves as an indicator to see when we can stop adjusting row numbers */ adjustRowNumbersAndGetCount() { let unadjustedCount = this.adjustRowNumbersByRow(); if (unadjustedCount === 0) { return 0; } for (let barcodeColumn = 1; barcodeColumn < this.barcodeColumnCount + 1; barcodeColumn++) { let codewords = this.detectionResultColumns[barcodeColumn].getCodewords(); for (let codewordsRow = 0; codewordsRow < codewords.length; codewordsRow++) { if (codewords[codewordsRow] == null) { continue; } if (!codewords[codewordsRow].hasValidRowNumber()) { this.adjustRowNumbers(barcodeColumn, codewordsRow, codewords); } } } return unadjustedCount; } adjustRowNumbersByRow() { this.adjustRowNumbersFromBothRI(); let unadjustedCount = this.adjustRowNumbersFromLRI(); return unadjustedCount + this.adjustRowNumbersFromRRI(); } adjustRowNumbersFromBothRI() { if (this.detectionResultColumns[0] == null || this.detectionResultColumns[this.barcodeColumnCount + 1] == null) { return; } let LRIcodewords = this.detectionResultColumns[0].getCodewords(); let RRIcodewords = this.detectionResultColumns[this.barcodeColumnCount + 1].getCodewords(); for (let codewordsRow = 0; codewordsRow < LRIcodewords.length; codewordsRow++) { if (LRIcodewords[codewordsRow] != null && RRIcodewords[codewordsRow] != null && LRIcodewords[codewordsRow].getRowNumber() === RRIcodewords[codewordsRow].getRowNumber()) { for (let barcodeColumn = 1; barcodeColumn <= this.barcodeColumnCount; barcodeColumn++) { let codeword = this.detectionResultColumns[barcodeColumn].getCodewords()[codewordsRow]; if (codeword == null) { continue; } codeword.setRowNumber(LRIcodewords[codewordsRow].getRowNumber()); if (!codeword.hasValidRowNumber()) { this.detectionResultColumns[barcodeColumn].getCodewords()[codewordsRow] = null; } } } } } adjustRowNumbersFromRRI() { if (this.detectionResultColumns[this.barcodeColumnCount + 1] == null) { return 0; } let unadjustedCount = 0; let codewords = this.detectionResultColumns[this.barcodeColumnCount + 1].getCodewords(); for (let codewordsRow = 0; codewordsRow < codewords.length; codewordsRow++) { if (codewords[codewordsRow] == null) { continue; } let rowIndicatorRowNumber = codewords[codewordsRow].getRowNumber(); let invalidRowCounts = 0; for (let barcodeColumn = this.barcodeColumnCount + 1; barcodeColumn > 0 && invalidRowCounts < this.ADJUST_ROW_NUMBER_SKIP; barcodeColumn--) { let codeword = this.detectionResultColumns[barcodeColumn].getCodewords()[codewordsRow]; if (codeword != null) { invalidRowCounts = DetectionResult.adjustRowNumberIfValid(rowIndicatorRowNumber, invalidRowCounts, codeword); if (!codeword.hasValidRowNumber()) { unadjustedCount++; } } } } return unadjustedCount; } adjustRowNumbersFromLRI() { if (this.detectionResultColumns[0] == null) { return 0; } let unadjustedCount = 0; let codewords = this.detectionResultColumns[0].getCodewords(); for (let codewordsRow = 0; codewordsRow < codewords.length; codewordsRow++) { if (codewords[codewordsRow] == null) { continue; } let rowIndicatorRowNumber = codewords[codewordsRow].getRowNumber(); let invalidRowCounts = 0; for (let barcodeColumn = 1; barcodeColumn < this.barcodeColumnCount + 1 && invalidRowCounts < this.ADJUST_ROW_NUMBER_SKIP; barcodeColumn++) { let codeword = this.detectionResultColumns[barcodeColumn].getCodewords()[codewordsRow]; if (codeword != null) { invalidRowCounts = DetectionResult.adjustRowNumberIfValid(rowIndicatorRowNumber, invalidRowCounts, codeword); if (!codeword.hasValidRowNumber()) { unadjustedCount++; } } } } return unadjustedCount; } static adjustRowNumberIfValid(rowIndicatorRowNumber, invalidRowCounts, codeword) { if (codeword == null) { return invalidRowCounts; } if (!codeword.hasValidRowNumber()) { if (codeword.isValidRowNumber(rowIndicatorRowNumber)) { codeword.setRowNumber(rowIndicatorRowNumber); invalidRowCounts = 0; } else { ++invalidRowCounts; } } return invalidRowCounts; } adjustRowNumbers(barcodeColumn, codewordsRow, codewords) { if (!this.detectionResultColumns[barcodeColumn - 1]) { return; } let codeword = codewords[codewordsRow]; let previousColumnCodewords = this.detectionResultColumns[barcodeColumn - 1].getCodewords(); let nextColumnCodewords = previousColumnCodewords; if (this.detectionResultColumns[barcodeColumn + 1] != null) { nextColumnCodewords = this.detectionResultColumns[barcodeColumn + 1].getCodewords(); } let otherCodewords = new Array(14); otherCodewords[2] = previousColumnCodewords[codewordsRow]; otherCodewords[3] = nextColumnCodewords[codewordsRow]; if (codewordsRow > 0) { otherCodewords[0] = codewords[codewordsRow - 1]; otherCodewords[4] = previousColumnCodewords[codewordsRow - 1]; otherCodewords[5] = nextColumnCodewords[codewordsRow - 1]; } if (codewordsRow > 1) { otherCodewords[8] = codewords[codewordsRow - 2]; otherCodewords[10] = previousColumnCodewords[codewordsRow - 2]; otherCodewords[11] = nextColumnCodewords[codewordsRow - 2]; } if (codewordsRow < codewords.length - 1) { otherCodewords[1] = codewords[codewordsRow + 1]; otherCodewords[6] = previousColumnCodewords[codewordsRow + 1]; otherCodewords[7] = nextColumnCodewords[codewordsRow + 1]; } if (codewordsRow < codewords.length - 2) { otherCodewords[9] = codewords[codewordsRow + 2]; otherCodewords[12] = previousColumnCodewords[codewordsRow + 2]; otherCodewords[13] = nextColumnCodewords[codewordsRow + 2]; } for (let otherCodeword of otherCodewords) { if (DetectionResult.adjustRowNumber(codeword, otherCodeword)) { return; } } } /** * @return true, if row number was adjusted, false otherwise */ static adjustRowNumber(codeword, otherCodeword) { if (otherCodeword == null) { return false; } if (otherCodeword.hasValidRowNumber() && otherCodeword.getBucket() === codeword.getBucket()) { codeword.setRowNumber(otherCodeword.getRowNumber()); return true; } return false; } getBarcodeColumnCount() { return this.barcodeColumnCount; } getBarcodeRowCount() { return this.barcodeMetadata.getRowCount(); } getBarcodeECLevel() { return this.barcodeMetadata.getErrorCorrectionLevel(); } setBoundingBox(boundingBox) { this.boundingBox = boundingBox; } getBoundingBox() { return this.boundingBox; } setDetectionResultColumn(barcodeColumn, detectionResultColumn) { this.detectionResultColumns[barcodeColumn] = detectionResultColumn; } getDetectionResultColumn(barcodeColumn) { return this.detectionResultColumns[barcodeColumn]; } // @Override toString() { let rowIndicatorColumn = this.detectionResultColumns[0]; if (rowIndicatorColumn == null) { rowIndicatorColumn = this.detectionResultColumns[this.barcodeColumnCount + 1]; } let formatter = new Formatter(); for (let codewordsRow = 0; codewordsRow < rowIndicatorColumn.getCodewords().length; codewordsRow++) { formatter.format("CW %3d:", codewordsRow); for (let barcodeColumn = 0; barcodeColumn < this.barcodeColumnCount + 2; barcodeColumn++) { if (this.detectionResultColumns[barcodeColumn] == null) { formatter.format(" | "); continue; } let codeword = this.detectionResultColumns[barcodeColumn].getCodewords()[codewordsRow]; if (codeword == null) { formatter.format(" | "); continue; } formatter.format(" %3d|%3d", codeword.getRowNumber(), codeword.getValue()); } formatter.format("%n"); } return formatter.toString(); } } class Codeword { constructor(startX, endX, bucket, value) { this.rowNumber = Codeword.BARCODE_ROW_UNKNOWN; this.startX = Math.trunc(startX); this.endX = Math.trunc(endX); this.bucket = Math.trunc(bucket); this.value = Math.trunc(value); } hasValidRowNumber() { return this.isValidRowNumber(this.rowNumber); } isValidRowNumber(rowNumber) { return rowNumber !== Codeword.BARCODE_ROW_UNKNOWN && this.bucket === rowNumber % 3 * 3; } setRowNumberAsRowIndicatorColumn() { this.rowNumber = Math.trunc(Math.trunc(this.value / 30) * 3 + Math.trunc(this.bucket / 3)); } getWidth() { return this.endX - this.startX; } getStartX() { return this.startX; } getEndX() { return this.endX; } getBucket() { return this.bucket; } getValue() { return this.value; } getRowNumber() { return this.rowNumber; } setRowNumber(rowNumber) { this.rowNumber = rowNumber; } // @Override toString() { return this.rowNumber + "|" + this.value; } } Codeword.BARCODE_ROW_UNKNOWN = -1; class PDF417CodewordDecoder { /* @note * this action have to be performed before first use of class * - static constructor * working with 32bit float (based from Java logic) */ static initialize() { for (let i = 0; i < PDF417Common.SYMBOL_TABLE.length; i++) { let currentSymbol = PDF417Common.SYMBOL_TABLE[i]; let currentBit = currentSymbol & 1; for (let j = 0; j < PDF417Common.BARS_IN_MODULE; j++) { let size = 0; while ((currentSymbol & 1) === currentBit) { size += 1; currentSymbol >>= 1; } currentBit = currentSymbol & 1; if (!PDF417CodewordDecoder.RATIOS_TABLE[i]) { PDF417CodewordDecoder.RATIOS_TABLE[i] = new Array(PDF417Common.BARS_IN_MODULE); } PDF417CodewordDecoder.RATIOS_TABLE[i][PDF417Common.BARS_IN_MODULE - j - 1] = Math.fround(size / PDF417Common.MODULES_IN_CODEWORD); } } this.bSymbolTableReady = true; } static getDecodedValue(moduleBitCount) { let decodedValue = PDF417CodewordDecoder.getDecodedCodewordValue(PDF417CodewordDecoder.sampleBitCounts(moduleBitCount)); if (decodedValue !== -1) { return decodedValue; } return PDF417CodewordDecoder.getClosestDecodedValue(moduleBitCount); } static sampleBitCounts(moduleBitCount) { let bitCountSum = MathUtils.sum(moduleBitCount); let result = new Int32Array(PDF417Common.BARS_IN_MODULE); let bitCountIndex = 0; let sumPreviousBits = 0; for (let i = 0; i < PDF417Common.MODULES_IN_CODEWORD; i++) { let sampleIndex = bitCountSum / (2 * PDF417Common.MODULES_IN_CODEWORD) + i * bitCountSum / PDF417Common.MODULES_IN_CODEWORD; if (sumPreviousBits + moduleBitCount[bitCountIndex] <= sampleIndex) { sumPreviousBits += moduleBitCount[bitCountIndex]; bitCountIndex++; } result[bitCountIndex]++; } return result; } static getDecodedCodewordValue(moduleBitCount) { let decodedValue = PDF417CodewordDecoder.getBitValue(moduleBitCount); return PDF417Common.getCodeword(decodedValue) === -1 ? -1 : decodedValue; } static getBitValue(moduleBitCount) { let result = ( /*long*/ 0 ); for (let i = 0; i < moduleBitCount.length; i++) { for (let bit = 0; bit < moduleBitCount[i]; bit++) { result = result << 1 | (i % 2 === 0 ? 1 : 0); } } return Math.trunc(result); } // working with 32bit float (as in Java) static getClosestDecodedValue(moduleBitCount) { let bitCountSum = MathUtils.sum(moduleBitCount); let bitCountRatios = new Array(PDF417Common.BARS_IN_MODULE); if (bitCountSum > 1) { for (let i = 0; i < bitCountRatios.length; i++) { bitCountRatios[i] = Math.fround(moduleBitCount[i] / bitCountSum); } } let bestMatchError = Float.MAX_VALUE; let bestMatch = -1; if (!this.bSymbolTableReady) { PDF417CodewordDecoder.initialize(); } for (let j = 0; j < PDF417CodewordDecoder.RATIOS_TABLE.length; j++) { let error = 0; let ratioTableRow = PDF417CodewordDecoder.RATIOS_TABLE[j]; for (let k = 0; k < PDF417Common.BARS_IN_MODULE; k++) { let diff = Math.fround(ratioTableRow[k] - bitCountRatios[k]); error += Math.fround(diff * diff); if (error >= bestMatchError) { break; } } if (error < bestMatchError) { bestMatchError = error; bestMatch = PDF417Common.SYMBOL_TABLE[j]; } } return bestMatch; } } PDF417CodewordDecoder.bSymbolTableReady = false; PDF417CodewordDecoder.RATIOS_TABLE = new Array(PDF417Common.SYMBOL_TABLE.length).map((x) => x = new Array(PDF417Common.BARS_IN_MODULE)); class PDF417ResultMetadata { constructor() { this.segmentCount = -1; this.fileSize = -1; this.timestamp = -1; this.checksum = -1; } /** * The Segment ID represents the segment of the whole file distributed over different symbols. * * @return File segment index */ getSegmentIndex() { return this.segmentIndex; } setSegmentIndex(segmentIndex) { this.segmentIndex = segmentIndex; } /** * Is the same for each related PDF417 symbol * * @return File ID */ getFileId() { return this.fileId; } setFileId(fileId) { this.fileId = fileId; } /** * @return always null * @deprecated use dedicated already parsed fields */ // @Deprecated getOptionalData() { return this.optionalData; } /** * @param optionalData old optional data format as int array * @deprecated parse and use new fields */ // @Deprecated setOptionalData(optionalData) { this.optionalData = optionalData; } /** * @return true if it is the last segment */ isLastSegment() { return this.lastSegment; } setLastSegment(lastSegment) { this.lastSegment = lastSegment; } /** * @return count of segments, -1 if not set */ getSegmentCount() { return this.segmentCount; } setSegmentCount(segmentCount) { this.segmentCount = segmentCount; } getSender() { return this.sender || null; } setSender(sender) { this.sender = sender; } getAddressee() { return this.addressee || null; } setAddressee(addressee) { this.addressee = addressee; } /** * Filename of the encoded file * * @return filename */ getFileName() { return this.fileName; } setFileName(fileName) { this.fileName = fileName; } /** * filesize in bytes of the encoded file * * @return filesize in bytes, -1 if not set */ getFileSize() { return this.fileSize; } setFileSize(fileSize) { this.fileSize = fileSize; } /** * 16-bit CRC checksum using CCITT-16 * * @return crc checksum, -1 if not set */ getChecksum() { return this.checksum; } setChecksum(checksum) { this.checksum = checksum; } /** * unix epock timestamp, elapsed seconds since 1970-01-01 * * @return elapsed seconds, -1 if not set */ getTimestamp() { return this.timestamp; } setTimestamp(timestamp) { this.timestamp = timestamp; } } class Long { /** * Parses a string to a number, since JS has no really Int64. * * @param num Numeric string. * @param radix Destination radix. */ static parseLong(num, radix = void 0) { return parseInt(num, radix); } } class NullPointerException extends Exception { } NullPointerException.kind = "NullPointerException"; class OutputStream { /** * Writesb.length bytes from the specified byte array
* to this output stream. The general contract for write(b)
* is that it should have exactly the same effect as the call
* write(b, 0, b.length).
*
* @param b the data.
* @exception IOException if an I/O error occurs.
* @see java.io.OutputStream#write(byte[], int, int)
*/
writeBytes(b) {
this.writeBytesOffset(b, 0, b.length);
}
/**
* Writes len bytes from the specified byte array
* starting at offset off to this output stream.
* The general contract for write(b, off, len) is that
* some of the bytes in the array b are written to the
* output stream in order; element b[off] is the first
* byte written and b[off+len-1] is the last byte written
* by this operation.
*
* The write method of OutputStream calls
* the write method of one argument on each of the bytes to be
* written out. Subclasses are encouraged to override this method and
* provide a more efficient implementation.
*
* If b is null, a
* NullPointerException is thrown.
*
* If off is negative, or len is negative, or
* off+len is greater than the length of the array
* b, then an IndexOutOfBoundsException is thrown.
*
* @param b the data.
* @param off the start offset in the data.
* @param len the number of bytes to write.
* @exception IOException if an I/O error occurs. In particular,
* an IOException is thrown if the output
* stream is closed.
*/
writeBytesOffset(b, off, len) {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || off > b.length || len < 0 || off + len > b.length || off + len < 0) {
throw new IndexOutOfBoundsException();
} else if (len === 0) {
return;
}
for (let i = 0; i < len; i++) {
this.write(b[off + i]);
}
}
/**
* Flushes this output stream and forces any buffered output bytes
* to be written out. The general contract of flush is
* that calling it is an indication that, if any bytes previously
* written have been buffered by the implementation of the output
* stream, such bytes should immediately be written to their
* intended destination.
*
* If the intended destination of this stream is an abstraction provided by * the underlying operating system, for example a file, then flushing the * stream guarantees only that bytes previously written to the stream are * passed to the operating system for writing; it does not guarantee that * they are actually written to a physical device such as a disk drive. *
* The flush method of OutputStream does nothing.
*
* @exception IOException if an I/O error occurs.
*/
flush() {
}
/**
* Closes this output stream and releases any system resources
* associated with this stream. The general contract of close
* is that it closes the output stream. A closed stream cannot perform
* output operations and cannot be reopened.
*
* The close method of OutputStream does nothing.
*
* @exception IOException if an I/O error occurs.
*/
close() {
}
}
class OutOfMemoryError extends Exception {
}
class ByteArrayOutputStream extends OutputStream {
/**
* Creates a new byte array output stream. The buffer capacity is
* initially 32 bytes, though its size increases if necessary.
*/
// public constructor() {
// this(32);
// }
/**
* Creates a new byte array output stream, with a buffer capacity of
* the specified size, in bytes.
*
* @param size the initial size.
* @exception IllegalArgumentException if size is negative.
*/
constructor(size = 32) {
super();
this.count = 0;
if (size < 0) {
throw new IllegalArgumentException("Negative initial size: " + size);
}
this.buf = new Uint8Array(size);
}
/**
* Increases the capacity if necessary to ensure that it can hold
* at least the number of elements specified by the minimum
* capacity argument.
*
* @param minCapacity the desired minimum capacity
* @throws OutOfMemoryError if {@code minCapacity < 0}. This is
* interpreted as a request for the unsatisfiably large capacity
* {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}.
*/
ensureCapacity(minCapacity) {
if (minCapacity - this.buf.length > 0)
this.grow(minCapacity);
}
/**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
grow(minCapacity) {
let oldCapacity = this.buf.length;
let newCapacity = oldCapacity << 1;
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity < 0) {
if (minCapacity < 0)
throw new OutOfMemoryError();
newCapacity = Integer.MAX_VALUE;
}
this.buf = Arrays.copyOfUint8Array(this.buf, newCapacity);
}
/**
* Writes the specified byte to this byte array output stream.
*
* @param b the byte to be written.
*/
write(b) {
this.ensureCapacity(this.count + 1);
this.buf[this.count] = /*(byte)*/
b;
this.count += 1;
}
/**
* Writes len bytes from the specified byte array
* starting at offset off to this byte array output stream.
*
* @param b the data.
* @param off the start offset in the data.
* @param len the number of bytes to write.
*/
writeBytesOffset(b, off, len) {
if (off < 0 || off > b.length || len < 0 || off + len - b.length > 0) {
throw new IndexOutOfBoundsException();
}
this.ensureCapacity(this.count + len);
System.arraycopy(b, off, this.buf, this.count, len);
this.count += len;
}
/**
* Writes the complete contents of this byte array output stream to
* the specified output stream argument, as if by calling the output
* stream's write method using out.write(buf, 0, count).
*
* @param out the output stream to which to write the data.
* @exception IOException if an I/O error occurs.
*/
writeTo(out) {
out.writeBytesOffset(this.buf, 0, this.count);
}
/**
* Resets the count field of this byte array output
* stream to zero, so that all currently accumulated output in the
* output stream is discarded. The output stream can be used again,
* reusing the already allocated buffer space.
*
* @see java.io.ByteArrayInputStream#count
*/
reset() {
this.count = 0;
}
/**
* Creates a newly allocated byte array. Its size is the current
* size of this output stream and the valid contents of the buffer
* have been copied into it.
*
* @return the current contents of this output stream, as a byte array.
* @see java.io.ByteArrayOutputStream#size()
*/
toByteArray() {
return Arrays.copyOfUint8Array(this.buf, this.count);
}
/**
* Returns the current size of the buffer.
*
* @return the value of the count field, which is the number
* of valid bytes in this output stream.
* @see java.io.ByteArrayOutputStream#count
*/
size() {
return this.count;
}
toString(param) {
if (!param) {
return this.toString_void();
}
if (typeof param === "string") {
return this.toString_string(param);
}
return this.toString_number(param);
}
/**
* Converts the buffer's contents into a string decoding bytes using the
* platform's default character set. The length of the new String
* is a function of the character set, and hence may not be equal to the
* size of the buffer.
*
*
This method always replaces malformed-input and unmappable-character * sequences with the default replacement string for the platform's * default character set. The {@linkplain java.nio.charset.CharsetDecoder} * class should be used when more control over the decoding process is * required. * * @return String decoded from the buffer's contents. * @since JDK1.1 */ toString_void() { return new String( this.buf /*, 0, this.count*/ ).toString(); } /** * Converts the buffer's contents into a string by decoding the bytes using * the specified {@link java.nio.charset.Charset charsetName}. The length of * the new String is a function of the charset, and hence may not be * equal to the length of the byte array. * *
This method always replaces malformed-input and unmappable-character
* sequences with this charset's default replacement string. The {@link
* java.nio.charset.CharsetDecoder} class should be used when more control
* over the decoding process is required.
*
* @param charsetName the name of a supported
* {@linkplain java.nio.charset.Charset charset
*
* @throws IOException
*/
close() {
}
}
var Mode$2;
(function(Mode2) {
Mode2[Mode2["ALPHA"] = 0] = "ALPHA";
Mode2[Mode2["LOWER"] = 1] = "LOWER";
Mode2[Mode2["MIXED"] = 2] = "MIXED";
Mode2[Mode2["PUNCT"] = 3] = "PUNCT";
Mode2[Mode2["ALPHA_SHIFT"] = 4] = "ALPHA_SHIFT";
Mode2[Mode2["PUNCT_SHIFT"] = 5] = "PUNCT_SHIFT";
})(Mode$2 || (Mode$2 = {}));
function getBigIntConstructor() {
if (typeof window !== "undefined") {
return window["BigInt"] || null;
}
if (typeof global !== "undefined") {
return global["BigInt"] || null;
}
if (typeof self !== "undefined") {
return self["BigInt"] || null;
}
throw new Error("Can't search globals for BigInt!");
}
let BigInteger;
function createBigInt(num) {
if (typeof BigInteger === "undefined") {
BigInteger = getBigIntConstructor();
}
if (BigInteger === null) {
throw new Error("BigInt is not supported!");
}
return BigInteger(num);
}
function getEXP900() {
let EXP900 = [];
EXP900[0] = createBigInt(1);
let nineHundred = createBigInt(900);
EXP900[1] = nineHundred;
for (let i = 2; i < 16; i++) {
EXP900[i] = EXP900[i - 1] * nineHundred;
}
return EXP900;
}
class DecodedBitStreamParser$2 {
// private DecodedBitStreamParser() {
// }
/**
*
* @param codewords
* @param ecLevel
*
* @throws FormatException
*/
static decode(codewords, ecLevel) {
let result = new StringBuilder("");
let encoding = CharacterSetECI.ISO8859_1;
result.enableDecoding(encoding);
let codeIndex = 1;
let code = codewords[codeIndex++];
let resultMetadata = new PDF417ResultMetadata();
while (codeIndex < codewords[0]) {
switch (code) {
case DecodedBitStreamParser$2.TEXT_COMPACTION_MODE_LATCH:
codeIndex = DecodedBitStreamParser$2.textCompaction(codewords, codeIndex, result);
break;
case DecodedBitStreamParser$2.BYTE_COMPACTION_MODE_LATCH:
case DecodedBitStreamParser$2.BYTE_COMPACTION_MODE_LATCH_6:
codeIndex = DecodedBitStreamParser$2.byteCompaction(code, codewords, encoding, codeIndex, result);
break;
case DecodedBitStreamParser$2.MODE_SHIFT_TO_BYTE_COMPACTION_MODE:
result.append(
/*(char)*/
codewords[codeIndex++]
);
break;
case DecodedBitStreamParser$2.NUMERIC_COMPACTION_MODE_LATCH:
codeIndex = DecodedBitStreamParser$2.numericCompaction(codewords, codeIndex, result);
break;
case DecodedBitStreamParser$2.ECI_CHARSET:
let charsetECI = CharacterSetECI.getCharacterSetECIByValue(codewords[codeIndex++]);
break;
case DecodedBitStreamParser$2.ECI_GENERAL_PURPOSE:
codeIndex += 2;
break;
case DecodedBitStreamParser$2.ECI_USER_DEFINED:
codeIndex++;
break;
case DecodedBitStreamParser$2.BEGIN_MACRO_PDF417_CONTROL_BLOCK:
codeIndex = DecodedBitStreamParser$2.decodeMacroBlock(codewords, codeIndex, resultMetadata);
break;
case DecodedBitStreamParser$2.BEGIN_MACRO_PDF417_OPTIONAL_FIELD:
case DecodedBitStreamParser$2.MACRO_PDF417_TERMINATOR:
throw new FormatException();
default:
codeIndex--;
codeIndex = DecodedBitStreamParser$2.textCompaction(codewords, codeIndex, result);
break;
}
if (codeIndex < codewords.length) {
code = codewords[codeIndex++];
} else {
throw FormatException.getFormatInstance();
}
}
if (result.length() === 0) {
throw FormatException.getFormatInstance();
}
let decoderResult = new DecoderResult(null, result.toString(), null, ecLevel);
decoderResult.setOther(resultMetadata);
return decoderResult;
}
/**
*
* @param int
* @param param1
* @param codewords
* @param int
* @param codeIndex
* @param PDF417ResultMetadata
* @param resultMetadata
*
* @throws FormatException
*/
// @SuppressWarnings("deprecation")
static decodeMacroBlock(codewords, codeIndex, resultMetadata) {
if (codeIndex + DecodedBitStreamParser$2.NUMBER_OF_SEQUENCE_CODEWORDS > codewords[0]) {
throw FormatException.getFormatInstance();
}
let segmentIndexArray = new Int32Array(DecodedBitStreamParser$2.NUMBER_OF_SEQUENCE_CODEWORDS);
for (let i = 0; i < DecodedBitStreamParser$2.NUMBER_OF_SEQUENCE_CODEWORDS; i++, codeIndex++) {
segmentIndexArray[i] = codewords[codeIndex];
}
resultMetadata.setSegmentIndex(Integer.parseInt(DecodedBitStreamParser$2.decodeBase900toBase10(segmentIndexArray, DecodedBitStreamParser$2.NUMBER_OF_SEQUENCE_CODEWORDS)));
let fileId = new StringBuilder();
codeIndex = DecodedBitStreamParser$2.textCompaction(codewords, codeIndex, fileId);
resultMetadata.setFileId(fileId.toString());
let optionalFieldsStart = -1;
if (codewords[codeIndex] === DecodedBitStreamParser$2.BEGIN_MACRO_PDF417_OPTIONAL_FIELD) {
optionalFieldsStart = codeIndex + 1;
}
while (codeIndex < codewords[0]) {
switch (codewords[codeIndex]) {
case DecodedBitStreamParser$2.BEGIN_MACRO_PDF417_OPTIONAL_FIELD:
codeIndex++;
switch (codewords[codeIndex]) {
case DecodedBitStreamParser$2.MACRO_PDF417_OPTIONAL_FIELD_FILE_NAME:
let fileName = new StringBuilder();
codeIndex = DecodedBitStreamParser$2.textCompaction(codewords, codeIndex + 1, fileName);
resultMetadata.setFileName(fileName.toString());
break;
case DecodedBitStreamParser$2.MACRO_PDF417_OPTIONAL_FIELD_SENDER:
let sender = new StringBuilder();
codeIndex = DecodedBitStreamParser$2.textCompaction(codewords, codeIndex + 1, sender);
resultMetadata.setSender(sender.toString());
break;
case DecodedBitStreamParser$2.MACRO_PDF417_OPTIONAL_FIELD_ADDRESSEE:
let addressee = new StringBuilder();
codeIndex = DecodedBitStreamParser$2.textCompaction(codewords, codeIndex + 1, addressee);
resultMetadata.setAddressee(addressee.toString());
break;
case DecodedBitStreamParser$2.MACRO_PDF417_OPTIONAL_FIELD_SEGMENT_COUNT:
let segmentCount = new StringBuilder();
codeIndex = DecodedBitStreamParser$2.numericCompaction(codewords, codeIndex + 1, segmentCount);
resultMetadata.setSegmentCount(Integer.parseInt(segmentCount.toString()));
break;
case DecodedBitStreamParser$2.MACRO_PDF417_OPTIONAL_FIELD_TIME_STAMP:
let timestamp = new StringBuilder();
codeIndex = DecodedBitStreamParser$2.numericCompaction(codewords, codeIndex + 1, timestamp);
resultMetadata.setTimestamp(Long.parseLong(timestamp.toString()));
break;
case DecodedBitStreamParser$2.MACRO_PDF417_OPTIONAL_FIELD_CHECKSUM:
let checksum = new StringBuilder();
codeIndex = DecodedBitStreamParser$2.numericCompaction(codewords, codeIndex + 1, checksum);
resultMetadata.setChecksum(Integer.parseInt(checksum.toString()));
break;
case DecodedBitStreamParser$2.MACRO_PDF417_OPTIONAL_FIELD_FILE_SIZE:
let fileSize = new StringBuilder();
codeIndex = DecodedBitStreamParser$2.numericCompaction(codewords, codeIndex + 1, fileSize);
resultMetadata.setFileSize(Long.parseLong(fileSize.toString()));
break;
default:
throw FormatException.getFormatInstance();
}
break;
case DecodedBitStreamParser$2.MACRO_PDF417_TERMINATOR:
codeIndex++;
resultMetadata.setLastSegment(true);
break;
default:
throw FormatException.getFormatInstance();
}
}
if (optionalFieldsStart !== -1) {
let optionalFieldsLength = codeIndex - optionalFieldsStart;
if (resultMetadata.isLastSegment()) {
optionalFieldsLength--;
}
resultMetadata.setOptionalData(Arrays.copyOfRange(codewords, optionalFieldsStart, optionalFieldsStart + optionalFieldsLength));
}
return codeIndex;
}
/**
* Text Compaction mode (see 5.4.1.5) permits all printable ASCII characters to be
* encoded, i.e. values 32 - 126 inclusive in accordance with ISO/IEC 646 (IRV), as
* well as selected control characters.
*
* @param codewords The array of codewords (data + error)
* @param codeIndex The current index into the codeword array.
* @param result The decoded data is appended to the result.
* @return The next index into the codeword array.
*/
static textCompaction(codewords, codeIndex, result) {
let textCompactionData = new Int32Array((codewords[0] - codeIndex) * 2);
let byteCompactionData = new Int32Array((codewords[0] - codeIndex) * 2);
let index = 0;
let end = false;
while (codeIndex < codewords[0] && !end) {
let code = codewords[codeIndex++];
if (code < DecodedBitStreamParser$2.TEXT_COMPACTION_MODE_LATCH) {
textCompactionData[index] = code / 30;
textCompactionData[index + 1] = code % 30;
index += 2;
} else {
switch (code) {
case DecodedBitStreamParser$2.TEXT_COMPACTION_MODE_LATCH:
textCompactionData[index++] = DecodedBitStreamParser$2.TEXT_COMPACTION_MODE_LATCH;
break;
case DecodedBitStreamParser$2.BYTE_COMPACTION_MODE_LATCH:
case DecodedBitStreamParser$2.BYTE_COMPACTION_MODE_LATCH_6:
case DecodedBitStreamParser$2.NUMERIC_COMPACTION_MODE_LATCH:
case DecodedBitStreamParser$2.BEGIN_MACRO_PDF417_CONTROL_BLOCK:
case DecodedBitStreamParser$2.BEGIN_MACRO_PDF417_OPTIONAL_FIELD:
case DecodedBitStreamParser$2.MACRO_PDF417_TERMINATOR:
codeIndex--;
end = true;
break;
case DecodedBitStreamParser$2.MODE_SHIFT_TO_BYTE_COMPACTION_MODE:
textCompactionData[index] = DecodedBitStreamParser$2.MODE_SHIFT_TO_BYTE_COMPACTION_MODE;
code = codewords[codeIndex++];
byteCompactionData[index] = code;
index++;
break;
}
}
}
DecodedBitStreamParser$2.decodeTextCompaction(textCompactionData, byteCompactionData, index, result);
return codeIndex;
}
/**
* The Text Compaction mode includes all the printable ASCII characters
* (i.e. values from 32 to 126) and three ASCII control characters: HT or tab
* (9: e), LF or line feed (10: e), and CR or carriage
* return (13: e). The Text Compaction mode also includes various latch
* and shift characters which are used exclusively within the mode. The Text
* Compaction mode encodes up to 2 characters per codeword. The compaction rules
* for converting data into PDF417 codewords are defined in 5.4.2.2. The sub-mode
* switches are defined in 5.4.2.3.
*
* @param textCompactionData The text compaction data.
* @param byteCompactionData The byte compaction data if there
* was a mode shift.
* @param length The size of the text compaction and byte compaction data.
* @param result The decoded data is appended to the result.
*/
static decodeTextCompaction(textCompactionData, byteCompactionData, length, result) {
let subMode = Mode$2.ALPHA;
let priorToShiftMode = Mode$2.ALPHA;
let i = 0;
while (i < length) {
let subModeCh = textCompactionData[i];
let ch = (
/*char*/
""
);
switch (subMode) {
case Mode$2.ALPHA:
if (subModeCh < 26) {
ch = /*(char)('A' + subModeCh) */
String.fromCharCode(65 + subModeCh);
} else {
switch (subModeCh) {
case 26:
ch = " ";
break;
case DecodedBitStreamParser$2.LL:
subMode = Mode$2.LOWER;
break;
case DecodedBitStreamParser$2.ML:
subMode = Mode$2.MIXED;
break;
case DecodedBitStreamParser$2.PS:
priorToShiftMode = subMode;
subMode = Mode$2.PUNCT_SHIFT;
break;
case DecodedBitStreamParser$2.MODE_SHIFT_TO_BYTE_COMPACTION_MODE:
result.append(
/*(char)*/
byteCompactionData[i]
);
break;
case DecodedBitStreamParser$2.TEXT_COMPACTION_MODE_LATCH:
subMode = Mode$2.ALPHA;
break;
}
}
break;
case Mode$2.LOWER:
if (subModeCh < 26) {
ch = /*(char)('a' + subModeCh)*/
String.fromCharCode(97 + subModeCh);
} else {
switch (subModeCh) {
case 26:
ch = " ";
break;
case DecodedBitStreamParser$2.AS:
priorToShiftMode = subMode;
subMode = Mode$2.ALPHA_SHIFT;
break;
case DecodedBitStreamParser$2.ML:
subMode = Mode$2.MIXED;
break;
case DecodedBitStreamParser$2.PS:
priorToShiftMode = subMode;
subMode = Mode$2.PUNCT_SHIFT;
break;
case DecodedBitStreamParser$2.MODE_SHIFT_TO_BYTE_COMPACTION_MODE:
result.append(
/*(char)*/
byteCompactionData[i]
);
break;
case DecodedBitStreamParser$2.TEXT_COMPACTION_MODE_LATCH:
subMode = Mode$2.ALPHA;
break;
}
}
break;
case Mode$2.MIXED:
if (subModeCh < DecodedBitStreamParser$2.PL) {
ch = DecodedBitStreamParser$2.MIXED_CHARS[subModeCh];
} else {
switch (subModeCh) {
case DecodedBitStreamParser$2.PL:
subMode = Mode$2.PUNCT;
break;
case 26:
ch = " ";
break;
case DecodedBitStreamParser$2.LL:
subMode = Mode$2.LOWER;
break;
case DecodedBitStreamParser$2.AL:
subMode = Mode$2.ALPHA;
break;
case DecodedBitStreamParser$2.PS:
priorToShiftMode = subMode;
subMode = Mode$2.PUNCT_SHIFT;
break;
case DecodedBitStreamParser$2.MODE_SHIFT_TO_BYTE_COMPACTION_MODE:
result.append(
/*(char)*/
byteCompactionData[i]
);
break;
case DecodedBitStreamParser$2.TEXT_COMPACTION_MODE_LATCH:
subMode = Mode$2.ALPHA;
break;
}
}
break;
case Mode$2.PUNCT:
if (subModeCh < DecodedBitStreamParser$2.PAL) {
ch = DecodedBitStreamParser$2.PUNCT_CHARS[subModeCh];
} else {
switch (subModeCh) {
case DecodedBitStreamParser$2.PAL:
subMode = Mode$2.ALPHA;
break;
case DecodedBitStreamParser$2.MODE_SHIFT_TO_BYTE_COMPACTION_MODE:
result.append(
/*(char)*/
byteCompactionData[i]
);
break;
case DecodedBitStreamParser$2.TEXT_COMPACTION_MODE_LATCH:
subMode = Mode$2.ALPHA;
break;
}
}
break;
case Mode$2.ALPHA_SHIFT:
subMode = priorToShiftMode;
if (subModeCh < 26) {
ch = /*(char)('A' + subModeCh)*/
String.fromCharCode(65 + subModeCh);
} else {
switch (subModeCh) {
case 26:
ch = " ";
break;
case DecodedBitStreamParser$2.TEXT_COMPACTION_MODE_LATCH:
subMode = Mode$2.ALPHA;
break;
}
}
break;
case Mode$2.PUNCT_SHIFT:
subMode = priorToShiftMode;
if (subModeCh < DecodedBitStreamParser$2.PAL) {
ch = DecodedBitStreamParser$2.PUNCT_CHARS[subModeCh];
} else {
switch (subModeCh) {
case DecodedBitStreamParser$2.PAL:
subMode = Mode$2.ALPHA;
break;
case DecodedBitStreamParser$2.MODE_SHIFT_TO_BYTE_COMPACTION_MODE:
result.append(
/*(char)*/
byteCompactionData[i]
);
break;
case DecodedBitStreamParser$2.TEXT_COMPACTION_MODE_LATCH:
subMode = Mode$2.ALPHA;
break;
}
}
break;
}
if (ch !== "") {
result.append(ch);
}
i++;
}
}
/**
* Byte Compaction mode (see 5.4.3) permits all 256 possible 8-bit byte values to be encoded.
* This includes all ASCII characters value 0 to 127 inclusive and provides for international
* character set support.
*
* @param mode The byte compaction mode i.e. 901 or 924
* @param codewords The array of codewords (data + error)
* @param encoding Currently active character encoding
* @param codeIndex The current index into the codeword array.
* @param result The decoded data is appended to the result.
* @return The next index into the codeword array.
*/
static byteCompaction(mode, codewords, encoding, codeIndex, result) {
let decodedBytes = new ByteArrayOutputStream();
let count = 0;
let value = (
/*long*/
0
);
let end = false;
switch (mode) {
case DecodedBitStreamParser$2.BYTE_COMPACTION_MODE_LATCH:
let byteCompactedCodewords = new Int32Array(6);
let nextCode = codewords[codeIndex++];
while (codeIndex < codewords[0] && !end) {
byteCompactedCodewords[count++] = nextCode;
value = 900 * value + nextCode;
nextCode = codewords[codeIndex++];
switch (nextCode) {
case DecodedBitStreamParser$2.TEXT_COMPACTION_MODE_LATCH:
case DecodedBitStreamParser$2.BYTE_COMPACTION_MODE_LATCH:
case DecodedBitStreamParser$2.NUMERIC_COMPACTION_MODE_LATCH:
case DecodedBitStreamParser$2.BYTE_COMPACTION_MODE_LATCH_6:
case DecodedBitStreamParser$2.BEGIN_MACRO_PDF417_CONTROL_BLOCK:
case DecodedBitStreamParser$2.BEGIN_MACRO_PDF417_OPTIONAL_FIELD:
case DecodedBitStreamParser$2.MACRO_PDF417_TERMINATOR:
codeIndex--;
end = true;
break;
default:
if (count % 5 === 0 && count > 0) {
for (let j = 0; j < 6; ++j) {
decodedBytes.write(
/*(byte)*/
Number(createBigInt(value) >> createBigInt(8 * (5 - j)))
);
}
value = 0;
count = 0;
}
break;
}
}
if (codeIndex === codewords[0] && nextCode < DecodedBitStreamParser$2.TEXT_COMPACTION_MODE_LATCH) {
byteCompactedCodewords[count++] = nextCode;
}
for (let i = 0; i < count; i++) {
decodedBytes.write(
/*(byte)*/
byteCompactedCodewords[i]
);
}
break;
case DecodedBitStreamParser$2.BYTE_COMPACTION_MODE_LATCH_6:
while (codeIndex < codewords[0] && !end) {
let code = codewords[codeIndex++];
if (code < DecodedBitStreamParser$2.TEXT_COMPACTION_MODE_LATCH) {
count++;
value = 900 * value + code;
} else {
switch (code) {
case DecodedBitStreamParser$2.TEXT_COMPACTION_MODE_LATCH:
case DecodedBitStreamParser$2.BYTE_COMPACTION_MODE_LATCH:
case DecodedBitStreamParser$2.NUMERIC_COMPACTION_MODE_LATCH:
case DecodedBitStreamParser$2.BYTE_COMPACTION_MODE_LATCH_6:
case DecodedBitStreamParser$2.BEGIN_MACRO_PDF417_CONTROL_BLOCK:
case DecodedBitStreamParser$2.BEGIN_MACRO_PDF417_OPTIONAL_FIELD:
case DecodedBitStreamParser$2.MACRO_PDF417_TERMINATOR:
codeIndex--;
end = true;
break;
}
}
if (count % 5 === 0 && count > 0) {
for (let j = 0; j < 6; ++j) {
decodedBytes.write(
/*(byte)*/
Number(createBigInt(value) >> createBigInt(8 * (5 - j)))
);
}
value = 0;
count = 0;
}
}
break;
}
result.append(StringEncoding.decode(decodedBytes.toByteArray(), encoding));
return codeIndex;
}
/**
* Numeric Compaction mode (see 5.4.4) permits efficient encoding of numeric data strings.
*
* @param codewords The array of codewords (data + error)
* @param codeIndex The current index into the codeword array.
* @param result The decoded data is appended to the result.
* @return The next index into the codeword array.
*
* @throws FormatException
*/
static numericCompaction(codewords, codeIndex, result) {
let count = 0;
let end = false;
let numericCodewords = new Int32Array(DecodedBitStreamParser$2.MAX_NUMERIC_CODEWORDS);
while (codeIndex < codewords[0] && !end) {
let code = codewords[codeIndex++];
if (codeIndex === codewords[0]) {
end = true;
}
if (code < DecodedBitStreamParser$2.TEXT_COMPACTION_MODE_LATCH) {
numericCodewords[count] = code;
count++;
} else {
switch (code) {
case DecodedBitStreamParser$2.TEXT_COMPACTION_MODE_LATCH:
case DecodedBitStreamParser$2.BYTE_COMPACTION_MODE_LATCH:
case DecodedBitStreamParser$2.BYTE_COMPACTION_MODE_LATCH_6:
case DecodedBitStreamParser$2.BEGIN_MACRO_PDF417_CONTROL_BLOCK:
case DecodedBitStreamParser$2.BEGIN_MACRO_PDF417_OPTIONAL_FIELD:
case DecodedBitStreamParser$2.MACRO_PDF417_TERMINATOR:
codeIndex--;
end = true;
break;
}
}
if ((count % DecodedBitStreamParser$2.MAX_NUMERIC_CODEWORDS === 0 || code === DecodedBitStreamParser$2.NUMERIC_COMPACTION_MODE_LATCH || end) && count > 0) {
result.append(DecodedBitStreamParser$2.decodeBase900toBase10(numericCodewords, count));
count = 0;
}
}
return codeIndex;
}
/**
* Convert a list of Numeric Compacted codewords from Base 900 to Base 10.
*
* @param codewords The array of codewords
* @param count The number of codewords
* @return The decoded string representing the Numeric data.
*
* EXAMPLE
* Encode the fifteen digit numeric string 000213298174000
* Prefix the numeric string with a 1 and set the initial value of
* t = 1 000 213 298 174 000
* Calculate codeword 0
* d0 = 1 000 213 298 174 000 mod 900 = 200
*
* t = 1 000 213 298 174 000 div 900 = 1 111 348 109 082
* Calculate codeword 1
* d1 = 1 111 348 109 082 mod 900 = 282
*
* t = 1 111 348 109 082 div 900 = 1 234 831 232
* Calculate codeword 2
* d2 = 1 234 831 232 mod 900 = 632
*
* t = 1 234 831 232 div 900 = 1 372 034
* Calculate codeword 3
* d3 = 1 372 034 mod 900 = 434
*
* t = 1 372 034 div 900 = 1 524
* Calculate codeword 4
* d4 = 1 524 mod 900 = 624
*
* t = 1 524 div 900 = 1
* Calculate codeword 5
* d5 = 1 mod 900 = 1
* t = 1 div 900 = 0
* Codeword sequence is: 1, 624, 434, 632, 282, 200
*
* Decode the above codewords involves
* 1 x 900 power of 5 + 624 x 900 power of 4 + 434 x 900 power of 3 +
* 632 x 900 power of 2 + 282 x 900 power of 1 + 200 x 900 power of 0 = 1000213298174000
*
* Remove leading 1 => Result is 000213298174000
*
* @throws FormatException
*/
static decodeBase900toBase10(codewords, count) {
let result = createBigInt(0);
for (let i = 0; i < count; i++) {
result += DecodedBitStreamParser$2.EXP900[count - i - 1] * createBigInt(codewords[i]);
}
let resultString = result.toString();
if (resultString.charAt(0) !== "1") {
throw new FormatException();
}
return resultString.substring(1);
}
}
DecodedBitStreamParser$2.TEXT_COMPACTION_MODE_LATCH = 900;
DecodedBitStreamParser$2.BYTE_COMPACTION_MODE_LATCH = 901;
DecodedBitStreamParser$2.NUMERIC_COMPACTION_MODE_LATCH = 902;
DecodedBitStreamParser$2.BYTE_COMPACTION_MODE_LATCH_6 = 924;
DecodedBitStreamParser$2.ECI_USER_DEFINED = 925;
DecodedBitStreamParser$2.ECI_GENERAL_PURPOSE = 926;
DecodedBitStreamParser$2.ECI_CHARSET = 927;
DecodedBitStreamParser$2.BEGIN_MACRO_PDF417_CONTROL_BLOCK = 928;
DecodedBitStreamParser$2.BEGIN_MACRO_PDF417_OPTIONAL_FIELD = 923;
DecodedBitStreamParser$2.MACRO_PDF417_TERMINATOR = 922;
DecodedBitStreamParser$2.MODE_SHIFT_TO_BYTE_COMPACTION_MODE = 913;
DecodedBitStreamParser$2.MAX_NUMERIC_CODEWORDS = 15;
DecodedBitStreamParser$2.MACRO_PDF417_OPTIONAL_FIELD_FILE_NAME = 0;
DecodedBitStreamParser$2.MACRO_PDF417_OPTIONAL_FIELD_SEGMENT_COUNT = 1;
DecodedBitStreamParser$2.MACRO_PDF417_OPTIONAL_FIELD_TIME_STAMP = 2;
DecodedBitStreamParser$2.MACRO_PDF417_OPTIONAL_FIELD_SENDER = 3;
DecodedBitStreamParser$2.MACRO_PDF417_OPTIONAL_FIELD_ADDRESSEE = 4;
DecodedBitStreamParser$2.MACRO_PDF417_OPTIONAL_FIELD_FILE_SIZE = 5;
DecodedBitStreamParser$2.MACRO_PDF417_OPTIONAL_FIELD_CHECKSUM = 6;
DecodedBitStreamParser$2.PL = 25;
DecodedBitStreamParser$2.LL = 27;
DecodedBitStreamParser$2.AS = 27;
DecodedBitStreamParser$2.ML = 28;
DecodedBitStreamParser$2.AL = 28;
DecodedBitStreamParser$2.PS = 29;
DecodedBitStreamParser$2.PAL = 29;
DecodedBitStreamParser$2.PUNCT_CHARS = ";<>@[\\]_`~!\r ,:\n-.$/\"|*()?{}'";
DecodedBitStreamParser$2.MIXED_CHARS = "0123456789&\r ,:#-.$/+%*=^";
DecodedBitStreamParser$2.EXP900 = getBigIntConstructor() ? getEXP900() : [];
DecodedBitStreamParser$2.NUMBER_OF_SEQUENCE_CODEWORDS = 2;
class PDF417ScanningDecoder {
constructor() {
}
/**
* @TODO don't pass in minCodewordWidth and maxCodewordWidth, pass in barcode columns for start and stop pattern
*
* columns. That way width can be deducted from the pattern column.
* This approach also allows to detect more details about the barcode, e.g. if a bar type (white or black) is wider
* than it should be. This can happen if the scanner used a bad blackpoint.
*
* @param BitMatrix
* @param image
* @param ResultPoint
* @param imageTopLeft
* @param ResultPoint
* @param imageBottomLeft
* @param ResultPoint
* @param imageTopRight
* @param ResultPoint
* @param imageBottomRight
* @param int
* @param minCodewordWidth
* @param int
* @param maxCodewordWidth
*
* @throws NotFoundException
* @throws FormatException
* @throws ChecksumException
*/
static decode(image, imageTopLeft, imageBottomLeft, imageTopRight, imageBottomRight, minCodewordWidth, maxCodewordWidth) {
let boundingBox = new BoundingBox(image, imageTopLeft, imageBottomLeft, imageTopRight, imageBottomRight);
let leftRowIndicatorColumn = null;
let rightRowIndicatorColumn = null;
let detectionResult;
for (let firstPass = true; ; firstPass = false) {
if (imageTopLeft != null) {
leftRowIndicatorColumn = PDF417ScanningDecoder.getRowIndicatorColumn(image, boundingBox, imageTopLeft, true, minCodewordWidth, maxCodewordWidth);
}
if (imageTopRight != null) {
rightRowIndicatorColumn = PDF417ScanningDecoder.getRowIndicatorColumn(image, boundingBox, imageTopRight, false, minCodewordWidth, maxCodewordWidth);
}
detectionResult = PDF417ScanningDecoder.merge(leftRowIndicatorColumn, rightRowIndicatorColumn);
if (detectionResult == null) {
throw NotFoundException.getNotFoundInstance();
}
let resultBox = detectionResult.getBoundingBox();
if (firstPass && resultBox != null && (resultBox.getMinY() < boundingBox.getMinY() || resultBox.getMaxY() > boundingBox.getMaxY())) {
boundingBox = resultBox;
} else {
break;
}
}
detectionResult.setBoundingBox(boundingBox);
let maxBarcodeColumn = detectionResult.getBarcodeColumnCount() + 1;
detectionResult.setDetectionResultColumn(0, leftRowIndicatorColumn);
detectionResult.setDetectionResultColumn(maxBarcodeColumn, rightRowIndicatorColumn);
let leftToRight = leftRowIndicatorColumn != null;
for (let barcodeColumnCount = 1; barcodeColumnCount <= maxBarcodeColumn; barcodeColumnCount++) {
let barcodeColumn = leftToRight ? barcodeColumnCount : maxBarcodeColumn - barcodeColumnCount;
if (detectionResult.getDetectionResultColumn(barcodeColumn) !== /* null */
void 0) {
continue;
}
let detectionResultColumn;
if (barcodeColumn === 0 || barcodeColumn === maxBarcodeColumn) {
detectionResultColumn = new DetectionResultRowIndicatorColumn(boundingBox, barcodeColumn === 0);
} else {
detectionResultColumn = new DetectionResultColumn(boundingBox);
}
detectionResult.setDetectionResultColumn(barcodeColumn, detectionResultColumn);
let startColumn = -1;
let previousStartColumn = startColumn;
for (let imageRow = boundingBox.getMinY(); imageRow <= boundingBox.getMaxY(); imageRow++) {
startColumn = PDF417ScanningDecoder.getStartColumn(detectionResult, barcodeColumn, imageRow, leftToRight);
if (startColumn < 0 || startColumn > boundingBox.getMaxX()) {
if (previousStartColumn === -1) {
continue;
}
startColumn = previousStartColumn;
}
let codeword = PDF417ScanningDecoder.detectCodeword(image, boundingBox.getMinX(), boundingBox.getMaxX(), leftToRight, startColumn, imageRow, minCodewordWidth, maxCodewordWidth);
if (codeword != null) {
detectionResultColumn.setCodeword(imageRow, codeword);
previousStartColumn = startColumn;
minCodewordWidth = Math.min(minCodewordWidth, codeword.getWidth());
maxCodewordWidth = Math.max(maxCodewordWidth, codeword.getWidth());
}
}
}
return PDF417ScanningDecoder.createDecoderResult(detectionResult);
}
/**
*
* @param leftRowIndicatorColumn
* @param rightRowIndicatorColumn
*
* @throws NotFoundException
*/
static merge(leftRowIndicatorColumn, rightRowIndicatorColumn) {
if (leftRowIndicatorColumn == null && rightRowIndicatorColumn == null) {
return null;
}
let barcodeMetadata = PDF417ScanningDecoder.getBarcodeMetadata(leftRowIndicatorColumn, rightRowIndicatorColumn);
if (barcodeMetadata == null) {
return null;
}
let boundingBox = BoundingBox.merge(PDF417ScanningDecoder.adjustBoundingBox(leftRowIndicatorColumn), PDF417ScanningDecoder.adjustBoundingBox(rightRowIndicatorColumn));
return new DetectionResult(barcodeMetadata, boundingBox);
}
/**
*
* @param rowIndicatorColumn
*
* @throws NotFoundException
*/
static adjustBoundingBox(rowIndicatorColumn) {
if (rowIndicatorColumn == null) {
return null;
}
let rowHeights = rowIndicatorColumn.getRowHeights();
if (rowHeights == null) {
return null;
}
let maxRowHeight = PDF417ScanningDecoder.getMax(rowHeights);
let missingStartRows = 0;
for (let rowHeight of rowHeights) {
missingStartRows += maxRowHeight - rowHeight;
if (rowHeight > 0) {
break;
}
}
let codewords = rowIndicatorColumn.getCodewords();
for (let row = 0; missingStartRows > 0 && codewords[row] == null; row++) {
missingStartRows--;
}
let missingEndRows = 0;
for (let row = rowHeights.length - 1; row >= 0; row--) {
missingEndRows += maxRowHeight - rowHeights[row];
if (rowHeights[row] > 0) {
break;
}
}
for (let row = codewords.length - 1; missingEndRows > 0 && codewords[row] == null; row--) {
missingEndRows--;
}
return rowIndicatorColumn.getBoundingBox().addMissingRows(missingStartRows, missingEndRows, rowIndicatorColumn.isLeft());
}
static getMax(values) {
let maxValue = -1;
for (let value of values) {
maxValue = Math.max(maxValue, value);
}
return maxValue;
}
static getBarcodeMetadata(leftRowIndicatorColumn, rightRowIndicatorColumn) {
let leftBarcodeMetadata;
if (leftRowIndicatorColumn == null || (leftBarcodeMetadata = leftRowIndicatorColumn.getBarcodeMetadata()) == null) {
return rightRowIndicatorColumn == null ? null : rightRowIndicatorColumn.getBarcodeMetadata();
}
let rightBarcodeMetadata;
if (rightRowIndicatorColumn == null || (rightBarcodeMetadata = rightRowIndicatorColumn.getBarcodeMetadata()) == null) {
return leftBarcodeMetadata;
}
if (leftBarcodeMetadata.getColumnCount() !== rightBarcodeMetadata.getColumnCount() && leftBarcodeMetadata.getErrorCorrectionLevel() !== rightBarcodeMetadata.getErrorCorrectionLevel() && leftBarcodeMetadata.getRowCount() !== rightBarcodeMetadata.getRowCount()) {
return null;
}
return leftBarcodeMetadata;
}
static getRowIndicatorColumn(image, boundingBox, startPoint, leftToRight, minCodewordWidth, maxCodewordWidth) {
let rowIndicatorColumn = new DetectionResultRowIndicatorColumn(boundingBox, leftToRight);
for (let i = 0; i < 2; i++) {
let increment = i === 0 ? 1 : -1;
let startColumn = Math.trunc(Math.trunc(startPoint.getX()));
for (let imageRow = Math.trunc(Math.trunc(startPoint.getY())); imageRow <= boundingBox.getMaxY() && imageRow >= boundingBox.getMinY(); imageRow += increment) {
let codeword = PDF417ScanningDecoder.detectCodeword(image, 0, image.getWidth(), leftToRight, startColumn, imageRow, minCodewordWidth, maxCodewordWidth);
if (codeword != null) {
rowIndicatorColumn.setCodeword(imageRow, codeword);
if (leftToRight) {
startColumn = codeword.getStartX();
} else {
startColumn = codeword.getEndX();
}
}
}
}
return rowIndicatorColumn;
}
/**
*
* @param detectionResult
* @param BarcodeValue
* @param param2
* @param param3
* @param barcodeMatrix
*
* @throws NotFoundException
*/
static adjustCodewordCount(detectionResult, barcodeMatrix) {
let barcodeMatrix01 = barcodeMatrix[0][1];
let numberOfCodewords = barcodeMatrix01.getValue();
let calculatedNumberOfCodewords = detectionResult.getBarcodeColumnCount() * detectionResult.getBarcodeRowCount() - PDF417ScanningDecoder.getNumberOfECCodeWords(detectionResult.getBarcodeECLevel());
if (numberOfCodewords.length === 0) {
if (calculatedNumberOfCodewords < 1 || calculatedNumberOfCodewords > PDF417Common.MAX_CODEWORDS_IN_BARCODE) {
throw NotFoundException.getNotFoundInstance();
}
barcodeMatrix01.setValue(calculatedNumberOfCodewords);
} else if (numberOfCodewords[0] !== calculatedNumberOfCodewords) {
barcodeMatrix01.setValue(calculatedNumberOfCodewords);
}
}
/**
*
* @param detectionResult
*
* @throws FormatException
* @throws ChecksumException
* @throws NotFoundException
*/
static createDecoderResult(detectionResult) {
let barcodeMatrix = PDF417ScanningDecoder.createBarcodeMatrix(detectionResult);
PDF417ScanningDecoder.adjustCodewordCount(detectionResult, barcodeMatrix);
let erasures = new Array();
let codewords = new Int32Array(detectionResult.getBarcodeRowCount() * detectionResult.getBarcodeColumnCount());
let ambiguousIndexValuesList = (
/*List Given data and error-correction codewords received, possibly corrupted by errors, attempts to
* correct the errors in-place. Encode a sequence of code words (symbols) using Reed-Solomon to allow decoders
* to detect and correct errors that may have been introduced when the resulting
* data is stored or transmitted.}
* @return String decoded from the buffer's contents.
* @exception UnsupportedEncodingException
* If the named charset is not supported
* @since JDK1.1
*/
toString_string(charsetName) {
return new String(
this.buf
/*, 0, this.count, charsetName*/
).toString();
}
/**
* Creates a newly allocated string. Its size is the current size of
* the output stream and the valid contents of the buffer have been
* copied into it. Each character c in the resulting string is
* constructed from the corresponding element b in the byte
* array such that:
*
*
* @deprecated This method does not properly convert bytes into characters.
* As of JDK 1.1, the preferred way to do this is via the
*
* c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
* toString(String enc) method, which takes an encoding-name
* argument, or the toString() method, which uses the
* platform's default character encoding.
*
* @param hibyte the high byte of each resulting Unicode character.
* @return the current contents of the output stream, as a string.
* @see java.io.ByteArrayOutputStream#size()
* @see java.io.ByteArrayOutputStream#toString(String)
* @see java.io.ByteArrayOutputStream#toString()
*/
// @Deprecated
toString_number(hibyte) {
return new String(
this.buf
/*, hibyte, 0, this.count*/
).toString();
}
/**
* Closing a ByteArrayOutputStream has no effect. The methods in
* this class can be called after the stream has been closed without
* generating an IOException.
*
";
qrCodeScanRegion.appendChild(this.cameraScanImage);
return;
}
this.cameraScanImage = new Image();
this.cameraScanImage.onload = function(_) {
qrCodeScanRegion.innerHTML = "
";
qrCodeScanRegion.appendChild($this.cameraScanImage);
};
this.cameraScanImage.width = 64;
this.cameraScanImage.style.opacity = "0.8";
this.cameraScanImage.src = ASSET_CAMERA_SCAN;
this.cameraScanImage.alt = Html5QrcodeScannerStrings.cameraScanAltText();
};
Html5QrcodeScanner2.prototype.insertFileScanImageToScanRegion = function() {
var $this = this;
var qrCodeScanRegion = document.getElementById(this.getScanRegionId());
if (this.fileScanImage) {
qrCodeScanRegion.innerHTML = "
";
qrCodeScanRegion.appendChild(this.fileScanImage);
return;
}
this.fileScanImage = new Image();
this.fileScanImage.onload = function(_) {
qrCodeScanRegion.innerHTML = "
";
qrCodeScanRegion.appendChild($this.fileScanImage);
};
this.fileScanImage.width = 64;
this.fileScanImage.style.opacity = "0.8";
this.fileScanImage.src = ASSET_FILE_SCAN;
this.fileScanImage.alt = Html5QrcodeScannerStrings.fileScanAltText();
};
Html5QrcodeScanner2.prototype.clearScanRegion = function() {
var qrCodeScanRegion = document.getElementById(this.getScanRegionId());
qrCodeScanRegion.innerHTML = "";
};
Html5QrcodeScanner2.prototype.getDashboardSectionId = function() {
return "".concat(this.elementId, "__dashboard_section");
};
Html5QrcodeScanner2.prototype.getDashboardSectionCameraScanRegionId = function() {
return "".concat(this.elementId, "__dashboard_section_csr");
};
Html5QrcodeScanner2.prototype.getDashboardSectionSwapLinkId = function() {
return PublicUiElementIdAndClasses.SCAN_TYPE_CHANGE_ANCHOR_ID;
};
Html5QrcodeScanner2.prototype.getScanRegionId = function() {
return "".concat(this.elementId, "__scan_region");
};
Html5QrcodeScanner2.prototype.getDashboardId = function() {
return "".concat(this.elementId, "__dashboard");
};
Html5QrcodeScanner2.prototype.getHeaderMessageContainerId = function() {
return "".concat(this.elementId, "__header_message");
};
Html5QrcodeScanner2.prototype.getCameraPermissionButtonId = function() {
return PublicUiElementIdAndClasses.CAMERA_PERMISSION_BUTTON_ID;
};
Html5QrcodeScanner2.prototype.getCameraScanRegion = function() {
return document.getElementById(this.getDashboardSectionCameraScanRegionId());
};
Html5QrcodeScanner2.prototype.getDashboardSectionSwapLink = function() {
return document.getElementById(this.getDashboardSectionSwapLinkId());
};
Html5QrcodeScanner2.prototype.getHeaderMessageDiv = function() {
return document.getElementById(this.getHeaderMessageContainerId());
};
return Html5QrcodeScanner2;
})();
// src/crypto/cose-qr.js
var cbor = __toESM(require_cbor());
// node_modules/pako/dist/pako.esm.mjs
var Z_FIXED$1 = 4;
var Z_BINARY = 0;
var Z_TEXT = 1;
var Z_UNKNOWN$1 = 2;
function zero$1(buf) {
let len = buf.length;
while (--len >= 0) {
buf[len] = 0;
}
}
var STORED_BLOCK = 0;
var STATIC_TREES = 1;
var DYN_TREES = 2;
var MIN_MATCH$1 = 3;
var MAX_MATCH$1 = 258;
var LENGTH_CODES$1 = 29;
var LITERALS$1 = 256;
var L_CODES$1 = LITERALS$1 + 1 + LENGTH_CODES$1;
var D_CODES$1 = 30;
var BL_CODES$1 = 19;
var HEAP_SIZE$1 = 2 * L_CODES$1 + 1;
var MAX_BITS$1 = 15;
var Buf_size = 16;
var MAX_BL_BITS = 7;
var END_BLOCK = 256;
var REP_3_6 = 16;
var REPZ_3_10 = 17;
var REPZ_11_138 = 18;
var extra_lbits = (
/* extra bits for each length code */
new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0])
);
var extra_dbits = (
/* extra bits for each distance code */
new Uint8Array([0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13])
);
var extra_blbits = (
/* extra bits for each bit length code */
new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7])
);
var bl_order = new Uint8Array([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
var DIST_CODE_LEN = 512;
var static_ltree = new Array((L_CODES$1 + 2) * 2);
zero$1(static_ltree);
var static_dtree = new Array(D_CODES$1 * 2);
zero$1(static_dtree);
var _dist_code = new Array(DIST_CODE_LEN);
zero$1(_dist_code);
var _length_code = new Array(MAX_MATCH$1 - MIN_MATCH$1 + 1);
zero$1(_length_code);
var base_length = new Array(LENGTH_CODES$1);
zero$1(base_length);
var base_dist = new Array(D_CODES$1);
zero$1(base_dist);
function StaticTreeDesc(static_tree, extra_bits, extra_base, elems, max_length) {
this.static_tree = static_tree;
this.extra_bits = extra_bits;
this.extra_base = extra_base;
this.elems = elems;
this.max_length = max_length;
this.has_stree = static_tree && static_tree.length;
}
var static_l_desc;
var static_d_desc;
var static_bl_desc;
function TreeDesc(dyn_tree, stat_desc) {
this.dyn_tree = dyn_tree;
this.max_code = 0;
this.stat_desc = stat_desc;
}
var d_code = (dist) => {
return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)];
};
var put_short = (s, w) => {
s.pending_buf[s.pending++] = w & 255;
s.pending_buf[s.pending++] = w >>> 8 & 255;
};
var send_bits = (s, value, length) => {
if (s.bi_valid > Buf_size - length) {
s.bi_buf |= value << s.bi_valid & 65535;
put_short(s, s.bi_buf);
s.bi_buf = value >> Buf_size - s.bi_valid;
s.bi_valid += length - Buf_size;
} else {
s.bi_buf |= value << s.bi_valid & 65535;
s.bi_valid += length;
}
};
var send_code = (s, c, tree) => {
send_bits(
s,
tree[c * 2],
tree[c * 2 + 1]
/*.Len*/
);
};
var bi_reverse = (code, len) => {
let res = 0;
do {
res |= code & 1;
code >>>= 1;
res <<= 1;
} while (--len > 0);
return res >>> 1;
};
var bi_flush = (s) => {
if (s.bi_valid === 16) {
put_short(s, s.bi_buf);
s.bi_buf = 0;
s.bi_valid = 0;
} else if (s.bi_valid >= 8) {
s.pending_buf[s.pending++] = s.bi_buf & 255;
s.bi_buf >>= 8;
s.bi_valid -= 8;
}
};
var gen_bitlen = (s, desc) => {
const tree = desc.dyn_tree;
const max_code = desc.max_code;
const stree = desc.stat_desc.static_tree;
const has_stree = desc.stat_desc.has_stree;
const extra = desc.stat_desc.extra_bits;
const base = desc.stat_desc.extra_base;
const max_length = desc.stat_desc.max_length;
let h;
let n, m;
let bits;
let xbits;
let f;
let overflow = 0;
for (bits = 0; bits <= MAX_BITS$1; bits++) {
s.bl_count[bits] = 0;
}
tree[s.heap[s.heap_max] * 2 + 1] = 0;
for (h = s.heap_max + 1; h < HEAP_SIZE$1; h++) {
n = s.heap[h];
bits = tree[tree[n * 2 + 1] * 2 + 1] + 1;
if (bits > max_length) {
bits = max_length;
overflow++;
}
tree[n * 2 + 1] = bits;
if (n > max_code) {
continue;
}
s.bl_count[bits]++;
xbits = 0;
if (n >= base) {
xbits = extra[n - base];
}
f = tree[n * 2];
s.opt_len += f * (bits + xbits);
if (has_stree) {
s.static_len += f * (stree[n * 2 + 1] + xbits);
}
}
if (overflow === 0) {
return;
}
do {
bits = max_length - 1;
while (s.bl_count[bits] === 0) {
bits--;
}
s.bl_count[bits]--;
s.bl_count[bits + 1] += 2;
s.bl_count[max_length]--;
overflow -= 2;
} while (overflow > 0);
for (bits = max_length; bits !== 0; bits--) {
n = s.bl_count[bits];
while (n !== 0) {
m = s.heap[--h];
if (m > max_code) {
continue;
}
if (tree[m * 2 + 1] !== bits) {
s.opt_len += (bits - tree[m * 2 + 1]) * tree[m * 2];
tree[m * 2 + 1] = bits;
}
n--;
}
}
};
var gen_codes = (tree, max_code, bl_count) => {
const next_code = new Array(MAX_BITS$1 + 1);
let code = 0;
let bits;
let n;
for (bits = 1; bits <= MAX_BITS$1; bits++) {
code = code + bl_count[bits - 1] << 1;
next_code[bits] = code;
}
for (n = 0; n <= max_code; n++) {
let len = tree[n * 2 + 1];
if (len === 0) {
continue;
}
tree[n * 2] = bi_reverse(next_code[len]++, len);
}
};
var tr_static_init = () => {
let n;
let bits;
let length;
let code;
let dist;
const bl_count = new Array(MAX_BITS$1 + 1);
length = 0;
for (code = 0; code < LENGTH_CODES$1 - 1; code++) {
base_length[code] = length;
for (n = 0; n < 1 << extra_lbits[code]; n++) {
_length_code[length++] = code;
}
}
_length_code[length - 1] = code;
dist = 0;
for (code = 0; code < 16; code++) {
base_dist[code] = dist;
for (n = 0; n < 1 << extra_dbits[code]; n++) {
_dist_code[dist++] = code;
}
}
dist >>= 7;
for (; code < D_CODES$1; code++) {
base_dist[code] = dist << 7;
for (n = 0; n < 1 << extra_dbits[code] - 7; n++) {
_dist_code[256 + dist++] = code;
}
}
for (bits = 0; bits <= MAX_BITS$1; bits++) {
bl_count[bits] = 0;
}
n = 0;
while (n <= 143) {
static_ltree[n * 2 + 1] = 8;
n++;
bl_count[8]++;
}
while (n <= 255) {
static_ltree[n * 2 + 1] = 9;
n++;
bl_count[9]++;
}
while (n <= 279) {
static_ltree[n * 2 + 1] = 7;
n++;
bl_count[7]++;
}
while (n <= 287) {
static_ltree[n * 2 + 1] = 8;
n++;
bl_count[8]++;
}
gen_codes(static_ltree, L_CODES$1 + 1, bl_count);
for (n = 0; n < D_CODES$1; n++) {
static_dtree[n * 2 + 1] = 5;
static_dtree[n * 2] = bi_reverse(n, 5);
}
static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS$1 + 1, L_CODES$1, MAX_BITS$1);
static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES$1, MAX_BITS$1);
static_bl_desc = new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES$1, MAX_BL_BITS);
};
var init_block = (s) => {
let n;
for (n = 0; n < L_CODES$1; n++) {
s.dyn_ltree[n * 2] = 0;
}
for (n = 0; n < D_CODES$1; n++) {
s.dyn_dtree[n * 2] = 0;
}
for (n = 0; n < BL_CODES$1; n++) {
s.bl_tree[n * 2] = 0;
}
s.dyn_ltree[END_BLOCK * 2] = 1;
s.opt_len = s.static_len = 0;
s.sym_next = s.matches = 0;
};
var bi_windup = (s) => {
if (s.bi_valid > 8) {
put_short(s, s.bi_buf);
} else if (s.bi_valid > 0) {
s.pending_buf[s.pending++] = s.bi_buf;
}
s.bi_buf = 0;
s.bi_valid = 0;
};
var smaller = (tree, n, m, depth) => {
const _n2 = n * 2;
const _m2 = m * 2;
return tree[_n2] < tree[_m2] || tree[_n2] === tree[_m2] && depth[n] <= depth[m];
};
var pqdownheap = (s, tree, k) => {
const v = s.heap[k];
let j = k << 1;
while (j <= s.heap_len) {
if (j < s.heap_len && smaller(tree, s.heap[j + 1], s.heap[j], s.depth)) {
j++;
}
if (smaller(tree, v, s.heap[j], s.depth)) {
break;
}
s.heap[k] = s.heap[j];
k = j;
j <<= 1;
}
s.heap[k] = v;
};
var compress_block = (s, ltree, dtree) => {
let dist;
let lc;
let sx = 0;
let code;
let extra;
if (s.sym_next !== 0) {
do {
dist = s.pending_buf[s.sym_buf + sx++] & 255;
dist += (s.pending_buf[s.sym_buf + sx++] & 255) << 8;
lc = s.pending_buf[s.sym_buf + sx++];
if (dist === 0) {
send_code(s, lc, ltree);
} else {
code = _length_code[lc];
send_code(s, code + LITERALS$1 + 1, ltree);
extra = extra_lbits[code];
if (extra !== 0) {
lc -= base_length[code];
send_bits(s, lc, extra);
}
dist--;
code = d_code(dist);
send_code(s, code, dtree);
extra = extra_dbits[code];
if (extra !== 0) {
dist -= base_dist[code];
send_bits(s, dist, extra);
}
}
} while (sx < s.sym_next);
}
send_code(s, END_BLOCK, ltree);
};
var build_tree = (s, desc) => {
const tree = desc.dyn_tree;
const stree = desc.stat_desc.static_tree;
const has_stree = desc.stat_desc.has_stree;
const elems = desc.stat_desc.elems;
let n, m;
let max_code = -1;
let node;
s.heap_len = 0;
s.heap_max = HEAP_SIZE$1;
for (n = 0; n < elems; n++) {
if (tree[n * 2] !== 0) {
s.heap[++s.heap_len] = max_code = n;
s.depth[n] = 0;
} else {
tree[n * 2 + 1] = 0;
}
}
while (s.heap_len < 2) {
node = s.heap[++s.heap_len] = max_code < 2 ? ++max_code : 0;
tree[node * 2] = 1;
s.depth[node] = 0;
s.opt_len--;
if (has_stree) {
s.static_len -= stree[node * 2 + 1];
}
}
desc.max_code = max_code;
for (n = s.heap_len >> 1; n >= 1; n--) {
pqdownheap(s, tree, n);
}
node = elems;
do {
n = s.heap[
1
/*SMALLEST*/
];
s.heap[
1
/*SMALLEST*/
] = s.heap[s.heap_len--];
pqdownheap(
s,
tree,
1
/*SMALLEST*/
);
m = s.heap[
1
/*SMALLEST*/
];
s.heap[--s.heap_max] = n;
s.heap[--s.heap_max] = m;
tree[node * 2] = tree[n * 2] + tree[m * 2];
s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1;
tree[n * 2 + 1] = tree[m * 2 + 1] = node;
s.heap[
1
/*SMALLEST*/
] = node++;
pqdownheap(
s,
tree,
1
/*SMALLEST*/
);
} while (s.heap_len >= 2);
s.heap[--s.heap_max] = s.heap[
1
/*SMALLEST*/
];
gen_bitlen(s, desc);
gen_codes(tree, max_code, s.bl_count);
};
var scan_tree = (s, tree, max_code) => {
let n;
let prevlen = -1;
let curlen;
let nextlen = tree[0 * 2 + 1];
let count = 0;
let max_count = 7;
let min_count = 4;
if (nextlen === 0) {
max_count = 138;
min_count = 3;
}
tree[(max_code + 1) * 2 + 1] = 65535;
for (n = 0; n <= max_code; n++) {
curlen = nextlen;
nextlen = tree[(n + 1) * 2 + 1];
if (++count < max_count && curlen === nextlen) {
continue;
} else if (count < min_count) {
s.bl_tree[curlen * 2] += count;
} else if (curlen !== 0) {
if (curlen !== prevlen) {
s.bl_tree[curlen * 2]++;
}
s.bl_tree[REP_3_6 * 2]++;
} else if (count <= 10) {
s.bl_tree[REPZ_3_10 * 2]++;
} else {
s.bl_tree[REPZ_11_138 * 2]++;
}
count = 0;
prevlen = curlen;
if (nextlen === 0) {
max_count = 138;
min_count = 3;
} else if (curlen === nextlen) {
max_count = 6;
min_count = 3;
} else {
max_count = 7;
min_count = 4;
}
}
};
var send_tree = (s, tree, max_code) => {
let n;
let prevlen = -1;
let curlen;
let nextlen = tree[0 * 2 + 1];
let count = 0;
let max_count = 7;
let min_count = 4;
if (nextlen === 0) {
max_count = 138;
min_count = 3;
}
for (n = 0; n <= max_code; n++) {
curlen = nextlen;
nextlen = tree[(n + 1) * 2 + 1];
if (++count < max_count && curlen === nextlen) {
continue;
} else if (count < min_count) {
do {
send_code(s, curlen, s.bl_tree);
} while (--count !== 0);
} else if (curlen !== 0) {
if (curlen !== prevlen) {
send_code(s, curlen, s.bl_tree);
count--;
}
send_code(s, REP_3_6, s.bl_tree);
send_bits(s, count - 3, 2);
} else if (count <= 10) {
send_code(s, REPZ_3_10, s.bl_tree);
send_bits(s, count - 3, 3);
} else {
send_code(s, REPZ_11_138, s.bl_tree);
send_bits(s, count - 11, 7);
}
count = 0;
prevlen = curlen;
if (nextlen === 0) {
max_count = 138;
min_count = 3;
} else if (curlen === nextlen) {
max_count = 6;
min_count = 3;
} else {
max_count = 7;
min_count = 4;
}
}
};
var build_bl_tree = (s) => {
let max_blindex;
scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
build_tree(s, s.bl_desc);
for (max_blindex = BL_CODES$1 - 1; max_blindex >= 3; max_blindex--) {
if (s.bl_tree[bl_order[max_blindex] * 2 + 1] !== 0) {
break;
}
}
s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
return max_blindex;
};
var send_all_trees = (s, lcodes, dcodes, blcodes) => {
let rank2;
send_bits(s, lcodes - 257, 5);
send_bits(s, dcodes - 1, 5);
send_bits(s, blcodes - 4, 4);
for (rank2 = 0; rank2 < blcodes; rank2++) {
send_bits(s, s.bl_tree[bl_order[rank2] * 2 + 1], 3);
}
send_tree(s, s.dyn_ltree, lcodes - 1);
send_tree(s, s.dyn_dtree, dcodes - 1);
};
var detect_data_type = (s) => {
let block_mask = 4093624447;
let n;
for (n = 0; n <= 31; n++, block_mask >>>= 1) {
if (block_mask & 1 && s.dyn_ltree[n * 2] !== 0) {
return Z_BINARY;
}
}
if (s.dyn_ltree[9 * 2] !== 0 || s.dyn_ltree[10 * 2] !== 0 || s.dyn_ltree[13 * 2] !== 0) {
return Z_TEXT;
}
for (n = 32; n < LITERALS$1; n++) {
if (s.dyn_ltree[n * 2] !== 0) {
return Z_TEXT;
}
}
return Z_BINARY;
};
var static_init_done = false;
var _tr_init$1 = (s) => {
if (!static_init_done) {
tr_static_init();
static_init_done = true;
}
s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc);
s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc);
s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc);
s.bi_buf = 0;
s.bi_valid = 0;
init_block(s);
};
var _tr_stored_block$1 = (s, buf, stored_len, last) => {
send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3);
bi_windup(s);
put_short(s, stored_len);
put_short(s, ~stored_len);
if (stored_len) {
s.pending_buf.set(s.window.subarray(buf, buf + stored_len), s.pending);
}
s.pending += stored_len;
};
var _tr_align$1 = (s) => {
send_bits(s, STATIC_TREES << 1, 3);
send_code(s, END_BLOCK, static_ltree);
bi_flush(s);
};
var _tr_flush_block$1 = (s, buf, stored_len, last) => {
let opt_lenb, static_lenb;
let max_blindex = 0;
if (s.level > 0) {
if (s.strm.data_type === Z_UNKNOWN$1) {
s.strm.data_type = detect_data_type(s);
}
build_tree(s, s.l_desc);
build_tree(s, s.d_desc);
max_blindex = build_bl_tree(s);
opt_lenb = s.opt_len + 3 + 7 >>> 3;
static_lenb = s.static_len + 3 + 7 >>> 3;
if (static_lenb <= opt_lenb) {
opt_lenb = static_lenb;
}
} else {
opt_lenb = static_lenb = stored_len + 5;
}
if (stored_len + 4 <= opt_lenb && buf !== -1) {
_tr_stored_block$1(s, buf, stored_len, last);
} else if (s.strategy === Z_FIXED$1 || static_lenb === opt_lenb) {
send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3);
compress_block(s, static_ltree, static_dtree);
} else {
send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3);
send_all_trees(s, s.l_desc.max_code + 1, s.d_desc.max_code + 1, max_blindex + 1);
compress_block(s, s.dyn_ltree, s.dyn_dtree);
}
init_block(s);
if (last) {
bi_windup(s);
}
};
var _tr_tally$1 = (s, dist, lc) => {
s.pending_buf[s.sym_buf + s.sym_next++] = dist;
s.pending_buf[s.sym_buf + s.sym_next++] = dist >> 8;
s.pending_buf[s.sym_buf + s.sym_next++] = lc;
if (dist === 0) {
s.dyn_ltree[lc * 2]++;
} else {
s.matches++;
dist--;
s.dyn_ltree[(_length_code[lc] + LITERALS$1 + 1) * 2]++;
s.dyn_dtree[d_code(dist) * 2]++;
}
return s.sym_next === s.sym_end;
};
var _tr_init_1 = _tr_init$1;
var _tr_stored_block_1 = _tr_stored_block$1;
var _tr_flush_block_1 = _tr_flush_block$1;
var _tr_tally_1 = _tr_tally$1;
var _tr_align_1 = _tr_align$1;
var trees = {
_tr_init: _tr_init_1,
_tr_stored_block: _tr_stored_block_1,
_tr_flush_block: _tr_flush_block_1,
_tr_tally: _tr_tally_1,
_tr_align: _tr_align_1
};
var adler32 = (adler, buf, len, pos) => {
let s1 = adler & 65535 | 0, s2 = adler >>> 16 & 65535 | 0, n = 0;
while (len !== 0) {
n = len > 2e3 ? 2e3 : len;
len -= n;
do {
s1 = s1 + buf[pos++] | 0;
s2 = s2 + s1 | 0;
} while (--n);
s1 %= 65521;
s2 %= 65521;
}
return s1 | s2 << 16 | 0;
};
var adler32_1 = adler32;
var makeTable = () => {
let c, table = [];
for (var n = 0; n < 256; n++) {
c = n;
for (var k = 0; k < 8; k++) {
c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
}
table[n] = c;
}
return table;
};
var crcTable = new Uint32Array(makeTable());
var crc32 = (crc, buf, len, pos) => {
const t = crcTable;
const end = pos + len;
crc ^= -1;
for (let i = pos; i < end; i++) {
crc = crc >>> 8 ^ t[(crc ^ buf[i]) & 255];
}
return crc ^ -1;
};
var crc32_1 = crc32;
var messages = {
2: "need dictionary",
/* Z_NEED_DICT 2 */
1: "stream end",
/* Z_STREAM_END 1 */
0: "",
/* Z_OK 0 */
"-1": "file error",
/* Z_ERRNO (-1) */
"-2": "stream error",
/* Z_STREAM_ERROR (-2) */
"-3": "data error",
/* Z_DATA_ERROR (-3) */
"-4": "insufficient memory",
/* Z_MEM_ERROR (-4) */
"-5": "buffer error",
/* Z_BUF_ERROR (-5) */
"-6": "incompatible version"
/* Z_VERSION_ERROR (-6) */
};
var constants$2 = {
/* Allowed flush values; see deflate() and inflate() below for details */
Z_NO_FLUSH: 0,
Z_PARTIAL_FLUSH: 1,
Z_SYNC_FLUSH: 2,
Z_FULL_FLUSH: 3,
Z_FINISH: 4,
Z_BLOCK: 5,
Z_TREES: 6,
/* Return codes for the compression/decompression functions. Negative values
* are errors, positive values are used for special but normal events.
*/
Z_OK: 0,
Z_STREAM_END: 1,
Z_NEED_DICT: 2,
Z_ERRNO: -1,
Z_STREAM_ERROR: -2,
Z_DATA_ERROR: -3,
Z_MEM_ERROR: -4,
Z_BUF_ERROR: -5,
//Z_VERSION_ERROR: -6,
/* compression levels */
Z_NO_COMPRESSION: 0,
Z_BEST_SPEED: 1,
Z_BEST_COMPRESSION: 9,
Z_DEFAULT_COMPRESSION: -1,
Z_FILTERED: 1,
Z_HUFFMAN_ONLY: 2,
Z_RLE: 3,
Z_FIXED: 4,
Z_DEFAULT_STRATEGY: 0,
/* Possible values of the data_type field (though see inflate()) */
Z_BINARY: 0,
Z_TEXT: 1,
//Z_ASCII: 1, // = Z_TEXT (deprecated)
Z_UNKNOWN: 2,
/* The deflate compression method */
Z_DEFLATED: 8
//Z_NULL: null // Use -1 or null inline, depending on var type
};
var { _tr_init, _tr_stored_block, _tr_flush_block, _tr_tally, _tr_align } = trees;
var {
Z_NO_FLUSH: Z_NO_FLUSH$2,
Z_PARTIAL_FLUSH,
Z_FULL_FLUSH: Z_FULL_FLUSH$1,
Z_FINISH: Z_FINISH$3,
Z_BLOCK: Z_BLOCK$1,
Z_OK: Z_OK$3,
Z_STREAM_END: Z_STREAM_END$3,
Z_STREAM_ERROR: Z_STREAM_ERROR$2,
Z_DATA_ERROR: Z_DATA_ERROR$2,
Z_BUF_ERROR: Z_BUF_ERROR$1,
Z_DEFAULT_COMPRESSION: Z_DEFAULT_COMPRESSION$1,
Z_FILTERED,
Z_HUFFMAN_ONLY,
Z_RLE,
Z_FIXED,
Z_DEFAULT_STRATEGY: Z_DEFAULT_STRATEGY$1,
Z_UNKNOWN,
Z_DEFLATED: Z_DEFLATED$2
} = constants$2;
var MAX_MEM_LEVEL = 9;
var MAX_WBITS$1 = 15;
var DEF_MEM_LEVEL = 8;
var LENGTH_CODES = 29;
var LITERALS = 256;
var L_CODES = LITERALS + 1 + LENGTH_CODES;
var D_CODES = 30;
var BL_CODES = 19;
var HEAP_SIZE = 2 * L_CODES + 1;
var MAX_BITS = 15;
var MIN_MATCH = 3;
var MAX_MATCH = 258;
var MIN_LOOKAHEAD = MAX_MATCH + MIN_MATCH + 1;
var PRESET_DICT = 32;
var INIT_STATE = 42;
var GZIP_STATE = 57;
var EXTRA_STATE = 69;
var NAME_STATE = 73;
var COMMENT_STATE = 91;
var HCRC_STATE = 103;
var BUSY_STATE = 113;
var FINISH_STATE = 666;
var BS_NEED_MORE = 1;
var BS_BLOCK_DONE = 2;
var BS_FINISH_STARTED = 3;
var BS_FINISH_DONE = 4;
var OS_CODE = 3;
var err = (strm, errorCode) => {
strm.msg = messages[errorCode];
return errorCode;
};
var rank = (f) => {
return f * 2 - (f > 4 ? 9 : 0);
};
var zero = (buf) => {
let len = buf.length;
while (--len >= 0) {
buf[len] = 0;
}
};
var slide_hash = (s) => {
let n, m;
let p;
let wsize = s.w_size;
n = s.hash_size;
p = n;
do {
m = s.head[--p];
s.head[p] = m >= wsize ? m - wsize : 0;
} while (--n);
n = wsize;
p = n;
do {
m = s.prev[--p];
s.prev[p] = m >= wsize ? m - wsize : 0;
} while (--n);
};
var HASH_ZLIB = (s, prev, data) => (prev << s.hash_shift ^ data) & s.hash_mask;
var HASH = HASH_ZLIB;
var flush_pending = (strm) => {
const s = strm.state;
let len = s.pending;
if (len > strm.avail_out) {
len = strm.avail_out;
}
if (len === 0) {
return;
}
strm.output.set(s.pending_buf.subarray(s.pending_out, s.pending_out + len), strm.next_out);
strm.next_out += len;
s.pending_out += len;
strm.total_out += len;
strm.avail_out -= len;
s.pending -= len;
if (s.pending === 0) {
s.pending_out = 0;
}
};
var flush_block_only = (s, last) => {
_tr_flush_block(s, s.block_start >= 0 ? s.block_start : -1, s.strstart - s.block_start, last);
s.block_start = s.strstart;
flush_pending(s.strm);
};
var put_byte = (s, b) => {
s.pending_buf[s.pending++] = b;
};
var putShortMSB = (s, b) => {
s.pending_buf[s.pending++] = b >>> 8 & 255;
s.pending_buf[s.pending++] = b & 255;
};
var read_buf = (strm, buf, start, size) => {
let len = strm.avail_in;
if (len > size) {
len = size;
}
if (len === 0) {
return 0;
}
strm.avail_in -= len;
buf.set(strm.input.subarray(strm.next_in, strm.next_in + len), start);
if (strm.state.wrap === 1) {
strm.adler = adler32_1(strm.adler, buf, len, start);
} else if (strm.state.wrap === 2) {
strm.adler = crc32_1(strm.adler, buf, len, start);
}
strm.next_in += len;
strm.total_in += len;
return len;
};
var longest_match = (s, cur_match) => {
let chain_length = s.max_chain_length;
let scan = s.strstart;
let match;
let len;
let best_len = s.prev_length;
let nice_match = s.nice_match;
const limit = s.strstart > s.w_size - MIN_LOOKAHEAD ? s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0;
const _win = s.window;
const wmask = s.w_mask;
const prev = s.prev;
const strend = s.strstart + MAX_MATCH;
let scan_end1 = _win[scan + best_len - 1];
let scan_end = _win[scan + best_len];
if (s.prev_length >= s.good_match) {
chain_length >>= 2;
}
if (nice_match > s.lookahead) {
nice_match = s.lookahead;
}
do {
match = cur_match;
if (_win[match + best_len] !== scan_end || _win[match + best_len - 1] !== scan_end1 || _win[match] !== _win[scan] || _win[++match] !== _win[scan + 1]) {
continue;
}
scan += 2;
match++;
do {
} while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && scan < strend);
len = MAX_MATCH - (strend - scan);
scan = strend - MAX_MATCH;
if (len > best_len) {
s.match_start = cur_match;
best_len = len;
if (len >= nice_match) {
break;
}
scan_end1 = _win[scan + best_len - 1];
scan_end = _win[scan + best_len];
}
} while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0);
if (best_len <= s.lookahead) {
return best_len;
}
return s.lookahead;
};
var fill_window = (s) => {
const _w_size = s.w_size;
let n, more, str;
do {
more = s.window_size - s.lookahead - s.strstart;
if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {
s.window.set(s.window.subarray(_w_size, _w_size + _w_size - more), 0);
s.match_start -= _w_size;
s.strstart -= _w_size;
s.block_start -= _w_size;
if (s.insert > s.strstart) {
s.insert = s.strstart;
}
slide_hash(s);
more += _w_size;
}
if (s.strm.avail_in === 0) {
break;
}
n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more);
s.lookahead += n;
if (s.lookahead + s.insert >= MIN_MATCH) {
str = s.strstart - s.insert;
s.ins_h = s.window[str];
s.ins_h = HASH(s, s.ins_h, s.window[str + 1]);
while (s.insert) {
s.ins_h = HASH(s, s.ins_h, s.window[str + MIN_MATCH - 1]);
s.prev[str & s.w_mask] = s.head[s.ins_h];
s.head[s.ins_h] = str;
str++;
s.insert--;
if (s.lookahead + s.insert < MIN_MATCH) {
break;
}
}
}
} while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0);
};
var deflate_stored = (s, flush) => {
let min_block = s.pending_buf_size - 5 > s.w_size ? s.w_size : s.pending_buf_size - 5;
let len, left, have, last = 0;
let used = s.strm.avail_in;
do {
len = 65535;
have = s.bi_valid + 42 >> 3;
if (s.strm.avail_out < have) {
break;
}
have = s.strm.avail_out - have;
left = s.strstart - s.block_start;
if (len > left + s.strm.avail_in) {
len = left + s.strm.avail_in;
}
if (len > have) {
len = have;
}
if (len < min_block && (len === 0 && flush !== Z_FINISH$3 || flush === Z_NO_FLUSH$2 || len !== left + s.strm.avail_in)) {
break;
}
last = flush === Z_FINISH$3 && len === left + s.strm.avail_in ? 1 : 0;
_tr_stored_block(s, 0, 0, last);
s.pending_buf[s.pending - 4] = len;
s.pending_buf[s.pending - 3] = len >> 8;
s.pending_buf[s.pending - 2] = ~len;
s.pending_buf[s.pending - 1] = ~len >> 8;
flush_pending(s.strm);
if (left) {
if (left > len) {
left = len;
}
s.strm.output.set(s.window.subarray(s.block_start, s.block_start + left), s.strm.next_out);
s.strm.next_out += left;
s.strm.avail_out -= left;
s.strm.total_out += left;
s.block_start += left;
len -= left;
}
if (len) {
read_buf(s.strm, s.strm.output, s.strm.next_out, len);
s.strm.next_out += len;
s.strm.avail_out -= len;
s.strm.total_out += len;
}
} while (last === 0);
used -= s.strm.avail_in;
if (used) {
if (used >= s.w_size) {
s.matches = 2;
s.window.set(s.strm.input.subarray(s.strm.next_in - s.w_size, s.strm.next_in), 0);
s.strstart = s.w_size;
s.insert = s.strstart;
} else {
if (s.window_size - s.strstart <= used) {
s.strstart -= s.w_size;
s.window.set(s.window.subarray(s.w_size, s.w_size + s.strstart), 0);
if (s.matches < 2) {
s.matches++;
}
if (s.insert > s.strstart) {
s.insert = s.strstart;
}
}
s.window.set(s.strm.input.subarray(s.strm.next_in - used, s.strm.next_in), s.strstart);
s.strstart += used;
s.insert += used > s.w_size - s.insert ? s.w_size - s.insert : used;
}
s.block_start = s.strstart;
}
if (s.high_water < s.strstart) {
s.high_water = s.strstart;
}
if (last) {
return BS_FINISH_DONE;
}
if (flush !== Z_NO_FLUSH$2 && flush !== Z_FINISH$3 && s.strm.avail_in === 0 && s.strstart === s.block_start) {
return BS_BLOCK_DONE;
}
have = s.window_size - s.strstart;
if (s.strm.avail_in > have && s.block_start >= s.w_size) {
s.block_start -= s.w_size;
s.strstart -= s.w_size;
s.window.set(s.window.subarray(s.w_size, s.w_size + s.strstart), 0);
if (s.matches < 2) {
s.matches++;
}
have += s.w_size;
if (s.insert > s.strstart) {
s.insert = s.strstart;
}
}
if (have > s.strm.avail_in) {
have = s.strm.avail_in;
}
if (have) {
read_buf(s.strm, s.window, s.strstart, have);
s.strstart += have;
s.insert += have > s.w_size - s.insert ? s.w_size - s.insert : have;
}
if (s.high_water < s.strstart) {
s.high_water = s.strstart;
}
have = s.bi_valid + 42 >> 3;
have = s.pending_buf_size - have > 65535 ? 65535 : s.pending_buf_size - have;
min_block = have > s.w_size ? s.w_size : have;
left = s.strstart - s.block_start;
if (left >= min_block || (left || flush === Z_FINISH$3) && flush !== Z_NO_FLUSH$2 && s.strm.avail_in === 0 && left <= have) {
len = left > have ? have : left;
last = flush === Z_FINISH$3 && s.strm.avail_in === 0 && len === left ? 1 : 0;
_tr_stored_block(s, s.block_start, len, last);
s.block_start += len;
flush_pending(s.strm);
}
return last ? BS_FINISH_STARTED : BS_NEED_MORE;
};
var deflate_fast = (s, flush) => {
let hash_head;
let bflush;
for (; ; ) {
if (s.lookahead < MIN_LOOKAHEAD) {
fill_window(s);
if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH$2) {
return BS_NEED_MORE;
}
if (s.lookahead === 0) {
break;
}
}
hash_head = 0;
if (s.lookahead >= MIN_MATCH) {
s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
s.head[s.ins_h] = s.strstart;
}
if (hash_head !== 0 && s.strstart - hash_head <= s.w_size - MIN_LOOKAHEAD) {
s.match_length = longest_match(s, hash_head);
}
if (s.match_length >= MIN_MATCH) {
bflush = _tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH);
s.lookahead -= s.match_length;
if (s.match_length <= s.max_lazy_match && s.lookahead >= MIN_MATCH) {
s.match_length--;
do {
s.strstart++;
s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
s.head[s.ins_h] = s.strstart;
} while (--s.match_length !== 0);
s.strstart++;
} else {
s.strstart += s.match_length;
s.match_length = 0;
s.ins_h = s.window[s.strstart];
s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + 1]);
}
} else {
bflush = _tr_tally(s, 0, s.window[s.strstart]);
s.lookahead--;
s.strstart++;
}
if (bflush) {
flush_block_only(s, false);
if (s.strm.avail_out === 0) {
return BS_NEED_MORE;
}
}
}
s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1;
if (flush === Z_FINISH$3) {
flush_block_only(s, true);
if (s.strm.avail_out === 0) {
return BS_FINISH_STARTED;
}
return BS_FINISH_DONE;
}
if (s.sym_next) {
flush_block_only(s, false);
if (s.strm.avail_out === 0) {
return BS_NEED_MORE;
}
}
return BS_BLOCK_DONE;
};
var deflate_slow = (s, flush) => {
let hash_head;
let bflush;
let max_insert;
for (; ; ) {
if (s.lookahead < MIN_LOOKAHEAD) {
fill_window(s);
if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH$2) {
return BS_NEED_MORE;
}
if (s.lookahead === 0) {
break;
}
}
hash_head = 0;
if (s.lookahead >= MIN_MATCH) {
s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
s.head[s.ins_h] = s.strstart;
}
s.prev_length = s.match_length;
s.prev_match = s.match_start;
s.match_length = MIN_MATCH - 1;
if (hash_head !== 0 && s.prev_length < s.max_lazy_match && s.strstart - hash_head <= s.w_size - MIN_LOOKAHEAD) {
s.match_length = longest_match(s, hash_head);
if (s.match_length <= 5 && (s.strategy === Z_FILTERED || s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096)) {
s.match_length = MIN_MATCH - 1;
}
}
if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) {
max_insert = s.strstart + s.lookahead - MIN_MATCH;
bflush = _tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH);
s.lookahead -= s.prev_length - 1;
s.prev_length -= 2;
do {
if (++s.strstart <= max_insert) {
s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]);
hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
s.head[s.ins_h] = s.strstart;
}
} while (--s.prev_length !== 0);
s.match_available = 0;
s.match_length = MIN_MATCH - 1;
s.strstart++;
if (bflush) {
flush_block_only(s, false);
if (s.strm.avail_out === 0) {
return BS_NEED_MORE;
}
}
} else if (s.match_available) {
bflush = _tr_tally(s, 0, s.window[s.strstart - 1]);
if (bflush) {
flush_block_only(s, false);
}
s.strstart++;
s.lookahead--;
if (s.strm.avail_out === 0) {
return BS_NEED_MORE;
}
} else {
s.match_available = 1;
s.strstart++;
s.lookahead--;
}
}
if (s.match_available) {
bflush = _tr_tally(s, 0, s.window[s.strstart - 1]);
s.match_available = 0;
}
s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1;
if (flush === Z_FINISH$3) {
flush_block_only(s, true);
if (s.strm.avail_out === 0) {
return BS_FINISH_STARTED;
}
return BS_FINISH_DONE;
}
if (s.sym_next) {
flush_block_only(s, false);
if (s.strm.avail_out === 0) {
return BS_NEED_MORE;
}
}
return BS_BLOCK_DONE;
};
var deflate_rle = (s, flush) => {
let bflush;
let prev;
let scan, strend;
const _win = s.window;
for (; ; ) {
if (s.lookahead <= MAX_MATCH) {
fill_window(s);
if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH$2) {
return BS_NEED_MORE;
}
if (s.lookahead === 0) {
break;
}
}
s.match_length = 0;
if (s.lookahead >= MIN_MATCH && s.strstart > 0) {
scan = s.strstart - 1;
prev = _win[scan];
if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) {
strend = s.strstart + MAX_MATCH;
do {
} while (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && scan < strend);
s.match_length = MAX_MATCH - (strend - scan);
if (s.match_length > s.lookahead) {
s.match_length = s.lookahead;
}
}
}
if (s.match_length >= MIN_MATCH) {
bflush = _tr_tally(s, 1, s.match_length - MIN_MATCH);
s.lookahead -= s.match_length;
s.strstart += s.match_length;
s.match_length = 0;
} else {
bflush = _tr_tally(s, 0, s.window[s.strstart]);
s.lookahead--;
s.strstart++;
}
if (bflush) {
flush_block_only(s, false);
if (s.strm.avail_out === 0) {
return BS_NEED_MORE;
}
}
}
s.insert = 0;
if (flush === Z_FINISH$3) {
flush_block_only(s, true);
if (s.strm.avail_out === 0) {
return BS_FINISH_STARTED;
}
return BS_FINISH_DONE;
}
if (s.sym_next) {
flush_block_only(s, false);
if (s.strm.avail_out === 0) {
return BS_NEED_MORE;
}
}
return BS_BLOCK_DONE;
};
var deflate_huff = (s, flush) => {
let bflush;
for (; ; ) {
if (s.lookahead === 0) {
fill_window(s);
if (s.lookahead === 0) {
if (flush === Z_NO_FLUSH$2) {
return BS_NEED_MORE;
}
break;
}
}
s.match_length = 0;
bflush = _tr_tally(s, 0, s.window[s.strstart]);
s.lookahead--;
s.strstart++;
if (bflush) {
flush_block_only(s, false);
if (s.strm.avail_out === 0) {
return BS_NEED_MORE;
}
}
}
s.insert = 0;
if (flush === Z_FINISH$3) {
flush_block_only(s, true);
if (s.strm.avail_out === 0) {
return BS_FINISH_STARTED;
}
return BS_FINISH_DONE;
}
if (s.sym_next) {
flush_block_only(s, false);
if (s.strm.avail_out === 0) {
return BS_NEED_MORE;
}
}
return BS_BLOCK_DONE;
};
function Config(good_length, max_lazy, nice_length, max_chain, func) {
this.good_length = good_length;
this.max_lazy = max_lazy;
this.nice_length = nice_length;
this.max_chain = max_chain;
this.func = func;
}
var configuration_table = [
/* good lazy nice chain */
new Config(0, 0, 0, 0, deflate_stored),
/* 0 store only */
new Config(4, 4, 8, 4, deflate_fast),
/* 1 max speed, no lazy matches */
new Config(4, 5, 16, 8, deflate_fast),
/* 2 */
new Config(4, 6, 32, 32, deflate_fast),
/* 3 */
new Config(4, 4, 16, 16, deflate_slow),
/* 4 lazy matches */
new Config(8, 16, 32, 32, deflate_slow),
/* 5 */
new Config(8, 16, 128, 128, deflate_slow),
/* 6 */
new Config(8, 32, 128, 256, deflate_slow),
/* 7 */
new Config(32, 128, 258, 1024, deflate_slow),
/* 8 */
new Config(32, 258, 258, 4096, deflate_slow)
/* 9 max compression */
];
var lm_init = (s) => {
s.window_size = 2 * s.w_size;
zero(s.head);
s.max_lazy_match = configuration_table[s.level].max_lazy;
s.good_match = configuration_table[s.level].good_length;
s.nice_match = configuration_table[s.level].nice_length;
s.max_chain_length = configuration_table[s.level].max_chain;
s.strstart = 0;
s.block_start = 0;
s.lookahead = 0;
s.insert = 0;
s.match_length = s.prev_length = MIN_MATCH - 1;
s.match_available = 0;
s.ins_h = 0;
};
function DeflateState() {
this.strm = null;
this.status = 0;
this.pending_buf = null;
this.pending_buf_size = 0;
this.pending_out = 0;
this.pending = 0;
this.wrap = 0;
this.gzhead = null;
this.gzindex = 0;
this.method = Z_DEFLATED$2;
this.last_flush = -1;
this.w_size = 0;
this.w_bits = 0;
this.w_mask = 0;
this.window = null;
this.window_size = 0;
this.prev = null;
this.head = null;
this.ins_h = 0;
this.hash_size = 0;
this.hash_bits = 0;
this.hash_mask = 0;
this.hash_shift = 0;
this.block_start = 0;
this.match_length = 0;
this.prev_match = 0;
this.match_available = 0;
this.strstart = 0;
this.match_start = 0;
this.lookahead = 0;
this.prev_length = 0;
this.max_chain_length = 0;
this.max_lazy_match = 0;
this.level = 0;
this.strategy = 0;
this.good_match = 0;
this.nice_match = 0;
this.dyn_ltree = new Uint16Array(HEAP_SIZE * 2);
this.dyn_dtree = new Uint16Array((2 * D_CODES + 1) * 2);
this.bl_tree = new Uint16Array((2 * BL_CODES + 1) * 2);
zero(this.dyn_ltree);
zero(this.dyn_dtree);
zero(this.bl_tree);
this.l_desc = null;
this.d_desc = null;
this.bl_desc = null;
this.bl_count = new Uint16Array(MAX_BITS + 1);
this.heap = new Uint16Array(2 * L_CODES + 1);
zero(this.heap);
this.heap_len = 0;
this.heap_max = 0;
this.depth = new Uint16Array(2 * L_CODES + 1);
zero(this.depth);
this.sym_buf = 0;
this.lit_bufsize = 0;
this.sym_next = 0;
this.sym_end = 0;
this.opt_len = 0;
this.static_len = 0;
this.matches = 0;
this.insert = 0;
this.bi_buf = 0;
this.bi_valid = 0;
}
var deflateStateCheck = (strm) => {
if (!strm) {
return 1;
}
const s = strm.state;
if (!s || s.strm !== strm || s.status !== INIT_STATE && //#ifdef GZIP
s.status !== GZIP_STATE && //#endif
s.status !== EXTRA_STATE && s.status !== NAME_STATE && s.status !== COMMENT_STATE && s.status !== HCRC_STATE && s.status !== BUSY_STATE && s.status !== FINISH_STATE) {
return 1;
}
return 0;
};
var deflateResetKeep = (strm) => {
if (deflateStateCheck(strm)) {
return err(strm, Z_STREAM_ERROR$2);
}
strm.total_in = strm.total_out = 0;
strm.data_type = Z_UNKNOWN;
const s = strm.state;
s.pending = 0;
s.pending_out = 0;
if (s.wrap < 0) {
s.wrap = -s.wrap;
}
s.status = //#ifdef GZIP
s.wrap === 2 ? GZIP_STATE : (
//#endif
s.wrap ? INIT_STATE : BUSY_STATE
);
strm.adler = s.wrap === 2 ? 0 : 1;
s.last_flush = -2;
_tr_init(s);
return Z_OK$3;
};
var deflateReset = (strm) => {
const ret = deflateResetKeep(strm);
if (ret === Z_OK$3) {
lm_init(strm.state);
}
return ret;
};
var deflateSetHeader = (strm, head) => {
if (deflateStateCheck(strm) || strm.state.wrap !== 2) {
return Z_STREAM_ERROR$2;
}
strm.state.gzhead = head;
return Z_OK$3;
};
var deflateInit2 = (strm, level, method, windowBits, memLevel, strategy) => {
if (!strm) {
return Z_STREAM_ERROR$2;
}
let wrap = 1;
if (level === Z_DEFAULT_COMPRESSION$1) {
level = 6;
}
if (windowBits < 0) {
wrap = 0;
windowBits = -windowBits;
} else if (windowBits > 15) {
wrap = 2;
windowBits -= 16;
}
if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED$2 || windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED || windowBits === 8 && wrap !== 1) {
return err(strm, Z_STREAM_ERROR$2);
}
if (windowBits === 8) {
windowBits = 9;
}
const s = new DeflateState();
strm.state = s;
s.strm = strm;
s.status = INIT_STATE;
s.wrap = wrap;
s.gzhead = null;
s.w_bits = windowBits;
s.w_size = 1 << s.w_bits;
s.w_mask = s.w_size - 1;
s.hash_bits = memLevel + 7;
s.hash_size = 1 << s.hash_bits;
s.hash_mask = s.hash_size - 1;
s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH);
s.window = new Uint8Array(s.w_size * 2);
s.head = new Uint16Array(s.hash_size);
s.prev = new Uint16Array(s.w_size);
s.lit_bufsize = 1 << memLevel + 6;
s.pending_buf_size = s.lit_bufsize * 4;
s.pending_buf = new Uint8Array(s.pending_buf_size);
s.sym_buf = s.lit_bufsize;
s.sym_end = (s.lit_bufsize - 1) * 3;
s.level = level;
s.strategy = strategy;
s.method = method;
return deflateReset(strm);
};
var deflateInit = (strm, level) => {
return deflateInit2(strm, level, Z_DEFLATED$2, MAX_WBITS$1, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY$1);
};
var deflate$2 = (strm, flush) => {
if (deflateStateCheck(strm) || flush > Z_BLOCK$1 || flush < 0) {
return strm ? err(strm, Z_STREAM_ERROR$2) : Z_STREAM_ERROR$2;
}
const s = strm.state;
if (!strm.output || strm.avail_in !== 0 && !strm.input || s.status === FINISH_STATE && flush !== Z_FINISH$3) {
return err(strm, strm.avail_out === 0 ? Z_BUF_ERROR$1 : Z_STREAM_ERROR$2);
}
const old_flush = s.last_flush;
s.last_flush = flush;
if (s.pending !== 0) {
flush_pending(strm);
if (strm.avail_out === 0) {
s.last_flush = -1;
return Z_OK$3;
}
} else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) && flush !== Z_FINISH$3) {
return err(strm, Z_BUF_ERROR$1);
}
if (s.status === FINISH_STATE && strm.avail_in !== 0) {
return err(strm, Z_BUF_ERROR$1);
}
if (s.status === INIT_STATE && s.wrap === 0) {
s.status = BUSY_STATE;
}
if (s.status === INIT_STATE) {
let header = Z_DEFLATED$2 + (s.w_bits - 8 << 4) << 8;
let level_flags = -1;
if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
level_flags = 0;
} else if (s.level < 6) {
level_flags = 1;
} else if (s.level === 6) {
level_flags = 2;
} else {
level_flags = 3;
}
header |= level_flags << 6;
if (s.strstart !== 0) {
header |= PRESET_DICT;
}
header += 31 - header % 31;
putShortMSB(s, header);
if (s.strstart !== 0) {
putShortMSB(s, strm.adler >>> 16);
putShortMSB(s, strm.adler & 65535);
}
strm.adler = 1;
s.status = BUSY_STATE;
flush_pending(strm);
if (s.pending !== 0) {
s.last_flush = -1;
return Z_OK$3;
}
}
if (s.status === GZIP_STATE) {
strm.adler = 0;
put_byte(s, 31);
put_byte(s, 139);
put_byte(s, 8);
if (!s.gzhead) {
put_byte(s, 0);
put_byte(s, 0);
put_byte(s, 0);
put_byte(s, 0);
put_byte(s, 0);
put_byte(s, s.level === 9 ? 2 : s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ? 4 : 0);
put_byte(s, OS_CODE);
s.status = BUSY_STATE;
flush_pending(strm);
if (s.pending !== 0) {
s.last_flush = -1;
return Z_OK$3;
}
} else {
put_byte(
s,
(s.gzhead.text ? 1 : 0) + (s.gzhead.hcrc ? 2 : 0) + (!s.gzhead.extra ? 0 : 4) + (!s.gzhead.name ? 0 : 8) + (!s.gzhead.comment ? 0 : 16)
);
put_byte(s, s.gzhead.time & 255);
put_byte(s, s.gzhead.time >> 8 & 255);
put_byte(s, s.gzhead.time >> 16 & 255);
put_byte(s, s.gzhead.time >> 24 & 255);
put_byte(s, s.level === 9 ? 2 : s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ? 4 : 0);
put_byte(s, s.gzhead.os & 255);
if (s.gzhead.extra && s.gzhead.extra.length) {
put_byte(s, s.gzhead.extra.length & 255);
put_byte(s, s.gzhead.extra.length >> 8 & 255);
}
if (s.gzhead.hcrc) {
strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending, 0);
}
s.gzindex = 0;
s.status = EXTRA_STATE;
}
}
if (s.status === EXTRA_STATE) {
if (s.gzhead.extra) {
let beg = s.pending;
let left = (s.gzhead.extra.length & 65535) - s.gzindex;
while (s.pending + left > s.pending_buf_size) {
let copy = s.pending_buf_size - s.pending;
s.pending_buf.set(s.gzhead.extra.subarray(s.gzindex, s.gzindex + copy), s.pending);
s.pending = s.pending_buf_size;
if (s.gzhead.hcrc && s.pending > beg) {
strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
}
s.gzindex += copy;
flush_pending(strm);
if (s.pending !== 0) {
s.last_flush = -1;
return Z_OK$3;
}
beg = 0;
left -= copy;
}
let gzhead_extra = new Uint8Array(s.gzhead.extra);
s.pending_buf.set(gzhead_extra.subarray(s.gzindex, s.gzindex + left), s.pending);
s.pending += left;
if (s.gzhead.hcrc && s.pending > beg) {
strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
}
s.gzindex = 0;
}
s.status = NAME_STATE;
}
if (s.status === NAME_STATE) {
if (s.gzhead.name) {
let beg = s.pending;
let val;
do {
if (s.pending === s.pending_buf_size) {
if (s.gzhead.hcrc && s.pending > beg) {
strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
}
flush_pending(strm);
if (s.pending !== 0) {
s.last_flush = -1;
return Z_OK$3;
}
beg = 0;
}
if (s.gzindex < s.gzhead.name.length) {
val = s.gzhead.name.charCodeAt(s.gzindex++) & 255;
} else {
val = 0;
}
put_byte(s, val);
} while (val !== 0);
if (s.gzhead.hcrc && s.pending > beg) {
strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
}
s.gzindex = 0;
}
s.status = COMMENT_STATE;
}
if (s.status === COMMENT_STATE) {
if (s.gzhead.comment) {
let beg = s.pending;
let val;
do {
if (s.pending === s.pending_buf_size) {
if (s.gzhead.hcrc && s.pending > beg) {
strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
}
flush_pending(strm);
if (s.pending !== 0) {
s.last_flush = -1;
return Z_OK$3;
}
beg = 0;
}
if (s.gzindex < s.gzhead.comment.length) {
val = s.gzhead.comment.charCodeAt(s.gzindex++) & 255;
} else {
val = 0;
}
put_byte(s, val);
} while (val !== 0);
if (s.gzhead.hcrc && s.pending > beg) {
strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg);
}
}
s.status = HCRC_STATE;
}
if (s.status === HCRC_STATE) {
if (s.gzhead.hcrc) {
if (s.pending + 2 > s.pending_buf_size) {
flush_pending(strm);
if (s.pending !== 0) {
s.last_flush = -1;
return Z_OK$3;
}
}
put_byte(s, strm.adler & 255);
put_byte(s, strm.adler >> 8 & 255);
strm.adler = 0;
}
s.status = BUSY_STATE;
flush_pending(strm);
if (s.pending !== 0) {
s.last_flush = -1;
return Z_OK$3;
}
}
if (strm.avail_in !== 0 || s.lookahead !== 0 || flush !== Z_NO_FLUSH$2 && s.status !== FINISH_STATE) {
let bstate = s.level === 0 ? deflate_stored(s, flush) : s.strategy === Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : s.strategy === Z_RLE ? deflate_rle(s, flush) : configuration_table[s.level].func(s, flush);
if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {
s.status = FINISH_STATE;
}
if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {
if (strm.avail_out === 0) {
s.last_flush = -1;
}
return Z_OK$3;
}
if (bstate === BS_BLOCK_DONE) {
if (flush === Z_PARTIAL_FLUSH) {
_tr_align(s);
} else if (flush !== Z_BLOCK$1) {
_tr_stored_block(s, 0, 0, false);
if (flush === Z_FULL_FLUSH$1) {
zero(s.head);
if (s.lookahead === 0) {
s.strstart = 0;
s.block_start = 0;
s.insert = 0;
}
}
}
flush_pending(strm);
if (strm.avail_out === 0) {
s.last_flush = -1;
return Z_OK$3;
}
}
}
if (flush !== Z_FINISH$3) {
return Z_OK$3;
}
if (s.wrap <= 0) {
return Z_STREAM_END$3;
}
if (s.wrap === 2) {
put_byte(s, strm.adler & 255);
put_byte(s, strm.adler >> 8 & 255);
put_byte(s, strm.adler >> 16 & 255);
put_byte(s, strm.adler >> 24 & 255);
put_byte(s, strm.total_in & 255);
put_byte(s, strm.total_in >> 8 & 255);
put_byte(s, strm.total_in >> 16 & 255);
put_byte(s, strm.total_in >> 24 & 255);
} else {
putShortMSB(s, strm.adler >>> 16);
putShortMSB(s, strm.adler & 65535);
}
flush_pending(strm);
if (s.wrap > 0) {
s.wrap = -s.wrap;
}
return s.pending !== 0 ? Z_OK$3 : Z_STREAM_END$3;
};
var deflateEnd = (strm) => {
if (deflateStateCheck(strm)) {
return Z_STREAM_ERROR$2;
}
const status = strm.state.status;
strm.state = null;
return status === BUSY_STATE ? err(strm, Z_DATA_ERROR$2) : Z_OK$3;
};
var deflateSetDictionary = (strm, dictionary) => {
let dictLength = dictionary.length;
if (deflateStateCheck(strm)) {
return Z_STREAM_ERROR$2;
}
const s = strm.state;
const wrap = s.wrap;
if (wrap === 2 || wrap === 1 && s.status !== INIT_STATE || s.lookahead) {
return Z_STREAM_ERROR$2;
}
if (wrap === 1) {
strm.adler = adler32_1(strm.adler, dictionary, dictLength, 0);
}
s.wrap = 0;
if (dictLength >= s.w_size) {
if (wrap === 0) {
zero(s.head);
s.strstart = 0;
s.block_start = 0;
s.insert = 0;
}
let tmpDict = new Uint8Array(s.w_size);
tmpDict.set(dictionary.subarray(dictLength - s.w_size, dictLength), 0);
dictionary = tmpDict;
dictLength = s.w_size;
}
const avail = strm.avail_in;
const next = strm.next_in;
const input = strm.input;
strm.avail_in = dictLength;
strm.next_in = 0;
strm.input = dictionary;
fill_window(s);
while (s.lookahead >= MIN_MATCH) {
let str = s.strstart;
let n = s.lookahead - (MIN_MATCH - 1);
do {
s.ins_h = HASH(s, s.ins_h, s.window[str + MIN_MATCH - 1]);
s.prev[str & s.w_mask] = s.head[s.ins_h];
s.head[s.ins_h] = str;
str++;
} while (--n);
s.strstart = str;
s.lookahead = MIN_MATCH - 1;
fill_window(s);
}
s.strstart += s.lookahead;
s.block_start = s.strstart;
s.insert = s.lookahead;
s.lookahead = 0;
s.match_length = s.prev_length = MIN_MATCH - 1;
s.match_available = 0;
strm.next_in = next;
strm.input = input;
strm.avail_in = avail;
s.wrap = wrap;
return Z_OK$3;
};
var deflateInit_1 = deflateInit;
var deflateInit2_1 = deflateInit2;
var deflateReset_1 = deflateReset;
var deflateResetKeep_1 = deflateResetKeep;
var deflateSetHeader_1 = deflateSetHeader;
var deflate_2$1 = deflate$2;
var deflateEnd_1 = deflateEnd;
var deflateSetDictionary_1 = deflateSetDictionary;
var deflateInfo = "pako deflate (from Nodeca project)";
var deflate_1$2 = {
deflateInit: deflateInit_1,
deflateInit2: deflateInit2_1,
deflateReset: deflateReset_1,
deflateResetKeep: deflateResetKeep_1,
deflateSetHeader: deflateSetHeader_1,
deflate: deflate_2$1,
deflateEnd: deflateEnd_1,
deflateSetDictionary: deflateSetDictionary_1,
deflateInfo
};
var _has = (obj, key) => {
return Object.prototype.hasOwnProperty.call(obj, key);
};
var assign = function(obj) {
const sources = Array.prototype.slice.call(arguments, 1);
while (sources.length) {
const source = sources.shift();
if (!source) {
continue;
}
if (typeof source !== "object") {
throw new TypeError(source + "must be non-object");
}
for (const p in source) {
if (_has(source, p)) {
obj[p] = source[p];
}
}
}
return obj;
};
var flattenChunks = (chunks) => {
let len = 0;
for (let i = 0, l = chunks.length; i < l; i++) {
len += chunks[i].length;
}
const result = new Uint8Array(len);
for (let i = 0, pos = 0, l = chunks.length; i < l; i++) {
let chunk = chunks[i];
result.set(chunk, pos);
pos += chunk.length;
}
return result;
};
var common = {
assign,
flattenChunks
};
var STR_APPLY_UIA_OK = true;
try {
String.fromCharCode.apply(null, new Uint8Array(1));
} catch (__) {
STR_APPLY_UIA_OK = false;
}
var _utf8len = new Uint8Array(256);
for (let q = 0; q < 256; q++) {
_utf8len[q] = q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1;
}
_utf8len[254] = _utf8len[254] = 1;
var string2buf = (str) => {
if (typeof TextEncoder === "function" && TextEncoder.prototype.encode) {
return new TextEncoder().encode(str);
}
let buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
for (m_pos = 0; m_pos < str_len; m_pos++) {
c = str.charCodeAt(m_pos);
if ((c & 64512) === 55296 && m_pos + 1 < str_len) {
c2 = str.charCodeAt(m_pos + 1);
if ((c2 & 64512) === 56320) {
c = 65536 + (c - 55296 << 10) + (c2 - 56320);
m_pos++;
}
}
buf_len += c < 128 ? 1 : c < 2048 ? 2 : c < 65536 ? 3 : 4;
}
buf = new Uint8Array(buf_len);
for (i = 0, m_pos = 0; i < buf_len; m_pos++) {
c = str.charCodeAt(m_pos);
if ((c & 64512) === 55296 && m_pos + 1 < str_len) {
c2 = str.charCodeAt(m_pos + 1);
if ((c2 & 64512) === 56320) {
c = 65536 + (c - 55296 << 10) + (c2 - 56320);
m_pos++;
}
}
if (c < 128) {
buf[i++] = c;
} else if (c < 2048) {
buf[i++] = 192 | c >>> 6;
buf[i++] = 128 | c & 63;
} else if (c < 65536) {
buf[i++] = 224 | c >>> 12;
buf[i++] = 128 | c >>> 6 & 63;
buf[i++] = 128 | c & 63;
} else {
buf[i++] = 240 | c >>> 18;
buf[i++] = 128 | c >>> 12 & 63;
buf[i++] = 128 | c >>> 6 & 63;
buf[i++] = 128 | c & 63;
}
}
return buf;
};
var buf2binstring = (buf, len) => {
if (len < 65534) {
if (buf.subarray && STR_APPLY_UIA_OK) {
return String.fromCharCode.apply(null, buf.length === len ? buf : buf.subarray(0, len));
}
}
let result = "";
for (let i = 0; i < len; i++) {
result += String.fromCharCode(buf[i]);
}
return result;
};
var buf2string = (buf, max) => {
const len = max || buf.length;
if (typeof TextDecoder === "function" && TextDecoder.prototype.decode) {
return new TextDecoder().decode(buf.subarray(0, max));
}
let i, out;
const utf16buf = new Array(len * 2);
for (out = 0, i = 0; i < len; ) {
let c = buf[i++];
if (c < 128) {
utf16buf[out++] = c;
continue;
}
let c_len = _utf8len[c];
if (c_len > 4) {
utf16buf[out++] = 65533;
i += c_len - 1;
continue;
}
c &= c_len === 2 ? 31 : c_len === 3 ? 15 : 7;
while (c_len > 1 && i < len) {
c = c << 6 | buf[i++] & 63;
c_len--;
}
if (c_len > 1) {
utf16buf[out++] = 65533;
continue;
}
if (c < 65536) {
utf16buf[out++] = c;
} else {
c -= 65536;
utf16buf[out++] = 55296 | c >> 10 & 1023;
utf16buf[out++] = 56320 | c & 1023;
}
}
return buf2binstring(utf16buf, out);
};
var utf8border = (buf, max) => {
max = max || buf.length;
if (max > buf.length) {
max = buf.length;
}
let pos = max - 1;
while (pos >= 0 && (buf[pos] & 192) === 128) {
pos--;
}
if (pos < 0) {
return max;
}
if (pos === 0) {
return max;
}
return pos + _utf8len[buf[pos]] > max ? pos : max;
};
var strings = {
string2buf,
buf2string,
utf8border
};
function ZStream() {
this.input = null;
this.next_in = 0;
this.avail_in = 0;
this.total_in = 0;
this.output = null;
this.next_out = 0;
this.avail_out = 0;
this.total_out = 0;
this.msg = "";
this.state = null;
this.data_type = 2;
this.adler = 0;
}
var zstream = ZStream;
var toString$1 = Object.prototype.toString;
var {
Z_NO_FLUSH: Z_NO_FLUSH$1,
Z_SYNC_FLUSH,
Z_FULL_FLUSH,
Z_FINISH: Z_FINISH$2,
Z_OK: Z_OK$2,
Z_STREAM_END: Z_STREAM_END$2,
Z_DEFAULT_COMPRESSION,
Z_DEFAULT_STRATEGY,
Z_DEFLATED: Z_DEFLATED$1
} = constants$2;
function Deflate$1(options) {
this.options = common.assign({
level: Z_DEFAULT_COMPRESSION,
method: Z_DEFLATED$1,
chunkSize: 16384,
windowBits: 15,
memLevel: 8,
strategy: Z_DEFAULT_STRATEGY
}, options || {});
let opt = this.options;
if (opt.raw && opt.windowBits > 0) {
opt.windowBits = -opt.windowBits;
} else if (opt.gzip && opt.windowBits > 0 && opt.windowBits < 16) {
opt.windowBits += 16;
}
this.err = 0;
this.msg = "";
this.ended = false;
this.chunks = [];
this.strm = new zstream();
this.strm.avail_out = 0;
let status = deflate_1$2.deflateInit2(
this.strm,
opt.level,
opt.method,
opt.windowBits,
opt.memLevel,
opt.strategy
);
if (status !== Z_OK$2) {
throw new Error(messages[status]);
}
if (opt.header) {
deflate_1$2.deflateSetHeader(this.strm, opt.header);
}
if (opt.dictionary) {
let dict;
if (typeof opt.dictionary === "string") {
dict = strings.string2buf(opt.dictionary);
} else if (toString$1.call(opt.dictionary) === "[object ArrayBuffer]") {
dict = new Uint8Array(opt.dictionary);
} else {
dict = opt.dictionary;
}
status = deflate_1$2.deflateSetDictionary(this.strm, dict);
if (status !== Z_OK$2) {
throw new Error(messages[status]);
}
this._dict_set = true;
}
}
Deflate$1.prototype.push = function(data, flush_mode) {
const strm = this.strm;
const chunkSize = this.options.chunkSize;
let status, _flush_mode;
if (this.ended) {
return false;
}
if (flush_mode === ~~flush_mode) _flush_mode = flush_mode;
else _flush_mode = flush_mode === true ? Z_FINISH$2 : Z_NO_FLUSH$1;
if (typeof data === "string") {
strm.input = strings.string2buf(data);
} else if (toString$1.call(data) === "[object ArrayBuffer]") {
strm.input = new Uint8Array(data);
} else {
strm.input = data;
}
strm.next_in = 0;
strm.avail_in = strm.input.length;
for (; ; ) {
if (strm.avail_out === 0) {
strm.output = new Uint8Array(chunkSize);
strm.next_out = 0;
strm.avail_out = chunkSize;
}
if ((_flush_mode === Z_SYNC_FLUSH || _flush_mode === Z_FULL_FLUSH) && strm.avail_out <= 6) {
this.onData(strm.output.subarray(0, strm.next_out));
strm.avail_out = 0;
continue;
}
status = deflate_1$2.deflate(strm, _flush_mode);
if (status === Z_STREAM_END$2) {
if (strm.next_out > 0) {
this.onData(strm.output.subarray(0, strm.next_out));
}
status = deflate_1$2.deflateEnd(this.strm);
this.onEnd(status);
this.ended = true;
return status === Z_OK$2;
}
if (strm.avail_out === 0) {
this.onData(strm.output);
continue;
}
if (_flush_mode > 0 && strm.next_out > 0) {
this.onData(strm.output.subarray(0, strm.next_out));
strm.avail_out = 0;
continue;
}
if (strm.avail_in === 0) break;
}
return true;
};
Deflate$1.prototype.onData = function(chunk) {
this.chunks.push(chunk);
};
Deflate$1.prototype.onEnd = function(status) {
if (status === Z_OK$2) {
this.result = common.flattenChunks(this.chunks);
}
this.chunks = [];
this.err = status;
this.msg = this.strm.msg;
};
function deflate$1(input, options) {
const deflator = new Deflate$1(options);
deflator.push(input, true);
if (deflator.err) {
throw deflator.msg || messages[deflator.err];
}
return deflator.result;
}
function deflateRaw$1(input, options) {
options = options || {};
options.raw = true;
return deflate$1(input, options);
}
function gzip$1(input, options) {
options = options || {};
options.gzip = true;
return deflate$1(input, options);
}
var Deflate_1$1 = Deflate$1;
var deflate_2 = deflate$1;
var deflateRaw_1$1 = deflateRaw$1;
var gzip_1$1 = gzip$1;
var constants$1 = constants$2;
var deflate_1$1 = {
Deflate: Deflate_1$1,
deflate: deflate_2,
deflateRaw: deflateRaw_1$1,
gzip: gzip_1$1,
constants: constants$1
};
var BAD$1 = 16209;
var TYPE$1 = 16191;
var inffast = function inflate_fast(strm, start) {
let _in;
let last;
let _out;
let beg;
let end;
let dmax;
let wsize;
let whave;
let wnext;
let s_window;
let hold;
let bits;
let lcode;
let dcode;
let lmask;
let dmask;
let here;
let op;
let len;
let dist;
let from;
let from_source;
let input, output;
const state = strm.state;
_in = strm.next_in;
input = strm.input;
last = _in + (strm.avail_in - 5);
_out = strm.next_out;
output = strm.output;
beg = _out - (start - strm.avail_out);
end = _out + (strm.avail_out - 257);
dmax = state.dmax;
wsize = state.wsize;
whave = state.whave;
wnext = state.wnext;
s_window = state.window;
hold = state.hold;
bits = state.bits;
lcode = state.lencode;
dcode = state.distcode;
lmask = (1 << state.lenbits) - 1;
dmask = (1 << state.distbits) - 1;
top:
do {
if (bits < 15) {
hold += input[_in++] << bits;
bits += 8;
hold += input[_in++] << bits;
bits += 8;
}
here = lcode[hold & lmask];
dolen:
for (; ; ) {
op = here >>> 24;
hold >>>= op;
bits -= op;
op = here >>> 16 & 255;
if (op === 0) {
output[_out++] = here & 65535;
} else if (op & 16) {
len = here & 65535;
op &= 15;
if (op) {
if (bits < op) {
hold += input[_in++] << bits;
bits += 8;
}
len += hold & (1 << op) - 1;
hold >>>= op;
bits -= op;
}
if (bits < 15) {
hold += input[_in++] << bits;
bits += 8;
hold += input[_in++] << bits;
bits += 8;
}
here = dcode[hold & dmask];
dodist:
for (; ; ) {
op = here >>> 24;
hold >>>= op;
bits -= op;
op = here >>> 16 & 255;
if (op & 16) {
dist = here & 65535;
op &= 15;
if (bits < op) {
hold += input[_in++] << bits;
bits += 8;
if (bits < op) {
hold += input[_in++] << bits;
bits += 8;
}
}
dist += hold & (1 << op) - 1;
if (dist > dmax) {
strm.msg = "invalid distance too far back";
state.mode = BAD$1;
break top;
}
hold >>>= op;
bits -= op;
op = _out - beg;
if (dist > op) {
op = dist - op;
if (op > whave) {
if (state.sane) {
strm.msg = "invalid distance too far back";
state.mode = BAD$1;
break top;
}
}
from = 0;
from_source = s_window;
if (wnext === 0) {
from += wsize - op;
if (op < len) {
len -= op;
do {
output[_out++] = s_window[from++];
} while (--op);
from = _out - dist;
from_source = output;
}
} else if (wnext < op) {
from += wsize + wnext - op;
op -= wnext;
if (op < len) {
len -= op;
do {
output[_out++] = s_window[from++];
} while (--op);
from = 0;
if (wnext < len) {
op = wnext;
len -= op;
do {
output[_out++] = s_window[from++];
} while (--op);
from = _out - dist;
from_source = output;
}
}
} else {
from += wnext - op;
if (op < len) {
len -= op;
do {
output[_out++] = s_window[from++];
} while (--op);
from = _out - dist;
from_source = output;
}
}
while (len > 2) {
output[_out++] = from_source[from++];
output[_out++] = from_source[from++];
output[_out++] = from_source[from++];
len -= 3;
}
if (len) {
output[_out++] = from_source[from++];
if (len > 1) {
output[_out++] = from_source[from++];
}
}
} else {
from = _out - dist;
do {
output[_out++] = output[from++];
output[_out++] = output[from++];
output[_out++] = output[from++];
len -= 3;
} while (len > 2);
if (len) {
output[_out++] = output[from++];
if (len > 1) {
output[_out++] = output[from++];
}
}
}
} else if ((op & 64) === 0) {
here = dcode[(here & 65535) + (hold & (1 << op) - 1)];
continue dodist;
} else {
strm.msg = "invalid distance code";
state.mode = BAD$1;
break top;
}
break;
}
} else if ((op & 64) === 0) {
here = lcode[(here & 65535) + (hold & (1 << op) - 1)];
continue dolen;
} else if (op & 32) {
state.mode = TYPE$1;
break top;
} else {
strm.msg = "invalid literal/length code";
state.mode = BAD$1;
break top;
}
break;
}
} while (_in < last && _out < end);
len = bits >> 3;
_in -= len;
bits -= len << 3;
hold &= (1 << bits) - 1;
strm.next_in = _in;
strm.next_out = _out;
strm.avail_in = _in < last ? 5 + (last - _in) : 5 - (_in - last);
strm.avail_out = _out < end ? 257 + (end - _out) : 257 - (_out - end);
state.hold = hold;
state.bits = bits;
return;
};
var MAXBITS = 15;
var ENOUGH_LENS$1 = 852;
var ENOUGH_DISTS$1 = 592;
var CODES$1 = 0;
var LENS$1 = 1;
var DISTS$1 = 2;
var lbase = new Uint16Array([
/* Length codes 257..285 base */
3,
4,
5,
6,
7,
8,
9,
10,
11,
13,
15,
17,
19,
23,
27,
31,
35,
43,
51,
59,
67,
83,
99,
115,
131,
163,
195,
227,
258,
0,
0
]);
var lext = new Uint8Array([
/* Length codes 257..285 extra */
16,
16,
16,
16,
16,
16,
16,
16,
17,
17,
17,
17,
18,
18,
18,
18,
19,
19,
19,
19,
20,
20,
20,
20,
21,
21,
21,
21,
16,
72,
78
]);
var dbase = new Uint16Array([
/* Distance codes 0..29 base */
1,
2,
3,
4,
5,
7,
9,
13,
17,
25,
33,
49,
65,
97,
129,
193,
257,
385,
513,
769,
1025,
1537,
2049,
3073,
4097,
6145,
8193,
12289,
16385,
24577,
0,
0
]);
var dext = new Uint8Array([
/* Distance codes 0..29 extra */
16,
16,
16,
16,
17,
17,
18,
18,
19,
19,
20,
20,
21,
21,
22,
22,
23,
23,
24,
24,
25,
25,
26,
26,
27,
27,
28,
28,
29,
29,
64,
64
]);
var inflate_table = (type, lens, lens_index, codes, table, table_index, work, opts) => {
const bits = opts.bits;
let len = 0;
let sym = 0;
let min = 0, max = 0;
let root = 0;
let curr = 0;
let drop = 0;
let left = 0;
let used = 0;
let huff = 0;
let incr;
let fill;
let low;
let mask;
let next;
let base = null;
let match;
const count = new Uint16Array(MAXBITS + 1);
const offs = new Uint16Array(MAXBITS + 1);
let extra = null;
let here_bits, here_op, here_val;
for (len = 0; len <= MAXBITS; len++) {
count[len] = 0;
}
for (sym = 0; sym < codes; sym++) {
count[lens[lens_index + sym]]++;
}
root = bits;
for (max = MAXBITS; max >= 1; max--) {
if (count[max] !== 0) {
break;
}
}
if (root > max) {
root = max;
}
if (max === 0) {
table[table_index++] = 1 << 24 | 64 << 16 | 0;
table[table_index++] = 1 << 24 | 64 << 16 | 0;
opts.bits = 1;
return 0;
}
for (min = 1; min < max; min++) {
if (count[min] !== 0) {
break;
}
}
if (root < min) {
root = min;
}
left = 1;
for (len = 1; len <= MAXBITS; len++) {
left <<= 1;
left -= count[len];
if (left < 0) {
return -1;
}
}
if (left > 0 && (type === CODES$1 || max !== 1)) {
return -1;
}
offs[1] = 0;
for (len = 1; len < MAXBITS; len++) {
offs[len + 1] = offs[len] + count[len];
}
for (sym = 0; sym < codes; sym++) {
if (lens[lens_index + sym] !== 0) {
work[offs[lens[lens_index + sym]]++] = sym;
}
}
if (type === CODES$1) {
base = extra = work;
match = 20;
} else if (type === LENS$1) {
base = lbase;
extra = lext;
match = 257;
} else {
base = dbase;
extra = dext;
match = 0;
}
huff = 0;
sym = 0;
len = min;
next = table_index;
curr = root;
drop = 0;
low = -1;
used = 1 << root;
mask = used - 1;
if (type === LENS$1 && used > ENOUGH_LENS$1 || type === DISTS$1 && used > ENOUGH_DISTS$1) {
return 1;
}
for (; ; ) {
here_bits = len - drop;
if (work[sym] + 1 < match) {
here_op = 0;
here_val = work[sym];
} else if (work[sym] >= match) {
here_op = extra[work[sym] - match];
here_val = base[work[sym] - match];
} else {
here_op = 32 + 64;
here_val = 0;
}
incr = 1 << len - drop;
fill = 1 << curr;
min = fill;
do {
fill -= incr;
table[next + (huff >> drop) + fill] = here_bits << 24 | here_op << 16 | here_val | 0;
} while (fill !== 0);
incr = 1 << len - 1;
while (huff & incr) {
incr >>= 1;
}
if (incr !== 0) {
huff &= incr - 1;
huff += incr;
} else {
huff = 0;
}
sym++;
if (--count[len] === 0) {
if (len === max) {
break;
}
len = lens[lens_index + work[sym]];
}
if (len > root && (huff & mask) !== low) {
if (drop === 0) {
drop = root;
}
next += min;
curr = len - drop;
left = 1 << curr;
while (curr + drop < max) {
left -= count[curr + drop];
if (left <= 0) {
break;
}
curr++;
left <<= 1;
}
used += 1 << curr;
if (type === LENS$1 && used > ENOUGH_LENS$1 || type === DISTS$1 && used > ENOUGH_DISTS$1) {
return 1;
}
low = huff & mask;
table[low] = root << 24 | curr << 16 | next - table_index | 0;
}
}
if (huff !== 0) {
table[next + huff] = len - drop << 24 | 64 << 16 | 0;
}
opts.bits = root;
return 0;
};
var inftrees = inflate_table;
var CODES = 0;
var LENS = 1;
var DISTS = 2;
var {
Z_FINISH: Z_FINISH$1,
Z_BLOCK,
Z_TREES,
Z_OK: Z_OK$1,
Z_STREAM_END: Z_STREAM_END$1,
Z_NEED_DICT: Z_NEED_DICT$1,
Z_STREAM_ERROR: Z_STREAM_ERROR$1,
Z_DATA_ERROR: Z_DATA_ERROR$1,
Z_MEM_ERROR: Z_MEM_ERROR$1,
Z_BUF_ERROR,
Z_DEFLATED
} = constants$2;
var HEAD = 16180;
var FLAGS = 16181;
var TIME = 16182;
var OS = 16183;
var EXLEN = 16184;
var EXTRA = 16185;
var NAME = 16186;
var COMMENT = 16187;
var HCRC = 16188;
var DICTID = 16189;
var DICT = 16190;
var TYPE = 16191;
var TYPEDO = 16192;
var STORED = 16193;
var COPY_ = 16194;
var COPY = 16195;
var TABLE = 16196;
var LENLENS = 16197;
var CODELENS = 16198;
var LEN_ = 16199;
var LEN = 16200;
var LENEXT = 16201;
var DIST = 16202;
var DISTEXT = 16203;
var MATCH = 16204;
var LIT = 16205;
var CHECK = 16206;
var LENGTH = 16207;
var DONE = 16208;
var BAD = 16209;
var MEM = 16210;
var SYNC = 16211;
var ENOUGH_LENS = 852;
var ENOUGH_DISTS = 592;
var MAX_WBITS = 15;
var DEF_WBITS = MAX_WBITS;
var zswap32 = (q) => {
return (q >>> 24 & 255) + (q >>> 8 & 65280) + ((q & 65280) << 8) + ((q & 255) << 24);
};
function InflateState() {
this.strm = null;
this.mode = 0;
this.last = false;
this.wrap = 0;
this.havedict = false;
this.flags = 0;
this.dmax = 0;
this.check = 0;
this.total = 0;
this.head = null;
this.wbits = 0;
this.wsize = 0;
this.whave = 0;
this.wnext = 0;
this.window = null;
this.hold = 0;
this.bits = 0;
this.length = 0;
this.offset = 0;
this.extra = 0;
this.lencode = null;
this.distcode = null;
this.lenbits = 0;
this.distbits = 0;
this.ncode = 0;
this.nlen = 0;
this.ndist = 0;
this.have = 0;
this.next = null;
this.lens = new Uint16Array(320);
this.work = new Uint16Array(288);
this.lendyn = null;
this.distdyn = null;
this.sane = 0;
this.back = 0;
this.was = 0;
}
var inflateStateCheck = (strm) => {
if (!strm) {
return 1;
}
const state = strm.state;
if (!state || state.strm !== strm || state.mode < HEAD || state.mode > SYNC) {
return 1;
}
return 0;
};
var inflateResetKeep = (strm) => {
if (inflateStateCheck(strm)) {
return Z_STREAM_ERROR$1;
}
const state = strm.state;
strm.total_in = strm.total_out = state.total = 0;
strm.msg = "";
if (state.wrap) {
strm.adler = state.wrap & 1;
}
state.mode = HEAD;
state.last = 0;
state.havedict = 0;
state.flags = -1;
state.dmax = 32768;
state.head = null;
state.hold = 0;
state.bits = 0;
state.lencode = state.lendyn = new Int32Array(ENOUGH_LENS);
state.distcode = state.distdyn = new Int32Array(ENOUGH_DISTS);
state.sane = 1;
state.back = -1;
return Z_OK$1;
};
var inflateReset = (strm) => {
if (inflateStateCheck(strm)) {
return Z_STREAM_ERROR$1;
}
const state = strm.state;
state.wsize = 0;
state.whave = 0;
state.wnext = 0;
return inflateResetKeep(strm);
};
var inflateReset2 = (strm, windowBits) => {
let wrap;
if (inflateStateCheck(strm)) {
return Z_STREAM_ERROR$1;
}
const state = strm.state;
if (windowBits < 0) {
wrap = 0;
windowBits = -windowBits;
} else {
wrap = (windowBits >> 4) + 5;
if (windowBits < 48) {
windowBits &= 15;
}
}
if (windowBits && (windowBits < 8 || windowBits > 15)) {
return Z_STREAM_ERROR$1;
}
if (state.window !== null && state.wbits !== windowBits) {
state.window = null;
}
state.wrap = wrap;
state.wbits = windowBits;
return inflateReset(strm);
};
var inflateInit2 = (strm, windowBits) => {
if (!strm) {
return Z_STREAM_ERROR$1;
}
const state = new InflateState();
strm.state = state;
state.strm = strm;
state.window = null;
state.mode = HEAD;
const ret = inflateReset2(strm, windowBits);
if (ret !== Z_OK$1) {
strm.state = null;
}
return ret;
};
var inflateInit = (strm) => {
return inflateInit2(strm, DEF_WBITS);
};
var virgin = true;
var lenfix;
var distfix;
var fixedtables = (state) => {
if (virgin) {
lenfix = new Int32Array(512);
distfix = new Int32Array(32);
let sym = 0;
while (sym < 144) {
state.lens[sym++] = 8;
}
while (sym < 256) {
state.lens[sym++] = 9;
}
while (sym < 280) {
state.lens[sym++] = 7;
}
while (sym < 288) {
state.lens[sym++] = 8;
}
inftrees(LENS, state.lens, 0, 288, lenfix, 0, state.work, { bits: 9 });
sym = 0;
while (sym < 32) {
state.lens[sym++] = 5;
}
inftrees(DISTS, state.lens, 0, 32, distfix, 0, state.work, { bits: 5 });
virgin = false;
}
state.lencode = lenfix;
state.lenbits = 9;
state.distcode = distfix;
state.distbits = 5;
};
var updatewindow = (strm, src, end, copy) => {
let dist;
const state = strm.state;
if (state.window === null) {
state.wsize = 1 << state.wbits;
state.wnext = 0;
state.whave = 0;
state.window = new Uint8Array(state.wsize);
}
if (copy >= state.wsize) {
state.window.set(src.subarray(end - state.wsize, end), 0);
state.wnext = 0;
state.whave = state.wsize;
} else {
dist = state.wsize - state.wnext;
if (dist > copy) {
dist = copy;
}
state.window.set(src.subarray(end - copy, end - copy + dist), state.wnext);
copy -= dist;
if (copy) {
state.window.set(src.subarray(end - copy, end), 0);
state.wnext = copy;
state.whave = state.wsize;
} else {
state.wnext += dist;
if (state.wnext === state.wsize) {
state.wnext = 0;
}
if (state.whave < state.wsize) {
state.whave += dist;
}
}
}
return 0;
};
var inflate$2 = (strm, flush) => {
let state;
let input, output;
let next;
let put;
let have, left;
let hold;
let bits;
let _in, _out;
let copy;
let from;
let from_source;
let here = 0;
let here_bits, here_op, here_val;
let last_bits, last_op, last_val;
let len;
let ret;
const hbuf = new Uint8Array(4);
let opts;
let n;
const order = (
/* permutation of code lengths */
new Uint8Array([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15])
);
if (inflateStateCheck(strm) || !strm.output || !strm.input && strm.avail_in !== 0) {
return Z_STREAM_ERROR$1;
}
state = strm.state;
if (state.mode === TYPE) {
state.mode = TYPEDO;
}
put = strm.next_out;
output = strm.output;
left = strm.avail_out;
next = strm.next_in;
input = strm.input;
have = strm.avail_in;
hold = state.hold;
bits = state.bits;
_in = have;
_out = left;
ret = Z_OK$1;
inf_leave:
for (; ; ) {
switch (state.mode) {
case HEAD:
if (state.wrap === 0) {
state.mode = TYPEDO;
break;
}
while (bits < 16) {
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
if (state.wrap & 2 && hold === 35615) {
if (state.wbits === 0) {
state.wbits = 15;
}
state.check = 0;
hbuf[0] = hold & 255;
hbuf[1] = hold >>> 8 & 255;
state.check = crc32_1(state.check, hbuf, 2, 0);
hold = 0;
bits = 0;
state.mode = FLAGS;
break;
}
if (state.head) {
state.head.done = false;
}
if (!(state.wrap & 1) || /* check if zlib header allowed */
(((hold & 255) << 8) + (hold >> 8)) % 31) {
strm.msg = "incorrect header check";
state.mode = BAD;
break;
}
if ((hold & 15) !== Z_DEFLATED) {
strm.msg = "unknown compression method";
state.mode = BAD;
break;
}
hold >>>= 4;
bits -= 4;
len = (hold & 15) + 8;
if (state.wbits === 0) {
state.wbits = len;
}
if (len > 15 || len > state.wbits) {
strm.msg = "invalid window size";
state.mode = BAD;
break;
}
state.dmax = 1 << state.wbits;
state.flags = 0;
strm.adler = state.check = 1;
state.mode = hold & 512 ? DICTID : TYPE;
hold = 0;
bits = 0;
break;
case FLAGS:
while (bits < 16) {
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
state.flags = hold;
if ((state.flags & 255) !== Z_DEFLATED) {
strm.msg = "unknown compression method";
state.mode = BAD;
break;
}
if (state.flags & 57344) {
strm.msg = "unknown header flags set";
state.mode = BAD;
break;
}
if (state.head) {
state.head.text = hold >> 8 & 1;
}
if (state.flags & 512 && state.wrap & 4) {
hbuf[0] = hold & 255;
hbuf[1] = hold >>> 8 & 255;
state.check = crc32_1(state.check, hbuf, 2, 0);
}
hold = 0;
bits = 0;
state.mode = TIME;
/* falls through */
case TIME:
while (bits < 32) {
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
if (state.head) {
state.head.time = hold;
}
if (state.flags & 512 && state.wrap & 4) {
hbuf[0] = hold & 255;
hbuf[1] = hold >>> 8 & 255;
hbuf[2] = hold >>> 16 & 255;
hbuf[3] = hold >>> 24 & 255;
state.check = crc32_1(state.check, hbuf, 4, 0);
}
hold = 0;
bits = 0;
state.mode = OS;
/* falls through */
case OS:
while (bits < 16) {
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
if (state.head) {
state.head.xflags = hold & 255;
state.head.os = hold >> 8;
}
if (state.flags & 512 && state.wrap & 4) {
hbuf[0] = hold & 255;
hbuf[1] = hold >>> 8 & 255;
state.check = crc32_1(state.check, hbuf, 2, 0);
}
hold = 0;
bits = 0;
state.mode = EXLEN;
/* falls through */
case EXLEN:
if (state.flags & 1024) {
while (bits < 16) {
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
state.length = hold;
if (state.head) {
state.head.extra_len = hold;
}
if (state.flags & 512 && state.wrap & 4) {
hbuf[0] = hold & 255;
hbuf[1] = hold >>> 8 & 255;
state.check = crc32_1(state.check, hbuf, 2, 0);
}
hold = 0;
bits = 0;
} else if (state.head) {
state.head.extra = null;
}
state.mode = EXTRA;
/* falls through */
case EXTRA:
if (state.flags & 1024) {
copy = state.length;
if (copy > have) {
copy = have;
}
if (copy) {
if (state.head) {
len = state.head.extra_len - state.length;
if (!state.head.extra) {
state.head.extra = new Uint8Array(state.head.extra_len);
}
state.head.extra.set(
input.subarray(
next,
// extra field is limited to 65536 bytes
// - no need for additional size check
next + copy
),
/*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/
len
);
}
if (state.flags & 512 && state.wrap & 4) {
state.check = crc32_1(state.check, input, copy, next);
}
have -= copy;
next += copy;
state.length -= copy;
}
if (state.length) {
break inf_leave;
}
}
state.length = 0;
state.mode = NAME;
/* falls through */
case NAME:
if (state.flags & 2048) {
if (have === 0) {
break inf_leave;
}
copy = 0;
do {
len = input[next + copy++];
if (state.head && len && state.length < 65536) {
state.head.name += String.fromCharCode(len);
}
} while (len && copy < have);
if (state.flags & 512 && state.wrap & 4) {
state.check = crc32_1(state.check, input, copy, next);
}
have -= copy;
next += copy;
if (len) {
break inf_leave;
}
} else if (state.head) {
state.head.name = null;
}
state.length = 0;
state.mode = COMMENT;
/* falls through */
case COMMENT:
if (state.flags & 4096) {
if (have === 0) {
break inf_leave;
}
copy = 0;
do {
len = input[next + copy++];
if (state.head && len && state.length < 65536) {
state.head.comment += String.fromCharCode(len);
}
} while (len && copy < have);
if (state.flags & 512 && state.wrap & 4) {
state.check = crc32_1(state.check, input, copy, next);
}
have -= copy;
next += copy;
if (len) {
break inf_leave;
}
} else if (state.head) {
state.head.comment = null;
}
state.mode = HCRC;
/* falls through */
case HCRC:
if (state.flags & 512) {
while (bits < 16) {
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
if (state.wrap & 4 && hold !== (state.check & 65535)) {
strm.msg = "header crc mismatch";
state.mode = BAD;
break;
}
hold = 0;
bits = 0;
}
if (state.head) {
state.head.hcrc = state.flags >> 9 & 1;
state.head.done = true;
}
strm.adler = state.check = 0;
state.mode = TYPE;
break;
case DICTID:
while (bits < 32) {
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
strm.adler = state.check = zswap32(hold);
hold = 0;
bits = 0;
state.mode = DICT;
/* falls through */
case DICT:
if (state.havedict === 0) {
strm.next_out = put;
strm.avail_out = left;
strm.next_in = next;
strm.avail_in = have;
state.hold = hold;
state.bits = bits;
return Z_NEED_DICT$1;
}
strm.adler = state.check = 1;
state.mode = TYPE;
/* falls through */
case TYPE:
if (flush === Z_BLOCK || flush === Z_TREES) {
break inf_leave;
}
/* falls through */
case TYPEDO:
if (state.last) {
hold >>>= bits & 7;
bits -= bits & 7;
state.mode = CHECK;
break;
}
while (bits < 3) {
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
state.last = hold & 1;
hold >>>= 1;
bits -= 1;
switch (hold & 3) {
case 0:
state.mode = STORED;
break;
case 1:
fixedtables(state);
state.mode = LEN_;
if (flush === Z_TREES) {
hold >>>= 2;
bits -= 2;
break inf_leave;
}
break;
case 2:
state.mode = TABLE;
break;
case 3:
strm.msg = "invalid block type";
state.mode = BAD;
}
hold >>>= 2;
bits -= 2;
break;
case STORED:
hold >>>= bits & 7;
bits -= bits & 7;
while (bits < 32) {
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
if ((hold & 65535) !== (hold >>> 16 ^ 65535)) {
strm.msg = "invalid stored block lengths";
state.mode = BAD;
break;
}
state.length = hold & 65535;
hold = 0;
bits = 0;
state.mode = COPY_;
if (flush === Z_TREES) {
break inf_leave;
}
/* falls through */
case COPY_:
state.mode = COPY;
/* falls through */
case COPY:
copy = state.length;
if (copy) {
if (copy > have) {
copy = have;
}
if (copy > left) {
copy = left;
}
if (copy === 0) {
break inf_leave;
}
output.set(input.subarray(next, next + copy), put);
have -= copy;
next += copy;
left -= copy;
put += copy;
state.length -= copy;
break;
}
state.mode = TYPE;
break;
case TABLE:
while (bits < 14) {
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
state.nlen = (hold & 31) + 257;
hold >>>= 5;
bits -= 5;
state.ndist = (hold & 31) + 1;
hold >>>= 5;
bits -= 5;
state.ncode = (hold & 15) + 4;
hold >>>= 4;
bits -= 4;
if (state.nlen > 286 || state.ndist > 30) {
strm.msg = "too many length or distance symbols";
state.mode = BAD;
break;
}
state.have = 0;
state.mode = LENLENS;
/* falls through */
case LENLENS:
while (state.have < state.ncode) {
while (bits < 3) {
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
state.lens[order[state.have++]] = hold & 7;
hold >>>= 3;
bits -= 3;
}
while (state.have < 19) {
state.lens[order[state.have++]] = 0;
}
state.lencode = state.lendyn;
state.lenbits = 7;
opts = { bits: state.lenbits };
ret = inftrees(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts);
state.lenbits = opts.bits;
if (ret) {
strm.msg = "invalid code lengths set";
state.mode = BAD;
break;
}
state.have = 0;
state.mode = CODELENS;
/* falls through */
case CODELENS:
while (state.have < state.nlen + state.ndist) {
for (; ; ) {
here = state.lencode[hold & (1 << state.lenbits) - 1];
here_bits = here >>> 24;
here_op = here >>> 16 & 255;
here_val = here & 65535;
if (here_bits <= bits) {
break;
}
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
if (here_val < 16) {
hold >>>= here_bits;
bits -= here_bits;
state.lens[state.have++] = here_val;
} else {
if (here_val === 16) {
n = here_bits + 2;
while (bits < n) {
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
hold >>>= here_bits;
bits -= here_bits;
if (state.have === 0) {
strm.msg = "invalid bit length repeat";
state.mode = BAD;
break;
}
len = state.lens[state.have - 1];
copy = 3 + (hold & 3);
hold >>>= 2;
bits -= 2;
} else if (here_val === 17) {
n = here_bits + 3;
while (bits < n) {
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
hold >>>= here_bits;
bits -= here_bits;
len = 0;
copy = 3 + (hold & 7);
hold >>>= 3;
bits -= 3;
} else {
n = here_bits + 7;
while (bits < n) {
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
hold >>>= here_bits;
bits -= here_bits;
len = 0;
copy = 11 + (hold & 127);
hold >>>= 7;
bits -= 7;
}
if (state.have + copy > state.nlen + state.ndist) {
strm.msg = "invalid bit length repeat";
state.mode = BAD;
break;
}
while (copy--) {
state.lens[state.have++] = len;
}
}
}
if (state.mode === BAD) {
break;
}
if (state.lens[256] === 0) {
strm.msg = "invalid code -- missing end-of-block";
state.mode = BAD;
break;
}
state.lenbits = 9;
opts = { bits: state.lenbits };
ret = inftrees(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts);
state.lenbits = opts.bits;
if (ret) {
strm.msg = "invalid literal/lengths set";
state.mode = BAD;
break;
}
state.distbits = 6;
state.distcode = state.distdyn;
opts = { bits: state.distbits };
ret = inftrees(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts);
state.distbits = opts.bits;
if (ret) {
strm.msg = "invalid distances set";
state.mode = BAD;
break;
}
state.mode = LEN_;
if (flush === Z_TREES) {
break inf_leave;
}
/* falls through */
case LEN_:
state.mode = LEN;
/* falls through */
case LEN:
if (have >= 6 && left >= 258) {
strm.next_out = put;
strm.avail_out = left;
strm.next_in = next;
strm.avail_in = have;
state.hold = hold;
state.bits = bits;
inffast(strm, _out);
put = strm.next_out;
output = strm.output;
left = strm.avail_out;
next = strm.next_in;
input = strm.input;
have = strm.avail_in;
hold = state.hold;
bits = state.bits;
if (state.mode === TYPE) {
state.back = -1;
}
break;
}
state.back = 0;
for (; ; ) {
here = state.lencode[hold & (1 << state.lenbits) - 1];
here_bits = here >>> 24;
here_op = here >>> 16 & 255;
here_val = here & 65535;
if (here_bits <= bits) {
break;
}
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
if (here_op && (here_op & 240) === 0) {
last_bits = here_bits;
last_op = here_op;
last_val = here_val;
for (; ; ) {
here = state.lencode[last_val + ((hold & (1 << last_bits + last_op) - 1) >> last_bits)];
here_bits = here >>> 24;
here_op = here >>> 16 & 255;
here_val = here & 65535;
if (last_bits + here_bits <= bits) {
break;
}
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
hold >>>= last_bits;
bits -= last_bits;
state.back += last_bits;
}
hold >>>= here_bits;
bits -= here_bits;
state.back += here_bits;
state.length = here_val;
if (here_op === 0) {
state.mode = LIT;
break;
}
if (here_op & 32) {
state.back = -1;
state.mode = TYPE;
break;
}
if (here_op & 64) {
strm.msg = "invalid literal/length code";
state.mode = BAD;
break;
}
state.extra = here_op & 15;
state.mode = LENEXT;
/* falls through */
case LENEXT:
if (state.extra) {
n = state.extra;
while (bits < n) {
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
state.length += hold & (1 << state.extra) - 1;
hold >>>= state.extra;
bits -= state.extra;
state.back += state.extra;
}
state.was = state.length;
state.mode = DIST;
/* falls through */
case DIST:
for (; ; ) {
here = state.distcode[hold & (1 << state.distbits) - 1];
here_bits = here >>> 24;
here_op = here >>> 16 & 255;
here_val = here & 65535;
if (here_bits <= bits) {
break;
}
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
if ((here_op & 240) === 0) {
last_bits = here_bits;
last_op = here_op;
last_val = here_val;
for (; ; ) {
here = state.distcode[last_val + ((hold & (1 << last_bits + last_op) - 1) >> last_bits)];
here_bits = here >>> 24;
here_op = here >>> 16 & 255;
here_val = here & 65535;
if (last_bits + here_bits <= bits) {
break;
}
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
hold >>>= last_bits;
bits -= last_bits;
state.back += last_bits;
}
hold >>>= here_bits;
bits -= here_bits;
state.back += here_bits;
if (here_op & 64) {
strm.msg = "invalid distance code";
state.mode = BAD;
break;
}
state.offset = here_val;
state.extra = here_op & 15;
state.mode = DISTEXT;
/* falls through */
case DISTEXT:
if (state.extra) {
n = state.extra;
while (bits < n) {
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
state.offset += hold & (1 << state.extra) - 1;
hold >>>= state.extra;
bits -= state.extra;
state.back += state.extra;
}
if (state.offset > state.dmax) {
strm.msg = "invalid distance too far back";
state.mode = BAD;
break;
}
state.mode = MATCH;
/* falls through */
case MATCH:
if (left === 0) {
break inf_leave;
}
copy = _out - left;
if (state.offset > copy) {
copy = state.offset - copy;
if (copy > state.whave) {
if (state.sane) {
strm.msg = "invalid distance too far back";
state.mode = BAD;
break;
}
}
if (copy > state.wnext) {
copy -= state.wnext;
from = state.wsize - copy;
} else {
from = state.wnext - copy;
}
if (copy > state.length) {
copy = state.length;
}
from_source = state.window;
} else {
from_source = output;
from = put - state.offset;
copy = state.length;
}
if (copy > left) {
copy = left;
}
left -= copy;
state.length -= copy;
do {
output[put++] = from_source[from++];
} while (--copy);
if (state.length === 0) {
state.mode = LEN;
}
break;
case LIT:
if (left === 0) {
break inf_leave;
}
output[put++] = state.length;
left--;
state.mode = LEN;
break;
case CHECK:
if (state.wrap) {
while (bits < 32) {
if (have === 0) {
break inf_leave;
}
have--;
hold |= input[next++] << bits;
bits += 8;
}
_out -= left;
strm.total_out += _out;
state.total += _out;
if (state.wrap & 4 && _out) {
strm.adler = state.check = /*UPDATE_CHECK(state.check, put - _out, _out);*/
state.flags ? crc32_1(state.check, output, _out, put - _out) : adler32_1(state.check, output, _out, put - _out);
}
_out = left;
if (state.wrap & 4 && (state.flags ? hold : zswap32(hold)) !== state.check) {
strm.msg = "incorrect data check";
state.mode = BAD;
break;
}
hold = 0;
bits = 0;
}
state.mode = LENGTH;
/* falls through */
case LENGTH:
if (state.wrap && state.flags) {
while (bits < 32) {
if (have === 0) {
break inf_leave;
}
have--;
hold += input[next++] << bits;
bits += 8;
}
if (state.wrap & 4 && hold !== (state.total & 4294967295)) {
strm.msg = "incorrect length check";
state.mode = BAD;
break;
}
hold = 0;
bits = 0;
}
state.mode = DONE;
/* falls through */
case DONE:
ret = Z_STREAM_END$1;
break inf_leave;
case BAD:
ret = Z_DATA_ERROR$1;
break inf_leave;
case MEM:
return Z_MEM_ERROR$1;
case SYNC:
/* falls through */
default:
return Z_STREAM_ERROR$1;
}
}
strm.next_out = put;
strm.avail_out = left;
strm.next_in = next;
strm.avail_in = have;
state.hold = hold;
state.bits = bits;
if (state.wsize || _out !== strm.avail_out && state.mode < BAD && (state.mode < CHECK || flush !== Z_FINISH$1)) {
if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) ;
}
_in -= strm.avail_in;
_out -= strm.avail_out;
strm.total_in += _in;
strm.total_out += _out;
state.total += _out;
if (state.wrap & 4 && _out) {
strm.adler = state.check = /*UPDATE_CHECK(state.check, strm.next_out - _out, _out);*/
state.flags ? crc32_1(state.check, output, _out, strm.next_out - _out) : adler32_1(state.check, output, _out, strm.next_out - _out);
}
strm.data_type = state.bits + (state.last ? 64 : 0) + (state.mode === TYPE ? 128 : 0) + (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0);
if ((_in === 0 && _out === 0 || flush === Z_FINISH$1) && ret === Z_OK$1) {
ret = Z_BUF_ERROR;
}
return ret;
};
var inflateEnd = (strm) => {
if (inflateStateCheck(strm)) {
return Z_STREAM_ERROR$1;
}
let state = strm.state;
if (state.window) {
state.window = null;
}
strm.state = null;
return Z_OK$1;
};
var inflateGetHeader = (strm, head) => {
if (inflateStateCheck(strm)) {
return Z_STREAM_ERROR$1;
}
const state = strm.state;
if ((state.wrap & 2) === 0) {
return Z_STREAM_ERROR$1;
}
state.head = head;
head.done = false;
return Z_OK$1;
};
var inflateSetDictionary = (strm, dictionary) => {
const dictLength = dictionary.length;
let state;
let dictid;
let ret;
if (inflateStateCheck(strm)) {
return Z_STREAM_ERROR$1;
}
state = strm.state;
if (state.wrap !== 0 && state.mode !== DICT) {
return Z_STREAM_ERROR$1;
}
if (state.mode === DICT) {
dictid = 1;
dictid = adler32_1(dictid, dictionary, dictLength, 0);
if (dictid !== state.check) {
return Z_DATA_ERROR$1;
}
}
ret = updatewindow(strm, dictionary, dictLength, dictLength);
if (ret) {
state.mode = MEM;
return Z_MEM_ERROR$1;
}
state.havedict = 1;
return Z_OK$1;
};
var inflateReset_1 = inflateReset;
var inflateReset2_1 = inflateReset2;
var inflateResetKeep_1 = inflateResetKeep;
var inflateInit_1 = inflateInit;
var inflateInit2_1 = inflateInit2;
var inflate_2$1 = inflate$2;
var inflateEnd_1 = inflateEnd;
var inflateGetHeader_1 = inflateGetHeader;
var inflateSetDictionary_1 = inflateSetDictionary;
var inflateInfo = "pako inflate (from Nodeca project)";
var inflate_1$2 = {
inflateReset: inflateReset_1,
inflateReset2: inflateReset2_1,
inflateResetKeep: inflateResetKeep_1,
inflateInit: inflateInit_1,
inflateInit2: inflateInit2_1,
inflate: inflate_2$1,
inflateEnd: inflateEnd_1,
inflateGetHeader: inflateGetHeader_1,
inflateSetDictionary: inflateSetDictionary_1,
inflateInfo
};
function GZheader() {
this.text = 0;
this.time = 0;
this.xflags = 0;
this.os = 0;
this.extra = null;
this.extra_len = 0;
this.name = "";
this.comment = "";
this.hcrc = 0;
this.done = false;
}
var gzheader = GZheader;
var toString = Object.prototype.toString;
var {
Z_NO_FLUSH,
Z_FINISH,
Z_OK,
Z_STREAM_END,
Z_NEED_DICT,
Z_STREAM_ERROR,
Z_DATA_ERROR,
Z_MEM_ERROR
} = constants$2;
function Inflate$1(options) {
this.options = common.assign({
chunkSize: 1024 * 64,
windowBits: 15,
to: ""
}, options || {});
const opt = this.options;
if (opt.raw && opt.windowBits >= 0 && opt.windowBits < 16) {
opt.windowBits = -opt.windowBits;
if (opt.windowBits === 0) {
opt.windowBits = -15;
}
}
if (opt.windowBits >= 0 && opt.windowBits < 16 && !(options && options.windowBits)) {
opt.windowBits += 32;
}
if (opt.windowBits > 15 && opt.windowBits < 48) {
if ((opt.windowBits & 15) === 0) {
opt.windowBits |= 15;
}
}
this.err = 0;
this.msg = "";
this.ended = false;
this.chunks = [];
this.strm = new zstream();
this.strm.avail_out = 0;
let status = inflate_1$2.inflateInit2(
this.strm,
opt.windowBits
);
if (status !== Z_OK) {
throw new Error(messages[status]);
}
this.header = new gzheader();
inflate_1$2.inflateGetHeader(this.strm, this.header);
if (opt.dictionary) {
if (typeof opt.dictionary === "string") {
opt.dictionary = strings.string2buf(opt.dictionary);
} else if (toString.call(opt.dictionary) === "[object ArrayBuffer]") {
opt.dictionary = new Uint8Array(opt.dictionary);
}
if (opt.raw) {
status = inflate_1$2.inflateSetDictionary(this.strm, opt.dictionary);
if (status !== Z_OK) {
throw new Error(messages[status]);
}
}
}
}
Inflate$1.prototype.push = function(data, flush_mode) {
const strm = this.strm;
const chunkSize = this.options.chunkSize;
const dictionary = this.options.dictionary;
let status, _flush_mode, last_avail_out;
if (this.ended) return false;
if (flush_mode === ~~flush_mode) _flush_mode = flush_mode;
else _flush_mode = flush_mode === true ? Z_FINISH : Z_NO_FLUSH;
if (toString.call(data) === "[object ArrayBuffer]") {
strm.input = new Uint8Array(data);
} else {
strm.input = data;
}
strm.next_in = 0;
strm.avail_in = strm.input.length;
for (; ; ) {
if (strm.avail_out === 0) {
strm.output = new Uint8Array(chunkSize);
strm.next_out = 0;
strm.avail_out = chunkSize;
}
status = inflate_1$2.inflate(strm, _flush_mode);
if (status === Z_NEED_DICT && dictionary) {
status = inflate_1$2.inflateSetDictionary(strm, dictionary);
if (status === Z_OK) {
status = inflate_1$2.inflate(strm, _flush_mode);
} else if (status === Z_DATA_ERROR) {
status = Z_NEED_DICT;
}
}
while (strm.avail_in > 0 && status === Z_STREAM_END && strm.state.wrap > 0 && data[strm.next_in] !== 0) {
inflate_1$2.inflateReset(strm);
status = inflate_1$2.inflate(strm, _flush_mode);
}
switch (status) {
case Z_STREAM_ERROR:
case Z_DATA_ERROR:
case Z_NEED_DICT:
case Z_MEM_ERROR:
this.onEnd(status);
this.ended = true;
return false;
}
last_avail_out = strm.avail_out;
if (strm.next_out) {
if (strm.avail_out === 0 || status === Z_STREAM_END) {
if (this.options.to === "string") {
let next_out_utf8 = strings.utf8border(strm.output, strm.next_out);
let tail = strm.next_out - next_out_utf8;
let utf8str = strings.buf2string(strm.output, next_out_utf8);
strm.next_out = tail;
strm.avail_out = chunkSize - tail;
if (tail) strm.output.set(strm.output.subarray(next_out_utf8, next_out_utf8 + tail), 0);
this.onData(utf8str);
} else {
this.onData(strm.output.length === strm.next_out ? strm.output : strm.output.subarray(0, strm.next_out));
}
}
}
if (status === Z_OK && last_avail_out === 0) continue;
if (status === Z_STREAM_END) {
status = inflate_1$2.inflateEnd(this.strm);
this.onEnd(status);
this.ended = true;
return true;
}
if (strm.avail_in === 0) break;
}
return true;
};
Inflate$1.prototype.onData = function(chunk) {
this.chunks.push(chunk);
};
Inflate$1.prototype.onEnd = function(status) {
if (status === Z_OK) {
if (this.options.to === "string") {
this.result = this.chunks.join("");
} else {
this.result = common.flattenChunks(this.chunks);
}
}
this.chunks = [];
this.err = status;
this.msg = this.strm.msg;
};
function inflate$1(input, options) {
const inflator = new Inflate$1(options);
inflator.push(input);
if (inflator.err) throw inflator.msg || messages[inflator.err];
return inflator.result;
}
function inflateRaw$1(input, options) {
options = options || {};
options.raw = true;
return inflate$1(input, options);
}
var Inflate_1$1 = Inflate$1;
var inflate_2 = inflate$1;
var inflateRaw_1$1 = inflateRaw$1;
var ungzip$1 = inflate$1;
var constants = constants$2;
var inflate_1$1 = {
Inflate: Inflate_1$1,
inflate: inflate_2,
inflateRaw: inflateRaw_1$1,
ungzip: ungzip$1,
constants
};
var { Deflate, deflate, deflateRaw, gzip } = deflate_1$1;
var { Inflate, inflate, inflateRaw, ungzip } = inflate_1$1;
var deflate_1 = deflate;
var inflate_1 = inflate;
// src/crypto/cose-qr.js
var base64 = __toESM(require_base64_js());
function toBase64Url(uint8) {
let b64 = base64.fromByteArray(uint8);
return b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
}
function fromBase64Url(str) {
str = str.replace(/-/g, "+").replace(/_/g, "/");
while (str.length % 4) str += "=";
return base64.toByteArray(str);
}
function generateUUID() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
const r = Math.random() * 16 | 0;
const v = c === "x" ? r : r & 3 | 8;
return v.toString(16);
});
}
async function packSecurePayload(payloadObj, senderEcdsaPrivKey = null, recipientEcdhPubKey = null) {
try {
console.log("\u{1F510} Starting COSE packing...");
const payloadJson = JSON.stringify(payloadObj);
console.log(`\u{1F4CA} Original payload size: ${payloadJson.length} characters`);
let ciphertextCose;
let ephemeralRaw = null;
if (recipientEcdhPubKey) {
console.log("\u{1F510} Encrypting for specific recipient...");
const ecdhPair = await crypto.subtle.generateKey(
{ name: "ECDH", namedCurve: "P-384" },
true,
["deriveKey", "deriveBits"]
);
ephemeralRaw = new Uint8Array(await crypto.subtle.exportKey("raw", ecdhPair.publicKey));
const sharedBits = await crypto.subtle.deriveBits(
{ name: "ECDH", public: recipientEcdhPubKey },
ecdhPair.privateKey,
384
);
const hkdfKey = await crypto.subtle.importKey("raw", sharedBits, "HKDF", false, ["deriveKey"]);
const cek = await crypto.subtle.deriveKey(
{
name: "HKDF",
hash: "SHA-384",
salt: new Uint8Array(0),
info: new TextEncoder().encode("SecureBit QR ECDH AES key")
},
hkdfKey,
{ name: "AES-GCM", length: 256 },
true,
["encrypt", "decrypt"]
);
const iv = crypto.getRandomValues(new Uint8Array(12));
const enc = await crypto.subtle.encrypt(
{ name: "AES-GCM", iv },
cek,
new TextEncoder().encode(payloadJson)
);
ciphertextCose = {
protected: { alg: "A256GCM" },
unprotected: { epk: ephemeralRaw },
ciphertext: new Uint8Array(enc),
iv
};
} else {
console.log("\u{1F510} Using broadcast mode (no encryption)...");
ephemeralRaw = crypto.getRandomValues(new Uint8Array(97));
ciphertextCose = {
plaintext: new TextEncoder().encode(payloadJson),
epk: ephemeralRaw
};
}
let coseSign1;
const toSign = cbor.encode(ciphertextCose);
if (senderEcdsaPrivKey) {
console.log("\u{1F510} Signing payload...");
const signature = new Uint8Array(await crypto.subtle.sign(
{ name: "ECDSA", hash: "SHA-384" },
senderEcdsaPrivKey,
toSign
));
const protectedHeader = cbor.encode({ alg: "ES384" });
const unprotectedHeader = { kid: "securebit-sender" };
coseSign1 = [protectedHeader, unprotectedHeader, toSign, signature];
} else {
console.log("\u{1F510} No signing key provided, using unsigned structure...");
const protectedHeader = cbor.encode({ alg: "none" });
const unprotectedHeader = {};
coseSign1 = [protectedHeader, unprotectedHeader, toSign, new Uint8Array(0)];
}
const cborFinal = cbor.encode(coseSign1);
const compressed = deflate_1(cborFinal);
const encoded = toBase64Url(compressed);
console.log(`\u{1F4CA} Compressed size: ${encoded.length} characters (${Math.round((1 - encoded.length / payloadJson.length) * 100)}% reduction)`);
const TARGET_CHUNKS = 10;
const QR_MAX = Math.max(200, Math.floor(encoded.length / TARGET_CHUNKS));
const chunks = [];
if (encoded.length <= QR_MAX) {
chunks.push(JSON.stringify({
hdr: { v: 1, id: generateUUID(), seq: 1, total: 1 },
body: encoded
}));
} else {
const id = generateUUID();
const totalChunks = Math.ceil(encoded.length / QR_MAX);
console.log(`\u{1F4CA} COSE: Splitting ${encoded.length} chars into ${totalChunks} chunks (max ${QR_MAX} chars per chunk)`);
for (let i = 0, seq = 1; i < encoded.length; i += QR_MAX, seq++) {
const part = encoded.slice(i, i + QR_MAX);
chunks.push(JSON.stringify({
hdr: { v: 1, id, seq, total: totalChunks },
body: part
}));
}
}
console.log(`\u{1F4CA} Generated ${chunks.length} QR chunk(s)`);
return chunks;
} catch (error) {
console.error("\u274C Error in packSecurePayload:", error);
throw error;
}
}
async function receiveAndProcess(qrStrings, recipientEcdhPrivKey = null, trustedSenderPubKey = null) {
try {
console.log("\u{1F513} Starting COSE processing...");
console.log(`\u{1F4CA} Processing ${qrStrings.length} QR string(s)`);
const assembled = await assembleFromQrStrings(qrStrings);
if (!assembled.length) {
console.error("\u274C No complete packets found after assembly");
throw new Error("No complete packets found");
}
console.log(`\u{1F4CA} Assembled ${assembled.length} complete packet(s)`);
console.log("\u{1F4CA} First assembled packet:", assembled[0]);
const results = [];
for (const pack of assembled) {
try {
const encoded = pack.jsonObj;
const compressed = fromBase64Url(encoded.body || encoded);
const cborBytes = inflate_1(compressed);
console.log("\u{1F513} Decompressed CBOR bytes length:", cborBytes.length);
console.log("\u{1F513} CBOR bytes type:", typeof cborBytes, cborBytes.constructor.name);
const cborArrayBuffer = cborBytes.buffer.slice(cborBytes.byteOffset, cborBytes.byteOffset + cborBytes.byteLength);
console.log("\u{1F513} Converted to ArrayBuffer, length:", cborArrayBuffer.byteLength);
const coseSign1 = cbor.decode(cborArrayBuffer);
console.log("\u{1F513} Decoded COSE structure");
let protectedHeader, unprotectedHeader, payload, signature;
if (Array.isArray(coseSign1)) {
[protectedHeader, unprotectedHeader, payload, signature] = coseSign1;
console.log("\u{1F513} COSE structure is array format");
} else {
protectedHeader = coseSign1.protected;
unprotectedHeader = coseSign1.unprotected;
payload = coseSign1.payload;
signature = coseSign1.signature;
console.log("\u{1F513} COSE structure is object format (legacy)");
}
if (trustedSenderPubKey && signature && signature.length > 0) {
const toVerify = cbor.encode([protectedHeader, unprotectedHeader, payload]);
const isValid = await crypto.subtle.verify(
{ name: "ECDSA", hash: "SHA-384" },
trustedSenderPubKey,
signature,
toVerify
);
if (!isValid) {
console.warn("\u26A0\uFE0F Signature verification failed");
continue;
}
console.log("\u2705 Signature verified");
}
let inner;
if (payload instanceof Uint8Array) {
const innerArrayBuffer = payload.buffer.slice(payload.byteOffset, payload.byteOffset + payload.byteLength);
inner = cbor.decode(innerArrayBuffer);
} else {
inner = payload;
}
console.log("\u{1F513} Inner payload type:", typeof inner, inner.constructor.name);
console.log("\u{1F513} Inner payload keys:", Object.keys(inner));
console.log("\u{1F513} Inner payload full object:", inner);
let payloadObj;
if (inner.ciphertext && recipientEcdhPrivKey) {
console.log("\u{1F513} Decrypting encrypted payload...");
const epkRaw = inner.unprotected?.epk || inner.epk;
const ephemeralPub = await crypto.subtle.importKey(
"raw",
epkRaw,
{ name: "ECDH", namedCurve: "P-384" },
true,
[]
);
const sharedBits = await crypto.subtle.deriveBits(
{ name: "ECDH", public: ephemeralPub },
recipientEcdhPrivKey,
384
);
const hkdfKey = await crypto.subtle.importKey("raw", sharedBits, "HKDF", false, ["deriveKey"]);
const cek = await crypto.subtle.deriveKey(
{
name: "HKDF",
hash: "SHA-384",
salt: new Uint8Array(0),
info: new TextEncoder().encode("SecureBit QR ECDH AES key")
},
hkdfKey,
{ name: "AES-GCM", length: 256 },
true,
["decrypt"]
);
const plaintext = await crypto.subtle.decrypt(
{ name: "AES-GCM", iv: inner.iv },
cek,
inner.ciphertext
);
const payloadJson = new TextDecoder().decode(plaintext);
payloadObj = JSON.parse(payloadJson);
} else if (inner.plaintext) {
console.log("\u{1F513} Processing plaintext payload...");
payloadObj = JSON.parse(new TextDecoder().decode(inner.plaintext));
} else if (Object.keys(inner).length === 0) {
console.log("\u{1F513} Empty inner payload, using alternative approach...");
try {
const originalBody = encoded.body || encoded;
console.log("\u{1F513} Trying to decode original body:", originalBody.substring(0, 50) + "...");
const compressed2 = fromBase64Url(originalBody);
const decompressed = inflate_1(compressed2);
console.log("\u{1F513} Decompressed length:", decompressed.length);
const decompressedArrayBuffer = decompressed.buffer.slice(decompressed.byteOffset, decompressed.byteOffset + decompressed.byteLength);
const cborDecoded = cbor.decode(decompressedArrayBuffer);
console.log("\u{1F513} CBOR decoded structure:", cborDecoded);
let payload2;
if (Array.isArray(cborDecoded)) {
console.log("\u{1F513} Alternative: COSE structure is array format");
console.log("\u{1F513} Array length:", cborDecoded.length);
console.log("\u{1F513} Array elements:", cborDecoded.map((el, i) => `${i}: ${typeof el} ${el.constructor.name}`));
payload2 = cborDecoded[2];
console.log("\u{1F513} Payload at index 2:", payload2);
} else {
payload2 = cborDecoded.payload;
console.log("\u{1F513} Alternative: COSE structure is object format (legacy)");
}
if (payload2 && payload2 instanceof Uint8Array) {
const payloadArrayBuffer = payload2.buffer.slice(payload2.byteOffset, payload2.byteOffset + payload2.byteLength);
const innerCbor = cbor.decode(payloadArrayBuffer);
console.log("\u{1F513} Inner CBOR structure:", innerCbor);
if (innerCbor.plaintext) {
const jsonString = new TextDecoder().decode(innerCbor.plaintext);
payloadObj = JSON.parse(jsonString);
console.log("\u{1F513} Successfully decoded via alternative approach");
console.log("\u{1F513} Alternative payloadObj:", payloadObj);
} else {
console.error("\u274C No plaintext found in inner CBOR structure");
continue;
}
} else if (payload2 && typeof payload2 === "object" && Object.keys(payload2).length > 0) {
console.log("\u{1F513} Payload is already decoded object:", payload2);
if (payload2.plaintext) {
const jsonString = new TextDecoder().decode(payload2.plaintext);
payloadObj = JSON.parse(jsonString);
console.log("\u{1F513} Successfully decoded from payload object");
console.log("\u{1F513} Alternative payloadObj:", payloadObj);
} else {
console.error("\u274C No plaintext found in payload object");
continue;
}
} else {
console.error("\u274C No payload found in CBOR structure");
console.log("\u{1F513} CBOR structure keys:", Object.keys(cborDecoded));
console.log("\u{1F513} Payload type:", typeof payload2);
console.log("\u{1F513} Payload value:", payload2);
continue;
}
} catch (altError) {
console.error("\u274C Alternative approach failed:", altError);
continue;
}
} else {
console.warn("\u26A0\uFE0F Unknown payload format:", inner);
continue;
}
results.push({
payloadObj,
senderVerified: !!trustedSenderPubKey,
encrypted: !!inner.ciphertext
});
} catch (packError) {
console.error("\u274C Error processing packet:", packError);
continue;
}
}
console.log(`\u2705 Successfully processed ${results.length} payload(s)`);
return results;
} catch (error) {
console.error("\u274C Error in receiveAndProcess:", error);
throw error;
}
}
async function assembleFromQrStrings(qrStrings) {
const packets = /* @__PURE__ */ new Map();
console.log("\u{1F527} Starting assembly of QR strings...");
for (const qrString of qrStrings) {
try {
console.log("\u{1F527} Parsing QR string:", qrString.substring(0, 100) + "...");
const parsed = JSON.parse(qrString);
console.log("\u{1F527} Parsed structure:", parsed);
if (parsed.hdr && parsed.body) {
const id = parsed.hdr.id;
console.log(`\u{1F527} Processing packet ID: ${id}, seq: ${parsed.hdr.seq}, total: ${parsed.hdr.total}`);
if (!packets.has(id)) {
packets.set(id, {
id,
chunks: /* @__PURE__ */ new Map(),
total: parsed.hdr.total
});
console.log(`\u{1F527} Created new packet for ID: ${id}`);
}
const packet = packets.get(id);
packet.chunks.set(parsed.hdr.seq, parsed.body);
console.log(`\u{1F527} Added chunk ${parsed.hdr.seq} to packet ${id}. Current chunks: ${packet.chunks.size}/${packet.total}`);
if (packet.chunks.size === packet.total) {
console.log(`\u{1F527} Packet ${id} is complete! Assembling body...`);
let assembledBody = "";
for (let i = 1; i <= packet.total; i++) {
assembledBody += packet.chunks.get(i);
}
packet.jsonObj = { body: assembledBody };
packet.complete = true;
console.log(`\u{1F527} Assembled body length: ${assembledBody.length} characters`);
}
} else {
console.warn("\u26A0\uFE0F QR string missing hdr or body:", parsed);
}
} catch (error) {
console.warn("\u26A0\uFE0F Failed to parse QR string:", error);
continue;
}
}
const completePackets = Array.from(packets.values()).filter((p) => p.complete);
console.log(`\u{1F527} Assembly complete. Found ${completePackets.length} complete packets`);
return completePackets;
}
window.packSecurePayload = packSecurePayload;
window.receiveAndProcess = receiveAndProcess;
// src/scripts/qr-local.js
async function generateQRCode(text, opts = {}) {
const size = opts.size || 512;
const margin = opts.margin ?? 2;
const errorCorrectionLevel = opts.errorCorrectionLevel || "M";
return await QRCode.toDataURL(text, { width: size, margin, errorCorrectionLevel });
}
async function generateCOSEQRCode(data, senderKey = null, recipientKey = null) {
try {
console.log("\u{1F510} Generating COSE-based QR code...");
const chunks = await packSecurePayload(data, senderKey, recipientKey);
if (chunks.length === 1) {
return await generateQRCode(chunks[0]);
} else {
console.warn(`\u{1F4CA} COSE packing produced ${chunks.length} chunks; falling back to non-COSE strategy`);
throw new Error("COSE QR would require multiple chunks");
}
} catch (error) {
console.error("Error generating COSE QR code:", error);
throw error;
}
}
window.generateQRCode = generateQRCode;
window.generateCOSEQRCode = generateCOSEQRCode;
window.Html5Qrcode = Html5Qrcode;
window.packSecurePayload = packSecurePayload;
window.receiveAndProcess = receiveAndProcess;
console.log("QR libraries loaded: generateQRCode, generateCOSEQRCode, Html5Qrcode, COSE functions");
/*! Bundled license information:
pako/dist/pako.esm.mjs:
(*! pako 2.1.0 https://github.com/nodeca/pako @license (MIT AND Zlib) *)
*/
//# sourceMappingURL=qr-local.js.map