@mathzetta/qblur 0.0.0-stage → 0.1.2

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/src/cpu.js ADDED
@@ -0,0 +1,586 @@
1
+ /**
2
+ * Filter expressions → JavaScript, for running a filter without a GPU.
3
+ *
4
+ * The same language as the shaders (mathzetta/glsl's filterShader): the
5
+ * same names (r g b a x y px py w h t gray rgb unchanged), the same
6
+ * functions (pixel, blur, gaussian, sobel, zoom, the pictures a filter
7
+ * names…), the same rules for turning a number or a colour into a pixel.
8
+ * The GLSL is typed and so is this: every node of the expression gets a
9
+ * type — float, bool, vec2, vec3 or vec4 — and becomes one local variable
10
+ * per component inside a loop over the pixels. No vector objects exist at
11
+ * run time; a filter is one straight-line loop body.
12
+ *
13
+ * Neighbourhood functions whose argument is the same for every pixel
14
+ * (blur(radius) with radius a parameter) are worked out once per pass,
15
+ * over the whole picture and separably — blur, gaussian, dilate and erode
16
+ * over a square are a horizontal pass then a vertical one, the same sums
17
+ * in a different order — then read per pixel. An argument that changes
18
+ * per pixel falls back to computing that pixel's neighbourhood directly.
19
+ *
20
+ * The generated code is compiled with new Function only after mathzetta's
21
+ * checkGeneratedJs has passed it: every name in it is one this file
22
+ * generates ($-prefixed) or a Math member, it holds no strings, and only
23
+ * the arrays made here can be indexed. A filter is user input — shared by
24
+ * link, pasted as JSON — and can read the picture and write colours,
25
+ * nothing else.
26
+ */
27
+
28
+ import { parseTree } from 'mathzetta';
29
+ import { checkGeneratedJs } from 'mathzetta/formulas';
30
+ import { FILTER_VALUES, FILTER_FUNCTIONS, filterPictureCalls } from 'mathzetta/glsl';
31
+ import * as H from './cpu-helpers.js';
32
+
33
+ const CUSTOM = '\\:';
34
+ const HELPERS = Object.keys(H).filter((k) => typeof H[k] === 'function');
35
+
36
+ /** A lookup table with no inherited names: `constructor` and `toString` are a filter's own names, never Object's. */
37
+ const table = (entries) => Object.assign(Object.create(null), entries);
38
+
39
+ /** Constants and leaves every expression has. */
40
+ const SCALARS = table({ r: '$r', g: '$g', b: '$b', a: '$a', x: '$x', y: '$y', px: '$px', py: '$py', w: '$w', h: '$h', t: '$t', gray: '$gray' });
41
+ const CONSTANTS = table({ pi: 'Math.PI', 'π': 'Math.PI', e: 'Math.E', tau: '(2 * Math.PI)', 'τ': '(2 * Math.PI)', '∞': 'Infinity', '-∞': '(-Infinity)' });
42
+
43
+ /** Functions applied to each component: name → (args as JS) => JS. */
44
+ const COMPONENTWISE = table({
45
+ sqrt: 1, abs: 1, floor: 1, ceil: 1, ln: 'log', sin: 1, cos: 1, tan: 1, arcsin: 'asin', arccos: 'acos', arctan: 'atan',
46
+ sinh: 1, cosh: 1, tanh: 1, arcsinh: 'asinh', arccosh: 'acosh', arctanh: 'atanh', sign: 1, exp: 1, exp2: (x) => `Math.pow(2, ${x})`,
47
+ log2: 1, expm1: (x) => `(Math.exp(${x}) - 1)`, loglog: (x) => `Math.log(Math.log(${x}))`, logloglog: (x) => `Math.log(Math.log(Math.log(${x})))`,
48
+ csc: (x) => `(1 / Math.sin(${x}))`, sec: (x) => `(1 / Math.cos(${x}))`, cot: (x) => `(1 / Math.tan(${x}))`,
49
+ fract: (x) => `(${x} - Math.floor(${x}))`, radians: (x) => `(${x} * 0.017453292519943295)`, degrees: (x) => `(${x} * 57.29577951308232)`,
50
+ inversesqrt: (x) => `(1 / Math.sqrt(${x}))`,
51
+ atan2: (y, x) => `Math.atan2(${y}, ${x})`, pow: (x, y) => `Math.pow(${x}, ${y})`,
52
+ mod: (x, y) => `(${x} - ${y} * Math.floor(${x} / ${y}))`,
53
+ step: (edge, x) => `(${x} < ${edge} ? 0 : 1)`,
54
+ clamp: (x, lo, hi) => `Math.min(Math.max(${x}, ${lo}), ${hi})`,
55
+ mix: (a, b, k) => `(${a} + (${b} - ${a}) * ${k})`,
56
+ smoothstep: (e0, e1, x) => `$h_smoothstep(${e0}, ${e1}, ${x})`,
57
+ });
58
+ const FIXED_ARITY = table({ atan2: 2, pow: 2, mod: 2, step: 2, clamp: 3, mix: 3, smoothstep: 3 });
59
+
60
+ /** Neighbourhood functions: the result kind, and whether the per-pass plane is computed once for a constant argument. */
61
+ const PLANES = table({
62
+ blur: 3, blurx: 3, blury: 3, gaussian: 3, gaussianx: 3, gaussiany: 3, dilate: 3, erode: 3, sharpen: 3,
63
+ sobel: 1, sobelx: 1, sobely: 1, laplacian: 1, emboss: 1,
64
+ });
65
+
66
+ /** A value: a kind ('f' number, 'b' true/false, 'v' vector) and one JS expression per component. */
67
+ const val = (kind, c) => ({ kind, c });
68
+ const f = (e) => val('f', [e]);
69
+ const bool = (e) => val('b', [e]);
70
+ const vec = (c) => (c.length === 1 ? f(c[0]) : val('v', c));
71
+ /** A component as a number: true/false count as 1/0, as when GLSL converts. */
72
+ const num = (v, k = 0) => (v.kind === 'b' ? `(${v.c[0]} ? 1 : 0)` : v.kind === 'f' ? v.c[0] : v.c[k] ?? v.c[0]);
73
+ const size = (v) => (v.kind === 'v' ? v.c.length : 1);
74
+
75
+ class CompileError extends Error {}
76
+
77
+ /**
78
+ * Compile one pass of a compiled filter (compileFilter().passes[i]) for the CPU.
79
+ * @returns {{ run: (ctx) => void, source: string }}
80
+ */
81
+ export function compileCpuPass(pass) {
82
+ if (pass.blendKept) return { blend: true, source: '' };
83
+ const params = pass.params ?? {};
84
+ const pictures = new Set([...Object.keys(pass.placements ?? {}), 'source', 'selection']);
85
+ const g = new Generator(params, pictures, pass.space ?? 'srgb');
86
+ g.graphics = new Set(Object.keys(pass.graphics ?? {})); // drawn by the caller; transparent until they are
87
+ g.bind = table(pass.bind ?? {}); // a graphic's name here → the key its picture is bound under
88
+ const channels = typeof pass.color === 'string' ? { rgba: pass.color } : pass.color;
89
+ const parse = (text, whole) => {
90
+ const names = [...FILTER_FUNCTIONS, ...Object.keys(pass.placements ?? {})];
91
+ const values = [...FILTER_VALUES, ...Object.keys(params)];
92
+ return filterPictureCalls(parseTree(text, { names, values, blocks: null }), pictures, { whole });
93
+ };
94
+ for (const ch of ['rgba', 'rgb', 'r', 'g', 'b', 'a']) {
95
+ if (!channels[ch]) continue;
96
+ let tree;
97
+ try { tree = parse(channels[ch], ch === 'rgba'); } catch (e) { throw new CompileError(e.message); }
98
+ g.channel(ch, tree);
99
+ }
100
+ const source = g.source();
101
+ // the gate: only names made here, Math, operators and numbers; no strings; only our arrays indexed
102
+ checkGeneratedJs(source, { variables: /^\$\w+$/, indexable: /^\$[A-Z]\w*$/, strings: false });
103
+ const fn = new Function(...g.parameters(), source);
104
+ return {
105
+ source,
106
+ uses: g.uses,
107
+ pictures: [...g.pictureNames],
108
+ run(ctx) { fn(...g.arguments(ctx)); },
109
+ };
110
+ }
111
+
112
+ class Generator {
113
+ constructor(params, pictures, space = 'srgb') {
114
+ this.params = params;
115
+ this.pictures = pictures;
116
+ this.space = space === 'srgb' ? null : space; // the frame holds this space: pictures are converted as read
117
+ this.pre = []; // before the loop: params, planes
118
+ this.body = []; // per pixel
119
+ this.n = 0;
120
+ this.uses = new Set(); // 'source', picture names, 'random' …
121
+ this.pictureNames = new Set();
122
+ this.perPixel = new Map(); // picture sample at this pixel: name → vec4 value (computed once)
123
+ this.planes = new Map(); // key → plane variable
124
+ this.scratch = 0;
125
+ }
126
+
127
+ temp(expr, into = this.body) {
128
+ const name = `$v${this.n++}`;
129
+ into.push(`let ${name} = ${expr};`);
130
+ return name;
131
+ }
132
+
133
+ /** A value whose components are temps (so an expression used twice is computed once). */
134
+ keep(v, into = this.body) {
135
+ return val(v.kind, v.c.map((e) => (/^[\w$.]+$|^\(?-?[\d.e+]+\)?$/.test(e) ? e : this.temp(e, into))));
136
+ }
137
+
138
+ fail(why) { throw new CompileError(why); }
139
+
140
+ // ---- leaves ------------------------------------------------------------------
141
+
142
+ leaf(node) {
143
+ if (node.type === 'number') return f(`(${node.value.replace(/\.+$/, '')})`);
144
+ if (node.type === 'string') this.fail('a filter has no text: numbers and colours only');
145
+ const v = node.value;
146
+ const percent = /^(-?(?:\d+\.?\d*|\.\d+))%$/.exec(v);
147
+ if (percent) return f(`(${percent[1]} / 100)`); // 50% is 0.5, as in the shader
148
+ if (v === 't') this.uses.add('t'); // the filter animates (the page hides time for a still one)
149
+ if (SCALARS[v]) return f(SCALARS[v]);
150
+ if (v === 'rgb') return vec(['$r', '$g', '$b']);
151
+ if (v === 'unchanged') return f('$u'); // the kept value; as a whole if/else branch it takes the other branch's type (ternary)
152
+ if (v === 'selected') return f(this.pictureHere('selection').c[0]); // the selection mask: white = 1; white everywhere when none
153
+ if (CONSTANTS[v]) return f(CONSTANTS[v]);
154
+ if (v === 'true' || v === 'false') return bool(v);
155
+ if (Object.prototype.hasOwnProperty.call(this.params, v)) {
156
+ const name = `$p_${v}`;
157
+ return this.params[v].type === 'color' ? vec([`${name}_0`, `${name}_1`, `${name}_2`]) : f(name);
158
+ }
159
+ if (this.pictures.has(v)) { const p = this.pictureHere(v); return vec(p.c.slice(0, 3)); }
160
+ if (v.startsWith('mz_px_') && this.pictures.has(v.slice(6))) return this.pictureHere(v.slice(6));
161
+ this.fail(`unknown name "${v}"`);
162
+ }
163
+
164
+ /** A named picture's colour at this pixel (vec4), sampled once per pixel. */
165
+ pictureHere(name) {
166
+ if (!this.perPixel.has(name)) {
167
+ const s = this.sampler(name, '$px', '$py');
168
+ this.perPixel.set(name, this.keep(s));
169
+ }
170
+ return this.perPixel.get(name);
171
+ }
172
+
173
+ /** vec4 of picture `name` at frame position (X, Y) — `source`, or a placed picture, transparent outside. */
174
+ sampler(name, X, Y) {
175
+ const out = this.scratchArray();
176
+ if (name === 'source') {
177
+ this.uses.add('source');
178
+ this.body.push(`$h_sample($S, $w, $h, ${X}, ${Y}, ${out});`);
179
+ } else {
180
+ this.pictureNames.add(name);
181
+ this.body.push(`$h_picture($Q_${name}, $qw_${name}, $qh_${name}, $w, $h, $qz_${name}, $qx_${name}, $qy_${name}, $qa_${name}, $qm_${name}, ${X}, ${Y}, ${out});`);
182
+ }
183
+ if (this.space && name !== 'selection') this.body.push(`$h_col_to${this.space}(${out}[0], ${out}[1], ${out}[2], ${out});`); // a mask is not a colour
184
+ return vec([0, 1, 2, 3].map((k) => `${out}[${k}]`));
185
+ }
186
+
187
+ scratchArray() {
188
+ const name = `$T${this.scratch++}`;
189
+ this.pre.push(`let ${name} = $h_scratch();`);
190
+ return name;
191
+ }
192
+
193
+ // ---- operations -------------------------------------------------------------
194
+
195
+ value(node) {
196
+ return node.type === 'op' ? this.op(node) : this.leaf(node);
197
+ }
198
+
199
+ op(node) {
200
+ const { op, args } = node;
201
+ const custom = op.startsWith(CUSTOM) ? op.slice(CUSTOM.length) : null;
202
+ if (custom !== null) return this.call(custom, args, node);
203
+ const A = () => args.map((a) => this.value(a));
204
+ switch (op) {
205
+ case '+': case '-': case '*': case '/': {
206
+ const [a, b] = A();
207
+ return this.componentwise([a, b], (x, y) => `(${x} ${op} ${y})`);
208
+ }
209
+ case 'neg': return this.componentwise(A(), (x) => `(-${x})`);
210
+ case '^': return this.power(args);
211
+ case 'mod': return this.componentwise(A(), COMPONENTWISE.mod);
212
+ case '<': case '>': case '≤': case '≥': {
213
+ const [a, b] = A();
214
+ const sym = { '≤': '<=', '≥': '>=' }[op] ?? op;
215
+ return bool(`(${num(a)} ${sym} ${num(b)})`);
216
+ }
217
+ case '=': case '===': case '≠': case '≠=': case '≡': {
218
+ const [a, b] = A();
219
+ const n = Math.max(size(a), size(b));
220
+ const all = Array.from({ length: n }, (_, k) => `${num(a, k)} === ${num(b, k)}`).join(' && ');
221
+ return bool(op === '≠' || op === '≠=' ? `!(${all})` : `(${all})`);
222
+ }
223
+ case '≈': { const [a, b] = A(); return bool(`(Math.abs(${num(a)} - ${num(b)}) < 1e-5)`); }
224
+ case '&': { const [a, b] = A(); return bool(`(${this.truth(a)} && ${this.truth(b)})`); }
225
+ case '|': { const [a, b] = A(); return bool(`(${this.truth(a)} || ${this.truth(b)})`); }
226
+ case '\\xor': { const [a, b] = A(); return bool(`(${this.truth(a)} !== ${this.truth(b)})`); }
227
+ case '\\nand': { const [a, b] = A(); return bool(`!(${this.truth(a)} && ${this.truth(b)})`); }
228
+ case '\\nor': { const [a, b] = A(); return bool(`!(${this.truth(a)} || ${this.truth(b)})`); }
229
+ case '\\not': { const [a] = A(); return bool(`!${this.truth(a)}`); }
230
+ case '?:': return this.ternary(args);
231
+ case 'max': case 'min': return this.componentwise(A(), (...xs) => `Math.${op}(${xs.join(', ')})`);
232
+ case 'sum': return this.componentwise(A(), (...xs) => `(${xs.join(' + ')})`);
233
+ case 'prod': return this.componentwise(A(), (...xs) => `(${xs.join(' * ')})`);
234
+ case 'mean': return this.componentwise(A(), (...xs) => `((${xs.join(' + ')}) / ${xs.length})`);
235
+ case '()': case '[]': case '{}': case '(]': case '[)': {
236
+ const parts = A().flatMap((v) => (v.kind === 'b' ? [num(v)] : v.c.map((_, k) => num(v, k))));
237
+ if (parts.length > 4) this.fail('a colour has at most 4 values (r, g, b, a)');
238
+ return vec(parts);
239
+ }
240
+ case '!': return this.componentwise(A(), (x) => `$h_factorial(${x})`);
241
+ case 'perm': return this.componentwise(A(), (n, k) => `$h_perm(${n}, ${k})`);
242
+ case 'comb': return this.componentwise(A(), (n, k) => `($h_perm(${n}, ${k}) / $h_factorial(${k}))`);
243
+ case 'gcd': return this.componentwise(A(), (...xs) => xs.reduce((acc, x) => `$h_gcd(${acc}, ${x})`));
244
+ case 'round': {
245
+ const [v, d] = A();
246
+ return this.componentwise([v, d], (x, digits) => `$h_round(${x}, ${digits})`);
247
+ }
248
+ case 'log': { const [base, x] = A(); return this.componentwise([base, x], (b, xx) => `(Math.log(${xx}) / Math.log(${b}))`); }
249
+ case 'root': { const [n, x] = A(); return this.componentwise([n, x], (nn, xx) => `$h_root(${nn}, ${xx})`); }
250
+ case '∈': case '∉': return this.membership(node);
251
+ case 'random': return this.random();
252
+ case '_': {
253
+ const [a, b] = args;
254
+ if (a.type === 'symbol' && (b.type === 'number' || b.type === 'symbol')) return this.leaf({ type: 'symbol', value: `${a.value}_${b.value}` });
255
+ break;
256
+ }
257
+ }
258
+ if (COMPONENTWISE[op]) return this.builtin(op, A());
259
+ this.fail(`"${op}" isn't available in a filter`);
260
+ }
261
+
262
+ truth(v) { return v.kind === 'b' ? v.c[0] : `(${num(v)} !== 0)`; }
263
+
264
+ /** Apply fn to each component; a number (or true/false) is used for every component of a vector. */
265
+ componentwise(values, fn) {
266
+ const sizes = values.map(size);
267
+ const n = Math.max(...sizes);
268
+ const vectors = new Set(sizes.filter((s) => s > 1));
269
+ if (vectors.size > 1) this.fail(`can't combine vectors of ${[...vectors].join(' and ')} values`);
270
+ const out = Array.from({ length: n }, (_, k) => fn(...values.map((v) => num(v, k))));
271
+ return this.keep(vec(out));
272
+ }
273
+
274
+ builtin(name, values) {
275
+ const spec = COMPONENTWISE[name];
276
+ if (FIXED_ARITY[name] && values.length !== FIXED_ARITY[name]) this.fail(`${name}() takes ${FIXED_ARITY[name]} values`);
277
+ if (typeof spec === 'function') return this.componentwise(values, spec);
278
+ const fn = spec === 1 ? name : spec;
279
+ return this.componentwise(values, (...xs) => `Math.${fn}(${xs.join(', ')})`);
280
+ }
281
+
282
+ /** x^n as the shader writes it: products for small whole powers, exp for e^x, pow otherwise. */
283
+ power([base, exp]) {
284
+ const n = exp.type === 'number' && /^-?\d+$/.test(exp.value) ? Number(exp.value) : null;
285
+ const b = this.keep(this.value(base));
286
+ const times = (k) => this.componentwise([b], (x) => Array(k).fill(x).join(' * '));
287
+ if (n === 0) return this.componentwise([b], () => '1');
288
+ if (n !== null && n >= 1 && n <= 4) return times(n);
289
+ if (n !== null && n <= -1 && n >= -4) return this.componentwise([times(-n)], (x) => `(1 / ${x})`);
290
+ if (base.type === 'symbol' && base.value === 'e') return this.componentwise([this.value(exp)], (x) => `Math.exp(${x})`);
291
+ if (base.type === 'number' && base.value === '2') return this.componentwise([this.value(exp)], (x) => `Math.pow(2, ${x})`);
292
+ return this.componentwise([b, this.value(exp)], (x, y) => `Math.pow(${x}, ${y})`);
293
+ }
294
+
295
+ ternary([c, yes, no]) {
296
+ const cond = this.truth(this.value(c));
297
+ const isU = (n) => n.type === 'symbol' && n.value === 'unchanged';
298
+ let a = isU(yes) ? null : this.value(yes);
299
+ let b = isU(no) ? null : this.value(no);
300
+ // `unchanged` in a branch takes the other branch's type (mz_unchanged in the shader)
301
+ const unchangedLike = (other) => (other.kind === 'b' ? bool('($u > 0.5)') : other.kind === 'f' ? f('$u')
302
+ : vec(['$k0', '$k1', '$k2', '$k3'].slice(0, other.c.length === 4 ? 4 : 3)));
303
+ if (!a && !b) return f('$u');
304
+ if (!a) a = unchangedLike(b);
305
+ if (!b) b = unchangedLike(a);
306
+ if (a.kind === 'b' && b.kind === 'b') return bool(this.temp(`(${cond} ? ${a.c[0]} : ${b.c[0]})`));
307
+ const n = Math.max(size(a), size(b));
308
+ const out = Array.from({ length: n }, (_, k) => this.temp(`(${cond} ? ${num(a, k)} : ${num(b, k)})`));
309
+ return vec(out);
310
+ }
311
+
312
+ membership(node) {
313
+ const [x, set] = node.args;
314
+ const v = num(this.value(x));
315
+ const negate = node.op === '∉';
316
+ let test;
317
+ if (set.type === 'op' && ['()', '[]', '(]', '[)'].includes(set.op) && set.args.length === 2) {
318
+ const [lo, hi] = set.args.map((a) => num(this.value(a)));
319
+ test = `(${v} ${set.op[0] === '[' ? '>=' : '>'} ${lo} && ${v} ${set.op[1] === ']' ? '<=' : '<'} ${hi})`;
320
+ } else if (set.type === 'op' && set.op === '{}') {
321
+ test = `(${set.args.map((item) => `${v} === ${num(this.value(item))}`).join(' || ')})`;
322
+ } else if (set.type === 'symbol' && ['ℝ', 'ℤ', 'ℕ'].includes(set.value)) {
323
+ test = { 'ℝ': `Number.isFinite(${v})`, 'ℤ': `Number.isInteger(${v})`, 'ℕ': `(Number.isInteger(${v}) && ${v} > 0)` }[set.value];
324
+ } else this.fail('∈ needs an interval [a, b], a set {…}, ℝ, ℤ or ℕ');
325
+ return bool(negate ? `!${test}` : test);
326
+ }
327
+
328
+ random() {
329
+ this.uses.add('random');
330
+ return f(this.temp('$h_random($px, $py, $h, ++$seed)'));
331
+ }
332
+
333
+ // ---- functions: pictures, geometry, neighbourhoods, GLSL built-ins ------------
334
+
335
+ call(name, args, node) {
336
+ const A = () => args.map((a) => this.value(a));
337
+ if (name.startsWith('mz_sample_')) { // a picture read with its alpha: 0 outside it
338
+ const picture = name.slice(10);
339
+ const [p0, p1] = A();
340
+ const [X, Y] = p1 ? [num(p0), num(p1)] : [num(p0, 0), num(p0, 1)];
341
+ if (picture === 'image') {
342
+ const out = this.scratchArray();
343
+ this.body.push(`$h_sampleOrZero($I, $w, $h, ${X}, ${Y}, ${out});`);
344
+ return this.keep(vec([0, 1, 2, 3].map((k) => `${out}[${k}]`)));
345
+ }
346
+ if (!this.pictures.has(picture)) this.fail(`unknown picture "${picture}"`);
347
+ return this.keep(this.sampler(picture, X, Y));
348
+ }
349
+ if (name.startsWith('mz_at_')) {
350
+ const picture = name.slice(6);
351
+ const [p0, p1] = A();
352
+ const [X, Y] = p1 ? [num(p0), num(p1)] : [num(p0, 0), num(p0, 1)];
353
+ if (picture === 'image') {
354
+ const out = this.scratchArray();
355
+ this.body.push(`$h_sampleOrZero($I, $w, $h, ${X}, ${Y}, ${out});`);
356
+ return vec([0, 1, 2].map((k) => `${out}[${k}]`));
357
+ }
358
+ if (!this.pictures.has(picture)) this.fail(`unknown picture "${picture}"`);
359
+ return this.keep(vec(this.sampler(picture, X, Y).c.slice(0, 3)));
360
+ }
361
+ if (name.startsWith('mz_width_') || name.startsWith('mz_height_')) {
362
+ const picture = name.replace(/^mz_(width|height)_/, '');
363
+ this.pictureNames.add(picture);
364
+ return f(name.startsWith('mz_width_') ? `$qw_${picture}` : `$qh_${picture}`);
365
+ }
366
+ switch (name) {
367
+ case 'pixel': {
368
+ const [X, Y] = A().map((v) => num(v));
369
+ const out = this.scratchArray();
370
+ this.body.push(`$h_sample($I, $w, $h, ${X}, ${Y}, ${out});`);
371
+ return this.keep(vec([0, 1, 2, 3].map((k) => `${out}[${k}]`)));
372
+ }
373
+ case 'red': case 'green': case 'blue': case 'alpha': case 'luma': {
374
+ let v;
375
+ if (args.length === 2) v = this.call('pixel', args);
376
+ else if (args.length === 1) v = this.value(args[0]);
377
+ else this.fail(`${name}() takes a colour, or a position px, py`);
378
+ const k = { red: 0, green: 1, blue: 2, alpha: 3 }[name];
379
+ if (name === 'luma') return f(`(${num(v, 0)} * 0.299 + ${num(v, 1)} * 0.587 + ${num(v, 2)} * 0.114)`);
380
+ if (name === 'alpha' && size(v) === 3) return f('1');
381
+ return f(num(v, k));
382
+ }
383
+ case 'length': { const [v] = A(); return f(`Math.sqrt(${v.c.map((_, k) => `${num(v, k)} * ${num(v, k)}`).join(' + ')})`); }
384
+ case 'distance': {
385
+ const [a, b] = A();
386
+ const d = this.componentwise([a, b], (x, y) => `(${x} - ${y})`);
387
+ return f(`Math.sqrt(${d.c.map((e) => `${e} * ${e}`).join(' + ')})`);
388
+ }
389
+ case 'dot': {
390
+ const [a, b] = A();
391
+ const n = Math.max(size(a), size(b));
392
+ return f(`(${Array.from({ length: n }, (_, k) => `${num(a, k)} * ${num(b, k)}`).join(' + ')})`);
393
+ }
394
+ case 'normalize': {
395
+ const [v] = A();
396
+ const len = this.temp(`Math.sqrt(${v.c.map((_, k) => `${num(v, k)} * ${num(v, k)}`).join(' + ')})`);
397
+ return this.componentwise([v], (x) => `(${x} / ${len})`);
398
+ }
399
+ case 'cross': {
400
+ const [a, b] = A();
401
+ if (size(a) !== 3 || size(b) !== 3) this.fail('cross() takes two colours (3 values each)');
402
+ const [x1, y1, z1] = a.c; const [x2, y2, z2] = b.c;
403
+ return this.keep(vec([`(${y1} * ${z2} - ${z1} * ${y2})`, `(${z1} * ${x2} - ${x1} * ${z2})`, `(${x1} * ${y2} - ${y1} * ${x2})`]));
404
+ }
405
+ case 'convolve': {
406
+ const ks = A().map((v) => num(v));
407
+ if (ks.length !== 9) this.fail('convolve() takes 9 numbers: the 3×3 kernel, row by row');
408
+ const out = this.scratchArray();
409
+ this.body.push(`$h_convolve($I, $w, $h, $px, $py, ${ks.join(', ')}, ${out});`);
410
+ return vec([0, 1, 2].map((k) => `${out}[${k}]`));
411
+ }
412
+ case 'mz_zoom': case 'mz_shift': case 'mz_rotate': case 'mz_crop': case 'mz_tile': case 'mz_flipx': case 'mz_flipy':
413
+ return this.geometry(name.slice(3), A().map((v) => num(v)));
414
+ case 'tolinear': case 'fromlinear': case 'tohsv': case 'fromhsv': case 'tohsl': case 'fromhsl':
415
+ case 'tolab': case 'fromlab': case 'tooklab': case 'fromoklab': case 'toycbcr': case 'fromycbcr': {
416
+ const a = A();
417
+ const c = a.length === 3 ? a.map((v) => num(v)) : [0, 1, 2].map((k) => num(a[0], k)); // a colour, or three numbers
418
+ if (a.length !== 1 && a.length !== 3) this.fail(`${name}() takes a colour`);
419
+ const out = this.scratchArray();
420
+ this.body.push(`$h_col_${name}(${c.join(', ')}, ${out});`);
421
+ return this.keep(vec([0, 1, 2].map((k) => `${out}[${k}]`)));
422
+ }
423
+ case 'random': return this.random();
424
+ case 'exp': case 'exp2': case 'log2': case 'sign': case 'fract': case 'radians': case 'degrees': case 'inversesqrt':
425
+ case 'pow': case 'mod': case 'step': case 'clamp': case 'mix': case 'smoothstep':
426
+ case 'abs': case 'floor': case 'ceil': case 'sqrt': case 'sin': case 'cos': case 'tan':
427
+ return this.builtin(name, A());
428
+ case 'asin': return this.builtin('arcsin', A());
429
+ case 'acos': return this.builtin('arccos', A());
430
+ case 'atan': return args.length === 2 ? this.builtin('atan2', A()) : this.builtin('arctan', A());
431
+ case 'log': return this.builtin('ln', A());
432
+ case 'max': case 'min': return this.componentwise(A(), (...xs) => `Math.${name}(${xs.join(', ')})`);
433
+ }
434
+ if (PLANES[name]) return this.neighbourhood(name, args);
435
+ this.fail(`unknown function "${name}"`);
436
+ }
437
+
438
+ /** Where to read the picture from (vec2, pixels): the shader's mz_zoom … mz_flipy. */
439
+ geometry(kind, a) {
440
+ const P = (x, y) => this.keep(vec([x, y]));
441
+ switch (kind) {
442
+ case 'zoom': {
443
+ const [s, cx = '0.5', cy = '0.5'] = a;
444
+ const c = P(`(${cx} * $w - 0.5)`, `(${cy} * $h - 0.5)`);
445
+ return P(`(${c.c[0]} + ($px - ${c.c[0]}) / ${s})`, `(${c.c[1]} + ($py - ${c.c[1]}) / ${s})`);
446
+ }
447
+ case 'shift': {
448
+ const [dx, dy] = a;
449
+ return P(`($h_fmod($px - ${dx} + 0.5, $w) - 0.5)`, `($h_fmod($py - ${dy} + 0.5, $h) - 0.5)`);
450
+ }
451
+ case 'rotate': {
452
+ const [ang, cx = '0.5', cy = '0.5'] = a;
453
+ const c = P(`(${cx} * $w - 0.5)`, `(${cy} * $h - 0.5)`);
454
+ const d = P(`($px - ${c.c[0]})`, `($py - ${c.c[1]})`);
455
+ const cs = this.keep(vec([`Math.cos(${ang})`, `Math.sin(${ang})`]));
456
+ return P(`(${c.c[0]} + ${cs.c[0]} * ${d.c[0]} + ${cs.c[1]} * ${d.c[1]})`, `(${c.c[1]} + ${cs.c[0]} * ${d.c[1]} - ${cs.c[1]} * ${d.c[0]})`);
457
+ }
458
+ case 'crop': {
459
+ const [x0, y0, x1, y1] = a;
460
+ return P(`((${x0} + (${x1} - ${x0}) * ($px + 0.5) / $w) * $w - 0.5)`, `((${y0} + (${y1} - ${y0}) * ($py + 0.5) / $h) * $h - 0.5)`);
461
+ }
462
+ case 'tile': {
463
+ const [nx, ny = nx] = a;
464
+ return P(`($h_fmod(($px + 0.5) * ${nx}, $w) - 0.5)`, `($h_fmod(($py + 0.5) * ${ny}, $h) - 0.5)`);
465
+ }
466
+ case 'flipx': return P('($w - 1 - $px)', '$py');
467
+ case 'flipy': return P('$px', '($h - 1 - $py)');
468
+ }
469
+ this.fail(`unknown geometry ${kind}`);
470
+ }
471
+
472
+ /**
473
+ * blur(radius) and friends. With an argument the same for every pixel (numbers, params, t),
474
+ * the whole plane is computed once before the loop; otherwise this pixel's neighbourhood is.
475
+ */
476
+ neighbourhood(name, args) {
477
+ const kind = PLANES[name];
478
+ const constant = args.every((a) => this.isUniform(a));
479
+ if (constant) {
480
+ const argJs = args.map((a) => num(this.uniformValue(a)));
481
+ const key = `${name}(${argJs.join(',')})`;
482
+ if (!this.planes.has(key)) {
483
+ const plane = `$N${this.planes.size}`;
484
+ this.pre.push(`let ${plane} = $h_plane_${name}($I, $w, $h, $y0, $y1${argJs.map((x) => `, ${x}`).join('')});`);
485
+ this.planes.set(key, plane);
486
+ }
487
+ const plane = this.planes.get(key);
488
+ return kind === 3 ? vec([0, 1, 2].map((k) => `${plane}[$j3 + ${k}]`)) : f(`${plane}[$j]`);
489
+ }
490
+ const argJs = args.map((a) => num(this.value(a)));
491
+ if (kind === 1) return f(this.temp(`$h_at_${name}($I, $w, $h, $px, $py${argJs.map((x) => `, ${x}`).join('')})`));
492
+ const out = this.scratchArray();
493
+ this.body.push(`$h_at_${name}($I, $w, $h, $px, $py${argJs.map((x) => `, ${x}`).join('')}, ${out});`);
494
+ return vec([0, 1, 2].map((k) => `${out}[${k}]`));
495
+ }
496
+
497
+ /** Does this subtree read nothing that changes from pixel to pixel? */
498
+ isUniform(node) {
499
+ if (node.type === 'number') return true;
500
+ if (node.type === 'symbol') {
501
+ return ['w', 'h', 't'].includes(node.value) || Boolean(CONSTANTS[node.value])
502
+ || (Object.prototype.hasOwnProperty.call(this.params, node.value) && this.params[node.value].type !== 'color');
503
+ }
504
+ if (node.type !== 'op' || node.op.startsWith(CUSTOM) || node.op === 'random') return false;
505
+ return node.args.every((a) => this.isUniform(a));
506
+ }
507
+
508
+ /** A uniform subtree, generated before the loop. */
509
+ uniformValue(node) {
510
+ const saved = this.body;
511
+ this.body = this.pre;
512
+ try { return this.value(node); } finally { this.body = saved; }
513
+ }
514
+
515
+ // ---- assembling the pass ------------------------------------------------------
516
+
517
+ /** One channel line of the colour, as filterShader writes it (mz_keep, then mz_rgba / mz_rgb / mz_ch_*). */
518
+ channel(ch, tree) {
519
+ const keepValue = ch === 'rgba' || ch === 'rgb' ? '$gray' : `$c${'rgba'.indexOf(ch)}`;
520
+ this.body.push(`$u = ${keepValue}; $k0 = $c0; $k1 = $c1; $k2 = $c2; $k3 = $c3;`);
521
+ const v = this.value(tree);
522
+ const n = size(v);
523
+ const comp = (k) => num(v, k);
524
+ if (ch === 'rgba') {
525
+ if (n === 2) this.fail('a colour is one number (grey), three (r, g, b) or four (r, g, b, a) — not two');
526
+ const c = n === 1 ? [comp(0), comp(0), comp(0), '1'] : n === 3 ? [comp(0), comp(1), comp(2), '1'] : [0, 1, 2, 3].map(comp);
527
+ this.body.push(`$c0 = ${c[0]}; $c1 = ${c[1]}; $c2 = ${c[2]}; $c3 = ${c[3]};`);
528
+ } else if (ch === 'rgb') {
529
+ if (n === 2) this.fail('a colour is one number (grey), three (r, g, b) or four — not two');
530
+ const c = n === 1 ? [comp(0), comp(0), comp(0)] : [comp(0), comp(1), comp(2)];
531
+ this.body.push(`$c0 = ${c[0]}; $c1 = ${c[1]}; $c2 = ${c[2]};`);
532
+ } else {
533
+ const k = 'rgba'.indexOf(ch);
534
+ const x = n === 1 ? comp(0) : n === 3 ? (ch === 'a' ? '1' : comp(k)) : comp(k);
535
+ this.body.push(`$c${k} = ${x};`);
536
+ }
537
+ }
538
+
539
+ parameters() {
540
+ const pictures = [...this.pictureNames];
541
+ return ['$I', '$O', '$S', '$w', '$h', '$t', '$P', '$op', '$y0', '$y1',
542
+ ...pictures.flatMap((p) => [`$Q_${p}`, `$qw_${p}`, `$qh_${p}`, `$qz_${p}`, `$qx_${p}`, `$qy_${p}`, `$qa_${p}`, `$qm_${p}`]),
543
+ ...HELPERS.map((k) => `$h_${k}`)];
544
+ }
545
+
546
+ arguments(ctx) {
547
+ const pictures = [...this.pictureNames];
548
+ return [ctx.input, ctx.output, ctx.source, ctx.width, ctx.height, ctx.t, ctx.params, ctx.opacity, ctx.y0 ?? 0, ctx.y1 ?? ctx.height,
549
+ ...pictures.flatMap((p) => {
550
+ const pic = ctx.pictures[this.bind?.[p] ?? p] ?? (this.graphics?.has(p) ? H.CLEAR : H.BLANK);
551
+ const pl = ctx.placements[p] ?? {};
552
+ return [pic.data, pic.width, pic.height, Number(pl.zoom ?? 1), Number(pl.x ?? 0.5), Number(pl.y ?? 0.5), Number(pl.angle ?? 0), H.placeCode(pl.place)];
553
+ }),
554
+ ...HELPERS.map((k) => H[k])];
555
+ }
556
+
557
+ source() {
558
+ const paramLines = Object.entries(this.params).map(([name, p], i) => (p.type === 'color'
559
+ ? `let $p_${name}_0 = $P[${i}][0], $p_${name}_1 = $P[${i}][1], $p_${name}_2 = $P[${i}][2];`
560
+ : `let $p_${name} = $P[${i}];`));
561
+ return [
562
+ ...paramLines.map((l) => l.replace(/\$P\[(\d+)\]\[(\d)\]/g, '$h_at($P, $1, $2)')),
563
+ 'let $u = 0, $k0 = 0, $k1 = 0, $k2 = 0, $k3 = 0, $seed = 0, $CS = $h_scratch();',
564
+ ...this.pre,
565
+ 'for (let $py = $y0; $py < $y1; $py++) {',
566
+ ' for (let $px = 0; $px < $w; $px++) {',
567
+ ' let $j = $py * $w + $px, $j3 = $j * 3, $j4 = $j * 4;',
568
+ ' let $r = $I[$j4], $g = $I[$j4 + 1], $b = $I[$j4 + 2], $a = $I[$j4 + 3];',
569
+ ' let $c0 = $r, $c1 = $g, $c2 = $b, $c3 = $a;',
570
+ ' let $x = ($px + 0.5) / $w, $y = ($py + 0.5) / $h;',
571
+ this.space
572
+ ? ` $h_col_from${this.space}($r, $g, $b, $CS); let $gray = $CS[0] * 0.299 + $CS[1] * 0.587 + $CS[2] * 0.114;` // the brightness as seen
573
+ : ' let $gray = $r * 0.299 + $g * 0.587 + $b * 0.114;',
574
+ ' $seed = $t;',
575
+ ...this.body.map((l) => ` ${l}`),
576
+ ' $O[$j4] = $r + ($c0 - $r) * $op;',
577
+ ' $O[$j4 + 1] = $g + ($c1 - $g) * $op;',
578
+ ' $O[$j4 + 2] = $b + ($c2 - $b) * $op;',
579
+ ' $O[$j4 + 3] = $a + ($c3 - $a) * $op;',
580
+ ' }',
581
+ '}',
582
+ ].join('\n');
583
+ }
584
+ }
585
+
586
+ export { CompileError };
@@ -0,0 +1,16 @@
1
+ /** `import '@mathzetta/qblur/element'` defines <qblur-image>; the class is exported for subclassing. */
2
+ export declare class QBlurImage extends HTMLElement {
3
+ static readonly observedAttributes: string[];
4
+ /** The drawing, a 2D canvas the picture's size (for toBlob). */
5
+ readonly canvas: HTMLCanvasElement;
6
+ /** Maths, a built-in filter's name, filter JSON or a share link (the `filter` attribute). */
7
+ filter: string;
8
+ /** Knob values; set an object, or the `params` attribute as JSON. */
9
+ params: Record<string, number | [number, number, number]>;
10
+ /** Draws again; resolves when drawn. */
11
+ redraw(): Promise<void>;
12
+ }
13
+
14
+ declare global {
15
+ interface HTMLElementTagNameMap { 'qblur-image': QBlurImage }
16
+ }