apertaluce 0.1.0 → 0.3.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,90 +1,141 @@
1
- # apertaluce
1
+ # /apertaluce/
2
2
 
3
- Keyed, invisible marks for pictures, sound and video that survive re-encoding and editing.
4
- Pure TypeScript, no dependencies; runs in Node, Bun, Deno, browsers and edge runtimes.
3
+ <p align="center">
4
+ <a href="https://www.npmjs.com/package/apertaluce"><img src="https://img.shields.io/npm/v/apertaluce?style=flat-square&label=npm&color=111111" alt="npm" /></a>
5
+ <a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-111111?style=flat-square" alt="MIT" /></a>
6
+ <a href="https://nodejs.org"><img src="https://img.shields.io/badge/node-20+-111111?style=flat-square" alt="node 20+" /></a>
7
+ <a href="package.json"><img src="https://img.shields.io/badge/dependencies-0-111111?style=flat-square" alt="0 dependencies" /></a>
8
+ </p>
5
9
 
6
- A mark says one thing: this content was marked with this key. It carries no payload. Reading
7
- or forging it needs the key. The library works on raw pixels and samples and never touches a
8
- codec: decoding and encoding stay with the host.
10
+ ---
11
+
12
+ ```
13
+ .------------------------------------------------------------------------.
14
+ | .--. __ __ .--. |
15
+ | | | ____ _____ ___ _____/ /_____ _/ /_ __________ | | |
16
+ | '--' / __ `/ __ \/ _ \/ ___/ __/ __ `/ / / / / ___/ _ \ '--' |
17
+ | / /_/ / /_/ / __/ / / /_/ /_/ / / /_/ / /__/ __/ |
18
+ | [+] \__,_/ .___/\___/_/ \__/\__,_/_/\__,_/\___/\___/ [+] |
19
+ | /_/ |
20
+ | .--. .--. |
21
+ | | | I n v i s i b l e M a r k s | | |
22
+ | '--' '--' |
23
+ '------------------------------------------------------------------------'
24
+ ```
25
+
26
+ <p align="center">
27
+ <a href="docs/MEASURED.md">measured</a>
28
+ ·
29
+ <a href="https://www.npmjs.com/package/apertaluce">npm</a>
30
+ </p>
31
+
32
+ Invisible marks for pictures, sound and video. Keyed, survive re-encoding and editing. No dependencies, no codecs, runs anywhere JavaScript does.
33
+
34
+ ## Install
35
+
36
+ ```bash
37
+ npm install apertaluce
38
+ ```
39
+
40
+ Node 20+, Bun, Deno, browsers, edge runtimes. ESM with types.
9
41
 
10
42
  ```ts
11
- import { derivePixelKey, embedPixelMark, detectPixelMark } from "apertaluce/image";
12
- import { deriveAudioKey, createAudioMarker, embedAudioMark, detectAudioMark } from "apertaluce/audio";
13
- import { frameMarkOverlayPgm, detectFrameMark } from "apertaluce/video";
43
+ import { derivePixelKey, embedPixelMark, detectPixelMark } from 'apertaluce/image';
44
+ import { deriveAudioKey, embedAudioMark, detectAudioMark } from 'apertaluce/audio';
45
+ import { frameMarkOverlayPgm, detectFrameMark } from 'apertaluce/video';
14
46
 
15
47
  const pixels = await derivePixelKey(secret);
16
- embedPixelMark(rgba, pixels); // in place
17
- detectPixelMark(suspectRgba, pixels).detected;
48
+ embedPixelMark(rgba, pixels); // in place
49
+ detectPixelMark(suspect, pixels).detected;
18
50
 
19
- const sounds = await deriveAudioKey(secret);
20
- embedAudioMark([left, right], 44_100, sounds); // in place; or createAudioMarker() for a stream
21
- detectAudioMark(suspectSamples, 48_000, sounds).detected;
51
+ const sound = await deriveAudioKey(secret);
52
+ embedAudioMark([left, right], 44_100, sound); // in place
53
+ detectAudioMark(samples, 48_000, sound).detected;
22
54
 
23
- // Video: blend one grey plane onto the luma of every frame (ffmpeg: blend=all_mode=grainmerge),
24
- // then read a dozen frames of the suspect.
25
- const overlay = frameMarkOverlayPgm(1280, 720, pixels);
26
- detectFrameMark(frames, pixels).detected;
55
+ const overlay = frameMarkOverlayPgm(1280, 720, pixels); // blend onto every frame's luma
56
+ detectFrameMark(frames, pixels).detected; // a dozen frames of the suspect
27
57
  ```
