Remove dead code, split long functions (tilt search, wipe, sync search, blob mask/ellipse, scan attempt); center split title

This commit is contained in:
2026-08-17 02:15:05 +01:00
parent 9308b875b5
commit 98cf8e5f6d
5 changed files with 157 additions and 189 deletions
+128 -98
View File
@@ -180,6 +180,34 @@ const EASE = {
// deterministic per-dash phase so every dash breathes on its own rhythm
const phase = (k, j) => ((Math.sin(k * 12.9898 + j * 78.233) * 43758.5453) % 1 + 1) % 1;
// the bean keeps its shape, only its inside changes: a soft gradient slides across it from a random end,
// fading the new tone in behind and the old one out ahead. conic so it follows the arc exactly
// arc: center, angular extent cap to cap, and how to stroke the dash
function wipeDash(ctx, { cx, cy, lo, hi, dash }, from, to, fade, k, j) {
const dir = phase(k + 7, j + (fade.seed || 0) * 31) < 0.5 ? 1 : -1;
const delay = phase(k + 3, j + (fade.seed || 0) * 17) * 0.15, x = Math.min(1, Math.max(0, (fade.k - delay) / 0.85));
const e = (EASE[fade.ease] || EASE.sine)(x);
if (!ctx.createConicGradient) {
ctx.globalAlpha = from + (to - from) * e;
dash();
return;
}
const soft = (hi - lo) * 0.6, edge = dir > 0 ? lo - soft + (hi - lo + 2 * soft) * e : hi + soft - (hi - lo + 2 * soft) * e;
const start = lo - soft - 0.2, at = (th) => Math.min(1, Math.max(0, (th - start) / (2 * Math.PI)));
const g = ctx.createConicGradient(start, cx, cy);
const [behind, ahead] = dir > 0 ? [to, from] : [from, to];
for (let i = 0; i <= 6; i++) {
const u = i / 6, w = u * u * (3 - 2 * u);
g.addColorStop(at(edge - soft + 2 * soft * u), `rgba(28,32,54,${behind + (ahead - behind) * w})`);
}
g.addColorStop(0, `rgba(28,32,54,${behind})`);
g.addColorStop(1, `rgba(28,32,54,${ahead})`);
ctx.globalAlpha = 1;
ctx.strokeStyle = g;
dash();
ctx.strokeStyle = INK;
}
// fade: { from: previous bits, k: 0..1, seed } while the code switches. a changed dash is wiped:
// one edge crosses it from a random end, the new tone fills behind it while the old one empties ahead
export function render(ctx, cx, cy, unit, theta, bits, t = 0, fade = null) {
@@ -189,7 +217,6 @@ export function render(ctx, cx, cy, unit, theta, bits, t = 0, fade = null) {
ctx.beginPath(); ctx.arc(cx, cy, CENTER * unit, 0, 2 * Math.PI); ctx.fill();
logo(ctx, cx, cy, CENTER * unit);
const prev = fade && fade.k < 1 ? ringsOf(fade.from) : null;
const ease = fade ? fade.k * fade.k * (3 - 2 * fade.k) : 1;
ctx.lineCap = "round";
ctx.lineWidth = THICK * unit;
ctx.strokeStyle = INK;
@@ -208,32 +235,7 @@ export function render(ctx, cx, cy, unit, theta, bits, t = 0, fade = null) {
dash();
continue;
}
// the bean keeps its shape, only its inside changes: a soft gradient slides across it from a random end,
// fading the new tone in behind and the old one out ahead. conic so it follows the arc exactly
const from = alphaOf(prev[k][j]), to = alphaOf(ring[j]);
const lo = a - half - cap, hi = a + half + cap;
const dir = phase(k + 7, j + (fade.seed || 0) * 31) < 0.5 ? 1 : -1;
const delay = phase(k + 3, j + (fade.seed || 0) * 17) * 0.15, x = Math.min(1, Math.max(0, (fade.k - delay) / 0.85));
const e = (EASE[fade.ease] || EASE.sine)(x);
if (!ctx.createConicGradient) {
ctx.globalAlpha = from + (to - from) * e;
dash();
continue;
}
const soft = (hi - lo) * 0.6, edge = dir > 0 ? lo - soft + (hi - lo + 2 * soft) * e : hi + soft - (hi - lo + 2 * soft) * e;
const start = lo - soft - 0.2, at = (th) => Math.min(1, Math.max(0, (th - start) / (2 * Math.PI)));
const g = ctx.createConicGradient(start, cx, cy);
const [behind, ahead] = dir > 0 ? [to, from] : [from, to];
for (let i = 0; i <= 6; i++) {
const u = i / 6, w = u * u * (3 - 2 * u);
g.addColorStop(at(edge - soft + 2 * soft * u), `rgba(28,32,54,${behind + (ahead - behind) * w})`);
}
g.addColorStop(0, `rgba(28,32,54,${behind})`);
g.addColorStop(1, `rgba(28,32,54,${ahead})`);
ctx.globalAlpha = 1;
ctx.strokeStyle = g;
dash();
ctx.strokeStyle = INK;
wipeDash(ctx, { cx, cy, lo: a - half - cap, hi: a + half + cap, dash }, alphaOf(prev[k][j]), alphaOf(ring[j]), fade, k, j);
}
});
ctx.globalAlpha = 1;
@@ -326,10 +328,9 @@ function solve(A, B) {
return B.map((b, i) => b / A[i][i]);
}
// dark blobs of one pyramid level that look like a filled ellipse. s = level px size in image px
function blobs(g, w, h, s, ratio, out) {
// dark = clearly under the brightest thing nearby. a washed projector keeps black/white around 0.6,
// and the disc always has its light surround within reach, whatever the room does around the screen
// dark = clearly under the brightest thing nearby. a washed projector keeps black/white around 0.6,
// and the disc always has its light surround within reach, whatever the room does around the screen
function darkMask(g, w, h, ratio) {
const T = 8, tw = Math.ceil(w / T), th = Math.ceil(h / T), tmax = new Float32Array(tw * th);
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {
const i = ((y / T) | 0) * tw + ((x / T) | 0), v = g[y * w + x];
@@ -347,6 +348,36 @@ function blobs(g, w, h, s, ratio, out) {
const thr = m * ratio, y1 = Math.min(h, (ty + 1) * T), x1 = Math.min(w, (tx + 1) * T);
for (let y = ty * T; y < y1; y++) for (let x = tx * T; x < x1; x++) if (g[y * w + x] < thr) dark[y * w + x] = 1;
}
return dark;
}
// ellipse from the moments of a component filled row by row (so the light logo inside doesn't count as
// holes), or null when the shape is not a filled ellipse. rows y0..y1 where stamp[y] === id hold lo..hi
function rowEllipse(lo, hi, stamp, id, y0, y1, count, s) {
let m0 = 0, mx = 0, my = 0, mxx = 0, myy = 0, mxy = 0;
for (let y = y0; y <= y1; y++) {
if (stamp[y] !== id) continue;
const a0 = lo[y], a1 = hi[y], n = a1 - a0 + 1, sx = ((a0 + a1) * n) / 2;
const sxx = (a1 * (a1 + 1) * (2 * a1 + 1) - (a0 - 1) * a0 * (2 * a0 - 1)) / 6;
m0 += n; mx += sx; my += y * n; mxx += sxx; myy += y * y * n; mxy += y * sx;
}
const ux = mx / m0, uy = my / m0;
const cxx = mxx / m0 - ux * ux + 1 / 12, cyy = myy / m0 - uy * uy + 1 / 12, cxy = mxy / m0 - ux * uy;
const tr = cxx + cyy, dt = Math.sqrt(Math.max(0, ((cxx - cyy) / 2) ** 2 + cxy * cxy));
const l1 = tr / 2 + dt, l2 = tr / 2 - dt;
if (l2 <= 0) return null;
const a = 2 * Math.sqrt(l1), b = 2 * Math.sqrt(l2);
const fill = m0 / (Math.PI * a * b), solidity = count / m0;
if (fill < 0.84 || fill > 1.16 || solidity < 0.5 || b / a < 0.38 || b < 3.5) return null;
const phi = 0.5 * Math.atan2(2 * cxy, cxx - cyy);
const c = Math.cos(phi), sn = Math.sin(phi);
const k1 = (a * s) / CENTER, k2 = (b * s) / CENTER;
return { cx: (ux + 0.5) * s - 0.5, cy: (uy + 0.5) * s - 0.5, A: [c * k1, -sn * k2, sn * k1, c * k2], area: m0 * s * s, r: Math.sqrt(a * b) * s };
}
// dark blobs of one pyramid level that look like a filled ellipse. s = level px size in image px
function blobs(g, w, h, s, ratio, out) {
const dark = darkMask(g, w, h, ratio);
const stack = new Int32Array(w * h), lo = new Int32Array(h), hi = new Int32Array(h), stamp = new Int32Array(h).fill(-1);
let id = 0;
for (let start = 0; start < w * h; start++) {
@@ -365,26 +396,8 @@ function blobs(g, w, h, s, ratio, out) {
if (y < h - 1 && dark[i + w]) { dark[i + w] = 0; stack[sp++] = i + w; }
}
if (count < 30 || y1 - y0 < 5) continue;
// moments of the row-filled shape, so the light logo inside doesn't count as holes
let m0 = 0, mx = 0, my = 0, mxx = 0, myy = 0, mxy = 0;
for (let y = y0; y <= y1; y++) {
if (stamp[y] !== id) continue;
const a0 = lo[y], a1 = hi[y], n = a1 - a0 + 1, sx = ((a0 + a1) * n) / 2;
const sxx = (a1 * (a1 + 1) * (2 * a1 + 1) - (a0 - 1) * a0 * (2 * a0 - 1)) / 6;
m0 += n; mx += sx; my += y * n; mxx += sxx; myy += y * y * n; mxy += y * sx;
}
const ux = mx / m0, uy = my / m0;
const cxx = mxx / m0 - ux * ux + 1 / 12, cyy = myy / m0 - uy * uy + 1 / 12, cxy = mxy / m0 - ux * uy;
const tr = cxx + cyy, dt = Math.sqrt(Math.max(0, ((cxx - cyy) / 2) ** 2 + cxy * cxy));
const l1 = tr / 2 + dt, l2 = tr / 2 - dt;
if (l2 <= 0) continue;
const a = 2 * Math.sqrt(l1), b = 2 * Math.sqrt(l2);
const fill = m0 / (Math.PI * a * b), solidity = count / m0;
if (fill < 0.84 || fill > 1.16 || solidity < 0.5 || b / a < 0.38 || b < 3.5) continue;
const phi = 0.5 * Math.atan2(2 * cxy, cxx - cyy);
const c = Math.cos(phi), sn = Math.sin(phi);
const k1 = (a * s) / CENTER, k2 = (b * s) / CENTER;
out.push({ cx: (ux + 0.5) * s - 0.5, cy: (uy + 0.5) * s - 0.5, A: [c * k1, -sn * k2, sn * k1, c * k2], area: m0 * s * s, r: Math.sqrt(a * b) * s });
const e = rowEllipse(lo, hi, stamp, id, y0, y1, count, s);
if (e) out.push(e);
}
}
@@ -812,6 +825,56 @@ function tokenOf(bits, soft, strict = true) {
return { token: tokenFromPayload(fixed.slice(0, fixed.length - NSYM)), fixes: bad };
}
// looks like a code but the geometry is off: a screen seen at 30-45 deg bends the outer rings more than the
// conic fit can follow. search the vanishing line directly, scored on the sync ring and the outer ring.
// tone(u, v) reads brightness at image coordinates
function searchTilt(img, cand, tone, best, opts) {
const D = discConic(img, cand);
if (!D) return null;
const at = (map) => (x, y) => { const [u, v] = map(x, y); return tone(u, v); };
const n0 = RING_SECTORS[0], s0 = (2 * Math.PI) / n0, nk = RING_SECTORS[3], sk = (2 * Math.PI) / nk;
const quick = (S) => {
let top = null;
for (let dt = -12; dt <= 12; dt += 2) {
const th = best.theta + (dt * Math.PI) / 180, vals = [];
for (let j = 0; j < n0; j++) vals.push(sector(S, RADII[0], sectorAngle(th, j, n0), s0));
if (vals.some(Number.isNaN)) return null;
const sc = Math.max(corr(vals, SYNC_BITS), corrFlat(vals));
if (!top || sc > top.sc) top = { sc, th };
}
const outer = [];
for (let j = 0; j < nk; j++) outer.push(sector(S, RADII[3], sectorAngle(top.th, j, nk), sk));
if (outer.some(Number.isNaN)) return null;
return top.sc + split(outer).quality;
};
const tryLine = (lx, ly) => {
const map = mapFromLine(D.Q1, [lx, ly, 1], D.mx, D.my, D.sc);
if (!map) return null;
const S = at(map), q = quick(S);
return q == null ? null : { q, S, l: [lx, ly] };
};
const tries = [];
for (const m of [0.015, 0.03, 0.045, 0.06, 0.08, 0.1]) for (let i = 0; i < 16; i++) {
const t = tryLine(m * Math.cos((i / 16) * 2 * Math.PI), m * Math.sin((i / 16) * 2 * Math.PI));
if (t) tries.push(t);
}
tries.sort((p, q) => q.q - p.q);
let out = null;
for (const t of tries.slice(0, 3)) {
// polish around the pick on a finer grid before the full read
let pick = t;
for (const dx of [-0.008, 0, 0.008]) for (const dy of [-0.008, 0, 0.008]) {
if (!dx && !dy) continue;
const c = tryLine(t.l[0] + dx, t.l[1] + dy);
if (c && c.q > pick.q) pick = c;
}
const r = readRings(pick.S, opts);
if (r && r.token) return { ...r, tilt: pick.l };
if (r && (!out || r.score > out.score)) out = r;
}
return out;
}
// reads one candidate. returns { token, theta, score, scale, soft } or { token: null, ... } or null.
// tries the projective geometry (better under tilt) and the plain affine one (safer on soft frames)
export function decodeAt(img, cand, opts = {}) {
@@ -838,55 +901,16 @@ export function decodeAt(img, cand, opts = {}) {
if (r && r.token) return r;
if (r && (!best || r.score > best.score)) best = r;
}
// looks like a code but the geometry is off: a screen seen at 30-45 deg bends the outer rings more than the
// conic fit can follow. search the vanishing line directly, scored on the sync ring and the outer ring
if (opts.tilt !== false && best && best.score > 0.55 && best.theta != null) {
const D = discConic(img, cand);
if (D) {
const at = (map) => (x, y) => { const [u, v] = map(x, y); return bilinear(tone, u - x0, v - y0); };
const n0 = RING_SECTORS[0], s0 = (2 * Math.PI) / n0, nk = RING_SECTORS[3], sk = (2 * Math.PI) / nk;
const quick = (S) => {
let top = null;
for (let dt = -12; dt <= 12; dt += 2) {
const th = best.theta + (dt * Math.PI) / 180, vals = [];
for (let j = 0; j < n0; j++) vals.push(sector(S, RADII[0], sectorAngle(th, j, n0), s0));
if (vals.some(Number.isNaN)) return null;
const sc = Math.max(corr(vals, SYNC_BITS), corrFlat(vals));
if (!top || sc > top.sc) top = { sc, th };
}
const outer = [];
for (let j = 0; j < nk; j++) outer.push(sector(S, RADII[3], sectorAngle(top.th, j, nk), sk));
if (outer.some(Number.isNaN)) return null;
return top.sc + split(outer).quality;
};
const tries = [];
for (const m of [0.015, 0.03, 0.045, 0.06, 0.08, 0.1]) for (let i = 0; i < 16; i++) {
const a = (i / 16) * 2 * Math.PI, map = mapFromLine(D.Q1, [m * Math.cos(a), m * Math.sin(a), 1], D.mx, D.my, D.sc);
if (!map) continue;
const S = at(map), q = quick(S);
if (q != null) tries.push({ q, S, l: [m * Math.cos(a), m * Math.sin(a)] });
}
tries.sort((p, q) => q.q - p.q);
for (const t of tries.slice(0, 3)) {
// polish around the pick on a finer grid before the full read
let pick = t;
for (const dx of [-0.008, 0, 0.008]) for (const dy of [-0.008, 0, 0.008]) {
if (!dx && !dy) continue;
const map = mapFromLine(D.Q1, [t.l[0] + dx, t.l[1] + dy, 1], D.mx, D.my, D.sc);
if (!map) continue;
const S = at(map), q = quick(S);
if (q != null && q > pick.q) pick = { q, S, l: [t.l[0] + dx, t.l[1] + dy] };
}
const r = readRings(pick.S, { ...opts, span });
if (r && r.token) return { ...r, tilt: pick.l };
if (r && r.score > best.score) best = r;
}
}
const r = searchTilt(img, cand, (x, y) => bilinear(tone, x - x0, y - y0), best, { ...opts, span });
if (r && r.token) return r;
if (r && r.score > best.score) best = r;
}
return best;
}
function readRings(S, opts) {
// rotation and scale of the sync ring: { best } to go on, or { done } when the read stops here
function findSync(S, opts) {
const minScore = opts.minScore ?? 0.8;
const n0 = RING_SECTORS[0], step0 = (2 * Math.PI) / n0;
const syncAt = (theta, scale) => {
@@ -900,13 +924,13 @@ function readRings(S, opts) {
for (const scale of opts.scales || [0.96, 1.0, 1.04]) {
for (let t = 0; t < 72; t++) {
const r = syncAt((t / 72) * 2 * Math.PI, scale);
if (!r) return null;
if (!r) return { done: null };
coarse.push(r);
}
}
coarse.sort((p, q) => q.score - p.score);
// nothing like the sync pattern at any angle: not a code, skip the fine search
if (coarse[0].score < 0.5) return { token: null, ...coarse[0], vals: undefined };
if (coarse[0].score < 0.5) return { done: { token: null, ...coarse[0], vals: undefined } };
let best = null;
for (const c of coarse.slice(0, 3)) {
for (const ds of [-0.02, 0, 0.02]) for (let dt = -10; dt <= 10; dt++) {
@@ -915,7 +939,13 @@ function readRings(S, opts) {
}
}
// weak sync means we're not looking at a ring code (or a bad frame): don't let rs "fix" noise
if (!best || best.score < minScore) return best ? { token: null, ...best, vals: undefined } : null;
if (!best || best.score < minScore) return { done: best ? { token: null, ...best, vals: undefined } : null };
return { best };
}
function readRings(S, opts) {
const { best, done } = findSync(S, opts);
if (!best) return done;
const dark = [], light = [];
best.vals.forEach((v, j) => (SYNC_BITS[j] ? dark : light).push(v));
const md = dark.reduce((x, y) => x + y) / dark.length, ml = light.reduce((x, y) => x + y) / light.length;