devmandan Claude Opus 5.5 commited on
Commit
5b3cd1e
·
verified ·
1 Parent(s): a59c3c4

Facts from each code at its own size; the design test's server, not yet on the page

Browse files

- Where zxing-cpp read a content more than once in a frame, the facts and checks leave out its reads from its own smaller copies of that frame (README, /api/measure).
- The design test's server (/api/design) is added and starts with the Space; the page doesn't offer it yet.
- Chrome on Android leaves its own scanner out of a test whose raw bytes aren't UTF-8 text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HykRoFZnnb98KRf6Xxomka

README.md CHANGED
@@ -235,11 +235,17 @@ byte filled with zeros, in base64; a turned or light-on-dark code's squares come
235
  code's transposed); `raw_sha256` and `raw_bytes`, the SHA-256 and the count of the bytes zxing-cpp read; and `copies`,
236
  how many copies of that code it read there: its reads of the content with the same error correction and mask and,
237
  where both have corners, the same squares across and a size within 10% (zxing-cpp returned at most 255 codes from one
238
- picture in tests). All of these are null when zxing-cpp never read the content with that figure, and for a reading cut
239
- short. `not_run` lists the decodes the many-codes rule left out, as {scanner: [frame sizes]}, empty when none. And it
240
- adds `content.sha256`, the content's digest, taken from its raw bytes so that pictures of one code match whichever
241
- scanners read them, and `content.sha256s`, the digests of every set of raw bytes the content can stand for: one, or
242
- two for a non-ASCII text whose characters fit both ISO-8859-1 and Shift JIS (such as "25°C"), which either can encode.
 
 
 
 
 
 
243
 
244
  The page pairs your pictures by those digests (two pictures are of one content when they share one), in your
245
  browser: a later picture names the earliest one with the same content, and a photo compares with the original
 
235
  code's transposed); `raw_sha256` and `raw_bytes`, the SHA-256 and the count of the bytes zxing-cpp read; and `copies`,
236
  how many copies of that code it read there: its reads of the content with the same error correction and mask and,
237
  where both have corners, the same squares across and a size within 10% (zxing-cpp returned at most 255 codes from one
238
+ picture in tests). Where zxing-cpp read the content two or more times in the first frame it read it in, none of these
239
+ facts from that frame, nor the checks, use a read it made in its own smaller copies of the frame (a third the size, and
240
+ a third of that while that copy is over 500 pixels on its longer side, which it reads only while it has fewer than 255
241
+ codes), whose corners are coarse and whose squares were sampled at a third of the frame's resolution or less: it reads
242
+ the frame again at its own size alone (unless the reading has run out of time, or that look fails), and only the reads
243
+ that look repeats count, unless it repeats none. All of these are
244
+ null when zxing-cpp never read the content with that figure, and for a reading cut short. `not_run` lists the decodes
245
+ the many-codes rule left out, as {scanner: [frame sizes]}, empty when none. And it adds `content.sha256`, the content's
246
+ digest, taken from its raw bytes so that pictures of one code match whichever scanners read them, and `content.sha256s`,
247
+ the digests of every set of raw bytes the content can stand for: one, or two for a non-ASCII text whose characters fit
248
+ both ISO-8859-1 and Shift JIS (such as "25°C"), which either can encode.
249
 
250
  The page pairs your pictures by those digests (two pictures are of one content when they share one), in your
251
  browser: a later picture names the earliest one with the same content, and a photo compares with the original
