@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.
- package/dist/deskew.js +28 -15
- package/dist/fft-autocorr.d.ts +12 -0
- package/dist/fft-autocorr.js +116 -0
- package/dist/grid-arbitrate.d.ts +8 -0
- package/dist/grid-arbitrate.js +227 -66
- 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-phase-field.js +128 -48
- package/dist/grid-pitch-lattice.js +21 -9
- package/dist/grid-pitch-search.js +38 -18
- 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/package.json +1 -1
|
@@ -128,13 +128,24 @@ function contentOf(image) {
|
|
|
128
128
|
bottom--;
|
|
129
129
|
if (right - left === width && bottom - top === height)
|
|
130
130
|
return { content: image, left, top };
|
|
131
|
+
const cropWidth = right - left;
|
|
132
|
+
const parentRowOffsets = image.rowOffsets;
|
|
133
|
+
let cropRowOffsets;
|
|
134
|
+
if (parentRowOffsets) {
|
|
135
|
+
const fullRow = new Int32Array(width);
|
|
136
|
+
cropRowOffsets = (y, out) => {
|
|
137
|
+
parentRowOffsets(y + top, fullRow);
|
|
138
|
+
out.set(fullRow.subarray(left, left + cropWidth));
|
|
139
|
+
};
|
|
140
|
+
}
|
|
131
141
|
const content = {
|
|
132
|
-
width:
|
|
142
|
+
width: cropWidth,
|
|
133
143
|
height: bottom - top,
|
|
134
144
|
data,
|
|
135
145
|
offset: offset
|
|
136
146
|
? (x, y) => offset(x + left, y + top)
|
|
137
147
|
: (x, y) => ((y + top) * width + x + left) * STRIDE,
|
|
148
|
+
rowOffsets: cropRowOffsets,
|
|
138
149
|
};
|
|
139
150
|
return { content, left, top };
|
|
140
151
|
}
|
|
@@ -154,17 +165,74 @@ export function describedLines(lines, length) {
|
|
|
154
165
|
}
|
|
155
166
|
export function describeImage(input) {
|
|
156
167
|
const { content: image, left, top } = contentOf(input);
|
|
157
|
-
const { width, height, data, offset } = image;
|
|
168
|
+
const { width, height, data, offset, rowOffsets } = image;
|
|
158
169
|
const lines = describedLines(height, width);
|
|
159
170
|
const count = lines.length;
|
|
160
171
|
const values = new Float64Array(count * width * CHANNELS);
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
172
|
+
const invAlpha = 1 / ALPHA_SCALE;
|
|
173
|
+
if (rowOffsets) {
|
|
174
|
+
const offsets = new Int32Array(width);
|
|
175
|
+
for (let r = 0; r < count; r++) {
|
|
176
|
+
rowOffsets(lines[r], offsets);
|
|
177
|
+
let v = r * width * CHANNELS;
|
|
178
|
+
for (let x = 0; x < width; x++, v += CHANNELS) {
|
|
179
|
+
const o = offsets[x];
|
|
180
|
+
const a = data[o + ALPHA_CHANNEL];
|
|
181
|
+
if (a === 255) {
|
|
182
|
+
values[v] = data[o];
|
|
183
|
+
values[v + 1] = data[o + 1];
|
|
184
|
+
values[v + 2] = data[o + 2];
|
|
185
|
+
}
|
|
186
|
+
else if (a !== 0) {
|
|
187
|
+
const alpha = a * invAlpha;
|
|
188
|
+
values[v] = data[o] * alpha;
|
|
189
|
+
values[v + 1] = data[o + 1] * alpha;
|
|
190
|
+
values[v + 2] = data[o + 2] * alpha;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
167
193
|
}
|
|
194
|
+
}
|
|
195
|
+
else if (!offset) {
|
|
196
|
+
for (let r = 0; r < count; r++) {
|
|
197
|
+
let o = lines[r] * width * STRIDE;
|
|
198
|
+
let v = r * width * CHANNELS;
|
|
199
|
+
for (let x = 0; x < width; x++, o += STRIDE, v += CHANNELS) {
|
|
200
|
+
const a = data[o + ALPHA_CHANNEL];
|
|
201
|
+
if (a === 255) {
|
|
202
|
+
values[v] = data[o];
|
|
203
|
+
values[v + 1] = data[o + 1];
|
|
204
|
+
values[v + 2] = data[o + 2];
|
|
205
|
+
}
|
|
206
|
+
else if (a !== 0) {
|
|
207
|
+
const alpha = a * invAlpha;
|
|
208
|
+
values[v] = data[o] * alpha;
|
|
209
|
+
values[v + 1] = data[o + 1] * alpha;
|
|
210
|
+
values[v + 2] = data[o + 2] * alpha;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
for (let r = 0; r < count; r++) {
|
|
217
|
+
const y = lines[r];
|
|
218
|
+
let v = r * width * CHANNELS;
|
|
219
|
+
for (let x = 0; x < width; x++, v += CHANNELS) {
|
|
220
|
+
const o = offset(x, y);
|
|
221
|
+
const a = data[o + ALPHA_CHANNEL];
|
|
222
|
+
if (a === 255) {
|
|
223
|
+
values[v] = data[o];
|
|
224
|
+
values[v + 1] = data[o + 1];
|
|
225
|
+
values[v + 2] = data[o + 2];
|
|
226
|
+
}
|
|
227
|
+
else if (a !== 0) {
|
|
228
|
+
const alpha = a * invAlpha;
|
|
229
|
+
values[v] = data[o] * alpha;
|
|
230
|
+
values[v + 1] = data[o + 1] * alpha;
|
|
231
|
+
values[v + 2] = data[o + 2] * alpha;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
168
236
|
const samples = count * width * CHANNELS;
|
|
169
237
|
return { content: image, left, top, lines, values, samples, parameter: Math.log(samples) / 2 };
|
|
170
238
|
}
|
|
@@ -253,73 +321,107 @@ function blurWeights(cuts, cells, sigma) {
|
|
|
253
321
|
function cellMoments(described, cutsX, cutsY) {
|
|
254
322
|
const { content: image, lines, values } = described;
|
|
255
323
|
const { width } = image;
|
|
324
|
+
const rowStride = width * CHANNELS;
|
|
256
325
|
const across = cutsX.length - 1, down = cutsY.length - 1, cells = across * down;
|
|
257
326
|
const counts = new Float64Array(cells), means = new Float64Array(cells * CHANNELS), slopes = new Float64Array(cells);
|
|
327
|
+
// Precompute per-column horizontal coordinate sums (Cartesian product -> mxy === 0).
|
|
328
|
+
const colLefts = new Int32Array(across);
|
|
329
|
+
const colRights = new Int32Array(across);
|
|
330
|
+
const colSx1 = new Float64Array(across);
|
|
331
|
+
const colMxx1 = new Float64Array(across);
|
|
332
|
+
for (let i = 0; i < across; i++) {
|
|
333
|
+
const left = Math.trunc(cutsX[i]);
|
|
334
|
+
const right = Math.trunc(cutsX[i + 1]);
|
|
335
|
+
colLefts[i] = left;
|
|
336
|
+
colRights[i] = right;
|
|
337
|
+
const cols = right - left;
|
|
338
|
+
if (cols <= 0)
|
|
339
|
+
continue;
|
|
340
|
+
const dx = left - cutsX[i];
|
|
341
|
+
const sx1 = cols * dx + (cols * (cols - 1)) * 0.5;
|
|
342
|
+
const sxx1 = cols * dx * dx + dx * cols * (cols - 1) + ((cols - 1) * cols * (2 * cols - 1)) / 6;
|
|
343
|
+
colSx1[i] = sx1;
|
|
344
|
+
colMxx1[i] = sxx1 - (sx1 * sx1) / cols;
|
|
345
|
+
}
|
|
258
346
|
let flat = 0, first = 0;
|
|
259
347
|
for (let j = 0; j < down; j++) {
|
|
348
|
+
const cutY = cutsY[j];
|
|
260
349
|
const bottom = Math.trunc(cutsY[j + 1]);
|
|
261
350
|
let last = first;
|
|
262
351
|
while (last < lines.length && lines[last] < bottom)
|
|
263
352
|
last++;
|
|
353
|
+
const rowsInCell = last - first;
|
|
354
|
+
if (rowsInCell <= 0) {
|
|
355
|
+
first = last;
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
let sy1 = 0, syy1 = 0;
|
|
359
|
+
for (let r = first; r < last; r++) {
|
|
360
|
+
const ly = lines[r] - cutY;
|
|
361
|
+
sy1 += ly;
|
|
362
|
+
syy1 += ly * ly;
|
|
363
|
+
}
|
|
364
|
+
const myy1 = syy1 - (sy1 * sy1) / rowsInCell;
|
|
365
|
+
const meanY = sy1 / rowsInCell;
|
|
264
366
|
for (let i = 0; i < across; i++) {
|
|
265
|
-
const
|
|
266
|
-
|
|
367
|
+
const left = colLefts[i], right = colRights[i];
|
|
368
|
+
const cols = right - left;
|
|
369
|
+
if (cols <= 0)
|
|
370
|
+
continue;
|
|
371
|
+
const cell = j * across + i;
|
|
372
|
+
const cutX = cutsX[i];
|
|
373
|
+
const n = rowsInCell * cols;
|
|
374
|
+
let squares = 0, sr = 0, sg = 0, sb = 0, srx = 0, sgx = 0, sbx = 0, sry = 0, sgy = 0, sby = 0;
|
|
267
375
|
for (let r = first; r < last; r++) {
|
|
268
|
-
const ly = lines[r] -
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
sr += red;
|
|
376
|
+
const ly = lines[r] - cutY;
|
|
377
|
+
let at = r * rowStride + left * CHANNELS;
|
|
378
|
+
let rowR = 0, rowG = 0, rowB = 0;
|
|
379
|
+
for (let x = left; x < right; x++, at += CHANNELS) {
|
|
380
|
+
const lx = x - cutX;
|
|
381
|
+
const red = values[at], green = values[at + 1], blue = values[at + 2];
|
|
382
|
+
rowR += red;
|
|
383
|
+
rowG += green;
|
|
384
|
+
rowB += blue;
|
|
278
385
|
srx += red * lx;
|
|
279
|
-
sry += red * ly;
|
|
280
|
-
squares += red * red;
|
|
281
|
-
sg += green;
|
|
282
386
|
sgx += green * lx;
|
|
283
|
-
sgy += green * ly;
|
|
284
|
-
squares += green * green;
|
|
285
|
-
sb += blue;
|
|
286
387
|
sbx += blue * lx;
|
|
287
|
-
|
|
288
|
-
squares += blue * blue;
|
|
388
|
+
squares += red * red + green * green + blue * blue;
|
|
289
389
|
}
|
|
390
|
+
sr += rowR;
|
|
391
|
+
sg += rowG;
|
|
392
|
+
sb += rowB;
|
|
393
|
+
sry += rowR * ly;
|
|
394
|
+
sgy += rowG * ly;
|
|
395
|
+
sby += rowB * ly;
|
|
290
396
|
}
|
|
291
|
-
if (!n)
|
|
292
|
-
continue;
|
|
293
397
|
counts[cell] = n;
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
const
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
const
|
|
308
|
-
if (det > SINGULAR && n >= MIN_PLANAR_AREA)
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
removed
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
else if (myy > SINGULAR && n >= MIN_PLANAR_PIXELS)
|
|
322
|
-
removed += (gby * gby) / myy;
|
|
398
|
+
const invN = 1 / n;
|
|
399
|
+
const mxx = rowsInCell * colMxx1[i];
|
|
400
|
+
const myy = cols * myy1;
|
|
401
|
+
const det = mxx * myy;
|
|
402
|
+
const meanX = colSx1[i] / cols;
|
|
403
|
+
const c3 = cell * CHANNELS;
|
|
404
|
+
means[c3] = sr * invN;
|
|
405
|
+
means[c3 + 1] = sg * invN;
|
|
406
|
+
means[c3 + 2] = sb * invN;
|
|
407
|
+
let residual = squares - (sr * sr + sg * sg + sb * sb) * invN;
|
|
408
|
+
let removed = 0;
|
|
409
|
+
const grx = srx - meanX * sr, gry = sry - meanY * sr;
|
|
410
|
+
const ggx = sgx - meanX * sg, ggy = sgy - meanY * sg;
|
|
411
|
+
const gbx = sbx - meanX * sb, gby = sby - meanY * sb;
|
|
412
|
+
if (det > SINGULAR && n >= MIN_PLANAR_AREA) {
|
|
413
|
+
const invMxx = 1 / mxx;
|
|
414
|
+
const invMyy = 1 / myy;
|
|
415
|
+
removed =
|
|
416
|
+
(grx * grx + ggx * ggx + gbx * gbx) * invMxx +
|
|
417
|
+
(gry * gry + ggy * ggy + gby * gby) * invMyy;
|
|
418
|
+
}
|
|
419
|
+
else if (mxx > SINGULAR && n >= MIN_PLANAR_PIXELS) {
|
|
420
|
+
removed = (grx * grx + ggx * ggx + gbx * gbx) / mxx;
|
|
421
|
+
}
|
|
422
|
+
else if (myy > SINGULAR && n >= MIN_PLANAR_PIXELS) {
|
|
423
|
+
removed = (gry * gry + ggy * ggy + gby * gby) / myy;
|
|
424
|
+
}
|
|
323
425
|
residual = Math.max(0, residual);
|
|
324
426
|
flat += residual;
|
|
325
427
|
slopes[cell] = Math.max(0, Math.min(removed, residual));
|
|
@@ -374,31 +476,62 @@ function bilinearError(described, cutsX, cutsY, bound) {
|
|
|
374
476
|
const cx = centres(cutsX, width), cy = centres(cutsY, height);
|
|
375
477
|
const finiteWeights = cx.weight.every(Number.isFinite) && cy.weight.every(Number.isFinite);
|
|
376
478
|
const colours = new Float64Array(across * down * CHANNELS);
|
|
479
|
+
const across3 = across * CHANNELS;
|
|
480
|
+
const rowBuf = new Float64Array(across3);
|
|
481
|
+
const cxWeight = cx.weight;
|
|
482
|
+
const cyLower = cy.lower;
|
|
483
|
+
const cyWeight = cy.weight;
|
|
377
484
|
const lefts = new Int32Array(width), rights = new Int32Array(width);
|
|
378
485
|
for (let x = 0; x < width; x++) {
|
|
379
486
|
lefts[x] = cx.lower[x] * CHANNELS;
|
|
380
487
|
rights[x] = Math.min(across - 1, cx.lower[x] + 1) * CHANNELS;
|
|
381
488
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
for (let
|
|
385
|
-
const
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
489
|
+
if (finiteWeights) {
|
|
490
|
+
// Separable Pass 1: accumulate along x into a 1D row buffer, then scatter vertically.
|
|
491
|
+
for (let r = 0; r < lines.length; r++) {
|
|
492
|
+
const y = lines[r], top = cyLower[y] * across3, bottom = Math.min(down - 1, cyLower[y] + 1) * across3, ty = cyWeight[y], wTop = 1 - ty;
|
|
493
|
+
rowBuf.fill(0);
|
|
494
|
+
let v = r * width * CHANNELS;
|
|
495
|
+
for (let x = 0; x < width; x++, v += CHANNELS) {
|
|
496
|
+
const vr = values[v], vg = values[v + 1], vb = values[v + 2];
|
|
497
|
+
if (vr === 0 && vg === 0 && vb === 0)
|
|
498
|
+
continue;
|
|
499
|
+
const tx = cxWeight[x], w0 = 1 - tx, l = lefts[x], ri = rights[x];
|
|
500
|
+
rowBuf[l] += w0 * vr;
|
|
501
|
+
rowBuf[l + 1] += w0 * vg;
|
|
502
|
+
rowBuf[l + 2] += w0 * vb;
|
|
503
|
+
rowBuf[ri] += tx * vr;
|
|
504
|
+
rowBuf[ri + 1] += tx * vg;
|
|
505
|
+
rowBuf[ri + 2] += tx * vb;
|
|
506
|
+
}
|
|
507
|
+
for (let k = 0; k < across3; k++) {
|
|
508
|
+
const val = rowBuf[k];
|
|
509
|
+
if (val !== 0) {
|
|
510
|
+
colours[top + k] += wTop * val;
|
|
511
|
+
colours[bottom + k] += ty * val;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
else {
|
|
517
|
+
for (let r = 0; r < lines.length; r++) {
|
|
518
|
+
const y = lines[r], top = cyLower[y] * across3, bottom = Math.min(down - 1, cyLower[y] + 1) * across3, ty = cyWeight[y];
|
|
519
|
+
for (let x = 0; x < width; x++) {
|
|
520
|
+
const v = (r * width + x) * CHANNELS;
|
|
521
|
+
const tx = cxWeight[x], o0 = top + lefts[x], o1 = top + rights[x], o2 = bottom + lefts[x], o3 = bottom + rights[x], s0 = (1 - tx) * (1 - ty), s1 = tx * (1 - ty), s2 = (1 - tx) * ty, s3 = tx * ty;
|
|
522
|
+
colours[o0] += s0 * values[v];
|
|
523
|
+
colours[o0 + 1] += s0 * values[v + 1];
|
|
524
|
+
colours[o0 + 2] += s0 * values[v + 2];
|
|
525
|
+
colours[o1] += s1 * values[v];
|
|
526
|
+
colours[o1 + 1] += s1 * values[v + 1];
|
|
527
|
+
colours[o1 + 2] += s1 * values[v + 2];
|
|
528
|
+
colours[o2] += s2 * values[v];
|
|
529
|
+
colours[o2 + 1] += s2 * values[v + 1];
|
|
530
|
+
colours[o2 + 2] += s2 * values[v + 2];
|
|
531
|
+
colours[o3] += s3 * values[v];
|
|
532
|
+
colours[o3 + 1] += s3 * values[v + 1];
|
|
533
|
+
colours[o3 + 2] += s3 * values[v + 2];
|
|
534
|
+
}
|
|
402
535
|
}
|
|
403
536
|
}
|
|
404
537
|
const columns = Int32Array.from({ length: width }, (_, x) => x);
|
|
@@ -406,25 +539,24 @@ function bilinearError(described, cutsX, cutsY, bound) {
|
|
|
406
539
|
const scratch = new Float64Array(Math.max(across, down));
|
|
407
540
|
for (let j = 0; j < down; j++)
|
|
408
541
|
for (let c = 0; c < CHANNELS; c++)
|
|
409
|
-
solveTridiagonal(gramX, colours, j *
|
|
542
|
+
solveTridiagonal(gramX, colours, j * across3 + c, CHANNELS, scratch);
|
|
410
543
|
for (let i = 0; i < across; i++)
|
|
411
544
|
for (let c = 0; c < CHANNELS; c++)
|
|
412
|
-
solveTridiagonal(gramY, colours, i * CHANNELS + c,
|
|
545
|
+
solveTridiagonal(gramY, colours, i * CHANNELS + c, across3, scratch);
|
|
413
546
|
const fittedBound = useBound && finiteWeights && colours.every(Number.isFinite);
|
|
414
547
|
let error = 0;
|
|
415
548
|
for (let r = 0; r < lines.length; r++) {
|
|
416
|
-
const y = lines[r], top =
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
const
|
|
423
|
-
const
|
|
424
|
-
|
|
425
|
-
const
|
|
426
|
-
|
|
427
|
-
error += difference2 * difference2;
|
|
549
|
+
const y = lines[r], top = cyLower[y] * across3, bottom = Math.min(down - 1, cyLower[y] + 1) * across3, ty = cyWeight[y], wTop = 1 - ty;
|
|
550
|
+
// Separable Pass 2: pre-blend top and bottom cell rows once per scanline.
|
|
551
|
+
for (let k = 0; k < across3; k++)
|
|
552
|
+
rowBuf[k] = wTop * colours[top + k] + ty * colours[bottom + k];
|
|
553
|
+
let v = r * width * CHANNELS;
|
|
554
|
+
for (let x = 0; x < width; x++, v += CHANNELS) {
|
|
555
|
+
const tx = cxWeight[x], w0 = 1 - tx, l = lefts[x], ri = rights[x];
|
|
556
|
+
const d0 = values[v] - (w0 * rowBuf[l] + tx * rowBuf[ri]);
|
|
557
|
+
const d1 = values[v + 1] - (w0 * rowBuf[l + 1] + tx * rowBuf[ri + 1]);
|
|
558
|
+
const d2 = values[v + 2] - (w0 * rowBuf[l + 2] + tx * rowBuf[ri + 2]);
|
|
559
|
+
error += d0 * d0 + d1 * d1 + d2 * d2;
|
|
428
560
|
}
|
|
429
561
|
if (fittedBound) {
|
|
430
562
|
// Remaining squared residuals and the colour code cannot reduce this rendering length.
|
|
@@ -451,8 +583,161 @@ function blurredError(described, moments, cutsX, cutsY, columns, rows, sigma, bo
|
|
|
451
583
|
const across = cutsX.length - 1, down = cutsY.length - 1;
|
|
452
584
|
const wx = blurWeights(cutsX, columns, sigma), wy = blurWeights(cutsY, rows, sigma);
|
|
453
585
|
const colours = new Float64Array(moments.means);
|
|
454
|
-
const correction = new Float64Array(colours.length);
|
|
455
586
|
const finiteHorizontalWeights = wx.every(Number.isFinite);
|
|
587
|
+
const finiteVerticalWeights = wy.every(Number.isFinite);
|
|
588
|
+
const across3 = across * CHANNELS;
|
|
589
|
+
if (finiteHorizontalWeights && finiteVerticalWeights) {
|
|
590
|
+
// Exact O(cells) deconvolution: cell pixel sums are already in moments.means * moments.counts,
|
|
591
|
+
// and the separable blur sums over each cell factor into cell-level horizontal + vertical weights.
|
|
592
|
+
const sumWx = new Float64Array(across * BLUR_TAPS);
|
|
593
|
+
for (let x = 0; x < width; x++) {
|
|
594
|
+
const ci = columns[x] * BLUR_TAPS;
|
|
595
|
+
const t = x * BLUR_TAPS;
|
|
596
|
+
sumWx[ci] += wx[t];
|
|
597
|
+
sumWx[ci + 1] += wx[t + 1];
|
|
598
|
+
sumWx[ci + 2] += wx[t + 2];
|
|
599
|
+
}
|
|
600
|
+
const sumWy = new Float64Array(down * BLUR_TAPS);
|
|
601
|
+
for (let r = 0; r < lines.length; r++) {
|
|
602
|
+
const y = lines[r];
|
|
603
|
+
const cj = rows[y] * BLUR_TAPS;
|
|
604
|
+
const t = y * BLUR_TAPS;
|
|
605
|
+
sumWy[cj] += wy[t];
|
|
606
|
+
sumWy[cj + 1] += wy[t + 1];
|
|
607
|
+
sumWy[cj + 2] += wy[t + 2];
|
|
608
|
+
}
|
|
609
|
+
const hConv = new Float64Array(down * across3);
|
|
610
|
+
for (let step = 0; step < DECONVOLUTION_STEPS; step++) {
|
|
611
|
+
for (let jj = 0; jj < down; jj++) {
|
|
612
|
+
const rowBase = jj * across3;
|
|
613
|
+
for (let i = 0; i < across; i++) {
|
|
614
|
+
const wt = i * BLUR_TAPS;
|
|
615
|
+
const w0 = sumWx[wt], w1 = sumWx[wt + 1], w2 = sumWx[wt + 2];
|
|
616
|
+
const c1 = rowBase + i * CHANNELS;
|
|
617
|
+
let r0 = w1 * colours[c1], g0 = w1 * colours[c1 + 1], b0 = w1 * colours[c1 + 2];
|
|
618
|
+
if (i > 0 && w0) {
|
|
619
|
+
const c0 = c1 - CHANNELS;
|
|
620
|
+
r0 += w0 * colours[c0];
|
|
621
|
+
g0 += w0 * colours[c0 + 1];
|
|
622
|
+
b0 += w0 * colours[c0 + 2];
|
|
623
|
+
}
|
|
624
|
+
if (i + 1 < across && w2) {
|
|
625
|
+
const c2 = c1 + CHANNELS;
|
|
626
|
+
r0 += w2 * colours[c2];
|
|
627
|
+
g0 += w2 * colours[c2 + 1];
|
|
628
|
+
b0 += w2 * colours[c2 + 2];
|
|
629
|
+
}
|
|
630
|
+
hConv[c1] = r0;
|
|
631
|
+
hConv[c1 + 1] = g0;
|
|
632
|
+
hConv[c1 + 2] = b0;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
for (let j = 0; j < down; j++) {
|
|
636
|
+
const wt = j * BLUR_TAPS;
|
|
637
|
+
const wy0 = sumWy[wt], wy1 = sumWy[wt + 1], wy2 = sumWy[wt + 2];
|
|
638
|
+
const rowBase = j * across3;
|
|
639
|
+
const prevBase = rowBase - across3;
|
|
640
|
+
const nextBase = rowBase + across3;
|
|
641
|
+
for (let i = 0; i < across; i++) {
|
|
642
|
+
const cell = j * across + i;
|
|
643
|
+
const n = moments.counts[cell];
|
|
644
|
+
if (!n)
|
|
645
|
+
continue;
|
|
646
|
+
const o = i * CHANNELS;
|
|
647
|
+
const c1 = rowBase + o;
|
|
648
|
+
let pr = wy1 * hConv[c1], pg = wy1 * hConv[c1 + 1], pb = wy1 * hConv[c1 + 2];
|
|
649
|
+
if (j > 0 && wy0) {
|
|
650
|
+
const c0 = prevBase + o;
|
|
651
|
+
pr += wy0 * hConv[c0];
|
|
652
|
+
pg += wy0 * hConv[c0 + 1];
|
|
653
|
+
pb += wy0 * hConv[c0 + 2];
|
|
654
|
+
}
|
|
655
|
+
if (j + 1 < down && wy2) {
|
|
656
|
+
const c2 = nextBase + o;
|
|
657
|
+
pr += wy2 * hConv[c2];
|
|
658
|
+
pg += wy2 * hConv[c2 + 1];
|
|
659
|
+
pb += wy2 * hConv[c2 + 2];
|
|
660
|
+
}
|
|
661
|
+
const invN = 1 / n;
|
|
662
|
+
colours[c1] += moments.means[c1] - pr * invN;
|
|
663
|
+
colours[c1 + 1] += moments.means[c1 + 1] - pg * invN;
|
|
664
|
+
colours[c1 + 2] += moments.means[c1 + 2] - pb * invN;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
// Final pass: measure residual with cell-hoisted blend registers.
|
|
669
|
+
const cellLeft = new Int32Array(across);
|
|
670
|
+
const cellRight = new Int32Array(across);
|
|
671
|
+
for (let i = 0; i < across; i++) {
|
|
672
|
+
cellLeft[i] = Math.trunc(cutsX[i]);
|
|
673
|
+
cellRight[i] = Math.trunc(cutsX[i + 1]);
|
|
674
|
+
}
|
|
675
|
+
const zeroBlend = new Uint8Array(across);
|
|
676
|
+
const blend = new Float64Array((across + 2 * BLUR_REACH) * CHANNELS);
|
|
677
|
+
let error = 0;
|
|
678
|
+
for (let r = 0; r < lines.length; r++) {
|
|
679
|
+
const y = lines[r], j = rows[y];
|
|
680
|
+
blend.fill(0);
|
|
681
|
+
for (let d = -BLUR_REACH; d <= BLUR_REACH; d++) {
|
|
682
|
+
const w = wy[y * BLUR_TAPS + d + BLUR_REACH], jj = j + d;
|
|
683
|
+
if (!w || jj < 0 || jj >= down)
|
|
684
|
+
continue;
|
|
685
|
+
const srcBase = jj * across3;
|
|
686
|
+
for (let k = 0; k < across3; k++)
|
|
687
|
+
blend[BLUR_REACH * CHANNELS + k] += w * colours[srcBase + k];
|
|
688
|
+
}
|
|
689
|
+
for (let i = 0; i < across; i++) {
|
|
690
|
+
const b = i * CHANNELS;
|
|
691
|
+
zeroBlend[i] = Number(blend[b] === 0 &&
|
|
692
|
+
blend[b + 1] === 0 &&
|
|
693
|
+
blend[b + 2] === 0 &&
|
|
694
|
+
blend[b + 3] === 0 &&
|
|
695
|
+
blend[b + 4] === 0 &&
|
|
696
|
+
blend[b + 5] === 0 &&
|
|
697
|
+
blend[b + 6] === 0 &&
|
|
698
|
+
blend[b + 7] === 0 &&
|
|
699
|
+
blend[b + 8] === 0);
|
|
700
|
+
}
|
|
701
|
+
const rowPix = r * width * CHANNELS;
|
|
702
|
+
for (let i = 0; i < across; i++) {
|
|
703
|
+
const left = cellLeft[i], right = cellRight[i];
|
|
704
|
+
if (left >= right)
|
|
705
|
+
continue;
|
|
706
|
+
if (zeroBlend[i]) {
|
|
707
|
+
for (let x = left, v = rowPix + left * CHANNELS; x < right; x++, v += CHANNELS) {
|
|
708
|
+
const pr = pixels[v], pg = pixels[v + 1], pb = pixels[v + 2];
|
|
709
|
+
if (pr !== 0 || pg !== 0 || pb !== 0)
|
|
710
|
+
error += pr * pr + pg * pg + pb * pb;
|
|
711
|
+
}
|
|
712
|
+
continue;
|
|
713
|
+
}
|
|
714
|
+
const b = i * CHANNELS;
|
|
715
|
+
const b0r = blend[b], b0g = blend[b + 1], b0b = blend[b + 2];
|
|
716
|
+
const b1r = blend[b + 3], b1g = blend[b + 4], b1b = blend[b + 5];
|
|
717
|
+
const b2r = blend[b + 6], b2g = blend[b + 7], b2b = blend[b + 8];
|
|
718
|
+
for (let x = left, v = rowPix + left * CHANNELS, t = left * BLUR_TAPS; x < right; x++, v += CHANNELS, t += BLUR_TAPS) {
|
|
719
|
+
const w0 = wx[t], w1 = wx[t + 1], w2 = wx[t + 2];
|
|
720
|
+
const dr = pixels[v] - (w0 * b0r + w1 * b1r + w2 * b2r);
|
|
721
|
+
const dg = pixels[v + 1] - (w0 * b0g + w1 * b1g + w2 * b2g);
|
|
722
|
+
const db = pixels[v + 2] - (w0 * b0b + w1 * b1b + w2 * b2b);
|
|
723
|
+
error += dr * dr + dg * dg + db * db;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
if (useBound) {
|
|
727
|
+
const lower = (samples / 2) * Math.log(error / samples + RESIDUAL_FLOOR) +
|
|
728
|
+
0 +
|
|
729
|
+
bound.structure +
|
|
730
|
+
parameter;
|
|
731
|
+
const slack = LENGTH_BOUND_ROUNDING_ULPS *
|
|
732
|
+
Number.EPSILON *
|
|
733
|
+
(Math.abs(lower) + Math.abs(bound.best) + 1);
|
|
734
|
+
if (Number.isFinite(lower) && Number.isFinite(slack) && lower - bound.best > slack)
|
|
735
|
+
return { error, colours, discarded: true };
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
return { error, colours, discarded: false };
|
|
739
|
+
}
|
|
740
|
+
const correction = new Float64Array(colours.length);
|
|
456
741
|
const zeroBlend = new Uint8Array(across);
|
|
457
742
|
// One cell of zeros pads each end of a row's vertical blend.
|
|
458
743
|
const blend = new Float64Array((across + 2 * BLUR_REACH) * CHANNELS);
|
|
@@ -650,11 +935,26 @@ export function descriptionLength(described, cuts) {
|
|
|
650
935
|
}
|
|
651
936
|
return rendering + cutLength(described, cuts);
|
|
652
937
|
}
|
|
938
|
+
const COLOUR_STEP_LUT_MAX = 65535;
|
|
939
|
+
let colourStepLut = null;
|
|
940
|
+
function getColourStepLut() {
|
|
941
|
+
if (!colourStepLut) {
|
|
942
|
+
colourStepLut = new Float64Array(COLOUR_STEP_LUT_MAX + 1);
|
|
943
|
+
const denom = CHANNELS * RESIDUAL_FLOOR;
|
|
944
|
+
for (let s = 1; s <= COLOUR_STEP_LUT_MAX; s++)
|
|
945
|
+
colourStepLut[s] = (CHANNELS / 2) * Math.log1p(s / denom);
|
|
946
|
+
}
|
|
947
|
+
return colourStepLut;
|
|
948
|
+
}
|
|
653
949
|
/**
|
|
654
950
|
* Nats of a pixel's colour coded against a neighbour `step` (squared premultiplied distance) away,
|
|
655
951
|
* beyond the residual floor every sample pays, as `pixelLength` codes every pixel.
|
|
656
952
|
*/
|
|
657
953
|
export function colourStepCost(step) {
|
|
954
|
+
if (step === 0)
|
|
955
|
+
return 0;
|
|
956
|
+
if (step > 0 && step <= COLOUR_STEP_LUT_MAX && (step | 0) === step)
|
|
957
|
+
return getColourStepLut()[step];
|
|
658
958
|
return (CHANNELS / 2) * Math.log1p(step / (CHANNELS * RESIDUAL_FLOOR));
|
|
659
959
|
}
|
|
660
960
|
/**
|
|
@@ -664,28 +964,33 @@ export function colourStepCost(step) {
|
|
|
664
964
|
export function pixelLength(described) {
|
|
665
965
|
const { content: image, lines, values, samples, parameter } = described;
|
|
666
966
|
const { width } = image;
|
|
967
|
+
const rowStride = width * CHANNELS;
|
|
667
968
|
let colours = 0;
|
|
668
969
|
for (let r = 0; r < lines.length; r++) {
|
|
669
970
|
const above = r > 0 && lines[r - 1] === lines[r] - 1;
|
|
670
|
-
|
|
671
|
-
|
|
971
|
+
const rowBase = r * rowStride;
|
|
972
|
+
for (let x = 0, v = rowBase; x < width; x++, v += CHANNELS) {
|
|
973
|
+
const vr = values[v], vg = values[v + 1], vb = values[v + 2];
|
|
672
974
|
let step = Infinity;
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
}
|
|
679
|
-
if (
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
step
|
|
975
|
+
if (x > 0) {
|
|
976
|
+
const dr = vr - values[v - 3];
|
|
977
|
+
const dg = vg - values[v - 2];
|
|
978
|
+
const db = vb - values[v - 1];
|
|
979
|
+
step = dr * dr + dg * dg + db * db;
|
|
980
|
+
}
|
|
981
|
+
if (above) {
|
|
982
|
+
const u = v - rowStride;
|
|
983
|
+
const dr = vr - values[u];
|
|
984
|
+
const dg = vg - values[u + 1];
|
|
985
|
+
const db = vb - values[u + 2];
|
|
986
|
+
const up = dr * dr + dg * dg + db * db;
|
|
987
|
+
if (up < step)
|
|
988
|
+
step = up;
|
|
687
989
|
}
|
|
688
|
-
|
|
990
|
+
if (step === Infinity)
|
|
991
|
+
step = vr * vr + vg * vg + vb * vb;
|
|
992
|
+
if (step !== 0)
|
|
993
|
+
colours += colourStepCost(step);
|
|
689
994
|
}
|
|
690
995
|
}
|
|
691
996
|
const cells = lines.length * width;
|