@xprite/pixelate 0.1.0 → 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 CHANGED
@@ -5,23 +5,34 @@
5
5
 
6
6
  English | [简体中文](https://github.com/rhinoc/xprite-pixelate/blob/main/README.zh-CN.md)
7
7
 
8
- | AI-generated, 887×887 | Recovered grid | Real pixel art, 48×49 |
9
- | :---: | :---: | :---: |
10
- | ![Input](docs/images/griffith-input.png) | ![Grid](docs/images/griffith-mesh.png) | ![Result](docs/images/griffith-result.png) |
8
+ Recover the real pixel grid of AI-generated pixel art and get back one clean pixel per cell. It
9
+ also turns ordinary images into pixel art. It runs in browsers and Node, has no runtime
10
+ dependencies, and is the importer of the [Xprite](https://xprite.cc) pixel editor.
11
11
 
12
- ## Summary
12
+ ## Compared with other tools
13
+
14
+ A real generated image (887×887, cells 15 to 21 px wide), recovered by each tool with its default
15
+ settings. Xprite puts each cut on a colour edge, so it does not cut through the eye, the boots or
16
+ the sword.
13
17
 
14
- Image generators draw "pixel art" with soft edges, noise, half-transparent outlines and cells of
15
- uneven width. This package finds the real pixel grid in such an image and gives back one clean
16
- pixel per cell. It also turns ordinary images into pixel art.
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
+ | ![Grid](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/griffith-mesh.png) | ![Xprite](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/griffith-xprite.png) | ![pixel-art-fixer](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/griffith-fixer.png) | ![perfectPixel](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/griffith-perfectpixel.png) | ![proper-pixel-art](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/griffith-proper.png) | ![Sprite Fusion](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/732237d34dfed3f9cc54d51b6c12ed5121c0978b/docs/images/griffith-spritefusion.png) |
17
21
 
18
- It runs in browsers and Node and has no runtime dependencies. One WebAssembly module ships inside
19
- the package. It is the importer of the [Xprite](https://xprite.cc) pixel editor.
22
+ On the AI suite of [our benchmark](#benchmark) (1746 cases), Xprite gets the exact size most
23
+ often:
24
+
25
+ | Method | Exact size | Within 1 cell | Pixel match | Fringe (alpha 1–254) | Median time |
26
+ | --- | --- | --- | --- | --- | --- |
27
+ | Xprite Auto | **83.6%** | **90.0%** | **99.7%** | 0.1% | 2.80 s |
28
+ | pixel-art-fixer | 49.7% | 80.9% | 98.3% | 0.0% | 0.18 s |
29
+ | perfectPixel | 53.3% | 68.0% | 96.7% | - | 0.03 s |
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% | — |
20
32
 
21
33
  ## Contents
22
34
 
23
35
  - [Examples](#examples)
24
- - [Compared with other tools](#compared-with-other-tools)
25
36
  - [Install](#install)
26
37
  - [Usage](#usage)
27
38
  - [How it works](#how-it-works)
@@ -30,52 +41,57 @@ the package. It is the importer of the [Xprite](https://xprite.cc) pixel editor.
30
41
 
31
42
  ## Examples
32
43
 
33
- These inputs come from the benchmark's AI suite: CC0 pixel art rendered the way generators fake
34
- it. Each result has exactly the size of the original art.
44
+ Each example is one case from a category of the benchmark's AI suite: CC0 pixel art rendered the
45
+ way generators fake it, with a known native size. The sizes are the visible art in cells; ✅ marks
46
+ the exact native size. Every tool runs with its default settings.
35
47
 
36
48
  ### Two sprites on one canvas
37
49
 
38
- - Goblins by LuizMelo ([Monsters Creatures Fantasy](https://luizmelo.itch.io/monsters-creatures-fantasy), CC0)
50
+ - [Goblins by LuizMelo](https://luizmelo.itch.io/monsters-creatures-fantasy), CC0. Category `gen_pair`. Native art: 78×36.
39
51
 
40
- | Input, 1800×845 | Recovered grid | Result, 103×49 |
41
- | :---: | :---: | :---: |
42
- | ![Input](docs/images/goblins-input.png) | ![Grid](docs/images/goblins-mesh.png) | ![Result](docs/images/goblins-result.png) |
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
+ | ![Input, 1800×845](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/pair-input.png) | ![Xprite](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/pair-xprite.png) | ![pixel-art-fixer](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/pair-fixer.png) | ![perfectPixel](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/pair-perfectpixel.png) | ![proper-pixel-art](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/pair-proper.png) | ![Sprite Fusion](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/732237d34dfed3f9cc54d51b6c12ed5121c0978b/docs/images/pair-spritefusion.png) |
43
55
 
44
- ### Painterly shading inside cells
56
+ ### Cells that drift and jitter
45
57
 
46
- - Naga mage from [Dungeon Crawl Stone Soup tiles](https://github.com/crawl/tiles) (CC0)
58
+ - [Boss dragon by ansimuz](https://opengameart.org/content/sideview-fantasy-patreon-collection), CC0. Category `gen_wobble`. Native art: 71×39.
47
59
 
48
- | Input, 411×425 | Recovered grid | Result, 50×50 |
49
- | :---: | :---: | :---: |
50
- | ![Input](docs/images/naga-input.png) | ![Grid](docs/images/naga-mesh.png) | ![Result](docs/images/naga-result.png) |
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
+ | ![Input, 1226×664](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/wobble-input.png) | ![Xprite](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/wobble-xprite.png) | ![pixel-art-fixer](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/wobble-fixer.png) | ![perfectPixel](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/wobble-perfectpixel.png) | ![proper-pixel-art](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/wobble-proper.png) | ![Sprite Fusion](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/732237d34dfed3f9cc54d51b6c12ed5121c0978b/docs/images/wobble-spritefusion.png) |
51
63
 
52
- ### Cells that drift and jitter
64
+ ### Small cells and WebP damage
53
65
 
54
- - Monster by Denzi ([Denzi's public domain art](https://opengameart.org/content/denzis-public-domain-art), CC0)
66
+ - [Monster by Denzi](https://opengameart.org/content/denzis-public-domain-art), CC0. Category `gen_small_pitch`. Native art: 32×48.
55
67
 
56
- | Input, 606×924 | Recovered grid | Result, 45×67 |
57
- | :---: | :---: | :---: |
58
- | ![Input](docs/images/wobble-input.png) | ![Grid](docs/images/wobble-mesh.png) | ![Result](docs/images/wobble-result.png) |
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
+ | ![Input, 218×339](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/small-input.png) | ![Xprite](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/small-xprite.png) | ![pixel-art-fixer](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/small-fixer.png) | ![perfectPixel](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/small-perfectpixel.png) | ![proper-pixel-art](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/small-proper.png) | ![Sprite Fusion](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/732237d34dfed3f9cc54d51b6c12ed5121c0978b/docs/images/small-spritefusion.png) |
59
71
 
60
- ### Small cells and WebP damage
72
+ ### A small sprite on a large clear canvas
61
73
 
62
- - Old man by ansimuz ([Gothicvania Town](https://opengameart.org/content/gothicvania-town), CC0)
74
+ - [Nekomimi girl by cawfeecrow](https://opengameart.org/content/nekomimi-girl-portrait), CC0. Category `gen_sparse`. Native art: 123×136.
63
75
 
64
- | Input, 250×380 | Recovered grid | Result, 39×57 |
65
- | :---: | :---: | :---: |
66
- | ![Input](docs/images/oldman-input.png) | ![Grid](docs/images/oldman-mesh.png) | ![Result](docs/images/oldman-result.png) |
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
+ | ![Input, 1797×1800](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/sparse-input.png) | ![Xprite](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/sparse-xprite.png) | ![pixel-art-fixer](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/sparse-fixer.png) | ![perfectPixel](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/sparse-perfectpixel.png) | ![proper-pixel-art](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/sparse-proper.png) | ![Sprite Fusion](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/732237d34dfed3f9cc54d51b6c12ed5121c0978b/docs/images/sparse-spritefusion.png) |
67
79
 
68
- ## Compared with other tools
80
+ ### An off-white background
81
+
82
+ - [Dragon by ansimuz](https://opengameart.org/content/sideview-fantasy-patreon-collection), CC0. Category `gen_white`. Native art: 162×106.
69
83
 
70
- The image at the top, recovered by each tool with its default settings:
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
+ | ![Input, 1800×1210](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/white-input.png) | ![Xprite](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/white-xprite.png) | ![pixel-art-fixer](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/white-fixer.png) | ![perfectPixel](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/white-perfectpixel.png) | ![proper-pixel-art](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/white-proper.png) | ![Sprite Fusion](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/732237d34dfed3f9cc54d51b6c12ed5121c0978b/docs/images/white-spritefusion.png) |
87
+
88
+ ### Painterly shading inside cells
71
89
 
72
- | Xprite Auto, 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 |
73
- | :---: | :---: | :---: | :---: |
74
- | ![Xprite](docs/images/griffith-result.png) | ![pixel-art-fixer](docs/images/griffith-fixer.png) | ![perfectPixel](docs/images/griffith-perfectpixel.png) | ![proper-pixel-art](docs/images/griffith-proper.png) |
90
+ - [Naga mage, Dungeon Crawl Stone Soup](https://github.com/crawl/tiles), CC0. Category `gen_painterly`. Native art: 31×32.
75
91
 
76
- The cells of this image are 15 to 21 px wide, so a grid of even cells cuts through drawn pixels
77
- somewhere. Xprite puts each cut on a colour edge. The [Benchmark](#benchmark) section has scores
78
- on 1746 cases.
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
+ | ![Input, 411×425](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/painterly-input.png) | ![Xprite](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/painterly-xprite.png) | ![pixel-art-fixer](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/painterly-fixer.png) | ![perfectPixel](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/painterly-perfectpixel.png) | ![proper-pixel-art](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/painterly-proper.png) | ![Sprite Fusion](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/732237d34dfed3f9cc54d51b6c12ed5121c0978b/docs/images/painterly-spritefusion.png) |
79
95
 
80
96
  ## Install
81
97
 
@@ -204,33 +220,51 @@ Each source file in [`src/`](src) describes its step.
204
220
 
205
221
  ## Benchmark
206
222
 
207
- [`bench/`](bench) compares this package with three other tools on CC0 native pixel art. The
223
+ [`bench/`](bench) compares this package with four other tools on mostly CC0 native pixel art (plus one CC-BY source). The
208
224
  benchmark damages the art in known ways and scores each result against the original. Every tool
209
225
  runs with its default settings.
210
226
 
211
- AI suite (1746 cases rendered the way generators fake pixel art):
227
+ AI suite (1746 cases in 12 categories, rendered the way generators fake pixel art):
212
228
 
213
- | Method | Exact size | Within 1 cell | Pixel match | ΔE | Half-transparent fringe | Median time |
229
+ | Method | Exact size | Within 1 cell | Pixel match | ΔE | Fringe (alpha 1–254) | Median time |
214
230
  | --- | --- | --- | --- | --- | --- | --- |
215
- | Xprite Auto | **83.6%** | **90.0%** | **99.8%** | 0.82 | 0.1% | 1.55 s |
231
+ | Xprite Auto | **83.6%** | **90.0%** | **99.7%** | 0.85 | 0.1% | 2.80 s |
216
232
  | pixel-art-fixer | 49.7% | 80.9% | 98.3% | 1.74 | 0.0% | 0.18 s |
217
233
  | perfectPixel | 53.3% | 68.0% | 96.7% | 2.42 | - | 0.03 s |
218
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% | — |
236
+
237
+ Exact size per category:
238
+
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% |
219
253
 
220
254
  [pixel-bench](https://github.com/Retro-Diffusion/pixel-bench) suite v1 (7651 cases of upscales and
221
255
  lossy pipelines):
222
256
 
223
257
  | Method | Exact size | Within 1 cell | Pixel match | ΔE | Median time |
224
258
  | --- | --- | --- | --- | --- | --- |
225
- | Xprite Auto | **87.1%** | **90.1%** | 94.3% | **2.79** | 0.24 s |
259
+ | Xprite Auto | **87.1%** | **90.1%** | 94.4% | **2.76** | 0.27 s |
226
260
  | pixel-art-fixer | 77.8% | 83.5% | **94.4%** | 2.83 | 0.04 s |
227
261
  | perfectPixel | 24.2% | 32.9% | 89.7% | 4.31 | 0.002 s |
228
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.
229
266
 
230
- Pixel match and ΔE are measured only on the cases with the exact size. Xprite is the slowest of
231
- the four tools. The damage is synthetic, so the scores show how the tools rank on these features,
232
- not how often they succeed on real images. [bench/README.md](bench/README.md) gives the method,
233
- 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.
234
268
 
235
269
  ## License
236
270
 
package/README.zh-CN.md CHANGED
@@ -5,20 +5,29 @@
5
5
 
6
6
  [English](https://github.com/rhinoc/xprite-pixelate/blob/main/README.md) | 简体中文
7
7
 
8
- | AI 生成,887×887 | 找到的网格 | 真正的像素画,48×49 |
9
- | :---: | :---: | :---: |
10
- | ![输入](docs/images/griffith-input.png) | ![网格](docs/images/griffith-mesh.png) | ![结果](docs/images/griffith-result.png) |
8
+ 找出 AI 生成像素画中真正的像素网格,每一格还原成一个干净的像素。它也能把普通图片转成像素画。它可以在浏览器和 Node 中运行,没有运行时依赖,是像素编辑器 [Xprite](https://xprite.cc) 的导入器。
11
9
 
12
- ## 简介
10
+ ## 与其他工具对比
11
+
12
+ 一张真实的 AI 生成图(887×887,格宽 15 到 21 像素不等),各工具用默认设置还原的结果。Xprite 把每一刀都切在颜色边缘上,所以不会切开眼睛、靴子和剑。
13
13
 
14
- 图像生成模型画出的“像素画”边缘发虚,有噪点,轮廓半透明,每一格的宽度也不一样。这个包找出图里真正的像素网格,每一格还原成一个干净的像素。它也能把普通图片转成像素画。
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
+ | ![网格](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/griffith-mesh.png) | ![Xprite](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/griffith-xprite.png) | ![pixel-art-fixer](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/griffith-fixer.png) | ![perfectPixel](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/griffith-perfectpixel.png) | ![proper-pixel-art](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/griffith-proper.png) | ![Sprite Fusion](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/732237d34dfed3f9cc54d51b6c12ed5121c0978b/docs/images/griffith-spritefusion.png) |
15
17
 
16
- 它可以在浏览器和 Node 中运行,没有运行时依赖,包内自带一个 WebAssembly 模块。它是像素编辑器 [Xprite](https://xprite.cc) 的导入器。
18
+ 在[我们的基准测试](#基准测试)的 AI 套件(1746 个用例)上,Xprite 得到正确尺寸的次数最多:
19
+
20
+ | 方法 | 尺寸完全正确 | 差 1 格以内 | 像素一致率 | 毛边(alpha 1–254) | 中位耗时 |
21
+ | --- | --- | --- | --- | --- | --- |
22
+ | Xprite Auto | **83.6%** | **90.0%** | **99.7%** | 0.1% | 2.80 s |
23
+ | pixel-art-fixer | 49.7% | 80.9% | 98.3% | 0.0% | 0.18 s |
24
+ | perfectPixel | 53.3% | 68.0% | 96.7% | - | 0.03 s |
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% | — |
17
27
 
18
28
  ## 目录
19
29
 
20
30
  - [示例](#示例)
21
- - [与其他工具对比](#与其他工具对比)
22
31
  - [安装](#安装)
23
32
  - [用法](#用法)
24
33
  - [原理](#原理)
@@ -27,49 +36,55 @@
27
36
 
28
37
  ## 示例
29
38
 
30
- 下面的输入来自基准测试的 AI 套件:把 CC0 像素画按生成模型的方式渲染成假像素画。每个结果的尺寸都和原画完全一致。
39
+ 每个示例取自基准测试 AI 套件的一个类别:把 CC0 像素画按生成模型的方式渲染,原画尺寸已知。尺寸是可见画面的格数,✅ 表示与原画尺寸完全一致。每个工具都使用默认设置。
31
40
 
32
41
  ### 一张画布上的两个角色
33
42
 
34
- - 哥布林,作者 LuizMelo([Monsters Creatures Fantasy](https://luizmelo.itch.io/monsters-creatures-fantasy),CC0)
43
+ - [哥布林,作者 LuizMelo](https://luizmelo.itch.io/monsters-creatures-fantasy),CC0。类别 `gen_pair`。原画:78×36。
35
44
 
36
- | 输入,1800×845 | 找到的网格 | 结果,103×49 |
37
- | :---: | :---: | :---: |
38
- | ![输入](docs/images/goblins-input.png) | ![网格](docs/images/goblins-mesh.png) | ![结果](docs/images/goblins-result.png) |
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
+ | ![输入,1800×845](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/pair-input.png) | ![Xprite](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/pair-xprite.png) | ![pixel-art-fixer](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/pair-fixer.png) | ![perfectPixel](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/pair-perfectpixel.png) | ![proper-pixel-art](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/pair-proper.png) | ![Sprite Fusion](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/732237d34dfed3f9cc54d51b6c12ed5121c0978b/docs/images/pair-spritefusion.png) |
39
48
 
40
- ### 格内有绘画式明暗
49
+ ### 格子漂移和抖动
41
50
 
42
- - 娜迦法师,来自 [Dungeon Crawl Stone Soup tiles](https://github.com/crawl/tiles)(CC0)
51
+ - [巨龙,作者 ansimuz](https://opengameart.org/content/sideview-fantasy-patreon-collection),CC0。类别 `gen_wobble`。原画:71×39。
43
52
 
44
- | 输入,411×425 | 找到的网格 | 结果,50×50 |
45
- | :---: | :---: | :---: |
46
- | ![输入](docs/images/naga-input.png) | ![网格](docs/images/naga-mesh.png) | ![结果](docs/images/naga-result.png) |
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
+ | ![输入,1226×664](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/wobble-input.png) | ![Xprite](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/wobble-xprite.png) | ![pixel-art-fixer](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/wobble-fixer.png) | ![perfectPixel](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/wobble-perfectpixel.png) | ![proper-pixel-art](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/wobble-proper.png) | ![Sprite Fusion](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/732237d34dfed3f9cc54d51b6c12ed5121c0978b/docs/images/wobble-spritefusion.png) |
47
56
 
48
- ### 格子漂移和抖动
57
+ ### 小格子和 WebP 压缩损伤
49
58
 
50
- - 怪物,作者 Denzi([Denzi's public domain art](https://opengameart.org/content/denzis-public-domain-art),CC0)
59
+ - [怪物,作者 Denzi](https://opengameart.org/content/denzis-public-domain-art),CC0。类别 `gen_small_pitch`。原画:32×48。
51
60
 
52
- | 输入,606×924 | 找到的网格 | 结果,45×67 |
53
- | :---: | :---: | :---: |
54
- | ![输入](docs/images/wobble-input.png) | ![网格](docs/images/wobble-mesh.png) | ![结果](docs/images/wobble-result.png) |
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
+ | ![输入,218×339](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/small-input.png) | ![Xprite](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/small-xprite.png) | ![pixel-art-fixer](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/small-fixer.png) | ![perfectPixel](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/small-perfectpixel.png) | ![proper-pixel-art](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/small-proper.png) | ![Sprite Fusion](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/732237d34dfed3f9cc54d51b6c12ed5121c0978b/docs/images/small-spritefusion.png) |
55
64
 
56
- ### 小格子和 WebP 压缩损伤
65
+ ### 大块透明画布上的小角色
57
66
 
58
- - 老人,作者 ansimuz([Gothicvania Town](https://opengameart.org/content/gothicvania-town),CC0)
67
+ - [猫耳少女,作者 cawfeecrow](https://opengameart.org/content/nekomimi-girl-portrait),CC0。类别 `gen_sparse`。原画:123×136。
59
68
 
60
- | 输入,250×380 | 找到的网格 | 结果,39×57 |
61
- | :---: | :---: | :---: |
62
- | ![输入](docs/images/oldman-input.png) | ![网格](docs/images/oldman-mesh.png) | ![结果](docs/images/oldman-result.png) |
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
+ | ![输入,1797×1800](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/sparse-input.png) | ![Xprite](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/sparse-xprite.png) | ![pixel-art-fixer](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/sparse-fixer.png) | ![perfectPixel](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/sparse-perfectpixel.png) | ![proper-pixel-art](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/sparse-proper.png) | ![Sprite Fusion](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/732237d34dfed3f9cc54d51b6c12ed5121c0978b/docs/images/sparse-spritefusion.png) |
63
72
 
64
- ## 与其他工具对比
73
+ ### 近白色背景
74
+
75
+ - [飞龙,作者 ansimuz](https://opengameart.org/content/sideview-fantasy-patreon-collection),CC0。类别 `gen_white`。原画:162×106。
65
76
 
66
- 顶部那张图,各工具用默认设置还原的结果:
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
+ | ![输入,1800×1210](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/white-input.png) | ![Xprite](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/white-xprite.png) | ![pixel-art-fixer](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/white-fixer.png) | ![perfectPixel](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/white-perfectpixel.png) | ![proper-pixel-art](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/white-proper.png) | ![Sprite Fusion](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/732237d34dfed3f9cc54d51b6c12ed5121c0978b/docs/images/white-spritefusion.png) |
80
+
81
+ ### 格内有绘画式明暗
67
82
 
68
- | Xprite Auto,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 |
69
- | :---: | :---: | :---: | :---: |
70
- | ![Xprite](docs/images/griffith-result.png) | ![pixel-art-fixer](docs/images/griffith-fixer.png) | ![perfectPixel](docs/images/griffith-perfectpixel.png) | ![proper-pixel-art](docs/images/griffith-proper.png) |
83
+ - [娜迦法师,Dungeon Crawl Stone Soup](https://github.com/crawl/tiles),CC0。类别 `gen_painterly`。原画:31×32。
71
84
 
72
- 这张图的格宽在 15 到 21 像素之间,所以等宽网格总会在某处切进画好的像素。Xprite 把每一刀都切在颜色边缘上。1746 个用例的得分见[基准测试](#基准测试)。
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
+ | ![输入,411×425](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/painterly-input.png) | ![Xprite](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/painterly-xprite.png) | ![pixel-art-fixer](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/painterly-fixer.png) | ![perfectPixel](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/painterly-perfectpixel.png) | ![proper-pixel-art](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/main/docs/images/painterly-proper.png) | ![Sprite Fusion](https://raw.githubusercontent.com/rhinoc/xprite-pixelate/732237d34dfed3f9cc54d51b6c12ed5121c0978b/docs/images/painterly-spritefusion.png) |
73
88
 
74
89
  ## 安装
75
90
 
@@ -182,27 +197,48 @@ const grid = await detectWithAlgorithmAsync(image);
182
197
 
183
198
  ## 基准测试
184
199
 
185
- [`bench/`](bench) 在 CC0 原生像素画上比较本包和另外三个工具。基准测试以已知方式损伤原画,再把每个结果和原画对比打分。每个工具都使用默认设置。
200
+ [`bench/`](bench) 在以 CC0 为主(另有一个 CC-BY 来源)的原生像素画上比较本包和另外四个工具。基准测试以已知方式损伤原画,再把每个结果和原画对比打分。每个工具都使用默认设置。
186
201
 
187
- AI 套件(1746 个用例,按生成模型伪造像素画的方式渲染):
202
+ AI 套件(12 个类别共 1746 个用例,按生成模型伪造像素画的方式渲染):
188
203
 
189
- | 方法 | 尺寸完全正确 | 差 1 格以内 | 像素一致率 | ΔE | 半透明毛边 | 中位耗时 |
204
+ | 方法 | 尺寸完全正确 | 差 1 格以内 | 像素一致率 | ΔE | 毛边(alpha 1–254) | 中位耗时 |
190
205
  | --- | --- | --- | --- | --- | --- | --- |
191
- | Xprite Auto | **83.6%** | **90.0%** | **99.8%** | 0.82 | 0.1% | 1.55 s |
206
+ | Xprite Auto | **83.6%** | **90.0%** | **99.7%** | 0.85 | 0.1% | 2.80 s |
192
207
  | pixel-art-fixer | 49.7% | 80.9% | 98.3% | 1.74 | 0.0% | 0.18 s |
193
208
  | perfectPixel | 53.3% | 68.0% | 96.7% | 2.42 | - | 0.03 s |
194
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% | — |
211
+
212
+ 各类别的尺寸完全正确率:
213
+
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% |
195
228
 
196
229
  [pixel-bench](https://github.com/Retro-Diffusion/pixel-bench) v1 套件(7651 个放大和有损处理用例):
197
230
 
198
231
  | 方法 | 尺寸完全正确 | 差 1 格以内 | 像素一致率 | ΔE | 中位耗时 |
199
232
  | --- | --- | --- | --- | --- | --- |
200
- | Xprite Auto | **87.1%** | **90.1%** | 94.3% | **2.79** | 0.24 s |
233
+ | Xprite Auto | **87.1%** | **90.1%** | 94.4% | **2.76** | 0.27 s |
201
234
  | pixel-art-fixer | 77.8% | 83.5% | **94.4%** | 2.83 | 0.04 s |
202
235
  | perfectPixel | 24.2% | 32.9% | 89.7% | 4.31 | 0.002 s |
203
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 的像素,包括接近不透明的像素。
204
240
 
205
- 像素一致率和 ΔE 只在尺寸完全正确的用例上统计。四个工具中 Xprite 最慢。损伤是合成的,所以得分反映各工具在这些特征上的排名,不代表在真实图片上的成功率。方法、类别和运行步骤见 [bench/README.md](bench/README.md)。
241
+ 各方法使用默认颜色处理方式,调色板可能不同;表中是端到端结果。计时方式也有差异,包含进程启动和 PNG 读写的计时未列入表格。所有结果使用相同的用例和评分代码,部分结果来自先前的运行。损伤是合成的,不能代表真实图片的成功率。各方法的版本、计时口径和复现方法见 [bench/README.md](bench/README.md)。
206
242
 
207
243
  ## 许可证
208
244
 
@@ -1,10 +1,10 @@
1
- import { rgbToLab, labToRgb } from "./color-lab.js";
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 `MERGE_DELTA` in CIE Lab) are ever merged without a budget, and
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
- /** CIE76 distance below which two colours are the same shade for practical purposes. */
19
- const MERGE_DELTA = 3;
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
- lab: rgbToLab(data[o], data[o + 1], data[o + 2]),
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 MERGE_DELTA share a group. */
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
- let best = -1, bestDistance = MERGE_DELTA;
69
+ const oklab = shades[i].oklab;
70
+ let best = -1, bestDistance2 = threshold2;
69
71
  for (let g = 0; g < groups.length; g++) {
70
- const d = distance(shades[i].lab, shades[groups[g][0]].lab);
71
- if (d <= bestDistance) {
72
- bestDistance = d;
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].lab[c] * shades[m].count;
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
- lab: [sum[0] / count, sum[1] / count, sum[2] / count],
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].lab];
107
- const nearest = points.map((p) => distance(p.lab, seeds[0]));
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].lab);
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].lab, points[pick].lab));
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 < points.length; 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 d = distance(points[i].lab, centers[c]);
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 < points.length; i++) {
148
- const weight = points[i].count ** PULL_WEIGHT;
182
+ for (let i = 0; i < n; i++) {
183
+ const weight = pointWeight[i];
149
184
  const s = sums[assign[i]];
150
- for (let c = 0; c < 3; c++)
151
- s[c] += points[i].lab[c] * weight;
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
- centers[c] = [sums[c][0] / sums[c][3], sums[c][1] / sums[c][3], sums[c][2] / sums[c][3]];
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 paletteLabs(palette) {
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
- labs: visible.map((c) => rgbToLab(c[0], c[1], c[2])),
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(lab, labs, rgb) {
209
+ function nearestPaletteRgb(oklab, oklabs, rgb) {
170
210
  let best = 0, bestDistance = Infinity;
171
- for (let i = 0; i < labs.length; i++) {
172
- const d = distance(lab, labs[i]);
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 Lab nearest, this still dithers when every pixel is already a palette colour.
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 Lab-nearest space.
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 { labs, rgb } = paletteLabs(palette);
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(rgbToLab(sample[0], sample[1], sample[2]), labs, rgb);
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 paletteLabs(options.palette).rgb;
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]) ?? { lab: [0, 0, 0], count: 0 };
336
+ const entry = sums.get(assignment[i]) ?? { oklab: [0, 0, 0], count: 0 };
297
337
  for (let c = 0; c < 3; c++)
298
- entry.lab[c] += point.lab[c] * point.count;
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 { lab, count } of sums.values()) {
304
- const [r, g, b] = labToRgb(lab[0] / count, lab[1] / count, lab[2] / count);
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 { labs, rgb } = paletteLabs(options.palette);
315
- target = shades.map((shade) => nearestPaletteRgb(shade.lab, labs, rgb));
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]) ?? { lab: [0, 0, 0], count: 0 };
368
+ const entry = sums.get(assignment[i]) ?? { oklab: [0, 0, 0], count: 0 };
329
369
  for (let c = 0; c < 3; c++)
330
- entry.lab[c] += point.lab[c] * point.count;
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, { lab, count }] of sums)
336
- colorOf.set(id, labToRgb(lab[0] / count, lab[1] / count, lab[2] / count));
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 Lab nearest.
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.
@@ -1,6 +1,11 @@
1
1
  import type { Rgba } from "./base.ts";
2
2
  import type { PaletteEntry } from "./types.ts";
3
- /** Oklab plus opacity; invisible RGB never participates in matching. */
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[];
@@ -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
- /** Oklab plus opacity; invisible RGB never participates in matching. */
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 l = Math.cbrt(dot(LINEAR_SRGB_TO_LMS[0], r, g, b));
40
- const m = Math.cbrt(dot(LINEAR_SRGB_TO_LMS[1], r, g, b));
41
- const s = Math.cbrt(dot(LINEAR_SRGB_TO_LMS[2], r, g, b));
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(LMS_TO_OKLAB[0], l, m, s) * opacity,
44
- dot(LMS_TO_OKLAB[1], l, m, s) * opacity,
45
- dot(LMS_TO_OKLAB[2], l, m, s) * opacity,
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 (Lab ΔE) in the recovered image. */
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 (CIE Lab) instead of clustering. */
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: Lab k-means to a colour count that keeps rare colours, or snapping to a given
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 (Lab ΔE) in the recovered image. */
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.0",
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
- "homepage": "https://github.com/rhinoc/xprite-pixelate#readme",
12
- "keywords": [
13
- "pixel-art",
14
- "pixelate",
15
- "grid-detection",
16
- "ai-art",
17
- "sprite",
18
- "image-processing"
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
  }