@xprite/pixelate 0.1.3 → 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 +2 -2
- 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
|
+
}
|
|
@@ -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
|
@@ -63,12 +63,12 @@ function pickCells(image, grid, picker, x, y) {
|
|
|
63
63
|
}
|
|
64
64
|
// TwoStage picker: Fixer-like two-stage packing (adaptive K, pure triangular weights)
|
|
65
65
|
if (picker === CellColorPicker.TwoStage) {
|
|
66
|
-
return samplePixelation(image, width, height,
|
|
66
|
+
return samplePixelation(image, width, height, "two-stage", { x, y });
|
|
67
67
|
}
|
|
68
68
|
// Dual RT picker: Structure vs TwoStage, winner chosen by round-trip error
|
|
69
69
|
if (picker === CellColorPicker.DualRtTwoStage) {
|
|
70
70
|
const candidateA = samplePixelation(image, width, height, PixelationMethod.Structure, { x, y });
|
|
71
|
-
const candidateB = samplePixelation(image, width, height,
|
|
71
|
+
const candidateB = samplePixelation(image, width, height, "two-stage", { x, y });
|
|
72
72
|
const cells = { x: pixelCells(image.width, x), y: pixelCells(image.height, y) };
|
|
73
73
|
const errorA = roundTripError(image, candidateA, cells);
|
|
74
74
|
const errorB = roundTripError(image, candidateB, cells);
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,9 @@ export { snapPalette } from "./palette-snap.ts";
|
|
|
4
4
|
export { removeOrphanPixels, type RemoveOrphanPixelsOptions } from "./orphan-pixels.ts";
|
|
5
5
|
export { settlePixelAlpha } from "./pixel-alpha.ts";
|
|
6
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";
|
|
7
10
|
export { resizeNearest } from "./resize.ts";
|
|
8
11
|
export { pixelateImage, pixelateImageAsync } from "./pixelate.ts";
|
|
9
12
|
export { PixelArtClassification, PixelationMethod, PixelationMode, PixelationAlgorithm, CellColorPicker, LineGuideMode, OutlinePrepass, PixelationDither, PaletteColorMode, PixelGridSamplingAxis, } from "./types.ts";
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,9 @@ export { snapPalette } from "./palette-snap.js";
|
|
|
4
4
|
export { removeOrphanPixels } from "./orphan-pixels.js";
|
|
5
5
|
export { settlePixelAlpha } from "./pixel-alpha.js";
|
|
6
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";
|
|
7
10
|
export { resizeNearest } from "./resize.js";
|
|
8
11
|
export { pixelateImage, pixelateImageAsync } from "./pixelate.js";
|
|
9
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;
|
package/dist/kuwahara.js
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { assertDimension, assertPixelBuffer, createBufferMemo, } from "./base.js";
|
|
2
|
+
import { fromFloatRgba, gaussianBlur, resizeAreaFloat, sobelTensor, toFloatRgba, toOklabPlanes, } from "./float-image.js";
|
|
3
|
+
import { oklabToRgbFloat } from "./quantization.js";
|
|
4
|
+
const WORKING_SCALE = 4;
|
|
5
|
+
const MAX_WORKING_PIXELS = 1_048_576;
|
|
6
|
+
const CHANNELS = 4;
|
|
7
|
+
const KUWAHARA_RADIUS = 3.4;
|
|
8
|
+
const KUWAHARA_ALPHA = 1.0;
|
|
9
|
+
const KUWAHARA_SHARPNESS = 8;
|
|
10
|
+
const KUWAHARA_TENSOR_SIGMA = 1.5;
|
|
11
|
+
const KUWAHARA_PASSES = 2;
|
|
12
|
+
const KUWAHARA_SECTORS = 8;
|
|
13
|
+
const DETAIL_PROTECT_RADIUS_DROP = 0.62;
|
|
14
|
+
const CLARITY_AMOUNT = 0.54;
|
|
15
|
+
const CLARITY_SIGMA = 3.2;
|
|
16
|
+
const CLARITY_KNEE = 0.14;
|
|
17
|
+
const CLARITY_POS_GAIN = 0.72;
|
|
18
|
+
const CLARITY_NEG_GAIN = 0.92;
|
|
19
|
+
const S_CURVE_AMOUNT = 0.032;
|
|
20
|
+
const CHROMA_SAT_BOOST = 0.22;
|
|
21
|
+
const PIXEL_ART_EXACT_NEIGHBOR_CUTOFF = 0.45;
|
|
22
|
+
const FLAT_ART_RATIO_START = 0.45;
|
|
23
|
+
const FLAT_ART_SINGLE_PASS_CUTOFF = 0.52;
|
|
24
|
+
const rememberKuwahara = createBufferMemo();
|
|
25
|
+
/** Fast strided neighbor-flatness check so crisp pixel art & flat cel art aren't over-processed. */
|
|
26
|
+
function measureSourceProfile(image) {
|
|
27
|
+
const { width: w, height: h, data } = image;
|
|
28
|
+
if (w < 2 || h < 2)
|
|
29
|
+
return { exactNeighborRatio: 0, nearFlatRatio: 0 };
|
|
30
|
+
const stride = Math.max(1, Math.floor(Math.sqrt((w * h) / 16_384)));
|
|
31
|
+
let exact = 0;
|
|
32
|
+
let nearFlat = 0;
|
|
33
|
+
let pairs = 0;
|
|
34
|
+
for (let y = 0; y < h - 1; y += stride) {
|
|
35
|
+
for (let x = 0; x < w - 1; x += stride) {
|
|
36
|
+
const i = (y * w + x) * CHANNELS;
|
|
37
|
+
if (data[i + 3] < 128)
|
|
38
|
+
continue;
|
|
39
|
+
for (const j of [i + CHANNELS, i + w * CHANNELS]) {
|
|
40
|
+
if (data[j + 3] < 128)
|
|
41
|
+
continue;
|
|
42
|
+
pairs += 1;
|
|
43
|
+
const maxD = Math.max(Math.abs(data[i] - data[j]), Math.abs(data[i + 1] - data[j + 1]), Math.abs(data[i + 2] - data[j + 2]));
|
|
44
|
+
if (maxD === 0)
|
|
45
|
+
exact += 1;
|
|
46
|
+
if (maxD <= 3)
|
|
47
|
+
nearFlat += 1;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return pairs === 0
|
|
52
|
+
? { exactNeighborRatio: 0, nearFlatRatio: 0 }
|
|
53
|
+
: { exactNeighborRatio: exact / pairs, nearFlatRatio: nearFlat / pairs };
|
|
54
|
+
}
|
|
55
|
+
function computeOrientation(img, sigma) {
|
|
56
|
+
const { E, F, G } = sobelTensor([img.r, img.g, img.b], img.w, img.h, sigma);
|
|
57
|
+
const n = img.w * img.h;
|
|
58
|
+
const cosT = new Float32Array(n);
|
|
59
|
+
const sinT = new Float32Array(n);
|
|
60
|
+
const aniso = new Float32Array(n);
|
|
61
|
+
for (let i = 0; i < n; i++) {
|
|
62
|
+
const e = E[i];
|
|
63
|
+
const f = F[i];
|
|
64
|
+
const g = G[i];
|
|
65
|
+
const disc = Math.sqrt((e - g) * (e - g) + 4 * f * f);
|
|
66
|
+
const l1 = 0.5 * (e + g + disc);
|
|
67
|
+
const l2 = 0.5 * (e + g - disc);
|
|
68
|
+
aniso[i] = (l1 - l2) / (l1 + l2 + 1e-6);
|
|
69
|
+
const tx = l1 - e;
|
|
70
|
+
const ty = -f;
|
|
71
|
+
const len = Math.hypot(tx, ty);
|
|
72
|
+
cosT[i] = len > 1e-7 ? tx / len : 1;
|
|
73
|
+
sinT[i] = len > 1e-7 ? ty / len : 0;
|
|
74
|
+
}
|
|
75
|
+
return { cosT, sinT, aniso };
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* OKLab micro-feature protection mask: detects isolated 1-2 output-pixel landmarks (eyes, lips,
|
|
79
|
+
* specular highlights) via Shi-Tomasi cornerness + luminance/chroma blobs while staying 0 on
|
|
80
|
+
* smooth planes and straight edges.
|
|
81
|
+
*/
|
|
82
|
+
function computeMicroFeatureMask(img) {
|
|
83
|
+
const { a: srcA, w, h, opaque } = img;
|
|
84
|
+
const { L, A, B } = toOklabPlanes(img);
|
|
85
|
+
const g1 = gaussianBlur(L, w, h, 1.4);
|
|
86
|
+
const g2 = gaussianBlur(L, w, h, 5.0);
|
|
87
|
+
const a1 = gaussianBlur(A, w, h, 1.4);
|
|
88
|
+
const b1 = gaussianBlur(B, w, h, 1.4);
|
|
89
|
+
const a2 = gaussianBlur(A, w, h, 5.0);
|
|
90
|
+
const b2 = gaussianBlur(B, w, h, 5.0);
|
|
91
|
+
const { E, F, G } = sobelTensor([g1], w, h, 2.5);
|
|
92
|
+
const raw = new Float32Array(w * h);
|
|
93
|
+
for (let i = 0; i < raw.length; i++) {
|
|
94
|
+
if (!opaque && srcA[i] <= 1e-4)
|
|
95
|
+
continue;
|
|
96
|
+
const e = E[i];
|
|
97
|
+
const f = F[i];
|
|
98
|
+
const g = G[i];
|
|
99
|
+
const l2 = Math.max(0, 0.5 * (e + g - Math.sqrt((e - g) * (e - g) + 4 * f * f)));
|
|
100
|
+
const score = Math.sqrt(l2) * 5.0 +
|
|
101
|
+
Math.abs(g1[i] - g2[i]) * 2.2 +
|
|
102
|
+
Math.hypot(a1[i] - a2[i], b1[i] - b2[i]) * 4.0;
|
|
103
|
+
raw[i] = Math.min(1.0, Math.max(0, (score - 0.14) / 0.28));
|
|
104
|
+
}
|
|
105
|
+
return gaussianBlur(raw, w, h, 1.5);
|
|
106
|
+
}
|
|
107
|
+
/** Orientation-aligned 8-sector Anisotropic Kuwahara filter with local micro-feature radius protection. */
|
|
108
|
+
function anisotropicKuwahara(img, passes, radius, protect) {
|
|
109
|
+
let cur = img;
|
|
110
|
+
const { w, h, a: srcA, opaque } = img;
|
|
111
|
+
const cosSector = Math.cos(Math.PI / KUWAHARA_SECTORS);
|
|
112
|
+
const invSinSector2 = 1 / Math.sin(Math.PI / KUWAHARA_SECTORS) ** 2;
|
|
113
|
+
const mR = new Float64Array(KUWAHARA_SECTORS);
|
|
114
|
+
const mG = new Float64Array(KUWAHARA_SECTORS);
|
|
115
|
+
const mB = new Float64Array(KUWAHARA_SECTORS);
|
|
116
|
+
const sR = new Float64Array(KUWAHARA_SECTORS);
|
|
117
|
+
const sG = new Float64Array(KUWAHARA_SECTORS);
|
|
118
|
+
const sB = new Float64Array(KUWAHARA_SECTORS);
|
|
119
|
+
const wSum = new Float64Array(KUWAHARA_SECTORS);
|
|
120
|
+
const ws = new Float64Array(KUWAHARA_SECTORS);
|
|
121
|
+
for (let pass = 0; pass < passes; pass++) {
|
|
122
|
+
const { cosT, sinT, aniso } = computeOrientation(cur, KUWAHARA_TENSOR_SIGMA);
|
|
123
|
+
const outR = new Float32Array(w * h);
|
|
124
|
+
const outG = new Float32Array(w * h);
|
|
125
|
+
const outB = new Float32Array(w * h);
|
|
126
|
+
const { r: srcR, g: srcG, b: srcB } = cur;
|
|
127
|
+
for (let y = 0; y < h; y++) {
|
|
128
|
+
for (let x = 0; x < w; x++) {
|
|
129
|
+
const idx = y * w + x;
|
|
130
|
+
if (!opaque && srcA[idx] <= 1e-4)
|
|
131
|
+
continue;
|
|
132
|
+
const localRadius = Math.max(1.1, radius * (1 - DETAIL_PROTECT_RADIUS_DROP * protect[idx]));
|
|
133
|
+
const zeta = 2.0 / localRadius;
|
|
134
|
+
const eta = (zeta + cosSector) * invSinSector2;
|
|
135
|
+
const A = aniso[idx];
|
|
136
|
+
const aAxis = (localRadius * (KUWAHARA_ALPHA + A)) / KUWAHARA_ALPHA;
|
|
137
|
+
const bAxis = (localRadius * KUWAHARA_ALPHA) / (KUWAHARA_ALPHA + A);
|
|
138
|
+
const c = cosT[idx];
|
|
139
|
+
const s = sinT[idx];
|
|
140
|
+
const maxX = Math.ceil(Math.sqrt(aAxis * aAxis * c * c + bAxis * bAxis * s * s));
|
|
141
|
+
const maxY = Math.ceil(Math.sqrt(aAxis * aAxis * s * s + bAxis * bAxis * c * c));
|
|
142
|
+
const invA = 1 / aAxis;
|
|
143
|
+
const invB = 1 / bAxis;
|
|
144
|
+
mR.fill(0);
|
|
145
|
+
mG.fill(0);
|
|
146
|
+
mB.fill(0);
|
|
147
|
+
sR.fill(0);
|
|
148
|
+
sG.fill(0);
|
|
149
|
+
sB.fill(0);
|
|
150
|
+
wSum.fill(0);
|
|
151
|
+
for (let dy = -maxY; dy <= maxY; dy++) {
|
|
152
|
+
const row = Math.max(0, Math.min(h - 1, y + dy)) * w;
|
|
153
|
+
for (let dx = -maxX; dx <= maxX; dx++) {
|
|
154
|
+
const vx = (c * dx + s * dy) * invA;
|
|
155
|
+
const vy = (-s * dx + c * dy) * invB;
|
|
156
|
+
const r2 = vx * vx + vy * vy;
|
|
157
|
+
if (r2 > 1.0)
|
|
158
|
+
continue;
|
|
159
|
+
const p = row + Math.max(0, Math.min(w - 1, x + dx));
|
|
160
|
+
const alphaWeight = opaque ? 1 : srcA[p];
|
|
161
|
+
if (alphaWeight <= 1e-4)
|
|
162
|
+
continue;
|
|
163
|
+
const cr = srcR[p];
|
|
164
|
+
const cg = srcG[p];
|
|
165
|
+
const cb = srcB[p];
|
|
166
|
+
const gauss = Math.exp(-3.125 * r2) * alphaWeight;
|
|
167
|
+
const rx = 0.70710678 * (vx - vy);
|
|
168
|
+
const ry = 0.70710678 * (vx + vy);
|
|
169
|
+
const w0 = Math.max(0, vy + zeta - eta * vx * vx);
|
|
170
|
+
const w1 = Math.max(0, ry + zeta - eta * rx * rx);
|
|
171
|
+
const w2 = Math.max(0, -vx + zeta - eta * vy * vy);
|
|
172
|
+
const w3 = Math.max(0, -rx + zeta - eta * ry * ry);
|
|
173
|
+
const w4 = Math.max(0, -vy + zeta - eta * vx * vx);
|
|
174
|
+
const w5 = Math.max(0, -ry + zeta - eta * rx * rx);
|
|
175
|
+
const w6 = Math.max(0, vx + zeta - eta * vy * vy);
|
|
176
|
+
const w7 = Math.max(0, rx + zeta - eta * ry * ry);
|
|
177
|
+
ws[0] = w0 * w0;
|
|
178
|
+
ws[1] = w1 * w1;
|
|
179
|
+
ws[2] = w2 * w2;
|
|
180
|
+
ws[3] = w3 * w3;
|
|
181
|
+
ws[4] = w4 * w4;
|
|
182
|
+
ws[5] = w5 * w5;
|
|
183
|
+
ws[6] = w6 * w6;
|
|
184
|
+
ws[7] = w7 * w7;
|
|
185
|
+
for (let k = 0; k < KUWAHARA_SECTORS; k++) {
|
|
186
|
+
const wk = ws[k] * gauss;
|
|
187
|
+
if (wk > 1e-6) {
|
|
188
|
+
wSum[k] += wk;
|
|
189
|
+
mR[k] += wk * cr;
|
|
190
|
+
mG[k] += wk * cg;
|
|
191
|
+
mB[k] += wk * cb;
|
|
192
|
+
sR[k] += wk * cr * cr;
|
|
193
|
+
sG[k] += wk * cg * cg;
|
|
194
|
+
sB[k] += wk * cb * cb;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
let accR = 0;
|
|
200
|
+
let accG = 0;
|
|
201
|
+
let accB = 0;
|
|
202
|
+
let accW = 0;
|
|
203
|
+
for (let k = 0; k < KUWAHARA_SECTORS; k++) {
|
|
204
|
+
if (wSum[k] <= 1e-8)
|
|
205
|
+
continue;
|
|
206
|
+
const inv = 1 / wSum[k];
|
|
207
|
+
const mr = mR[k] * inv;
|
|
208
|
+
const mg = mG[k] * inv;
|
|
209
|
+
const mb = mB[k] * inv;
|
|
210
|
+
const vr = Math.max(0, sR[k] * inv - mr * mr);
|
|
211
|
+
const vg = Math.max(0, sG[k] * inv - mg * mg);
|
|
212
|
+
const vb = Math.max(0, sB[k] * inv - mb * mb);
|
|
213
|
+
const wk = 1.0 / (1.0 + (Math.sqrt(vr + vg + vb) * 16.0) ** KUWAHARA_SHARPNESS);
|
|
214
|
+
accR += wk * mr;
|
|
215
|
+
accG += wk * mg;
|
|
216
|
+
accB += wk * mb;
|
|
217
|
+
accW += wk;
|
|
218
|
+
}
|
|
219
|
+
outR[idx] = accW > 0 ? accR / accW : srcR[idx];
|
|
220
|
+
outG[idx] = accW > 0 ? accG / accW : srcG[idx];
|
|
221
|
+
outB[idx] = accW > 0 ? accB / accW : srcB[idx];
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
cur = { r: outR, g: outG, b: outB, a: srcA, w, h, opaque };
|
|
225
|
+
}
|
|
226
|
+
return cur;
|
|
227
|
+
}
|
|
228
|
+
/** Neutral anti-halo OKLab local clarity plus shadow-safe chroma lift. */
|
|
229
|
+
function enhanceAntiHaloClarity(img, nearFlatRatio) {
|
|
230
|
+
const { a: srcA, w, h, opaque } = img;
|
|
231
|
+
const { L, A, B } = toOklabPlanes(img);
|
|
232
|
+
const flatScale = Math.max(0.28, Math.min(1.0, 1.0 - (nearFlatRatio - FLAT_ART_RATIO_START) * 2.2));
|
|
233
|
+
const clarityStrength = CLARITY_AMOUNT * flatScale;
|
|
234
|
+
const sCurve = S_CURVE_AMOUNT * flatScale;
|
|
235
|
+
const satBoost = CHROMA_SAT_BOOST * (0.4 + 0.6 * flatScale);
|
|
236
|
+
const blurL = gaussianBlur(L, w, h, CLARITY_SIGMA);
|
|
237
|
+
const n = w * h;
|
|
238
|
+
const oR = new Float32Array(n);
|
|
239
|
+
const oG = new Float32Array(n);
|
|
240
|
+
const oB = new Float32Array(n);
|
|
241
|
+
const invKnee2 = 1 / (CLARITY_KNEE * CLARITY_KNEE);
|
|
242
|
+
for (let i = 0; i < n; i++) {
|
|
243
|
+
if (!opaque && srcA[i] <= 1e-4)
|
|
244
|
+
continue;
|
|
245
|
+
const rawDelta = L[i] - blurL[i];
|
|
246
|
+
let delta = rawDelta / (1.0 + rawDelta * rawDelta * invKnee2);
|
|
247
|
+
delta *=
|
|
248
|
+
delta > 0
|
|
249
|
+
? CLARITY_POS_GAIN * Math.min(1.0, Math.max(0.0, (0.98 - L[i]) * 5.0))
|
|
250
|
+
: CLARITY_NEG_GAIN;
|
|
251
|
+
let nl = Math.max(0, Math.min(1, L[i] + clarityStrength * delta));
|
|
252
|
+
nl = Math.max(0, Math.min(1, nl - sCurve * Math.sin(2 * Math.PI * (nl - 0.5))));
|
|
253
|
+
const chromaGate = Math.min(1.0, Math.max(0, (Math.hypot(A[i], B[i]) - 0.025) / 0.04));
|
|
254
|
+
const lumGate = Math.min(1.0, Math.max(0, (nl - 0.22) / 0.3));
|
|
255
|
+
const satScale = 1.0 + satBoost * chromaGate * (0.35 + 0.65 * lumGate);
|
|
256
|
+
const [nr, ng, nb] = oklabToRgbFloat(nl, A[i] * satScale, B[i] * satScale);
|
|
257
|
+
oR[i] = nr;
|
|
258
|
+
oG[i] = ng;
|
|
259
|
+
oB[i] = nb;
|
|
260
|
+
}
|
|
261
|
+
return { r: oR, g: oG, b: oB, a: srcA, w, h, opaque };
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Cel-shading simplification pre-pass for photo/illustration pixelation:
|
|
265
|
+
* downscales to a 4× target working grid, runs detail-protected Anisotropic Kuwahara to flatten
|
|
266
|
+
* hatching/grain while preserving 1-2 px facial/specular landmarks, then applies neutral anti-halo
|
|
267
|
+
* OKLab clarity. Already-crisp flat pixel art is passed through untouched.
|
|
268
|
+
*/
|
|
269
|
+
export function kuwaharaPrepass(image, target) {
|
|
270
|
+
assertPixelBuffer(image);
|
|
271
|
+
assertDimension(target.width, "target.width");
|
|
272
|
+
assertDimension(target.height, "target.height");
|
|
273
|
+
const profile = measureSourceProfile(image);
|
|
274
|
+
if (profile.exactNeighborRatio >= PIXEL_ART_EXACT_NEIGHBOR_CUTOFF) {
|
|
275
|
+
return { width: image.width, height: image.height, data: image.data.slice() };
|
|
276
|
+
}
|
|
277
|
+
const scale = Math.min(WORKING_SCALE, Math.sqrt(MAX_WORKING_PIXELS / (target.width * target.height)));
|
|
278
|
+
const workW = Math.min(image.width, Math.max(target.width, Math.round(target.width * scale)));
|
|
279
|
+
const workH = Math.min(image.height, Math.max(target.height, Math.round(target.height * scale)));
|
|
280
|
+
return rememberKuwahara(image, `c5:${workW}x${workH}`, () => {
|
|
281
|
+
const working = resizeAreaFloat(toFloatRgba(image), workW, workH);
|
|
282
|
+
const protect = computeMicroFeatureMask(working);
|
|
283
|
+
const isFlatArt = profile.nearFlatRatio >= FLAT_ART_SINGLE_PASS_CUTOFF;
|
|
284
|
+
const smoothed = anisotropicKuwahara(working, isFlatArt ? 1 : KUWAHARA_PASSES, isFlatArt ? 2.6 : KUWAHARA_RADIUS, protect);
|
|
285
|
+
return fromFloatRgba(enhanceAntiHaloClarity(smoothed, profile.nearFlatRatio));
|
|
286
|
+
});
|
|
287
|
+
}
|