@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.
@@ -1,4 +1,5 @@
1
1
  import { pixelGridCuts } from "./grid.js";
2
+ import { imageStepProfiles } from "./grid-step-profiles.js";
2
3
  /**
3
4
  * Edge coherence of a grid: how much of the image's colour steps fall on its cuts. Each step
4
5
  * between two neighbouring pixels counts with the cosine of its phase inside its cell, so a step on
@@ -29,10 +30,6 @@ import { pixelGridCuts } from "./grid.js";
29
30
  * A bilinear or blurred upscale spreads every edge over its cells; no grid reaches the minimums,
30
31
  * and every proposal is read.
31
32
  */
32
- const STRIDE = 4;
33
- const CHANNELS = 4;
34
- const ALPHA_CHANNEL = 3;
35
- const ALPHA_SCALE = 255;
36
33
  /** Share of the coherence that rejects a grid a grid must keep to be read. */
37
34
  const COHERENCE_SHARE = 0.6;
38
35
  /** Rigid coherence a grid needs to reject others. */
@@ -45,8 +42,6 @@ const MIN_REJECTING_CELLS = 12;
45
42
  const COARSER_PITCH = 1.5;
46
43
  /** Grids read at most when local coherence selects them. */
47
44
  const MAX_COHERENT_GRIDS = 10;
48
- /** Strips across each axis the local steps are summed over. */
49
- const BANDS = 8;
50
45
  /** Cells along a strip that share one phase in the local score. */
51
46
  const RUN_CELLS = 16;
52
47
  /**
@@ -54,37 +49,6 @@ const RUN_CELLS = 16;
54
49
  * cut. Finer grids are always read and reject none.
55
50
  */
56
51
  const MIN_COHERENCE_PITCH = 2;
57
- /** Colour step (premultiplied RGB and alpha) into each column and row, per strip. */
58
- function stepProfiles(image) {
59
- const { width, height, data } = image;
60
- const x = Array.from({ length: BANDS }, () => new Float64Array(width)), y = Array.from({ length: BANDS }, () => new Float64Array(height));
61
- const columnBand = Int32Array.from({ length: width }, (_, column) => Math.floor((column * BANDS) / width));
62
- // One row of premultiplied colours at a time, with the row above it.
63
- let above = new Float64Array(width * CHANNELS), current = new Float64Array(width * CHANNELS);
64
- for (let row = 0; row < height; row++) {
65
- const across = x[Math.floor((row * BANDS) / height)];
66
- for (let column = 0, o = row * width * STRIDE; column < width; column++, o += STRIDE) {
67
- const alpha = data[o + ALPHA_CHANNEL] / ALPHA_SCALE, at = column * CHANNELS;
68
- current[at] = data[o] * alpha;
69
- current[at + 1] = data[o + 1] * alpha;
70
- current[at + 2] = data[o + 2] * alpha;
71
- current[at + ALPHA_CHANNEL] = data[o + ALPHA_CHANNEL];
72
- }
73
- for (let column = 0, at = 0; column < width; column++, at += CHANNELS) {
74
- let right = 0, down = 0;
75
- for (let c = 0; c < CHANNELS; c++) {
76
- if (column)
77
- right += Math.abs(current[at + c] - current[at - CHANNELS + c]);
78
- if (row)
79
- down += Math.abs(current[at + c] - above[at + c]);
80
- }
81
- across[column] += right;
82
- y[columnBand[column]][row] += down;
83
- }
84
- [above, current] = [current, above];
85
- }
86
- return { x, y };
87
- }
88
52
  /**
89
53
  * Share of one axis's steps on the cuts, weighted by the cosine of their phase in the cell. With
90
54
  * `run`, the steps of every `run` cells of each strip add up as phasors and only their length
@@ -112,19 +76,13 @@ function axisCoherence(strips, cuts, run) {
112
76
  }
113
77
  return total ? aligned / total : 0;
114
78
  }
115
- const sum = (strips) => {
116
- const total = new Float64Array(strips[0].length);
117
- for (const strip of strips)
118
- for (let p = 0; p < strip.length; p++)
119
- total[p] += strip[p];
120
- return [total];
121
- };
122
79
  /** The proposals Auto reads, per proposer: those the image's edges do not reject (see above). */
