@333eco/brand 1.10.0 → 1.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -2
- package/brand.lock +3 -2
- package/data/brand.json +1 -1
- package/dist/Brand.kt +1 -1
- package/dist/Brand.swift +1 -1
- package/dist/tokens.json +1 -1
- package/package.json +3 -2
- package/qr/bqr.ts +49 -18
- package/qr/fingerprint.ts +136 -0
package/README.md
CHANGED
|
@@ -139,13 +139,19 @@ an H3QR names whom you thank, and a B-QR is how that code is drawn.
|
|
|
139
139
|
marker stands at the heart's point and its three markers mirror about the heart's own line (`bqrTurned`; always, no option). It
|
|
140
140
|
shipped by the founder's override of a failed scan gate, kept on the record in `scripts/bqr.test.mjs` §turn. A small ™ stands in the
|
|
141
141
|
bottom lobe: it names the **B-Emblem™**, never *B-QR* (unmarked), and shows which way is up. `lobe: "none"` or `autograph` (a person's own
|
|
142
|
-
strokes, *Open* only, ⛔ throws in *Full*) are for a person's own picture.
|
|
143
|
-
|
|
142
|
+
strokes, *Open* only, ⛔ throws in *Full*) are for a person's own picture. Codes printed before keep working. ⚠️ Superseded by v1.11.0: *always turned* (only a B-Link
|
|
143
|
+
is) and *the top lobe keeps its dots* (in our own codes).
|
|
144
144
|
- **Centered, and a coded top lobe (v1.10.0, founder 2026-11-24).** The ™, and an autograph in its place, stand at the bottom lobe's center, each
|
|
145
145
|
version's own (`BQR_LOBE_CENTER`, solved from the silhouette by the test). An autograph is FITTED to its box: its own shape kept, wherever on
|
|
146
146
|
the pad it was drawn. `coded` puts 96 bits in the TOP lobe (Open only, ⛔ throws in Full): for a mark in a person's picture, where its caller
|
|
147
147
|
writes a short fingerprint of that picture, so a heart pasted on another is caught with no network. ⛔ Never a person, a serial, a claim or a
|
|
148
148
|
key; nothing about it is secret. Our own codes keep their dots.
|
|
149
|
+
- **Four positions, read from the address (v1.11.0, founder 2026-11-24: *"keep all four positions"*).** The middle marker stands at the POINT for a B-Link,
|
|
150
|
+
STANDARD for any other address of the estate, and at the NOTCH or NON-STANDARD for the same two drawn by an outsider through our API
|
|
151
|
+
(`by: "outside"`, which only the API passes). `bqrPosition` reads the address; a caller never chooses. ⛔ Any other address THROWS: our heart never
|
|
152
|
+
frames an address someone chose. ⛔ A position is a declaration, never a proof: nothing public may say one shows who drew it.
|
|
153
|
+
- **B-Watermark℠ (v1.11.0)** = the invisible mark, the B-QR, its position, B-Autograph℠ and B-Fingerprint℠, in a person's own picture: `/watermark/`. The
|
|
154
|
+
fingerprint is `qr/fingerprint.ts`, open and self-contained (`npm run fingerprint:test`).
|
|
149
155
|
- **The card's words are PLACED, never aligned (v1.6.1).** `paintQrCard` measures each line, shrinks its size until it fits,
|
|
150
156
|
and draws it from the left at the center minus half its width. Safari on iOS 26 ignores `textAlign = "center"` and the
|
|
151
157
|
`maxWidth` argument for a complex script: a Khmer caption printed starting at the center line. Measured on the Simulator
|
package/brand.lock
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.
|
|
2
|
+
"version": "1.11.0",
|
|
3
3
|
"files": {
|
|
4
4
|
"css/metta.css": "9bfe986c46c303bf9a48be85322d68e3d626571a424ef3a8aa81faaf5c866692",
|
|
5
5
|
"css/motion.css": "9bfbe39c0de6a9dfaf9fdc2c61e327b3c8e70221c34dff9befd85da448b9e0c2",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"emblem/emblem.svg": "368f520ad2d2f9db50d83ca9cd7aee9475948dc8e38c61b7afa13f7717fef903",
|
|
12
12
|
"emblem/emblem.ts": "6e150389eed576c392ff6231c0b38b8fea5d4c11f7a5b21936bb25afab069890",
|
|
13
13
|
"qr/bqr-card.ts": "55b4cf3d742bb758f789fd18f91737e8da1169610b6b953ad71e95d8bb713168",
|
|
14
|
-
"qr/bqr.ts": "
|
|
14
|
+
"qr/bqr.ts": "fd55381f7298d3b79dcfbec01fbb5b6db117ddaffad48872f59f01bb15a3f6df",
|
|
15
|
+
"qr/fingerprint.ts": "4a587cdb3b5ff6026262f995889c329466a2513044a68c0b5d2fa31c2bdd45a3"
|
|
15
16
|
}
|
|
16
17
|
}
|
package/data/brand.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"into brand.lock, which is vendored byte-identical into every consumer,",
|
|
9
9
|
"so 'which sites are on which brand version' is answerable by grep."
|
|
10
10
|
],
|
|
11
|
-
"version": "1.
|
|
11
|
+
"version": "1.11.0",
|
|
12
12
|
"wordmark": {
|
|
13
13
|
"$comment": [
|
|
14
14
|
"The composition has an internal short-name as of 2026-09-20: B-Wordmark",
|
package/dist/Brand.kt
CHANGED
|
@@ -18,7 +18,7 @@ import androidx.compose.ui.graphics.lerp
|
|
|
18
18
|
import androidx.compose.ui.unit.dp
|
|
19
19
|
|
|
20
20
|
object Brand {
|
|
21
|
-
const val VERSION = "1.
|
|
21
|
+
const val VERSION = "1.11.0"
|
|
22
22
|
|
|
23
23
|
/** The B-Gem media-type palette. A colour here MEANS a medium.
|
|
24
24
|
* Not a site palette, and not the six-TLD rainbow. */
|
package/dist/Brand.swift
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
import SwiftUI
|
|
15
15
|
|
|
16
16
|
public enum Brand {
|
|
17
|
-
public static let version = "1.
|
|
17
|
+
public static let version = "1.11.0"
|
|
18
18
|
|
|
19
19
|
/// The B-Gem media-type palette. A colour here MEANS a medium.
|
|
20
20
|
/// ⚠️ Not a site palette, and not the six-TLD rainbow. Using a gem as an
|
package/dist/tokens.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$comment": "GENERATED by scripts/generate.mjs. Do not edit. Values are parsed from css/tokens.css, which is canonical; semantics come from data/. Run `node scripts/generate.mjs --check` to prove this file is current.",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"gems": [
|
|
5
5
|
{
|
|
6
6
|
"name": "ruby",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@333eco/brand",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"description": "The canonical brand layer for the institution — design tokens, themes, motion, and the B-Emblem. A READ-ONLY MIRROR; institution repos vendor this layer, they do not install it.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Thon Ly",
|
|
@@ -45,7 +45,8 @@
|
|
|
45
45
|
"check": "node scripts/check-brand.mjs",
|
|
46
46
|
"check:publish": "node scripts/check-publish.mjs",
|
|
47
47
|
"generate": "node scripts/generate.mjs",
|
|
48
|
-
"bqr:test": "node scripts/bqr.test.mjs"
|
|
48
|
+
"bqr:test": "node scripts/bqr.test.mjs",
|
|
49
|
+
"fingerprint:test": "node scripts/fingerprint.test.mjs"
|
|
49
50
|
},
|
|
50
51
|
"publishConfig": {
|
|
51
52
|
"access": "public"
|
package/qr/bqr.ts
CHANGED
|
@@ -35,9 +35,9 @@
|
|
|
35
35
|
* chosen by a rule written into the test BEFORE its sweep ran: the LIGHTEST candidate that prints at ISO/IEC 15415
|
|
36
36
|
* symbol-contrast grade A (Rpaper 0.85 − Rink ≥ 0.70) and decodes no worse than the old near-black by more than 2 of 90 cases on two
|
|
37
37
|
* independent decoders. The prediction (#106430) was wrong and the rule's pick was kept — ⛔ never move the rule to a preferred shade.
|
|
38
|
-
* ⭐
|
|
39
|
-
*
|
|
40
|
-
* printed before is reprinted (a code reads at any angle).
|
|
38
|
+
* ⭐ FOUR POSITIONS (v1.11.0, founder 2026-11-24): the middle position marker stands where the ADDRESS puts it (`bqrPosition`): at the heart's point
|
|
39
|
+
* for a B-Link, standard for any other address of the estate; the notch and non-standard are the same two from outside. At the point and
|
|
40
|
+
* the notch the code's mirror line is the heart's (v1.9.0, §turn). Nothing printed before is reprinted (a code reads at any angle).
|
|
41
41
|
* ⭐ DETERMINISTIC: the decoration is seeded from a hash of the text, so the same address always draws the same picture and the preview
|
|
42
42
|
* a person sees is the card they download.
|
|
43
43
|
*
|
|
@@ -256,8 +256,10 @@ export interface BqrOptions {
|
|
|
256
256
|
fitAutograph?: boolean;
|
|
257
257
|
/** ⭐ v1.10.0 — 96 bits in the TOP lobe, for a mark in a person's picture, in the `outline` version only (⛔ throws in `pixels`). */
|
|
258
258
|
coded?: readonly number[];
|
|
259
|
-
/**
|
|
260
|
-
|
|
259
|
+
/** ⭐ v1.11.0 — who draws it. ⛔ Only our API passes "outside"; an app never says where the middle marker stands (the address does). */
|
|
260
|
+
by?: BqrBy;
|
|
261
|
+
/** ⛔ TEST ONLY — the position, whatever the address says. */
|
|
262
|
+
position?: BqrPosition;
|
|
261
263
|
/** ⭐ v1.6.0 — the drawing's color family, drawn at `BQR_INK`'s darkness (`bqrInkFor`). Default: `BQR_INK` itself.
|
|
262
264
|
* ⛔ Products pass `owner`, never `hue`; `hue` is for the brand page following a visitor's `--emblem`. */
|
|
263
265
|
hue?: string;
|
|
@@ -381,14 +383,44 @@ function drawAutograph(g: CanvasRenderingContext2D, a: BqrAutograph, ink: string
|
|
|
381
383
|
}
|
|
382
384
|
|
|
383
385
|
/**
|
|
384
|
-
* ⭐ v1.
|
|
385
|
-
*
|
|
386
|
-
*
|
|
386
|
+
* ⭐ v1.11.0 (founder 2026-11-24: *"keep all four positions"*) — WHERE THE MIDDLE MARKER STANDS SAYS WHAT KIND OF ADDRESS IT IS, read from the address
|
|
387
|
+
* itself: at the heart's POINT for a B-Link, STANDARD (top left) for any other address of the estate; the NOTCH and NON-STANDARD (bottom
|
|
388
|
+
* right) are the same two, drawn by an outsider through our API (`by: "outside"`; nothing passes it until that API exists). The point and
|
|
389
|
+
* the notch are the two where the code's own mirror line is the heart's (§turn), so a B-Link is the symmetric one. A camera reads any angle.
|
|
390
|
+
* ⛔ A declaration, never a proof: anyone can draw any position, so nothing public may say that one shows who drew it.
|
|
387
391
|
*/
|
|
388
|
-
export
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
+
export type BqrPosition = "point" | "notch" | "standard" | "non-standard";
|
|
393
|
+
export type BqrBy = "us" | "outside";
|
|
394
|
+
/** Every domain a B-QR may open a page of: the estate's (b.333.eco's own list; §position K4 holds the two equal). */
|
|
395
|
+
export const BQR_HOSTS: readonly string[] = [
|
|
396
|
+
"333.eco", "heartbank.org", "heartbank.net", "heartbank.com", "heartbank.ceo", "heartbank.me", "heartbank.us",
|
|
397
|
+
"thonly.org", "thonly.com", "thonly.net", "siliconwat.com", "siliconwat.org", "siliconwat.dev",
|
|
398
|
+
"missaquarius.org", "missaquarius.com", "playsey.app", "sayyourname.app", "seysays.org",
|
|
399
|
+
];
|
|
400
|
+
/** What a code's text is: a B-Link, another estate address, or no address at all (a bare gift code). ⛔ Throws for an address anywhere else: our
|
|
401
|
+
* heart never frames an address someone chose. `localhost` and the estate's Firebase preview hosts are a developer's phone. */
|
|
402
|
+
export function bqrKind(text: string, hosts: readonly string[] = BQR_HOSTS): "b-link" | "estate" | "text" {
|
|
403
|
+
if (!/^[a-z][a-z0-9+.-]*:/i.test(text.trim())) return "text";
|
|
404
|
+
let u: URL;
|
|
405
|
+
try { u = new URL(text.trim()); } catch { throw new Error("B-QR: that isn't an address."); }
|
|
406
|
+
if (/^(localhost|127\.0\.0\.1|192\.168\.\d+\.\d+|10\.\d+\.\d+\.\d+)$/.test(u.hostname) || /^(thank3-heartbank-com|heartbank-bc44f)(--[a-z0-9-]+)?\.(web\.app|firebaseapp\.com)$/.test(u.hostname)) return "estate";
|
|
407
|
+
if (u.protocol !== "https:" || u.port || u.username || u.password || !hosts.some((d) => u.hostname === d || u.hostname.endsWith("." + d))) throw new Error("B-QR: only an https address of the estate (" + u.hostname + ").");
|
|
408
|
+
return u.hostname === "b.333.eco" ? "b-link" : "estate";
|
|
409
|
+
}
|
|
410
|
+
export function bqrPosition(text: string, by: BqrBy = "us"): BqrPosition {
|
|
411
|
+
return bqrKind(text) === "b-link" ? (by === "outside" ? "notch" : "point") : by === "outside" ? "non-standard" : "standard";
|
|
412
|
+
}
|
|
413
|
+
/** Quarter turns counterclockwise from upright. */
|
|
414
|
+
export const BQR_TURNS: Readonly<Record<BqrPosition, 0 | 1 | 2 | 3>> = { standard: 0, point: 1, "non-standard": 2, notch: 3 };
|
|
415
|
+
/** The matrix turned that many quarters counterclockwise. Decoders read a code at any angle, so nothing printed before stops working. */
|
|
416
|
+
export function bqrTurned(mat: BqrMatrix, quarters = 1): BqrMatrix {
|
|
417
|
+
let m: BqrMatrix = mat;
|
|
418
|
+
for (let q = 0; q < quarters; q++) {
|
|
419
|
+
const n = m.size, data = new Uint8Array(n * n), from = m.data;
|
|
420
|
+
for (let r = 0; r < n; r++) for (let c = 0; c < n; c++) data[r * n + c] = from[c * n + (n - 1 - r)] ? 1 : 0;
|
|
421
|
+
m = { size: n, data };
|
|
422
|
+
}
|
|
423
|
+
return m;
|
|
392
424
|
}
|
|
393
425
|
|
|
394
426
|
/**
|
|
@@ -441,8 +473,6 @@ export function bqrLobeCells(mat: BqrMatrix, o: { style?: BqrStyle } = {}): [num
|
|
|
441
473
|
if (out.length < 2 * BQR_CODED_BITS) throw new Error("B-QR: this code is too coarse for coded modules.");
|
|
442
474
|
return out.slice(0, 2 * BQR_CODED_BITS);
|
|
443
475
|
}
|
|
444
|
-
/** A drawn cell as the DECODER sees it (the code's own upright grid), for reading coded modules back from a photo of the drawing. */
|
|
445
|
-
export const bqrUpright = (n: number, [c, r]: readonly [number, number]): [number, number] => [n - 1 - r, c];
|
|
446
476
|
|
|
447
477
|
// ---- drawing ------------------------------------------------------------------------------------------------------------------------
|
|
448
478
|
|
|
@@ -454,8 +484,9 @@ export function bqrPlacement(size: number, x = 0, y = 0): { k: number; tx: numbe
|
|
|
454
484
|
|
|
455
485
|
/** Paint a B-QR from a module matrix into a 2D context. Synchronous; `drawBqr` makes the matrix. */
|
|
456
486
|
export function paintBqr(g: CanvasRenderingContext2D, upright: BqrMatrix, seed: string, o: BqrOptions): void {
|
|
457
|
-
// ⭐ v1.
|
|
458
|
-
|
|
487
|
+
// ⭐ v1.11.0 — turned by what the address IS (`bqrPosition`; it throws for an address outside the estate). The layout is the grid's, so the
|
|
488
|
+
// decoration and the outline do not move.
|
|
489
|
+
const where = bqrPosition(seed, o.by), mat = bqrTurned(upright, BQR_TURNS[o.position ?? where]);
|
|
459
490
|
// ⭐ §4AA — one green for the whole drawing: the decoration and the outline default to the code's own ink.
|
|
460
491
|
// ⭐ v1.6.0 — `hue` sets the code, the decoration and the outline at once, darkened by `bqrInkFor`; none = `BQR_INK`, verbatim.
|
|
461
492
|
// ⭐ v1.7.0 — `owner` sets it from the body palette instead; ⛔ both at once throws (a product cannot pick its own color).
|
|
@@ -517,11 +548,11 @@ export async function drawBqr(g: CanvasRenderingContext2D, text: string, o: BqrO
|
|
|
517
548
|
}
|
|
518
549
|
|
|
519
550
|
/** A B-QR as a PNG data URL — the on-screen preview of a code a person may also download, so what they see is what they print. */
|
|
520
|
-
export async function bqrDataUrl(text: string, o: { size?: number; accent?: string; ecc?: "L" | "M" | "Q" | "H"; style?: BqrStyle; ground?: BqrGround; hue?: string; owner?: BqrOwner; lobe?: BqrLobe; autograph?: BqrAutograph } = {}): Promise<string> {
|
|
551
|
+
export async function bqrDataUrl(text: string, o: { size?: number; accent?: string; ecc?: "L" | "M" | "Q" | "H"; style?: BqrStyle; ground?: BqrGround; hue?: string; owner?: BqrOwner; lobe?: BqrLobe; autograph?: BqrAutograph; by?: BqrBy } = {}): Promise<string> {
|
|
521
552
|
const size = o.size ?? 640;
|
|
522
553
|
const canvas = document.createElement("canvas");
|
|
523
554
|
canvas.width = size; canvas.height = size;
|
|
524
555
|
// ⛔ v1.5.0 — `ground: "heart"` only for a picture that stays on screen; a URL handed to a download takes the default `paper`.
|
|
525
|
-
await drawBqr(canvas.getContext("2d")!, text, { size, accent: o.accent, ecc: o.ecc, style: o.style, ground: o.ground, hue: o.hue, owner: o.owner, lobe: o.lobe, autograph: o.autograph });
|
|
556
|
+
await drawBqr(canvas.getContext("2d")!, text, { size, accent: o.accent, ecc: o.ecc, style: o.style, ground: o.ground, hue: o.hue, owner: o.owner, lobe: o.lobe, autograph: o.autograph, by: o.by });
|
|
526
557
|
return canvas.toDataURL("image/png");
|
|
527
558
|
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* B-Fingerprint℠ (founder 2026-11-24): what the dots in a marked heart's TOP lobe say — a short fingerprint of the PICTURE the heart was drawn for
|
|
3
|
+
* (⛔ never of a person), so a heart copied onto another picture is told apart by anyone, with no network. Open on purpose: this file writes it and
|
|
4
|
+
* reads it, and needs nothing but `bqr.ts`.
|
|
5
|
+
* ⛔ It says nothing about a person, carries no serial, no claim and no key, and none of it is secret: someone can redraw a heart for another
|
|
6
|
+
* picture. It catches a COPIED heart; the page the code opens shows the signed picture, and that decides.
|
|
7
|
+
* ⛔ An edited copy of the same picture looks the same to 36 coarse cells: this never says a picture is unchanged.
|
|
8
|
+
* Measured 2026-11-24 on 97 pictures, 194 posted corners at 70% white: the dots read whenever the code did (194 of 194 at 1,080 px q80, 182 of
|
|
9
|
+
* 182 at 720 q70) and never called the right picture different; a heart copied onto another varied photo was told apart 144 times of 152.
|
|
10
|
+
*/
|
|
11
|
+
import { BQR_CODED_BITS, BQR_GEOMETRY, BQR_QUIET, BQR_TURNS, bqrLobeCells, bqrPlacement, bqrTurned, type BqrMatrix, type BqrPosition } from "./bqr";
|
|
12
|
+
export const GRID = 6, CELLS = GRID * GRID;
|
|
13
|
+
export type Kind = "corner" | "strip";
|
|
14
|
+
const KIND: Record<Kind, number> = { corner: 1, strip: 2 }; // the first four bits; any other number is not ours
|
|
15
|
+
export interface Box { x: number; y: number; s: number }
|
|
16
|
+
export interface Print { bits: number[]; seen: boolean[] }
|
|
17
|
+
/** 36 cells of a picture, each brighter or not than the picture's own middle; the cells the heart stands on are not seen. */
|
|
18
|
+
export function fingerprint(rgba: ArrayLike<number>, w: number, h: number, heart: Box | null): Print {
|
|
19
|
+
const sum = new Float64Array(CELLS), n = new Float64Array(CELLS);
|
|
20
|
+
for (let y = 0; y < h; y++) { const r = Math.min(GRID - 1, Math.floor(y * GRID / h)) * GRID; for (let x = 0, i = y * w * 4; x < w; x++, i += 4) { const c = r + Math.min(GRID - 1, Math.floor(x * GRID / w)); sum[c] += 0.299 * rgba[i] + 0.587 * rgba[i + 1] + 0.114 * rgba[i + 2]; n[c]++; } }
|
|
21
|
+
const mean = Array.from(sum, (v, i) => v / Math.max(1, n[i])), seen = mean.map(() => true);
|
|
22
|
+
if (heart) {
|
|
23
|
+
const p = heart.s * 0.04, x0 = (heart.x - p) * GRID / w, x1 = (heart.x + heart.s + p) * GRID / w, y0 = (heart.y - p) * GRID / h, y1 = (heart.y + heart.s + p) * GRID / h;
|
|
24
|
+
for (let r = 0; r < GRID; r++) for (let c = 0; c < GRID; c++) if (c + 1 > x0 && c < x1 && r + 1 > y0 && r < y1) seen[r * GRID + c] = false;
|
|
25
|
+
}
|
|
26
|
+
const used = mean.filter((_, i) => seen[i]).sort((a, b) => a - b), mid = used.length ? (used[(used.length - 1) >> 1] + used[used.length >> 1]) / 2 : 0;
|
|
27
|
+
return { bits: mean.map((v, i) => (seen[i] && v > mid ? 1 : 0)), seen };
|
|
28
|
+
}
|
|
29
|
+
// ---- the error correction: BCH over GF(2^7), 40 data bits + 56, up to 8 wrong bits put right. Written here so a reader needs nothing else;
|
|
30
|
+
// the same code TrustMark's BCH_SUPER is (polynomial 137), held to it by three recorded words in scripts/fingerprint.test.mjs ----
|
|
31
|
+
const M = 7, N = 127, T = 8, DATA = 40, ECC = 56;
|
|
32
|
+
const EXP = new Uint8Array(N * 2), LOG = new Uint8Array(N + 1);
|
|
33
|
+
for (let i = 0, x = 1; i < N; i++) { EXP[i] = EXP[i + N] = x; LOG[x] = i; x <<= 1; if (x & (1 << M)) x ^= 137; }
|
|
34
|
+
const mul = (a: number, b: number) => (a && b ? EXP[LOG[a] + LOG[b]] : 0);
|
|
35
|
+
/** The generator: the least polynomial with α¹ … α¹⁶ among its roots, highest power first. */
|
|
36
|
+
const GEN = (() => {
|
|
37
|
+
const roots = new Set<number>();
|
|
38
|
+
for (let j = 1; j <= 2 * T; j++) for (let k = 0, r = j; k < M; k++, r = (r * 2) % N) roots.add(r);
|
|
39
|
+
let g = [1];
|
|
40
|
+
for (const r of roots) { const n = new Array<number>(g.length + 1).fill(0); g.forEach((c, i) => { n[i] ^= c; n[i + 1] ^= mul(c, EXP[r]); }); g = n; }
|
|
41
|
+
return g;
|
|
42
|
+
})();
|
|
43
|
+
export function bchEncode(data: readonly number[]): number[] {
|
|
44
|
+
const r = [...data, ...new Array<number>(ECC).fill(0)];
|
|
45
|
+
for (let i = 0; i < DATA; i++) if (r[i]) for (let k = 0; k <= ECC; k++) r[i + k] ^= GEN[k];
|
|
46
|
+
return r.slice(DATA);
|
|
47
|
+
}
|
|
48
|
+
/** The 40 data bits, up to 8 wrong of the 96 put right; null when there are more. */
|
|
49
|
+
export function bchDecode(word: readonly number[]): number[] | null {
|
|
50
|
+
const n = DATA + ECC, s: number[] = [];
|
|
51
|
+
for (let j = 1; j <= 2 * T; j++) { let v = 0; for (let i = 0; i < n; i++) if (word[i]) v ^= EXP[(j * (n - 1 - i)) % N]; s.push(v); }
|
|
52
|
+
if (s.every((v) => v === 0)) return word.slice(0, DATA);
|
|
53
|
+
let c = [1], b = [1], L = 0, m = 1, bb = 1;
|
|
54
|
+
for (let k = 0; k < 2 * T; k++) {
|
|
55
|
+
let d = s[k]; for (let i = 1; i <= L; i++) d ^= mul(c[i] ?? 0, s[k - i]);
|
|
56
|
+
if (!d) { m++; continue; }
|
|
57
|
+
const t = [...c], f = EXP[(LOG[d] + N - LOG[bb]) % N];
|
|
58
|
+
for (let i = 0; i < b.length; i++) c[i + m] = (c[i + m] ?? 0) ^ mul(f, b[i]);
|
|
59
|
+
for (let i = 0; i < c.length; i++) c[i] ??= 0;
|
|
60
|
+
if (2 * L <= k) { L = k + 1 - L; b = t; bb = d; m = 1; } else m++;
|
|
61
|
+
}
|
|
62
|
+
if (L > T) return null;
|
|
63
|
+
const out = [...word]; let found = 0;
|
|
64
|
+
for (let i = 0; i < n; i++) { const x = (N - ((n - 1 - i) % N)) % N; let v = 0; for (let k = 0; k <= L; k++) v ^= mul(c[k] ?? 0, EXP[(k * x) % N]); if (!v) { out[i] ^= 1; found++; } }
|
|
65
|
+
return found === L ? out.slice(0, DATA) : null;
|
|
66
|
+
}
|
|
67
|
+
/** The 96 dots: the kind (4 bits), the 36 cells, and 56 bits that put up to 8 wrong ones right. */
|
|
68
|
+
export function codedOf(kind: Kind, p: Print): number[] {
|
|
69
|
+
const d = [...Array.from({ length: 4 }, (_, i) => (KIND[kind] >> (3 - i)) & 1), ...p.bits];
|
|
70
|
+
return [...d, ...bchEncode(d)];
|
|
71
|
+
}
|
|
72
|
+
/** The kind and cells these 96 read values carry, or null: not ours, or too damaged to put right. */
|
|
73
|
+
export function readCoded(bits: readonly boolean[]): { kind: Kind; bits: number[] } | null {
|
|
74
|
+
const d = bits.length === BQR_CODED_BITS ? bchDecode(bits.map(Number)) : null;
|
|
75
|
+
if (!d) return null;
|
|
76
|
+
const k = parseInt(d.slice(0, 4).join(""), 2), kind = (Object.keys(KIND) as Kind[]).find((x) => KIND[x] === k);
|
|
77
|
+
return kind ? { kind, bits: d.slice(4) } : null;
|
|
78
|
+
}
|
|
79
|
+
export type Pt = { x: number; y: number };
|
|
80
|
+
type Gray = (x: number, y: number) => number;
|
|
81
|
+
/** Light between pixels: at a small size a dot is two or three of them. */
|
|
82
|
+
export function gray(rgba: ArrayLike<number>, w: number, h: number): Gray {
|
|
83
|
+
const g = (a: number, b: number) => { const i = (b * w + a) * 4; return 0.299 * rgba[i] + 0.587 * rgba[i + 1] + 0.114 * rgba[i + 2]; };
|
|
84
|
+
return (x, y) => { const X = Math.max(0, Math.min(w - 1.001, x - 0.5)), Y = Math.max(0, Math.min(h - 1.001, y - 0.5)), a = Math.floor(X), b = Math.floor(Y), fx = X - a, fy = Y - b; return (g(a, b) * (1 - fx) + g(a + 1, b) * fx) * (1 - fy) + (g(a, b + 1) * (1 - fx) + g(a + 1, b + 1) * fx) * fy; };
|
|
85
|
+
}
|
|
86
|
+
/** Where the DRAWN (turned) grid lies: its left, its top and one module, in pixels. A posted picture is upright, so nothing else is needed. */
|
|
87
|
+
export interface Grid { x: number; y: number; m: number; position?: BqrPosition }
|
|
88
|
+
/**
|
|
89
|
+
* The grid, found again from the code itself: the reader's corners are about a pixel out, and the dots stand a whole code's width away, so
|
|
90
|
+
* the 625 modules, whose values are known, are laid over the photo and moved until they fit best. Null when they don't (a tilted photo).
|
|
91
|
+
*/
|
|
92
|
+
export function register(upright: BqrMatrix, light: Gray, corners: readonly Pt[]): Grid | null {
|
|
93
|
+
let best: { grid: Grid; score: number } | null = null;
|
|
94
|
+
for (const position of ["point", "notch"] as const) { const r = fitted(bqrTurned(upright, BQR_TURNS[position]), light, corners); if (r && (!best || r.score > best.score)) best = { grid: { ...r.grid, position }, score: r.score }; }
|
|
95
|
+
return best?.grid ?? null;
|
|
96
|
+
}
|
|
97
|
+
function fitted(mat: BqrMatrix, light: Gray, corners: readonly Pt[]): { grid: Grid; score: number } | null {
|
|
98
|
+
const n = mat.size, xs = corners.map((p) => p.x).sort((a, b) => a - b), ys = corners.map((p) => p.y).sort((a, b) => a - b);
|
|
99
|
+
const start: Grid = { x: (xs[0] + xs[1]) / 2, y: (ys[0] + ys[1]) / 2, m: ((xs[2] + xs[3] - xs[0] - xs[1]) / 2 + (ys[2] + ys[3] - ys[0] - ys[1]) / 2) / 2 / n };
|
|
100
|
+
if (!(start.m > 0.5)) return null;
|
|
101
|
+
const cells: [number, number, number][] = []; // drawn column, drawn row, +1 light / −1 dark
|
|
102
|
+
let dark = 0;
|
|
103
|
+
for (let r = 0; r < n; r++) for (let c = 0; c < n; c++) { const d = mat.data[r * n + c] ? 1 : 0; dark += d; cells.push([c, r, d ? -1 : 1]); }
|
|
104
|
+
// near two corners of each module: its middle alone says nothing of where a large one ends
|
|
105
|
+
const fit = (g: Grid) => { let a = 0, b = 0; for (const [c, r, v] of cells) { const l = (light(g.x + (c + 0.22) * g.m, g.y + (r + 0.22) * g.m) + light(g.x + (c + 0.78) * g.m, g.y + (r + 0.78) * g.m)) / 2; if (v > 0) a += l; else b += l; } return a / (n * n - dark) - b / dark; };
|
|
106
|
+
let best = start, score = fit(start);
|
|
107
|
+
for (const step of [0.6, 0.3, 0.15, 0.07]) for (let turn = 0; turn < 6; turn++) {
|
|
108
|
+
let moved = false;
|
|
109
|
+
for (const g of [{ ...best, x: best.x + step }, { ...best, x: best.x - step }, { ...best, y: best.y + step }, { ...best, y: best.y - step },
|
|
110
|
+
{ x: best.x - step * 0.5, y: best.y - step * 0.5, m: best.m + step / n }, { x: best.x + step * 0.5, y: best.y + step * 0.5, m: best.m - step / n }]) { const q = fit(g); if (q > score) { best = g; score = q; moved = true; } }
|
|
111
|
+
if (!moved) break;
|
|
112
|
+
}
|
|
113
|
+
return score > 25 ? { grid: best, score } : null;
|
|
114
|
+
}
|
|
115
|
+
/** The 96 values in a photo of the drawing: each dot's light, both places added, judged against the lobe's own light and dark. */
|
|
116
|
+
export function sampleCoded(mat: BqrMatrix, light: Gray, g: Grid): boolean[] {
|
|
117
|
+
const v = bqrLobeCells(mat).map(([c, r]) => { let s = 0; for (const [dx, dy] of [[0, 0], [-0.2, 0], [0.2, 0], [0, -0.2], [0, 0.2]]) s += light(g.x + (c + 0.5 + dx) * g.m, g.y + (r + 0.5 + dy) * g.m); return s / 5; });
|
|
118
|
+
const sorted = [...v].sort((a, b) => a - b), mid = (sorted[Math.floor(v.length * 0.15)] + sorted[Math.floor(v.length * 0.85)]) / 2;
|
|
119
|
+
return Array.from({ length: BQR_CODED_BITS }, (_, i) => (mid - v[i]) + (mid - v[i + BQR_CODED_BITS]) > 0);
|
|
120
|
+
}
|
|
121
|
+
/** Where the whole heart's square stands in the photo. */
|
|
122
|
+
export function heartBox(mat: BqrMatrix, g: Grid): Box {
|
|
123
|
+
const G = BQR_GEOMETRY.outline, m = G.body.s / (mat.size + 2 * BQR_QUIET), one = bqrPlacement(1), s = g.m / m / one.k;
|
|
124
|
+
return { s, x: g.x - (one.tx + (G.body.x + BQR_QUIET * m) * one.k) * s, y: g.y - (one.ty + (G.body.y + BQR_QUIET * m) * one.k) * s };
|
|
125
|
+
}
|
|
126
|
+
/** How the dots stand to the picture around them. ⛔ Never "unchanged": "same" means it looks like the picture the heart was drawn for. */
|
|
127
|
+
export const NEAR = 0.2;
|
|
128
|
+
export function drawnFor(read: { kind: Kind; bits: number[] }, rgba: ArrayLike<number>, w: number, h: number, box: Box): { answer: "same" | "different"; off: number; of: number } {
|
|
129
|
+
let H = h, heart: Box | null = box;
|
|
130
|
+
if (read.kind === "strip") { H = Math.max(GRID, Math.min(h, Math.round(box.y - (box.s / 0.9) * 0.05))); heart = null; }
|
|
131
|
+
const p = fingerprint(rgba, w, H, heart);
|
|
132
|
+
let off = 0, of = 0;
|
|
133
|
+
for (let i = 0; i < CELLS; i++) if (p.seen[i]) { of++; if (p.bits[i] !== read.bits[i]) off++; }
|
|
134
|
+
return { answer: off <= NEAR * of ? "same" : "different", off, of };
|
|
135
|
+
}
|
|
136
|
+
|