@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.
@@ -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
- let step = 0;
71
- step += (red - neighbourRed) ** 2;
72
- step += (green - neighbourGreen) ** 2;
73
- step += (blue - neighbourBlue) ** 2;
74
- drops[row + p + 1] = drops[row + p] + colourStepCost(step);
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 intervalEnergies = new Float64Array(uniqueFroms.length);
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) * (length + 1) * (CHANNELS + 1) - 1] === 0)
154
+ if (zeroLineSafe && line[(k + 1) * lineStride - 1] === 0)
139
155
  continue;
140
- const base = k * (length + 1) * (CHANNELS + 1);
141
- for (let i = 0; i < intervalEnergies.length; i++)
142
- intervalEnergies[i] = intervalEnergy(line, base, uniqueFroms[i], uniqueTos[i]);
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
- for (; cell < boundaries[slot]; cell++)
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, window, drop) {
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 [low, high] = window(k);
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 + change.jump;
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 energy = bandEnergy(table, k, j, from, to) * scale + drop(k, shift) + tieBias[j];
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 (current[j] < nextBest) {
203
- nextBest = current[j];
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] + change.step < cost) {
210
- cost = previous[j - 1] + change.step;
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] + change.step < cost) {
214
- cost = previous[j + 1] + change.step;
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
- current[j] = cost + energy;
222
- if (current[j] < nextBest) {
223
- nextBest = current[j];
280
+ const total = cost + energy;
281
+ current[j] = total;
282
+ if (total < nextBest) {
283
+ nextBest = total;
224
284
  nextBestAt = j;
225
285
  }
226
- back[k * span + j] = source;
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
- shifts[k] = first + at;
242
- energy += bandEnergy(table, k, at, from, to);
243
- cost += drop(k, shifts[k]);
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 ? change.step : change.jump;
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 stationaryWindow = [low, high];
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 window = anchor
280
- ? (k) => [
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 = rangedTable(strips.prefix(false), cutsX);
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 anti = cached?.table === columnTable
378
- ? (cached.anti ??= columnView(strips, cutsX, columnTable, antiField(columnTable, columns)))
379
- : columnView(strips, cutsX, columnTable, antiField(columnTable, columns));
380
- const rowTable = rangedTable(anti.rowPrefix, cutsY);
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 = rangedTable(rowPrefix, cutsY);
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))
@@ -40,18 +40,30 @@ function axisSamples(image, horizontal, maxLines) {
40
40
  const lines = Math.ceil(cross / stride);
41
41
  const row = (length + 1) * CHANNELS;
42
42
  const prefix = new Float64Array(lines * row);
43
+ const invAlpha = 1 / ALPHA_SCALE;
44
+ const stepO = (horizontal ? 1 : width) * CHANNELS;
43
45
  for (let line = 0; line < lines; line++) {
44
46
  const at = line * stride;
45
47
  let r = 0, g = 0, b = 0, squares = 0;
46
- for (let i = 0; i < length; i++) {
47
- const o = (horizontal ? at * width + i : i * width + at) * CHANNELS;
48
- const alpha = data[o + 3] / ALPHA_SCALE;
49
- const pr = data[o] * alpha, pg = data[o + 1] * alpha, pb = data[o + 2] * alpha;
50
- r += pr;
51
- g += pg;
52
- b += pb;
53
- squares += pr * pr + pg * pg + pb * pb;
54
- const p = line * row + (i + 1) * CHANNELS;
48
+ let o = (horizontal ? at * width : at) * CHANNELS;
49
+ let p = line * row + CHANNELS;
50
+ for (let i = 0; i < length; i++, o += stepO, p += CHANNELS) {
51
+ const a = data[o + 3];
52
+ if (a === 255) {
53
+ const pr = data[o], pg = data[o + 1], pb = data[o + 2];
54
+ r += pr;
55
+ g += pg;
56
+ b += pb;
57
+ squares += pr * pr + pg * pg + pb * pb;
58
+ }
59
+ else if (a !== 0) {
60
+ const alpha = a * invAlpha;
61
+ const pr = data[o] * alpha, pg = data[o + 1] * alpha, pb = data[o + 2] * alpha;
62
+ r += pr;
63
+ g += pg;
64
+ b += pb;
65
+ squares += pr * pr + pg * pg + pb * pb;
66
+ }
55
67
  prefix[p] = r;
56
68
  prefix[p + 1] = g;
57
69
  prefix[p + 2] = b;
@@ -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
- for (let line = 0; line < cross; line += lineStride, lines++) {
73
- const start = (horizontal ? line * width : line) * CHANNELS;
74
- for (let i = span; i < length - span; i++) {
75
- const o = start + i * unit;
76
- let energy = 0;
77
- for (let c = 0; c < 3; c++) {
78
- const d = order === ProfileOrder.Curvature
79
- ? data[o + step + c] - 2 * data[o + c] + data[o - step + c]
80
- : data[o + step + c] - data[o - step + c];
81
- energy += d * d;
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
- centred[i] = signal[i] - mean;
100
- energy += centred[i] ** 2;
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
- for (let lag = 0; lag <= maxLag; lag++) {
106
- let sum = 0;
107
- for (let i = 0; i < n - lag; i++)
108
- sum += centred[i] * centred[i + lag];
109
- result[lag] = sum / energy;
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
  }
@@ -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. */
@@ -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
- offset = previous
84
- ? (x, y) => {
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
- : (x, y) => {
89
- const at = byLine[x]?.[y];
90
- return at === undefined ? original(x, y) : (at * width + x) * STRIDE;
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
- offset = previous
95
- ? (x, y) => {
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
- : (x, y) => {
100
- const at = byLine[y]?.[x];
101
- return at === undefined ? original(x, y) : (y * width + at) * STRIDE;
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;