@xprite/pixelate 0.1.3 → 0.2.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 +14 -0
- package/README.zh-CN.md +14 -0
- package/dist/base.d.ts +4 -0
- package/dist/base.js +33 -0
- package/dist/deskew.js +28 -15
- package/dist/fft-autocorr.d.ts +12 -0
- package/dist/fft-autocorr.js +116 -0
- package/dist/float-image.d.ts +25 -0
- package/dist/float-image.js +184 -0
- package/dist/grid-arbitrate.d.ts +8 -0
- package/dist/grid-arbitrate.js +219 -65
- package/dist/grid-cut-snap.js +2 -38
- package/dist/grid-description-length.js +416 -111
- package/dist/grid-edge-coherence.js +4 -46
- package/dist/grid-edge-path.js +2 -33
- package/dist/grid-frame-lattice.js +109 -14
- package/dist/grid-period-comb.js +126 -22
- package/dist/grid-period-distance.js +59 -10
- package/dist/grid-pitch-lattice.js +21 -9
- package/dist/grid-pitch-search.js +38 -18
- package/dist/grid-sample.js +2 -2
- package/dist/grid-sampling.d.ts +6 -0
- package/dist/grid-sampling.js +111 -10
- package/dist/grid-step-profiles.d.ts +13 -0
- package/dist/grid-step-profiles.js +88 -0
- package/dist/grid-strip-shift.js +153 -35
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/kuwahara.d.ts +9 -0
- package/dist/kuwahara.js +287 -0
- package/dist/line-guided/pixeloe-entry.d.ts +2 -2
- package/dist/line-guided/pixeloe-entry.js +6 -35
- package/dist/orphan-pixels.d.ts +7 -0
- package/dist/orphan-pixels.js +133 -0
- package/dist/palette-snap.d.ts +1 -1
- package/dist/palette-snap.js +127 -58
- package/dist/pixelate.d.ts +4 -7
- package/dist/pixelate.js +133 -98
- package/dist/quantization.d.ts +4 -0
- package/dist/quantization.js +23 -16
- package/dist/resize-crisp.d.ts +11 -0
- package/dist/resize-crisp.js +213 -0
- package/dist/resize-lanczos.d.ts +7 -0
- package/dist/resize-lanczos.js +205 -0
- package/dist/sampling.d.ts +2 -1
- package/dist/sampling.js +1 -1
- package/dist/settings.d.ts +6 -3
- package/dist/settings.js +26 -12
- package/dist/types.d.ts +14 -2
- package/dist/types.js +2 -2
- package/package.json +1 -1
- package/dist/contours.d.ts +0 -4
- package/dist/contours.js +0 -66
- package/dist/most-frequent.d.ts +0 -3
- package/dist/most-frequent.js +0 -56
package/README.md
CHANGED
|
@@ -9,6 +9,8 @@ Recover the real pixel grid of AI-generated pixel art and get back one clean pix
|
|
|
9
9
|
also turns ordinary images into pixel art. It runs in browsers and Node, has no runtime
|
|
10
10
|
dependencies, and is the importer of the [Xprite](https://xprite.cc) pixel editor.
|
|
11
11
|
|
|
12
|
+
> **Live demo & write-up:** [Pixel Art Restoration](https://outtascope.com/writing/pixel-art-restoration/) | [Image to Pixel Art](https://outtascope.com/writing/image-to-pixel-art/) | [Open Xprite Editor](https://xprite.cc) | [All Xprite projects](#xprite-ecosystem)
|
|
13
|
+
|
|
12
14
|
## Compared with other tools
|
|
13
15
|
|
|
14
16
|
A real generated image (887×887, cells 15 to 21 px wide), recovered by each tool with its default
|
|
@@ -37,6 +39,7 @@ often:
|
|
|
37
39
|
- [Usage](#usage)
|
|
38
40
|
- [How it works](#how-it-works)
|
|
39
41
|
- [Benchmark](#benchmark)
|
|
42
|
+
- [Xprite Ecosystem](#xprite-ecosystem)
|
|
40
43
|
- [License](#license)
|
|
41
44
|
|
|
42
45
|
## Examples
|
|
@@ -266,6 +269,17 @@ Pixel match, ΔE and fringe are measured only on each method's exact-size cases.
|
|
|
266
269
|
|
|
267
270
|
Each method uses its default color processing, so these are end-to-end results with different palettes. Timing methods also differ; measurements that include process startup and PNG I/O are omitted from the table. Results use the same cases and scoring code; some were recorded in an earlier run. Synthetic damage does not establish real-world success rates. See [bench/README.md](bench/README.md) for method versions, timing and reproduction.
|
|
268
271
|
|
|
272
|
+
<!-- xprite-ecosystem:start -->
|
|
273
|
+
## Xprite Ecosystem
|
|
274
|
+
|
|
275
|
+
| Project | Description | Links |
|
|
276
|
+
| :--- | :--- | :--- |
|
|
277
|
+
| [Xprite](https://github.com/rhinoc/xprite) | Browser-based pixel art & animation editor | [Open Editor](https://xprite.cc) |
|
|
278
|
+
| **@xprite/pixelate** *(this repo)* | Recover pixel grids from AI/upscaled art and pixelate images | [npm](https://www.npmjs.com/package/@xprite/pixelate), [Demo & Write-up](https://outtascope.com/writing/pixel-art-restoration/) ([Pixelation](https://outtascope.com/writing/image-to-pixel-art/)) |
|
|
279
|
+
| [@xprite/lighter](https://github.com/rhinoc/xprite-lighter) | Generate normal maps, light masks, and relighted renders for pixel art | [npm](https://www.npmjs.com/package/@xprite/lighter), [Demo & Write-up](https://outtascope.com/writing/pixel-art-lighting/) |
|
|
280
|
+
| [@xprite/phase](https://github.com/rhinoc/xprite-phase) | 2.5D layered geometry, phase-field weathering, and particles for pixel art | [npm](https://www.npmjs.com/package/@xprite/phase), [Demo & Write-up](https://outtascope.com/writing/pixel-art-phase/) |
|
|
281
|
+
<!-- xprite-ecosystem:end -->
|
|
282
|
+
|
|
269
283
|
## License
|
|
270
284
|
|
|
271
285
|
MIT. Parts are adapted from pixel-art-fixer, PixelOE, proper-pixel-art and tensorhead/dithering
|
package/README.zh-CN.md
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
|
|
8
8
|
找出 AI 生成像素画中真正的像素网格,每一格还原成一个干净的像素。它也能把普通图片转成像素画。它可以在浏览器和 Node 中运行,没有运行时依赖,是像素编辑器 [Xprite](https://xprite.cc) 的导入器。
|
|
9
9
|
|
|
10
|
+
> **在线演示与原理文章:** [像素图修复](https://outtascope.com/zh/writing/pixel-art-restoration/) | [图片转像素画](https://outtascope.com/zh/writing/image-to-pixel-art/) | [打开 Xprite 编辑器](https://xprite.cc) | [全部 Xprite 项目](#xprite-生态)
|
|
11
|
+
|
|
10
12
|
## 与其他工具对比
|
|
11
13
|
|
|
12
14
|
一张真实的 AI 生成图(887×887,格宽 15 到 21 像素不等),各工具用默认设置还原的结果。Xprite 把每一刀都切在颜色边缘上,所以不会切开眼睛、靴子和剑。
|
|
@@ -32,6 +34,7 @@
|
|
|
32
34
|
- [用法](#用法)
|
|
33
35
|
- [原理](#原理)
|
|
34
36
|
- [基准测试](#基准测试)
|
|
37
|
+
- [Xprite 生态](#xprite-生态)
|
|
35
38
|
- [许可证](#许可证)
|
|
36
39
|
|
|
37
40
|
## 示例
|
|
@@ -240,6 +243,17 @@ AI 套件(12 个类别共 1746 个用例,按生成模型伪造像素画的
|
|
|
240
243
|
|
|
241
244
|
各方法使用默认颜色处理方式,调色板可能不同;表中是端到端结果。计时方式也有差异,包含进程启动和 PNG 读写的计时未列入表格。所有结果使用相同的用例和评分代码,部分结果来自先前的运行。损伤是合成的,不能代表真实图片的成功率。各方法的版本、计时口径和复现方法见 [bench/README.md](bench/README.md)。
|
|
242
245
|
|
|
246
|
+
<!-- xprite-ecosystem:start -->
|
|
247
|
+
## Xprite 生态
|
|
248
|
+
|
|
249
|
+
| 项目 | 简介 | 链接 |
|
|
250
|
+
| :--- | :--- | :--- |
|
|
251
|
+
| [Xprite](https://github.com/rhinoc/xprite) | 浏览器里的像素画与动画编辑器 | [打开编辑器](https://xprite.cc) |
|
|
252
|
+
| **@xprite/pixelate**(当前仓库) | 还原 AI/放大像素画的真实网格,以及图片像素化 | [npm](https://www.npmjs.com/package/@xprite/pixelate)、[演示与原理](https://outtascope.com/zh/writing/pixel-art-restoration/)([图片像素化](https://outtascope.com/zh/writing/image-to-pixel-art/)) |
|
|
253
|
+
| [@xprite/lighter](https://github.com/rhinoc/xprite-lighter) | 为像素画生成法线贴图、光照遮罩与重光照效果 | [npm](https://www.npmjs.com/package/@xprite/lighter)、[演示与原理](https://outtascope.com/zh/writing/pixel-art-lighting/) |
|
|
254
|
+
| [@xprite/phase](https://github.com/rhinoc/xprite-phase) | 面向 2D 像素画的 2.5D 几何、相场风化与粒子引擎 | [npm](https://www.npmjs.com/package/@xprite/phase)、[演示与原理](https://outtascope.com/zh/writing/pixel-art-phase/) |
|
|
255
|
+
<!-- xprite-ecosystem:end -->
|
|
256
|
+
|
|
243
257
|
## 许可证
|
|
244
258
|
|
|
245
259
|
MIT。部分代码改编自 pixel-art-fixer、PixelOE、proper-pixel-art 和 tensorhead/dithering(见 [NOTICE.md](NOTICE.md))。示例图均为 CC0(见 [bench/corpus/CREDITS.md](bench/corpus/CREDITS.md))。
|
package/dist/base.d.ts
CHANGED
|
@@ -16,3 +16,7 @@ export declare function clampByte(value: number): number;
|
|
|
16
16
|
export declare function assertDimension(value: number, label: string): number;
|
|
17
17
|
export declare function assertPixelCount(width: number, height: number, label?: string): void;
|
|
18
18
|
export declare function assertPixelBuffer(image: PixelBuffer, label?: string): void;
|
|
19
|
+
/** Two independent 32-bit hashes (FNV-1a + multiplicative mix) over RGBA pixels. */
|
|
20
|
+
export declare function fingerprintBuffer(data: Uint8ClampedArray): string;
|
|
21
|
+
/** Small per-callsite LRU cache returning defensive pixel copies. */
|
|
22
|
+
export declare function createBufferMemo(limit?: number): (image: PixelBuffer, variant: string, compute: () => PixelBuffer) => PixelBuffer;
|
package/dist/base.js
CHANGED
|
@@ -40,3 +40,36 @@ export function assertPixelBuffer(image, label = "image") {
|
|
|
40
40
|
throw new RangeError(`${label}.data must contain exactly ${expectedLength} bytes`);
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
|
+
const FNV_OFFSET = 0x811c9dc5;
|
|
44
|
+
const FNV_PRIME = 0x01000193;
|
|
45
|
+
const MIX_PRIME = 0x9e3779b1;
|
|
46
|
+
const DEFAULT_MEMO_SIZE = 4;
|
|
47
|
+
const MAX_REMEMBERED_PIXELS = 2_097_152;
|
|
48
|
+
/** Two independent 32-bit hashes (FNV-1a + multiplicative mix) over RGBA pixels. */
|
|
49
|
+
export function fingerprintBuffer(data) {
|
|
50
|
+
const bytes = data.byteOffset % 4 ? data.slice() : data;
|
|
51
|
+
const words = new Uint32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength >> 2);
|
|
52
|
+
let a = FNV_OFFSET;
|
|
53
|
+
let b = 0;
|
|
54
|
+
for (let i = 0; i < words.length; i++) {
|
|
55
|
+
a = Math.imul(a ^ words[i], FNV_PRIME);
|
|
56
|
+
b = Math.imul(b + words[i], MIX_PRIME) ^ (b >>> 15);
|
|
57
|
+
}
|
|
58
|
+
return `${(a >>> 0).toString(36)}${(b >>> 0).toString(36)}`;
|
|
59
|
+
}
|
|
60
|
+
/** Small per-callsite LRU cache returning defensive pixel copies. */
|
|
61
|
+
export function createBufferMemo(limit = DEFAULT_MEMO_SIZE) {
|
|
62
|
+
const entries = [];
|
|
63
|
+
return (image, variant, compute) => {
|
|
64
|
+
const key = `${image.width}x${image.height}:${fingerprintBuffer(image.data)}:${variant}`;
|
|
65
|
+
const hit = entries.find((entry) => entry.key === key);
|
|
66
|
+
if (hit)
|
|
67
|
+
return { ...hit.result, data: hit.result.data.slice() };
|
|
68
|
+
const result = compute();
|
|
69
|
+
if (result.width * result.height <= MAX_REMEMBERED_PIXELS) {
|
|
70
|
+
entries.unshift({ key, result: { ...result, data: result.data.slice() } });
|
|
71
|
+
entries.length = Math.min(entries.length, limit);
|
|
72
|
+
}
|
|
73
|
+
return result;
|
|
74
|
+
};
|
|
75
|
+
}
|
package/dist/deskew.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { neighbourSteps, stepOf } from "./color-step.js";
|
|
1
|
+
import { neighbourSteps, OPAQUE_STEPS, stepOf, TRANSLUCENT } from "./color-step.js";
|
|
2
2
|
/**
|
|
3
3
|
* Tilt estimation and straightening for pixel grids that were photographed, scanned or rotated a
|
|
4
4
|
* little before they reached the importer. The tilt is the angle that makes the projection
|
|
@@ -49,13 +49,21 @@ function collectEdges(image) {
|
|
|
49
49
|
let vertical = 0, horizontal = 0;
|
|
50
50
|
for (let y = 0, p = 0; y < height; y++)
|
|
51
51
|
for (let x = 0; x < width; x++, p++) {
|
|
52
|
-
if (x + 1 < width
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
if (x + 1 < width) {
|
|
53
|
+
const rc = right[p];
|
|
54
|
+
const rStep = rc < TRANSLUCENT ? OPAQUE_STEPS[rc] : stepOf(right, data, p, 1);
|
|
55
|
+
if (rStep > EDGE_STEP) {
|
|
56
|
+
flags[p] |= VERTICAL_EDGE;
|
|
57
|
+
vertical++;
|
|
58
|
+
}
|
|
55
59
|
}
|
|
56
|
-
if (y + 1 < height
|
|
57
|
-
|
|
58
|
-
|
|
60
|
+
if (y + 1 < height) {
|
|
61
|
+
const dc = down[p];
|
|
62
|
+
const dStep = dc < TRANSLUCENT ? OPAQUE_STEPS[dc] : stepOf(down, data, p, width);
|
|
63
|
+
if (dStep > EDGE_STEP) {
|
|
64
|
+
flags[p] |= HORIZONTAL_EDGE;
|
|
65
|
+
horizontal++;
|
|
66
|
+
}
|
|
59
67
|
}
|
|
60
68
|
}
|
|
61
69
|
return {
|
|
@@ -71,13 +79,18 @@ function sharpness(points, degrees, scale, bins, offset) {
|
|
|
71
79
|
const cos = Math.cos(degrees * DEGREES) * scale, sin = Math.sin(degrees * DEGREES) * scale;
|
|
72
80
|
const { vertical, horizontal } = points;
|
|
73
81
|
// Vertical edges project on u = x cos + y sin, horizontal edges on v = -x sin + y cos.
|
|
82
|
+
// With offset = longest * scale and |degrees| <= MAX_TILT_DEGREES, projections are strictly >= 0.
|
|
74
83
|
bins.fill(0);
|
|
75
|
-
for (let i = 0
|
|
76
|
-
|
|
84
|
+
for (let i = 0, len = vertical.length; i < len; i += 2) {
|
|
85
|
+
const val = vertical[i] * cos + vertical[i + 1] * sin + offset;
|
|
86
|
+
bins[val >= 0 ? val | 0 : Math.floor(val)]++;
|
|
87
|
+
}
|
|
77
88
|
let total = squaredCounts(bins);
|
|
78
89
|
bins.fill(0);
|
|
79
|
-
for (let i = 0
|
|
80
|
-
|
|
90
|
+
for (let i = 0, len = horizontal.length; i < len; i += 2) {
|
|
91
|
+
const val = -horizontal[i] * sin + horizontal[i + 1] * cos + offset;
|
|
92
|
+
bins[val >= 0 ? val | 0 : Math.floor(val)]++;
|
|
93
|
+
}
|
|
81
94
|
total += squaredCounts(bins);
|
|
82
95
|
return total / Math.max(1, (vertical.length + horizontal.length) / 2);
|
|
83
96
|
}
|
|
@@ -117,15 +130,15 @@ function measureTilt(image) {
|
|
|
117
130
|
return UPRIGHT;
|
|
118
131
|
const bins = new Int32Array(Math.ceil(3 * longest) + 8);
|
|
119
132
|
const offset = longest;
|
|
120
|
-
const evaluate = (points, degrees, scale) => sharpness(points, degrees, scale, bins, offset * scale);
|
|
121
133
|
// Coarse pass on a shrunken copy of the coordinates so the narrow peak is not stepped over.
|
|
122
134
|
const coarseScale = Math.min(1, COARSE_SIZE / longest);
|
|
135
|
+
const coarseBins = coarseScale < 1 ? new Int32Array(Math.ceil(3 * longest * coarseScale) + 8) : bins;
|
|
123
136
|
const coarsePoints = scaled(edges, coarseScale);
|
|
124
137
|
let best = 0, bestScore = -Infinity;
|
|
125
138
|
const scores = new Map();
|
|
126
139
|
for (let step = -MAX_TILT_DEGREES / COARSE_STEP; step <= MAX_TILT_DEGREES / COARSE_STEP; step++) {
|
|
127
140
|
const degrees = Math.round(step * COARSE_STEP * 1000) / 1000;
|
|
128
|
-
const score =
|
|
141
|
+
const score = sharpness(coarsePoints, degrees, coarseScale, coarseBins, offset * coarseScale);
|
|
129
142
|
scores.set(degrees, score);
|
|
130
143
|
if (score > bestScore) {
|
|
131
144
|
bestScore = score;
|
|
@@ -136,13 +149,13 @@ function measureTilt(image) {
|
|
|
136
149
|
const fineStep = Math.max(0.005, Math.min(0.05, 1 / longest / DEGREES / 2));
|
|
137
150
|
let fine = best, fineScore = -Infinity;
|
|
138
151
|
for (let degrees = best - FINE_SPAN; degrees <= best + FINE_SPAN + 1e-9; degrees += fineStep) {
|
|
139
|
-
const score =
|
|
152
|
+
const score = sharpness(edges, degrees, 1, bins, offset);
|
|
140
153
|
if (score > fineScore) {
|
|
141
154
|
fineScore = score;
|
|
142
155
|
fine = degrees;
|
|
143
156
|
}
|
|
144
157
|
}
|
|
145
|
-
const upright =
|
|
158
|
+
const upright = sharpness(edges, 0, 1, bins, offset);
|
|
146
159
|
const gain = upright > 0 ? fineScore / upright : 1;
|
|
147
160
|
const angle = Math.round(fine * 1000) / 1000;
|
|
148
161
|
if (Math.abs(angle) < MIN_TILT_DEGREES || gain < MIN_SHARPNESS_GAIN)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 1D Radix-2 Cooley-Tukey FFT autocorrelation for real signals.
|
|
3
|
+
*
|
|
4
|
+
* Computes the unnormalized linear autocorrelation R[lag] = sum_{i=0}^{n-1-lag} x[i] * x[i+lag]
|
|
5
|
+
* for lag = 0..maxLag in O(N log N) time where N = nextPow2(n + maxLag), matching the naive
|
|
6
|
+
* O(n * maxLag) inner product to floating-point precision (~1e-12).
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Computes linear autocorrelation `out[lag] = sum_{i=0}^{n-1-lag} signal[i] * signal[i+lag]`
|
|
10
|
+
* for `0 <= lag <= maxLag`. Uses direct summation for small inputs where FFT overhead exceeds it.
|
|
11
|
+
*/
|
|
12
|
+
export declare function linearAutocorrelation(signal: Float64Array, maxLag: number, out: Float64Array): void;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 1D Radix-2 Cooley-Tukey FFT autocorrelation for real signals.
|
|
3
|
+
*
|
|
4
|
+
* Computes the unnormalized linear autocorrelation R[lag] = sum_{i=0}^{n-1-lag} x[i] * x[i+lag]
|
|
5
|
+
* for lag = 0..maxLag in O(N log N) time where N = nextPow2(n + maxLag), matching the naive
|
|
6
|
+
* O(n * maxLag) inner product to floating-point precision (~1e-12).
|
|
7
|
+
*/
|
|
8
|
+
const plans = new Map();
|
|
9
|
+
function getPlan(size) {
|
|
10
|
+
let plan = plans.get(size);
|
|
11
|
+
if (plan)
|
|
12
|
+
return plan;
|
|
13
|
+
const bits = Math.round(Math.log2(size));
|
|
14
|
+
const bitrev = new Uint32Array(size);
|
|
15
|
+
for (let i = 0; i < size; i++) {
|
|
16
|
+
let x = i;
|
|
17
|
+
let r = 0;
|
|
18
|
+
for (let b = 0; b < bits; b++) {
|
|
19
|
+
r = (r << 1) | (x & 1);
|
|
20
|
+
x >>= 1;
|
|
21
|
+
}
|
|
22
|
+
bitrev[i] = r;
|
|
23
|
+
}
|
|
24
|
+
const half = size >> 1;
|
|
25
|
+
const cos = new Float64Array(half);
|
|
26
|
+
const sin = new Float64Array(half);
|
|
27
|
+
const step = (-2 * Math.PI) / size;
|
|
28
|
+
for (let k = 0; k < half; k++) {
|
|
29
|
+
const angle = k * step;
|
|
30
|
+
cos[k] = Math.cos(angle);
|
|
31
|
+
sin[k] = Math.sin(angle);
|
|
32
|
+
}
|
|
33
|
+
plan = {
|
|
34
|
+
size,
|
|
35
|
+
bitrev,
|
|
36
|
+
cos,
|
|
37
|
+
sin,
|
|
38
|
+
re: new Float64Array(size),
|
|
39
|
+
im: new Float64Array(size),
|
|
40
|
+
};
|
|
41
|
+
plans.set(size, plan);
|
|
42
|
+
return plan;
|
|
43
|
+
}
|
|
44
|
+
function fftInPlace(plan, inverse) {
|
|
45
|
+
const { size, bitrev, cos, sin, re, im } = plan;
|
|
46
|
+
for (let i = 0; i < size; i++) {
|
|
47
|
+
const j = bitrev[i];
|
|
48
|
+
if (j > i) {
|
|
49
|
+
const tr = re[i];
|
|
50
|
+
re[i] = re[j];
|
|
51
|
+
re[j] = tr;
|
|
52
|
+
const ti = im[i];
|
|
53
|
+
im[i] = im[j];
|
|
54
|
+
im[j] = ti;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const sign = inverse ? -1 : 1;
|
|
58
|
+
for (let len = 2; len <= size; len <<= 1) {
|
|
59
|
+
const half = len >> 1;
|
|
60
|
+
const stride = size / len;
|
|
61
|
+
for (let start = 0; start < size; start += len) {
|
|
62
|
+
for (let k = 0, tw = 0; k < half; k++, tw += stride) {
|
|
63
|
+
const wr = cos[tw];
|
|
64
|
+
const wi = sign * sin[tw];
|
|
65
|
+
const u = start + k;
|
|
66
|
+
const v = u + half;
|
|
67
|
+
const vr = re[v] * wr - im[v] * wi;
|
|
68
|
+
const vi = re[v] * wi + im[v] * wr;
|
|
69
|
+
re[v] = re[u] - vr;
|
|
70
|
+
im[v] = im[u] - vi;
|
|
71
|
+
re[u] += vr;
|
|
72
|
+
im[u] += vi;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (inverse) {
|
|
77
|
+
const inv = 1 / size;
|
|
78
|
+
for (let i = 0; i < size; i++) {
|
|
79
|
+
re[i] *= inv;
|
|
80
|
+
im[i] *= inv;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Computes linear autocorrelation `out[lag] = sum_{i=0}^{n-1-lag} signal[i] * signal[i+lag]`
|
|
86
|
+
* for `0 <= lag <= maxLag`. Uses direct summation for small inputs where FFT overhead exceeds it.
|
|
87
|
+
*/
|
|
88
|
+
export function linearAutocorrelation(signal, maxLag, out) {
|
|
89
|
+
const n = signal.length;
|
|
90
|
+
if (n * (maxLag + 1) <= 8192) {
|
|
91
|
+
for (let lag = 0; lag <= maxLag; lag++) {
|
|
92
|
+
let s = 0;
|
|
93
|
+
for (let i = 0, end = n - lag; i < end; i++)
|
|
94
|
+
s += signal[i] * signal[i + lag];
|
|
95
|
+
out[lag] = s;
|
|
96
|
+
}
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
let size = 1;
|
|
100
|
+
const needed = n + maxLag;
|
|
101
|
+
while (size < needed)
|
|
102
|
+
size <<= 1;
|
|
103
|
+
const plan = getPlan(size);
|
|
104
|
+
const { re, im } = plan;
|
|
105
|
+
re.set(signal);
|
|
106
|
+
re.fill(0, n);
|
|
107
|
+
im.fill(0);
|
|
108
|
+
fftInPlace(plan, false);
|
|
109
|
+
for (let i = 0; i < size; i++) {
|
|
110
|
+
re[i] = re[i] * re[i] + im[i] * im[i];
|
|
111
|
+
im[i] = 0;
|
|
112
|
+
}
|
|
113
|
+
fftInPlace(plan, true);
|
|
114
|
+
for (let lag = 0; lag <= maxLag; lag++)
|
|
115
|
+
out[lag] = re[lag];
|
|
116
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type PixelBuffer } from "./base.ts";
|
|
2
|
+
export interface FloatRgba {
|
|
3
|
+
r: Float32Array;
|
|
4
|
+
g: Float32Array;
|
|
5
|
+
b: Float32Array;
|
|
6
|
+
a: Float32Array;
|
|
7
|
+
w: number;
|
|
8
|
+
h: number;
|
|
9
|
+
opaque: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare function toFloatRgba(image: PixelBuffer): FloatRgba;
|
|
12
|
+
export declare function fromFloatRgba(img: FloatRgba): PixelBuffer;
|
|
13
|
+
export declare function toOklabPlanes(img: FloatRgba, alphaCutoff?: number): {
|
|
14
|
+
L: Float32Array<ArrayBuffer>;
|
|
15
|
+
A: Float32Array<ArrayBuffer>;
|
|
16
|
+
B: Float32Array<ArrayBuffer>;
|
|
17
|
+
};
|
|
18
|
+
export declare function resizeAreaFloat(img: FloatRgba, tw: number, th: number): FloatRgba;
|
|
19
|
+
export declare function gaussianBlur(src: Float32Array, w: number, h: number, sigma: number, radiusMult?: number): Float32Array;
|
|
20
|
+
/** Sobel gradient structure tensor (E, F, G) smoothed by `sigma`, over one or more channels. */
|
|
21
|
+
export declare function sobelTensor(channels: readonly Float32Array[], w: number, h: number, sigma: number): {
|
|
22
|
+
E: Float32Array<ArrayBufferLike>;
|
|
23
|
+
F: Float32Array<ArrayBufferLike>;
|
|
24
|
+
G: Float32Array<ArrayBufferLike>;
|
|
25
|
+
};
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { clampByte, UINT8_MAX } from "./base.js";
|
|
2
|
+
import { rgbFloatToOklab } from "./quantization.js";
|
|
3
|
+
const CHANNELS = 4;
|
|
4
|
+
export function toFloatRgba(image) {
|
|
5
|
+
const { width: w, height: h, data } = image;
|
|
6
|
+
const n = w * h;
|
|
7
|
+
const r = new Float32Array(n);
|
|
8
|
+
const g = new Float32Array(n);
|
|
9
|
+
const b = new Float32Array(n);
|
|
10
|
+
const a = new Float32Array(n);
|
|
11
|
+
let opaque = true;
|
|
12
|
+
for (let i = 0, p = 0; i < n; i++, p += CHANNELS) {
|
|
13
|
+
r[i] = data[p] / UINT8_MAX;
|
|
14
|
+
g[i] = data[p + 1] / UINT8_MAX;
|
|
15
|
+
b[i] = data[p + 2] / UINT8_MAX;
|
|
16
|
+
const alpha = data[p + 3];
|
|
17
|
+
if (alpha !== UINT8_MAX)
|
|
18
|
+
opaque = false;
|
|
19
|
+
a[i] = alpha / UINT8_MAX;
|
|
20
|
+
}
|
|
21
|
+
return { r, g, b, a, w, h, opaque };
|
|
22
|
+
}
|
|
23
|
+
export function fromFloatRgba(img) {
|
|
24
|
+
const n = img.w * img.h;
|
|
25
|
+
const data = new Uint8ClampedArray(n * CHANNELS);
|
|
26
|
+
for (let i = 0, p = 0; i < n; i++, p += CHANNELS) {
|
|
27
|
+
const alpha = clampByte(img.a[i] * UINT8_MAX);
|
|
28
|
+
if (alpha === 0)
|
|
29
|
+
continue;
|
|
30
|
+
data[p] = clampByte(img.r[i] * UINT8_MAX);
|
|
31
|
+
data[p + 1] = clampByte(img.g[i] * UINT8_MAX);
|
|
32
|
+
data[p + 2] = clampByte(img.b[i] * UINT8_MAX);
|
|
33
|
+
data[p + 3] = alpha;
|
|
34
|
+
}
|
|
35
|
+
return { width: img.w, height: img.h, data };
|
|
36
|
+
}
|
|
37
|
+
export function toOklabPlanes(img, alphaCutoff = 1e-4) {
|
|
38
|
+
const { r, g, b, a, w, h, opaque } = img;
|
|
39
|
+
const n = w * h;
|
|
40
|
+
const L = new Float32Array(n);
|
|
41
|
+
const A = new Float32Array(n);
|
|
42
|
+
const B = new Float32Array(n);
|
|
43
|
+
for (let i = 0; i < n; i++) {
|
|
44
|
+
if (!opaque && a[i] <= alphaCutoff)
|
|
45
|
+
continue;
|
|
46
|
+
const [l, ca, cb] = rgbFloatToOklab(r[i], g[i], b[i]);
|
|
47
|
+
L[i] = l;
|
|
48
|
+
A[i] = ca;
|
|
49
|
+
B[i] = cb;
|
|
50
|
+
}
|
|
51
|
+
return { L, A, B };
|
|
52
|
+
}
|
|
53
|
+
export function resizeAreaFloat(img, tw, th) {
|
|
54
|
+
if (img.w === tw && img.h === th)
|
|
55
|
+
return img;
|
|
56
|
+
const { r, g, b, a, w, h, opaque } = img;
|
|
57
|
+
const n = tw * th;
|
|
58
|
+
const oR = new Float32Array(n);
|
|
59
|
+
const oG = new Float32Array(n);
|
|
60
|
+
const oB = new Float32Array(n);
|
|
61
|
+
const oA = new Float32Array(n);
|
|
62
|
+
const sx = w / tw;
|
|
63
|
+
const sy = h / th;
|
|
64
|
+
for (let ty = 0; ty < th; ty++) {
|
|
65
|
+
const y0 = ty * sy;
|
|
66
|
+
const y1 = (ty + 1) * sy;
|
|
67
|
+
const iy0 = Math.floor(y0);
|
|
68
|
+
const iy1 = Math.min(h - 1, Math.floor(y1 - 1e-6));
|
|
69
|
+
for (let tx = 0; tx < tw; tx++) {
|
|
70
|
+
const x0 = tx * sx;
|
|
71
|
+
const x1 = (tx + 1) * sx;
|
|
72
|
+
const ix0 = Math.floor(x0);
|
|
73
|
+
const ix1 = Math.min(w - 1, Math.floor(x1 - 1e-6));
|
|
74
|
+
let ar = 0;
|
|
75
|
+
let ag = 0;
|
|
76
|
+
let ab = 0;
|
|
77
|
+
let aa = 0;
|
|
78
|
+
let aw = 0;
|
|
79
|
+
for (let y = iy0; y <= iy1; y++) {
|
|
80
|
+
const wy = Math.min(y + 1, y1) - Math.max(y, y0);
|
|
81
|
+
const row = y * w;
|
|
82
|
+
for (let x = ix0; x <= ix1; x++) {
|
|
83
|
+
const wt = (Math.min(x + 1, x1) - Math.max(x, x0)) * wy;
|
|
84
|
+
const p = row + x;
|
|
85
|
+
const alpha = opaque ? 1 : a[p];
|
|
86
|
+
const cw = wt * alpha;
|
|
87
|
+
ar += r[p] * cw;
|
|
88
|
+
ag += g[p] * cw;
|
|
89
|
+
ab += b[p] * cw;
|
|
90
|
+
aa += alpha * wt;
|
|
91
|
+
aw += wt;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const o = ty * tw + tx;
|
|
95
|
+
oA[o] = aw > 0 ? aa / aw : 0;
|
|
96
|
+
if (aa > 1e-8) {
|
|
97
|
+
oR[o] = ar / aa;
|
|
98
|
+
oG[o] = ag / aa;
|
|
99
|
+
oB[o] = ab / aa;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return { r: oR, g: oG, b: oB, a: oA, w: tw, h: th, opaque };
|
|
104
|
+
}
|
|
105
|
+
export function gaussianBlur(src, w, h, sigma, radiusMult = 3) {
|
|
106
|
+
const rad = Math.max(1, Math.ceil(sigma * radiusMult));
|
|
107
|
+
const kern = new Float32Array(2 * rad + 1);
|
|
108
|
+
let sum = 0;
|
|
109
|
+
for (let i = -rad; i <= rad; i++) {
|
|
110
|
+
const v = Math.exp(-(i * i) / (2 * sigma * sigma));
|
|
111
|
+
kern[i + rad] = v;
|
|
112
|
+
sum += v;
|
|
113
|
+
}
|
|
114
|
+
for (let i = 0; i < kern.length; i++)
|
|
115
|
+
kern[i] /= sum;
|
|
116
|
+
const tmp = new Float32Array(w * h);
|
|
117
|
+
for (let y = 0; y < h; y++) {
|
|
118
|
+
const row = y * w;
|
|
119
|
+
for (let x = 0; x < w; x++) {
|
|
120
|
+
let acc = 0;
|
|
121
|
+
for (let k = -rad; k <= rad; k++) {
|
|
122
|
+
acc += src[row + Math.max(0, Math.min(w - 1, x + k))] * kern[k + rad];
|
|
123
|
+
}
|
|
124
|
+
tmp[row + x] = acc;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const out = new Float32Array(w * h);
|
|
128
|
+
for (let y = 0; y < h; y++) {
|
|
129
|
+
for (let x = 0; x < w; x++) {
|
|
130
|
+
let acc = 0;
|
|
131
|
+
for (let k = -rad; k <= rad; k++) {
|
|
132
|
+
acc += tmp[Math.max(0, Math.min(h - 1, y + k)) * w + x] * kern[k + rad];
|
|
133
|
+
}
|
|
134
|
+
out[y * w + x] = acc;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return out;
|
|
138
|
+
}
|
|
139
|
+
/** Sobel gradient structure tensor (E, F, G) smoothed by `sigma`, over one or more channels. */
|
|
140
|
+
export function sobelTensor(channels, w, h, sigma) {
|
|
141
|
+
const n = w * h;
|
|
142
|
+
const E = new Float32Array(n);
|
|
143
|
+
const F = new Float32Array(n);
|
|
144
|
+
const G = new Float32Array(n);
|
|
145
|
+
for (let y = 0; y < h; y++) {
|
|
146
|
+
const ym = Math.max(0, y - 1);
|
|
147
|
+
const yp = Math.min(h - 1, y + 1);
|
|
148
|
+
for (let x = 0; x < w; x++) {
|
|
149
|
+
const xm = Math.max(0, x - 1);
|
|
150
|
+
const xp = Math.min(w - 1, x + 1);
|
|
151
|
+
let ee = 0;
|
|
152
|
+
let ff = 0;
|
|
153
|
+
let gg = 0;
|
|
154
|
+
for (const ch of channels) {
|
|
155
|
+
const gx = (ch[ym * w + xp] +
|
|
156
|
+
2 * ch[y * w + xp] +
|
|
157
|
+
ch[yp * w + xp] -
|
|
158
|
+
ch[ym * w + xm] -
|
|
159
|
+
2 * ch[y * w + xm] -
|
|
160
|
+
ch[yp * w + xm]) *
|
|
161
|
+
0.25;
|
|
162
|
+
const gy = (ch[yp * w + xm] +
|
|
163
|
+
2 * ch[yp * w + x] +
|
|
164
|
+
ch[yp * w + xp] -
|
|
165
|
+
ch[ym * w + xm] -
|
|
166
|
+
2 * ch[ym * w + x] -
|
|
167
|
+
ch[ym * w + xp]) *
|
|
168
|
+
0.25;
|
|
169
|
+
ee += gx * gx;
|
|
170
|
+
ff += gx * gy;
|
|
171
|
+
gg += gy * gy;
|
|
172
|
+
}
|
|
173
|
+
const idx = y * w + x;
|
|
174
|
+
E[idx] = ee;
|
|
175
|
+
F[idx] = ff;
|
|
176
|
+
G[idx] = gg;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
E: gaussianBlur(E, w, h, sigma),
|
|
181
|
+
F: gaussianBlur(F, w, h, sigma),
|
|
182
|
+
G: gaussianBlur(G, w, h, sigma),
|
|
183
|
+
};
|
|
184
|
+
}
|
package/dist/grid-arbitrate.d.ts
CHANGED
|
@@ -37,12 +37,20 @@ export declare class GridEvidenceReader {
|
|
|
37
37
|
private readonly described;
|
|
38
38
|
private readonly strips;
|
|
39
39
|
private readonly pixels;
|
|
40
|
+
private readonly placedReadings;
|
|
40
41
|
private readonly readings;
|
|
41
42
|
constructor(image: PixelBuffer);
|
|
43
|
+
private admit;
|
|
44
|
+
private toEvidence;
|
|
42
45
|
read(grids: readonly PixelGridAnalysis[]): {
|
|
43
46
|
readings: GridEvidence[];
|
|
44
47
|
pixels: number;
|
|
45
48
|
};
|
|
49
|
+
/**
|
|
50
|
+
* Two-pass arbitration: evaluate cheap placed readings first to tighten passthrough and global
|
|
51
|
+
* score bounds before running expensive strip-shift / phase-field / chance-evidence searches.
|
|
52
|
+
*/
|
|
53
|
+
arbitrate(grids: readonly PixelGridAnalysis[]): ArbitratedGrid | null;
|
|
46
54
|
}
|
|
47
55
|
/**
|
|
48
56
|
* The best-scoring grid among the proposals, or null when the passthrough scores lower. The
|