server/design.py ADDED
@@ -0,0 +1,299 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """The design test: a picture marked as the original graphic is sent again, rendered beside a plain code with the same
2
+ content, version, error correction and mask in 20 simulated everyday scenes, and read there by zxing-cpp and WeChat's
3
+ scanner: one test at a time across all visitors, in a low-priority worker of its own (design_worker.py) that never
4
+ takes a reading slot. When the visitor's browser has a scanner the test can use, its page also fetches each scene's
5
+ pictures and reads them itself; what it reads never comes back. Only answers leave the server, never reads or counts.
6
+
7
+ POST /api/design/slot[?ticket=] the worker for this page, before any picture is sent: {slot, hold_s}; busy
8
+ (503, Retry-After) with a ticket that keeps the page's place; or off (503)
9
+ POST /api/design?slot=[&frames=jpeg|png] the picture, under /api/measure's rules: 202 {test, n, scenes, frames,
10
+ version, ec_level, mask, truths}, or 200 {verdict: "cant", why}
11
+ GET /api/design/<test>[?frames=0] {state: "running", done}, {state: "done", scanners, ms} or {state: "late"};
12
+ from frames=0 on, no pictures are kept for the page
13
+ GET /api/design/<test>/scene/<s> a scene's box and its two frames' SHA-256, once it has rendered
14
+ GET /api/design/<test>/scene/<s>/jpeg the design's capture JPEG; .../patch, the plain code's patch as PNG
15
+ GET /api/design/<test>/frame/<k> a whole frame as PNG: k = 2s the plain code's, 2s + 1 the design's
16
+ DELETE /api/design/<id> the page leaves a test, a slot (and any test started under it) or a ticket
17
+ The state lives in memory only (design_queue.py), under random ids; nothing about a test is logged or stored. For
18
+ acceptance runs only, QSD_DESIGN_FAIL_SELFTEST=1 turns the test off as a failed self-test does.
19
+ """
20
+
21
+ from __future__ import annotations
22
+
23
+ import asyncio
24
+ import json
25
+ import logging
26
+ import os
27
+ import re
28
+ import tempfile
29
+ import threading
30
+ import time
31
+ from pathlib import Path
32
+
33
+ from anyio import CapacityLimiter, to_thread
34
+ from starlette.requests import ClientDisconnect, Request
35
+ from starlette.responses import JSONResponse, Response
36
+ from starlette.routing import Route
37
+
38
+ from .design_queue import Queue
39
+ from .design_rules import PINS, answer, judge, picture_size, settings
40
+ from .design_worker import SCENE_S, SELFTEST_S, SETUP_S, DesignWorker
41
+
42
+ __all__ = ["answer", "design_health", "design_routes", "start_design", "stop_design"]
43
+
44
+ ID = re.compile(r"[0-9a-f]{32}")
45
+ INDEX = re.compile(r"0|[1-9][0-9]?")
46
+ UNREADABLE = "This picture could not be opened." # the reading's own words for such a picture
47
+ BUILDS = CapacityLimiter(2) # fallback frames built at once; the rest wait, never in the threads readings share
48
+ _S: dict = {"queue": None, "worker": None, "cfg": None, "helpers": {}, "same_frames": None, "closing": False}
49
+
50
+
51
+ def start_design(data_dir: Path, make_worker=DesignWorker, now=time.monotonic) -> None:
52
+ """Start the test, after the reading's own self-test: its settings, its worker and its start-up self-test. Never
53
+ raises: on any failure the test is off, /api/health says so, and the page starts no test."""
54
+ stop_design()
55
+ _S["closing"], _S["same_frames"], worker = False, None, None
56
+ try:
57
+ cfg = settings(Path(data_dir) / "test.json", Path(data_dir) / "sizes.json")
58
+ pins = json.loads(PINS.read_text(encoding="utf-8"))
59
+ worker = make_worker(cfg["nice"])
60
+ got = worker.ask(("selftest", cfg["check"]["png"], cfg["check"]["jpeg"], pins["seeds"], cfg["worker"]),
61
+ SELFTEST_S)
62
+ ok, _S["same_frames"] = judge(got[1] if got and got[0] == "selftest" else None, pins,
63
+ cfg["check"]["jpeg_grey_sha256"])
64
+ if not ok or os.environ.get("QSD_DESIGN_FAIL_SELFTEST") == "1":
65
+ raise RuntimeError("its start-up self-test failed")
66
+ except Exception as e: # noqa: BLE001 - the test is off; the reading runs as before
67
+ if worker is not None:
68
+ worker.close()
69
+ logging.getLogger("uvicorn.error").warning("The design test is off: %s", e)
70
+ return
71
+ _S.update(cfg=cfg, worker=worker, queue=Queue(cfg["queue"], now))
72
+
73
+
74
+ def stop_design() -> None:
75
+ q, w = _S["queue"], _S["worker"]
76
+ _S.update(closing=True, queue=None, worker=None)
77
+ if q is not None:
78
+ q.on = False
79
+ if w is not None:
80
+ w.close()
81
+
82
+
83
+ def design_health() -> dict:
84
+ q = _S["queue"]
85
+ return {"on": bool(q is not None and q.on), "busy": bool(q is not None and q.busy()),
86
+ "same_frames": _S["same_frames"]}
87
+
88
+
89
+ # ---- a test, in threads of its own -------------------------------------------------------------------------------
90
+
91
+ def _replace(q: Queue, w: DesignWorker) -> None:
92
+ """A worker that went silent or died is replaced; when none can start, the test is off."""
93
+ try:
94
+ if not _S["closing"]:
95
+ w.replace()
96
+ except Exception: # noqa: BLE001 - no worker: the test is off
97
+ q.on = False
98
+ q.release()
99
+
100
+
101
+ def _setup(q: Queue, w: DesignWorker, test, data: bytes, suffix: str) -> tuple:
102
+ """Setup, and the state it leads to, made here, whether or not the request still waits for it: the worker's
103
+ reply, ("failed",) when it went silent or died, or ("gone",) when the page left during setup."""
104
+ try:
105
+ with tempfile.NamedTemporaryFile(prefix="qsd-", suffix=suffix) as f: # deleted once setup has read it
106
+ f.write(data)
107
+ f.flush()
108
+ got = w.ask(("setup", f.name, test.mode, _S["cfg"]["worker"]), min(SETUP_S, test.deadline - q.now()))
109
+ except Exception: # noqa: BLE001 - a worker that couldn't start again: as one that died
110
+ got = None
111
+ if got is None or got[0] not in ("set", "cant", "unreadable"):
112
+ q.lose(test)
113
+ threading.Thread(target=_replace, args=(q, w), daemon=True).start()
114
+ return ("failed",)
115
+ if got[0] != "set":
116
+ q.no_test(test)
117
+ elif not q.started(test, got[1]): # cleared before it is freed, so that no next setup reaches it first
118
+ w.tell(("clear",))
119
+ q.release()
120
+ return ("gone",)
121
+ else:
122
+ threading.Thread(target=_run, args=(q, w, _S["cfg"]["rule"], test), daemon=True).start()
123
+ return got
124
+
125
+
126
+ def _run(q: Queue, w: DesignWorker, rule: dict, test) -> None:
127
+ """The test's scenes, one at a time, then the rule; late past the deadline or when the worker fails. Once its page
128
+ has left it, the scene in hand is the last, and nothing is kept."""
129
+ try:
130
+ while True:
131
+ step = q.next_scene(test)
132
+ if step is None:
133
+ w.tell(("clear",))
134
+ q.release()
135
+ return
136
+ got = w.ask(("scene", *step), min(SCENE_S, test.deadline - q.now()))
137
+ if got is None or got[0] != "scene" or got[1] != step[0] or not isinstance(got[2], dict):
138
+ break
139
+ if q.scene_done(test, step[0], got[2]):
140
+ w.tell(("clear",)) # before finish() frees the worker, as above
141
+ q.finish(test, {name: answer(*counts, **rule) for name, counts in test.counts.items()})
142
+ return
143
+ except Exception: # noqa: BLE001 - a fault ends the test as late, never the server
144
+ pass
145
+ q.lose(test)
146
+ _replace(q, w)
147
+
148
+
149
+ # ---- the routes ---------------------------------------------------------------------------------------------------
150
+
151
+ def _json(data, status: int = 200, headers: dict | None = None) -> JSONResponse:
152
+ return JSONResponse(data, status_code=status, headers={"Cache-Control": "no-store", **(headers or {})})
153
+
154
+
155
+ def _err(status: int, code: str, headers: dict | None = None) -> JSONResponse:
156
+ return _json({"error": code}, status, headers)
157
+
158
+
159
+ def _picture(data: bytes, kind: str) -> Response:
160
+ return Response(data, media_type=f"image/{kind}", headers={"Cache-Control": "no-store"})
161
+
162
+
163
+ async def slot(request: Request) -> JSONResponse:
164
+ q, ticket = _S["queue"], request.query_params.get("ticket")
165
+ if q is None:
166
+ return _err(503, "off")
167
+ waiting = bool(_S["cfg"]["readings_switch"] and _S["helpers"]["readings_waiting"]())
168
+ got = q.slot(ticket if ticket and ID.fullmatch(ticket) else None, waiting)
169
+ if got[0] == "go":
170
+ return _json({"slot": got[1], "hold_s": got[2]})
171
+ if got[0] == "off":
172
+ return _err(503, "off")
173
+ return _json({"error": "busy", "ticket": got[1], "retry_s": got[2]}, 503, {"Retry-After": str(got[2])})
174
+
175
+
176
+ async def upload(request: Request) -> Response:
177
+ q, h = _S["queue"], _S["helpers"]
178
+ mode, slot_id = request.query_params.get("frames"), request.query_params.get("slot", "")
179
+ if mode is not None and mode not in ("jpeg", "png"):
180
+ return _err(400, "bad request")
181
+ if q is None or not ID.fullmatch(slot_id) or not q.upload(slot_id):
182
+ return _err(409, "slot")
183
+ test, retry = None, False
184
+ try:
185
+ try:
186
+ async with asyncio.timeout(h["body_s"]):
187
+ data = await h["body"](request)
188
+ except TimeoutError:
189
+ return h["problem"](408, h["messages"][408])
190
+ except ClientDisconnect:
191
+ return h["problem"](400, h["messages"][400])
192
+ if data is None:
193
+ return h["problem"](413, h["messages"][413])
194
+ suffix = h["sniff"](data)
195
+ if suffix is None:
196
+ retry = True
197
+ return h["problem"](415, h["messages"][415])
198
+ size = picture_size(data)
199
+ if size is None or size[0] * size[1] > _S["cfg"]["worker"]["max_pixels"]: # never opened as float
200
+ return _json({"verdict": "cant", "why": "big"}) if size else h["problem"](422, UNREADABLE)
201
+ test = q.arrive(slot_id, mode)
202
+ return _err(409, "slot") if test is None else await _start(q, test, data, suffix)
203
+ finally:
204
+ if test is None:
205
+ q.refuse(slot_id, retry)
206
+
207
+
208
+ async def _start(q: Queue, test, data: bytes, suffix: str) -> Response:
209
+ got = await to_thread.run_sync(_setup, q, _S["worker"], test, data, suffix)
210
+ if got[0] == "failed":
211
+ return _err(503, "failed")
212
+ if got[0] == "gone": # its page left during setup
213
+ return _err(409, "slot")
214
+ if got[0] == "unreadable":
215
+ return _S["helpers"]["problem"](422, UNREADABLE)
216
+ if got[0] == "cant":
217
+ return _json({"verdict": "cant", "why": got[1]})
218
+ f, n = got[1], _S["cfg"]["n"]
219
+ return _json({"test": test.id, "n": n, "scenes": n if test.mode else 0, "frames": test.mode,
220
+ "version": f["version"], "ec_level": f["ec_level"], "mask": f["mask"],
221
+ "truths": [f["raw_sha256"], f["text_sha256"]]}, 202)
222
+
223
+
224
+ async def poll(request: Request) -> JSONResponse:
225
+ q, tid = _S["queue"], request.path_params["test"]
226
+ got = q.poll(tid, request.query_params.get("frames") == "0") if q is not None and ID.fullmatch(tid) else None
227
+ return _err(404, "gone") if got is None else _json(got)
228
+
229
+
230
+ async def delete(request: Request) -> Response:
231
+ q, any_id = _S["queue"], request.path_params["test"]
232
+ if q is not None and ID.fullmatch(any_id):
233
+ q.delete(any_id)
234
+ return Response(status_code=204, headers={"Cache-Control": "no-store"})
235
+
236
+
237
+ async def _held(request: Request, key: str = "s", per: int = 1):
238
+ """(what the scene holds, the test's mode, the index) once it has rendered, after waiting up to scene_wait_s for
239
+ it; else the 404 or 503 to answer."""
240
+ q, tid, k = _S["queue"], request.path_params["test"], request.path_params[key]
241
+ if q is None or not ID.fullmatch(tid) or not INDEX.fullmatch(k) or int(k) >= per * _S["cfg"]["n"]:
242
+ return _err(404, "gone")
243
+ end = time.monotonic() + _S["cfg"]["scene_wait_s"]
244
+ while (got := q.scene(tid, int(k) // per)) is not None and got[0] == "wait":
245
+ if time.monotonic() >= end:
246
+ return _err(503, "rendering", {"Retry-After": "1"})
247
+ await asyncio.sleep(0.05)
248
+ return _err(404, "gone") if got is None else (got[1], got[2], int(k))
249
+
250
+
251
+ async def scene(request: Request) -> Response:
252
+ got = await _held(request)
253
+ return got if isinstance(got, Response) else _json({"box": got[0]["box"], "sha256": got[0]["sha256"]})
254
+
255
+
256
+ async def scene_jpeg(request: Request) -> Response:
257
+ got = await _held(request)
258
+ if isinstance(got, Response):
259
+ return got
260
+ return _picture(got[0]["jpeg"], "jpeg") if got[1] == "jpeg" and got[0]["jpeg"] else _err(404, "gone")
261
+
262
+
263
+ async def scene_patch(request: Request) -> Response:
264
+ got = await _held(request)
265
+ if isinstance(got, Response):
266
+ return got
267
+ return _picture(got[0]["patch"], "png") if got[0]["patch"] else _err(404, "gone")
268
+
269
+
270
+ def _fallback(held: dict, plain: bool) -> bytes | None:
271
+ from . import design_frames # OpenCV and numpy reach the server only with its first fallback frame
272
+ return design_frames.fallback(held, plain)
273
+
274
+
275
+ async def frame(request: Request) -> Response:
276
+ got = await _held(request, "k", 2)
277
+ if isinstance(got, Response):
278
+ return got
279
+ held, mode, k = got
280
+ if mode == "png" and k % 2: # in a test with PNG frames, the design's comes from the worker
281
+ return _picture(held["png"], "png")
282
+ png = await to_thread.run_sync(_fallback, held, k % 2 == 0, limiter=BUILDS)
283
+ return _picture(png, "png") if png else _err(500, "frame")
284
+
285
+
286
+ def design_routes(helpers: dict) -> list[Route]:
287
+ """The routes, for main.py to put before its static files. helpers: /api/measure's own body(), sniff() and
288
+ problem(), its body_s and the messages it answers with, and readings_waiting(), whether pictures wait to be read."""
289
+ _S["helpers"] = helpers
290
+ return [
291
+ Route("/api/design/slot", slot, methods=["POST"]),
292
+ Route("/api/design", upload, methods=["POST"]),
293
+ Route("/api/design/{test}", poll, methods=["GET"]),
294
+ Route("/api/design/{test}", delete, methods=["DELETE"]),
295
+ Route("/api/design/{test}/scene/{s}", scene, methods=["GET"]),
296
+ Route("/api/design/{test}/scene/{s}/jpeg", scene_jpeg, methods=["GET"]),
297
+ Route("/api/design/{test}/scene/{s}/patch", scene_patch, methods=["GET"]),
298
+ Route("/api/design/{test}/frame/{k}", frame, methods=["GET"]),
299
+ ]
server/design_frames.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """The design test's frames (see design.py): a capture JPEG decoded as OpenCV decodes it (libjpeg-turbo's accurate
2
+ integer IDCT), the whole-number shrink from the 1920 x 1440 capture to the 1280 x 960 grey frame every scanner reads,
3
+ each frame's SHA-256 over its 1,228,800 bytes row by row, the box where the plain code's frame differs from the
4
+ design's, the plain code's patch (its frame inside that box, as a grey PNG at compression level 1), the paste that
5
+ rebuilds its frame from the design's, and a fallback frame: a whole frame as a grey PNG at level 1, built from what the
6
+ server holds for its scene and checked against the scene's own hash before it is sent.
7
+
8
+ The shrink: on each axis, output index i reads the source (3i + 1) >> 1 with weight 2/3 and 3(i >> 1) + 1 with weight
9
+ 1/3. With a the source pixel weighted 2/3 both ways, b and c 2/3 one way and 1/3 the other, and d 1/3 both ways, a frame
10
+ pixel is (4a + 2b + 2c + d + 4) div 9: OpenCV's INTER_AREA at this size, exactly, in whole numbers, so the page makes
11
+ the same frames from the same JPEG on any processor.
12
+
13
+ The test worker imports this module, and the server itself only for its first fallback frame: it never loads the scene
14
+ renderer or the scanners.
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import base64
20
+ import hashlib
21
+
22
+ import cv2
23
+ import numpy as np
24
+
25
+ CAPTURE = (1920, 1440) # width, height
26
+ FRAME = (1280, 960)
27
+ PNG = [cv2.IMWRITE_PNG_COMPRESSION, 1]
28
+ _X, _Y = np.arange(FRAME[0]), np.arange(FRAME[1])
29
+ _X2, _X1 = (3 * _X + 1) >> 1, 3 * (_X >> 1) + 1 # each output's source weighted 2/3, and the one weighted 1/3
30
+ _Y2, _Y1 = (3 * _Y + 1) >> 1, 3 * (_Y >> 1) + 1
31
+
32
+
33
+ def decode_grey(jpeg: bytes) -> np.ndarray | None:
34
+ """A JPEG's grey pixels as OpenCV decodes them; None when it doesn't decode."""
35
+ g = cv2.imdecode(np.frombuffer(jpeg, np.uint8), cv2.IMREAD_GRAYSCALE)
36
+ return g if g is not None and g.dtype == np.uint8 and g.ndim == 2 else None
37
+
38
+
39
+ def shrink(capture: np.ndarray) -> np.ndarray:
40
+ """The 1280 x 960 frame of a 1920 x 1440 grey capture, by the whole-number rule."""
41
+ if capture.shape != (CAPTURE[1], CAPTURE[0]) or capture.dtype != np.uint8:
42
+ raise ValueError(f"not a {CAPTURE[0]} x {CAPTURE[1]} grey capture: {capture.shape} {capture.dtype}")
43
+ g = capture.astype(np.uint16) # at most 9 x 255: no overflow
44
+ v = 2 * g[_Y2] + g[_Y1]
45
+ t = 2 * v[:, _X2] + v[:, _X1]
46
+ return ((t + 4) // 9).astype(np.uint8)
47
+
48
+
49
+ def frame_of(jpeg: bytes) -> np.ndarray | None:
50
+ """The frame the scanners read from a capture JPEG; None when it isn't a 1920 x 1440 grey JPEG."""
51
+ g = decode_grey(jpeg)
52
+ return None if g is None or g.shape != (CAPTURE[1], CAPTURE[0]) else shrink(g)
53
+
54
+
55
+ def sha256(frame: np.ndarray) -> str:
56
+ """The SHA-256 of a frame's bytes, row by row, one byte a pixel."""
57
+ return hashlib.sha256(np.ascontiguousarray(frame, np.uint8).tobytes()).hexdigest()
58
+
59
+
60
+ def box_of(plain: np.ndarray, design: np.ndarray) -> list[int]:
61
+ """[x, y, w, h], in frame pixels, of where the plain code's frame differs from the design's; [0, 0, 0, 0] when
62
+ they are equal."""
63
+ differ = plain != design
64
+ rows = np.flatnonzero(differ.any(axis=1))
65
+ if rows.size == 0:
66
+ return [0, 0, 0, 0]
67
+ cols = np.flatnonzero(differ.any(axis=0))
68
+ return [int(cols[0]), int(rows[0]), int(cols[-1] + 1 - cols[0]), int(rows[-1] + 1 - rows[0])]
69
+
70
+
71
+ def png_of(grey: np.ndarray) -> bytes:
72
+ """A grey picture as a PNG at compression level 1."""
73
+ ok, enc = cv2.imencode(".png", np.ascontiguousarray(grey, np.uint8), PNG)
74
+ if not ok:
75
+ raise RuntimeError("PNG encoding failed")
76
+ return enc.tobytes()
77
+
78
+
79
+ def patch_of(plain: np.ndarray, box: list[int]) -> bytes | None:
80
+ """The plain code's frame inside the box, as a grey PNG at level 1; None for an empty box."""
81
+ x, y, w, h = box
82
+ return png_of(plain[y:y + h, x:x + w]) if w and h else None
83
+
84
+
85
+ def decode_png(png: bytes, w: int, h: int) -> np.ndarray | None:
86
+ """A grey PNG of exactly w x h pixels, one byte each; None otherwise."""
87
+ g = cv2.imdecode(np.frombuffer(png, np.uint8), cv2.IMREAD_UNCHANGED)
88
+ return g if g is not None and g.dtype == np.uint8 and g.shape == (h, w) else None
89
+
90
+
91
+ def paste(design: np.ndarray, patch: bytes | None, box: list[int]) -> np.ndarray | None:
92
+ """The plain code's frame: a copy of the design's with the patch written at the box; the design's own for an
93
+ empty box. None when the box doesn't lie inside the frame or the patch isn't exactly its size."""
94
+ x, y, w, h = box
95
+ if not (w and h):
96
+ return design.copy()
97
+ if x < 0 or y < 0 or x + w > FRAME[0] or y + h > FRAME[1] or patch is None:
98
+ return None
99
+ p = decode_png(patch, w, h)
100
+ if p is None:
101
+ return None
102
+ out = design.copy()
103
+ out[y:y + h, x:x + w] = p
104
+ return out
105
+
106
+
107
+ def fallback(held: dict, plain: bool) -> bytes | None:
108
+ """A scene's whole frame as a grey PNG at level 1: the design's (from its capture JPEG, or its PNG frame), or the
109
+ plain code's (its patch pasted over the design's). None when what is held doesn't rebuild the frame to the scene's
110
+ own hash, so a frame that fails is never sent."""
111
+ if held.get("png") is not None:
112
+ design = decode_png(held["png"], *FRAME)
113
+ else:
114
+ design = frame_of(held["jpeg"]) if held.get("jpeg") is not None else None
115
+ if design is None:
116
+ return None
117
+ frame = paste(design, held.get("patch"), held["box"]) if plain else design
118
+ if frame is None or sha256(frame) != held["sha256"]["plain" if plain else "design"]:
119
+ return None
120
+ return png_of(frame)
121
+
122
+
123
+ def block_means(frame: np.ndarray) -> str:
124
+ """The frame's 32 x 32 block means, each round(16 x the mean), as 30 x 40 little-endian uint16 in base64: the
125
+ start-up self-test's pixel check."""
126
+ m = frame.reshape(FRAME[1] // 32, 32, FRAME[0] // 32, 32).astype(np.float64).mean(axis=(1, 3))
127
+ return base64.b64encode(np.rint(16 * m).astype("<u2").tobytes()).decode("ascii")
server/design_pins.json ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "about": "The design test’s start-up self-test (design.py): the working check’s code from web/data/test.json as a design beside its own plain twin, in the size table’s everyday scenes at seeds 1 and 2, at version 2’s need (2.6 pixels a square). The facts and both scanners’ reads ([plain code’s frame, design’s frame]) must equal these, every frame’s 32 x 32 block means (x16, little-endian uint16, base64) must lie within tolerance_x16 of these, and each patch must rebuild its frame to its hash. same_frames says whether the frames’ bytes also equal those rendered on a Mac (frame_sha256.mac).",
3
+ "made_with": {
4
+ "opencv": "4.12.0",
5
+ "numpy": "2.2.6",
6
+ "zxing-cpp": "3.1.1",
7
+ "machine": "macOS 14.3.1, arm64"
8
+ },
9
+ "facts": {
10
+ "version": 2,
11
+ "ec_level": "M",
12
+ "mask": 2,
13
+ "raw_sha256": "c127abccc1678a8782bbe8aa087f9b68ec1b0f451b3f4322198c9df89e26d965"
14
+ },
15
+ "seeds": [
16
+ 1,
17
+ 2
18
+ ],
19
+ "tolerance_x16": 16,
20
+ "scenes": [
21
+ {
22
+ "seed": 1,
23
+ "box": [
24
+ 613,
25
+ 469,
26
+ 134,
27
+ 129
28
+ ],
29
+ "reads": {
30
+ "zxing-cpp": [
31
+ 1,
32
+ 1
33
+ ],
34
+ "wechat": [
35
+ 1,
36
+ 1
37
+ ]
38
+ },
39
+ "frame_sha256": {
40
+ "mac": {
41
+ "plain": "dfefb90618551d34d9379e73725c502bbbb1ec568498e1738c6975d0a3232ab9",
42
+ "design": "31286dfc8bf3409e7fa1a3cc15d8316fcc86e50e1eac682e8cba6124cd632990"
43
+ }
44
+ },
45
+ "block_means_x16_b64": {
46
+ "plain": "fQOGA8wDUATwBJMFLQakBuUG6AbGBpMGYAYfBtQFkAVtBWwFvQVEBtcGOwdHB+kGNwZrBbEENAQhBFsEwAQcBUUFMwUEBcQEgwRQBDoEMgSKA5QD2gNbBPMElwXTBYAFlgWYBY0F4gVcBiIG1gWTBW8FdQXEBUoG1wY2B0MH3wYzBmwFsgQ3BCQEZATABB8FSgU+BQoFzgSKBFgEQwQ6BNQD2wMZBJEEGAWtBZcFrQSmBKoEqgRiBVsGKQb1BcEFowWoBegFVwbIBhUHEQe1BhYGXQW5BFMESASHBO0ESwV8BXAFQwUCBcIEkQR9BH0EYgRpBJcE8gRbBdAFogWrBKsEqASqBFUFUAY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QM1A0kDugNhBBoFEgZHB0cHSAdGB0cHRwdHB2kFrATLBOIE5wTrBFIDUgNfA2gDcQODA5wD1wMjBHsEzwQPBTUFMAUMBdEEhgQ7BOkDlgNWAzUDSAOhAykEvgS5BUkHSQdHB0gHSgdKB0cHWQWXBLwE2gTmBOoEXQNgA24DhwOiA8AD6AMeBF4EngTSBPkECwX7BNoErARwBDIE9AO8A40DcANyA6ED6wNFBDkFSQdJB0cHSAdHB0gHSAdGBXwEowTDBNIE1wR9A3wDlgO/A+wDGgRMBHwEowS/BMwE0wTNBMEEqwSPBG0ERQQbBPoD2wPGA7EDqwOzA8QDDQTEBMgExQTGBM0E4AT3BH4EYQSHBKQEuQS/BJ8DowPEA/QDLwRxBKsE1gTlBN0EygStBJEEggR9BHYEbQRbBE4EPQQyBBoE8wO7A38DQwMVA/4CBAMbA0EDdgOsA+cDFgREBGoEhwSgBKEEuQPAA98DGQRpBLgE9gQeBRoF+wTDBI8EZgRTBFYEXARnBGcEZARsBHIEXAQgBMEDUAPcAoICUAJQAn4CzQIlA3wDxwP9Ay0EVgR1BIsEjgTDA8UD5wMtBIEE2AQcBUIFNwUJBcUEfgRSBD8EQwRQBGEEaARuBIEEiwR4BDgEyAMzA6MCMALvAfQBMQKLAvkCYQO2A/EDIQREBGsEhgSKBMQDygPsAy0EgwTcBCUFQgU9BQcFxgSBBEsEPAQ+BEwEXwRlBHEEgQSRBH0EOwTDAzEDnwIkAuIB5QEgAoEC9AJeA7ID7QMeBEMEagSEBIUE"
48
+ }
49
+ },
50
+ {
51
+ "seed": 2,
52
+ "box": [
53
+ 666,
54
+ 442,
55
+ 134,
56
+ 140
57
+ ],
58
+ "reads": {
59
+ "zxing-cpp": [
60
+ 1,
61
+ 1
62
+ ],
63
+ "wechat": [
64
+ 1,
65
+ 1
66
+ ]
67
+ },
68
+ "frame_sha256": {
69
+ "mac": {
70
+ "plain": "f1444e5eb97ab2a98cc0c6569a4c76de33cbc36a10b35712c6a2de44ac2192b0",
71
+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1girCJoIswjhCB4JTAltCWcJSwkfCQEJAwkdCVUJiAnECfEJCwoLCh8HJgdWB6sHGgiKCO8INwk/CRoJ3QimCI8IpQjRCAwJPAlQCT4JDQnZCKkInQiwCOYIIQlXCXgJdAlQCSQJBgkDCSUJWQmVCcwJ9wkTChgK"
77
+ }
78
+ }
79
+ ]
80
+ }
server/design_queue.py ADDED
@@ -0,0 +1,295 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """The design test's state (see design.py), in memory only: the worker's one hold, the slot and its upload, the tickets
2
+ of pages told busy, and the tests with what they keep for their pages. Pure Python with no I/O: design.py calls it from
3
+ the event loop, a test's own thread and the frame builder, and its checks drive it with a fake clock.
4
+
5
+ The hold is one of: free; a slot, waiting for its upload to start (within hold_s) and arrive; a test in setup; a test
6
+ running, scene by scene; a test its page left, stopping after the scene in hand; a ticket's, kept for the oldest live
7
+ ticket until its page comes back; or replacing, while a worker that went silent or died is replaced. When it frees
8
+ with tickets live it goes to the oldest at once, so pages start in the order they were first told busy. A ticket
9
+ lapses when no slot request carrying it comes within its last Retry-After plus ticket_extra_s of its last busy answer.
10
+
11
+ A test is dropped (its routes answer 404) at the first of: a poll with frames=0 that returns "done"; a poll that
12
+ returns "late"; the page's DELETE; idle_s with no request naming it; after_deadline_s past its deadline. Its pictures
13
+ go with it, at a poll with frames=0, or when it is late; and past held_max bytes across tests, the pictures of the
14
+ finished tests idle longest go first.
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import math
20
+ import secrets
21
+ import threading
22
+ import time
23
+ from collections.abc import Callable
24
+ from dataclasses import dataclass, field
25
+
26
+
27
+ def new_id() -> str:
28
+ """A random 128-bit id, for tests, slots and tickets alike."""
29
+ return secrets.token_hex(16)
30
+
31
+
32
+ @dataclass
33
+ class Ticket:
34
+ id: str
35
+ issued_at: float
36
+ last_retry_s: int
37
+ last_answer_at: float
38
+
39
+
40
+ @dataclass
41
+ class Slot:
42
+ id: str
43
+ since: float # when its window for an upload to start opened: the grant, or a 415 answer
44
+ phase: str = "wait" # wait: no upload yet; up: one is arriving; retry: one more may start, after a 415
45
+ retried: bool = False
46
+
47
+
48
+ @dataclass
49
+ class Test:
50
+ id: str
51
+ slot: str
52
+ mode: str | None # "jpeg" or "png" while its page takes pictures; None without them, or from frames=0 on
53
+ arrived_at: float
54
+ deadline: float
55
+ last_request_at: float
56
+ state: str = "setup" # setup, running, done or late
57
+ done: int = 0 # scenes rendered and read
58
+ first_at: float | None = None
59
+ counts: dict = field(default_factory=lambda: {"zxing-cpp": [0, 0, 0, 0], "wechat": [0, 0, 0, 0]})
60
+ scenes: dict = field(default_factory=dict)
61
+ held: int = 0 # bytes of pictures kept for the page
62
+ answers: dict | None = None
63
+ ms: int | None = None
64
+ facts: dict | None = None
65
+ dropped: bool = False
66
+
67
+
68
+ class Queue:
69
+ """The hold, the tickets and the tests. cfg: n, deadline_s, hold_s, ticket_extra_s, idle_s, after_deadline_s,
70
+ retry_s (lowest, highest), max_tickets and held_max (bytes). Every method takes the lock."""
71
+
72
+ def __init__(self, cfg: dict, now: Callable[[], float] = time.monotonic):
73
+ self.cfg, self.now, self.lock = cfg, now, threading.RLock()
74
+ self.hold: tuple = ("free", None)
75
+ self.tickets: dict[str, Ticket] = {} # in the order they were issued
76
+ self.tests: dict[str, Test] = {}
77
+ self.on = True
78
+
79
+ def busy(self) -> bool:
80
+ with self.lock:
81
+ self._tick()
82
+ return self.hold[0] != "free"
83
+
84
+ def release(self) -> None:
85
+ """The worker is free: held for the oldest live ticket, if any, else free."""
86
+ with self.lock:
87
+ self.hold = ("hold", next(iter(self.tickets.values()))) if self.tickets else ("free", None)
88
+
89
+ def retry_s(self) -> int:
90
+ """The running test's own estimate of what's left, in whole seconds from retry_s's lowest to its highest."""
91
+ with self.lock:
92
+ kind, held = self.hold
93
+ now, n, c, per = self.now(), self.cfg["n"], self.cfg, 0.5
94
+ if kind in ("running", "stopping") and held.done and held.first_at is not None:
95
+ per = (now - held.first_at) / held.done
96
+ left = n * per + 1 # free, setup or replacing
97
+ if kind == "running":
98
+ left = (n - held.done) * per + 1
99
+ elif kind == "stopping":
100
+ left = per + 1
101
+ elif kind == "slot" and held.phase != "up":
102
+ left += max(0.0, c["hold_s"] - (now - held.since))
103
+ elif kind == "hold":
104
+ left += max(0.0, held.last_answer_at + held.last_retry_s + c["ticket_extra_s"] - now)
105
+ return int(min(c["retry_s"][1], max(c["retry_s"][0], math.ceil(left))))
106
+
107
+ def slot(self, ticket: str | None, readings_wait: bool = False) -> tuple:
108
+ """A slot request: ("go", slot, hold_s), ("busy", ticket or None, retry_s) or ("off",)."""
109
+ with self.lock:
110
+ self._tick()
111
+ if not self.on:
112
+ return ("off",)
113
+ if self.hold[0] == "free" and self.tickets:
114
+ self.release()
115
+ kind, held = self.hold
116
+ now = self.now()
117
+ if not readings_wait and (kind == "free" or (kind == "hold" and held.id == ticket)):
118
+ if kind == "hold":
119
+ del self.tickets[held.id]
120
+ s = Slot(new_id(), now)
121
+ self.hold = ("slot", s)
122
+ return ("go", s.id, self.cfg["hold_s"])
123
+ retry = self.retry_s()
124
+ t = self.tickets.get(ticket) if ticket else None
125
+ if t is None:
126
+ if len(self.tickets) >= self.cfg["max_tickets"]:
127
+ return ("busy", None, self.cfg["retry_s"][1])
128
+ t = Ticket(new_id(), now, retry, now)
129
+ self.tickets[t.id] = t
130
+ t.last_retry_s, t.last_answer_at = retry, now
131
+ return ("busy", t.id, retry)
132
+
133
+ def upload(self, slot: str) -> bool:
134
+ """An upload starts under slot: True while the slot is live and no upload of it is under way."""
135
+ with self.lock:
136
+ self._tick()
137
+ kind, s = self.hold
138
+ if kind != "slot" or s.id != slot or s.phase == "up":
139
+ return False
140
+ s.phase = "up"
141
+ return True
142
+
143
+ def refuse(self, slot: str, retry: bool = False) -> None:
144
+ """The upload under slot ended without a test: the slot lapses, or after its first 415 waits once more."""
145
+ with self.lock:
146
+ kind, s = self.hold
147
+ if kind == "slot" and s.id == slot:
148
+ if retry and not s.retried:
149
+ s.phase, s.since, s.retried = "retry", self.now(), True
150
+ else:
151
+ self.release()
152
+
153
+ def arrive(self, slot: str, mode: str | None) -> Test | None:
154
+ """The picture under slot has arrived: its test, in setup; None when the slot went meanwhile."""
155
+ with self.lock:
156
+ kind, s = self.hold
157
+ if kind != "slot" or s.id != slot or s.phase != "up":
158
+ return None
159
+ now = self.now()
160
+ t = Test(new_id(), slot, mode, now, now + self.cfg["deadline_s"], now)
161
+ self.tests[t.id], self.hold = t, ("setup", t)
162
+ return t
163
+
164
+ def started(self, test: Test, facts: dict) -> bool:
165
+ """Setup passed: the test runs, unless its page left it meanwhile. Then the caller clears the worker, and only
166
+ then frees it (release), so that no next setup reaches the worker before the clear."""
167
+ with self.lock:
168
+ if test.dropped:
169
+ return False
170
+ test.state, test.facts, self.hold = "running", facts, ("running", test)
171
+ return True
172
+
173
+ def no_test(self, test: Test) -> None:
174
+ """Setup refused the picture, or couldn't open it: no test, and the worker is free."""
175
+ with self.lock:
176
+ self._forget(test)
177
+ self.release()
178
+
179
+ def lose(self, test: Test) -> None:
180
+ """The worker went silent or died with this test: it is late (in setup, it never was), and the worker is being
181
+ replaced, after which release() follows."""
182
+ with self.lock:
183
+ if test.state == "setup":
184
+ self._forget(test)
185
+ else:
186
+ test.state, test.scenes, test.held = "late", {}, 0
187
+ self.hold = ("replacing", None)
188
+
189
+ def next_scene(self, test: Test) -> tuple[int, str | None] | None:
190
+ """The next scene to render, and the pictures it keeps for the page; None once the page has left the test
191
+ (the caller then frees the worker)."""
192
+ with self.lock:
193
+ self._tick()
194
+ if test.dropped:
195
+ return None
196
+ if test.first_at is None:
197
+ test.first_at = self.now()
198
+ return test.done, test.mode
199
+
200
+ def scene_done(self, test: Test, s: int, got: dict) -> bool:
201
+ """A scene's reads, added to the counts and not kept, and its pictures while the page takes them; True after
202
+ the last scene."""
203
+ with self.lock:
204
+ for name, (t, d) in got["reads"].items():
205
+ c = test.counts[name]
206
+ c[0], c[1], c[2], c[3] = c[0] + int(t and not d), c[1] + int(d and not t), c[2] + int(t), c[3] + int(d)
207
+ test.done = s + 1
208
+ if test.mode and not test.dropped and got.get("sha256"):
209
+ kept = test.scenes[s] = {k: got.get(k) for k in ("box", "sha256", "jpeg", "png", "patch")}
210
+ test.held += sum(len(kept[k] or b"") for k in ("jpeg", "png", "patch"))
211
+ self._bound()
212
+ return test.done >= self.cfg["n"]
213
+
214
+ def finish(self, test: Test, answers: dict) -> None:
215
+ """After the last scene: the answers, and the worker is free."""
216
+ with self.lock:
217
+ test.state, test.answers = "done", answers
218
+ test.ms = round(1000 * (self.now() - test.arrived_at))
219
+ self.release()
220
+
221
+ def poll(self, tid: str, frames0: bool) -> dict | None:
222
+ """A poll's answer, or None (404)."""
223
+ with self.lock:
224
+ self._tick()
225
+ t = self.tests.get(tid)
226
+ if t is None:
227
+ return None
228
+ t.last_request_at = self.now()
229
+ if frames0:
230
+ t.mode, t.scenes, t.held = None, {}, 0
231
+ if t.state == "done":
232
+ if frames0:
233
+ self._drop(t)
234
+ return {"state": "done", "scanners": dict(t.answers), "ms": t.ms}
235
+ if t.state == "late":
236
+ self._drop(t)
237
+ return {"state": "late"}
238
+ return {"state": "running", "done": t.done}
239
+
240
+ def scene(self, tid: str, s: int) -> tuple | None:
241
+ """("ready", what scene s keeps, the test's mode), ("wait", None, mode) while it renders, or None (404)."""
242
+ with self.lock:
243
+ self._tick()
244
+ t = self.tests.get(tid)
245
+ if t is None or t.mode is None or not 0 <= s < self.cfg["n"]:
246
+ return None
247
+ t.last_request_at = self.now()
248
+ if s in t.scenes:
249
+ return "ready", t.scenes[s], t.mode
250
+ return ("wait", None, t.mode) if t.state == "running" and s >= t.done else None
251
+
252
+ def delete(self, any_id: str) -> None:
253
+ """The page leaves a test, a slot or a ticket: dropped at once. A slot's id also names the test started under
254
+ it, which the page may know by its slot alone: in setup it doesn't start, and running it stops after the scene
255
+ in hand."""
256
+ with self.lock:
257
+ self._tick()
258
+ kind, held = self.hold
259
+ t = self.tests.get(any_id) or next((x for x in self.tests.values() if x.slot == any_id), None)
260
+ if t is not None:
261
+ self._drop(t)
262
+ elif kind == "slot" and held.id == any_id:
263
+ self.release()
264
+ elif self.tickets.pop(any_id, None) is not None and kind == "hold" and held.id == any_id:
265
+ self.release()
266
+
267
+ def _forget(self, t: Test) -> None:
268
+ t.dropped, t.scenes, t.held = True, {}, 0
269
+ self.tests.pop(t.id, None)
270
+
271
+ def _drop(self, t: Test) -> None:
272
+ self._forget(t)
273
+ if self.hold[0] == "running" and self.hold[1] is t:
274
+ self.hold = ("stopping", t)
275
+
276
+ def _bound(self) -> None:
277
+ total = sum(t.held for t in self.tests.values())
278
+ for t in sorted((t for t in self.tests.values() if t.state == "done" and t.held),
279
+ key=lambda t: t.last_request_at):
280
+ if total <= self.cfg["held_max"]:
281
+ return
282
+ total, t.scenes, t.held = total - t.held, {}, 0
283
+
284
+ def _tick(self) -> None:
285
+ now, c = self.now(), self.cfg
286
+ kind, held = self.hold
287
+ if kind == "slot" and held.phase != "up" and now - held.since > c["hold_s"]:
288
+ self.release()
289
+ for t in [t for t in self.tickets.values() if now - t.last_answer_at > t.last_retry_s + c["ticket_extra_s"]]:
290
+ del self.tickets[t.id]
291
+ if self.hold[0] == "hold" and self.hold[1] is t:
292
+ self.release()
293
+ for t in [t for t in self.tests.values()
294
+ if now - t.last_request_at > c["idle_s"] or now > t.deadline + c["after_deadline_s"]]:
295
+ self._drop(t)
server/design_rules.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """The design test's rules (see design.py): its settings, read once at start-up from web/data/test.json (the page's own
2
+ file, so the page and the server never disagree) and sizes.json's needs; each scanner's answer; the size check from a
3
+ picture's header, before it is opened; the start-up self-test's judgement against its pinned values
4
+ (design_pins.json); and, in the test worker, the setup's checks on zxing-cpp's reads of a picture
5
+ (design_worker._setup): its reads at the picture's own size, copies of one code, polarity and mirroring.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import base64
11
+ import json
12
+ import struct
13
+ from math import comb, inf
14
+ from pathlib import Path
15
+ from types import SimpleNamespace
16
+
17
+ from .reading_facts import _copy_of, own_reads
18
+ from .reading_worker import _corners
19
+
20
+ PINS = Path(__file__).with_name("design_pins.json")
21
+
22
+
23
+ def answer(b: int, c: int, t: int, d: int, inv: int = 20, cost: int = 2, q: int = 16) -> str:
24
+ """One scanner's answer over the paired scenes: b counts the scenes where it read the plain code and missed the
25
+ design, c the reverse, t and d the scenes where it read the plain code and the design. "worse": a one-sided exact
26
+ sign test on (b, c) under 1 / inv, in whole numbers (inv x the tail's count below 2 ** (b + c)); "like": a cost
27
+ b - c of at most `cost`, with the plain code or the design read in at least q scenes; otherwise "close" (a cost
28
+ over `cost`) or "few"."""
29
+ n = b + c
30
+ if inv * sum(comb(n, k) for k in range(b, n + 1)) < 2 ** n:
31
+ return "worse"
32
+ if b - c <= cost and (t >= q or d >= q):
33
+ return "like"
34
+ return "close" if b - c > cost else "few"
35
+
36
+
37
+ def picture_size(data: bytes) -> tuple[int, int] | None:
38
+ """(width, height) from a PNG's, a JPEG's or a WebP's header; None when the header doesn't say."""
39
+ try:
40
+ if data[:8] == b"\x89PNG\r\n\x1a\n" and data[12:16] == b"IHDR":
41
+ return struct.unpack(">II", data[16:24])
42
+ if data[:4] == b"RIFF" and data[8:12] == b"WEBP":
43
+ if data[12:16] == b"VP8 ": # a key frame: its start code, then 14-bit width and height
44
+ w, h = struct.unpack("<HH", data[26:30])
45
+ return w & 0x3FFF, h & 0x3FFF
46
+ if data[12:16] == b"VP8L":
47
+ bits = int.from_bytes(data[21:25], "little")
48
+ return (bits & 0x3FFF) + 1, ((bits >> 14) & 0x3FFF) + 1
49
+ if data[12:16] == b"VP8X":
50
+ return int.from_bytes(data[24:27], "little") + 1, int.from_bytes(data[27:30], "little") + 1
51
+ return None
52
+ i = 2 if data[:2] == b"\xff\xd8" else len(data)
53
+ while i + 9 <= len(data) and data[i] == 0xFF:
54
+ m = data[i + 1]
55
+ if m == 0xFF: # a fill byte
56
+ i += 1
57
+ elif m == 0x01 or 0xD0 <= m <= 0xD7: # TEM and RST0-7 carry no length, as libjpeg reads them
58
+ i += 2
59
+ elif 0xC0 <= m <= 0xCF and m not in (0xC4, 0xC8, 0xCC): # a frame header: height, then width
60
+ h, w = struct.unpack(">HH", data[i + 5:i + 9])
61
+ return w, h
62
+ elif m == 0xDA: # scan data before any frame header
63
+ return None
64
+ else:
65
+ i += 2 + struct.unpack(">H", data[i + 2:i + 4])[0]
66
+ except struct.error:
67
+ return None
68
+ return None
69
+
70
+
71
+ def settings(test_json: Path, sizes_json: Path) -> dict:
72
+ """The test's settings; raises on a missing or invalid file."""
73
+ tj = json.loads(test_json.read_text(encoding="utf-8"))
74
+ needs = json.loads(sizes_json.read_text(encoding="utf-8"))["needs"]
75
+ sv, lim, rule, check = tj["server"], tj["limits"], tj["rule"], tj["check"]
76
+ n, seeds, inv = int(tj["scenes"]["s"]), [int(s) for s in tj["scenes"]["seeds"]], round(1 / rule["alpha"])
77
+ if len(seeds) != n or abs(1 / inv - rule["alpha"]) > 1e-12 or not needs:
78
+ raise ValueError("test.json: its scenes, or its rule's alpha")
79
+ if int(lim["max_version"]) > max(int(x["version"]) for x in needs): # p(v) is the next measured version up
80
+ raise ValueError("test.json: max_version beyond sizes.json's needs")
81
+ lo, hi = (int(x) for x in sv["retry_s"])
82
+ return {
83
+ "n": n, "nice": int(sv["nice"]), "scene_wait_s": float(sv["scene_wait_s"]),
84
+ "readings_switch": bool(sv["busy_while_readings_wait"]),
85
+ "rule": {"inv": inv, "cost": int(rule["like_cost"]), "q": int(rule["q"])},
86
+ "worker": {"max_version": int(lim["max_version"]), "min_ppm": float(lim["min_ppm"]),
87
+ "min_ppm_v1": float(lim["min_ppm_v1"]), "max_pixels": int(lim["max_pixels"]), "needs": needs,
88
+ "seeds": seeds},
89
+ "queue": {"n": n, "deadline_s": float(sv["deadline_s"]), "hold_s": int(sv["slot_hold_s"]),
90
+ "ticket_extra_s": float(sv["ticket_extra_s"]), "idle_s": float(sv["idle_s"]),
91
+ "after_deadline_s": float(sv["after_deadline_s"]), "retry_s": (lo, hi),
92
+ "max_tickets": int(sv["max_tickets"]), "held_max": int(sv["held_mb_max"]) * 1_000_000},
93
+ "check": {"png": base64.b64decode(check["png"]["b64"]), "jpeg": base64.b64decode(check["jpeg"]["b64"]),
94
+ "jpeg_grey_sha256": check["jpeg"]["grey_sha256"]},
95
+ }
96
+
97
+
98
+ def _near(a: str, b: str, tolerance: int) -> bool:
99
+ """Two frames' block means (base64 of little-endian uint16, x16) within the tolerance everywhere."""
100
+ x, y = base64.b64decode(a), base64.b64decode(b)
101
+ if len(x) != len(y) or not x or len(x) % 2:
102
+ return False
103
+ x, y = struct.unpack(f"<{len(x) // 2}H", x), struct.unpack(f"<{len(y) // 2}H", y)
104
+ return max(abs(i - j) for i, j in zip(x, y, strict=True)) <= tolerance
105
+
106
+
107
+ def judge(got: dict | None, pins: dict, jpeg_grey_sha256: str) -> tuple[bool, bool | None]:
108
+ """(passed, same_frames) for the self-test's result: the code's facts and both scanners' reads as pinned, every
109
+ frame's 32 x 32 block means within the tolerance, each patch rebuilding its frame to its hash, and the working
110
+ check's JPEG decoding to the hash test.json pins. same_frames: whether the frames also equal the bytes pinned from
111
+ the Mac; None when no scene came back."""
112
+ got = got or {}
113
+ facts, scenes, want = got.get("facts") or {}, got.get("scenes") or [], pins["scenes"]
114
+ if not scenes or len(scenes) != len(want):
115
+ return False, None
116
+ ok = all(facts.get(k) == pins["facts"][k] for k in ("version", "ec_level", "mask", "raw_sha256"))
117
+ ok = ok and got.get("check_jpeg_grey_sha256") == jpeg_grey_sha256
118
+ same = True
119
+ for g, p in zip(scenes, want, strict=True):
120
+ ok = ok and g["seed"] == p["seed"] and g["reads"] == p["reads"] and g["paste_ok"] is True
121
+ for kind in ("plain", "design"):
122
+ ok = ok and _near(g["block_means_x16_b64"][kind], p["block_means_x16_b64"][kind], pins["tolerance_x16"])
123
+ same = same and g["frame_sha256"][kind] == p["frame_sha256"]["mac"][kind]
124
+ return bool(ok), same
125
+
126
+
127
+ # ---- the setup's checks, in the test worker: T holds its OpenCV, numpy, scene renderer and quality.py -------------
128
+
129
+ def _inverted(T, gray, corners, n: int) -> bool:
130
+ """Light on dark: the three finders' outer rings (24 squares each) sampled lighter than their separators, the light
131
+ strips between each finder and the rest of the code (15 squares each), at the squares' centres through the read's
132
+ corners."""
133
+ np, cv2 = T.np, T.cv2
134
+ ring = [(r0 + i, c0 + j) for r0, c0 in ((0, 0), (0, n - 7), (n - 7, 0)) for i in range(7) for j in range(7)
135
+ if i in (0, 6) or j in (0, 6)]
136
+ e = n - 8
137
+ sep = ([(7, j) for j in range(8)] + [(i, 7) for i in range(7)] + [(7, e + j) for j in range(8)]
138
+ + [(i, e) for i in range(7)] + [(e, j) for j in range(8)] + [(e + 1 + i, 7) for i in range(7)])
139
+ hm = cv2.getPerspectiveTransform(np.float32([[0, 0], [n, 0], [n, n], [0, n]]), np.float32(corners))
140
+
141
+ def mean(cells):
142
+ xy = cv2.perspectiveTransform(np.float32([[[c + 0.5, r + 0.5] for r, c in cells]]), hm)[0]
143
+ x = np.clip(np.floor(xy[:, 0]).astype(int), 0, gray.shape[1] - 1)
144
+ y = np.clip(np.floor(xy[:, 1]).astype(int), 0, gray.shape[0] - 1)
145
+ return float(gray[y, x].mean())
146
+ return mean(ring) > mean(sep)
147
+
148
+
149
+ def _mirrored(T, r, twin, n: int) -> bool:
150
+ """zxing-cpp gives a mirrored code's squares transposed: its two copies of the format information, set by the error
151
+ correction and mask alone (the data squares can differ from the twin's), match the twin's better transposed."""
152
+ np = T.np
153
+ try:
154
+ seen = np.asarray(r.to_image(scale=1, add_quiet_zones=False)) < 128
155
+ except Exception: # noqa: BLE001 - no squares to compare: not found mirrored
156
+ return False
157
+ k = twin.shape[0] // (n + 2 * T.scene.QUIET)
158
+ at = (T.scene.QUIET + np.arange(n)) * k + k // 2
159
+ plain, f = twin[np.ix_(at, at)] < 128, np.zeros((n, n), bool)
160
+ f[8, [*range(6), 7, 8, *range(n - 8, n)]] = f[[*range(6), 7, *range(n - 7, n)], 8] = True
161
+ return seen.shape == (n, n) and int((seen.T != plain)[f].sum()) < int((seen != plain)[f].sum())
162
+
163
+
164
+ def _corners_of(T, r):
165
+ """A zxing-cpp read's corners as the reading takes them (reading_worker._corners)."""
166
+ return _corners(T.quality, "zxing-cpp", lambda y: (y.position, y.extra), r)
167
+
168
+
169
+ def _copy(T, d, r) -> bool:
170
+ """zxing-cpp's read d is a copy of r's code by the reading's own rule (reading_facts.chosen's copies): the same
171
+ content, error correction and mask (with the version: scene.same_code), and, where both have corners as the reading
172
+ takes them (reading_worker._corners), the same squares across and a mean side within COPY_SIDE of r's."""
173
+ return T.scene.same_code(d) == T.scene.same_code(r) and _copy_of(_corners_of(T, d), _corners_of(T, r))
174
+
175
+
176
+ def _own(T, gray, reads: list) -> list:
177
+ """The reads (zxing-cpp's at library defaults) less those it made in its own smaller copies of the picture, by the
178
+ reading's own-size rule (reading_facts.own_reads) for the content of the read nearest the centre: where zxing-cpp
179
+ read that content two or more times, the picture is read again at its own size alone (try_downscale off), and only
180
+ the reads that look repeats exactly (the same bytes and corners) are kept, all of them when none does. zxing-cpp
181
+ reads copies a third the size only while the picture itself gave it fewer than 255 codes, as it can on another
182
+ processor; a read there has coarse corners, and were it the one nearest the centre, the test would take it and the
183
+ twin's scale would follow it."""
184
+ key, content = (0, "zxing-cpp"), bytes(T.scene.nearest(reads, gray.shape).bytes)
185
+ look = SimpleNamespace(second_look=lambda g, how: [(bytes(x.bytes), _corners_of(T, x), None)
186
+ for x in T.scene.qr_reads(g, **how)])
187
+ _, kept = own_reads(look, [(None, None, None, gray)], {key: [bytes(r.bytes) for r in reads]},
188
+ {key: [_corners_of(T, r) for r in reads]}, {key: list(reads)}, lambda ps: content in ps, inf)
189
+ return [r for r in kept[key] if r is not None]
server/design_worker.py ADDED
@@ -0,0 +1,257 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """The design test's worker process (see design.py), on the reading's pattern (reading_worker.py): one spawned process
2
+ at a low priority, with one OpenCV and one BLAS thread, that holds the scene renderer (scene.py), zxing-cpp and
3
+ WeChat's scanner, called through the reading's own decoder functions on frames in memory (none of OpenCV's detectors
4
+ is called). DesignWorker, in the server, starts it, asks it one thing at a time, and kills and replaces it when it goes
5
+ silent or dies.
6
+
7
+ Setup checks a picture in this order and answers ("cant", why) at the first that fails: its size (16 megapixels at
8
+ most), zxing-cpp's facts at full size (a valid QR read with its version, error correction and mask), one code (every
9
+ read a copy of the one nearest the centre, which the test takes, by the reading's own rules, its reads from zxing-cpp's
10
+ smaller copies of the picture left out first: design_rules._own and _copy), the version, the pixels per square, its
11
+ polarity (the three finders' outer rings darker than the light strips round them), the plain twin (made and read back
12
+ the same) and mirroring (zxing-cpp's sampled format squares match the twin's transposed).
13
+ Then each scene renders the twin's and the design's captures with their shared parts, makes both frames
14
+ (design_frames.py) and reads each with both scanners. A read counts when a payload's SHA-256 is one of the code's
15
+ truths: its raw bytes' and its text's in UTF-8. For a page whose browser reads the frames too, a scene also gives both
16
+ frames' hashes, the patch's box and the patch, and the design's capture JPEG, or, in a test with PNG frames, the
17
+ design's frame as PNG. The reads stay in the server; design.py keeps only their counts.
18
+
19
+ Messages, each a tuple whose first item names it; replies come back on the same pipe:
20
+ ("setup", path, mode, rules) -> ("set", facts) | ("cant", why) | ("unreadable",)
21
+ ("scene", s, mode) -> ("scene", s, {"reads", "box", "sha256", "jpeg", "png", "patch"})
22
+ ("selftest", png, jpeg, seeds, rules) -> ("selftest", {"facts", "scenes", "check_jpeg_grey_sha256"})
23
+ ("clear",) -> no reply; the test's arrays are dropped
24
+ mode is "jpeg", "png" or None (no pictures for the page). For acceptance runs only, QSD_DESIGN_SLOW_S makes each scene
25
+ start that many seconds late.
26
+ """
27
+
28
+ from __future__ import annotations
29
+
30
+ import hashlib
31
+ import os
32
+ import threading
33
+ import time
34
+ from multiprocessing import get_context
35
+ from types import SimpleNamespace
36
+
37
+ from .design_rules import _copy, _inverted, _mirrored, _own
38
+ from .reading_rules import VENDOR
39
+ from .reading_worker import _decoders
40
+
41
+ SCANNERS = ("zxing-cpp", "wechat")
42
+ SPAWN_S = 180 # the worker loads its renderer and scanners within this, as the reading's does
43
+ SETUP_S = 30 # opening and checking one picture, within the test's own deadline too
44
+ SCENE_S = 20 # one scene, within the test's own deadline too
45
+ SELFTEST_S = 120 # the start-up self-test: a setup and two scenes
46
+ THREADS = ("OMP_NUM_THREADS", "OPENBLAS_NUM_THREADS", "MKL_NUM_THREADS")
47
+
48
+
49
+ # ---- the worker process: the setup's checks on zxing-cpp's reads are design_rules.py's -----------------------------
50
+
51
+ def _setup(T, src, rules: dict):
52
+ """(reply, the test's state or None) for a picture's path or bytes, checked in the order above."""
53
+ S = T.scene
54
+ try:
55
+ lum, gray = S.open_design(src)
56
+ except T.cv2.error as e: # past OPENCV_IO_MAX_IMAGE_PIXELS OpenCV raises, rather than return None
57
+ return (("cant", "big") if "MAX_IMAGE_PIXELS" in str(e) else ("unreadable",)), None
58
+ except Exception: # noqa: BLE001 - not a picture OpenCV opens
59
+ return ("unreadable",), None
60
+ if gray is None or gray.shape[0] * gray.shape[1] > rules["max_pixels"]:
61
+ return (("cant", "big") if gray is not None else ("unreadable",)), None
62
+ reads = S.qr_reads(gray)
63
+ if not reads:
64
+ return ("cant", "facts"), None
65
+ reads = _own(T, gray, reads)
66
+ r = S.nearest(reads, gray.shape)
67
+ if not all(_copy(T, d, r) for d in reads):
68
+ return ("cant", "several"), None
69
+ f = S.facts_of(r)
70
+ v = f["version"]
71
+ n = 17 + 4 * v
72
+ if v > rules["max_version"]:
73
+ return ("cant", "version"), None
74
+ if S.px_per_square(f["corners"], v) < rules["min_ppm_v1" if v == 1 else "min_ppm"]:
75
+ return ("cant", "small"), None
76
+ if _inverted(T, gray, f["corners"], n):
77
+ return ("cant", "inverted"), None
78
+ design = S.Design(lum, gray, f=f)
79
+ try:
80
+ twin = design.twin()
81
+ except Exception: # noqa: BLE001 - the content doesn't fit such a code, or it reads back differently
82
+ return ("cant", "twin"), None
83
+ if _mirrored(T, r, twin.gray, n):
84
+ return ("cant", "mirrored"), None
85
+ ppm = next(x["ppm"] for x in rules["needs"] if x["version"] >= max(v, 2))
86
+ text = hashlib.sha256((f["text"] or "").encode("utf-8")).hexdigest()
87
+ facts = {"version": v, "ec_level": f["ec"], "mask": f["mask"], "raw_sha256": f["sha256"], "text_sha256": text,
88
+ "ppm": ppm, "n": n}
89
+ return ("set", facts), {"objs": (twin, design), "truths": {f["sha256"], text}, "ppm": ppm,
90
+ "seeds": rules["seeds"]}
91
+
92
+
93
+ def _frames(T, test: dict, seed: int):
94
+ """The twin's and the design's capture JPEGs at a seed, and their frames."""
95
+ got = T.scene.seed_captures(test["objs"], seed, (test["ppm"],))
96
+ jpegs = got[(0, 0)], got[(0, 1)]
97
+ return jpegs, tuple(T.frames.frame_of(j) for j in jpegs)
98
+
99
+
100
+ def _reads(T, frames, truths) -> dict:
101
+ """{scanner: [plain code's frame read, design's frame read]}, each 0 or 1."""
102
+ out = {name: [0, 0] for name in SCANNERS}
103
+ for i, frame in enumerate(frames):
104
+ bgr = T.cv2.cvtColor(frame, T.cv2.COLOR_GRAY2BGR)
105
+ for name in SCANNERS:
106
+ try:
107
+ payloads, _ = T.decoders[name](bgr)
108
+ except Exception: # noqa: BLE001 - a decoder that fails reads nothing, as in the reading
109
+ payloads = []
110
+ out[name][i] = int(any(hashlib.sha256(p).hexdigest() in truths for p in payloads))
111
+ return out
112
+
113
+
114
+ def _scene(T, test: dict, s: int, mode: str | None) -> dict:
115
+ (_, design_jpeg), (plain, design) = _frames(T, test, test["seeds"][s])
116
+ out = {"reads": _reads(T, (plain, design), test["truths"]), "box": None, "sha256": None, "jpeg": None,
117
+ "png": None, "patch": None}
118
+ if mode:
119
+ F = T.frames
120
+ box = F.box_of(plain, design)
121
+ out.update(box=box, sha256={"plain": F.sha256(plain), "design": F.sha256(design)}, patch=F.patch_of(plain, box))
122
+ out["jpeg" if mode == "jpeg" else "png"] = design_jpeg if mode == "jpeg" else F.png_of(design)
123
+ return out
124
+
125
+
126
+ def _selftest(T, png: bytes, jpeg: bytes, seeds, rules: dict) -> dict:
127
+ """The working check's code as a design beside its own twin, set up and rendered as any test is, at fixed seeds."""
128
+ F = T.frames
129
+ reply, test = _setup(T, png, rules)
130
+ out = {"facts": reply[1] if reply[0] == "set" else {"cant": reply[-1]}, "scenes": [],
131
+ "check_jpeg_grey_sha256": None}
132
+ for seed in seeds if test is not None else ():
133
+ _, (plain, design) = _frames(T, test, seed)
134
+ box = F.box_of(plain, design)
135
+ rebuilt = F.paste(design, F.patch_of(plain, box), box)
136
+ out["scenes"].append({
137
+ "seed": seed, "box": box, "reads": _reads(T, (plain, design), test["truths"]),
138
+ "frame_sha256": {"plain": F.sha256(plain), "design": F.sha256(design)},
139
+ "block_means_x16_b64": {"plain": F.block_means(plain), "design": F.block_means(design)},
140
+ "paste_ok": rebuilt is not None and F.sha256(rebuilt) == F.sha256(plain)})
141
+ grey = F.decode_grey(jpeg)
142
+ out["check_jpeg_grey_sha256"] = None if grey is None else hashlib.sha256(grey.tobytes()).hexdigest()
143
+ return out
144
+
145
+
146
+ def _child(conn, wechat_dir: str, nice: int) -> None:
147
+ try:
148
+ os.nice(nice)
149
+ for name in THREADS: # before numpy and OpenCV load, which read them once
150
+ os.environ[name] = "1"
151
+ import cv2
152
+ import numpy as np
153
+
154
+ cv2.setNumThreads(1)
155
+ from . import design_frames, quality, scene
156
+
157
+ _, decoders = _decoders(wechat_dir, quality)
158
+ T = SimpleNamespace(cv2=cv2, np=np, scene=scene, frames=design_frames, decoders=decoders, quality=quality)
159
+ slow = float(os.environ.get("QSD_DESIGN_SLOW_S") or 0)
160
+ except Exception as e: # noqa: BLE001 - reported to the parent, which leaves the test off
161
+ conn.send(("broken", repr(e)))
162
+ return
163
+ conn.send(("ready",))
164
+ test = None
165
+ while True:
166
+ try:
167
+ msg = conn.recv()
168
+ except EOFError:
169
+ return
170
+ try:
171
+ if msg[0] == "setup":
172
+ reply, test = _setup(T, msg[1], msg[3])
173
+ conn.send(reply)
174
+ elif msg[0] == "scene":
175
+ time.sleep(slow)
176
+ conn.send(("scene", msg[1], None if test is None else _scene(T, test, msg[1], msg[2])))
177
+ elif msg[0] == "selftest":
178
+ conn.send(("selftest", _selftest(T, *msg[1:])))
179
+ elif msg[0] == "clear":
180
+ test = None
181
+ except Exception as e: # noqa: BLE001 - the parent counts it as the worker failing
182
+ test = None
183
+ conn.send(("failed", repr(e)))
184
+
185
+
186
+ # ---- in the server --------------------------------------------------------------------------------------------------
187
+
188
+ class DesignWorker:
189
+ """The test worker process. A silent or dead one is killed; replace() starts another."""
190
+
191
+ def __init__(self, nice: int = 10):
192
+ self._ctx, self._nice, self._lock = get_context("spawn"), nice, threading.Lock()
193
+ self._spawn()
194
+
195
+ def _spawn(self, wait: float = SPAWN_S) -> None:
196
+ self.conn, child = self._ctx.Pipe()
197
+ self.proc = self._ctx.Process(target=_child, args=(child, str(VENDOR / "wechat"), self._nice), daemon=True)
198
+ self.proc.start()
199
+ child.close()
200
+ msg = self._recv(wait)
201
+ if msg is None or msg[0] != "ready":
202
+ self._close()
203
+ raise RuntimeError(f"design worker did not start: {msg}")
204
+
205
+ def _close(self) -> None:
206
+ self.proc.kill()
207
+ self.proc.join(5)
208
+ self.conn.close()
209
+
210
+ def _recv(self, timeout: float):
211
+ try:
212
+ if not self.conn.poll(max(0.0, timeout)):
213
+ return None
214
+ return self.conn.recv()
215
+ except (EOFError, OSError):
216
+ return None
217
+
218
+ def alive(self) -> bool:
219
+ return not self.conn.closed and self.proc.is_alive()
220
+
221
+ def close(self) -> None:
222
+ self.proc.kill() # first, without the lock, so that a call waiting on the worker returns at once
223
+ with self._lock:
224
+ self._close()
225
+
226
+ def replace(self) -> None:
227
+ """Kill this worker (if it still runs) and start another; raises RuntimeError when that fails."""
228
+ with self._lock:
229
+ self._close()
230
+ self._spawn()
231
+
232
+ def ask(self, msg: tuple, timeout: float):
233
+ """The worker's reply to msg, or None when it stays silent for timeout seconds or dies: it is then killed, and
234
+ the caller replaces it. One that died while idle is replaced first, within the same timeout."""
235
+ end = time.monotonic() + timeout
236
+ with self._lock:
237
+ if not self.alive():
238
+ self._close()
239
+ self._spawn(min(SPAWN_S, timeout))
240
+ try:
241
+ self.conn.send(msg)
242
+ except OSError:
243
+ self._close()
244
+ return None
245
+ got = self._recv(end - time.monotonic())
246
+ if got is None:
247
+ self._close()
248
+ return got
249
+
250
+ def tell(self, msg: tuple) -> None:
251
+ """Send msg without waiting for a reply (it has none); a dead worker is left for the next ask."""
252
+ with self._lock:
253
+ try:
254
+ if self.alive():
255
+ self.conn.send(msg)
256
+ except OSError:
257
+ pass
server/main.py CHANGED
@@ -10,7 +10,11 @@ POST /api/measure one picture (JPEG, PNG or WebP, at most 25 MB) as the req
10
  "ec_level", "mask", the "squares" it sampled, "raw_sha256", "raw_bytes" and the "copies" of
11
  its code there; see reading_facts.chosen) and the content's digests (content.sha256 and
12
  content.sha256s, for pairing pictures in the page)
13
- GET /api/health what is running
 
 
 
 
14
  Pictures are read from a temporary file that is deleted when the reading ends; nothing is stored, and no access log is
15
  kept.
16
  """
@@ -29,6 +33,7 @@ from starlette.responses import JSONResponse
29
  from starlette.routing import Mount, Route
30
  from starlette.staticfiles import StaticFiles
31
 
 
32
  from .place import place
33
  from .rating import rating
34
  from .reading import FRAMES, SCANNERS, PictureError, Reader
@@ -40,6 +45,12 @@ MAX_WAITING = 16 # pictures waiting beyond those (arriving, or waiting for
40
  SLOT_WAIT_S = 10 # the longest a picture waits for a slot, so that a request ends within a minute
41
  BODY_S = 60 # the longest a picture may take to arrive; a stalled upload doesn't keep its place
42
  BUSY = "Many pictures are being read right now. Try again in a minute."
 
 
 
 
 
 
43
  CSP = "; ".join([
44
  "default-src 'self'", "img-src 'self' blob: data:", "style-src 'self' 'unsafe-inline'", "script-src 'self'",
45
  "connect-src 'self'", "object-src 'none'", "base-uri 'none'", "form-action 'none'",
@@ -99,14 +110,14 @@ async def measure(request: Request) -> JSONResponse:
99
  async with asyncio.timeout(BODY_S):
100
  data = await body(request)
101
  except TimeoutError:
102
- return problem(408, "This picture took too long to arrive. Try again, or send a smaller copy.")
103
  except ClientDisconnect: # the visitor left mid-upload: nobody will read this answer
104
- return problem(400, "The picture did not arrive.")
105
  if data is None:
106
- return problem(413, "This picture is over 25 MB.")
107
  suffix = sniff(data)
108
  if suffix is None:
109
- return problem(415, "Send a JPEG, PNG or WebP picture.")
110
  try:
111
  await asyncio.wait_for(STATE["gate"].acquire(), SLOT_WAIT_S)
112
  except TimeoutError:
@@ -127,6 +138,7 @@ async def health(request: Request) -> JSONResponse:
127
  "ok": True, "scanners": SCANNERS, "frames": FRAMES, "slots": SLOTS,
128
  "versions": {"opencv-contrib": version("opencv-contrib-python-headless"), "zxing-cpp": version("zxing-cpp"),
129
  "zxing": "3.5.2"},
 
130
  }, headers={"Cache-Control": "no-store"})
131
 
132
 
@@ -135,7 +147,9 @@ async def lifespan(app):
135
  STATE["reader"] = await run_in_threadpool(Reader, SLOTS)
136
  STATE["gate"] = asyncio.Semaphore(SLOTS)
137
  STATE["busy"] = 0
 
138
  yield
 
139
  STATE["reader"].close()
140
 
141
 
@@ -168,5 +182,7 @@ class SecurityHeaders:
168
  app = SecurityHeaders(Starlette(lifespan=lifespan, routes=[
169
  Route("/api/measure", measure, methods=["POST"]),
170
  Route("/api/health", health, methods=["GET"]),
 
 
171
  Mount("/", StaticFiles(directory=WEB, html=True), name="web"),
172
  ]))
 
10
  "ec_level", "mask", the "squares" it sampled, "raw_sha256", "raw_bytes" and the "copies" of
11
  its code there; see reading_facts.chosen) and the content's digests (content.sha256 and
12
  content.sha256s, for pairing pictures in the page)
13
+ GET /api/health what is running, and whether the design test is on ("design": on, busy, same_frames)
14
+ POST /api/design/slot the design test (design.py), one at a time across all visitors, in a worker of its own: a slot
15
+ for this page, then POST /api/design?slot= with the picture marked as the original graphic,
16
+ GET /api/design/<test> for its answer (and its scenes and frames when the page reads them
17
+ too), and DELETE /api/design/<id> when the page leaves it
18
  Pictures are read from a temporary file that is deleted when the reading ends; nothing is stored, and no access log is
19
  kept.
20
  """
 
33
  from starlette.routing import Mount, Route
34
  from starlette.staticfiles import StaticFiles
35
 
36
+ from .design import design_health, design_routes, start_design, stop_design
37
  from .place import place
38
  from .rating import rating
39
  from .reading import FRAMES, SCANNERS, PictureError, Reader
 
45
  SLOT_WAIT_S = 10 # the longest a picture waits for a slot, so that a request ends within a minute
46
  BODY_S = 60 # the longest a picture may take to arrive; a stalled upload doesn't keep its place
47
  BUSY = "Many pictures are being read right now. Try again in a minute."
48
+ UPLOAD = { # the answers to a picture that didn't arrive whole, for /api/measure and the design test alike
49
+ 400: "The picture did not arrive.",
50
+ 408: "This picture took too long to arrive. Try again, or send a smaller copy.",
51
+ 413: "This picture is over 25 MB.",
52
+ 415: "Send a JPEG, PNG or WebP picture.",
53
+ }
54
  CSP = "; ".join([
55
  "default-src 'self'", "img-src 'self' blob: data:", "style-src 'self' 'unsafe-inline'", "script-src 'self'",
56
  "connect-src 'self'", "object-src 'none'", "base-uri 'none'", "form-action 'none'",
 
110
  async with asyncio.timeout(BODY_S):
111
  data = await body(request)
112
  except TimeoutError:
113
+ return problem(408, UPLOAD[408])
114
  except ClientDisconnect: # the visitor left mid-upload: nobody will read this answer
115
+ return problem(400, UPLOAD[400])
116
  if data is None:
117
+ return problem(413, UPLOAD[413])
118
  suffix = sniff(data)
119
  if suffix is None:
120
+ return problem(415, UPLOAD[415])
121
  try:
122
  await asyncio.wait_for(STATE["gate"].acquire(), SLOT_WAIT_S)
123
  except TimeoutError:
 
138
  "ok": True, "scanners": SCANNERS, "frames": FRAMES, "slots": SLOTS,
139
  "versions": {"opencv-contrib": version("opencv-contrib-python-headless"), "zxing-cpp": version("zxing-cpp"),
140
  "zxing": "3.5.2"},
141
+ "design": design_health(),
142
  }, headers={"Cache-Control": "no-store"})
143
 
144
 
 
147
  STATE["reader"] = await run_in_threadpool(Reader, SLOTS)
148
  STATE["gate"] = asyncio.Semaphore(SLOTS)
149
  STATE["busy"] = 0
150
+ await run_in_threadpool(start_design, WEB / "data") # never raises: on a failure the design test is off
151
  yield
152
+ stop_design()
153
  STATE["reader"].close()
154
 
155
 
 
182
  app = SecurityHeaders(Starlette(lifespan=lifespan, routes=[
183
  Route("/api/measure", measure, methods=["POST"]),
184
  Route("/api/health", health, methods=["GET"]),
185
+ *design_routes({"body": body, "sniff": sniff, "problem": problem, "body_s": BODY_S, "messages": UPLOAD,
186
+ "readings_waiting": lambda: STATE["busy"] > SLOTS}),
187
  Mount("/", StaticFiles(directory=WEB, html=True), name="web"),
188
  ]))
server/reading.py CHANGED
@@ -28,6 +28,10 @@ and mask ("ec_level", "mask"), the squares it sampled ("squares"), its raw bytes
28
  "raw_bytes") and how many copies of that code it read there ("copies"); all None when it never read the content with
29
  a UEC. It also gives the content's SHA-256 ("content": {"sha256"}), as one content's raw bytes, and the SHA-256s of
30
  every set of raw bytes it stands for ("sha256s"; reading_content.py), which the page pairs pictures of one code by.
 
 
 
 
31
 
32
  The reader is here (Reader, and _checks for the picture-quality checks); the settings are in reading_rules.py, the
33
  decoder worker in reading_worker.py, ZXing's Java process in reading_java.py, the content in reading_content.py and
@@ -45,7 +49,7 @@ import time
45
  from multiprocessing import get_context
46
 
47
  from .reading_content import _consensus, _same, describe
48
- from .reading_facts import NO_FACTS, chosen
49
  from .reading_java import _Java
50
  from .reading_rules import DEADLINE_S, FRAMES, POOL, SCANNERS, SELF_TEST, VENDOR, PictureError, frame_sizes
51
  from .reading_worker import _decoders, _open, _TooBig, _Worker # noqa: F401 - _decoders, _open and _TooBig: for tools
@@ -136,6 +140,8 @@ class Reader:
136
  else:
137
  content = None
138
  def hit(ps): return any(hashlib.sha256(p).hexdigest() in truth for p in ps) # noqa: E306
 
 
139
  checks = None if partial else _checks(worker, frames, w, pool, geo, hit, deadline)
140
  code = dict(NO_FACTS) if partial else chosen(worker, frames, pool, geo, facts, hit, deadline)
141
  finally:
 
28
  "raw_bytes") and how many copies of that code it read there ("copies"); all None when it never read the content with
