@sythos/js_barcode_universal 1.5.7 → 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.
Files changed (267) hide show
  1. package/LICENSE +5 -4
  2. package/NOTICE.md +2 -2
  3. package/README.md +50 -37
  4. package/bundle/sythos-barcode.esm.js +12051 -11484
  5. package/bundle/sythos-barcode.js +12051 -11484
  6. package/package.json +52 -45
  7. package/src/index.d.ts +363 -0
  8. package/src/index.js +573 -428
  9. package/src/js/aztec/decoder.js +326 -0
  10. package/src/js/aztec/detector.js +251 -0
  11. package/src/js/aztec/encoder.js +276 -0
  12. package/src/js/aztec/high-level.js +216 -0
  13. package/src/js/aztec/index.js +34 -0
  14. package/src/js/aztec/tables.js +196 -0
  15. package/src/js/aztecrune/decoder.js +146 -0
  16. package/src/js/aztecrune/detector.js +180 -0
  17. package/src/js/aztecrune/encoder.js +103 -0
  18. package/src/js/aztecrune/index.js +34 -0
  19. package/src/js/aztecrune/tables.js +142 -0
  20. package/src/js/compactpdf417/decoder.js +123 -0
  21. package/src/js/compactpdf417/detector.js +138 -0
  22. package/src/js/compactpdf417/encoder.js +127 -0
  23. package/src/js/compactpdf417/index.js +5 -0
  24. package/src/js/compactpdf417/tables.js +164 -0
  25. package/src/js/core/bit-buffer.js +158 -0
  26. package/src/js/core/bit-matrix.js +243 -0
  27. package/src/js/core/errors.js +59 -0
  28. package/src/js/core/galois-field.js +193 -0
  29. package/src/js/core/index.js +39 -0
  30. package/src/js/core/reed-solomon.js +318 -0
  31. package/src/js/databar/codec.js +166 -0
  32. package/src/js/databar/decoder.js +224 -0
  33. package/src/js/databar/encoder.js +88 -0
  34. package/src/js/databar/gs1.js +176 -0
  35. package/src/js/databar/index.js +36 -0
  36. package/src/js/databar/patterns.js +139 -0
  37. package/src/js/databar/tables.js +123 -0
  38. package/src/js/datamatrix/decoder.js +296 -0
  39. package/src/js/datamatrix/detector.js +254 -0
  40. package/src/js/datamatrix/encoder.js +250 -0
  41. package/src/js/datamatrix/index.js +34 -0
  42. package/src/js/datamatrix/tables.js +122 -0
  43. package/src/js/frameqr/decoder.js +216 -0
  44. package/src/js/frameqr/detector.js +199 -0
  45. package/src/js/frameqr/encoder.js +143 -0
  46. package/src/js/frameqr/index.js +33 -0
  47. package/src/js/frameqr/tables.js +252 -0
  48. package/src/js/image/binarizer.js +254 -0
  49. package/src/js/image/grid-sampler.js +148 -0
  50. package/src/js/image/index.js +38 -0
  51. package/src/js/image/luminance.js +185 -0
  52. package/src/js/image/perspective.js +158 -0
  53. package/src/js/micropdf417/compaction.js +115 -0
  54. package/src/js/micropdf417/decoder.js +180 -0
  55. package/src/js/micropdf417/detector.js +152 -0
  56. package/src/js/micropdf417/encoder.js +184 -0
  57. package/src/js/micropdf417/error-correction.js +50 -0
  58. package/src/js/micropdf417/index.js +35 -0
  59. package/src/js/micropdf417/tables.js +197 -0
  60. package/src/js/microqr/decoder.js +240 -0
  61. package/src/js/microqr/detector.js +339 -0
  62. package/src/js/microqr/encoder.js +314 -0
  63. package/src/js/microqr/index.js +34 -0
  64. package/src/js/microqr/tables.js +324 -0
  65. package/src/js/oned/addons.js +396 -0
  66. package/src/js/oned/index.js +75 -0
  67. package/src/js/oned/patterns.js +365 -0
  68. package/src/js/oned/reader.js +1383 -0
  69. package/src/js/oned/writers.js +691 -0
  70. package/src/js/pdf417/compaction.js +391 -0
  71. package/src/js/pdf417/decoder.js +110 -0
  72. package/src/js/pdf417/detector.js +484 -0
  73. package/src/js/pdf417/encoder.js +102 -0
  74. package/src/js/pdf417/error-correction.js +44 -0
  75. package/src/{pdf417 → js/pdf417}/index.js +0 -5
  76. package/src/js/pdf417/tables.js +315 -0
  77. package/src/js/qr/decoder.js +539 -0
  78. package/src/js/qr/detector.js +613 -0
  79. package/src/js/qr/encoder.js +873 -0
  80. package/src/js/qr/index.js +42 -0
  81. package/src/js/qr/tables.js +690 -0
  82. package/src/js/render/image-data.js +120 -0
  83. package/src/js/render/index.js +128 -0
  84. package/src/js/render/options.js +152 -0
  85. package/src/js/render/png.js +270 -0
  86. package/src/js/render/svg.js +117 -0
  87. package/src/js/render/webgl.js +200 -0
  88. package/src/js/render/webgpu.js +357 -0
  89. package/src/js/rmqr/decoder.js +209 -0
  90. package/src/js/rmqr/detector.js +122 -0
  91. package/src/js/rmqr/encoder.js +321 -0
  92. package/src/js/rmqr/index.js +33 -0
  93. package/src/js/rmqr/tables.js +177 -0
  94. package/src/{aztec/decoder.js → ts/aztec/decoder.ts} +0 -1
  95. package/{typings → src/ts}/aztec/detector.d.ts +2 -2
  96. package/src/{aztec/detector.js → ts/aztec/detector.ts} +0 -1
  97. package/src/{aztec/encoder.js → ts/aztec/encoder.ts} +0 -1
  98. package/src/{aztec/high-level.js → ts/aztec/high-level.ts} +0 -1
  99. package/src/{aztec/index.js → ts/aztec/index.ts} +0 -1
  100. package/src/{aztec/tables.js → ts/aztec/tables.ts} +0 -1
  101. package/src/{aztecrune/decoder.js → ts/aztecrune/decoder.ts} +0 -1
  102. package/src/{aztecrune/detector.js → ts/aztecrune/detector.ts} +0 -1
  103. package/src/{aztecrune/encoder.js → ts/aztecrune/encoder.ts} +0 -1
  104. package/src/{aztecrune/index.js → ts/aztecrune/index.ts} +0 -1
  105. package/src/{aztecrune/tables.js → ts/aztecrune/tables.ts} +0 -1
  106. package/src/{compactpdf417/decoder.js → ts/compactpdf417/decoder.ts} +0 -1
  107. package/src/{compactpdf417/detector.js → ts/compactpdf417/detector.ts} +0 -1
  108. package/src/{compactpdf417/encoder.js → ts/compactpdf417/encoder.ts} +0 -1
  109. package/src/{compactpdf417/index.js → ts/compactpdf417/index.ts} +0 -5
  110. package/src/{compactpdf417/tables.js → ts/compactpdf417/tables.ts} +0 -1
  111. package/src/{core/bit-buffer.js → ts/core/bit-buffer.ts} +0 -1
  112. package/src/{core/bit-matrix.js → ts/core/bit-matrix.ts} +0 -1
  113. package/src/{core/errors.js → ts/core/errors.ts} +0 -1
  114. package/src/{core/galois-field.js → ts/core/galois-field.ts} +0 -1
  115. package/src/{core/index.js → ts/core/index.ts} +0 -1
  116. package/src/{core/reed-solomon.js → ts/core/reed-solomon.ts} +0 -1
  117. package/src/{databar/codec.js → ts/databar/codec.ts} +0 -1
  118. package/src/{databar/decoder.js → ts/databar/decoder.ts} +0 -1
  119. package/src/{databar/encoder.js → ts/databar/encoder.ts} +0 -1
  120. package/src/{databar/gs1.js → ts/databar/gs1.ts} +0 -1
  121. package/src/{databar/index.js → ts/databar/index.ts} +0 -1
  122. package/src/{databar/patterns.js → ts/databar/patterns.ts} +0 -1
  123. package/src/{databar/tables.js → ts/databar/tables.ts} +0 -1
  124. package/src/{datamatrix/decoder.js → ts/datamatrix/decoder.ts} +0 -1
  125. package/{typings → src/ts}/datamatrix/detector.d.ts +2 -2
  126. package/src/{datamatrix/detector.js → ts/datamatrix/detector.ts} +0 -1
  127. package/src/{datamatrix/encoder.js → ts/datamatrix/encoder.ts} +0 -1
  128. package/src/{datamatrix/index.js → ts/datamatrix/index.ts} +0 -1
  129. package/src/{datamatrix/tables.js → ts/datamatrix/tables.ts} +0 -1
  130. package/src/{frameqr/decoder.js → ts/frameqr/decoder.ts} +15 -16
  131. package/src/{frameqr/detector.js → ts/frameqr/detector.ts} +2 -3
  132. package/src/{frameqr/encoder.js → ts/frameqr/encoder.ts} +7 -8
  133. package/src/{frameqr/index.js → ts/frameqr/index.ts} +0 -1
  134. package/src/{frameqr/tables.js → ts/frameqr/tables.ts} +10 -11
  135. package/src/{image/binarizer.js → ts/image/binarizer.ts} +0 -1
  136. package/src/{image/grid-sampler.js → ts/image/grid-sampler.ts} +0 -1
  137. package/src/{image/index.js → ts/image/index.ts} +0 -1
  138. package/src/{image/luminance.js → ts/image/luminance.ts} +0 -1
  139. package/src/{image/perspective.js → ts/image/perspective.ts} +0 -1
  140. package/{typings → src/ts}/index.d.ts +3 -3
  141. package/src/ts/index.ts +790 -0
  142. package/src/{micropdf417/compaction.js → ts/micropdf417/compaction.ts} +0 -1
  143. package/src/{micropdf417/decoder.js → ts/micropdf417/decoder.ts} +0 -1
  144. package/src/{micropdf417/detector.js → ts/micropdf417/detector.ts} +0 -1
  145. package/src/{micropdf417/encoder.js → ts/micropdf417/encoder.ts} +0 -1
  146. package/src/{micropdf417/error-correction.js → ts/micropdf417/error-correction.ts} +0 -1
  147. package/src/{micropdf417/index.js → ts/micropdf417/index.ts} +0 -1
  148. package/src/{micropdf417/tables.js → ts/micropdf417/tables.ts} +0 -1
  149. package/src/{microqr/decoder.js → ts/microqr/decoder.ts} +0 -1
  150. package/src/{microqr/detector.js → ts/microqr/detector.ts} +0 -1
  151. package/src/{microqr/encoder.js → ts/microqr/encoder.ts} +0 -1
  152. package/src/{microqr/index.js → ts/microqr/index.ts} +0 -1
  153. package/src/{microqr/tables.js → ts/microqr/tables.ts} +0 -1
  154. package/src/{oned/addons.js → ts/oned/addons.ts} +0 -1
  155. package/src/{oned/index.js → ts/oned/index.ts} +0 -1
  156. package/src/{oned/patterns.js → ts/oned/patterns.ts} +0 -1
  157. package/src/{oned/reader.js → ts/oned/reader.ts} +0 -1
  158. package/src/{oned/writers.js → ts/oned/writers.ts} +0 -1
  159. package/src/{pdf417/compaction.js → ts/pdf417/compaction.ts} +0 -1
  160. package/src/{pdf417/decoder.js → ts/pdf417/decoder.ts} +0 -1
  161. package/src/{pdf417/detector.js → ts/pdf417/detector.ts} +0 -1
  162. package/src/{pdf417/encoder.js → ts/pdf417/encoder.ts} +0 -1
  163. package/src/{pdf417/error-correction.js → ts/pdf417/error-correction.ts} +0 -1
  164. package/src/ts/pdf417/index.ts +6 -0
  165. package/src/{pdf417/tables.js → ts/pdf417/tables.ts} +0 -1
  166. package/src/{qr/decoder.js → ts/qr/decoder.ts} +0 -1
  167. package/src/{qr/detector.js → ts/qr/detector.ts} +0 -1
  168. package/src/{qr/encoder.js → ts/qr/encoder.ts} +0 -1
  169. package/src/{qr/index.js → ts/qr/index.ts} +0 -1
  170. package/src/{qr/tables.js → ts/qr/tables.ts} +0 -1
  171. package/src/{render/image-data.js → ts/render/image-data.ts} +0 -1
  172. package/src/{render/index.js → ts/render/index.ts} +0 -1
  173. package/src/{render/options.js → ts/render/options.ts} +0 -1
  174. package/src/{render/png.js → ts/render/png.ts} +0 -1
  175. package/src/{render/svg.js → ts/render/svg.ts} +0 -1
  176. package/src/{render/webgl.js → ts/render/webgl.ts} +0 -1
  177. package/src/{render/webgpu.js → ts/render/webgpu.ts} +0 -1
  178. package/src/{rmqr/decoder.js → ts/rmqr/decoder.ts} +0 -1
  179. package/src/{rmqr/detector.js → ts/rmqr/detector.ts} +0 -1
  180. package/src/{rmqr/encoder.js → ts/rmqr/encoder.ts} +0 -1
  181. package/src/{rmqr/index.js → ts/rmqr/index.ts} +0 -1
  182. package/src/{rmqr/tables.js → ts/rmqr/tables.ts} +0 -1
  183. /package/{typings → src/ts}/aztec/decoder.d.ts +0 -0
  184. /package/{typings → src/ts}/aztec/encoder.d.ts +0 -0
  185. /package/{typings → src/ts}/aztec/high-level.d.ts +0 -0
  186. /package/{typings → src/ts}/aztec/index.d.ts +0 -0
  187. /package/{typings → src/ts}/aztec/tables.d.ts +0 -0
  188. /package/{typings → src/ts}/aztecrune/decoder.d.ts +0 -0
  189. /package/{typings → src/ts}/aztecrune/detector.d.ts +0 -0
  190. /package/{typings → src/ts}/aztecrune/encoder.d.ts +0 -0
  191. /package/{typings → src/ts}/aztecrune/index.d.ts +0 -0
  192. /package/{typings → src/ts}/aztecrune/tables.d.ts +0 -0
  193. /package/{typings → src/ts}/bundle/sythos-barcode.d.ts +0 -0
  194. /package/{typings → src/ts}/bundle/sythos-barcode.esm.d.ts +0 -0
  195. /package/{typings → src/ts}/compactpdf417/decoder.d.ts +0 -0
  196. /package/{typings → src/ts}/compactpdf417/detector.d.ts +0 -0
  197. /package/{typings → src/ts}/compactpdf417/encoder.d.ts +0 -0
  198. /package/{typings → src/ts}/compactpdf417/index.d.ts +0 -0
  199. /package/{typings → src/ts}/compactpdf417/tables.d.ts +0 -0
  200. /package/{typings → src/ts}/core/bit-buffer.d.ts +0 -0
  201. /package/{typings → src/ts}/core/bit-matrix.d.ts +0 -0
  202. /package/{typings → src/ts}/core/errors.d.ts +0 -0
  203. /package/{typings → src/ts}/core/galois-field.d.ts +0 -0
  204. /package/{typings → src/ts}/core/index.d.ts +0 -0
  205. /package/{typings → src/ts}/core/reed-solomon.d.ts +0 -0
  206. /package/{typings → src/ts}/databar/codec.d.ts +0 -0
  207. /package/{typings → src/ts}/databar/decoder.d.ts +0 -0
  208. /package/{typings → src/ts}/databar/encoder.d.ts +0 -0
  209. /package/{typings → src/ts}/databar/gs1.d.ts +0 -0
  210. /package/{typings → src/ts}/databar/index.d.ts +0 -0
  211. /package/{typings → src/ts}/databar/patterns.d.ts +0 -0
  212. /package/{typings → src/ts}/databar/tables.d.ts +0 -0
  213. /package/{typings → src/ts}/datamatrix/decoder.d.ts +0 -0
  214. /package/{typings → src/ts}/datamatrix/encoder.d.ts +0 -0
  215. /package/{typings → src/ts}/datamatrix/index.d.ts +0 -0
  216. /package/{typings → src/ts}/datamatrix/tables.d.ts +0 -0
  217. /package/{typings → src/ts}/frameqr/decoder.d.ts +0 -0
  218. /package/{typings → src/ts}/frameqr/detector.d.ts +0 -0
  219. /package/{typings → src/ts}/frameqr/encoder.d.ts +0 -0
  220. /package/{typings → src/ts}/frameqr/index.d.ts +0 -0
  221. /package/{typings → src/ts}/frameqr/tables.d.ts +0 -0
  222. /package/{typings → src/ts}/image/binarizer.d.ts +0 -0
  223. /package/{typings → src/ts}/image/grid-sampler.d.ts +0 -0
  224. /package/{typings → src/ts}/image/index.d.ts +0 -0
  225. /package/{typings → src/ts}/image/luminance.d.ts +0 -0
  226. /package/{typings → src/ts}/image/perspective.d.ts +0 -0
  227. /package/{typings → src/ts}/micropdf417/compaction.d.ts +0 -0
  228. /package/{typings → src/ts}/micropdf417/decoder.d.ts +0 -0
  229. /package/{typings → src/ts}/micropdf417/detector.d.ts +0 -0
  230. /package/{typings → src/ts}/micropdf417/encoder.d.ts +0 -0
  231. /package/{typings → src/ts}/micropdf417/error-correction.d.ts +0 -0
  232. /package/{typings → src/ts}/micropdf417/index.d.ts +0 -0
  233. /package/{typings → src/ts}/micropdf417/tables.d.ts +0 -0
  234. /package/{typings → src/ts}/microqr/decoder.d.ts +0 -0
  235. /package/{typings → src/ts}/microqr/detector.d.ts +0 -0
  236. /package/{typings → src/ts}/microqr/encoder.d.ts +0 -0
  237. /package/{typings → src/ts}/microqr/index.d.ts +0 -0
  238. /package/{typings → src/ts}/microqr/tables.d.ts +0 -0
  239. /package/{typings → src/ts}/oned/addons.d.ts +0 -0
  240. /package/{typings → src/ts}/oned/index.d.ts +0 -0
  241. /package/{typings → src/ts}/oned/patterns.d.ts +0 -0
  242. /package/{typings → src/ts}/oned/reader.d.ts +0 -0
  243. /package/{typings → src/ts}/oned/writers.d.ts +0 -0
  244. /package/{typings → src/ts}/pdf417/compaction.d.ts +0 -0
  245. /package/{typings → src/ts}/pdf417/decoder.d.ts +0 -0
  246. /package/{typings → src/ts}/pdf417/detector.d.ts +0 -0
  247. /package/{typings → src/ts}/pdf417/encoder.d.ts +0 -0
  248. /package/{typings → src/ts}/pdf417/error-correction.d.ts +0 -0
  249. /package/{typings → src/ts}/pdf417/index.d.ts +0 -0
  250. /package/{typings → src/ts}/pdf417/tables.d.ts +0 -0
  251. /package/{typings → src/ts}/qr/decoder.d.ts +0 -0
  252. /package/{typings → src/ts}/qr/detector.d.ts +0 -0
  253. /package/{typings → src/ts}/qr/encoder.d.ts +0 -0
  254. /package/{typings → src/ts}/qr/index.d.ts +0 -0
  255. /package/{typings → src/ts}/qr/tables.d.ts +0 -0
  256. /package/{typings → src/ts}/render/image-data.d.ts +0 -0
  257. /package/{typings → src/ts}/render/index.d.ts +0 -0
  258. /package/{typings → src/ts}/render/options.d.ts +0 -0
  259. /package/{typings → src/ts}/render/png.d.ts +0 -0
  260. /package/{typings → src/ts}/render/svg.d.ts +0 -0
  261. /package/{typings → src/ts}/render/webgl.d.ts +0 -0
  262. /package/{typings → src/ts}/render/webgpu.d.ts +0 -0
  263. /package/{typings → src/ts}/rmqr/decoder.d.ts +0 -0
  264. /package/{typings → src/ts}/rmqr/detector.d.ts +0 -0
  265. /package/{typings → src/ts}/rmqr/encoder.d.ts +0 -0
  266. /package/{typings → src/ts}/rmqr/index.d.ts +0 -0
  267. /package/{typings → src/ts}/rmqr/tables.d.ts +0 -0
