@sythos/js_barcode_universal 1.1.0 → 1.3.1
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 +24 -18
- package/NOTICE.md +27 -22
- package/README.md +596 -460
- package/bundle/sythos-barcode.esm.js +4606 -228
- package/bundle/sythos-barcode.js +4586 -228
- package/examples/create.html +1011 -731
- package/licenses/README.md +23 -18
- package/licenses/aztec-code.license +9 -7
- package/licenses/codabar.license +16 -13
- package/licenses/code-11.license +11 -9
- package/licenses/code-128.license +8 -6
- package/licenses/code-39.license +8 -6
- package/licenses/code-93.license +13 -11
- package/licenses/data-matrix.license +11 -9
- package/licenses/ean-13.license +7 -5
- package/licenses/ean-8.license +7 -5
- package/licenses/frameqr.license +84 -0
- package/licenses/gs1-128.license +7 -5
- package/licenses/isbn.license +8 -6
- package/licenses/itf-14.license +7 -5
- package/licenses/itf.license +7 -5
- package/licenses/micro-qr.license +79 -0
- package/licenses/micropdf417.license +81 -0
- package/licenses/msi-plessey.license +12 -10
- package/licenses/pdf417.license +78 -0
- package/licenses/pharmacode.license +13 -11
- package/licenses/qr-code.license +7 -5
- package/licenses/rmqr.license +79 -0
- package/licenses/upc-a.license +9 -7
- package/licenses/upc-e.license +7 -5
- package/package.json +104 -88
- package/src/core/reed-solomon.js +326 -312
- package/src/datamatrix/decoder.js +262 -262
- package/src/datamatrix/detector.js +225 -225
- package/src/datamatrix/encoder.js +191 -191
- package/src/datamatrix/index.js +42 -42
- package/src/datamatrix/tables.js +123 -123
- package/src/frameqr/decoder.js +239 -0
- package/src/frameqr/detector.js +192 -0
- package/src/frameqr/encoder.js +156 -0
- package/src/frameqr/index.js +42 -0
- package/src/frameqr/tables.js +270 -0
- package/src/index.js +166 -3
- package/src/micropdf417/compaction.js +116 -0
- package/src/micropdf417/decoder.js +183 -0
- package/src/micropdf417/detector.js +149 -0
- package/src/micropdf417/encoder.js +209 -0
- package/src/micropdf417/error-correction.js +55 -0
- package/src/micropdf417/index.js +49 -0
- package/src/micropdf417/tables.js +184 -0
- package/src/microqr/decoder.js +245 -0
- package/src/microqr/detector.js +355 -0
- package/src/microqr/encoder.js +269 -0
- package/src/microqr/index.js +36 -0
- package/src/microqr/tables.js +316 -0
- package/src/oned/index.js +59 -59
- package/src/pdf417/compaction.js +298 -0
- package/src/pdf417/decoder.js +75 -0
- package/src/pdf417/detector.js +468 -0
- package/src/pdf417/encoder.js +91 -0
- package/src/pdf417/error-correction.js +47 -0
- package/src/pdf417/index.js +6 -0
- package/src/pdf417/tables.js +317 -0
- package/src/rmqr/decoder.js +101 -0
- package/src/rmqr/detector.js +90 -0
- package/src/rmqr/encoder.js +172 -0
- package/src/rmqr/index.js +37 -0
- package/src/rmqr/tables.js +154 -0
|
@@ -1,262 +1,262 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Sythos Barcode Suite
|
|
3
|
-
*
|
|
4
|
-
* MIT License
|
|
5
|
-
*
|
|
6
|
-
* Copyright (c) 2026 Sythos
|
|
7
|
-
*
|
|
8
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
-
* in the Software without restriction, including without limitation the rights
|
|
11
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
-
* furnished to do so, subject to the following conditions:
|
|
14
|
-
*
|
|
15
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
-
* copies or substantial portions of the Software.
|
|
17
|
-
*
|
|
18
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
-
* SOFTWARE.
|
|
25
|
-
*
|
|
26
|
-
* SPDX-License-Identifier: MIT
|
|
27
|
-
*
|
|
28
|
-
* Original work. No code from any other barcode implementation.
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Data Matrix ECC 200 decoder for an already sampled symbol.
|
|
33
|
-
*
|
|
34
|
-
* The detector owns locating, perspective correction and orientation. This
|
|
35
|
-
* module starts with the complete, upright symbol including its finder borders.
|
|
36
|
-
* The table entry is deliberately read through a small normalizer so table data
|
|
37
|
-
* remains declarative: it needs total rows/columns, one data-region's rows and
|
|
38
|
-
* columns, data/ECC codeword counts, and either a block count or data block
|
|
39
|
-
* lengths. The standard 144x144 uneven data blocks are supported.
|
|
40
|
-
*
|
|
41
|
-
* @module datamatrix/decoder
|
|
42
|
-
*/
|
|
43
|
-
|
|
44
|
-
import { ChecksumError, FormatError } from '../core/errors.js';
|
|
45
|
-
import { GF256_DM } from '../core/galois-field.js';
|
|
46
|
-
import { rsDecode } from '../core/reed-solomon.js';
|
|
47
|
-
import { SYMBOLS } from './tables.js';
|
|
48
|
-
|
|
49
|
-
const CW_PAD = 129;
|
|
50
|
-
const CW_BASE256 = 231;
|
|
51
|
-
|
|
52
|
-
/** @param {object} entry @param {...string} names @returns {number | undefined} */
|
|
53
|
-
function numberField(entry, ...names) {
|
|
54
|
-
for (const name of names) if (Number.isInteger(entry[name])) return entry[name];
|
|
55
|
-
return undefined;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** Normalize the public table entry into the decoder's geometry contract. */
|
|
59
|
-
function layoutFor(width, height) {
|
|
60
|
-
const entry = SYMBOLS.find((s) =>
|
|
61
|
-
numberField(s, 'columns', 'cols', 'matrixColumns', 'width') === width &&
|
|
62
|
-
numberField(s, 'rows', 'matrixRows', 'height') === height);
|
|
63
|
-
if (!entry) throw new FormatError(`Data Matrix: ${width}x${height} is not an ECC 200 symbol size`);
|
|
64
|
-
|
|
65
|
-
const regionRows = numberField(entry, 'dataRegionRows', 'regionRows') ??
|
|
66
|
-
(numberField(entry, 'regionHeight') ? numberField(entry, 'regionHeight') - 2 : undefined);
|
|
67
|
-
const regionCols = numberField(entry, 'dataRegionColumns', 'dataRegionCols', 'regionColumns') ??
|
|
68
|
-
(numberField(entry, 'regionWidth') ? numberField(entry, 'regionWidth') - 2 : undefined);
|
|
69
|
-
const dataCount = numberField(entry, 'dataCodewords', 'dataCapacity');
|
|
70
|
-
const eccCount = numberField(entry, 'errorCodewords', 'eccCodewords');
|
|
71
|
-
const blockCount = numberField(entry, 'interleavedBlocks', 'interleavedBlockCount', 'blockCount', 'rsBlocks') || 1;
|
|
72
|
-
if (!regionRows || !regionCols || dataCount === undefined || eccCount === undefined ||
|
|
73
|
-
height % (regionRows + 2) || width % (regionCols + 2) || eccCount % blockCount) {
|
|
74
|
-
throw new FormatError(`Data Matrix: invalid table layout for ${width}x${height}`);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const rows = height / (regionRows + 2);
|
|
78
|
-
const cols = width / (regionCols + 2);
|
|
79
|
-
const blockData = Array.isArray(entry.blockDataCodewords) ? entry.blockDataCodewords.slice() :
|
|
80
|
-
Array.isArray(entry.dataCodewordsPerBlock) ? entry.dataCodewordsPerBlock.slice() : null;
|
|
81
|
-
let dataLengths;
|
|
82
|
-
if (blockData) {
|
|
83
|
-
dataLengths = blockData;
|
|
84
|
-
} else {
|
|
85
|
-
// The sole uneven ECC 200 distribution is 144x144: its first eight of ten
|
|
86
|
-
// blocks contain one extra data codeword. This derives it instead of hiding
|
|
87
|
-
// a magic size check in the deinterleaver.
|
|
88
|
-
const short = Math.floor(dataCount / blockCount);
|
|
89
|
-
dataLengths = new Array(blockCount).fill(short);
|
|
90
|
-
for (let i = 0; i < dataCount % blockCount; i++) dataLengths[i]++;
|
|
91
|
-
}
|
|
92
|
-
if (dataLengths.length !== blockCount || dataLengths.reduce((a, b) => a + b, 0) !== dataCount) {
|
|
93
|
-
throw new FormatError(`Data Matrix: inconsistent block layout for ${width}x${height}`);
|
|
94
|
-
}
|
|
95
|
-
return { entry, regionRows, regionCols, regionRowCount: rows, regionColCount: cols,
|
|
96
|
-
dataRows: rows * regionRows, dataCols: cols * regionCols, dataCount, eccCount,
|
|
97
|
-
blockCount, eccPerBlock: eccCount / blockCount, dataLengths };
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/** Remove the L/finders from every data region, retaining only placement modules. */
|
|
101
|
-
function extractDataModules(matrix, layout) {
|
|
102
|
-
const data = new Uint8Array(layout.dataRows * layout.dataCols);
|
|
103
|
-
for (let regionY = 0; regionY < layout.regionRowCount; regionY++) {
|
|
104
|
-
for (let regionX = 0; regionX < layout.regionColCount; regionX++) {
|
|
105
|
-
const sourceX = regionX * (layout.regionCols + 2) + 1;
|
|
106
|
-
const sourceY = regionY * (layout.regionRows + 2) + 1;
|
|
107
|
-
for (let y = 0; y < layout.regionRows; y++) {
|
|
108
|
-
const targetY = regionY * layout.regionRows + y;
|
|
109
|
-
for (let x = 0; x < layout.regionCols; x++) {
|
|
110
|
-
data[targetY * layout.dataCols + regionX * layout.regionCols + x] =
|
|
111
|
-
matrix.get(sourceX + x, sourceY + y) ? 1 : 0;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
return data;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/** Read placement codewords using the ECC 200 Utah sweep (the inverse writer path). */
|
|
120
|
-
function readPlacement(modules, rows, cols, count) {
|
|
121
|
-
const seen = new Uint8Array(rows * cols);
|
|
122
|
-
const out = new Uint8Array(count);
|
|
123
|
-
const get = (row, col) => modules[row * cols + col] !== 0;
|
|
124
|
-
const module = (row, col) => {
|
|
125
|
-
if (row < 0) { row += rows; col += 4 - ((rows + 4) % 8); }
|
|
126
|
-
if (col < 0) { col += cols; row += 4 - ((cols + 4) % 8); }
|
|
127
|
-
if (row < 0 || row >= rows || col < 0 || col >= cols) {
|
|
128
|
-
throw new FormatError('Data Matrix: placement coordinate escaped data region');
|
|
129
|
-
}
|
|
130
|
-
seen[row * cols + col] = 1;
|
|
131
|
-
return get(row, col) ? 1 : 0;
|
|
132
|
-
};
|
|
133
|
-
const bits = (coords) => coords.reduce((value, p) => (value << 1) | module(p[0], p[1]), 0);
|
|
134
|
-
const utah = (row, col) => bits([[row - 2, col - 2], [row - 2, col - 1], [row - 1, col - 2], [row - 1, col - 1],
|
|
135
|
-
[row - 1, col], [row, col - 2], [row, col - 1], [row, col]]);
|
|
136
|
-
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]]);
|
|
137
|
-
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]]);
|
|
138
|
-
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]]);
|
|
139
|
-
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]]);
|
|
140
|
-
|
|
141
|
-
let row = 4, col = 0, n = 0;
|
|
142
|
-
const put = (value) => { if (n < count) out[n++] = value; };
|
|
143
|
-
do {
|
|
144
|
-
if (row === rows && col === 0) put(corner1());
|
|
145
|
-
if (row === rows - 2 && col === 0 && cols % 4 !== 0) put(corner2());
|
|
146
|
-
if (row === rows - 2 && col === 0 && cols % 8 === 4) put(corner3());
|
|
147
|
-
if (row === rows + 4 && col === 2 && cols % 8 === 0) put(corner4());
|
|
148
|
-
do { if (row < rows && col >= 0 && !seen[row * cols + col]) put(utah(row, col)); row -= 2; col += 2; } while (row >= 0 && col < cols);
|
|
149
|
-
row += 1; col += 3;
|
|
150
|
-
do { if (row >= 0 && col < cols && !seen[row * cols + col]) put(utah(row, col)); row += 2; col -= 2; } while (row < rows && col >= 0);
|
|
151
|
-
row += 3; col += 1;
|
|
152
|
-
} while (row < rows || col < cols);
|
|
153
|
-
if (n !== count) throw new FormatError(`Data Matrix: placement yielded ${n}, expected ${count} codewords`);
|
|
154
|
-
return out;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/** Restore RS blocks, correct them, then concatenate their data portions. */
|
|
158
|
-
function deinterleaveAndCorrect(codewords, layout) {
|
|
159
|
-
if (codewords.length !== layout.dataCount + layout.eccCount) throw new FormatError('Data Matrix: codeword count mismatch');
|
|
160
|
-
const blocks = layout.dataLengths.map((len) => new Uint8Array(len + layout.eccPerBlock));
|
|
161
|
-
// Data codewords arrive in their original stream order. ECC 200 deals that
|
|
162
|
-
// stream round-robin across the RS blocks, so the inverse is determined by
|
|
163
|
-
// the wire index rather than by splitting it into consecutive block-sized
|
|
164
|
-
// chunks. For 144x144, indices 1550..1557 naturally land in the eight long
|
|
165
|
-
// blocks while the two short blocks remain at 155 data codewords.
|
|
166
|
-
for (let i = 0; i < layout.dataCount; i++) {
|
|
167
|
-
blocks[i % layout.blockCount][Math.floor(i / layout.blockCount)] = codewords[i];
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// Parity normally begins with block zero. The uneven 144x144 layout rotates
|
|
171
|
-
// the parity wire order to begin with its first short block; derive the same
|
|
172
|
-
// mapping from the declarative lengths instead of keying it to dimensions.
|
|
173
|
-
const longest = Math.max(...layout.dataLengths);
|
|
174
|
-
const firstShort = layout.dataLengths.findIndex((length) => length < longest);
|
|
175
|
-
const rotation = firstShort < 0 ? 0 : firstShort;
|
|
176
|
-
let at = layout.dataCount;
|
|
177
|
-
for (let i = 0; i < layout.eccPerBlock; i++) {
|
|
178
|
-
for (let slot = 0; slot < layout.blockCount; slot++) {
|
|
179
|
-
const block = (slot + rotation) % layout.blockCount;
|
|
180
|
-
blocks[block][layout.dataLengths[block] + i] = codewords[at++];
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
let corrections = 0;
|
|
185
|
-
const data = new Uint8Array(layout.dataCount);
|
|
186
|
-
for (let b = 0; b < blocks.length; b++) {
|
|
187
|
-
corrections += rsDecode(blocks[b], layout.eccPerBlock, GF256_DM, 1);
|
|
188
|
-
}
|
|
189
|
-
// Rebuild the original high-level codeword stream after correction. Keeping
|
|
190
|
-
// this in wire-index order is essential: concatenating block data passes
|
|
191
|
-
// single-block round trips but scrambles every multi-block payload.
|
|
192
|
-
for (let i = 0; i < layout.dataCount; i++) {
|
|
193
|
-
data[i] = blocks[i % layout.blockCount][Math.floor(i / layout.blockCount)];
|
|
194
|
-
}
|
|
195
|
-
return { data, corrections };
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function unrandomize(value, position) {
|
|
199
|
-
const pseudo = ((149 * position) % 255) + 1;
|
|
200
|
-
return value - pseudo >= 0 ? value - pseudo : value - pseudo + 256;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/** Decode ASCII plus Base 256, preserving semantic bytes alongside text. */
|
|
204
|
-
function parseData(data) {
|
|
205
|
-
let text = '';
|
|
206
|
-
const bytes = [];
|
|
207
|
-
let upperShift = false;
|
|
208
|
-
let gs1 = false;
|
|
209
|
-
for (let i = 0; i < data.length;) {
|
|
210
|
-
const cw = data[i++];
|
|
211
|
-
if (cw === CW_PAD) break;
|
|
212
|
-
if (cw <= 128) {
|
|
213
|
-
const value = cw - 1 + (upperShift ? 128 : 0);
|
|
214
|
-
upperShift = false;
|
|
215
|
-
text += String.fromCharCode(value); bytes.push(value); continue;
|
|
216
|
-
}
|
|
217
|
-
if (cw <= 229) {
|
|
218
|
-
const pair = cw - 130;
|
|
219
|
-
const digits = String(pair).padStart(2, '0'); text += digits; bytes.push(digits.charCodeAt(0), digits.charCodeAt(1)); continue;
|
|
220
|
-
}
|
|
221
|
-
if (cw === 232) {
|
|
222
|
-
if (i === 1) gs1 = true;
|
|
223
|
-
else { text += '\x1d'; bytes.push(29); }
|
|
224
|
-
continue;
|
|
225
|
-
}
|
|
226
|
-
if (cw === 235) { upperShift = true; continue; }
|
|
227
|
-
if (cw === CW_BASE256) {
|
|
228
|
-
if (i >= data.length) throw new FormatError('Data Matrix: Base 256 length is missing');
|
|
229
|
-
let length = unrandomize(data[i], i + 1); i++;
|
|
230
|
-
if (length === 0) length = data.length - i;
|
|
231
|
-
else if (length >= 250) {
|
|
232
|
-
if (i >= data.length) throw new FormatError('Data Matrix: Base 256 extended length is missing');
|
|
233
|
-
length = 250 * (length - 249) + unrandomize(data[i], i + 1); i++;
|
|
234
|
-
}
|
|
235
|
-
if (i + length > data.length) throw new FormatError('Data Matrix: Base 256 segment exceeds data capacity');
|
|
236
|
-
const segment = new Uint8Array(length);
|
|
237
|
-
for (let n = 0; n < length; n++, i++) segment[n] = unrandomize(data[i], i + 1);
|
|
238
|
-
bytes.push(...segment);
|
|
239
|
-
for (let n = 0; n < segment.length; n++) text += String.fromCharCode(segment[n]);
|
|
240
|
-
continue;
|
|
241
|
-
}
|
|
242
|
-
throw new FormatError(`Data Matrix: unsupported encoding codeword ${cw}`);
|
|
243
|
-
}
|
|
244
|
-
return { text, bytes: Uint8Array.from(bytes), gs1 };
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* Decode an upright, sampled Data Matrix ECC 200 symbol.
|
|
249
|
-
*
|
|
250
|
-
* @param {import('../core/bit-matrix.js').BitMatrix} matrix Full symbol, no quiet zone.
|
|
251
|
-
* @returns {{text: string, bytes: Uint8Array, correctedErrors: number, symbol: object}}
|
|
252
|
-
*/
|
|
253
|
-
export function decodeDataMatrix(matrix) {
|
|
254
|
-
if (!matrix || !Number.isInteger(matrix.width) || !Number.isInteger(matrix.height)) throw new FormatError('Data Matrix: no matrix supplied');
|
|
255
|
-
const layout = layoutFor(matrix.width, matrix.height);
|
|
256
|
-
const placement = readPlacement(extractDataModules(matrix, layout), layout.dataRows, layout.dataCols, layout.dataCount + layout.eccCount);
|
|
257
|
-
const { data, corrections } = deinterleaveAndCorrect(placement, layout);
|
|
258
|
-
const result = parseData(data);
|
|
259
|
-
return { ...result, corrections, correctedErrors: corrections, symbol: layout.entry };
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
export { ChecksumError };
|
|
1
|
+
/*!
|
|
2
|
+
* Sythos Barcode Suite
|
|
3
|
+
*
|
|
4
|
+
* MIT License
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) 2026 Sythos
|
|
7
|
+
*
|
|
8
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
* in the Software without restriction, including without limitation the rights
|
|
11
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
* furnished to do so, subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
* copies or substantial portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
* SOFTWARE.
|
|
25
|
+
*
|
|
26
|
+
* SPDX-License-Identifier: MIT
|
|
27
|
+
*
|
|
28
|
+
* Original work. No code from any other barcode implementation.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Data Matrix ECC 200 decoder for an already sampled symbol.
|
|
33
|
+
*
|
|
34
|
+
* The detector owns locating, perspective correction and orientation. This
|
|
35
|
+
* module starts with the complete, upright symbol including its finder borders.
|
|
36
|
+
* The table entry is deliberately read through a small normalizer so table data
|
|
37
|
+
* remains declarative: it needs total rows/columns, one data-region's rows and
|
|
38
|
+
* columns, data/ECC codeword counts, and either a block count or data block
|
|
39
|
+
* lengths. The standard 144x144 uneven data blocks are supported.
|
|
40
|
+
*
|
|
41
|
+
* @module datamatrix/decoder
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
import { ChecksumError, FormatError } from '../core/errors.js';
|
|
45
|
+
import { GF256_DM } from '../core/galois-field.js';
|
|
46
|
+
import { rsDecode } from '../core/reed-solomon.js';
|
|
47
|
+
import { SYMBOLS } from './tables.js';
|
|
48
|
+
|
|
49
|
+
const CW_PAD = 129;
|
|
50
|
+
const CW_BASE256 = 231;
|
|
51
|
+
|
|
52
|
+
/** @param {object} entry @param {...string} names @returns {number | undefined} */
|
|
53
|
+
function numberField(entry, ...names) {
|
|
54
|
+
for (const name of names) if (Number.isInteger(entry[name])) return entry[name];
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Normalize the public table entry into the decoder's geometry contract. */
|
|
59
|
+
function layoutFor(width, height) {
|
|
60
|
+
const entry = SYMBOLS.find((s) =>
|
|
61
|
+
numberField(s, 'columns', 'cols', 'matrixColumns', 'width') === width &&
|
|
62
|
+
numberField(s, 'rows', 'matrixRows', 'height') === height);
|
|
63
|
+
if (!entry) throw new FormatError(`Data Matrix: ${width}x${height} is not an ECC 200 symbol size`);
|
|
64
|
+
|
|
65
|
+
const regionRows = numberField(entry, 'dataRegionRows', 'regionRows') ??
|
|
66
|
+
(numberField(entry, 'regionHeight') ? numberField(entry, 'regionHeight') - 2 : undefined);
|
|
67
|
+
const regionCols = numberField(entry, 'dataRegionColumns', 'dataRegionCols', 'regionColumns') ??
|
|
68
|
+
(numberField(entry, 'regionWidth') ? numberField(entry, 'regionWidth') - 2 : undefined);
|
|
69
|
+
const dataCount = numberField(entry, 'dataCodewords', 'dataCapacity');
|
|
70
|
+
const eccCount = numberField(entry, 'errorCodewords', 'eccCodewords');
|
|
71
|
+
const blockCount = numberField(entry, 'interleavedBlocks', 'interleavedBlockCount', 'blockCount', 'rsBlocks') || 1;
|
|
72
|
+
if (!regionRows || !regionCols || dataCount === undefined || eccCount === undefined ||
|
|
73
|
+
height % (regionRows + 2) || width % (regionCols + 2) || eccCount % blockCount) {
|
|
74
|
+
throw new FormatError(`Data Matrix: invalid table layout for ${width}x${height}`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const rows = height / (regionRows + 2);
|
|
78
|
+
const cols = width / (regionCols + 2);
|
|
79
|
+
const blockData = Array.isArray(entry.blockDataCodewords) ? entry.blockDataCodewords.slice() :
|
|
80
|
+
Array.isArray(entry.dataCodewordsPerBlock) ? entry.dataCodewordsPerBlock.slice() : null;
|
|
81
|
+
let dataLengths;
|
|
82
|
+
if (blockData) {
|
|
83
|
+
dataLengths = blockData;
|
|
84
|
+
} else {
|
|
85
|
+
// The sole uneven ECC 200 distribution is 144x144: its first eight of ten
|
|
86
|
+
// blocks contain one extra data codeword. This derives it instead of hiding
|
|
87
|
+
// a magic size check in the deinterleaver.
|
|
88
|
+
const short = Math.floor(dataCount / blockCount);
|
|
89
|
+
dataLengths = new Array(blockCount).fill(short);
|
|
90
|
+
for (let i = 0; i < dataCount % blockCount; i++) dataLengths[i]++;
|
|
91
|
+
}
|
|
92
|
+
if (dataLengths.length !== blockCount || dataLengths.reduce((a, b) => a + b, 0) !== dataCount) {
|
|
93
|
+
throw new FormatError(`Data Matrix: inconsistent block layout for ${width}x${height}`);
|
|
94
|
+
}
|
|
95
|
+
return { entry, regionRows, regionCols, regionRowCount: rows, regionColCount: cols,
|
|
96
|
+
dataRows: rows * regionRows, dataCols: cols * regionCols, dataCount, eccCount,
|
|
97
|
+
blockCount, eccPerBlock: eccCount / blockCount, dataLengths };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Remove the L/finders from every data region, retaining only placement modules. */
|
|
101
|
+
function extractDataModules(matrix, layout) {
|
|
102
|
+
const data = new Uint8Array(layout.dataRows * layout.dataCols);
|
|
103
|
+
for (let regionY = 0; regionY < layout.regionRowCount; regionY++) {
|
|
104
|
+
for (let regionX = 0; regionX < layout.regionColCount; regionX++) {
|
|
105
|
+
const sourceX = regionX * (layout.regionCols + 2) + 1;
|
|
106
|
+
const sourceY = regionY * (layout.regionRows + 2) + 1;
|
|
107
|
+
for (let y = 0; y < layout.regionRows; y++) {
|
|
108
|
+
const targetY = regionY * layout.regionRows + y;
|
|
109
|
+
for (let x = 0; x < layout.regionCols; x++) {
|
|
110
|
+
data[targetY * layout.dataCols + regionX * layout.regionCols + x] =
|
|
111
|
+
matrix.get(sourceX + x, sourceY + y) ? 1 : 0;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return data;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Read placement codewords using the ECC 200 Utah sweep (the inverse writer path). */
|
|
120
|
+
function readPlacement(modules, rows, cols, count) {
|
|
121
|
+
const seen = new Uint8Array(rows * cols);
|
|
122
|
+
const out = new Uint8Array(count);
|
|
123
|
+
const get = (row, col) => modules[row * cols + col] !== 0;
|
|
124
|
+
const module = (row, col) => {
|
|
125
|
+
if (row < 0) { row += rows; col += 4 - ((rows + 4) % 8); }
|
|
126
|
+
if (col < 0) { col += cols; row += 4 - ((cols + 4) % 8); }
|
|
127
|
+
if (row < 0 || row >= rows || col < 0 || col >= cols) {
|
|
128
|
+
throw new FormatError('Data Matrix: placement coordinate escaped data region');
|
|
129
|
+
}
|
|
130
|
+
seen[row * cols + col] = 1;
|
|
131
|
+
return get(row, col) ? 1 : 0;
|
|
132
|
+
};
|
|
133
|
+
const bits = (coords) => coords.reduce((value, p) => (value << 1) | module(p[0], p[1]), 0);
|
|
134
|
+
const utah = (row, col) => bits([[row - 2, col - 2], [row - 2, col - 1], [row - 1, col - 2], [row - 1, col - 1],
|
|
135
|
+
[row - 1, col], [row, col - 2], [row, col - 1], [row, col]]);
|
|
136
|
+
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]]);
|
|
137
|
+
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]]);
|
|
138
|
+
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]]);
|
|
139
|
+
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]]);
|
|
140
|
+
|
|
141
|
+
let row = 4, col = 0, n = 0;
|
|
142
|
+
const put = (value) => { if (n < count) out[n++] = value; };
|
|
143
|
+
do {
|
|
144
|
+
if (row === rows && col === 0) put(corner1());
|
|
145
|
+
if (row === rows - 2 && col === 0 && cols % 4 !== 0) put(corner2());
|
|
146
|
+
if (row === rows - 2 && col === 0 && cols % 8 === 4) put(corner3());
|
|
147
|
+
if (row === rows + 4 && col === 2 && cols % 8 === 0) put(corner4());
|
|
148
|
+
do { if (row < rows && col >= 0 && !seen[row * cols + col]) put(utah(row, col)); row -= 2; col += 2; } while (row >= 0 && col < cols);
|
|
149
|
+
row += 1; col += 3;
|
|
150
|
+
do { if (row >= 0 && col < cols && !seen[row * cols + col]) put(utah(row, col)); row += 2; col -= 2; } while (row < rows && col >= 0);
|
|
151
|
+
row += 3; col += 1;
|
|
152
|
+
} while (row < rows || col < cols);
|
|
153
|
+
if (n !== count) throw new FormatError(`Data Matrix: placement yielded ${n}, expected ${count} codewords`);
|
|
154
|
+
return out;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Restore RS blocks, correct them, then concatenate their data portions. */
|
|
158
|
+
function deinterleaveAndCorrect(codewords, layout) {
|
|
159
|
+
if (codewords.length !== layout.dataCount + layout.eccCount) throw new FormatError('Data Matrix: codeword count mismatch');
|
|
160
|
+
const blocks = layout.dataLengths.map((len) => new Uint8Array(len + layout.eccPerBlock));
|
|
161
|
+
// Data codewords arrive in their original stream order. ECC 200 deals that
|
|
162
|
+
// stream round-robin across the RS blocks, so the inverse is determined by
|
|
163
|
+
// the wire index rather than by splitting it into consecutive block-sized
|
|
164
|
+
// chunks. For 144x144, indices 1550..1557 naturally land in the eight long
|
|
165
|
+
// blocks while the two short blocks remain at 155 data codewords.
|
|
166
|
+
for (let i = 0; i < layout.dataCount; i++) {
|
|
167
|
+
blocks[i % layout.blockCount][Math.floor(i / layout.blockCount)] = codewords[i];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Parity normally begins with block zero. The uneven 144x144 layout rotates
|
|
171
|
+
// the parity wire order to begin with its first short block; derive the same
|
|
172
|
+
// mapping from the declarative lengths instead of keying it to dimensions.
|
|
173
|
+
const longest = Math.max(...layout.dataLengths);
|
|
174
|
+
const firstShort = layout.dataLengths.findIndex((length) => length < longest);
|
|
175
|
+
const rotation = firstShort < 0 ? 0 : firstShort;
|
|
176
|
+
let at = layout.dataCount;
|
|
177
|
+
for (let i = 0; i < layout.eccPerBlock; i++) {
|
|
178
|
+
for (let slot = 0; slot < layout.blockCount; slot++) {
|
|
179
|
+
const block = (slot + rotation) % layout.blockCount;
|
|
180
|
+
blocks[block][layout.dataLengths[block] + i] = codewords[at++];
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
let corrections = 0;
|
|
185
|
+
const data = new Uint8Array(layout.dataCount);
|
|
186
|
+
for (let b = 0; b < blocks.length; b++) {
|
|
187
|
+
corrections += rsDecode(blocks[b], layout.eccPerBlock, GF256_DM, 1);
|
|
188
|
+
}
|
|
189
|
+
// Rebuild the original high-level codeword stream after correction. Keeping
|
|
190
|
+
// this in wire-index order is essential: concatenating block data passes
|
|
191
|
+
// single-block round trips but scrambles every multi-block payload.
|
|
192
|
+
for (let i = 0; i < layout.dataCount; i++) {
|
|
193
|
+
data[i] = blocks[i % layout.blockCount][Math.floor(i / layout.blockCount)];
|
|
194
|
+
}
|
|
195
|
+
return { data, corrections };
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function unrandomize(value, position) {
|
|
199
|
+
const pseudo = ((149 * position) % 255) + 1;
|
|
200
|
+
return value - pseudo >= 0 ? value - pseudo : value - pseudo + 256;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Decode ASCII plus Base 256, preserving semantic bytes alongside text. */
|
|
204
|
+
function parseData(data) {
|
|
205
|
+
let text = '';
|
|
206
|
+
const bytes = [];
|
|
207
|
+
let upperShift = false;
|
|
208
|
+
let gs1 = false;
|
|
209
|
+
for (let i = 0; i < data.length;) {
|
|
210
|
+
const cw = data[i++];
|
|
211
|
+
if (cw === CW_PAD) break;
|
|
212
|
+
if (cw <= 128) {
|
|
213
|
+
const value = cw - 1 + (upperShift ? 128 : 0);
|
|
214
|
+
upperShift = false;
|
|
215
|
+
text += String.fromCharCode(value); bytes.push(value); continue;
|
|
216
|
+
}
|
|
217
|
+
if (cw <= 229) {
|
|
218
|
+
const pair = cw - 130;
|
|
219
|
+
const digits = String(pair).padStart(2, '0'); text += digits; bytes.push(digits.charCodeAt(0), digits.charCodeAt(1)); continue;
|
|
220
|
+
}
|
|
221
|
+
if (cw === 232) {
|
|
222
|
+
if (i === 1) gs1 = true;
|
|
223
|
+
else { text += '\x1d'; bytes.push(29); }
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
if (cw === 235) { upperShift = true; continue; }
|
|
227
|
+
if (cw === CW_BASE256) {
|
|
228
|
+
if (i >= data.length) throw new FormatError('Data Matrix: Base 256 length is missing');
|
|
229
|
+
let length = unrandomize(data[i], i + 1); i++;
|
|
230
|
+
if (length === 0) length = data.length - i;
|
|
231
|
+
else if (length >= 250) {
|
|
232
|
+
if (i >= data.length) throw new FormatError('Data Matrix: Base 256 extended length is missing');
|
|
233
|
+
length = 250 * (length - 249) + unrandomize(data[i], i + 1); i++;
|
|
234
|
+
}
|
|
235
|
+
if (i + length > data.length) throw new FormatError('Data Matrix: Base 256 segment exceeds data capacity');
|
|
236
|
+
const segment = new Uint8Array(length);
|
|
237
|
+
for (let n = 0; n < length; n++, i++) segment[n] = unrandomize(data[i], i + 1);
|
|
238
|
+
bytes.push(...segment);
|
|
239
|
+
for (let n = 0; n < segment.length; n++) text += String.fromCharCode(segment[n]);
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
throw new FormatError(`Data Matrix: unsupported encoding codeword ${cw}`);
|
|
243
|
+
}
|
|
244
|
+
return { text, bytes: Uint8Array.from(bytes), gs1 };
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Decode an upright, sampled Data Matrix ECC 200 symbol.
|
|
249
|
+
*
|
|
250
|
+
* @param {import('../core/bit-matrix.js').BitMatrix} matrix Full symbol, no quiet zone.
|
|
251
|
+
* @returns {{text: string, bytes: Uint8Array, correctedErrors: number, symbol: object}}
|
|
252
|
+
*/
|
|
253
|
+
export function decodeDataMatrix(matrix) {
|
|
254
|
+
if (!matrix || !Number.isInteger(matrix.width) || !Number.isInteger(matrix.height)) throw new FormatError('Data Matrix: no matrix supplied');
|
|
255
|
+
const layout = layoutFor(matrix.width, matrix.height);
|
|
256
|
+
const placement = readPlacement(extractDataModules(matrix, layout), layout.dataRows, layout.dataCols, layout.dataCount + layout.eccCount);
|
|
257
|
+
const { data, corrections } = deinterleaveAndCorrect(placement, layout);
|
|
258
|
+
const result = parseData(data);
|
|
259
|
+
return { ...result, corrections, correctedErrors: corrections, symbol: layout.entry };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export { ChecksumError };
|