29
  a UEC. It also gives the content's SHA-256 ("content": {"sha256"}), as one content's raw bytes, and the SHA-256s of
30
  every set of raw bytes it stands for ("sha256s"; reading_content.py), which the page pairs pictures of one code by.
31
+ Where zxing-cpp read the content two or more times in the first frame it read it in, the checks and these facts leave
32
+ out its reads from its own smaller copies of that frame (reading_facts.own_reads): it reads those copies only while
33
+ the frame itself gave it fewer than 255 codes, and their reads have coarse corners and squares sampled at a third of
34
+ the frame's resolution or less.
35
 
36
  The reader is here (Reader, and _checks for the picture-quality checks); the settings are in reading_rules.py, the
37
  decoder worker in reading_worker.py, ZXing's Java process in reading_java.py, the content in reading_content.py and
 
49
  from multiprocessing import get_context
50
 
51
  from .reading_content import _consensus, _same, describe
52
+ from .reading_facts import NO_FACTS, chosen, own_reads
53
  from .reading_java import _Java
54
  from .reading_rules import DEADLINE_S, FRAMES, POOL, SCANNERS, SELF_TEST, VENDOR, PictureError, frame_sizes
55
  from .reading_worker import _decoders, _open, _TooBig, _Worker # noqa: F401 - _decoders, _open and _TooBig: for tools
 
140
  else:
141
  content = None
142
  def hit(ps): return any(hashlib.sha256(p).hexdigest() in truth for p in ps) # noqa: E306
143
+ if not partial: # the checks and the facts leave out zxing-cpp's reads from its smaller copies
144
+ geo, facts = own_reads(worker, frames, pool, geo, facts, hit, deadline)
145
  checks = None if partial else _checks(worker, frames, w, pool, geo, hit, deadline)
146
  code = dict(NO_FACTS) if partial else chosen(worker, frames, pool, geo, facts, hit, deadline)
147
  finally:
server/reading_facts.py CHANGED
@@ -1,9 +1,10 @@
1
  """zxing-cpp's facts about a picture's code (see reading.py): the error correction the code uses and how much of it its
2
  reads left unused, its mask, its squares as zxing-cpp sampled them, its raw bytes and how many copies of it it read.
3
  The decoder worker takes them from each of zxing-cpp's reads as plain data (read_facts) and reads a frame again for
4
- the second look (second_look); the reader picks the one read the facts come from, takes the unused error correction
5
- as the lowest over its reads of the content in that read's frame, and counts the copies of that read's code there
6
- (chosen).
 
7
  """
8
 
9
  from __future__ import annotations
@@ -26,6 +27,9 @@ SECOND_LOOK_BELOW = 3.0 # pixels per square in the frame read, by a read's own
26
  # styled codes the default binarizer's figure tracks phones' misses far better than the
27
  # global threshold's, which reads squares that art flips higher
28
  COPY_SIDE = 0.1 # a copy's mean side lies within this share of the chosen read's (copies)
 
 
 
29
 
30
 
31
  # ---- in the decoder worker ------------------------------------------------------------------------------------------
@@ -67,13 +71,14 @@ def pack(img) -> str | None:
67
  return base64.b64encode(np.packbits(a < 128)).decode("ascii")
68
 
69
 
70
- def second_look(cv2, zxing_cpp, path: str) -> list | None:
71
  """[(content, corners, facts)] for zxing-cpp's reads of the staged frame at path with its GlobalHistogram
72
- binarizer (see chosen), as plain data; None when that fails. Diagnostics only, as the checks."""
 
73
  try:
74
  facts: list = []
75
  payloads, corners = zxing_cpp(cv2.cvtColor(cv2.imread(path, cv2.IMREAD_GRAYSCALE), cv2.COLOR_GRAY2BGR), facts,
76
- "GlobalHistogram")
77
  return list(zip(payloads, corners, facts, strict=True))
78
  except Exception: # noqa: BLE001 - diagnostics only: a failure leaves the first look's facts
79
  return None
@@ -110,6 +115,39 @@ def _copy_of(d, c) -> bool:
110
  return d is None or c is None or (d[1] == c[1] and abs(_side(d) - _side(c)) <= COPY_SIDE * _side(c))
111
 
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  def chosen(worker, frames: list, pool: dict, geo: dict, facts: dict, hit, deadline: float) -> dict:
114
  """zxing-cpp's facts about the content, on the largest frame where it read the content with a UEC (frames run
115
  largest first): "uec_frame" is that frame's largest size. Each of its reads of the content with a UEC there has a
 
1
  """zxing-cpp's facts about a picture's code (see reading.py): the error correction the code uses and how much of it its
2
  reads left unused, its mask, its squares as zxing-cpp sampled them, its raw bytes and how many copies of it it read.
3
  The decoder worker takes them from each of zxing-cpp's reads as plain data (read_facts) and reads a frame again for
4
+ the second look and the own-size look (second_look); the reader leaves out the reads zxing-cpp made in its smaller
5
+ copies of a frame where it read the content more than once there (own_reads), picks the one read the facts come from,
6
+ takes the unused error correction as the lowest over its reads of the content in that read's frame, and counts the
7
+ copies of that read's code there (chosen).
8
  """
9
 
10
  from __future__ import annotations
 
27
  # styled codes the default binarizer's figure tracks phones' misses far better than the
28
  # global threshold's, which reads squares that art flips higher
29
  COPY_SIDE = 0.1 # a copy's mean side lies within this share of the chosen read's (copies)
30
+ GLOBAL = {"binarizer": "GlobalHistogram"} # the second look's option (chosen)
31
+ OWN_SIZE = {"try_downscale": False} # the own-size look's (own_reads): the frame alone, none of zxing-cpp's smaller
32
+ # copies of it; its other options are the reading's
33
 
34
 
35
  # ---- in the decoder worker ------------------------------------------------------------------------------------------
 
71
  return base64.b64encode(np.packbits(a < 128)).decode("ascii")
72
 
73
 
74
+ def second_look(cv2, zxing_cpp, path: str, how: dict | None = None) -> list | None:
75
  """[(content, corners, facts)] for zxing-cpp's reads of the staged frame at path with its GlobalHistogram
76
+ binarizer (see chosen), or with the options how (OWN_SIZE: see own_reads), as plain data; None when that fails.
77
+ Diagnostics only, as the checks."""
78
  try:
79
  facts: list = []
80
  payloads, corners = zxing_cpp(cv2.cvtColor(cv2.imread(path, cv2.IMREAD_GRAYSCALE), cv2.COLOR_GRAY2BGR), facts,
81
+ how or GLOBAL)
82
  return list(zip(payloads, corners, facts, strict=True))
83
  except Exception: # noqa: BLE001 - diagnostics only: a failure leaves the first look's facts
84
  return None
 
115
  return d is None or c is None or (d[1] == c[1] and abs(_side(d) - _side(c)) <= COPY_SIDE * _side(c))
116
 
117
 
118
+ def own_reads(worker, frames: list, pool: dict, geo: dict, facts: dict, hit, deadline: float) -> tuple[dict, dict]:
119
+ """The corners and facts the checks and chosen take: as read, less zxing-cpp's reads from its smaller copies of a
120
+ frame. zxing-cpp reads a frame at its own size and then, only while that gave it fewer than 255 codes, copies of it
121
+ a third the size, and a third of that, while over 500 pixels: it keeps a read there, after the frame's own reads and
122
+ with its corners scaled back, unless its centre lies in an earlier read of the same content. Such a read's squares
123
+ were sampled at a third of the frame's resolution or less, so its UEC and squares aren't the code's at this size,
124
+ and its corners are coarse. So, where zxing-cpp read the content two or more times in the first frame it read it
125
+ in, it reads that frame again, once, at its own size alone (OWN_SIZE; started before the deadline only), and each
126
+ read of the content there that this look doesn't repeat exactly (the same bytes and corners) loses its corners and
127
+ facts, unless it repeats none. The payloads stay, so what each scanner read never changes; and as a read whose
128
+ centre lies in an earlier read of the content is dropped, a picture of one code is not looked at again.
129
+ Diagnostics only: on a failure the decodes stand as read."""
130
+ try:
131
+ k = next((k for k in range(len(frames)) if any(hit([p]) for p in pool.get((k, "zxing-cpp"), []))), None)
132
+ key = (k, "zxing-cpp")
133
+ mine = [i for i, p in enumerate(pool.get(key, [])) if hit([p])]
134
+ if len(mine) < 2 or time.monotonic() >= deadline:
135
+ return geo, facts
136
+ seen = [(q, d) for q, d, _ in worker.second_look(frames[k][3], OWN_SIZE) or []]
137
+ gone = set()
138
+ for i in mine:
139
+ if (pool[key][i], geo[key][i]) in seen:
140
+ seen.remove((pool[key][i], geo[key][i]))
141
+ else:
142
+ gone.add(i)
143
+ if gone and len(gone) < len(mine):
144
+ geo = {**geo, key: [None if i in gone else c for i, c in enumerate(geo[key])]}
145
+ facts = {**facts, key: [None if i in gone else f for i, f in enumerate(facts[key])]}
146
+ except Exception: # noqa: BLE001 - diagnostics never cost the reading
147
+ pass
148
+ return geo, facts
149
+
150
+
151
  def chosen(worker, frames: list, pool: dict, geo: dict, facts: dict, hit, deadline: float) -> dict:
152
  """zxing-cpp's facts about the content, on the largest frame where it read the content with a UEC (frames run
153
  largest first): "uec_frame" is that frame's largest size. Each of its reads of the content with a UEC there has a
server/reading_worker.py CHANGED
@@ -45,9 +45,9 @@ def _corners(quality, scanner: str, take, *args):
45
  def _decoders(wechat_dir: str, quality):
46
  """Per scanner, a function from a frame to (contents, corners): one corners entry per content, what
47
  quality.corners_from makes of the scanner's own result for that code, or None. The contents are as measured.
48
- zxing-cpp's also takes a list, which it fills with each content's facts (reading_facts.read_facts), and the name
49
- of another of its binarizers, for the second look only (reading_facts.second_look): the reading uses the library's
50
- default."""
51
  import cv2
52
  import zxingcpp
53
 
@@ -58,9 +58,9 @@ def _decoders(wechat_dir: str, quality):
58
  qr_format = zxingcpp.BarcodeFormat.QRCode # in zxing-cpp 3 this also returns Micro QR and rMQR, dropped below
59
  dropped = (zxingcpp.BarcodeFormat.MicroQRCode, zxingcpp.BarcodeFormat.RMQRCode)
60
 
61
- def zxing_cpp(bgr, facts=None, binarizer=None):
62
  out, geo = [], []
63
- how = {} if binarizer is None else {"binarizer": getattr(zxingcpp.Binarizer, binarizer)}
64
  for r in zxingcpp.read_barcodes(cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY), formats=qr_format, **how):
65
  if r.format in dropped or not getattr(r, "valid", True):
66
  continue
@@ -173,7 +173,7 @@ def _child(conn, wechat_dir: str) -> None:
173
  elif msg[0] == "measure":
174
  conn.send(("measured", quality.measure_staged(*msg[1:])))
175
  elif msg[0] == "look":
176
- conn.send(("look", second_look(cv2, decoders["zxing-cpp"], msg[1])))
177
 
178
 
179
  class _Worker:
@@ -277,9 +277,9 @@ class _Worker:
277
  """The picture-quality checks, by quality.measure_staged in this worker (see _ask)."""