@@ -0,0 +1,120 @@
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
+ * Raster output as ImageData, and 2D-canvas drawing.
32
+ *
33
+ * @module render/image-data
34
+ */
35
+ import { normalizeOptions, parseColor } from './options.js';
36
+ /**
37
+ * Render to an `ImageData`-shaped object.
38
+ *
39
+ * A plain object rather than a real `ImageData`, so this works in Node and in
40
+ * workers without a DOM. It is accepted directly by `ctx.putImageData` in the
41
+ * browser, and by this library's own reader.
42
+ *
43
+ * @param {import('../core/bit-matrix.js').BitMatrix} matrix
44
+ * @param {import('./options.js').RenderOptions} [options]
45
+ * @returns {{data: Uint8ClampedArray, width: number, height: number}}
46
+ */
47
+ export function toImageData(matrix, options = {}) {
48
+ const opts = normalizeOptions(matrix, options);
49
+ const { scale, source, pixelWidth, pixelHeight } = opts;
50
+ const dark = parseColor(opts.dark);
51
+ const light = parseColor(opts.light);
52
+ const data = new Uint8ClampedArray(pixelWidth * pixelHeight * 4);
53
+ // Fill one scanline per module row, then copy it `scale` times. Barcodes are
54
+ // wide and repetitive, so this is markedly faster than a per-pixel loop.
55
+ const line = new Uint8ClampedArray(pixelWidth * 4);
56
+ for (let my = 0; my < source.height; my++) {
57
+ for (let mx = 0; mx < source.width; mx++) {
58
+ const colour = source.get(mx, my) ? dark : light;
59
+ const start = mx * scale * 4;
60
+ for (let px = 0; px < scale; px++) {
61
+ const p = start + px * 4;
62
+ line[p] = colour[0];
63
+ line[p + 1] = colour[1];
64
+ line[p + 2] = colour[2];
65
+ line[p + 3] = colour[3];
66
+ }
67
+ }
68
+ for (let py = 0; py < scale; py++) {
69
+ data.set(line, ((my * scale + py) * pixelWidth) * 4);
70
+ }
71
+ }
72
+ return { data, width: pixelWidth, height: pixelHeight };
73
+ }
74
+ /**
75
+ * Draw into a canvas using its 2D context.
76
+ *
77
+ * This is the universal fallback: every browser that runs JavaScript at all
78
+ * has a 2D context, including every iOS Safari version.
79
+ *
80
+ * @param {import('../core/bit-matrix.js').BitMatrix} matrix
81
+ * @param {HTMLCanvasElement | OffscreenCanvas} canvas
82
+ * @param {import('./options.js').RenderOptions} [options]
83
+ * @returns {boolean} True when it drew.
84
+ */
85
+ export function toCanvas(matrix, canvas, options = {}) {
86
+ const opts = normalizeOptions(matrix, options);
87
+ const ctx = canvas.getContext('2d');
88
+ if (!ctx)
89
+ return false;
90
+ canvas.width = opts.pixelWidth;
91
+ canvas.height = opts.pixelHeight;
92
+ // Draw with fillRect rather than putImageData: it respects the canvas's
93
+ // alpha compositing, so a transparent `light` leaves the page showing
94
+ // through instead of punching a hole.
95
+ const [, , , lightAlpha] = parseColor(opts.light);
96
+ if (lightAlpha > 0) {
97
+ ctx.fillStyle = opts.light;
98
+ ctx.fillRect(0, 0, opts.pixelWidth, opts.pixelHeight);
99
+ }
100
+ else {
101
+ ctx.clearRect(0, 0, opts.pixelWidth, opts.pixelHeight);
102
+ }
103
+ ctx.fillStyle = opts.dark;
104
+ const { source, scale } = opts;
105
+ for (let y = 0; y < source.height; y++) {
106
+ let x = 0;
107
+ while (x < source.width) {
108
+ if (!source.get(x, y)) {
109
+ x++;
110
+ continue;
111
+ }
112
+ let run = 1;
113
+ while (x + run < source.width && source.get(x + run, y))
114
+ run++;
115
+ ctx.fillRect(x * scale, y * scale, run * scale, scale);
116
+ x += run;
117
+ }
118
+ }
119
+ return true;
120
+ }
@@ -0,0 +1,128 @@
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
+ * Output backends.
32
+ *
33
+ * ## On GPU acceleration — read this before assuming what it does
34
+ *
35
+ * The WebGL2 and WebGPU backends accelerate **drawing** a barcode, not
36
+ * **computing** one. That distinction is worth stating plainly, because "GPU
37
+ * barcode generation" naturally sounds like the latter.
38
+ *
39
+ * Encoding is sequential integer work: Reed-Solomon polynomial division, mask
40
+ * penalty scoring, bit placement along a zig-zag path. Each step depends on the
41
+ * one before it, which is precisely the shape a GPU cannot exploit. A complete
42
+ * QR encode takes well under a millisecond on the CPU — less time than it takes
43
+ * to dispatch a compute shader and read the result back. Moving it to the GPU
44
+ * would make it slower, not faster, and no amount of engineering changes that.
45
+ *
46
+ * What the GPU genuinely helps with:
47
+ *
48
+ * - **Drawing** large symbols, or many symbols per frame, straight into a
49
+ * canvas without a CPU-side pixel buffer.
50
+ * - **Reading**, where per-frame greyscale conversion and block statistics
51
+ * over a 1080p or 4K camera image are the real bottleneck and are
52
+ * embarrassingly parallel.
53
+ *
54
+ * So: encoding stays on the CPU because that is the correct engineering answer,
55
+ * not because of a missing feature.
56
+ *
57
+ * @module render
58
+ */
59
+ export { toSVG, toSVGDataURI } from './svg.js';
60
+ export { toImageData, toCanvas } from './image-data.js';
61
+ export { toPNG, toPNGDataURI, deflateStored } from './png.js';
62
+ export { isWebGL2Available, renderToCanvasWebGL } from './webgl.js';
63
+ export { isWebGPUAvailable, renderToCanvasWebGPU } from './webgpu.js';
64
+ export { normalizeOptions, parseColor } from './options.js';
65
+ import { toCanvas } from './image-data.js';
66
+ import { isWebGL2Available, renderToCanvasWebGL } from './webgl.js';
67
+ import { isWebGPUAvailable, renderToCanvasWebGPU } from './webgpu.js';
68
+ /**
69
+ * Draw into a canvas using the best backend available.
70
+ *
71
+ * Tries WebGL2, then the 2D context. The 2D path is always available, so this
72
+ * never fails on a browser that can run the library at all — including every
73
+ * version of Safari on iOS.
74
+ *
75
+ * WebGPU is not reachable from here, and cannot be: obtaining an adapter is
76
+ * asynchronous, so a synchronous function can never wait for one. Use
77
+ * `renderToCanvasAutoAsync` to include it. This one stays synchronous because
78
+ * it is the documented signature and callers rely on the returned backend name
79
+ * being available immediately.
80
+ *
81
+ * @param {import('../core/bit-matrix.js').BitMatrix} matrix
82
+ * @param {HTMLCanvasElement | OffscreenCanvas} canvas
83
+ * @param {import('./options.js').RenderOptions & {backend?: 'auto'|'webgl2'|'2d'}} [options]
84
+ * @returns {{backend: 'webgl2' | '2d' | 'none'}}
85
+ */
86
+ export function renderToCanvasAuto(matrix, canvas, options = {}) {
87
+ const preferred = options.backend ?? 'auto';
88
+ if ((preferred === 'auto' || preferred === 'webgl2') && isWebGL2Available()) {
89
+ if (renderToCanvasWebGL(matrix, canvas, options))
90
+ return { backend: 'webgl2' };
91
+ }
92
+ if (toCanvas(matrix, canvas, options))
93
+ return { backend: '2d' };
94
+ return { backend: 'none' };
95
+ }
96
+ /**
97
+ * Draw into a canvas using the best backend available, including WebGPU.
98
+ *
99
+ * Tries WebGPU, then WebGL2, then the 2D context, and returns the name of the
100
+ * one that drew.
101
+ *
102
+ * Each backend is *probed* before the canvas is handed to it. That ordering is
103
+ * deliberate: a canvas can only ever have one kind of context, so committing it
104
+ * to WebGPU and failing afterwards would leave it unable to fall back to
105
+ * WebGL2 or 2D. The probes use throwaway objects of their own, so the caller's
106
+ * canvas is only touched by a backend that is already known to work.
107
+ *
108
+ * @param {import('../core/bit-matrix.js').BitMatrix} matrix
109
+ * @param {HTMLCanvasElement | OffscreenCanvas} canvas
110
+ * @param {import('./options.js').RenderOptions & {backend?: 'auto'|'webgpu'|'webgl2'|'2d'}} [options]
111
+ * @returns {Promise<{backend: 'webgpu' | 'webgl2' | '2d' | 'none'}>}
112
+ */
113
+ export async function renderToCanvasAutoAsync(matrix, canvas, options = {}) {
114
+ const preferred = options.backend ?? 'auto';
115
+ if (preferred === 'auto' || preferred === 'webgpu') {
116
+ if (await isWebGPUAvailable()) {
117
+ if (await renderToCanvasWebGPU(matrix, canvas, options))
118
+ return { backend: 'webgpu' };
119
+ }
120
+ }
121
+ if ((preferred === 'auto' || preferred === 'webgl2') && isWebGL2Available()) {
122
+ if (renderToCanvasWebGL(matrix, canvas, options))
123
+ return { backend: 'webgl2' };
124
+ }
125
+ if (toCanvas(matrix, canvas, options))
126
+ return { backend: '2d' };
127
+ return { backend: 'none' };
128
+ }
@@ -0,0 +1,152 @@
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
+ * Shared render options, normalised once so every backend agrees.
32
+ *
33
+ * @module render/options
34
+ */
35
+ import { BitMatrix } from '../core/bit-matrix.js';
36
+ /**
37
+ * @typedef {object} RenderOptions
38
+ * @property {number} [scale] Pixels per module. Default 8.
39
+ * @property {number} [margin] Quiet-zone modules on every side. Default 4.
40
+ * @property {string} [dark] Colour of set modules. Default '#000000'.
41
+ * @property {string} [light] Colour of clear modules, or 'none' for transparent.
42
+ * @property {number} [barHeight] For 1D symbols: total bar height in pixels.
43
+ */
44
+ /**
45
+ * Expand and pad the matrix, and resolve every dimension.
46
+ *
47
+ * Linear symbols arrive one module tall. They are stretched to `barHeight`
48
+ * *before* the quiet zone is applied, so the margin ends up uniform on all
49
+ * four sides — padding first would leave a quiet zone one module tall against
50
+ * bars a hundred pixels tall, which no scanner would accept.
51
+ *
52
+ * @param {BitMatrix} matrix
53
+ * @param {RenderOptions} options
54
+ */
55
+ export function normalizeOptions(matrix, options = {}) {
56
+ const scale = Math.max(1, Math.floor(options.scale ?? 8));
57
+ const margin = Math.max(0, Math.floor(options.margin ?? 4));
58
+ const dark = options.dark ?? '#000000';
59
+ const light = options.light ?? '#ffffff';
60
+ const barHeight = options.barHeight ?? null;
61
+ let base = matrix;
62
+ const is1D = matrix.height === 1;
63
+ if (is1D) {
64
+ // Default to a bar height that stays scannable: tall enough that a laser
65
+ // crossing at a slight angle still passes through the whole symbol.
66
+ const targetPixels = barHeight ?? Math.max(40, Math.round(matrix.width * scale * 0.15));
67
+ const rows = Math.max(1, Math.round(targetPixels / scale));
68
+ base = new BitMatrix(matrix.width, rows);
69
+ for (let x = 0; x < matrix.width; x++) {
70
+ if (!matrix.get(x, 0))
71
+ continue;
72
+ for (let y = 0; y < rows; y++)
73
+ base.set(x, y);
74
+ }
75
+ }
76
+ const source = margin > 0 ? base.withMargin(margin) : base;
77
+ return {
78
+ scale,
79
+ margin,
80
+ dark,
81
+ light,
82
+ is1D,
83
+ source,
84
+ rowHeight: scale,
85
+ pixelWidth: source.width * scale,
86
+ pixelHeight: source.height * scale,
87
+ };
88
+ }
89
+ /**
90
+ * Parse a CSS colour into RGBA bytes.
91
+ *
92
+ * Supports the forms a barcode actually needs: #rgb, #rgba, #rrggbb,
93
+ * #rrggbbaa, rgb(), rgba(), plus 'none' and 'transparent'.
94
+ *
95
+ * @param {string} colour
96
+ * @returns {[number, number, number, number]}
97
+ */
98
+ export function parseColor(colour) {
99
+ const value = String(colour).trim().toLowerCase();
100
+ if (value === 'none' || value === 'transparent')
101
+ return [0, 0, 0, 0];
102
+ if (value === 'white')
103
+ return [255, 255, 255, 255];
104
+ if (value === 'black')
105
+ return [0, 0, 0, 255];
106
+ if (value[0] === '#') {
107
+ const hex = value.slice(1);
108
+ const expand = (c) => parseInt(c + c, 16);
109
+ if (hex.length === 3) {
110
+ return [expand(hex[0]), expand(hex[1]), expand(hex[2]), 255];
111
+ }
112
+ if (hex.length === 4) {
113
+ return [expand(hex[0]), expand(hex[1]), expand(hex[2]), expand(hex[3])];
114
+ }
115
+ if (hex.length === 6) {
116
+ return [
117
+ parseInt(hex.slice(0, 2), 16),
118
+ parseInt(hex.slice(2, 4), 16),
119
+ parseInt(hex.slice(4, 6), 16),
120
+ 255,
121
+ ];
122
+ }
123
+ if (hex.length === 8) {
124
+ return [
125
+ parseInt(hex.slice(0, 2), 16),
126
+ parseInt(hex.slice(2, 4), 16),
127
+ parseInt(hex.slice(4, 6), 16),
128
+ parseInt(hex.slice(6, 8), 16),
129
+ ];
130
+ }
131
+ }
132
+ const fn = value.match(/^rgba?\(([^)]+)\)$/);
133
+ if (fn) {
134
+ const parts = fn[1].split(/[,/\s]+/).filter(Boolean);
135
+ const channel = (s) => (s.endsWith('%')
136
+ ? Math.round((parseFloat(s) / 100) * 255)
137
+ : Math.round(parseFloat(s)));
138
+ const r = channel(parts[0]);
139
+ const g = channel(parts[1]);
140
+ const b = channel(parts[2]);
141
+ let a = 255;
142
+ if (parts.length > 3) {
143
+ a = parts[3].endsWith('%')
144
+ ? Math.round((parseFloat(parts[3]) / 100) * 255)
145
+ : Math.round(parseFloat(parts[3]) * 255);
146
+ }
147
+ return [r, g, b, a];
148
+ }
149
+ // Unrecognised: fall back to opaque black rather than throwing, so an
150
+ // unusual colour never costs someone a barcode.
151
+ return [0, 0, 0, 255];
152
+ }
@@ -0,0 +1,270 @@
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
+ * PNG output, with no dependencies and no compression library of our own.
32
+ *
33
+ * A barcode is a two-colour image, so this writes a 1-bit palette PNG: eight
34
+ * modules per byte, and a two-entry palette. That is both the smallest and the
35
+ * simplest correct encoding.
36
+ *
37
+ * Compression strategy, in order of preference:
38
+ *
39
+ * 1. `node:zlib` in Node.
40
+ * 2. `CompressionStream('deflate')` in browsers that have it.
41
+ * 3. Stored (uncompressed) deflate blocks, written here.
42
+ *
43
+ * Writing a Huffman coder would be a week of work to save a few kilobytes on
44
+ * an image that is mostly already tiny. The stored-block fallback is about
45
+ * eighty lines and produces a completely valid PNG; the only cost is size, on
46
+ * the minority of platforms that reach it.
47
+ *
48
+ * @module render/png
49
+ */
50
+ import { normalizeOptions, parseColor } from './options.js';
51
+ /* ------------------------------------------------------------------ *
52
+ * Checksums
53
+ * ------------------------------------------------------------------ */
54
+ const CRC_TABLE = (() => {
55
+ const table = new Uint32Array(256);
56
+ for (let n = 0; n < 256; n++) {
57
+ let c = n;
58
+ for (let k = 0; k < 8; k++) {
59
+ c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
60
+ }
61
+ table[n] = c >>> 0;
62
+ }
63
+ return table;
64
+ })();
65
+ /**
66
+ * @param {Uint8Array} bytes
67
+ * @returns {number}
68
+ */
69
+ function crc32(bytes) {
70
+ let c = 0xffffffff;
71
+ for (let i = 0; i < bytes.length; i++) {
72
+ c = CRC_TABLE[(c ^ bytes[i]) & 0xff] ^ (c >>> 8);
73
+ }
74
+ return (c ^ 0xffffffff) >>> 0;
75
+ }
76
+ /**
77
+ * @param {Uint8Array} bytes
78
+ * @returns {number}
79
+ */
80
+ function adler32(bytes) {
81
+ let a = 1;
82
+ let b = 0;
83
+ // 5552 is the largest run that cannot overflow the 32-bit accumulator.
84
+ for (let i = 0; i < bytes.length;) {
85
+ const end = Math.min(i + 5552, bytes.length);
86
+ for (; i < end; i++) {
87
+ a += bytes[i];
88
+ b += a;
89
+ }
90
+ a %= 65521;
91
+ b %= 65521;
92
+ }
93
+ return ((b << 16) | a) >>> 0;
94
+ }
95
+ /* ------------------------------------------------------------------ *
96
+ * Deflate
97
+ * ------------------------------------------------------------------ */
98
+ /**
99
+ * Wrap data in stored (type 00) deflate blocks with a zlib header.
100
+ *
101
+ * A stored block's length field is sixteen bits, so each block caps at 65535
102
+ * bytes and longer data must be split. BFINAL is set on the last block only —
103
+ * getting that wrong yields a stream that decodes correctly for any small
104
+ * image and truncates on the first large one.
105
+ *
106
+ * @param {Uint8Array} data
107
+ * @returns {Uint8Array}
108
+ */
109
+ export function deflateStored(data) {
110
+ const MAX = 0xffff;
111
+ const blockCount = Math.max(1, Math.ceil(data.length / MAX));
112
+ const out = new Uint8Array(2 + blockCount * 5 + data.length + 4);
113
+ let p = 0;
114
+ // zlib header: deflate, 32K window, no preset dictionary. 0x78 0x01 is a
115
+ // valid FCHECK pair (0x7801 % 31 === 0).
116
+ out[p++] = 0x78;
117
+ out[p++] = 0x01;
118
+ for (let i = 0; i < blockCount; i++) {
119
+ const start = i * MAX;
120
+ const len = Math.min(MAX, data.length - start);
121
+ const isLast = i === blockCount - 1;
122
+ out[p++] = isLast ? 1 : 0; // BFINAL, BTYPE = 00
123
+ out[p++] = len & 0xff; // LEN, little endian
124
+ out[p++] = (len >>> 8) & 0xff;
125
+ out[p++] = ~len & 0xff; // NLEN, one's complement
126
+ out[p++] = (~len >>> 8) & 0xff;
127
+ out.set(data.subarray(start, start + len), p);
128
+ p += len;
129
+ }
130
+ const sum = adler32(data);
131
+ out[p++] = (sum >>> 24) & 0xff; // adler32, big endian
132
+ out[p++] = (sum >>> 16) & 0xff;
133
+ out[p++] = (sum >>> 8) & 0xff;
134
+ out[p++] = sum & 0xff;
135
+ return out.subarray(0, p);
136
+ }
137
+ /**
138
+ * Compress with whatever the platform provides, falling back to stored blocks.
139
+ *
140
+ * @param {Uint8Array} data
141
+ * @returns {Promise<Uint8Array>}
142
+ */
143
+ async function deflate(data) {
144
+ // Node: zlib is built in, so this is not a dependency.
145
+ if (typeof process !== 'undefined' && process.versions && process.versions.node) {
146
+ try {
147
+ const zlib = await import('node:zlib');
148
+ return new Uint8Array(zlib.deflateSync(data));
149
+ }
150
+ catch {
151
+ /* fall through */
152
+ }
153
+ }
154
+ // Browsers: CompressionStream('deflate') emits the zlib format PNG wants.
155
+ if (typeof CompressionStream === 'function') {
156
+ try {
157
+ const stream = new Blob([data]).stream().pipeThrough(new CompressionStream('deflate'));
158
+ const buffer = await new Response(stream).arrayBuffer();
159
+ return new Uint8Array(buffer);
160
+ }
161
+ catch {
162
+ /* fall through */
163
+ }
164
+ }
165
+ return deflateStored(data);
166
+ }
167
+ /* ------------------------------------------------------------------ *
168
+ * PNG assembly
169
+ * ------------------------------------------------------------------ */
170
+ const SIGNATURE = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
171
+ /**
172
+ * @param {string} type Four ASCII characters.
173
+ * @param {Uint8Array} payload
174
+ * @returns {Uint8Array}
175
+ */
176
+ function chunk(type, payload) {
177
+ const out = new Uint8Array(12 + payload.length);
178
+ const view = new DataView(out.buffer);
179
+ view.setUint32(0, payload.length);
180
+ for (let i = 0; i < 4; i++)
181
+ out[4 + i] = type.charCodeAt(i);
182
+ out.set(payload, 8);
183
+ // The CRC covers the type and the payload, but not the length.
184
+ view.setUint32(8 + payload.length, crc32(out.subarray(4, 8 + payload.length)));
185
+ return out;
186
+ }
187
+ /**
188
+ * Render to a PNG file.
189
+ *
190
+ * @param {import('../core/bit-matrix.js').BitMatrix} matrix
191
+ * @param {import('./options.js').RenderOptions} [options]
192
+ * @returns {Promise<Uint8Array>}
193
+ */
194
+ export async function toPNG(matrix, options = {}) {
195
+ const opts = normalizeOptions(matrix, options);
196
+ const { scale, source, pixelWidth, pixelHeight } = opts;
197
+ const dark = parseColor(opts.dark);
198
+ const light = parseColor(opts.light);
199
+ // --- Scanlines: filter byte 0, then one bit per pixel, MSB leftmost.
200
+ const bytesPerRow = (pixelWidth + 7) >> 3;
201
+ const raw = new Uint8Array((bytesPerRow + 1) * pixelHeight);
202
+ const rowBits = new Uint8Array(bytesPerRow);
203
+ for (let my = 0; my < source.height; my++) {
204
+ rowBits.fill(0);
205
+ for (let mx = 0; mx < source.width; mx++) {
206
+ if (!source.get(mx, my))
207
+ continue;
208
+ const from = mx * scale;
209
+ for (let px = from; px < from + scale; px++) {
210
+ rowBits[px >> 3] |= 0x80 >> (px & 7);
211
+ }
212
+ }
213
+ for (let py = 0; py < scale; py++) {
214
+ const offset = (my * scale + py) * (bytesPerRow + 1);
215
+ raw[offset] = 0; // filter type 0 (None)
216
+ raw.set(rowBits, offset + 1);
217
+ }
218
+ }
219
+ // --- Chunks.
220
+ const ihdr = new Uint8Array(13);
221
+ const ihdrView = new DataView(ihdr.buffer);
222
+ ihdrView.setUint32(0, pixelWidth);
223
+ ihdrView.setUint32(4, pixelHeight);
224
+ ihdr[8] = 1; // bit depth
225
+ ihdr[9] = 3; // colour type 3: palette
226
+ ihdr[10] = 0; // compression: deflate
227
+ ihdr[11] = 0; // filter method
228
+ ihdr[12] = 0; // no interlace
229
+ // Palette index 0 is light (a clear module), index 1 is dark.
230
+ const plte = new Uint8Array([
231
+ light[0], light[1], light[2],
232
+ dark[0], dark[1], dark[2],
233
+ ]);
234
+ const parts = [SIGNATURE, chunk('IHDR', ihdr), chunk('PLTE', plte)];
235
+ // tRNS is only emitted when something is actually translucent, so the common
236
+ // opaque case produces a file every decoder handles identically.
237
+ if (light[3] < 255 || dark[3] < 255) {
238
+ parts.push(chunk('tRNS', new Uint8Array([light[3], dark[3]])));
239
+ }
240
+ parts.push(chunk('IDAT', await deflate(raw)));
241
+ parts.push(chunk('IEND', new Uint8Array(0)));
242
+ let total = 0;
243
+ for (const part of parts)
244
+ total += part.length;
245
+ const file = new Uint8Array(total);
246
+ let offset = 0;
247
+ for (const part of parts) {
248
+ file.set(part, offset);
249
+ offset += part.length;
250
+ }
251
+ return file;
252
+ }
253
+ /**
254
+ * Render to a data URI usable as an `<img>` src or a download href.
255
+ *
256
+ * @param {import('../core/bit-matrix.js').BitMatrix} matrix
257
+ * @param {import('./options.js').RenderOptions} [options]
258
+ * @returns {Promise<string>}
259
+ */
260
+ export async function toPNGDataURI(matrix, options = {}) {
261
+ const bytes = await toPNG(matrix, options);
262
+ let binary = '';
263
+ for (let i = 0; i < bytes.length; i++)
264
+ binary += String.fromCharCode(bytes[i]);
265
+ const base64 = typeof btoa === 'function'
266
+ ? btoa(binary)
267
+ /* eslint-disable-next-line no-undef */
268
+ : Buffer.from(bytes).toString('base64');
269
+ return 'data:image/png;base64,' + base64;
270
+ }