@xprite/pixelate 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -0
- package/README.zh-CN.md +14 -0
- package/dist/base.d.ts +4 -0
- package/dist/base.js +33 -0
- package/dist/float-image.d.ts +25 -0
- package/dist/float-image.js +184 -0
- package/dist/grid-sample.js +3 -78
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -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 +18 -0
- package/dist/orphan-pixels.js +207 -0
- package/dist/palette-snap.d.ts +1 -1
- package/dist/palette-snap.js +127 -58
- package/dist/pixel-alpha.d.ts +10 -0
- package/dist/pixel-alpha.js +108 -0
- package/dist/pixelate.d.ts +4 -7
- package/dist/pixelate.js +146 -101
- 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 +15 -3
- 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
|
+
}
|
|
@@ -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-sample.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { getColorPickerEntry } from "./cell-color-picker.js";
|
|
2
2
|
import { MAX_RECOVERED_GRID_SIZE, pixelGridCuts } from "./grid.js";
|
|
3
|
+
import { settleEdgeAlpha } from "./pixel-alpha.js";
|
|
3
4
|
import { samplePixelation } from "./sampling.js";
|
|
4
5
|
import { CellColorPicker, PixelationMethod, } from "./types.js";
|
|
5
6
|
const CHANNELS = 4;
|
|
@@ -43,82 +44,6 @@ function roundTripError(input, candidate, cells) {
|
|
|
43
44
|
}
|
|
44
45
|
return pixelCount > 0 ? errorSum / pixelCount : 0;
|
|
45
46
|
}
|
|
46
|
-
/**
|
|
47
|
-
* Pixel art is drawn with whole pixels: a cell is either covered or clear. A generated or resampled
|
|
48
|
-
* sprite carries a soft edge instead: the pixels along its outline are partly transparent, and its
|
|
49
|
-
* covered pixels often sit a little below full opacity. Averaged into cells, that leaves a
|
|
50
|
-
* half-transparent ring around the recovered sprite and an almost-opaque inside.
|
|
51
|
-
*
|
|
52
|
-
* A cell that holds both covered pixels (alpha of at least HALF_ALPHA) and uncovered ones lies on
|
|
53
|
-
* the outline. In translucent art, a cell with no covered pixel takes the median alpha of its
|
|
54
|
-
* pixels: a shadow keeps its level, a soft outline (mostly clear) is cleared. It is in the art when its covered pixels fill at least half of it, and clear
|
|
55
|
-
* otherwise; in opaque art, so is a cell with no covered pixel. A covered cell takes the mean alpha of its covered pixels, raised to OPAQUE when the
|
|
56
|
-
* source's art is opaque: at most TRANSLUCENT_SHARE of its visible pixels between TRANSLUCENT_LOW
|
|
57
|
-
* and TRANSLUCENT_HIGH. Art drawn translucent (glass, shadows, glow) keeps its alpha, except that a
|
|
58
|
-
* covered cell above TRANSLUCENT_HIGH is still raised to OPAQUE.
|
|
59
|
-
*/
|
|
60
|
-
const HALF_ALPHA = 128;
|
|
61
|
-
const HALF = 0.5;
|
|
62
|
-
const OPAQUE = 255;
|
|
63
|
-
const TRANSLUCENT_LOW = 16;
|
|
64
|
-
const TRANSLUCENT_HIGH = 224;
|
|
65
|
-
/** Share of the visible pixels a soft outline around opaque art stays below. */
|
|
66
|
-
const TRANSLUCENT_SHARE = 0.2;
|
|
67
|
-
/** Whether the image's visible pixels are opaque but for a soft edge. */
|
|
68
|
-
function opaqueArt(image) {
|
|
69
|
-
let visible = 0, translucent = 0;
|
|
70
|
-
for (let o = COLOR_CHANNELS; o < image.data.length; o += CHANNELS) {
|
|
71
|
-
const alpha = image.data[o];
|
|
72
|
-
if (!alpha)
|
|
73
|
-
continue;
|
|
74
|
-
visible++;
|
|
75
|
-
if (alpha >= TRANSLUCENT_LOW && alpha <= TRANSLUCENT_HIGH)
|
|
76
|
-
translucent++;
|
|
77
|
-
}
|
|
78
|
-
return visible > 0 && translucent <= TRANSLUCENT_SHARE * visible;
|
|
79
|
-
}
|
|
80
|
-
function settleEdgeAlpha(image, x, y, out) {
|
|
81
|
-
const opaque = opaqueArt(image);
|
|
82
|
-
for (let row = 0; row < out.height; row++)
|
|
83
|
-
for (let column = 0; column < out.width; column++) {
|
|
84
|
-
const offset = (row * out.width + column) * CHANNELS;
|
|
85
|
-
if (!out.data[offset + COLOR_CHANNELS])
|
|
86
|
-
continue;
|
|
87
|
-
const left = Math.max(0, Math.ceil(x[column])), right = Math.min(image.width, Math.ceil(x[column + 1])), top = Math.max(0, Math.ceil(y[row])), bottom = Math.min(image.height, Math.ceil(y[row + 1]));
|
|
88
|
-
let covered = 0, coveredAlpha = 0;
|
|
89
|
-
const alphas = [];
|
|
90
|
-
for (let py = top; py < bottom; py++)
|
|
91
|
-
for (let px = left; px < right; px++) {
|
|
92
|
-
const alpha = image.data[(py * image.width + px) * CHANNELS + COLOR_CHANNELS];
|
|
93
|
-
alphas.push(alpha);
|
|
94
|
-
if (alpha < HALF_ALPHA)
|
|
95
|
-
continue;
|
|
96
|
-
covered++;
|
|
97
|
-
coveredAlpha += alpha;
|
|
98
|
-
}
|
|
99
|
-
const area = (right - left) * (bottom - top);
|
|
100
|
-
if (!covered && !opaque) {
|
|
101
|
-
// Translucent art fills its cell evenly; a soft outline leaves most of the cell clear.
|
|
102
|
-
alphas.sort((a, b) => a - b);
|
|
103
|
-
const median = alphas[alphas.length >> 1] ?? 0;
|
|
104
|
-
if (median < TRANSLUCENT_LOW)
|
|
105
|
-
out.data.fill(0, offset, offset + CHANNELS);
|
|
106
|
-
else
|
|
107
|
-
out.data[offset + COLOR_CHANNELS] = median;
|
|
108
|
-
continue;
|
|
109
|
-
}
|
|
110
|
-
if (covered < HALF * area)
|
|
111
|
-
out.data.fill(0, offset, offset + CHANNELS);
|
|
112
|
-
else if (opaque)
|
|
113
|
-
out.data[offset + COLOR_CHANNELS] = OPAQUE;
|
|
114
|
-
else {
|
|
115
|
-
// Translucent art (a shadow, a glow) keeps its alpha, but a covered cell above
|
|
116
|
-
// TRANSLUCENT_HIGH is opaque art drawn a little below 255, as everywhere else.
|
|
117
|
-
const alpha = covered < area ? coveredAlpha / covered : out.data[offset + COLOR_CHANNELS];
|
|
118
|
-
out.data[offset + COLOR_CHANNELS] = alpha > TRANSLUCENT_HIGH ? OPAQUE : Math.round(alpha);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
47
|
/** One RGBA pixel per grid cell: explicit cuts when the grid carries them, else one regular lattice. */
|
|
123
48
|
export function recoverGrid(image, grid, picker = CellColorPicker.MostCommon) {
|
|
124
49
|
const x = grid.cutsX ?? pixelGridCuts(image.width, grid.cellWidth, grid.offsetX), y = grid.cutsY ?? pixelGridCuts(image.height, grid.cellHeight, grid.offsetY);
|
|
@@ -138,12 +63,12 @@ function pickCells(image, grid, picker, x, y) {
|
|
|
138
63
|
}
|
|
139
64
|
// TwoStage picker: Fixer-like two-stage packing (adaptive K, pure triangular weights)
|
|
140
65
|
if (picker === CellColorPicker.TwoStage) {
|
|
141
|
-
return samplePixelation(image, width, height,
|
|
66
|
+
return samplePixelation(image, width, height, "two-stage", { x, y });
|
|
142
67
|
}
|
|
143
68
|
// Dual RT picker: Structure vs TwoStage, winner chosen by round-trip error
|
|
144
69
|
if (picker === CellColorPicker.DualRtTwoStage) {
|
|
145
70
|
const candidateA = samplePixelation(image, width, height, PixelationMethod.Structure, { x, y });
|
|
146
|
-
const candidateB = samplePixelation(image, width, height,
|
|
71
|
+
const candidateB = samplePixelation(image, width, height, "two-stage", { x, y });
|
|
147
72
|
const cells = { x: pixelCells(image.width, x), y: pixelCells(image.height, y) };
|
|
148
73
|
const errorA = roundTripError(image, candidateA, cells);
|
|
149
74
|
const errorB = roundTripError(image, candidateB, cells);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
export { analyzePixelArt, analyzePixelArtAsync } from "./analyze.ts";
|
|
2
2
|
export { extractPalette, extractPixelationPalette, countDistinctColors, reduceToPalette, } from "./palette.ts";
|
|
3
3
|
export { snapPalette } from "./palette-snap.ts";
|
|
4
|
+
export { removeOrphanPixels, type RemoveOrphanPixelsOptions } from "./orphan-pixels.ts";
|
|
5
|
+
export { settlePixelAlpha } from "./pixel-alpha.ts";
|
|
4
6
|
export { estimateTilt, straighten, MAX_TILT_DEGREES } from "./deskew.ts";
|
|
7
|
+
export { kuwaharaPrepass } from "./kuwahara.ts";
|
|
8
|
+
export { resizeCrisp2Means } from "./resize-crisp.ts";
|
|
9
|
+
export { resizeLanczos } from "./resize-lanczos.ts";
|
|
5
10
|
export { resizeNearest } from "./resize.ts";
|
|
6
11
|
export { pixelateImage, pixelateImageAsync } from "./pixelate.ts";
|
|
7
12
|
export { PixelArtClassification, PixelationMethod, PixelationMode, PixelationAlgorithm, CellColorPicker, LineGuideMode, OutlinePrepass, PixelationDither, PaletteColorMode, PixelGridSamplingAxis, } from "./types.ts";
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
export { analyzePixelArt, analyzePixelArtAsync } from "./analyze.js";
|
|
2
2
|
export { extractPalette, extractPixelationPalette, countDistinctColors, reduceToPalette, } from "./palette.js";
|
|
3
3
|
export { snapPalette } from "./palette-snap.js";
|
|
4
|
+
export { removeOrphanPixels } from "./orphan-pixels.js";
|
|
5
|
+
export { settlePixelAlpha } from "./pixel-alpha.js";
|
|
4
6
|
export { estimateTilt, straighten, MAX_TILT_DEGREES } from "./deskew.js";
|
|
7
|
+
export { kuwaharaPrepass } from "./kuwahara.js";
|
|
8
|
+
export { resizeCrisp2Means } from "./resize-crisp.js";
|
|
9
|
+
export { resizeLanczos } from "./resize-lanczos.js";
|
|
5
10
|
export { resizeNearest } from "./resize.js";
|
|
6
11
|
export { pixelateImage, pixelateImageAsync } from "./pixelate.js";
|
|
7
12
|
export { PixelArtClassification, PixelationMethod, PixelationMode, PixelationAlgorithm, CellColorPicker, LineGuideMode, OutlinePrepass, PixelationDither, PaletteColorMode, PixelGridSamplingAxis, } from "./types.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type PixelBuffer } from "./base.ts";
|
|
2
|
+
import type { PixelationSize } from "./types.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Cel-shading simplification pre-pass for photo/illustration pixelation:
|
|
5
|
+
* downscales to a 4× target working grid, runs detail-protected Anisotropic Kuwahara to flatten
|
|
6
|
+
* hatching/grain while preserving 1-2 px facial/specular landmarks, then applies neutral anti-halo
|
|
7
|
+
* OKLab clarity. Already-crisp flat pixel art is passed through untouched.
|
|
8
|
+
*/
|
|
9
|
+
export declare function kuwaharaPrepass(image: PixelBuffer, target: PixelationSize): PixelBuffer;
|