qr 0.5.3 → 0.5.5

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/README.md CHANGED
@@ -1,12 +1,13 @@
1
1
  # paulmillr-qr
2
2
 
3
- Minimal 0-dep QR code generator & reader.
3
+ Minimal 0-dependency QR code generator & reader.
4
4
 
5
5
  - 🔒 Auditable, 0-dependency
6
- - 🏞️ Encoding (generating) supports ASCII, term, gif and svg codes
6
+ - 🏎 Fast: [faster](#speed) than all JS implementations
7
+ - 🔍 Reliable: 100MB+ of extensive test vectors ensure correctness
8
+ - 🏞️ Encoding (generating) supports ASCII, term, gif, svg and png codes
7
9
  - 📷 Decoding (reading) supports camera feed input, files and non-browser environments
8
- - 🔍 Extensive tests ensure correctness: 100MB+ of vectors
9
- - 🪶 14KB (gzipped) for encoding + decoding, 7KB for encoding
10
+ - 🪶 18KB (gzipped) for encoding + decoding, 9KB for encoding
10
11
 
11
12
  Check out:
12
13
 
@@ -52,6 +53,8 @@ const terminalFriendly = encodeQR(txt, 'term'); // 2x larger, all fonts are OK
52
53
  const gifBytes = encodeQR(txt, 'gif'); // Uncompressed GIF
53
54
  const svgElement = encodeQR(txt, 'svg'); // SVG vector image element
54
55
  const array = encodeQR(txt, 'raw'); // 2d array for canvas or other libs
56
+ // import { svgToPng } from 'qr/dom.js';
57
+ // const png = svgToPng(svgElement, 512, 512); // .png, using DOM
55
58
 
56
59
  // Options
57
60
  // Custom error correction level
@@ -135,10 +138,6 @@ function decodeWithExternal() {
135
138
  const decoded = decodeQR(parseGIF(gifBytes));
136
139
  console.log('decoded(gif)', decoded);
137
140
  }
138
-
139
- // c) draw gif/svg to browser DOM canvas
140
- import { svgToPng } from 'qr/dom.js';
141
- const png = svgToPng(encodeQR('Hello world', 'svg'), 512, 512);
142
141
  ```
143
142
 
144
143
  ### Decoding options
@@ -235,27 +234,29 @@ Future plans:
235
234
 
236
235
  ## Speed
237
236
 
238
- Benchmarks measured with Apple M2 on MacOS 13 with node.js 19.
237
+ Benchmarks measured with Apple M4 with node.js 25.
239
238
 
240
239
  ```
241
- ======== encode/ascii ========
242
- encode/paulmillr x 2,995 ops/sec @ 333μs/op
243
- encode/qrcode-generator x 6,029 ops/sec @ 165μs/op ± 1.39% (min: 142μs, max: 2ms)
244
- encode/nuintun x 3,647 ops/sec @ 274μs/op
245
- ======== encode/gif ========
246
- encode/paulmillr x 2,967 ops/sec @ 337μs/op
247
- encode/qrcode-generator x 3,486 ops/sec @ 286μs/op
248
- encode/nuintun x 3,643 ops/sec @ 274μs/op
249
- ======== encode: big ========
250
- encode/paulmillr x 156 ops/sec @ 6ms/op
251
- encode/qrcode-generator x 200 ops/sec @ 4ms/op
252
- encode/nuintun x 223 ops/sec @ 4ms/op
253
- ======== decode ========
254
- decode/paulmillr x 154 ops/sec @ 6ms/op ± 1.29% (min: 6ms, max: 18ms)
255
- decode/jsqr x 52 ops/sec @ 18ms/op
256
- decode/nuintun x 51 ops/sec @ 19ms/op
257
- decode/instascan x 158 ops/sec @ 6ms/op ± 9.06% (min: 3ms, max: 144ms)======== Decoding quality ========
258
- blurred(45): paulmillr-qr=12 (26.66%) jsqr=13 (28.88%) nuintun=13 (28.88%) instascan=11 (24.44%)
240
+ # encode format=ascii
241
+ @paulmillr/qr x 9,050 ops/sec @ 110μs/op
242
+ qrcode-generator x 4,543 ops/sec @ 220μs/op
243
+ nuintun x 3,413 ops/sec @ 292μs/op
244
+
245
+ # encode format=gif
246
+ @paulmillr/qr x 8,439 ops/sec @ 118μs/op
247
+ qrcode-generator x 2,909 ops/sec @ 343μs/op
248
+ nuintun x 3,470 ops/sec @ 288μs/op
249
+
250
+ # encode of large qr
251
+ @paulmillr/qr x 334 ops/sec @ 2ms/op
252
+ qrcode-generator x 174 ops/sec @ 5ms/op
253
+ nuintun x 221 ops/sec @ 4ms/op
254
+
255
+ # decode
256
+ @paulmillr/qr x 662 ops/sec @ 1ms/op
257
+ jsqr x 50 ops/sec @ 19ms/op
258
+ nuintun x 49 ops/sec @ 20ms/op
259
+ instascan x 128 ops/sec @ 7ms/op ± 31.44% (4ms..166ms)
259
260
  ```
260
261
 
261
262
  ## License
package/decode.d.ts CHANGED
@@ -27,7 +27,21 @@ import { Bitmap } from './index.ts';
27
27
  export type FinderPoints = [Pattern, Pattern, Point, Pattern];
28
28
  /**
29
29
  * Convert to grayscale. The function is the most expensive part of decoding:
30
- * it takes up to 90% of time. TODO: check gamma correction / sqr.
30
+ * it takes up to 90% of time.
31
+ *
32
+ * Binarization pipeline:
33
+ * 1. Convert RGB/RGBA image to one luma byte per pixel.
34
+ * 2. Split the image into 8x8 blocks and collect per-block mean/min/max.
35
+ * 3. Build a 5x5 neighborhood mean over those block means.
36
+ * 4. Turn each 8x8 block into bitmap bits using a local cut derived from:
37
+ * - the neighborhood mean,
38
+ * - the current block statistics,
39
+ * - a cheap whole-image color-spread estimate,
40
+ * - and, on risky scenes, a local variance field over block means.
41
+ *
42
+ * Instead of producing "best looking" thresholding: we produce a
43
+ * bitmap where finder patterns survive perspective / blur / highlights while
44
+ * keeping false dark regions low enough for downstream finder selection.
31
45
  */
32
46
  declare function toBitmap(img: Image): Bitmap;
33
47
  type Pattern = Point & {
@@ -43,7 +57,7 @@ declare function detect(b: Bitmap): {
43
57
  bits: Bitmap;
44
58
  points: FinderPoints;
45
59
  };
46
- declare function decodeBitmap(b: Bitmap, decoder?: (bytes: Uint8Array) => string): string;
60
+ declare function decodeBitmap(b: Bitmap, decoder?: (bytes: Uint8Array, eci: number) => string): string;
47
61
  export type DecodeOpts = {
48
62
  cropToSquare?: boolean;
49
63
  textDecoder?: (bytes: Uint8Array) => string;
package/decode.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["src/decode.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;EAeE;AACF;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAiC,KAAK,EAAQ,KAAK,EAAE,MAAM,YAAY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