@mathzetta/qblur 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/LICENSE +21 -0
- package/README.md +143 -2
- package/dist/cpu-worker.js +2 -0
- package/dist/qblur-apply.js +2 -0
- package/dist/qblur-element.js +2 -0
- package/dist/qblur.core.js +2 -0
- package/dist/qblur.external.global.min.js +2 -0
- package/dist/qblur.external.js +2 -0
- package/dist/qblur.global.min.js +2 -0
- package/dist/qblur.js +2 -0
- package/docs/filters.md +841 -0
- package/docs/library.md +604 -0
- package/package.json +112 -4
- package/src/apply-filter.js +245 -0
- package/src/apply.d.ts +64 -0
- package/src/apply.js +14 -0
- package/src/auto.js +41 -0
- package/src/cpu-helpers.js +416 -0
- package/src/cpu-runner.js +268 -0
- package/src/cpu-worker.js +58 -0
- package/src/cpu.js +581 -0
- package/src/element.d.ts +16 -0
- package/src/element.js +142 -0
- package/src/format.js +605 -0
- package/src/graphics.js +305 -0
- package/src/index.d.ts +283 -0
- package/src/index.js +13 -0
- package/src/library.js +173 -0
- package/src/png.js +154 -0
- package/src/runtime.js +229 -0
- package/src/save.js +108 -0
- package/src/strokes.js +342 -0
|
@@ -0,0 +1,268 @@
|
|
|
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 sent = new Map(); // picture name → the object last sent to the pool
|
|
40
|
+
|
|
41
|
+
const runner = {
|
|
42
|
+
passes: compiled.passes,
|
|
43
|
+
get precision() { return precision; },
|
|
44
|
+
/** The generated JavaScript of each pass, for the curious (and the tests). */
|
|
45
|
+
sources: programs.map((p) => p.source),
|
|
46
|
+
|
|
47
|
+
setImage(name, image) {
|
|
48
|
+
if (typeof name !== 'string') { image = name; name = null; }
|
|
49
|
+
const picture = toPicture(image);
|
|
50
|
+
if (!name || name === 'source') source = picture;
|
|
51
|
+
else pictures[name] = picture;
|
|
52
|
+
return runner;
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
/** Runs every pass; returns the result as 8-bit RGBA (and draws it on `canvas` when given). */
|
|
56
|
+
draw({ t = 0, canvas = null } = {}) {
|
|
57
|
+
if (!source) throw new Error('setImage() first');
|
|
58
|
+
const { width, height } = source;
|
|
59
|
+
const n = width * height * 4;
|
|
60
|
+
let input = source.data;
|
|
61
|
+
const kept = [];
|
|
62
|
+
compiled.passes.forEach((pass, i) => {
|
|
63
|
+
for (let k = 0; k < pass.keepInput; k++) kept.push(input);
|
|
64
|
+
const output = new Float32Array(n);
|
|
65
|
+
if (pass.blendKept) {
|
|
66
|
+
const base = kept.pop();
|
|
67
|
+
const o = pass.opacity;
|
|
68
|
+
for (let j = 0; j < n; j++) output[j] = base[j] + (input[j] - base[j]) * o;
|
|
69
|
+
} else {
|
|
70
|
+
programs[i].run({
|
|
71
|
+
input, output, source: source.data, width, height, t,
|
|
72
|
+
params: Object.keys(pass.params ?? {}).map((name) => pass.values[name]),
|
|
73
|
+
opacity: pass.opacity, pictures, placements: pass.placements ?? {},
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
if (precision === 'byte') quantize(output);
|
|
77
|
+
input = output;
|
|
78
|
+
});
|
|
79
|
+
const data = new Uint8ClampedArray(n);
|
|
80
|
+
for (let j = 0; j < n; j++) {
|
|
81
|
+
const v = input[j];
|
|
82
|
+
data[j] = v > 0 ? (v < 1 ? Math.round(v * 255) : 255) : 0; // NaN → 0, as a GPU writes it
|
|
83
|
+
}
|
|
84
|
+
const out = { data, width, height };
|
|
85
|
+
if (canvas) {
|
|
86
|
+
canvas.width = width;
|
|
87
|
+
canvas.height = height;
|
|
88
|
+
canvas.getContext('2d').putImageData(new ImageData(data, width, height), 0, 0);
|
|
89
|
+
}
|
|
90
|
+
return out;
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* As draw(), with each pass's rows spread over workers when the picture is large;
|
|
95
|
+
* smaller pictures (or no workers available) run here, as draw().
|
|
96
|
+
*/
|
|
97
|
+
async drawAsync({ t = 0, canvas = null } = {}) {
|
|
98
|
+
if (!source) throw new Error('setImage() first');
|
|
99
|
+
const { width, height } = source;
|
|
100
|
+
if (wanted < 2 || width * height < parallelFrom) return runner.draw({ t, canvas });
|
|
101
|
+
if (pool === null) pool = await createPool(wanted, compiled.passes, options.workerUrl).catch(() => false); // false: workers can't start here
|
|
102
|
+
if (!pool) return runner.draw({ t, canvas });
|
|
103
|
+
const n = width * height * 4;
|
|
104
|
+
const alloc = () => (pool.shared ? new Float32Array(new SharedArrayBuffer(n * 4)) : new Float32Array(n));
|
|
105
|
+
// pictures the workers haven't seen (as shared copies when we can share)
|
|
106
|
+
for (const [name, picture] of [['source', source], ...Object.entries(pictures)]) {
|
|
107
|
+
if (sent.get(name) === picture) continue;
|
|
108
|
+
await pool.broadcast({ type: 'picture', name, picture: pool.shared ? sharedPicture(picture) : picture });
|
|
109
|
+
sent.set(name, picture);
|
|
110
|
+
}
|
|
111
|
+
let input = pool.shared ? sharedPicture(source).data : source.data;
|
|
112
|
+
const kept = [];
|
|
113
|
+
for (let i = 0; i < compiled.passes.length; i++) {
|
|
114
|
+
const pass = compiled.passes[i];
|
|
115
|
+
for (let k = 0; k < pass.keepInput; k++) kept.push(input);
|
|
116
|
+
const output = alloc();
|
|
117
|
+
if (pass.blendKept) {
|
|
118
|
+
const base = kept.pop();
|
|
119
|
+
for (let j = 0; j < n; j++) output[j] = base[j] + (input[j] - base[j]) * pass.opacity;
|
|
120
|
+
if (precision === 'byte') quantize(output);
|
|
121
|
+
} else {
|
|
122
|
+
const params = Object.keys(pass.params ?? {}).map((name) => pass.values[name]);
|
|
123
|
+
const bands = splitRows(height, pool.size);
|
|
124
|
+
const replies = await Promise.all(bands.map(([y0, y1], k) => pool.ask(k, {
|
|
125
|
+
type: 'run', pass: i, input, output: pool.shared ? output : null, width, height, t, params,
|
|
126
|
+
opacity: pass.opacity, placements: pass.placements ?? {}, y0, y1, quantize: precision === 'byte',
|
|
127
|
+
})));
|
|
128
|
+
if (!pool.shared) replies.forEach((r, k) => output.set(r.band, bands[k][0] * width * 4));
|
|
129
|
+
}
|
|
130
|
+
input = output;
|
|
131
|
+
}
|
|
132
|
+
const data = new Uint8ClampedArray(n);
|
|
133
|
+
for (let j = 0; j < n; j++) { const v = input[j]; data[j] = v > 0 ? (v < 1 ? Math.round(v * 255) : 255) : 0; }
|
|
134
|
+
const out = { data, width, height };
|
|
135
|
+
if (canvas) {
|
|
136
|
+
canvas.width = width;
|
|
137
|
+
canvas.height = height;
|
|
138
|
+
canvas.getContext('2d').putImageData(new ImageData(data, width, height), 0, 0);
|
|
139
|
+
}
|
|
140
|
+
return out;
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
/** How many workers drawAsync() uses (0 until it first needs them). */
|
|
144
|
+
get workers() { return pool ? pool.size : 0; },
|
|
145
|
+
|
|
146
|
+
dispose() {
|
|
147
|
+
source = null;
|
|
148
|
+
for (const k of Object.keys(pictures)) delete pictures[k];
|
|
149
|
+
if (pool) pool.close();
|
|
150
|
+
pool = null;
|
|
151
|
+
sent = new Map();
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
return runner;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Clamp to 0–1 and round to 1/255: what an 8-bit texture keeps. */
|
|
158
|
+
function quantize(a) {
|
|
159
|
+
for (let j = 0; j < a.length; j++) {
|
|
160
|
+
const v = a[j];
|
|
161
|
+
a[j] = v > 0 ? (v < 1 ? Math.round(v * 255) / 255 : 1) : 0;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** ImageData, { data, width, height } (8-bit or float), or a drawable in a browser → { data: Float32Array 0–1, width, height }. */
|
|
166
|
+
export function toPicture(image) {
|
|
167
|
+
if (image && image.data && image.width && image.height) {
|
|
168
|
+
const src = image.data;
|
|
169
|
+
const scale = src instanceof Float32Array || src instanceof Float64Array ? 1 : 1 / 255;
|
|
170
|
+
const data = new Float32Array(image.width * image.height * 4);
|
|
171
|
+
for (let j = 0; j < data.length; j++) data[j] = src[j] * scale;
|
|
172
|
+
return { data, width: image.width, height: image.height };
|
|
173
|
+
}
|
|
174
|
+
if (typeof document !== 'undefined' || typeof OffscreenCanvas !== 'undefined') {
|
|
175
|
+
const width = image.naturalWidth || image.videoWidth || image.width;
|
|
176
|
+
const height = image.naturalHeight || image.videoHeight || image.height;
|
|
177
|
+
if (!width || !height) throw new Error('setImage: the picture has no size yet (not loaded?)');
|
|
178
|
+
const canvas = typeof OffscreenCanvas !== 'undefined' ? new OffscreenCanvas(width, height) : Object.assign(document.createElement('canvas'), { width, height });
|
|
179
|
+
const ctx = canvas.getContext('2d', { willReadFrequently: true });
|
|
180
|
+
ctx.drawImage(image, 0, 0, width, height);
|
|
181
|
+
return toPicture(ctx.getImageData(0, 0, width, height));
|
|
182
|
+
}
|
|
183
|
+
throw new Error('setImage: give ImageData or { data, width, height }');
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// ---- the worker pool -------------------------------------------------------------------
|
|
187
|
+
|
|
188
|
+
function defaultWorkers() {
|
|
189
|
+
const cores = typeof navigator !== 'undefined' && navigator.hardwareConcurrency ? navigator.hardwareConcurrency : 4;
|
|
190
|
+
return Math.max(0, Math.min(8, cores - 1));
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Row bands [y0, y1) of about equal size, one per worker. */
|
|
194
|
+
export function splitRows(height, parts) {
|
|
195
|
+
const out = [];
|
|
196
|
+
for (let k = 0; k < parts; k++) {
|
|
197
|
+
const y0 = Math.floor((height * k) / parts);
|
|
198
|
+
const y1 = Math.floor((height * (k + 1)) / parts);
|
|
199
|
+
if (y1 > y0) out.push([y0, y1]);
|
|
200
|
+
}
|
|
201
|
+
return out;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const canShare = () => typeof SharedArrayBuffer !== 'undefined' && (typeof crossOriginIsolated === 'undefined' || crossOriginIsolated === true);
|
|
205
|
+
|
|
206
|
+
function sharedPicture(picture) {
|
|
207
|
+
if (picture.data.buffer instanceof (globalThis.SharedArrayBuffer ?? Object)) return picture;
|
|
208
|
+
const data = new Float32Array(new SharedArrayBuffer(picture.data.byteLength));
|
|
209
|
+
data.set(picture.data);
|
|
210
|
+
picture.shared ??= { data, width: picture.width, height: picture.height };
|
|
211
|
+
return picture.shared;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Workers that each compiled the filter; null when this platform can't start them. */
|
|
215
|
+
async function createPool(size, passes, workerUrl) {
|
|
216
|
+
const url = new URL(workerUrl ?? './cpu-worker.js', import.meta.url);
|
|
217
|
+
let make;
|
|
218
|
+
let revoke = () => {}; // the blob: address, once the workers no longer need it
|
|
219
|
+
if (typeof Worker !== 'undefined' && typeof window !== 'undefined') {
|
|
220
|
+
// a worker script must come from the page's own origin: from a CDN, a blob: module that imports it does
|
|
221
|
+
const start = url.origin === location.origin ? url
|
|
222
|
+
: URL.createObjectURL(new Blob([`import ${JSON.stringify(url.href)};`], { type: 'text/javascript' }));
|
|
223
|
+
if (start !== url) revoke = () => URL.revokeObjectURL(start);
|
|
224
|
+
make = () => {
|
|
225
|
+
const w = new Worker(start, { type: 'module' });
|
|
226
|
+
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() };
|
|
227
|
+
};
|
|
228
|
+
} else {
|
|
229
|
+
try {
|
|
230
|
+
const { Worker: NodeWorker } = await import('node:worker_threads');
|
|
231
|
+
make = () => {
|
|
232
|
+
const w = new NodeWorker(url);
|
|
233
|
+
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() };
|
|
234
|
+
};
|
|
235
|
+
} catch {
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
const workers = Array.from({ length: size }, make);
|
|
240
|
+
let next = 1;
|
|
241
|
+
const waiting = workers.map(() => new Map());
|
|
242
|
+
workers.forEach((w, k) => w.on((m) => {
|
|
243
|
+
if (m.id === -1) { for (const [, cb] of waiting[k]) cb.reject(new Error(m.error)); waiting[k].clear(); return; }
|
|
244
|
+
const cb = waiting[k].get(m.id);
|
|
245
|
+
if (!cb) return;
|
|
246
|
+
waiting[k].delete(m.id);
|
|
247
|
+
if (m.ok) cb.resolve(m); else cb.reject(new Error(m.error));
|
|
248
|
+
}));
|
|
249
|
+
const ask = (k, message) => new Promise((resolve, reject) => {
|
|
250
|
+
const id = next++;
|
|
251
|
+
waiting[k].set(id, { resolve, reject });
|
|
252
|
+
workers[k].send({ ...message, id });
|
|
253
|
+
});
|
|
254
|
+
const pool = {
|
|
255
|
+
size, shared: canShare(), ask,
|
|
256
|
+
broadcast: (message) => Promise.all(workers.map((_, k) => ask(k, message))),
|
|
257
|
+
close: () => workers.forEach((w) => w.close()),
|
|
258
|
+
};
|
|
259
|
+
try {
|
|
260
|
+
await pool.broadcast({ type: 'init', passes: passes.map(({ fragment, ...rest }) => rest) });
|
|
261
|
+
} catch (error) {
|
|
262
|
+
pool.close(); // workers that couldn't start the filter don't stay running
|
|
263
|
+
throw error;
|
|
264
|
+
} finally {
|
|
265
|
+
revoke(); // every worker has loaded its script (or failed to)
|
|
266
|
+
}
|
|
267
|
+
return pool;
|
|
268
|
+
}
|
|
@@ -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
|
+
});
|