asciianything 1.0.1 → 1.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/README.md CHANGED
@@ -1,106 +1,170 @@
1
+ <div align="center">
2
+
1
3
  # asciianything
2
4
 
3
- Turn any video into a mosaic of coloured lowercase prose — frame-accurate, full length, full resolution.
5
+ **A video rebuilt from coloured lowercase prose. One command, every frame, at the source's own resolution and frame rate.**
4
6
 
5
- ```bash
6
- npx asciianything night-drive.mp4
7
- ```
7
+ [![npm](https://img.shields.io/npm/v/asciianything?style=flat-square&color=1d1d1f&logo=npm&logoColor=white)](https://www.npmjs.com/package/asciianything)
8
+ [![downloads](https://img.shields.io/npm/dm/asciianything?style=flat-square&color=1d1d1f&label=downloads)](https://www.npmjs.com/package/asciianything)
9
+ [![stars](https://img.shields.io/github/stars/sloemo01/asciianything?style=flat-square&color=1d1d1f&logo=github&label=stars)](https://github.com/sloemo01/asciianything/stargazers)
10
+ [![license](https://img.shields.io/github/license/sloemo01/asciianything?style=flat-square&color=1d1d1f)](LICENSE)
11
+ [![node](https://img.shields.io/badge/node-%E2%89%A518-1d1d1f?style=flat-square)](package.json)
8
12
 
9
- ![night highway, source and ascii prose](https://raw.githubusercontent.com/sloemo01/asciianything/main/assets/comparison.png)
13
+ <p>
14
+ <a href="assets/night-highway-1080p.mp4"><img src="https://raw.githubusercontent.com/sloemo01/asciianything/main/assets/night-highway-1080p.jpg" width="32%" alt="Night highway in ASCII prose, 1920x1080"></a>
15
+ <a href="assets/sunset-ascii.mp4"><img src="https://raw.githubusercontent.com/sloemo01/asciianything/main/assets/sunset.jpg" width="32%" alt="Sunset in ASCII prose, 3840x2160 at 60 fps"></a>
16
+ <a href="assets/demo.mp4"><img src="https://raw.githubusercontent.com/sloemo01/asciianything/main/assets/portrait.jpg" width="32%" alt="Night highway in ASCII prose, vertical 1440x2560"></a>
17
+ </p>
10
18
 
11
- Not an `@%#*+=-:.` ramp. Every cell of the picture is one stable English word-character, the prose is readable up close, and at normal viewing distance the mosaic reads as the original footage: same framing, same colours, same exposure, same motion.
19
+ <sub><b>Night highway</b>, 19.4 s, 1920×1080 &nbsp;·&nbsp; <b>Sunset</b>, 11.6 s, 3840×2160·60 &nbsp;·&nbsp; <b>Night highway</b>, 19.4 s, 1440×2560</sub>
12
20
 
13
- ![ascii frame](https://raw.githubusercontent.com/sloemo01/asciianything/main/assets/preview.png)
21
+ <sub>Three clips rendered by this package. Click a tile for the MP4.</sub>
14
22
 
15
- ## Why it looks like the video and not like text wallpaper
23
+ [**Run it**](#run-it) &nbsp;·&nbsp; [What it is](#what-it-is) &nbsp;·&nbsp; [Options](#options) &nbsp;·&nbsp; [How it works](#how-it-works) &nbsp;·&nbsp; [Verification](#verification)
16
24
 
17
- A glyph only covers part of its cell. Drawing the sampled source colour into the letter multiplies the source by the ink coverage and turns bright footage into a dark wall of text. So the renderer measures, for every character at every weight, how much of the cell it actually inks, and divides:
25
+ </div>
18
26
 
19
- ```
20
- emitted = scene / (coverage × calibration)
21
- ```
27
+ Every cell of the picture holds one stable English character. Close up it is lowercase prose you can read; at normal
28
+ viewing distance the same cells add up to the footage — same framing, same colour, same exposure, same motion. Nothing is
29
+ painted under the letters: all visible structure comes from glyph ink, weight and colour.
22
30
 
23
- Each glyph is therefore brighter than its source cell, and the **integrated cell** — letters plus the black gaps between them — averages back to the sampled colour in linear light. Bright regions stay bright; a blue sky stays blue instead of going navy.
31
+ This is not an `@%#*+=-:.` brightness ramp, and it is not text laid over a video. One source frame goes in, one rendered
32
+ frame comes out, and the only things that change between frames are scene colour, sampled luminance, glyph weight and
33
+ emitted colour. No zoom, pan, transition, grain or vignette is ever added.
24
34
 
25
- The rest of the contract:
35
+ ![source and output, same frame](https://raw.githubusercontent.com/sloemo01/asciianything/main/assets/sunset-comparison.jpg)
26
36
 
27
- - **One fixed prose field.** A sentence bank is shuffled per row with a deterministic hash, so no row repeats the row above and every cell keeps the same character for the whole video. Only colour and glyph weight follow the scene.
28
- - **Subcell sampling.** Each cell integrates a `cellW × cellH` block of the full-resolution frame, recovering percentiles and the bright-quartile colour, so a light smaller than a cell still lifts it.
29
- - **Linear light.** sRGB is decoded to linear before any averaging or compensation, then encoded once on output.
30
- - **No synthetic cinematography.** No zoom, pan, transitions, grain, vignette or animated font size. One source frame in, one rendered frame out.
37
+ ## What it is
31
38
 
32
- ## Requirements
39
+ A small Node CLI around a deterministic renderer.
33
40
 
34
- - Node 18+
35
- - `ffmpeg` and `ffprobe` on `PATH`
41
+ - **Prose, not noise.** A sentence bank is shuffled per row with a deterministic hash: every row is a different slice of
42
+ the bank, no row repeats the row above it, and each cell keeps the same character for the whole video.
43
+ - **Fidelity first.** One fixed cover transform, one fixed grid, one rendered frame per decoded source frame. Frame
44
+ count, duration, resolution and frame rate are probed from the source and reproduced exactly.
45
+ - **Linear light.** sRGB is decoded before any averaging, compensation or comparison, and encoded once on the way out.
46
+ - **Zero disk churn.** Raw frames are piped from `ffmpeg` into the renderer and straight into `libx264`; no PNG sequence
47
+ is ever written.
36
48
 
37
- ## Usage
49
+ ## Run it
38
50
 
39
51
  ```bash
40
- # whole video, same resolution / frame rate / duration
41
- npx asciianything input.mp4 -o output.mp4
52
+ npx asciianything night-drive.mp4
53
+ ```
54
+
55
+ That writes `night-drive-ascii.mp4` at the source's resolution, frame rate and duration. Node 18+ and `ffmpeg` /
56
+ `ffprobe` on `PATH` are the only requirements.
42
57
 
43
- # bigger letters
44
- npx asciianything input.mp4 --font-size 32
58
+ ```bash
59
+ # a specific output, larger letters
60
+ npx asciianything clip.mp4 -o poster.mp4 --font-size 32
45
61
 
46
- # one still as PNG (seconds into the video)
47
- npx asciianything input.mp4 --still 8 -o frame.png
62
+ # one still, 8 seconds in
63
+ npx asciianything clip.mp4 --still 8 -o frame.png
48
64
 
49
- # preview the first 60 frames
50
- npx asciianything input.mp4 --limit 60 -o preview.mp4
65
+ # 60 frames to check a look before committing to the whole reel
66
+ npx asciianything clip.mp4 --limit 60 -o preview.mp4
51
67
 
52
- # your own prose instead of the bundled night-highway bank
53
- npx asciianything input.mp4 --prose my-words.txt
68
+ # your own sentences instead of the bundled bank
69
+ npx asciianything clip.mp4 --prose my-words.txt
54
70
  ```
55
71
 
56
- ### Options
72
+ ## Options
57
73
 
58
74
  | flag | default | meaning |
59
75
  | --- | --- | --- |
60
76
  | `-o, --out <file>` | `<input>-ascii.mp4` | output path |
61
- | `--font-size <px>` | `20` | glyph size in output pixels; cells are `0.6 × fs` wide, `1.12 × fs` tall |
77
+ | `--font-size <px>` | `20` | glyph size in output pixels — cells are `0.6 × fs` wide, `1.12 × fs` tall |
62
78
  | `--cell-ratio <r>` | `1.12` | cell height ÷ font size |
63
- | `--weights <a,b,…>` | `400,500,600,700,800` | font weights available to the atlas |
64
- | `--weight-min / --weight-max` | `600` / `800` | weight range chosen from scene luminance |
65
- | `--stroke <px>` | `0.5` | extra outline that raises ink coverage |
66
- | `--calibration <n>` | `0.86` | ink-coverage calibration for the final composite |
79
+ | `--weights <a,b,…>` | `400,500,600,700,800` | font weights rasterised into the atlas |
80
+ | `--weight-min` / `--weight-max` | `600` / `800` | weight range picked from scene luminance |
81
+ | `--stroke <px>` | `0.5` | outline that raises ink coverage |
82
+ | `--calibration <n>` | `0.86` | ink-coverage calibration for the whole field |
83
+ | `--halo <n>` / `--halo-radius <px>` | `0` / `0.9 × cell width` | re-emit light the ink cannot carry (sun, specular paths, lamps) |
67
84
  | `--diffusion <n>` | `0.1` | base softening of ink into the gaps |
68
- | `--diffusion-adaptive <n>` | `0.25` | extra softening for bright cells (tames highlight clipping) |
69
- | `--detail <n>` | `0.14` | subcell luminance recovery |
70
- | `--color-detail <n>` | `0.1` | subcell colour recovery |
71
- | `--prose <file>` | bundled bank | `.txt` (sentences) or `.json` (array of sentences) |
85
+ | `--diffusion-adaptive <n>` | `0.25` | extra softening for bright cells |
86
+ | `--detail <n>` / `--color-detail <n>` | `0.14` / `0.1` | subcell luminance and colour recovery |
87
+ | `--prose <file>` | bundled bank | `.txt` sentences or a `.json` array |
72
88
  | `--crf <n>` / `--preset <p>` | `17` / `slow` | x264 quality and speed |
73
- | `--still <seconds>` | — | render a single PNG instead |
89
+ | `--still <seconds>` | — | render one PNG instead of a video |
74
90
  | `--limit <n>` | — | stop after n frames |
75
- | `--info` | — | print grid, cell size and measured coverage |
91
+ | `--info` | — | print grid, cell size and measured coverage, then exit |
76
92
 
77
- ## The bundled look
93
+ ## How it works
78
94
 
79
- `assets/prose.json` holds 1,844 deterministic sentences about night highways, sodium light, lane dashes and distant towers. It is the house voice for the demo — pass `--prose` to write your own. Any lowercase English works; longer sentences read better at small font sizes.
95
+ **A glyph inks only part of its cell, so the colour is divided by that part.** Drawing the sampled source colour into a
96
+ letter would multiply the scene by the ink fraction — the classic dark wall of text. Instead the atlas is measured: every
97
+ character at every weight is rasterised through Skia at 4× and box-averaged down to the cell pitch, and its real coverage
98
+ is stored. The renderer then emits
80
99
 
81
- ![prose detail](https://raw.githubusercontent.com/sloemo01/asciianything/main/assets/detail.png)
100
+ ```text
101
+ emitted = scene / (coverage × calibration)
102
+ ```
82
103
 
83
- ## Demo
104
+ so individual letters are brighter than their source cell and the integrated cell — ink plus the black gaps — lands back
105
+ on the sampled colour.
84
106
 
85
- `assets/demo.mp4` — 1440×2560, 29.97 fps, 19.42 s, rendered from a night freeway clip at `--font-size 20` (120 × 117 cells, 14,040 characters). The MP4 was produced by this package's renderer and encoder path.
107
+ **Spaces carry no ink, and they are about a fifth of the field.** Coverage compensation alone still leaves the field mean
108
+ at `p_ink × scene`, because spaces draw nothing. `--calibration` is therefore set near the measured non-space fraction
109
+ (sunset: `0.8025`, night highway: `0.76–0.86`), which restores the field mean. Flat-field tests confirm the arithmetic: at
110
+ calibration `1.0` a flat midtone renders at `p_ink` of its source, and the ratio rises as `1/calibration` predicts.
86
111
 
87
- ## How it is built
112
+ **Highlights have a ceiling, and the halo pays for it.** Coverage caps how bright one cell can get, so a sun disc or a
113
+ specular path physically cannot be reproduced by ink alone. `--halo` measures what each cell's ink actually delivers and
114
+ re-emits the shortfall as a bloom shaped by the glyph itself; cells that already match contribute nothing, so smooth sky,
115
+ sand and rock are untouched. It is off by default.
88
116
 
89
- - `src/atlas.js` rasterises every character at every weight through Skia (`@napi-rs/canvas`), supersampled 4× and box-averaged down to the cell pitch, then measures the real ink coverage of each tile.
90
- - `src/render.js` samples the frame, computes per-cell linear colour and luminance, picks a weight, divides by measured coverage, and composites the ink into an RGB buffer.
91
- - `src/prose.js` builds the fixed character field from the sentence bank.
92
- - `src/cli.js` pipes raw frames from `ffmpeg` into the renderer and rendered frames straight into `libx264`, so no intermediate frame sequence ever touches disk.
117
+ **Prose stays still.** Rows are shuffled deterministically from the sentence bank, every row starts at a deterministic
118
+ offset, and the character grid is frozen for the whole clip. Motion in the output is the source's motion, sampled — never
119
+ reshuffled text.
93
120
 
94
- Rendering is deterministic: same input frame and settings produce the same output frame.
121
+ ## Verification
122
+
123
+ Measured against the sources after encoding, in linear light, one-cell blur:
124
+
125
+ | clip | grid | whole-frame luminance | settings |
126
+ | --- | --- | --- | --- |
127
+ | night highway · 1920×1080 | 320 × 99 (31,680) | +2.5 % | `--font-size 10 --calibration 0.76 --halo 1.2 --halo-radius 8` |
128
+ | night highway · 1440×2560 | 120 × 117 (14,040) | +0.5 … +7 % | `--font-size 20`, six sampled times |
129
+ | sunset · 3840×2160·60 | 384 × 114 (43,776) | +0.9 % | `--font-size 17 --calibration 0.8025 --halo 1.7 --halo-radius 14` |
130
+
131
+ Frame counts, durations, dimensions and colour tags are probed independently after encoding. The sunset run additionally
132
+ holds every measured region — top sky, cirrus, cloud fan, sea band, wet sand, rocks — inside ±1.6 %.
133
+
134
+ ## Demos
135
+
136
+ The three clips above are rendered with this package:
137
+
138
+ ```bash
139
+ # 1920x1080, cropped from a 1440x2560 source, 582 frames at 29.97 fps
140
+ npx asciianything night-1080p.mp4 --font-size 10 --calibration 0.76 \
141
+ --halo 1.2 --halo-radius 8 -o night-highway-1080p.mp4
142
+
143
+ # 3840x2160 at 60 fps, 694 frames, custom 256-sentence sunset bank
144
+ npx asciianything sunset.mp4 --font-size 17 --calibration 0.8025 \
145
+ --halo 1.7 --halo-radius 14 --prose sunset-prose.txt -o sunset-ascii.mp4
146
+ ```
147
+
148
+ The bundled bank (`assets/prose.json`) holds 1,844 deterministic sentences about night highways, sodium light and distant
149
+ towers — the house voice for the night-highway clips. Any lowercase English works; longer sentences read better at small
150
+ font sizes.
95
151
 
96
152
  ## Limits
97
153
 
98
- Large glyphs mean fewer cells, and a cell can only average as bright as its ink coverage allows. Very bright, very large highlights (a full-white sky at `--font-size 32`) will top out below the source; lowering `--calibration` brightens the whole mosaic, raising it darkens it. Small font sizes preserve more detail and read as a finer texture.
154
+ Large glyphs mean fewer cells, and one cell can only average as bright as its ink coverage allows. A very large, very
155
+ bright highlight will top out below the source unless `--halo` makes up the difference. Lowering `--calibration` brightens
156
+ the whole field; raising it darkens it. Small font sizes preserve more detail and read as a finer texture; large ones read
157
+ as a poster and are legible from across a room.
99
158
 
100
159
  ## Credits
101
160
 
102
- Demo footage: [Night Traffic Flow in Urban Highway View](https://www.pexels.com/video/night-traffic-flow-in-urban-highway-view-35835506/) by [Sururi Ballıdağ](https://www.pexels.com/@drector/), used under the [Pexels license](https://www.pexels.com/license/). `assets/demo.mp4` is the ASCII derivative of that clip.
161
+ Night highway footage: [Night Traffic Flow in Urban Highway View](https://www.pexels.com/video/night-traffic-flow-in-urban-highway-view-35835506/)
162
+ by [Sururi Ballıdağ](https://www.pexels.com/@drector/), used under the [Pexels license](https://www.pexels.com/license/).
163
+
164
+ Sunset footage: Pexels clip `12556562`, used under the [Pexels license](https://www.pexels.com/license/).
165
+
166
+ Bundled typeface: [JetBrains Mono](https://github.com/JetBrains/JetBrainsMono), SIL OFL 1.1 — see `assets/OFL.txt`.
103
167
 
104
168
  ## Licence
105
169
 
106
- MIT for the code. The bundled JetBrains Mono font is SIL OFL 1.1 — see `assets/OFL.txt`.
170
+ MIT for the code.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "asciianything",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Turn any video into a mosaic of coloured lowercase prose, frame-accurate, with ffmpeg.",
5
5
  "keywords": [
6
6
  "ascii",
package/src/cli.js CHANGED
@@ -25,6 +25,8 @@ options:
25
25
  --calibration <n> ink-coverage calibration (default 0.86)
26
26
  --diffusion <n> base ink softening, 0..1 (default 0.1)
27
27
  --diffusion-adaptive extra softening for bright cells (default 0.25)
28
+ --halo <n> highlight halo for light the ink cannot carry (default 0)
29
+ --halo-radius <px> halo blur radius (default 0.9 x cell width)
28
30
  --detail <n> subcell luminance detail recovery (default 0.14)
29
31
  --color-detail <n> subcell colour recovery (default 0.1)
30
32
  --prose <file> sentence bank (.txt or .json) instead of the bundled one
@@ -129,6 +131,8 @@ async function main() {
129
131
  calibration: { type: 'string' },
130
132
  diffusion: { type: 'string' },
131
133
  'diffusion-adaptive': { type: 'string' },
134
+ halo: { type: 'string' },
135
+ 'halo-radius': { type: 'string' },
132
136
  detail: { type: 'string' },
133
137
  'color-detail': { type: 'string' },
134
138
  prose: { type: 'string' },
@@ -174,6 +178,8 @@ async function main() {
174
178
  calibration: num(values.calibration, 0.86),
175
179
  diffusion: num(values.diffusion, 0.1),
176
180
  diffusionAdaptive: num(values['diffusion-adaptive'], 0.25),
181
+ halo: num(values.halo, 0),
182
+ haloRadius: values['halo-radius'] === undefined ? undefined : num(values['halo-radius'], 0),
177
183
  detailLuma: num(values.detail, 0.14),
178
184
  detailColor: num(values['color-detail'], 0.1),
179
185
  prosePath: values.prose,
package/src/render.js CHANGED
@@ -156,6 +156,54 @@ class Renderer {
156
156
 
157
157
  this.rgb = new Uint8Array(this.width * this.height * 3);
158
158
  this.alpha = new Float32Array(this.width * this.height);
159
+
160
+ // highlight halo: light the ink cannot carry is re-emitted as a local
161
+ // glow shaped by the glyph itself, so a sun or a specular path keeps the
162
+ // energy the source has instead of topping out at the coverage ceiling.
163
+ this.halo = cfg.halo === undefined ? 0 : cfg.halo;
164
+ this.haloRadius = Math.max(
165
+ 1,
166
+ Math.round(cfg.haloRadius === undefined ? this.cellw * 0.9 : cfg.haloRadius)
167
+ );
168
+ this.haloGain = new Float32Array(this.cells * 3);
169
+ this.sumR = new Float32Array(this.cells);
170
+ this.sumG = new Float32Array(this.cells);
171
+ this.sumB = new Float32Array(this.cells);
172
+ this.count = new Uint16Array(this.cells);
173
+ this.glowBuf = this.halo > 0 ? new Float32Array(this.width * this.height * 3) : null;
174
+ if (this.halo > 0) this._buildGlowTiles();
175
+ }
176
+
177
+ /** Pre-blurred glyph tiles, used to scatter each clipped cell's lost light. */
178
+ _buildGlowTiles() {
179
+ const R = this.haloRadius;
180
+ const gw = this.cellw + 2 * R;
181
+ const gh = this.cellh + 2 * R;
182
+ const n = this.atlas.weights.length * this.atlas.chars.length;
183
+ this.glowTiles = new Float32Array(n * gw * gh);
184
+ this.glowW = gw;
185
+ this.glowH = gh;
186
+
187
+ const tile = new Float32Array(gw * gh);
188
+ const tmp = new Float32Array(gw * gh);
189
+ const atile = this.atlas;
190
+ for (let wi = 0; wi < atile.weights.length; wi++) {
191
+ for (let ci = 0; ci < atile.chars.length; ci++) {
192
+ const src = (wi * atile.chars.length + ci) * atile.area;
193
+ tile.fill(0);
194
+ if (atile.chars[ci] !== ' ') {
195
+ for (let y = 0; y < this.cellh; y++) {
196
+ const srow = src + (atile.pad + y) * atile.tilew + atile.pad;
197
+ const drow = (y + R) * gw + R;
198
+ for (let x = 0; x < this.cellw; x++) tile[drow + x] = atile.alpha[srow + x];
199
+ }
200
+ }
201
+ // two separable box passes -> soft bloom with the cell's own shape
202
+ boxBlurR(tile, tmp, gw, gh, Math.max(1, R >> 1));
203
+ boxBlurR(tmp, tile, gw, gh, Math.max(1, R >> 1));
204
+ this.glowTiles.set(tile, (wi * atile.chars.length + ci) * gw * gh);
205
+ }
206
+ }
159
207
  }
160
208
 
161
209
  stats() {
@@ -292,9 +340,16 @@ class Renderer {
292
340
  }
293
341
 
294
342
  this.wgrid[c] = wi;
295
- this.colors[c * 3] = Math.min(gr, maxEmitted);
296
- this.colors[c * 3 + 1] = Math.min(gg, maxEmitted);
297
- this.colors[c * 3 + 2] = Math.min(gb, maxEmitted);
343
+ // a space carries no ink: emit nothing rather than dividing by the floor
344
+ if (ch === 0) {
345
+ this.colors[c * 3] = 0;
346
+ this.colors[c * 3 + 1] = 0;
347
+ this.colors[c * 3 + 2] = 0;
348
+ } else {
349
+ this.colors[c * 3] = Math.min(gr, maxEmitted);
350
+ this.colors[c * 3 + 1] = Math.min(gg, maxEmitted);
351
+ this.colors[c * 3 + 2] = Math.min(gb, maxEmitted);
352
+ }
298
353
  }
299
354
  }
300
355
 
@@ -325,6 +380,9 @@ class Renderer {
325
380
  }
326
381
  }
327
382
 
383
+ let glow = null;
384
+ if (this.halo > 0) glow = this._halo();
385
+
328
386
  for (let i = 0; i < px; i++) {
329
387
  const cell = cellId[i];
330
388
  const ink = a[i];
@@ -334,6 +392,12 @@ class Renderer {
334
392
  let r = ink * cr;
335
393
  let g = ink * cg;
336
394
  let b = ink * cb;
395
+ if (glow) {
396
+ const o3 = i * 3;
397
+ r += glow[o3];
398
+ g += glow[o3 + 1];
399
+ b += glow[o3 + 2];
400
+ }
337
401
  const y = LUMA_R * r + LUMA_G * g + LUMA_B * b;
338
402
  r = y + (r - y) * sat;
339
403
  g = y + (g - y) * sat;
@@ -348,6 +412,84 @@ class Renderer {
348
412
  }
349
413
  return out;
350
414
  }
415
+
416
+ /**
417
+ * Measure what each cell's ink actually delivers, and re-emit the shortfall
418
+ * as a bloom shaped by the glyph. Only cells whose source light exceeds what
419
+ * their coverage can carry contribute, so smooth sky is untouched.
420
+ */
421
+ _halo() {
422
+ const { cols, rows, cellw, cellh, cellId, colors, chars, wgrid } = this;
423
+ const a = this.alpha;
424
+ const px = this.width * this.height;
425
+ const sumR = this.sumR, sumG = this.sumG, sumB = this.sumB, count = this.count;
426
+ const gains = this.haloGain;
427
+ sumR.fill(0); sumG.fill(0); sumB.fill(0); count.fill(0);
428
+
429
+ for (let i = 0; i < px; i++) {
430
+ const c = cellId[i];
431
+ const ink = a[i];
432
+ sumR[c] += Math.min(ink * colors[c * 3], 1);
433
+ sumG[c] += Math.min(ink * colors[c * 3 + 1], 1);
434
+ sumB[c] += Math.min(ink * colors[c * 3 + 2], 1);
435
+ count[c]++;
436
+ }
437
+
438
+ const calib = this.calibration;
439
+ const nw = this.atlas.chars.length;
440
+ const cov = this.atlas.coverage;
441
+ let any = false;
442
+ for (let c = 0; c < this.cells; c++) {
443
+ const n = count[c] || 1;
444
+ if (chars[c] === 0) {
445
+ gains[c * 3] = 0; gains[c * 3 + 1] = 0; gains[c * 3 + 2] = 0;
446
+ continue;
447
+ }
448
+ const C = Math.max(cov[wgrid[c] * nw + chars[c]], 0.02);
449
+ const wantR = this.sceneRGB[c * 3] / calib;
450
+ const wantG = this.sceneRGB[c * 3 + 1] / calib;
451
+ const wantB = this.sceneRGB[c * 3 + 2] / calib;
452
+ const defR = wantR - sumR[c] / n;
453
+ const defG = wantG - sumG[c] / n;
454
+ const defB = wantB - sumB[c] / n;
455
+ const gR = defR > 0 ? (this.halo * defR) / C : 0;
456
+ const gG = defG > 0 ? (this.halo * defG) / C : 0;
457
+ const gB = defB > 0 ? (this.halo * defB) / C : 0;
458
+ gains[c * 3] = gR; gains[c * 3 + 1] = gG; gains[c * 3 + 2] = gB;
459
+ if (gR > 0 || gG > 0 || gB > 0) any = true;
460
+ }
461
+
462
+ const glow = this.glowBuf;
463
+ glow.fill(0);
464
+ if (!any) return glow;
465
+
466
+ const gw = this.glowW, gh = this.glowH, R = this.haloRadius, W = this.width, H = this.height;
467
+ for (let c = 0; c < this.cells; c++) {
468
+ const gR = gains[c * 3], gG = gains[c * 3 + 1], gB = gains[c * 3 + 2];
469
+ if (gR === 0 && gG === 0 && gB === 0) continue;
470
+ const tile = this.glowTiles.subarray(
471
+ (wgrid[c] * nw + chars[c]) * gw * gh,
472
+ (wgrid[c] * nw + chars[c] + 1) * gw * gh
473
+ );
474
+ const cx = (c % cols) * cellw - R;
475
+ const cy = ((c / cols) | 0) * cellh - R;
476
+ const y0 = Math.max(0, -cy), y1 = Math.min(gh, H - cy);
477
+ const x0 = Math.max(0, -cx), x1 = Math.min(gw, W - cx);
478
+ for (let y = y0; y < y1; y++) {
479
+ const row = (cy + y) * W;
480
+ const trow = y * gw;
481
+ for (let x = x0; x < x1; x++) {
482
+ const t = tile[trow + x];
483
+ if (t === 0) continue;
484
+ const o = (row + cx + x) * 3;
485
+ glow[o] += gR * t;
486
+ glow[o + 1] += gG * t;
487
+ glow[o + 2] += gB * t;
488
+ }
489
+ }
490
+ }
491
+ return glow;
492
+ }
351
493
  }
352
494
 
353
495
  function buildOutputLUT(n = 4096) {
@@ -360,6 +502,38 @@ function buildOutputLUT(n = 4096) {
360
502
  return lut;
361
503
  }
362
504
 
505
+ /** separable box blur of radius r, energy preserving with edge clamping */
506
+ function boxBlurR(src, dst, width, height, r) {
507
+ if (r < 1) {
508
+ dst.set(src);
509
+ return dst;
510
+ }
511
+ const norm = 1 / (2 * r + 1);
512
+ const tmp = new Float32Array(src.length);
513
+ for (let y = 0; y < height; y++) {
514
+ const row = y * width;
515
+ let sum = 0;
516
+ for (let k = -r; k <= r; k++) sum += src[row + Math.min(width - 1, Math.max(0, k))];
517
+ for (let x = 0; x < width; x++) {
518
+ tmp[row + x] = sum * norm;
519
+ const add = Math.min(width - 1, x + r + 1);
520
+ const sub = Math.max(0, x - r);
521
+ sum += src[row + add] - src[row + sub];
522
+ }
523
+ }
524
+ for (let x = 0; x < width; x++) {
525
+ let sum = 0;
526
+ for (let k = -r; k <= r; k++) sum += tmp[Math.min(height - 1, Math.max(0, k)) * width + x];
527
+ for (let y = 0; y < height; y++) {
528
+ dst[y * width + x] = sum * norm;
529
+ const add = Math.min(height - 1, y + r + 1);
530
+ const sub = Math.max(0, y - r);
531
+ sum += tmp[add * width + x] - tmp[sub * width + x];
532
+ }
533
+ }
534
+ return dst;
535
+ }
536
+
363
537
  /** separable 1-2-1 blur over a float image, energy preserving */
364
538
  function box3(src, width, height, dst) {
365
539
  const out = dst || new Float32Array(src.length);