@sythos/js_barcode_universal 1.5.8 → 1.5.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +5 -4
- package/NOTICE.md +2 -2
- package/README.md +17 -13
- package/bundle/sythos-barcode.esm.js +11658 -11244
- package/bundle/sythos-barcode.js +11658 -11244
- package/package.json +10 -3
- package/src/index.d.ts +1 -1
- package/src/index.js +508 -516
- package/src/js/aztec/decoder.js +208 -199
- package/src/js/aztec/detector.js +153 -126
- package/src/js/aztec/encoder.js +172 -153
- package/src/js/aztec/high-level.js +106 -101
- package/src/js/aztec/index.js +1 -12
- package/src/js/aztec/tables.js +106 -120
- package/src/js/aztecrune/decoder.js +76 -85
- package/src/js/aztecrune/detector.js +107 -93
- package/src/js/aztecrune/encoder.js +50 -68
- package/src/js/aztecrune/index.js +1 -16
- package/src/js/aztecrune/tables.js +59 -54
- package/src/js/compactpdf417/decoder.js +69 -74
- package/src/js/compactpdf417/detector.js +74 -75
- package/src/js/compactpdf417/encoder.js +68 -81
- package/src/js/compactpdf417/index.js +1 -13
- package/src/js/compactpdf417/tables.js +71 -80
- package/src/js/core/bit-buffer.js +106 -122
- package/src/js/core/bit-matrix.js +196 -194
- package/src/js/core/errors.js +13 -15
- package/src/js/core/galois-field.js +119 -133
- package/src/js/core/index.js +2 -19
- package/src/js/core/reed-solomon.js +195 -204
- package/src/js/databar/codec.js +93 -108
- package/src/js/databar/decoder.js +149 -140
- package/src/js/databar/encoder.js +47 -55
- package/src/js/databar/gs1.js +122 -123
- package/src/js/databar/index.js +4 -36
- package/src/js/databar/patterns.js +81 -85
- package/src/js/databar/tables.js +51 -52
- package/src/js/datamatrix/decoder.js +216 -182
- package/src/js/datamatrix/detector.js +161 -132
- package/src/js/datamatrix/encoder.js +192 -133
- package/src/js/datamatrix/index.js +1 -9
- package/src/js/datamatrix/tables.js +68 -69
- package/src/js/frameqr/decoder.js +102 -125
- package/src/js/frameqr/detector.js +101 -94
- package/src/js/frameqr/encoder.js +67 -80
- package/src/js/frameqr/index.js +1 -10
- package/src/js/frameqr/tables.js +145 -163
- package/src/js/image/binarizer.js +146 -162
- package/src/js/image/grid-sampler.js +63 -79
- package/src/js/image/index.js +0 -2
- package/src/js/image/luminance.js +129 -140
- package/src/js/image/perspective.js +112 -149
- package/src/js/micropdf417/compaction.js +64 -65
- package/src/js/micropdf417/decoder.js +106 -109
- package/src/js/micropdf417/detector.js +73 -70
- package/src/js/micropdf417/encoder.js +129 -154
- package/src/js/micropdf417/error-correction.js +6 -11
- package/src/js/micropdf417/index.js +2 -16
- package/src/js/micropdf417/tables.js +112 -99
- package/src/js/microqr/decoder.js +177 -182
- package/src/js/microqr/detector.js +240 -256
- package/src/js/microqr/encoder.js +239 -194
- package/src/js/microqr/index.js +0 -2
- package/src/js/microqr/tables.js +208 -200
- package/src/js/oned/addons.js +167 -191
- package/src/js/oned/index.js +27 -58
- package/src/js/oned/patterns.js +194 -213
- package/src/js/oned/reader.js +1003 -984
- package/src/js/oned/writers.js +411 -466
- package/src/js/pdf417/compaction.js +315 -222
- package/src/js/pdf417/decoder.js +71 -36
- package/src/js/pdf417/detector.js +377 -361
- package/src/js/pdf417/encoder.js +57 -46
- package/src/js/pdf417/error-correction.js +5 -8
- package/src/js/pdf417/tables.js +248 -250
- package/src/js/qr/decoder.js +311 -347
- package/src/js/qr/detector.js +378 -395
- package/src/js/qr/encoder.js +514 -599
- package/src/js/qr/index.js +0 -2
- package/src/js/qr/tables.js +375 -422
- package/src/js/render/image-data.js +57 -62
- package/src/js/render/index.js +21 -23
- package/src/js/render/options.js +82 -90
- package/src/js/render/png.js +144 -169
- package/src/js/render/svg.js +41 -44
- package/src/js/render/webgl.js +103 -109
- package/src/js/render/webgpu.js +205 -217
- package/src/js/rmqr/decoder.js +161 -53
- package/src/js/rmqr/detector.js +73 -41
- package/src/js/rmqr/encoder.js +253 -104
- package/src/js/rmqr/index.js +1 -5
- package/src/js/rmqr/tables.js +108 -85
- package/src/ts/aztec/decoder.ts +317 -0
- package/src/ts/aztec/detector.ts +224 -0
- package/src/ts/aztec/encoder.ts +257 -0
- package/src/ts/aztec/high-level.ts +211 -0
- package/src/ts/aztec/index.ts +45 -0
- package/src/ts/aztec/tables.ts +210 -0
- package/src/ts/aztecrune/decoder.ts +155 -0
- package/src/ts/aztecrune/detector.ts +166 -0
- package/src/ts/aztecrune/encoder.ts +121 -0
- package/src/ts/aztecrune/index.ts +49 -0
- package/src/ts/aztecrune/tables.ts +137 -0
- package/src/ts/compactpdf417/decoder.ts +128 -0
- package/src/ts/compactpdf417/detector.ts +139 -0
- package/src/ts/compactpdf417/encoder.ts +140 -0
- package/src/ts/compactpdf417/index.ts +17 -0
- package/src/ts/compactpdf417/tables.ts +173 -0
- package/src/ts/core/bit-buffer.ts +174 -0
- package/src/ts/core/bit-matrix.ts +241 -0
- package/src/ts/core/errors.ts +61 -0
- package/src/ts/core/galois-field.ts +207 -0
- package/src/ts/core/index.ts +56 -0
- package/src/ts/core/reed-solomon.ts +327 -0
- package/src/ts/databar/codec.ts +181 -0
- package/src/ts/databar/decoder.ts +215 -0
- package/src/ts/databar/encoder.ts +96 -0
- package/src/ts/databar/gs1.ts +177 -0
- package/src/ts/databar/index.ts +68 -0
- package/src/ts/databar/patterns.ts +143 -0
- package/src/ts/databar/tables.ts +124 -0
- package/src/ts/datamatrix/decoder.ts +262 -0
- package/src/ts/datamatrix/detector.ts +225 -0
- package/src/ts/datamatrix/encoder.ts +191 -0
- package/src/ts/datamatrix/index.ts +42 -0
- package/src/ts/datamatrix/tables.ts +123 -0
- package/src/ts/frameqr/decoder.ts +239 -0
- package/src/ts/frameqr/detector.ts +192 -0
- package/src/ts/frameqr/encoder.ts +156 -0
- package/src/ts/frameqr/index.ts +42 -0
- package/src/ts/frameqr/tables.ts +270 -0
- package/src/ts/image/binarizer.ts +270 -0
- package/src/ts/image/grid-sampler.ts +164 -0
- package/src/ts/image/index.ts +40 -0
- package/src/ts/image/luminance.ts +196 -0
- package/src/ts/image/perspective.ts +195 -0
- package/src/ts/index.d.ts +1 -1
- package/src/ts/index.ts +790 -0
- package/src/ts/micropdf417/compaction.ts +116 -0
- package/src/ts/micropdf417/decoder.ts +183 -0
- package/src/ts/micropdf417/detector.ts +149 -0
- package/src/ts/micropdf417/encoder.ts +209 -0
- package/src/ts/micropdf417/error-correction.ts +55 -0
- package/src/ts/micropdf417/index.ts +49 -0
- package/src/ts/micropdf417/tables.ts +184 -0
- package/src/ts/microqr/decoder.ts +245 -0
- package/src/ts/microqr/detector.ts +355 -0
- package/src/ts/microqr/encoder.ts +269 -0
- package/src/ts/microqr/index.ts +36 -0
- package/src/ts/microqr/tables.ts +316 -0
- package/src/ts/oned/addons.ts +420 -0
- package/src/ts/oned/index.ts +106 -0
- package/src/ts/oned/patterns.ts +384 -0
- package/src/ts/oned/reader.ts +1364 -0
- package/src/ts/oned/writers.ts +746 -0
- package/src/ts/pdf417/compaction.ts +298 -0
- package/src/ts/pdf417/decoder.ts +75 -0
- package/src/ts/pdf417/detector.ts +468 -0
- package/src/ts/pdf417/encoder.ts +91 -0
- package/src/ts/pdf417/error-correction.ts +47 -0
- package/src/ts/pdf417/index.ts +6 -0
- package/src/ts/pdf417/tables.ts +317 -0
- package/src/ts/qr/decoder.ts +575 -0
- package/src/ts/qr/detector.ts +630 -0
- package/src/ts/qr/encoder.ts +958 -0
- package/src/ts/qr/index.ts +44 -0
- package/src/ts/qr/tables.ts +737 -0
- package/src/ts/render/image-data.ts +125 -0
- package/src/ts/render/index.ts +130 -0
- package/src/ts/render/options.ts +160 -0
- package/src/ts/render/png.ts +295 -0
- package/src/ts/render/svg.ts +120 -0
- package/src/ts/render/webgl.ts +206 -0
- package/src/ts/render/webgpu.ts +369 -0
- package/src/ts/rmqr/decoder.ts +101 -0
- package/src/ts/rmqr/detector.ts +90 -0
- package/src/ts/rmqr/encoder.ts +172 -0
- package/src/ts/rmqr/index.ts +37 -0
- package/src/ts/rmqr/tables.ts +154 -0
package/src/js/databar/tables.js
CHANGED
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
*
|
|
28
28
|
* Original work. No code from any other barcode implementation.
|
|
29
29
|
*/
|
|
30
|
-
|
|
31
30
|
/**
|
|
32
31
|
* Static GS1 DataBar facts used by the GTIN compaction codec.
|
|
33
32
|
*
|
|
@@ -38,87 +37,87 @@
|
|
|
38
37
|
*
|
|
39
38
|
* @module databar/tables
|
|
40
39
|
*/
|
|
41
|
-
|
|
42
40
|
const group = (first, last, gsum, oddModules, evenModules, oddWidest, evenWidest, oddTotal, evenTotal) => Object.freeze({
|
|
43
|
-
|
|
41
|
+
first, last, gsum, oddModules, evenModules, oddWidest, evenWidest, oddTotal, evenTotal,
|
|
44
42
|
});
|
|
45
|
-
|
|
46
43
|
/** The four GTIN-only layouts sharing the DataBar-14 compaction rules. */
|
|
47
44
|
export const DATABAR14_VARIANTS = Object.freeze({
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
45
|
+
omnidirectional: Object.freeze({ id: 'omnidirectional', rows: 1, modules: 96, checksumModulus: 79, pointOfSale: true }),
|
|
46
|
+
truncated: Object.freeze({ id: 'truncated', rows: 1, modules: 96, checksumModulus: 79, pointOfSale: false }),
|
|
47
|
+
stacked: Object.freeze({ id: 'stacked', rows: 2, modules: 50, checksumModulus: 79, pointOfSale: false }),
|
|
48
|
+
'stacked-omnidirectional': Object.freeze({ id: 'stacked-omnidirectional', rows: 2, modules: 50, checksumModulus: 79, pointOfSale: true }),
|
|
52
49
|
});
|
|
53
|
-
|
|
54
50
|
/** GS1 DataBar Limited is a distinct two-character symbology. */
|
|
55
51
|
export const DATABAR_LIMITED_VARIANT = Object.freeze({
|
|
56
|
-
|
|
57
|
-
|
|
52
|
+
id: 'limited', rows: 1, checksumModulus: 89, pointOfSale: false,
|
|
53
|
+
permittedIndicatorDigits: Object.freeze([0, 1]),
|
|
58
54
|
});
|
|
59
|
-
|
|
60
55
|
/** (16,4) outside character groups, ISO/IEC 24724:2011 Table 1. */
|
|
61
56
|
export const DATABAR14_OUTSIDE_GROUPS = Object.freeze([
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
57
|
+
group(0, 160, 0, 12, 4, 8, 1, 161, 1),
|
|
58
|
+
group(161, 960, 161, 10, 6, 6, 3, 80, 10),
|
|
59
|
+
group(961, 2014, 961, 8, 8, 4, 5, 31, 34),
|
|
60
|
+
group(2015, 2714, 2015, 6, 10, 3, 6, 10, 70),
|
|
61
|
+
group(2715, 2840, 2715, 4, 12, 1, 8, 1, 126),
|
|
67
62
|
]);
|
|
68
|
-
|
|
69
63
|
/** (15,4) inside character groups, ISO/IEC 24724:2011 Table 2. */
|
|
70
64
|
export const DATABAR14_INSIDE_GROUPS = Object.freeze([
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
65
|
+
group(0, 335, 0, 5, 10, 2, 7, 4, 84),
|
|
66
|
+
group(336, 1035, 336, 7, 8, 4, 5, 20, 35),
|
|
67
|
+
group(1036, 1515, 1036, 9, 6, 6, 3, 48, 10),
|
|
68
|
+
group(1516, 1596, 1516, 11, 4, 8, 1, 81, 1),
|
|
75
69
|
]);
|
|
76
|
-
|
|
77
70
|
/** Number of values carried by an inside (15,4) character. */
|
|
78
71
|
export const DATABAR14_INSIDE_RADIX = 1597;
|
|
79
72
|
/** Number of values carried by one outside/inside character pair. */
|
|
80
73
|
export const DATABAR14_PAIR_RADIX = 4537077;
|
|
81
74
|
/** Stand-alone and composite DataBar-14 payload domain, including linkage. */
|
|
82
75
|
export const DATABAR14_SYMBOL_LIMIT = 20000000000000n;
|
|
83
|
-
|
|
84
76
|
/** GS1 DataBar Limited divides its 13 data digits into two character values. */
|
|
85
77
|
export const DATABAR_LIMITED_PAIR_RADIX = 2013571;
|
|
86
78
|
/** Offset that switches a DataBar Limited value from stand-alone to composite. */
|
|
87
79
|
export const DATABAR_LIMITED_LINKAGE_OFFSET = 2015133531096n;
|
|
88
80
|
/** DataBar Limited carries GTIN values whose first digit is zero or one. */
|
|
89
81
|
export const DATABAR_LIMITED_DATA_LIMIT = 2000000000000n;
|
|
90
|
-
|
|
91
82
|
/** Locate the value group that an outside or inside character belongs to. */
|
|
92
83
|
export function dataBar14GroupFor(value, kind) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
84
|
+
if (!Number.isInteger(value))
|
|
85
|
+
throw new TypeError('GS1 DataBar character value must be an integer');
|
|
86
|
+
const groups = kind === 'outside' ? DATABAR14_OUTSIDE_GROUPS
|
|
87
|
+
: kind === 'inside' ? DATABAR14_INSIDE_GROUPS : null;
|
|
88
|
+
if (!groups)
|
|
89
|
+
throw new RangeError(`Unknown GS1 DataBar character kind "${kind}"`);
|
|
90
|
+
const found = groups.find((entry) => value >= entry.first && value <= entry.last);
|
|
91
|
+
if (!found)
|
|
92
|
+
throw new RangeError(`GS1 DataBar ${kind} character value ${value} is out of range`);
|
|
93
|
+
return found;
|
|
100
94
|
}
|
|
101
95
|
/** Check the independently represented table identities. */
|
|
102
96
|
export function validateDataBarTables() {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
97
|
+
const problems = [];
|
|
98
|
+
const validateGroups = (name, groups, expectedLast) => {
|
|
99
|
+
let next = 0;
|
|
100
|
+
for (const entry of groups) {
|
|
101
|
+
if (entry.first !== next)
|
|
102
|
+
problems.push(`${name}: gap before ${entry.first}`);
|
|
103
|
+
if (entry.last < entry.first)
|
|
104
|
+
problems.push(`${name}: inverted range ${entry.first}-${entry.last}`);
|
|
105
|
+
if (entry.oddTotal * entry.evenTotal !== entry.last - entry.first + 1) {
|
|
106
|
+
problems.push(`${name}: combination count mismatch at ${entry.first}`);
|
|
107
|
+
}
|
|
108
|
+
next = entry.last + 1;
|
|
109
|
+
}
|
|
110
|
+
if (next - 1 !== expectedLast)
|
|
111
|
+
problems.push(`${name}: final value is ${next - 1}, expected ${expectedLast}`);
|
|
112
|
+
};
|
|
113
|
+
validateGroups('outside', DATABAR14_OUTSIDE_GROUPS, 2840);
|
|
114
|
+
validateGroups('inside', DATABAR14_INSIDE_GROUPS, 1596);
|
|
115
|
+
if (DATABAR14_PAIR_RADIX !== 2841 * DATABAR14_INSIDE_RADIX)
|
|
116
|
+
problems.push('pair radix mismatch');
|
|
117
|
+
if (DATABAR14_SYMBOL_LIMIT !== 2n * 10000000000000n)
|
|
118
|
+
problems.push('symbol limit mismatch');
|
|
119
|
+
if (DATABAR_LIMITED_LINKAGE_OFFSET !== BigInt(DATABAR_LIMITED_PAIR_RADIX) * 1000776n) {
|
|
120
|
+
problems.push('limited linkage offset mismatch');
|
|
113
121
|
}
|
|
114
|
-
|
|
115
|
-
};
|
|
116
|
-
validateGroups('outside', DATABAR14_OUTSIDE_GROUPS, 2840);
|
|
117
|
-
validateGroups('inside', DATABAR14_INSIDE_GROUPS, 1596);
|
|
118
|
-
if (DATABAR14_PAIR_RADIX !== 2841 * DATABAR14_INSIDE_RADIX) problems.push('pair radix mismatch');
|
|
119
|
-
if (DATABAR14_SYMBOL_LIMIT !== 2n * 10000000000000n) problems.push('symbol limit mismatch');
|
|
120
|
-
if (DATABAR_LIMITED_LINKAGE_OFFSET !== BigInt(DATABAR_LIMITED_PAIR_RADIX) * 1000776n) {
|
|
121
|
-
problems.push('limited linkage offset mismatch');
|
|
122
|
-
}
|
|
123
|
-
return problems;
|
|
122
|
+
return problems;
|
|
124
123
|
}
|
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
*
|
|
28
28
|
* Original work. No code from any other barcode implementation.
|
|
29
29
|
*/
|
|
30
|
-
|
|
31
30
|
/**
|
|
32
31
|
* Data Matrix ECC 200 decoder for an already sampled symbol.
|
|
33
32
|
*
|
|
@@ -40,210 +39,245 @@
|
|
|
40
39
|
*
|
|
41
40
|
* @module datamatrix/decoder
|
|
42
41
|
*/
|
|
43
|
-
|
|
44
42
|
import { ChecksumError, FormatError } from '../core/errors.js';
|
|
45
43
|
import { GF256_DM } from '../core/galois-field.js';
|
|
46
44
|
import { rsDecode } from '../core/reed-solomon.js';
|
|
47
45
|
import { SYMBOLS } from './tables.js';
|
|
48
|
-
|
|
49
46
|
const CW_PAD = 129;
|
|
50
47
|
const CW_BASE256 = 231;
|
|
51
|
-
|
|
52
48
|
/** @param {object} entry @param {...string} names @returns {number | undefined} */
|
|
53
49
|
function numberField(entry, ...names) {
|
|
54
|
-
|
|
55
|
-
|
|
50
|
+
for (const name of names)
|
|
51
|
+
if (Number.isInteger(entry[name]))
|
|
52
|
+
return entry[name];
|
|
53
|
+
return undefined;
|
|
56
54
|
}
|
|
57
|
-
|
|
58
55
|
/** Normalize the public table entry into the decoder's geometry contract. */
|
|
59
56
|
function layoutFor(width, height) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
57
|
+
const entry = SYMBOLS.find((s) => numberField(s, 'columns', 'cols', 'matrixColumns', 'width') === width &&
|
|
58
|
+
numberField(s, 'rows', 'matrixRows', 'height') === height);
|
|
59
|
+
if (!entry)
|
|
60
|
+
throw new FormatError(`Data Matrix: ${width}x${height} is not an ECC 200 symbol size`);
|
|
61
|
+
const regionRows = numberField(entry, 'dataRegionRows', 'regionRows') ??
|
|
62
|
+
(numberField(entry, 'regionHeight') ? numberField(entry, 'regionHeight') - 2 : undefined);
|
|
63
|
+
const regionCols = numberField(entry, 'dataRegionColumns', 'dataRegionCols', 'regionColumns') ??
|
|
64
|
+
(numberField(entry, 'regionWidth') ? numberField(entry, 'regionWidth') - 2 : undefined);
|
|
65
|
+
const dataCount = numberField(entry, 'dataCodewords', 'dataCapacity');
|
|
66
|
+
const eccCount = numberField(entry, 'errorCodewords', 'eccCodewords');
|
|
67
|
+
const blockCount = numberField(entry, 'interleavedBlocks', 'interleavedBlockCount', 'blockCount', 'rsBlocks') || 1;
|
|
68
|
+
if (!regionRows || !regionCols || dataCount === undefined || eccCount === undefined ||
|
|
69
|
+
height % (regionRows + 2) || width % (regionCols + 2) || eccCount % blockCount) {
|
|
70
|
+
throw new FormatError(`Data Matrix: invalid table layout for ${width}x${height}`);
|
|
71
|
+
}
|
|
72
|
+
const rows = height / (regionRows + 2);
|
|
73
|
+
const cols = width / (regionCols + 2);
|
|
74
|
+
const blockData = Array.isArray(entry.blockDataCodewords) ? entry.blockDataCodewords.slice() :
|
|
75
|
+
Array.isArray(entry.dataCodewordsPerBlock) ? entry.dataCodewordsPerBlock.slice() : null;
|
|
76
|
+
let dataLengths;
|
|
77
|
+
if (blockData) {
|
|
78
|
+
dataLengths = blockData;
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
// The sole uneven ECC 200 distribution is 144x144: its first eight of ten
|
|
82
|
+
// blocks contain one extra data codeword. This derives it instead of hiding
|
|
83
|
+
// a magic size check in the deinterleaver.
|
|
84
|
+
const short = Math.floor(dataCount / blockCount);
|
|
85
|
+
dataLengths = new Array(blockCount).fill(short);
|
|
86
|
+
for (let i = 0; i < dataCount % blockCount; i++)
|
|
87
|
+
dataLengths[i]++;
|
|
88
|
+
}
|
|
89
|
+
if (dataLengths.length !== blockCount || dataLengths.reduce((a, b) => a + b, 0) !== dataCount) {
|
|
90
|
+
throw new FormatError(`Data Matrix: inconsistent block layout for ${width}x${height}`);
|
|
91
|
+
}
|
|
92
|
+
return { entry, regionRows, regionCols, regionRowCount: rows, regionColCount: cols,
|
|
93
|
+
dataRows: rows * regionRows, dataCols: cols * regionCols, dataCount, eccCount,
|
|
94
|
+
blockCount, eccPerBlock: eccCount / blockCount, dataLengths };
|
|
98
95
|
}
|
|
99
|
-
|
|
100
96
|
/** Remove the L/finders from every data region, retaining only placement modules. */
|
|
101
97
|
function extractDataModules(matrix, layout) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
98
|
+
const data = new Uint8Array(layout.dataRows * layout.dataCols);
|
|
99
|
+
for (let regionY = 0; regionY < layout.regionRowCount; regionY++) {
|
|
100
|
+
for (let regionX = 0; regionX < layout.regionColCount; regionX++) {
|
|
101
|
+
const sourceX = regionX * (layout.regionCols + 2) + 1;
|
|
102
|
+
const sourceY = regionY * (layout.regionRows + 2) + 1;
|
|
103
|
+
for (let y = 0; y < layout.regionRows; y++) {
|
|
104
|
+
const targetY = regionY * layout.regionRows + y;
|
|
105
|
+
for (let x = 0; x < layout.regionCols; x++) {
|
|
106
|
+
data[targetY * layout.dataCols + regionX * layout.regionCols + x] =
|
|
107
|
+
matrix.get(sourceX + x, sourceY + y) ? 1 : 0;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
112
110
|
}
|
|
113
|
-
}
|
|
114
111
|
}
|
|
115
|
-
|
|
116
|
-
return data;
|
|
112
|
+
return data;
|
|
117
113
|
}
|
|
118
|
-
|
|
119
114
|
/** Read placement codewords using the ECC 200 Utah sweep (the inverse writer path). */
|
|
120
115
|
function readPlacement(modules, rows, cols, count) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
116
|
+
const seen = new Uint8Array(rows * cols);
|
|
117
|
+
const out = new Uint8Array(count);
|
|
118
|
+
const get = (row, col) => modules[row * cols + col] !== 0;
|
|
119
|
+
const module = (row, col) => {
|
|
120
|
+
if (row < 0) {
|
|
121
|
+
row += rows;
|
|
122
|
+
col += 4 - ((rows + 4) % 8);
|
|
123
|
+
}
|
|
124
|
+
if (col < 0) {
|
|
125
|
+
col += cols;
|
|
126
|
+
row += 4 - ((cols + 4) % 8);
|
|
127
|
+
}
|
|
128
|
+
if (row < 0 || row >= rows || col < 0 || col >= cols) {
|
|
129
|
+
throw new FormatError('Data Matrix: placement coordinate escaped data region');
|
|
130
|
+
}
|
|
131
|
+
seen[row * cols + col] = 1;
|
|
132
|
+
return get(row, col) ? 1 : 0;
|
|
133
|
+
};
|
|
134
|
+
const bits = (coords) => coords.reduce((value, p) => (value << 1) | module(p[0], p[1]), 0);
|
|
135
|
+
const utah = (row, col) => bits([[row - 2, col - 2], [row - 2, col - 1], [row - 1, col - 2], [row - 1, col - 1],
|
|
136
|
+
[row - 1, col], [row, col - 2], [row, col - 1], [row, col]]);
|
|
137
|
+
const corner1 = () => bits([[rows - 1, 0], [rows - 1, 1], [rows - 1, 2], [0, cols - 2], [0, cols - 1], [1, cols - 1], [2, cols - 1], [3, cols - 1]]);
|
|
138
|
+
const corner2 = () => bits([[rows - 3, 0], [rows - 2, 0], [rows - 1, 0], [0, cols - 4], [0, cols - 3], [0, cols - 2], [0, cols - 1], [1, cols - 1]]);
|
|
139
|
+
const corner3 = () => bits([[rows - 3, 0], [rows - 2, 0], [rows - 1, 0], [0, cols - 2], [0, cols - 1], [1, cols - 1], [2, cols - 1], [3, cols - 1]]);
|
|
140
|
+
const corner4 = () => bits([[rows - 1, 0], [rows - 1, cols - 1], [0, cols - 3], [0, cols - 2], [0, cols - 1], [1, cols - 3], [1, cols - 2], [1, cols - 1]]);
|
|
141
|
+
let row = 4, col = 0, n = 0;
|
|
142
|
+
const put = (value) => { if (n < count)
|
|
143
|
+
out[n++] = value; };
|
|
144
|
+
do {
|
|
145
|
+
if (row === rows && col === 0)
|
|
146
|
+
put(corner1());
|
|
147
|
+
if (row === rows - 2 && col === 0 && cols % 4 !== 0)
|
|
148
|
+
put(corner2());
|
|
149
|
+
if (row === rows - 2 && col === 0 && cols % 8 === 4)
|
|
150
|
+
put(corner3());
|
|
151
|
+
if (row === rows + 4 && col === 2 && cols % 8 === 0)
|
|
152
|
+
put(corner4());
|
|
153
|
+
do {
|
|
154
|
+
if (row < rows && col >= 0 && !seen[row * cols + col])
|
|
155
|
+
put(utah(row, col));
|
|
156
|
+
row -= 2;
|
|
157
|
+
col += 2;
|
|
158
|
+
} while (row >= 0 && col < cols);
|
|
159
|
+
row += 1;
|
|
160
|
+
col += 3;
|
|
161
|
+
do {
|
|
162
|
+
if (row >= 0 && col < cols && !seen[row * cols + col])
|
|
163
|
+
put(utah(row, col));
|
|
164
|
+
row += 2;
|
|
165
|
+
col -= 2;
|
|
166
|
+
} while (row < rows && col >= 0);
|
|
167
|
+
row += 3;
|
|
168
|
+
col += 1;
|
|
169
|
+
} while (row < rows || col < cols);
|
|
170
|
+
if (n !== count)
|
|
171
|
+
throw new FormatError(`Data Matrix: placement yielded ${n}, expected ${count} codewords`);
|
|
172
|
+
return out;
|
|
155
173
|
}
|
|
156
|
-
|
|
157
174
|
/** Restore RS blocks, correct them, then concatenate their data portions. */
|
|
158
175
|
function deinterleaveAndCorrect(codewords, layout) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
176
|
+
if (codewords.length !== layout.dataCount + layout.eccCount)
|
|
177
|
+
throw new FormatError('Data Matrix: codeword count mismatch');
|
|
178
|
+
const blocks = layout.dataLengths.map((len) => new Uint8Array(len + layout.eccPerBlock));
|
|
179
|
+
// Data codewords arrive in their original stream order. ECC 200 deals that
|
|
180
|
+
// stream round-robin across the RS blocks, so the inverse is determined by
|
|
181
|
+
// the wire index rather than by splitting it into consecutive block-sized
|
|
182
|
+
// chunks. For 144x144, indices 1550..1557 naturally land in the eight long
|
|
183
|
+
// blocks while the two short blocks remain at 155 data codewords.
|
|
184
|
+
for (let i = 0; i < layout.dataCount; i++) {
|
|
185
|
+
blocks[i % layout.blockCount][Math.floor(i / layout.blockCount)] = codewords[i];
|
|
186
|
+
}
|
|
187
|
+
// Parity normally begins with block zero. The uneven 144x144 layout rotates
|
|
188
|
+
// the parity wire order to begin with its first short block; derive the same
|
|
189
|
+
// mapping from the declarative lengths instead of keying it to dimensions.
|
|
190
|
+
const longest = Math.max(...layout.dataLengths);
|
|
191
|
+
const firstShort = layout.dataLengths.findIndex((length) => length < longest);
|
|
192
|
+
const rotation = firstShort < 0 ? 0 : firstShort;
|
|
193
|
+
let at = layout.dataCount;
|
|
194
|
+
for (let i = 0; i < layout.eccPerBlock; i++) {
|
|
195
|
+
for (let slot = 0; slot < layout.blockCount; slot++) {
|
|
196
|
+
const block = (slot + rotation) % layout.blockCount;
|
|
197
|
+
blocks[block][layout.dataLengths[block] + i] = codewords[at++];
|
|
198
|
+
}
|
|
181
199
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
}
|
|
195
|
-
return { data, corrections };
|
|
200
|
+
let corrections = 0;
|
|
201
|
+
const data = new Uint8Array(layout.dataCount);
|
|
202
|
+
for (let b = 0; b < blocks.length; b++) {
|
|
203
|
+
corrections += rsDecode(blocks[b], layout.eccPerBlock, GF256_DM, 1);
|
|
204
|
+
}
|
|
205
|
+
// Rebuild the original high-level codeword stream after correction. Keeping
|
|
206
|
+
// this in wire-index order is essential: concatenating block data passes
|
|
207
|
+
// single-block round trips but scrambles every multi-block payload.
|
|
208
|
+
for (let i = 0; i < layout.dataCount; i++) {
|
|
209
|
+
data[i] = blocks[i % layout.blockCount][Math.floor(i / layout.blockCount)];
|
|
210
|
+
}
|
|
211
|
+
return { data, corrections };
|
|
196
212
|
}
|
|
197
|
-
|
|
198
213
|
function unrandomize(value, position) {
|
|
199
|
-
|
|
200
|
-
|
|
214
|
+
const pseudo = ((149 * position) % 255) + 1;
|
|
215
|
+
return value - pseudo >= 0 ? value - pseudo : value - pseudo + 256;
|
|
201
216
|
}
|
|
202
|
-
|
|
203
217
|
/** Decode ASCII plus Base 256, preserving semantic bytes alongside text. */
|
|
204
218
|
function parseData(data) {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
219
|
+
let text = '';
|
|
220
|
+
const bytes = [];
|
|
221
|
+
let upperShift = false;
|
|
222
|
+
let gs1 = false;
|
|
223
|
+
for (let i = 0; i < data.length;) {
|
|
224
|
+
const cw = data[i++];
|
|
225
|
+
if (cw === CW_PAD)
|
|
226
|
+
break;
|
|
227
|
+
if (cw <= 128) {
|
|
228
|
+
const value = cw - 1 + (upperShift ? 128 : 0);
|
|
229
|
+
upperShift = false;
|
|
230
|
+
text += String.fromCharCode(value);
|
|
231
|
+
bytes.push(value);
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
if (cw <= 229) {
|
|
235
|
+
const pair = cw - 130;
|
|
236
|
+
const digits = String(pair).padStart(2, '0');
|
|
237
|
+
text += digits;
|
|
238
|
+
bytes.push(digits.charCodeAt(0), digits.charCodeAt(1));
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (cw === 232) {
|
|
242
|
+
if (i === 1)
|
|
243
|
+
gs1 = true;
|
|
244
|
+
else {
|
|
245
|
+
text += '\x1d';
|
|
246
|
+
bytes.push(29);
|
|
247
|
+
}
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (cw === 235) {
|
|
251
|
+
upperShift = true;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if (cw === CW_BASE256) {
|
|
255
|
+
if (i >= data.length)
|
|
256
|
+
throw new FormatError('Data Matrix: Base 256 length is missing');
|
|
257
|
+
let length = unrandomize(data[i], i + 1);
|
|
258
|
+
i++;
|
|
259
|
+
if (length === 0)
|
|
260
|
+
length = data.length - i;
|
|
261
|
+
else if (length >= 250) {
|
|
262
|
+
if (i >= data.length)
|
|
263
|
+
throw new FormatError('Data Matrix: Base 256 extended length is missing');
|
|
264
|
+
length = 250 * (length - 249) + unrandomize(data[i], i + 1);
|
|
265
|
+
i++;
|
|
266
|
+
}
|
|
267
|
+
if (i + length > data.length)
|
|
268
|
+
throw new FormatError('Data Matrix: Base 256 segment exceeds data capacity');
|
|
269
|
+
const segment = new Uint8Array(length);
|
|
270
|
+
for (let n = 0; n < length; n++, i++)
|
|
271
|
+
segment[n] = unrandomize(data[i], i + 1);
|
|
272
|
+
bytes.push(...segment);
|
|
273
|
+
for (let n = 0; n < segment.length; n++)
|
|
274
|
+
text += String.fromCharCode(segment[n]);
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
throw new FormatError(`Data Matrix: unsupported encoding codeword ${cw}`);
|
|
241
278
|
}
|
|
242
|
-
|
|
243
|
-
}
|
|
244
|
-
return { text, bytes: Uint8Array.from(bytes), gs1 };
|
|
279
|
+
return { text, bytes: Uint8Array.from(bytes), gs1 };
|
|
245
280
|
}
|
|
246
|
-
|
|
247
281
|
/**
|
|
248
282
|
* Decode an upright, sampled Data Matrix ECC 200 symbol.
|
|
249
283
|
*
|
|
@@ -251,12 +285,12 @@ function parseData(data) {
|
|
|
251
285
|
* @returns {{text: string, bytes: Uint8Array, correctedErrors: number, symbol: object}}
|
|
252
286
|
*/
|
|
253
287
|
export function decodeDataMatrix(matrix) {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
288
|
+
if (!matrix || !Number.isInteger(matrix.width) || !Number.isInteger(matrix.height))
|
|
289
|
+
throw new FormatError('Data Matrix: no matrix supplied');
|
|
290
|
+
const layout = layoutFor(matrix.width, matrix.height);
|
|
291
|
+
const placement = readPlacement(extractDataModules(matrix, layout), layout.dataRows, layout.dataCols, layout.dataCount + layout.eccCount);
|
|
292
|
+
const { data, corrections } = deinterleaveAndCorrect(placement, layout);
|
|
293
|
+
const result = parseData(data);
|
|
294
|
+
return { ...result, corrections, correctedErrors: corrections, symbol: layout.entry };
|
|
260
295
|
}
|
|
261
|
-
|
|
262
296
|
export { ChecksumError };
|