28
58
 
29
- ## How it works
59
+ A mark says one thing: this was marked with this key. No payload. Reading or forging it needs the key. Decoding and encoding stay with the host: the library takes pixels and samples.
60
+
61
+ ## Marks
62
+
63
+ | Mark | |
64
+ | ------- | -------------------------------------------------------------------------------------------- |
65
+ | Picture | Keyed tile: faint on flat colour, strong in texture and on strokes; crop, turn, screen capture |
66
+ | Video | The same tile on every frame; H.264/HEVC/VP9 re-encodes, reduction, crop, screen recording |
67
+ | Sound | Keyed sequence under the sound; MP3/AAC/Opus, resampling, trimming, noise, mixing, echo |
68
+ | Sound | Keyed balance of neighbouring bands; another tempo, another speed (partly, see Limits) |
69
+
70
+ ## API
30
71
 
31
- **Pictures.** One keyed, band-limited noise tile is repeated across the picture and added to
32
- its luminance. The detector folds the suspect at a candidate tile period and correlates the
33
- fold with the key tile over every shift at once, so scale is the period and a crop is a
34
- shift. A few sinusoids in the tile show up as sharp points in the suspect's spectrum; from
35
- them the detector reads rotation, mirroring and stretch.
72
+ `apertaluce/image`:
36
73
 
37
- **Video.** The same tile at the same place in every frame. The mean of several frames keeps
38
- the mark and blurs the moving picture away.
74
+ | Function | |
75
+ | ----------------------------------------------- | ------------------------------------------------------ |
76
+ | `derivePixelKey(secret, id?)` | Key from a secret of 128 bits or more |
77
+ | `embedPixelMark(image, key, options?)` | Marks RGBA pixels in place; returns the PSNR |
78
+ | `detectPixelMark(image, key, options?)` | `detected`, `score`, rotation, mirroring |
79
+ | `pixelMarkPlane(width, height, key, tiles?)` | The mark alone, for pipelines that add it themselves |
80
+ | `pictureFingerprint(image)` | 256-bit perceptual fingerprint, 64 hex digits |
81
+ | `fingerprintDistance(a, b)` | Bits two fingerprints differ in |
39
82
 
40
- **Sound.** Two layers. A keyed noise sequence, repeated every two seconds, is added under the
41
- sound and shaped to follow its spectrum; the detector divides the sound by its own smoothed
42
- spectrum, folds at the period and correlates. A second layer nudges the level difference of
43
- neighbouring bands onto a keyed grid; it is read from copies whose waveform no longer lines
44
- up (another tempo, another speed).
83
+ `apertaluce/audio`:
84
+
85
+ | Function | |
86
+ | ----------------------------------------------- | ------------------------------------------------------ |
87
+ | `deriveAudioKey(secret, id?)` | Key from a secret of 128 bits or more |
88
+ | `embedAudioMark(channels, rate, key, options?)` | Marks Float32 samples in place; returns sound over mark |
89
+ | `createAudioMarker(key, rate, options?)` | Streaming: `process(chunk)`, `flush()`, `delay` |
90
+ | `detectAudioMark(channels, rate, key, options?)`| `detected`, `score`, `layer`, tempo and pitch |
91
+ | `resample(samples, from, to)` | Windowed-sinc rate conversion |
92
+
93
+ `apertaluce/video`:
94
+
95
+ | Function | |
96
+ | ----------------------------------------------- | ------------------------------------------------------ |
97
+ | `frameMarkOverlay(width, height, key, options?)`| Grey plane, 128 = leave the pixel |
98
+ | `frameMarkOverlayPgm(...)` | The same as a PGM file for ffmpeg |
99
+ | `detectFrameMark(frames, key, options?)` | Reads the mean of the frames, then single frames |
100
+
101
+ ```bash
102
+ # One overlay for the whole video; only the luma plane meets it.
103
+ ffmpeg -i in.mp4 -loop 1 -i overlay.pgm -filter_complex \
104
+ "[0:v]format=yuv420p,extractplanes=y+u+v[y][u][v];[1:v]format=gray[o];[y][o]blend=all_mode=grainmerge:shortest=1[m];[m][u][v]mergeplanes=0x001020:yuv420p" out.mp4
105
+ ```
106
+
107
+ Options: `strength` (picture, sound), `balance: false` (sound: skip the second layer and its 0.2 s delay), `search: false` (sound: skip the tempo and pitch search), `amplitude` and `tilesPerShortSide` (video).
45
108
 
