@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.
@@ -0,0 +1,282 @@
1
+ /**
2
+ * Runs a compiled filter (compileFilter()) on the CPU — no GPU, no WebGL —
3
+ * with the same interface as createFilterRunner:
4
+ *
5
+ * const runner = createCpuRunner(compileFilter(def));
6
+ * runner.setImage(imageData); // ImageData, { data, width, height }, or in a browser an <img>/<canvas>/<video>
7
+ * runner.setImage('mask', maskImage);
8
+ * const out = runner.draw({ t: 0 }); // { data: Uint8ClampedArray, width, height }
9
+ * runner.draw({ t: 0, canvas }); // …and put it on a canvas
10
+ * runner.passes[0].values.cutoff = 0.3; // knobs change without recompiling
11
+ *
12
+ * Works in Node too (thumbnails, servers, tests without a browser).
13
+ *
14
+ * draw() runs on the calling thread. drawAsync() spreads each pass's rows
15
+ * over a pool of workers when the picture is large enough to be worth it
16
+ * (≥ options.parallelFrom pixels, 250k by default); with SharedArrayBuffer
17
+ * (Node, or a cross-origin isolated page) the workers read and write the
18
+ * pictures in place, otherwise each gets a copy of the input and sends its
19
+ * rows back. Both give the same pixels. The worker is cpu-worker.js beside this
20
+ * module (options.workerUrl to put it elsewhere); from another origin — a CDN —
21
+ * it is started through a same-origin blob: module that imports it. When no
22
+ * worker can start, drawAsync() draws on the calling thread instead.
23
+ *
24
+ * Between passes the picture is rounded to 8 bits, as the GPU runner's
25
+ * textures are, so both give the same pixels; { precision: 'float' } keeps
26
+ * full precision instead (like 'float' textures on the GPU).
27
+ */
28
+
29
+ import { compileCpuPass } from './cpu.js';
30
+
31
+ export function createCpuRunner(compiled, options = {}) {
32
+ const precision = options.precision === 'float' || options.precision === 'half' ? 'float' : 'byte';
33
+ const programs = compiled.passes.map((pass) => compileCpuPass(pass));
34
+ const pictures = {};
35
+ let source = null;
36
+ const parallelFrom = options.parallelFrom ?? 250000;
37
+ const wanted = options.workers === undefined || options.workers === 'auto' ? defaultWorkers() : Number(options.workers);
38
+ let pool = null; // created on the first drawAsync() that needs it
39
+ let starting = null; // the promise of that pool: every call shares one
40
+ let sent = new Map(); // picture name → the object last sent to the pool
41
+
42
+ const runner = {
43
+ passes: compiled.passes,
44
+ get precision() { return precision; },
45
+ /** The generated JavaScript of each pass, for the curious (and the tests). */
46
+ sources: programs.map((p) => p.source),
47
+
48
+ setImage(name, image) {
49
+ if (typeof name !== 'string') { image = name; name = null; }
50
+ const picture = toPicture(image);
51
+ if (!name || name === 'source') source = picture;
52
+ else pictures[name] = picture;
53
+ return runner;
54
+ },
55
+
56
+ /** Runs every pass; returns the result as 8-bit RGBA (and draws it on `canvas` when given). */
57
+ draw({ t = 0, canvas = null } = {}) {
58
+ if (!source) throw new Error('setImage() first');
59
+ const { width, height } = source;
60
+ const n = width * height * 4;
61
+ let input = source.data;
62
+ const kept = [];
63
+ compiled.passes.forEach((pass, i) => {
64
+ for (let k = 0; k < pass.keepInput; k++) kept.push(input);
65
+ const output = new Float32Array(n);
66
+ if (pass.blendKept) {
67
+ const base = kept.pop();
68
+ const o = pass.opacity;
69
+ for (let j = 0; j < n; j++) output[j] = base[j] + (input[j] - base[j]) * o;
70
+ } else {
71
+ programs[i].run({
72
+ input, output, source: source.data, width, height, t,
73
+ params: Object.keys(pass.params ?? {}).map((name) => pass.values[name]),
74
+ opacity: pass.opacity, pictures, placements: pass.placements ?? {},
75
+ });
76
+ }
77
+ if (precision === 'byte') quantize(output);
78
+ input = output;
79
+ });
80
+ const data = new Uint8ClampedArray(n);
81
+ for (let j = 0; j < n; j++) {
82
+ const v = input[j];
83
+ data[j] = v > 0 ? (v < 1 ? Math.round(v * 255) : 255) : 0; // NaN → 0, as a GPU writes it
84
+ }
85
+ const out = { data, width, height };
86
+ if (canvas) {
87
+ canvas.width = width;
88
+ canvas.height = height;
89
+ canvas.getContext('2d').putImageData(new ImageData(data, width, height), 0, 0);
90
+ }
91
+ return out;
92
+ },
93
+
94
+ /**
95
+ * As draw(), with each pass's rows spread over workers when the picture is large;
96
+ * smaller pictures (or no workers available) run here, as draw().
97
+ */
98
+ async drawAsync({ t = 0, canvas = null } = {}) {
99
+ if (!source) throw new Error('setImage() first');
100
+ const { width, height } = source;
101
+ if (wanted < 2 || width * height < parallelFrom) return runner.draw({ t, canvas });
102
+ // one pool, however many calls arrive before it is ready; false: workers can't start here
103
+ starting ??= createPool(wanted, compiled.passes, options.workerUrl).catch(() => false);
104
+ const mine = starting;
105
+ const ready = await mine;
106
+ if (mine !== starting) throw new Error('The runner was disposed of while drawing');
107
+ pool = ready;
108
+ if (!ready) return runner.draw({ t, canvas });
109
+ const n = width * height * 4;
110
+ const alloc = () => (ready.shared ? new Float32Array(new SharedArrayBuffer(n * 4)) : new Float32Array(n));
111
+ // pictures the workers haven't seen (as shared copies when we can share)
112
+ for (const [name, picture] of [['source', source], ...Object.entries(pictures)]) {
113
+ if (sent.get(name) === picture) continue;
114
+ await ready.broadcast({ type: 'picture', name, picture: ready.shared ? sharedPicture(picture) : picture });
115
+ sent.set(name, picture);
116
+ }
117
+ let input = ready.shared ? sharedPicture(source).data : source.data;
118
+ const kept = [];
119
+ for (let i = 0; i < compiled.passes.length; i++) {
120
+ const pass = compiled.passes[i];
121
+ for (let k = 0; k < pass.keepInput; k++) kept.push(input);
122
+ const output = alloc();
123
+ if (pass.blendKept) {
124
+ const base = kept.pop();
125
+ for (let j = 0; j < n; j++) output[j] = base[j] + (input[j] - base[j]) * pass.opacity;
126
+ if (precision === 'byte') quantize(output);
127
+ } else {
128
+ const params = Object.keys(pass.params ?? {}).map((name) => pass.values[name]);
129
+ const bands = splitRows(height, ready.size);
130
+ const replies = await Promise.all(bands.map(([y0, y1], k) => ready.ask(k, {
131
+ type: 'run', pass: i, input, output: ready.shared ? output : null, width, height, t, params,
132
+ opacity: pass.opacity, placements: pass.placements ?? {}, y0, y1, quantize: precision === 'byte',
133
+ })));
134
+ if (!ready.shared) replies.forEach((r, k) => output.set(r.band, bands[k][0] * width * 4));
135
+ }
136
+ input = output;
137
+ }
138
+ const data = new Uint8ClampedArray(n);
139
+ for (let j = 0; j < n; j++) { const v = input[j]; data[j] = v > 0 ? (v < 1 ? Math.round(v * 255) : 255) : 0; }
140
+ const out = { data, width, height };
141
+ if (canvas) {
142
+ canvas.width = width;
143
+ canvas.height = height;
144
+ canvas.getContext('2d').putImageData(new ImageData(data, width, height), 0, 0);
145
+ }
146
+ return out;
147
+ },
148
+
149
+ /** How many workers drawAsync() uses (0 until it first needs them). */
150
+ get workers() { return pool ? pool.size : 0; },
151
+
152
+ dispose() {
153
+ source = null;
154
+ for (const k of Object.keys(pictures)) delete pictures[k];
155
+ if (starting) starting.then((p) => p && p.close()); // also a pool still starting
156
+ starting = null;
157
+ pool = null;
158
+ sent = new Map();
159
+ },
160
+ };
161
+ return runner;
162
+ }
163
+
164
+ /** Clamp to 0–1 and round to 1/255: what an 8-bit texture keeps. */
165
+ function quantize(a) {
166
+ for (let j = 0; j < a.length; j++) {
167
+ const v = a[j];
168
+ a[j] = v > 0 ? (v < 1 ? Math.round(v * 255) / 255 : 1) : 0;
169
+ }
170
+ }
171
+
172
+ /** ImageData, { data, width, height } (8-bit or float), or a drawable in a browser → { data: Float32Array 0–1, width, height }. */
173
+ export function toPicture(image) {
174
+ if (image && image.data && image.width && image.height) {
175
+ const src = image.data;
176
+ const scale = src instanceof Float32Array || src instanceof Float64Array ? 1 : 1 / 255;
177
+ const data = new Float32Array(image.width * image.height * 4);
178
+ for (let j = 0; j < data.length; j++) data[j] = src[j] * scale;
179
+ return { data, width: image.width, height: image.height };
180
+ }
181
+ if (typeof document !== 'undefined' || typeof OffscreenCanvas !== 'undefined') {
182
+ const width = image.naturalWidth || image.videoWidth || image.width;
183
+ const height = image.naturalHeight || image.videoHeight || image.height;
184
+ if (!width || !height) throw new Error('setImage: the picture has no size yet (not loaded?)');
185
+ const canvas = typeof OffscreenCanvas !== 'undefined' ? new OffscreenCanvas(width, height) : Object.assign(document.createElement('canvas'), { width, height });
186
+ const ctx = canvas.getContext('2d', { willReadFrequently: true });
187
+ ctx.drawImage(image, 0, 0, width, height);
188
+ return toPicture(ctx.getImageData(0, 0, width, height));
189
+ }
190
+ throw new Error('setImage: give ImageData or { data, width, height }');
191
+ }
192
+
193
+ // ---- the worker pool -------------------------------------------------------------------
194
+
195
+ function defaultWorkers() {
196
+ const cores = typeof navigator !== 'undefined' && navigator.hardwareConcurrency ? navigator.hardwareConcurrency : 4;
197
+ return Math.max(0, Math.min(8, cores - 1));
198
+ }
199
+
200
+ /** Row bands [y0, y1) of about equal size, one per worker. */
201
+ export function splitRows(height, parts) {
202
+ const out = [];
203
+ for (let k = 0; k < parts; k++) {
204
+ const y0 = Math.floor((height * k) / parts);
205
+ const y1 = Math.floor((height * (k + 1)) / parts);
206
+ if (y1 > y0) out.push([y0, y1]);
207
+ }
208
+ return out;
209
+ }
210
+
211
+ const canShare = () => typeof SharedArrayBuffer !== 'undefined' && (typeof crossOriginIsolated === 'undefined' || crossOriginIsolated === true);
212
+
213
+ function sharedPicture(picture) {
214
+ if (picture.data.buffer instanceof (globalThis.SharedArrayBuffer ?? Object)) return picture;
215
+ if (picture.shared) return picture.shared; // copied once, not on every draw
216
+ const data = new Float32Array(new SharedArrayBuffer(picture.data.byteLength));
217
+ data.set(picture.data);
218
+ picture.shared = { data, width: picture.width, height: picture.height };
219
+ return picture.shared;
220
+ }
221
+
222
+ /** Workers that each compiled the filter; null when this platform can't start them. */
223
+ async function createPool(size, passes, workerUrl) {
224
+ const url = new URL(workerUrl ?? './cpu-worker.js', import.meta.url);
225
+ let make;
226
+ let revoke = () => {}; // the blob: address, once the workers no longer need it
227
+ if (typeof Worker !== 'undefined' && typeof window !== 'undefined') {
228
+ // a worker script must come from the page's own origin: from a CDN, a blob: module that imports it does
229
+ const start = url.origin === location.origin ? url
230
+ : URL.createObjectURL(new Blob([`import ${JSON.stringify(url.href)};`], { type: 'text/javascript' }));
231
+ if (start !== url) revoke = () => URL.revokeObjectURL(start);
232
+ make = () => {
233
+ const w = new Worker(start, { type: 'module' });
234
+ return { send: (m) => w.postMessage(m), on: (fn) => { w.onmessage = (e) => fn(e.data); w.onerror = (e) => fn({ id: -1, ok: false, error: e.message }); }, close: () => w.terminate() };
235
+ };
236
+ } else {
237
+ try {
238
+ const { Worker: NodeWorker } = await import('node:worker_threads');
239
+ make = () => {
240
+ const w = new NodeWorker(url);
241
+ return { send: (m) => w.postMessage(m), on: (fn) => { w.on('message', fn); w.on('error', (e) => fn({ id: -1, ok: false, error: e.message })); }, close: () => w.terminate() };
242
+ };
243
+ } catch {
244
+ return null;
245
+ }
246
+ }
247
+ const workers = Array.from({ length: size }, make);
248
+ let next = 1;
249
+ const waiting = workers.map(() => new Map());
250
+ workers.forEach((w, k) => w.on((m) => {
251
+ if (m.id === -1) { for (const [, cb] of waiting[k]) cb.reject(new Error(m.error)); waiting[k].clear(); return; }
252
+ const cb = waiting[k].get(m.id);
253
+ if (!cb) return;
254
+ waiting[k].delete(m.id);
255
+ if (m.ok) cb.resolve(m); else cb.reject(new Error(m.error));
256
+ }));
257
+ const ask = (k, message) => new Promise((resolve, reject) => {
258
+ const id = next++;
259
+ waiting[k].set(id, { resolve, reject });
260
+ workers[k].send({ ...message, id });
261
+ });
262
+ const pool = {
263
+ size, shared: canShare(), ask,
264
+ broadcast: (message) => Promise.all(workers.map((_, k) => ask(k, message))),
265
+ close: () => {
266
+ workers.forEach((w) => w.close());
267
+ for (const asked of waiting) { // nobody waits for an answer that can't come
268
+ for (const [, cb] of asked) cb.reject(new Error('The runner was disposed of while drawing'));
269
+ asked.clear();
270
+ }
271
+ },
272
+ };
273
+ try {
274
+ await pool.broadcast({ type: 'init', passes: passes.map(({ fragment, ...rest }) => rest) });
275
+ } catch (error) {
276
+ pool.close(); // workers that couldn't start the filter don't stay running
277
+ throw error;
278
+ } finally {
279
+ revoke(); // every worker has loaded its script (or failed to)
280
+ }
281
+ return pool;
282
+ }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * A worker for createCpuRunner's drawAsync(): it compiles the filter's
3
+ * passes itself (through the same whitelist gate), keeps the pictures it
4
+ * is sent, and runs one pass over a band of rows when asked.
5
+ *
6
+ * Works as a browser module worker and as a Node worker_thread.
7
+ */
8
+
9
+ import { compileCpuPass } from './cpu.js';
10
+
11
+ const port = typeof self !== 'undefined' && typeof self.postMessage === 'function' && typeof window === 'undefined'
12
+ ? { on: (fn) => { self.onmessage = (e) => fn(e.data); }, post: (m, t) => self.postMessage(m, t) }
13
+ : await (async () => {
14
+ const { parentPort } = await import('node:worker_threads');
15
+ return { on: (fn) => parentPort.on('message', fn), post: (m, t) => parentPort.postMessage(m, t) };
16
+ })();
17
+
18
+ let programs = [];
19
+ const pictures = {};
20
+ let source = null;
21
+ let scratch = null;
22
+
23
+ port.on((m) => {
24
+ try {
25
+ if (m.type === 'init') {
26
+ programs = m.passes.map((pass) => compileCpuPass(pass));
27
+ port.post({ id: m.id, ok: true });
28
+ } else if (m.type === 'picture') {
29
+ if (m.name === 'source') source = m.picture;
30
+ else pictures[m.name] = m.picture;
31
+ port.post({ id: m.id, ok: true });
32
+ } else if (m.type === 'run') {
33
+ const { width, height, y0, y1 } = m;
34
+ const n = width * height * 4;
35
+ // shared memory: write straight into the shared output; otherwise into our own, then send our rows back
36
+ const output = m.output ?? (scratch && scratch.length === n ? scratch : (scratch = new Float32Array(n)));
37
+ programs[m.pass].run({
38
+ input: m.input, output, source: source.data, width, height, t: m.t, params: m.params,
39
+ opacity: m.opacity, pictures, placements: m.placements, y0, y1,
40
+ });
41
+ const a = y0 * width * 4;
42
+ const b = y1 * width * 4;
43
+ if (m.quantize) {
44
+ for (let j = a; j < b; j++) {
45
+ const v = output[j];
46
+ output[j] = v > 0 ? (v < 1 ? Math.round(v * 255) / 255 : 1) : 0;
47
+ }
48
+ }
49
+ if (m.output) port.post({ id: m.id, ok: true });
50
+ else {
51
+ const band = output.slice(a, b);
52
+ port.post({ id: m.id, ok: true, band }, [band.buffer]);
53
+ }
54
+ }
55
+ } catch (e) {
56
+ port.post({ id: m.id, ok: false, error: e.message });
57
+ }
58
+ });