@xprite/pixelate 0.2.1 → 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.
- package/dist/grid-arbitrate.js +8 -1
- package/dist/grid-phase-field.js +128 -48
- package/package.json +1 -1
package/dist/grid-arbitrate.js
CHANGED
|
@@ -212,9 +212,16 @@ function applyShiftedReadings(base, grid, cutsX, cutsY, shifted, bounds) {
|
|
|
212
212
|
for (const { shear, frame, twin, chance } of shifted()) {
|
|
213
213
|
const straight = frame();
|
|
214
214
|
const cost = shear.cost / straight.samples;
|
|
215
|
+
const length = descriptionLength(straight, cuts) + cost;
|
|
216
|
+
// If straightening + paying shift cost fails to improve description length over the
|
|
217
|
+
// incumbent reading AND cannot lower passthrough, skip expensive anti-phase twin evaluation.
|
|
218
|
+
if (bounds && length >= best.length && length >= bounds.passthrough) {
|
|
219
|
+
exact = false;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
215
222
|
const reading = {
|
|
216
223
|
cuts,
|
|
217
|
-
length
|
|
224
|
+
length,
|
|
218
225
|
twin: descriptionLength(twin(), cuts) + cost,
|
|
219
226
|
chance: 0,
|
|
220
227
|
frame,
|
package/dist/grid-phase-field.js
CHANGED
|
@@ -67,11 +67,13 @@ function dropTable(prefix) {
|
|
|
67
67
|
neighbourGreen = sums[neighbourAt + 1] - sums[neighbourBefore + 1];
|
|
68
68
|
neighbourBlue = sums[neighbourAt + 2] - sums[neighbourBefore + 2];
|
|
69
69
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
if (red === neighbourRed && green === neighbourGreen && blue === neighbourBlue) {
|
|
71
|
+
drops[row + p + 1] = drops[row + p];
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
const dr = red - neighbourRed, dg = green - neighbourGreen, db = blue - neighbourBlue;
|
|
75
|
+
drops[row + p + 1] = drops[row + p] + colourStepCost(dr * dr + dg * dg + db * db);
|
|
76
|
+
}
|
|
75
77
|
previousRed = red;
|
|
76
78
|
previousGreen = green;
|
|
77
79
|
previousBlue = blue;
|
|
@@ -125,7 +127,8 @@ function energyTable(prefix, cuts, first, last) {
|
|
|
125
127
|
}
|
|
126
128
|
ids[i] = id;
|
|
127
129
|
}
|
|
128
|
-
const
|
|
130
|
+
const uniqueCount = uniqueFroms.length;
|
|
131
|
+
const intervalEnergies = new Float64Array(uniqueCount);
|
|
129
132
|
// Preserve non-integer typed-array lookup behaviour for fractional cell boundaries.
|
|
130
133
|
const zeroLineSafe = uniqueFroms.every((from, i) => Number.isInteger(from) &&
|
|
131
134
|
Number.isInteger(uniqueTos[i]) &&
|
|
@@ -133,18 +136,51 @@ function energyTable(prefix, cuts, first, last) {
|
|
|
133
136
|
from <= length &&
|
|
134
137
|
uniqueTos[i] >= 0 &&
|
|
135
138
|
uniqueTos[i] <= length);
|
|
139
|
+
const intStart4 = zeroLineSafe ? new Int32Array(uniqueCount) : null;
|
|
140
|
+
const intEnd4 = zeroLineSafe ? new Int32Array(uniqueCount) : null;
|
|
141
|
+
const intInvCount = zeroLineSafe ? new Float64Array(uniqueCount) : null;
|
|
142
|
+
if (zeroLineSafe) {
|
|
143
|
+
for (let i = 0; i < uniqueCount; i++) {
|
|
144
|
+
const f = uniqueFroms[i];
|
|
145
|
+
const t = uniqueTos[i];
|
|
146
|
+
intStart4[i] = f << 2;
|
|
147
|
+
intEnd4[i] = t << 2;
|
|
148
|
+
intInvCount[i] = t > f ? 1 / (t - f) : 0;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const lineStride = (length + 1) * (CHANNELS + 1);
|
|
136
152
|
for (let k = 0; k < lines.length; k++) {
|
|
137
153
|
// linePrefix accumulates nonnegative squared premultiplied RGB; a zero sum codes a zero line.
|
|
138
|
-
if (zeroLineSafe && line[(k + 1) *
|
|
154
|
+
if (zeroLineSafe && line[(k + 1) * lineStride - 1] === 0)
|
|
139
155
|
continue;
|
|
140
|
-
const base = k *
|
|
141
|
-
|
|
142
|
-
|
|
156
|
+
const base = k * lineStride;
|
|
157
|
+
if (zeroLineSafe) {
|
|
158
|
+
for (let i = 0; i < uniqueCount; i++) {
|
|
159
|
+
const inv = intInvCount[i];
|
|
160
|
+
if (inv === 0) {
|
|
161
|
+
intervalEnergies[i] = 0;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
const s = base + intStart4[i];
|
|
165
|
+
const e = base + intEnd4[i];
|
|
166
|
+
const sq = line[e + 3] - line[s + 3];
|
|
167
|
+
const r = line[e] - line[s];
|
|
168
|
+
const g = line[e + 1] - line[s + 1];
|
|
169
|
+
const b = line[e + 2] - line[s + 2];
|
|
170
|
+
intervalEnergies[i] =
|
|
171
|
+
sq === 0 && r === 0 && g === 0 && b === 0 ? 0 : sq - (r * r + g * g + b * b) * inv;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
for (let i = 0; i < uniqueCount; i++)
|
|
176
|
+
intervalEnergies[i] = intervalEnergy(line, base, uniqueFroms[i], uniqueTos[i]);
|
|
177
|
+
}
|
|
143
178
|
for (let j = 0; j < span; j++) {
|
|
144
179
|
const row = (k * span + j) * bandStride, bounds = j * cells;
|
|
145
180
|
let total = 0, cell = 0;
|
|
146
181
|
for (let slot = 1; slot < bandStride; slot++) {
|
|
147
|
-
|
|
182
|
+
const endCell = boundaries[slot];
|
|
183
|
+
for (; cell < endCell; cell++)
|
|
148
184
|
total += intervalEnergies[ids[bounds + cell]];
|
|
149
185
|
sums[row + slot] = total;
|
|
150
186
|
}
|
|
@@ -181,49 +217,73 @@ function dropped(table, k, from, to) {
|
|
|
181
217
|
* line's shifts. Shifts a whole cell apart cut the lines alike, so on equal cuts the path keeps to
|
|
182
218
|
* the smaller shift.
|
|
183
219
|
*/
|
|
184
|
-
function bandPath(table, from, to, scale, change,
|
|
185
|
-
const lines = table.prefix.lines.length, { span, first } = table;
|
|
220
|
+
function bandPath(table, from, to, scale, change, winLow, winHigh, anchorBand, antiFromOffset, antiToOffset, startCut, beforeBand, isLastBand) {
|
|
221
|
+
const lines = table.prefix.lines.length, lineLength = table.prefix.length, dropStride = lineLength + 1, { span, first, sums, bandStride, drops } = table;
|
|
222
|
+
const fromSlot = table.bandSlots[from], toSlot = table.bandSlots[to];
|
|
186
223
|
const back = new Int32Array(lines * span);
|
|
187
224
|
let previous = new Float64Array(span), current = new Float64Array(span), best = Infinity, bestAt = -1;
|
|
188
225
|
const tieBias = Float64Array.from({ length: span }, (_, j) => Math.abs(first + j) * TIE);
|
|
226
|
+
const stepCost = change.step;
|
|
227
|
+
const jumpDelta = change.jump;
|
|
189
228
|
for (let k = 0; k < lines; k++) {
|
|
190
|
-
const
|
|
229
|
+
const low = anchorBand ? Math.ceil(anchorBand[k] + antiFromOffset) : winLow;
|
|
230
|
+
const high = anchorBand ? Math.floor(anchorBand[k] + antiToOffset) : winHigh;
|
|
231
|
+
const dropRow = k * dropStride;
|
|
232
|
+
const dropFrom = beforeBand ? startCut + beforeBand[k] : 0;
|
|
233
|
+
const clampedFrom = dropFrom < 0 ? 0 : dropFrom > lineLength ? lineLength : dropFrom;
|
|
234
|
+
const baseDropFrom = drops[dropRow + clampedFrom];
|
|
235
|
+
const lineEndDrop = drops[dropRow + lineLength];
|
|
191
236
|
let nextBest = Infinity, nextBestAt = -1;
|
|
192
|
-
const jumpCost = best +
|
|
237
|
+
const jumpCost = best + jumpDelta;
|
|
238
|
+
const kSpan = k * span;
|
|
193
239
|
for (let j = 0; j < span; j++) {
|
|
194
240
|
const shift = first + j;
|
|
195
241
|
if (shift < low || shift > high) {
|
|
196
242
|
current[j] = Infinity;
|
|
197
243
|
continue;
|
|
198
244
|
}
|
|
199
|
-
const
|
|
245
|
+
const row = (kSpan + j) * bandStride;
|
|
246
|
+
const cellE = sums[row + toSlot] - sums[row + fromSlot];
|
|
247
|
+
let dCost = 0;
|
|
248
|
+
const dropTo = startCut + shift;
|
|
249
|
+
const clampedTo = dropTo < 0 ? 0 : dropTo > lineLength ? lineLength : dropTo;
|
|
250
|
+
if (clampedTo > clampedFrom)
|
|
251
|
+
dCost += drops[dropRow + clampedTo] - baseDropFrom;
|
|
252
|
+
if (isLastBand && shift < 0) {
|
|
253
|
+
const tailLow = lineLength + shift;
|
|
254
|
+
const clampedTail = tailLow < 0 ? 0 : tailLow > lineLength ? lineLength : tailLow;
|
|
255
|
+
if (lineLength > clampedTail)
|
|
256
|
+
dCost += lineEndDrop - drops[dropRow + clampedTail];
|
|
257
|
+
}
|
|
258
|
+
const energy = cellE * scale + dCost + tieBias[j];
|
|
200
259
|
if (k === 0) {
|
|
201
260
|
current[j] = energy;
|
|
202
|
-
if (
|
|
203
|
-
nextBest =
|
|
261
|
+
if (energy < nextBest) {
|
|
262
|
+
nextBest = energy;
|
|
204
263
|
nextBestAt = j;
|
|
205
264
|
}
|
|
206
265
|
continue;
|
|
207
266
|
}
|
|
208
267
|
let cost = previous[j], source = j;
|
|
209
|
-
if (j > 0 && previous[j - 1] +
|
|
210
|
-
cost = previous[j - 1] +
|
|
268
|
+
if (j > 0 && previous[j - 1] + stepCost < cost) {
|
|
269
|
+
cost = previous[j - 1] + stepCost;
|
|
211
270
|
source = j - 1;
|
|
212
271
|
}
|
|
213
|
-
if (j + 1 < span && previous[j + 1] +
|
|
214
|
-
cost = previous[j + 1] +
|
|
272
|
+
if (j + 1 < span && previous[j + 1] + stepCost < cost) {
|
|
273
|
+
cost = previous[j + 1] + stepCost;
|
|
215
274
|
source = j + 1;
|
|
216
275
|
}
|
|
217
276
|
if (jumpCost < cost) {
|
|
218
277
|
cost = jumpCost;
|
|
219
278
|
source = bestAt;
|
|
220
279
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
280
|
+
const total = cost + energy;
|
|
281
|
+
current[j] = total;
|
|
282
|
+
if (total < nextBest) {
|
|
283
|
+
nextBest = total;
|
|
224
284
|
nextBestAt = j;
|
|
225
285
|
}
|
|
226
|
-
back[
|
|
286
|
+
back[kSpan + j] = source;
|
|
227
287
|
}
|
|
228
288
|
const scratch = previous;
|
|
229
289
|
previous = current;
|
|
@@ -238,13 +298,17 @@ function bandPath(table, from, to, scale, change, window, drop) {
|
|
|
238
298
|
const shifts = new Int32Array(lines);
|
|
239
299
|
let energy = 0, cost = 0;
|
|
240
300
|
for (let k = lines - 1; k >= 0; k--) {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
301
|
+
const shift = first + at;
|
|
302
|
+
shifts[k] = shift;
|
|
303
|
+
const row = (k * span + at) * bandStride;
|
|
304
|
+
energy += sums[row + toSlot] - sums[row + fromSlot];
|
|
305
|
+
cost +=
|
|
306
|
+
dropped(table, k, beforeBand ? startCut + beforeBand[k] : 0, startCut + shift) +
|
|
307
|
+
(isLastBand && shift < 0 ? dropped(table, k, lineLength + shift, lineLength) : 0);
|
|
244
308
|
if (k > 0) {
|
|
245
309
|
const before = back[k * span + at];
|
|
246
310
|
if (before !== at)
|
|
247
|
-
cost += Math.abs(before - at) === 1 ?
|
|
311
|
+
cost += Math.abs(before - at) === 1 ? stepCost : jumpDelta;
|
|
248
312
|
at = before;
|
|
249
313
|
}
|
|
250
314
|
}
|
|
@@ -268,30 +332,44 @@ function bandsOf(cells, count) {
|
|
|
268
332
|
*/
|
|
269
333
|
function splitField(table, bands, variance, anchor) {
|
|
270
334
|
const { low, high, pitch } = table.range;
|
|
271
|
-
const { length } = table.prefix;
|
|
272
335
|
const options = high - low + 1;
|
|
273
336
|
const line = Math.log(Math.max(2, table.prefix.lines.length)) + Math.LN2;
|
|
274
337
|
const change = { step: line + Math.LN2, jump: line + Math.log(options - 1) };
|
|
275
338
|
const shifts = [];
|
|
276
|
-
const
|
|
339
|
+
const antiFromOffset = ANTI_PHASE_FROM * pitch;
|
|
340
|
+
const antiToOffset = ANTI_PHASE_TO * pitch;
|
|
341
|
+
const scale = 1 / (2 * variance);
|
|
277
342
|
let energy = 0, cost = 0;
|
|
278
343
|
for (let b = 0; b + 1 < bands.length; b++) {
|
|
279
|
-
const
|
|
280
|
-
|
|
281
|
-
Math.ceil(anchor.shifts[b][k] + ANTI_PHASE_FROM * pitch),
|
|
282
|
-
Math.floor(anchor.shifts[b][k] + ANTI_PHASE_TO * pitch),
|
|
283
|
-
]
|
|
284
|
-
: () => stationaryWindow;
|
|
285
|
-
const start = b === 0 ? 0 : table.cuts[bands[b]], before = shifts[b - 1], last = b + 2 === bands.length;
|
|
286
|
-
const drop = (k, shift) => dropped(table, k, before ? start + before[k] : 0, start + shift) +
|
|
287
|
-
(last && shift < 0 ? dropped(table, k, length + shift, length) : 0);
|
|
288
|
-
const path = bandPath(table, bands[b], bands[b + 1], 1 / (2 * variance), change, window, drop);
|
|
344
|
+
const start = b === 0 ? 0 : table.cuts[bands[b]], before = b > 0 ? shifts[b - 1] : null, last = b + 2 === bands.length;
|
|
345
|
+
const path = bandPath(table, bands[b], bands[b + 1], scale, change, low, high, anchor ? anchor.shifts[b] : null, antiFromOffset, antiToOffset, start, before, last);
|
|
289
346
|
shifts.push(path.shifts);
|
|
290
347
|
energy += path.energy;
|
|
291
348
|
cost += Math.log(options) + path.cost;
|
|
292
349
|
}
|
|
293
350
|
return { bands: [...bands], shifts, energy, cost };
|
|
294
351
|
}
|
|
352
|
+
function forwardTable(prefix, cuts) {
|
|
353
|
+
const range = shiftRange(cuts);
|
|
354
|
+
return { ...energyTable(prefix, cuts, range.low, range.high), range };
|
|
355
|
+
}
|
|
356
|
+
function antiTable(prefix, cuts, anchor) {
|
|
357
|
+
const range = shiftRange(cuts);
|
|
358
|
+
let minShift = Infinity;
|
|
359
|
+
let maxShift = -Infinity;
|
|
360
|
+
for (const band of anchor.shifts) {
|
|
361
|
+
for (let k = 0; k < band.length; k++) {
|
|
362
|
+
const s = band[k];
|
|
363
|
+
if (s < minShift)
|
|
364
|
+
minShift = s;
|
|
365
|
+
if (s > maxShift)
|
|
366
|
+
maxShift = s;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
const first = Math.ceil(minShift + ANTI_PHASE_FROM * range.pitch);
|
|
370
|
+
const last = Math.floor(maxShift + ANTI_PHASE_TO * range.pitch);
|
|
371
|
+
return { ...energyTable(prefix, cuts, first, Math.max(first, last)), range };
|
|
372
|
+
}
|
|
295
373
|
function rangedTable(prefix, cuts) {
|
|
296
374
|
const range = shiftRange(cuts);
|
|
297
375
|
const last = range.high + Math.floor(ANTI_PHASE_TO * range.pitch);
|
|
@@ -361,7 +439,7 @@ function columnsOf(strips, cutsX) {
|
|
|
361
439
|
const cached = lastColumns.get(strips);
|
|
362
440
|
if (cached?.key === key)
|
|
363
441
|
return cached;
|
|
364
|
-
const table =
|
|
442
|
+
const table = forwardTable(strips.prefix(false), cutsX);
|
|
365
443
|
const field = bestField(table);
|
|
366
444
|
const entry = { key, table, field, ...columnView(strips, cutsX, table, field) };
|
|
367
445
|
lastColumns.set(strips, entry);
|
|
@@ -374,10 +452,12 @@ function columnsOf(strips, cutsX) {
|
|
|
374
452
|
function fieldTwin(strips, cutsX, cutsY, columnTable, columns, rows) {
|
|
375
453
|
return () => {
|
|
376
454
|
const cached = lastColumns.get(strips);
|
|
377
|
-
const
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
455
|
+
const buildAntiColumns = () => {
|
|
456
|
+
const colAntiTable = antiTable(strips.prefix(false), cutsX, columns);
|
|
457
|
+
return columnView(strips, cutsX, colAntiTable, antiField(colAntiTable, columns));
|
|
458
|
+
};
|
|
459
|
+
const anti = cached?.table === columnTable ? (cached.anti ??= buildAntiColumns()) : buildAntiColumns();
|
|
460
|
+
const rowTable = antiTable(anti.rowPrefix, cutsY, rows);
|
|
381
461
|
const rowField = antiField(rowTable, rows);
|
|
382
462
|
return moved(rowField)
|
|
383
463
|
? unfield(anti.once, true, cutsY, rowTable.prefix.lines, rowField)
|
|
@@ -387,7 +467,7 @@ function fieldTwin(strips, cutsX, cutsY, columnTable, columns, rows) {
|
|
|
387
467
|
/** The grid read through phase fields, or null when no line of any band moves. */
|
|
388
468
|
export function phaseField(strips, cutsX, cutsY) {
|
|
389
469
|
const { table: columnTable, field: columns, once, rowPrefix } = columnsOf(strips, cutsX);
|
|
390
|
-
const rowTable =
|
|
470
|
+
const rowTable = forwardTable(rowPrefix, cutsY);
|
|
391
471
|
const rows = bestField(rowTable);
|
|
392
472
|
const straight = moved(rows) ? unfield(once, true, cutsY, rowTable.prefix.lines, rows) : once;
|
|
393
473
|
if (!moved(columns) && !moved(rows))
|