46
109
  ## Measured
47
110
 
48
- Benches are in `bench/` and go through the ffmpeg command line. Scores are in standard
49
- deviations of the detector's own correlation surface; "found" is the count over the cut.
50
-
51
- **Sound, sequence layer** (`bench/audio.ts`, 2026-12-01: four 20-second pieces of recorded
52
- speech, 44.1 kHz; sound over mark 30.8 dB, median). Found in 4 of 4 after each of: 16-bit
53
- WAV; MP3 128, 64 and 32 kbit/s; AAC 96 and 48; Opus 64, 24 and 12; Vorbis q2; MP3 → AAC →
54
- MP3; resampling to 48, 22.05, 16 and 8 kHz; telephone band; 20 dB quieter; clipped; noise
55
- 30, 20 and 10 dB under the sound; another recording mixed in at the same level and 10 dB
56
- louder; excerpts of 10, 5, 3, 2 and 1 second (the 1-second excerpts scored 6.8 at the
57
- lowest, against a cut of 6.5); bass and treble; echo; dynamic compression; band-limited with
58
- echo, noise and MP3 64. Unmarked pieces and marked pieces read with another key: 0 of 12,
59
- highest score 4.65.
60
-
61
- **Sound, band balance** (same bench, three pieces of 20 seconds, both layers on; sound over
62
- mark 27.8 dB): 10% faster with pitch kept, 3 of 3; 5% faster (resampled), 3 of 3. Not
63
- found: 1% faster, 25% faster, one semitone higher with tempo kept (0 of 3 each). Unmarked
64
- or another key: highest 5.02 against a cut of 8.5.
65
-
66
- **Video** (`bench/video.ts`, 2026-12-01: ten 8-second clips of 800×500, overlay amplitude 3,
67
- first encode H.264 CRF 23, twelve frames read). Found in 10 of 10 after each of: H.264 CRF
68
- 28, 33 and 38; 500 kbit/s; 480p, 360p and 240p; HEVC CRF 30; VP9 CRF 36; 12 frames per
69
- second; 1.25× speed; crop to 70%; a quarter of the frame; mirrored; turned a quarter; turned
70
- 7°; a 0.6× screen recording inside a desktop; brighter with more contrast; 360p at 300
71
- kbit/s. At amplitude 2 the quarter-frame crop was found in 9 of 10, the rest in 10 of 10.
72
- Unmarked clips and another key: 0 of 30, highest score 5.64 against a cut of 7.5.
73
-
74
- **Pictures**: measured in the application this code came from, on 30 flat and 30 textured
75
- pictures; the bench has not moved here yet.
111
+ Full tables in [docs/MEASURED.md](docs/MEASURED.md). Found / tried, 2026-12-01:
112
+
113
+ | Mark | Hosts | Held | Unmarked |
114
+ | ---------------- | ----------------- | -------------------------------------------------------------- | -------- |
115
+ | Picture | 29 pictures | JPEG 50, half size, crop, turn, mirror, screen capture to 0.5 | 0/2,174 |
116
+ | Video | 10 clips | CRF 38, 240p, HEVC, VP9, quarter crop, screen recording | 0/30 |
117
+ | Sound, sequence | 4 speech pieces | MP3 32, Opus 12, telephone band, 1 s excerpt, noise at 10 dB | 0/12 |
118
+ | Sound, balance | 3 speech pieces | 10% faster with pitch kept, 5% faster | 0/9 |
76
119
 
77
120
  ## Limits
78
121
 
