@xprite/pixelate 0.1.3 → 0.2.1
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/deskew.js +28 -15
- package/dist/fft-autocorr.d.ts +12 -0
- package/dist/fft-autocorr.js +116 -0
- package/dist/float-image.d.ts +25 -0
- package/dist/float-image.js +184 -0
- package/dist/grid-arbitrate.d.ts +8 -0
- package/dist/grid-arbitrate.js +219 -65
- package/dist/grid-cut-snap.js +2 -38
- package/dist/grid-description-length.js +416 -111
- package/dist/grid-edge-coherence.js +4 -46
- package/dist/grid-edge-path.js +2 -33
- package/dist/grid-frame-lattice.js +109 -14
- package/dist/grid-period-comb.js +126 -22
- package/dist/grid-period-distance.js +59 -10
- package/dist/grid-pitch-lattice.js +21 -9
- package/dist/grid-pitch-search.js +38 -18
- package/dist/grid-sample.js +2 -2
- package/dist/grid-sampling.d.ts +6 -0
- package/dist/grid-sampling.js +111 -10
- package/dist/grid-step-profiles.d.ts +13 -0
- package/dist/grid-step-profiles.js +88 -0
- package/dist/grid-strip-shift.js +153 -35
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/kuwahara.d.ts +9 -0
- package/dist/kuwahara.js +287 -0
- package/dist/line-guided/pixeloe-entry.d.ts +2 -2
- package/dist/line-guided/pixeloe-entry.js +6 -35
- package/dist/orphan-pixels.d.ts +7 -0
- package/dist/orphan-pixels.js +133 -0
- package/dist/palette-snap.d.ts +1 -1
- package/dist/palette-snap.js +127 -58
- package/dist/pixelate.d.ts +4 -7
- package/dist/pixelate.js +133 -98
- package/dist/quantization.d.ts +4 -0
- package/dist/quantization.js +23 -16
- package/dist/resize-crisp.d.ts +11 -0
- package/dist/resize-crisp.js +213 -0
- package/dist/resize-lanczos.d.ts +7 -0
- package/dist/resize-lanczos.js +205 -0
- package/dist/sampling.d.ts +2 -1
- package/dist/sampling.js +1 -1
- package/dist/settings.d.ts +6 -3
- package/dist/settings.js +26 -12
- package/dist/types.d.ts +14 -2
- package/dist/types.js +2 -2
- package/package.json +1 -1
- package/dist/contours.d.ts +0 -4
- package/dist/contours.js +0 -66
- package/dist/most-frequent.d.ts +0 -3
- package/dist/most-frequent.js +0 -56
package/dist/quantization.d.ts
CHANGED
|
@@ -5,6 +5,10 @@ export type Oklab = readonly [lightness: number, a: number, b: number];
|
|
|
5
5
|
export declare function paletteCoordinates(color: Rgba): Rgba;
|
|
6
6
|
/** Convert opaque sRGB bytes to OKLab coordinates. Cached per packed RGB. */
|
|
7
7
|
export declare function rgbToOklab(r: number, g: number, b: number): Oklab;
|
|
8
|
+
/** Convert normalized sRGB floats (0..1) to OKLab. */
|
|
9
|
+
export declare function rgbFloatToOklab(r: number, g: number, b: number): Oklab;
|
|
10
|
+
/** Convert OKLab coordinates back to normalized sRGB floats (0..1). */
|
|
11
|
+
export declare function oklabToRgbFloat(lightness: number, a: number, b: number): [number, number, number];
|
|
8
12
|
/** Convert OKLab coordinates back to 8-bit sRGB. */
|
|
9
13
|
export declare function oklabToRgb(lightness: number, a: number, b: number): [number, number, number];
|
|
10
14
|
/** Variance-driven color partitioning keeps accents instead of selecting only frequent shades. */
|
package/dist/quantization.js
CHANGED
|
@@ -8,12 +8,10 @@ const LINEAR_DIVISOR = 12.92;
|
|
|
8
8
|
const GAMMA_OFFSET = 0.055;
|
|
9
9
|
const GAMMA_SCALE = 1.055;
|
|
10
10
|
const GAMMA_EXPONENT = 2.4;
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
: ((value + GAMMA_OFFSET) / GAMMA_SCALE) ** GAMMA_EXPONENT;
|
|
16
|
-
};
|
|
11
|
+
const srgbFloatToLinear = (value) => value <= LINEAR_THRESHOLD
|
|
12
|
+
? value / LINEAR_DIVISOR
|
|
13
|
+
: ((value + GAMMA_OFFSET) / GAMMA_SCALE) ** GAMMA_EXPONENT;
|
|
14
|
+
const linear = (byte) => srgbFloatToLinear(byte / UINT8_MAX);
|
|
17
15
|
const LINEAR = Float64Array.from({ length: UINT8_MAX + 1 }, (_, byte) => linear(byte));
|
|
18
16
|
const linearChannel = (byte) => (byte === 0 ? byte : (LINEAR[byte] ?? linear(byte)));
|
|
19
17
|
// Oklab matrices published by Björn Ottosson (public domain):
|
|
@@ -71,23 +69,32 @@ export function rgbToOklab(r, g, b) {
|
|
|
71
69
|
oklabCache.set(key, value);
|
|
72
70
|
return value;
|
|
73
71
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
: GAMMA_SCALE * value ** (1 / GAMMA_EXPONENT) - GAMMA_OFFSET;
|
|
78
|
-
return clampByte(encoded * UINT8_MAX);
|
|
72
|
+
/** Convert normalized sRGB floats (0..1) to OKLab. */
|
|
73
|
+
export function rgbFloatToOklab(r, g, b) {
|
|
74
|
+
return linearRgbToOklab(srgbFloatToLinear(r), srgbFloatToLinear(g), srgbFloatToLinear(b));
|
|
79
75
|
}
|
|
80
|
-
|
|
81
|
-
|
|
76
|
+
function linearToSrgbFloat(value) {
|
|
77
|
+
const c = Math.max(0, Math.min(1, value));
|
|
78
|
+
return c <= LINEAR_TO_SRGB_THRESHOLD
|
|
79
|
+
? c * LINEAR_DIVISOR
|
|
80
|
+
: GAMMA_SCALE * c ** (1 / GAMMA_EXPONENT) - GAMMA_OFFSET;
|
|
81
|
+
}
|
|
82
|
+
/** Convert OKLab coordinates back to normalized sRGB floats (0..1). */
|
|
83
|
+
export function oklabToRgbFloat(lightness, a, b) {
|
|
82
84
|
const l = dot(OKLAB_TO_LMS[0], lightness, a, b) ** 3;
|
|
83
85
|
const m = dot(OKLAB_TO_LMS[1], lightness, a, b) ** 3;
|
|
84
86
|
const s = dot(OKLAB_TO_LMS[2], lightness, a, b) ** 3;
|
|
85
87
|
return [
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
88
|
+
linearToSrgbFloat(dot(LMS_TO_LINEAR_SRGB[0], l, m, s)),
|
|
89
|
+
linearToSrgbFloat(dot(LMS_TO_LINEAR_SRGB[1], l, m, s)),
|
|
90
|
+
linearToSrgbFloat(dot(LMS_TO_LINEAR_SRGB[2], l, m, s)),
|
|
89
91
|
];
|
|
90
92
|
}
|
|
93
|
+
/** Convert OKLab coordinates back to 8-bit sRGB. */
|
|
94
|
+
export function oklabToRgb(lightness, a, b) {
|
|
95
|
+
const [r, g, bl] = oklabToRgbFloat(lightness, a, b);
|
|
96
|
+
return [clampByte(r * UINT8_MAX), clampByte(g * UINT8_MAX), clampByte(bl * UINT8_MAX)];
|
|
97
|
+
}
|
|
91
98
|
function box(samples) {
|
|
92
99
|
const sum = [0, 0, 0, 0], squares = [0, 0, 0, 0];
|
|
93
100
|
let count = 0;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type PixelBuffer } from "./base.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Surround-contrast 2-means non-AA downsampler (C5):
|
|
4
|
+
* Splits each output cell in OKLab into two colour clusters instead of averaging across boundaries:
|
|
5
|
+
* - On straight edges, the dominant side wins cleanly with zero anti-aliased grey halo;
|
|
6
|
+
* - On thin bright (eye sclera, pearl, lantern), dark (pupil, ink line), or high-chroma (lips,
|
|
7
|
+
* iris, red seal) micro-features, the higher OKLab ΔE against the 3×3 cell surround wins cleanly.
|
|
8
|
+
* `detailStrength` (0-100, default 50) tunes the trade-off between clean flat clusters (0) and
|
|
9
|
+
* aggressive 1 px thin-line / micro-detail preservation (100).
|
|
10
|
+
*/
|
|
11
|
+
export declare function resizeCrisp2Means(image: PixelBuffer, width: number, height: number, detailStrength?: number): PixelBuffer;
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { assertDimension, assertPixelBuffer, clampByte, UINT8_MAX } from "./base.js";
|
|
2
|
+
import { gaussianBlur, resizeAreaFloat, toFloatRgba, toOklabPlanes } from "./float-image.js";
|
|
3
|
+
import { oklabToRgbFloat } from "./quantization.js";
|
|
4
|
+
import { resizeNearest } from "./resize.js";
|
|
5
|
+
const WORKING_SCALE = 4;
|
|
6
|
+
const CHANNELS = 4;
|
|
7
|
+
const CRISP_CHROMA_WEIGHT = 2.2;
|
|
8
|
+
const CRISP_SURROUND_CHROMA_WEIGHT = 3.8;
|
|
9
|
+
const DEFAULT_CRISP_DETAIL = 50;
|
|
10
|
+
/**
|
|
11
|
+
* Surround-contrast 2-means non-AA downsampler (C5):
|
|
12
|
+
* Splits each output cell in OKLab into two colour clusters instead of averaging across boundaries:
|
|
13
|
+
* - On straight edges, the dominant side wins cleanly with zero anti-aliased grey halo;
|
|
14
|
+
* - On thin bright (eye sclera, pearl, lantern), dark (pupil, ink line), or high-chroma (lips,
|
|
15
|
+
* iris, red seal) micro-features, the higher OKLab ΔE against the 3×3 cell surround wins cleanly.
|
|
16
|
+
* `detailStrength` (0-100, default 50) tunes the trade-off between clean flat clusters (0) and
|
|
17
|
+
* aggressive 1 px thin-line / micro-detail preservation (100).
|
|
18
|
+
*/
|
|
19
|
+
export function resizeCrisp2Means(image, width, height, detailStrength = DEFAULT_CRISP_DETAIL) {
|
|
20
|
+
assertPixelBuffer(image);
|
|
21
|
+
assertDimension(width, "width");
|
|
22
|
+
assertDimension(height, "height");
|
|
23
|
+
if (image.width <= width && image.height <= height) {
|
|
24
|
+
return resizeNearest(image, width, height);
|
|
25
|
+
}
|
|
26
|
+
const t = Math.max(0, Math.min(1, detailStrength / 100));
|
|
27
|
+
const surroundGain = 1.4 + 3.2 * t;
|
|
28
|
+
const countExponent = 0.92 - 0.16 * t;
|
|
29
|
+
const minFeatureRatio = 0.28 - 0.12 * t;
|
|
30
|
+
const splitThreshold = 0.07 - 0.035 * t;
|
|
31
|
+
const maxWorkW = Math.max(width, width * WORKING_SCALE);
|
|
32
|
+
const maxWorkH = Math.max(height, height * WORKING_SCALE);
|
|
33
|
+
const floatSrc = toFloatRgba(image);
|
|
34
|
+
const working = floatSrc.w > maxWorkW || floatSrc.h > maxWorkH
|
|
35
|
+
? resizeAreaFloat(floatSrc, Math.min(floatSrc.w, maxWorkW), Math.min(floatSrc.h, maxWorkH))
|
|
36
|
+
: floatSrc;
|
|
37
|
+
const { r, g, b, a, w, h, opaque } = working;
|
|
38
|
+
const { L, A, B } = toOklabPlanes(working, 0.5);
|
|
39
|
+
const sx = w / width;
|
|
40
|
+
const sy = h / height;
|
|
41
|
+
const bgSigma = Math.max(1.2, ((sx + sy) * 0.5) * 1.125);
|
|
42
|
+
let Lbg = gaussianBlur(L, w, h, bgSigma);
|
|
43
|
+
let Abg = gaussianBlur(A, w, h, bgSigma);
|
|
44
|
+
let Bbg = gaussianBlur(B, w, h, bgSigma);
|
|
45
|
+
if (!opaque) {
|
|
46
|
+
// Normalize surround blur by visible-pixel weight so transparent pixels (L=0) never act
|
|
47
|
+
// like a fake black surround that suppresses dark outer outlines on transparent sprites.
|
|
48
|
+
const n = w * h;
|
|
49
|
+
const visMask = new Float32Array(n);
|
|
50
|
+
for (let i = 0; i < n; i++)
|
|
51
|
+
if (a[i] > 0.5)
|
|
52
|
+
visMask[i] = 1;
|
|
53
|
+
const wBg = gaussianBlur(visMask, w, h, bgSigma);
|
|
54
|
+
for (let i = 0; i < n; i++) {
|
|
55
|
+
const inv = wBg[i] > 1e-4 ? 1 / wBg[i] : 0;
|
|
56
|
+
Lbg[i] *= inv;
|
|
57
|
+
Abg[i] *= inv;
|
|
58
|
+
Bbg[i] *= inv;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const out = new Uint8ClampedArray(width * height * CHANNELS);
|
|
62
|
+
const pts = [];
|
|
63
|
+
for (let gy = 0; gy < height; gy++) {
|
|
64
|
+
const y0 = Math.floor(gy * sy);
|
|
65
|
+
const y1 = Math.min(h, Math.max(y0 + 1, Math.ceil((gy + 1) * sy)));
|
|
66
|
+
for (let gx = 0; gx < width; gx++) {
|
|
67
|
+
const x0 = Math.floor(gx * sx);
|
|
68
|
+
const x1 = Math.min(w, Math.max(x0 + 1, Math.ceil((gx + 1) * sx)));
|
|
69
|
+
pts.length = 0;
|
|
70
|
+
let total = 0;
|
|
71
|
+
let sBgL = 0;
|
|
72
|
+
let sBgA = 0;
|
|
73
|
+
let sBgB = 0;
|
|
74
|
+
for (let y = y0; y < y1; y++) {
|
|
75
|
+
const row = y * w;
|
|
76
|
+
for (let x = x0; x < x1; x++) {
|
|
77
|
+
const idx = row + x;
|
|
78
|
+
total += 1;
|
|
79
|
+
if (!opaque && a[idx] <= 0.5)
|
|
80
|
+
continue;
|
|
81
|
+
pts.push(idx);
|
|
82
|
+
sBgL += Lbg[idx];
|
|
83
|
+
sBgA += Abg[idx];
|
|
84
|
+
sBgB += Bbg[idx];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const oi = (gy * width + gx) * CHANNELS;
|
|
88
|
+
const visible = pts.length;
|
|
89
|
+
if (visible === 0 || (!opaque && visible * 2 < total))
|
|
90
|
+
continue;
|
|
91
|
+
if (visible === 1) {
|
|
92
|
+
const p = pts[0];
|
|
93
|
+
out[oi] = clampByte(r[p] * UINT8_MAX);
|
|
94
|
+
out[oi + 1] = clampByte(g[p] * UINT8_MAX);
|
|
95
|
+
out[oi + 2] = clampByte(b[p] * UINT8_MAX);
|
|
96
|
+
out[oi + 3] = UINT8_MAX;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const bgL = sBgL / visible;
|
|
100
|
+
const bgA = sBgA / visible;
|
|
101
|
+
const bgB = sBgB / visible;
|
|
102
|
+
// Seed 2-means with the two farthest visible pixels in chroma-weighted OKLab.
|
|
103
|
+
let pA = pts[0];
|
|
104
|
+
let maxD0 = -1;
|
|
105
|
+
for (let k = 0; k < visible; k++) {
|
|
106
|
+
const i = pts[k];
|
|
107
|
+
const d = (L[i] - bgL) ** 2 + CRISP_CHROMA_WEIGHT * ((A[i] - bgA) ** 2 + (B[i] - bgB) ** 2);
|
|
108
|
+
if (d > maxD0) {
|
|
109
|
+
maxD0 = d;
|
|
110
|
+
pA = i;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
let pB = pts[0];
|
|
114
|
+
let maxD1 = -1;
|
|
115
|
+
const lA = L[pA];
|
|
116
|
+
const aA = A[pA];
|
|
117
|
+
const bA = B[pA];
|
|
118
|
+
for (let k = 0; k < visible; k++) {
|
|
119
|
+
const i = pts[k];
|
|
120
|
+
const d = (L[i] - lA) ** 2 + CRISP_CHROMA_WEIGHT * ((A[i] - aA) ** 2 + (B[i] - bA) ** 2);
|
|
121
|
+
if (d > maxD1) {
|
|
122
|
+
maxD1 = d;
|
|
123
|
+
pB = i;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
let chosenL;
|
|
127
|
+
let chosenA;
|
|
128
|
+
let chosenB;
|
|
129
|
+
if (Math.sqrt(maxD1) < splitThreshold) {
|
|
130
|
+
let sl = 0;
|
|
131
|
+
let sa = 0;
|
|
132
|
+
let sb = 0;
|
|
133
|
+
for (let k = 0; k < visible; k++) {
|
|
134
|
+
const i = pts[k];
|
|
135
|
+
sl += L[i];
|
|
136
|
+
sa += A[i];
|
|
137
|
+
sb += B[i];
|
|
138
|
+
}
|
|
139
|
+
chosenL = sl / visible;
|
|
140
|
+
chosenA = sa / visible;
|
|
141
|
+
chosenB = sb / visible;
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
let c0L = L[pA];
|
|
145
|
+
let c0A = A[pA];
|
|
146
|
+
let c0B = B[pA];
|
|
147
|
+
let c1L = L[pB];
|
|
148
|
+
let c1A = A[pB];
|
|
149
|
+
let c1B = B[pB];
|
|
150
|
+
let n0 = 0;
|
|
151
|
+
let n1 = 0;
|
|
152
|
+
for (let iter = 0; iter < 3; iter++) {
|
|
153
|
+
let s0L = 0;
|
|
154
|
+
let s0A = 0;
|
|
155
|
+
let s0B = 0;
|
|
156
|
+
let s1L = 0;
|
|
157
|
+
let s1A = 0;
|
|
158
|
+
let s1B = 0;
|
|
159
|
+
n0 = 0;
|
|
160
|
+
n1 = 0;
|
|
161
|
+
for (let k = 0; k < visible; k++) {
|
|
162
|
+
const i = pts[k];
|
|
163
|
+
const d0 = (L[i] - c0L) ** 2 + CRISP_CHROMA_WEIGHT * ((A[i] - c0A) ** 2 + (B[i] - c0B) ** 2);
|
|
164
|
+
const d1 = (L[i] - c1L) ** 2 + CRISP_CHROMA_WEIGHT * ((A[i] - c1A) ** 2 + (B[i] - c1B) ** 2);
|
|
165
|
+
if (d0 <= d1) {
|
|
166
|
+
s0L += L[i];
|
|
167
|
+
s0A += A[i];
|
|
168
|
+
s0B += B[i];
|
|
169
|
+
n0 += 1;
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
s1L += L[i];
|
|
173
|
+
s1A += A[i];
|
|
174
|
+
s1B += B[i];
|
|
175
|
+
n1 += 1;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (n0 > 0) {
|
|
179
|
+
c0L = s0L / n0;
|
|
180
|
+
c0A = s0A / n0;
|
|
181
|
+
c0B = s0B / n0;
|
|
182
|
+
}
|
|
183
|
+
if (n1 > 0) {
|
|
184
|
+
c1L = s1L / n1;
|
|
185
|
+
c1A = s1A / n1;
|
|
186
|
+
c1B = s1B / n1;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const dev0 = Math.sqrt((c0L - bgL) ** 2 + CRISP_SURROUND_CHROMA_WEIGHT * ((c0A - bgA) ** 2 + (c0B - bgB) ** 2));
|
|
190
|
+
const dev1 = Math.sqrt((c1L - bgL) ** 2 + CRISP_SURROUND_CHROMA_WEIGHT * ((c1A - bgA) ** 2 + (c1B - bgB) ** 2));
|
|
191
|
+
const minFeaturePixels = Math.max(1, Math.round(visible * minFeatureRatio));
|
|
192
|
+
const eff0 = n0 >= minFeaturePixels ? n0 ** countExponent * (1 + surroundGain * dev0) : n0 * 0.4;
|
|
193
|
+
const eff1 = n1 >= minFeaturePixels ? n1 ** countExponent * (1 + surroundGain * dev1) : n1 * 0.4;
|
|
194
|
+
if (eff0 >= eff1) {
|
|
195
|
+
chosenL = c0L;
|
|
196
|
+
chosenA = c0A;
|
|
197
|
+
chosenB = c0B;
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
chosenL = c1L;
|
|
201
|
+
chosenA = c1A;
|
|
202
|
+
chosenB = c1B;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
const [or, og, ob] = oklabToRgbFloat(chosenL, chosenA, chosenB);
|
|
206
|
+
out[oi] = clampByte(or * UINT8_MAX);
|
|
207
|
+
out[oi + 1] = clampByte(og * UINT8_MAX);
|
|
208
|
+
out[oi + 2] = clampByte(ob * UINT8_MAX);
|
|
209
|
+
out[oi + 3] = UINT8_MAX;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return { width, height, data: out };
|
|
213
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type PixelBuffer } from "./base.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Alpha-aware separable Lanczos-3 resampler with anti-ringing clamping:
|
|
4
|
+
* each output pixel is clamped to its local source footprint's min/max RGB so negative filter
|
|
5
|
+
* lobes never introduce overshoot halos along hard outlines.
|
|
6
|
+
*/
|
|
7
|
+
export declare function resizeLanczos(image: PixelBuffer, width: number, height: number, sharpen?: boolean): PixelBuffer;
|
|
@@ -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
|