278
  return self._ask(("measure", path, found, sizes, scale), "measured")
279
 
280
- def second_look(self, path: str) -> list | None:
281
- """zxing-cpp's second look at a staged frame, by reading_facts.second_look in this worker (see _ask)."""
282
- return self._ask(("look", path), "look")
283
 
284
  def _ask(self, msg: tuple, reply: str):
285
  """A diagnostic's answer from this worker; None when it isn't running (diagnostics never start one), or when
 
45
  def _decoders(wechat_dir: str, quality):
46
  """Per scanner, a function from a frame to (contents, corners): one corners entry per content, what
47
  quality.corners_from makes of the scanner's own result for that code, or None. The contents are as measured.
48
+ zxing-cpp's also takes a list, which it fills with each content's facts (reading_facts.read_facts), and options
49
+ for the looks only (reading_facts.second_look: another of its binarizers, by name, or the frame's own size alone):
50
+ the reading uses the library's defaults."""
51
  import cv2
52
  import zxingcpp
53
 
 
58
  qr_format = zxingcpp.BarcodeFormat.QRCode # in zxing-cpp 3 this also returns Micro QR and rMQR, dropped below
59
  dropped = (zxingcpp.BarcodeFormat.MicroQRCode, zxingcpp.BarcodeFormat.RMQRCode)
60
 
61
+ def zxing_cpp(bgr, facts=None, how=None):
62
  out, geo = [], []
63
+ how = {k: getattr(zxingcpp.Binarizer, v) if k == "binarizer" else v for k, v in (how or {}).items()}
64
  for r in zxingcpp.read_barcodes(cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY), formats=qr_format, **how):
65
  if r.format in dropped or not getattr(r, "valid", True):
66
  continue
 
173
  elif msg[0] == "measure":
174
  conn.send(("measured", quality.measure_staged(*msg[1:])))
175
  elif msg[0] == "look":
176
+ conn.send(("look", second_look(cv2, decoders["zxing-cpp"], *msg[1:])))
177
 
178
 
179
  class _Worker:
 
277
  """The picture-quality checks, by quality.measure_staged in this worker (see _ask)."""
278
  return self._ask(("measure", path, found, sizes, scale), "measured")
279
 
280
+ def second_look(self, path: str, how: dict | None = None) -> list | None:
281
+ """zxing-cpp's second look at a staged frame, or its look with how, by reading_facts.second_look (see _ask)."""
282
+ return self._ask(("look", path, how), "look")
283
 
284
  def _ask(self, msg: tuple, reply: str):