79
- - Nobody has listened to marked sound or watched marked video under controlled conditions:
80
- the levels come from masking rules and the numbers above, not from a listening test.
81
- - Sound whose speed changed by 1%, by 25%, or whose pitch alone changed is not recognised.
82
- - The sound bench covers speech; music has not been measured.
83
- - A sound shorter than about a second, or a video frame smaller than about 200 pixels, holds
84
- too little of a mark to read.
122
+ - Nobody has listened to marked sound or watched marked video under controlled conditions. The levels come from masking rules and the numbers above.
123
+ - Sound 1% faster, 25% faster, or shifted in pitch alone is not recognised.
124
+ - The sound bench covers speech. Music has not been measured.
125
+ - Under about a second of sound there is too little mark to read.
126
+ - The picture mark is faint on purpose: flat colour gets single levels of grain. A thumbnail of 400 px or less, a reduction to 0.4 with JPEG on top, or a flat drawing reduced and saved as WebP is not recognised. Whole pictures made small are what the fingerprint is for.
85
127
  - Whoever holds the key can mark anything. The mark proves the key was used, not who used it.
86
128
 
87
- ## Releasing
129
+ ## Develop
130
+
131
+ ```bash
132
+ npm run tsc && npm test
133
+ node bench/audio.ts --dir <folder of audio or video> # needs ffmpeg
134
+ node bench/video.ts --dir <folder of videos>
135
+ ```
136
+
137
+ Release: `gh workflow run release.yml -R lofcz/apertaluce -f release_type=minor`. First publish of a new package name: `npm run bootstrap:publish` (npm login, publish, trusted publisher).
138
+
139
+ ## License
88
140
 
89
- `gh workflow run release.yml -R lofcz/apertaluce -f release_type=patch`. The first publish of
90
- the package name and its trusted publisher: `npm run bootstrap:publish` (interactive).
141
+ MIT
@@ -16,6 +16,6 @@ export declare function pictureFingerprint(image: RgbaImage): string | null;
16
16
  export declare function fingerprintDistance(a: string, b: string): number;
17
17
  /**
18
18
  * Two pictures within this many bits are the same picture. Measured in
19
- * bench/image.ts --fingerprint.
19
+ * the application this came from, on 477 pictures: 2 of 113,526 pairs of different pictures within it.
20
20
  */
21
21
  export declare const FINGERPRINT_MATCH_BITS = 40;
@@ -88,6 +88,6 @@ export function fingerprintDistance(a, b) {
88
88
  }
89
89
  /**
90
90
  * Two pictures within this many bits are the same picture. Measured in
91
- * bench/image.ts --fingerprint.
91
+ * the application this came from, on 477 pictures: 2 of 113,526 pairs of different pictures within it.
92
92
  */
93
93
  export const FINGERPRINT_MATCH_BITS = 40;
@@ -4,13 +4,16 @@ export interface RgbaImage {
4
4
  height: number;
5
5
  }
6
6
  /** Bump when the tile layout changes; old keys keep detecting old pictures. */
7
- export declare const PIXEL_MARK_SCHEME = 1;
7
+ export declare const PIXEL_MARK_SCHEME = 3;
8
8
  /** Tiles across the picture's shorter side at embed time. */
9
9
  export declare const TILES_PER_SHORT_SIDE = 4;
