@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/library.js ADDED
@@ -0,0 +1,173 @@
1
+ /**
2
+ * The built-in filters: what the editor offers, and a catalogue anyone
3
+ * embedding the runner can show. Each is a filter document (docs/filters.md)
4
+ * plus a `group` for display (not part of the format; sharing drops it);
5
+ * `npm test` checks that every one validates, compiles and survives a link.
6
+ */
7
+
8
+ const FILTERS = [
9
+ { name: 'Wave', group: 'Shape & motion', description: 'Rows slide back and forth as t advances',
10
+ params: { amplitude: { default: 8, min: 0, max: 40 }, wavelength: { default: 10, min: 1, max: 60 }, speed: { default: 5, min: 1, max: 30 } },
11
+ color: 'pixel(px + amplitude sin(py / wavelength + t / speed), py)' },
12
+ { name: 'Invert', group: 'Colour & tone', color: '(1 - r, 1 - g, 1 - b)' },
13
+ { name: 'Grayscale', group: 'Colour & tone', color: 'gray' },
14
+ { name: 'Threshold', group: 'Colour & tone', params: { cutoff: 0.5 }, color: 'if gray > cutoff then 1 else 0' },
15
+ { name: 'Posterize', group: 'Colour & tone', params: { levels: { default: 4, min: 2, max: 16, step: 1 } }, color: 'min(floor(rgb * levels) / (levels - 1), 1)' },
16
+ { name: 'Brightness & contrast', group: 'Colour & tone',
17
+ params: { brightness: { default: 0, min: -0.5, max: 0.5 }, contrast: { default: 1, min: 0, max: 3 } },
18
+ color: '(rgb - 0.5) contrast + 0.5 + brightness' },
19
+ { name: 'Tint', group: 'Colour & tone', params: { tint: [1, 0.85, 0.7] }, color: 'rgb * tint' },
20
+ { name: 'Sepia', group: 'Colour & tone', color: '(0.393r + 0.769g + 0.189b, 0.349r + 0.686g + 0.168b, 0.272r + 0.534g + 0.131b)' },
21
+ { name: 'Colour circle', group: 'Colour & tone', description: 'Black and white, except a soft circle that keeps its colour',
22
+ params: { across: { default: 0.5, min: 0, max: 1, label: 'where, across' }, down: { default: 0.5, min: 0, max: 1, label: 'where, down' },
23
+ radius: { default: 0.3, min: 0, max: 1 }, softness: { default: 0.08, min: 0, max: 0.5 } },
24
+ notes: ['The colour stays inside a circle and fades to grey over the softness. Sizes are parts of the picture\'s shorter side, so it stays round.',
25
+ { param: 'across' }, { param: 'down' }, { param: 'radius' }, { param: 'softness' }],
26
+ color: 'mix((gray, gray, gray), rgb, 1 - smoothstep(radius, radius + softness, distance((px, py), (across w, down h)) / min(w, h)))' },
27
+ { name: 'Vignette', group: 'Colour & tone', params: { strength: { default: 1.6, min: 0, max: 4 } }, color: 'rgb * (1 - strength ((x - 0.5)^2 + (y - 0.5)^2))' },
28
+ { name: 'Sobel edges', group: 'Blur & edges', description: 'How fast the brightness changes', params: { gain: { default: 2, min: 0.2, max: 10 } }, color: 'gain sobel()' },
29
+ { name: 'Edge map', group: 'Blur & edges', description: 'White where an edge is stronger than the cutoff', params: { cutoff: { default: 0.3, min: 0, max: 2 } }, color: 'if sobel() > cutoff then 1 else 0' },
30
+ { name: 'Outline', group: 'Blur & edges', description: 'Ink lines over the colour', params: { gain: { default: 3, min: 0, max: 10 } }, color: 'rgb (1 - gain sobel())' },
31
+ { name: 'Box blur', group: 'Blur & edges', params: { radius: { default: 2, min: 0, max: 8, step: 1 } }, color: 'blur(radius)' },
32
+ { name: 'Gaussian blur', group: 'Blur & edges', description: 'Computed in two passes, across then down, so it can be wide', params: { sigma: { default: 3, min: 0, max: 8 } }, color: 'gaussian(sigma)' },
33
+ { name: 'Canny edges', group: 'Blur & edges', description: 'Thin, clean edge lines: smooth, gradient, thinning, two thresholds, hysteresis — a chain',
34
+ steps: [
35
+ { params: { sigma: { default: 1.4, min: 0.5, max: 4 } }, color: 'gaussian(sigma)' },
36
+ // gradient strength (r) and its direction as one of four buckets (g: 0, ¼, ½, ¾ for 0°, 45°, 90°, 135°)
37
+ { color: '(min(sobel() / 4, 1), floor(fract((atan2(sobely(), sobelx()) + π/8) / π) 4) / 4, 0)' },
38
+ // non-maximum suppression: keep a pixel only if it is at least as strong as its two neighbours along the gradient
39
+ { color: 'if green(px, py) < 0.125 then (if r >= red(px - 1, py) & r >= red(px + 1, py) then r else 0)'
40
+ + ' else if green(px, py) < 0.375 then (if r >= red(px + 1, py + 1) & r >= red(px - 1, py - 1) then r else 0)'
41
+ + ' else if green(px, py) < 0.625 then (if r >= red(px, py - 1) & r >= red(px, py + 1) then r else 0)'
42
+ + ' else (if r >= red(px - 1, py + 1) & r >= red(px + 1, py - 1) then r else 0)' },
43
+ { params: { low: { default: 0.08, min: 0, max: 0.5 }, high: { default: 0.2, min: 0, max: 1 } }, color: 'if r > high then 1 else if r > low then 0.5 else 0' },
44
+ // hysteresis: a weak edge next to a strong one becomes strong (three rounds), the rest goes
45
+ 'if r > 0.9 then 1 else if r > 0.4 then (if red(dilate(1)) > 0.9 then 1 else 0.5) else 0',
46
+ 'if r > 0.9 then 1 else if r > 0.4 then (if red(dilate(1)) > 0.9 then 1 else 0.5) else 0',
47
+ 'if r > 0.9 then 1 else if r > 0.4 then (if red(dilate(1)) > 0.9 then 1 else 0.5) else 0',
48
+ 'if r > 0.9 then 1 else 0',
49
+ ] },
50
+ { name: 'Sharpen', group: 'Blur & edges', params: { amount: { default: 0.6, min: 0, max: 3 } }, color: 'sharpen(amount)' },
51
+ { name: 'Unsharp mask', group: 'Blur & edges', description: 'The picture plus what a blur removes', params: { amount: { default: 1.5, min: 0, max: 5 }, sigma: { default: 2, min: 0.5, max: 4 } }, color: 'rgb + amount (rgb - gaussian(sigma))' },
52
+ { name: 'Bloom', group: 'Blur & edges', description: 'Bright parts glow', params: { amount: { default: 0.8, min: 0, max: 3 }, sigma: { default: 3, min: 0.5, max: 4 }, cutoff: { default: 0.6, min: 0, max: 1 } },
53
+ color: 'rgb + amount max(gaussian(sigma) - cutoff, 0)' },
54
+ { name: 'Emboss', group: 'Blur & edges', color: 'emboss()' },
55
+ { name: 'Dilate', group: 'Blur & edges', description: 'Light areas grow', params: { radius: { default: 2, min: 0, max: 8, step: 1 } }, color: 'dilate(radius)' },
56
+ { name: 'Erode', group: 'Blur & edges', description: 'Dark areas grow', params: { radius: { default: 2, min: 0, max: 8, step: 1 } }, color: 'erode(radius)' },
57
+ { name: '3×3 kernel', group: 'Blur & edges', description: 'Your own convolution; the default is a sharpen',
58
+ params: { k1: { default: 0, min: -8, max: 8 }, k2: { default: -1, min: -8, max: 8 }, k3: { default: 0, min: -8, max: 8 }, k4: { default: -1, min: -8, max: 8 }, k5: { default: 5, min: -8, max: 8 }, k6: { default: -1, min: -8, max: 8 }, k7: { default: 0, min: -8, max: 8 }, k8: { default: -1, min: -8, max: 8 }, k9: { default: 0, min: -8, max: 8 } },
59
+ color: 'convolve(k1, k2, k3, k4, k5, k6, k7, k8, k9)' },
60
+ { name: 'Pixelate', group: 'Shape & motion', params: { size: { default: 8, min: 1, max: 32, step: 1 } }, color: 'pixel(floor(px / size) size, floor(py / size) size)' },
61
+ { name: 'Mirror', group: 'Shape & motion', color: 'pixel(w - px, py)' },
62
+ { name: 'Scanlines', group: 'Shape & motion', params: { period: { default: 4, min: 2, max: 16, step: 1 } }, color: 'if py mod period < period / 2 then rgb else rgb * 0.6' },
63
+ { name: 'Pulse', group: 'Shape & motion', description: 'Brightness breathes with t', params: { speed: { default: 6, min: 1, max: 30 }, depth: { default: 0.25, min: 0, max: 1 } },
64
+ color: 'rgb (1 - depth + depth sin(t / speed))' },
65
+ { name: 'Chroma key', group: 'Colour & tone', description: 'Replaces one colour with another',
66
+ params: { key: [0.22, 0.71, 0.29], tolerance: { default: 0.35, min: 0, max: 1 }, background: [0.1, 0.1, 0.4] },
67
+ color: 'if distance(rgb, key) < tolerance then background else rgb' },
68
+ { name: 'Through a mask', group: 'Other pictures', description: 'Put this last: the steps above show only where the mask is light',
69
+ images: { mask: { description: 'light where the effect shows' } }, color: 'mix(source, rgb, luma(mask))' },
70
+ { name: 'Mix', group: 'Other pictures', description: 'Fade towards another picture', images: ['overlay'], params: { amount: 0.5 }, color: 'mix(rgb, overlay, amount)' },
71
+ { name: 'Masked mix', group: 'Other pictures', images: { overlay: {}, mask: { description: 'light where the overlay shows' } }, color: 'mix(rgb, overlay, luma(mask))' },
72
+ { name: 'Multiply', group: 'Other pictures', images: ['overlay'], color: 'rgb * overlay' },
73
+ { name: 'Screen', group: 'Other pictures', images: ['overlay'], color: '1 - (1 - rgb)(1 - overlay)' },
74
+ { name: 'Overlay blend', group: 'Other pictures', images: ['overlay'], color: 'if gray < 0.5 then 2 rgb overlay else 1 - 2 (1 - rgb)(1 - overlay)' },
75
+ { name: 'Difference', group: 'Other pictures', images: ['overlay'], color: 'abs(rgb - overlay)' },
76
+ { name: 'Alpha over', group: 'Other pictures', description: 'A picture with transparency laid on top', images: ['overlay'], color: 'mix(rgb, overlay, alpha(overlay))' },
77
+ { name: 'Chroma key onto a picture', group: 'Other pictures', images: { background: {} },
78
+ params: { key: [0.22, 0.71, 0.29], tolerance: { default: 0.35, min: 0, max: 1 } }, color: 'if distance(rgb, key) < tolerance then background else rgb' },
79
+ { name: 'Only inside the selection', group: 'Other pictures', description: 'Put this last: the steps above show only inside what you selected on the picture (everywhere, without a selection)',
80
+ color: 'mix(source, rgb, selected)' },
81
+ { name: 'Only outside the selection', group: 'Other pictures', description: 'Put this last: the steps above show only outside what you selected', color: 'mix(rgb, source, selected)' },
82
+ { name: 'Caption', group: 'Text & shapes', description: 'Words along the bottom, with a soft shadow; change them under the step',
83
+ graphics: { title: { text: 'Your caption here', y: 0.88 } }, color: 'mix(rgb, title, alpha(title))' },
84
+ { name: 'Poster title', group: 'Text & shapes', description: 'Big outlined letters across the top',
85
+ graphics: { title: { text: 'BIG TITLE', font: 'Impact, Haettenschweiler, sans-serif', weight: 'normal', size: 0.16, y: 0.14, outline: { color: '#000000', width: 0.08 }, shadow: null } },
86
+ color: 'mix(rgb, title, alpha(title))' },
87
+ { name: 'Gradient text', group: 'Text & shapes', description: 'The letters coloured by maths: a sunset from left to right',
88
+ graphics: { title: { text: 'Golden hour', font: 'Georgia, serif', size: 0.13, y: 0.5, shadow: { color: 'rgba(0,0,0,0.5)', blur: 0.2, dx: 0, dy: 0.05 } } },
89
+ color: 'mix(rgb, mix((1, 0.35, 0.3), (1, 0.85, 0.25), x), alpha(title))' },
90
+ { name: 'Neon sign', group: 'Text & shapes', description: 'White letters with a coloured glow',
91
+ graphics: { title: { text: 'OPEN', font: 'Trebuchet MS, sans-serif', size: 0.18, y: 0.5, shadow: { color: '#00e5ff', blur: 0.45, dx: 0, dy: 0 } } },
92
+ color: 'mix(rgb * 0.6, title, alpha(title))' },
93
+ { name: 'Layered shadow', group: 'Text & shapes', description: 'Two shadow layers, each with its own filter: a soft glow that darkens and breathes with t, and a hard offset coloured by a gradient',
94
+ graphics: { title: { text: 'LAYERS', font: 'Impact, Haettenschweiler, sans-serif', weight: 'normal', size: 0.17, y: 0.5, shadow: null,
95
+ shadows: [{ color: '#000000', blur: 0.7, dx: 0, dy: 0.08 }, { color: '#000000', blur: 0, dx: 0.07, dy: 0.07 }] } },
96
+ color: 'mix(mix(mix(rgb, rgb * 0.25, alpha(title_shadow1) (0.7 + 0.3 sin(t / 10))), mix((1, 0.2, 0.6), (0.2, 0.6, 1), x), alpha(title_shadow2)), title, alpha(title))' },
97
+ { name: 'Ink brush', group: 'Brushes', description: 'Paint in a colour; draw on the picture with "draw" under the step',
98
+ graphics: { brush: { type: 'strokes', brush: 'round', size: 0.025 } }, params: { ink: [0.95, 0.2, 0.25] }, color: 'mix(rgb, ink, alpha(brush))' },
99
+ { name: 'Blur brush', group: 'Brushes', description: 'Paint a blur in — stronger every time a stroke goes over the same place',
100
+ graphics: { brush: { type: 'strokes', brush: 'soft', size: 0.08 } }, color: 'mix(rgb, gaussian(2 + 3 min(4 green(brush), 3)), alpha(brush))' },
101
+ { name: 'Darken brush', group: 'Brushes', description: 'Each pass darkens a little more, like layers of a wash',
102
+ graphics: { brush: { type: 'strokes', brush: 'soft', size: 0.07, flow: 0.7 } }, color: 'rgb (1 - 0.22 min(4 green(brush), 4))' },
103
+ { name: 'Colour reveal brush', group: 'Brushes', description: 'The picture starts grey; the brush paints its colour back',
104
+ graphics: { brush: { type: 'strokes', brush: 'soft', size: 0.1 } }, color: 'mix((gray, gray, gray), rgb, alpha(brush))' },
105
+ { name: 'Fuse brush', group: 'Brushes', description: 'Paint glows as it goes down, then cools to charcoal (red(brush) is its age)',
106
+ graphics: { brush: { type: 'strokes', brush: 'round', size: 0.02, age: 40 } },
107
+ color: 'mix(rgb, mix((1, 0.9, 0.5), (0.1, 0.07, 0.06), red(brush)), alpha(brush)) + (1, 0.5, 0.1) alpha(brush) max(0, 1 - 3 red(brush)) 0.6' },
108
+ { name: 'Rainbow brush', group: 'Brushes', description: 'The colour runs round the wheel along the drawing (blue(brush) is how far through it)',
109
+ graphics: { brush: { type: 'strokes', brush: 'marker', size: 0.035 } }, color: 'mix(rgb, fromhsv(blue(brush), 0.85, 1), alpha(brush))' },
110
+ { name: 'Chalk outline', group: 'Brushes', description: 'Chalk round an object: select it, then "outline the selection" under the step',
111
+ graphics: { brush: { type: 'strokes', brush: 'chalk', size: 0.018, timing: 'even', speed: 2 } }, color: 'mix(rgb, (0.97, 0.97, 0.92), alpha(brush))' },
112
+ { name: 'Frosted panel', group: 'Text & shapes', description: 'A rounded panel that blurs and darkens what is behind it — a shape with its own filter',
113
+ graphics: { panel: { type: 'rect', y: 0.86, w: 0.9, h: 0.2, radius: 0.25 } }, color: 'mix(rgb, gaussian(5) * 0.7, alpha(panel))' },
114
+ { name: 'Spotlight oval', group: 'Text & shapes', description: 'Darkens everything but a soft oval',
115
+ graphics: { spot: { type: 'ellipse', x: 0.5, y: 0.45, w: 0.45, h: 0.55, shadow: { color: '#ffffff', blur: 0.12, dx: 0, dy: 0 } } }, color: 'mix(rgb * 0.3, rgb, alpha(spot))' },
116
+ { name: 'Scrolling banner', group: 'Text & shapes', description: 'Text that slides across and wraps round (animated with t)',
117
+ graphics: { title: { text: 'Breaking news · scrolling text · ', size: 0.07, y: 0.92 } }, params: { speed: { default: 3, min: -10, max: 10 } },
118
+ color: 'mix(rgb, shift(title, -speed t, 0), alpha(shift(title, -speed t, 0)))' },
119
+ { name: 'Pulsing title', group: 'Text & shapes', description: 'Text that grows and shrinks a little (animated with t)',
120
+ graphics: { title: { text: 'Hello!', size: 0.14, y: 0.5 } }, params: { amount: { default: 0.06, min: 0, max: 0.3 } },
121
+ color: 'mix(rgb, zoom(title, 1 + amount sin(t / 8)), alpha(zoom(title, 1 + amount sin(t / 8))))' },
122
+ { name: 'Fade-in title', group: 'Text & shapes', description: 'Text that fades in over the first frames (animated with t)',
123
+ graphics: { title: { text: 'Once upon a time', font: 'Georgia, serif', style: 'italic', weight: 'normal', size: 0.09, y: 0.5 } }, params: { frames: { default: 40, min: 1, max: 200 } },
124
+ color: 'mix(rgb, title, alpha(title) clamp(t / frames, 0, 1))' },
125
+ { name: 'Zoom', group: 'Shape & motion', params: { scale: { default: 1.5, min: 0.25, max: 4 }, cx: 0.5, cy: 0.5 }, color: 'zoom(scale, cx, cy)' },
126
+ { name: 'Ken Burns', group: 'Shape & motion', description: 'A slow zoom that drifts across — a still picture that moves',
127
+ params: { speed: { default: 0.002, min: 0, max: 0.01 }, drift: { default: 0.0003, min: -0.001, max: 0.001 } }, color: 'zoom(1 + speed t, 0.5 + drift t, 0.45)' },
128
+ { name: 'Scroll', group: 'Shape & motion', description: 'Slides and wraps around', params: { dx: { default: 1, min: -5, max: 5 }, dy: { default: 0, min: -5, max: 5 } }, color: 'shift(dx t, dy t)' },
129
+ { name: 'Spin', group: 'Shape & motion', params: { speed: { default: 0.01, min: -0.05, max: 0.05 } }, color: 'rotate(speed t)' },
130
+ { name: 'Tile', group: 'Shape & motion', params: { n: { default: 2, min: 1, max: 8, step: 1 } }, color: 'tile(n)' },
131
+ { name: 'Crop', group: 'Shape & motion', description: 'A part of the picture, filling the frame', params: { left: 0.1, top: 0.1, right: 0.9, bottom: 0.9 }, color: 'crop(left, top, right, bottom)' },
132
+ { name: 'Flip', group: 'Shape & motion', color: 'flipx()' },
133
+ { name: 'Picture in picture', group: 'Other pictures', description: 'Another picture, fitted small in a corner',
134
+ images: { inset: { description: 'any size: it is fitted whole', place: 'fit', zoom: 0.35, x: 0.8, y: 0.78 } }, color: 'mix(rgb, inset, alpha(inset))' },
135
+ { name: 'Moving overlay', group: 'Other pictures', description: 'Another picture scrolling across at half strength', images: ['overlay'], params: { speed: { default: 2, min: -10, max: 10 } },
136
+ color: 'mix(rgb, shift(overlay, speed t, 0), 0.5)' },
137
+ { name: 'Hue shift', group: 'Colour & tone', description: 'Turns every colour around the hue wheel (in HSV)', space: 'hsv',
138
+ params: { amount: { default: 0.1, min: -0.5, max: 0.5 } }, color: '(fract(r + amount), g, b)' },
139
+ { name: 'Saturation', group: 'Colour & tone', description: 'More or less colour (in HSV)', space: 'hsv',
140
+ params: { amount: { default: 1.5, min: 0, max: 3 } }, color: '(r, clamp(g amount, 0, 1), b)' },
141
+ { name: 'Lightness', group: 'Colour & tone', description: 'Lighter or darker without changing the colours (in Lab)', space: 'lab',
142
+ params: { amount: { default: 1.15, min: 0.5, max: 2 } }, color: '(clamp(r amount, 0, 1), g, b)' },
143
+ { name: 'Vivid', group: 'Colour & tone', description: 'Colourfulness, perceptually even (in OKLab)', space: 'oklab',
144
+ params: { amount: { default: 1.6, min: 0, max: 3 } }, color: '(r, 0.5 + (g - 0.5) amount, 0.5 + (b - 0.5) amount)' },
145
+ { name: 'Linear-light blur', group: 'Blur & edges', description: 'A Gaussian in linear light: bright spots stay bright, as a lens does', space: 'linear',
146
+ params: { sigma: { default: 3, min: 0, max: 8 } }, color: 'gaussian(sigma)' },
147
+ { name: 'Film grain', group: 'Colour & tone', description: 'Noise that changes every frame', params: { amount: { default: 0.15, min: 0, max: 0.6 } }, color: 'rgb + amount (random() - 0.5)' },
148
+ { name: 'Swirl', group: 'Shape & motion', params: { strength: { default: 0.02, min: 0, max: 0.1 } },
149
+ color: 'pixel(w/2 + (px - w/2) cos(strength t gray) - (py - h/2) sin(strength t gray), h/2 + (px - w/2) sin(strength t gray) + (py - h/2) cos(strength t gray))' },
150
+ ];
151
+ const byName = (name) => FILTERS.find((f) => f.name === name);
152
+ FILTERS.push(
153
+ { name: 'Old film', group: 'Chains', description: 'Sepia, a vignette and faint scanlines — a chain of three',
154
+ steps: [byName('Sepia'), { filter: byName('Vignette'), with: { strength: 1.2 } }, { filter: byName('Scanlines'), opacity: 0.5 }] },
155
+ { name: 'Comic', group: 'Chains', description: 'Bold flat colours with ink lines: a small blur first, so a photo\'s fine texture isn\'t inked — a chain of four',
156
+ steps: [{ filter: byName('Gaussian blur'), with: { sigma: 1.6 } }, { filter: byName('Vivid'), with: { amount: 1.5 } },
157
+ { filter: byName('Posterize'), with: { levels: 4 } }, { filter: byName('Outline'), with: { gain: 1.2 } }] },
158
+ { name: 'Cartoon', group: 'Chains', description: 'Blur, then flatten the colours, then outline — a chain of three',
159
+ steps: [{ filter: byName('Gaussian blur'), with: { sigma: 1.5 } }, { filter: byName('Posterize'), with: { levels: 6 } }, { filter: byName('Outline'), with: { gain: 2 } }] },
160
+ { name: 'Dreamy', group: 'Chains', description: 'A soft glow mixed over the picture',
161
+ steps: [{ filter: byName('Bloom'), with: { amount: 1.2 } }, { filter: byName('Gaussian blur'), with: { sigma: 2 }, opacity: 0.4 }] },
162
+ { name: 'Tilt shift', group: 'Chains', description: 'Blurred where the mask is light — a chain',
163
+ steps: [{ filter: byName('Gaussian blur'), with: { sigma: 4 } }, byName('Through a mask')] },
164
+ { name: 'Title card', group: 'Chains', description: 'A frosted panel with a caption on it — a chain of two',
165
+ steps: [byName('Frosted panel'), { filter: byName('Caption'), with: { title: { text: 'A title on a panel', y: 0.86 } } }] },
166
+ { name: 'Colour spotlight', group: 'Chains', description: 'Grey except where the mask is dark — a chain',
167
+ steps: [byName('Grayscale'), byName('Through a mask')] },
168
+ );
169
+
170
+ export const LIBRARY = FILTERS;
171
+
172
+ /** The groups the editor shows the library in, in this order; every filter names one. */
173
+ export const GROUPS = ['Colour & tone', 'Blur & edges', 'Shape & motion', 'Text & shapes', 'Brushes', 'Other pictures', 'Chains'];
package/src/png.js ADDED
@@ -0,0 +1,157 @@
1
+ /**
2
+ * A PNG writer that can do what the browser's cannot: an indexed picture
3
+ * (PNG-8) at 1, 2, 4 or 8 bits a pixel when the picture has few colours —
4
+ * a threshold or posterize result is 5–20× smaller that way — and RGB
5
+ * rather than RGBA when nothing is transparent. Truecolour rows use the
6
+ * Paeth filter, which deflate likes for photos.
7
+ *
8
+ * const { blob, kind, colours, bits } = await encodePng({ width, height, data, palette: 'auto', colours: 256, dither: true });
9
+ *
10
+ * `palette`: 'auto' (indexed if the picture has ≤ 256 colours, else
11
+ * truecolour), 'none', or 'force' (reduce to `colours` colours, with
12
+ * optional dithering — that needs a `quantize(data, colours, { dither, width })`
13
+ * returning { palette, indices }: the package has no quantizer of its own,
14
+ * so it stays dependency-free; the editor passes one built on gifenc).
15
+ * Deflate comes from CompressionStream (every current browser, and Node
16
+ * ≥ 18); without it encodePng() returns null and the caller falls back to
17
+ * canvas.toBlob.
18
+ */
19
+ const CRC = new Int32Array(256);
20
+ for (let n = 0; n < 256; n++) {
21
+ let c = n;
22
+ for (let k = 0; k < 8; k++) c = c & 1 ? 0xEDB88320 ^ (c >>> 1) : c >>> 1;
23
+ CRC[n] = c;
24
+ }
25
+
26
+ function crc32(bytes) {
27
+ let c = -1;
28
+ for (let i = 0; i < bytes.length; i++) c = CRC[(c ^ bytes[i]) & 0xFF] ^ (c >>> 8);
29
+ return (c ^ -1) >>> 0;
30
+ }
31
+
32
+ function chunk(type, data) {
33
+ const out = new Uint8Array(12 + data.length);
34
+ const view = new DataView(out.buffer);
35
+ view.setUint32(0, data.length);
36
+ for (let i = 0; i < 4; i++) out[4 + i] = type.charCodeAt(i);
37
+ out.set(data, 8);
38
+ view.setUint32(8 + data.length, crc32(out.subarray(4, 8 + data.length)));
39
+ return out;
40
+ }
41
+
42
+ async function deflate(bytes) {
43
+ if (typeof CompressionStream === 'undefined') return null;
44
+ const stream = new CompressionStream('deflate'); // zlib framing, as PNG wants
45
+ const writer = stream.writable.getWriter();
46
+ writer.write(bytes);
47
+ writer.close();
48
+ return new Uint8Array(await new Response(stream.readable).arrayBuffer());
49
+ }
50
+
51
+ /** How many distinct RGBA colours, counting up to `limit`. */
52
+ export function colourCount(data, limit = 257) {
53
+ const seen = new Set();
54
+ for (let i = 0; i < data.length; i += 4) {
55
+ seen.add(((data[i] << 24) | (data[i + 1] << 16) | (data[i + 2] << 8) | data[i + 3]) >>> 0);
56
+ if (seen.size >= limit) return seen.size;
57
+ }
58
+ return seen.size;
59
+ }
60
+
61
+ /** Every colour as it is, when there are at most 256: { palette: [[r, g, b, a]…], indices }. */
62
+ export function exactPalette(data) {
63
+ const index = new Map();
64
+ const palette = [];
65
+ const indices = new Uint8Array(data.length / 4);
66
+ for (let i = 0, p = 0; i < data.length; i += 4, p++) {
67
+ const key = ((data[i] << 24) | (data[i + 1] << 16) | (data[i + 2] << 8) | data[i + 3]) >>> 0;
68
+ let k = index.get(key);
69
+ if (k === undefined) {
70
+ if (palette.length === 256) return null;
71
+ k = palette.length;
72
+ index.set(key, k);
73
+ palette.push([data[i], data[i + 1], data[i + 2], data[i + 3]]);
74
+ }
75
+ indices[p] = k;
76
+ }
77
+ return { palette, indices };
78
+ }
79
+
80
+ function paeth(a, b, c) {
81
+ const p = a + b - c, pa = Math.abs(p - a), pb = Math.abs(p - b), pc = Math.abs(p - c);
82
+ return pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
83
+ }
84
+
85
+ /**
86
+ * @param {object} o { width, height, data: RGBA bytes, palette: 'auto' | 'none' | 'force', colours, dither, quantize }
87
+ * @returns {Promise<{ blob: Blob, kind: 'palette' | 'rgb' | 'rgba', colours: number, bits: number } | null>}
88
+ */
89
+ export async function encodePng({ width, height, data, palette = 'auto', colours = 256, dither = true, quantize = null }) {
90
+ if (typeof CompressionStream === 'undefined') return null;
91
+ if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1 || !data || data.length < width * height * 4) {
92
+ throw new Error('encodePng: width and height are whole numbers of pixels, and data holds 4 bytes for each');
93
+ }
94
+ let indexed = null;
95
+ if (palette === 'auto' || palette === 'force') {
96
+ indexed = exactPalette(data);
97
+ if (indexed && indexed.palette.length > colours) indexed = null;
98
+ if (!indexed && palette === 'force') {
99
+ if (!quantize) throw new Error('encodePng: palette "force" needs a quantize function (the picture has more than `colours` colours)');
100
+ indexed = quantize(data, Math.min(256, Math.max(2, colours)), { dither, width });
101
+ }
102
+ }
103
+ const chunks = [new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10])];
104
+ const ihdr = new Uint8Array(13);
105
+ const view = new DataView(ihdr.buffer);
106
+ view.setUint32(0, width);
107
+ view.setUint32(4, height);
108
+ let rows, kind, bits;
109
+ if (indexed) {
110
+ const n = indexed.palette.length;
111
+ bits = n <= 2 ? 1 : n <= 4 ? 2 : n <= 16 ? 4 : 8;
112
+ kind = 'palette';
113
+ ihdr[8] = bits; ihdr[9] = 3; // colour type 3: indexed
114
+ const stride = Math.ceil(width * bits / 8);
115
+ rows = new Uint8Array(height * (1 + stride));
116
+ for (let y = 0; y < height; y++) {
117
+ const at = y * (1 + stride) + 1; // filter byte 0: none
118
+ for (let x = 0; x < width; x++) {
119
+ const k = indexed.indices[y * width + x];
120
+ if (bits === 8) rows[at + x] = k;
121
+ else rows[at + ((x * bits) >> 3)] |= k << (8 - bits - (x * bits) % 8);
122
+ }
123
+ }
124
+ const plte = new Uint8Array(n * 3);
125
+ const trns = new Uint8Array(n);
126
+ let transparent = false;
127
+ indexed.palette.forEach(([r, g, b, a], i) => { plte.set([r, g, b], i * 3); trns[i] = a; if (a < 255) transparent = true; });
128
+ chunks.push(chunk('IHDR', ihdr), chunk('PLTE', plte));
129
+ if (transparent) chunks.push(chunk('tRNS', trns));
130
+ } else {
131
+ let opaque = true;
132
+ for (let i = 3; i < data.length; i += 4) if (data[i] < 255) { opaque = false; break; }
133
+ const bpp = opaque ? 3 : 4;
134
+ kind = opaque ? 'rgb' : 'rgba';
135
+ bits = 8;
136
+ ihdr[8] = 8; ihdr[9] = opaque ? 2 : 6;
137
+ const stride = width * bpp;
138
+ rows = new Uint8Array(height * (1 + stride));
139
+ const raw = new Uint8Array(stride);
140
+ const prev = new Uint8Array(stride);
141
+ for (let y = 0; y < height; y++) {
142
+ for (let x = 0, i = y * width * 4, j = 0; x < width; x++, i += 4, j += bpp) {
143
+ raw[j] = data[i]; raw[j + 1] = data[i + 1]; raw[j + 2] = data[i + 2];
144
+ if (!opaque) raw[j + 3] = data[i + 3];
145
+ }
146
+ const at = y * (1 + stride);
147
+ rows[at] = 4; // Paeth
148
+ for (let j = 0; j < stride; j++) {
149
+ rows[at + 1 + j] = raw[j] - paeth(j >= bpp ? raw[j - bpp] : 0, prev[j], j >= bpp ? prev[j - bpp] : 0);
150
+ }
151
+ prev.set(raw);
152
+ }
153
+ chunks.push(chunk('IHDR', ihdr));
154
+ }
155
+ chunks.push(chunk('IDAT', await deflate(rows)), chunk('IEND', new Uint8Array(0)));
156
+ return { blob: new Blob(chunks, { type: 'image/png' }), kind, colours: indexed ? indexed.palette.length : colourCount(data), bits };
157
+ }
package/src/runtime.js ADDED
@@ -0,0 +1,248 @@
1
+ /**
2
+ * Runs a compiled filter (compileFilter()) in WebGL: uploads the picture,
3
+ * draws each pass into a texture, and the last one onto the canvas.
4
+ *
5
+ * const runner = createFilterRunner(gl, compileFilter(def));
6
+ * runner.setImage(imageOrVideoOrCanvas);
7
+ * runner.setImage('mask', maskImage); // each picture the filter names (compiled.images)
8
+ * runner.draw({ t: frameIndex });
9
+ * runner.passes[0].values.cutoff = 0.3; // knobs change without recompiling
10
+ * runner.dispose();
11
+ *
12
+ * Works with WebGL 1 and 2 (`compileFilter(def, { version: 300 })` for
13
+ * the latter). The output is the size of the canvas; the picture is
14
+ * stretched to it, so size the canvas to the picture.
15
+ *
16
+ * Between passes the picture lives in 8-bit textures, so values clamp
17
+ * to 0–1 and round to 1/255 at each step. `{ precision: 'half' }` or
18
+ * `'float'` keeps more (bloom's bright spill, edge strengths over 1)
19
+ * where the GPU can render to such textures; `runner.precision` says
20
+ * what was granted.
21
+ */
22
+ import { filterVertexShader } from 'mathzetta/glsl';
23
+
24
+ export function createFilterRunner(gl, compiled, options = {}) {
25
+ if (gl.isContextLost()) throw new Error('The WebGL context is lost');
26
+ const precision = texturePrecision(gl, options.precision);
27
+ const vertex = filterVertexShader({ version: compiled.version });
28
+ const programs = [];
29
+ try {
30
+ for (const pass of compiled.passes) programs.push(link(gl, vertex, pass.fragment, pass.name));
31
+ } catch (error) {
32
+ for (const program of programs) gl.deleteProgram(program); // a later pass refused: the earlier ones go too
33
+ throw error;
34
+ }
35
+ /** The key a pass's picture is bound under (a graphic's own key; a picture's name). */
36
+ const bound = (pass, name) => (pass.bind && Object.hasOwn(pass.bind, name) ? pass.bind[name] : name);
37
+ const locations = programs.map(() => new Map());
38
+ const location = (i, name) => {
39
+ if (!locations[i].has(name)) locations[i].set(name, gl.getUniformLocation(programs[i], name));
40
+ return locations[i].get(name);
41
+ };
42
+
43
+ const triangle = gl.createBuffer();
44
+ gl.bindBuffer(gl.ARRAY_BUFFER, triangle);
45
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW); // one triangle covers the canvas
46
+
47
+ const source = { texture: gl.createTexture(), framebuffer: null };
48
+ gl.bindTexture(gl.TEXTURE_2D, source.texture);
49
+ clampAndFilter(gl);
50
+ const pictures = new Map(); // name → texture, for compiled.images
51
+ const sizes = new Map(); // name → [width, height] of that picture
52
+ const blank = gl.createTexture(); // a picture not yet given: white, 1×1
53
+ gl.bindTexture(gl.TEXTURE_2D, blank);
54
+ clampAndFilter(gl);
55
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([255, 255, 255, 255]));
56
+ const clear = gl.createTexture(); // a graphic not yet drawn: transparent, 1×1
57
+ gl.bindTexture(gl.TEXTURE_2D, clear);
58
+ clampAndFilter(gl);
59
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([0, 0, 0, 0]));
60
+ const pool = []; // textures passes draw into, reused between frames
61
+ let width = 0;
62
+ let height = 0;
63
+
64
+ function buffer(exclude) {
65
+ let found = pool.find((b) => !exclude.includes(b));
66
+ if (!found) {
67
+ found = { texture: gl.createTexture(), framebuffer: gl.createFramebuffer() };
68
+ gl.bindTexture(gl.TEXTURE_2D, found.texture);
69
+ clampAndFilter(gl, precision.filter);
70
+ gl.texImage2D(gl.TEXTURE_2D, 0, precision.internal, width, height, 0, gl.RGBA, precision.type, null);
71
+ gl.bindFramebuffer(gl.FRAMEBUFFER, found.framebuffer);
72
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, found.texture, 0);
73
+ if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE && precision.name !== 'byte') {
74
+ Object.assign(precision, texturePrecision(gl, 'byte')); // this GPU won't render to it after all
75
+ gl.bindTexture(gl.TEXTURE_2D, found.texture);
76
+ clampAndFilter(gl, precision.filter);
77
+ gl.texImage2D(gl.TEXTURE_2D, 0, precision.internal, width, height, 0, gl.RGBA, precision.type, null);
78
+ }
79
+ pool.push(found);
80
+ }
81
+ return found;
82
+ }
83
+
84
+ const runner = {
85
+ /** The passes with their live `values`; change one and the next draw uses it. */
86
+ passes: compiled.passes,
87
+ /** 'byte', 'half' or 'float': what the textures between passes hold. */
88
+ get precision() { return precision.name; },
89
+
90
+ /**
91
+ * Uploads a picture: an <img>, <video>, <canvas> or ImageData. Call
92
+ * again for each video frame. With a name first, it is one of the
93
+ * pictures the filter asks for (`compiled.images`); `source` is the
94
+ * main picture itself.
95
+ */
96
+ setImage(name, image) {
97
+ if (typeof name !== 'string') { image = name; name = null; }
98
+ if (name && name !== 'source' && image == null) { // setImage(name, null): back to "not given" (white)
99
+ if (pictures.has(name)) gl.deleteTexture(pictures.get(name));
100
+ pictures.delete(name);
101
+ sizes.delete(name);
102
+ return runner;
103
+ }
104
+ let texture = source.texture;
105
+ if (name && name !== 'source') {
106
+ if (!pictures.has(name)) {
107
+ pictures.set(name, gl.createTexture());
108
+ gl.bindTexture(gl.TEXTURE_2D, pictures.get(name));
109
+ clampAndFilter(gl);
110
+ }
111
+ texture = pictures.get(name);
112
+ }
113
+ // a plain picture { data, width, height } (what the CPU runner and Node give; floats are 0–1): as ImageData
114
+ if (image && image.data && image.width && image.height && typeof ImageData !== 'undefined' && !(image instanceof ImageData)) {
115
+ const src = image.data, float = src instanceof Float32Array || src instanceof Float64Array;
116
+ const bytes = src instanceof Uint8ClampedArray ? src : Uint8ClampedArray.from(src, float ? (v) => v * 255 : undefined);
117
+ image = new ImageData(bytes, image.width, image.height);
118
+ }
119
+ gl.bindTexture(gl.TEXTURE_2D, texture);
120
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true); // so y = 0 is the top, as in the picture
121
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
122
+ if (name) sizes.set(name, [image.naturalWidth || image.videoWidth || image.width || 1, image.naturalHeight || image.videoHeight || image.height || 1]);
123
+ return runner;
124
+ },
125
+
126
+ /** Draws the filtered picture onto the canvas; `t` is the frame index. */
127
+ draw({ t = 0 } = {}) {
128
+ if (gl.drawingBufferWidth !== width || gl.drawingBufferHeight !== height) {
129
+ width = gl.drawingBufferWidth;
130
+ height = gl.drawingBufferHeight;
131
+ for (const b of pool) {
132
+ gl.bindTexture(gl.TEXTURE_2D, b.texture);
133
+ gl.texImage2D(gl.TEXTURE_2D, 0, precision.internal, width, height, 0, gl.RGBA, precision.type, null);
134
+ }
135
+ }
136
+ let input = source;
137
+ const kept = []; // inputs of chains still waiting for their blend pass
138
+ compiled.passes.forEach((pass, i) => {
139
+ for (let k = 0; k < pass.keepInput; k++) kept.push(input);
140
+ const last = i === compiled.passes.length - 1;
141
+ const target = last ? null : buffer([input, ...kept]);
142
+ gl.bindFramebuffer(gl.FRAMEBUFFER, target ? target.framebuffer : null);
143
+ gl.viewport(0, 0, width, height);
144
+ const program = programs[i];
145
+ gl.useProgram(program);
146
+ const position = gl.getAttribLocation(program, 'position');
147
+ gl.bindBuffer(gl.ARRAY_BUFFER, triangle);
148
+ gl.enableVertexAttribArray(position);
149
+ gl.vertexAttribPointer(position, 2, gl.FLOAT, false, 0, 0);
150
+ gl.activeTexture(gl.TEXTURE0);
151
+ gl.bindTexture(gl.TEXTURE_2D, input.texture);
152
+ gl.uniform1i(location(i, 'image'), 0);
153
+ gl.activeTexture(gl.TEXTURE1);
154
+ gl.bindTexture(gl.TEXTURE_2D, pass.blendKept ? kept[kept.length - 1].texture : null);
155
+ if (pass.blendKept) gl.uniform1i(location(i, 'base'), 1);
156
+ pass.images.forEach((name, k) => {
157
+ gl.activeTexture(gl.TEXTURE2 + k);
158
+ gl.bindTexture(gl.TEXTURE_2D, name === 'source' ? source.texture : (pictures.get(bound(pass, name)) ?? (pass.graphics?.[name] ? clear : blank)));
159
+ gl.uniform1i(location(i, `mz_img_${name}`), 2 + k);
160
+ if (!pass.placements[name]) return; // source, selection: the frame's size, no placement
161
+ const size = sizes.get(bound(pass, name)) ?? [1, 1];
162
+ const place = pass.placements[name];
163
+ gl.uniform2f(location(i, `mz_size_${name}`), size[0], size[1]);
164
+ gl.uniform4f(location(i, `mz_place_${name}`), Number(place.zoom), Number(place.x), Number(place.y), Number(place.angle));
165
+ });
166
+ gl.uniform2f(location(i, 'resolution'), width, height);
167
+ gl.uniform1f(location(i, 't'), t);
168
+ gl.uniform1f(location(i, 'mz_opacity'), pass.opacity);
169
+ for (const [name, param] of Object.entries(pass.params)) {
170
+ const value = pass.values[name];
171
+ if (param.type === 'color') gl.uniform3fv(location(i, name), value.map(Number));
172
+ else gl.uniform1f(location(i, name), Number(value));
173
+ }
174
+ gl.drawArrays(gl.TRIANGLES, 0, 3);
175
+ if (pass.blendKept) kept.pop();
176
+ input = target;
177
+ });
178
+ return runner;
179
+ },
180
+
181
+ dispose() {
182
+ for (const program of programs) gl.deleteProgram(program);
183
+ gl.deleteTexture(source.texture);
184
+ gl.deleteTexture(blank);
185
+ gl.deleteTexture(clear);
186
+ for (const texture of pictures.values()) gl.deleteTexture(texture);
187
+ for (const b of pool) { gl.deleteTexture(b.texture); gl.deleteFramebuffer(b.framebuffer); }
188
+ gl.deleteBuffer(triangle);
189
+ },
190
+ };
191
+ return runner;
192
+ }
193
+
194
+ function clampAndFilter(gl, filter = gl.LINEAR) {
195
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
196
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
197
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filter);
198
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter);
199
+ }
200
+
201
+ /** The texture format for the pictures between passes: 8-bit unless asked for and available. */
202
+ function texturePrecision(gl, wanted = 'byte') {
203
+ const byte = { name: 'byte', internal: gl.RGBA, type: gl.UNSIGNED_BYTE, filter: gl.LINEAR };
204
+ if (wanted !== 'half' && wanted !== 'float') return byte;
205
+ const webgl2 = typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext;
206
+ if (webgl2) {
207
+ if (!gl.getExtension('EXT_color_buffer_float')) return byte;
208
+ const linear = gl.getExtension('OES_texture_float_linear') ? gl.LINEAR : gl.NEAREST;
209
+ return wanted === 'float'
210
+ ? { name: 'float', internal: gl.RGBA32F, type: gl.FLOAT, filter: linear }
211
+ : { name: 'half', internal: gl.RGBA16F, type: gl.HALF_FLOAT, filter: gl.LINEAR };
212
+ }
213
+ if (wanted === 'float' && gl.getExtension('OES_texture_float') && gl.getExtension('WEBGL_color_buffer_float')) {
214
+ return { name: 'float', internal: gl.RGBA, type: gl.FLOAT, filter: gl.getExtension('OES_texture_float_linear') ? gl.LINEAR : gl.NEAREST };
215
+ }
216
+ const half = gl.getExtension('OES_texture_half_float');
217
+ if (half && gl.getExtension('EXT_color_buffer_half_float')) {
218
+ return { name: 'half', internal: gl.RGBA, type: half.HALF_FLOAT_OES, filter: gl.getExtension('OES_texture_half_float_linear') ? gl.LINEAR : gl.NEAREST };
219
+ }
220
+ return byte;
221
+ }
222
+
223
+ function link(gl, vertex, fragment, name) {
224
+ const program = gl.createProgram();
225
+ const shaders = [];
226
+ let problem = null;
227
+ for (const [type, source] of [[gl.VERTEX_SHADER, vertex], [gl.FRAGMENT_SHADER, fragment]]) {
228
+ const shader = gl.createShader(type);
229
+ shaders.push(shader);
230
+ gl.shaderSource(shader, source);
231
+ gl.compileShader(shader);
232
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
233
+ problem = `${name || 'The filter'} does not compile: ${(gl.getShaderInfoLog(shader) ?? '').trim()}`;
234
+ break;
235
+ }
236
+ gl.attachShader(program, shader);
237
+ }
238
+ if (!problem) {
239
+ gl.linkProgram(program);
240
+ if (!gl.getProgramParameter(program, gl.LINK_STATUS)) problem = `${name || 'The filter'} does not link: ${(gl.getProgramInfoLog(program) ?? '').trim()}`;
241
+ }
242
+ for (const shader of shaders) gl.deleteShader(shader); // a linked program keeps what it needs of them
243
+ if (problem) {
244
+ gl.deleteProgram(program);
245
+ throw new Error(problem);
246
+ }
247
+ return program;
248
+ }