apertaluce 0.2.0 → 0.3.1
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 +4 -3
- package/lib/image/mark.d.ts +18 -10
- package/lib/image/mark.js +351 -149
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -62,7 +62,7 @@ A mark says one thing: this was marked with this key. No payload. Reading or for
|
|
|
62
62
|
|
|
63
63
|
| Mark | |
|
|
64
64
|
| ------- | -------------------------------------------------------------------------------------------- |
|
|
65
|
-
| Picture | Keyed tile
|
|
65
|
+
| Picture | Keyed tile: faint on flat colour, strong in texture and on strokes; crop, turn, screen capture |
|
|
66
66
|
| Video | The same tile on every frame; H.264/HEVC/VP9 re-encodes, reduction, crop, screen recording |
|
|
67
67
|
| Sound | Keyed sequence under the sound; MP3/AAC/Opus, resampling, trimming, noise, mixing, echo |
|
|
68
68
|
| Sound | Keyed balance of neighbouring bands; another tempo, another speed (partly, see Limits) |
|
|
@@ -112,7 +112,7 @@ Full tables in [docs/MEASURED.md](docs/MEASURED.md). Found / tried, 2026-12-01:
|
|
|
112
112
|
|
|
113
113
|
| Mark | Hosts | Held | Unmarked |
|
|
114
114
|
| ---------------- | ----------------- | -------------------------------------------------------------- | -------- |
|
|
115
|
-
| Picture |
|
|
115
|
+
| Picture | 29 pictures | JPEG 50, half size, crop, turn, mirror, screen capture to 0.5 | 0/2,174 |
|
|
116
116
|
| Video | 10 clips | CRF 38, 240p, HEVC, VP9, quarter crop, screen recording | 0/30 |
|
|
117
117
|
| Sound, sequence | 4 speech pieces | MP3 32, Opus 12, telephone band, 1 s excerpt, noise at 10 dB | 0/12 |
|
|
118
118
|
| Sound, balance | 3 speech pieces | 10% faster with pitch kept, 5% faster | 0/9 |
|
|
@@ -122,7 +122,8 @@ Full tables in [docs/MEASURED.md](docs/MEASURED.md). Found / tried, 2026-12-01:
|
|
|
122
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
123
|
- Sound 1% faster, 25% faster, or shifted in pitch alone is not recognised.
|
|
124
124
|
- The sound bench covers speech. Music has not been measured.
|
|
125
|
-
- Under about a second of sound
|
|
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.
|
|
126
127
|
- Whoever holds the key can mark anything. The mark proves the key was used, not who used it.
|
|
127
128
|
|
|
128
129
|
## Develop
|
package/lib/image/mark.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ 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 =
|
|
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 {
|
|
@@ -13,7 +13,7 @@ export interface PixelKey {
|
|
|
13
13
|
/** The coarse and the fine part, each of unit variance (the embedder scales them separately). */
|
|
14
14
|
coarse: Float32Array;
|
|
15
15
|
fine: Float32Array;
|
|
16
|
-
/** Tile frequencies (cycles per tile) of the synchronisation peaks. */
|
|
16
|
+
/** Tile frequencies (cycles per tile) of the synchronisation peaks: the coarse part's eight, then the fine part's. */
|
|
17
17
|
sync: Array<[number, number]>;
|
|
18
18
|
/** conj(FFT(tile)) restricted to the band, per fold size. */
|
|
19
19
|
spectra: Map<number, Float64Array>;
|
|
@@ -58,14 +58,15 @@ export interface PixelMarkResult {
|
|
|
58
58
|
* test looks at 9 shifts of a handful of hypotheses; the search looks at every
|
|
59
59
|
* shift of the grids the synchronisation peaks propose, so its cut is higher.
|
|
60
60
|
*
|
|
61
|
-
* Measured (docs/MEASURED.md) on
|
|
62
|
-
*
|
|
63
|
-
* exponential fit to the top tenth
|
|
64
|
-
*
|
|
65
|
-
*
|
|
61
|
+
* Measured (docs/MEASURED.md) on unmarked pictures, as they are and inside a
|
|
62
|
+
* page. Search: 2,168 scores, no detection, highest 6.33, 99 % under 5.88; an
|
|
63
|
+
* exponential fit to the top tenth puts the chance of passing 7.5 at about 1 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.
|
|
66
67
|
*/
|
|
67
68
|
export declare const ALIGNED_CUT = 6.5;
|
|
68
|
-
export declare const SEARCH_CUT =
|
|
69
|
+
export declare const SEARCH_CUT = 7.5;
|
|
69
70
|
interface Whitened {
|
|
70
71
|
values: Float32Array;
|
|
71
72
|
width: number;
|
|
@@ -87,13 +88,15 @@ interface FoldScratch {
|
|
|
87
88
|
im2: Float64Array;
|
|
88
89
|
sums: Float64Array;
|
|
89
90
|
energies: Float64Array;
|
|
91
|
+
polishRe: Float64Array;
|
|
92
|
+
polishIm: Float64Array;
|
|
90
93
|
colBin: Int32Array;
|
|
91
94
|
}
|
|
92
95
|
declare function foldScratch(width: number): FoldScratch;
|
|
93
96
|
export interface DetectOptions {
|
|
94
97
|
/** Skip the scale search (the aligned test only). */
|
|
95
98
|
alignedOnly?: boolean;
|
|
96
|
-
/**
|
|
99
|
+
/** No longer has an effect (the slow last pass it skipped is gone); kept so that callers need not change. */
|
|
97
100
|
fast?: boolean;
|
|
98
101
|
}
|
|
99
102
|
/** Looks for the mark of `key` in `image`. */
|
|
@@ -108,6 +111,9 @@ interface PeakSearchResult {
|
|
|
108
111
|
}
|
|
109
112
|
interface Spectrum {
|
|
110
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;
|
|
111
117
|
/** Power of each bin over the mean power at its radius. */
|
|
112
118
|
power: Float32Array;
|
|
113
119
|
/** The same, each bin replaced by the highest of its 3×3 neighbours. */
|
|
@@ -121,7 +127,7 @@ interface GridCandidate {
|
|
|
121
127
|
fu: Vec;
|
|
122
128
|
fv: Vec;
|
|
123
129
|
}
|
|
124
|
-
/**
|
|
130
|
+
/** How far the constellation stands out for the tile axes (fu, fv); see {@link standOut}. */
|
|
125
131
|
declare function constellationScore(spectrum: Spectrum, sync: Array<[number, number]>, fu: Vec, fv: Vec): number;
|
|
126
132
|
/**
|
|
127
133
|
* Every turned, scaled and mirrored square grid: the constellation's points are
|
|
@@ -147,6 +153,8 @@ declare function searchByPeaks(w: Whitened & {
|
|
|
147
153
|
factor: number;
|
|
148
154
|
}, key: PixelKey, scratch: FoldScratch, sharp?: Whitened & {
|
|
149
155
|
factor: number;
|
|
156
|
+
}, middle?: Whitened & {
|
|
157
|
+
factor: number;
|
|
150
158
|
}): PeakSearchResult | null;
|
|
151
159
|
/** For the robustness bench and tests only. */
|
|
152
160
|
export declare const __internals: {
|
package/lib/image/mark.js
CHANGED
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
*/
|
|
24
24
|
import { fft2d, toComplex } from "../fft.js";
|
|
25
25
|
/** Bump when the tile layout changes; old keys keep detecting old pictures. */
|
|
26
|
-
export const PIXEL_MARK_SCHEME =
|
|
26
|
+
export const PIXEL_MARK_SCHEME = 3;
|
|
27
27
|
/** Canonical tile resolution (cells per side). */
|
|
28
28
|
const TILE = 128;
|
|
29
29
|
/** Tiles across the picture's shorter side at embed time. */
|
|
@@ -37,9 +37,20 @@ export const TILES_PER_SHORT_SIDE = 4;
|
|
|
37
37
|
const BAND_LO = 3;
|
|
38
38
|
const BAND_SPLIT = 26;
|
|
39
39
|
const BAND_HI = 48;
|
|
40
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* Synchronisation peaks: plain sinusoids in the tile, sharp points in a copy's spectrum that
|
|
42
|
+
* turn and scale with it. Each part of the mark has its own eight. Those of the coarse part
|
|
43
|
+
* survive a reduction to a third; they hold two thirds of that part's energy
|
|
44
|
+
* (`COARSE_SYNC` each, next to unit-variance noise), because a faint pattern is found by its
|
|
45
|
+
* peaks or not at all. Those of the fine part (`FINE_SYNC` each) sit at the grain of texture
|
|
46
|
+
* and are as strong as the texture lets the fine part be: a copy that is not much reduced is
|
|
47
|
+
* found by them even when the picture is texture all over.
|
|
48
|
+
*/
|
|
41
49
|
const SYNC_PEAKS = 8;
|
|
42
|
-
const
|
|
50
|
+
const COARSE_SYNC = 0.5;
|
|
51
|
+
const FINE_SYNC = 0.225;
|
|
52
|
+
const FINE_SYNC_RADIUS_MIN = 30;
|
|
53
|
+
const FINE_SYNC_RADIUS_MAX = 44;
|
|
43
54
|
/**
|
|
44
55
|
* Their radii in cycles per tile: low enough to survive a reduction to a third, high enough to stay
|
|
45
56
|
* clear of picture structure. Measured on textured illustrations (docs/MEASURED.md): at
|
|
@@ -123,30 +134,36 @@ export async function derivePixelKey(secret, id = "k1") {
|
|
|
123
134
|
// Synchronisation peaks: directions spread over the half-turn (the other half is their mirror
|
|
124
135
|
// image in a real picture's spectrum), every radius different, so one rotation and one scale
|
|
125
136
|
// fit the constellation, and its mirror image fits only a mirrored picture.
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
for (let
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
+
const constellation = (radiusMin, radiusMax) => {
|
|
138
|
+
const points = [];
|
|
139
|
+
for (let i = 0; i < SYNC_PEAKS; i++) {
|
|
140
|
+
for (let attempt = 0; attempt < 50; attempt++) {
|
|
141
|
+
const angle = ((i + 0.15 + 0.7 * random()) * Math.PI) / SYNC_PEAKS;
|
|
142
|
+
const radius = radiusMin + ((i * 5) % SYNC_PEAKS + random()) * ((radiusMax - radiusMin) / SYNC_PEAKS);
|
|
143
|
+
const u = Math.round(radius * Math.cos(angle));
|
|
144
|
+
const v = Math.round(radius * Math.sin(angle));
|
|
145
|
+
if (points.some(([pu, pv]) => (pu === u && pv === v) || (pu === -u && pv === -v) || Math.abs(Math.hypot(pu, pv) - Math.hypot(u, v)) < 0.35))
|
|
146
|
+
continue;
|
|
147
|
+
points.push([u, v]);
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
137
150
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
-
for (let
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
fine[y * TILE + x] += wave;
|
|
151
|
+
return points;
|
|
152
|
+
};
|
|
153
|
+
const addPeaks = (target, points, amplitude) => {
|
|
154
|
+
for (const [u, v] of points) {
|
|
155
|
+
const phase = 2 * Math.PI * random();
|
|
156
|
+
for (let y = 0; y < TILE; y++) {
|
|
157
|
+
for (let x = 0; x < TILE; x++)
|
|
158
|
+
target[y * TILE + x] += amplitude * Math.SQRT2 * Math.cos((2 * Math.PI * (u * x + v * y)) / TILE + phase);
|
|
147
159
|
}
|
|
148
160
|
}
|
|
149
|
-
}
|
|
161
|
+
};
|
|
162
|
+
const coarseSync = constellation(SYNC_RADIUS_MIN, SYNC_RADIUS_MAX);
|
|
163
|
+
const fineSync = constellation(FINE_SYNC_RADIUS_MIN, FINE_SYNC_RADIUS_MAX);
|
|
164
|
+
addPeaks(coarse, coarseSync, COARSE_SYNC);
|
|
165
|
+
addPeaks(fine, fineSync, FINE_SYNC);
|
|
166
|
+
const sync = [...coarseSync, ...fineSync];
|
|
150
167
|
unit(coarse);
|
|
151
168
|
unit(fine);
|
|
152
169
|
const tile = new Float32Array(TILE * TILE);
|
|
@@ -238,13 +255,22 @@ function boxBlur(src, width, height, radius) {
|
|
|
238
255
|
* neighbourhood, so flat ground beside a line, and the inside of a drawn
|
|
239
256
|
* shape, stay flat.
|
|
240
257
|
*/
|
|
241
|
-
const COARSE_FLOOR = 0.
|
|
258
|
+
const COARSE_FLOOR = 0.44;
|
|
242
259
|
const COARSE_SLOPE = 0.05;
|
|
243
260
|
const COARSE_CEIL = 0.8;
|
|
244
261
|
const FINE_SLOPE = 0.4;
|
|
245
262
|
const FINE_CEIL = 4;
|
|
246
263
|
/** Detail under this (grey levels) is sensor grain or compression residue, not texture. */
|
|
247
264
|
const DETAIL_FREE = 0.8;
|
|
265
|
+
/**
|
|
266
|
+
* On the stroke of a line or an edge itself (never beside it): a share of how far the pixels
|
|
267
|
+
* there differ from their surroundings, counted above `EDGE_FREE` grey levels and up to
|
|
268
|
+
* `EDGE_CEIL`. The coarse part gets `EDGE_COARSE` of it.
|
|
269
|
+
*/
|
|
270
|
+
const EDGE_SHARE = 0.12;
|
|
271
|
+
const EDGE_FREE = 6;
|
|
272
|
+
const EDGE_CEIL = 3;
|
|
273
|
+
const EDGE_COARSE = 0.8;
|
|
248
274
|
/** Lowest value within `radius` cells, separable. */
|
|
249
275
|
function minFilter(src, width, height, radius) {
|
|
250
276
|
const tmp = new Float32Array(src.length);
|
|
@@ -273,21 +299,70 @@ function minFilter(src, width, height, radius) {
|
|
|
273
299
|
}
|
|
274
300
|
return out;
|
|
275
301
|
}
|
|
302
|
+
/** 3×3 mean, separable; at the border, of the cells that exist. */
|
|
303
|
+
function blur3(src, width, height) {
|
|
304
|
+
const tmp = new Float32Array(src.length);
|
|
305
|
+
const out = new Float32Array(src.length);
|
|
306
|
+
const third = 1 / 3;
|
|
307
|
+
for (let y = 0; y < height; y++) {
|
|
308
|
+
const row = y * width;
|
|
309
|
+
if (width === 1) {
|
|
310
|
+
tmp[row] = src[row];
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
tmp[row] = (src[row] + src[row + 1]) * 0.5;
|
|
314
|
+
for (let i = row + 1, end = row + width - 1; i < end; i++)
|
|
315
|
+
tmp[i] = (src[i - 1] + src[i] + src[i + 1]) * third;
|
|
316
|
+
tmp[row + width - 1] = (src[row + width - 2] + src[row + width - 1]) * 0.5;
|
|
317
|
+
}
|
|
318
|
+
if (height === 1)
|
|
319
|
+
return tmp;
|
|
320
|
+
for (let x = 0; x < width; x++) {
|
|
321
|
+
out[x] = (tmp[x] + tmp[width + x]) * 0.5;
|
|
322
|
+
const last = (height - 1) * width + x;
|
|
323
|
+
out[last] = (tmp[last - width] + tmp[last]) * 0.5;
|
|
324
|
+
}
|
|
325
|
+
for (let y = 1; y < height - 1; y++) {
|
|
326
|
+
for (let i = y * width, end = i + width; i < end; i++)
|
|
327
|
+
out[i] = (tmp[i - width] + tmp[i] + tmp[i + width]) * third;
|
|
328
|
+
}
|
|
329
|
+
return out;
|
|
330
|
+
}
|
|
276
331
|
/**
|
|
277
|
-
* The amplitude
|
|
278
|
-
*
|
|
332
|
+
* The two amplitude maps on a grid of cells, `factor` pixels a side (the maps
|
|
333
|
+
* are smooth, or follow a stroke cell by cell; every pixel takes its cell's
|
|
334
|
+
* value). `strength` is folded in.
|
|
279
335
|
*/
|
|
280
|
-
function amplitudeMap(image, factor) {
|
|
336
|
+
function amplitudeMap(image, factor, strength) {
|
|
281
337
|
const { data, width, height } = image;
|
|
282
338
|
const mw = Math.ceil(width / factor);
|
|
283
339
|
const mh = Math.ceil(height / factor);
|
|
284
340
|
const mean = new Float32Array(mw * mh);
|
|
285
341
|
// Detail finer than a cell: mean absolute deviation inside it.
|
|
286
342
|
const fine = new Float32Array(mw * mh);
|
|
343
|
+
const fullW = Math.floor(width / factor);
|
|
344
|
+
const fullH = Math.floor(height / factor);
|
|
345
|
+
if (factor === 2) {
|
|
346
|
+
// The usual case (pictures of about a megapixel), unrolled.
|
|
347
|
+
const down = 4 * width;
|
|
348
|
+
for (let my = 0; my < fullH; my++) {
|
|
349
|
+
let p = 8 * my * width;
|
|
350
|
+
for (let i = my * mw, end = i + fullW; i < end; i++, p += 8) {
|
|
351
|
+
const a = 0.299 * data[p] + 0.587 * data[p + 1] + 0.114 * data[p + 2];
|
|
352
|
+
const b = 0.299 * data[p + 4] + 0.587 * data[p + 5] + 0.114 * data[p + 6];
|
|
353
|
+
const c = 0.299 * data[p + down] + 0.587 * data[p + down + 1] + 0.114 * data[p + down + 2];
|
|
354
|
+
const d = 0.299 * data[p + down + 4] + 0.587 * data[p + down + 5] + 0.114 * data[p + down + 6];
|
|
355
|
+
const m = (a + b + c + d) * 0.25;
|
|
356
|
+
mean[i] = m;
|
|
357
|
+
fine[i] = (Math.abs(a - m) + Math.abs(b - m) + Math.abs(c - m) + Math.abs(d - m)) * 0.25;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
287
361
|
const cell = new Float32Array(factor * factor);
|
|
288
362
|
for (let my = 0; my < mh; my++) {
|
|
289
363
|
const yEnd = Math.min(height, (my + 1) * factor);
|
|
290
|
-
|
|
364
|
+
// After the unrolled pass only the cells cut by the right and bottom border are left.
|
|
365
|
+
for (let mx = factor === 2 && my < fullH ? fullW : 0; mx < mw; mx++) {
|
|
291
366
|
const xEnd = Math.min(width, (mx + 1) * factor);
|
|
292
367
|
let sum = 0;
|
|
293
368
|
let n = 0;
|
|
@@ -308,19 +383,40 @@ function amplitudeMap(image, factor) {
|
|
|
308
383
|
}
|
|
309
384
|
}
|
|
310
385
|
// Detail of the two finest octaves only: what has the grain of the fine part and can hide it.
|
|
311
|
-
const smooth =
|
|
386
|
+
const smooth = blur3(mean, mw, mh);
|
|
312
387
|
for (let i = 0; i < fine.length; i++)
|
|
313
388
|
fine[i] = fine[i] + Math.abs(mean[i] - smooth[i]);
|
|
314
|
-
//
|
|
315
|
-
// the
|
|
316
|
-
|
|
317
|
-
const
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
389
|
+
// Texture is judged on a grid twice as coarse again. A line answers within two cells of
|
|
390
|
+
// itself; the lowest value within four cells is then that of the flat ground beside it.
|
|
391
|
+
// Texture has no flat ground that near, and keeps its value.
|
|
392
|
+
const pw = Math.ceil(mw / 2);
|
|
393
|
+
const ph = Math.ceil(mh / 2);
|
|
394
|
+
const pooled = new Float32Array(pw * ph);
|
|
395
|
+
for (let py = 0; py < ph; py++) {
|
|
396
|
+
const rowA = 2 * py * mw;
|
|
397
|
+
const rowB = Math.min(mh - 1, 2 * py + 1) * mw;
|
|
398
|
+
for (let px = 0; px < pw; px++) {
|
|
399
|
+
const xa = 2 * px;
|
|
400
|
+
const xb = Math.min(mw - 1, xa + 1);
|
|
401
|
+
pooled[py * pw + px] = (fine[rowA + xa] + fine[rowA + xb] + fine[rowB + xa] + fine[rowB + xb]) * 0.25;
|
|
402
|
+
}
|
|
322
403
|
}
|
|
323
|
-
|
|
404
|
+
const hidden = blur3(minFilter(pooled, pw, ph, 2), pw, ph);
|
|
405
|
+
const coarse = new Float32Array(mw * mh);
|
|
406
|
+
for (let my = 0; my < mh; my++) {
|
|
407
|
+
const from = (my >> 1) * pw;
|
|
408
|
+
for (let mx = 0, i = my * mw; mx < mw; mx++, i++) {
|
|
409
|
+
const detail = hidden[from + (mx >> 1)] - DETAIL_FREE;
|
|
410
|
+
const texture = detail > 0 ? detail : 0;
|
|
411
|
+
// A change far smaller than the step it sits on does not show, and it is what a flat drawing
|
|
412
|
+
// keeps of the mark after lossy compression and reduction.
|
|
413
|
+
const stroke = fine[i] - EDGE_FREE;
|
|
414
|
+
const onEdge = stroke > 0 ? Math.min(EDGE_CEIL, EDGE_SHARE * stroke) : 0;
|
|
415
|
+
coarse[i] = strength * Math.max(Math.min(COARSE_CEIL, COARSE_FLOOR + COARSE_SLOPE * texture), EDGE_COARSE * onEdge);
|
|
416
|
+
fine[i] = strength * Math.min(FINE_CEIL, Math.max(FINE_SLOPE * texture, onEdge));
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return { coarse, fine, width: mw, height: mh };
|
|
324
420
|
}
|
|
325
421
|
/**
|
|
326
422
|
* Adds the mark to `image` in place. Fully transparent pixels are left alone.
|
|
@@ -329,30 +425,24 @@ function amplitudeMap(image, factor) {
|
|
|
329
425
|
export function embedPixelMark(image, key, options = {}) {
|
|
330
426
|
const { data, width, height } = image;
|
|
331
427
|
const short = Math.min(width, height);
|
|
332
|
-
const strength = options.strength ?? 1;
|
|
333
428
|
const factor = Math.max(1, Math.round(short / 512));
|
|
334
|
-
const amplitude = amplitudeMap(image, factor);
|
|
429
|
+
const amplitude = amplitudeMap(image, factor, options.strength ?? 1);
|
|
430
|
+
const coarseAmp = amplitude.coarse;
|
|
431
|
+
const fineAmp = amplitude.fine;
|
|
335
432
|
// Tile phase per column / row: the period is short / TILES_PER_SHORT_SIDE pixels.
|
|
336
433
|
const cellsPerPixel = (TILES_PER_SHORT_SIDE * TILE) / short;
|
|
337
434
|
const x0 = new Int32Array(width);
|
|
338
435
|
const xw = new Float32Array(width);
|
|
339
|
-
//
|
|
340
|
-
const
|
|
341
|
-
const aw = new Float32Array(width);
|
|
436
|
+
// The amplitude cell of each column.
|
|
437
|
+
const cellOf = new Int32Array(width);
|
|
342
438
|
for (let x = 0; x < width; x++) {
|
|
343
439
|
const pos = (x * cellsPerPixel) % TILE;
|
|
344
440
|
x0[x] = Math.floor(pos);
|
|
345
441
|
xw[x] = pos - x0[x];
|
|
346
|
-
|
|
347
|
-
ax[x] = Math.min(amplitude.width - 2, Math.floor(at));
|
|
348
|
-
aw[x] = amplitude.width > 1 ? at - ax[x] : 0;
|
|
349
|
-
if (amplitude.width === 1)
|
|
350
|
-
ax[x] = 0;
|
|
442
|
+
cellOf[x] = Math.floor(x / factor);
|
|
351
443
|
}
|
|
352
444
|
const coarseRow = new Float32Array(TILE + 1);
|
|
353
445
|
const fineRow = new Float32Array(TILE + 1);
|
|
354
|
-
const coarseAmp = new Float32Array(amplitude.width + 1);
|
|
355
|
-
const fineAmp = new Float32Array(amplitude.width + 1);
|
|
356
446
|
let squared = 0;
|
|
357
447
|
let touched = 0;
|
|
358
448
|
// The dither: one draw per pixel, the same for every run on the same picture.
|
|
@@ -369,17 +459,7 @@ export function embedPixelMark(image, key, options = {}) {
|
|
|
369
459
|
}
|
|
370
460
|
coarseRow[TILE] = coarseRow[0];
|
|
371
461
|
fineRow[TILE] = fineRow[0];
|
|
372
|
-
const
|
|
373
|
-
const ay = Math.min(Math.max(0, amplitude.height - 2), Math.floor(at));
|
|
374
|
-
const wa = amplitude.height > 1 ? at - ay : 0;
|
|
375
|
-
const mapA = ay * amplitude.width;
|
|
376
|
-
const mapB = Math.min(amplitude.height - 1, ay + 1) * amplitude.width;
|
|
377
|
-
for (let c = 0; c < amplitude.width; c++) {
|
|
378
|
-
coarseAmp[c] = strength * (amplitude.coarse[mapA + c] * (1 - wa) + amplitude.coarse[mapB + c] * wa);
|
|
379
|
-
fineAmp[c] = strength * (amplitude.fine[mapA + c] * (1 - wa) + amplitude.fine[mapB + c] * wa);
|
|
380
|
-
}
|
|
381
|
-
coarseAmp[amplitude.width] = coarseAmp[amplitude.width - 1];
|
|
382
|
-
fineAmp[amplitude.width] = fineAmp[amplitude.width - 1];
|
|
462
|
+
const cells = Math.floor(py / factor) * amplitude.width;
|
|
383
463
|
let p = 4 * py * width;
|
|
384
464
|
for (let px = 0; px < width; px++, p += 4) {
|
|
385
465
|
seed = (Math.imul(seed, 1664525) + 1013904223) | 0;
|
|
@@ -388,10 +468,9 @@ export function embedPixelMark(image, key, options = {}) {
|
|
|
388
468
|
touched++;
|
|
389
469
|
const cx = x0[px];
|
|
390
470
|
const wx = xw[px];
|
|
391
|
-
const
|
|
392
|
-
const
|
|
393
|
-
|
|
394
|
-
(fineAmp[ac] * (1 - wa2) + fineAmp[ac + 1] * wa2) * (fineRow[cx] * (1 - wx) + fineRow[cx + 1] * wx);
|
|
471
|
+
const at = cells + cellOf[px];
|
|
472
|
+
const change = coarseAmp[at] * (coarseRow[cx] + (coarseRow[cx + 1] - coarseRow[cx]) * wx) +
|
|
473
|
+
fineAmp[at] * (fineRow[cx] + (fineRow[cx + 1] - fineRow[cx]) * wx);
|
|
395
474
|
// Dithered rounding: the mean change is the wanted one even where that is a fraction of a level.
|
|
396
475
|
const delta = Math.floor(change + (seed >>> 8) / 16777216);
|
|
397
476
|
if (delta === 0)
|
|
@@ -448,14 +527,15 @@ export function pixelMarkPlane(width, height, key, tilesPerShortSide = TILES_PER
|
|
|
448
527
|
* test looks at 9 shifts of a handful of hypotheses; the search looks at every
|
|
449
528
|
* shift of the grids the synchronisation peaks propose, so its cut is higher.
|
|
450
529
|
*
|
|
451
|
-
* Measured (docs/MEASURED.md) on
|
|
452
|
-
*
|
|
453
|
-
* exponential fit to the top tenth
|
|
454
|
-
*
|
|
455
|
-
*
|
|
530
|
+
* Measured (docs/MEASURED.md) on unmarked pictures, as they are and inside a
|
|
531
|
+
* page. Search: 2,168 scores, no detection, highest 6.33, 99 % under 5.88; an
|
|
532
|
+
* exponential fit to the top tenth puts the chance of passing 7.5 at about 1 in
|
|
533
|
+
* a million per picture and key. Aligned test alone: 1,610 scores, highest 4.68;
|
|
534
|
+
* the same fit gives about 5 in ten million for 6.5. Both are extrapolations,
|
|
535
|
+
* not counts.
|
|
456
536
|
*/
|
|
457
537
|
export const ALIGNED_CUT = 6.5;
|
|
458
|
-
export const SEARCH_CUT =
|
|
538
|
+
export const SEARCH_CUT = 7.5;
|
|
459
539
|
/** Shorter sides (pixels) pictures are generated in; used when a copy is an unscaled piece of one. */
|
|
460
540
|
const USUAL_SHORT_SIDES = [512, 720, 768, 832, 864, 896, 1024, 1080, 1152, 1280, 1344, 1536, 2048];
|
|
461
541
|
/** Aspect ratios pictures are generated in; used when a copy was stretched. */
|
|
@@ -532,7 +612,15 @@ function bandPlan(key, n) {
|
|
|
532
612
|
bins.sort((p, q) => p.r - q.r);
|
|
533
613
|
const edges = NESTED_BANDS.filter((edge, at) => edge < n / 2 || at === 0 || NESTED_BANDS[at - 1] < n / 2 - 1);
|
|
534
614
|
const fineFrom = bins.findIndex((b) => b.r > BAND_SPLIT + 2);
|
|
615
|
+
const steps = (shift) => ({
|
|
616
|
+
cx: Float32Array.from(bins, (b) => Math.cos((2 * Math.PI * b.fu * shift) / n)),
|
|
617
|
+
sx: Float32Array.from(bins, (b) => Math.sin((2 * Math.PI * b.fu * shift) / n)),
|
|
618
|
+
cy: Float32Array.from(bins, (b) => Math.cos((2 * Math.PI * b.fv * shift) / n)),
|
|
619
|
+
sy: Float32Array.from(bins, (b) => Math.sin((2 * Math.PI * b.fv * shift) / n)),
|
|
620
|
+
});
|
|
535
621
|
const plan = {
|
|
622
|
+
quarter: steps(0.25),
|
|
623
|
+
half: steps(0.5),
|
|
536
624
|
ranges: [],
|
|
537
625
|
index: Int32Array.from(bins, (b) => b.i),
|
|
538
626
|
fu: Int16Array.from(bins, (b) => b.fu),
|
|
@@ -551,7 +639,11 @@ function bandPlan(key, n) {
|
|
|
551
639
|
plan.keyRe[k] = re * Math.cos(angle) - im * Math.sin(angle);
|
|
552
640
|
plan.keyIm[k] = re * Math.sin(angle) + im * Math.cos(angle);
|
|
553
641
|
});
|
|
642
|
+
// The narrow nested bands are for reduced copies, which fold on a smaller grid; on the full
|
|
643
|
+
// grid the coarse part, everything, and the fine part alone are read.
|
|
554
644
|
plan.ranges = plan.ends.map((end) => [0, end]);
|
|
645
|
+
if (n === 128 && plan.ends.length > 2)
|
|
646
|
+
plan.ranges = plan.ranges.slice(-2);
|
|
555
647
|
// In a textured picture the coarse rings hold almost no mark; read with them, the fine part drowns.
|
|
556
648
|
if (fineFrom > 0 && bins.length - fineFrom > 200)
|
|
557
649
|
plan.ranges.push([fineFrom, bins.length]);
|
|
@@ -630,6 +722,8 @@ function foldScratch(width) {
|
|
|
630
722
|
im2: new Float64Array(cells),
|
|
631
723
|
sums: new Float64Array(cells + 1),
|
|
632
724
|
energies: new Float64Array(cells + 1),
|
|
725
|
+
polishRe: new Float64Array(cells),
|
|
726
|
+
polishIm: new Float64Array(cells),
|
|
633
727
|
colBin: new Int32Array(width),
|
|
634
728
|
};
|
|
635
729
|
}
|
|
@@ -724,26 +818,47 @@ function scoreAllShifts(plan, scratch, n, bands, halfTurn) {
|
|
|
724
818
|
}
|
|
725
819
|
}
|
|
726
820
|
// The surface is known at whole bins; the true offset falls between them, and half a bin is a
|
|
727
|
-
// sixth of a wave at the fine end of the band. Around
|
|
728
|
-
// directly at quarter-bin steps.
|
|
821
|
+
// sixth of a wave at the fine end of the band. Around a peak that could still reach the cut,
|
|
822
|
+
// the correlation is summed directly at quarter-bin steps.
|
|
823
|
+
if (bestScore < POLISH_FROM)
|
|
824
|
+
return { score: bestScore, shift: bestShift };
|
|
729
825
|
const [from, end] = bestRange;
|
|
826
|
+
const [sx, sy] = bestShift;
|
|
827
|
+
// The spectrum turned to the peak's own shift, once.
|
|
828
|
+
const pr = scratch.polishRe;
|
|
829
|
+
const pi = scratch.polishIm;
|
|
730
830
|
let energy = 0;
|
|
731
|
-
for (let k = from; k < end; k++)
|
|
831
|
+
for (let k = from; k < end; k++) {
|
|
832
|
+
const angle = (2 * Math.PI * (plan.fu[k] * sx + plan.fv[k] * sy)) / n;
|
|
833
|
+
const c = Math.cos(angle);
|
|
834
|
+
const s = Math.sin(angle);
|
|
835
|
+
pr[k] = re[k] * c - im[k] * s;
|
|
836
|
+
pi[k] = re[k] * s + im[k] * c;
|
|
732
837
|
energy += re[k] * re[k] + im[k] * im[k];
|
|
838
|
+
}
|
|
733
839
|
const norm = 1 / Math.sqrt(energy + 1e-30);
|
|
734
|
-
const [
|
|
735
|
-
for (let
|
|
736
|
-
for (let
|
|
737
|
-
if (
|
|
840
|
+
const tables = [plan.half, plan.quarter];
|
|
841
|
+
for (let iy = -2; iy <= 2; iy++) {
|
|
842
|
+
for (let ix = -2; ix <= 2; ix++) {
|
|
843
|
+
if (ix === 0 && iy === 0)
|
|
738
844
|
continue;
|
|
845
|
+
// cos and sin of the x and the y part of the extra shift; a negative shift flips the sine.
|
|
846
|
+
const tx = ix === 0 ? null : tables[Math.abs(ix) - 1 === 1 ? 0 : 1];
|
|
847
|
+
const ty = iy === 0 ? null : tables[Math.abs(iy) - 1 === 1 ? 0 : 1];
|
|
848
|
+
const signX = ix < 0 ? -1 : 1;
|
|
849
|
+
const signY = iy < 0 ? -1 : 1;
|
|
739
850
|
let sum = 0;
|
|
740
851
|
for (let k = from; k < end; k++) {
|
|
741
|
-
const
|
|
742
|
-
|
|
852
|
+
const cx = tx ? tx.cx[k] : 1;
|
|
853
|
+
const sxk = tx ? signX * tx.sx[k] : 0;
|
|
854
|
+
const cy = ty ? ty.cy[k] : 1;
|
|
855
|
+
const syk = ty ? signY * ty.sy[k] : 0;
|
|
856
|
+
// real part of (pr + i·pi) · e^{i(ax + ay)}
|
|
857
|
+
sum += pr[k] * (cx * cy - sxk * syk) - pi[k] * (sxk * cy + cx * syk);
|
|
743
858
|
}
|
|
744
859
|
if (sum * norm > bestScore) {
|
|
745
860
|
bestScore = sum * norm;
|
|
746
|
-
bestShift = [sx +
|
|
861
|
+
bestShift = [sx + ix * 0.25, sy + iy * 0.25];
|
|
747
862
|
}
|
|
748
863
|
}
|
|
749
864
|
}
|
|
@@ -794,6 +909,19 @@ export function detectPixelMark(image, key, options = {}) {
|
|
|
794
909
|
cut = { score: peak.score, period: period * w.factor };
|
|
795
910
|
}
|
|
796
911
|
if (cut.score >= SEARCH_CUT) {
|
|
912
|
+
// A copy reduced to nearly one of those sizes passes here too, with its period a little
|
|
913
|
+
// off: look around for where the mark reads best.
|
|
914
|
+
const plan = bandPlan(key, 128);
|
|
915
|
+
const near = cut.period / w.factor;
|
|
916
|
+
for (let k = -8; k <= 8; k++) {
|
|
917
|
+
if (k === 0)
|
|
918
|
+
continue;
|
|
919
|
+
const period = near * (1 + 0.002 * k);
|
|
920
|
+
crossSpectrum(w, plan, [1 / period, 0], [0, 1 / period], 128, scratch);
|
|
921
|
+
const peak = scoreAllShifts(plan, scratch, 128, plan.ranges.length, false);
|
|
922
|
+
if (peak.score > cut.score)
|
|
923
|
+
cut = { score: peak.score, period: period * w.factor };
|
|
924
|
+
}
|
|
797
925
|
return { detected: true, score: cut.score, mode: "search", period: [cut.period, cut.period], rotation: 0, mirrored: false, keyId: key.id };
|
|
798
926
|
}
|
|
799
927
|
// 2. Unknown geometry (crop, screen capture, rotation, mirror, stretch): the
|
|
@@ -802,7 +930,7 @@ export function detectPixelMark(image, key, options = {}) {
|
|
|
802
930
|
// The peaks are looked for in a reduced picture; the grid they give is then checked on one
|
|
803
931
|
// sharp enough to still hold the fine part of the mark.
|
|
804
932
|
const sharp = w.factor === 1 ? w : whiten(image, 1100);
|
|
805
|
-
const found = searchByPeaks(coarse, key, scratch, sharp.factor < coarse.factor ? sharp : coarse);
|
|
933
|
+
const found = searchByPeaks(coarse, key, scratch, sharp.factor < coarse.factor ? sharp : coarse, w);
|
|
806
934
|
if (found && found.score >= SEARCH_CUT) {
|
|
807
935
|
return {
|
|
808
936
|
detected: true,
|
|
@@ -814,42 +942,7 @@ export function detectPixelMark(image, key, options = {}) {
|
|
|
814
942
|
keyId: key.id,
|
|
815
943
|
};
|
|
816
944
|
}
|
|
817
|
-
|
|
818
|
-
return { ...none, score: Math.max(0, aligned.score, cut.score, found?.score ?? 0) };
|
|
819
|
-
// 3. Too weak for the peaks (a small or heavily recompressed crop that was not
|
|
820
|
-
// turned): scan the period on the lowest rings, which gather the whole mark.
|
|
821
|
-
const plan32 = bandPlan(key, 32);
|
|
822
|
-
const candidates = [];
|
|
823
|
-
const maxPeriod = Math.max(coarse.width, coarse.height) * 1.5;
|
|
824
|
-
for (let period = 16; period <= maxPeriod; period *= 1.012) {
|
|
825
|
-
crossSpectrum(coarse, plan32, [1 / period, 0], [0, 1 / period], 32, scratch);
|
|
826
|
-
candidates.push({ score: scoreAllShifts(plan32, scratch, 32, 2, false).score, period });
|
|
827
|
-
}
|
|
828
|
-
candidates.sort((p, q) => q.score - p.score);
|
|
829
|
-
const seeds = [];
|
|
830
|
-
for (const candidate of candidates) {
|
|
831
|
-
if (seeds.length >= 4)
|
|
832
|
-
break;
|
|
833
|
-
if (seeds.some((seed) => Math.abs(seed / candidate.period - 1) < 0.03))
|
|
834
|
-
continue;
|
|
835
|
-
seeds.push(candidate.period);
|
|
836
|
-
}
|
|
837
|
-
let scanned = { score: -Infinity, period: 0 };
|
|
838
|
-
for (const seed of seeds) {
|
|
839
|
-
for (let k = -8; k <= 8; k++) {
|
|
840
|
-
const period = seed * (1 + k * 0.0015);
|
|
841
|
-
const n = gridFor(period);
|
|
842
|
-
const plan = bandPlan(key, n);
|
|
843
|
-
crossSpectrum(coarse, plan, [1 / period, 0], [0, 1 / period], n, scratch);
|
|
844
|
-
const peak = scoreAllShifts(plan, scratch, n, plan.ends.length, false);
|
|
845
|
-
if (peak.score > scanned.score)
|
|
846
|
-
scanned = { score: peak.score, period: period * coarse.factor };
|
|
847
|
-
}
|
|
848
|
-
}
|
|
849
|
-
if (scanned.score >= SEARCH_CUT) {
|
|
850
|
-
return { detected: true, score: scanned.score, mode: "search", period: [scanned.period, scanned.period], rotation: 0, mirrored: false, keyId: key.id };
|
|
851
|
-
}
|
|
852
|
-
return { ...none, score: Math.max(0, aligned.score, cut.score, found?.score ?? 0, scanned.score) };
|
|
945
|
+
return { ...none, score: Math.max(0, aligned.score, cut.score, found?.score ?? 0) };
|
|
853
946
|
}
|
|
854
947
|
/** Fourier power of the whitened picture, zero-padded so a peak spans a few bins, flattened over radius. */
|
|
855
948
|
function peakSpectrum(w) {
|
|
@@ -860,9 +953,22 @@ function peakSpectrum(w) {
|
|
|
860
953
|
const offY = Math.max(0, Math.floor((w.height - n) / 2));
|
|
861
954
|
const cw = Math.min(n, w.width);
|
|
862
955
|
const ch = Math.min(n, w.height);
|
|
956
|
+
// Tapered edges (the outer 15 % of each side): without them the frame of the picture and every
|
|
957
|
+
// long line in a page smear across the spectrum and bury faint peaks; a full window would cost
|
|
958
|
+
// the peaks a third of their height.
|
|
959
|
+
const wx = new Float32Array(cw);
|
|
960
|
+
const wy = new Float32Array(ch);
|
|
961
|
+
const taper = (at, length) => {
|
|
962
|
+
const edge = Math.min(at + 0.5, length - at - 0.5) / (0.15 * length);
|
|
963
|
+
return edge >= 1 ? 1 : 0.5 - 0.5 * Math.cos(Math.PI * edge);
|
|
964
|
+
};
|
|
965
|
+
for (let x = 0; x < cw; x++)
|
|
966
|
+
wx[x] = taper(x, cw);
|
|
967
|
+
for (let y = 0; y < ch; y++)
|
|
968
|
+
wy[y] = taper(y, ch);
|
|
863
969
|
for (let y = 0; y < ch; y++) {
|
|
864
970
|
for (let x = 0; x < cw; x++)
|
|
865
|
-
grid[2 * (y * n + x)] = w.values[(y + offY) * w.width + x + offX];
|
|
971
|
+
grid[2 * (y * n + x)] = w.values[(y + offY) * w.width + x + offX] * wx[x] * wy[y];
|
|
866
972
|
}
|
|
867
973
|
fft2d(grid, n);
|
|
868
974
|
const power = new Float32Array(n * n);
|
|
@@ -937,8 +1043,32 @@ function peakSpectrum(w) {
|
|
|
937
1043
|
peaks[mid + u] = Math.max(power[up + l], power[up + u], power[up + r], power[mid + l], power[mid + u], power[mid + r], power[down + l], power[down + u], power[down + r]);
|
|
938
1044
|
}
|
|
939
1045
|
}
|
|
940
|
-
|
|
1046
|
+
// What a point of the spectrum reads by chance: every 37th bin outside the lowest frequencies.
|
|
1047
|
+
let count = 0, sum = 0, sumSq = 0;
|
|
1048
|
+
for (let i = 0; i < n * n; i += 37) {
|
|
1049
|
+
const u = i & (n - 1);
|
|
1050
|
+
const v = i >> 10;
|
|
1051
|
+
const fu = u < half ? u : u - n;
|
|
1052
|
+
const fv = v < half ? v : v - n;
|
|
1053
|
+
if (fu * fu + fv * fv < MIN_PEAK_RADIUS_SQ)
|
|
1054
|
+
continue;
|
|
1055
|
+
const value = peaks[i] > PEAK_CAP ? PEAK_CAP : peaks[i];
|
|
1056
|
+
sum += value;
|
|
1057
|
+
sumSq += value * value;
|
|
1058
|
+
count++;
|
|
1059
|
+
}
|
|
1060
|
+
const peakMean = sum / count;
|
|
1061
|
+
return { n, power, peaks, peakMean, peakSpread: Math.sqrt(Math.max(1e-6, sumSq / count - peakMean * peakMean)) };
|
|
941
1062
|
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Bins nearer the centre of the spectrum than this hold what is left of the picture's own slow
|
|
1065
|
+
* changes (and of a page's layout) after whitening, not peaks; a constellation point that falls
|
|
1066
|
+
* there does not count.
|
|
1067
|
+
*/
|
|
1068
|
+
const MIN_PEAK_RADIUS = 24;
|
|
1069
|
+
const MIN_PEAK_RADIUS_SQ = MIN_PEAK_RADIUS * MIN_PEAK_RADIUS;
|
|
1070
|
+
/** How many turned grids the search hands on to be checked by folding. */
|
|
1071
|
+
const SQUARE_GRIDS_KEPT = 12;
|
|
942
1072
|
/** A peak far above its ring counts no more than this: one line of the picture must not outvote the constellation. */
|
|
943
1073
|
const PEAK_CAP = 24;
|
|
944
1074
|
function keepBest(list, candidate, limit) {
|
|
@@ -957,22 +1087,46 @@ function keepBest(list, candidate, limit) {
|
|
|
957
1087
|
if (list.length > limit)
|
|
958
1088
|
list.length = limit;
|
|
959
1089
|
}
|
|
960
|
-
/**
|
|
1090
|
+
/**
|
|
1091
|
+
* How far a constellation stands out, from the sums of capped peak heights of its two halves
|
|
1092
|
+
* (`inside` points of each fell inside the spectrum): each half alone and both together are
|
|
1093
|
+
* put on one scale (standard deviations above what chance gives for that many points), and
|
|
1094
|
+
* the best counts. A reduced copy has lost the fine half, a picture of texture all over shows
|
|
1095
|
+
* little of the coarse half; neither should drag the other down. Returned as an equivalent sum
|
|
1096
|
+
* of eight peaks, so that the numbers stay comparable.
|
|
1097
|
+
*/
|
|
1098
|
+
function standOut(spectrum, coarseSum, coarseInside, fineSum, fineInside) {
|
|
1099
|
+
const { peakMean } = spectrum;
|
|
1100
|
+
let best = -Infinity;
|
|
1101
|
+
if (coarseInside >= SYNC_PEAKS - 2)
|
|
1102
|
+
best = (coarseSum - coarseInside * peakMean) / Math.sqrt(coarseInside);
|
|
1103
|
+
if (fineInside >= SYNC_PEAKS - 2) {
|
|
1104
|
+
best = Math.max(best, (fineSum - fineInside * peakMean) / Math.sqrt(fineInside));
|
|
1105
|
+
if (coarseInside >= SYNC_PEAKS - 2)
|
|
1106
|
+
best = Math.max(best, (coarseSum + fineSum - (coarseInside + fineInside) * peakMean) / Math.sqrt(coarseInside + fineInside));
|
|
1107
|
+
}
|
|
1108
|
+
if (best === -Infinity)
|
|
1109
|
+
return 0;
|
|
1110
|
+
return Math.max(0.001, SYNC_PEAKS * peakMean + best * Math.sqrt(SYNC_PEAKS));
|
|
1111
|
+
}
|
|
1112
|
+
/** How far the constellation stands out for the tile axes (fu, fv); see {@link standOut}. */
|
|
961
1113
|
function constellationScore(spectrum, sync, fu, fv) {
|
|
962
1114
|
const { n, peaks } = spectrum;
|
|
963
1115
|
const half = n / 2;
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
for (
|
|
1116
|
+
const sums = [0, 0];
|
|
1117
|
+
const inside = [0, 0];
|
|
1118
|
+
for (let i = 0; i < sync.length; i++) {
|
|
1119
|
+
const [u, v] = sync[i];
|
|
967
1120
|
const kx = Math.round(n * (u * fu[0] + v * fv[0]));
|
|
968
1121
|
const ky = Math.round(n * (u * fu[1] + v * fv[1]));
|
|
969
|
-
if (kx >= half - 1 || kx <= 1 - half || ky >= half - 1 || ky <= 1 - half || kx * kx + ky * ky <
|
|
1122
|
+
if (kx >= half - 1 || kx <= 1 - half || ky >= half - 1 || ky <= 1 - half || kx * kx + ky * ky < MIN_PEAK_RADIUS_SQ)
|
|
970
1123
|
continue;
|
|
971
1124
|
const value = peaks[((ky + n) % n) * n + ((kx + n) % n)];
|
|
972
|
-
|
|
973
|
-
|
|
1125
|
+
const part = i < SYNC_PEAKS ? 0 : 1;
|
|
1126
|
+
sums[part] += value > PEAK_CAP ? PEAK_CAP : value;
|
|
1127
|
+
inside[part]++;
|
|
974
1128
|
}
|
|
975
|
-
return
|
|
1129
|
+
return standOut(spectrum, sums[0], inside[0], sums[1], inside[1]);
|
|
976
1130
|
}
|
|
977
1131
|
/**
|
|
978
1132
|
* Every turned, scaled and mirrored square grid: the constellation's points are
|
|
@@ -997,25 +1151,52 @@ function searchSquareGrids(spectrum, key, minPeriod, maxPeriod) {
|
|
|
997
1151
|
}
|
|
998
1152
|
for (let period = minPeriod; period <= maxPeriod; period *= 1.01) {
|
|
999
1153
|
const inverse = 1 / period;
|
|
1000
|
-
let
|
|
1001
|
-
let inside = 0;
|
|
1154
|
+
let coarseSum = 0, fineSum = 0, coarseInside = 0, fineInside = 0;
|
|
1002
1155
|
for (let i = 0; i < count; i++) {
|
|
1003
1156
|
const kx = Math.round(cos[i] * inverse);
|
|
1004
1157
|
const ky = Math.round(sin[i] * inverse);
|
|
1005
|
-
if (kx >= half - 1 || kx <= 1 - half || ky >= half - 1 || ky <= 1 - half || kx * kx + ky * ky <
|
|
1158
|
+
if (kx >= half - 1 || kx <= 1 - half || ky >= half - 1 || ky <= 1 - half || kx * kx + ky * ky < MIN_PEAK_RADIUS_SQ)
|
|
1006
1159
|
continue;
|
|
1007
1160
|
const value = peaks[((ky + n) & (n - 1)) * n + ((kx + n) & (n - 1))];
|
|
1008
|
-
|
|
1009
|
-
|
|
1161
|
+
const capped = value > PEAK_CAP ? PEAK_CAP : value;
|
|
1162
|
+
if (i < SYNC_PEAKS) {
|
|
1163
|
+
coarseSum += capped;
|
|
1164
|
+
coarseInside++;
|
|
1165
|
+
}
|
|
1166
|
+
else {
|
|
1167
|
+
fineSum += capped;
|
|
1168
|
+
fineInside++;
|
|
1169
|
+
}
|
|
1010
1170
|
}
|
|
1011
|
-
|
|
1171
|
+
const score = standOut(spectrum, coarseSum, coarseInside, fineSum, fineInside);
|
|
1172
|
+
if (score <= 0)
|
|
1012
1173
|
continue;
|
|
1013
|
-
score
|
|
1014
|
-
if (best.length >= 8 && score <= best[best.length - 1].score)
|
|
1174
|
+
if (best.length >= SQUARE_GRIDS_KEPT && score <= best[best.length - 1].score)
|
|
1015
1175
|
continue;
|
|
1016
1176
|
const c = Math.cos(angle) * inverse;
|
|
1017
1177
|
const s = Math.sin(angle) * inverse;
|
|
1018
|
-
keepBest(best, { score, fu: [c, s], fv: [-s * mirror, c * mirror] },
|
|
1178
|
+
keepBest(best, { score, fu: [c, s], fv: [-s * mirror, c * mirror] }, SQUARE_GRIDS_KEPT);
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
return best;
|
|
1183
|
+
}
|
|
1184
|
+
/**
|
|
1185
|
+
* Upright grids only (also turned by a quarter and mirrored): what a screen capture, a
|
|
1186
|
+
* reduced copy and a crop hold. One unknown, the period, instead of period and angle, so far
|
|
1187
|
+
* fewer chance fits and a fainter constellation still stands out.
|
|
1188
|
+
*/
|
|
1189
|
+
function searchUprightGrids(spectrum, key, minPeriod, maxPeriod) {
|
|
1190
|
+
const best = [];
|
|
1191
|
+
for (const [ux, uy] of [[1, 0], [0, 1]]) {
|
|
1192
|
+
for (const mirror of [1, -1]) {
|
|
1193
|
+
for (let period = minPeriod; period <= maxPeriod; period *= 1.003) {
|
|
1194
|
+
const f = 1 / period;
|
|
1195
|
+
const fu = [ux * f, uy * f];
|
|
1196
|
+
const fv = [-uy * f * mirror, ux * f * mirror];
|
|
1197
|
+
const score = constellationScore(spectrum, key.sync, fu, fv);
|
|
1198
|
+
if (score > 0)
|
|
1199
|
+
keepBest(best, { score, fu, fv }, 4);
|
|
1019
1200
|
}
|
|
1020
1201
|
}
|
|
1021
1202
|
}
|
|
@@ -1035,7 +1216,7 @@ function searchAnyGrids(spectrum, key, minPeriod, maxPeriod) {
|
|
|
1035
1216
|
for (let kx = -half + 2; kx < half - 2; kx++) {
|
|
1036
1217
|
if (ky === 0 && kx <= 0)
|
|
1037
1218
|
continue;
|
|
1038
|
-
if (kx * kx + ky * ky <
|
|
1219
|
+
if (kx * kx + ky * ky < MIN_PEAK_RADIUS_SQ)
|
|
1039
1220
|
continue;
|
|
1040
1221
|
const row = ky * n;
|
|
1041
1222
|
const col = (kx + n) & (n - 1);
|
|
@@ -1053,7 +1234,7 @@ function searchAnyGrids(spectrum, key, minPeriod, maxPeriod) {
|
|
|
1053
1234
|
}
|
|
1054
1235
|
}
|
|
1055
1236
|
found.sort((p, q) => q.value - p.value);
|
|
1056
|
-
const strong = found.slice(0,
|
|
1237
|
+
const strong = found.slice(0, 48);
|
|
1057
1238
|
const best = [];
|
|
1058
1239
|
const minF = 1 / maxPeriod;
|
|
1059
1240
|
const maxF = 1 / minPeriod;
|
|
@@ -1066,7 +1247,9 @@ function searchAnyGrids(spectrum, key, minPeriod, maxPeriod) {
|
|
|
1066
1247
|
const bx = strong[b].x * sign, by = strong[b].y * sign;
|
|
1067
1248
|
for (let i = 0; i < key.sync.length; i++) {
|
|
1068
1249
|
const [u1, v1] = key.sync[i];
|
|
1069
|
-
|
|
1250
|
+
// Both points from the same half: the coarse peaks, or the fine ones.
|
|
1251
|
+
const half0 = i < SYNC_PEAKS ? 0 : SYNC_PEAKS;
|
|
1252
|
+
for (let j = half0; j < half0 + SYNC_PEAKS; j++) {
|
|
1070
1253
|
if (i === j)
|
|
1071
1254
|
continue;
|
|
1072
1255
|
const [u2, v2] = key.sync[j];
|
|
@@ -1174,7 +1357,9 @@ function verifyGrid(w, key, scratch, grid) {
|
|
|
1174
1357
|
};
|
|
1175
1358
|
}
|
|
1176
1359
|
/** A grid is worth refining from this score: above what most unmarked pictures reach on any grid. */
|
|
1177
|
-
const REFINE_FROM =
|
|
1360
|
+
const REFINE_FROM = 5.6;
|
|
1361
|
+
/** A peak is worth polishing between the bins from this score (polishing adds at most about a half to it). */
|
|
1362
|
+
const POLISH_FROM = 4.6;
|
|
1178
1363
|
function refineGrid(w, key, scratch, start, first) {
|
|
1179
1364
|
let best = first;
|
|
1180
1365
|
let grid = start;
|
|
@@ -1201,10 +1386,12 @@ function refineGrid(w, key, scratch, start, first) {
|
|
|
1201
1386
|
}
|
|
1202
1387
|
return best;
|
|
1203
1388
|
}
|
|
1204
|
-
function searchByPeaks(w, key, scratch, sharp = w) {
|
|
1205
|
-
if (key.sync.length < SYNC_PEAKS -
|
|
1389
|
+
function searchByPeaks(w, key, scratch, sharp = w, middle) {
|
|
1390
|
+
if (key.sync.length < 2 * SYNC_PEAKS - 2)
|
|
1206
1391
|
return null;
|
|
1207
|
-
|
|
1392
|
+
let spectrum = peakSpectrum(w);
|
|
1393
|
+
/** What the spectrum in use was taken from; candidate grids are in its pixels. */
|
|
1394
|
+
let from = w;
|
|
1208
1395
|
const maxPeriod = Math.max(w.width, w.height) * 1.5;
|
|
1209
1396
|
const minPeriod = 24;
|
|
1210
1397
|
let best = null;
|
|
@@ -1212,8 +1399,8 @@ function searchByPeaks(w, key, scratch, sharp = w) {
|
|
|
1212
1399
|
for (const candidate of candidates) {
|
|
1213
1400
|
const fitted = fitGrid(spectrum, key.sync, candidate);
|
|
1214
1401
|
for (const grid of fitted ? [fitted] : [candidate]) {
|
|
1215
|
-
// Cycles per pixel of the
|
|
1216
|
-
const ratio = sharp.factor /
|
|
1402
|
+
// Cycles per pixel of the picture the spectrum came from, in pixels of the sharp one.
|
|
1403
|
+
const ratio = sharp.factor / from.factor;
|
|
1217
1404
|
const onSharp = { score: grid.score, fu: [grid.fu[0] * ratio, grid.fu[1] * ratio], fv: [grid.fv[0] * ratio, grid.fv[1] * ratio] };
|
|
1218
1405
|
let result = verifyGrid(sharp, key, scratch, onSharp);
|
|
1219
1406
|
// The peaks give the grid to about a bin of the spectrum; the fine part of the mark only
|
|
@@ -1221,7 +1408,8 @@ function searchByPeaks(w, key, scratch, sharp = w) {
|
|
|
1221
1408
|
// mark faintly is nudged in scale and angle to where it shows best.
|
|
1222
1409
|
if (result.score >= REFINE_FROM)
|
|
1223
1410
|
result = refineGrid(sharp, key, scratch, onSharp, result);
|
|
1224
|
-
|
|
1411
|
+
// Reported in pixels of the reduced picture, as the caller expects.
|
|
1412
|
+
result.period = [(result.period[0] * sharp.factor) / w.factor, (result.period[1] * sharp.factor) / w.factor];
|
|
1225
1413
|
result.sync = constellationScore(spectrum, key.sync, grid.fu, grid.fv);
|
|
1226
1414
|
if (!best || result.score > best.score)
|
|
1227
1415
|
best = result;
|
|
@@ -1231,7 +1419,21 @@ function searchByPeaks(w, key, scratch, sharp = w) {
|
|
|
1231
1419
|
}
|
|
1232
1420
|
return false;
|
|
1233
1421
|
};
|
|
1234
|
-
if (consider(
|
|
1422
|
+
if (consider(searchUprightGrids(spectrum, key, minPeriod, maxPeriod)))
|
|
1423
|
+
return best;
|
|
1424
|
+
const reduced = spectrum;
|
|
1425
|
+
if (middle && middle.factor < w.factor) {
|
|
1426
|
+
// The reduced picture has lost the fine peaks. An upright copy is looked for once more in a
|
|
1427
|
+
// sharper one (its middle, when it is larger than the transform), where they still stand.
|
|
1428
|
+
spectrum = peakSpectrum(middle);
|
|
1429
|
+
from = middle;
|
|
1430
|
+
const found = consider(searchUprightGrids(spectrum, key, minPeriod, Math.max(middle.width, middle.height) * 1.5));
|
|
1431
|
+
spectrum = reduced;
|
|
1432
|
+
from = w;
|
|
1433
|
+
if (found)
|
|
1434
|
+
return best;
|
|
1435
|
+
}
|
|
1436
|
+
if (consider(searchSquareGrids(spectrum, key, minPeriod, maxPeriod)))
|
|
1235
1437
|
return best;
|
|
1236
1438
|
consider(searchAnyGrids(spectrum, key, minPeriod, maxPeriod).slice(0, 4));
|
|
1237
1439
|
return best;
|
package/package.json
CHANGED