@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
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { linearAutocorrelation } from "./fft-autocorr.js";
|
|
1
2
|
import { MAX_GRID_CELL_SIZE, MAX_RECOVERED_GRID_SIZE } from "./grid.js";
|
|
2
3
|
/**
|
|
3
4
|
* Pitch search for upscaled pixel art whose edges are too soft for the edge-projection detectors
|
|
@@ -69,18 +70,28 @@ function energyProfile(image, horizontal, order, span) {
|
|
|
69
70
|
const step = unit * span;
|
|
70
71
|
const profile = new Float64Array(length);
|
|
71
72
|
let lines = 0;
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
73
|
+
if (order === ProfileOrder.Curvature) {
|
|
74
|
+
for (let line = 0; line < cross; line += lineStride, lines++) {
|
|
75
|
+
const start = (horizontal ? line * width : line) * CHANNELS;
|
|
76
|
+
for (let i = span, end = length - span; i < end; i++) {
|
|
77
|
+
const o = start + i * unit;
|
|
78
|
+
const dr = data[o + step] - 2 * data[o] + data[o - step];
|
|
79
|
+
const dg = data[o + step + 1] - 2 * data[o + 1] + data[o - step + 1];
|
|
80
|
+
const db = data[o + step + 2] - 2 * data[o + 2] + data[o - step + 2];
|
|
81
|
+
profile[i] += dr * dr + dg * dg + db * db;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
for (let line = 0; line < cross; line += lineStride, lines++) {
|
|
87
|
+
const start = (horizontal ? line * width : line) * CHANNELS;
|
|
88
|
+
for (let i = span, end = length - span; i < end; i++) {
|
|
89
|
+
const o = start + i * unit;
|
|
90
|
+
const dr = data[o + step] - data[o - step];
|
|
91
|
+
const dg = data[o + step + 1] - data[o - step + 1];
|
|
92
|
+
const db = data[o + step + 2] - data[o - step + 2];
|
|
93
|
+
profile[i] += dr * dr + dg * dg + db * db;
|
|
82
94
|
}
|
|
83
|
-
profile[i] += energy;
|
|
84
95
|
}
|
|
85
96
|
}
|
|
86
97
|
for (let i = 0; i < length; i++)
|
|
@@ -96,17 +107,26 @@ function normalizedAutocorrelation(signal, maxLag) {
|
|
|
96
107
|
let energy = 0;
|
|
97
108
|
const centred = new Float64Array(n);
|
|
98
109
|
for (let i = 0; i < n; i++) {
|
|
99
|
-
|
|
100
|
-
|
|
110
|
+
const v = signal[i] - mean;
|
|
111
|
+
centred[i] = v;
|
|
112
|
+
energy += v * v;
|
|
101
113
|
}
|
|
102
114
|
const result = new Float64Array(maxLag + 1);
|
|
103
115
|
if (energy < 1e-12)
|
|
104
116
|
return result;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
117
|
+
if (n > 1024) {
|
|
118
|
+
linearAutocorrelation(centred, maxLag, result);
|
|
119
|
+
const invEnergy = 1 / energy;
|
|
120
|
+
for (let lag = 0; lag <= maxLag; lag++)
|
|
121
|
+
result[lag] *= invEnergy;
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
for (let lag = 0; lag <= maxLag; lag++) {
|
|
125
|
+
let sum = 0;
|
|
126
|
+
for (let i = 0; i < n - lag; i++)
|
|
127
|
+
sum += centred[i] * centred[i + lag];
|
|
128
|
+
result[lag] = sum / energy;
|
|
129
|
+
}
|
|
110
130
|
}
|
|
111
131
|
return result;
|
|
112
132
|
}
|
package/dist/grid-sample.js
CHANGED
|
@@ -63,12 +63,12 @@ function pickCells(image, grid, picker, x, y) {
|
|
|
63
63
|
}
|
|
64
64
|
// TwoStage picker: Fixer-like two-stage packing (adaptive K, pure triangular weights)
|
|
65
65
|
if (picker === CellColorPicker.TwoStage) {
|
|
66
|
-
return samplePixelation(image, width, height,
|
|
66
|
+
return samplePixelation(image, width, height, "two-stage", { x, y });
|
|
67
67
|
}
|
|
68
68
|
// Dual RT picker: Structure vs TwoStage, winner chosen by round-trip error
|
|
69
69
|
if (picker === CellColorPicker.DualRtTwoStage) {
|
|
70
70
|
const candidateA = samplePixelation(image, width, height, PixelationMethod.Structure, { x, y });
|
|
71
|
-
const candidateB = samplePixelation(image, width, height,
|
|
71
|
+
const candidateB = samplePixelation(image, width, height, "two-stage", { x, y });
|
|
72
72
|
const cells = { x: pixelCells(image.width, x), y: pixelCells(image.height, y) };
|
|
73
73
|
const errorA = roundTripError(image, candidateA, cells);
|
|
74
74
|
const errorB = roundTripError(image, candidateB, cells);
|
package/dist/grid-sampling.d.ts
CHANGED
|
@@ -3,6 +3,12 @@ import { type PixelGridAnalysis, type PixelGridSamplingStage } from "./types.ts"
|
|
|
3
3
|
/** Internal image view. Its data stays in the source; only offset reads apply local shifts. */
|
|
4
4
|
export interface GridImageView extends PixelBuffer {
|
|
5
5
|
offset?: (x: number, y: number) => number;
|
|
6
|
+
/** Fill `out[x]` with RGBA byte offset for pixel `(x, y)`, `0 <= x < width`. */
|
|
7
|
+
rowOffsets?: (y: number, out: Int32Array) => void;
|
|
8
|
+
/** Fill `out[y]` with RGBA byte offset for pixel `(x, y)`, `0 <= y < height`. */
|
|
9
|
+
colOffsets?: (x: number, out: Int32Array) => void;
|
|
10
|
+
/** Internal horizontal per-row x-coordinate lookup when this view is a single horizontal shift. */
|
|
11
|
+
_horizByRow?: readonly Int32Array[];
|
|
6
12
|
sampling?: readonly PixelGridSamplingStage[];
|
|
7
13
|
}
|
|
8
14
|
/** Compose inverse steps in reverse order, keeping clamping at each intermediate image. */
|
package/dist/grid-sampling.js
CHANGED
|
@@ -79,28 +79,129 @@ export function shiftedGridImage(image, stage) {
|
|
|
79
79
|
return coordinates;
|
|
80
80
|
});
|
|
81
81
|
const byLine = Array.from({ length: perpendicular }, (_, p) => maps[ids[across[p]]]);
|
|
82
|
+
let rowOffsets;
|
|
83
|
+
let colOffsets;
|
|
84
|
+
let horizByRow;
|
|
82
85
|
if (vertical) {
|
|
83
|
-
|
|
84
|
-
|
|
86
|
+
const prevHoriz = image._horizByRow;
|
|
87
|
+
if (!previous) {
|
|
88
|
+
offset = (x, y) => {
|
|
89
|
+
const at = byLine[x]?.[y];
|
|
90
|
+
return at === undefined ? original(x, y) : (at * width + x) * STRIDE;
|
|
91
|
+
};
|
|
92
|
+
rowOffsets = (y, out) => {
|
|
93
|
+
for (let x = 0; x < width; x++)
|
|
94
|
+
out[x] = (byLine[x][y] * width + x) * STRIDE;
|
|
95
|
+
};
|
|
96
|
+
colOffsets = (x, out) => {
|
|
97
|
+
const map = byLine[x];
|
|
98
|
+
for (let y = 0; y < height; y++)
|
|
99
|
+
out[y] = (map[y] * width + x) * STRIDE;
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
else if (prevHoriz) {
|
|
103
|
+
offset = (x, y) => {
|
|
104
|
+
const at = byLine[x]?.[y];
|
|
105
|
+
return at === undefined ? original(x, y) : (at * width + prevHoriz[at][x]) * STRIDE;
|
|
106
|
+
};
|
|
107
|
+
rowOffsets = (y, out) => {
|
|
108
|
+
for (let x = 0; x < width; x++) {
|
|
109
|
+
const sy = byLine[x][y];
|
|
110
|
+
out[x] = (sy * width + prevHoriz[sy][x]) * STRIDE;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
colOffsets = (x, out) => {
|
|
114
|
+
const map = byLine[x];
|
|
115
|
+
for (let y = 0; y < height; y++) {
|
|
116
|
+
const sy = map[y];
|
|
117
|
+
out[y] = (sy * width + prevHoriz[sy][x]) * STRIDE;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
offset = (x, y) => {
|
|
85
123
|
const at = byLine[x]?.[y];
|
|
86
124
|
return at === undefined ? original(x, y) : previous(x, at);
|
|
125
|
+
};
|
|
126
|
+
const prevCol = image.colOffsets;
|
|
127
|
+
if (prevCol) {
|
|
128
|
+
const scratch = new Int32Array(height);
|
|
129
|
+
colOffsets = (x, out) => {
|
|
130
|
+
prevCol(x, scratch);
|
|
131
|
+
const map = byLine[x];
|
|
132
|
+
for (let y = 0; y < height; y++)
|
|
133
|
+
out[y] = scratch[map[y]];
|
|
134
|
+
};
|
|
87
135
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
136
|
+
else {
|
|
137
|
+
colOffsets = (x, out) => {
|
|
138
|
+
const map = byLine[x];
|
|
139
|
+
for (let y = 0; y < height; y++)
|
|
140
|
+
out[y] = previous(x, map[y]);
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
rowOffsets = (y, out) => {
|
|
144
|
+
for (let x = 0; x < width; x++)
|
|
145
|
+
out[x] = previous(x, byLine[x][y]);
|
|
91
146
|
};
|
|
147
|
+
}
|
|
92
148
|
}
|
|
93
149
|
else {
|
|
94
|
-
|
|
95
|
-
|
|
150
|
+
if (!previous) {
|
|
151
|
+
horizByRow = byLine;
|
|
152
|
+
offset = (x, y) => {
|
|
153
|
+
const at = byLine[y]?.[x];
|
|
154
|
+
return at === undefined ? original(x, y) : (y * width + at) * STRIDE;
|
|
155
|
+
};
|
|
156
|
+
rowOffsets = (y, out) => {
|
|
157
|
+
const map = byLine[y];
|
|
158
|
+
const rowBase = y * width;
|
|
159
|
+
for (let x = 0; x < width; x++)
|
|
160
|
+
out[x] = (rowBase + map[x]) * STRIDE;
|
|
161
|
+
};
|
|
162
|
+
colOffsets = (x, out) => {
|
|
163
|
+
for (let y = 0; y < height; y++)
|
|
164
|
+
out[y] = (y * width + byLine[y][x]) * STRIDE;
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
offset = (x, y) => {
|
|
96
169
|
const at = byLine[y]?.[x];
|
|
97
170
|
return at === undefined ? original(x, y) : previous(at, y);
|
|
171
|
+
};
|
|
172
|
+
const prevRow = image.rowOffsets;
|
|
173
|
+
if (prevRow) {
|
|
174
|
+
const scratch = new Int32Array(width);
|
|
175
|
+
rowOffsets = (y, out) => {
|
|
176
|
+
prevRow(y, scratch);
|
|
177
|
+
const map = byLine[y];
|
|
178
|
+
for (let x = 0; x < width; x++)
|
|
179
|
+
out[x] = scratch[map[x]];
|
|
180
|
+
};
|
|
98
181
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
182
|
+
else {
|
|
183
|
+
rowOffsets = (y, out) => {
|
|
184
|
+
const map = byLine[y];
|
|
185
|
+
for (let x = 0; x < width; x++)
|
|
186
|
+
out[x] = previous(map[x], y);
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
colOffsets = (x, out) => {
|
|
190
|
+
for (let y = 0; y < height; y++)
|
|
191
|
+
out[y] = previous(byLine[y][x], y);
|
|
102
192
|
};
|
|
193
|
+
}
|
|
103
194
|
}
|
|
195
|
+
return {
|
|
196
|
+
width,
|
|
197
|
+
height,
|
|
198
|
+
data,
|
|
199
|
+
sampling: [...(image.sampling ?? []), stage],
|
|
200
|
+
offset,
|
|
201
|
+
rowOffsets,
|
|
202
|
+
colOffsets,
|
|
203
|
+
_horizByRow: horizByRow,
|
|
204
|
+
};
|
|
104
205
|
}
|
|
105
206
|
}
|
|
106
207
|
return { width, height, data, sampling: [...(image.sampling ?? []), stage], offset };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { PixelBuffer } from "./base.ts";
|
|
2
|
+
export declare const STEP_PROFILE_BANDS = 8;
|
|
3
|
+
export interface BandedStepProfiles {
|
|
4
|
+
/** Per-band horizontal step sums (length = width). */
|
|
5
|
+
bandsX: readonly Float64Array[];
|
|
6
|
+
/** Per-band vertical step sums (length = height). */
|
|
7
|
+
bandsY: readonly Float64Array[];
|
|
8
|
+
/** Whole-image horizontal step sums (length = width). */
|
|
9
|
+
x: Float64Array;
|
|
10
|
+
/** Whole-image vertical step sums (length = height). */
|
|
11
|
+
y: Float64Array;
|
|
12
|
+
}
|
|
13
|
+
export declare function imageStepProfiles(image: PixelBuffer): BandedStepProfiles;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared premultiplied-RGBA column/row colour-step profiles, computed once per image and reused
|
|
3
|
+
* across edge-coherence screening, edge-path DP, and lattice cut snapping.
|
|
4
|
+
*/
|
|
5
|
+
const STRIDE = 4;
|
|
6
|
+
const CHANNELS = 4;
|
|
7
|
+
const ALPHA_CHANNEL = 3;
|
|
8
|
+
const ALPHA_SCALE = 255;
|
|
9
|
+
export const STEP_PROFILE_BANDS = 8;
|
|
10
|
+
const cache = new WeakMap();
|
|
11
|
+
export function imageStepProfiles(image) {
|
|
12
|
+
const cached = cache.get(image);
|
|
13
|
+
if (cached)
|
|
14
|
+
return cached;
|
|
15
|
+
const { width, height, data } = image;
|
|
16
|
+
const bandsX = Array.from({ length: STEP_PROFILE_BANDS }, () => new Float64Array(width));
|
|
17
|
+
const bandsY = Array.from({ length: STEP_PROFILE_BANDS }, () => new Float64Array(height));
|
|
18
|
+
const columnBand = Int32Array.from({ length: width }, (_, column) => Math.floor((column * STEP_PROFILE_BANDS) / width));
|
|
19
|
+
const invAlpha = 1 / ALPHA_SCALE;
|
|
20
|
+
let above = new Float64Array(width * CHANNELS);
|
|
21
|
+
let current = new Float64Array(width * CHANNELS);
|
|
22
|
+
for (let row = 0; row < height; row++) {
|
|
23
|
+
const across = bandsX[Math.floor((row * STEP_PROFILE_BANDS) / height)];
|
|
24
|
+
for (let column = 0, o = row * width * STRIDE, at = 0; column < width; column++, o += STRIDE, at += CHANNELS) {
|
|
25
|
+
const a = data[o + ALPHA_CHANNEL];
|
|
26
|
+
if (a === 255) {
|
|
27
|
+
current[at] = data[o];
|
|
28
|
+
current[at + 1] = data[o + 1];
|
|
29
|
+
current[at + 2] = data[o + 2];
|
|
30
|
+
current[at + 3] = 255;
|
|
31
|
+
}
|
|
32
|
+
else if (a === 0) {
|
|
33
|
+
current[at] = 0;
|
|
34
|
+
current[at + 1] = 0;
|
|
35
|
+
current[at + 2] = 0;
|
|
36
|
+
current[at + 3] = 0;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const alpha = a * invAlpha;
|
|
40
|
+
current[at] = data[o] * alpha;
|
|
41
|
+
current[at + 1] = data[o + 1] * alpha;
|
|
42
|
+
current[at + 2] = data[o + 2] * alpha;
|
|
43
|
+
current[at + 3] = a;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (row > 0) {
|
|
47
|
+
for (let column = 0, at = 0; column < width; column++, at += CHANNELS) {
|
|
48
|
+
if (column > 0) {
|
|
49
|
+
across[column] +=
|
|
50
|
+
Math.abs(current[at] - current[at - 4]) +
|
|
51
|
+
Math.abs(current[at + 1] - current[at - 3]) +
|
|
52
|
+
Math.abs(current[at + 2] - current[at - 2]) +
|
|
53
|
+
Math.abs(current[at + 3] - current[at - 1]);
|
|
54
|
+
}
|
|
55
|
+
bandsY[columnBand[column]][row] +=
|
|
56
|
+
Math.abs(current[at] - above[at]) +
|
|
57
|
+
Math.abs(current[at + 1] - above[at + 1]) +
|
|
58
|
+
Math.abs(current[at + 2] - above[at + 2]) +
|
|
59
|
+
Math.abs(current[at + 3] - above[at + 3]);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
for (let column = 1, at = CHANNELS; column < width; column++, at += CHANNELS) {
|
|
64
|
+
across[column] +=
|
|
65
|
+
Math.abs(current[at] - current[at - 4]) +
|
|
66
|
+
Math.abs(current[at + 1] - current[at - 3]) +
|
|
67
|
+
Math.abs(current[at + 2] - current[at - 2]) +
|
|
68
|
+
Math.abs(current[at + 3] - current[at - 1]);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const tmp = above;
|
|
72
|
+
above = current;
|
|
73
|
+
current = tmp;
|
|
74
|
+
}
|
|
75
|
+
const x = new Float64Array(width);
|
|
76
|
+
const y = new Float64Array(height);
|
|
77
|
+
for (let b = 0; b < STEP_PROFILE_BANDS; b++) {
|
|
78
|
+
const bx = bandsX[b];
|
|
79
|
+
const by = bandsY[b];
|
|
80
|
+
for (let i = 0; i < width; i++)
|
|
81
|
+
x[i] += bx[i];
|
|
82
|
+
for (let i = 0; i < height; i++)
|
|
83
|
+
y[i] += by[i];
|
|
84
|
+
}
|
|
85
|
+
const result = { bandsX, bandsY, x, y };
|
|
86
|
+
cache.set(image, result);
|
|
87
|
+
return result;
|
|
88
|
+
}
|
package/dist/grid-strip-shift.js
CHANGED
|
@@ -32,36 +32,99 @@ const MIN_OFF_PITCH_CELLS = 3;
|
|
|
32
32
|
const MIN_OFF_PITCH = 1.5;
|
|
33
33
|
const TIE = 1e-9;
|
|
34
34
|
export function linePrefix(image, vertical) {
|
|
35
|
-
const { width, height, data, offset } = image;
|
|
35
|
+
const { width, height, data, offset, rowOffsets, colOffsets } = image;
|
|
36
36
|
const length = vertical ? height : width;
|
|
37
37
|
const lines = describedLines(vertical ? width : height, length);
|
|
38
|
-
const
|
|
38
|
+
const stride4 = CHANNELS + 1;
|
|
39
|
+
const rowStride = (length + 1) * stride4;
|
|
40
|
+
const sums = new Float64Array(lines.length * rowStride);
|
|
41
|
+
const invAlpha = 1 / ALPHA_SCALE;
|
|
42
|
+
const offsets = (vertical ? colOffsets : rowOffsets) ? new Int32Array(length) : null;
|
|
39
43
|
for (let k = 0; k < lines.length; k++) {
|
|
40
|
-
const line = lines[k], base = k *
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
44
|
+
const line = lines[k], base = k * rowStride;
|
|
45
|
+
let sr = 0, sg = 0, sb = 0, sq = 0;
|
|
46
|
+
if (offsets) {
|
|
47
|
+
if (vertical)
|
|
48
|
+
colOffsets(line, offsets);
|
|
49
|
+
else
|
|
50
|
+
rowOffsets(line, offsets);
|
|
51
|
+
for (let p = 0; p < length; p++) {
|
|
52
|
+
const o = offsets[p];
|
|
53
|
+
const a = data[o + CHANNELS];
|
|
54
|
+
if (a === 255) {
|
|
55
|
+
const red = data[o], green = data[o + 1], blue = data[o + 2];
|
|
56
|
+
sr += red;
|
|
57
|
+
sg += green;
|
|
58
|
+
sb += blue;
|
|
59
|
+
sq += red * red + green * green + blue * blue;
|
|
60
|
+
}
|
|
61
|
+
else if (a !== 0) {
|
|
62
|
+
const alpha = a * invAlpha;
|
|
63
|
+
const red = data[o] * alpha, green = data[o + 1] * alpha, blue = data[o + 2] * alpha;
|
|
64
|
+
sr += red;
|
|
65
|
+
sg += green;
|
|
66
|
+
sb += blue;
|
|
67
|
+
sq += red * red + green * green + blue * blue;
|
|
68
|
+
}
|
|
69
|
+
const at = base + (p + 1) * stride4;
|
|
70
|
+
sums[at] = sr;
|
|
71
|
+
sums[at + 1] = sg;
|
|
72
|
+
sums[at + 2] = sb;
|
|
73
|
+
sums[at + 3] = sq;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else if (!offset) {
|
|
77
|
+
const pStride = vertical ? width * STRIDE : STRIDE;
|
|
78
|
+
let o = (vertical ? line : line * width) * STRIDE;
|
|
79
|
+
for (let p = 0; p < length; p++, o += pStride) {
|
|
80
|
+
const a = data[o + CHANNELS];
|
|
81
|
+
if (a === 255) {
|
|
82
|
+
const red = data[o], green = data[o + 1], blue = data[o + 2];
|
|
83
|
+
sr += red;
|
|
84
|
+
sg += green;
|
|
85
|
+
sb += blue;
|
|
86
|
+
sq += red * red + green * green + blue * blue;
|
|
87
|
+
}
|
|
88
|
+
else if (a !== 0) {
|
|
89
|
+
const alpha = a * invAlpha;
|
|
90
|
+
const red = data[o] * alpha, green = data[o + 1] * alpha, blue = data[o + 2] * alpha;
|
|
91
|
+
sr += red;
|
|
92
|
+
sg += green;
|
|
93
|
+
sb += blue;
|
|
94
|
+
sq += red * red + green * green + blue * blue;
|
|
95
|
+
}
|
|
96
|
+
const at = base + (p + 1) * stride4;
|
|
97
|
+
sums[at] = sr;
|
|
98
|
+
sums[at + 1] = sg;
|
|
99
|
+
sums[at + 2] = sb;
|
|
100
|
+
sums[at + 3] = sq;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
for (let p = 0; p < length; p++) {
|
|
105
|
+
const o = vertical ? offset(line, p) : offset(p, line);
|
|
106
|
+
const a = data[o + CHANNELS];
|
|
107
|
+
if (a === 255) {
|
|
108
|
+
const red = data[o], green = data[o + 1], blue = data[o + 2];
|
|
109
|
+
sr += red;
|
|
110
|
+
sg += green;
|
|
111
|
+
sb += blue;
|
|
112
|
+
sq += red * red + green * green + blue * blue;
|
|
113
|
+
}
|
|
114
|
+
else if (a !== 0) {
|
|
115
|
+
const alpha = a * invAlpha;
|
|
116
|
+
const red = data[o] * alpha, green = data[o + 1] * alpha, blue = data[o + 2] * alpha;
|
|
117
|
+
sr += red;
|
|
118
|
+
sg += green;
|
|
119
|
+
sb += blue;
|
|
120
|
+
sq += red * red + green * green + blue * blue;
|
|
121
|
+
}
|
|
122
|
+
const at = base + (p + 1) * stride4;
|
|
123
|
+
sums[at] = sr;
|
|
124
|
+
sums[at + 1] = sg;
|
|
125
|
+
sums[at + 2] = sb;
|
|
126
|
+
sums[at + 3] = sq;
|
|
55
127
|
}
|
|
56
|
-
const red = data[o] * alpha, green = data[o + 1] * alpha, blue = data[o + 2] * alpha;
|
|
57
|
-
sums[at] = sums[previous] + red;
|
|
58
|
-
sums[at + 1] = sums[previous + 1] + green;
|
|
59
|
-
sums[at + 2] = sums[previous + 2] + blue;
|
|
60
|
-
let square = 0;
|
|
61
|
-
square += red * red;
|
|
62
|
-
square += green * green;
|
|
63
|
-
square += blue * blue;
|
|
64
|
-
sums[at + CHANNELS] = sums[previous + CHANNELS] + square;
|
|
65
128
|
}
|
|
66
129
|
}
|
|
67
130
|
return { length, lines, sums };
|
|
@@ -116,22 +179,77 @@ function stripShifts(prefix, cuts, across, anchor) {
|
|
|
116
179
|
const pitch = pitchOf(cuts);
|
|
117
180
|
const low = -Math.floor((pitch - 1) * HALF), high = Math.floor(pitch * HALF);
|
|
118
181
|
const shifts = [];
|
|
119
|
-
const
|
|
182
|
+
const lineStride = (length + 1) * (CHANNELS + 1);
|
|
183
|
+
let allInteger = true;
|
|
184
|
+
for (let i = 0; i <= cells; i++) {
|
|
185
|
+
if (!Number.isInteger(cuts[i])) {
|
|
186
|
+
allInteger = false;
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
const intPos = allInteger ? new Int32Array(cells + 1) : null;
|
|
191
|
+
const pos4 = allInteger ? new Int32Array(cells + 1) : null;
|
|
192
|
+
const invCounts = allInteger ? new Float64Array(cells) : null;
|
|
193
|
+
const positions = allInteger ? null : new Float64Array(cells + 1);
|
|
120
194
|
let energy = 0;
|
|
121
195
|
for (let k = 0; k + 1 < across.length; k++) {
|
|
122
196
|
const from = anchor ? Math.ceil(anchor[k] + ANTI_PHASE_FROM * pitch) : low, to = anchor ? Math.floor(anchor[k] + ANTI_PHASE_TO * pitch) : high;
|
|
123
197
|
const first = lineIndex(lines, across[k]), last = lineIndex(lines, across[k + 1]);
|
|
198
|
+
let stripTotalSq = 0;
|
|
199
|
+
if (allInteger) {
|
|
200
|
+
for (let line = first; line < last; line++)
|
|
201
|
+
stripTotalSq += sums[line * lineStride + length * 4 + 3];
|
|
202
|
+
}
|
|
124
203
|
let best = from, least = Infinity;
|
|
125
204
|
for (let shift = from; shift <= to; shift++) {
|
|
126
|
-
for (let i = 1; i <= cells; i++) {
|
|
127
|
-
const at = i === cells ? length : Math.max(0, Math.min(length, cuts[i] + shift));
|
|
128
|
-
positions[i] = Math.max(positions[i - 1], at);
|
|
129
|
-
}
|
|
130
205
|
let sum = 0;
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
206
|
+
if (allInteger) {
|
|
207
|
+
let prev = 0;
|
|
208
|
+
intPos[0] = 0;
|
|
209
|
+
pos4[0] = 0;
|
|
210
|
+
for (let i = 1; i <= cells; i++) {
|
|
211
|
+
let at = i === cells ? length : cuts[i] + shift;
|
|
212
|
+
if (at < 0)
|
|
213
|
+
at = 0;
|
|
214
|
+
else if (at > length)
|
|
215
|
+
at = length;
|
|
216
|
+
if (at < prev)
|
|
217
|
+
at = prev;
|
|
218
|
+
intPos[i] = at;
|
|
219
|
+
pos4[i] = at << 2;
|
|
220
|
+
const count = at - prev;
|
|
221
|
+
invCounts[i - 1] = count > 0 ? 1 / count : 0;
|
|
222
|
+
prev = at;
|
|
223
|
+
}
|
|
224
|
+
let explained = 0;
|
|
225
|
+
for (let line = first; line < last; line++) {
|
|
226
|
+
const base = line * lineStride;
|
|
227
|
+
if (sums[base + (length << 2) + 3] === 0)
|
|
228
|
+
continue;
|
|
229
|
+
for (let i = 0; i < cells; i++) {
|
|
230
|
+
const inv = invCounts[i];
|
|
231
|
+
if (inv === 0)
|
|
232
|
+
continue;
|
|
233
|
+
const s = base + pos4[i];
|
|
234
|
+
const e = base + pos4[i + 1];
|
|
235
|
+
const r = sums[e] - sums[s];
|
|
236
|
+
const g = sums[e + 1] - sums[s + 1];
|
|
237
|
+
const b = sums[e + 2] - sums[s + 2];
|
|
238
|
+
explained += (r * r + g * g + b * b) * inv;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
sum = stripTotalSq - explained;
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
for (let i = 1; i <= cells; i++) {
|
|
245
|
+
const at = i === cells ? length : Math.max(0, Math.min(length, cuts[i] + shift));
|
|
246
|
+
positions[i] = Math.max(positions[i - 1], at);
|
|
247
|
+
}
|
|
248
|
+
for (let line = first; line < last; line++) {
|
|
249
|
+
const base = line * lineStride;
|
|
250
|
+
for (let i = 1; i <= cells; i++)
|
|
251
|
+
sum += intervalEnergy(sums, base, positions[i - 1], positions[i]);
|
|
252
|
+
}
|
|
135
253
|
}
|
|
136
254
|
const nearer = !anchor && Math.abs(shift) < Math.abs(best);
|
|
137
255
|
if (sum < least - TIE || (sum <= least + TIE && nearer)) {
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,9 @@ export { snapPalette } from "./palette-snap.ts";
|
|
|
4
4
|
export { removeOrphanPixels, type RemoveOrphanPixelsOptions } from "./orphan-pixels.ts";
|
|
5
5
|
export { settlePixelAlpha } from "./pixel-alpha.ts";
|
|
6
6
|
export { estimateTilt, straighten, MAX_TILT_DEGREES } from "./deskew.ts";
|
|
7
|
+
export { kuwaharaPrepass } from "./kuwahara.ts";
|
|
8
|
+
export { resizeCrisp2Means } from "./resize-crisp.ts";
|
|
9
|
+
export { resizeLanczos } from "./resize-lanczos.ts";
|
|
7
10
|
export { resizeNearest } from "./resize.ts";
|
|
8
11
|
export { pixelateImage, pixelateImageAsync } from "./pixelate.ts";
|
|
9
12
|
export { PixelArtClassification, PixelationMethod, PixelationMode, PixelationAlgorithm, CellColorPicker, LineGuideMode, OutlinePrepass, PixelationDither, PaletteColorMode, PixelGridSamplingAxis, } from "./types.ts";
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,9 @@ export { snapPalette } from "./palette-snap.js";
|
|
|
4
4
|
export { removeOrphanPixels } from "./orphan-pixels.js";
|
|
5
5
|
export { settlePixelAlpha } from "./pixel-alpha.js";
|
|
6
6
|
export { estimateTilt, straighten, MAX_TILT_DEGREES } from "./deskew.js";
|
|
7
|
+
export { kuwaharaPrepass } from "./kuwahara.js";
|
|
8
|
+
export { resizeCrisp2Means } from "./resize-crisp.js";
|
|
9
|
+
export { resizeLanczos } from "./resize-lanczos.js";
|
|
7
10
|
export { resizeNearest } from "./resize.js";
|
|
8
11
|
export { pixelateImage, pixelateImageAsync } from "./pixelate.js";
|
|
9
12
|
export { PixelArtClassification, PixelationMethod, PixelationMode, PixelationAlgorithm, CellColorPicker, LineGuideMode, OutlinePrepass, PixelationDither, PaletteColorMode, PixelGridSamplingAxis, } from "./types.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type PixelBuffer } from "./base.ts";
|
|
2
|
+
import type { PixelationSize } from "./types.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Cel-shading simplification pre-pass for photo/illustration pixelation:
|
|
5
|
+
* downscales to a 4× target working grid, runs detail-protected Anisotropic Kuwahara to flatten
|
|
6
|
+
* hatching/grain while preserving 1-2 px facial/specular landmarks, then applies neutral anti-halo
|
|
7
|
+
* OKLab clarity. Already-crisp flat pixel art is passed through untouched.
|
|
8
|
+
*/
|
|
9
|
+
export declare function kuwaharaPrepass(image: PixelBuffer, target: PixelationSize): PixelBuffer;
|