Rework scan screen, make lookups non-blocking, fix review findings

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-18 10:19:32 -07:00
1 parent 468cafc6db
commit b5defb4e4f
46 files changed
+1957 -406

No files matched your search

+81
View File
@@ -0,0 +1,81 @@
// Helpers for scripts/browser-check.ts: an EAN-13 bitmap, and a .y4m video of it for Chromium's fake camera.
import { writeFileSync } from 'node:fs';
/** EAN-13 module pattern (95 modules, 1 = bar) for a 13-digit string. */
export function ean13Modules(code: string): string {
const L = ['0001101', '0011001', '0010011', '0111101', '0100011', '0110001', '0101111', '0111011', '0110111', '0001011'];
const R = L.map((p) => [...p].map((b) => (b === '0' ? '1' : '0')).join(''));
const G = R.map((p) => [...p].reverse().join(''));
const parity = ['LLLLLL', 'LLGLGG', 'LLGGLG', 'LLGGGL', 'LGLLGG', 'LGGLLG', 'LGGGLL', 'LGLGLG', 'LGLGGL', 'LGGLGL'];
const d = [...code].map(Number);
let out = '101';
for (let i = 0; i < 6; i++) out += parity[d[0]][i] === 'L' ? L[d[i + 1]] : G[d[i + 1]];
out += '01010';
for (let i = 7; i < 13; i++) out += R[d[i]];
return out + '101';
}
export interface VideoOptions {
isbn: string;
width?: number; height?: number; frames?: number;
/** Pixels per barcode module. A real book held about 30 cm from a 720p webcam is roughly 1.2 to 2 px per module. */
moduleScale?: number;
}
/** Deterministic noise so a run is repeatable. */
function rng(seed: number) { let s = seed >>> 0; return () => ((s = (Math.imul(s, 1664525) + 1013904223) >>> 0) / 4294967296); }
/** Gray frames (Uint8Array, width*height) of a barcode on a white page: soft focus, sensor noise, hand jitter. */
export function barcodeFrames(o: VideoOptions): Uint8Array[] {
const W = o.width ?? 1280, H = o.height ?? 720, n = o.frames ?? 24, m = o.moduleScale ?? 2.6;
const mods = ean13Modules(o.isbn);
const bw = 95 * m, bh = Math.round(bw * 0.62);
const rand = rng(7);
const frames: Uint8Array[] = [];
for (let f = 0; f < n; f++) {
const jx = (rand() - 0.5) * 5, jy = (rand() - 0.5) * 5;
const x0 = W / 2 - bw / 2 + jx, y0 = H / 2 - bh / 2 + jy;
const raw = new Float32Array(W * H);
for (let y = 0; y < H; y++) {
for (let x = 0; x < W; x++) {
let v = 238 - 14 * (x / W) - 8 * (y / H); // a lit page, slightly uneven
if (y >= y0 && y < y0 + bh) {
let dark = 0;
for (let s = 0; s < 4; s++) { // 4 samples across the pixel
const mi = Math.floor((x + (s + 0.5) / 4 - x0) / m);
if (mi >= 0 && mi < 95 && mods[mi] === '1') dark++;
}
v = v * (1 - dark / 4) + 22 * (dark / 4);
}
raw[y * W + x] = v;
}
}
// Soft focus: a [1 2 1] blur both ways, then noise.
const out = new Uint8Array(W * H);
const tmp = new Float32Array(W * H);
for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) {
const a = raw[y * W + Math.max(0, x - 1)], b = raw[y * W + x], c = raw[y * W + Math.min(W - 1, x + 1)];
tmp[y * W + x] = (a + 2 * b + c) / 4;
}
for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) {
const a = tmp[Math.max(0, y - 1) * W + x], b = tmp[y * W + x], c = tmp[Math.min(H - 1, y + 1) * W + x];
out[y * W + x] = Math.max(0, Math.min(255, Math.round((a + 2 * b + c) / 4 + (rand() - 0.5) * 8)));
}
frames.push(out);
}
return frames;
}
/** Write gray frames as a YUV4MPEG2 (4:2:0) file, the format Chromium's --use-file-for-fake-video-capture reads. */
export function writeY4m(path: string, frames: Uint8Array[], width: number, height: number): void {
const header = Buffer.from(`YUV4MPEG2 W${width} H${height} F30:1 Ip A1:1 C420jpeg\n`);
const chroma = Buffer.alloc((width / 2) * (height / 2) * 2, 128);
const parts: Buffer[] = [header];
for (const f of frames) parts.push(Buffer.from('FRAME\n'), Buffer.from(f), chroma);
writeFileSync(path, Buffer.concat(parts));
}
export function makeBarcodeY4m(path: string, o: VideoOptions): void {
const W = o.width ?? 1280, H = o.height ?? 720;
writeY4m(path, barcodeFrames(o), W, H);
}