@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/CHANGELOG.md +58 -0
- package/LICENSE +21 -0
- package/README.md +152 -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 +601 -0
- package/package.json +118 -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 +418 -0
- package/src/cpu-runner.js +282 -0
- package/src/cpu-worker.js +58 -0
- package/src/cpu.js +586 -0
- package/src/element.d.ts +16 -0
- package/src/element.js +142 -0
- package/src/format.js +614 -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 +157 -0
- package/src/runtime.js +248 -0
- package/src/save.js +108 -0
- package/src/strokes.js +350 -0
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the generated CPU filter code calls: sampling the picture the way a
|
|
3
|
+
* WebGL texture does, and the neighbourhood functions as whole planes.
|
|
4
|
+
*
|
|
5
|
+
* Pictures are { data: Float32Array RGBA 0–1, width, height }, rows top
|
|
6
|
+
* down. Positions are pixel centres in that frame: (px, py) is pixel px of
|
|
7
|
+
* row py, so sampling there returns that pixel exactly and a fractional
|
|
8
|
+
* position blends the four around it (bilinear, edges clamped) — what
|
|
9
|
+
* texture2D does with LINEAR filtering and CLAMP_TO_EDGE.
|
|
10
|
+
*
|
|
11
|
+
* Every export that is a function is passed to the generated code as
|
|
12
|
+
* $h_<name>; nothing else is reachable from it.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { REACH } from 'mathzetta/glsl';
|
|
16
|
+
|
|
17
|
+
export const BLANK = { data: new Float32Array([1, 1, 1, 1]), width: 1, height: 1 }; // a picture not given yet: white
|
|
18
|
+
export const CLEAR = { data: new Float32Array([0, 0, 0, 0]), width: 1, height: 1 }; // a graphic not drawn yet: transparent
|
|
19
|
+
|
|
20
|
+
const PLACE = { cover: 0, fit: 1, stretch: 2, tile: 3 };
|
|
21
|
+
export function placeCode(place) { return PLACE[place ?? 'cover'] ?? 0; }
|
|
22
|
+
|
|
23
|
+
export function scratch() { return new Float64Array(4); }
|
|
24
|
+
/** P[i][k], for colour params (the generated code may not index twice). */
|
|
25
|
+
export function at(P, i, k) { return Number(P[i][k]); }
|
|
26
|
+
|
|
27
|
+
export function fmod(a, b) { return a - b * Math.floor(a / b); }
|
|
28
|
+
export function smoothstep(e0, e1, x) { const t = Math.min(Math.max((x - e0) / (e1 - e0), 0), 1); return t * t * (3 - 2 * t); }
|
|
29
|
+
export function factorial(n) { let r = 1; for (let i = 2; i <= 170; i++) { if (i > n) break; r *= i; } return r; }
|
|
30
|
+
export function perm(n, k) { let r = 1; for (let i = 0; i < 170; i++) { if (i >= k) break; r *= n - i; } return r; }
|
|
31
|
+
export function gcd(a, b) { a = Math.abs(a); b = Math.abs(b); for (let i = 0; i < 64; i++) { if (b === 0) break; const t = fmod(a, b); a = b; b = t; } return a; }
|
|
32
|
+
/** Halves go away from zero, as the shader's mz_round and every other MathZetta output round. */
|
|
33
|
+
export function round(x, digits) { const p = Math.pow(10, digits); return Math.sign(x) * Math.floor(Math.abs(x) * p + 0.5) / p; }
|
|
34
|
+
export function root(n, x) { return n === 2 ? Math.sqrt(x) : n === 3 ? Math.sign(x) * Math.pow(Math.abs(x), 1 / 3) : Math.pow(x, 1 / n); }
|
|
35
|
+
/** The shader's hash: a new value per pixel and per call, seeded with t. (A GPU's float sin differs, so the values do too.) */
|
|
36
|
+
export function random(px, py, h, seed) {
|
|
37
|
+
const v = Math.sin((px + 0.5) * 12.9898 + (h - py - 0.5) * 78.233 + seed * 37.719) * 43758.5453;
|
|
38
|
+
return v - Math.floor(v);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ---- sampling --------------------------------------------------------------------
|
|
42
|
+
|
|
43
|
+
const clampi = (v, max) => (v < 0 ? 0 : v > max ? max : v);
|
|
44
|
+
|
|
45
|
+
/** Bilinear sample of picture data at pixel-centre position (X, Y), edges clamped, into out[0..3]. */
|
|
46
|
+
export function sample(D, w, h, X, Y, out) {
|
|
47
|
+
const x0f = Math.floor(X);
|
|
48
|
+
const y0f = Math.floor(Y);
|
|
49
|
+
const fx = X - x0f;
|
|
50
|
+
const fy = Y - y0f;
|
|
51
|
+
const x0 = clampi(x0f, w - 1), x1 = clampi(x0f + 1, w - 1);
|
|
52
|
+
const y0 = clampi(y0f, h - 1), y1 = clampi(y0f + 1, h - 1);
|
|
53
|
+
const i00 = (y0 * w + x0) * 4, i10 = (y0 * w + x1) * 4, i01 = (y1 * w + x0) * 4, i11 = (y1 * w + x1) * 4;
|
|
54
|
+
for (let k = 0; k < 4; k++) {
|
|
55
|
+
const top = D[i00 + k] + (D[i10 + k] - D[i00 + k]) * fx;
|
|
56
|
+
const bottom = D[i01 + k] + (D[i11 + k] - D[i01 + k]) * fx;
|
|
57
|
+
out[k] = top + (bottom - top) * fy;
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** As sample(), but transparent black outside the picture (the shader's mz_at_image). */
|
|
63
|
+
export function sampleOrZero(D, w, h, X, Y, out) {
|
|
64
|
+
const qx = X + 0.5;
|
|
65
|
+
const qy = Y + 0.5;
|
|
66
|
+
if (qx < 0 || qy < 0 || qx > w || qy > h) { out[0] = out[1] = out[2] = out[3] = 0; return out; }
|
|
67
|
+
return sample(D, w, h, X, Y, out);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* A named picture laid over the frame (the shader's mz_sample_<name>): scaled by `place`
|
|
72
|
+
* (0 cover, 1 fit, 2 stretch, 3 tile) times zoom, centred at (x, y) of the frame, turned by
|
|
73
|
+
* angle; transparent outside unless tiled.
|
|
74
|
+
*/
|
|
75
|
+
export function picture(Q, qw, qh, w, h, zoom, x, y, angle, mode, X, Y, out) {
|
|
76
|
+
let sx;
|
|
77
|
+
let sy;
|
|
78
|
+
if (mode === 0) sx = sy = Math.max(w / qw, h / qh);
|
|
79
|
+
else if (mode === 1) sx = sy = Math.min(w / qw, h / qh);
|
|
80
|
+
else if (mode === 2) { sx = w / qw; sy = h / qh; } else sx = sy = 1;
|
|
81
|
+
sx *= zoom;
|
|
82
|
+
sy *= zoom;
|
|
83
|
+
const dx = X + 0.5 - x * w;
|
|
84
|
+
const dy = Y + 0.5 - y * h;
|
|
85
|
+
const co = Math.cos(angle);
|
|
86
|
+
const si = Math.sin(angle);
|
|
87
|
+
let qx = (co * dx + si * dy) / sx + qw * 0.5;
|
|
88
|
+
let qy = (co * dy - si * dx) / sy + qh * 0.5;
|
|
89
|
+
if (mode === 3) { qx = fmod(qx, qw); qy = fmod(qy, qh); }
|
|
90
|
+
else if (qx < 0 || qy < 0 || qx > qw || qy > qh) { out[0] = out[1] = out[2] = out[3] = 0; return out; }
|
|
91
|
+
return sample(Q, qw, qh, qx - 0.5, qy - 0.5, out);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ---- neighbourhoods: whole planes, computed once per pass ------------------------------
|
|
95
|
+
//
|
|
96
|
+
// Taps are whole-pixel offsets, edges clamped; dy runs downwards. Square windows are done as a
|
|
97
|
+
// horizontal pass then a vertical one — the same sums, maxima and weights as the shader's
|
|
98
|
+
// double loop, in another order.
|
|
99
|
+
|
|
100
|
+
/** Offsets -r..r kept by `keep(offset)`, with r = min(max(ceil(limit), 0), reach). */
|
|
101
|
+
function offsets(limit, reach, keep) {
|
|
102
|
+
const r = Math.min(Math.max(Math.ceil(limit), 0), reach);
|
|
103
|
+
const out = [];
|
|
104
|
+
for (let i = -r; i <= r; i++) if (keep(i)) out.push(i);
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* One separable pass over rgb along x or y: a weighted sum over whole-pixel offsets `taps`
|
|
110
|
+
* (edges clamped), divided by `norm`. Tight loops — no closures per tap — since this is where
|
|
111
|
+
* a blur spends its time.
|
|
112
|
+
*/
|
|
113
|
+
function weighted1D(src, stride, w, h, axis, taps, weights, norm, y0 = 0, y1 = h) {
|
|
114
|
+
const out = new Float32Array(w * h * 3);
|
|
115
|
+
const n = taps.length;
|
|
116
|
+
const T = Int32Array.from(taps);
|
|
117
|
+
const W = Float64Array.from(weights ?? taps.map(() => 1));
|
|
118
|
+
const along = axis === 'x' ? w : h;
|
|
119
|
+
const idx = new Int32Array(n);
|
|
120
|
+
for (let y = y0; y < y1; y++) {
|
|
121
|
+
for (let x = 0; x < w; x++) {
|
|
122
|
+
const pos = axis === 'x' ? x : y;
|
|
123
|
+
for (let t = 0; t < n; t++) {
|
|
124
|
+
let q = pos + T[t];
|
|
125
|
+
q = q < 0 ? 0 : q >= along ? along - 1 : q;
|
|
126
|
+
idx[t] = (axis === 'x' ? y * w + q : q * w + x) * stride;
|
|
127
|
+
}
|
|
128
|
+
let s0 = 0, s1 = 0, s2 = 0;
|
|
129
|
+
for (let t = 0; t < n; t++) { const i = idx[t], wt = W[t]; s0 += src[i] * wt; s1 += src[i + 1] * wt; s2 += src[i + 2] * wt; }
|
|
130
|
+
const o = (y * w + x) * 3;
|
|
131
|
+
out[o] = s0 / norm; out[o + 1] = s1 / norm; out[o + 2] = s2 / norm;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** A box along x or y with running sums: the same average whatever the radius costs the same. */
|
|
138
|
+
function box1D(src, stride, w, h, axis, lo, hi, y0 = 0, y1 = h) {
|
|
139
|
+
const out = new Float32Array(w * h * 3);
|
|
140
|
+
const along = axis === 'x' ? w : h;
|
|
141
|
+
const lines = axis === 'x' ? h : w;
|
|
142
|
+
const count = hi - lo + 1;
|
|
143
|
+
const at = (line, q) => (axis === 'x' ? line * w + q : q * w + line);
|
|
144
|
+
const clampq = (q) => (q < 0 ? 0 : q >= along ? along - 1 : q);
|
|
145
|
+
// along x: the rows y0..y1, all of each; along y: every column, only positions y0..y1
|
|
146
|
+
const [l0, l1] = axis === 'x' ? [y0, y1] : [0, lines];
|
|
147
|
+
const [p0, p1] = axis === 'x' ? [0, along] : [y0, y1];
|
|
148
|
+
for (let line = l0; line < l1; line++) {
|
|
149
|
+
let s0 = 0, s1 = 0, s2 = 0;
|
|
150
|
+
for (let t = p0 + lo; t <= p0 + hi; t++) { const i = at(line, clampq(t)) * stride; s0 += src[i]; s1 += src[i + 1]; s2 += src[i + 2]; }
|
|
151
|
+
for (let pos = p0; pos < p1; pos++) {
|
|
152
|
+
const o = at(line, pos) * 3;
|
|
153
|
+
out[o] = s0 / count; out[o + 1] = s1 / count; out[o + 2] = s2 / count;
|
|
154
|
+
const drop = at(line, clampq(pos + lo)) * stride;
|
|
155
|
+
const take = at(line, clampq(pos + hi + 1)) * stride;
|
|
156
|
+
s0 += src[take] - src[drop]; s1 += src[take + 1] - src[drop + 1]; s2 += src[take + 2] - src[drop + 2];
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Max or min along x or y, edges clamped, starting from `init` (the shader starts dilate at 0, erode at 1). */
|
|
163
|
+
function extreme1D(src, stride, w, h, axis, taps, init, isMax, y0 = 0, y1 = h) {
|
|
164
|
+
const out = new Float32Array(w * h * 3);
|
|
165
|
+
const along = axis === 'x' ? w : h;
|
|
166
|
+
for (let y = y0; y < y1; y++) {
|
|
167
|
+
for (let x = 0; x < w; x++) {
|
|
168
|
+
const pos = axis === 'x' ? x : y;
|
|
169
|
+
let m0 = init, m1 = init, m2 = init;
|
|
170
|
+
for (let t = 0; t < taps.length; t++) {
|
|
171
|
+
let q = pos + taps[t];
|
|
172
|
+
q = q < 0 ? 0 : q >= along ? along - 1 : q;
|
|
173
|
+
const i = (axis === 'x' ? y * w + q : q * w + x) * stride;
|
|
174
|
+
if (isMax) { if (src[i] > m0) m0 = src[i]; if (src[i + 1] > m1) m1 = src[i + 1]; if (src[i + 2] > m2) m2 = src[i + 2]; }
|
|
175
|
+
else { if (src[i] < m0) m0 = src[i]; if (src[i + 1] < m1) m1 = src[i + 1]; if (src[i + 2] < m2) m2 = src[i + 2]; }
|
|
176
|
+
}
|
|
177
|
+
const o = (y * w + x) * 3;
|
|
178
|
+
out[o] = m0; out[o + 1] = m1; out[o + 2] = m2;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const add = (acc, v, wt) => acc + v * wt;
|
|
185
|
+
const maxOf = (acc, v) => (v > acc ? v : acc);
|
|
186
|
+
const minOf = (acc, v) => (v < acc ? v : acc);
|
|
187
|
+
|
|
188
|
+
function rgbOf(I, w, h, y0 = 0, y1 = h) {
|
|
189
|
+
const out = new Float32Array(w * h * 3);
|
|
190
|
+
for (let j = y0 * w; j < y1 * w; j++) { out[j * 3] = I[j * 4]; out[j * 3 + 1] = I[j * 4 + 1]; out[j * 3 + 2] = I[j * 4 + 2]; }
|
|
191
|
+
return out;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Rows the first (horizontal) pass must cover so the vertical pass can read ±reach around y0..y1. */
|
|
195
|
+
const widen = (y0, y1, reach, h) => [Math.max(0, y0 - reach), Math.min(h, y1 + reach)];
|
|
196
|
+
|
|
197
|
+
function boxPlanes(I, w, h, y0, y1, radius, axes, reach) {
|
|
198
|
+
const taps = offsets(radius, reach, (i) => Math.abs(i) <= radius);
|
|
199
|
+
if (!taps.length) return rgbOf(I, w, h, y0, y1);
|
|
200
|
+
const lo = taps[0];
|
|
201
|
+
const hi = taps[taps.length - 1];
|
|
202
|
+
if (axes.length === 1) return box1D(I, 4, w, h, axes[0], lo, hi, y0, y1);
|
|
203
|
+
const [a0, a1] = widen(y0, y1, hi, h);
|
|
204
|
+
return box1D(box1D(I, 4, w, h, 'x', lo, hi, a0, a1), 3, w, h, 'y', lo, hi, y0, y1);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function gaussPlanes(I, w, h, y0, y1, sigma, axes, reach) {
|
|
208
|
+
if (!(sigma > 0)) return rgbOf(I, w, h, y0, y1);
|
|
209
|
+
const limit = 3 * sigma;
|
|
210
|
+
const taps = offsets(limit, reach, (i) => Math.abs(i) <= limit);
|
|
211
|
+
const weights = taps.map((i) => Math.exp(-(i * i) / (2 * sigma * sigma)));
|
|
212
|
+
const total = weights.reduce((a, b) => a + b, 0);
|
|
213
|
+
if (axes.length === 1) return weighted1D(I, 4, w, h, axes[0], taps, weights, total, y0, y1);
|
|
214
|
+
const [a0, a1] = widen(y0, y1, taps[taps.length - 1], h);
|
|
215
|
+
return weighted1D(weighted1D(I, 4, w, h, 'x', taps, weights, total, a0, a1), 3, w, h, 'y', taps, weights, total, y0, y1);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function extremePlanes(I, w, h, y0, y1, radius, init, isMax) {
|
|
219
|
+
const taps = offsets(radius, REACH.square, (i) => Math.abs(i) <= radius);
|
|
220
|
+
if (!taps.length) return new Float32Array(w * h * 3).fill(init);
|
|
221
|
+
const [a0, a1] = widen(y0, y1, taps[taps.length - 1], h);
|
|
222
|
+
return extreme1D(extreme1D(I, 4, w, h, 'x', taps, init, isMax, a0, a1), 3, w, h, 'y', taps, init, isMax, y0, y1);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export function plane_blur(I, w, h, y0, y1, radius) { return boxPlanes(I, w, h, y0, y1, radius, ['x', 'y'], REACH.square); }
|
|
226
|
+
export function plane_blurx(I, w, h, y0, y1, radius) { return boxPlanes(I, w, h, y0, y1, radius, ['x'], REACH.line); }
|
|
227
|
+
export function plane_blury(I, w, h, y0, y1, radius) { return boxPlanes(I, w, h, y0, y1, radius, ['y'], REACH.line); }
|
|
228
|
+
export function plane_gaussian(I, w, h, y0, y1, sigma) { return gaussPlanes(I, w, h, y0, y1, sigma, ['x', 'y'], REACH.square); }
|
|
229
|
+
export function plane_gaussianx(I, w, h, y0, y1, sigma) { return gaussPlanes(I, w, h, y0, y1, sigma, ['x'], REACH.line); }
|
|
230
|
+
export function plane_gaussiany(I, w, h, y0, y1, sigma) { return gaussPlanes(I, w, h, y0, y1, sigma, ['y'], REACH.line); }
|
|
231
|
+
export function plane_dilate(I, w, h, y0, y1, radius) { return extremePlanes(I, w, h, y0, y1, radius, 0, true); }
|
|
232
|
+
export function plane_erode(I, w, h, y0, y1, radius) { return extremePlanes(I, w, h, y0, y1, radius, 1, false); }
|
|
233
|
+
|
|
234
|
+
export function plane_sharpen(I, w, h, y0, y1, amount) {
|
|
235
|
+
const out = new Float32Array(w * h * 3);
|
|
236
|
+
for (let y = y0; y < y1; y++) for (let x = 0; x < w; x++) {
|
|
237
|
+
const t = (xx, yy) => (clampi(yy, h - 1) * w + clampi(xx, w - 1)) * 4;
|
|
238
|
+
const c = t(x, y), l = t(x - 1, y), r = t(x + 1, y), u = t(x, y - 1), d = t(x, y + 1);
|
|
239
|
+
for (let k = 0; k < 3; k++) out[(y * w + x) * 3 + k] = I[c + k] + amount * (4 * I[c + k] - I[l + k] - I[r + k] - I[u + k] - I[d + k]);
|
|
240
|
+
}
|
|
241
|
+
return out;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function lumaPlane(I, w, h) {
|
|
245
|
+
const L = new Float32Array(w * h);
|
|
246
|
+
for (let j = 0; j < w * h; j++) L[j] = I[j * 4] * 0.299 + I[j * 4 + 1] * 0.587 + I[j * 4 + 2] * 0.114;
|
|
247
|
+
return L;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function lumaOp(I, w, h, y0, y1, fn) {
|
|
251
|
+
const L = lumaPlane(I, w, h);
|
|
252
|
+
const out = new Float32Array(w * h);
|
|
253
|
+
for (let y = y0; y < y1; y++) for (let x = 0; x < w; x++) {
|
|
254
|
+
const at = (dx, dy) => L[clampi(y + dy, h - 1) * w + clampi(x + dx, w - 1)];
|
|
255
|
+
out[y * w + x] = fn(at);
|
|
256
|
+
}
|
|
257
|
+
return out;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const SOBELX = (L) => (L(1, -1) + 2 * L(1, 0) + L(1, 1)) - (L(-1, -1) + 2 * L(-1, 0) + L(-1, 1));
|
|
261
|
+
const SOBELY = (L) => (L(-1, 1) + 2 * L(0, 1) + L(1, 1)) - (L(-1, -1) + 2 * L(0, -1) + L(1, -1));
|
|
262
|
+
const LAPLACIAN = (L) => 4 * L(0, 0) - L(-1, 0) - L(1, 0) - L(0, -1) - L(0, 1);
|
|
263
|
+
const EMBOSS = (L) => 0.5 + L(-1, -1) - L(1, 1);
|
|
264
|
+
const SOBEL = (L) => Math.hypot(SOBELX(L), SOBELY(L));
|
|
265
|
+
|
|
266
|
+
export function plane_sobelx(I, w, h, y0, y1) { return lumaOp(I, w, h, y0, y1, SOBELX); }
|
|
267
|
+
export function plane_sobely(I, w, h, y0, y1) { return lumaOp(I, w, h, y0, y1, SOBELY); }
|
|
268
|
+
export function plane_sobel(I, w, h, y0, y1) { return lumaOp(I, w, h, y0, y1, SOBEL); }
|
|
269
|
+
export function plane_laplacian(I, w, h, y0, y1) { return lumaOp(I, w, h, y0, y1, LAPLACIAN); }
|
|
270
|
+
export function plane_emboss(I, w, h, y0, y1) { return lumaOp(I, w, h, y0, y1, EMBOSS); }
|
|
271
|
+
|
|
272
|
+
// ---- the same at one pixel, for an argument that changes from pixel to pixel ---------------
|
|
273
|
+
|
|
274
|
+
function window2D(I, w, h, px, py, taps, weightOf, init, combine, out, finish) {
|
|
275
|
+
for (let k = 0; k < 3; k++) {
|
|
276
|
+
let acc = init;
|
|
277
|
+
let total = 0;
|
|
278
|
+
for (const i of taps) for (const j of taps) {
|
|
279
|
+
const wt = weightOf ? weightOf(i, j) : 1;
|
|
280
|
+
acc = combine(acc, I[(clampi(py + j, h - 1) * w + clampi(px + i, w - 1)) * 4 + k], wt);
|
|
281
|
+
total += wt;
|
|
282
|
+
}
|
|
283
|
+
out[k] = finish ? finish(acc, total) : acc;
|
|
284
|
+
}
|
|
285
|
+
return out;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function window1D(I, w, h, px, py, axis, taps, weightOf, out) {
|
|
289
|
+
for (let k = 0; k < 3; k++) {
|
|
290
|
+
let acc = 0;
|
|
291
|
+
let total = 0;
|
|
292
|
+
for (const i of taps) {
|
|
293
|
+
const wt = weightOf ? weightOf(i) : 1;
|
|
294
|
+
const xx = axis === 'x' ? clampi(px + i, w - 1) : px;
|
|
295
|
+
const yy = axis === 'y' ? clampi(py + i, h - 1) : py;
|
|
296
|
+
acc += I[(yy * w + xx) * 4 + k] * wt;
|
|
297
|
+
total += wt;
|
|
298
|
+
}
|
|
299
|
+
out[k] = acc / total;
|
|
300
|
+
}
|
|
301
|
+
return out;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const here = (I, w, px, py, out) => { const c = (py * w + px) * 4; out[0] = I[c]; out[1] = I[c + 1]; out[2] = I[c + 2]; return out; };
|
|
305
|
+
|
|
306
|
+
export function at_blur(I, w, h, px, py, radius, out) {
|
|
307
|
+
return window2D(I, w, h, px, py, offsets(radius, REACH.square, (i) => Math.abs(i) <= radius), null, 0, add, out, (a, n) => a / n);
|
|
308
|
+
}
|
|
309
|
+
export function at_blurx(I, w, h, px, py, radius, out) { return window1D(I, w, h, px, py, 'x', offsets(radius, REACH.line, (i) => Math.abs(i) <= radius), null, out); }
|
|
310
|
+
export function at_blury(I, w, h, px, py, radius, out) { return window1D(I, w, h, px, py, 'y', offsets(radius, REACH.line, (i) => Math.abs(i) <= radius), null, out); }
|
|
311
|
+
export function at_gaussian(I, w, h, px, py, sigma, out) {
|
|
312
|
+
if (!(sigma > 0)) return here(I, w, px, py, out);
|
|
313
|
+
const limit = 3 * sigma;
|
|
314
|
+
const g = (i, j) => Math.exp(-(i * i + j * j) / (2 * sigma * sigma));
|
|
315
|
+
return window2D(I, w, h, px, py, offsets(limit, REACH.square, (i) => Math.abs(i) <= limit), g, 0, add, out, (a, n) => a / n);
|
|
316
|
+
}
|
|
317
|
+
export function at_gaussianx(I, w, h, px, py, sigma, out) {
|
|
318
|
+
if (!(sigma > 0)) return here(I, w, px, py, out);
|
|
319
|
+
const limit = 3 * sigma;
|
|
320
|
+
return window1D(I, w, h, px, py, 'x', offsets(limit, REACH.line, (i) => Math.abs(i) <= limit), (i) => Math.exp(-(i * i) / (2 * sigma * sigma)), out);
|
|
321
|
+
}
|
|
322
|
+
export function at_gaussiany(I, w, h, px, py, sigma, out) {
|
|
323
|
+
if (!(sigma > 0)) return here(I, w, px, py, out);
|
|
324
|
+
const limit = 3 * sigma;
|
|
325
|
+
return window1D(I, w, h, px, py, 'y', offsets(limit, REACH.line, (i) => Math.abs(i) <= limit), (i) => Math.exp(-(i * i) / (2 * sigma * sigma)), out);
|
|
326
|
+
}
|
|
327
|
+
export function at_dilate(I, w, h, px, py, radius, out) { return window2D(I, w, h, px, py, offsets(radius, REACH.square, (i) => Math.abs(i) <= radius), null, 0, maxOf, out); }
|
|
328
|
+
export function at_erode(I, w, h, px, py, radius, out) { return window2D(I, w, h, px, py, offsets(radius, REACH.square, (i) => Math.abs(i) <= radius), null, 1, minOf, out); }
|
|
329
|
+
export function at_sharpen(I, w, h, px, py, amount, out) {
|
|
330
|
+
const t = (xx, yy) => (clampi(yy, h - 1) * w + clampi(xx, w - 1)) * 4;
|
|
331
|
+
const c = t(px, py), l = t(px - 1, py), r = t(px + 1, py), u = t(px, py - 1), d = t(px, py + 1);
|
|
332
|
+
for (let k = 0; k < 3; k++) out[k] = I[c + k] + amount * (4 * I[c + k] - I[l + k] - I[r + k] - I[u + k] - I[d + k]);
|
|
333
|
+
return out;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** 3×3 kernel, row by row from the top-left (k00 at dx = -1, dy = -1). */
|
|
337
|
+
export function convolve(I, w, h, px, py, k00, k01, k02, k10, k11, k12, k20, k21, k22, out) {
|
|
338
|
+
const t = (dx, dy) => (clampi(py + dy, h - 1) * w + clampi(px + dx, w - 1)) * 4;
|
|
339
|
+
const taps = [[k00, -1, -1], [k01, 0, -1], [k02, 1, -1], [k10, -1, 0], [k11, 0, 0], [k12, 1, 0], [k20, -1, 1], [k21, 0, 1], [k22, 1, 1]];
|
|
340
|
+
for (let k = 0; k < 3; k++) { let s = 0; for (const [wt, dx, dy] of taps) s += wt * I[t(dx, dy) + k]; out[k] = s; }
|
|
341
|
+
return out;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// ---- colour spaces: the shader's to<space> / from<space> (mathzetta render/glsl.js colourGlsl), sRGB on the other side ----
|
|
345
|
+
const toLin = (c) => (c < 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4));
|
|
346
|
+
const fromLin = (c) => { c = Math.max(c, 0); return c < 0.0031308 ? c * 12.92 : 1.055 * Math.pow(c, 1 / 2.4) - 0.055; };
|
|
347
|
+
const hue = (r, g, b, v, d) => { const h = v === r ? (g - b) / d : v === g ? 2 + (b - r) / d : 4 + (r - g) / d; return h / 6 - Math.floor(h / 6); };
|
|
348
|
+
const tri = (h, o) => { const k = Math.abs(((h + o) - Math.floor(h + o)) * 6 - 3); return Math.min(Math.max(k - 1, 0), 1); };
|
|
349
|
+
export function col_tolinear(r, g, b, out) { out[0] = toLin(r); out[1] = toLin(g); out[2] = toLin(b); return out; }
|
|
350
|
+
export function col_fromlinear(r, g, b, out) { out[0] = fromLin(r); out[1] = fromLin(g); out[2] = fromLin(b); return out; }
|
|
351
|
+
export function col_tohsv(r, g, b, out) {
|
|
352
|
+
const v = Math.max(r, g, b), d = v - Math.min(r, g, b);
|
|
353
|
+
out[0] = d > 0 ? hue(r, g, b, v, d) : 0; out[1] = v > 0 ? d / v : 0; out[2] = v;
|
|
354
|
+
return out;
|
|
355
|
+
}
|
|
356
|
+
export function col_fromhsv(h, s, v, out) {
|
|
357
|
+
out[0] = v * (1 + (tri(h, 0) - 1) * s); out[1] = v * (1 + (tri(h, 2 / 3) - 1) * s); out[2] = v * (1 + (tri(h, 1 / 3) - 1) * s);
|
|
358
|
+
return out;
|
|
359
|
+
}
|
|
360
|
+
export function col_tohsl(r, g, b, out) {
|
|
361
|
+
const v = Math.max(r, g, b), m = Math.min(r, g, b), d = v - m, l = (v + m) * 0.5;
|
|
362
|
+
out[0] = d > 0 ? hue(r, g, b, v, d) : 0; out[1] = d > 0 ? d / (1 - Math.abs(2 * l - 1)) : 0; out[2] = l;
|
|
363
|
+
return out;
|
|
364
|
+
}
|
|
365
|
+
export function col_fromhsl(h, s, l, out) {
|
|
366
|
+
const C = (1 - Math.abs(2 * l - 1)) * s;
|
|
367
|
+
out[0] = l - C * 0.5 + C * tri(h, 0); out[1] = l - C * 0.5 + C * tri(h, 2 / 3); out[2] = l - C * 0.5 + C * tri(h, 1 / 3);
|
|
368
|
+
return out;
|
|
369
|
+
}
|
|
370
|
+
export function col_toycbcr(r, g, b, out) {
|
|
371
|
+
const y = r * 0.299 + g * 0.587 + b * 0.114;
|
|
372
|
+
out[0] = y; out[1] = 0.5 + (b - y) * 0.5643; out[2] = 0.5 + (r - y) * 0.7132;
|
|
373
|
+
return out;
|
|
374
|
+
}
|
|
375
|
+
export function col_fromycbcr(y, cb, cr, out) {
|
|
376
|
+
cb -= 0.5; cr -= 0.5;
|
|
377
|
+
out[0] = y + 1.402 * cr; out[1] = y - 0.344136 * cb - 0.714136 * cr; out[2] = y + 1.772 * cb;
|
|
378
|
+
return out;
|
|
379
|
+
}
|
|
380
|
+
const labF = (t) => (t < 0.008856 ? 7.787 * t + 16 / 116 : Math.cbrt(t));
|
|
381
|
+
const labFinv = (f) => (f < 0.206893 ? (f - 16 / 116) / 7.787 : f * f * f);
|
|
382
|
+
export function col_tolab(r, g, b, out) {
|
|
383
|
+
const R = toLin(r), G = toLin(g), B = toLin(b);
|
|
384
|
+
const fx = labF((0.4124564 * R + 0.3575761 * G + 0.1804375 * B) / 0.95047);
|
|
385
|
+
const fy = labF(0.2126729 * R + 0.7151522 * G + 0.0721750 * B);
|
|
386
|
+
const fz = labF((0.0193339 * R + 0.1191920 * G + 0.9503041 * B) / 1.08883);
|
|
387
|
+
out[0] = (116 * fy - 16) / 100; out[1] = 0.5 + 500 * (fx - fy) / 256; out[2] = 0.5 + 200 * (fy - fz) / 256;
|
|
388
|
+
return out;
|
|
389
|
+
}
|
|
390
|
+
export function col_fromlab(L, A, B, out) {
|
|
391
|
+
const fy = (L * 100 + 16) / 116, fx = fy + (A - 0.5) * 256 / 500, fz = fy - (B - 0.5) * 256 / 200;
|
|
392
|
+
const X = labFinv(fx) * 0.95047, Y = labFinv(fy), Z = labFinv(fz) * 1.08883;
|
|
393
|
+
const lr = Math.min(Math.max(3.2404542 * X - 1.5371385 * Y - 0.4985314 * Z, 0), 1);
|
|
394
|
+
const lg = Math.min(Math.max(-0.9692660 * X + 1.8760108 * Y + 0.0415560 * Z, 0), 1);
|
|
395
|
+
const lb = Math.min(Math.max(0.0556434 * X - 0.2040259 * Y + 1.0572252 * Z, 0), 1);
|
|
396
|
+
out[0] = fromLin(lr); out[1] = fromLin(lg); out[2] = fromLin(lb);
|
|
397
|
+
return out;
|
|
398
|
+
}
|
|
399
|
+
export function col_tooklab(r, g, b, out) {
|
|
400
|
+
const R = toLin(r), G = toLin(g), B = toLin(b);
|
|
401
|
+
const l = Math.cbrt(0.4122214708 * R + 0.5363325363 * G + 0.0514459929 * B);
|
|
402
|
+
const m = Math.cbrt(0.2119034982 * R + 0.6806995451 * G + 0.1073969566 * B);
|
|
403
|
+
const s = Math.cbrt(0.0883024619 * R + 0.2817188376 * G + 0.6299787005 * B);
|
|
404
|
+
out[0] = 0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * s;
|
|
405
|
+
out[1] = 0.5 + 1.9779984951 * l - 2.4285922050 * m + 0.4505937099 * s;
|
|
406
|
+
out[2] = 0.5 + 0.0259040371 * l + 0.7827717662 * m - 0.8086757660 * s;
|
|
407
|
+
return out;
|
|
408
|
+
}
|
|
409
|
+
export function col_fromoklab(L, A, B, out) {
|
|
410
|
+
A -= 0.5; B -= 0.5;
|
|
411
|
+
let l = L + 0.3963377774 * A + 0.2158037573 * B, m = L - 0.1055613458 * A - 0.0638541728 * B, s = L - 0.0894841775 * A - 1.2914855480 * B;
|
|
412
|
+
l = l * l * l; m = m * m * m; s = s * s * s;
|
|
413
|
+
const lr = Math.min(Math.max(4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s, 0), 1);
|
|
414
|
+
const lg = Math.min(Math.max(-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s, 0), 1);
|
|
415
|
+
const lb = Math.min(Math.max(-0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s, 0), 1);
|
|
416
|
+
out[0] = fromLin(lr); out[1] = fromLin(lg); out[2] = fromLin(lb);
|
|
417
|
+
return out;
|
|
418
|
+
}
|