10
10
  export interface PixelKey {
11
- /** Unit-variance tile, TILE×TILE. */
11
+ /** The detector's reference: the coarse and the fine tile added up, TILE×TILE. */
12
12
  tile: Float32Array;
13
- /** Tile frequencies (cycles per tile) of the synchronisation peaks. */
13
+ /** The coarse and the fine part, each of unit variance (the embedder scales them separately). */
14
+ coarse: Float32Array;
15
+ fine: Float32Array;
16
+ /** Tile frequencies (cycles per tile) of the synchronisation peaks: the coarse part's eight, then the fine part's. */
14
17
  sync: Array<[number, number]>;
15
18
  /** conj(FFT(tile)) restricted to the band, per fold size. */
16
19
  spectra: Map<number, Float64Array>;
@@ -55,11 +58,12 @@ export interface PixelMarkResult {
55
58
  * test looks at 9 shifts of a handful of hypotheses; the search looks at every
56
59
  * shift of the grids the synchronisation peaks propose, so its cut is higher.
57
60
  *
58
- * Measured with bench/image.ts --null-only on 802
59
- * unmarked pictures (as they are and inside a page, three keys, 3,204 scores):
60
- * no detection, highest score 6.61. An exponential fit to the top tenth of
61
- * those scores puts the chance of an unmarked picture passing the search cut
62
- * at about 3 in a million per key (an extrapolation, not a count).
61
+ * Measured (docs/MEASURED.md) on unmarked pictures, as they are and inside a
62
+ * page. Search: 2,174 scores, no detection, highest 6.33, 99 % under 5.91; an
63
+ * exponential fit to the top tenth puts the chance of passing 7.5 at about 2 in
64
+ * a million per picture and key. Aligned test alone: 1,610 scores, highest 4.68;
65
+ * the same fit gives about 5 in ten million for 6.5. Both are extrapolations,
66
+ * not counts.
63
67
  */
64
68
  export declare const ALIGNED_CUT = 6.5;
65
69
  export declare const SEARCH_CUT = 7.5;
@@ -82,13 +86,17 @@ interface FoldScratch {
82
86
  im: Float64Array;
83
87
  re2: Float64Array;
84
88
  im2: Float64Array;
89
+ sums: Float64Array;
90
+ energies: Float64Array;
91
+ polishRe: Float64Array;
92
+ polishIm: Float64Array;
85
93
  colBin: Int32Array;
86
94
  }
87
95
  declare function foldScratch(width: number): FoldScratch;
88
96
  export interface DetectOptions {
89
97
  /** Skip the scale search (the aligned test only). */
90
98
  alignedOnly?: boolean;
91
- /** Skip the last, slowest pass (the period scan for weak crops). */
99
+ /** No longer has an effect (the slow last pass it skipped is gone); kept so that callers need not change. */
92
100
  fast?: boolean;
93
101
  }
94
102
  /** Looks for the mark of `key` in `image`. */
@@ -103,6 +111,9 @@ interface PeakSearchResult {
103
111
  }
104
112
  interface Spectrum {
105
113
  n: number;
114
+ /** Mean and spread of a capped value of `peaks` where there is no mark (taken over the whole spectrum). */
115
+ peakMean: number;
116
+ peakSpread: number;
106
117
  /** Power of each bin over the mean power at its radius. */
107
118
  power: Float32Array;
108
119
  /** The same, each bin replaced by the highest of its 3×3 neighbours. */
@@ -116,7 +127,7 @@ interface GridCandidate {
116
127
  fu: Vec;
117
128
  fv: Vec;
118
129
  }
119
- /** Sum of the capped peak heights where the constellation falls for the tile axes (fu, fv). */
130
+ /** How far the constellation stands out for the tile axes (fu, fv); see {@link standOut}. */
120
131
  declare function constellationScore(spectrum: Spectrum, sync: Array<[number, number]>, fu: Vec, fv: Vec): number;
121
132
  /**
122
133
  * Every turned, scaled and mirrored square grid: the constellation's points are
@@ -138,7 +149,11 @@ declare function searchAnyGrids(spectrum: Spectrum, key: PixelKey, minPeriod: nu
138
149
  declare function fitGrid(spectrum: Spectrum, sync: Array<[number, number]>, start: GridCandidate): GridCandidate | null;
139
150
  /** Folds along a candidate grid and correlates with the key, the tile upright and turned by a half-turn. */
140
151
  declare function verifyGrid(w: Whitened, key: PixelKey, scratch: FoldScratch, grid: GridCandidate): PeakSearchResult;
141
- declare function searchByPeaks(w: Whitened, key: PixelKey, scratch: FoldScratch): PeakSearchResult | null;
152
+ declare function searchByPeaks(w: Whitened & {
153
+ factor: number;
154
+ }, key: PixelKey, scratch: FoldScratch, sharp?: Whitened & {
155
+ factor: number;
156
+ }): PeakSearchResult | null;
142
157
  /** For the robustness bench and tests only. */
143
158
  export declare const __internals: {
144
159
  searchByPeaks: typeof searchByPeaks;