@xprite/pixelate 0.1.1 → 0.1.2
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 +48 -46
- package/README.zh-CN.md +48 -43
- package/dist/palette-snap.js +84 -44
- package/dist/quantization.d.ts +6 -1
- package/dist/quantization.js +50 -8
- package/dist/types.d.ts +3 -3
- package/dist/types.js +1 -1
- package/package.json +21 -21
package/README.md
CHANGED
|
@@ -15,19 +15,20 @@ A real generated image (887×887, cells 15 to 21 px wide), recovered by each too
|
|
|
15
15
|
settings. Xprite puts each cut on a colour edge, so it does not cut through the eye, the boots or
|
|
16
16
|
the sword.
|
|
17
17
|
|
|
18
|
-
| Recovered grid | Xprite, 48×49 | [pixel-art-fixer](https://github.com/Retro-Diffusion/pixel-art-fixer), 47×48 | [perfectPixel](https://github.com/theamusing/perfectPixel), 50×50 | [proper-pixel-art](https://github.com/KennethJAllen/proper-pixel-art), 53×54 |
|
|
19
|
-
| :---: | :---: | :---: | :---: | :---: |
|
|
20
|
-
|  |  |  |  |  |
|
|
18
|
+
| Recovered grid | Xprite, 48×49 | [pixel-art-fixer](https://github.com/Retro-Diffusion/pixel-art-fixer), 47×48 | [perfectPixel](https://github.com/theamusing/perfectPixel), 50×50 | [proper-pixel-art](https://github.com/KennethJAllen/proper-pixel-art), 53×54 | [Sprite Fusion Pixel Snapper](https://github.com/Hugo-Dz/spritefusion-pixel-snapper), 56×53 |
|
|
19
|
+
| :---: | :---: | :---: | :---: | :---: | :---: |
|
|
20
|
+
|  |  |  |  |  |  |
|
|
21
21
|
|
|
22
22
|
On the AI suite of [our benchmark](#benchmark) (1746 cases), Xprite gets the exact size most
|
|
23
23
|
often:
|
|
24
24
|
|
|
25
|
-
| Method | Exact size | Within 1 cell | Pixel match |
|
|
25
|
+
| Method | Exact size | Within 1 cell | Pixel match | Fringe (alpha 1–254) | Median time |
|
|
26
26
|
| --- | --- | --- | --- | --- | --- |
|
|
27
|
-
| Xprite Auto | **83.6%** | **90.0%** | **99.
|
|
27
|
+
| Xprite Auto | **83.6%** | **90.0%** | **99.7%** | 0.1% | 2.80 s |
|
|
28
28
|
| pixel-art-fixer | 49.7% | 80.9% | 98.3% | 0.0% | 0.18 s |
|
|
29
29
|
| perfectPixel | 53.3% | 68.0% | 96.7% | - | 0.03 s |
|
|
30
30
|
| proper-pixel-art | 72.2% | 80.4% | 99.7% | 0.0% | 0.49 s |
|
|
31
|
+
| Sprite Fusion Pixel Snapper | 56.8% | 77.9% | 98.5% | 91.0% | — |
|
|
31
32
|
|
|
32
33
|
## Contents
|
|
33
34
|
|
|
@@ -41,56 +42,56 @@ often:
|
|
|
41
42
|
## Examples
|
|
42
43
|
|
|
43
44
|
Each example is one case from a category of the benchmark's AI suite: CC0 pixel art rendered the
|
|
44
|
-
way generators fake it, with a known native size. The sizes are the visible art in cells;
|
|
45
|
+
way generators fake it, with a known native size. The sizes are the visible art in cells; ✅ marks
|
|
45
46
|
the exact native size. Every tool runs with its default settings.
|
|
46
47
|
|
|
47
48
|
### Two sprites on one canvas
|
|
48
49
|
|
|
49
50
|
- [Goblins by LuizMelo](https://luizmelo.itch.io/monsters-creatures-fantasy), CC0. Category `gen_pair`. Native art: 78×36.
|
|
50
51
|
|
|
51
|
-
| Input, 1800×845 | Xprite, 78×36
|
|
52
|
-
| :---: | :---: | :---: | :---: | :---: |
|
|
53
|
-
|  |  |  |  |  |
|
|
52
|
+
| Input, 1800×845 | Xprite, 78×36 ✅ | pixel-art-fixer, 78×35 ❌ | perfectPixel, 80×36 ❌ | proper-pixel-art, 79×36 ❌ | Sprite Fusion, 78×36 ✅ |
|
|
53
|
+
| :---: | :---: | :---: | :---: | :---: | :---: |
|
|
54
|
+
|  |  |  |  |  |  |
|
|
54
55
|
|
|
55
56
|
### Cells that drift and jitter
|
|
56
57
|
|
|
57
58
|
- [Boss dragon by ansimuz](https://opengameart.org/content/sideview-fantasy-patreon-collection), CC0. Category `gen_wobble`. Native art: 71×39.
|
|
58
59
|
|
|
59
|
-
| Input, 1226×664 | Xprite, 71×39
|
|
60
|
-
| :---: | :---: | :---: | :---: | :---: |
|
|
61
|
-
|  |  |  |  |  |
|
|
60
|
+
| Input, 1226×664 | Xprite, 71×39 ✅ | pixel-art-fixer, 69×38 ❌ | perfectPixel, 74×39 ❌ | proper-pixel-art, 69×38 ❌ | Sprite Fusion, 71×38 ❌ |
|
|
61
|
+
| :---: | :---: | :---: | :---: | :---: | :---: |
|
|
62
|
+
|  |  |  |  |  |  |
|
|
62
63
|
|
|
63
64
|
### Small cells and WebP damage
|
|
64
65
|
|
|
65
66
|
- [Monster by Denzi](https://opengameart.org/content/denzis-public-domain-art), CC0. Category `gen_small_pitch`. Native art: 32×48.
|
|
66
67
|
|
|
67
|
-
| Input, 218×339 | Xprite, 32×48
|
|
68
|
-
| :---: | :---: | :---: | :---: | :---: |
|
|
69
|
-
|  |  |  |  |  |
|
|
68
|
+
| Input, 218×339 | Xprite, 32×48 ✅ | pixel-art-fixer, 32×47 ❌ | perfectPixel, 32×49 ❌ | proper-pixel-art, 34×49 ❌ | Sprite Fusion, 32×48 ✅ |
|
|
69
|
+
| :---: | :---: | :---: | :---: | :---: | :---: |
|
|
70
|
+
|  |  |  |  |  |  |
|
|
70
71
|
|
|
71
72
|
### A small sprite on a large clear canvas
|
|
72
73
|
|
|
73
74
|
- [Nekomimi girl by cawfeecrow](https://opengameart.org/content/nekomimi-girl-portrait), CC0. Category `gen_sparse`. Native art: 123×136.
|
|
74
75
|
|
|
75
|
-
| Input, 1797×1800 | Xprite, 123×136
|
|
76
|
-
| :---: | :---: | :---: | :---: | :---: |
|
|
77
|
-
|  |  |  |  |  |
|
|
76
|
+
| Input, 1797×1800 | Xprite, 123×136 ✅ | pixel-art-fixer, 122×136 ❌ | perfectPixel, 124×136 ❌ | proper-pixel-art, 127×142 ❌ | Sprite Fusion, 118×133 ❌ |
|
|
77
|
+
| :---: | :---: | :---: | :---: | :---: | :---: |
|
|
78
|
+
|  |  |  |  |  |  |
|
|
78
79
|
|
|
79
80
|
### An off-white background
|
|
80
81
|
|
|
81
82
|
- [Dragon by ansimuz](https://opengameart.org/content/sideview-fantasy-patreon-collection), CC0. Category `gen_white`. Native art: 162×106.
|
|
82
83
|
|
|
83
|
-
| Input, 1800×1210 | Xprite, 162×106
|
|
84
|
-
| :---: | :---: | :---: | :---: | :---: |
|
|
85
|
-
|  |  |  |  |  |
|
|
84
|
+
| Input, 1800×1210 | Xprite, 162×106 ✅ | pixel-art-fixer, 325×208 ❌ | perfectPixel, 161×105 ❌ | proper-pixel-art, 168×106 ❌ | Sprite Fusion, 162×106 ✅ |
|
|
85
|
+
| :---: | :---: | :---: | :---: | :---: | :---: |
|
|
86
|
+
|  |  |  |  |  |  |
|
|
86
87
|
|
|
87
88
|
### Painterly shading inside cells
|
|
88
89
|
|
|
89
90
|
- [Naga mage, Dungeon Crawl Stone Soup](https://github.com/crawl/tiles), CC0. Category `gen_painterly`. Native art: 31×32.
|
|
90
91
|
|
|
91
|
-
| Input, 411×425 | Xprite, 31×32
|
|
92
|
-
| :---: | :---: | :---: | :---: | :---: |
|
|
93
|
-
|  |  |  |  |  |
|
|
92
|
+
| Input, 411×425 | Xprite, 31×32 ✅ | pixel-art-fixer, 30×33 ❌ | perfectPixel, 31×32 ✅ | proper-pixel-art, 31×34 ❌ | Sprite Fusion, 31×32 ✅ |
|
|
93
|
+
| :---: | :---: | :---: | :---: | :---: | :---: |
|
|
94
|
+
|  |  |  |  |  |  |
|
|
94
95
|
|
|
95
96
|
## Install
|
|
96
97
|
|
|
@@ -219,50 +220,51 @@ Each source file in [`src/`](src) describes its step.
|
|
|
219
220
|
|
|
220
221
|
## Benchmark
|
|
221
222
|
|
|
222
|
-
[`bench/`](bench) compares this package with
|
|
223
|
+
[`bench/`](bench) compares this package with four other tools on mostly CC0 native pixel art (plus one CC-BY source). The
|
|
223
224
|
benchmark damages the art in known ways and scores each result against the original. Every tool
|
|
224
225
|
runs with its default settings.
|
|
225
226
|
|
|
226
227
|
AI suite (1746 cases in 12 categories, rendered the way generators fake pixel art):
|
|
227
228
|
|
|
228
|
-
| Method | Exact size | Within 1 cell | Pixel match | ΔE |
|
|
229
|
+
| Method | Exact size | Within 1 cell | Pixel match | ΔE | Fringe (alpha 1–254) | Median time |
|
|
229
230
|
| --- | --- | --- | --- | --- | --- | --- |
|
|
230
|
-
| Xprite Auto | **83.6%** | **90.0%** | **99.
|
|
231
|
+
| Xprite Auto | **83.6%** | **90.0%** | **99.7%** | 0.85 | 0.1% | 2.80 s |
|
|
231
232
|
| pixel-art-fixer | 49.7% | 80.9% | 98.3% | 1.74 | 0.0% | 0.18 s |
|
|
232
233
|
| perfectPixel | 53.3% | 68.0% | 96.7% | 2.42 | - | 0.03 s |
|
|
233
234
|
| proper-pixel-art | 72.2% | 80.4% | 99.7% | **0.64** | 0.0% | 0.49 s |
|
|
235
|
+
| Sprite Fusion Pixel Snapper | 56.8% | 77.9% | 98.5% | 1.93 | 91.0% | — |
|
|
234
236
|
|
|
235
237
|
Exact size per category:
|
|
236
238
|
|
|
237
|
-
| Category | Xprite | pixel-art-fixer | perfectPixel | proper-pixel-art |
|
|
238
|
-
| --- | --- | --- | --- | --- |
|
|
239
|
-
| Clean (`gen_clean`) | **93.6%** | 71.7% | 58.4% | 74.6% |
|
|
240
|
-
| Sparse canvas (`gen_sparse`) | **99.0%** | 63.5% | 71.9% | 88.5% |
|
|
241
|
-
| Off-white background (`gen_white`) | 77.7% | 65.2% | 66.1% | **82.1%** |
|
|
242
|
-
| Soft outline (`gen_soft_edge`) | **95.8%** | 61.9% | 67.8% | 82.2% |
|
|
243
|
-
| Drift and WebP (`gen_webp`) | **80.9%** | 40.5% | 57.8% | 71.1% |
|
|
244
|
-
| Halos and JPEG (`gen_halo_jpeg`) | **82.7%** | 52.6% | 60.1% | 72.8% |
|
|
245
|
-
| Painterly shading (`gen_painterly`) | **84.5%** | 39.1% | 67.8% | 79.3% |
|
|
246
|
-
| Small cells (4-8 px) (`gen_small_pitch`) | **72.2%** | 47.9% | 40.2% | 43.8% |
|
|
247
|
-
| Large cells (24-48 px) (`gen_large_pitch`) | 68.2% | 22.0% | 68.2% | **78.0%** |
|
|
248
|
-
| Strong drift (`gen_wobble`) | **73.3%** | 30.8% | 30.8% | 54.1% |
|
|
249
|
-
| Two sprites (`gen_pair`) | **97.1%** | 62.7% | 45.1% | 72.5% |
|
|
250
|
-
| Drop shadow (`gen_shadow`) | **97.3%** | 64.0% | 0.0% | 84.7% |
|
|
239
|
+
| Category | Xprite | pixel-art-fixer | perfectPixel | proper-pixel-art | Sprite Fusion |
|
|
240
|
+
| --- | --- | --- | --- | --- | --- |
|
|
241
|
+
| Clean (`gen_clean`) | **93.6%** | 71.7% | 58.4% | 74.6% | 56.6% |
|
|
242
|
+
| Sparse canvas (`gen_sparse`) | **99.0%** | 63.5% | 71.9% | 88.5% | 69.8% |
|
|
243
|
+
| Off-white background (`gen_white`) | 77.7% | 65.2% | 66.1% | **82.1%** | 78.6% |
|
|
244
|
+
| Soft outline (`gen_soft_edge`) | **95.8%** | 61.9% | 67.8% | 82.2% | 59.3% |
|
|
245
|
+
| Drift and WebP (`gen_webp`) | **80.9%** | 40.5% | 57.8% | 71.1% | 48.6% |
|
|
246
|
+
| Halos and JPEG (`gen_halo_jpeg`) | **82.7%** | 52.6% | 60.1% | 72.8% | 59.0% |
|
|
247
|
+
| Painterly shading (`gen_painterly`) | **84.5%** | 39.1% | 67.8% | 79.3% | 50.0% |
|
|
248
|
+
| Small cells (4-8 px) (`gen_small_pitch`) | **72.2%** | 47.9% | 40.2% | 43.8% | 42.6% |
|
|
249
|
+
| Large cells (24-48 px) (`gen_large_pitch`) | 68.2% | 22.0% | 68.2% | **78.0%** | 57.8% |
|
|
250
|
+
| Strong drift (`gen_wobble`) | **73.3%** | 30.8% | 30.8% | 54.1% | 49.4% |
|
|
251
|
+
| Two sprites (`gen_pair`) | **97.1%** | 62.7% | 45.1% | 72.5% | 60.8% |
|
|
252
|
+
| Drop shadow (`gen_shadow`) | **97.3%** | 64.0% | 0.0% | 84.7% | 68.5% |
|
|
251
253
|
|
|
252
254
|
[pixel-bench](https://github.com/Retro-Diffusion/pixel-bench) suite v1 (7651 cases of upscales and
|
|
253
255
|
lossy pipelines):
|
|
254
256
|
|
|
255
257
|
| Method | Exact size | Within 1 cell | Pixel match | ΔE | Median time |
|
|
256
258
|
| --- | --- | --- | --- | --- | --- |
|
|
257
|
-
| Xprite Auto | **87.1%** | **90.1%** | 94.
|
|
259
|
+
| Xprite Auto | **87.1%** | **90.1%** | 94.4% | **2.76** | 0.27 s |
|
|
258
260
|
| pixel-art-fixer | 77.8% | 83.5% | **94.4%** | 2.83 | 0.04 s |
|
|
259
261
|
| perfectPixel | 24.2% | 32.9% | 89.7% | 4.31 | 0.002 s |
|
|
260
262
|
| proper-pixel-art | 16.5% | 28.6% | 93.0% | 3.85 | 0.05 s |
|
|
263
|
+
| Sprite Fusion Pixel Snapper | 6.6% | 26.8% | 91.0% | 4.42 | — |
|
|
264
|
+
|
|
265
|
+
Pixel match, ΔE and fringe are measured only on each method's exact-size cases. The sample sets can differ, so these values are not directly comparable across methods. Fringe counts alpha values from 1 to 254, including nearly opaque pixels.
|
|
261
266
|
|
|
262
|
-
|
|
263
|
-
the four tools. The damage is synthetic, so the scores show how the tools rank on these features,
|
|
264
|
-
not how often they succeed on real images. [bench/README.md](bench/README.md) gives the method,
|
|
265
|
-
the categories and the steps to run it.
|
|
267
|
+
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.
|
|
266
268
|
|
|
267
269
|
## License
|
|
268
270
|
|
package/README.zh-CN.md
CHANGED
|
@@ -11,18 +11,19 @@
|
|
|
11
11
|
|
|
12
12
|
一张真实的 AI 生成图(887×887,格宽 15 到 21 像素不等),各工具用默认设置还原的结果。Xprite 把每一刀都切在颜色边缘上,所以不会切开眼睛、靴子和剑。
|
|
13
13
|
|
|
14
|
-
| 找到的网格 | Xprite,48×49 | [pixel-art-fixer](https://github.com/Retro-Diffusion/pixel-art-fixer),47×48 | [perfectPixel](https://github.com/theamusing/perfectPixel),50×50 | [proper-pixel-art](https://github.com/KennethJAllen/proper-pixel-art),53×54 |
|
|
15
|
-
| :---: | :---: | :---: | :---: | :---: |
|
|
16
|
-
|  |  |  |  |  |
|
|
14
|
+
| 找到的网格 | Xprite,48×49 | [pixel-art-fixer](https://github.com/Retro-Diffusion/pixel-art-fixer),47×48 | [perfectPixel](https://github.com/theamusing/perfectPixel),50×50 | [proper-pixel-art](https://github.com/KennethJAllen/proper-pixel-art),53×54 | [Sprite Fusion Pixel Snapper](https://github.com/Hugo-Dz/spritefusion-pixel-snapper),56×53 |
|
|
15
|
+
| :---: | :---: | :---: | :---: | :---: | :---: |
|
|
16
|
+
|  |  |  |  |  |  |
|
|
17
17
|
|
|
18
18
|
在[我们的基准测试](#基准测试)的 AI 套件(1746 个用例)上,Xprite 得到正确尺寸的次数最多:
|
|
19
19
|
|
|
20
|
-
| 方法 | 尺寸完全正确 | 差 1 格以内 | 像素一致率 |
|
|
20
|
+
| 方法 | 尺寸完全正确 | 差 1 格以内 | 像素一致率 | 毛边(alpha 1–254) | 中位耗时 |
|
|
21
21
|
| --- | --- | --- | --- | --- | --- |
|
|
22
|
-
| Xprite Auto | **83.6%** | **90.0%** | **99.
|
|
22
|
+
| Xprite Auto | **83.6%** | **90.0%** | **99.7%** | 0.1% | 2.80 s |
|
|
23
23
|
| pixel-art-fixer | 49.7% | 80.9% | 98.3% | 0.0% | 0.18 s |
|
|
24
24
|
| perfectPixel | 53.3% | 68.0% | 96.7% | - | 0.03 s |
|
|
25
25
|
| proper-pixel-art | 72.2% | 80.4% | 99.7% | 0.0% | 0.49 s |
|
|
26
|
+
| Sprite Fusion Pixel Snapper | 56.8% | 77.9% | 98.5% | 91.0% | — |
|
|
26
27
|
|
|
27
28
|
## 目录
|
|
28
29
|
|
|
@@ -35,55 +36,55 @@
|
|
|
35
36
|
|
|
36
37
|
## 示例
|
|
37
38
|
|
|
38
|
-
每个示例取自基准测试 AI 套件的一个类别:把 CC0
|
|
39
|
+
每个示例取自基准测试 AI 套件的一个类别:把 CC0 像素画按生成模型的方式渲染,原画尺寸已知。尺寸是可见画面的格数,✅ 表示与原画尺寸完全一致。每个工具都使用默认设置。
|
|
39
40
|
|
|
40
41
|
### 一张画布上的两个角色
|
|
41
42
|
|
|
42
43
|
- [哥布林,作者 LuizMelo](https://luizmelo.itch.io/monsters-creatures-fantasy),CC0。类别 `gen_pair`。原画:78×36。
|
|
43
44
|
|
|
44
|
-
| 输入,1800×845 | Xprite,78×36
|
|
45
|
-
| :---: | :---: | :---: | :---: | :---: |
|
|
46
|
-
|  |  |  |  |  |
|
|
45
|
+
| 输入,1800×845 | Xprite,78×36 ✅ | pixel-art-fixer,78×35 ❌ | perfectPixel,80×36 ❌ | proper-pixel-art,79×36 ❌ | Sprite Fusion,78×36 ✅ |
|
|
46
|
+
| :---: | :---: | :---: | :---: | :---: | :---: |
|
|
47
|
+
|  |  |  |  |  |  |
|
|
47
48
|
|
|
48
49
|
### 格子漂移和抖动
|
|
49
50
|
|
|
50
51
|
- [巨龙,作者 ansimuz](https://opengameart.org/content/sideview-fantasy-patreon-collection),CC0。类别 `gen_wobble`。原画:71×39。
|
|
51
52
|
|
|
52
|
-
| 输入,1226×664 | Xprite,71×39
|
|
53
|
-
| :---: | :---: | :---: | :---: | :---: |
|
|
54
|
-
|  |  |  |  |  |
|
|
53
|
+
| 输入,1226×664 | Xprite,71×39 ✅ | pixel-art-fixer,69×38 ❌ | perfectPixel,74×39 ❌ | proper-pixel-art,69×38 ❌ | Sprite Fusion,71×38 ❌ |
|
|
54
|
+
| :---: | :---: | :---: | :---: | :---: | :---: |
|
|
55
|
+
|  |  |  |  |  |  |
|
|
55
56
|
|
|
56
57
|
### 小格子和 WebP 压缩损伤
|
|
57
58
|
|
|
58
59
|
- [怪物,作者 Denzi](https://opengameart.org/content/denzis-public-domain-art),CC0。类别 `gen_small_pitch`。原画:32×48。
|
|
59
60
|
|
|
60
|
-
| 输入,218×339 | Xprite,32×48
|
|
61
|
-
| :---: | :---: | :---: | :---: | :---: |
|
|
62
|
-
|  |  |  |  |  |
|
|
61
|
+
| 输入,218×339 | Xprite,32×48 ✅ | pixel-art-fixer,32×47 ❌ | perfectPixel,32×49 ❌ | proper-pixel-art,34×49 ❌ | Sprite Fusion,32×48 ✅ |
|
|
62
|
+
| :---: | :---: | :---: | :---: | :---: | :---: |
|
|
63
|
+
|  |  |  |  |  |  |
|
|
63
64
|
|
|
64
65
|
### 大块透明画布上的小角色
|
|
65
66
|
|
|
66
67
|
- [猫耳少女,作者 cawfeecrow](https://opengameart.org/content/nekomimi-girl-portrait),CC0。类别 `gen_sparse`。原画:123×136。
|
|
67
68
|
|
|
68
|
-
| 输入,1797×1800 | Xprite,123×136
|
|
69
|
-
| :---: | :---: | :---: | :---: | :---: |
|
|
70
|
-
|  |  |  |  |  |
|
|
69
|
+
| 输入,1797×1800 | Xprite,123×136 ✅ | pixel-art-fixer,122×136 ❌ | perfectPixel,124×136 ❌ | proper-pixel-art,127×142 ❌ | Sprite Fusion,118×133 ❌ |
|
|
70
|
+
| :---: | :---: | :---: | :---: | :---: | :---: |
|
|
71
|
+
|  |  |  |  |  |  |
|
|
71
72
|
|
|
72
73
|
### 近白色背景
|
|
73
74
|
|
|
74
75
|
- [飞龙,作者 ansimuz](https://opengameart.org/content/sideview-fantasy-patreon-collection),CC0。类别 `gen_white`。原画:162×106。
|
|
75
76
|
|
|
76
|
-
| 输入,1800×1210 | Xprite,162×106
|
|
77
|
-
| :---: | :---: | :---: | :---: | :---: |
|
|
78
|
-
|  |  |  |  |  |
|
|
77
|
+
| 输入,1800×1210 | Xprite,162×106 ✅ | pixel-art-fixer,325×208 ❌ | perfectPixel,161×105 ❌ | proper-pixel-art,168×106 ❌ | Sprite Fusion,162×106 ✅ |
|
|
78
|
+
| :---: | :---: | :---: | :---: | :---: | :---: |
|
|
79
|
+
|  |  |  |  |  |  |
|
|
79
80
|
|
|
80
81
|
### 格内有绘画式明暗
|
|
81
82
|
|
|
82
83
|
- [娜迦法师,Dungeon Crawl Stone Soup](https://github.com/crawl/tiles),CC0。类别 `gen_painterly`。原画:31×32。
|
|
83
84
|
|
|
84
|
-
| 输入,411×425 | Xprite,31×32
|
|
85
|
-
| :---: | :---: | :---: | :---: | :---: |
|
|
86
|
-
|  |  |  |  |  |
|
|
85
|
+
| 输入,411×425 | Xprite,31×32 ✅ | pixel-art-fixer,30×33 ❌ | perfectPixel,31×32 ✅ | proper-pixel-art,31×34 ❌ | Sprite Fusion,31×32 ✅ |
|
|
86
|
+
| :---: | :---: | :---: | :---: | :---: | :---: |
|
|
87
|
+
|  |  |  |  |  |  |
|
|
87
88
|
|
|
88
89
|
## 安装
|
|
89
90
|
|
|
@@ -196,44 +197,48 @@ const grid = await detectWithAlgorithmAsync(image);
|
|
|
196
197
|
|
|
197
198
|
## 基准测试
|
|
198
199
|
|
|
199
|
-
[`bench/`](bench)
|
|
200
|
+
[`bench/`](bench) 在以 CC0 为主(另有一个 CC-BY 来源)的原生像素画上比较本包和另外四个工具。基准测试以已知方式损伤原画,再把每个结果和原画对比打分。每个工具都使用默认设置。
|
|
200
201
|
|
|
201
202
|
AI 套件(12 个类别共 1746 个用例,按生成模型伪造像素画的方式渲染):
|
|
202
203
|
|
|
203
|
-
| 方法 | 尺寸完全正确 | 差 1 格以内 | 像素一致率 | ΔE |
|
|
204
|
+
| 方法 | 尺寸完全正确 | 差 1 格以内 | 像素一致率 | ΔE | 毛边(alpha 1–254) | 中位耗时 |
|
|
204
205
|
| --- | --- | --- | --- | --- | --- | --- |
|
|
205
|
-
| Xprite Auto | **83.6%** | **90.0%** | **99.
|
|
206
|
+
| Xprite Auto | **83.6%** | **90.0%** | **99.7%** | 0.85 | 0.1% | 2.80 s |
|
|
206
207
|
| pixel-art-fixer | 49.7% | 80.9% | 98.3% | 1.74 | 0.0% | 0.18 s |
|
|
207
208
|
| perfectPixel | 53.3% | 68.0% | 96.7% | 2.42 | - | 0.03 s |
|
|
208
209
|
| proper-pixel-art | 72.2% | 80.4% | 99.7% | **0.64** | 0.0% | 0.49 s |
|
|
210
|
+
| Sprite Fusion Pixel Snapper | 56.8% | 77.9% | 98.5% | 1.93 | 91.0% | — |
|
|
209
211
|
|
|
210
212
|
各类别的尺寸完全正确率:
|
|
211
213
|
|
|
212
|
-
| 类别 | Xprite | pixel-art-fixer | perfectPixel | proper-pixel-art |
|
|
213
|
-
| --- | --- | --- | --- | --- |
|
|
214
|
-
| 干净 (`gen_clean`) | **93.6%** | 71.7% | 58.4% | 74.6% |
|
|
215
|
-
| 稀疏画布 (`gen_sparse`) | **99.0%** | 63.5% | 71.9% | 88.5% |
|
|
216
|
-
| 近白色背景 (`gen_white`) | 77.7% | 65.2% | 66.1% | **82.1%** |
|
|
217
|
-
| 柔和轮廓 (`gen_soft_edge`) | **95.8%** | 61.9% | 67.8% | 82.2% |
|
|
218
|
-
| 漂移和 WebP (`gen_webp`) | **80.9%** | 40.5% | 57.8% | 71.1% |
|
|
219
|
-
| 光晕和 JPEG (`gen_halo_jpeg`) | **82.7%** | 52.6% | 60.1% | 72.8% |
|
|
220
|
-
| 绘画式明暗 (`gen_painterly`) | **84.5%** | 39.1% | 67.8% | 79.3% |
|
|
221
|
-
| 小格子(4-8 px) (`gen_small_pitch`) | **72.2%** | 47.9% | 40.2% | 43.8% |
|
|
222
|
-
| 大格子(24-48 px) (`gen_large_pitch`) | 68.2% | 22.0% | 68.2% | **78.0%** |
|
|
223
|
-
| 强漂移 (`gen_wobble`) | **73.3%** | 30.8% | 30.8% | 54.1% |
|
|
224
|
-
| 两个角色 (`gen_pair`) | **97.1%** | 62.7% | 45.1% | 72.5% |
|
|
225
|
-
| 投影 (`gen_shadow`) | **97.3%** | 64.0% | 0.0% | 84.7% |
|
|
214
|
+
| 类别 | Xprite | pixel-art-fixer | perfectPixel | proper-pixel-art | Sprite Fusion |
|
|
215
|
+
| --- | --- | --- | --- | --- | --- |
|
|
216
|
+
| 干净 (`gen_clean`) | **93.6%** | 71.7% | 58.4% | 74.6% | 56.6% |
|
|
217
|
+
| 稀疏画布 (`gen_sparse`) | **99.0%** | 63.5% | 71.9% | 88.5% | 69.8% |
|
|
218
|
+
| 近白色背景 (`gen_white`) | 77.7% | 65.2% | 66.1% | **82.1%** | 78.6% |
|
|
219
|
+
| 柔和轮廓 (`gen_soft_edge`) | **95.8%** | 61.9% | 67.8% | 82.2% | 59.3% |
|
|
220
|
+
| 漂移和 WebP (`gen_webp`) | **80.9%** | 40.5% | 57.8% | 71.1% | 48.6% |
|
|
221
|
+
| 光晕和 JPEG (`gen_halo_jpeg`) | **82.7%** | 52.6% | 60.1% | 72.8% | 59.0% |
|
|
222
|
+
| 绘画式明暗 (`gen_painterly`) | **84.5%** | 39.1% | 67.8% | 79.3% | 50.0% |
|
|
223
|
+
| 小格子(4-8 px) (`gen_small_pitch`) | **72.2%** | 47.9% | 40.2% | 43.8% | 42.6% |
|
|
224
|
+
| 大格子(24-48 px) (`gen_large_pitch`) | 68.2% | 22.0% | 68.2% | **78.0%** | 57.8% |
|
|
225
|
+
| 强漂移 (`gen_wobble`) | **73.3%** | 30.8% | 30.8% | 54.1% | 49.4% |
|
|
226
|
+
| 两个角色 (`gen_pair`) | **97.1%** | 62.7% | 45.1% | 72.5% | 60.8% |
|
|
227
|
+
| 投影 (`gen_shadow`) | **97.3%** | 64.0% | 0.0% | 84.7% | 68.5% |
|
|
226
228
|
|
|
227
229
|
[pixel-bench](https://github.com/Retro-Diffusion/pixel-bench) v1 套件(7651 个放大和有损处理用例):
|
|
228
230
|
|
|
229
231
|
| 方法 | 尺寸完全正确 | 差 1 格以内 | 像素一致率 | ΔE | 中位耗时 |
|
|
230
232
|
| --- | --- | --- | --- | --- | --- |
|
|
231
|
-
| Xprite Auto | **87.1%** | **90.1%** | 94.
|
|
233
|
+
| Xprite Auto | **87.1%** | **90.1%** | 94.4% | **2.76** | 0.27 s |
|
|
232
234
|
| pixel-art-fixer | 77.8% | 83.5% | **94.4%** | 2.83 | 0.04 s |
|
|
233
235
|
| perfectPixel | 24.2% | 32.9% | 89.7% | 4.31 | 0.002 s |
|
|
234
236
|
| proper-pixel-art | 16.5% | 28.6% | 93.0% | 3.85 | 0.05 s |
|
|
237
|
+
| Sprite Fusion Pixel Snapper | 6.6% | 26.8% | 91.0% | 4.42 | — |
|
|
238
|
+
|
|
239
|
+
像素一致率、ΔE 和毛边只在各方法尺寸完全正确的用例上统计。样本集合可能不同,这些数值不能在方法之间直接比较。毛边统计包含 alpha 为 1 至 254 的像素,包括接近不透明的像素。
|
|
235
240
|
|
|
236
|
-
|
|
241
|
+
各方法使用默认颜色处理方式,调色板可能不同;表中是端到端结果。计时方式也有差异,包含进程启动和 PNG 读写的计时未列入表格。所有结果使用相同的用例和评分代码,部分结果来自先前的运行。损伤是合成的,不能代表真实图片的成功率。各方法的版本、计时口径和复现方法见 [bench/README.md](bench/README.md)。
|
|
237
242
|
|
|
238
243
|
## 许可证
|
|
239
244
|
|
package/dist/palette-snap.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { oklabToRgb, rgbToOklab } from "./quantization.js";
|
|
2
2
|
import { PixelationDither } from "./types.js";
|
|
3
3
|
/**
|
|
4
4
|
* Optional "snap palette" step for a recovered (small) image: collapse the many near-shades that
|
|
5
5
|
* AI art and JPEG sources leave behind into a clean, flat palette, or snap to a palette the user
|
|
6
6
|
* supplies. Rare-but-important colours (skin tones, highlights) are kept by two rules:
|
|
7
|
-
* - only near-duplicates (within
|
|
7
|
+
* - only near-duplicates (within the OKLab distance threshold) are ever merged without a budget, and
|
|
8
8
|
* - k-means is seeded by weighted farthest-point selection, so a colour far from everything else
|
|
9
9
|
* gets its own palette entry before a frequent shade that is only a little different.
|
|
10
10
|
* Alpha is never clustered: each pixel keeps its own opacity and fully transparent pixels stay put.
|
|
@@ -15,8 +15,8 @@ import { PixelationDither } from "./types.js";
|
|
|
15
15
|
const MIN_COLORS = 2;
|
|
16
16
|
const MAX_COLORS = 256;
|
|
17
17
|
const DEFAULT_COLORS = 16;
|
|
18
|
-
/**
|
|
19
|
-
const
|
|
18
|
+
/** OKLab Euclidean distance below which two colours are treated as near-duplicates. */
|
|
19
|
+
const NEAR_DUPLICATE_OKLAB_DISTANCE = 0.03;
|
|
20
20
|
/** Seeding score = distance * count^SEED_WEIGHT: mostly distance, a little frequency. */
|
|
21
21
|
const SEED_WEIGHT = 0.25;
|
|
22
22
|
/** Cluster pull = count^PULL_WEIGHT, so large flat areas do not drag centroids off rare colours. */
|
|
@@ -51,7 +51,7 @@ function histogram(image) {
|
|
|
51
51
|
seen.set(key, at);
|
|
52
52
|
shades.push({
|
|
53
53
|
rgb: [data[o], data[o + 1], data[o + 2]],
|
|
54
|
-
|
|
54
|
+
oklab: rgbToOklab(data[o], data[o + 1], data[o + 2]),
|
|
55
55
|
count: 0,
|
|
56
56
|
});
|
|
57
57
|
}
|
|
@@ -60,17 +60,26 @@ function histogram(image) {
|
|
|
60
60
|
}
|
|
61
61
|
return { shades, index };
|
|
62
62
|
}
|
|
63
|
-
/** Greedy leader clustering, heaviest first: only colours within
|
|
63
|
+
/** Greedy leader clustering, heaviest first: only colours within the OKLab threshold share a group. */
|
|
64
64
|
function mergeNearDuplicates(shades) {
|
|
65
65
|
const order = shades.map((_, i) => i).sort((a, b) => shades[b].count - shades[a].count || a - b);
|
|
66
66
|
const groups = [];
|
|
67
|
+
const threshold2 = NEAR_DUPLICATE_OKLAB_DISTANCE ** 2;
|
|
67
68
|
for (const i of order) {
|
|
68
|
-
|
|
69
|
+
const oklab = shades[i].oklab;
|
|
70
|
+
let best = -1, bestDistance2 = threshold2;
|
|
69
71
|
for (let g = 0; g < groups.length; g++) {
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
72
|
+
const leader = shades[groups[g][0]].oklab;
|
|
73
|
+
const dx = oklab[0] - leader[0];
|
|
74
|
+
const dy = oklab[1] - leader[1];
|
|
75
|
+
const dz = oklab[2] - leader[2];
|
|
76
|
+
const d2 = dx * dx + dy * dy + dz * dz;
|
|
77
|
+
if (d2 <= bestDistance2) {
|
|
78
|
+
bestDistance2 = d2;
|
|
73
79
|
best = g;
|
|
80
|
+
// Exact match is optimal; skip remaining groups.
|
|
81
|
+
if (d2 === 0)
|
|
82
|
+
break;
|
|
74
83
|
}
|
|
75
84
|
}
|
|
76
85
|
if (best < 0)
|
|
@@ -87,10 +96,11 @@ function toPoints(shades, groups) {
|
|
|
87
96
|
for (const m of members) {
|
|
88
97
|
count += shades[m].count;
|
|
89
98
|
for (let c = 0; c < 3; c++)
|
|
90
|
-
sum[c] += shades[m].
|
|
99
|
+
sum[c] += shades[m].oklab[c] * shades[m].count;
|
|
91
100
|
}
|
|
101
|
+
const oklab = [sum[0] / count, sum[1] / count, sum[2] / count];
|
|
92
102
|
return {
|
|
93
|
-
|
|
103
|
+
oklab,
|
|
94
104
|
count,
|
|
95
105
|
members,
|
|
96
106
|
};
|
|
@@ -103,8 +113,8 @@ function seed(points, k) {
|
|
|
103
113
|
for (let i = 1; i < points.length; i++)
|
|
104
114
|
if (points[i].count > points[first].count)
|
|
105
115
|
first = i;
|
|
106
|
-
const seeds = [points[first].
|
|
107
|
-
const nearest = points.map((p) => distance(p.
|
|
116
|
+
const seeds = [points[first].oklab];
|
|
117
|
+
const nearest = points.map((p) => distance(p.oklab, seeds[0]));
|
|
108
118
|
while (seeds.length < k) {
|
|
109
119
|
let pick = -1, pickScore = 0;
|
|
110
120
|
for (let i = 0; i < points.length; i++) {
|
|
@@ -116,21 +126,46 @@ function seed(points, k) {
|
|
|
116
126
|
}
|
|
117
127
|
if (pick < 0)
|
|
118
128
|
break;
|
|
119
|
-
seeds.push(points[pick].
|
|
129
|
+
seeds.push(points[pick].oklab);
|
|
120
130
|
for (let i = 0; i < points.length; i++)
|
|
121
|
-
nearest[i] = Math.min(nearest[i], distance(points[i].
|
|
131
|
+
nearest[i] = Math.min(nearest[i], distance(points[i].oklab, points[pick].oklab));
|
|
122
132
|
}
|
|
123
133
|
return seeds;
|
|
124
134
|
}
|
|
125
135
|
function cluster(points, k) {
|
|
126
136
|
const centers = seed(points, k);
|
|
127
137
|
const assign = new Int32Array(points.length).fill(-1);
|
|
138
|
+
// Flatten points and centres into typed arrays so the assignment loop avoids
|
|
139
|
+
// per-access bounds checks and object property lookups in the inner loop.
|
|
140
|
+
const n = points.length;
|
|
141
|
+
const pointData = new Float64Array(n * 3);
|
|
142
|
+
const pointWeight = new Float64Array(n);
|
|
143
|
+
for (let i = 0; i < n; i++) {
|
|
144
|
+
pointData[i * 3] = points[i].oklab[0];
|
|
145
|
+
pointData[i * 3 + 1] = points[i].oklab[1];
|
|
146
|
+
pointData[i * 3 + 2] = points[i].oklab[2];
|
|
147
|
+
pointWeight[i] = points[i].count ** PULL_WEIGHT;
|
|
148
|
+
}
|
|
149
|
+
const centerData = new Float64Array(centers.length * 3);
|
|
150
|
+
const writeCenter = (c, value) => {
|
|
151
|
+
centerData[c * 3] = value[0];
|
|
152
|
+
centerData[c * 3 + 1] = value[1];
|
|
153
|
+
centerData[c * 3 + 2] = value[2];
|
|
154
|
+
};
|
|
155
|
+
for (let c = 0; c < centers.length; c++)
|
|
156
|
+
writeCenter(c, centers[c]);
|
|
128
157
|
for (let iteration = 0; iteration < MAX_ITERATIONS; iteration++) {
|
|
129
158
|
let changed = false;
|
|
130
|
-
for (let i = 0; i <
|
|
159
|
+
for (let i = 0; i < n; i++) {
|
|
160
|
+
const px = pointData[i * 3];
|
|
161
|
+
const py = pointData[i * 3 + 1];
|
|
162
|
+
const pz = pointData[i * 3 + 2];
|
|
131
163
|
let best = 0, bestDistance = Infinity;
|
|
132
164
|
for (let c = 0; c < centers.length; c++) {
|
|
133
|
-
const
|
|
165
|
+
const dx = px - centerData[c * 3];
|
|
166
|
+
const dy = py - centerData[c * 3 + 1];
|
|
167
|
+
const dz = pz - centerData[c * 3 + 2];
|
|
168
|
+
const d = dx * dx + dy * dy + dz * dz;
|
|
134
169
|
if (d < bestDistance) {
|
|
135
170
|
bestDistance = d;
|
|
136
171
|
best = c;
|
|
@@ -144,32 +179,37 @@ function cluster(points, k) {
|
|
|
144
179
|
if (!changed)
|
|
145
180
|
break;
|
|
146
181
|
const sums = centers.map(() => [0, 0, 0, 0]);
|
|
147
|
-
for (let i = 0; i <
|
|
148
|
-
const weight =
|
|
182
|
+
for (let i = 0; i < n; i++) {
|
|
183
|
+
const weight = pointWeight[i];
|
|
149
184
|
const s = sums[assign[i]];
|
|
150
|
-
|
|
151
|
-
|
|
185
|
+
s[0] += pointData[i * 3] * weight;
|
|
186
|
+
s[1] += pointData[i * 3 + 1] * weight;
|
|
187
|
+
s[2] += pointData[i * 3 + 2] * weight;
|
|
152
188
|
s[3] += weight;
|
|
153
189
|
}
|
|
154
190
|
for (let c = 0; c < centers.length; c++)
|
|
155
191
|
if (sums[c][3] > 0)
|
|
156
|
-
|
|
192
|
+
writeCenter(c, [
|
|
193
|
+
sums[c][0] / sums[c][3],
|
|
194
|
+
sums[c][1] / sums[c][3],
|
|
195
|
+
sums[c][2] / sums[c][3],
|
|
196
|
+
]);
|
|
157
197
|
}
|
|
158
198
|
return Array.from(assign);
|
|
159
199
|
}
|
|
160
|
-
function
|
|
200
|
+
function paletteOklabs(palette) {
|
|
161
201
|
const visible = palette.filter((color) => color[3] > 0);
|
|
162
202
|
if (!visible.length)
|
|
163
203
|
throw new RangeError("Choose a palette containing a visible color.");
|
|
164
204
|
return {
|
|
165
|
-
|
|
205
|
+
oklabs: visible.map((c) => rgbToOklab(c[0], c[1], c[2])),
|
|
166
206
|
rgb: visible.map((c) => c),
|
|
167
207
|
};
|
|
168
208
|
}
|
|
169
|
-
function nearestPaletteRgb(
|
|
209
|
+
function nearestPaletteRgb(oklab, oklabs, rgb) {
|
|
170
210
|
let best = 0, bestDistance = Infinity;
|
|
171
|
-
for (let i = 0; i <
|
|
172
|
-
const d = distance(
|
|
211
|
+
for (let i = 0; i < oklabs.length; i++) {
|
|
212
|
+
const d = distance(oklab, oklabs[i]);
|
|
173
213
|
if (d < bestDistance) {
|
|
174
214
|
bestDistance = d;
|
|
175
215
|
best = i;
|
|
@@ -214,7 +254,7 @@ function nearestPaletteRgbEuclidean(r, g, b, palette) {
|
|
|
214
254
|
/**
|
|
215
255
|
* Ordered Bayer 8×8 → palette. Port of tensorhead/dithering `ordered_palette` (MIT):
|
|
216
256
|
* threshold offset (t − 0.5) scaled by each channel's palette spread, then RGB nearest.
|
|
217
|
-
* Unlike ±RGB Bayer then
|
|
257
|
+
* Unlike ±RGB Bayer then OKLab nearest, this still dithers when every pixel is already a palette colour.
|
|
218
258
|
*/
|
|
219
259
|
function orderedBayer8ToPalette(image, palette) {
|
|
220
260
|
const spread = paletteSpread(palette);
|
|
@@ -235,11 +275,11 @@ function orderedBayer8ToPalette(image, palette) {
|
|
|
235
275
|
return { width, height, data: output };
|
|
236
276
|
}
|
|
237
277
|
/**
|
|
238
|
-
* Floyd–Steinberg error diffusion to `palette` in
|
|
278
|
+
* Floyd–Steinberg error diffusion to `palette` in OKLab-nearest space.
|
|
239
279
|
* Transparent pixels are left alone.
|
|
240
280
|
*/
|
|
241
281
|
function floydSteinbergToPalette(image, palette) {
|
|
242
|
-
const {
|
|
282
|
+
const { oklabs, rgb } = paletteOklabs(palette);
|
|
243
283
|
const output = new Uint8ClampedArray(image.data);
|
|
244
284
|
const { width, height, data } = image;
|
|
245
285
|
const error = new Float32Array(width * height * 3);
|
|
@@ -254,7 +294,7 @@ function floydSteinbergToPalette(image, palette) {
|
|
|
254
294
|
clampByte(data[at + 1] + error[i * 3 + 1]),
|
|
255
295
|
clampByte(data[at + 2] + error[i * 3 + 2]),
|
|
256
296
|
];
|
|
257
|
-
const chosen = nearestPaletteRgb(
|
|
297
|
+
const chosen = nearestPaletteRgb(rgbToOklab(sample[0], sample[1], sample[2]), oklabs, rgb);
|
|
258
298
|
output[at] = chosen[0];
|
|
259
299
|
output[at + 1] = chosen[1];
|
|
260
300
|
output[at + 2] = chosen[2];
|
|
@@ -282,7 +322,7 @@ function ditherToPalette(image, palette, dither) {
|
|
|
282
322
|
/** Build the reduced RGB palette (k-means or the given list); null when the image already fits. */
|
|
283
323
|
function buildPalette(shades, options) {
|
|
284
324
|
if (options.palette)
|
|
285
|
-
return
|
|
325
|
+
return paletteOklabs(options.palette).rgb;
|
|
286
326
|
const groups = mergeNearDuplicates(shades);
|
|
287
327
|
const colors = options.adaptive
|
|
288
328
|
? Math.min(groups.length, MAX_COLORS)
|
|
@@ -293,15 +333,15 @@ function buildPalette(shades, options) {
|
|
|
293
333
|
const assignment = points.length <= colors ? points.map((_, i) => i) : cluster(points, colors);
|
|
294
334
|
const sums = new Map();
|
|
295
335
|
points.forEach((point, i) => {
|
|
296
|
-
const entry = sums.get(assignment[i]) ?? {
|
|
336
|
+
const entry = sums.get(assignment[i]) ?? { oklab: [0, 0, 0], count: 0 };
|
|
297
337
|
for (let c = 0; c < 3; c++)
|
|
298
|
-
entry.
|
|
338
|
+
entry.oklab[c] += point.oklab[c] * point.count;
|
|
299
339
|
entry.count += point.count;
|
|
300
340
|
sums.set(assignment[i], entry);
|
|
301
341
|
});
|
|
302
342
|
const palette = [];
|
|
303
|
-
for (const {
|
|
304
|
-
const [r, g, b] =
|
|
343
|
+
for (const { oklab, count } of sums.values()) {
|
|
344
|
+
const [r, g, b] = oklabToRgb(oklab[0] / count, oklab[1] / count, oklab[2] / count);
|
|
305
345
|
palette.push([r, g, b, 255]);
|
|
306
346
|
}
|
|
307
347
|
return palette;
|
|
@@ -311,8 +351,8 @@ function remapShades(image, shades, index, options) {
|
|
|
311
351
|
const output = new Uint8ClampedArray(image.data);
|
|
312
352
|
let target;
|
|
313
353
|
if (options.palette) {
|
|
314
|
-
const {
|
|
315
|
-
target = shades.map((shade) => nearestPaletteRgb(shade.
|
|
354
|
+
const { oklabs, rgb } = paletteOklabs(options.palette);
|
|
355
|
+
target = shades.map((shade) => nearestPaletteRgb(shade.oklab, oklabs, rgb));
|
|
316
356
|
}
|
|
317
357
|
else {
|
|
318
358
|
const groups = mergeNearDuplicates(shades);
|
|
@@ -325,15 +365,15 @@ function remapShades(image, shades, index, options) {
|
|
|
325
365
|
const assignment = points.length <= colors ? points.map((_, i) => i) : cluster(points, colors);
|
|
326
366
|
const sums = new Map();
|
|
327
367
|
points.forEach((point, i) => {
|
|
328
|
-
const entry = sums.get(assignment[i]) ?? {
|
|
368
|
+
const entry = sums.get(assignment[i]) ?? { oklab: [0, 0, 0], count: 0 };
|
|
329
369
|
for (let c = 0; c < 3; c++)
|
|
330
|
-
entry.
|
|
370
|
+
entry.oklab[c] += point.oklab[c] * point.count;
|
|
331
371
|
entry.count += point.count;
|
|
332
372
|
sums.set(assignment[i], entry);
|
|
333
373
|
});
|
|
334
374
|
const colorOf = new Map();
|
|
335
|
-
for (const [id, {
|
|
336
|
-
colorOf.set(id,
|
|
375
|
+
for (const [id, { oklab, count }] of sums)
|
|
376
|
+
colorOf.set(id, oklabToRgb(oklab[0] / count, oklab[1] / count, oklab[2] / count));
|
|
337
377
|
target = Array.from({ length: shades.length }, () => [0, 0, 0]);
|
|
338
378
|
points.forEach((point, i) => {
|
|
339
379
|
for (const member of point.members)
|
|
@@ -358,7 +398,7 @@ export function snapPalette(image, options) {
|
|
|
358
398
|
const { shades, index } = histogram(image);
|
|
359
399
|
if (dither === PixelationDither.None)
|
|
360
400
|
return remapShades(image, shades, index, options);
|
|
361
|
-
// Ordered / Floyd–Steinberg need a concrete palette, then per-pixel
|
|
401
|
+
// Ordered / Floyd–Steinberg need a concrete palette, then per-pixel OKLab nearest.
|
|
362
402
|
const palette = buildPalette(shades, options);
|
|
363
403
|
if (!palette) {
|
|
364
404
|
// Already within budget and no user palette: dither among the image's own shades.
|
package/dist/quantization.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { Rgba } from "./base.ts";
|
|
2
2
|
import type { PaletteEntry } from "./types.ts";
|
|
3
|
-
|
|
3
|
+
export type Oklab = readonly [lightness: number, a: number, b: number];
|
|
4
|
+
/** OKLab plus opacity; invisible RGB never participates in matching. */
|
|
4
5
|
export declare function paletteCoordinates(color: Rgba): Rgba;
|
|
6
|
+
/** Convert opaque sRGB bytes to OKLab coordinates. Cached per packed RGB. */
|
|
7
|
+
export declare function rgbToOklab(r: number, g: number, b: number): Oklab;
|
|
8
|
+
/** Convert OKLab coordinates back to 8-bit sRGB. */
|
|
9
|
+
export declare function oklabToRgb(lightness: number, a: number, b: number): [number, number, number];
|
|
5
10
|
/** Variance-driven color partitioning keeps accents instead of selecting only frequent shades. */
|
|
6
11
|
export declare function optimizePalette(entries: PaletteEntry[], limit: number): PaletteEntry[];
|
package/dist/quantization.js
CHANGED
|
@@ -3,6 +3,7 @@ import { clampByte } from "./base.js";
|
|
|
3
3
|
const CHANNELS = 4;
|
|
4
4
|
const COLOR_CHANNELS = 3;
|
|
5
5
|
const LINEAR_THRESHOLD = 0.04045;
|
|
6
|
+
const LINEAR_TO_SRGB_THRESHOLD = 0.0031308;
|
|
6
7
|
const LINEAR_DIVISOR = 12.92;
|
|
7
8
|
const GAMMA_OFFSET = 0.055;
|
|
8
9
|
const GAMMA_SCALE = 1.055;
|
|
@@ -27,23 +28,64 @@ const LMS_TO_OKLAB = [
|
|
|
27
28
|
[1.9779984951, -2.428592205, 0.4505937099],
|
|
28
29
|
[0.0259040371, 0.7827717662, -0.808675766],
|
|
29
30
|
];
|
|
31
|
+
const OKLAB_TO_LMS = [
|
|
32
|
+
[1, 0.3963377774, 0.2158037573],
|
|
33
|
+
[1, -0.1055613458, -0.0638541728],
|
|
34
|
+
[1, -0.0894841775, -1.291485548],
|
|
35
|
+
];
|
|
36
|
+
const LMS_TO_LINEAR_SRGB = [
|
|
37
|
+
[4.0767416621, -3.3077115913, 0.2309699292],
|
|
38
|
+
[-1.2684380046, 2.6097574011, -0.3413193965],
|
|
39
|
+
[-0.0041960863, -0.7034186147, 1.707614701],
|
|
40
|
+
];
|
|
30
41
|
function dot(row, a, b, c) {
|
|
31
42
|
return row[0] * a + row[1] * b + row[2] * c;
|
|
32
43
|
}
|
|
33
|
-
|
|
44
|
+
function linearRgbToOklab(r, g, b) {
|
|
45
|
+
const l = Math.cbrt(dot(LINEAR_SRGB_TO_LMS[0], r, g, b));
|
|
46
|
+
const m = Math.cbrt(dot(LINEAR_SRGB_TO_LMS[1], r, g, b));
|
|
47
|
+
const s = Math.cbrt(dot(LINEAR_SRGB_TO_LMS[2], r, g, b));
|
|
48
|
+
return [
|
|
49
|
+
dot(LMS_TO_OKLAB[0], l, m, s),
|
|
50
|
+
dot(LMS_TO_OKLAB[1], l, m, s),
|
|
51
|
+
dot(LMS_TO_OKLAB[2], l, m, s),
|
|
52
|
+
];
|
|
53
|
+
}
|
|
54
|
+
/** OKLab plus opacity; invisible RGB never participates in matching. */
|
|
34
55
|
export function paletteCoordinates(color) {
|
|
35
56
|
const opacity = color[3] / UINT8_MAX;
|
|
36
57
|
const r = linearChannel(color[0]);
|
|
37
58
|
const g = linearChannel(color[1]);
|
|
38
59
|
const b = linearChannel(color[2]);
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
60
|
+
const [lightness, a, blueYellow] = linearRgbToOklab(r, g, b);
|
|
61
|
+
return [lightness * opacity, a * opacity, blueYellow * opacity, opacity];
|
|
62
|
+
}
|
|
63
|
+
const oklabCache = new Map();
|
|
64
|
+
/** Convert opaque sRGB bytes to OKLab coordinates. Cached per packed RGB. */
|
|
65
|
+
export function rgbToOklab(r, g, b) {
|
|
66
|
+
const key = (r << 16) | (g << 8) | b;
|
|
67
|
+
const hit = oklabCache.get(key);
|
|
68
|
+
if (hit !== undefined)
|
|
69
|
+
return hit;
|
|
70
|
+
const value = linearRgbToOklab(linearChannel(r), linearChannel(g), linearChannel(b));
|
|
71
|
+
oklabCache.set(key, value);
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
function srgbByteFromLinear(value) {
|
|
75
|
+
const encoded = value <= LINEAR_TO_SRGB_THRESHOLD
|
|
76
|
+
? value * LINEAR_DIVISOR
|
|
77
|
+
: GAMMA_SCALE * value ** (1 / GAMMA_EXPONENT) - GAMMA_OFFSET;
|
|
78
|
+
return clampByte(encoded * UINT8_MAX);
|
|
79
|
+
}
|
|
80
|
+
/** Convert OKLab coordinates back to 8-bit sRGB. */
|
|
81
|
+
export function oklabToRgb(lightness, a, b) {
|
|
82
|
+
const l = dot(OKLAB_TO_LMS[0], lightness, a, b) ** 3;
|
|
83
|
+
const m = dot(OKLAB_TO_LMS[1], lightness, a, b) ** 3;
|
|
84
|
+
const s = dot(OKLAB_TO_LMS[2], lightness, a, b) ** 3;
|
|
42
85
|
return [
|
|
43
|
-
dot(
|
|
44
|
-
dot(
|
|
45
|
-
dot(
|
|
46
|
-
opacity,
|
|
86
|
+
srgbByteFromLinear(dot(LMS_TO_LINEAR_SRGB[0], l, m, s)),
|
|
87
|
+
srgbByteFromLinear(dot(LMS_TO_LINEAR_SRGB[1], l, m, s)),
|
|
88
|
+
srgbByteFromLinear(dot(LMS_TO_LINEAR_SRGB[2], l, m, s)),
|
|
47
89
|
];
|
|
48
90
|
}
|
|
49
91
|
function box(samples) {
|
package/dist/types.d.ts
CHANGED
|
@@ -92,7 +92,7 @@ export declare enum PixelationDither {
|
|
|
92
92
|
export declare enum PaletteColorMode {
|
|
93
93
|
/** User-specified fixed count (e.g. 16). */
|
|
94
94
|
Fixed = "fixed",
|
|
95
|
-
/** One colour per group of near-duplicate shades (
|
|
95
|
+
/** One colour per group of near-duplicate shades (OKLab distance) in the recovered image. */
|
|
96
96
|
Adaptive = "adaptive"
|
|
97
97
|
}
|
|
98
98
|
/** How a line drawing changes PixelOE's outline weight (1 = spread dark, 0 = spread bright). */
|
|
@@ -167,7 +167,7 @@ export interface PaletteSnapOptions {
|
|
|
167
167
|
colors?: number;
|
|
168
168
|
/** Estimate color count from the image instead of using a fixed count. */
|
|
169
169
|
adaptive?: boolean;
|
|
170
|
-
/** Snap every visible pixel to its nearest entry (
|
|
170
|
+
/** Snap every visible pixel to its nearest entry (OKLab) instead of clustering. */
|
|
171
171
|
palette?: readonly Rgba[];
|
|
172
172
|
/** Ordered / Floyd–Steinberg while snapping; None (nearest) when omitted. */
|
|
173
173
|
dither?: PixelationDither;
|
|
@@ -201,7 +201,7 @@ export interface PixelateOptions {
|
|
|
201
201
|
/** Nearest-neighbor upscale to the source dimensions after pixelation. */
|
|
202
202
|
preserveDimensions?: boolean;
|
|
203
203
|
/**
|
|
204
|
-
* Limit colors:
|
|
204
|
+
* Limit colors: OKLab k-means to a colour count that keeps rare colours, or snapping to a given
|
|
205
205
|
* palette. Alpha stays per pixel. Off when omitted.
|
|
206
206
|
*/
|
|
207
207
|
paletteSnap?: PaletteSnapOptions;
|
package/dist/types.js
CHANGED
|
@@ -56,7 +56,7 @@ export var PaletteColorMode;
|
|
|
56
56
|
(function (PaletteColorMode) {
|
|
57
57
|
/** User-specified fixed count (e.g. 16). */
|
|
58
58
|
PaletteColorMode["Fixed"] = "fixed";
|
|
59
|
-
/** One colour per group of near-duplicate shades (
|
|
59
|
+
/** One colour per group of near-duplicate shades (OKLab distance) in the recovered image. */
|
|
60
60
|
PaletteColorMode["Adaptive"] = "adaptive";
|
|
61
61
|
})(PaletteColorMode || (PaletteColorMode = {}));
|
|
62
62
|
/** How a line drawing changes PixelOE's outline weight (1 = spread dark, 0 = spread bright). */
|
package/package.json
CHANGED
|
@@ -1,21 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xprite/pixelate",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Recover the real pixel grid of AI-generated and upscaled pixel art, and turn images into pixel art.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ai-art",
|
|
7
|
+
"grid-detection",
|
|
8
|
+
"image-processing",
|
|
9
|
+
"pixel-art",
|
|
10
|
+
"pixelate",
|
|
11
|
+
"sprite"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/rhinoc/xprite-pixelate#readme",
|
|
5
14
|
"license": "MIT",
|
|
6
15
|
"author": "rhinoc",
|
|
7
16
|
"repository": {
|
|
8
17
|
"type": "git",
|
|
9
18
|
"url": "git+https://github.com/rhinoc/xprite-pixelate.git"
|
|
10
19
|
},
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"wasm",
|
|
23
|
+
"licenses",
|
|
24
|
+
"LICENSE",
|
|
25
|
+
"NOTICE.md",
|
|
26
|
+
"README.md",
|
|
27
|
+
"README.zh-CN.md"
|
|
19
28
|
],
|
|
20
29
|
"type": "module",
|
|
21
30
|
"sideEffects": false,
|
|
@@ -36,18 +45,6 @@
|
|
|
36
45
|
"./wasm/pixelfixer_detect_bg.wasm": "./wasm/pixelfixer_detect_bg.wasm",
|
|
37
46
|
"./wasm/ac-rl-detect-browser-worker.js": "./wasm/ac-rl-detect-browser-worker.js"
|
|
38
47
|
},
|
|
39
|
-
"files": [
|
|
40
|
-
"dist",
|
|
41
|
-
"wasm",
|
|
42
|
-
"licenses",
|
|
43
|
-
"LICENSE",
|
|
44
|
-
"NOTICE.md",
|
|
45
|
-
"README.md",
|
|
46
|
-
"README.zh-CN.md"
|
|
47
|
-
],
|
|
48
|
-
"engines": {
|
|
49
|
-
"node": ">=20"
|
|
50
|
-
},
|
|
51
48
|
"scripts": {
|
|
52
49
|
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
53
50
|
"typecheck": "tsc -p tsconfig.json",
|
|
@@ -60,5 +57,8 @@
|
|
|
60
57
|
"esbuild": "0.28.2",
|
|
61
58
|
"typescript": "^7.0.2",
|
|
62
59
|
"vitest": "^5.0.2"
|
|
60
|
+
},
|
|
61
|
+
"engines": {
|
|
62
|
+
"node": ">=20"
|
|
63
63
|
}
|
|
64
64
|
}
|