@instruments/quadrat 0.0.0-stage → 0.1.0

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.
Files changed (77) hide show
  1. package/README.md +40 -2
  2. package/dist/analysis/analyze-surface-regions.d.ts +34 -0
  3. package/dist/analysis/analyze-surface-regions.d.ts.map +1 -0
  4. package/dist/analysis/analyze-surface-regions.js +47 -0
  5. package/dist/analysis/decode.d.ts +23 -0
  6. package/dist/analysis/decode.d.ts.map +1 -0
  7. package/dist/analysis/decode.js +48 -0
  8. package/dist/analysis/descriptors.d.ts +112 -0
  9. package/dist/analysis/descriptors.d.ts.map +1 -0
  10. package/dist/analysis/descriptors.js +248 -0
  11. package/dist/analysis/diagnostics.d.ts +45 -0
  12. package/dist/analysis/diagnostics.d.ts.map +1 -0
  13. package/dist/analysis/diagnostics.js +34 -0
  14. package/dist/analysis/index.d.ts +6 -0
  15. package/dist/analysis/index.d.ts.map +1 -0
  16. package/dist/analysis/index.js +2 -0
  17. package/dist/analysis/outline.d.ts +44 -0
  18. package/dist/analysis/outline.d.ts.map +1 -0
  19. package/dist/analysis/outline.js +301 -0
  20. package/dist/analysis/rank-suggestions.d.ts +21 -0
  21. package/dist/analysis/rank-suggestions.d.ts.map +1 -0
  22. package/dist/analysis/rank-suggestions.js +150 -0
  23. package/dist/analysis/repetition.d.ts +31 -0
  24. package/dist/analysis/repetition.d.ts.map +1 -0
  25. package/dist/analysis/repetition.js +105 -0
  26. package/dist/analysis/segmentation.d.ts +26 -0
  27. package/dist/analysis/segmentation.d.ts.map +1 -0
  28. package/dist/analysis/segmentation.js +81 -0
  29. package/dist/analysis/square-geometry.d.ts +34 -0
  30. package/dist/analysis/square-geometry.d.ts.map +1 -0
  31. package/dist/analysis/square-geometry.js +91 -0
  32. package/dist/analysis/surface-candidates.d.ts +45 -0
  33. package/dist/analysis/surface-candidates.d.ts.map +1 -0
  34. package/dist/analysis/surface-candidates.js +471 -0
  35. package/dist/analysis/thresholds.d.ts +189 -0
  36. package/dist/analysis/thresholds.d.ts.map +1 -0
  37. package/dist/analysis/thresholds.js +189 -0
  38. package/dist/analysis/types.d.ts +42 -0
  39. package/dist/analysis/types.d.ts.map +1 -0
  40. package/dist/analysis/types.js +2 -0
  41. package/dist/crops/crop-editor-state.d.ts +88 -0
  42. package/dist/crops/crop-editor-state.d.ts.map +1 -0
  43. package/dist/crops/crop-editor-state.js +121 -0
  44. package/dist/crops/crop-entries.d.ts +47 -0
  45. package/dist/crops/crop-entries.d.ts.map +1 -0
  46. package/dist/crops/crop-entries.js +46 -0
  47. package/dist/crops/crop-export.d.ts +21 -0
  48. package/dist/crops/crop-export.d.ts.map +1 -0
  49. package/dist/crops/crop-export.js +38 -0
  50. package/dist/crops/crop-geometry.d.ts +130 -0
  51. package/dist/crops/crop-geometry.d.ts.map +1 -0
  52. package/dist/crops/crop-geometry.js +271 -0
  53. package/dist/crops/crop-gesture.d.ts +175 -0
  54. package/dist/crops/crop-gesture.d.ts.map +1 -0
  55. package/dist/crops/crop-gesture.js +188 -0
  56. package/dist/crops/index.d.ts +10 -0
  57. package/dist/crops/index.d.ts.map +1 -0
  58. package/dist/crops/index.js +5 -0
  59. package/dist/index.d.ts +19 -0
  60. package/dist/index.d.ts.map +1 -0
  61. package/dist/index.js +8 -0
  62. package/dist/surface-types.d.ts +41 -0
  63. package/dist/surface-types.d.ts.map +1 -0
  64. package/dist/surface-types.js +1 -0
  65. package/dist/worker/index.d.ts +4 -0
  66. package/dist/worker/index.d.ts.map +1 -0
  67. package/dist/worker/index.js +1 -0
  68. package/dist/worker/protocol.d.ts +28 -0
  69. package/dist/worker/protocol.d.ts.map +1 -0
  70. package/dist/worker/protocol.js +1 -0
  71. package/dist/worker/run-analysis.d.ts +21 -0
  72. package/dist/worker/run-analysis.d.ts.map +1 -0
  73. package/dist/worker/run-analysis.js +36 -0
  74. package/dist/worker/surface-worker.d.ts +2 -0
  75. package/dist/worker/surface-worker.d.ts.map +1 -0
  76. package/dist/worker/surface-worker.js +36 -0
  77. package/package.json +123 -3
