oda-engine 0.0.0-stage → 0.1.1
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 +151 -2
- package/bin/nowhere.mjs +352 -0
- package/lib/account.mjs +57 -0
- package/lib/agentModules.mjs +57 -0
- package/lib/devServer.mjs +351 -0
- package/lib/headless.mjs +231 -0
- package/lib/headlessPreview.mjs +212 -0
- package/lib/keys.mjs +517 -0
- package/lib/make.mjs +855 -0
- package/lib/mcp.mjs +537 -0
- package/lib/open.mjs +22 -0
- package/lib/preview.mjs +87 -0
- package/lib/previewRpc.mjs +134 -0
- package/lib/project.mjs +359 -0
- package/lib/publishFlow.mjs +131 -0
- package/lib/resources.mjs +50 -0
- package/lib/rules.mjs +60 -0
- package/lib/scaffold.mjs +42 -0
- package/lib/setup.mjs +59 -0
- package/lib/tools.mjs +668 -0
- package/package.json +51 -4
- package/template/game.config.json +9 -0
- package/template/scene.json +4 -0
- package/template/src/main.ts +11 -0
- package/vendor/DOCS.md +447 -0
- package/vendor/agent/appRun.mjs +520 -0
- package/vendor/agent/assets/brief.mjs +242 -0
- package/vendor/agent/assets/collider.mjs +531 -0
- package/vendor/agent/assets/environments/index.mjs +12 -0
- package/vendor/agent/assets/environments/worldlabs.mjs +161 -0
- package/vendor/agent/assets/fetch.mjs +168 -0
- package/vendor/agent/assets/footprint.mjs +13 -0
- package/vendor/agent/assets/gltf.mjs +547 -0
- package/vendor/agent/assets/hdr.mjs +269 -0
- package/vendor/agent/assets/imageOps.mjs +171 -0
- package/vendor/agent/assets/images/codex.mjs +307 -0
- package/vendor/agent/assets/images/fal.mjs +75 -0
- package/vendor/agent/assets/images/gemini.mjs +84 -0
- package/vendor/agent/assets/images/index.mjs +25 -0
- package/vendor/agent/assets/images/openai.mjs +75 -0
- package/vendor/agent/assets/images/stability.mjs +71 -0
- package/vendor/agent/assets/images/views.mjs +31 -0
- package/vendor/agent/assets/import.mjs +1352 -0
- package/vendor/agent/assets/inspect.mjs +465 -0
- package/vendor/agent/assets/jobs.mjs +196 -0
- package/vendor/agent/assets/ktx2.mjs +290 -0
- package/vendor/agent/assets/ledger.mjs +466 -0
- package/vendor/agent/assets/net.mjs +254 -0
- package/vendor/agent/assets/prices.json +39 -0
- package/vendor/agent/assets/providers/fal.mjs +115 -0
- package/vendor/agent/assets/providers/index.mjs +22 -0
- package/vendor/agent/assets/providers/meshy.mjs +119 -0
- package/vendor/agent/assets/providers/tripo.mjs +188 -0
- package/vendor/agent/assets/sources/ambientcg.mjs +129 -0
- package/vendor/agent/assets/sources/index.mjs +23 -0
- package/vendor/agent/assets/sources/polyhaven.mjs +237 -0
- package/vendor/agent/assets/sources/polypizza.mjs +121 -0
- package/vendor/agent/assets/splat.mjs +631 -0
- package/vendor/agent/assets/targets.mjs +122 -0
- package/vendor/agent/assets/tile.mjs +261 -0
- package/vendor/agent/assets/unzip.mjs +180 -0
- package/vendor/agent/blender/cleanup.py +360 -0
- package/vendor/agent/blender/convert.py +167 -0
- package/vendor/agent/blender/find.mjs +192 -0
- package/vendor/agent/blender/nw_export.py +104 -0
- package/vendor/agent/blender/nw_rubric.py +426 -0
- package/vendor/agent/blender/run.mjs +193 -0
- package/vendor/agent/blender/runner.py +252 -0
- package/vendor/agent/blender/sheet.py +743 -0
- package/vendor/agent/depth.mjs +134 -0
- package/vendor/agent/director/board.mjs +451 -0
- package/vendor/agent/director/contracts.mjs +663 -0
- package/vendor/agent/director/director.mjs +2365 -0
- package/vendor/agent/director/estimate.mjs +200 -0
- package/vendor/agent/director/gate.mjs +802 -0
- package/vendor/agent/director/limits.json +16 -0
- package/vendor/agent/director/methods.mjs +457 -0
- package/vendor/agent/director/policy.json +33 -0
- package/vendor/agent/director/prompts.mjs +244 -0
- package/vendor/agent/director/reading.mjs +341 -0
- package/vendor/agent/director/router.mjs +184 -0
- package/vendor/agent/director/seams.mjs +243 -0
- package/vendor/agent/director/session.mjs +643 -0
- package/vendor/agent/director/store.mjs +119 -0
- package/vendor/agent/director/taskGate.mjs +110 -0
- package/vendor/agent/director/teamLedger.mjs +118 -0
- package/vendor/agent/director/triage.mjs +63 -0
- package/vendor/agent/downloads.cjs +363 -0
- package/vendor/agent/flow.mjs +78 -0
- package/vendor/agent/guard.mjs +613 -0
- package/vendor/agent/host.mjs +1736 -0
- package/vendor/agent/language.mjs +58 -0
- package/vendor/agent/ledger.mjs +360 -0
- package/vendor/agent/locale.cjs +186 -0
- package/vendor/agent/loop/capacity.mjs +74 -0
- package/vendor/agent/loop/compose.mjs +127 -0
- package/vendor/agent/loop/gate.mjs +205 -0
- package/vendor/agent/loop/judge.mjs +189 -0
- package/vendor/agent/loop/levels.mjs +273 -0
- package/vendor/agent/loop/state.mjs +358 -0
- package/vendor/agent/notes.cjs +278 -0
- package/vendor/agent/pace.mjs +258 -0
- package/vendor/agent/panels.mjs +431 -0
- package/vendor/agent/prompt.mjs +342 -0
- package/vendor/agent/reference/board.mjs +410 -0
- package/vendor/agent/reference/compose.mjs +364 -0
- package/vendor/agent/reference/decode.mjs +56 -0
- package/vendor/agent/reference/facts.mjs +1874 -0
- package/vendor/agent/reference/png.mjs +107 -0
- package/vendor/agent/reference/records.mjs +63 -0
- package/vendor/agent/reference/web.mjs +252 -0
- package/vendor/agent/roles/artist.mjs +69 -0
- package/vendor/agent/roles/builder.mjs +79 -0
- package/vendor/agent/roles/checks.mjs +223 -0
- package/vendor/agent/roles/common.mjs +188 -0
- package/vendor/agent/roles/critic.mjs +171 -0
- package/vendor/agent/roles/index.mjs +39 -0
- package/vendor/agent/roles/modeller.mjs +142 -0
- package/vendor/agent/roles/qa.mjs +58 -0
- package/vendor/agent/roles/rubrics.mjs +79 -0
- package/vendor/agent/roles/sound.mjs +69 -0
- package/vendor/agent/rounds.cjs +333 -0
- package/vendor/agent/shared.mjs +284 -0
- package/vendor/agent/stopGate.mjs +574 -0
- package/vendor/agent/subagents.mjs +129 -0
- package/vendor/agent/tools/assets.mjs +499 -0
- package/vendor/agent/tools/blender.mjs +754 -0
- package/vendor/agent/tools/cards.mjs +479 -0
- package/vendor/agent/tools/choices.mjs +479 -0
- package/vendor/agent/tools/compose.mjs +74 -0
- package/vendor/agent/tools/environment.mjs +488 -0
- package/vendor/agent/tools/eyes.mjs +733 -0
- package/vendor/agent/tools/generate.mjs +338 -0
- package/vendor/agent/tools/guide.mjs +46 -0
- package/vendor/agent/tools/harness.mjs +193 -0
- package/vendor/agent/tools/image.mjs +643 -0
- package/vendor/agent/tools/index.mjs +162 -0
- package/vendor/agent/tools/look.mjs +578 -0
- package/vendor/agent/tools/loop.mjs +858 -0
- package/vendor/agent/tools/others.mjs +123 -0
- package/vendor/agent/tools/pictures.mjs +535 -0
- package/vendor/agent/tools/reference.mjs +366 -0
- package/vendor/agent/transcripts.mjs +118 -0
- package/vendor/agent-loop.mjs +602 -0
- package/vendor/agent-plugin/.claude-plugin/plugin.json +5 -0
- package/vendor/agent-plugin/skills/art-direction/SKILL.md +82 -0
- package/vendor/agent-plugin/skills/asset-legality/SKILL.md +101 -0
- package/vendor/agent-plugin/skills/asset-sources/SKILL.md +111 -0
- package/vendor/agent-plugin/skills/blender-modeling/SKILL.md +97 -0
- package/vendor/agent-plugin/skills/look-dev/SKILL.md +104 -0
- package/vendor/agent-plugin/skills/playtesting/SKILL.md +74 -0
- package/vendor/agent-plugin/skills/reference-loop/SKILL.md +102 -0
- package/vendor/agent-plugin/skills/sound-design/SKILL.md +60 -0
- package/vendor/agent-plugin/skills/style/SKILL.md +35 -0
- package/vendor/agent-plugin/skills/tsl-r185/SKILL.md +107 -0
- package/vendor/auth.cjs +570 -0
- package/vendor/build.cjs +953 -0
- package/vendor/catalog.json +455 -0
- package/vendor/connectors.cjs +688 -0
- package/vendor/eyes.mjs +733 -0
- package/vendor/gameExports.json +1390 -0
- package/vendor/git.cjs +222 -0
- package/vendor/models.json +57 -0
- package/vendor/publish.cjs +1018 -0
- package/vendor/refBoard.cjs +301 -0
- package/vendor/schema/dist/assets.d.ts +335 -0
- package/vendor/schema/dist/assets.js +439 -0
- package/vendor/schema/dist/brief.d.ts +357 -0
- package/vendor/schema/dist/brief.js +302 -0
- package/vendor/schema/dist/common.d.ts +9 -0
- package/vendor/schema/dist/common.js +13 -0
- package/vendor/schema/dist/config.d.ts +406 -0
- package/vendor/schema/dist/config.js +355 -0
- package/vendor/schema/dist/contract.d.ts +599 -0
- package/vendor/schema/dist/contract.js +1146 -0
- package/vendor/schema/dist/index.d.ts +14 -0
- package/vendor/schema/dist/index.js +21 -0
- package/vendor/schema/dist/look.d.ts +463 -0
- package/vendor/schema/dist/look.js +681 -0
- package/vendor/schema/dist/lookStats.d.ts +230 -0
- package/vendor/schema/dist/lookStats.js +768 -0
- package/vendor/schema/dist/pilot.d.ts +32 -0
- package/vendor/schema/dist/pilot.js +44 -0
- package/vendor/schema/dist/publish.d.ts +214 -0
- package/vendor/schema/dist/publish.js +291 -0
- package/vendor/schema/dist/scene.d.ts +75 -0
- package/vendor/schema/dist/scene.js +120 -0
- package/vendor/schema/dist/skyLight.d.ts +141 -0
- package/vendor/schema/dist/skyLight.js +436 -0
- package/vendor/schema/dist/style.d.ts +131 -0
- package/vendor/schema/dist/style.js +155 -0
- package/vendor/schema/package.json +1 -0
- package/vendor/selectionPrompt.mjs +51 -0
- package/vendor/useTools.mjs +195 -0
- package/vendor/userMessage.mjs +26 -0
- package/web-dist/assets/GaussianSplatUtils-CptQkDVX.js +1 -0
- package/web-dist/assets/KTX2Loader-C53LJqss.js +2 -0
- package/web-dist/assets/SPZLoader-BQ3tjaTv.js +1 -0
- package/web-dist/assets/gaussianSplat-_9tcWegB.js +1 -0
- package/web-dist/assets/index-Baka-ZO7.js +693 -0
- package/web-dist/assets/index-CcjyyQco.css +1 -0
- package/web-dist/assets/meshopt_decoder.module-BvwdjiD3.js +1 -0
- package/web-dist/assets/zstddec.module-CfFeQW7d.js +1 -0
- package/web-dist/index.html +34 -0
|
@@ -0,0 +1,768 @@
|
|
|
1
|
+
/** The analysed picture's long edge. */
|
|
2
|
+
export const STATS_LONG_EDGE = 256;
|
|
3
|
+
const K = 6;
|
|
4
|
+
const ITERATIONS = 10;
|
|
5
|
+
/** The sky band's share of the rows, from the top. A picture of a painting
|
|
6
|
+
* has no depth to find its sky by, so the top band stands in for it. */
|
|
7
|
+
export const SKY_BAND = 0.3;
|
|
8
|
+
/** The shade and the lit: this share of the pixels, darkest and brightest. */
|
|
9
|
+
export const SHADE_SHARE = 0.2;
|
|
10
|
+
/** The edge grid's cells on each side. */
|
|
11
|
+
export const EDGE_GRID = 8;
|
|
12
|
+
/**
|
|
13
|
+
* The statistics the verdict is made of. tone is the whole picture's
|
|
14
|
+
* palette, mean L* and contrast, the verdict there was; shade is the
|
|
15
|
+
* shade's colour, added because on the saved evidence
|
|
16
|
+
* (.fixtures-local/look-gate, rerun by scripts/look-gate-check.mts) its own
|
|
17
|
+
* verdict reproduced every expected one: the Red Arch against its request
|
|
18
|
+
* image far (where the tone says close), Laputa v1 against the film still
|
|
19
|
+
* far, laputa-v2 against it close or better, at 256 and 512 pixels and with
|
|
20
|
+
* the shade drawn anywhere from the darkest tenth to three tenths. The rest
|
|
21
|
+
* did not, and is only said: the sky band misses the arch at every cut, the
|
|
22
|
+
* subject at the higher sky line, the turn from lit to shade calls v2 far at
|
|
23
|
+
* a tenth, the edge layout parts the frames by 0.017 of correlation, too
|
|
24
|
+
* thin to hold, and the fine-to-coarse detail puts v2 further from the
|
|
25
|
+
* still than v1.
|
|
26
|
+
*/
|
|
27
|
+
export const JUDGED = ['tone', 'shade'];
|
|
28
|
+
// ---- colour ----
|
|
29
|
+
const SRGB_TO_LINEAR = new Float64Array(256);
|
|
30
|
+
for (let i = 0; i < 256; i++) {
|
|
31
|
+
const c = i / 255;
|
|
32
|
+
SRGB_TO_LINEAR[i] = c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
|
|
33
|
+
}
|
|
34
|
+
const linearToSrgb = (c) => (c <= 0.0031308 ? c * 12.92 : 1.055 * Math.pow(c, 1 / 2.4) - 0.055);
|
|
35
|
+
// D65 white, as sRGB's own matrix has it.
|
|
36
|
+
const XN = 0.95047;
|
|
37
|
+
const YN = 1;
|
|
38
|
+
const ZN = 1.08883;
|
|
39
|
+
const EPS = Math.pow(6 / 29, 3);
|
|
40
|
+
const f = (t) => (t > EPS ? Math.cbrt(t) : t / (3 * Math.pow(6 / 29, 2)) + 4 / 29);
|
|
41
|
+
const fInv = (t) => (t > 6 / 29 ? t * t * t : 3 * Math.pow(6 / 29, 2) * (t - 4 / 29));
|
|
42
|
+
/** Linear sRGB (0 to 1) to Lab. */
|
|
43
|
+
export function linearToLab(r, g, b) {
|
|
44
|
+
const x = (0.4124564 * r + 0.3575761 * g + 0.1804375 * b) / XN;
|
|
45
|
+
const y = (0.2126729 * r + 0.7151522 * g + 0.072175 * b) / YN;
|
|
46
|
+
const z = (0.0193339 * r + 0.119192 * g + 0.9503041 * b) / ZN;
|
|
47
|
+
const fy = f(y);
|
|
48
|
+
return [116 * fy - 16, 500 * (f(x) - fy), 200 * (fy - f(z))];
|
|
49
|
+
}
|
|
50
|
+
/** sRGB bytes to Lab. */
|
|
51
|
+
export function srgbToLab(r, g, b) {
|
|
52
|
+
return linearToLab(SRGB_TO_LINEAR[r & 255], SRGB_TO_LINEAR[g & 255], SRGB_TO_LINEAR[b & 255]);
|
|
53
|
+
}
|
|
54
|
+
/** Lab to '#rrggbb', clamped into sRGB. */
|
|
55
|
+
export function labToHex(lab) {
|
|
56
|
+
const fy = (lab[0] + 16) / 116;
|
|
57
|
+
const x = fInv(fy + lab[1] / 500) * XN;
|
|
58
|
+
const y = fInv(fy) * YN;
|
|
59
|
+
const z = fInv(fy - lab[2] / 200) * ZN;
|
|
60
|
+
const r = 3.2404542 * x - 1.5371385 * y - 0.4985314 * z;
|
|
61
|
+
const g = -0.969266 * x + 1.8760108 * y + 0.041556 * z;
|
|
62
|
+
const b = 0.0556434 * x - 0.2040259 * y + 1.0572252 * z;
|
|
63
|
+
const byte = (c) => Math.round(Math.min(1, Math.max(0, linearToSrgb(Math.max(0, c)))) * 255);
|
|
64
|
+
return `#${[r, g, b].map((c) => byte(c).toString(16).padStart(2, '0')).join('')}`;
|
|
65
|
+
}
|
|
66
|
+
/** '#rrggbb' to Lab. */
|
|
67
|
+
export function hexToLab(hex) {
|
|
68
|
+
const n = parseInt(hex.replace('#', ''), 16);
|
|
69
|
+
return srgbToLab((n >> 16) & 255, (n >> 8) & 255, n & 255);
|
|
70
|
+
}
|
|
71
|
+
/** CIEDE2000 (Sharma, Wu and Dalal 2005): the difference a person sees
|
|
72
|
+
* between two colours; about 1 is just noticeable, 10 is plainly another colour. */
|
|
73
|
+
export function deltaE2000(a, b) {
|
|
74
|
+
const [L1, a1, b1] = a;
|
|
75
|
+
const [L2, a2, b2] = b;
|
|
76
|
+
const rad = Math.PI / 180;
|
|
77
|
+
const C1 = Math.hypot(a1, b1);
|
|
78
|
+
const C2 = Math.hypot(a2, b2);
|
|
79
|
+
const Cbar = (C1 + C2) / 2;
|
|
80
|
+
const C7 = Math.pow(Cbar, 7);
|
|
81
|
+
const G = 0.5 * (1 - Math.sqrt(C7 / (C7 + Math.pow(25, 7))));
|
|
82
|
+
const a1p = (1 + G) * a1;
|
|
83
|
+
const a2p = (1 + G) * a2;
|
|
84
|
+
const C1p = Math.hypot(a1p, b1);
|
|
85
|
+
const C2p = Math.hypot(a2p, b2);
|
|
86
|
+
const hue = (bb, aa) => {
|
|
87
|
+
if (aa === 0 && bb === 0)
|
|
88
|
+
return 0;
|
|
89
|
+
const h = Math.atan2(bb, aa) / rad;
|
|
90
|
+
return h < 0 ? h + 360 : h;
|
|
91
|
+
};
|
|
92
|
+
const h1p = hue(b1, a1p);
|
|
93
|
+
const h2p = hue(b2, a2p);
|
|
94
|
+
const dLp = L2 - L1;
|
|
95
|
+
const dCp = C2p - C1p;
|
|
96
|
+
let dhp = 0;
|
|
97
|
+
if (C1p * C2p !== 0) {
|
|
98
|
+
dhp = h2p - h1p;
|
|
99
|
+
if (dhp > 180)
|
|
100
|
+
dhp -= 360;
|
|
101
|
+
else if (dhp < -180)
|
|
102
|
+
dhp += 360;
|
|
103
|
+
}
|
|
104
|
+
const dHp = 2 * Math.sqrt(C1p * C2p) * Math.sin((dhp / 2) * rad);
|
|
105
|
+
const Lbarp = (L1 + L2) / 2;
|
|
106
|
+
const Cbarp = (C1p + C2p) / 2;
|
|
107
|
+
let hbarp = h1p + h2p;
|
|
108
|
+
if (C1p * C2p !== 0) {
|
|
109
|
+
if (Math.abs(h1p - h2p) <= 180)
|
|
110
|
+
hbarp = (h1p + h2p) / 2;
|
|
111
|
+
else
|
|
112
|
+
hbarp = h1p + h2p < 360 ? (h1p + h2p + 360) / 2 : (h1p + h2p - 360) / 2;
|
|
113
|
+
}
|
|
114
|
+
const T = 1 -
|
|
115
|
+
0.17 * Math.cos((hbarp - 30) * rad) +
|
|
116
|
+
0.24 * Math.cos(2 * hbarp * rad) +
|
|
117
|
+
0.32 * Math.cos((3 * hbarp + 6) * rad) -
|
|
118
|
+
0.2 * Math.cos((4 * hbarp - 63) * rad);
|
|
119
|
+
const dTheta = 30 * Math.exp(-Math.pow((hbarp - 275) / 25, 2));
|
|
120
|
+
const Cp7 = Math.pow(Cbarp, 7);
|
|
121
|
+
const RC = 2 * Math.sqrt(Cp7 / (Cp7 + Math.pow(25, 7)));
|
|
122
|
+
const SL = 1 + (0.015 * Math.pow(Lbarp - 50, 2)) / Math.sqrt(20 + Math.pow(Lbarp - 50, 2));
|
|
123
|
+
const SC = 1 + 0.045 * Cbarp;
|
|
124
|
+
const SH = 1 + 0.015 * Cbarp * T;
|
|
125
|
+
const RT = -Math.sin(2 * dTheta * rad) * RC;
|
|
126
|
+
const l = dLp / SL;
|
|
127
|
+
const c = dCp / SC;
|
|
128
|
+
const h = dHp / SH;
|
|
129
|
+
return Math.sqrt(l * l + c * c + h * h + RT * c * h);
|
|
130
|
+
}
|
|
131
|
+
// ---- the picture ----
|
|
132
|
+
/** RGBA bytes box-averaged down to at most STATS_LONG_EDGE on the long edge. */
|
|
133
|
+
function downsample(rgba, width, height) {
|
|
134
|
+
const long = Math.max(width, height);
|
|
135
|
+
if (long <= STATS_LONG_EDGE) {
|
|
136
|
+
const data = new Uint8Array(width * height * 4);
|
|
137
|
+
for (let i = 0; i < data.length; i++)
|
|
138
|
+
data[i] = rgba[i];
|
|
139
|
+
return { data, width, height };
|
|
140
|
+
}
|
|
141
|
+
const k = STATS_LONG_EDGE / long;
|
|
142
|
+
const w = Math.max(1, Math.round(width * k));
|
|
143
|
+
const h = Math.max(1, Math.round(height * k));
|
|
144
|
+
const data = new Uint8Array(w * h * 4);
|
|
145
|
+
for (let y = 0; y < h; y++) {
|
|
146
|
+
const y0 = Math.floor((y * height) / h);
|
|
147
|
+
const y1 = Math.max(y0 + 1, Math.floor(((y + 1) * height) / h));
|
|
148
|
+
for (let x = 0; x < w; x++) {
|
|
149
|
+
const x0 = Math.floor((x * width) / w);
|
|
150
|
+
const x1 = Math.max(x0 + 1, Math.floor(((x + 1) * width) / w));
|
|
151
|
+
let r = 0;
|
|
152
|
+
let g = 0;
|
|
153
|
+
let b = 0;
|
|
154
|
+
let a = 0;
|
|
155
|
+
for (let sy = y0; sy < y1; sy++) {
|
|
156
|
+
for (let sx = x0; sx < x1; sx++) {
|
|
157
|
+
const o = (sy * width + sx) * 4;
|
|
158
|
+
r += rgba[o];
|
|
159
|
+
g += rgba[o + 1];
|
|
160
|
+
b += rgba[o + 2];
|
|
161
|
+
a += rgba[o + 3];
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const n = (y1 - y0) * (x1 - x0);
|
|
165
|
+
const o = (y * w + x) * 4;
|
|
166
|
+
data[o] = Math.round(r / n);
|
|
167
|
+
data[o + 1] = Math.round(g / n);
|
|
168
|
+
data[o + 2] = Math.round(b / n);
|
|
169
|
+
data[o + 3] = Math.round(a / n);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return { data, width: w, height: h };
|
|
173
|
+
}
|
|
174
|
+
/** L* of each of `n` RGBA pixels. */
|
|
175
|
+
function lightness(data, n) {
|
|
176
|
+
const L = new Float64Array(n);
|
|
177
|
+
for (let i = 0; i < n; i++)
|
|
178
|
+
L[i] = srgbToLab(data[i * 4], data[i * 4 + 1], data[i * 4 + 2])[0];
|
|
179
|
+
return L;
|
|
180
|
+
}
|
|
181
|
+
/** A picture without its border's rows and columns. */
|
|
182
|
+
function crop(pic, cut) {
|
|
183
|
+
if (cut.top + cut.bottom + cut.left + cut.right === 0)
|
|
184
|
+
return pic;
|
|
185
|
+
const w = pic.width - cut.left - cut.right;
|
|
186
|
+
const h = pic.height - cut.top - cut.bottom;
|
|
187
|
+
const data = new Uint8Array(w * h * 4);
|
|
188
|
+
for (let y = 0; y < h; y++) {
|
|
189
|
+
const from = ((y + cut.top) * pic.width + cut.left) * 4;
|
|
190
|
+
data.set(pic.data.subarray(from, from + w * 4), y * w * 4);
|
|
191
|
+
}
|
|
192
|
+
return { data, width: w, height: h };
|
|
193
|
+
}
|
|
194
|
+
const dist2 = (lab, i, c) => {
|
|
195
|
+
const dl = lab[i * 3] - c[0];
|
|
196
|
+
const da = lab[i * 3 + 1] - c[1];
|
|
197
|
+
const db = lab[i * 3 + 2] - c[2];
|
|
198
|
+
return dl * dl + da * da + db * db;
|
|
199
|
+
};
|
|
200
|
+
/** The picture's main colours: k-means in Lab from a fixed start (the pixels
|
|
201
|
+
* at L* quantiles 1/12, 3/12 ... 11/12), ten rounds, so the same picture always
|
|
202
|
+
* gives the same palette. Colours that end up the same are one entry. */
|
|
203
|
+
function paletteOf(lab, n, order) {
|
|
204
|
+
if (n === 0)
|
|
205
|
+
return [];
|
|
206
|
+
let centres = [];
|
|
207
|
+
for (let k = 0; k < K; k++) {
|
|
208
|
+
const i = order[Math.min(n - 1, Math.floor(((2 * k + 1) / (2 * K)) * n))];
|
|
209
|
+
const c = [lab[i * 3], lab[i * 3 + 1], lab[i * 3 + 2]];
|
|
210
|
+
if (!centres.some((d) => d[0] === c[0] && d[1] === c[1] && d[2] === c[2]))
|
|
211
|
+
centres.push(c);
|
|
212
|
+
}
|
|
213
|
+
const assign = new Uint8Array(n);
|
|
214
|
+
let counts = [];
|
|
215
|
+
for (let round = 0; round <= ITERATIONS; round++) {
|
|
216
|
+
for (let i = 0; i < n; i++) {
|
|
217
|
+
let best = 0;
|
|
218
|
+
let bestD = Infinity;
|
|
219
|
+
for (let k = 0; k < centres.length; k++) {
|
|
220
|
+
const d = dist2(lab, i, centres[k]);
|
|
221
|
+
if (d < bestD) {
|
|
222
|
+
bestD = d;
|
|
223
|
+
best = k;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
assign[i] = best;
|
|
227
|
+
}
|
|
228
|
+
const sums = centres.map(() => [0, 0, 0]);
|
|
229
|
+
counts = centres.map(() => 0);
|
|
230
|
+
for (let i = 0; i < n; i++) {
|
|
231
|
+
const k = assign[i];
|
|
232
|
+
sums[k][0] += lab[i * 3];
|
|
233
|
+
sums[k][1] += lab[i * 3 + 1];
|
|
234
|
+
sums[k][2] += lab[i * 3 + 2];
|
|
235
|
+
counts[k]++;
|
|
236
|
+
}
|
|
237
|
+
if (round === ITERATIONS)
|
|
238
|
+
break;
|
|
239
|
+
// An empty cluster is dropped rather than reseeded: reseeding at random
|
|
240
|
+
// would make the palette differ from one run to the next.
|
|
241
|
+
const next = [];
|
|
242
|
+
centres.forEach((_, k) => {
|
|
243
|
+
if (counts[k] > 0)
|
|
244
|
+
next.push([sums[k][0] / counts[k], sums[k][1] / counts[k], sums[k][2] / counts[k]]);
|
|
245
|
+
});
|
|
246
|
+
centres = next;
|
|
247
|
+
}
|
|
248
|
+
// Clusters that met in the middle are one colour.
|
|
249
|
+
const merged = [];
|
|
250
|
+
centres.forEach((c, k) => {
|
|
251
|
+
if (counts[k] === 0)
|
|
252
|
+
return;
|
|
253
|
+
const near = merged.find((m) => Math.hypot(m.lab[0] - c[0], m.lab[1] - c[1], m.lab[2] - c[2]) < 1);
|
|
254
|
+
if (near) {
|
|
255
|
+
const total = near.count + counts[k];
|
|
256
|
+
near.lab = [
|
|
257
|
+
(near.lab[0] * near.count + c[0] * counts[k]) / total,
|
|
258
|
+
(near.lab[1] * near.count + c[1] * counts[k]) / total,
|
|
259
|
+
(near.lab[2] * near.count + c[2] * counts[k]) / total,
|
|
260
|
+
];
|
|
261
|
+
near.count = total;
|
|
262
|
+
}
|
|
263
|
+
else
|
|
264
|
+
merged.push({ lab: [...c], count: counts[k] });
|
|
265
|
+
});
|
|
266
|
+
return merged
|
|
267
|
+
.sort((a, b) => b.count - a.count || b.lab[0] - a.lab[0])
|
|
268
|
+
.map((m) => ({ hex: labToHex(m.lab), share: m.count / n, lab: m.lab }));
|
|
269
|
+
}
|
|
270
|
+
/** Pixels `from` to `to` (whole rows) of a picture's Lab, measured on their own. */
|
|
271
|
+
function regionStats(lab, from, to) {
|
|
272
|
+
const n = Math.max(0, to - from);
|
|
273
|
+
const part = lab.subarray(from * 3, to * 3);
|
|
274
|
+
if (n === 0)
|
|
275
|
+
return { meanL: 0, stdL: 0, meanChroma: 0, palette: [] };
|
|
276
|
+
let sumL = 0;
|
|
277
|
+
let chroma = 0;
|
|
278
|
+
for (let i = 0; i < n; i++) {
|
|
279
|
+
sumL += part[i * 3];
|
|
280
|
+
chroma += Math.hypot(part[i * 3 + 1], part[i * 3 + 2]);
|
|
281
|
+
}
|
|
282
|
+
const meanL = sumL / n;
|
|
283
|
+
let spread = 0;
|
|
284
|
+
for (let i = 0; i < n; i++)
|
|
285
|
+
spread += (part[i * 3] - meanL) * (part[i * 3] - meanL);
|
|
286
|
+
const order = new Uint32Array(n);
|
|
287
|
+
for (let i = 0; i < n; i++)
|
|
288
|
+
order[i] = i;
|
|
289
|
+
order.sort((a, b) => part[a * 3] - part[b * 3] || a - b);
|
|
290
|
+
return { meanL, stdL: Math.sqrt(spread / n), meanChroma: chroma / n, palette: paletteOf(part, n, order) };
|
|
291
|
+
}
|
|
292
|
+
/** The mean Lab of the pixels order[from] to order[to - 1]. */
|
|
293
|
+
function meanLab(lab, order, from, to) {
|
|
294
|
+
const n = Math.max(1, to - from);
|
|
295
|
+
const out = [0, 0, 0];
|
|
296
|
+
for (let k = from; k < to; k++) {
|
|
297
|
+
const i = order[k];
|
|
298
|
+
out[0] += lab[i * 3];
|
|
299
|
+
out[1] += lab[i * 3 + 1];
|
|
300
|
+
out[2] += lab[i * 3 + 2];
|
|
301
|
+
}
|
|
302
|
+
return [out[0] / n, out[1] / n, out[2] / n];
|
|
303
|
+
}
|
|
304
|
+
/** Mean Sobel magnitude of L*, cell by cell on a grid x grid of the picture. */
|
|
305
|
+
function edgeGrid(L, w, h, grid) {
|
|
306
|
+
const sum = new Float64Array(grid * grid);
|
|
307
|
+
const count = new Float64Array(grid * grid);
|
|
308
|
+
for (let y = 1; y < h - 1; y++) {
|
|
309
|
+
const cy = Math.min(grid - 1, Math.floor((y * grid) / h));
|
|
310
|
+
for (let x = 1; x < w - 1; x++) {
|
|
311
|
+
const at = (dx, dy) => L[(y + dy) * w + x + dx];
|
|
312
|
+
const gx = at(1, -1) + 2 * at(1, 0) + at(1, 1) - at(-1, -1) - 2 * at(-1, 0) - at(-1, 1);
|
|
313
|
+
const gy = at(-1, 1) + 2 * at(0, 1) + at(1, 1) - at(-1, -1) - 2 * at(0, -1) - at(1, -1);
|
|
314
|
+
const c = cy * grid + Math.min(grid - 1, Math.floor((x * grid) / w));
|
|
315
|
+
sum[c] += Math.hypot(gx, gy);
|
|
316
|
+
count[c]++;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return Array.from(sum, (v, c) => Math.round((count[c] ? v / count[c] : 0) * 100) / 100);
|
|
320
|
+
}
|
|
321
|
+
/** A box blur of L* by `r` pixels each way, over what of the box is inside
|
|
322
|
+
* the picture, from a summed-area table. */
|
|
323
|
+
function boxBlur(table, w, h, r) {
|
|
324
|
+
const out = new Float64Array(w * h);
|
|
325
|
+
const W = w + 1;
|
|
326
|
+
for (let y = 0; y < h; y++) {
|
|
327
|
+
const y0 = Math.max(0, y - r);
|
|
328
|
+
const y1 = Math.min(h, y + r + 1);
|
|
329
|
+
for (let x = 0; x < w; x++) {
|
|
330
|
+
const x0 = Math.max(0, x - r);
|
|
331
|
+
const x1 = Math.min(w, x + r + 1);
|
|
332
|
+
const s = table[y1 * W + x1] - table[y0 * W + x1] - table[y1 * W + x0] + table[y0 * W + x0];
|
|
333
|
+
out[y * w + x] = s / ((y1 - y0) * (x1 - x0));
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return out;
|
|
337
|
+
}
|
|
338
|
+
/** How the picture's detail splits between fine and coarse. */
|
|
339
|
+
function detailOf(L, w, h) {
|
|
340
|
+
const W = w + 1;
|
|
341
|
+
const table = new Float64Array(W * (h + 1));
|
|
342
|
+
for (let y = 0; y < h; y++) {
|
|
343
|
+
let row = 0;
|
|
344
|
+
for (let x = 0; x < w; x++) {
|
|
345
|
+
row += L[y * w + x];
|
|
346
|
+
table[(y + 1) * W + x + 1] = table[y * W + x + 1] + row;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
const b1 = boxBlur(table, w, h, 1);
|
|
350
|
+
const b3 = boxBlur(table, w, h, 3);
|
|
351
|
+
const b9 = boxBlur(table, w, h, 9);
|
|
352
|
+
let fine = 0;
|
|
353
|
+
let coarse = 0;
|
|
354
|
+
for (let i = 0; i < L.length; i++) {
|
|
355
|
+
fine += Math.abs(L[i] - b1[i]);
|
|
356
|
+
coarse += Math.abs(b3[i] - b9[i]);
|
|
357
|
+
}
|
|
358
|
+
return { fine: fine / Math.max(1, L.length), coarse: coarse / Math.max(1, L.length) };
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* A uniform border around a picture's art (a letterbox, a pillarbox, a mat),
|
|
362
|
+
* in rows and columns of `L` (L*, w x h, top row first). The rule
|
|
363
|
+
* nw_reference reads a reference's frame by (reference/facts.mjs frameOf):
|
|
364
|
+
* a border is lines of one value (spread under 2 L*, each within 3 of the
|
|
365
|
+
* first), at least 2 and at most 15% of the side (a flat sky or wall beyond
|
|
366
|
+
* that is content), and it ends in a step: the three lines after it differ
|
|
367
|
+
* from it by 8 L* or more, where a smooth sky would only drift. A crop that
|
|
368
|
+
* would leave less than 40% of either side is no crop.
|
|
369
|
+
*/
|
|
370
|
+
export function uniformBorder(L, w, h) {
|
|
371
|
+
const line = (count, at) => {
|
|
372
|
+
let s = 0;
|
|
373
|
+
let q = 0;
|
|
374
|
+
for (let i = 0; i < count; i++) {
|
|
375
|
+
const v = at(i);
|
|
376
|
+
s += v;
|
|
377
|
+
q += v * v;
|
|
378
|
+
}
|
|
379
|
+
const m = s / count;
|
|
380
|
+
return { m, std: Math.sqrt(Math.max(0, q / count - m * m)) };
|
|
381
|
+
};
|
|
382
|
+
const row = (y) => line(w, (x) => L[y * w + x]);
|
|
383
|
+
const col = (x) => line(h, (y) => L[y * w + x]);
|
|
384
|
+
const run = (count, nth) => {
|
|
385
|
+
if (count < 5)
|
|
386
|
+
return 0;
|
|
387
|
+
const first = nth(0);
|
|
388
|
+
if (first.std >= 2)
|
|
389
|
+
return 0;
|
|
390
|
+
let k = 0;
|
|
391
|
+
while (k < count) {
|
|
392
|
+
const l = nth(k);
|
|
393
|
+
if (l.std >= 2 || Math.abs(l.m - first.m) >= 3)
|
|
394
|
+
break;
|
|
395
|
+
k++;
|
|
396
|
+
}
|
|
397
|
+
if (k < 2 || k > 0.15 * count || k + 3 > count)
|
|
398
|
+
return 0;
|
|
399
|
+
const after = (nth(k).m + nth(k + 1).m + nth(k + 2).m) / 3;
|
|
400
|
+
return Math.abs(after - first.m) >= 8 ? k : 0;
|
|
401
|
+
};
|
|
402
|
+
const top = run(h, (k) => row(k));
|
|
403
|
+
const bottom = run(h, (k) => row(h - 1 - k));
|
|
404
|
+
const left = run(w, (k) => col(k));
|
|
405
|
+
const right = run(w, (k) => col(w - 1 - k));
|
|
406
|
+
const framed = top + bottom + left + right > 0 && h - top - bottom >= 0.4 * h && w - left - right >= 0.4 * w;
|
|
407
|
+
return framed ? { top, bottom, left, right } : { top: 0, bottom: 0, left: 0, right: 0 };
|
|
408
|
+
}
|
|
409
|
+
/** What a picture's look is, in numbers. `rgba` is width x height RGBA bytes,
|
|
410
|
+
* top row first; alpha is ignored (a shot is an opaque JPEG). A uniform
|
|
411
|
+
* border is cut off first (uniformBorder; `cuts.border` false keeps it).
|
|
412
|
+
* `cuts` also moves the sky band's and the shade's lines from SKY_BAND and
|
|
413
|
+
* SHADE_SHARE, for a check that asks how much a verdict depends on where
|
|
414
|
+
* they are drawn. */
|
|
415
|
+
export function lookStats(rgba, width, height, cuts = {}) {
|
|
416
|
+
const whole = downsample(rgba, Math.max(1, Math.floor(width)), Math.max(1, Math.floor(height)));
|
|
417
|
+
const cut = cuts.border === false ? { top: 0, bottom: 0, left: 0, right: 0 } : uniformBorder(lightness(whole.data, whole.width * whole.height), whole.width, whole.height);
|
|
418
|
+
const pic = crop(whole, cut);
|
|
419
|
+
const { data, width: w, height: h } = pic;
|
|
420
|
+
const n = w * h;
|
|
421
|
+
const lab = new Float64Array(n * 3);
|
|
422
|
+
let sumL = 0;
|
|
423
|
+
let chroma = 0;
|
|
424
|
+
let warmth = 0;
|
|
425
|
+
let tint = 0;
|
|
426
|
+
let high = 0;
|
|
427
|
+
let low = 0;
|
|
428
|
+
const third = [0, 0, 0];
|
|
429
|
+
const thirdN = [0, 0, 0];
|
|
430
|
+
let border = 0;
|
|
431
|
+
let borderN = 0;
|
|
432
|
+
let centre = 0;
|
|
433
|
+
let centreN = 0;
|
|
434
|
+
const histogram = new Array(16).fill(0);
|
|
435
|
+
for (let y = 0; y < h; y++) {
|
|
436
|
+
const t = Math.min(2, Math.floor((y * 3) / h));
|
|
437
|
+
const edgeY = y < h * 0.15 || y >= h * 0.85;
|
|
438
|
+
for (let x = 0; x < w; x++) {
|
|
439
|
+
const i = y * w + x;
|
|
440
|
+
const r = data[i * 4];
|
|
441
|
+
const g = data[i * 4 + 1];
|
|
442
|
+
const b = data[i * 4 + 2];
|
|
443
|
+
const c = srgbToLab(r, g, b);
|
|
444
|
+
lab[i * 3] = c[0];
|
|
445
|
+
lab[i * 3 + 1] = c[1];
|
|
446
|
+
lab[i * 3 + 2] = c[2];
|
|
447
|
+
sumL += c[0];
|
|
448
|
+
chroma += Math.hypot(c[1], c[2]);
|
|
449
|
+
tint += c[1];
|
|
450
|
+
warmth += c[2];
|
|
451
|
+
if (r >= 250 && g >= 250 && b >= 250)
|
|
452
|
+
high++;
|
|
453
|
+
if (r <= 5 && g <= 5 && b <= 5)
|
|
454
|
+
low++;
|
|
455
|
+
third[t] += c[0];
|
|
456
|
+
thirdN[t]++;
|
|
457
|
+
if (edgeY || x < w * 0.15 || x >= w * 0.85) {
|
|
458
|
+
border += c[0];
|
|
459
|
+
borderN++;
|
|
460
|
+
}
|
|
461
|
+
else {
|
|
462
|
+
centre += c[0];
|
|
463
|
+
centreN++;
|
|
464
|
+
}
|
|
465
|
+
histogram[Math.min(15, Math.max(0, Math.floor((c[0] / 100) * 16)))]++;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
const meanL = sumL / n;
|
|
469
|
+
// Two passes, so a flat picture's spread is exactly 0 rather than what the
|
|
470
|
+
// rounding of a sum of squares leaves.
|
|
471
|
+
let spread = 0;
|
|
472
|
+
for (let i = 0; i < n; i++)
|
|
473
|
+
spread += (lab[i * 3] - meanL) * (lab[i * 3] - meanL);
|
|
474
|
+
const stdL = Math.sqrt(spread / n);
|
|
475
|
+
// Pixels by L*, darkest first: the percentiles and the palette's start.
|
|
476
|
+
const order = new Uint32Array(n);
|
|
477
|
+
for (let i = 0; i < n; i++)
|
|
478
|
+
order[i] = i;
|
|
479
|
+
order.sort((a, b) => lab[a * 3] - lab[b * 3] || a - b);
|
|
480
|
+
const pct = (q) => lab[order[Math.min(n - 1, Math.max(0, Math.round(q * (n - 1))))] * 3];
|
|
481
|
+
const borderMean = borderN ? border / borderN : meanL;
|
|
482
|
+
const centreMean = centreN ? centre / centreN : meanL;
|
|
483
|
+
// By region: the sky band's rows and the rest, the darkest and brightest
|
|
484
|
+
// fifth, the edges by place and the detail by scale.
|
|
485
|
+
const skyRows = Math.max(1, Math.min(h - 1, Math.round(h * (cuts.sky ?? SKY_BAND))));
|
|
486
|
+
const fifth = Math.max(1, Math.floor(n * (cuts.shade ?? SHADE_SHARE)));
|
|
487
|
+
const L = new Float64Array(n);
|
|
488
|
+
for (let i = 0; i < n; i++)
|
|
489
|
+
L[i] = lab[i * 3];
|
|
490
|
+
return {
|
|
491
|
+
width: w,
|
|
492
|
+
height: h,
|
|
493
|
+
meanL,
|
|
494
|
+
stdL,
|
|
495
|
+
p5: pct(0.05),
|
|
496
|
+
p50: pct(0.5),
|
|
497
|
+
p95: pct(0.95),
|
|
498
|
+
clipHigh: high / n,
|
|
499
|
+
clipLow: low / n,
|
|
500
|
+
meanChroma: chroma / n,
|
|
501
|
+
warmth: warmth / n,
|
|
502
|
+
tint: tint / n,
|
|
503
|
+
thirds: [0, 1, 2].map((k) => (thirdN[k] ? third[k] / thirdN[k] : meanL)),
|
|
504
|
+
edge: centreMean > 0.5 ? borderMean / centreMean : 1,
|
|
505
|
+
palette: paletteOf(lab, n, order),
|
|
506
|
+
histogram: histogram.map((c) => c / n),
|
|
507
|
+
regions: h > 1 ? { sky: regionStats(lab, 0, skyRows * w), subject: regionStats(lab, skyRows * w, n) } : undefined,
|
|
508
|
+
shade: meanLab(lab, order, 0, fifth),
|
|
509
|
+
lit: meanLab(lab, order, n - fifth, n),
|
|
510
|
+
edges: edgeGrid(L, w, h, EDGE_GRID),
|
|
511
|
+
detail: detailOf(L, w, h),
|
|
512
|
+
border: {
|
|
513
|
+
top: cut.top / whole.height,
|
|
514
|
+
bottom: cut.bottom / whole.height,
|
|
515
|
+
left: cut.left / whole.width,
|
|
516
|
+
right: cut.right / whole.width,
|
|
517
|
+
},
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
/** Share-weighted mean, over the reference's colours, of the nearest shot colour's deltaE2000. */
|
|
521
|
+
export function paletteDistance(shot, ref) {
|
|
522
|
+
if (ref.length === 0)
|
|
523
|
+
return 0;
|
|
524
|
+
if (shot.length === 0)
|
|
525
|
+
return 100;
|
|
526
|
+
let sum = 0;
|
|
527
|
+
let weight = 0;
|
|
528
|
+
for (const r of ref) {
|
|
529
|
+
let best = Infinity;
|
|
530
|
+
for (const s of shot)
|
|
531
|
+
best = Math.min(best, deltaE2000(r.lab, s.lab));
|
|
532
|
+
sum += best * r.share;
|
|
533
|
+
weight += r.share;
|
|
534
|
+
}
|
|
535
|
+
return weight > 0 ? sum / weight : 0;
|
|
536
|
+
}
|
|
537
|
+
/** A ratio that stays finite for flat pictures: half a unit is added to both. */
|
|
538
|
+
const ratio = (a, b) => (a + 0.5) / (b + 0.5);
|
|
539
|
+
/** The tone's verdict: matches with the palette within 6 deltaE, mean L*
|
|
540
|
+
* within 5 and contrast within 15%; close with the palette within 10 and
|
|
541
|
+
* mean L* within 10; otherwise far. */
|
|
542
|
+
function toneVerdict(paletteDeltaE, deltaMeanL, contrastRatio = 1) {
|
|
543
|
+
return paletteDeltaE <= 6 && Math.abs(deltaMeanL) <= 5 && contrastRatio >= 0.85 && contrastRatio <= 1.15
|
|
544
|
+
? 'matches'
|
|
545
|
+
: paletteDeltaE <= 10 && Math.abs(deltaMeanL) <= 10
|
|
546
|
+
? 'close'
|
|
547
|
+
: 'far';
|
|
548
|
+
}
|
|
549
|
+
/** One colour against another on the palette's scale: within 6 deltaE
|
|
550
|
+
* matches, within 10 close, otherwise far. */
|
|
551
|
+
const colourVerdict = (deltaE) => (deltaE <= 6 ? 'matches' : deltaE <= 10 ? 'close' : 'far');
|
|
552
|
+
const RANK = { matches: 0, close: 1, far: 2 };
|
|
553
|
+
/** The worse of verdicts. */
|
|
554
|
+
export const worstVerdict = (list) => list.reduce((w, v) => (RANK[v] > RANK[w] ? v : w), 'matches');
|
|
555
|
+
function regionComparison(shot, ref) {
|
|
556
|
+
const paletteDeltaE = paletteDistance(shot.palette, ref.palette);
|
|
557
|
+
const deltaMeanL = shot.meanL - ref.meanL;
|
|
558
|
+
return { paletteDeltaE, deltaMeanL, verdict: toneVerdict(paletteDeltaE, deltaMeanL, ratio(shot.stdL, ref.stdL)) };
|
|
559
|
+
}
|
|
560
|
+
/** Pearson correlation of two lists of the same length; 0 when either is flat. */
|
|
561
|
+
function correlation(a, b) {
|
|
562
|
+
const n = Math.min(a.length, b.length);
|
|
563
|
+
if (n === 0)
|
|
564
|
+
return 0;
|
|
565
|
+
let ma = 0;
|
|
566
|
+
let mb = 0;
|
|
567
|
+
for (let i = 0; i < n; i++) {
|
|
568
|
+
ma += a[i];
|
|
569
|
+
mb += b[i];
|
|
570
|
+
}
|
|
571
|
+
ma /= n;
|
|
572
|
+
mb /= n;
|
|
573
|
+
let ab = 0;
|
|
574
|
+
let aa = 0;
|
|
575
|
+
let bb = 0;
|
|
576
|
+
for (let i = 0; i < n; i++) {
|
|
577
|
+
ab += (a[i] - ma) * (b[i] - mb);
|
|
578
|
+
aa += (a[i] - ma) * (a[i] - ma);
|
|
579
|
+
bb += (b[i] - mb) * (b[i] - mb);
|
|
580
|
+
}
|
|
581
|
+
return aa > 0 && bb > 0 ? ab / Math.sqrt(aa * bb) : 0;
|
|
582
|
+
}
|
|
583
|
+
/** How a shot's look differs from a reference's. The verdict is the worst
|
|
584
|
+
* of the judged statistics (JUDGED): the tone (toneVerdict) and, when both
|
|
585
|
+
* pictures were measured by region, the shade's colour (colourVerdict).
|
|
586
|
+
* The rest is said and not judged. */
|
|
587
|
+
export function compareLook(shot, ref) {
|
|
588
|
+
const paletteDeltaE = paletteDistance(shot.palette, ref.palette);
|
|
589
|
+
const deltaMeanL = shot.meanL - ref.meanL;
|
|
590
|
+
const contrastRatio = ratio(shot.stdL, ref.stdL);
|
|
591
|
+
const chromaRatio = ratio(shot.meanChroma, ref.meanChroma);
|
|
592
|
+
const verdicts = { tone: toneVerdict(paletteDeltaE, deltaMeanL, contrastRatio) };
|
|
593
|
+
const out = {
|
|
594
|
+
paletteDeltaE,
|
|
595
|
+
deltaMeanL,
|
|
596
|
+
contrastRatio,
|
|
597
|
+
chromaRatio,
|
|
598
|
+
warmthDelta: shot.warmth - ref.warmth,
|
|
599
|
+
tintDelta: shot.tint - ref.tint,
|
|
600
|
+
clip: { high: [shot.clipHigh, ref.clipHigh], low: [shot.clipLow, ref.clipLow] },
|
|
601
|
+
thirds: { shot: [...shot.thirds], ref: [...ref.thirds] },
|
|
602
|
+
verdict: verdicts.tone,
|
|
603
|
+
verdicts,
|
|
604
|
+
};
|
|
605
|
+
if (shot.regions && ref.regions) {
|
|
606
|
+
out.regions = { sky: regionComparison(shot.regions.sky, ref.regions.sky), subject: regionComparison(shot.regions.subject, ref.regions.subject) };
|
|
607
|
+
}
|
|
608
|
+
if (shot.shade && ref.shade && shot.lit && ref.lit) {
|
|
609
|
+
const deltaE = deltaE2000(shot.shade, ref.shade);
|
|
610
|
+
out.shade = { deltaE, shot: [...shot.shade], ref: [...ref.shade] };
|
|
611
|
+
out.lit = { deltaE: deltaE2000(shot.lit, ref.lit), shot: [...shot.lit], ref: [...ref.lit] };
|
|
612
|
+
out.shadeTurn = Math.hypot(shot.shade[1] - shot.lit[1] - (ref.shade[1] - ref.lit[1]), shot.shade[2] - shot.lit[2] - (ref.shade[2] - ref.lit[2]));
|
|
613
|
+
verdicts.shade = colourVerdict(deltaE);
|
|
614
|
+
}
|
|
615
|
+
if (shot.edges && ref.edges)
|
|
616
|
+
out.layout = correlation(shot.edges, ref.edges);
|
|
617
|
+
if (shot.detail && ref.detail) {
|
|
618
|
+
const own = shot.detail.fine / Math.max(1e-6, shot.detail.coarse);
|
|
619
|
+
const theirs = ref.detail.fine / Math.max(1e-6, ref.detail.coarse);
|
|
620
|
+
out.detailRatio = own / Math.max(1e-6, theirs);
|
|
621
|
+
}
|
|
622
|
+
out.verdict = worstVerdict(Object.values(verdicts));
|
|
623
|
+
return out;
|
|
624
|
+
}
|
|
625
|
+
const round2 = (v) => Math.round(v * 100) / 100;
|
|
626
|
+
const clampTo = (v, lo, hi) => Math.min(hi, Math.max(lo, v));
|
|
627
|
+
/** Relative luminance from a mean L* (the inverse of L*'s curve). */
|
|
628
|
+
const yOfL = (L) => Math.pow((Math.max(0, L) + 16) / 116, 3);
|
|
629
|
+
/**
|
|
630
|
+
* Which knobs would bring the shot toward the reference, as look_apply set
|
|
631
|
+
* values with the reason, largest first in the order exposure, contrast,
|
|
632
|
+
* saturation, white balance. Each step is a measured guess: the tone curve
|
|
633
|
+
* and the grade interact, so look again after applying it.
|
|
634
|
+
*/
|
|
635
|
+
export function suggestLook(cmp, current) {
|
|
636
|
+
const out = [];
|
|
637
|
+
const push = (path, from, to, why) => {
|
|
638
|
+
const t = round2(to);
|
|
639
|
+
if (Math.abs(t - from) >= 0.01)
|
|
640
|
+
out.push({ path, from: round2(from), to: t, why });
|
|
641
|
+
};
|
|
642
|
+
const mean = (v) => v.reduce((s, x) => s + x, 0) / v.length;
|
|
643
|
+
if (Math.abs(cmp.deltaMeanL) > 2) {
|
|
644
|
+
const ev = Math.log2(yOfL(mean(cmp.thirds.ref)) / Math.max(1e-6, yOfL(mean(cmp.thirds.shot))));
|
|
645
|
+
if (Number.isFinite(ev) && Math.abs(ev) > 0.05) {
|
|
646
|
+
push('look.exposure', current.exposure, clampTo(current.exposure * Math.pow(2, ev), 0.05, 8), `the reference is ${Math.abs(ev).toFixed(2)} EV ${ev > 0 ? 'brighter' : 'darker'}`);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
if (cmp.contrastRatio < 0.9 || cmp.contrastRatio > 1.1) {
|
|
650
|
+
push('look.grade.contrast', current.grade.contrast, clampTo(current.grade.contrast * Math.pow(1 / cmp.contrastRatio, 0.6), 0.5, 1.5), `the shot's spread of light is ${Math.round(Math.abs(cmp.contrastRatio - 1) * 100)}% ${cmp.contrastRatio > 1 ? 'wider' : 'narrower'} than the reference's`);
|
|
651
|
+
}
|
|
652
|
+
if (cmp.chromaRatio < 0.9 || cmp.chromaRatio > 1.1) {
|
|
653
|
+
push('look.grade.saturation', current.grade.saturation, clampTo(current.grade.saturation / cmp.chromaRatio, 0, 2), `the shot is ${Math.round(Math.abs(cmp.chromaRatio - 1) * 100)}% ${cmp.chromaRatio > 1 ? 'more colourful' : 'greyer'} than the reference`);
|
|
654
|
+
}
|
|
655
|
+
if (Math.abs(cmp.warmthDelta) > 2) {
|
|
656
|
+
push('look.grade.temperature', current.grade.temperature, clampTo(current.grade.temperature - cmp.warmthDelta * 0.02, -1, 1), `the shot is ${cmp.warmthDelta > 0 ? 'warmer' : 'colder'} (b* ${cmp.warmthDelta > 0 ? '+' : ''}${cmp.warmthDelta.toFixed(1)})`);
|
|
657
|
+
}
|
|
658
|
+
if (Math.abs(cmp.tintDelta) > 2) {
|
|
659
|
+
push('look.grade.tint', current.grade.tint, clampTo(current.grade.tint - cmp.tintDelta * 0.02, -1, 1), `the shot is ${cmp.tintDelta > 0 ? 'more magenta' : 'greener'} (a* ${cmp.tintDelta > 0 ? '+' : ''}${cmp.tintDelta.toFixed(1)})`);
|
|
660
|
+
}
|
|
661
|
+
return out;
|
|
662
|
+
}
|
|
663
|
+
// ---- the grade, on the CPU ----
|
|
664
|
+
/** Rec. 709 luminance, what three's luminance() uses in linear sRGB. */
|
|
665
|
+
const LUMA = [0.2126, 0.7152, 0.0722];
|
|
666
|
+
/** The white balance gain the look pipeline multiplies by (GPU and CPU alike):
|
|
667
|
+
* temperature trades red against blue, tint green against magenta, and the
|
|
668
|
+
* gain is divided by its own luminance, so grey keeps its brightness. */
|
|
669
|
+
export function whiteBalanceGain(temperature, tint) {
|
|
670
|
+
const r = 1 + 0.18 * temperature + 0.05 * tint;
|
|
671
|
+
const g = 1 - 0.1 * tint;
|
|
672
|
+
const b = 1 - 0.18 * temperature + 0.05 * tint;
|
|
673
|
+
const l = LUMA[0] * r + LUMA[1] * g + LUMA[2] * b;
|
|
674
|
+
return [r / l, g / l, b / l];
|
|
675
|
+
}
|
|
676
|
+
/** A matrix given as three column vectors, as three's TSL mat3(vec3, vec3, vec3) is. */
|
|
677
|
+
const byColumns = (c, v) => [
|
|
678
|
+
c[0][0] * v[0] + c[1][0] * v[1] + c[2][0] * v[2],
|
|
679
|
+
c[0][1] * v[0] + c[1][1] * v[1] + c[2][1] * v[2],
|
|
680
|
+
c[0][2] * v[0] + c[1][2] * v[1] + c[2][2] * v[2],
|
|
681
|
+
];
|
|
682
|
+
/** One given as three rows, as TSL mat3 of nine numbers is. */
|
|
683
|
+
const byRows = (r, v) => [
|
|
684
|
+
r[0][0] * v[0] + r[0][1] * v[1] + r[0][2] * v[2],
|
|
685
|
+
r[1][0] * v[0] + r[1][1] * v[1] + r[1][2] * v[2],
|
|
686
|
+
r[2][0] * v[0] + r[2][1] * v[1] + r[2][2] * v[2],
|
|
687
|
+
];
|
|
688
|
+
const clamp01 = (c) => [clampTo(c[0], 0, 1), clampTo(c[1], 0, 1), clampTo(c[2], 0, 1)];
|
|
689
|
+
// three r185's own curves (nodes/display/ToneMappingFunctions.js), number for number.
|
|
690
|
+
const SRGB_TO_2020 = [[0.6274, 0.0691, 0.0164], [0.3293, 0.9195, 0.088], [0.0433, 0.0113, 0.8956]];
|
|
691
|
+
const REC2020_TO_SRGB = [[1.6605, -0.1246, -0.0182], [-0.5876, 1.1329, -0.1006], [-0.0728, -0.0083, 1.1187]];
|
|
692
|
+
const AGX_INSET = [
|
|
693
|
+
[0.856627153315983, 0.137318972929847, 0.11189821299995],
|
|
694
|
+
[0.0951212405381588, 0.761241990602591, 0.0767994186031903],
|
|
695
|
+
[0.0482516061458583, 0.101439036467562, 0.811302368396859],
|
|
696
|
+
];
|
|
697
|
+
const AGX_OUTSET = [
|
|
698
|
+
[1.1271005818144368, -0.1413297634984383, -0.14132976349843826],
|
|
699
|
+
[-0.11060664309660323, 1.157823702216272, -0.11060664309660294],
|
|
700
|
+
[-0.016493938717834573, -0.016493938717834257, 1.2519364065950405],
|
|
701
|
+
];
|
|
702
|
+
const ACES_IN = [[0.59719, 0.35458, 0.04823], [0.076, 0.90834, 0.01566], [0.0284, 0.13383, 0.83777]];
|
|
703
|
+
const ACES_OUT = [[1.60475, -0.53108, -0.07367], [-0.10208, 1.10813, -0.00605], [-0.00327, -0.07276, 1.07602]];
|
|
704
|
+
function agx(c) {
|
|
705
|
+
const minEv = -12.47393;
|
|
706
|
+
const maxEv = 4.026069;
|
|
707
|
+
let v = byColumns(AGX_INSET, byColumns(SRGB_TO_2020, c));
|
|
708
|
+
v = v.map((x) => clampTo((Math.log2(Math.max(x, 1e-10)) - minEv) / (maxEv - minEv), 0, 1));
|
|
709
|
+
v = v.map((x) => {
|
|
710
|
+
const x2 = x * x;
|
|
711
|
+
const x4 = x2 * x2;
|
|
712
|
+
return 15.5 * x4 * x2 - 40.14 * x4 * x + 31.96 * x4 - 6.868 * x2 * x + 0.4298 * x2 + 0.1191 * x - 0.00232;
|
|
713
|
+
});
|
|
714
|
+
v = byColumns(AGX_OUTSET, v);
|
|
715
|
+
v = v.map((x) => Math.pow(Math.max(0, x), 2.2));
|
|
716
|
+
return clamp01(byColumns(REC2020_TO_SRGB, v));
|
|
717
|
+
}
|
|
718
|
+
function neutral(c) {
|
|
719
|
+
const start = 0.8 - 0.04;
|
|
720
|
+
const desaturation = 0.15;
|
|
721
|
+
const x = Math.min(c[0], c[1], c[2]);
|
|
722
|
+
const offset = x < 0.08 ? x - 6.25 * x * x : 0.04;
|
|
723
|
+
let v = c.map((k) => k - offset);
|
|
724
|
+
const peak = Math.max(v[0], v[1], v[2]);
|
|
725
|
+
if (peak < start)
|
|
726
|
+
return v;
|
|
727
|
+
const d = 1 - start;
|
|
728
|
+
const newPeak = 1 - (d * d) / (peak + d - start);
|
|
729
|
+
v = v.map((k) => (k * newPeak) / peak);
|
|
730
|
+
const g = 1 - 1 / (desaturation * (peak - newPeak) + 1);
|
|
731
|
+
return v.map((k) => k + (newPeak - k) * g);
|
|
732
|
+
}
|
|
733
|
+
function aces(c) {
|
|
734
|
+
let v = byRows(ACES_IN, c.map((k) => k / 0.6));
|
|
735
|
+
v = v.map((k) => (k * (k + 0.0245786) - 0.000090537) / (k * ((k + 0.432951) * 0.983729) + 0.238081));
|
|
736
|
+
return clamp01(byRows(ACES_OUT, v));
|
|
737
|
+
}
|
|
738
|
+
/**
|
|
739
|
+
* The look's grade on one colour, as the pipeline does it on the GPU, for what
|
|
740
|
+
* is coloured without it (the splat the universe shows from space): exposure,
|
|
741
|
+
* white balance, CDL, contrast, saturation, the tone curve and posterize, in
|
|
742
|
+
* that order. Linear light in, linear display light out (the sRGB transfer is
|
|
743
|
+
* the caller's). The LUT, vignette and grain are left out: a LUT is a file
|
|
744
|
+
* this cannot read, and the other two belong to the screen, not the colour.
|
|
745
|
+
*/
|
|
746
|
+
export function gradeCpu(rgbLinear, look) {
|
|
747
|
+
const { grade } = look;
|
|
748
|
+
const wb = whiteBalanceGain(grade.temperature, grade.tint);
|
|
749
|
+
let c = [rgbLinear[0] * look.exposure * wb[0], rgbLinear[1] * look.exposure * wb[1], rgbLinear[2] * look.exposure * wb[2]];
|
|
750
|
+
// ASC CDL as three's cdl() does it, saturation 1.
|
|
751
|
+
const { slope, offset, power } = grade.cdl;
|
|
752
|
+
c = c.map((k, i) => {
|
|
753
|
+
const v = Math.max(k * slope[i] + offset[i], 0);
|
|
754
|
+
return v > 0 ? Math.pow(v, power[i]) : v;
|
|
755
|
+
});
|
|
756
|
+
c = c.map((k) => 0.18 * Math.pow(Math.max(k, 0) / 0.18, grade.contrast));
|
|
757
|
+
const l = LUMA[0] * c[0] + LUMA[1] * c[1] + LUMA[2] * c[2];
|
|
758
|
+
c = c.map((k) => Math.max(0, l + (k - l) * grade.saturation));
|
|
759
|
+
c = look.toneMapping === 'agx' ? agx(c) : look.toneMapping === 'neutral' ? neutral(c) : look.toneMapping === 'aces' ? aces(c) : clamp01(c);
|
|
760
|
+
if (grade.posterize > 0) {
|
|
761
|
+
const steps = grade.posterize;
|
|
762
|
+
c = c.map((k) => {
|
|
763
|
+
const s = Math.floor(linearToSrgb(clampTo(k, 0, 1)) * steps) / steps;
|
|
764
|
+
return s <= 0.04045 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
|
|
765
|
+
});
|
|
766
|
+
}
|
|
767
|
+
return c;
|
|
768
|
+
}
|