285
  """A diagnostic's answer from this worker; None when it isn't running (diagnostics never start one), or when
server/scene.py ADDED
@@ -0,0 +1,290 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """The design test's scene: a design file printed and photographed in the size table's everyday scene, beside its
2
+ plain twin (the same content, version, error correction and mask, with a 4-square margin).
3
+
4
+ Each capture is a 1920 x 1440 grey JPEG of the printed sheet in a scene; design_frames.py shrinks it to the 1280 x 960
5
+ frame the scanners read. Its code gets `ppm` camera pixels per square in the 1280 frame, head-on at the code's centre,
6
+ before the 15-degree tilt. A seed is one scene for a design and its twin at every size, and seed_captures renders what
7
+ the two share at a seed once, with the same bytes as rendering each on its own.
8
+
9
+ The everyday scene: tilt 15 degrees, focus blur 0.7 px, a 2 px shake streak, ink 47 on paper 202 (61% contrast),
10
+ uneven light 0.15, noise 4 and JPEG quality 82, with pixel figures quoted at the 1280 frame.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ import hashlib
16
+ import math
17
+ import random
18
+
19
+ import cv2
20
+ import numpy as np
21
+ import zxingcpp
22
+
23
+ FRAME_W, FRAME_H = 1920, 1440 # the capture
24
+ K = FRAME_W / 1280 # pixels per square and blur are quoted at the 1280 frame
25
+ TEX = 12 # sheet texels per square
26
+ FOCAL = FRAME_W * 0.8 # pinhole focal length in pixels (about 64 degrees across)
27
+ MARGIN = 6 # squares of sheet shown round the code; anything of the design further out is cut
28
+ QUIET = 4 # the twin's margin, in squares
29
+ SUPER = 2 # the file is sampled on a grid this much finer than the sheet, then averaged
30
+ SNAP = 1e-4 # coverage this close to 0 or 1 is 0 or 1
31
+ SCENE = {"tilt": 15.0, "blur": 0.7, "motion": 2.0, "ink": 47.0, "paper": 202.0, "gradient": 0.15, "noise": 4.0,
32
+ "jpeg": 82}
33
+ SEEDS = "grid:everyday" # the size table's own scene streams, so a seed is one of its measured scenes
34
+ QR = zxingcpp.BarcodeFormat.QRCode
35
+ DROPPED = (zxingcpp.BarcodeFormat.MicroQRCode, zxingcpp.BarcodeFormat.RMQRCode)
36
+ W, H = FRAME_W, FRAME_H
37
+ YY, XX = np.mgrid[0:H, 0:W].astype(np.float32) # made once per process
38
+
39
+
40
+ # ---- the design file ---------------------------------------------------------------------------------------------
41
+
42
+ def open_design(src):
43
+ """(luma float32 0..255, grey uint8) of a file, by its path or its bytes: BT.601 luma on the sRGB values, any
44
+ transparent part on white. Both reads take the pixels as stored: IMREAD_UNCHANGED ignores EXIF orientation, and
45
+ the grey read is told to, so the luma and the corners describe the same pixels."""
46
+ def read(flags):
47
+ if isinstance(src, bytes | bytearray):
48
+ return cv2.imdecode(np.frombuffer(src, np.uint8), flags)
49
+ return cv2.imread(str(src), flags)
50
+
51
+ im = read(cv2.IMREAD_UNCHANGED)
52
+ if im is None:
53
+ raise ValueError("cannot open the picture")
54
+ top = float(np.iinfo(im.dtype).max)
55
+ x = im.astype(np.float32) * np.float32(255.0 / top)
56
+ if x.ndim == 2:
57
+ lum = x
58
+ else:
59
+ bgr = x[..., :3] if x.shape[2] >= 3 else np.repeat(x[..., :1], 3, axis=2)
60
+ if x.shape[2] == 4:
61
+ a = x[..., 3:] / np.float32(255.0)
62
+ bgr = bgr * a + np.float32(255.0) * (1 - a)
63
+ lum = np.float32(0.114) * bgr[..., 0] + np.float32(0.587) * bgr[..., 1] + np.float32(0.299) * bgr[..., 2]
64
+ gray = read(cv2.IMREAD_GRAYSCALE | cv2.IMREAD_IGNORE_ORIENTATION)
65
+ if im.ndim == 3 and im.shape[2] == 4 and im[..., 3].min() < top:
66
+ gray = np.rint(lum).astype(np.uint8)
67
+ return lum.astype(np.float32), gray
68
+
69
+
70
+ def facts_of(r):
71
+ x, pos, raw = (r.extra or {}), r.position, bytes(r.bytes)
72
+ return {"text": r.text, "bytes": raw, "binary": r.content_type == zxingcpp.ContentType.Binary,
73
+ "version": int(x["Version"]), "ec": str(x["ECLevel"]), "mask": int(x["DataMask"]), "uec": x.get("UEC"),
74
+ "sha256": hashlib.sha256(raw).hexdigest(),
75
+ "corners": [[float(q.x), float(q.y)] for q in (pos.top_left, pos.top_right, pos.bottom_right,
76
+ pos.bottom_left)]}
77
+
78
+
79
+ def qr_reads(gray, **how):
80
+ """zxing-cpp's reads at library defaults (QR only), or with the options how (design_rules._own: try_downscale
81
+ off): valid, and each with its version, error correction and mask."""
82
+ rs = [r for r in zxingcpp.read_barcodes(gray, formats=QR, **how) if r.format not in DROPPED and r.valid]
83
+ return [r for r in rs if (r.extra or {}).get("DataMask") is not None and (r.extra or {}).get("ECLevel")
84
+ and (r.extra or {}).get("Version")]
85
+
86
+
87
+ def same_code(r):
88
+ """What copies of one code share: the content, version, error correction and mask."""
89
+ x = r.extra
90
+ return bytes(r.bytes), int(x["Version"]), str(x["ECLevel"]), int(x["DataMask"])
91
+
92
+
93
+ def nearest(rs, shape):
94
+ """Of several reads, the one nearest the picture's centre."""
95
+ h, w = shape[:2]
96
+
97
+ def off(r):
98
+ p = r.position
99
+ c = np.mean([[q.x, q.y] for q in (p.top_left, p.top_right, p.bottom_right, p.bottom_left)], axis=0)
100
+ return float(np.hypot(c[0] - w / 2, c[1] - h / 2))
101
+ return min(rs, key=off)
102
+
103
+
104
+ def facts(gray):
105
+ """zxing-cpp's read at library defaults (QR only): content, version, error correction, mask and corners (pixel
106
+ edges). One code, or copies of one content (the copy nearest the picture's centre); None otherwise, or when the
107
+ error correction or mask is unknown."""
108
+ rs = qr_reads(gray)
109
+ if not rs or len({bytes(r.bytes) for r in rs}) != 1:
110
+ return None
111
+ return facts_of(nearest(rs, gray.shape))
112
+
113
+
114
+ def px_per_square(corners, version):
115
+ c = np.float32(corners)
116
+ return float(np.mean([np.linalg.norm(c[i] - c[(i + 1) % 4]) for i in range(4)])) / (17 + 4 * version)
117
+
118
+
119
+ def twin_image(f, scale):
120
+ """The plain twin at `scale` px per square with a 4-square margin, from the text (the bytes for binary content);
121
+ raises if it can't be made or reads back differently."""
122
+ content = f["bytes"] if f["binary"] else f["text"]
123
+ b = zxingcpp.create_barcode(content, QR, ec_level=f["ec"], version=f["version"], data_mask=f["mask"])
124
+ img = np.array(zxingcpp.write_barcode_to_image(b, scale=int(scale), add_quiet_zones=True))
125
+ back = facts(img)
126
+ if back is None or any(back[k] != f[k] for k in ("version", "ec", "mask", "bytes")):
127
+ raise ValueError("the twin reads back differently")
128
+ return img
129
+
130
+
131
+ # ---- onto the printed sheet --------------------------------------------------------------------------------------
132
+
133
+ def _shift(d):
134
+ return np.array([[1, 0, d], [0, 1, d], [0, 0, 1]], np.float64)
135
+
136
+
137
+ def place(L, corners, n, beyond="scene"):
138
+ """The design's luma (0..1) and coverage on the sheet: t x t texels, TEX per square, the code's corners MARGIN
139
+ squares from each edge. Each file pixel prints as a solid square, sampled by area. beyond="paper" puts blank paper
140
+ beyond the file's edge (a check anchor); "scene" lets the scene show there."""
141
+ t = (n + 2 * MARGIN) * TEX
142
+ c = np.float32(corners)
143
+ k = px_per_square(corners, (n - 17) // 4)
144
+ if k > TEX:
145
+ w, h = max(1, round(L.shape[1] * TEX / k)), max(1, round(L.shape[0] * TEX / k))
146
+ sx, sy = w / L.shape[1], h / L.shape[0]
147
+ L = cv2.resize(L, (w, h), interpolation=cv2.INTER_AREA)
148
+ c = c * np.float32([sx, sy])
149
+ cover = np.ones(L.shape, np.float32)
150
+ u0, u1 = MARGIN * TEX * SUPER, (MARGIN + n) * TEX * SUPER
151
+ edge = cv2.getPerspectiveTransform(c, np.float32([[u0, u0], [u1, u0], [u1, u1], [u0, u1]]))
152
+ hc = _shift(-0.5) @ edge @ _shift(0.5)
153
+ ts = t * SUPER
154
+ Lt = cv2.warpPerspective(L, hc, (ts, ts), flags=cv2.INTER_NEAREST, borderMode=cv2.BORDER_REPLICATE)
155
+ cov = cv2.warpPerspective(cover, hc, (ts, ts), flags=cv2.INTER_NEAREST, borderMode=cv2.BORDER_CONSTANT,
156
+ borderValue=0)
157
+ if SUPER > 1:
158
+ Lt = cv2.resize(Lt, (t, t), interpolation=cv2.INTER_AREA)
159
+ cov = cv2.resize(cov, (t, t), interpolation=cv2.INTER_AREA)
160
+ cov[cov > 1 - SNAP] = 1
161
+ cov[cov < SNAP] = 0
162
+ Lt = np.clip(Lt, 0, 1).astype(np.float32)
163
+ if beyond == "paper":
164
+ return Lt * cov + (1 - cov), np.ones_like(cov)
165
+ return Lt, cov
166
+
167
+
168
+ class Design:
169
+ """A design (or twin) with its facts, placed once on the sheet."""
170
+
171
+ def __init__(self, lum, gray, f=None, beyond="scene"):
172
+ self.lum, self.gray, self.beyond = lum.astype(np.float32), gray, beyond
173
+ self.facts = f if f is not None else facts(gray)
174
+ self._placed = None
175
+
176
+ @classmethod
177
+ def open(cls, path, beyond="scene"):
178
+ lum, gray = open_design(path)
179
+ return cls(lum, gray, beyond=beyond)
180
+
181
+ def twin(self, scale=None):
182
+ f = self.facts
183
+ scale = scale or max(1, round(px_per_square(f["corners"], f["version"])))
184
+ img = twin_image(f, scale)
185
+ return Design(img.astype(np.float32), img, beyond=self.beyond)
186
+
187
+ @property
188
+ def n(self):
189
+ return 17 + 4 * self.facts["version"]
190
+
191
+ def placed(self):
192
+ if self._placed is None:
193
+ self._placed = place(self.lum / np.float32(255.0), self.facts["corners"], self.n, self.beyond)
194
+ return self._placed
195
+
196
+
197
+ # ---- the scene ---------------------------------------------------------------------------------------------------
198
+
199
+ def smooth_field(shape, rng, scale, amp):
200
+ """A low-frequency random field, mean 0."""
201
+ h, w = shape
202
+ small = np.array([[rng.gauss(0, 1) for _ in range(max(2, w // scale))] for _ in range(max(2, h // scale))],
203
+ dtype=np.float32)
204
+ return cv2.resize(small, (w, h), interpolation=cv2.INTER_CUBIC) * amp
205
+
206
+
207
+ def project(points, tilt_deg, axis_deg, psi_deg, center):
208
+ """Turn plane points by psi, tilt them about an in-plane axis, and project them through a pinhole."""
209
+ psi, phi, th = map(math.radians, (psi_deg, axis_deg, tilt_deg))
210
+ c, s = math.cos(psi), math.sin(psi)
211
+ pts = np.array([[c * x - s * y, s * x + c * y, 0.0] for x, y in points])
212
+ ax = np.array([math.cos(phi), math.sin(phi), 0.0])
213
+ k = np.array([[0, -ax[2], ax[1]], [ax[2], 0, -ax[0]], [-ax[1], ax[0], 0]])
214
+ rot = np.eye(3) + math.sin(th) * k + (1 - math.cos(th)) * (k @ k)
215
+ p3 = pts @ rot.T
216
+ z = FOCAL + p3[:, 2]
217
+ return np.stack([FOCAL * p3[:, 0] / z + center[0], FOCAL * p3[:, 1] / z + center[1]], 1).astype(np.float32)
218
+
219
+
220
+ def streams(version, seed):
221
+ """The scene's random streams for a version and seed: the turn and tilt axis, then the scene, the sheet's texture
222
+ and the shake, each from its own string seed."""
223
+ name = f"{SEEDS}:{int(version)}:{int(seed)}"
224
+ rng = random.Random(name)
225
+ psi, axis = rng.uniform(0, 360), rng.uniform(0, 180)
226
+ return rng, psi, axis, random.Random(f"{name}:texture"), random.Random(f"{name}:motion")
227
+
228
+
229
+ def seed_captures(objs, seed, steps):
230
+ """{(step index, object index): JPEG bytes} for objects of one version (a design and its twin) at one seed. What
231
+ they share at the seed (the background, the sheet's paper, the shake and the noise) and at a step (the uneven
232
+ light) is computed once."""
233
+ n = objs[0].n
234
+ rng, psi, axis, trng, mrng = streams(objs[0].facts["version"], seed)
235
+ t = objs[0].placed()[0].shape[0]
236
+ paper = np.full((t, t), float(SCENE["paper"]), np.float32)
237
+ paper += smooth_field((t, t), trng, 96, 4.0) + np.random.default_rng(trng.randrange(1 << 30)).normal(
238
+ 0, 2.0, (t, t))
239
+ u1, u2 = rng.uniform(-0.5, 0.5), rng.uniform(-0.5, 0.5)
240
+ back = rng.uniform(85, 135) + smooth_field((H, W), rng, 240, 14.0)
241
+ for _ in range(rng.randrange(2, 6)): # a few soft-edged objects
242
+ x0, y0 = rng.uniform(0, W), rng.uniform(0, H)
243
+ cv2.rectangle(back, (int(x0), int(y0)), (int(x0 + rng.uniform(100, 600)), int(y0 + rng.uniform(80, 500))),
244
+ rng.uniform(60, 180), -1)
245
+ back = cv2.GaussianBlur(back, (0, 0), 3)
246
+ beta = rng.uniform(0, 2 * math.pi) # uneven light
247
+ length = max(1, int(round(SCENE["motion"] * K))) # shake
248
+ kern = np.zeros((length, length), np.float32)
249
+ kern[length // 2, :] = 1.0 / length
250
+ rot = cv2.getRotationMatrix2D((length / 2 - 0.5, length / 2 - 0.5), mrng.uniform(0, 180), 1.0)
251
+ kern = cv2.warpAffine(kern, rot, (length, length))
252
+ kern = kern / max(kern.sum(), 1e-6)
253
+ nrng = np.random.default_rng(rng.randrange(1 << 30))
254
+ n1 = nrng.normal(0, 1.0 + SCENE["noise"], back.shape)
255
+ n2 = nrng.normal(0, 1, back.shape)
256
+ out = {}
257
+ for si, ppm in enumerate(steps):
258
+ mpx = ppm * K
259
+ s = mpx / TEX
260
+ half = t * s / 2
261
+ code_half = (n / 2 + 4) * mpx
262
+ room_x = max(0.0, W / 2 - code_half * 1.4)
263
+ room_y = max(0.0, H / 2 - code_half * 1.4)
264
+ center = (W / 2 + u1 * room_x, H / 2 + u2 * room_y)
265
+ dst = project([(-half, -half), (half, -half), (half, half), (-half, half)], SCENE["tilt"], axis, psi, center)
266
+ hm = cv2.getPerspectiveTransform(np.float32([[0, 0], [t, 0], [t, t], [0, t]]), dst)
267
+ code_w = n * mpx
268
+ ramp = ((XX - center[0]) * math.cos(beta) + (YY - center[1]) * math.sin(beta)) / max(code_w, 1)
269
+ light = 1 + SCENE["gradient"] * np.clip(ramp, -1.5, 1.5) / 1.5
270
+ for oi, obj in enumerate(objs):
271
+ L, cov = obj.placed()
272
+ tex = L * paper + (1 - L) * np.float32(SCENE["ink"]) # the sheet: L between ink and textured paper
273
+ if s < 1: # optical anti-aliasing before the warp
274
+ tex = cv2.GaussianBlur(tex, (0, 0), 0.42 / s)
275
+ if cov.min() < 1:
276
+ cov = np.clip(cv2.GaussianBlur(cov, (0, 0), 0.42 / s), 0, 1)
277
+ full = back.copy()
278
+ cv2.warpPerspective(tex, hm, (W, H), dst=full, flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_TRANSPARENT)
279
+ a = np.zeros_like(back)
280
+ cv2.warpPerspective(cov, hm, (W, H), dst=a, flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_TRANSPARENT)
281
+ a[a > 1 - SNAP] = 1
282
+ a[a < SNAP] = 0
283
+ sc = back * (1 - a) + full * a
284
+ sc *= light
285
+ sc = cv2.GaussianBlur(sc, (0, 0), SCENE["blur"] * K) # focus
286
+ sc = cv2.filter2D(sc, -1, kern)
287
+ sc += n1 + n2 * np.sqrt(np.clip(sc, 0, 255)) * 0.15
288
+ img = np.clip(sc, 0, 255).astype(np.uint8)
289
+ out[(si, oi)] = cv2.imencode(".jpg", img, [cv2.IMWRITE_JPEG_QUALITY, int(SCENE["jpeg"])])[1].tobytes()
290
+ return out
web/app.js CHANGED
@@ -7,6 +7,8 @@ import { drawLevelAxis, fillHow, initLevels, levelName, levelNames, levelOf, lev
7
  import { lowConf, notRun, panelName, rowLine, toSection } from './verdict.js';
8
  import { announceText, loadPlan, panelPlan, rowPlan, tipLines, whenPlanChanges } from './plan_view.js';
9
  import { kindDefault, renderYours } from './yours.js';
 
 
10
 
11
  const PILE_LO = -6; // codes every scanner read at every resolution
12
  const PILE_HI = 106; // codes no scanner read at any resolution
@@ -14,9 +16,6 @@ const X_MIN = -12;
14
  const X_MAX = 112;
15
  const [APPLE, GOOGLE, HUAWEI] = ['apple-vision', 'google', 'hms-scankit'];
16
  const MAX_PICTURES = 40;
17
- const MAX_MB = 25;
18
- const MAX_BYTES = MAX_MB * 1024 * 1024;
19
- const MAX_CANVAS = 16e6; // pixels: a picture this browser converts is drawn at most this big (every browser can)
20
  const TIP_LINGER = 400; // ms: a tip stays this long after the pointer leaves what it names, so the pointer can reach it
21
  const WORLD = { pristine: 0.2, everyday: 0.5, tough: 0.2, dataset: 0.1 }; // the curve's illustrative mix
22
 
@@ -566,6 +565,7 @@ function again(p, key) {
566
  }
567
 
568
  function readAgain(p) {
 
569
  const was = chosen === p.n;
570
  if (was) chosen = null; // shown again once it is read
571
  wanted = new Set([p]);
@@ -648,6 +648,7 @@ function unchoose() {
648
  function removePicture(p, byKey = false) {
649
  const i = pictures.indexOf(p);
650
  p.abort?.abort();
 
651
  URL.revokeObjectURL(p.url);
652
  pictures.splice(i, 1);
653
  if (lit === p.n) lit = null;
@@ -668,6 +669,7 @@ function removePicture(p, byKey = false) {
668
  function clearPictures() {
669
  for (const p of pictures) {
670
  p.abort?.abort();
 
671
  URL.revokeObjectURL(p.url);
672
  }
673
  const was = chosen != null;
@@ -694,6 +696,7 @@ function addFiles(list) {
694
  return { n: counter, name: f.name || t('rows.pasted', { n: counter }), file: f, url: URL.createObjectURL(f), status: 'waiting', kind: kindDefault() };
695
  });
696
  pictures.push(...added);
 
697
  if (added.length) wanted = new Set(added); // the first of these to be read is shown beside the bell
698
  const notes = [];
699
  if (fit.length > room) notes.push(t('add.too_many', { max: MAX_PICTURES, n: fit.length - room }));
@@ -716,7 +719,6 @@ async function pump() {
716
  p.status = 'done';
717
  p.readings = (p.readings || 0) + 1; // a reading again redraws "Your picture"
718
  p.retry = Boolean(p.result.partial); // a reading the deadline cut short can be tried again
719
- if (!p.retry) p.file = null;
720
  } catch (err) {
721
  if (!pictures.includes(p)) continue; // removed while it was read
722
  p.status = 'failed';
@@ -733,74 +735,13 @@ async function pump() {
733
  renderPictures();
734
  if (p.status === 'done' && wanted?.has(p)) choose(p, 'read'); // shown beside the bell
735
  else renderBell();
 
736
  if (p.status === 'failed' && p.retry) break; // a busy or unreachable server: the rest wait for Try again
737
  }
738
  pumping = false;
739
  renderStatus();
740
  }
741
 
742
- function failure(message, retry = false) {
743
- const err = new Error(message);
744
- err.retry = retry;
745
- return err;
746
- }
747
-
748
- async function send(blob, signal) {
749
- try {
750
- return await fetch('api/measure', { method: 'POST', body: blob, signal, headers: { 'Content-Type': blob.type || 'application/octet-stream' } });
751
- } catch {
752
- throw failure(t('errors.unreachable'), true);
753
- }
754
- }
755
-
756
- /** A picture in a format the server doesn't open (HEIC, AVIF, GIF…) as this browser shows it, with transparent parts
757
- * on white as on a printed page: as PNG, or as a high-quality JPEG when the PNG would be over 25 MB. One bigger than
758
- * MAX_CANVAS pixels is drawn at that size, still well above the largest size it is read at (1920 pixels). */
759
- async function converted(file) {
760
- let bitmap;
761
- try {
762
- bitmap = await createImageBitmap(file);
763
- } catch {
764
- throw failure(t('errors.format'));
765
- }
766
- const k = Math.min(1, Math.sqrt(MAX_CANVAS / (bitmap.width * bitmap.height)));
767
- const canvas = document.createElement('canvas');
768
- canvas.width = Math.max(1, Math.round(bitmap.width * k));
769
- canvas.height = Math.max(1, Math.round(bitmap.height * k));
770
- const ctx = canvas.getContext('2d');
771
- ctx.fillStyle = '#fff';
772
- ctx.fillRect(0, 0, canvas.width, canvas.height);
773
- ctx.imageSmoothingQuality = 'high';
774
- ctx.drawImage(bitmap, 0, 0, canvas.width, canvas.height);
775
- bitmap.close();
776
- const encode = (type, quality) => new Promise((resolve) => canvas.toBlob(resolve, type, quality));
777
- let out = await encode('image/png');
778
- if (out && out.size > MAX_BYTES) out = await encode('image/jpeg', 0.95);
779
- if (!out) throw failure(t('errors.prepare'));
780
- if (out.size > MAX_BYTES) throw failure(t('errors.too_big_converted', { mb: MAX_MB }));
781
- return out;
782
- }
783
-
784
- async function measure(file, signal) {
785
- if (file.type === 'image/svg+xml' || /\.svg$/i.test(file.name)) throw failure(t('errors.svg'));
786
- if (file.size > MAX_BYTES) throw failure(t('errors.too_big', { mb: MAX_MB }));
787
- if (!file.size) throw failure(t('rows.empty_file'));
788
- const opened = /^image\/(jpeg|png|webp)$/.test(file.type); // the formats the server opens; others are converted
789
- let res = await send(opened ? file : await converted(file), signal);
790
- if (res.status === 415 && opened) res = await send(await converted(file), signal); // a file whose type and content disagree
791
- if (!res.ok) {
792
- let message = '';
793
- try { message = (await res.json()).error; } catch { /* not JSON */ }
794
- const retry = res.status === 408 || res.status >= 500; // slow to arrive, busy, or a server fault
795
- throw failure(message || t(retry ? 'errors.busy' : 'errors.unreadable'), retry);
796
- }
797
- try {
798
- return await res.json();
799
- } catch {
800
- throw failure(t('errors.busy'), true); // cut off on the way, or not the server's answer
801
- }
802
- }
803
-
804
  // ---- wiring -------------------------------------------------------------------------------
805
 
806
  function renderLede() {
@@ -858,7 +799,7 @@ async function main() {
858
  const params = new URLSearchParams(location.search);
859
  const theme = params.get('theme') || params.get('__theme'); // __theme: the theme Hugging Face's page passes to the Space
860
  if (theme === 'light' || theme === 'dark') document.documentElement.dataset.theme = theme;
861
- const [data, levels] = await Promise.all([getJson('data/bell.json'), getJson('data/levels.json'), loadCopy('data/copy.json'), loadPlan('data/sizes.json')]);
862
  DATA = data;
863
  initLevels(levels); // the cuts and names the server rates with
864
  RES = decodeResults(DATA);
@@ -872,6 +813,7 @@ async function main() {
872
  levelStats({ n: N, d: (i) => DATA.items.d[i], read1280: (i, k) => reads(i, fi, decIndex(k)), set: (i) => source(i).key });
873
  renderLevelsList($('levelsMore'));
874
  fillHow();
 
875
  const caption = t('bell.caption_levels');
876
  if (caption) $('bellNote').append(` ${caption}`);
877
  const about = t('how.meta_description');
 
7
  import { lowConf, notRun, panelName, rowLine, toSection } from './verdict.js';
8
  import { announceText, loadPlan, panelPlan, rowPlan, tipLines, whenPlanChanges } from './plan_view.js';
9
  import { kindDefault, renderYours } from './yours.js';
10
+ import { measure } from './upload.js';
11
+ import { initTest, loadTest, pictureRemoved, readingReturned, testAdded } from './test.js';
12
 
13
  const PILE_LO = -6; // codes every scanner read at every resolution
14
  const PILE_HI = 106; // codes no scanner read at any resolution
 
16
  const X_MAX = 112;
17
  const [APPLE, GOOGLE, HUAWEI] = ['apple-vision', 'google', 'hms-scankit'];
18
  const MAX_PICTURES = 40;
 
 
 
19
  const TIP_LINGER = 400; // ms: a tip stays this long after the pointer leaves what it names, so the pointer can reach it
20
  const WORLD = { pristine: 0.2, everyday: 0.5, tough: 0.2, dataset: 0.1 }; // the curve's illustrative mix
21
 
 
565
  }
566
 
567
  function readAgain(p) {
568
+ pictureRemoved(p); // its design test stops: the reading may change
569
  const was = chosen === p.n;
570
  if (was) chosen = null; // shown again once it is read
571
  wanted = new Set([p]);
 
648
  function removePicture(p, byKey = false) {
649
  const i = pictures.indexOf(p);
650
  p.abort?.abort();
651
+ pictureRemoved(p); // its design test stops, and the server drops it
652
  URL.revokeObjectURL(p.url);
653
  pictures.splice(i, 1);
654
  if (lit === p.n) lit = null;
 
669
  function clearPictures() {
670
  for (const p of pictures) {
671
  p.abort?.abort();
672
+ pictureRemoved(p);
673
  URL.revokeObjectURL(p.url);
674
  }
675
  const was = chosen != null;
 
696
  return { n: counter, name: f.name || t('rows.pasted', { n: counter }), file: f, url: URL.createObjectURL(f), status: 'waiting', kind: kindDefault() };
697
  });
698
  pictures.push(...added);
699
+ if (added.length) testAdded(); // the server's design test: read once, with the first picture
700
  if (added.length) wanted = new Set(added); // the first of these to be read is shown beside the bell
701
  const notes = [];
702
  if (fit.length > room) notes.push(t('add.too_many', { max: MAX_PICTURES, n: fit.length - room }));
 
719
  p.status = 'done';
720
  p.readings = (p.readings || 0) + 1; // a reading again redraws "Your picture"
721
  p.retry = Boolean(p.result.partial); // a reading the deadline cut short can be tried again
 
722
  } catch (err) {
723
  if (!pictures.includes(p)) continue; // removed while it was read
724
  p.status = 'failed';
 
735
  renderPictures();
736
  if (p.status === 'done' && wanted?.has(p)) choose(p, 'read'); // shown beside the bell
737
  else renderBell();
738
+ readingReturned(p); // marked The original graphic already (read again, or by its record): tested at once
739
  if (p.status === 'failed' && p.retry) break; // a busy or unreachable server: the rest wait for Try again
740
  }
741
  pumping = false;
742
  renderStatus();
743
  }
744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
745
  // ---- wiring -------------------------------------------------------------------------------
746
 
747
  function renderLede() {
 
799
  const params = new URLSearchParams(location.search);
800
  const theme = params.get('theme') || params.get('__theme'); // __theme: the theme Hugging Face's page passes to the Space
801
  if (theme === 'light' || theme === 'dark') document.documentElement.dataset.theme = theme;
802
+ const [data, levels] = await Promise.all([getJson('data/bell.json'), getJson('data/levels.json'), loadCopy('data/copy.json'), loadPlan('data/sizes.json'), loadTest('data/test.json')]);
803
  DATA = data;
804
  initLevels(levels); // the cuts and names the server rates with
805
  RES = decodeResults(DATA);
 
813
  levelStats({ n: N, d: (i) => DATA.items.d[i], read1280: (i, k) => reads(i, fi, decIndex(k)), set: (i) => source(i).key });
814
  renderLevelsList($('levelsMore'));
815
  fillHow();
816
+ initTest(); // the How lines a design test adds, and leaving the page (pagehide, pageshow)
817
  const caption = t('bell.caption_levels');
818
  if (caption) $('bellNote').append(` ${caption}`);
819
  const about = t('how.meta_description');
web/data/copy.json CHANGED
@@ -745,6 +745,88 @@
745
  "capacity": "Version {v2} comes from the QR standard’s capacity tables: the smallest version that holds the whole content in one mode at error correction {ec}, with room for GS1’s FNC1 marker."
746
  }
747
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
748
  "try": {
749
  "title": "What to try",
750
  "general": [
@@ -801,7 +883,8 @@
801
  "levels": "Each picture also gets a level from its difficulty: Easy below {c1}, Everyday from {c1}, Tough from {c2} and Advanced from {c3}; the pile on the left is Easy and the pile on the right Advanced. On the curve, the levels line up with the simulated prints: most clean prints are Easy ({clean_easy} of {clean_n}), most everyday prints Everyday ({everyday_everyday} of {everyday_n}) and most tough prints Tough ({tough_tough} of {tough_n}). Beside your picture, its level is described by measured codes in pictures of the same size: those over 1280 pixels on the long side, as a full-size phone photo is, are mostly simulated prints, and smaller ones mostly research images. For a picture in the pile on the right it is described by the pile’s own codes instead, both sizes together, because the codes the five place at Advanced were read far more often.",
802
  "confidence": "Its confidence, high, medium or low, says how far to trust the level. The five scanners here stand in for eight: on the measured codes, the level from the five’s reads matched the one from all eight for {agree_m} of {agree_n}. A picture’s confidence is how often that held for measured codes read like it. For a picture in a pile, those are the codes in the same pile. Otherwise they are codes read by as many of the five at 1280 pixels (fewer than two, or more) that sit about as close to a level boundary; with two or more reads, they are also in pictures of the same size class (over 1280 pixels on the long side, or not) and, near a boundary, on the same side of it, except that a picture over 1280 pixels near the Advanced boundary counts with those just below it. It is high where at least {high} matched, medium from {medium}, and low below that. A reading cut short by the time limit is always low, and one where OpenCV’s newer detector wasn’t run at some sizes, on a picture with many codes, is at most medium. Confidence covers only the stand-in, not the photo: another photo of the same code can land at another level.",
803
  "checks": "The checks measure the picture where the scanners found the code: camera pixels per square, size, margin, sharpness, contrast, lighting and angle. Their limits come from QR Scan Difficulty’s factor tests, where one code all eight scanners read was changed one thing at a time, with 80 test codes a step read at 1280 pixels. A check is Fine where all eight read 9 in 10 test codes like it, Borderline where the phones’ own scanners did but at least one other didn’t, and a Problem where one of the phones’ own didn’t. Each limit sits halfway between the check’s typical values on the last step with one status and the first step with the next; where two tests set one limit, it sits in the middle of the range both allow. For pixels per square, the Problem limit rises with the code’s version. The margin follows the QR standard’s 4 squares instead, and a code that fills much of the picture gets a note about framing, not a status. The checks explain and advise; they never change the level or its confidence.",
804
- "selector": "Each picture whose version a scanner reported, or the checks estimate, also gets how wide to print its code for a scanning distance: from the version, the camera pixels per square that Apple’s, Google’s and Huawei’s scanners were measured to need at that version in a quick everyday scan (Apple’s and Google’s alone from version 16), and an assumed phone camera. It is an estimate, never a promise that a phone will scan. You can say what a picture shows: the original graphic, a printed code or a code on a screen. That picks what is shown first, and the advice: for the original graphic, how wide to print the picture itself. It never changes the level, its confidence or the checks, and the choice, the distance and any width you give stay in your browser."
 
805
  },
806
  "lede": "All {N} codes measured at seven sizes, on one scale from easy to hard, shaded by how many of {eight} scanners read each one. Add pictures of your own codes to see where they fall, which level they reach, and why.",
807
  "meta_description": "Where does your QR code fall among thousands of measured codes? Add a picture: five open-source scanners read it at seven sizes, place it on the scale eight scanners set, and rate it Easy, Everyday, Tough or Advanced."
 
745
  "capacity": "Version {v2} comes from the QR standard’s capacity tables: the smallest version that holds the whole content in one mode at error correction {ec}, with room for GS1’s FNC1 marker."
746
  }
747
  },
748
+ "test": {
749
+ "hint": "It’s scanned beside a plain code with the same content in {s} simulated everyday scenes, {at}.",
750
+ "at": {
751
+ "width": "At that width",
752
+ "distance": "From that distance",
753
+ "width_lower": "at that width",
754
+ "distance_lower": "from that distance"
755
+ },
756
+ "hint_browser": "{name} in this browser joins in: this browser downloads about {mb} MB of test pictures for it.",
757
+ "hint_offer": "{name} in this browser can join in: this browser would download about {mb} MB of test pictures for it.",
758
+ "add": "Add {name}",
759
+ "leave": "Leave it out",
760
+ "running": "Testing… {k} of {s} scenes",
761
+ "announce": "Testing the design.",
762
+ "announce_named": "Picture {n}, {name}: {line}",
763
+ "queued": "Waiting to be tested.",
764
+ "waiting": "The test is busy just now: try again in a few minutes.",
765
+ "busy": "Another design is being tested: this one starts by itself in about {n} seconds.",
766
+ "late": "It couldn’t finish in time: try again later.",
767
+ "like": "{at}, {who} read it as well as a plain code.",
768
+ "worse": "{at}, {who} read it worse than a plain code. Printing it wider or styling it more plainly may help: scan a test print with a few phones before a big run.",
769
+ "unsure": "{at}, the test couldn’t tell whether {who} read it as well as a plain code: scan a test print with a few phones before a big run.",
770
+ "missing": {
771
+ "google": "Google’s scanner wasn’t in this test: test it in Chrome on an Android phone for its answer.",
772
+ "apple": "Apple’s scanner wasn’t in this test: test it in Chrome on a Mac for its answer.",
773
+ "mac": "Phones’ scanners weren’t in this test, and can read such designs worse: test it in Chrome on this Mac for Apple’s answer, or scan a test print with a few phones.",
774
+ "mac_none": "Phones’ scanners weren’t in this test, and can read such designs worse: this browser has none the test can use, but Chrome on an Android phone gives Google’s answer; or scan a test print with a few phones.",
775
+ "android_none": "Phones’ scanners weren’t in this test, and can read such designs worse: this browser has none the test can use, but Chrome on a Mac gives Apple’s answer; or scan a test print with a few phones.",
776
+ "other": "Phones’ scanners weren’t in this test, and can read such designs worse: test it in Chrome on a Mac or an Android phone for Apple’s or Google’s answer, or scan a test print with a few phones.",
777
+ "save_data": "This browser asks to save data, so its scanner wasn’t in this test, and phones’ scanners can read such designs worse: turn data saving off, then try again, or scan a test print with a few phones.",
778
+ "left_out": "{name} was left out of this test to save data, and phones’ scanners can read such designs worse: add it to test again, or scan a test print with a few phones."
779
+ },
780
+ "fallback": {
781
+ "start": "{name} didn’t start in this browser this time, so it wasn’t in this test, and phones’ scanners can read such designs worse: try again to bring it back, or scan a test print with a few phones.",
782
+ "stopped": "{name} stopped partway, so it wasn’t in this test, and phones’ scanners can read such designs worse: try again, or scan a test print with a few phones.",
783
+ "slow": "The test pictures came too slowly for {name}, so it wasn’t in this test, and phones’ scanners can read such designs worse: try again on a faster connection, or scan a test print with a few phones."
784
+ },
785
+ "cant": {
786
+ "facts": "It can’t be tested: a plain code needs its error correction and mask, which zxing-cpp didn’t read. Scan a test print with a few phones before a big run.",
787
+ "inverted": "Light-on-dark codes can’t be tested here: the test compares a design with a dark-on-light plain code. Scan a test print with a few phones before a big run.",
788
+ "mirrored": "This code is mirrored: flip it the right way round before printing.",
789
+ "small": "Export it with at least {min} pixels per square to test it.",
790
+ "big": "Export it at {mp} megapixels or fewer to test it.",
791
+ "version": "Versions over {max} can’t be tested here yet: scan a test print with a few phones before a big run.",
792
+ "several": "It can’t be tested: it holds more than one code. Add a crop of the file that holds one code and its margin.",
793
+ "twin": "It can’t be tested: a plain code of the same content couldn’t be made. Scan a test print with a few phones before a big run.",
794
+ "differ": "It can’t be tested: the test read its code differently from the first reading. Scan a test print with a few phones before a big run."
795
+ },
796
+ "name": {
797
+ "apple": "Apple’s scanner",
798
+ "google": "Google’s scanner",
799
+ "zxing-cpp": "zxing-cpp",
800
+ "wechat": "WeChat’s scanner"
801
+ },
802
+ "more_summary": "How this was tested",
803
+ "more": {
804
+ "method": "The design, with up to {margin} squares of it round its code, and a plain code with the same content, version, error correction and mask were each scanned in the same {s} simulated scenes: the quick everyday scan the width is worked out for, with each square at {p} camera pixels, version {v}’s need.",
805
+ "rule": "Each scanner is judged on its own, scene by scene. It read the design worse when it missed the design where it read the plain code too often to put down to chance: in at least {least} scenes with none the other way round, or more when there are some. It read it as well as a plain code when that happened at most {cost} more times than the other way round, and it read the plain code, or the design, in at least {q} scenes. Otherwise it couldn’t tell. One scanner reading it worse is enough.",
806
+ "each": "{name}: {result}.",
807
+ "result": {
808
+ "like": "as well as a plain code",
809
+ "worse": "worse than a plain code",
810
+ "close": "a little worse, too little to be sure",
811
+ "few": "too few scenes read to compare"
812
+ },
813
+ "apple": "Apple’s scanner here is the one Chrome uses on a Mac, built into macOS. In tests, Chrome on macOS {os} read {n} of {n} simulated scans exactly as Apple’s scanner did on its own. Other versions may read differently, but it read the plain code too, so the two still compare.",
814
+ "google": "Google’s scanner here is the one Chrome uses on Android phones with Google Play. In tests on an Android {os} emulator with Google Play, Chrome read {n} of {n} simulated scans exactly as Google’s scanner did on its own. Other versions may read differently, but it read the plain code too, so the two still compare.",
815
+ "cover_open": "In tests on {N} designs from one code generator, zxing-cpp and WeChat’s scanner read {y} as well as a plain code. Apple’s or Google’s scanner read {x} of those worse, and couldn’t tell for {z} more.",
816
+ "cover_open_worse": "Of the {y} that zxing-cpp or WeChat’s scanner read worse in those tests, Apple’s or Google’s read {x} worse too.",
817
+ "cover_apple": "In tests on {N} designs from one code generator, Apple’s scanner, zxing-cpp and WeChat’s read {y} as well as a plain code. Google’s scanner read {x} of those worse, and couldn’t tell for {z} more.",
818
+ "cover_google": "In tests on {N} designs from one code generator, Google’s scanner, zxing-cpp and WeChat’s read {y} as well as a plain code. Apple’s scanner read {x} of those worse, and couldn’t tell for {z} more.",
819
+ "limits": "{s} scenes show big differences, not small ones: a design that misses {big} of the scans a plain code reads still gets “as well as a plain code” about {big_pass}, and one that misses {small}, about {small_pass}.",
820
+ "limits_share": "{a} in {b}",
821
+ "limits_times_one": "{a} time in {b}",
822
+ "limits_times_many": "{a} times in {b}",
823
+ "scope": "The test pictures are simulated scans of a print, read as still pictures: no phone’s camera or app took part. The figures above come from {N} designs from one code generator, in {styles} styles at versions {vlo} to {vhi}.",
824
+ "private": "What this browser’s scanner read stayed in this page."
825
+ },
826
+ "privacy": "A picture marked as the original graphic is sent again for its design test, and when this browser can read the test’s simulated scans, it fetches them from the server and reads them here. Nothing is kept.",
827
+ "choice_hint": "The original graphic also starts a design test, which sends the picture again.",
828
+ "how_scanners": "In Chrome on a Mac or an Android phone, a design test can add Apple’s or Google’s in your own browser."
829
+ },
830
  "try": {
831
  "title": "What to try",
832
  "general": [
 
883
  "levels": "Each picture also gets a level from its difficulty: Easy below {c1}, Everyday from {c1}, Tough from {c2} and Advanced from {c3}; the pile on the left is Easy and the pile on the right Advanced. On the curve, the levels line up with the simulated prints: most clean prints are Easy ({clean_easy} of {clean_n}), most everyday prints Everyday ({everyday_everyday} of {everyday_n}) and most tough prints Tough ({tough_tough} of {tough_n}). Beside your picture, its level is described by measured codes in pictures of the same size: those over 1280 pixels on the long side, as a full-size phone photo is, are mostly simulated prints, and smaller ones mostly research images. For a picture in the pile on the right it is described by the pile’s own codes instead, both sizes together, because the codes the five place at Advanced were read far more often.",
884
  "confidence": "Its confidence, high, medium or low, says how far to trust the level. The five scanners here stand in for eight: on the measured codes, the level from the five’s reads matched the one from all eight for {agree_m} of {agree_n}. A picture’s confidence is how often that held for measured codes read like it. For a picture in a pile, those are the codes in the same pile. Otherwise they are codes read by as many of the five at 1280 pixels (fewer than two, or more) that sit about as close to a level boundary; with two or more reads, they are also in pictures of the same size class (over 1280 pixels on the long side, or not) and, near a boundary, on the same side of it, except that a picture over 1280 pixels near the Advanced boundary counts with those just below it. It is high where at least {high} matched, medium from {medium}, and low below that. A reading cut short by the time limit is always low, and one where OpenCV’s newer detector wasn’t run at some sizes, on a picture with many codes, is at most medium. Confidence covers only the stand-in, not the photo: another photo of the same code can land at another level.",
885
  "checks": "The checks measure the picture where the scanners found the code: camera pixels per square, size, margin, sharpness, contrast, lighting and angle. Their limits come from QR Scan Difficulty’s factor tests, where one code all eight scanners read was changed one thing at a time, with 80 test codes a step read at 1280 pixels. A check is Fine where all eight read 9 in 10 test codes like it, Borderline where the phones’ own scanners did but at least one other didn’t, and a Problem where one of the phones’ own didn’t. Each limit sits halfway between the check’s typical values on the last step with one status and the first step with the next; where two tests set one limit, it sits in the middle of the range both allow. For pixels per square, the Problem limit rises with the code’s version. The margin follows the QR standard’s 4 squares instead, and a code that fills much of the picture gets a note about framing, not a status. The checks explain and advise; they never change the level or its confidence.",
886
+ "selector": "Each picture whose version a scanner reported, or the checks estimate, also gets how wide to print its code for a scanning distance: from the version, the camera pixels per square that Apple’s, Google’s and Huawei’s scanners were measured to need at that version in a quick everyday scan (Apple’s and Google’s alone from version 16), and an assumed phone camera. It is an estimate, never a promise that a phone will scan. You can say what a picture shows: the original graphic, a printed code or a code on a screen. That picks what is shown first, and the advice: for the original graphic, how wide to print the picture itself. It never changes the level, its confidence or the checks, and the choice, the distance and any width you give stay in your browser.",
887
+ "selector_test": "Each picture whose version a scanner reported, or the checks estimate, also gets how wide to print its code for a scanning distance: from the version, the camera pixels per square that Apple’s, Google’s and Huawei’s scanners were measured to need at that version in a quick everyday scan (Apple’s and Google’s alone from version 16), and an assumed phone camera. It is an estimate, never a promise that a phone will scan. You can say what a picture shows: the original graphic, a printed code or a code on a screen. That picks what is shown first, and the advice: for the original graphic, how wide to print the picture itself. It never changes the level, its confidence or the checks, and the choice, the distance and any width you give stay in your browser, except that marking a picture as the original graphic starts a design test, which sends the picture again."
888
  },
889
  "lede": "All {N} codes measured at seven sizes, on one scale from easy to hard, shaded by how many of {eight} scanners read each one. Add pictures of your own codes to see where they fall, which level they reach, and why.",
890
  "meta_description": "Where does your QR code fall among thousands of measured codes? Add a picture: five open-source scanners read it at seven sizes, place it on the scale eight scanners set, and rate it Easy, Everyday, Tough or Advanced."
web/data/test.json ADDED
@@ -0,0 +1,211 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "about": "The design test's named choices, the figures behind its \"How this was tested\", and the working check's two pictures, each with its source. The page (test.js, scanner.js) and the server's design test (design.py) both read this file; the server reads it once at start-up, and a missing or broken file turns the test off.",
3
+ "version": "2026-11-15",
4
+ "page": false,
5
+ "scenes": {
6
+ "s": 20,
7
+ "seeds": [
8
+ 0,
9
+ 1,
10
+ 2,
11
+ 3,
12
+ 4,
13
+ 5,
14
+ 6,
15
+ 7,
16
+ 8,
17
+ 9,
18
+ 10,
19
+ 11,
20
+ 12,
21
+ 13,
22
+ 14,
23
+ 15,
24
+ 16,
25
+ 17,
26
+ 18,
27
+ 19
28
+ ],
29
+ "streams": "grid:everyday",
30
+ "source": "the study's seeds: seeds 0 to 19 of the size table's own everyday streams for the version; 19 is the fewest scenes at which a clean pass rules out a 15% loss (13.9% at 20)"
31
+ },
32
+ "step": {
33
+ "at": "p(v)",
34
+ "source": "one step, at the measured table's need for the version (sizes.json needs); a version between measured ones takes the next one up, version 1 takes version 2's"
35
+ },
36
+ "rule": {
37
+ "alpha": 0.05,
38
+ "like_cost": 2,
39
+ "q": 16,
40
+ "least_worse": 5,
41
+ "source": "worse: a one-sided exact sign test under 0.05 (a definition), which needs a cost of at least 5; as well as a plain code: a cost of 2 or less, the study's flag being 3, with the plain code or the design read in at least 16 of 20 scenes (a named power choice, the same answers from 14 to 16 on the tuning half); otherwise can't tell: close with a cost of 3 or more, else few"
42
+ },
43
+ "limits": {
44
+ "min_ppm": 2,
45
+ "min_ppm_v1": 3,
46
+ "max_version": 21,
47
+ "max_version_after_b": 21,
48
+ "max_pixels": 16000000,
49
+ "source": "at least 2 pixels per square in the file (uec_min_ppm in sizes.json; the study's 2-pixel files were tested), 3 for version 1 (uec_min_ppm_v1, the same floor); versions up to 10, and up to 21, the size table's last measured version, once plain codes at 13, 15, 18 and 21 never read worse; at most 16 megapixels, the page's own conversion cap"
50
+ },
51
+ "server": {
52
+ "deadline_s": 40,
53
+ "nice": 10,
54
+ "slot_hold_s": 10,
55
+ "ticket_extra_s": 5,
56
+ "idle_s": 60,
57
+ "after_deadline_s": 60,
58
+ "scene_wait_s": 10,
59
+ "retry_s": [
60
+ 2,
61
+ 40
62
+ ],
63
+ "max_tickets": 256,
64
+ "held_mb_max": 128,
65
+ "busy_while_readings_wait": false,
66
+ "source": "the server reads these at start-up: its deadline equals the reading's 40 s; the worker's priority, the deadline and the browser's budget are set again from the live timing run; a slot gives its upload 10 s to start; a ticket is held for at most its last Retry-After plus 5 s; a test is dropped after 60 s with no request, or 60 s after its deadline; a scene not yet rendered is waited for 10 s; Retry-After runs from 2 to 40 s; at most 256 tickets wait at once; the scenes held for pages are bounded at 128 MB; the busy answer while readings queue stays off unless the live run calls for it"
67
+ },
68
+ "page_waits": {
69
+ "settle_s": 1,
70
+ "between_tests_s": 2,
71
+ "poll_s": 1,
72
+ "busy_total_s": 120,
73
+ "check_retry_s": 2,
74
+ "probe_wait_s": 3,
75
+ "scene_retry_s": 1,
76
+ "upload_s": 100,
77
+ "source": "a choice still standing after 1 s starts a test (the keyboard passes choices it doesn't keep); 2 s between a page's tests as a courtesy; polls about once a second; busy for up to 2 minutes; 2 s before the working check's retry; a test waits at most 3 s for the probe (a cold detect took 0.97 s); a scene's facts are asked for again 1 s after the server says it isn't rendered yet; a slot request unanswered within the slot's own 10 s hold (slot_hold_s), or an upload unanswered after 100 s, ends as the server unreachable, with Try again: the server answers a slot request at once, gives a picture 60 s to arrive, as for a reading, and starts its test within the 40 s deadline"
78
+ },
79
+ "budget": {
80
+ "fetch_s": 45,
81
+ "project_after_scenes": 2,
82
+ "detect_s": 5,
83
+ "fetch_tries": 2,
84
+ "source": "45 s of fetching the scenes' pictures from the test's start, projected from the link's own pace after 2 scenes and after each one from then on; it holds down to about 2 Mbit/s up to version 10 and 3 Mbit/s at 21; a failed fetch is tried once more; each detect has 5 s"
85
+ },
86
+ "join": 3,
87
+ "join_source": "the browser's scanner joins by itself for a page session's first 3 tests, then is offered with one tap: so a page downloads at most about 24 MB of test pictures unasked",
88
+ "browsers": {
89
+ "brand": "Google Chrome",
90
+ "platforms": {
91
+ "macOS": "apple",
92
+ "Android": "google"
93
+ },
94
+ "source": "Google Chrome on macOS and on Android, named by platform after the working check; other browsers get the open-source answer until each is measured"
95
+ },
96
+ "frames": {
97
+ "capture": [
98
+ 1920,
99
+ 1440
100
+ ],
101
+ "frame": [
102
+ 1280,
103
+ 960
104
+ ],
105
+ "jpeg_quality": 82,
106
+ "png_level": 1,
107
+ "margin_squares": 6,
108
+ "twin_margin": 4,
109
+ "source": "the size table's everyday scene: each capture a baseline JPEG at quality 82, shrunk to the 1280 x 960 grey frame the scanners read by the whole-number rule (4a + 2b + 2c + d + 4) div 9; the patches and the fallback frames are grey PNGs at compression level 1; up to 6 squares of the design show round its code, and the plain code has a 4-square margin"
110
+ },
111
+ "mb": {
112
+ "jpeg": {
113
+ "10": 9,
114
+ "21": 16
115
+ },
116
+ "png": {
117
+ "10": 16,
118
+ "21": 23
119
+ },
120
+ "source": "the most measured for the code's range, rounded up: with JPEG frames 8.46 MB up to version 10 and 15.02 at version 21; with PNG frames 14.55 and 21.33, plus room for two scenes' JPEGs fetched before a switch"
121
+ },
122
+ "check": {
123
+ "png": {
124
+ "b64": "iVBORw0KGgoAAAANSUhEUgAAAJQAAACUCAAAAABQV18IAAABkklEQVR42u2ay6rDMAxEpXL//5fVrRHWw6aLi3WyK3HM0DlIIydq8v+ujyAKUYhCFKIQhShEIQpRiEIUon50/UU3NHnIljXm1mb3on2wbwZTkeeeH3Uc6cU+2DeHKQlqkDiWfF2yg32wbx5T1WWuZknCFfbBVLeGWZKnsA+mujl6x48FnMkhX9j3LlParFU+R+2yO/bBlF3ydLMP9j0lSq3BS8aJFj1PLzjDvrd7346X3TnCSR2r8jv2vctUxpNu1ni+LGCs4gr73ux9Wb3JeNCCSYGpsXVqZcGfZ3Yzl27mQMW+0b0v64fVmbkG/bKqc9g3J09lubt6ryzJPtg3r05181PF0Lqm6oHY937v6+Qgba5f3+Ng39y573S+28160fyHffPmvqhuRd93isTfvWTPYN+cjB6xps0aZM2zB+ybk6equmLFbHeS97FvTkbvZPfqvbJu2KP3wVR1jiDNjMVZwmymOt9FebYyfpj75p4lVHOfFr+rPeh985jin0IUohCFKEQhClGIQhSiEIWoJ0R9AcClgkW7JaIYAAAAAElFTkSuQmCC",
125
+ "w": 148,
126
+ "h": 148,
127
+ "grey_sha256": "75617964609cd3d236f991324d72afe13f686203826f150753756fc5ccbdba0b",
128
+ "content_sha256": "c127abccc1678a8782bbe8aa087f9b68ec1b0f451b3f4322198c9df89e26d965"
129
+ },
130
+ "jpeg": {
131
+ "b64": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBAUEBAYFBQUGBgYHCQ4JCQgICRINDQoOFRIWFhUSFBQXGiEcFxgfGRQUHScdHyIjJSUlFhwpLCgkKyEkJST/wAALCABgAIABAREA/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/9oACAEBAAA/AObEZ3EjPHOP/rU9Y2yMlQcfnT9mCBjORn260vlA9iAecnP+fWniI4PODngd6esRYAgjGMHg5HuacIGOBvPuM8Yp8cW98glQTztH9Kcse1RtXBJ49qcIuA+BgY79ackQDnJ6ntk1IsJyCeF6HPWhIOSozuzg+3+c1IkfzZ27ieoFKI+pJBIyevWpBEFdsDJ4PJ6n+lYflgHJ5GdvNO8smQA56fpT1Tay8Z4xgCt/wz4YGvGX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vTyzykBjzXm+raxrlt4iltLWS4SwWbaqD7m3PQV7TpOjeG/KtZo7ezFxsBzwSrf41V8cAFIO+QeM5AFcnhSQB0yO2cf5/pTzHlT8uOpGc04Drt5zwe+OK5lZAQM/MR0OelODgnGMHjH1/rThIdwYcbhljkdvb/AD1rtPAm6ey1Lbklo+OOteNX/g/XpL+4b+z5yGdiCEJzyea6T4Y+FtY0/wAW289xZTwoo5crxXpHxhs9QutBjj0xJnk8wf6s8isb4O6NqEa3Q1i2dh1Tzhn+f4V6d/ZFlt2m1gYDq2wAHNeE/EPQdck8UTNp9vdC2GMCMHb+GPpXrOkaSi+E4VmtE88QYbIywbHc+v8AhXjehaN4ki8WRObe78jzzz0AGa9a8cltlsOMqO/1/SuRV0POCQe/5U8MVBYK2MYGetKjANgE8emMcj/69cgLn5dyvwT6c81Ksmw7s5C5znvR5oJxtGR+ldJ4X8Wf2Ak37sSeZjaScf8A1q3l+KKIQRZDoSTkfpSr8TkHIskJAxg8fjmnr8T1yqfZGA6nP8ulCfEtVDbbUdQw54x/k1JH8S1IGbYAngDPoKQ/EmJ8mS0GfX/H86efiQQgC2YHPTPX/CkHxFRSrfZBnvWRr3iVdb2BYwgUfX9RWKsp3nnJwBnPFSeZksOABjPXI/zxUmWDdenO32/z/OuMWTIOcLn05xxThLgcDgdfrTkkIBOCAe/Yf4dP509XPOGPXHvTvNzxj8OmPapEc8DGQv480onIPvjHX/PpUhlPABALY79e9PDch1IBI9evOKVJyGCnoew5p3mgqduCcgn/AD/npTxNuOBjnkHHftTxNwCFAPAPbNOEnzkH8AeaBLuyABtPoev4n6VI0w28rtb1Ff/Z",
132
+ "w": 128,
133
+ "h": 96,
134
+ "grey_sha256": "d401cd2e47bb5934a59db3b51d93b05e0165a69e3ad0cf56eb122e5a8173575f"
135
+ },
136
+ "source": "a plain code of a test link on example.com (version 2, error correction M, mask 2, 4 pixels a square, a 6-square margin) and a small grey JPEG made as the captures are (baseline, quality 82, one channel); each hash is the SHA-256 of its grey bytes as OpenCV decodes it, which is libjpeg-turbo's accurate integer IDCT for the JPEG; the fast and float IDCTs give other hashes"
137
+ },
138
+ "figures": {
139
+ "N": 125,
140
+ "cover": {
141
+ "open": {
142
+ "y": 53,
143
+ "x": 14,
144
+ "z": 11
145
+ },
146
+ "open_worse": {
147
+ "y": 69,
148
+ "x": 64
149
+ },
150
+ "apple": {
151
+ "y": 32,
152
+ "x": 1,
153
+ "z": 3
154
+ },
155
+ "google": {
156
+ "y": 38,
157
+ "x": 2,
158
+ "z": 8
159
+ }
160
+ },
161
+ "chrome": {
162
+ "apple": {
163
+ "os": "14.3.1",
164
+ "n": 13040,
165
+ "chrome": 157
166
+ },
167
+ "google": {
168
+ "os": "14",
169
+ "n": 3200,
170
+ "chrome": 113,
171
+ "play_services": "26.41.31"
172
+ }
173
+ },
174
+ "limits": {
175
+ "big": [
176
+ 3,
177
+ 10
178
+ ],
179
+ "big_pass": [
180
+ 1,
181
+ 30
182
+ ],
183
+ "small": [
184
+ 1,
185
+ 10
186
+ ],
187
+ "small_pass": [
188
+ 2,
189
+ 3
190
+ ],
191
+ "binomial": [
192
+ 0.035,
193
+ 0.677
194
+ ]
195
+ },
196
+ "scope": {
197
+ "styles": 4,
198
+ "versions": [
199
+ 3,
200
+ 8
201
+ ]
202
+ },
203
+ "source": "a study of one code generator's 125 distinct styled designs that are dark on light, beside plain codes with the same content, in 13,040 simulated captures; coverage counts rest on Apple's and Google's scanners only; Chrome 157 on macOS 14.3.1 read all 13,040 captures exactly as Apple's scanner did, and Chrome 113 on Google's Android 14 emulator with Google Play (Play services 26.41.31) all 3,200 of a 40-design sample exactly as Google's; the limits, each [a, b] for a in b, are the binomial chance that a design losing that share of 20 scans still passes"
204
+ },
205
+ "acceptance": {
206
+ "band": 24,
207
+ "measure_median_x": 1.25,
208
+ "measure_p95_x": 1.5,
209
+ "source": "set before the live run: the open-source answers allowed to move to or from can't tell on the server's own processor, the number of the held-out half's answers that one changed read (of the 40 per scanner per design) would move to or from can't tell on a Mac, 24 of 170, none of which one read could swap between as well as and worse; and /api/measure's budgets while tests run; no code reads these"
210
+ }
211
+ }
web/frames.js ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // A design test's simulated scans, rebuilt in this browser exactly as the server's scanners read them. For each scene:
2
+ // its facts (the box where the plain code's frame differs from the design's, and both frames' SHA-256), the design's
3
+ // capture JPEG, decoded by this browser and shrunk to the 1280 x 960 grey frame by the whole-number rule, and the
4
+ // plain code's patch pasted over a copy of that frame. A frame whose SHA-256 isn't the server's comes again as PNG.
5
+ // Pixels are read back through an OffscreenCanvas that is never shown: no frame is shown, sent or kept.
6
+
7
+ export const CAPTURE = [1920, 1440]; // a scene's capture, as its JPEG comes
8
+ export const FRAME = [1280, 960]; // the grey frame every scanner reads: the shrink below is exact for these two sizes
9
+ const [CW, CH] = CAPTURE;
10
+ const [FW, FH] = FRAME;
11
+ const HEX = /^[0-9a-f]{64}$/;
12
+
13
+ const hex = (buf) => Array.from(new Uint8Array(buf), (b) => b.toString(16).padStart(2, '0')).join('');
14
+ /** The SHA-256 of some bytes, in lower-case hex. */
15
+ export const sha256Hex = async (bytes) => hex(await crypto.subtle.digest('SHA-256', bytes));
16
+
17
+ /** An error that says how the browser's part ends: 'stopped', 'slow', 'dropped' or 'aborted'. */
18
+ export const ending = (end) => Object.assign(new Error(end), { end });
19
+
20
+ /** An AbortController that aborts with signal too (each request its own, so one can be aborted alone). */
21
+ export function linked(signal) {
22
+ const ctl = new AbortController();
23
+ if (signal.aborted) ctl.abort();
24
+ else signal.addEventListener('abort', () => ctl.abort(), { once: true });
25
+ return ctl;
26
+ }
27
+
28
+ /** Wait ms, or reject as 'aborted' once signal aborts. */
29
+ export const sleep = (ms, signal) => new Promise((resolve, reject) => {
30
+ if (signal.aborted) { reject(ending('aborted')); return; }
31
+ const id = setTimeout(resolve, ms);
32
+ signal.addEventListener('abort', () => { clearTimeout(id); reject(ending('aborted')); }, { once: true });
33
+ });
34
+
35
+ let away = 0; // ms this page spent frozen before `since`, which the pictures' budget leaves out (get)
36
+ let since = null; // when the page was frozen, while it still is
37
+ /** test.js, on the page's freeze and resume: whether the page is frozen now (it doesn't run). A page hidden but
38
+ * running isn't away: it keeps fetching at the link's own pace. */
39
+ export function hidden(now) {
40
+ if (now && since === null) since = performance.now();
41
+ if (!now && since !== null) {
42
+ away += performance.now() - since;
43
+ since = null;
44
+ }
45
+ }
46
+ const awayMs = () => away + (since === null ? 0 : performance.now() - since);
47
+
48
+ const contexts = new Map(); // a 2D context for each full size, reused; a fresh one for any other size
49
+ function contextFor(w, h) {
50
+ const key = `${w}x${h}`;
51
+ if (contexts.has(key)) return contexts.get(key);
52
+ const ctx = new OffscreenCanvas(w, h).getContext('2d', { willReadFrequently: true });
53
+ if ((w === CW && h === CH) || (w === FW && h === FH)) contexts.set(key, ctx);
54
+ return ctx;
55
+ }
56
+
57
+ /** A picture's pixels as one grey byte each, its red channel (every test picture is grey), decoded by this browser:
58
+ * createImageBitmap, drawn into an OffscreenCanvas made for frequent reads, read back with getImageData. Throws when
59
+ * it doesn't decode, or not at w x h. */
60
+ export async function decodeGrey(bytes, type, w, h) {
61
+ const bitmap = await createImageBitmap(new Blob([bytes], { type }));
62
+ try {
63
+ if (bitmap.width !== w || bitmap.height !== h) throw new Error(`decoded at ${bitmap.width} x ${bitmap.height}`);
64
+ const ctx = contextFor(w, h);
65
+ ctx.clearRect(0, 0, w, h);
66
+ ctx.drawImage(bitmap, 0, 0);
67
+ const rgba = ctx.getImageData(0, 0, w, h).data;
68
+ const grey = new Uint8Array(w * h);
69
+ for (let i = 0, j = 0; i < grey.length; i += 1, j += 4) grey[i] = rgba[j];
70
+ return grey;
71
+ } finally {
72
+ bitmap.close();
73
+ }
74
+ }
75
+
76
+ /** The whole-number shrink, 1920 x 1440 to 1280 x 960, as the server makes its frames (equal to OpenCV's INTER_AREA
77
+ * at this size): on each axis, output i reads source (3i + 1) >> 1 with weight 2/3 and 3 (i >> 1) + 1 with weight
78
+ * 1/3, so frame = (4a + 2b + 2c + d + 4) div 9, with a weighted 2/3 both ways and d 1/3 both ways. */
79
+ export function shrink(cap) {
80
+ const out = new Uint8Array(FW * FH);
81
+ for (let y = 0; y < FH; y += 1) {
82
+ const r2 = ((3 * y + 1) >> 1) * CW;
83
+ const r1 = (3 * (y >> 1) + 1) * CW;
84
+ for (let x = 0; x < FW; x += 1) {
85
+ const c2 = (3 * x + 1) >> 1;
86
+ const c1 = 3 * (x >> 1) + 1;
87
+ out[y * FW + x] = ((4 * cap[r2 + c2] + 2 * cap[r2 + c1] + 2 * cap[r1 + c2] + cap[r1 + c1] + 4) / 9) | 0;
88
+ }
89
+ }
90
+ return out;
91
+ }
92
+
93
+ /** A copy of the design's frame with the plain code's patch (h rows of w bytes) written at (x, y): the plain code's
94
+ * frame. Throws when the box isn't inside the frame, or the patch isn't the box's size. */
95
+ export function paste(base, patch, box) {
96
+ const [x, y, w, h] = box;
97
+ if (!(x >= 0 && y >= 0 && w > 0 && h > 0 && x + w <= FW && y + h <= FH) || patch.length !== w * h) {
98
+ throw new Error('the patch doesn’t fit');
99
+ }
100
+ const out = new Uint8Array(base);
101
+ for (let r = 0; r < h; r += 1) out.set(patch.subarray(r * w, (r + 1) * w), (y + r) * FW + x);
102
+ return out;
103
+ }
104
+
105
+ /** An ImageBitmap of grey bytes (w x h) with every alpha 255: what the browser's scanner reads. */
106
+ export function toBitmap(grey, w, h) {
107
+ const rgba = new Uint8ClampedArray(w * h * 4);
108
+ for (let i = 0, j = 0; i < grey.length; i += 1, j += 4) {
109
+ rgba[j] = grey[i];
110
+ rgba[j + 1] = grey[i];
111
+ rgba[j + 2] = grey[i];
112
+ rgba[j + 3] = 255;
113
+ }
114
+ return createImageBitmap(new ImageData(rgba, w, h));
115
+ }
116
+
117
+ /** One of a scene's routes, as JSON or (picture) as bytes. A 503 (the scene isn't rendered yet) is asked again after
118
+ * meter.wait and isn't a failure; a 404 means the server no longer has the test; any other failure is tried again up
119
+ * to meter.tries in all, then stops the browser's part. A picture counts its bytes and the time it took, from the
120
+ * request to its last byte, in the budget (meter.ms; 503s aside), and ends the part as 'slow' when the budget's
121
+ * time (meter.limit) runs out while it comes; the time the page spent frozen meanwhile counts in neither.
122
+ * A scene's facts that haven't come within meter.hold (longer than the server ever holds the request) count as a
123
+ * failed fetch. */
124
+ async function get(url, signal, meter, picture) {
125
+ for (let fails = 0; ;) {
126
+ if (signal.aborted) throw ending('aborted');
127
+ const ctl = linked(signal);
128
+ let slow = false;
129
+ const limit = picture ? Math.max(0, meter.limit - meter.ms) : meter.hold;
130
+ const t0 = performance.now();
131
+ const a0 = awayMs();
132
+ const took = () => performance.now() - t0 - (picture ? awayMs() - a0 : 0);
133
+ let timer = 0;
134
+ const due = () => { // past the limit by the page's own time; set again for the time it spent away
135
+ if (took() < limit) timer = setTimeout(due, limit - took());
136
+ else { slow = picture; ctl.abort(); }
137
+ };
138
+ timer = setTimeout(due, limit);
139
+ let res = null;
140
+ let body = null;
141
+ try {
142
+ res = await fetch(url, { cache: 'no-store', signal: ctl.signal });
143
+ if (res.ok) body = picture ? new Uint8Array(await res.arrayBuffer()) : await res.json();
144
+ } catch { /* a failed fetch, or aborted: below */ }
145
+ clearTimeout(timer);
146
+ if (picture && res?.status !== 503) meter.ms += took();
147
+ if (slow) throw ending('slow');
148
+ if (signal.aborted) throw ending('aborted');
149
+ if (body) {
150
+ if (picture) meter.bytes += body.length;
151
+ return body;
152
+ }
153
+ if (res?.status === 404) throw ending('dropped');
154
+ if (res?.status === 503) await sleep(meter.wait, signal);
155
+ else if ((fails += 1) >= meter.tries) throw ending('stopped');
156
+ }
157
+ }
158
+
159
+ /** The scene's facts: the box (an empty box when the two frames are the same) and both frames' SHA-256. */
160
+ function factsOf(j) {
161
+ const box = j?.box;
162
+ const ok = Array.isArray(box) && box.length === 4 && box.every((v) => Number.isInteger(v) && v >= 0);
163
+ if (!ok || !HEX.test(j.sha256?.plain) || !HEX.test(j.sha256?.design)) throw ending('stopped');
164
+ return { box, empty: box[2] === 0 || box[3] === 0, plain: j.sha256.plain, design: j.sha256.design };
165
+ }
166
+
167
+ /** Decoded grey bytes, or null when this browser doesn't decode the picture at that size. */
168
+ async function grey(bytes, type, w, h, signal) {
169
+ let out = null;
170
+ try { out = await decodeGrey(bytes, type, w, h); } catch { /* checked as a failed frame */ }
171
+ if (signal.aborted) throw ending('aborted');
172
+ return out;
173
+ }
174
+
175
+ const same = async (frame, want) => Boolean(frame) && (await sha256Hex(frame)) === want;
176
+
177
+ /** A frame fetched as PNG (k = 2s for the plain code's, 2s + 1 for the design's), which must pass its check, or the
178
+ * browser's part stops. */
179
+ async function pngFrame(base, k, want, signal, meter) {
180
+ const frame = await grey(await get(`${base}/frame/${k}`, signal, meter, true), 'image/png', FW, FH, signal);
181
+ if (!(await same(frame, want))) throw ending('stopped');
182
+ return frame;
183
+ }
184
+
185
+ /** One scene's two frames, rebuilt and checked against the server's SHA-256: { plain, design } (1280 x 960 grey bytes
186
+ * each) and switched, true when a frame failed its check (the rest of the test then takes PNG frames). mode 'jpeg'
187
+ * rebuilds the design's frame from its capture JPEG, and 'png' fetches it as PNG, as it does for a JPEG frame that
188
+ * fails. The plain code's frame is its patch over the design's (the design's own for an empty box), and comes as
189
+ * PNG when that fails. meter: the budget's count and the fetches' limits (scanner.js: bytes, ms, scenes, limit,
190
+ * tries, wait and hold). Throws ending(...) as get() and pngFrame() do. */
191
+ export async function fetchScene(base, s, mode, signal, meter) {
192
+ const f = factsOf(await get(`${base}/scene/${s}`, signal, meter, false));
193
+ let switched = false;
194
+ let design = null;
195
+ if (mode === 'jpeg') {
196
+ const cap = await grey(await get(`${base}/scene/${s}/jpeg`, signal, meter, true), 'image/jpeg', CW, CH, signal);
197
+ design = cap && shrink(cap);
198
+ if (!(await same(design, f.design))) {
199
+ design = null;
200
+ switched = true;
201
+ }
202
+ }
203
+ design ||= await pngFrame(base, 2 * s + 1, f.design, signal, meter);
204
+ let plain = f.empty && f.plain === f.design ? design : null;
205
+ if (!f.empty) {
206
+ const [, , w, h] = f.box;
207
+ const patch = await grey(await get(`${base}/scene/${s}/patch`, signal, meter, true), 'image/png', w, h, signal);
208
+ try { plain = patch && paste(design, patch, f.box); } catch { /* a box outside the frame: checked as failed */ }
209
+ if (!(await same(plain, f.plain))) plain = null;
210
+ }
211
+ if (!plain) {
212
+ switched = true;
213
+ plain = await pngFrame(base, 2 * s, f.plain, signal, meter);
214
+ }
215
+ meter.scenes += 1;
216
+ return { plain, design, switched };
217
+ }
web/index.html CHANGED
@@ -5,7 +5,7 @@
5
  <meta name="viewport" content="width=device-width, initial-scale=1">
6
  <title>QR Scan Difficulty Check</title>
7
  <meta name="description" content="Where does your QR code fall among thousands of measured codes? Add a picture: five open-source scanners read it at seven sizes, place it on the scale eight scanners set, and rate it Easy, Everyday, Tough or Advanced.">
8
- <link rel="stylesheet" href="style.css?v=2026-11-16">
9
  </head>
10
  <body>
11
  <main class="page">
@@ -19,6 +19,7 @@
19
  <p class="add-note" id="addNote" role="status">Read by five of the eight scanners. Nothing is kept.</p>
20
  <p class="visually-hidden" id="readNote" role="status"></p>
21
  <p class="visually-hidden" id="kindNote" role="status"></p>
 
22
  <input type="file" id="file" accept="image/*" multiple hidden>
23
  </div>
24
  </header>
@@ -56,13 +57,13 @@
56
  <summary>How your pictures are placed</summary>
57
  <div class="more-body method">
58
  <p>Each picture is read the way every measured code was. It is turned grey and shrunk, never enlarged, to seven sizes from 1920 down to 224 pixels on its long side, and five of the eight scanners read it at each size, with the versions and settings of the measurement: WeChat’s scanner, zxing-cpp, Android Settings’ scanner (ZXing) and OpenCV’s two detectors. Transparent parts count as white, as on a printed page. A scanner reads a picture when it returns the content that more of the five return than any other, even if only one returns any; a picture with several codes is placed by that one. How many of the five read a picture is counted at 1280 pixels, the long side of a 720p video frame.</p>
59
- <p>The other three, Apple’s, Google’s and Huawei’s, can’t run on this server: Apple’s runs only on Apple devices, and Google’s and Huawei’s, as measured, need Android. So a picture is placed from its 35 reads, five scanners at seven sizes, on the scale all eight set, which stays as measured. Tried on the measured codes that at least two of the five read at 1280 pixels, 9 in 10 land close to where all eight put them: within about a tenth of the way from clean to tough prints. <span id="howDashed"></span></p>
60
  <p><span id="howPileHi"></span> At some size, phones’ own scanners still read about 1 in 6 of the measured codes like that, most of them distorted research images, and 33 of the 59 photos and prints among them.</p>
61
  <p id="howLevels" hidden></p>
62
  <p id="howConfidence" hidden></p>
63
  <p id="howChecks" hidden></p>
64
  <p id="howSelector" hidden></p>
65
- <p>Pictures are read from a temporary file that is deleted as soon as the reading ends. The server keeps no copy and no access log. Hugging Face, which hosts it, has its own <a href="https://huggingface.co/privacy" target="_blank" rel="noopener">privacy policy</a>.</p>
66
  </div>
67
  </details>
68
  <details class="more" id="about">
@@ -96,6 +97,6 @@
96
  <div class="tooltip" id="tip" hidden></div>
97
  <div class="drop" id="drop" hidden><p>Drop to read</p></div>
98
  </main>
99
- <script type="module" src="app.js?v=2026-11-16"></script>
100
  </body>
101
  </html>
 
5
  <meta name="viewport" content="width=device-width, initial-scale=1">
6
  <title>QR Scan Difficulty Check</title>
7
  <meta name="description" content="Where does your QR code fall among thousands of measured codes? Add a picture: five open-source scanners read it at seven sizes, place it on the scale eight scanners set, and rate it Easy, Everyday, Tough or Advanced.">
8
+ <link rel="stylesheet" href="style.css?v=2026-11-16-2">
9
  </head>
10
  <body>
11
  <main class="page">
 
19
  <p class="add-note" id="addNote" role="status">Read by five of the eight scanners. Nothing is kept.</p>
20
  <p class="visually-hidden" id="readNote" role="status"></p>
21
  <p class="visually-hidden" id="kindNote" role="status"></p>
22
+ <p class="visually-hidden" id="testNote" role="status"></p>
23
  <input type="file" id="file" accept="image/*" multiple hidden>
24
  </div>
25
  </header>
 
57
  <summary>How your pictures are placed</summary>
58
  <div class="more-body method">
59
  <p>Each picture is read the way every measured code was. It is turned grey and shrunk, never enlarged, to seven sizes from 1920 down to 224 pixels on its long side, and five of the eight scanners read it at each size, with the versions and settings of the measurement: WeChat’s scanner, zxing-cpp, Android Settings’ scanner (ZXing) and OpenCV’s two detectors. Transparent parts count as white, as on a printed page. A scanner reads a picture when it returns the content that more of the five return than any other, even if only one returns any; a picture with several codes is placed by that one. How many of the five read a picture is counted at 1280 pixels, the long side of a 720p video frame.</p>
60
+ <p>The other three, Apple’s, Google’s and Huawei’s, can’t run on this server: Apple’s runs only on Apple devices, and Google’s and Huawei’s, as measured, need Android. So a picture is placed from its 35 reads, five scanners at seven sizes, on the scale all eight set, which stays as measured. Tried on the measured codes that at least two of the five read at 1280 pixels, 9 in 10 land close to where all eight put them: within about a tenth of the way from clean to tough prints. <span id="howDashed"></span> <span id="howTestScanners"></span></p>
61
  <p><span id="howPileHi"></span> At some size, phones’ own scanners still read about 1 in 6 of the measured codes like that, most of them distorted research images, and 33 of the 59 photos and prints among them.</p>
62
  <p id="howLevels" hidden></p>
63
  <p id="howConfidence" hidden></p>
64
  <p id="howChecks" hidden></p>
65
  <p id="howSelector" hidden></p>
66
+ <p>Pictures are read from a temporary file that is deleted as soon as the reading ends. The server keeps no copy and no access log. Hugging Face, which hosts it, has its own <a href="https://huggingface.co/privacy" target="_blank" rel="noopener">privacy policy</a>. <span id="howTestPrivacy"></span></p>
67
  </div>
68
  </details>
69
  <details class="more" id="about">
 
97
  <div class="tooltip" id="tip" hidden></div>
98
  <div class="drop" id="drop" hidden><p>Drop to read</p></div>
99
  </main>
100
+ <script type="module" src="app.js?v=2026-11-16-2"></script>
101
  </body>
102
  </html>
web/plan_view.js CHANGED
@@ -7,6 +7,8 @@ import { t } from './copy.js';
7
  import { panelVerdict, prefixText, rowVerdict } from './verdict.js';
8
  import { pairLines, pairWith } from './pair.js';
9
  import { madeHead, madeLines, refreshMade } from './made.js';
 
 
10
  import { loadCapacity } from './capacity.js';
11
  import { INCH, clean, parseInches } from './units.js';
12
  import {
@@ -44,6 +46,7 @@ export function planChanged(p, fallback) {
44
  const answer = planned && answerText(plan) !== before ? answerText(plan) : '';
45
  onChange();
46
  say([answer || fallback, suggestion(p), ...madeLines(p, plan)].filter(Boolean).join(' '));
 
47
  }
48
 
49
  // ---- "Your picture" -----------------------------------------------------------------------------------------------
@@ -55,6 +58,7 @@ export function planHead(p) {
55
  el('h2', { id: 'yoursTitle', class: 'plan-answer', tabindex: '-1' }),
56
  el('div', { class: 'plan-lines' }),
57
  madeHead(),
 
58
  el('div', { class: 'plan-tools' }, [distanceField(p), widthField(p), el('details', { class: 'more plan-how' }, [
59
  el('summary', { text: t('plan.how.summary') }), el('div', { class: 'more-body method' }),
60
  ])]),
@@ -76,6 +80,7 @@ export function refreshPlan(box, p) {
76
  const lines = answerLines(p, plan);
77
  box.querySelector('.plan-lines')?.replaceChildren(...lines.map((s, i) => el('p', { class: i === lines.length - 1 ? 'plan-assume' : '', text: s })));
78
  refreshMade(box, p, plan);
 
79
  const width = box.querySelector('.plan-width');
80
  if (width) width.hidden = !(p.kind === 'graphic' && plan.share);
81
  box.querySelector('.plan-how .more-body')?.replaceChildren(...howLines(plan).map((s) => el('p', { text: s })), howTable(plan));
 
7
  import { panelVerdict, prefixText, rowVerdict } from './verdict.js';
8
  import { pairLines, pairWith } from './pair.js';
9
  import { madeHead, madeLines, refreshMade } from './made.js';
10
+ import { refreshTest, testHead } from './test_view.js';
11
+ import { kindChanged } from './test.js';
12
  import { loadCapacity } from './capacity.js';
13
  import { INCH, clean, parseInches } from './units.js';
14
  import {
 
46
  const answer = planned && answerText(plan) !== before ? answerText(plan) : '';
47
  onChange();
48
  say([answer || fallback, suggestion(p), ...madeLines(p, plan)].filter(Boolean).join(' '));
49
+ kindChanged(p); // a design test settles, stops, or shows again
50
  }
51
 
52
  // ---- "Your picture" -----------------------------------------------------------------------------------------------
 
58
  el('h2', { id: 'yoursTitle', class: 'plan-answer', tabindex: '-1' }),
59
  el('div', { class: 'plan-lines' }),
60
  madeHead(),
61
+ testHead(p),
62
  el('div', { class: 'plan-tools' }, [distanceField(p), widthField(p), el('details', { class: 'more plan-how' }, [
63
  el('summary', { text: t('plan.how.summary') }), el('div', { class: 'more-body method' }),
64
  ])]),
 
80
  const lines = answerLines(p, plan);
81
  box.querySelector('.plan-lines')?.replaceChildren(...lines.map((s, i) => el('p', { class: i === lines.length - 1 ? 'plan-assume' : '', text: s })));
82
  refreshMade(box, p, plan);
83
+ refreshTest(box, p, plan);
84
  const width = box.querySelector('.plan-width');
85
  if (width) width.hidden = !(p.kind === 'graphic' && plan.share);
86
  box.querySelector('.plan-how .more-body')?.replaceChildren(...howLines(plan).map((s) => el('p', { text: s })), howTable(plan));
web/scanner.js ADDED
@@ -0,0 +1,262 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This browser's own scanner in a design test. Whether the browser has one the test can use is found once a page
2
+ // session, sending nothing (the probe and its working check), and it is named by platform. In a test it reads the
3
+ // scenes' rebuilt frames (frames.js), the plain code's and the design's, as they arrive, and stops once no reads of
4
+ // the scenes left could change its answer (the exact early stop), or when the scans come too slowly for the budget.
5
+ // The rule every scanner is judged by is here too, as the server has it. What this scanner reads stays in the page.
6
+ import { CAPTURE, FRAME, decodeGrey, ending, fetchScene, linked, sha256Hex, toBitmap } from './frames.js';
7
+
8
+ let T = null; // web/data/test.json (useTest)
9
+ let C = null; // binomial coefficients C[n][k], for n up to the test's scenes
10
+ const encoder = new TextEncoder();
11
+
12
+ /** test.js, before useTest: what the code relies on in test.json: whole numbers where it counts, the frame sizes the
13
+ * shrink is exact for, and a sign test whose alpha is one over a whole number. */
14
+ export function fits(s) {
15
+ const whole = (...xs) => xs.every((x) => Number.isInteger(x) && x >= 0);
16
+ const pair = (a, b) => Array.isArray(a) && a[0] === b[0] && a[1] === b[1];
17
+ const inv = 1 / s.rule.alpha;
18
+ return typeof s.page === 'boolean' && whole(s.scenes.s, s.rule.like_cost, s.rule.q, s.join, s.limits.max_version)
19
+ && s.scenes.s >= 1 && s.scenes.s <= 20 && Math.abs(inv - Math.round(inv)) < 1e-9 && pair(s.frames.capture, CAPTURE)
20
+ && pair(s.frames.frame, FRAME) && Boolean(s.check.png && s.check.jpeg && s.mb.jpeg && s.mb.png && s.figures);
21
+ }
22
+
23
+ /** test.js, once test.json is in: the settings everything here reads. */
24
+ export function useTest(settings) {
25
+ T = settings;
26
+ C = [[1]];
27
+ for (let n = 1; n <= T.scenes.s; n += 1) C.push(C[n - 1].map((v, k) => v + (C[n - 1][k - 1] || 0)).concat(1));
28
+ }
29
+
30
+ // ---- the rule, per scanner, over the paired scenes -----------------------------------------------------------------
31
+
32
+ /** The one-sided exact sign test on (b, c) under the rule's alpha, in whole numbers: sum over k = b..n of C(n, k),
33
+ * times 1 / alpha, under 2^n (n = b + c), so this and the server's never round differently. */
34
+ const under = (b, c) => Math.round(1 / T.rule.alpha) * C[b + c].slice(b).reduce((sum, v) => sum + v, 0) < 2 ** (b + c);
35
+
36
+ /** One scanner's answer: b counts the scenes where it read the plain code and missed the design, c the reverse, t
37
+ * and d the scenes where it read the plain code and the design. 'worse' when the sign test confirms the loss; 'like'
38
+ * (as well as a plain code) with a cost b - c of at most like_cost, and t or d at least q; otherwise it can't tell:
39
+ * 'close' with a higher cost, else 'few'. */
40
+ export function answer(b, c, t, d) {
41
+ const { like_cost: cost, q } = T.rule;
42
+ if (under(b, c)) return 'worse';
43
+ if (b - c <= cost && (t >= q || d >= q)) return 'like';
44
+ return b - c > cost ? 'close' : 'few';
45
+ }
46
+
47
+ /** The answer once no reads of the r scenes left could change it, else null: the exact early stop in closed form
48
+ * (equal to trying every way the scenes left could read). At r = 0 it is the answer itself. */
49
+ export function settled(b, c, t, d, r) {
50
+ const { like_cost: cost, q } = T.rule;
51
+ if (r === 0) return answer(b, c, t, d);
52
+ if (under(b, c + r)) return 'worse';
53
+ if ((t >= q || d >= q) && b - c + r <= cost) return 'like';
54
+ if (b - c - r > cost && !under(b + r, c)) return 'close';
55
+ if (b - c + r <= cost && t + r < q && d + r < q) return 'few';
56
+ return null;
57
+ }
58
+
59
+ // ---- the probe: once a page session, sending nothing ----------------------------------------------------------------
60
+
61
+ let probing = null;
62
+ let found = { state: 'pending' };
63
+
64
+ /** Whether this browser has a scanner the test can use: { state: 'ready', name: 'apple' | 'google', frames: 'jpeg' |
65
+ * 'png' }, 'none' with the step that failed (1: no BarcodeDetector here, 2: not Google Chrome on macOS or Android,
66
+ * 3: it lists no QR codes), 'broken' (the working check's read failed) or 'blocked' (this browser's canvas reads are
67
+ * altered or blocked). Run once a page session, when a picture is first marked The original graphic. */
68
+ export function probe() {
69
+ probing ||= steps().catch(() => ({ state: 'none', step: 1 })).then((r) => { found = r; return r; });
70
+ return probing;
71
+ }
72
+
73
+ /** The probe's result so far: { state: 'pending' } until it has one. */
74
+ export const probed = () => found;
75
+
76
+ async function steps() {
77
+ if (!(window.isSecureContext && 'BarcodeDetector' in window)) return { state: 'none', step: 1 };
78
+ if (!platformName()) return { state: 'none', step: 2 };
79
+ let formats = [];
80
+ try { formats = await BarcodeDetector.getSupportedFormats(); } catch { /* none listed */ }
81
+ if (!Array.isArray(formats) || !formats.includes('qr_code')) return { state: 'none', step: 3 };
82
+ return check();
83
+ }
84
+
85
+ /** After 'broken', a test's start runs the working check again (local, with the same two pictures), so the next test
86
+ * or Try again can bring the scanner back; any other result stands for the page session. */
87
+ export async function recheck() {
88
+ if (found.state === 'broken') found = await check();
89
+ return found;
90
+ }
91
+
92
+ /** 'apple' or 'google' for Google Chrome on macOS or on Android, from userAgentData's brands and platform (never its
93
+ * high-entropy hints, which the Hub's iframe may not pass), else ''. */
94
+ function platformName() {
95
+ const ua = navigator.userAgentData;
96
+ const { brand, platforms } = T.browsers;
97
+ const chrome = Array.isArray(ua?.brands) && ua.brands.some((b) => b.brand === brand);
98
+ return chrome && Object.hasOwn(platforms, ua.platform) ? platforms[ua.platform] : '';
99
+ }
100
+
101
+ /** Which words fit this browser: 'chrome-mac', 'chrome-android', 'mac' (a Mac, not Chrome: "Macintosh" with no touch
102
+ * points, so an iPad isn't taken for one) or 'other'. It only chooses words. */
103
+ export function platformKind() {
104
+ if (platformName()) return navigator.userAgentData.platform === 'Android' ? 'chrome-android' : 'chrome-mac';
105
+ return /Macintosh/.test(navigator.userAgent) && navigator.maxTouchPoints === 0 ? 'mac' : 'other';
106
+ }
107
+
108
+ /** Whether this browser's scanner can read a test's code, from the 202's two truths (the SHA-256 of its raw bytes, then
109
+ * of its text in UTF-8): in Chrome on Android only when they are equal, raw bytes that are UTF-8 text; elsewhere
110
+ * always. Chrome 113 on Android crashed its GPU process on every other code measured (detect rejected with
111
+ * NotSupportedError, each time), so trying again can't help there. */
112
+ export const readsContent = (truths) => platformKind() !== 'chrome-android' || truths[0] === truths[1];
113
+
114
+ /** Whether this browser asks to save data: read at every test's start, so turning it off and Try again bring the
115
+ * scanner in. */
116
+ export const saveDataOn = () => navigator.connection?.saveData === true;
117
+
118
+ /** What this browser's scanner does in a test (rec: test.js's record; joins: tests it joined by itself so far this
119
+ * page session), from the probe's result: { decision, name, frames }. It joins (with its frames: 'jpeg', or 'png'
120
+ * when this browser decodes JPEG differently); is offered, once a page session's first {join} tests have taken it
121
+ * by themselves, unless Add was pressed or this test's first decision was to join; is kept out by Save-Data; or
122
+ * isn't in the test: 'late' (the probe not done in time), 'broken', 'none' or 'blocked'. */
123
+ export function partFor(rec, joins, pr = found) {
124
+ const part = { name: pr.name || platformName(), frames: pr.frames || 'jpeg' };
125
+ if (pr.state !== 'ready') part.decision = pr.state === 'pending' ? 'late' : pr.state;
126
+ else if (saveDataOn()) part.decision = 'save_data';
127
+ else part.decision = rec.added || rec.first === 'joins' || joins < T.join ? 'joins' : 'offered';
128
+ return part;
129
+ }
130
+
131
+ /** At a test's start: the probe, waited for at most probe_wait_s, and after 'broken' the working check again. */
132
+ export async function decide(rec, joins) {
133
+ if (found.state === 'pending') await Promise.race([probe(), wait(T.page_waits.probe_wait_s * 1000)]);
134
+ if (found.state === 'broken') await recheck();
135
+ return partFor(rec, joins);
136
+ }
137
+
138
+ const bytesOf = (b64) => Uint8Array.from(atob(b64), (ch) => ch.charCodeAt(0));
139
+ const wait = (ms) => new Promise((resolve) => { setTimeout(resolve, ms); });
140
+
141
+ /** The working check, on test.json's two pictures, decoded along the frames' own path: the plain code's PNG must
142
+ * give its pinned grey bytes (else 'blocked': this browser's canvas reads are altered or blocked, as a readback that
143
+ * throws is), and a new detector must read its known content (else 'broken'); either is tried once more after
144
+ * check_retry_s. A JPEG that decodes to other bytes keeps the scanner, with PNG frames from the start. */
145
+ async function check() {
146
+ let got = await checkOnce();
147
+ if (got.state !== 'ready') {
148
+ await wait(T.page_waits.check_retry_s * 1000);
149
+ got = await checkOnce();
150
+ }
151
+ return got;
152
+ }
153
+
154
+ async function checkOnce() {
155
+ const { png, jpeg } = T.check;
156
+ let code;
157
+ let frames = 'jpeg';
158
+ try {
159
+ code = await decodeGrey(bytesOf(png.b64), 'image/png', png.w, png.h);
160
+ if ((await sha256Hex(code)) !== png.grey_sha256) return { state: 'blocked' };
161
+ const scene = await decodeGrey(bytesOf(jpeg.b64), 'image/jpeg', jpeg.w, jpeg.h);
162
+ if ((await sha256Hex(scene)) !== jpeg.grey_sha256) frames = 'png';
163
+ } catch {
164
+ return { state: 'blocked' };
165
+ }
166
+ try {
167
+ const detector = new BarcodeDetector({ formats: ['qr_code'] });
168
+ if (!(await reads(detector, code, png.w, png.h, [png.content_sha256]))) return { state: 'broken' };
169
+ } catch {
170
+ return { state: 'broken' };
171
+ }
172
+ return { state: 'ready', name: platformName(), frames };
173
+ }
174
+
175
+ /** Whether the detector reads one of truths (the content's UTF-8 SHA-256s) from grey bytes, through an ImageBitmap
176
+ * of them; throws when detect throws, or takes over detect_s. */
177
+ async function reads(detector, grey, w, h, truths) {
178
+ const bitmap = await toBitmap(grey, w, h);
179
+ const detecting = detector.detect(bitmap);
180
+ detecting.then(() => bitmap.close(), () => bitmap.close());
181
+ let timer = 0;
182
+ const late = new Promise((resolve, reject) => {
183
+ timer = setTimeout(() => reject(new Error('detect')), T.budget.detect_s * 1000);
184
+ });
185
+ try {
186
+ const codes = await Promise.race([detecting, late]);
187
+ for (const code of codes) if (truths.includes(await sha256Hex(encoder.encode(code.rawValue)))) return true;
188
+ return false;
189
+ } finally {
190
+ clearTimeout(timer);
191
+ }
192
+ }
193
+
194
+ // ---- the browser's part of one test --------------------------------------------------------------------------------
195
+
196
+ /** The browser's part of a test. opts: base (the test's routes), truths, mode ('jpeg' or 'png': the probe's frames),
197
+ * signal (aborted when the page leaves the test, or Leave it out is pressed), onScene(k) after each whole scene, and
198
+ * onMode('png') once the rest of the test takes PNG frames. One detector reads every frame; one scene is in hand and
199
+ * the next in flight. The budget counts only the pictures' fetches: after project_after_scenes scenes, and after
200
+ * each one from then on, the part stops when the time they took, projected over every scene from the link's own
201
+ * pace, passes fetch_s (it can't count on an early stop), and at once when they reach it. Resolves to { end, answer }:
202
+ * 'settled' (the early stop) or 'complete', with the scanner's answer; 'start', 'stopped' or 'slow' (a fallback);
203
+ * 'dropped' (the server no longer has the test); or 'aborted'. Never rejects. */
204
+ export async function runBrowserPart(opts) {
205
+ const n = T.scenes.s;
206
+ const [w, h] = FRAME;
207
+ const B = T.budget;
208
+ const meter = {
209
+ bytes: 0, ms: 0, scenes: 0, limit: B.fetch_s * 1000, tries: B.fetch_tries, wait: T.page_waits.scene_retry_s * 1000,
210
+ hold: 2 * T.server.scene_wait_s * 1000, // facts come within scene_wait_s, a 503 included: twice that is a stall
211
+ };
212
+ let detector;
213
+ try {
214
+ detector = new BarcodeDetector({ formats: ['qr_code'] });
215
+ } catch {
216
+ return { end: 'start' };
217
+ }
218
+ let mode = opts.mode;
219
+ const fetchOne = (i) => {
220
+ const ctl = linked(opts.signal);
221
+ const job = fetchScene(opts.base, i, mode, ctl.signal, meter);
222
+ job.catch(() => {}); // awaited in turn, or aborted unread
223
+ return { ctl, job };
224
+ };
225
+ let next = fetchOne(0);
226
+ let b = 0;
227
+ let c = 0;
228
+ let t = 0;
229
+ let d = 0;
230
+ try {
231
+ for (let i = 0; i < n; i += 1) {
232
+ const scene = await next.job;
233
+ if (scene.switched && mode === 'jpeg') {
234
+ mode = 'png';
235
+ opts.onMode('png');
236
+ }
237
+ next = i + 1 < n ? fetchOne(i + 1) : null; // in flight while this scene is read, so a switch wastes nothing
238
+ const plain = await reads(detector, scene.plain, w, h, opts.truths);
239
+ const design = await reads(detector, scene.design, w, h, opts.truths);
240
+ if (opts.signal.aborted) throw ending('aborted');
241
+ b += plain && !design;
242
+ c += design && !plain;
243
+ t += plain;
244
+ d += design;
245
+ const k = i + 1;
246
+ opts.onScene(k);
247
+ const a = settled(b, c, t, d, n - k);
248
+ if (a) {
249
+ next?.ctl.abort();
250
+ return { end: k === n ? 'complete' : 'settled', answer: a };
251
+ }
252
+ if (k >= B.project_after_scenes && (meter.ms * n) / Math.max(1, meter.scenes) > meter.limit) {
253
+ next?.ctl.abort();
254
+ return { end: 'slow' };
255
+ }
256
+ }
257
+ } catch (err) {
258
+ next?.ctl.abort();
259
+ return { end: err.end || (opts.signal.aborted ? 'aborted' : 'stopped') };
260
+ }
261
+ return { end: 'stopped' }; // not reached: the last scene always settles
262
+ }
web/style.css CHANGED
@@ -460,6 +460,7 @@ svg .level-name.on { fill: var(--ink); font-weight: 600; }
460
  /* on paper: the answer and what it rests on, without the controls or a "+" left closed */
461
  @media print {
462
  .plan-tools, .placement-suggest, .placement legend, .placement .opts, .placement-hint { display: none !important; }
 
463
  .pic-n { -webkit-print-color-adjust: exact; print-color-adjust: exact; } /* its disc prints, so its number shows */
464
  #yourPicture details:not([open]) > summary, .specimen details:not([open]) > summary { display: none !important; }
465
  }
@@ -475,7 +476,15 @@ svg .level-name.on { fill: var(--ink); font-weight: 600; }
475
  .made-more { margin-top: 10px; }
476
  .made-more summary { font-size: 14px; }
477
  .made-more .more-body { padding-top: 12px; }
478
- .made:not([hidden]) + .plan-tools { margin-top: 24px; } /* .more's space, so the block's "+" isn't read as a tool */
 
 
 
 
 
 
 
 
479
 
480
  /* Printing always uses the light theme's tokens: paper is light, whatever the screen's theme */
481
  @media print {
 
460
  /* on paper: the answer and what it rests on, without the controls or a "+" left closed */
461
  @media print {
462
  .plan-tools, .placement-suggest, .placement legend, .placement .opts, .placement-hint { display: none !important; }
463
+ .test .test-state.running, .test-hint, .test-size, .test-acts, .test button { display: none !important; }
464
  .pic-n { -webkit-print-color-adjust: exact; print-color-adjust: exact; } /* its disc prints, so its number shows */
465
  #yourPicture details:not([open]) > summary, .specimen details:not([open]) > summary { display: none !important; }
466
  }
 
476
  .made-more { margin-top: 10px; }
477
  .made-more summary { font-size: 14px; }
478
  .made-more .more-body { padding-top: 12px; }
479
+ .made:not([hidden]) + .plan-tools, .made:not([hidden]) + .test[hidden] + .plan-tools,
480
+ .test:not([hidden]) + .plan-tools { margin-top: 24px; } /* .more's space, so the block's "+" isn't read as a tool */
481
+ /* a file's design test, under its as-made lines (test_view.js): its state or answer, line 2, then "+" */
482
+ .test { margin-top: 14px; max-width: 46em; }
483
+ .test p { margin: 8px 0 0; font-size: 15px; color: var(--ink-2); }
484
+ .test .test-state { margin-top: 0; color: var(--ink); }
485
+ .test-more { margin-top: 10px; }
486
+ .test-more summary { font-size: 14px; }
487
+ .test-more .more-body { padding-top: 12px; }
488
 
489
  /* Printing always uses the light theme's tokens: paper is light, whatever the screen's theme */
490
  @media print {
web/test.js ADDED
@@ -0,0 +1,292 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // The design test, for each picture marked The original graphic. Once the choice has stood for the settle time, the
2
+ // test joins the page's queue and runs by itself, one test at a time, in the order the pictures were marked; its turn
3
+ // with the server is test_turn.js's, this browser's scanner scanner.js's, and test_view.js shows it. Marking the
4
+ // picture otherwise, or removing it, stops its test; an answer is kept, and shows again when the picture is marked
5
+ // The original graphic again. Nothing is stored, and what this browser's scanner read stays in the page.
6
+ import { getJson, t } from './copy.js';
7
+ import { planFor } from './plan.js';
8
+ import { hidden } from './frames.js';
9
+ import { decide, fits, partFor, probe, probed, useTest } from './scanner.js';
10
+ import { fetchHealth, late, leave, refusal, turn, useTurn } from './test_turn.js';
11
+ import { announce, howTest, paintTest, useView } from './test_view.js';
12
+
13
+ let S = null; // web/data/test.json, or null: the page starts no test
14
+ let health = 'unknown'; // the server's test, as /api/health says: 'unknown' (not read, or the read failed), 'on', 'off'
15
+ let reading = null; // the health read in flight
16
+ let joins = 0; // tests this browser's scanner joined by itself this page session, up to S.join
17
+ const queue = []; // the pictures whose tests wait their turn, in order
18
+ let active = null; // the picture whose test has the page's turn
19
+ let lastEnd = 0; // when the page's last test ended
20
+ let wake = 0; // the queue's timer
21
+
22
+ /** app.js, at the start: the test's settings. Never throws: a missing or broken file leaves the test off. */
23
+ export async function loadTest(url) {
24
+ try {
25
+ const s = await getJson(url);
26
+ if (!fits(s)) return;
27
+ S = s;
28
+ useTest(s);
29
+ useTurn(s, { paint, tell, joined: () => { joins += 1; queue.forEach(paint); } });
30
+ useView(s, { again: retryTest, add: addScanner, leave: leaveOut });
31
+ } catch { /* no test: the page loads as before */ }
32
+ }
33
+
34
+ /** Whether the page runs design tests: test.json says so, and the server's test isn't off. */
35
+ export const testOn = () => S?.page === true && health !== 'off';
36
+ /** yours.js: the choice's hint says a test sends the picture again, only while tests run. */
37
+ export const testChoiceHint = () => (testOn() ? t('test.choice_hint') : '');
38
+ /** A picture's test record, or null. */
39
+ export const testOf = (p) => p.test || null;
40
+
41
+ /** app.js, once the words are in: "How your pictures are placed" says a test sends the picture again (only while
42
+ * tests run); a page closed, reloaded or put in the back-forward cache leaves its test on the server, and a page
43
+ * shown again from that cache runs its queue again. Time the page spends frozen (as Chrome freezes a tab in the
44
+ * background), when it doesn't run, is left out of the budget for this browser's fetches (frames.js); a page hidden
45
+ * but running keeps fetching at the link's own pace, so that time counts. */
46
+ export function initTest() {
47
+ if (!S?.page) return;
48
+ howText();
49
+ window.addEventListener('pagehide', leavePage);
50
+ window.addEventListener('pageshow', (e) => { if (e.persisted) pump(); });
51
+ const away = (e) => hidden(e.type === 'freeze');
52
+ for (const type of ['freeze', 'resume']) window.addEventListener(type, away, { capture: true });
53
+ }
54
+
55
+ const howText = () => howTest(testOn());
56
+
57
+ /** app.js, after pictures are added: the server's test is read once, with the first picture. */
58
+ export function testAdded() {
59
+ if (S?.page && health === 'unknown' && !reading) readHealth();
60
+ }
61
+
62
+ function readHealth() {
63
+ reading = fetchHealth()
64
+ .then((h) => { if (h) health = h.design?.on === true ? 'on' : 'off'; }, () => { /* read again next time */ })
65
+ .then(() => {
66
+ reading = null;
67
+ if (health === 'off') {
68
+ howText();
69
+ for (const p of [...queue]) drop(p);
70
+ }
71
+ return health;
72
+ });
73
+ return reading;
74
+ }
75
+ const healthNow = () => (health !== 'unknown' ? Promise.resolve(health) : reading || readHealth());
76
+
77
+ /** plan_view.js, after what a picture shows changed (the change's own words said first). The original graphic: its
78
+ * test starts once the choice has stood for the settle time (arrowing past it sends nothing), or its kept answer
79
+ * shows again. Anything else: a test settling, waiting or running stops, and the server drops it; an answer or a
80
+ * can't line is kept, hidden. */
81
+ export function kindChanged(p) {
82
+ if (!S?.page) return;
83
+ if (p.kind === 'graphic' && !p.test) {
84
+ const rec = (p.test = fresh({ by: 'visitor' }, 'settling'));
85
+ rec.timer = setTimeout(() => gate(p, rec), S.page_waits.settle_s * 1000);
86
+ startProbe();
87
+ } else if (p.kind !== 'graphic' && p.test && !['done', 'cant'].includes(p.test.state)) {
88
+ stop(p);
89
+ pump();
90
+ }
91
+ paint(p);
92
+ }
93
+
94
+ /** app.js, when a picture's reading returns: one already marked The original graphic (read again, or marked by its
95
+ * own record) is tested at once, with no settle time. */
96
+ export function readingReturned(p) {
97
+ if (!S?.page || p.kind !== 'graphic' || p.status !== 'done' || p.test) return;
98
+ const rec = (p.test = fresh({ by: 'reading' }, 'settling'));
99
+ startProbe();
100
+ gate(p, rec);
101
+ }
102
+
103
+ /** app.js, when a picture is removed, or read again (its reading may change): its test stops, the server drops it,
104
+ * and nothing is kept. */
105
+ export function pictureRemoved(p) {
106
+ if (!p.test) return;
107
+ stop(p);
108
+ pump();
109
+ }
110
+
111
+ /** The probe runs once a page session, when a picture is first marked The original graphic and the server's test is
112
+ * on; tests that wait then show what this browser's scanner will do. */
113
+ function startProbe() {
114
+ healthNow().then((h) => { if (h === 'on') probe().then(() => queue.forEach(paint)); });
115
+ }
116
+
117
+ /** The settle time has passed, or a reading returned: no test (and no line) without a width planned from a scanner's
118
+ * version, a kept original, or the server's test on; while /api/health can't be read, the unreachable line, with Try
119
+ * again; a line for the page's own refusals; else the queue. */
120
+ async function gate(p, rec) {
121
+ const plan = planFor(p);
122
+ const fit = p.kind === 'graphic' && p.file && plan && plan.mode !== 'cant' && !plan.estimated;
123
+ const h = fit ? await healthNow() : 'off';
124
+ if (p.test !== rec) return;
125
+ if (h !== 'on') {
126
+ if (h === 'off') return drop(p);
127
+ Object.assign(rec, { state: 'failed', line: t('errors.unreachable'), retry: true });
128
+ tell(p, rec, 'end');
129
+ return paint(p);
130
+ }
131
+ rec.why = refusal(p.result);
132
+ rec.state = rec.why ? 'cant' : 'queued';
133
+ if (rec.why) tell(p, rec, 'end');
134
+ else {
135
+ queue.push(p);
136
+ if (active || queue.length > 1) tell(p, rec, 'queued');
137
+ }
138
+ paint(p);
139
+ pump();
140
+ }
141
+
142
+ /** The queue's first test starts when none of the page's has its turn, between_tests_s after the last one ended. */
143
+ function pump() {
144
+ clearTimeout(wake);
145
+ if (!S || active || !queue.length) return;
146
+ const left = lastEnd + S.page_waits.between_tests_s * 1000 - Date.now();
147
+ if (left > 0) {
148
+ wake = setTimeout(pump, left);
149
+ return;
150
+ }
151
+ active = queue.shift();
152
+ run(active, active.test);
153
+ }
154
+
155
+ /** A test's turn: what this browser's scanner does in it (the probe waited for at most probe_wait_s), then the
156
+ * server's part (test_turn.js), then its end. */
157
+ async function run(p, rec) {
158
+ rec.ctl = new AbortController();
159
+ rec.state = 'slot';
160
+ paint(p);
161
+ let out;
162
+ try {
163
+ rec.part = await decide(rec, joins);
164
+ rec.first ||= rec.part.decision; // kept when the test starts again
165
+ rec.decided = true;
166
+ out = await turn(p, rec, rec.ctl.signal);
167
+ } catch (err) { // a failure in the page's words (upload.js), or the server unreachable
168
+ const line = err.retry === undefined ? t('errors.unreachable') : err.message;
169
+ out = { state: 'failed', line, retry: err.retry !== false };
170
+ }
171
+ if (p.test === rec && !rec.ctl.signal.aborted) end(p, rec, out);
172
+ }
173
+
174
+ /** A turn ends: an answer, a can't line or a failure, said once; or the test waits at the queue's head again (a 404
175
+ * or a lost slot, the first time), or no test runs (the server's test is off). Then the next test's turn. */
176
+ function end(p, rec, out) {
177
+ rec.ctl.abort();
178
+ leave(rec);
179
+ active = null;
180
+ lastEnd = Date.now();
181
+ if (out.off) {
182
+ health = 'off';
183
+ howText();
184
+ for (const q of [p, ...queue]) drop(q);
185
+ } else if (out.redo && rec[out.redo] < 1) {
186
+ p.test = fresh({ ...rec, [out.redo]: rec[out.redo] + 1 }, 'queued');
187
+ queue.unshift(p);
188
+ } else {
189
+ Object.assign(rec, out.redo ? late() : out);
190
+ tell(p, rec, 'end');
191
+ }
192
+ paint(p);
193
+ pump();
194
+ }
195
+
196
+ /** Show the test; one that hasn't started shows what this browser's scanner would do in it. */
197
+ function paint(p) {
198
+ const rec = p.test;
199
+ if (S && rec && !rec.decided && rec.state !== 'settling') rec.part = partFor(rec, joins, probed());
200
+ paintTest(p);
201
+ }
202
+
203
+ /** testNote: after the visitor's own marking, the test's first state unnamed (the marking's words were just said);
204
+ * then, naming the picture, a test from the queue when it starts or is told busy (once), and each test's last line. */
205
+ function tell(p, rec, what) {
206
+ if ((what === 'start' && rec.said && (!rec.waited || rec.busy)) || (what === 'busy' && rec.busy)) return;
207
+ announce(p, what, rec.said || rec.by !== 'visitor');
208
+ rec.said = true;
209
+ rec.waited ||= what === 'queued';
210
+ rec.busy ||= what === 'busy';
211
+ }
212
+
213
+ /** A new test record: one started again keeps who started it, its first decision, whether it counted towards the
214
+ * bound, Add, what was said, and how often the server dropped it or its slot. */
215
+ const fresh = (from, state) => ({
216
+ state, by: from.by, first: from.first, counted: from.counted, added: from.added, said: from.said, busy: from.busy,
217
+ drops: from.drops || 0, conflicts: from.conflicts || 0,
218
+ });
219
+
220
+ /** A test left: its timer and requests stopped, what the server holds dropped, nothing kept. */
221
+ function stop(p) {
222
+ const rec = p.test;
223
+ clearTimeout(rec.timer);
224
+ rec.ctl?.abort();
225
+ leave(rec);
226
+ if (queue.includes(p)) queue.splice(queue.indexOf(p), 1);
227
+ if (active === p) {
228
+ active = null;
229
+ lastEnd = Date.now();
230
+ }
231
+ p.test = null;
232
+ }
233
+ function drop(p) {
234
+ if (p.test) stop(p);
235
+ paint(p);
236
+ }
237
+
238
+ /** Try again and Add: the test starts afresh at the end of the queue, its old one left on the server; test_view.js
239
+ * then moves focus to its state line. A new test, so its busy line is said again if it is told busy. */
240
+ function restart(p, rec, extra) {
241
+ stop(p);
242
+ const waits = Boolean(active) || queue.length > 0;
243
+ p.test = { ...fresh(rec, 'queued'), drops: 0, conflicts: 0, said: true, busy: false, waited: waits, ...extra };
244
+ queue.push(p);
245
+ paint(p);
246
+ pump();
247
+ }
248
+
249
+ /** Try again (after a fallback, Save-Data, the deadline, busy or an unreachable server): where line 2 asked to try
250
+ * again for this browser's scanner, it comes back, as with Add (never for a part only shown before the test ran). */
251
+ export function retryTest(p) {
252
+ const rec = p.test;
253
+ const again = Boolean(rec?.decided) && ['save_data', 'late', 'broken'].includes(rec.part?.decision);
254
+ if (rec) restart(p, rec, { added: rec.added || again });
255
+ }
256
+
257
+ /** Add {name}: before the picture is sent, the test takes this browser's scanner; after, it starts again with it. */
258
+ export function addScanner(p) {
259
+ const rec = p.test;
260
+ if (!rec) return;
261
+ if (!['queued', 'slot', 'busy'].includes(rec.state)) {
262
+ restart(p, rec, { added: true });
263
+ return;
264
+ }
265
+ rec.added = true;
266
+ if (rec.decided && rec.part.decision === 'offered') rec.part.decision = 'joins';
267
+ paint(p);
268
+ }
269
+
270
+ /** Leave it out: the browser's part ends at once (its fetches aborted, the polls then carry frames=0), and the test
271
+ * finishes with zxing-cpp's and WeChat's answers. */
272
+ export function leaveOut(p) {
273
+ const part = p.test?.part;
274
+ if (!part?.running) return;
275
+ part.left = true;
276
+ part.ctl.abort();
277
+ paint(p);
278
+ }
279
+
280
+ /** pagehide: the active test is left on the server (keepalive, so it goes as the page goes) and waits at the queue's
281
+ * head; a page shown again from the back-forward cache runs it again (pageshow). */
282
+ function leavePage() {
283
+ const p = active;
284
+ if (!p) return;
285
+ const rec = p.test;
286
+ rec.ctl?.abort();
287
+ leave(rec, true);
288
+ active = null;
289
+ p.test = fresh(rec, 'queued');
290
+ queue.unshift(p);
291
+ paint(p);
292
+ }
web/test_turn.js ADDED
@@ -0,0 +1,252 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // One design test's turn with the server (api/design): a slot for the test worker, asked for before any picture is
2
+ // sent (a ticket keeps the page's place while another design is tested); the picture sent under it, as for a reading
3
+ // (upload.js); the server's facts checked against the first reading's; then the polls, about once a second, while
4
+ // this browser's scanner, where it joins, reads the test's scenes here (scanner.js). It resolves to how the turn
5
+ // ended; test.js keeps the queue and shows it. The server only ever answers with words: never reads or counts.
6
+ import { t } from './copy.js';
7
+ import { ending, linked, sleep } from './frames.js';
8
+ import { readsContent, runBrowserPart } from './scanner.js';
9
+ import { failure, prepare, upload } from './upload.js';
10
+
11
+ const ID = /^[0-9a-f]{32}$/; // the server's ids, for tests, slots and tickets alike
12
+ const HEX = /^[0-9a-f]{64}$/;
13
+ const WHY = ['facts', 'version', 'small', 'big', 'inverted', 'mirrored', 'several', 'twin']; // the server's refusals
14
+ const ANSWERS = ['like', 'worse', 'close', 'few'];
15
+
16
+ let S = null; // web/data/test.json
17
+ let on = null; // test.js: { paint(p), tell(p, rec, what), joined() }
18
+
19
+ /** test.js, once test.json is in. */
20
+ export function useTurn(settings, hooks) {
21
+ S = settings;
22
+ on = hooks;
23
+ }
24
+
25
+ /** A turn that ends without an answer: the deadline's line, with Try again. */
26
+ export const late = () => ({ state: 'failed', line: t('test.late'), retry: true });
27
+
28
+ /** The page's own reasons a picture can't be tested, in order, or '': zxing-cpp's error correction, mask or raw bytes
29
+ * unknown; light on dark; under {min} pixels per square in the file; over max_pixels; over max_version. (A record's
30
+ * square check would add "mirrored" after light on dark.) The server checks each again itself. */
31
+ export function refusal(r) {
32
+ const c = r.checks;
33
+ const L = S.limits;
34
+ const mask = Number.isInteger(r.mask) && r.mask >= 0 && r.mask <= 7;
35
+ if (!['L', 'M', 'Q', 'H'].includes(r.ec_level) || !mask || !HEX.test(r.raw_sha256 || '')) return 'facts';
36
+ if (c.contrast?.inverted === true) return 'inverted';
37
+ if (Number.isFinite(c.ppm?.native) && c.ppm.native < (c.version === 1 ? L.min_ppm_v1 : L.min_ppm)) return 'small';
38
+ if (r.size?.[0] * r.size?.[1] > L.max_pixels) return 'big'; // no size known: the server checks it
39
+ return c.version > L.max_version ? 'version' : '';
40
+ }
41
+
42
+ /** DELETE what the server holds for a test, whichever is live: its test, its slot or its ticket (keepalive as the
43
+ * page goes). The server drops it at once, and a running test's worker stops after the scene in hand. */
44
+ export function leave(rec, keepalive = false) {
45
+ for (const id of [rec.id, rec.slot, rec.ticket]) {
46
+ if (id) fetch(`api/design/${id}`, { method: 'DELETE', keepalive, cache: 'no-store' }).catch(() => {});
47
+ }
48
+ rec.id = rec.slot = rec.ticket = null;
49
+ }
50
+
51
+ const body = (res) => res.json().catch(() => null);
52
+ async function call(url, signal, method = 'GET') {
53
+ try {
54
+ return await fetch(url, { method, signal, cache: 'no-store' });
55
+ } catch (err) {
56
+ if (signal.aborted) throw err;
57
+ throw failure(t('errors.unreachable'), true);
58
+ }
59
+ }
60
+
61
+ /** request(signal)'s response and its body read, [res, j], within s seconds (test.json): past them, as on a network
62
+ * failure, the server can't be reached, with Try again, and no progress line is left running. The page leaving the
63
+ * test still rejects as aborted. */
64
+ async function within(s, signal, request) {
65
+ const ctl = linked(signal);
66
+ const timer = setTimeout(() => ctl.abort(), s * 1000);
67
+ try {
68
+ const res = await request(ctl.signal);
69
+ const j = await body(res);
70
+ if (signal.aborted || !ctl.signal.aborted) return [res, j];
71
+ } catch (err) {
72
+ if (signal.aborted || !ctl.signal.aborted) throw err;
73
+ } finally {
74
+ clearTimeout(timer);
75
+ }
76
+ throw failure(t('errors.unreachable'), true);
77
+ }
78
+
79
+ /** test.js: /api/health's answer, or null, within the slot's hold (the server answers at once); past it, a failure. */
80
+ export const fetchHealth = () => within(S.server.slot_hold_s, new AbortController().signal,
81
+ (s) => fetch('api/health', { cache: 'no-store', signal: s })).then(([res, j]) => (res.ok ? j : null));
82
+
83
+ /** One test's turn, once rec.part says what this browser's scanner does in it. Resolves to the turn's end: { state:
84
+ * 'done', answers }, { state: 'cant', why }, { state: 'failed', line, retry }, { redo: 'drops' | 'conflicts' } (the
85
+ * server dropped the test, or its slot), or { off: true }. Throws a failure (upload.js) when the server can't be
86
+ * reached, and rejects as aborted when the page leaves the test. The page's own refusals come first, at every turn:
87
+ * Try again after the unreachable line can come before they were checked. */
88
+ export async function turn(p, rec, signal) {
89
+ const why = refusal(p.result);
90
+ if (why) return { state: 'cant', why };
91
+ const blob = await prepare(p.file); // checked and converted first: a slow conversion never uses up the slot's hold
92
+ const slot = await askSlot(p, rec, signal);
93
+ if (!slot.id) return slot;
94
+ rec.state = 'upload';
95
+ on.paint(p);
96
+ const frames = rec.part.decision === 'joins' ? `&frames=${rec.part.frames}` : '';
97
+ const url = `api/design?slot=${slot.id}${frames}`;
98
+ const [res, j] = await within(S.page_waits.upload_s, signal, (s) => upload(url, p.file, s, blob));
99
+ rec.slot = null; // used up, whatever the answer: until it is read, leaving DELETEs the slot, and the test with it
100
+ if (signal.aborted) throw ending('aborted');
101
+ if (res.status === 202 && ID.test(j?.test)) return started(p, rec, j, signal);
102
+ if (res.status === 200 && j?.verdict === 'cant' && WHY.includes(j.why)) { // the reading had its facts: 'differ'
103
+ return { state: 'cant', why: j.why === 'facts' ? 'differ' : j.why };
104
+ }
105
+ if (res.status === 409) return { redo: 'conflicts' };
106
+ if ([400, 413, 415, 422].includes(res.status)) return { state: 'failed', line: t('errors.unreadable'), retry: false };
107
+ return late();
108
+ }
109
+
110
+ /** The test worker for this page: a slot, or, while another design is tested, the busy line and a ticket that keeps
111
+ * the page's place, asked again after Retry-After (without the picture) for up to busy_total_s. The server answers
112
+ * each request at once, so each is given the slot's own hold: a slot that came any later would have lapsed. */
113
+ async function askSlot(p, rec, signal) {
114
+ let first = 0;
115
+ for (;;) {
116
+ const url = `api/design/slot${rec.ticket ? `?ticket=${rec.ticket}` : ''}`;
117
+ const [res, j] = await within(S.server.slot_hold_s, signal, (s) => call(url, s, 'POST'));
118
+ if (res.status === 200 && ID.test(j?.slot)) {
119
+ rec.ticket = null; // used up by the slot
120
+ rec.slot = j.slot;
121
+ on.tell(p, rec, 'start');
122
+ return { id: j.slot };
123
+ }
124
+ if (res.status === 503 && j?.error === 'off') return { off: true };
125
+ if (res.status !== 503 || j?.error !== 'busy') return late();
126
+ rec.ticket = ID.test(j.ticket) ? j.ticket : null;
127
+ const [lo, hi] = S.server.retry_s;
128
+ const header = Number(res.headers.get('Retry-After'));
129
+ rec.n = Math.min(hi, Math.max(lo, Number.isInteger(header) && header > 0 ? header : Number(j.retry_s) || hi));
130
+ first ||= Date.now();
131
+ if (Date.now() + rec.n * 1000 - first > S.page_waits.busy_total_s * 1000) {
132
+ leave(rec);
133
+ return { state: 'failed', line: t('test.waiting'), retry: true };
134
+ }
135
+ rec.state = 'busy';
136
+ on.tell(p, rec, 'busy');
137
+ on.paint(p);
138
+ await sleep(rec.n * 1000, signal);
139
+ }
140
+ }
141
+
142
+ /** Started: the server's facts must be the first reading's (version, error correction, mask and raw bytes), or the
143
+ * test read its code differently; then the server's part, and this browser's where it joins with its scenes. A code
144
+ * this browser's scanner can't read (scanner.js readsContent) keeps it out, whatever it would have done: line 2 then
145
+ * says this browser has none the test can use, with no Try again. */
146
+ async function started(p, rec, j, signal) {
147
+ rec.id = j.test;
148
+ const r = p.result;
149
+ const same = j.version === r.checks.version && j.ec_level === r.ec_level && j.mask === r.mask;
150
+ if (!same || !Array.isArray(j.truths) || j.truths[0] !== r.raw_sha256) {
151
+ leave(rec);
152
+ return { state: 'cant', why: 'differ' };
153
+ }
154
+ const part = rec.part;
155
+ if (!readsContent(j.truths)) part.decision = 'none'; // so nothing is fetched or read for it, and it isn't counted
156
+ const join = part.decision === 'joins';
157
+ if (join && !(j.scenes === S.scenes.s && j.frames === part.frames)) part.end = 'start'; // not given its scenes
158
+ else if (join && !rec.added && !rec.counted) {
159
+ rec.counted = true;
160
+ on.joined(); // by itself: towards the page session's bound
161
+ }
162
+ rec.state = 'running';
163
+ rec.done = 0;
164
+ const browser = join && !part.end ? browserPart(p, rec, j) : null;
165
+ part.over = !browser;
166
+ on.paint(p);
167
+ return watch(p, rec, browser, signal);
168
+ }
169
+
170
+ /** This browser's part: its scanner reads the scenes as they come; once it is over, the polls carry frames=0. */
171
+ async function browserPart(p, rec, j) {
172
+ const part = rec.part;
173
+ part.ctl = new AbortController();
174
+ rec.ctl.signal.addEventListener('abort', () => part.ctl.abort(), { once: true });
175
+ part.running = true;
176
+ part.k = 0;
177
+ const out = await runBrowserPart({
178
+ base: `api/design/${j.test}`, truths: j.truths.filter((x) => HEX.test(x)), mode: part.frames,
179
+ signal: part.ctl.signal,
180
+ onScene: (k) => { part.k = k; on.paint(p); },
181
+ onMode: (m) => { part.frames = m; on.paint(p); },
182
+ });
183
+ const cut = part.end; // watch() stopped it first: it outlived the test
184
+ const end = cut || (part.left && out.end === 'aborted' ? 'left' : out.end);
185
+ Object.assign(part, { running: false, over: true, end, answer: cut ? null : out.answer || null });
186
+ on.paint(p);
187
+ }
188
+
189
+ /** Whether job (the browser's part) ended before signal aborted. */
190
+ const ended = (job, signal) => new Promise((resolve) => {
191
+ if (signal.aborted) resolve(false);
192
+ signal.addEventListener('abort', () => resolve(false), { once: true });
193
+ job.then(() => resolve(true), () => resolve(true));
194
+ });
195
+
196
+ /** The polls until the server's answers come; the last one carries frames=0, so the server keeps nothing more. A 404
197
+ * (the server dropped the test: with its answers in hand, the turn ends with them, unless the browser's part found
198
+ * its scenes dropped too), "late", or the browser's part finding the test dropped, ends the turn. Nothing waits past
199
+ * the test's life, the server's own time for it (deadline_s + after_deadline_s from its start): a poll that hasn't
200
+ * come back by then gives the deadline's line, and a browser's part still reading, with the server's answers in,
201
+ * stops partway (end() then leaves the test). A page that didn't run for over idle_s (frozen in the background:
202
+ * the longest gap between its steps here) finds the test dropped, as its next poll would. */
203
+ async function watch(p, rec, browser, signal) {
204
+ const part = rec.part;
205
+ const life = linked(signal);
206
+ const timer = setTimeout(() => life.abort(), (S.server.deadline_s + S.server.after_deadline_s) * 1000);
207
+ let answers = null;
208
+ let tick = Date.now();
209
+ let gap = 0;
210
+ const beat = () => { gap = Math.max(gap, Date.now() - tick); tick = Date.now(); };
211
+ try {
212
+ for (;;) {
213
+ const zero = part.over;
214
+ const res = await call(`api/design/${rec.id}${zero ? '?frames=0' : ''}`, life.signal);
215
+ beat();
216
+ if (res.status === 404) return answers && part.end !== 'dropped' ? { state: 'done', answers } : { redo: 'drops' };
217
+ const j = await body(res);
218
+ if (j?.state === 'late') { // read before a scene's 404: the server drops the scenes of a test it has lost
219
+ rec.id = null; // dropped by the server with this answer
220
+ return late();
221
+ }
222
+ if (part.end === 'dropped') return { redo: 'drops' };
223
+ if (j?.state === 'running' && Number.isInteger(j.done)) rec.done = j.done;
224
+ if (j?.state === 'done') {
225
+ if (!ANSWERS.includes(j.scanners?.['zxing-cpp']) || !ANSWERS.includes(j.scanners?.wechat)) return late();
226
+ answers = j.scanners;
227
+ rec.done = S.scenes.s;
228
+ if (zero) {
229
+ rec.id = null; // dropped by the server with the frames=0 poll that gave its answers
230
+ return { state: 'done', answers };
231
+ }
232
+ // the scenes stay on the server until the browser's part is over: then one last poll
233
+ if (!(await ended(browser, life.signal))) throw ending('aborted');
234
+ continue;
235
+ }
236
+ on.paint(p);
237
+ await sleep(S.page_waits.poll_s * 1000, life.signal);
238
+ beat();
239
+ }
240
+ } catch (err) {
241
+ if (signal.aborted || !life.signal.aborted) throw err;
242
+ beat();
243
+ if (!answers || part.end === 'dropped') return gap > S.server.idle_s * 1000 ? { redo: 'drops' } : late();
244
+ if (part.running) {
245
+ Object.assign(part, { end: 'stopped', running: false, over: true });
246
+ part.ctl.abort();
247
+ }
248
+ return { state: 'done', answers };
249
+ } finally {
250
+ clearTimeout(timer);
251
+ }
252
+ }
web/test_view.js ADDED
@@ -0,0 +1,238 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // The design test's block in "Your picture", under a file's as-made lines (plan_view.js mounts it): while the test
2
+ // waits or runs, how far it has got, what it does and what this browser downloads for it; then its answer, why a
3
+ // phone's scanner wasn't in it, and behind "+", how it was tested. The test's state is test.js's; the words come from
4
+ // copy.json, every number from test.json or sizes.json. testNote says what changed once, and never the counter.
5
+ import { $, WORDS, count, el, join } from './util.js';
6
+ import { t } from './copy.js';
7
+ import { needFor, planFor } from './plan.js';
8
+ import { platformKind } from './scanner.js';
9
+
10
+ const SERVER = ['zxing-cpp', 'wechat']; // the server's scanners, in the order the lines name them
11
+ const WAITING = ['queued', 'slot', 'busy', 'upload', 'running'];
12
+ let S = null; // web/data/test.json
13
+ let act = null; // test.js: { again(p), add(p), leave(p) }
14
+
15
+ /** test.js, once test.json is in. */
16
+ export function useView(settings, actions) {
17
+ S = settings;
18
+ act = actions;
19
+ }
20
+
21
+ /** test.js: the "How your pictures are placed" lines that say a test sends the picture again, only while tests run
22
+ * (on); otherwise the choice's sentence as before. */
23
+ export function howTest(on) {
24
+ const fill = (id, text) => { const node = document.getElementById(id); if (node) node.textContent = text; };
25
+ fill('howTestScanners', on ? t('test.how_scanners') : '');
26
+ fill('howTestPrivacy', on ? t('test.privacy') : '');
27
+ const s = t(on ? 'how.index_html.selector_test' : 'how.index_html.selector');
28
+ if (s) fill('howSelector', s);
29
+ }
30
+
31
+ /** plan_view.js planHead: the block, hidden until the test has something to show, made once per section so that an
32
+ * open "+" stays open. Try again, Add and Leave it out are real buttons; each moves focus to the line that takes its
33
+ * place, which has tabindex -1, and the size line's two are described by its text. */
34
+ export function testHead(p) {
35
+ if (!S?.page) return null;
36
+ const size = `testSize-${p.n}`;
37
+ const button = (cls, what, about) => {
38
+ const b = el('button', { type: 'button', class: `linklike ${cls}`, hidden: '' });
39
+ if (about) b.setAttribute('aria-describedby', about);
40
+ b.addEventListener('click', () => { act[what](p); focusState(p); });
41
+ return b;
42
+ };
43
+ return el('div', { class: 'test', 'data-n': String(p.n), hidden: '' }, [
44
+ el('p', { class: 'test-state', id: `testState-${p.n}`, tabindex: '-1' }),
45
+ el('p', { class: 'test-hint', hidden: '' }),
46
+ el('p', { class: 'test-size', hidden: '' }, [
47
+ el('span', { class: 'test-size-text', id: size }), ' ', button('test-offer', 'add', size), ' ',
48
+ button('test-leave', 'leave', size),
49
+ ]),
50
+ el('p', { class: 'test-line2', id: `testLine2-${p.n}`, hidden: '' }),
51
+ el('p', { class: 'test-acts', hidden: '' }, [button('test-again', 'again'), ' ', button('test-add', 'add')]),
52
+ el('details', { class: 'more test-more', hidden: '' }, [
53
+ el('summary', { text: t('test.more_summary') }), el('div', { class: 'more-body method' }),
54
+ ]),
55
+ ]);
56
+ }
57
+
58
+ /** test.js: the picture's block, when "Your picture" shows it. */
59
+ export function paintTest(p) {
60
+ const node = $('yourPicture')?.querySelector(`.test[data-n="${p.n}"]`);
61
+ if (node) render(node, p, planFor(p));
62
+ }
63
+
64
+ /** plan_view.js refreshPlan: the block in place, its {at} following a typed width. */
65
+ export function refreshTest(box, p, plan) {
66
+ const node = box.querySelector('.test');
67
+ if (node) render(node, p, plan);
68
+ }
69
+
70
+ const set = (node, text) => {
71
+ node.textContent = text;
72
+ node.hidden = !text;
73
+ };
74
+
75
+ function render(node, p, plan) {
76
+ const rec = p.test;
77
+ node.hidden = !(rec && rec.state !== 'settling' && p.kind === 'graphic' && plan && plan.mode !== 'cant');
78
+ if (node.hidden) return;
79
+ const q = (sel) => node.querySelector(sel);
80
+ const had = [...node.querySelectorAll('.linklike')].find((b) => b === document.activeElement); // a button with focus
81
+ const waiting = WAITING.includes(rec.state);
82
+ const one = rec.state === 'done' ? lineOne(rec, plan) : null;
83
+ const two = one && one.kind !== 'worse' ? lineTwo(rec) : null; // after "worse" the answer already warns
84
+ const size = waiting ? sizeLine(rec, plan) : null;
85
+ const name = rec.part?.name ? t(`test.name.${rec.part.name}`) : '';
86
+ const state = q('.test-state');
87
+ state.textContent = one ? one.text : stateLine(rec, plan);
88
+ state.classList.toggle('running', waiting); // not printed: only lines 1 and 2 are
89
+ set(q('.test-hint'), waiting ? t('test.hint', { s: S.scenes.s, at: t(`test.at.${at(plan)}_lower`) }) : '');
90
+ set(q('.test-size-text'), size?.text || '');
91
+ q('.test-size').hidden = !size;
92
+ set(q('.test-offer'), size?.offer ? t('test.add', { name }) : '');
93
+ set(q('.test-leave'), size?.leave ? t('test.leave') : '');
94
+ const line2 = q('.test-line2');
95
+ set(line2, two ? t(two.key, { name }) : '');
96
+ const again = rec.state === 'failed' ? rec.retry : two?.act === 'again';
97
+ set(q('.test-again'), again ? t('rows.again') : '');
98
+ q('.test-again').setAttribute('aria-describedby', two ? line2.id : state.id); // heard as retrying the test
99
+ set(q('.test-add'), two?.act === 'add' ? t('test.add', { name }) : '');
100
+ q('.test-add').setAttribute('aria-describedby', line2.id);
101
+ q('.test-acts').hidden = q('.test-again').hidden && q('.test-add').hidden;
102
+ const more = one ? moreLines(rec, plan, one.kind) : [];
103
+ q('.test-more').hidden = !more.length;
104
+ q('.test-more .more-body').replaceChildren(...more.map((s) => el('p', { text: s })));
105
+ if (had?.hidden) state.focus(); // it hid by itself (the browser's part ended, or the answer came): focus isn't lost
106
+ }
107
+
108
+ const at = (plan) => (plan.mode === 'width' ? 'distance' : 'width'); // a printed width typed: from that distance
109
+
110
+ /** The block's first line while the test waits or runs, or its can't line or failure (an answer is lineOne's). The
111
+ * counter follows whichever is slower, the server or this browser's scanner while its part runs. */
112
+ function stateLine(rec, plan) {
113
+ const L = S.limits;
114
+ if (rec.state === 'queued') return t('test.queued');
115
+ if (rec.state === 'busy') return t('test.busy', { n: rec.n });
116
+ if (rec.state === 'failed') return rec.line;
117
+ if (rec.state === 'cant') {
118
+ const min = plan.v === 1 ? L.min_ppm_v1 : L.min_ppm;
119
+ return t(`test.cant.${rec.why}`, { min, mp: L.max_pixels / 1e6, max: L.max_version });
120
+ }
121
+ const part = rec.part;
122
+ return t('test.running', { k: part?.running ? Math.min(rec.done || 0, part.k) : rec.done || 0, s: S.scenes.s });
123
+ }
124
+
125
+ /** Whether this browser's scanner took part: its part ended with an answer (all its scenes, or the early stop). */
126
+ const took = (part) => Boolean(part?.answer) && (part.end === 'settled' || part.end === 'complete');
127
+
128
+ /** The scanners that ran, with their answers: this browser's first (when it took part), then the server's two. */
129
+ function ran(rec) {
130
+ const own = took(rec.part) ? [{ key: rec.part.name, answer: rec.part.answer }] : [];
131
+ return [...own, ...SERVER.map((key) => ({ key, answer: rec.answers[key] }))];
132
+ }
133
+
134
+ const names = (list) => join(list.map((x) => t(`test.name.${x.key}`)));
135
+
136
+ /** Line 1: worse if any scanner read it worse, naming those that did; as well as a plain code only if every scanner
137
+ * did, naming them all; otherwise the test couldn't tell, naming those that couldn't. */
138
+ function lineOne(rec, plan) {
139
+ const all = ran(rec);
140
+ const of = (...answers) => all.filter((x) => answers.includes(x.answer));
141
+ const kind = of('worse').length ? 'worse' : of('like').length === all.length ? 'like' : 'unsure';
142
+ const who = names(kind === 'worse' ? of('worse') : kind === 'like' ? all : of('close', 'few'));
143
+ return { kind, text: t(`test.${kind}`, { at: t(`test.at.${at(plan)}`), who }) };
144
+ }
145
+
146
+ /** Line 2, after "as well as" or "can't tell", the first that applies: the phone's scanner that wasn't in the test
147
+ * and where to get its answer; why this browser's own didn't take part, with Try again or Add where either helps; or
148
+ * what this browser can't do, by platform. */
149
+ function lineTwo(rec) {
150
+ const part = rec.part || {};
151
+ if (took(part)) return { key: part.name === 'apple' ? 'test.missing.google' : 'test.missing.apple' };
152
+ if (['start', 'stopped', 'slow'].includes(part.end)) return { key: `test.fallback.${part.end}`, act: 'again' };
153
+ if (part.end === 'left' || part.decision === 'offered') return { key: 'test.missing.left_out', act: 'add' };
154
+ if (part.decision === 'save_data') return { key: 'test.missing.save_data', act: 'again' };
155
+ if (part.decision === 'late' || part.decision === 'broken') return { key: 'test.fallback.start', act: 'again' };
156
+ const kind = platformKind(); // no Try again: trying again can't help
157
+ const which = { 'chrome-mac': 'mac_none', 'chrome-android': 'android_none' }[kind] || kind;
158
+ return { key: `test.missing.${which}` };
159
+ }
160
+
161
+ /** The size line while the test waits or runs: this browser's scanner joins (with Leave it out while its part runs),
162
+ * or can join (with Add); gone once its part ends by a fallback, the budget or Leave it out. {mb} is the most a test
163
+ * of the code's version range downloads, with the frames it takes. */
164
+ function sizeLine(rec, plan) {
165
+ const part = rec.part;
166
+ if (!part || !['joins', 'offered'].includes(part.decision)) return null;
167
+ if (part.decision === 'joins' && (part.left || (part.over && !took(part)))) return null; // Leave it out: at once
168
+ const range = S.mb[part.frames] || S.mb.jpeg;
169
+ const tops = Object.keys(range).map(Number).sort((a, b) => a - b);
170
+ const vars = { name: t(`test.name.${part.name}`), mb: range[String(tops.find((v) => plan.v <= v) ?? tops.at(-1))] };
171
+ if (part.decision === 'offered') return { text: t('test.hint_offer', vars), offer: true };
172
+ return { text: t('test.hint_browser', vars), leave: Boolean(part.running) };
173
+ }
174
+
175
+ /** The limits' figures in words, from test.json's [a, b]: a share of the scans ("3 in 10"), and how often a design
176
+ * still passes ("1 time in 30"). */
177
+ const share = ([a, b]) => t('test.more.limits_share', { a, b });
178
+ const times = ([a, b]) => t(a === 1 ? 'test.more.limits_times_one' : 'test.more.limits_times_many', { a, b });
179
+
180
+ /** Behind "+", after an answer, each where it applies: the method, the rule, each scanner's own answer (never its
181
+ * reads), the Chrome builds measured, what the figures say of the answer's path, the limits of its scenes, the
182
+ * figures' scope, and that this browser's reads stayed in the page. */
183
+ function moreLines(rec, plan, kind) {
184
+ const F = S.figures;
185
+ const lim = F.limits;
186
+ const limits = {
187
+ big: share(lim.big), big_pass: times(lim.big_pass), small: share(lim.small), small_pass: times(lim.small_pass),
188
+ };
189
+ const own = took(rec.part) ? rec.part.name : '';
190
+ const fair = kind !== 'worse';
191
+ const cover = (key, figures) => t(`test.more.${key}`, { N: F.N, ...figures });
192
+ const pathLine = own ? (fair ? cover(`cover_${own}`, F.cover[own]) : '')
193
+ : cover(fair ? 'cover_open' : 'cover_open_worse', F.cover[fair ? 'open' : 'open_worse']);
194
+ const [vlo, vhi] = F.scope.versions;
195
+ const each = (x) => t('test.more.each', { name: t(`test.name.${x.key}`), result: t(`test.more.result.${x.answer}`) });
196
+ const p = needFor(plan.v).ppm.toFixed(1); // p(v): the table's need for the version, as the test renders it
197
+ return [
198
+ t('test.more.method', { margin: S.frames.margin_squares, s: S.scenes.s, p, v: plan.v }),
199
+ t('test.more.rule', { least: S.rule.least_worse, cost: S.rule.like_cost, q: S.rule.q }),
200
+ ...ran(rec).map(each),
201
+ own ? t(`test.more.${own}`, { os: F.chrome[own].os, n: count(F.chrome[own].n) }) : '',
202
+ pathLine,
203
+ kind === 'like' ? t('test.more.limits', { s: S.scenes.s, ...limits }) : '',
204
+ t('test.more.scope', { N: F.N, styles: WORDS[F.scope.styles], vlo, vhi }),
205
+ own ? t('test.more.private') : '',
206
+ ].filter(Boolean);
207
+ }
208
+
209
+ let said = []; // what testNote says in the next frame, in order
210
+
211
+ /** testNote, the test's polite live region. what: 'queued', 'start', 'busy' or 'end' (the block's first line then),
212
+ * named for the picture when asked. Cleared, then written in the next animation frame, as the marking's note is. */
213
+ export function announce(p, what, named) {
214
+ const rec = p.test;
215
+ const plan = planFor(p);
216
+ let line = '';
217
+ if (what === 'queued') line = t('test.queued');
218
+ else if (what === 'start') line = t('test.announce');
219
+ else if (what === 'busy') line = t('test.busy', { n: rec.n });
220
+ else if (plan) line = rec.state === 'done' ? lineOne(rec, plan).text : stateLine(rec, plan);
221
+ const text = named ? t('test.announce_named', { n: p.n, name: p.name, line }) : line;
222
+ const node = $('testNote');
223
+ if (!node || !text) return;
224
+ if (!said.length) {
225
+ node.textContent = '';
226
+ requestAnimationFrame(() => {
227
+ node.textContent = said.join(' ');
228
+ said = [];
229
+ });
230
+ }
231
+ said.push(text);
232
+ }
233
+
234
+ /** After Try again, Add or Leave it out: focus on the line that takes the button's place. */
235
+ function focusState(p) {
236
+ const node = $('yourPicture')?.querySelector(`.test[data-n="${p.n}"] .test-state`);
237
+ if (node && !node.closest('.test').hidden) node.focus();
238
+ }
web/upload.js ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Sending a picture to the server, for its reading (api/measure) and for its design test (api/design): the checks
2
+ // this browser makes first, the file as it is or converted (a format the server doesn't open, or one whose content and
3
+ // type disagree), and the reading's errors in the page's words. Moved out of app.js so that both routes send a
4
+ // picture under the same rules. Nothing else is sent or kept.
5
+ import { t } from './copy.js';
6
+
7
+ export const MAX_MB = 25;
8
+ export const MAX_BYTES = MAX_MB * 1024 * 1024;
9
+ export const MAX_CANVAS = 16e6; // pixels: a picture this browser converts is drawn at most this big (every browser can)
10
+
11
+ export function failure(message, retry = false) {
12
+ const err = new Error(message);
13
+ err.retry = retry;
14
+ return err;
15
+ }
16
+
17
+ export async function send(url, blob, signal) {
18
+ try {
19
+ return await fetch(url, { method: 'POST', body: blob, signal, headers: { 'Content-Type': blob.type || 'application/octet-stream' } });
20
+ } catch {
21
+ throw failure(t('errors.unreachable'), true);
22
+ }
23
+ }
24
+
25
+ /** A picture in a format the server doesn't open (HEIC, AVIF, GIF…) as this browser shows it, with transparent parts
26
+ * on white as on a printed page: as PNG, or as a high-quality JPEG when the PNG would be over 25 MB. One bigger than
27
+ * MAX_CANVAS pixels is drawn at that size, still well above the largest size it is read at (1920 pixels). */
28
+ export async function converted(file) {
29
+ let bitmap;
30
+ try {
31
+ bitmap = await createImageBitmap(file);
32
+ } catch {
33
+ throw failure(t('errors.format'));
34
+ }
35
+ const k = Math.min(1, Math.sqrt(MAX_CANVAS / (bitmap.width * bitmap.height)));
36
+ const canvas = document.createElement('canvas');
37
+ canvas.width = Math.max(1, Math.round(bitmap.width * k));
38
+ canvas.height = Math.max(1, Math.round(bitmap.height * k));
39
+ const ctx = canvas.getContext('2d');
40
+ ctx.fillStyle = '#fff';
41
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
42
+ ctx.imageSmoothingQuality = 'high';
43
+ ctx.drawImage(bitmap, 0, 0, canvas.width, canvas.height);
44
+ bitmap.close();
45
+ const encode = (type, quality) => new Promise((resolve) => canvas.toBlob(resolve, type, quality));
46
+ let out = await encode('image/png');
47
+ if (out && out.size > MAX_BYTES) out = await encode('image/jpeg', 0.95);
48
+ if (!out) throw failure(t('errors.prepare'));
49
+ if (out.size > MAX_BYTES) throw failure(t('errors.too_big_converted', { mb: MAX_MB }));
50
+ return out;
51
+ }
52
+
53
+ /** What a picture goes to the server as, checked first: an SVG, a file over MAX_MB or an empty file isn't sent (a
54
+ * failure in the page's words); a format the server doesn't open goes converted, and any other as it is. A design
55
+ * test makes it before asking for its slot, so a slow conversion never uses up the slot's hold. */
56
+ export async function prepare(file) {
57
+ if (file.type === 'image/svg+xml' || /\.svg$/i.test(file.name)) throw failure(t('errors.svg'));
58
+ if (file.size > MAX_BYTES) throw failure(t('errors.too_big', { mb: MAX_MB }));
59
+ if (!file.size) throw failure(t('rows.empty_file'));
60
+ return /^image\/(jpeg|png|webp)$/.test(file.type) ? file : converted(file); // the formats the server opens
61
+ }
62
+
63
+ /** Send a picture to url under the server's rules, as prepare() makes it (or made it already: blob), and give back
64
+ * the server's response; the file sent as it is and refused as of the wrong type (415) goes once more, converted.
65
+ * Throws failure(errors.unreachable, true) when the server can't be reached. */
66
+ export async function upload(url, file, signal, blob = null) {
67
+ const sent = blob || (await prepare(file));
68
+ const res = await send(url, sent, signal);
69
+ if (res.status === 415 && sent === file) return send(url, await converted(file), signal); // type and content disagree
70
+ return res;
71
+ }
72
+
73
+ /** The reading of one picture: its reads, place, level and checks, or a failure in the page's words (retry: a busy,
74
+ * slow or unreachable server, which Try again can help). */
75
+ export async function measure(file, signal) {
76
+ const res = await upload('api/measure', file, signal);
77
+ if (!res.ok) {
78
+ let message = '';
79
+ try { message = (await res.json()).error; } catch { /* not JSON */ }
80
+ const retry = res.status === 408 || res.status >= 500; // slow to arrive, busy, or a server fault
81
+ throw failure(message || t(retry ? 'errors.busy' : 'errors.unreadable'), retry);
82
+ }
83
+ try {
84
+ return await res.json();
85
+ } catch {
86
+ throw failure(t('errors.busy'), true); // cut off on the way, or not the server's answer
87
+ }
88
+ }
web/yours.js CHANGED
@@ -8,6 +8,7 @@ import { CHECKS, agreementText, isBig, levelIndex, measuredChecks, notRunText, r
8
  import { adviceParts, noteLines, refreshValues, valueText } from './checks.js';
9
  import { answerLines, answerText, planFor } from './plan.js';
10
  import { planChanged, planHead, refreshPair, refreshPlan, suggestion } from './plan_view.js';
 
11
 
12
  const KINDS = ['unsure', 'graphic', 'print', 'screen'];
13
  const VIEW = 168; // CSS pixels: the enlarged code
@@ -119,7 +120,7 @@ function kindField(p) {
119
  el('legend', { text: t('placement.label') }),
120
  el('div', { class: 'opts' }, opts),
121
  el('p', { class: 'placement-suggest' }),
122
- el('p', { class: 'placement-hint', id: 'placementHint', text: [t('placement.hint'), t('placement.note_sticky')].filter(Boolean).join(' ') }),
123
  el('p', { class: 'placement-context' }),
124
  ]);
125
  }
 
8
  import { adviceParts, noteLines, refreshValues, valueText } from './checks.js';
9
  import { answerLines, answerText, planFor } from './plan.js';
10
  import { planChanged, planHead, refreshPair, refreshPlan, suggestion } from './plan_view.js';
11
+ import { testChoiceHint } from './test.js';
12
 
13
  const KINDS = ['unsure', 'graphic', 'print', 'screen'];
14
  const VIEW = 168; // CSS pixels: the enlarged code
 
120
  el('legend', { text: t('placement.label') }),
121
  el('div', { class: 'opts' }, opts),
122
  el('p', { class: 'placement-suggest' }),
123
+ el('p', { class: 'placement-hint', id: 'placementHint', text: [t('placement.hint'), testChoiceHint(), t('placement.note_sticky')].filter(Boolean).join(' ') }),
124
  el('p', { class: 'placement-context' }),
125
  ]);
126
  }