@automataframework/art 0.0.1-preview.12.sha-e1e5dd9 → 0.0.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 +3 -0
- package/index.js +1 -0
- package/package.json +7 -38
- package/LICENSE +0 -674
- package/dist/bin.d.ts +0 -3
- package/dist/bin.js +0 -38
- package/dist/features/clean/clean.d.ts +0 -12
- package/dist/features/clean/clean.js +0 -75
- package/dist/features/make/fal.d.ts +0 -10
- package/dist/features/make/fal.js +0 -19
- package/dist/features/make/ledger.d.ts +0 -17
- package/dist/features/make/ledger.js +0 -29
- package/dist/features/make/make.d.ts +0 -20
- package/dist/features/make/make.js +0 -97
- package/dist/features/make/models.d.ts +0 -11
- package/dist/features/make/models.js +0 -7
- package/dist/features/palette/duel.d.ts +0 -2
- package/dist/features/palette/duel.js +0 -22
- package/dist/features/palette/palette.d.ts +0 -8
- package/dist/features/palette/palette.js +0 -35
- package/dist/features/sidecar/sidecar.d.ts +0 -15
- package/dist/features/sidecar/sidecar.js +0 -35
- package/dist/index.d.ts +0 -7
- package/dist/index.js +0 -9
- package/dist/lib/png.d.ts +0 -4
- package/dist/lib/png.js +0 -49
- package/vendor/unfake/LICENSE +0 -21
- package/vendor/unfake/SOURCE.md +0 -24
- package/vendor/unfake/lib/core-client.js +0 -159
- package/vendor/unfake/lib/core-codec.js +0 -35
- package/vendor/unfake/lib/cvfree.js +0 -193
- package/vendor/unfake/lib/edge-detect.js +0 -127
- package/vendor/unfake/lib/pixel.js +0 -541
- package/vendor/unfake/lib/utils.js +0 -520
- package/vendor/unfake/vendor/unfake-core/unfake_wasm.js +0 -524
- package/vendor/unfake/vendor/unfake-core/unfake_wasm_bg.wasm +0 -0
- package/vendor/unfake/workers/unfake-core-worker.js +0 -259
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Main-thread facade for unfake-core WASM (detect / snap / downscale).
|
|
3
|
-
* OpenCV is not required for these ops.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { payloadToImageData, imageDataToPayload } from './core-codec.js';
|
|
7
|
-
|
|
8
|
-
let worker = null;
|
|
9
|
-
let workerReady = null;
|
|
10
|
-
let rpcId = 0;
|
|
11
|
-
const pending = new Map();
|
|
12
|
-
let coreAvailable = null;
|
|
13
|
-
|
|
14
|
-
function workerUrl(file) {
|
|
15
|
-
return new URL(file, import.meta.url).href;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function handleWorkerMessage(e) {
|
|
19
|
-
const msg = e.data;
|
|
20
|
-
if (msg.type === 'ready') {
|
|
21
|
-
workerReady?.resolve();
|
|
22
|
-
workerReady = null;
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
if (msg.type === 'init-error') {
|
|
26
|
-
workerReady?.reject(new Error(msg.error || 'unfake-core worker init failed'));
|
|
27
|
-
workerReady = null;
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
if (msg.type === 'rpc-result') {
|
|
31
|
-
const entry = pending.get(msg.id);
|
|
32
|
-
if (!entry) return;
|
|
33
|
-
pending.delete(msg.id);
|
|
34
|
-
if (msg.ok) entry.resolve(msg.result);
|
|
35
|
-
else entry.reject(new Error(msg.error || 'unfake-core RPC failed'));
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function spawnWorker() {
|
|
40
|
-
const w = new Worker(workerUrl('../workers/unfake-core-worker.js'), { type: 'module' });
|
|
41
|
-
w.onmessage = handleWorkerMessage;
|
|
42
|
-
w.onerror = (err) => {
|
|
43
|
-
workerReady?.reject(err.error || new Error('unfake-core worker error'));
|
|
44
|
-
workerReady = null;
|
|
45
|
-
};
|
|
46
|
-
return w;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* @returns {Promise<boolean>} true if WASM core is usable
|
|
51
|
-
*/
|
|
52
|
-
export async function ensureCoreWorker() {
|
|
53
|
-
if (coreAvailable === true) return true;
|
|
54
|
-
if (coreAvailable === false) return false;
|
|
55
|
-
|
|
56
|
-
if (typeof Worker === 'undefined') {
|
|
57
|
-
coreAvailable = false;
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (workerReady?.promise) {
|
|
62
|
-
try {
|
|
63
|
-
await workerReady.promise;
|
|
64
|
-
coreAvailable = true;
|
|
65
|
-
return true;
|
|
66
|
-
} catch {
|
|
67
|
-
coreAvailable = false;
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
let readyResolve;
|
|
73
|
-
let readyReject;
|
|
74
|
-
const readyPromise = new Promise((resolve, reject) => {
|
|
75
|
-
readyResolve = resolve;
|
|
76
|
-
readyReject = reject;
|
|
77
|
-
});
|
|
78
|
-
const timer = setTimeout(
|
|
79
|
-
() => readyReject(new Error('unfake-core worker init timeout')),
|
|
80
|
-
30_000,
|
|
81
|
-
);
|
|
82
|
-
workerReady = {
|
|
83
|
-
promise: readyPromise,
|
|
84
|
-
resolve: () => { clearTimeout(timer); readyResolve(); },
|
|
85
|
-
reject: (err) => { clearTimeout(timer); readyReject(err); },
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
worker = spawnWorker();
|
|
89
|
-
worker.postMessage({
|
|
90
|
-
type: 'init',
|
|
91
|
-
wasmUrl: '../vendor/unfake-core/unfake_wasm_bg.wasm',
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
try {
|
|
95
|
-
await workerReady.promise;
|
|
96
|
-
coreAvailable = true;
|
|
97
|
-
return true;
|
|
98
|
-
} catch (err) {
|
|
99
|
-
console.warn('[unfake.js] unfake-core WASM unavailable, using JS fallback:', err);
|
|
100
|
-
worker?.terminate();
|
|
101
|
-
worker = null;
|
|
102
|
-
workerReady = null;
|
|
103
|
-
coreAvailable = false;
|
|
104
|
-
return false;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/** @returns {Promise<ImageData|number|{x:number,y:number}|object|null>} */
|
|
109
|
-
export async function runCoreOp(op, payload = {}) {
|
|
110
|
-
const ok = await ensureCoreWorker();
|
|
111
|
-
if (!ok) throw new Error('unfake-core unavailable');
|
|
112
|
-
|
|
113
|
-
const id = ++rpcId;
|
|
114
|
-
const serialized = { ...payload };
|
|
115
|
-
const transfers = [];
|
|
116
|
-
if (payload.image instanceof ImageData) {
|
|
117
|
-
const copy = new Uint8ClampedArray(payload.image.data);
|
|
118
|
-
serialized.image = {
|
|
119
|
-
width: payload.image.width,
|
|
120
|
-
height: payload.image.height,
|
|
121
|
-
buffer: copy.buffer,
|
|
122
|
-
};
|
|
123
|
-
transfers.push(copy.buffer);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const result = await new Promise((resolve, reject) => {
|
|
127
|
-
pending.set(id, { resolve, reject });
|
|
128
|
-
worker.postMessage({ type: 'rpc', id, op, payload: serialized }, transfers);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
if (result?.type === 'image') return payloadToImageData(result);
|
|
132
|
-
if (result?.type === 'number') return result.value;
|
|
133
|
-
if (result?.type === 'offset') return { x: result.x, y: result.y };
|
|
134
|
-
if (result?.type === 'grid') return result.grid;
|
|
135
|
-
if (result?.type === 'bounds') return result.bounds;
|
|
136
|
-
if (result?.type === 'bytes') return new Uint8Array(result.buffer);
|
|
137
|
-
if (result?.type === 'quantize') {
|
|
138
|
-
return {
|
|
139
|
-
quantized: payloadToImageData({ width: result.width, height: result.height, buffer: result.buffer }),
|
|
140
|
-
palette: result.palette,
|
|
141
|
-
colorsUsed: result.colorsUsed,
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
return result;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export async function coreReady() {
|
|
148
|
-
return ensureCoreWorker();
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/** Downscale method id for WASM (matches unfake-wasm). */
|
|
152
|
-
export const CORE_DS = {
|
|
153
|
-
nearest: 0,
|
|
154
|
-
median: 1,
|
|
155
|
-
mode: 2,
|
|
156
|
-
dominant: 3,
|
|
157
|
-
qvote: 4,
|
|
158
|
-
mean: 5,
|
|
159
|
-
};
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared codec for the unfake-core WASM packed image format:
|
|
3
|
-
* [u32 LE width, u32 LE height, RGBA bytes...]
|
|
4
|
-
*
|
|
5
|
-
* Returns a payload whose `buffer` contains ONLY the pixels at offset 0,
|
|
6
|
-
* which is what `payloadToImageData` (below) expects. The copy
|
|
7
|
-
* also makes the buffer cleanly transferable back to the main thread.
|
|
8
|
-
*/
|
|
9
|
-
/** ImageData -> worker payload (copies the buffer so the original isn't detached). */
|
|
10
|
-
export function imageDataToPayload(img) {
|
|
11
|
-
if (!img) return null;
|
|
12
|
-
const copy = new Uint8ClampedArray(img.data);
|
|
13
|
-
return { width: img.width, height: img.height, buffer: copy.buffer };
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/** Worker payload (pixels at offset 0) -> ImageData. */
|
|
17
|
-
export function payloadToImageData(payload) {
|
|
18
|
-
if (!payload) return null;
|
|
19
|
-
const expected = payload.width * payload.height * 4;
|
|
20
|
-
const bytes = new Uint8ClampedArray(payload.buffer, 0, expected);
|
|
21
|
-
return new ImageData(bytes, payload.width, payload.height);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function unpackCoreImage(packed) {
|
|
25
|
-
const view = new DataView(packed.buffer, packed.byteOffset, packed.byteLength);
|
|
26
|
-
const width = view.getUint32(0, true);
|
|
27
|
-
const height = view.getUint32(4, true);
|
|
28
|
-
const pixels = new Uint8ClampedArray(
|
|
29
|
-
packed.buffer,
|
|
30
|
-
packed.byteOffset + 8,
|
|
31
|
-
width * height * 4,
|
|
32
|
-
);
|
|
33
|
-
const copy = pixels.slice(); // fresh buffer, offset 0, exact length
|
|
34
|
-
return { width, height, buffer: copy.buffer };
|
|
35
|
-
}
|
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pure-JS replacements for OpenCV steps in the pixel-art pipeline.
|
|
3
|
-
* Ported from experiments/lib/cvfree.mjs + detectors.mjs (experiment/headless).
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
function maxPeriod(n) {
|
|
7
|
-
return Math.max(2, Math.min(Math.floor(n / 8), 128));
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function median(arr) {
|
|
11
|
-
if (!arr.length) return 0;
|
|
12
|
-
const s = [...arr].sort((a, b) => a - b);
|
|
13
|
-
const m = s.length >> 1;
|
|
14
|
-
return s.length % 2 ? s[m] : (s[m - 1] + s[m]) / 2;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function reconcileClose(a, b) {
|
|
18
|
-
if (Math.abs(a - b) <= Math.max(1, 0.12 * Math.min(a, b))) return Math.min(a, b);
|
|
19
|
-
if (a === b) return a;
|
|
20
|
-
const g = gcd(a, b);
|
|
21
|
-
return g >= 2 ? g : Math.min(a, b);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function gcd(a, b) {
|
|
25
|
-
while (b) { [a, b] = [b, a % b]; }
|
|
26
|
-
return a;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function boundarySignals(img) {
|
|
30
|
-
const { data, width, height } = img;
|
|
31
|
-
const col = new Float64Array(width);
|
|
32
|
-
const row = new Float64Array(height);
|
|
33
|
-
for (let y = 0; y < height; y++) {
|
|
34
|
-
for (let x = 1; x < width; x++) {
|
|
35
|
-
const i = (y * width + x) * 4;
|
|
36
|
-
const j = i - 4;
|
|
37
|
-
col[x] += Math.abs(data[i] - data[j]) + Math.abs(data[i + 1] - data[j + 1])
|
|
38
|
-
+ Math.abs(data[i + 2] - data[j + 2]) + Math.abs(data[i + 3] - data[j + 3]);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
for (let x = 0; x < width; x++) {
|
|
42
|
-
for (let y = 1; y < height; y++) {
|
|
43
|
-
const i = (y * width + x) * 4;
|
|
44
|
-
const j = ((y - 1) * width + x) * 4;
|
|
45
|
-
row[y] += Math.abs(data[i] - data[j]) + Math.abs(data[i + 1] - data[j + 1])
|
|
46
|
-
+ Math.abs(data[i + 2] - data[j + 2]) + Math.abs(data[i + 3] - data[j + 3]);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return { col, row };
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export function peakLag(sig) {
|
|
53
|
-
const n = sig.length;
|
|
54
|
-
const maxLag = maxPeriod(n);
|
|
55
|
-
let mean = 0;
|
|
56
|
-
for (let i = 0; i < n; i++) mean += sig[i];
|
|
57
|
-
mean /= n;
|
|
58
|
-
const s = new Float64Array(n);
|
|
59
|
-
let norm = 0;
|
|
60
|
-
for (let i = 0; i < n; i++) { s[i] = sig[i] - mean; norm += s[i] * s[i]; }
|
|
61
|
-
norm = norm || 1e-9;
|
|
62
|
-
const ac = new Float64Array(maxLag + 2);
|
|
63
|
-
for (let lag = 2; lag <= maxLag; lag++) {
|
|
64
|
-
let r = 0;
|
|
65
|
-
for (let i = 0; i + lag < n; i++) r += s[i] * s[i + lag];
|
|
66
|
-
ac[lag] = r / norm;
|
|
67
|
-
}
|
|
68
|
-
let start = 2;
|
|
69
|
-
const cap = Math.max(3, Math.floor(maxLag / 2));
|
|
70
|
-
while (start < cap && ac[start + 1] <= ac[start]) start++;
|
|
71
|
-
|
|
72
|
-
let gmax = 0;
|
|
73
|
-
let gp = start;
|
|
74
|
-
for (let p = start; p <= maxLag; p++) if (ac[p] > gmax) { gmax = ac[p]; gp = p; }
|
|
75
|
-
if (gmax <= 0) return 1;
|
|
76
|
-
const thr = 0.6 * gmax;
|
|
77
|
-
for (let p = start; p <= maxLag; p++) {
|
|
78
|
-
if (ac[p] >= thr && ac[p] >= ac[p - 1] && ac[p] >= ac[p + 1]) return p;
|
|
79
|
-
}
|
|
80
|
-
return gp;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** Cv-free edge scale detector (acpeak). */
|
|
84
|
-
export function acpeakDetect(img) {
|
|
85
|
-
const { col, row } = boundarySignals(img);
|
|
86
|
-
return reconcileClose(peakLag(col), peakLag(row));
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function morph2x2(img, useMax) {
|
|
90
|
-
const { data, width: W, height: H } = img;
|
|
91
|
-
const out = new Uint8ClampedArray(data.length);
|
|
92
|
-
const nb = [[-1, -1], [0, -1], [-1, 0], [0, 0]];
|
|
93
|
-
for (let y = 0; y < H; y++) {
|
|
94
|
-
for (let x = 0; x < W; x++) {
|
|
95
|
-
const o = (y * W + x) * 4;
|
|
96
|
-
for (let ch = 0; ch < 4; ch++) {
|
|
97
|
-
let v = useMax ? 0 : 255;
|
|
98
|
-
for (let k = 0; k < 4; k++) {
|
|
99
|
-
const nx = x + nb[k][0];
|
|
100
|
-
const ny = y + nb[k][1];
|
|
101
|
-
if (nx < 0 || ny < 0 || nx >= W || ny >= H) continue;
|
|
102
|
-
const s = data[(ny * W + nx) * 4 + ch];
|
|
103
|
-
v = useMax ? (s > v ? s : v) : (s < v ? s : v);
|
|
104
|
-
}
|
|
105
|
-
out[o + ch] = v;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
return new ImageData(out, W, H);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const erode = (img) => morph2x2(img, false);
|
|
113
|
-
const dilate = (img) => morph2x2(img, true);
|
|
114
|
-
|
|
115
|
-
/** OPEN then CLOSE with 2×2 kernel, anchor (1,1) — matches OpenCV preset. */
|
|
116
|
-
export function morphologicalCleanupCvFree(img) {
|
|
117
|
-
return erode(dilate(dilate(erode(img))));
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export function findOptimalCropOffset(img, scale) {
|
|
121
|
-
const { data, width: W, height: H } = img;
|
|
122
|
-
const g = new Float64Array(W * H);
|
|
123
|
-
for (let i = 0, p = 0; i < data.length; i += 4, p++) {
|
|
124
|
-
g[p] = Math.round(0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2]);
|
|
125
|
-
}
|
|
126
|
-
const refl = (i, n) => {
|
|
127
|
-
if (n === 1) return 0;
|
|
128
|
-
while (i < 0 || i >= n) {
|
|
129
|
-
if (i < 0) i = -i;
|
|
130
|
-
if (i >= n) i = 2 * (n - 1) - i;
|
|
131
|
-
}
|
|
132
|
-
return i;
|
|
133
|
-
};
|
|
134
|
-
const kx = [-1, 0, 1, -2, 0, 2, -1, 0, 1];
|
|
135
|
-
const ky = [-1, -2, -1, 0, 0, 0, 1, 2, 1];
|
|
136
|
-
const profX = new Float64Array(W);
|
|
137
|
-
const profY = new Float64Array(H);
|
|
138
|
-
for (let y = 0; y < H; y++) {
|
|
139
|
-
for (let x = 0; x < W; x++) {
|
|
140
|
-
let sx = 0;
|
|
141
|
-
let sy = 0;
|
|
142
|
-
let k = 0;
|
|
143
|
-
for (let dy = -1; dy <= 1; dy++) {
|
|
144
|
-
for (let dx = -1; dx <= 1; dx++, k++) {
|
|
145
|
-
const v = g[refl(y + dy, H) * W + refl(x + dx, W)];
|
|
146
|
-
sx += v * kx[k];
|
|
147
|
-
sy += v * ky[k];
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
profX[x] += Math.abs(sx);
|
|
151
|
-
profY[y] += Math.abs(sy);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
const best = (prof, s) => {
|
|
155
|
-
let bo = 0;
|
|
156
|
-
let ms = -1;
|
|
157
|
-
for (let o = 0; o < s; o++) {
|
|
158
|
-
let c = 0;
|
|
159
|
-
for (let i = o; i < prof.length; i += s) c += prof[i] || 0;
|
|
160
|
-
if (c > ms) { ms = c; bo = o; }
|
|
161
|
-
}
|
|
162
|
-
return bo;
|
|
163
|
-
};
|
|
164
|
-
return { x: best(profX, scale), y: best(profY, scale) };
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
export function cropImageData(img, x, y, w, h) {
|
|
168
|
-
const out = new Uint8ClampedArray(w * h * 4);
|
|
169
|
-
for (let ty = 0; ty < h; ty++) {
|
|
170
|
-
for (let tx = 0; tx < w; tx++) {
|
|
171
|
-
const si = ((y + ty) * img.width + (x + tx)) * 4;
|
|
172
|
-
const di = (ty * w + tx) * 4;
|
|
173
|
-
out[di] = img.data[si];
|
|
174
|
-
out[di + 1] = img.data[si + 1];
|
|
175
|
-
out[di + 2] = img.data[si + 2];
|
|
176
|
-
out[di + 3] = img.data[si + 3];
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
return new ImageData(out, w, h);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/** Snap image to pixel grid using cv-free crop search. */
|
|
183
|
-
export function snapToGrid(img, scale, logger) {
|
|
184
|
-
const crop = findOptimalCropOffset(img, scale);
|
|
185
|
-
const newWidth = Math.floor((img.width - crop.x) / scale) * scale;
|
|
186
|
-
const newHeight = Math.floor((img.height - crop.y) / scale) * scale;
|
|
187
|
-
if (newWidth < scale || newHeight < scale) {
|
|
188
|
-
logger?.warn?.(`Snapping failed: resulting image size (${newWidth}x${newHeight}) is too small. Skipping snap.`);
|
|
189
|
-
return img;
|
|
190
|
-
}
|
|
191
|
-
logger?.log?.(`Image cropped to: ${newWidth} x ${newHeight} (from offset x:${crop.x}, y:${crop.y})`);
|
|
192
|
-
return cropImageData(img, crop.x, crop.y, newWidth, newHeight);
|
|
193
|
-
}
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Canonical tiled edge detector (synced from experiments/lib/edge-detect.mjs).
|
|
3
|
-
* JS fallback when unfake-core WASM is unavailable.
|
|
4
|
-
*/
|
|
5
|
-
import { peakLag } from './cvfree.js';
|
|
6
|
-
|
|
7
|
-
function mode(arr) {
|
|
8
|
-
if (!arr.length) return 0;
|
|
9
|
-
const counts = {};
|
|
10
|
-
let max = 0;
|
|
11
|
-
let res = arr[0];
|
|
12
|
-
for (const v of arr) {
|
|
13
|
-
counts[v] = (counts[v] || 0) + 1;
|
|
14
|
-
if (counts[v] > max) { max = counts[v]; res = v; }
|
|
15
|
-
}
|
|
16
|
-
return res;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const KX = [-1, 0, 1, -2, 0, 2, -1, 0, 1];
|
|
20
|
-
const KY = [-1, -2, -1, 0, 0, 0, 1, 2, 1];
|
|
21
|
-
|
|
22
|
-
function reflect(i, n) {
|
|
23
|
-
if (n === 1) return 0;
|
|
24
|
-
while (i < 0 || i >= n) {
|
|
25
|
-
if (i < 0) i = -i;
|
|
26
|
-
if (i >= n) i = 2 * (n - 1) - i;
|
|
27
|
-
}
|
|
28
|
-
return i;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function toGray(img) {
|
|
32
|
-
const { data, width: W, height: H } = img;
|
|
33
|
-
const g = new Float64Array(W * H);
|
|
34
|
-
for (let i = 0, p = 0; i < data.length; i += 4, p++) {
|
|
35
|
-
g[p] = 0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2];
|
|
36
|
-
}
|
|
37
|
-
return { g, W, H };
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function sobelProfiles(g, W, H, x0, y0, rw, rh) {
|
|
41
|
-
const profX = new Float64Array(rw);
|
|
42
|
-
const profY = new Float64Array(rh);
|
|
43
|
-
for (let ly = 0; ly < rh; ly++) {
|
|
44
|
-
for (let lx = 0; lx < rw; lx++) {
|
|
45
|
-
const y = y0 + ly;
|
|
46
|
-
const x = x0 + lx;
|
|
47
|
-
let sx = 0;
|
|
48
|
-
let sy = 0;
|
|
49
|
-
let k = 0;
|
|
50
|
-
for (let dy = -1; dy <= 1; dy++) {
|
|
51
|
-
for (let dx = -1; dx <= 1; dx++, k++) {
|
|
52
|
-
const v = g[reflect(y + dy, H) * W + reflect(x + dx, W)];
|
|
53
|
-
sx += v * KX[k];
|
|
54
|
-
sy += v * KY[k];
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
profX[lx] += Math.abs(sx);
|
|
58
|
-
profY[ly] += Math.abs(sy);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return { profX, profY };
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function regionStddev(g, W, x0, y0, rw, rh) {
|
|
65
|
-
let sum = 0;
|
|
66
|
-
let sum2 = 0;
|
|
67
|
-
const n = rw * rh;
|
|
68
|
-
for (let ly = 0; ly < rh; ly++) {
|
|
69
|
-
for (let lx = 0; lx < rw; lx++) {
|
|
70
|
-
const v = g[(y0 + ly) * W + (x0 + lx)];
|
|
71
|
-
sum += v;
|
|
72
|
-
sum2 += v * v;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
const mean = sum / n;
|
|
76
|
-
return Math.sqrt(Math.max(0, sum2 / n - mean * mean));
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function scalesFromRoi(g, W, H, x0, y0, rw, rh) {
|
|
80
|
-
if (rw < 3 || rh < 3) return { h: 1, v: 1 };
|
|
81
|
-
const { profX, profY } = sobelProfiles(g, W, H, x0, y0, rw, rh);
|
|
82
|
-
return { h: peakLag(profX), v: peakLag(profY) };
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function singleRegionEdgeDetect(img) {
|
|
86
|
-
const { g, W, H } = toGray(img);
|
|
87
|
-
const x0 = Math.floor(W * 0.125);
|
|
88
|
-
const y0 = Math.floor(H * 0.125);
|
|
89
|
-
const rw = Math.floor(W * 0.75);
|
|
90
|
-
const rh = Math.floor(H * 0.75);
|
|
91
|
-
if (rw < 3 || rh < 3) return 1;
|
|
92
|
-
const { h: hScale, v: vScale } = scalesFromRoi(g, W, H, x0, y0, rw, rh);
|
|
93
|
-
if (hScale > 1 && vScale > 1 && Math.abs(hScale - vScale) <= 2) {
|
|
94
|
-
return Math.round((hScale + vScale) / 2);
|
|
95
|
-
}
|
|
96
|
-
return Math.max(hScale, vScale, 1);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export function tiledEdgeDetect(img) {
|
|
100
|
-
const { g, W, H } = toGray(img);
|
|
101
|
-
const TILE_COUNT = 3;
|
|
102
|
-
const OVERLAP = 0.25;
|
|
103
|
-
const tileW = Math.floor(W / TILE_COUNT);
|
|
104
|
-
const tileH = Math.floor(H / TILE_COUNT);
|
|
105
|
-
const overlapW = Math.floor(tileW * OVERLAP);
|
|
106
|
-
const overlapH = Math.floor(tileH * OVERLAP);
|
|
107
|
-
|
|
108
|
-
if (tileW < 50 || tileH < 50) return singleRegionEdgeDetect(img);
|
|
109
|
-
|
|
110
|
-
const allScales = [];
|
|
111
|
-
for (let ty = 0; ty < TILE_COUNT; ty++) {
|
|
112
|
-
for (let tx = 0; tx < TILE_COUNT; tx++) {
|
|
113
|
-
const x0 = Math.max(0, tx * tileW - overlapW);
|
|
114
|
-
const y0 = Math.max(0, ty * tileH - overlapH);
|
|
115
|
-
const rw = Math.min(W - x0, tileW + 2 * overlapW);
|
|
116
|
-
const rh = Math.min(H - y0, tileH + 2 * overlapH);
|
|
117
|
-
if (rw < 30 || rh < 30) continue;
|
|
118
|
-
if (regionStddev(g, W, x0, y0, rw, rh) < 5.0) continue;
|
|
119
|
-
const { h: hScale, v: vScale } = scalesFromRoi(g, W, H, x0, y0, rw, rh);
|
|
120
|
-
if (hScale > 1) allScales.push(hScale);
|
|
121
|
-
if (vScale > 1) allScales.push(vScale);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (allScales.length === 0) return singleRegionEdgeDetect(img);
|
|
126
|
-
return mode(allScales) || 1;
|
|
127
|
-
}
|