123
80
  export function coherentGrids(image, proposals) {
124
81
  if (!proposals.some((grids) => grids.length))
125
82
  return proposals.map((grids) => [...grids]);
126
- const bands = stepProfiles(image);
127
- const whole = { x: sum(bands.x), y: sum(bands.y) };
83
+ const profiles = imageStepProfiles(image);
84
+ const bands = { x: profiles.bandsX, y: profiles.bandsY };
85
+ const whole = { x: [profiles.x], y: [profiles.y] };
128
86
  const scored = proposals.map((grids) => grids.map((grid) => {
129
87
  const cutsX = grid.cutsX ?? pixelGridCuts(image.width, grid.cellWidth, grid.offsetX), cutsY = grid.cutsY ?? pixelGridCuts(image.height, grid.cellHeight, grid.offsetY);
130
88
  const pitchX = image.width / (cutsX.length - 1), pitchY = image.height / (cutsY.length - 1);
@@ -1,4 +1,5 @@
1
1
  import { pixelGridCuts } from "./grid.js";
2
+ import { imageStepProfiles } from "./grid-step-profiles.js";
2
3
  import { PixelationAlgorithm } from "./types.js";
3
4
  /**
4
5
  * Edge paths: a mesh whose cuts follow the colour edges at a pitch another proposer found.
@@ -18,10 +19,6 @@ import { PixelationAlgorithm } from "./types.js";
18
19
  * for itself, so a path wins only where the art does not keep one pitch. A path already sits on
19
20
  * the edges, so it is read as placed, without strip shifts or a phase field.
20
21
  */
21
- const STRIDE = 4;
22
- const CHANNELS = 4;
23
- const ALPHA_CHANNEL = 3;
24
- const ALPHA_SCALE = 255;
25
22
  const MIN_WIDTH = 0.75;
26
23
  const MAX_WIDTH = 1.25;
27
24
  const PITCH_WEIGHT = 10;
@@ -38,34 +35,6 @@ const MIN_CELLS = 8;
38
35
  const PITCH_TOLERANCE = 0.5;
39
36
  /** Paths proposed at most: each costs a reading. */
40
37
  const MAX_PATHS = 3;
41
- /** Colour step into every column and row, summed over the image (premultiplied RGB and alpha). */
42
- function stepProfiles(image) {
43
- const { width, height, data } = image;
44
- const x = new Float64Array(width), y = new Float64Array(height);
45
- let above = new Float64Array(width * CHANNELS), current = new Float64Array(width * CHANNELS);
46
- for (let row = 0; row < height; row++) {
47
- for (let column = 0, o = row * width * STRIDE; column < width; column++, o += STRIDE) {
48
- const alpha = data[o + ALPHA_CHANNEL] / ALPHA_SCALE, at = column * CHANNELS;
49
- current[at] = data[o] * alpha;
50
- current[at + 1] = data[o + 1] * alpha;
51
- current[at + 2] = data[o + 2] * alpha;
52
- current[at + ALPHA_CHANNEL] = data[o + ALPHA_CHANNEL];
53
- }
54
- for (let column = 0, at = 0; column < width; column++, at += CHANNELS) {
55
- let right = 0, down = 0;
56
- for (let c = 0; c < CHANNELS; c++) {
57
- if (column)
58
- right += Math.abs(current[at + c] - current[at - CHANNELS + c]);
59
- if (row)
60
- down += Math.abs(current[at + c] - above[at + c]);
61
- }
62
- x[column] += right;
63
- y[row] += down;
64
- }
65
- [above, current] = [current, above];
66
- }
67
- return { x, y };
68
- }
69
38
  /** The cuts of one axis along its step profile at `pitch`; null when the axis has no content. */
70
39
  export function edgePath(profile, pitch) {
71
40
  const length = profile.length;
@@ -168,7 +137,7 @@ export function edgePathGrids(image, proposals) {
168
137
  }
169
138
  if (!pitches.length)
170
139
  return [];
171
- const profiles = stepProfiles(image);
140
+ const profiles = imageStepProfiles(image);
172
141
  const grids = [];
173
142
  const seen = new Set();
174
143
  for (const { x, y, confidence } of pitches) {
@@ -80,24 +80,119 @@ function energyProfile(image, horizontal, model) {
80
80
  const length = horizontal ? width : height;
81
81
  const cross = horizontal ? height : width;
82
82
  const stride = Math.max(1, Math.ceil(cross / MAX_SAMPLED_LINES));
83
- const unit = (horizontal ? 1 : width) * CHANNELS;
84
83
  const soft = model === FrameModel.Soft;
85
84
  const profile = new Float64Array(length);
85
+ const invAlpha = 1 / ALPHA_SCALE;
86
+ const end = length - (soft ? 1 : 0);
86
87
  let lines = 0;
87
- for (let line = 0; line < cross; line += stride, lines++) {
88
- const start = (horizontal ? line * width : line) * CHANNELS;
89
- for (let i = 1; i < length - (soft ? 1 : 0); i++) {
90
- const o = start + i * unit;
91
- let energy = 0;
92
- for (let c = 0; c < CHANNELS; c++) {
93
- const d = soft
94
- ? premultiplied(data, o - unit, c) -
95
- 2 * premultiplied(data, o, c) +
96
- premultiplied(data, o + unit, c)
97
- : premultiplied(data, o, c) - premultiplied(data, o - unit, c);
98
- energy += d * d;
88
+ if (horizontal) {
89
+ for (let line = 0; line < cross; line += stride, lines++) {
90
+ const start = line * width * CHANNELS;
91
+ if (soft) {
92
+ for (let i = 1; i < end; i++) {
93
+ const o = start + i * CHANNELS;
94
+ const oPrev = o - CHANNELS;
95
+ const oNext = o + CHANNELS;
96
+ const aP = data[oPrev + 3], aC = data[o + 3], aN = data[oNext + 3];
97
+ if (aP === 255 && aC === 255 && aN === 255) {
98
+ const dr = data[oPrev] - 2 * data[o] + data[oNext];
99
+ const dg = data[oPrev + 1] - 2 * data[o + 1] + data[oNext + 1];
100
+ const db = data[oPrev + 2] - 2 * data[o + 2] + data[oNext + 2];
101
+ profile[i] += dr * dr + dg * dg + db * db;
102
+ }
103
+ else {
104
+ const fP = aP * invAlpha, fC = aC * invAlpha, fN = aN * invAlpha;
105
+ const dr = data[oPrev] * fP - 2 * data[o] * fC + data[oNext] * fN;
106
+ const dg = data[oPrev + 1] * fP - 2 * data[o + 1] * fC + data[oNext + 1] * fN;
107
+ const db = data[oPrev + 2] * fP - 2 * data[o + 2] * fC + data[oNext + 2] * fN;
108
+ const da = aP - 2 * aC + aN;
109
+ profile[i] += dr * dr + dg * dg + db * db + da * da;
110
+ }
111
+ }
112
+ }
113
+ else {
114
+ for (let i = 1; i < end; i++) {
115
+ const o = start + i * CHANNELS;
116
+ const oPrev = o - CHANNELS;
117
+ const aP = data[oPrev + 3], aC = data[o + 3];
118
+ if (aP === 255 && aC === 255) {
119
+ const dr = data[o] - data[oPrev];
120
+ const dg = data[o + 1] - data[oPrev + 1];
121
+ const db = data[o + 2] - data[oPrev + 2];
122
+ profile[i] += dr * dr + dg * dg + db * db;
123
+ }
124
+ else {
125
+ const fP = aP * invAlpha, fC = aC * invAlpha;
126
+ const dr = data[o] * fC - data[oPrev] * fP;
127
+ const dg = data[o + 1] * fC - data[oPrev + 1] * fP;
128
+ const db = data[o + 2] * fC - data[oPrev + 2] * fP;
129
+ const da = aC - aP;
130
+ profile[i] += dr * dr + dg * dg + db * db + da * da;
131
+ }
132
+ }
133
+ }
134
+ }
135
+ }
136
+ else {
137
+ // Row-major accumulation over sampled columns to keep reads cache-friendly.
138
+ const rowStride = width * CHANNELS;
139
+ const xStep = stride * CHANNELS;
140
+ lines = Math.ceil(cross / stride);
141
+ if (soft) {
142
+ for (let i = 1; i < end; i++) {
143
+ const rowBase = i * rowStride;
144
+ const prevBase = rowBase - rowStride;
145
+ const nextBase = rowBase + rowStride;
146
+ let sum = 0;
147
+ for (let line = 0, xOff = 0; line < cross; line += stride, xOff += xStep) {
148
+ const oPrev = prevBase + xOff;
149
+ const o = rowBase + xOff;
150
+ const oNext = nextBase + xOff;
151
+ const aP = data[oPrev + 3], aC = data[o + 3], aN = data[oNext + 3];
152
+ if (aP === 255 && aC === 255 && aN === 255) {
153
+ const dr = data[oPrev] - 2 * data[o] + data[oNext];
154
+ const dg = data[oPrev + 1] - 2 * data[o + 1] + data[oNext + 1];
155
+ const db = data[oPrev + 2] - 2 * data[o + 2] + data[oNext + 2];
156
+ sum += dr * dr + dg * dg + db * db;
157
+ }
158
+ else {
159
+ const fP = aP * invAlpha, fC = aC * invAlpha, fN = aN * invAlpha;
160
+ const dr = data[oPrev] * fP - 2 * data[o] * fC + data[oNext] * fN;
161
+ const dg = data[oPrev + 1] * fP - 2 * data[o + 1] * fC + data[oNext + 1] * fN;
162
+ const db = data[oPrev + 2] * fP - 2 * data[o + 2] * fC + data[oNext + 2] * fN;
163
+ const da = aP - 2 * aC + aN;
164
+ sum += dr * dr + dg * dg + db * db + da * da;
165
+ }
166
+ }
167
+ profile[i] = sum;
168
+ }
169
+ }
170
+ else {
171
+ for (let i = 1; i < end; i++) {
172
+ const rowBase = i * rowStride;
173
+ const prevBase = rowBase - rowStride;
174
+ let sum = 0;
175
+ for (let line = 0, xOff = 0; line < cross; line += stride, xOff += xStep) {
176
+ const oPrev = prevBase + xOff;
177
+ const o = rowBase + xOff;
178
+ const aP = data[oPrev + 3], aC = data[o + 3];
179
+ if (aP === 255 && aC === 255) {
180
+ const dr = data[o] - data[oPrev];
181
+ const dg = data[o + 1] - data[oPrev + 1];
182
+ const db = data[o + 2] - data[oPrev + 2];
183
+ sum += dr * dr + dg * dg + db * db;
184
+ }
185
+ else {
186
+ const fP = aP * invAlpha, fC = aC * invAlpha;
187
+ const dr = data[o] * fC - data[oPrev] * fP;
188
+ const dg = data[o + 1] * fC - data[oPrev + 1] * fP;
189
+ const db = data[o + 2] * fC - data[oPrev + 2] * fP;
190
+ const da = aC - aP;
191
+ sum += dr * dr + dg * dg + db * db + da * da;
192
+ }
193
+ }
194
+ profile[i] = sum;
99
195
  }
100
- profile[i] += energy;
101
196
  }
102
197
  }
103
198
  for (let i = 0; i < length; i++)
@@ -1,3 +1,4 @@
1
+ import { linearAutocorrelation } from "./fft-autocorr.js";
1
2
  import { blockPartition, MAX_RECOVERED_GRID_SIZE } from "./grid.js";
2
3
  import { PixelationAlgorithm } from "./types.js";
3
4
  /**
@@ -56,20 +57,121 @@ function differences(image, horizontal, order) {
56
57
  const length = horizontal ? width : height;
57
58
  const lines = horizontal ? height : width;
58
59
  const out = new Float64Array(lines * length);
59
- const value = (along, line, c) => {
60
- const o = (horizontal ? line * width + along : along * width + line) * STRIDE;
61
- return (data[o + c] * data[o + ALPHA_CHANNEL]) / ALPHA_SCALE;
62
- };
63
- const end = order === Difference.Second ? length - 1 : length;
64
- for (let line = 0; line < lines; line++)
65
- for (let i = 1; i < end; i++) {
66
- let energy = 0;
67
- for (let c = 0; c < CHANNELS; c++) {
68
- const here = value(i, line, c), before = value(i - 1, line, c);
69
- energy += Math.abs(order === Difference.Second ? value(i + 1, line, c) - 2 * here + before : here - before);
60
+ const invAlpha = 1 / ALPHA_SCALE;
61
+ const second = order === Difference.Second;
62
+ if (horizontal) {
63
+ const end = second ? width - 1 : width;
64
+ for (let y = 0; y < height; y++) {
65
+ const rowOff = y * width * STRIDE;
66
+ const outRow = y * width;
67
+ let oPrev = rowOff;
68
+ let aPrev = data[oPrev + ALPHA_CHANNEL];
69
+ let rPrev = aPrev === 255 ? data[oPrev] : data[oPrev] * aPrev * invAlpha;
70
+ let gPrev = aPrev === 255 ? data[oPrev + 1] : data[oPrev + 1] * aPrev * invAlpha;
71
+ let bPrev = aPrev === 255 ? data[oPrev + 2] : data[oPrev + 2] * aPrev * invAlpha;
72
+ if (second) {
73
+ if (width < 3)
74
+ continue;
75
+ let oCurr = rowOff + STRIDE;
76
+ let aCurr = data[oCurr + ALPHA_CHANNEL];
77
+ let rCurr = aCurr === 255 ? data[oCurr] : data[oCurr] * aCurr * invAlpha;
78
+ let gCurr = aCurr === 255 ? data[oCurr + 1] : data[oCurr + 1] * aCurr * invAlpha;
79
+ let bCurr = aCurr === 255 ? data[oCurr + 2] : data[oCurr + 2] * aCurr * invAlpha;
80
+ for (let x = 1; x < end; x++) {
81
+ const oNext = rowOff + (x + 1) * STRIDE;
82
+ const aNext = data[oNext + ALPHA_CHANNEL];
83
+ const rNext = aNext === 255 ? data[oNext] : data[oNext] * aNext * invAlpha;
84
+ const gNext = aNext === 255 ? data[oNext + 1] : data[oNext + 1] * aNext * invAlpha;
85
+ const bNext = aNext === 255 ? data[oNext + 2] : data[oNext + 2] * aNext * invAlpha;
86
+ out[outRow + x] =
87
+ Math.abs(rNext - 2 * rCurr + rPrev) +
88
+ Math.abs(gNext - 2 * gCurr + gPrev) +
89
+ Math.abs(bNext - 2 * bCurr + bPrev);
90
+ rPrev = rCurr;
91
+ gPrev = gCurr;
92
+ bPrev = bCurr;
93
+ rCurr = rNext;
94
+ gCurr = gNext;
95
+ bCurr = bNext;
96
+ }
97
+ }
98
+ else {
99
+ for (let x = 1; x < end; x++) {
100
+ const oCurr = rowOff + x * STRIDE;
101
+ const aCurr = data[oCurr + ALPHA_CHANNEL];
102
+ const rCurr = aCurr === 255 ? data[oCurr] : data[oCurr] * aCurr * invAlpha;
103
+ const gCurr = aCurr === 255 ? data[oCurr + 1] : data[oCurr + 1] * aCurr * invAlpha;
104
+ const bCurr = aCurr === 255 ? data[oCurr + 2] : data[oCurr + 2] * aCurr * invAlpha;
105
+ out[outRow + x] =
106
+ Math.abs(rCurr - rPrev) + Math.abs(gCurr - gPrev) + Math.abs(bCurr - bPrev);
107
+ rPrev = rCurr;
108
+ gPrev = gCurr;
109
+ bPrev = bCurr;
110
+ }
111
+ }
112
+ }
113
+ }
114
+ else {
115
+ // Row-major scan to avoid column-stride cache misses when reading image pixels.
116
+ const rowStride = width * STRIDE;
117
+ const end = second ? height - 1 : height;
118
+ for (let y = 1; y < end; y++) {
119
+ const oCurrRow = y * rowStride;
120
+ const oPrevRow = oCurrRow - rowStride;
121
+ const oNextRow = oCurrRow + rowStride;
122
+ if (second) {
123
+ for (let x = 0; x < width; x++) {
124
+ const oPrev = oPrevRow + x * STRIDE;
125
+ const oCurr = oCurrRow + x * STRIDE;
126
+ const oNext = oNextRow + x * STRIDE;
127
+ const aPrev = data[oPrev + ALPHA_CHANNEL];
128
+ const aCurr = data[oCurr + ALPHA_CHANNEL];
129
+ const aNext = data[oNext + ALPHA_CHANNEL];
130
+ let energy;
131
+ if (aPrev === 255 && aCurr === 255 && aNext === 255) {
132
+ energy =
133
+ Math.abs(data[oNext] - 2 * data[oCurr] + data[oPrev]) +
134
+ Math.abs(data[oNext + 1] - 2 * data[oCurr + 1] + data[oPrev + 1]) +
135
+ Math.abs(data[oNext + 2] - 2 * data[oCurr + 2] + data[oPrev + 2]);
136
+ }
137
+ else {
138
+ const pA = aPrev * invAlpha;
139
+ const cA = aCurr * invAlpha;
140
+ const nA = aNext * invAlpha;
141
+ energy =
142
+ Math.abs(data[oNext] * nA - 2 * data[oCurr] * cA + data[oPrev] * pA) +
143
+ Math.abs(data[oNext + 1] * nA - 2 * data[oCurr + 1] * cA + data[oPrev + 1] * pA) +
144
+ Math.abs(data[oNext + 2] * nA - 2 * data[oCurr + 2] * cA + data[oPrev + 2] * pA);
145
+ }
146
+ out[x * height + y] = energy;
147
+ }
148
+ }
149
+ else {
150
+ for (let x = 0; x < width; x++) {
151
+ const oPrev = oPrevRow + x * STRIDE;
152
+ const oCurr = oCurrRow + x * STRIDE;
153
+ const aPrev = data[oPrev + ALPHA_CHANNEL];
154
+ const aCurr = data[oCurr + ALPHA_CHANNEL];
155
+ let energy;
156
+ if (aPrev === 255 && aCurr === 255) {
157
+ energy =
158
+ Math.abs(data[oCurr] - data[oPrev]) +
159
+ Math.abs(data[oCurr + 1] - data[oPrev + 1]) +
160
+ Math.abs(data[oCurr + 2] - data[oPrev + 2]);
161
+ }
162
+ else {
163
+ const pA = aPrev * invAlpha;
164
+ const cA = aCurr * invAlpha;
165
+ energy =
166
+ Math.abs(data[oCurr] * cA - data[oPrev] * pA) +
167
+ Math.abs(data[oCurr + 1] * cA - data[oPrev + 1] * pA) +
168
+ Math.abs(data[oCurr + 2] * cA - data[oPrev + 2] * pA);
169
+ }
170
+ out[x * height + y] = energy;
171
+ }
70
172
  }
71
- out[line * length + i] = energy;
72
173
  }
174
+ }
73
175
  return { lines, length, data: out };
74
176
  }
75
177
  /** Mean over bands of `band` lines of each band's unbiased autocorrelation, normalised at lag 0. */
@@ -80,31 +182,33 @@ function bandAutocorrelation(feature, band, maxLag) {
80
182
  const read = Math.min(bands, MAX_BANDS);
81
183
  const sum = new Float64Array(maxLag + 1);
82
184
  const profile = new Float64Array(length);
185
+ const acf = new Float64Array(maxLag + 1);
83
186
  let used = 0;
84
187
  for (let k = 0; k < read; k++) {
85
188
  const first = Math.floor((k * bands) / read) * size;
86
189
  profile.fill(0);
87
- for (let line = first; line < first + size; line++)
190
+ for (let line = first; line < first + size; line++) {
191
+ const base = line * length;
88
192
  for (let i = 0; i < length; i++)
89
- profile[i] += data[line * length + i];
193
+ profile[i] += data[base + i];
194
+ }
90
195
  let mean = 0;
91
196
  for (let i = 0; i < length; i++)
92
197
  mean += profile[i];
93
198
  mean /= length;
94
199
  let energy = 0;
95
200
  for (let i = 0; i < length; i++) {
96
- profile[i] -= mean;
97
- energy += profile[i] * profile[i];
201
+ const v = profile[i] - mean;
202
+ profile[i] = v;
203
+ energy += v * v;
98
204
  }
99
205
  if (!energy)
100
206
  continue;
101
207
  used++;
102
- for (let lag = 0; lag <= maxLag; lag++) {
103
- let s = 0;
104
- for (let i = 0; i + lag < length; i++)
105
- s += profile[i] * profile[i + lag];
106
- sum[lag] += (s / energy) * (length / (length - lag));
107
- }
208
+ linearAutocorrelation(profile, maxLag, acf);
209
+ const invEnergy = 1 / energy;
210
+ for (let lag = 0; lag <= maxLag; lag++)
211
+ sum[lag] += acf[lag] * invEnergy * (length / (length - lag));
108
212
  }
109
213
  if (used)
110
214
  for (let lag = 0; lag <= maxLag; lag++)
@@ -74,14 +74,25 @@ function lineSteps(image, horizontal) {
74
74
  values = new Float64Array(length - 1);
75
75
  const stride = horizontal ? STRIDE : width * STRIDE;
76
76
  let a = (horizontal ? line * width : line) * STRIDE;
77
+ const invAlpha = 1 / ALPHA_SCALE;
77
78
  for (let i = 0; i + 1 < length; i++) {
78
79
  const b = a + stride;
79
- const alphaA = data[a + ALPHA_CHANNEL] / ALPHA_SCALE, alphaB = data[b + ALPHA_CHANNEL] / ALPHA_SCALE;
80
- let step = Math.abs(data[a + ALPHA_CHANNEL] - data[b + ALPHA_CHANNEL]);
81
- step += Math.abs(data[a] * alphaA - data[b] * alphaB);
82
- step += Math.abs(data[a + 1] * alphaA - data[b + 1] * alphaB);
83
- step += Math.abs(data[a + 2] * alphaA - data[b + 2] * alphaB);
84
- values[i] = step;
80
+ const aa = data[a + ALPHA_CHANNEL];
81
+ const ab = data[b + ALPHA_CHANNEL];
82
+ if (aa === 255 && ab === 255) {
83
+ values[i] =
84
+ Math.abs(data[a] - data[b]) +
85
+ Math.abs(data[a + 1] - data[b + 1]) +
86
+ Math.abs(data[a + 2] - data[b + 2]);
87
+ }
88
+ else {
89
+ const alphaA = aa * invAlpha, alphaB = ab * invAlpha;
90
+ values[i] =
91
+ Math.abs(aa - ab) +
92
+ Math.abs(data[a] * alphaA - data[b] * alphaB) +
93
+ Math.abs(data[a + 1] * alphaA - data[b + 1] * alphaB) +
94
+ Math.abs(data[a + 2] * alphaA - data[b + 2] * alphaB);
95
+ }
85
96
  a = b;
86
97
  }
87
98
  cached.set(line, values);
@@ -93,15 +104,53 @@ function lineSteps(image, horizontal) {
93
104
  }
94
105
  return { lines, length: length - 1, steps };
95
106
  }
107
+ /** O(n) in-place quickselect returning the k-th smallest element of `arr[0..len-1]`. */
108
+ function quickselect(arr, len, k) {
109
+ let left = 0;
110
+ let right = len - 1;
111
+ while (left < right) {
112
+ const mid = (left + right) >> 1;
113
+ const pivot = arr[mid];
114
+ let i = left;
115
+ let j = right;
116
+ while (i <= j) {
117
+ while (arr[i] < pivot)
118
+ i++;
119
+ while (arr[j] > pivot)
120
+ j--;
121
+ if (i <= j) {
122
+ const tmp = arr[i];
123
+ arr[i] = arr[j];
124
+ arr[j] = tmp;
125
+ i++;
126
+ j--;
127
+ }
128
+ }
129
+ if (k <= j)
130
+ right = j;
131
+ else if (k >= i)
132
+ left = i;
133
+ else
134
+ return arr[k];
135
+ }
136
+ return arr[k];
137
+ }
96
138
  /** Histogram, in BIN-wide bins, of the distances between boundaries of the same line. */
97
139
  function boundaryDistances(image, horizontal) {
98
140
  const { lines, length, steps } = lineSteps(image, horizontal);
99
141
  const histogram = new Float64Array(Math.ceil(MAX_DISTANCE / BIN) + 1);
100
- const positive = steps.filter((step) => step > 0).sort();
101
- if (!positive.length)
142
+ let posCount = 0;
143
+ for (let i = 0; i < steps.length; i++)
144
+ if (steps[i] > 0)
145
+ posCount++;
146
+ if (!posCount)
102
147
  return histogram;
103
- const threshold = Math.max(EDGE_FLOOR, EDGE_SHARE *
104
- positive[Math.min(positive.length - 1, Math.floor(EDGE_QUANTILE * positive.length))]);
148
+ const positive = new Float64Array(posCount);
149
+ for (let i = 0, p = 0; i < steps.length; i++)
150
+ if (steps[i] > 0)
151
+ positive[p++] = steps[i];
152
+ const quantileIndex = Math.min(posCount - 1, Math.floor(EDGE_QUANTILE * posCount));
153
+ const threshold = Math.max(EDGE_FLOOR, EDGE_SHARE * quickselect(positive, posCount, quantileIndex));
105
154
  const boundaries = [];
106
155
  for (let k = 0; k < lines; k++) {
107
156
  const row = k * length;