@xprite/pixelate 0.2.0 → 0.2.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.
@@ -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
+ }
@@ -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 sums = new Float64Array(lines.length * (length + 1) * (CHANNELS + 1));
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 * (length + 1) * (CHANNELS + 1);
41
- for (let p = 0; p < length; p++) {
42
- const o = offset
43
- ? vertical
44
- ? offset(line, p)
45
- : offset(p, line)
46
- : (vertical ? p * width + line : line * width + p) * STRIDE;
47
- const alpha = data[o + CHANNELS] / ALPHA_SCALE;
48
- const at = base + (p + 1) * (CHANNELS + 1), previous = base + p * (CHANNELS + 1);
49
- if (alpha === 0) {
50
- sums[at] = sums[previous];
51
- sums[at + 1] = sums[previous + 1];
52
- sums[at + 2] = sums[previous + 2];
53
- sums[at + CHANNELS] = sums[previous + CHANNELS];
54
- continue;
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 positions = new Float64Array(cells + 1);
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
- for (let line = first; line < last; line++) {
132
- const base = line * (length + 1) * (CHANNELS + 1);
133
- for (let i = 1; i <= cells; i++)
134
- sum += intervalEnergy(sums, base, positions[i - 1], positions[i]);
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xprite/pixelate",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Recover the real pixel grid of AI-generated and upscaled pixel art, and turn images into pixel art.",
5
5
  "keywords": [
6
6
  "ai-art",