@litfamily/litclaude 1.0.35 → 1.0.36
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/CHANGELOG.md +52 -44
- package/README.md +8 -8
- package/README_ko-KR.md +8 -8
- package/bin/litclaude-ai.js +24 -11
- package/docs/agents.md +5 -0
- package/docs/assets/cover-motion-still.webp +0 -0
- package/docs/assets/cover-motion.webp +0 -0
- package/docs/assets/cover.webp +0 -0
- package/docs/assets/litfamily-machines.png +0 -0
- package/docs/assets/readme/ascii-readme.svg +1 -1
- package/docs/assets/readme/badge-version.svg +1 -1
- package/docs/assets/skills/lit-brainstorm.webp +0 -0
- package/docs/hooks.md +77 -14
- package/docs/migration.md +2 -2
- package/package.json +2 -1
- package/plugins/litclaude/.claude-plugin/plugin.json +1 -1
- package/plugins/litclaude/bin/litclaude-hook.js +164 -15
- package/plugins/litclaude/bin/litclaude-lsp-doctor.js +10 -3
- package/plugins/litclaude/bin/litclaude-mcp.js +1 -1
- package/plugins/litclaude/commands/deep-interview.md +4 -0
- package/plugins/litclaude/commands/lit-brainstorm.md +108 -0
- package/plugins/litclaude/commands/litgoal.md +1 -1
- package/plugins/litclaude/commands/review-work.md +13 -4
- package/plugins/litclaude/hooks/lit-band-client.mjs +7 -4
- package/plugins/litclaude/hooks/lit-mod.mjs +35 -10
- package/plugins/litclaude/lib/automatic-update.mjs +15 -4
- package/plugins/litclaude/lib/canonical-skill-catalog.mjs +1 -0
- package/plugins/litclaude/lib/canonical-skill-resources.mjs +66 -32
- package/plugins/litclaude/lib/lit-activation.mjs +60 -0
- package/plugins/litclaude/lib/lit-mark.mjs +1 -1
- package/plugins/litclaude/lib/mod/frame.mjs +24 -7
- package/plugins/litclaude/lib/mod/motion.mjs +2 -1
- package/plugins/litclaude/lib/mod/skills.mjs +1 -0
- package/plugins/litclaude/lib/mod/svg-card.mjs +2 -2
- package/plugins/litclaude/lib/mod/timeline.mjs +12 -0
- package/plugins/litclaude/lib/mod/toasts.mjs +89 -0
- package/plugins/litclaude/lib/mod/view.mjs +16 -10
- package/plugins/litclaude/lib/mod/workflow.mjs +1 -0
- package/plugins/litclaude/lib/motion-render-gate.mjs +9 -4
- package/plugins/litclaude/lib/npm-invocation.mjs +64 -0
- package/plugins/litclaude/lib/office-runtime.mjs +40 -14
- package/plugins/litclaude/lib/office_runtime_bootstrap.py +13 -1
- package/plugins/litclaude/lib/rules/engine.mjs +23 -5
- package/plugins/litclaude/lib/start-work-lifecycle.mjs +125 -9
- package/plugins/litclaude/skills/deep-interview/SKILL.md +1 -1
- package/plugins/litclaude/skills/lit-brainstorm/SKILL.md +430 -0
- package/plugins/litclaude/skills/lit-brainstorm/references/idea-lenses.md +192 -0
- package/plugins/litclaude/skills/lit-brainstorm/references/scoring-and-convergence.md +149 -0
- package/plugins/litclaude/skills/lit-brainstorm/references/session-notes-template.md +106 -0
- package/plugins/litclaude/skills/lit-diagram-drawer/scripts/export.mjs +3 -4
- package/plugins/litclaude/skills/lit-diagram-drawer/scripts/renderer.mjs +10 -2
- package/plugins/litclaude/skills/lit-docx/SKILL.md +1 -1
- package/plugins/litclaude/skills/lit-docx/scripts/convert_md_to_pdf.py +23 -0
- package/plugins/litclaude/skills/lit-docx/templates/latex/acs/template.tex +14 -0
- package/plugins/litclaude/skills/lit-docx/templates/latex/elsevier/template.tex +14 -0
- package/plugins/litclaude/skills/lit-docx/templates/latex/ieee/template.tex +14 -0
- package/plugins/litclaude/skills/lit-docx/templates/latex/korean-generic/template.tex +14 -0
- package/plugins/litclaude/skills/lit-docx/templates/latex/nature/template.tex +14 -0
- package/plugins/litclaude/skills/lit-loop/SKILL.md +11 -0
- package/plugins/litclaude/skills/lit-plan/SKILL.md +2 -1
- package/plugins/litclaude/skills/lit-typographic-motion/SKILL.md +14 -5
- package/plugins/litclaude/skills/lit-typographic-motion/engine/THIRD_PARTY_NOTICES +6 -0
- package/plugins/litclaude/skills/lit-typographic-motion/engine/core/constants.mjs +35 -0
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/chrome.mjs +3 -7
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/cover-assets.mjs +102 -0
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/cover-capture.mjs +210 -0
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/cover-gate.mjs +346 -0
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/cover-image.mjs +141 -0
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/cover-sheet.mjs +76 -0
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/cover-treatment.mjs +106 -0
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/cover-worker.mjs +32 -0
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/cover.mjs +373 -0
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/done.mjs +68 -2
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/encode.mjs +3 -5
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/host.mjs +31 -0
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/look.mjs +53 -17
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/runtime.mjs +42 -12
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/stage-capture.mjs +2 -1
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/stage-serve.mjs +6 -1
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/stage.mjs +2 -2
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/treatment.mjs +2 -1
- package/plugins/litclaude/skills/lit-typographic-motion/engine/node/webp.mjs +145 -0
- package/plugins/litclaude/skills/lit-typographic-motion/engine/stage/cover-init.js +37 -0
- package/plugins/litclaude/skills/lit-typographic-motion/engine/stage/loop-kit.js +106 -0
- package/plugins/litclaude/skills/lit-typographic-motion/fonts/jetbrains-mono/JetBrainsMono-wght-latin.woff2 +0 -0
- package/plugins/litclaude/skills/lit-typographic-motion/fonts/jetbrains-mono/OFL.txt +93 -0
- package/plugins/litclaude/skills/lit-typographic-motion/fonts/manifest.json +18 -0
- package/plugins/litclaude/skills/lit-typographic-motion/recipes/negatives/card-scenes/expected.json +16 -0
- package/plugins/litclaude/skills/lit-typographic-motion/recipes/negatives/card-scenes/index.html +27 -0
- package/plugins/litclaude/skills/lit-typographic-motion/recipes/negatives/reset-sweep/expected.json +16 -0
- package/plugins/litclaude/skills/lit-typographic-motion/recipes/negatives/reset-sweep/index.html +17 -0
- package/plugins/litclaude/skills/lit-typographic-motion/recipes/negatives/still-glow/expected.json +17 -0
- package/plugins/litclaude/skills/lit-typographic-motion/recipes/negatives/still-glow/index.html +18 -0
- package/plugins/litclaude/skills/lit-typographic-motion/recipes/negatives/title-slam/expected.json +17 -0
- package/plugins/litclaude/skills/lit-typographic-motion/recipes/negatives/title-slam/index.html +30 -0
- package/plugins/litclaude/skills/lit-typographic-motion/recipes/orbit-loop/expected.json +67 -0
- package/plugins/litclaude/skills/lit-typographic-motion/recipes/orbit-loop/index.html +152 -0
- package/plugins/litclaude/skills/lit-typographic-motion/recipes/painted-light/expected.json +95 -0
- package/plugins/litclaude/skills/lit-typographic-motion/recipes/painted-light/index.html +202 -0
- package/plugins/litclaude/skills/lit-typographic-motion/recipes/weight-tide/expected.json +67 -0
- package/plugins/litclaude/skills/lit-typographic-motion/recipes/weight-tide/index.html +100 -0
- package/plugins/litclaude/skills/lit-typographic-motion/references/cover.md +372 -0
- package/plugins/litclaude/skills/lit-typographic-motion/references/look.md +2 -1
- package/plugins/litclaude/skills/lit-typographic-motion/references/runtime.md +4 -3
- package/plugins/litclaude/skills/lit-typographic-motion/references/stage.md +1 -0
- package/plugins/litclaude/skills/lit-typographic-motion/references/treatment.md +4 -0
- package/plugins/litclaude/skills/lit-typographic-motion/scripts/motion-doctor.mjs +26 -6
- package/plugins/litclaude/skills/lit-typographic-motion/scripts/motion.mjs +43 -10
- package/plugins/litclaude/skills/litgoal/SKILL.md +9 -1
- package/plugins/litclaude/skills/lsp-setup/scripts/verify-lsp.ts +47 -6
- package/plugins/litclaude/skills/review-work/SKILL.md +37 -1
- package/plugins/litclaude/skills/start-work/SKILL.md +5 -0
- package/scripts/doctor.mjs +2 -2
- package/scripts/qa-cover-recipes.mjs +172 -0
- package/scripts/validate-plugin.mjs +12 -2
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
// The cover gate: G0 determinism, G1 budget and loop, G2 motion energy, G3 anti-slide, G4 seam,
|
|
2
|
+
// G5 title contrast and G6 title glyph height, measured on the DECODED cover (animated WebP, or the
|
|
3
|
+
// GIF fallback), never only on the frames that went into the encoder. Every metric runs on a fixed
|
|
4
|
+
// time grid, because an encoder merges identical frames into one long frame and frame-count
|
|
5
|
+
// statistics would make a slide film look busy. Two tiers: FLOOR fails the cover, TARGET is the
|
|
6
|
+
// cover-mode goal and `--strict` fails on it too, the 2 MiB size target included. Thresholds live
|
|
7
|
+
// in core/constants.mjs (COVER).
|
|
8
|
+
import { createHash } from "node:crypto";
|
|
9
|
+
import { spawnSync } from "node:child_process";
|
|
10
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
import { COVER } from "../core/constants.mjs";
|
|
13
|
+
import { lumaGrid } from "./cover-image.mjs";
|
|
14
|
+
import { findFfmpeg } from "./encode.mjs";
|
|
15
|
+
import { decodePng } from "./png.mjs";
|
|
16
|
+
import { decodeWebp } from "./webp.mjs";
|
|
17
|
+
|
|
18
|
+
const median = (xs) => {
|
|
19
|
+
const s = [...xs].sort((a, b) => a - b);
|
|
20
|
+
const m = s.length >> 1;
|
|
21
|
+
return s.length % 2 ? s[m] : (s[m - 1] + s[m]) / 2;
|
|
22
|
+
};
|
|
23
|
+
const pct = (xs, p) => {
|
|
24
|
+
const s = [...xs].sort((a, b) => a - b);
|
|
25
|
+
return s[Math.min(s.length - 1, Math.max(0, Math.round(p * (s.length - 1))))];
|
|
26
|
+
};
|
|
27
|
+
const meanAbs = (a, b) => {
|
|
28
|
+
let s = 0;
|
|
29
|
+
for (let i = 0; i < a.length; i++) s += Math.abs(a[i] - b[i]);
|
|
30
|
+
return s / a.length / 255;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const toRgba = ({ width, height, channels, pixels }) => {
|
|
34
|
+
if (channels === 4) return pixels;
|
|
35
|
+
const out = Buffer.alloc(width * height * 4, 255);
|
|
36
|
+
for (let i = 0; i < width * height; i++) for (let c = 0; c < 3; c++) out[i * 4 + c] = pixels[i * channels + (channels === 1 ? 0 : c)];
|
|
37
|
+
return out;
|
|
38
|
+
};
|
|
39
|
+
export const readPngRgba = (file) => {
|
|
40
|
+
const png = decodePng(readFileSync(file));
|
|
41
|
+
return { width: png.width, height: png.height, rgba: toRgba(png) };
|
|
42
|
+
};
|
|
43
|
+
export const pngFrames = (dir, prefix = "f") => (existsSync(dir) ? readdirSync(dir).filter((f) => f.startsWith(prefix) && f.endsWith(".png")).sort().map((f) => path.join(dir, f)) : []);
|
|
44
|
+
|
|
45
|
+
/** GIF container facts the decoder pipe does not give back: per-frame delays (ms) and the loop count. */
|
|
46
|
+
export function gifTiming(bytes) {
|
|
47
|
+
if (bytes.toString("latin1", 0, 3) !== "GIF") throw new Error("not a GIF file");
|
|
48
|
+
const delays = [];
|
|
49
|
+
let loop = null;
|
|
50
|
+
let pending = 100;
|
|
51
|
+
let o = 13;
|
|
52
|
+
if (bytes[10] & 0x80) o += 3 * (1 << ((bytes[10] & 0x07) + 1));
|
|
53
|
+
const skipBlocks = (p) => {
|
|
54
|
+
while (p < bytes.length && bytes[p] !== 0) p += bytes[p] + 1;
|
|
55
|
+
return p + 1;
|
|
56
|
+
};
|
|
57
|
+
while (o < bytes.length) {
|
|
58
|
+
const sep = bytes[o];
|
|
59
|
+
if (sep === 0x3b) break;
|
|
60
|
+
if (sep === 0x21) {
|
|
61
|
+
const label = bytes[o + 1];
|
|
62
|
+
if (label === 0xf9) pending = 10 * bytes.readUInt16LE(o + 4);
|
|
63
|
+
if (label === 0xff && bytes.toString("latin1", o + 3, o + 14) === "NETSCAPE2.0") loop = bytes.readUInt16LE(o + 16);
|
|
64
|
+
o = skipBlocks(o + 2);
|
|
65
|
+
} else if (sep === 0x2c) {
|
|
66
|
+
let p = o + 10;
|
|
67
|
+
if (bytes[o + 9] & 0x80) p += 3 * (1 << ((bytes[o + 9] & 0x07) + 1));
|
|
68
|
+
o = skipBlocks(p + 1);
|
|
69
|
+
delays.push(pending > 0 ? pending : 100);
|
|
70
|
+
pending = 100;
|
|
71
|
+
} else break;
|
|
72
|
+
}
|
|
73
|
+
return { delays, loop, width: bytes.readUInt16LE(6), height: bytes.readUInt16LE(8) };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Load a cover for the gate: { lumas, delays, width, height, loop, bytes, format, kept }. `keep(i,
|
|
78
|
+
* durationMs)` is called once per decoded frame, in order, and says which frames to keep whole (RGBA)
|
|
79
|
+
* for the sheet, the poster and the frame-0 checks.
|
|
80
|
+
*/
|
|
81
|
+
export function loadCover(input, { fps = 16, keep = () => false, env = process.env } = {}) {
|
|
82
|
+
const kept = new Map();
|
|
83
|
+
const lumas = [];
|
|
84
|
+
if (statSync(input).isDirectory()) {
|
|
85
|
+
const files = pngFrames(input);
|
|
86
|
+
let width = 0;
|
|
87
|
+
let height = 0;
|
|
88
|
+
files.forEach((f, i) => {
|
|
89
|
+
const img = readPngRgba(f);
|
|
90
|
+
({ width, height } = img);
|
|
91
|
+
lumas.push(lumaGrid(img.rgba, width, height).luma);
|
|
92
|
+
if (keep(i, 1000 / fps)) kept.set(i, img.rgba);
|
|
93
|
+
});
|
|
94
|
+
return { lumas, delays: files.map(() => 1000 / fps), width, height, loop: 0, bytes: null, format: "png", kept };
|
|
95
|
+
}
|
|
96
|
+
if (input.toLowerCase().endsWith(".gif")) {
|
|
97
|
+
const bytes = readFileSync(input);
|
|
98
|
+
const timing = gifTiming(bytes);
|
|
99
|
+
const ffmpeg = findFfmpeg(env);
|
|
100
|
+
if (!ffmpeg) throw new Error("ffmpeg is needed to decode a GIF cover");
|
|
101
|
+
const raw = spawnSync(ffmpeg, ["-v", "error", "-i", input, "-fps_mode", "passthrough", "-f", "rawvideo", "-pix_fmt", "rgba", "pipe:1"], { maxBuffer: 2 ** 31 });
|
|
102
|
+
if (raw.status !== 0) throw new Error(`ffmpeg could not decode ${input}: ${String(raw.stderr).trim().split("\n")[0]}`);
|
|
103
|
+
const size = timing.width * timing.height * 4;
|
|
104
|
+
for (let i = 0; i * size < raw.stdout.length; i++) {
|
|
105
|
+
const rgba = raw.stdout.subarray(i * size, (i + 1) * size);
|
|
106
|
+
lumas.push(lumaGrid(rgba, timing.width, timing.height).luma);
|
|
107
|
+
if (keep(i, timing.delays[i] ?? 100)) kept.set(i, Buffer.from(rgba));
|
|
108
|
+
}
|
|
109
|
+
return { lumas, delays: timing.delays.slice(0, lumas.length), width: timing.width, height: timing.height, loop: timing.loop, bytes: bytes.length, format: "gif", kept };
|
|
110
|
+
}
|
|
111
|
+
const info = decodeWebp(input, {
|
|
112
|
+
env,
|
|
113
|
+
onFrame: (i, rgba, ms, width, height) => {
|
|
114
|
+
lumas.push(lumaGrid(rgba, width, height).luma);
|
|
115
|
+
if (keep(i, ms > 0 ? ms : 100)) kept.set(i, Buffer.from(rgba));
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
return { ...info, lumas, delays: info.durations.map((d) => (d > 0 ? d : 100)), format: "webp", kept };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Resample a frame list to one sample per `step` ms (the shown frame at each grid time). */
|
|
122
|
+
export function resample(frames, delays, step) {
|
|
123
|
+
const total = delays.reduce((a, b) => a + b, 0);
|
|
124
|
+
const out = [];
|
|
125
|
+
let i = 0;
|
|
126
|
+
let end = delays[0];
|
|
127
|
+
for (let ms = 0; ms < total - 1e-6; ms += step) {
|
|
128
|
+
while (ms >= end && i < frames.length - 1) end += delays[++i];
|
|
129
|
+
out.push(frames[i]);
|
|
130
|
+
}
|
|
131
|
+
return out;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Motion metrics on the grid; the loop is a ring, so the seam is one more step. */
|
|
135
|
+
export function motionMetrics({ lumas, delays }) {
|
|
136
|
+
const step = Math.max(COVER.stepMs, Math.min(...delays));
|
|
137
|
+
const frames = resample(lumas, delays, step);
|
|
138
|
+
const n = frames.length;
|
|
139
|
+
const d = [];
|
|
140
|
+
for (let i = 1; i < n; i++) d.push(meanAbs(frames[i], frames[i - 1]));
|
|
141
|
+
const seam = meanAbs(frames[0], frames[n - 1]);
|
|
142
|
+
const all = [...d, seam];
|
|
143
|
+
const med = median(all);
|
|
144
|
+
const still = all.map((x) => x < COVER.stillEps);
|
|
145
|
+
let longest = 0;
|
|
146
|
+
if (still.every(Boolean)) longest = (n * step) / 1000;
|
|
147
|
+
else {
|
|
148
|
+
const start = still.findIndex((s) => !s);
|
|
149
|
+
let run = 0;
|
|
150
|
+
for (let j = 1; j <= n; j++) {
|
|
151
|
+
const k = (start + j) % n;
|
|
152
|
+
run = still[(k - 1 + n) % n] ? run + step : step;
|
|
153
|
+
longest = Math.max(longest, run);
|
|
154
|
+
}
|
|
155
|
+
longest /= 1000;
|
|
156
|
+
}
|
|
157
|
+
const around = n >= 4 ? (all[n - 3] + all[n - 2] + all[0] + all[1]) / 4 : med;
|
|
158
|
+
return {
|
|
159
|
+
gridFrames: n, stepMs: step, durationSec: (n * step) / 1000, sourceDurationSec: delays.reduce((a, b) => a + b, 0) / 1000,
|
|
160
|
+
medianD: med, p10D: pct(all, 0.1), p90D: pct(all, 0.9), maxD: Math.max(...all),
|
|
161
|
+
movingFrac: all.filter((x) => x >= COVER.stillEps).length / all.length,
|
|
162
|
+
longestStillSec: longest, spike: Math.max(...all) / Math.max(med, COVER.stillEps), seam,
|
|
163
|
+
seamLocal: seam / Math.max(COVER.stillEps, around), diffs: all,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const lin = (c) => {
|
|
168
|
+
const v = c / 255;
|
|
169
|
+
return v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
|
|
170
|
+
};
|
|
171
|
+
const relL = (r, g, b) => 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b);
|
|
172
|
+
const boxPx = (box, W, H) => box.map((v, k) => Math.round(v * (k % 2 ? H : W)));
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* G5: per frame, the median title pixel against the 90th-percentile background in the title box
|
|
176
|
+
* (background = box pixels at least 3 px from any title pixel). Needs the per-frame title masks the
|
|
177
|
+
* mask pass wrote (white = title); without them G5 fails and names what is missing.
|
|
178
|
+
*/
|
|
179
|
+
export function titleContrast({ framesDir, masksDir, box }) {
|
|
180
|
+
const frames = pngFrames(framesDir, "f");
|
|
181
|
+
const masks = pngFrames(masksDir, "m");
|
|
182
|
+
if (!frames.length || masks.length !== frames.length) return { measured: false, reason: `need one mask per frame (${frames.length} frames, ${masks.length} masks)` };
|
|
183
|
+
const per = [];
|
|
184
|
+
for (let i = 0; i < frames.length; i++) {
|
|
185
|
+
const { width: W, height: H, rgba } = readPngRgba(frames[i]);
|
|
186
|
+
const m = readPngRgba(masks[i]);
|
|
187
|
+
if (m.width !== W || m.height !== H) return { measured: false, reason: `mask ${i} is ${m.width}x${m.height}, the frame ${W}x${H}` };
|
|
188
|
+
const [bx, by, bw, bh] = boxPx(box, W, H);
|
|
189
|
+
const kind = new Uint8Array(W * H);
|
|
190
|
+
for (let p = 0; p < W * H; p++) if (m.rgba[p * 4] > 127) kind[p] = 2;
|
|
191
|
+
const R = 3;
|
|
192
|
+
for (let y = by; y < by + bh; y++) {
|
|
193
|
+
for (let x = bx; x < bx + bw; x++) {
|
|
194
|
+
if (kind[y * W + x] !== 2) continue;
|
|
195
|
+
for (let dy = -R; dy <= R; dy++) {
|
|
196
|
+
for (let dx = -R; dx <= R; dx++) {
|
|
197
|
+
const yy = y + dy;
|
|
198
|
+
const xx = x + dx;
|
|
199
|
+
if (yy >= 0 && yy < H && xx >= 0 && xx < W && kind[yy * W + xx] === 0) kind[yy * W + xx] = 1;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const text = [];
|
|
205
|
+
const bg = [];
|
|
206
|
+
for (let y = by; y < by + bh; y++) {
|
|
207
|
+
for (let x = bx; x < bx + bw; x++) {
|
|
208
|
+
const p = (y * W + x) * 4;
|
|
209
|
+
const L = relL(rgba[p], rgba[p + 1], rgba[p + 2]);
|
|
210
|
+
if (kind[y * W + x] === 2) text.push(L);
|
|
211
|
+
else if (kind[y * W + x] === 0) bg.push(L);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (!text.length || !bg.length) return { measured: false, reason: `frame ${i}: the title box holds ${text.length} title and ${bg.length} background pixels` };
|
|
215
|
+
const tl = median(text);
|
|
216
|
+
const worst = tl > median(bg) ? pct(bg, 0.9) : pct(bg, 0.1);
|
|
217
|
+
per.push((Math.max(tl, worst) + 0.05) / (Math.min(tl, worst) + 0.05));
|
|
218
|
+
}
|
|
219
|
+
return { measured: true, min: Math.min(...per), median: median(per), frames: per.length };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* G6: the title's cap height at the README display width. Per sampled mask frame, the white pixels in
|
|
224
|
+
* the title box split into connected glyphs; glyphs smaller than 1 % of the largest or shorter than
|
|
225
|
+
* 30 % of the tallest (dots, accents) are left out, and the median height of the rest is the cap
|
|
226
|
+
* height. The smallest over the samples, scaled from the cover width to `displayWidth`, must reach
|
|
227
|
+
* COVER.minGlyphPx. A title painted into declared base art is `exempt` (still measured, reported as
|
|
228
|
+
* INFO). G6 sits beside G5: contrast alone never makes a title readable.
|
|
229
|
+
*/
|
|
230
|
+
export function glyphHeights({ masksDir, box, displayWidth = COVER.displayWidth, exempt = null, samples = 12 }) {
|
|
231
|
+
const masks = pngFrames(masksDir, "m");
|
|
232
|
+
if (!masks.length) return { pass: false, reason: "no mask frames to measure the title on" };
|
|
233
|
+
const picks = [...new Set(Array.from({ length: Math.min(samples, masks.length) }, (_, k) => Math.floor((k * masks.length) / Math.min(samples, masks.length))))];
|
|
234
|
+
let cap = Infinity;
|
|
235
|
+
let glyphs = 0;
|
|
236
|
+
let width = 0;
|
|
237
|
+
for (const i of picks) {
|
|
238
|
+
const m = readPngRgba(masks[i]);
|
|
239
|
+
width = m.width;
|
|
240
|
+
const [bx, by, bw, bh] = boxPx(box, m.width, m.height);
|
|
241
|
+
const on = new Uint8Array(bw * bh);
|
|
242
|
+
for (let y = 0; y < bh; y++) for (let x = 0; x < bw; x++) on[y * bw + x] = m.rgba[((by + y) * m.width + bx + x) * 4] > 127 ? 1 : 0;
|
|
243
|
+
const comps = [];
|
|
244
|
+
const stack = [];
|
|
245
|
+
for (let start = 0; start < on.length; start++) {
|
|
246
|
+
if (on[start] !== 1) continue;
|
|
247
|
+
on[start] = 2;
|
|
248
|
+
stack.push(start);
|
|
249
|
+
let area = 0;
|
|
250
|
+
let top = bh;
|
|
251
|
+
let bottom = -1;
|
|
252
|
+
while (stack.length) {
|
|
253
|
+
const q = stack.pop();
|
|
254
|
+
const y = Math.floor(q / bw);
|
|
255
|
+
const x = q - y * bw;
|
|
256
|
+
area += 1;
|
|
257
|
+
if (y < top) top = y;
|
|
258
|
+
if (y > bottom) bottom = y;
|
|
259
|
+
for (const n of [x > 0 ? q - 1 : -1, x < bw - 1 ? q + 1 : -1, y > 0 ? q - bw : -1, y < bh - 1 ? q + bw : -1]) {
|
|
260
|
+
if (n >= 0 && on[n] === 1) {
|
|
261
|
+
on[n] = 2;
|
|
262
|
+
stack.push(n);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
comps.push({ area, height: bottom - top + 1 });
|
|
267
|
+
}
|
|
268
|
+
if (!comps.length) return { pass: false, reason: `mask ${i}: no title pixels in the title box` };
|
|
269
|
+
const maxArea = Math.max(...comps.map((c) => c.area));
|
|
270
|
+
const tallest = Math.max(...comps.map((c) => c.height));
|
|
271
|
+
const letters = comps.filter((c) => c.area >= 0.01 * maxArea && c.height >= 0.3 * tallest);
|
|
272
|
+
glyphs = Math.max(glyphs, letters.length);
|
|
273
|
+
cap = Math.min(cap, median(letters.map((c) => c.height)));
|
|
274
|
+
}
|
|
275
|
+
const capPx = (cap * displayWidth) / width;
|
|
276
|
+
const result = { capPx: +capPx.toFixed(1), coverPx: cap, displayWidth, glyphs, sampled: picks.length };
|
|
277
|
+
if (exempt) return { pass: null, ...result, reason: exempt };
|
|
278
|
+
return { pass: capPx >= COVER.minGlyphPx, ...result };
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** G0: SHA-256 per frame of two independent captures. */
|
|
282
|
+
export function determinism(a, b) {
|
|
283
|
+
const fa = pngFrames(a, "f");
|
|
284
|
+
const fb = pngFrames(b, "f");
|
|
285
|
+
const h = (f) => createHash("sha256").update(readFileSync(f)).digest("hex");
|
|
286
|
+
let same = 0;
|
|
287
|
+
const differing = [];
|
|
288
|
+
const n = Math.min(fa.length, fb.length);
|
|
289
|
+
for (let i = 0; i < n; i++) {
|
|
290
|
+
if (h(fa[i]) === h(fb[i])) same += 1;
|
|
291
|
+
else differing.push(i);
|
|
292
|
+
}
|
|
293
|
+
return { compared: n, identical: same, countsMatch: fa.length === fb.length, firstDiffering: differing[0] ?? null };
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Gate one cover. Options: frames / frames2 (two captures, G0), masks + title (G5), fps (a PNG
|
|
298
|
+
* directory input), keep (frames to keep whole). Returns the metrics, the per-gate verdicts and
|
|
299
|
+
* `pass` (FLOOR) / `target` (TARGET, what --strict needs).
|
|
300
|
+
*/
|
|
301
|
+
export function coverGate(input, { frames = null, frames2 = null, masks = null, title = null, glyph = null, fps = 16, keep, env = process.env } = {}) {
|
|
302
|
+
const src = loadCover(input, { fps, keep, env });
|
|
303
|
+
const m = motionMetrics(src);
|
|
304
|
+
const g = {};
|
|
305
|
+
if (frames && frames2) {
|
|
306
|
+
const det = determinism(frames, frames2);
|
|
307
|
+
g.G0 = { pass: det.countsMatch && det.identical === det.compared && det.compared > 0, ...det };
|
|
308
|
+
}
|
|
309
|
+
if (src.format !== "png") {
|
|
310
|
+
g.G1 = {
|
|
311
|
+
pass: src.bytes <= COVER.maxBytes && m.sourceDurationSec >= COVER.minSec && m.sourceDurationSec <= COVER.maxSec && src.loop === 0,
|
|
312
|
+
target: src.bytes <= COVER.targetBytes,
|
|
313
|
+
bytes: src.bytes, durationSec: m.sourceDurationSec, loop: src.loop, size: `${src.width}x${src.height}`,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
const F = COVER.floor;
|
|
317
|
+
const T = COVER.target;
|
|
318
|
+
g.G2 = { pass: m.medianD >= F.medianD && m.movingFrac >= F.movingFrac, target: m.medianD >= T.medianD && m.movingFrac >= T.movingFrac, medianD: m.medianD, movingFrac: m.movingFrac };
|
|
319
|
+
g.G3 = { pass: m.longestStillSec <= F.stillSec, target: m.longestStillSec <= T.stillSec && m.spike <= T.spike, longestStillSec: m.longestStillSec, spike: m.spike };
|
|
320
|
+
g.G4 = { pass: m.seamLocal <= F.seamLocal, target: m.seamLocal <= T.seamLocal && m.seamLocal >= T.seamLo, seam: m.seam, seamLocal: m.seamLocal };
|
|
321
|
+
if (title && frames) {
|
|
322
|
+
const c = titleContrast({ framesDir: frames, masksDir: masks, box: title });
|
|
323
|
+
g.G5 = c.measured ? { pass: c.min >= COVER.minContrast, min: c.min, median: c.median, frames: c.frames } : { pass: false, reason: c.reason };
|
|
324
|
+
}
|
|
325
|
+
if (title && masks && glyph) g.G6 = glyphHeights({ masksDir: masks, box: title, displayWidth: glyph.displayWidth ?? COVER.displayWidth, exempt: glyph.exempt ?? null });
|
|
326
|
+
const pass = Object.values(g).every((x) => x.pass !== false);
|
|
327
|
+
const target = pass && Object.values(g).every((x) => x.target !== false);
|
|
328
|
+
return { input, format: src.format, width: src.width, height: src.height, bytes: src.bytes, loop: src.loop, sourceFrames: src.lumas.length, durations: src.delays, ...m, gates: g, pass, target, kept: src.kept };
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const num = (x) => (typeof x === "number" && !Number.isInteger(x) ? +x.toPrecision(4) : x);
|
|
332
|
+
|
|
333
|
+
/** The report text: verdict line, size line, one line per gate. */
|
|
334
|
+
export function formatCoverReport(r, { strict = false } = {}) {
|
|
335
|
+
const verdict = r.pass ? (r.target ? "PASS+TARGET" : "PASS(floor)") : "FAIL";
|
|
336
|
+
const lines = [
|
|
337
|
+
`COVER gate${strict ? " --strict" : ""}: ${verdict}`,
|
|
338
|
+
` ${r.width}x${r.height} ${r.format} frames=${r.sourceFrames} dur=${num(r.sourceDurationSec)}s bytes=${r.bytes ?? "-"} loop=${r.loop} grid=${r.stepMs}ms`,
|
|
339
|
+
];
|
|
340
|
+
for (const [k, v] of Object.entries(r.gates)) {
|
|
341
|
+
const { pass, target, ...detail } = v;
|
|
342
|
+
const word = pass === null ? "INFO" : pass ? (target === false ? "pass(floor)" : "pass") : "FAIL";
|
|
343
|
+
lines.push(` ${k} ${word} ${Object.entries(detail).map(([a, b]) => `${a}=${num(b)}`).join(" ")}`);
|
|
344
|
+
}
|
|
345
|
+
return `${lines.join("\n")}\n`;
|
|
346
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// Pixel helpers for cover mode, all on RGBA8 buffers: a separable Lanczos-3 resize (the 2x seek
|
|
2
|
+
// capture is downsampled with it, as the pilot did), the gate's luma grid (Rec. 709 on gamma values,
|
|
3
|
+
// box-averaged to about 300 px wide so every gate number is measured at thumbnail scale), a crop, and
|
|
4
|
+
// an SSIM on luma (mean over 8x8 tiles) that compares the base art in decoded frame 0 with its source.
|
|
5
|
+
|
|
6
|
+
const lanczos = (x, a = 3) => {
|
|
7
|
+
if (x === 0) return 1;
|
|
8
|
+
if (Math.abs(x) >= a) return 0;
|
|
9
|
+
const px = Math.PI * x;
|
|
10
|
+
return (a * Math.sin(px) * Math.sin(px / a)) / (px * px);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/** Per output index: the first source index and its normalized weights (support widens when shrinking). */
|
|
14
|
+
function kernel(src, dst) {
|
|
15
|
+
const scale = src / dst;
|
|
16
|
+
const support = 3 * Math.max(1, scale);
|
|
17
|
+
const rows = [];
|
|
18
|
+
for (let i = 0; i < dst; i++) {
|
|
19
|
+
const center = (i + 0.5) * scale - 0.5;
|
|
20
|
+
const first = Math.max(0, Math.ceil(center - support));
|
|
21
|
+
const last = Math.min(src - 1, Math.floor(center + support));
|
|
22
|
+
const weights = [];
|
|
23
|
+
let sum = 0;
|
|
24
|
+
for (let j = first; j <= last; j++) {
|
|
25
|
+
const w = lanczos((j - center) / Math.max(1, scale));
|
|
26
|
+
weights.push(w);
|
|
27
|
+
sum += w;
|
|
28
|
+
}
|
|
29
|
+
rows.push({ first, weights: weights.map((w) => w / sum) });
|
|
30
|
+
}
|
|
31
|
+
return rows;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Lanczos-3 resize of RGBA8; alpha is kept at 255 (captures are opaque). */
|
|
35
|
+
export function lanczosResize(src, sw, sh, dw, dh) {
|
|
36
|
+
if (sw === dw && sh === dh) return Buffer.from(src);
|
|
37
|
+
const kx = kernel(sw, dw);
|
|
38
|
+
const ky = kernel(sh, dh);
|
|
39
|
+
const tmp = new Float32Array(dw * sh * 3);
|
|
40
|
+
for (let y = 0; y < sh; y++) {
|
|
41
|
+
const row = y * sw * 4;
|
|
42
|
+
for (let x = 0; x < dw; x++) {
|
|
43
|
+
const { first, weights } = kx[x];
|
|
44
|
+
let r = 0;
|
|
45
|
+
let g = 0;
|
|
46
|
+
let b = 0;
|
|
47
|
+
for (let k = 0; k < weights.length; k++) {
|
|
48
|
+
const o = row + (first + k) * 4;
|
|
49
|
+
const w = weights[k];
|
|
50
|
+
r += src[o] * w; g += src[o + 1] * w; b += src[o + 2] * w;
|
|
51
|
+
}
|
|
52
|
+
const t = (y * dw + x) * 3;
|
|
53
|
+
tmp[t] = r; tmp[t + 1] = g; tmp[t + 2] = b;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const out = Buffer.alloc(dw * dh * 4);
|
|
57
|
+
const clamp8 = (v) => (v <= 0 ? 0 : v >= 255 ? 255 : Math.round(v));
|
|
58
|
+
for (let y = 0; y < dh; y++) {
|
|
59
|
+
const { first, weights } = ky[y];
|
|
60
|
+
for (let x = 0; x < dw; x++) {
|
|
61
|
+
let r = 0;
|
|
62
|
+
let g = 0;
|
|
63
|
+
let b = 0;
|
|
64
|
+
for (let k = 0; k < weights.length; k++) {
|
|
65
|
+
const o = ((first + k) * dw + x) * 3;
|
|
66
|
+
const w = weights[k];
|
|
67
|
+
r += tmp[o] * w; g += tmp[o + 1] * w; b += tmp[o + 2] * w;
|
|
68
|
+
}
|
|
69
|
+
const t = (y * dw + x) * 4;
|
|
70
|
+
out[t] = clamp8(r); out[t + 1] = clamp8(g); out[t + 2] = clamp8(b); out[t + 3] = 255;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export const LUMA_WIDTH = 300;
|
|
77
|
+
|
|
78
|
+
/** Box-downsampled luma (0..255) at about LUMA_WIDTH wide: { luma, w, h }. */
|
|
79
|
+
export function lumaGrid(rgba, width, height, channels = 4) {
|
|
80
|
+
const f = Math.max(1, Math.round(width / LUMA_WIDTH));
|
|
81
|
+
const w = Math.floor(width / f);
|
|
82
|
+
const h = Math.floor(height / f);
|
|
83
|
+
const luma = new Float32Array(w * h);
|
|
84
|
+
for (let y = 0; y < h * f; y++) {
|
|
85
|
+
const row = y * width * channels;
|
|
86
|
+
const oy = Math.floor(y / f) * w;
|
|
87
|
+
for (let x = 0; x < w * f; x++) {
|
|
88
|
+
const p = row + x * channels;
|
|
89
|
+
luma[oy + Math.floor(x / f)] += 0.2126 * rgba[p] + 0.7152 * rgba[p + 1] + 0.0722 * rgba[p + 2];
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const n = f * f;
|
|
93
|
+
for (let i = 0; i < luma.length; i++) luma[i] /= n;
|
|
94
|
+
return { luma, w, h };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Copy the rectangle [x, y, w, h] out of an RGBA8 frame. */
|
|
98
|
+
export function crop(rgba, width, [x, y, w, h]) {
|
|
99
|
+
const out = Buffer.alloc(w * h * 4);
|
|
100
|
+
for (let row = 0; row < h; row++) rgba.copy(out, row * w * 4, ((y + row) * width + x) * 4, ((y + row) * width + x + w) * 4);
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** SSIM of two same-size RGBA8 images on luma: the mean of the SSIM term over 8x8 tiles. */
|
|
105
|
+
export function ssim(a, b, width, height) {
|
|
106
|
+
const n = width * height;
|
|
107
|
+
const la = new Float64Array(n);
|
|
108
|
+
const lb = new Float64Array(n);
|
|
109
|
+
for (let i = 0; i < n; i++) {
|
|
110
|
+
la[i] = 0.2126 * a[i * 4] + 0.7152 * a[i * 4 + 1] + 0.0722 * a[i * 4 + 2];
|
|
111
|
+
lb[i] = 0.2126 * b[i * 4] + 0.7152 * b[i * 4 + 1] + 0.0722 * b[i * 4 + 2];
|
|
112
|
+
}
|
|
113
|
+
const T = 8;
|
|
114
|
+
const C1 = (0.01 * 255) ** 2;
|
|
115
|
+
const C2 = (0.03 * 255) ** 2;
|
|
116
|
+
let total = 0;
|
|
117
|
+
let tiles = 0;
|
|
118
|
+
for (let ty = 0; ty + T <= height; ty += T) {
|
|
119
|
+
for (let tx = 0; tx + T <= width; tx += T) {
|
|
120
|
+
let ma = 0;
|
|
121
|
+
let mb = 0;
|
|
122
|
+
for (let y = ty; y < ty + T; y++) for (let x = tx; x < tx + T; x++) { ma += la[y * width + x]; mb += lb[y * width + x]; }
|
|
123
|
+
ma /= T * T;
|
|
124
|
+
mb /= T * T;
|
|
125
|
+
let va = 0;
|
|
126
|
+
let vb = 0;
|
|
127
|
+
let cov = 0;
|
|
128
|
+
for (let y = ty; y < ty + T; y++) {
|
|
129
|
+
for (let x = tx; x < tx + T; x++) {
|
|
130
|
+
const da = la[y * width + x] - ma;
|
|
131
|
+
const db = lb[y * width + x] - mb;
|
|
132
|
+
va += da * da; vb += db * db; cov += da * db;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const m = T * T - 1;
|
|
136
|
+
total += ((2 * ma * mb + C1) * (2 * (cov / m) + C2)) / ((ma * ma + mb * mb + C1) * (va / m + vb / m + C2));
|
|
137
|
+
tiles += 1;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return tiles ? total / tiles : 0;
|
|
141
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// The cover's review artifact: a contact sheet of decoded frames above a frame-difference plot of the
|
|
2
|
+
// whole loop (one point per grid step, the still threshold and the median drawn in). The plot shows
|
|
3
|
+
// what single stills cannot: holds, encoder pops, the reset at the seam. It is laid out as a small
|
|
4
|
+
// local page and captured by the same headless Chrome the cover renderer uses, so its labels are real
|
|
5
|
+
// type. `marks` shades time spans on the plot (a logged defect, a re-rendered window).
|
|
6
|
+
import { copyFileSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { COVER } from "../core/constants.mjs";
|
|
9
|
+
import { CoverBrowser } from "./cover-capture.mjs";
|
|
10
|
+
import { decodePng } from "./png.mjs";
|
|
11
|
+
|
|
12
|
+
const esc = (s) => String(s).replace(/[&<>"]/gu, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]);
|
|
13
|
+
|
|
14
|
+
/** The sheet page: tiles [{ file, caption }] (4 per row), then the plot of `diffs`. */
|
|
15
|
+
export function sheetHtml({ tiles, tileWidth, tileHeight, diffs, medianD, label, status, marks = [], durationSec }) {
|
|
16
|
+
const pad = 8;
|
|
17
|
+
const cols = 4;
|
|
18
|
+
const rows = Math.ceil(tiles.length / cols);
|
|
19
|
+
const width = cols * tileWidth + (cols + 1) * pad;
|
|
20
|
+
const plotH = 170;
|
|
21
|
+
const gridTop = rows * (tileHeight + 22) + (rows + 1) * pad;
|
|
22
|
+
const height = gridTop + 10 + plotH + 40;
|
|
23
|
+
const pw = width - 2 * pad;
|
|
24
|
+
const ymax = Math.max(0.01, ...diffs) * 1.05;
|
|
25
|
+
const X = (i) => pad + (i / Math.max(1, diffs.length - 1)) * pw;
|
|
26
|
+
const Y = (v) => gridTop + 10 + plotH - (v / ymax) * plotH;
|
|
27
|
+
const points = diffs.map((v, i) => `${X(i).toFixed(1)},${Y(v).toFixed(1)}`).join(" ");
|
|
28
|
+
const markX = (sec) => pad + (sec / durationSec) * pw;
|
|
29
|
+
const cells = tiles.map((tile, k) => {
|
|
30
|
+
const x = pad + (k % cols) * (tileWidth + pad);
|
|
31
|
+
const y = pad + Math.floor(k / cols) * (tileHeight + 22 + pad);
|
|
32
|
+
return `<div class="cap" style="left:${x}px;top:${y}px">${esc(tile.caption)}</div><img src="${esc(tile.file)}" style="left:${x}px;top:${y + 22}px;width:${tileWidth}px;height:${tileHeight}px">`;
|
|
33
|
+
}).join("\n");
|
|
34
|
+
const markLines = marks.map((m) => `<rect x="${markX(m.t0).toFixed(1)}" y="${gridTop + 10}" width="${Math.max(2, markX(m.t1) - markX(m.t0)).toFixed(1)}" height="${plotH}" fill="#ffb02e" opacity="0.18"/>`).join("");
|
|
35
|
+
const page = `<!doctype html><meta charset="utf-8"><link rel="stylesheet" href="/lit/fonts.css">
|
|
36
|
+
<style>html,body{margin:0;width:${width}px;height:${height}px;overflow:hidden;background:#080b10}
|
|
37
|
+
img{position:absolute;display:block}.cap{position:absolute;font:13px "JetBrains Mono",monospace;color:#9aa}
|
|
38
|
+
.foot{position:absolute;left:${pad}px;top:${gridTop + 10 + plotH + 12}px;font:14px "JetBrains Mono",monospace;color:#ccd;white-space:nowrap}</style>
|
|
39
|
+
<body>${cells}
|
|
40
|
+
<svg width="${width}" height="${height}" style="position:absolute;left:0;top:0">
|
|
41
|
+
<rect x="${pad}" y="${gridTop + 10}" width="${pw}" height="${plotH}" fill="#11161d"/>${markLines}
|
|
42
|
+
<line x1="${pad}" x2="${pad + pw}" y1="${Y(COVER.stillEps)}" y2="${Y(COVER.stillEps)}" stroke="#a33" stroke-dasharray="4 4"/>
|
|
43
|
+
<line x1="${pad}" x2="${pad + pw}" y1="${Y(medianD)}" y2="${Y(medianD)}" stroke="#5a8" stroke-dasharray="2 3"/>
|
|
44
|
+
<polyline points="${points}" fill="none" stroke="#ff7a46" stroke-width="1.5"/></svg>
|
|
45
|
+
<div class="foot">${esc(label)} | ${esc(status)}</div>
|
|
46
|
+
<script>window.renderFrame = () => {}; Promise.all([...document.images].map((i) => i.decode())).then(() => document.fonts.ready).then(() => { window.ready = true; });</script>`;
|
|
47
|
+
return { page, width, height };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Render the sheet to `file`. `tiles` carry PNG paths on disk; they are copied next to the page
|
|
52
|
+
* (the stage router serves nothing outside its directory) and the work directory is removed after.
|
|
53
|
+
*/
|
|
54
|
+
export async function renderSheet({ file, tiles, diffs, medianD, label, status, marks, durationSec, faces, workDir, env, log }) {
|
|
55
|
+
rmSync(workDir, { recursive: true, force: true });
|
|
56
|
+
mkdirSync(workDir, { recursive: true });
|
|
57
|
+
const first = decodePng(readFileSync(tiles[0].file));
|
|
58
|
+
const tileWidth = 400;
|
|
59
|
+
const tileHeight = Math.round((tileWidth * first.height) / first.width);
|
|
60
|
+
const local = tiles.map((t, i) => {
|
|
61
|
+
const name = `t${String(i).padStart(2, "0")}.png`;
|
|
62
|
+
copyFileSync(t.file, path.join(workDir, name));
|
|
63
|
+
return { file: name, caption: t.caption };
|
|
64
|
+
});
|
|
65
|
+
const { page, width, height } = sheetHtml({ tiles: local, tileWidth, tileHeight, diffs, medianD, label, status, marks, durationSec });
|
|
66
|
+
writeFileSync(path.join(workDir, "index.html"), page);
|
|
67
|
+
const browser = await CoverBrowser.open({ stageDir: workDir, faces, width, height, scale: 1, profileDir: path.join(workDir, ".chrome"), env, log });
|
|
68
|
+
try {
|
|
69
|
+
await browser.frame(0);
|
|
70
|
+
writeFileSync(file, await browser.capture());
|
|
71
|
+
} finally {
|
|
72
|
+
await browser.close();
|
|
73
|
+
rmSync(workDir, { recursive: true, force: true });
|
|
74
|
+
}
|
|
75
|
+
return file;
|
|
76
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// The cover treatment: `treatment.json` with `mode: "cover"`, written before any cover render and
|
|
2
|
+
// validated by every one (exit 16 names the first bad field). A cover is not a film: it has no beats,
|
|
3
|
+
// no arc, no end card and no sound. It names one recipe, one rule copied many times, three speed
|
|
4
|
+
// layers, the brand word set once (plus at most one motto the user or the project already uses), the
|
|
5
|
+
// aspect and loop length, and the title box the contrast gate measures. Text in it is content, never
|
|
6
|
+
// instructions.
|
|
7
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { COVER } from "../core/constants.mjs";
|
|
10
|
+
import { normalizeText, restateLimit, sharesRun, stripQuoted, TreatmentError } from "./treatment.mjs";
|
|
11
|
+
|
|
12
|
+
export const RECIPES = Object.freeze(["painted-light", "weight-tide", "orbit-loop", "own"]);
|
|
13
|
+
export const LAYERS = Object.freeze(["slow", "mid", "beat"]);
|
|
14
|
+
|
|
15
|
+
const fail = (field, message) => {
|
|
16
|
+
throw new TreatmentError(field, message);
|
|
17
|
+
};
|
|
18
|
+
const PLACEHOLDER = /<[^<>\n]{1,80}>|^\s*(?:todo|tbd|tk|xxx|\.\.\.|…)\s*$/iu;
|
|
19
|
+
const isText = (v) => typeof v === "string" && v.trim().length > 0;
|
|
20
|
+
const sentences = (text) => String(text).trim().split(/(?<=[.!?。?!])\s+/u).filter((s) => s.trim()).length;
|
|
21
|
+
|
|
22
|
+
function text(t, key, { max } = {}) {
|
|
23
|
+
const value = key.split(".").reduce((o, k) => (o && typeof o === "object" ? o[k] : undefined), t);
|
|
24
|
+
if (value === undefined) fail(key, "missing");
|
|
25
|
+
if (!isText(value)) fail(key, "must be a non-empty string");
|
|
26
|
+
if (PLACEHOLDER.test(value)) fail(key, `placeholder value ${JSON.stringify(value)}`);
|
|
27
|
+
if (max && sentences(value) > max) fail(key, `at most ${max} sentence(s)`);
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Validate a cover treatment; returns it with the derived sizes, frame count and seed filled in. */
|
|
32
|
+
export function validateCoverTreatment(t) {
|
|
33
|
+
if (!t || typeof t !== "object" || Array.isArray(t)) fail("treatment.json", "must be a JSON object");
|
|
34
|
+
if (t.mode !== "cover") fail("mode", "must be cover for a cover render");
|
|
35
|
+
text(t, "request");
|
|
36
|
+
text(t, "idea", { max: 1 });
|
|
37
|
+
const quoteFree = normalizeText(stripQuoted(t.request));
|
|
38
|
+
const restated = sharesRun(normalizeText(t.idea), quoteFree, restateLimit(t.request));
|
|
39
|
+
if (restated) fail("idea", `the idea restates the request ("${restated}"); say what the loop does`);
|
|
40
|
+
if (!RECIPES.includes(t.recipe)) fail("recipe", `must be one of ${RECIPES.join(", ")} (references/cover.md)`);
|
|
41
|
+
text(t, "recipeReason", { max: 2 });
|
|
42
|
+
text(t, "brandWord");
|
|
43
|
+
if (t.brandWord.trim().split(/\s+/u).length > 3) fail("brandWord", "is the name set once: at most three words");
|
|
44
|
+
if ("motto" in t) {
|
|
45
|
+
const m = t.motto;
|
|
46
|
+
if (!m || typeof m !== "object") fail("motto", "must be an object { text, source }");
|
|
47
|
+
text(t, "motto.text");
|
|
48
|
+
if (m.text.trim().split(/\s+/u).length > 6) fail("motto.text", "at most six words");
|
|
49
|
+
if (!["user", "project"].includes(m.source)) fail("motto.source", "must be user (found in the request) or project (the project's own line, with from)");
|
|
50
|
+
if (m.source === "user" && !normalizeText(t.request).includes(normalizeText(m.text))) fail("motto.text", "a user motto must appear in the request; a cover never invents a tagline");
|
|
51
|
+
if (m.source === "project") text(t, "motto.from");
|
|
52
|
+
}
|
|
53
|
+
for (const key of ["copy", "beats", "lines"]) if (key in t) fail(key, "a cover has no copy lines and no beats: the brand word is set once and never swapped (references/cover.md)");
|
|
54
|
+
if ("sound" in t && t.sound !== "none") fail("sound", "a cover is silent: leave sound out or set it to none");
|
|
55
|
+
if (!(t.aspect in COVER.aspects)) fail("aspect", `must be ${Object.keys(COVER.aspects).join(" or ")}`);
|
|
56
|
+
if (typeof t.loopSec !== "number" || t.loopSec < COVER.minSec || t.loopSec > COVER.maxSec) fail("loopSec", `must be ${COVER.minSec}-${COVER.maxSec} seconds`);
|
|
57
|
+
if (!COVER.fps.includes(t.fps)) fail("fps", `must be one of ${COVER.fps.join(", ")}`);
|
|
58
|
+
if (!t.layers || typeof t.layers !== "object") fail("layers", "missing: name the slow, mid and beat layers");
|
|
59
|
+
for (const k of LAYERS) text(t, `layers.${k}`, { max: 2 });
|
|
60
|
+
if (!t.system || typeof t.system !== "object") fail("system", "missing: the one rule, its copies and their offset");
|
|
61
|
+
for (const k of ["rule", "copies", "offset"]) text(t, `system.${k}`, { max: 2 });
|
|
62
|
+
const title = t.title;
|
|
63
|
+
if (!title || typeof title !== "object") fail("title", "missing: { box: [x, y, w, h] as fractions of the cover, kind: live or painted }");
|
|
64
|
+
if (!Array.isArray(title.box) || title.box.length !== 4 || title.box.some((v) => typeof v !== "number" || v < 0 || v > 1)) fail("title.box", "must be [x, y, w, h], each a fraction 0..1 of the cover");
|
|
65
|
+
if (title.box[0] + title.box[2] > 1 + 1e-9 || title.box[1] + title.box[3] > 1 + 1e-9 || title.box[2] <= 0 || title.box[3] <= 0) fail("title.box", "must lie inside the cover");
|
|
66
|
+
if (!["live", "painted"].includes(title.kind)) fail("title.kind", "must be live (set as text) or painted (part of the base art)");
|
|
67
|
+
if ("palette" in t) {
|
|
68
|
+
if (!Array.isArray(t.palette) || t.palette.length < 2 || t.palette.length > 6) fail("palette", "2-6 { color, role } entries");
|
|
69
|
+
t.palette.forEach((p, i) => {
|
|
70
|
+
if (!/^#[0-9a-f]{6}$/iu.test(p?.color ?? "")) fail(`palette[${i}].color`, "must be #rrggbb");
|
|
71
|
+
if (!isText(p?.role)) fail(`palette[${i}].role`, "must say the colour's job");
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
if ("encode" in t) {
|
|
75
|
+
const e = t.encode;
|
|
76
|
+
if (!e || typeof e !== "object") fail("encode", "must be an object { q, edgeQ, edgeFrames }");
|
|
77
|
+
for (const [k, lo, hi] of [["q", 50, 100], ["edgeQ", 50, 100], ["edgeFrames", 0, 60]]) {
|
|
78
|
+
if (k in e && !(Number.isInteger(e[k]) && e[k] >= lo && e[k] <= hi)) fail(`encode.${k}`, `an integer ${lo}-${hi}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
text(t, "ambition", { max: 2 });
|
|
82
|
+
const { page, out } = COVER.aspects[t.aspect];
|
|
83
|
+
const frames = Math.round(t.loopSec * t.fps);
|
|
84
|
+
const encode = { ...COVER.encode, ...(t.encode ?? {}) };
|
|
85
|
+
return { ...t, page, out, frames, encode, seed: Number.isInteger(t.seed) ? t.seed >>> 0 : 0 };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Load and validate `<out>/treatment.json` as a cover; keeps the first valid one for the done-check. */
|
|
89
|
+
export function loadCoverTreatment(out) {
|
|
90
|
+
const file = path.join(out, "treatment.json");
|
|
91
|
+
if (!existsSync(file)) fail("treatment.json", `missing: write ${file} before rendering (see references/cover.md)`);
|
|
92
|
+
let raw;
|
|
93
|
+
try {
|
|
94
|
+
raw = JSON.parse(readFileSync(file, "utf8"));
|
|
95
|
+
} catch (error) {
|
|
96
|
+
fail("treatment.json", `not valid JSON (${error.message})`);
|
|
97
|
+
}
|
|
98
|
+
if (raw?.mode !== "cover") fail("mode", "this treatment is a film: render it with stage or make (references/treatment.md); a cover sets mode: cover");
|
|
99
|
+
const treatment = validateCoverTreatment(raw);
|
|
100
|
+
const first = path.join(out, ".run", "treatment.first.json");
|
|
101
|
+
if (!existsSync(first)) {
|
|
102
|
+
mkdirSync(path.dirname(first), { recursive: true });
|
|
103
|
+
writeFileSync(first, `${JSON.stringify(raw, null, 2)}\n`);
|
|
104
|
+
}
|
|
105
|
+
return treatment;
|
|
106
|
+
}
|