@xprite/pixelate 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -0
- package/README.zh-CN.md +14 -0
- package/dist/base.d.ts +4 -0
- package/dist/base.js +33 -0
- package/dist/float-image.d.ts +25 -0
- package/dist/float-image.js +184 -0
- package/dist/grid-sample.js +3 -78
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/kuwahara.d.ts +9 -0
- package/dist/kuwahara.js +287 -0
- package/dist/line-guided/pixeloe-entry.d.ts +2 -2
- package/dist/line-guided/pixeloe-entry.js +6 -35
- package/dist/orphan-pixels.d.ts +18 -0
- package/dist/orphan-pixels.js +207 -0
- package/dist/palette-snap.d.ts +1 -1
- package/dist/palette-snap.js +127 -58
- package/dist/pixel-alpha.d.ts +10 -0
- package/dist/pixel-alpha.js +108 -0
- package/dist/pixelate.d.ts +4 -7
- package/dist/pixelate.js +146 -101
- package/dist/quantization.d.ts +4 -0
- package/dist/quantization.js +23 -16
- package/dist/resize-crisp.d.ts +11 -0
- package/dist/resize-crisp.js +213 -0
- package/dist/resize-lanczos.d.ts +7 -0
- package/dist/resize-lanczos.js +205 -0
- package/dist/sampling.d.ts +2 -1
- package/dist/sampling.js +1 -1
- package/dist/settings.d.ts +6 -3
- package/dist/settings.js +26 -12
- package/dist/types.d.ts +15 -3
- package/dist/types.js +2 -2
- package/package.json +1 -1
- package/dist/contours.d.ts +0 -4
- package/dist/contours.js +0 -66
- package/dist/most-frequent.d.ts +0 -3
- package/dist/most-frequent.js +0 -56
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { assertDimension, assertPixelBuffer } from "./base.js";
|
|
2
|
+
import { fromFloatRgba, gaussianBlur, toFloatRgba } from "./float-image.js";
|
|
3
|
+
const LANCZOS_LOBES = 3;
|
|
4
|
+
const UNSHARP_SIGMA = 0.75;
|
|
5
|
+
const UNSHARP_AMOUNT = 0.24;
|
|
6
|
+
function lanczosKernel(x) {
|
|
7
|
+
if (x === 0)
|
|
8
|
+
return 1;
|
|
9
|
+
if (Math.abs(x) >= LANCZOS_LOBES)
|
|
10
|
+
return 0;
|
|
11
|
+
const px = Math.PI * x;
|
|
12
|
+
return (Math.sin(px) / px) * (Math.sin(px / LANCZOS_LOBES) / (px / LANCZOS_LOBES));
|
|
13
|
+
}
|
|
14
|
+
function unsharpAntiRinging(img) {
|
|
15
|
+
const { r, g, b, a: srcA, w, h, opaque } = img;
|
|
16
|
+
const br = gaussianBlur(r, w, h, UNSHARP_SIGMA);
|
|
17
|
+
const bg = gaussianBlur(g, w, h, UNSHARP_SIGMA);
|
|
18
|
+
const bb = gaussianBlur(b, w, h, UNSHARP_SIGMA);
|
|
19
|
+
const n = w * h;
|
|
20
|
+
const oR = new Float32Array(n);
|
|
21
|
+
const oG = new Float32Array(n);
|
|
22
|
+
const oB = new Float32Array(n);
|
|
23
|
+
for (let y = 0; y < h; y++) {
|
|
24
|
+
const y0 = Math.max(0, y - 1);
|
|
25
|
+
const y1 = Math.min(h - 1, y + 1);
|
|
26
|
+
for (let x = 0; x < w; x++) {
|
|
27
|
+
const i = y * w + x;
|
|
28
|
+
if (!opaque && srcA[i] <= 1e-4)
|
|
29
|
+
continue;
|
|
30
|
+
const x0 = Math.max(0, x - 1);
|
|
31
|
+
const x1 = Math.min(w - 1, x + 1);
|
|
32
|
+
let minR = 1, maxR = 0, minG = 1, maxG = 0, minB = 1, maxB = 0;
|
|
33
|
+
for (let yy = y0; yy <= y1; yy++) {
|
|
34
|
+
const row = yy * w;
|
|
35
|
+
for (let xx = x0; xx <= x1; xx++) {
|
|
36
|
+
const p = row + xx;
|
|
37
|
+
if (!opaque && srcA[p] <= 1e-4)
|
|
38
|
+
continue;
|
|
39
|
+
if (r[p] < minR)
|
|
40
|
+
minR = r[p];
|
|
41
|
+
if (r[p] > maxR)
|
|
42
|
+
maxR = r[p];
|
|
43
|
+
if (g[p] < minG)
|
|
44
|
+
minG = g[p];
|
|
45
|
+
if (g[p] > maxG)
|
|
46
|
+
maxG = g[p];
|
|
47
|
+
if (b[p] < minB)
|
|
48
|
+
minB = b[p];
|
|
49
|
+
if (b[p] > maxB)
|
|
50
|
+
maxB = b[p];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
oR[i] = Math.max(minR, Math.min(maxR, r[i] + UNSHARP_AMOUNT * (r[i] - br[i])));
|
|
54
|
+
oG[i] = Math.max(minG, Math.min(maxG, g[i] + UNSHARP_AMOUNT * (g[i] - bg[i])));
|
|
55
|
+
oB[i] = Math.max(minB, Math.min(maxB, b[i] + UNSHARP_AMOUNT * (b[i] - bb[i])));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return { r: oR, g: oG, b: oB, a: srcA, w, h, opaque };
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Alpha-aware separable Lanczos-3 resampler with anti-ringing clamping:
|
|
62
|
+
* each output pixel is clamped to its local source footprint's min/max RGB so negative filter
|
|
63
|
+
* lobes never introduce overshoot halos along hard outlines.
|
|
64
|
+
*/
|
|
65
|
+
export function resizeLanczos(image, width, height, sharpen = false) {
|
|
66
|
+
assertPixelBuffer(image);
|
|
67
|
+
assertDimension(width, "width");
|
|
68
|
+
assertDimension(height, "height");
|
|
69
|
+
const { r, g, b, a, w, h, opaque } = toFloatRgba(image);
|
|
70
|
+
const tmpSize = width * h;
|
|
71
|
+
const tmpR = new Float32Array(tmpSize);
|
|
72
|
+
const tmpG = new Float32Array(tmpSize);
|
|
73
|
+
const tmpB = new Float32Array(tmpSize);
|
|
74
|
+
const tmpA = new Float32Array(tmpSize);
|
|
75
|
+
const tmpMinR = new Float32Array(tmpSize);
|
|
76
|
+
const tmpMaxR = new Float32Array(tmpSize);
|
|
77
|
+
const tmpMinG = new Float32Array(tmpSize);
|
|
78
|
+
const tmpMaxG = new Float32Array(tmpSize);
|
|
79
|
+
const tmpMinB = new Float32Array(tmpSize);
|
|
80
|
+
const tmpMaxB = new Float32Array(tmpSize);
|
|
81
|
+
const scaleX = w / width;
|
|
82
|
+
const supportX = Math.max(1, scaleX);
|
|
83
|
+
const radX = LANCZOS_LOBES * supportX;
|
|
84
|
+
for (let x = 0; x < width; x++) {
|
|
85
|
+
const cx = (x + 0.5) * scaleX - 0.5;
|
|
86
|
+
const x0 = Math.max(0, Math.floor(cx - radX));
|
|
87
|
+
const x1 = Math.min(w - 1, Math.ceil(cx + radX));
|
|
88
|
+
const bx0 = Math.max(0, Math.floor(cx - supportX));
|
|
89
|
+
const bx1 = Math.min(w - 1, Math.ceil(cx + supportX));
|
|
90
|
+
const ws = new Float32Array(x1 - x0 + 1);
|
|
91
|
+
let wsum = 0;
|
|
92
|
+
for (let sx = x0; sx <= x1; sx++) {
|
|
93
|
+
const wk = lanczosKernel((sx - cx) / supportX);
|
|
94
|
+
ws[sx - x0] = wk;
|
|
95
|
+
wsum += wk;
|
|
96
|
+
}
|
|
97
|
+
const invW = wsum !== 0 ? 1 / wsum : 1;
|
|
98
|
+
for (let y = 0; y < h; y++) {
|
|
99
|
+
let ar = 0, ag = 0, ab = 0, aa = 0;
|
|
100
|
+
let mnR = 1, mxR = 0, mnG = 1, mxG = 0, mnB = 1, mxB = 0;
|
|
101
|
+
let hasVisible = false;
|
|
102
|
+
const row = y * w;
|
|
103
|
+
for (let sx = x0; sx <= x1; sx++) {
|
|
104
|
+
const wk = ws[sx - x0] * invW;
|
|
105
|
+
const p = row + sx;
|
|
106
|
+
const alpha = opaque ? 1 : a[p];
|
|
107
|
+
ar += r[p] * alpha * wk;
|
|
108
|
+
ag += g[p] * alpha * wk;
|
|
109
|
+
ab += b[p] * alpha * wk;
|
|
110
|
+
aa += alpha * wk;
|
|
111
|
+
if (sx >= bx0 && sx <= bx1 && alpha > 1e-4) {
|
|
112
|
+
hasVisible = true;
|
|
113
|
+
if (r[p] < mnR)
|
|
114
|
+
mnR = r[p];
|
|
115
|
+
if (r[p] > mxR)
|
|
116
|
+
mxR = r[p];
|
|
117
|
+
if (g[p] < mnG)
|
|
118
|
+
mnG = g[p];
|
|
119
|
+
if (g[p] > mxG)
|
|
120
|
+
mxG = g[p];
|
|
121
|
+
if (b[p] < mnB)
|
|
122
|
+
mnB = b[p];
|
|
123
|
+
if (b[p] > mxB)
|
|
124
|
+
mxB = b[p];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const o = y * width + x;
|
|
128
|
+
tmpR[o] = ar;
|
|
129
|
+
tmpG[o] = ag;
|
|
130
|
+
tmpB[o] = ab;
|
|
131
|
+
tmpA[o] = aa;
|
|
132
|
+
tmpMinR[o] = hasVisible ? mnR : 0;
|
|
133
|
+
tmpMaxR[o] = hasVisible ? mxR : 1;
|
|
134
|
+
tmpMinG[o] = hasVisible ? mnG : 0;
|
|
135
|
+
tmpMaxG[o] = hasVisible ? mxG : 1;
|
|
136
|
+
tmpMinB[o] = hasVisible ? mnB : 0;
|
|
137
|
+
tmpMaxB[o] = hasVisible ? mxB : 1;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const outSize = width * height;
|
|
141
|
+
const oR = new Float32Array(outSize);
|
|
142
|
+
const oG = new Float32Array(outSize);
|
|
143
|
+
const oB = new Float32Array(outSize);
|
|
144
|
+
const oA = new Float32Array(outSize);
|
|
145
|
+
const scaleY = h / height;
|
|
146
|
+
const supportY = Math.max(1, scaleY);
|
|
147
|
+
const radY = LANCZOS_LOBES * supportY;
|
|
148
|
+
for (let y = 0; y < height; y++) {
|
|
149
|
+
const cy = (y + 0.5) * scaleY - 0.5;
|
|
150
|
+
const y0 = Math.max(0, Math.floor(cy - radY));
|
|
151
|
+
const y1 = Math.min(h - 1, Math.ceil(cy + radY));
|
|
152
|
+
const by0 = Math.max(0, Math.floor(cy - supportY));
|
|
153
|
+
const by1 = Math.min(h - 1, Math.ceil(cy + supportY));
|
|
154
|
+
const ws = new Float32Array(y1 - y0 + 1);
|
|
155
|
+
let wsum = 0;
|
|
156
|
+
for (let sy = y0; sy <= y1; sy++) {
|
|
157
|
+
const wk = lanczosKernel((sy - cy) / supportY);
|
|
158
|
+
ws[sy - y0] = wk;
|
|
159
|
+
wsum += wk;
|
|
160
|
+
}
|
|
161
|
+
const invW = wsum !== 0 ? 1 / wsum : 1;
|
|
162
|
+
for (let x = 0; x < width; x++) {
|
|
163
|
+
let ar = 0, ag = 0, ab = 0, aa = 0;
|
|
164
|
+
let mnR = 1, mxR = 0, mnG = 1, mxG = 0, mnB = 1, mxB = 0;
|
|
165
|
+
let hasBound = false;
|
|
166
|
+
for (let sy = y0; sy <= y1; sy++) {
|
|
167
|
+
const wk = ws[sy - y0] * invW;
|
|
168
|
+
const o = sy * width + x;
|
|
169
|
+
ar += tmpR[o] * wk;
|
|
170
|
+
ag += tmpG[o] * wk;
|
|
171
|
+
ab += tmpB[o] * wk;
|
|
172
|
+
aa += tmpA[o] * wk;
|
|
173
|
+
if (sy >= by0 && sy <= by1 && (opaque || tmpA[o] > 1e-4)) {
|
|
174
|
+
hasBound = true;
|
|
175
|
+
if (tmpMinR[o] < mnR)
|
|
176
|
+
mnR = tmpMinR[o];
|
|
177
|
+
if (tmpMaxR[o] > mxR)
|
|
178
|
+
mxR = tmpMaxR[o];
|
|
179
|
+
if (tmpMinG[o] < mnG)
|
|
180
|
+
mnG = tmpMinG[o];
|
|
181
|
+
if (tmpMaxG[o] > mxG)
|
|
182
|
+
mxG = tmpMaxG[o];
|
|
183
|
+
if (tmpMinB[o] < mnB)
|
|
184
|
+
mnB = tmpMinB[o];
|
|
185
|
+
if (tmpMaxB[o] > mxB)
|
|
186
|
+
mxB = tmpMaxB[o];
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const outIdx = y * width + x;
|
|
190
|
+
const clampedA = opaque ? 1 : Math.max(0, Math.min(1, aa));
|
|
191
|
+
oA[outIdx] = clampedA;
|
|
192
|
+
if (clampedA > 1e-4 && (opaque || aa > 1e-4)) {
|
|
193
|
+
const invA = opaque ? 1 : 1 / aa;
|
|
194
|
+
const vr = Math.max(0, Math.min(1, ar * invA));
|
|
195
|
+
const vg = Math.max(0, Math.min(1, ag * invA));
|
|
196
|
+
const vb = Math.max(0, Math.min(1, ab * invA));
|
|
197
|
+
oR[outIdx] = hasBound ? Math.max(mnR, Math.min(mxR, vr)) : vr;
|
|
198
|
+
oG[outIdx] = hasBound ? Math.max(mnG, Math.min(mxG, vg)) : vg;
|
|
199
|
+
oB[outIdx] = hasBound ? Math.max(mnB, Math.min(mxB, vb)) : vb;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const resampled = { r: oR, g: oG, b: oB, a: oA, w: width, h: height, opaque };
|
|
204
|
+
return fromFloatRgba(sharpen ? unsharpAntiRinging(resampled) : resampled);
|
|
205
|
+
}
|
package/dist/sampling.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { PixelBuffer } from "./base.ts";
|
|
2
2
|
import { PixelationMethod } from "./types.ts";
|
|
3
|
+
export type SampleMode = PixelationMethod.Dominant | PixelationMethod.Structure | "two-stage";
|
|
3
4
|
/** RGB is taken from the winning region. Conversion keeps area alpha; recovery keeps the chosen region’s original alpha. */
|
|
4
|
-
export declare function samplePixelation(image: PixelBuffer, width: number, height: number, method:
|
|
5
|
+
export declare function samplePixelation(image: PixelBuffer, width: number, height: number, method: SampleMode, cuts?: {
|
|
5
6
|
x: readonly number[];
|
|
6
7
|
y: readonly number[];
|
|
7
8
|
}): PixelBuffer;
|
package/dist/sampling.js
CHANGED
|
@@ -248,7 +248,7 @@ export function samplePixelation(image, width, height, method, cuts) {
|
|
|
248
248
|
const axes = recovery ? CHANNELS : COLOR_CHANNELS;
|
|
249
249
|
const points = method === PixelationMethod.Dominant ? coordinateLookup(image, axes) : undefined;
|
|
250
250
|
const useStructure = method === PixelationMethod.Structure;
|
|
251
|
-
const useTwoStage = method ===
|
|
251
|
+
const useTwoStage = method === "two-stage";
|
|
252
252
|
const palette = useStructure
|
|
253
253
|
? extractPixelationPalette(image, {
|
|
254
254
|
maxColors: STRUCTURE_COLORS,
|
package/dist/settings.d.ts
CHANGED
|
@@ -3,14 +3,17 @@ import { PixelationMethod, type PixelGridAnalysis, type PixelationSettings, type
|
|
|
3
3
|
export declare const MAX_PIXELATION_SIZE = 512;
|
|
4
4
|
export declare const PIXELATION_SIZES: readonly [16, 32, 64, 128, 256, 512];
|
|
5
5
|
export declare const PIXELATION_METHOD_OPTIONS: readonly [{
|
|
6
|
+
readonly value: PixelationMethod.Crisp;
|
|
7
|
+
readonly label: "Crisp pixel";
|
|
8
|
+
}, {
|
|
9
|
+
readonly value: PixelationMethod.Lanczos;
|
|
10
|
+
readonly label: "Lanczos";
|
|
11
|
+
}, {
|
|
6
12
|
readonly value: PixelationMethod.Structure;
|
|
7
13
|
readonly label: "Preserve edges";
|
|
8
14
|
}, {
|
|
9
15
|
readonly value: PixelationMethod.Dominant;
|
|
10
16
|
readonly label: "Dominant colors";
|
|
11
|
-
}, {
|
|
12
|
-
readonly value: PixelationMethod.MostFrequent;
|
|
13
|
-
readonly label: "Most frequent color";
|
|
14
17
|
}, {
|
|
15
18
|
readonly value: PixelationMethod.Box;
|
|
16
19
|
readonly label: "Box average";
|
package/dist/settings.js
CHANGED
|
@@ -1,28 +1,33 @@
|
|
|
1
1
|
import { manualPixelGrid, MAX_GRID_CELL_SIZE } from "./grid.js";
|
|
2
2
|
import { DEFAULT_LINE_GUIDE, DEFAULT_OUTLINE_PREPASS, readOutlinePrepass, } from "./outline-prepass.js";
|
|
3
|
-
import { CellColorPicker, LineGuideMode, OutlinePrepass, PaletteColorMode, PixelationDither, PixelationMethod, PixelationMode, } from "./types.js";
|
|
3
|
+
import { CellColorPicker, LineGuideMode, OutlinePrepass, PaletteColorMode, PixelationAlgorithm, PixelationDither, PixelationMethod, PixelationMode, } from "./types.js";
|
|
4
4
|
export const MAX_PIXELATION_SIZE = 512;
|
|
5
5
|
export const PIXELATION_SIZES = [16, 32, 64, 128, 256, MAX_PIXELATION_SIZE];
|
|
6
6
|
export const PIXELATION_METHOD_OPTIONS = [
|
|
7
|
+
{ value: PixelationMethod.Crisp, label: "Crisp pixel" },
|
|
8
|
+
{ value: PixelationMethod.Lanczos, label: "Lanczos" },
|
|
7
9
|
{ value: PixelationMethod.Structure, label: "Preserve edges" },
|
|
8
10
|
{ value: PixelationMethod.Dominant, label: "Dominant colors" },
|
|
9
|
-
{ value: PixelationMethod.MostFrequent, label: "Most frequent color" },
|
|
10
11
|
{ value: PixelationMethod.Box, label: "Box average" },
|
|
11
12
|
{ value: PixelationMethod.Nearest, label: "Nearest-neighbor" },
|
|
12
13
|
];
|
|
13
14
|
const DEFAULT_PIXELATION_SIZE = 64;
|
|
14
15
|
const DEFAULT_COLORS = 16;
|
|
16
|
+
const DEFAULT_CONVERT_COLORS = 18;
|
|
17
|
+
const DEFAULT_CRISP_DETAIL = 50;
|
|
15
18
|
const MAX_COLORS = 256;
|
|
16
19
|
export function defaultPixelationSettings(image, grid) {
|
|
17
20
|
const scale = Math.min(1, DEFAULT_PIXELATION_SIZE / Math.max(image.width, image.height));
|
|
18
21
|
const mode = grid?.reliable ? PixelationMode.RecoverGrid : PixelationMode.Convert;
|
|
19
|
-
|
|
20
|
-
//
|
|
22
|
+
const converting = mode === PixelationMode.Convert || grid?.algorithm === PixelationAlgorithm.CustomSize;
|
|
23
|
+
// Convert opens on C5 (detail-protected Kuwahara + Crisp 2-means + saliency 18-color palette);
|
|
24
|
+
// PixelOE remains selectable as an outline pre-pass. Recover Grid opens on PixelOE + lines (16).
|
|
21
25
|
return {
|
|
22
26
|
mode,
|
|
23
|
-
reduceColors:
|
|
24
|
-
outlineStrength: 0,
|
|
25
|
-
|
|
27
|
+
reduceColors: true,
|
|
28
|
+
outlineStrength: converting ? DEFAULT_CRISP_DETAIL : 0,
|
|
29
|
+
kuwahara: converting,
|
|
30
|
+
prepass: converting ? OutlinePrepass.None : DEFAULT_OUTLINE_PREPASS,
|
|
26
31
|
lineGuide: DEFAULT_LINE_GUIDE,
|
|
27
32
|
colorPicker: mode === PixelationMode.RecoverGrid ? CellColorPicker.TwoStage : CellColorPicker.Contrast,
|
|
28
33
|
autoGrid: true,
|
|
@@ -30,8 +35,8 @@ export function defaultPixelationSettings(image, grid) {
|
|
|
30
35
|
gridCellHeight: grid?.reliable ? grid.cellHeight : 8,
|
|
31
36
|
targetWidth: Math.max(1, Math.round(image.width * scale)),
|
|
32
37
|
targetHeight: Math.max(1, Math.round(image.height * scale)),
|
|
33
|
-
maxColors: DEFAULT_COLORS,
|
|
34
|
-
method: PixelationMethod.
|
|
38
|
+
maxColors: converting ? DEFAULT_CONVERT_COLORS : DEFAULT_COLORS,
|
|
39
|
+
method: PixelationMethod.Crisp,
|
|
35
40
|
blockPicker: false,
|
|
36
41
|
preserveDimensions: false,
|
|
37
42
|
removeNoise: false,
|
|
@@ -65,6 +70,7 @@ export function updatePixelationSettings(settings, patch, image) {
|
|
|
65
70
|
next.lineGuide = DEFAULT_LINE_GUIDE;
|
|
66
71
|
if (!Object.values(PixelationDither).includes(next.dither))
|
|
67
72
|
next.dither = PixelationDither.None;
|
|
73
|
+
next.kuwahara = Boolean(next.kuwahara);
|
|
68
74
|
// Turning a PixelOE pre-pass on starts from Contrast, PixelOE's own colour rule.
|
|
69
75
|
if (settings.prepass === OutlinePrepass.None &&
|
|
70
76
|
next.prepass !== OutlinePrepass.None &&
|
|
@@ -77,8 +83,11 @@ export function updatePixelationSettings(settings, patch, image) {
|
|
|
77
83
|
if (patch.palette === null)
|
|
78
84
|
next.paletteName = "";
|
|
79
85
|
}
|
|
80
|
-
// Mode-switch defaults: Recover uses
|
|
86
|
+
// Mode-switch defaults: Recover uses TwoStage and limits colours; plain Convert enables Kuwahara.
|
|
81
87
|
if (patch.mode !== undefined && patch.mode !== settings.mode) {
|
|
88
|
+
if (patch.kuwahara === undefined) {
|
|
89
|
+
next.kuwahara = patch.mode === PixelationMode.Convert;
|
|
90
|
+
}
|
|
82
91
|
if (patch.reduceColors === undefined) {
|
|
83
92
|
if (patch.mode === PixelationMode.RecoverGrid) {
|
|
84
93
|
next.reduceColors = true;
|
|
@@ -127,11 +136,14 @@ export function pixelationSettingsOptions(settings, image, detected) {
|
|
|
127
136
|
grid = manualPixelGrid(image, settings.gridCellWidth, settings.gridCellHeight);
|
|
128
137
|
}
|
|
129
138
|
}
|
|
139
|
+
const isConvertLike = settings.mode === PixelationMode.Convert ||
|
|
140
|
+
detected?.algorithm === PixelationAlgorithm.CustomSize;
|
|
130
141
|
return {
|
|
131
142
|
mode: settings.mode,
|
|
132
143
|
grid,
|
|
133
144
|
colorPicker: settings.colorPicker,
|
|
134
|
-
outlineStrength:
|
|
145
|
+
outlineStrength: isConvertLike ? settings.outlineStrength : 0,
|
|
146
|
+
kuwahara: settings.kuwahara && isConvertLike,
|
|
135
147
|
prepass: settings.prepass,
|
|
136
148
|
lineGuide: settings.lineGuide,
|
|
137
149
|
targetWidth: settings.targetWidth,
|
|
@@ -157,14 +169,16 @@ export function pixelationSettingsOptions(settings, image, detected) {
|
|
|
157
169
|
* to the plain convert (resize). Shared by the import dialog and the tool pages.
|
|
158
170
|
*/
|
|
159
171
|
export function applyPixelationGrid(settings, grid, image) {
|
|
172
|
+
const customSize = grid.algorithm === PixelationAlgorithm.CustomSize;
|
|
160
173
|
return updatePixelationSettings(settings, grid.reliable
|
|
161
174
|
? {
|
|
162
175
|
mode: PixelationMode.RecoverGrid,
|
|
163
176
|
autoGrid: true,
|
|
164
177
|
gridCellWidth: grid.cellWidth,
|
|
165
178
|
gridCellHeight: grid.cellHeight,
|
|
179
|
+
...(customSize ? { kuwahara: settings.kuwahara } : { kuwahara: false }),
|
|
166
180
|
}
|
|
167
|
-
: { mode: PixelationMode.Convert }, image);
|
|
181
|
+
: { mode: PixelationMode.Convert, kuwahara: true }, image);
|
|
168
182
|
}
|
|
169
183
|
/**
|
|
170
184
|
* The output size for typing one dimension: the other follows the image's aspect ratio. Null when the
|
package/dist/types.d.ts
CHANGED
|
@@ -50,10 +50,10 @@ export interface PixelArtAnalysis {
|
|
|
50
50
|
export declare enum PixelationMethod {
|
|
51
51
|
Nearest = "nearest",
|
|
52
52
|
Box = "box",
|
|
53
|
-
MostFrequent = "most-frequent",
|
|
54
53
|
Dominant = "dominant",
|
|
55
54
|
Structure = "structure",
|
|
56
|
-
|
|
55
|
+
Lanczos = "lanczos",
|
|
56
|
+
Crisp = "crisp"
|
|
57
57
|
}
|
|
58
58
|
export declare enum PixelationMode {
|
|
59
59
|
Convert = "convert",
|
|
@@ -171,6 +171,11 @@ export interface PaletteSnapOptions {
|
|
|
171
171
|
palette?: readonly Rgba[];
|
|
172
172
|
/** Ordered / Floyd–Steinberg while snapping; None (nearest) when omitted. */
|
|
173
173
|
dither?: PixelationDither;
|
|
174
|
+
/**
|
|
175
|
+
* Weight clustering by local OKLab brightness/chroma contrast so small focal accents
|
|
176
|
+
* (eyes, lips, highlights, seals) keep dedicated palette slots. Defaults to true.
|
|
177
|
+
*/
|
|
178
|
+
saliency?: boolean;
|
|
174
179
|
}
|
|
175
180
|
export interface PixelateOptions {
|
|
176
181
|
mode?: PixelationMode;
|
|
@@ -179,6 +184,11 @@ export interface PixelateOptions {
|
|
|
179
184
|
colorPicker?: CellColorPicker;
|
|
180
185
|
grid?: PixelGridGeometry;
|
|
181
186
|
outlineStrength?: number;
|
|
187
|
+
/**
|
|
188
|
+
* Cel-shading simplification pre-pass (Anisotropic Kuwahara + highlight-safe OKLab clarity)
|
|
189
|
+
* ahead of sampling and outline expansion. Off when omitted.
|
|
190
|
+
*/
|
|
191
|
+
kuwahara?: boolean;
|
|
182
192
|
/**
|
|
183
193
|
* Outline pre-pass; None when omitted. With the Contrast picker and no grid recovered from the
|
|
184
194
|
* source (a plain convert or Custom size), a PixelOE pre-pass runs stock PixelOE end to end
|
|
@@ -205,7 +215,7 @@ export interface PixelateOptions {
|
|
|
205
215
|
* palette. Alpha stays per pixel. Off when omitted.
|
|
206
216
|
*/
|
|
207
217
|
paletteSnap?: PaletteSnapOptions;
|
|
208
|
-
/**
|
|
218
|
+
/** Smooth similar colors and remove isolated pixels on the working grid. */
|
|
209
219
|
removeNoise?: boolean;
|
|
210
220
|
/** Box mode defaults to straight channel means; premultiplied is useful for compositing. */
|
|
211
221
|
alphaMode?: "straight" | "premultiplied";
|
|
@@ -229,6 +239,8 @@ export interface PixelationSettings {
|
|
|
229
239
|
*/
|
|
230
240
|
reduceColors: boolean;
|
|
231
241
|
outlineStrength: number;
|
|
242
|
+
/** Cel-shading simplification pre-pass ahead of conversion (see `PixelateOptions.kuwahara`). */
|
|
243
|
+
kuwahara: boolean;
|
|
232
244
|
/** Outline pre-pass ahead of the colour rule (see `PixelateOptions.prepass`). */
|
|
233
245
|
prepass: OutlinePrepass;
|
|
234
246
|
/** Line guide of the PixelOE + lines pre-pass. */
|
package/dist/types.js
CHANGED
|
@@ -9,10 +9,10 @@ export var PixelationMethod;
|
|
|
9
9
|
(function (PixelationMethod) {
|
|
10
10
|
PixelationMethod["Nearest"] = "nearest";
|
|
11
11
|
PixelationMethod["Box"] = "box";
|
|
12
|
-
PixelationMethod["MostFrequent"] = "most-frequent";
|
|
13
12
|
PixelationMethod["Dominant"] = "dominant";
|
|
14
13
|
PixelationMethod["Structure"] = "structure";
|
|
15
|
-
PixelationMethod["
|
|
14
|
+
PixelationMethod["Lanczos"] = "lanczos";
|
|
15
|
+
PixelationMethod["Crisp"] = "crisp";
|
|
16
16
|
})(PixelationMethod || (PixelationMethod = {}));
|
|
17
17
|
export var PixelationMode;
|
|
18
18
|
(function (PixelationMode) {
|
package/package.json
CHANGED
package/dist/contours.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { PixelBuffer } from "./base.ts";
|
|
2
|
-
/** Independent thin-ridge filter: opposite neighbors identify narrow light/dark features.
|
|
3
|
-
* Only RGB is blended into existing visible pixels; the alpha footprint never grows. */
|
|
4
|
-
export declare function protectPixelationContours(image: PixelBuffer, strength: number): PixelBuffer;
|
package/dist/contours.js
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { UINT8_MAX } from "./base.js";
|
|
2
|
-
const CHANNELS = 4;
|
|
3
|
-
const COLOR_CHANNELS = 3;
|
|
4
|
-
const LUMA = [0.2126, 0.7152, 0.0722];
|
|
5
|
-
const MIN_CONTRAST = 0.14;
|
|
6
|
-
const BACKGROUND_TOLERANCE = 0.12;
|
|
7
|
-
const MAX_BLEND = 0.65;
|
|
8
|
-
const PERCENT = 100;
|
|
9
|
-
/** Independent thin-ridge filter: opposite neighbors identify narrow light/dark features.
|
|
10
|
-
* Only RGB is blended into existing visible pixels; the alpha footprint never grows. */
|
|
11
|
-
export function protectPixelationContours(image, strength) {
|
|
12
|
-
if (!strength)
|
|
13
|
-
return image;
|
|
14
|
-
const count = image.width * image.height;
|
|
15
|
-
const luminance = new Float32Array(count);
|
|
16
|
-
const features = new Float32Array(count);
|
|
17
|
-
const source = image.data;
|
|
18
|
-
for (let i = 0; i < count; i++) {
|
|
19
|
-
const offset = i * CHANNELS;
|
|
20
|
-
luminance[i] =
|
|
21
|
-
(source[offset] * LUMA[0] + source[offset + 1] * LUMA[1] + source[offset + 2] * LUMA[2]) /
|
|
22
|
-
UINT8_MAX;
|
|
23
|
-
}
|
|
24
|
-
const ridge = (center, first, second) => {
|
|
25
|
-
if (!source[first * CHANNELS + COLOR_CHANNELS] || !source[second * CHANNELS + COLOR_CHANNELS])
|
|
26
|
-
return 0;
|
|
27
|
-
const left = luminance[center] - luminance[first], right = luminance[center] - luminance[second];
|
|
28
|
-
if (left * right <= 0 || Math.abs(luminance[first] - luminance[second]) > BACKGROUND_TOLERANCE)
|
|
29
|
-
return 0;
|
|
30
|
-
return Math.min(Math.abs(left), Math.abs(right));
|
|
31
|
-
};
|
|
32
|
-
for (let y = 1; y < image.height - 1; y++)
|
|
33
|
-
for (let x = 1; x < image.width - 1; x++) {
|
|
34
|
-
const i = y * image.width + x;
|
|
35
|
-
if (!source[i * CHANNELS + COLOR_CHANNELS])
|
|
36
|
-
continue;
|
|
37
|
-
features[i] = Math.max(ridge(i, i - 1, i + 1), ridge(i, i - image.width, i + image.width));
|
|
38
|
-
}
|
|
39
|
-
const data = new Uint8ClampedArray(source);
|
|
40
|
-
for (let y = 0; y < image.height; y++)
|
|
41
|
-
for (let x = 0; x < image.width; x++) {
|
|
42
|
-
const i = y * image.width + x, offset = i * CHANNELS;
|
|
43
|
-
if (!source[offset + COLOR_CHANNELS] || features[i] >= MIN_CONTRAST)
|
|
44
|
-
continue;
|
|
45
|
-
let best = i, score = MIN_CONTRAST;
|
|
46
|
-
for (let dy = -1; dy <= 1; dy++)
|
|
47
|
-
for (let dx = -1; dx <= 1; dx++) {
|
|
48
|
-
const nx = x + dx, ny = y + dy;
|
|
49
|
-
if ((!dx && !dy) || nx < 0 || ny < 0 || nx >= image.width || ny >= image.height)
|
|
50
|
-
continue;
|
|
51
|
-
const neighbor = ny * image.width + nx;
|
|
52
|
-
const coverage = source[neighbor * CHANNELS + COLOR_CHANNELS] / UINT8_MAX;
|
|
53
|
-
const candidate = (features[neighbor] * coverage) / Math.max(1, Math.abs(dx) + Math.abs(dy));
|
|
54
|
-
if (candidate > score) {
|
|
55
|
-
score = candidate;
|
|
56
|
-
best = neighbor;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
if (best === i)
|
|
60
|
-
continue;
|
|
61
|
-
const amount = (strength / PERCENT) * MAX_BLEND;
|
|
62
|
-
for (let c = 0; c < COLOR_CHANNELS; c++)
|
|
63
|
-
data[offset + c] = Math.round(source[offset + c] * (1 - amount) + source[best * CHANNELS + c] * amount);
|
|
64
|
-
}
|
|
65
|
-
return { width: image.width, height: image.height, data };
|
|
66
|
-
}
|
package/dist/most-frequent.d.ts
DELETED
package/dist/most-frequent.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { UINT8_MAX } from "./base.js";
|
|
2
|
-
import { assertDimension, assertPixelCount } from "./base.js";
|
|
3
|
-
const CHANNELS = 4;
|
|
4
|
-
const RGB_BASE = 256;
|
|
5
|
-
const EMPTY_SAMPLE = -1;
|
|
6
|
-
const MAX_SAMPLES_PER_AXIS = 64;
|
|
7
|
-
/** Choose a cell's most frequent RGB, counting transparent pixels as empty samples. */
|
|
8
|
-
export function resizeMostFrequent(source, width, height) {
|
|
9
|
-
assertDimension(source.width, "source width");
|
|
10
|
-
assertDimension(source.height, "source height");
|
|
11
|
-
assertDimension(width, "width");
|
|
12
|
-
assertDimension(height, "height");
|
|
13
|
-
assertPixelCount(width, height);
|
|
14
|
-
if (source.data.length !== source.width * source.height * CHANNELS)
|
|
15
|
-
throw new Error("Invalid pixel buffer.");
|
|
16
|
-
const data = new Uint8ClampedArray(width * height * CHANNELS);
|
|
17
|
-
for (let row = 0; row < height; row++) {
|
|
18
|
-
const top = Math.floor((row * source.height) / height);
|
|
19
|
-
const bottom = Math.min(source.height, Math.max(top + 1, Math.floor(((row + 1) * source.height) / height)));
|
|
20
|
-
const yStep = Math.ceil((bottom - top) / MAX_SAMPLES_PER_AXIS);
|
|
21
|
-
for (let column = 0; column < width; column++) {
|
|
22
|
-
const left = Math.floor((column * source.width) / width);
|
|
23
|
-
const right = Math.min(source.width, Math.max(left + 1, Math.floor(((column + 1) * source.width) / width)));
|
|
24
|
-
const xStep = Math.ceil((right - left) / MAX_SAMPLES_PER_AXIS);
|
|
25
|
-
const counts = new Map();
|
|
26
|
-
let alphaSum = 0;
|
|
27
|
-
let samples = 0;
|
|
28
|
-
for (let y = top; y < bottom; y += yStep)
|
|
29
|
-
for (let x = left; x < right; x += xStep) {
|
|
30
|
-
const offset = (y * source.width + x) * CHANNELS;
|
|
31
|
-
alphaSum += source.data[offset + 3];
|
|
32
|
-
samples++;
|
|
33
|
-
const key = source.data[offset + 3] === 0
|
|
34
|
-
? EMPTY_SAMPLE
|
|
35
|
-
: (source.data[offset] * RGB_BASE + source.data[offset + 1]) * RGB_BASE +
|
|
36
|
-
source.data[offset + 2];
|
|
37
|
-
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
38
|
-
}
|
|
39
|
-
let selected = EMPTY_SAMPLE;
|
|
40
|
-
let largest = 0;
|
|
41
|
-
for (const [color, count] of counts)
|
|
42
|
-
if (count > largest) {
|
|
43
|
-
selected = color;
|
|
44
|
-
largest = count;
|
|
45
|
-
}
|
|
46
|
-
if (selected === EMPTY_SAMPLE)
|
|
47
|
-
continue;
|
|
48
|
-
const offset = (row * width + column) * CHANNELS;
|
|
49
|
-
data[offset] = Math.floor(selected / (RGB_BASE * RGB_BASE));
|
|
50
|
-
data[offset + 1] = Math.floor(selected / RGB_BASE) % RGB_BASE;
|
|
51
|
-
data[offset + 2] = selected % RGB_BASE;
|
|
52
|
-
data[offset + 3] = Math.min(UINT8_MAX, Math.round(alphaSum / samples));
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return { width, height, data };
|
|
56
|
-
}
|