@@ -0,0 +1,105 @@
1
+ // Measures how a square repeats: labels its `basis` (stage 5), and gates the lighting second chance in stage 3 (`repeatsAtLeast`).
2
+ import { thresholds } from "./thresholds.js";
3
+ /**
4
+ * The Pearson correlation of brightness between a square (`rect`, analysis pixels) and itself moved `shift`
5
+ * pixels along a row (or, if `vertical`, a column): 1 is a perfect match, 0 none.
6
+ */
7
+ const shiftedCorrelation = (data, width, rect, vertical, shift) => {
8
+ const luminance = (x, y) => {
9
+ const p = (y * width + x) * 4;
10
+ return data[p] * 0.299 + data[p + 1] * 0.587 + data[p + 2] * 0.114;
11
+ };
12
+ // Running sums for Pearson correlation between each sampled pixel (a) and the one `shift` along (b).
13
+ let n = 0;
14
+ let sumA = 0;
15
+ let sumAA = 0;
16
+ let sumAB = 0;
17
+ let sumB = 0;
18
+ let sumBB = 0;
19
+ for (let { y } = rect; y < rect.y + rect.height - (vertical ? shift : 0); y += thresholds.repetition.sampleStep) {
20
+ for (let { x } = rect; x < rect.x + rect.width - (vertical ? 0 : shift); x += thresholds.repetition.sampleStep) {
21
+ const a = luminance(x, y);
22
+ const b = luminance(x + (vertical ? 0 : shift), y + (vertical ? shift : 0));
23
+ sumA += a;
24
+ sumB += b;
25
+ sumAA += a * a;
26
+ sumBB += b * b;
27
+ sumAB += a * b;
28
+ n++;
29
+ }
30
+ }
31
+ const denominator = Math.sqrt((sumAA - (sumA * sumA) / n) * (sumBB - (sumB * sumB) / n));
32
+ return denominator > 0 ? (sumAB - (sumA * sumB) / n) / denominator : 0;
33
+ };
34
+ /**
35
+ * How well a square lines up with a shifted copy of itself, for every shift along a row (or, if `vertical`,
36
+ * a column) from 1 pixel up to a limit. `profile[i]` is the Pearson correlation of brightness at a shift of
37
+ * `i + 1` pixels (1 is a perfect match, 0 none). `rect` is in analysis pixels.
38
+ */
39
+ export const correlationProfile = (data, width, rect, vertical) => {
40
+ const maximum = Math.min(thresholds.repetition.maximumLag, Math.floor((vertical ? rect.height : rect.width) /
41
+ thresholds.repetition.cropLagDivisor));
42
+ return Array.from({ length: maximum }, (_, i) => shiftedCorrelation(data, width, rect, vertical, i + 1));
43
+ };
44
+ /**
45
+ * The shift (in pixels) at which a correlation profile recovers to a repeat, or null when it never does.
46
+ * A recovered peak after a trough is repetition; a smooth gradient alone isn't.
47
+ * `trough` is the lowest correlation seen between a shift of 2 and the one before the candidate. A peak must be strong, rise well above
48
+ * that trough, and be a local maximum (higher than the next shift).
49
+ */
50
+ export const repeatPeriod = (correlations) => {
51
+ let trough = 1;
52
+ // correlations[i] is the shift i + 1, so the first candidate peak sits at shift `minimumLag`.
53
+ for (let i = thresholds.repetition.minimumLag - 1; i < correlations.length - 1; i++) {
54
+ trough = Math.min(trough, correlations[i - 1]);
55
+ if (correlations[i] > thresholds.repetition.minimumPeak &&
56
+ correlations[i] - trough > thresholds.repetition.minimumRecovery &&
57
+ correlations[i] > correlations[i + 1]) {
58
+ return i + 1;
59
+ }
60
+ }
61
+ return null;
62
+ };
63
+ /**
64
+ * Labels a square (`rect`, in analysis pixels) as repeating when its brightness pattern lines up with a
65
+ * shifted copy of itself. For each shift along a row or a column, it measures how well the square
66
+ * correlates with itself (`correlationProfile`). A repeating pattern dips as the copy slides off
67
+ * and then recovers to a peak when the shift equals the pattern's period (`repeatPeriod`); a smooth gradient never recovers.
68
+ * Either direction is enough. This only sets the suggestion's `basis`; it never proposes or rejects a crop.
69
+ */
70
+ export function repeatingTexture(data, width, rect) {
71
+ for (const vertical of [false, true]) {
72
+ if (repeatPeriod(correlationProfile(data, width, rect, vertical)) !== null) {
73
+ return true;
74
+ }
75
+ }
76
+ return false;
77
+ }
78
+ /**
79
+ * Whether a square repeats at least as strongly as `peak`: some recovered peak (one that rises
80
+ * `minimumRecovery` above the trough before it and tops the next shift) reaches `peak`, along rows or
81
+ * columns, with shifts up to half the square so a print's period fits twice. A printed or woven pattern
82
+ * reaches 0.6 and more; a blend of different materials rarely lines up with a shifted copy of itself.
83
+ * Correlations are computed shift by shift and the search stops at the first qualifying peak.
84
+ */
85
+ export const repeatsAtLeast = (data, width, rect, peak) => {
86
+ const { maximumLag, minimumLag, minimumRecovery } = thresholds.repetition;
87
+ for (const vertical of [false, true]) {
88
+ const maximum = Math.min(maximumLag, Math.floor((vertical ? rect.height : rect.width) / 2));
89
+ const at = (shift) => shiftedCorrelation(data, width, rect, vertical, shift);
90
+ // Shifts `shift - 1`, `shift` and `shift + 1` are held as before, here and next.
91
+ let before = at(minimumLag - 1);
92
+ let here = at(minimumLag);
93
+ let trough = 1;
94
+ for (let shift = minimumLag; shift < maximum; shift += 1) {
95
+ const next = at(shift + 1);
96
+ trough = Math.min(trough, before);
97
+ if (here >= peak && here - trough > minimumRecovery && here > next) {
98
+ return true;
99
+ }
100
+ before = here;
101
+ here = next;
102
+ }
103
+ }
104
+ return false;
105
+ };
@@ -0,0 +1,26 @@
1
+ /** Merge limits for one segmentation pass, in units of the caller's edge distance. */
2
+ export interface SegmentationLimits {
3
+ /** Hard cap on the edge distance that may join two components. */
4
+ mergeLimit: number;
5
+ /** Adaptive size term: a component of n elements tolerates an extra scale / n of internal distance. */
6
+ componentScale: number;
7
+ }
8
+ /**
9
+ * Felzenszwalb-style graph segmentation over a columns x rows grid with 4-neighbor edges.
10
+ *
11
+ * The idea: every pair of side-by-side windows is an edge whose weight is how different they look. Edges
12
+ * are visited from most similar to least. An edge joins its two groups only if its weight is under
13
+ * `mergeLimit` and no bigger than the tighter of the two groups' tolerances, where a group's tolerance is
14
+ * its own largest internal weight plus `componentScale / size`. So a lone window joins easily, while a big
15
+ * group refuses anything much rougher than what already holds it together.
16
+ *
17
+ * Elements with `valid[i] === 0` (windows with transparent pixels) never join anything and are labeled -1.
18
+ * Other labels are the index of one member element of the group, not 0..n-1, and `sizes` maps each label to
19
+ * its element count. `distance(a, b)` is called with element indices (row * columns + column).
20
+ * @see https://cs.brown.edu/people/pfelzens/papers/seg-ijcv.pdf
21
+ */
22
+ export declare function segmentGrid(columns: number, rows: number, valid: Uint8Array, distance: (a: number, b: number) => number, limits: SegmentationLimits): {
23
+ labels: Int32Array;
24
+ sizes: Map<number, number>;
25
+ };
26
+ //# sourceMappingURL=segmentation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"segmentation.d.ts","sourceRoot":"","sources":["../../src/analysis/segmentation.ts"],"names":[],"mappings":"AAEA,sFAAsF;AACtF,MAAM,WAAW,kBAAkB;IACjC,kEAAkE;IAClE,UAAU,EAAE,MAAM,CAAC;IACnB,uGAAuG;IACvG,cAAc,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,WAAW,CACzB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,UAAU,EACjB,QAAQ,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM,EAC1C,MAAM,EAAE,kBAAkB,GACzB;IAAE,MAAM,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,CAoEpD"}
@@ -0,0 +1,81 @@
1
+ // Stage 2 of the pipeline in analyze-surface-regions.ts: groups a grid of windows into segments of similar neighbors.
2
+ /**
3
+ * Felzenszwalb-style graph segmentation over a columns x rows grid with 4-neighbor edges.
4
+ *
5
+ * The idea: every pair of side-by-side windows is an edge whose weight is how different they look. Edges
6
+ * are visited from most similar to least. An edge joins its two groups only if its weight is under
7
+ * `mergeLimit` and no bigger than the tighter of the two groups' tolerances, where a group's tolerance is
8
+ * its own largest internal weight plus `componentScale / size`. So a lone window joins easily, while a big
9
+ * group refuses anything much rougher than what already holds it together.
10
+ *
11
+ * Elements with `valid[i] === 0` (windows with transparent pixels) never join anything and are labeled -1.
12
+ * Other labels are the index of one member element of the group, not 0..n-1, and `sizes` maps each label to
13
+ * its element count. `distance(a, b)` is called with element indices (row * columns + column).
14
+ * @see https://cs.brown.edu/people/pfelzens/papers/seg-ijcv.pdf
15
+ */
16
+ export function segmentGrid(columns, rows, valid, distance, limits) {
17
+ const count = columns * rows;
18
+ const from = [];
19
+ const to = [];
20
+ const weights = [];
21
+ for (let y = 0; y < rows; y++) {
22
+ for (let x = 0; x < columns; x++) {
23
+ const a = y * columns + x;
24
+ if (!valid[a]) {
25
+ continue;
26
+ }
27
+ for (const b of [
28
+ x + 1 < columns ? a + 1 : -1,
29
+ y + 1 < rows ? a + columns : -1,
30
+ ]) {
31
+ if (b < 0 || !valid[b]) {
32
+ continue;
33
+ }
34
+ from.push(a);
35
+ to.push(b);
36
+ weights.push(distance(a, b));
37
+ }
38
+ }
39
+ }
40
+ // Sorting indexes keeps ties in scan order.
41
+ const order = [...weights.keys()].sort((a, b) => weights[a] - weights[b] || a - b);
42
+ const parents = Int32Array.from({ length: count }, (_, i) => i);
43
+ const sizes = new Int32Array(count).fill(1);
44
+ // The largest edge weight used to build each group so far (its roughness), stored at the group's root.
45
+ const internal = new Float64Array(count);
46
+ /** Path compression makes later lookups skip already traversed parent chains. */
47
+ const root = (element) => {
48
+ while (parents[element] !== element) {
49
+ parents[element] = parents[parents[element]];
50
+ element = parents[element];
51
+ }
52
+ return element;
53
+ };
54
+ for (const index of order) {
55
+ let a = root(from[index]);
56
+ let b = root(to[index]);
57
+ const weight = weights[index];
58
+ if (a === b ||
59
+ weight >= limits.mergeLimit ||
60
+ weight >
61
+ Math.min(internal[a] + limits.componentScale / sizes[a], internal[b] + limits.componentScale / sizes[b])) {
62
+ continue;
63
+ }
64
+ // The smaller group joins the bigger, so the bigger group's root stays the label.
65
+ if (sizes[a] < sizes[b]) {
66
+ [a, b] = [b, a];
67
+ }
68
+ parents[b] = a;
69
+ sizes[a] += sizes[b];
70
+ internal[a] = Math.max(weight, internal[a], internal[b]);
71
+ }
72
+ const labels = new Int32Array(count);
73
+ const regionSizes = new Map();
74
+ for (let i = 0; i < count; i++) {
75
+ labels[i] = valid[i] ? root(i) : -1;
76
+ if (labels[i] >= 0) {
77
+ regionSizes.set(labels[i], (regionSizes.get(labels[i]) ?? 0) + 1);
78
+ }
79
+ }
80
+ return { labels, sizes: regionSizes };
81
+ }
@@ -0,0 +1,34 @@
1
+ import type { Rectangle, PercentRectangle } from "./types.js";
2
+ /**
3
+ * Overlap area as a fraction (0-1) of the smaller crop's area: catches a crop sitting inside a bigger one
4
+ * as well as two equal crops. Takes percent coordinates, so both crops must come from the same photo.
5
+ */
6
+ export declare function overlappingCrops(a: PercentRectangle, b: PercentRectangle): number;
7
+ /** How big a region is and where: its pixel count and bounding box in analysis pixels, with `x1` and `y1` inclusive. */
8
+ export interface RegionStats {
9
+ count: number;
10
+ x0: number;
11
+ y0: number;
12
+ x1: number;
13
+ y1: number;
14
+ }
15
+ /**
16
+ * Pixel count and bounding box of each label in a label map (one label per pixel, -1 for none),
17
+ * in one pass. Lets later steps search only inside a region's box instead of the whole image.
18
+ */
19
+ export declare function regionStats(labels: Int32Array, width: number, height: number): Map<number, RegionStats>;
20
+ /**
21
+ * Finds the largest square inside a region after `erosion` analysis pixels of erosion: a pixel counts
22
+ * only if every pixel within `erosion` of it also belongs to the region. The square stays inside the
23
+ * region and never covers its holes. `bounds` limits the search to the region's bounding box.
24
+ * Returns a zero-sized rectangle when nothing survives the erosion.
25
+ *
26
+ * Classic dynamic program: each cell holds the side of the biggest square whose bottom-right corner is
27
+ * that cell, which is one more than the smallest of its left, top and top-left neighbors' squares.
28
+ * Only the previous row is kept.
29
+ *
30
+ * Pass `table` (width x height zeros) to also receive every cell's value, which is the side of the
31
+ * largest square ending there: a heat map of "how big a square fits here". The result is the same either way.
32
+ */
33
+ export declare function largestInteriorSquare(labels: Int32Array, region: number, width: number, height: number, erosion: number, bounds: RegionStats, table?: Int32Array): Rectangle;
34
+ //# sourceMappingURL=square-geometry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"square-geometry.d.ts","sourceRoot":"","sources":["../../src/analysis/square-geometry.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9D;;;GAGG;AACH,wBAAgB,gBAAgB,CAC9B,CAAC,EAAE,gBAAgB,EACnB,CAAC,EAAE,gBAAgB,GAClB,MAAM,CAKR;AAED,wHAAwH;AACxH,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,EAAE,EAAE,MAAM,CAAC;IACX,EAAE,EAAE,MAAM,CAAC;IACX,EAAE,EAAE,MAAM,CAAC;IACX,EAAE,EAAE,MAAM,CAAC;CACZ;AAED;;;GAGG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,UAAU,EAClB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,GACb,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,CAiB1B;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,UAAU,EAClB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,WAAW,EACnB,KAAK,CAAC,EAAE,UAAU,GACjB,SAAS,CA2CX"}
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Overlap area as a fraction (0-1) of the smaller crop's area: catches a crop sitting inside a bigger one
3
+ * as well as two equal crops. Takes percent coordinates, so both crops must come from the same photo.
4
+ */
5
+ export function overlappingCrops(a, b) {
6
+ const overlap = Math.max(0, Math.min(a.x + a.width, b.x + b.width) - Math.max(a.x, b.x)) *
7
+ Math.max(0, Math.min(a.y + a.height, b.y + b.height) - Math.max(a.y, b.y));
8
+ return overlap / Math.min(a.width * a.height, b.width * b.height);
9
+ }
10
+ /**
11
+ * Pixel count and bounding box of each label in a label map (one label per pixel, -1 for none),
12
+ * in one pass. Lets later steps search only inside a region's box instead of the whole image.
13
+ */
14
+ export function regionStats(labels, width, height) {
15
+ const stats = new Map();
16
+ for (let y = 0; y < height; y++) {
17
+ for (let x = 0; x < width; x++) {
18
+ const label = labels[y * width + x];
19
+ if (label < 0) {
20
+ continue;
21
+ }
22
+ const entry = stats.get(label);
23
+ if (entry) {
24
+ entry.count++;
25
+ if (x < entry.x0)
26
+ entry.x0 = x;
27
+ if (x > entry.x1)
28
+ entry.x1 = x;
29
+ if (y > entry.y1)
30
+ entry.y1 = y;
31
+ }
32
+ else {
33
+ stats.set(label, { count: 1, x0: x, y0: y, x1: x, y1: y });
34
+ }
35
+ }
36
+ }
37
+ return stats;
38
+ }
39
+ /**
40
+ * Finds the largest square inside a region after `erosion` analysis pixels of erosion: a pixel counts
41
+ * only if every pixel within `erosion` of it also belongs to the region. The square stays inside the
42
+ * region and never covers its holes. `bounds` limits the search to the region's bounding box.
43
+ * Returns a zero-sized rectangle when nothing survives the erosion.
44
+ *
45
+ * Classic dynamic program: each cell holds the side of the biggest square whose bottom-right corner is
46
+ * that cell, which is one more than the smallest of its left, top and top-left neighbors' squares.
47
+ * Only the previous row is kept.
48
+ *
49
+ * Pass `table` (width x height zeros) to also receive every cell's value, which is the side of the
50
+ * largest square ending there: a heat map of "how big a square fits here". The result is the same either way.
51
+ */
52
+ export function largestInteriorSquare(labels, region, width, height, erosion, bounds, table) {
53
+ let previous = new Int32Array(width + 1);
54
+ let current = new Int32Array(width + 1);
55
+ let best = { height: 0, width: 0, x: 0, y: 0 };
56
+ const xa = Math.max(erosion, bounds.x0);
57
+ const xb = Math.min(width - erosion - 1, bounds.x1);
58
+ for (let y = Math.max(erosion, bounds.y0); y <= Math.min(height - erosion - 1, bounds.y1); y++) {
59
+ current.fill(0);
60
+ for (let x = xa; x <= xb; x++) {
61
+ let inside = true;
62
+ for (let dy = -erosion; dy <= erosion && inside; dy++) {
63
+ for (let dx = -erosion; dx <= erosion; dx++) {
64
+ if (labels[(y + dy) * width + x + dx] !== region) {
65
+ inside = false;
66
+ break;
67
+ }
68
+ }
69
+ }
70
+ if (!inside) {
71
+ continue;
72
+ }
73
+ // `x - 1` is -1 at column 0 only when erosion is 0; the `?? 0` covers that edge.
74
+ current[x] =
75
+ 1 + Math.min(previous[x], previous[x - 1] ?? 0, current[x - 1] ?? 0);
76
+ if (table !== undefined) {
77
+ table[y * width + x] = current[x];
78
+ }
79
+ if (current[x] > best.width) {
80
+ best = {
81
+ height: current[x],
82
+ width: current[x],
83
+ x: x - current[x] + 1,
84
+ y: y - current[x] + 1,
85
+ };
86
+ }
87
+ }
88
+ [previous, current] = [current, previous];
89
+ }
90
+ return best;
91
+ }
@@ -0,0 +1,45 @@
1
+ import type { Descriptor, Signature } from "./descriptors.js";
2
+ import type { Diagnostics } from "./diagnostics.js";
3
+ import type { AnalysisImage, SurfaceSuggestion } from "./types.js";
4
+ /** A proposal with the descriptor of its square, kept so ranking can compare crops in feature space. */
5
+ export interface Candidate {
6
+ /** The finished suggestion minus its outline, which is built later (see `outline`); its `id` is provisional. */
7
+ suggestion: Omit<SurfaceSuggestion, "path">;
8
+ /** Description of the whole square (analysis pixels), as opposed to the small windows inside it. */
9
+ descriptor: Descriptor;
10
+ /** Lighting-tolerant description of the square, for deciding whether two crops are one material. */
11
+ signature: Signature;
12
+ /** Size x homogeneity x quality with homogeneity weighted heavily: which of two look-alike crops to keep. */
13
+ representative: number;
14
+ /** Builds the smooth outline; deferred because only ranked survivors need one. */
15
+ outline: () => string;
16
+ }
17
+ /**
18
+ * Proposes squares of one consistent surface at several window scales.
19
+ * Each scale segments a grid of overlapping windows by descriptor distance, fits the largest square in
20
+ * every segment, and keeps it only if windows tiling the square agree with the square as a whole.
21
+ * A square must hold at least `minimumTiles` windows per side, so it contains repeats of any period a window spans;
22
+ * the scale at which a surface scores best is chosen later by ranking.
23
+ *
24
+ * Returns many overlapping candidates, unsorted: the same place usually appears once per scale, and once
25
+ * more if a union of segments also passed. Ranking tidies that up.
26
+ */
27
+ export declare function surfaceCandidates(image: AnalysisImage, diagnostics?: Diagnostics): Candidate[];
28
+ /**
29
+ * The window sides tried when testing a square of `width` pixels: the fixed scales plus the largest that
30
+ * still tiles it, smallest first. Coarse patterns and veined stone only agree once windows are a large
31
+ * share of the square, whatever its size in pixels.
32
+ */
33
+ export declare const stationaritySides: (width: number) => number[];
34
+ /**
35
+ * The distance no window may exceed at this many tiles per side. Fewer tiles means weaker evidence, so the
36
+ * limit is shaved: x0.8 at two tiles, x0.9 at three, and so on.
37
+ */
38
+ export declare const stationarityLimit: (tiles: number) => number;
39
+ /**
40
+ * Where the sampled windows start along one side of a square `width` pixels wide, for windows of `side`
41
+ * pixels. At most `maximumTilesPerSide` windows are spread from edge to edge (with gaps if the square holds more);
42
+ * the same offsets serve rows and columns. Needs at least `minimumTiles` windows to fit.
43
+ */
44
+ export declare const tileOffsets: (width: number, side: number) => number[];
45
+ //# sourceMappingURL=surface-candidates.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"surface-candidates.d.ts","sourceRoot":"","sources":["../../src/analysis/surface-candidates.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EACV,UAAU,EAEV,SAAS,EAEV,MAAM,kBAAkB,CAAC;AAE1B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAOpD,OAAO,KAAK,EAAE,aAAa,EAAa,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAE9E,wGAAwG;AACxG,MAAM,WAAW,SAAS;IACxB,gHAAgH;IAChH,UAAU,EAAE,IAAI,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC;IAC5C,oGAAoG;IACpG,UAAU,EAAE,UAAU,CAAC;IACvB,oGAAoG;IACpG,SAAS,EAAE,SAAS,CAAC;IACrB,6GAA6G;IAC7G,cAAc,EAAE,MAAM,CAAC;IACvB,kFAAkF;IAClF,OAAO,EAAE,MAAM,MAAM,CAAC;CACvB;AAED;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,aAAa,EACpB,WAAW,CAAC,EAAE,WAAW,GACxB,SAAS,EAAE,CAoGb;AAgJD;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,UAAW,MAAM,KAAG,MAAM,EAKvD,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,iBAAiB,UAAW,MAAM,KAAG,MAEO,CAAC;AAE1D;;;;GAIG;AACH,eAAO,MAAM,WAAW,UAAW,MAAM,QAAQ,MAAM,KAAG,MAAM,EAQ/D,CAAC"}