@olenbetong/synergi-react 5.0.0 → 5.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 (141) hide show
  1. package/es/components/Checkbox/index.js +1 -9
  2. package/es/components/CodeEditor/CodeEditor.css +6 -6
  3. package/es/components/FilePond.js +3 -12
  4. package/es/components/ImageEditor/plugins/redactPlugin/useRedactTool.js +18 -14
  5. package/es/components/ImageEditor/useImageHistory.js +13 -1
  6. package/es/components/MergeProgressDialog/MergeProgressDialogDs.js +1 -1
  7. package/es/components/MergeProgressDialog/useMergeJob.js +5 -4
  8. package/es/components/Portal/index.js +20 -20
  9. package/es/components/SplitContainer/index.js +3 -1
  10. package/es/components/Toolbar/Toolbar.css +137 -137
  11. package/es/components/Toolbar/index.js +6 -1
  12. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.js +8 -0
  13. package/es/elements/ElementLookupDialog/HIDScanner/useHIDScanner.js +3 -1
  14. package/es/elements-ds/DescriptionInput.d.ts +16 -0
  15. package/es/elements-ds/DescriptionInput.js +41 -0
  16. package/es/elements-ds/DuplicateElementDamage.d.ts +40 -0
  17. package/es/elements-ds/DuplicateElementDamage.js +57 -0
  18. package/es/elements-ds/ElementLookup.d.ts +23 -0
  19. package/es/elements-ds/ElementLookup.js +35 -0
  20. package/es/elements-ds/ElementLookupDialog.d.ts +7 -0
  21. package/es/elements-ds/ElementLookupDialog.js +84 -0
  22. package/es/elements-ds/FormFields.d.ts +43 -0
  23. package/es/elements-ds/FormFields.js +67 -0
  24. package/es/elements-ds/HIDScanner.d.ts +7 -0
  25. package/es/elements-ds/HIDScanner.js +36 -0
  26. package/es/elements-ds/ManualForm.d.ts +7 -0
  27. package/es/elements-ds/ManualForm.js +70 -0
  28. package/es/elements-ds/ProjectSelect.d.ts +28 -0
  29. package/es/elements-ds/ProjectSelect.js +45 -0
  30. package/es/elements-ds/elements-ds.css +157 -0
  31. package/es/elements-ds/index.d.ts +12 -0
  32. package/es/elements-ds/index.js +14 -0
  33. package/es/useTranslation/index.js +5 -3
  34. package/package.json +15 -3
  35. package/src/chrome/AppShell/AppShell.test.tsx +48 -0
  36. package/src/chrome/AppShell/index.tsx +29 -29
  37. package/src/chrome/openReport.test.ts +50 -0
  38. package/src/chrome/openReport.ts +34 -34
  39. package/src/colorScheme.test.ts +188 -0
  40. package/src/colorScheme.ts +125 -125
  41. package/src/components/Checkbox/Checkbox.browser.test.tsx +116 -0
  42. package/src/components/Checkbox/index.tsx +1 -10
  43. package/src/components/CodeEditor/CodeEditor.css +6 -6
  44. package/src/components/CodeEditor/CodeEditor.tsx +173 -173
  45. package/src/components/CodeEditor/index.ts +1 -1
  46. package/src/components/ColorCard/ColorCard.test.tsx +34 -0
  47. package/src/components/DateNavigator/DateNavigator.test.tsx +175 -0
  48. package/src/components/DateNavigator/index.tsx +186 -186
  49. package/src/components/FilePond.browser.test.tsx +389 -0
  50. package/src/components/FilePond.tsx +303 -312
  51. package/src/components/ImageEditor/ImageEditor.browser.test.tsx +286 -0
  52. package/src/components/ImageEditor/ImageEditor.test.tsx +299 -0
  53. package/src/components/ImageEditor/ImageEditor.tsx +348 -348
  54. package/src/components/ImageEditor/index.ts +9 -9
  55. package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +165 -165
  56. package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +48 -48
  57. package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.test.ts +225 -0
  58. package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +405 -405
  59. package/src/components/ImageEditor/plugins/plugins.browser.test.ts +252 -0
  60. package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
  61. package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.test.ts +89 -0
  62. package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.ts +16 -13
  63. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
  64. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
  65. package/src/components/ImageEditor/plugins/zoomPlugin/useZoomPan.test.ts +154 -0
  66. package/src/components/ImageEditor/useActiveTool.ts +21 -21
  67. package/src/components/ImageEditor/useEdgeDetection.test.ts +96 -0
  68. package/src/components/ImageEditor/useEdgeDetection.ts +319 -319
  69. package/src/components/ImageEditor/useImageHistory.test.ts +409 -0
  70. package/src/components/ImageEditor/useImageHistory.ts +206 -197
  71. package/src/components/ImageEditor/useOpenCV.test.ts +208 -0
  72. package/src/components/ImageEditor/useOpenCV.ts +184 -184
  73. package/src/components/LinkedCardList/index.scss +95 -95
  74. package/src/components/MergeProgressDialog/MergeProgressDialogDs.browser.test.tsx +156 -0
  75. package/src/components/MergeProgressDialog/MergeProgressDialogDs.tsx +1 -0
  76. package/src/components/MergeProgressDialog/useMergeJob.test.ts +326 -0
  77. package/src/components/MergeProgressDialog/useMergeJob.ts +4 -3
  78. package/src/components/PageBanner/index.tsx +80 -80
  79. package/src/components/PdfViewer/PdfViewer.browser.test.tsx +78 -0
  80. package/src/components/PdfViewer/PdfViewer.tsx +158 -158
  81. package/src/components/Portal/Portal.test.tsx +106 -0
  82. package/src/components/Portal/index.tsx +22 -25
  83. package/src/components/Sidebar/Sidebar.browser.test.tsx +57 -0
  84. package/src/components/Sidebar/Sidebar.test.tsx +123 -0
  85. package/src/components/SplitContainer/SplitContainer.browser.test.tsx +304 -0
  86. package/src/components/SplitContainer/index.tsx +335 -333
  87. package/src/components/Toolbar/Toolbar.css +137 -137
  88. package/src/components/Toolbar/Toolbar.test.tsx +216 -0
  89. package/src/components/Toolbar/ToolbarBreadcrumbs.browser.test.tsx +137 -0
  90. package/src/components/Toolbar/index.tsx +270 -265
  91. package/src/components/ValueToggleGroup/ValueToggleGroup.test.tsx +112 -0
  92. package/src/designsystemet-web-types.d.ts +24 -24
  93. package/src/elements/ElementDamage/DamageFormButtons.tsx +35 -35
  94. package/src/elements/ElementDamage/DescriptionInput.tsx +120 -120
  95. package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +82 -82
  96. package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +67 -67
  97. package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +45 -45
  98. package/src/elements/ElementDamage/ElementNumberTextField.tsx +41 -41
  99. package/src/elements/ElementDamage/RegisterFix.tsx +100 -100
  100. package/src/elements/ElementDamage/SubmitButtons.tsx +40 -40
  101. package/src/elements/ElementDamage/TransportElementSelect.tsx +52 -52
  102. package/src/elements/ElementLookup.tsx +123 -123
  103. package/src/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.browser.test.tsx +175 -0
  104. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +8 -0
  105. package/src/elements/ElementLookupDialog/BarcodeScanner/engines.browser.test.ts +225 -0
  106. package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
  107. package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.test.ts +124 -0
  108. package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.ts +2 -1
  109. package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
  110. package/src/elements/ElementLookupDialog/getElement.test.ts +133 -0
  111. package/src/elements/ElementLookupDialog/index.tsx +226 -226
  112. package/src/elements/ElementLookupDialog/utils/Giai.test.ts +53 -0
  113. package/src/elements/ElementLookupDialog/utils/Giai.ts +209 -209
  114. package/src/elements/ProjectSelect.tsx +130 -130
  115. package/src/elements/index.ts +15 -15
  116. package/src/elements-ds/DamageForm.browser.test.tsx +530 -0
  117. package/src/elements-ds/DescriptionInput.tsx +114 -0
  118. package/src/elements-ds/DuplicateElementDamage.tsx +185 -0
  119. package/src/elements-ds/ElementLookup.tsx +114 -0
  120. package/src/elements-ds/ElementLookupDialog.browser.test.tsx +207 -0
  121. package/src/elements-ds/ElementLookupDialog.tsx +196 -0
  122. package/src/elements-ds/FormFields.tsx +176 -0
  123. package/src/elements-ds/HIDScanner.tsx +53 -0
  124. package/src/elements-ds/ManualForm.tsx +129 -0
  125. package/src/elements-ds/ProjectSelect.tsx +120 -0
  126. package/src/elements-ds/elements-ds.css +157 -0
  127. package/src/elements-ds/index.ts +16 -0
  128. package/src/filter/index.ts +34 -34
  129. package/src/i18n.test.ts +31 -0
  130. package/src/i18n.ts +13 -13
  131. package/src/mui/MuiThemeProvider.tsx +135 -135
  132. package/src/test/fakeCamera.ts +98 -0
  133. package/src/test/fakeDataApi.ts +90 -0
  134. package/src/test/setup.browser.ts +21 -0
  135. package/src/test/setup.ts +23 -23
  136. package/src/useTranslation/index.ts +5 -3
  137. package/src/useTranslation/useTranslation.test.ts +128 -0
  138. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.d.ts +0 -1
  139. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.js +0 -103
  140. package/es/test/setup.d.ts +0 -6
  141. package/es/test/setup.js +0 -6
@@ -1,319 +1,319 @@
1
- import { useEffect, useState } from "react";
2
-
3
- export interface Point {
4
- x: number;
5
- y: number;
6
- }
7
-
8
- /** TL, TR, BR, BL */
9
- export type Quad = [Point, Point, Point, Point];
10
-
11
- export function fullImageQuad(width: number, height: number): Quad {
12
- return [
13
- { x: 0, y: 0 },
14
- { x: width, y: 0 },
15
- { x: width, y: height },
16
- { x: 0, y: height },
17
- ];
18
- }
19
-
20
- /** Longest side of the downscaled working image. Detection is scale-dependent, so it is fixed. */
21
- const WORK_SIZE = 512;
22
- /** Closing kernel that erases dark print (text, thin rules) so only surface boundaries remain. */
23
- const CLOSE_KERNEL = 5;
24
- /** Candidate lines kept per orientation family. The search is O(n⁴), so keep this small. */
25
- const MAX_LINES_PER_FAMILY = 14;
26
- /** Distances (working pixels) sampled on each side of a candidate edge. */
27
- const SAMPLE_OFFSET_MIN = 3;
28
- const SAMPLE_OFFSET_MAX = 9;
29
- /** Minimum Lab colour difference between the two sides for an edge sample to count. */
30
- const MIN_CONTRAST = 8;
31
- /** Cost of an unsupported stretch of side, relative to the gain of a supported one. */
32
- const UNSUPPORTED_COST = 0.5;
33
- /** Cost per pixel of using the image border as a side (document running off the frame). */
34
- const BORDER_COST = 0.1;
35
- /** Minimum normalised score for a suggestion; below it the full image is used. */
36
- const MIN_SCORE = 0.3;
37
- const MIN_AREA_FRACTION = 0.1;
38
-
39
- /** A line in normal form: x·cosθ + y·sinθ = ρ. */
40
- interface Line {
41
- theta: number;
42
- rho: number;
43
- weight: number;
44
- border?: boolean;
45
- }
46
-
47
- /**
48
- * Finds the most likely document outline in an RGBA image.
49
- *
50
- * Print is first removed with a morphological close, so text and table rules do not
51
- * compete with the paper's outline. Straight segments from a Hough transform are merged
52
- * into candidate lines, and every combination of two near-vertical and two near-horizontal
53
- * lines (the image borders included, for receipts that run off the photo) is scored by how
54
- * much of each side both lies on an edge and separates two differently coloured surfaces.
55
- * Scoring by supported length, rather than by the share of each side, lets a long receipt
56
- * whose edges bend at a fold beat a small, perfectly straight section of it.
57
- */
58
- // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
59
- export function findDocumentQuad(cv: any, src: any): Quad | null {
60
- let scale = Math.min(1, WORK_SIZE / Math.max(src.cols, src.rows));
61
- let small = new cv.Mat();
62
- let rgb = new cv.Mat();
63
- let colour = new cv.Mat();
64
- let lab = new cv.Mat();
65
- let gray = new cv.Mat();
66
- let edges = new cv.Mat();
67
- let support = new cv.Mat();
68
- let segments = new cv.Mat();
69
-
70
- try {
71
- cv.resize(src, small, new cv.Size(Math.round(src.cols * scale), Math.round(src.rows * scale)), 0, 0, cv.INTER_AREA);
72
- let w = small.cols;
73
- let h = small.rows;
74
- let minDim = Math.min(w, h);
75
-
76
- cv.cvtColor(small, rgb, cv.COLOR_RGBA2RGB);
77
- let closeKernel = cv.getStructuringElement(cv.MORPH_RECT, new cv.Size(CLOSE_KERNEL, CLOSE_KERNEL));
78
- cv.morphologyEx(rgb, rgb, cv.MORPH_CLOSE, closeKernel);
79
- closeKernel.delete();
80
- cv.GaussianBlur(rgb, colour, new cv.Size(5, 5), 0);
81
- cv.cvtColor(colour, lab, cv.COLOR_RGB2Lab);
82
- // Blurred after the grey conversion, not taken from `colour`: the rounding differs,
83
- // and the weaker receipt edges sit close enough to the Hough threshold to notice.
84
- cv.cvtColor(rgb, gray, cv.COLOR_RGB2GRAY);
85
- cv.GaussianBlur(gray, gray, new cv.Size(5, 5), 0);
86
-
87
- // Low thresholds: a receipt on a white table has very little contrast.
88
- cv.Canny(gray, edges, 20, 60);
89
- // A tolerant copy for scoring, so a side a pixel or two off its edge still counts.
90
- let supportKernel = cv.getStructuringElement(cv.MORPH_RECT, new cv.Size(5, 5));
91
- cv.dilate(edges, support, supportKernel);
92
- supportKernel.delete();
93
- cv.HoughLinesP(edges, segments, 1, Math.PI / 180, 30, minDim * 0.12, 8);
94
-
95
- let { vertical, horizontal } = groupLines(segments, minDim);
96
- vertical.push({ theta: 0, rho: 0, weight: 0, border: true }, { theta: 0, rho: w - 1, weight: 0, border: true });
97
- horizontal.push(
98
- { theta: Math.PI / 2, rho: 0, weight: 0, border: true },
99
- { theta: Math.PI / 2, rho: h - 1, weight: 0, border: true },
100
- );
101
-
102
- let labData: Uint8Array = lab.data;
103
- let supportData: Uint8Array = support.data;
104
- let labAt = (x: number, y: number, channel: number) => {
105
- let xi = Math.min(w - 1, Math.max(0, Math.round(x)));
106
- let yi = Math.min(h - 1, Math.max(0, Math.round(y)));
107
- return labData[(yi * w + xi) * 3 + channel];
108
- };
109
-
110
- /** Share of a side that lies on an edge with different surfaces on either side of it. */
111
- let coverage = (a: Point, b: Point) => {
112
- let len = distance(a, b);
113
- let n = Math.max(8, Math.round(len / 3));
114
- let nx = -(b.y - a.y) / len;
115
- let ny = (b.x - a.x) / len;
116
- let hits = 0;
117
- for (let i = 1; i < n; i++) {
118
- let px = a.x + ((b.x - a.x) * i) / n;
119
- let py = a.y + ((b.y - a.y) * i) / n;
120
- let x = Math.round(px);
121
- let y = Math.round(py);
122
- if (x < 0 || y < 0 || x >= w || y >= h || !supportData[y * w + x]) continue;
123
-
124
- let diff = [0, 0, 0];
125
- let count = 0;
126
- for (let d = SAMPLE_OFFSET_MIN; d <= SAMPLE_OFFSET_MAX; d += 2) {
127
- for (let c = 0; c < 3; c++) {
128
- diff[c] += labAt(px + nx * d, py + ny * d, c) - labAt(px - nx * d, py - ny * d, c);
129
- }
130
- count++;
131
- }
132
- // Chroma weighs double: a cream receipt on a grey-white table differs mostly in tint.
133
- if (Math.hypot(diff[0] / count, (2 * diff[1]) / count, (2 * diff[2]) / count) > MIN_CONTRAST) hits++;
134
- }
135
- return hits / (n - 1);
136
- };
137
-
138
- let margin = minDim * 0.02;
139
- let inBounds = (p: Point | null): p is Point =>
140
- p !== null && p.x >= -margin && p.y >= -margin && p.x <= w + margin && p.y <= h + margin;
141
- let minGap = minDim * 0.1;
142
-
143
- let best: Quad | null = null;
144
- let bestScore = -Infinity;
145
- for (let left of vertical) {
146
- for (let right of vertical) {
147
- if (xAt(left, h / 2) >= xAt(right, h / 2) - minGap) continue;
148
- for (let top of horizontal) {
149
- for (let bottom of horizontal) {
150
- if (yAt(top, w / 2) >= yAt(bottom, w / 2) - minGap) continue;
151
- let sides = [top, right, bottom, left];
152
- if (sides.filter((l) => l.border).length > 2) continue;
153
-
154
- let corners = [
155
- intersect(left, top),
156
- intersect(right, top),
157
- intersect(right, bottom),
158
- intersect(left, bottom),
159
- ];
160
- if (!corners.every(inBounds)) continue;
161
- let quad = corners as Quad;
162
- if (polygonArea(quad) < w * h * MIN_AREA_FRACTION || !isConvex(quad)) continue;
163
-
164
- let score = 0;
165
- for (let s = 0; s < 4; s++) {
166
- let a = quad[s];
167
- let b = quad[(s + 1) % 4];
168
- let len = distance(a, b);
169
- if (sides[s].border) {
170
- score -= BORDER_COST * len;
171
- } else {
172
- let cov = coverage(a, b);
173
- score += len * (cov - UNSUPPORTED_COST * (1 - cov));
174
- }
175
- }
176
- score /= w + h;
177
- if (score > bestScore) {
178
- bestScore = score;
179
- best = quad;
180
- }
181
- }
182
- }
183
- }
184
- }
185
-
186
- if (!best || bestScore < MIN_SCORE) return null;
187
- return best.map((p) => ({
188
- x: Math.min(src.cols, Math.max(0, p.x / scale)),
189
- y: Math.min(src.rows, Math.max(0, p.y / scale)),
190
- })) as Quad;
191
- } finally {
192
- for (let mat of [small, rgb, colour, lab, gray, edges, support, segments]) mat.delete();
193
- }
194
- }
195
-
196
- /**
197
- * Merges Hough segments that lie on the same line and splits the result into near-vertical
198
- * and near-horizontal families, each ordered by total segment length.
199
- */
200
- // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
201
- function groupLines(segments: any, minDim: number): { vertical: Line[]; horizontal: Line[] } {
202
- let lines: Line[] = [];
203
- let maxAngle = (3 * Math.PI) / 180;
204
- let maxOffset = minDim * 0.015;
205
-
206
- // Count from the data, not `rows`: OpenCV 4 returns an N×1 matrix of segments and
207
- // OpenCV 5 a 1×N one, and the loader may end up with either build.
208
- let data: Int32Array = segments.data32S;
209
- for (let i = 0; i + 3 < data.length; i += 4) {
210
- let [x1, y1, x2, y2] = data.subarray(i, i + 4);
211
- let len = Math.hypot(x2 - x1, y2 - y1);
212
- // Normal of the segment, folded into [0, π)
213
- let theta = Math.atan2(x1 - x2, y2 - y1);
214
- if (theta < 0) theta += Math.PI;
215
- if (theta >= Math.PI) theta -= Math.PI;
216
- let rho = x1 * Math.cos(theta) + y1 * Math.sin(theta);
217
-
218
- let match = lines.find((l) => Math.abs(l.theta - theta) < maxAngle && Math.abs(l.rho - rho) < maxOffset);
219
- if (match) {
220
- let total = match.weight + len;
221
- match.theta = (match.theta * match.weight + theta * len) / total;
222
- match.rho = (match.rho * match.weight + rho * len) / total;
223
- match.weight = total;
224
- } else {
225
- lines.push({ theta, rho, weight: len });
226
- }
227
- }
228
-
229
- lines.sort((a, b) => b.weight - a.weight);
230
- let isVertical = (l: Line) => l.theta < Math.PI / 4 || l.theta > (3 * Math.PI) / 4;
231
- return {
232
- vertical: lines.filter(isVertical).slice(0, MAX_LINES_PER_FAMILY),
233
- horizontal: lines.filter((l) => !isVertical(l)).slice(0, MAX_LINES_PER_FAMILY),
234
- };
235
- }
236
-
237
- /** x where a near-vertical line crosses the given y. */
238
- function xAt(line: Line, y: number): number {
239
- return (line.rho - y * Math.sin(line.theta)) / Math.cos(line.theta);
240
- }
241
-
242
- /** y where a near-horizontal line crosses the given x. */
243
- function yAt(line: Line, x: number): number {
244
- return (line.rho - x * Math.cos(line.theta)) / Math.sin(line.theta);
245
- }
246
-
247
- function intersect(a: Line, b: Line): Point | null {
248
- let ca = Math.cos(a.theta);
249
- let sa = Math.sin(a.theta);
250
- let cb = Math.cos(b.theta);
251
- let sb = Math.sin(b.theta);
252
- let det = ca * sb - sa * cb;
253
- if (Math.abs(det) < 1e-6) return null;
254
- return { x: (a.rho * sb - b.rho * sa) / det, y: (ca * b.rho - cb * a.rho) / det };
255
- }
256
-
257
- function distance(a: Point, b: Point): number {
258
- return Math.hypot(b.x - a.x, b.y - a.y);
259
- }
260
-
261
- function polygonArea(points: Point[]): number {
262
- let area = 0;
263
- for (let i = 0; i < points.length; i++) {
264
- let p = points[i];
265
- let q = points[(i + 1) % points.length];
266
- area += p.x * q.y - q.x * p.y;
267
- }
268
- return Math.abs(area) / 2;
269
- }
270
-
271
- function isConvex(points: Point[]): boolean {
272
- let sign = 0;
273
- for (let i = 0; i < points.length; i++) {
274
- let a = points[i];
275
- let b = points[(i + 1) % points.length];
276
- let c = points[(i + 2) % points.length];
277
- let s = Math.sign((b.x - a.x) * (c.y - b.y) - (b.y - a.y) * (c.x - b.x));
278
- if (s === 0) continue;
279
- if (sign && s !== sign) return false;
280
- sign = s;
281
- }
282
- return true;
283
- }
284
-
285
- interface UseEdgeDetectionResult {
286
- corners: Quad | null;
287
- }
288
-
289
- /**
290
- * Suggests document corners for the image on `canvas`.
291
- * Falls back to full-image corners if no document outline is found.
292
- *
293
- * `canvas` must hold the bare image: anything drawn over it, such as the crop overlay's
294
- * selection border, is detected as an edge like any other.
295
- */
296
- // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
297
- export function useEdgeDetection(cv: any, canvas: HTMLCanvasElement | null, enabled: boolean): UseEdgeDetectionResult {
298
- let [corners, setCorners] = useState<Quad | null>(null);
299
-
300
- useEffect(() => {
301
- if (!enabled || !cv || !canvas) return;
302
-
303
- try {
304
- let src = cv.imread(canvas);
305
- let quad: Quad | null;
306
- try {
307
- quad = findDocumentQuad(cv, src);
308
- } finally {
309
- src.delete();
310
- }
311
- setCorners(quad ?? fullImageQuad(canvas.width, canvas.height));
312
- } catch (err) {
313
- console.warn("[ImageEditor] Edge detection failed, falling back to the full image:", err);
314
- setCorners(fullImageQuad(canvas.width, canvas.height));
315
- }
316
- }, [cv, canvas, enabled]);
317
-
318
- return { corners };
319
- }
1
+ import { useEffect, useState } from "react";
2
+
3
+ export interface Point {
4
+ x: number;
5
+ y: number;
6
+ }
7
+
8
+ /** TL, TR, BR, BL */
9
+ export type Quad = [Point, Point, Point, Point];
10
+
11
+ export function fullImageQuad(width: number, height: number): Quad {
12
+ return [
13
+ { x: 0, y: 0 },
14
+ { x: width, y: 0 },
15
+ { x: width, y: height },
16
+ { x: 0, y: height },
17
+ ];
18
+ }
19
+
20
+ /** Longest side of the downscaled working image. Detection is scale-dependent, so it is fixed. */
21
+ const WORK_SIZE = 512;
22
+ /** Closing kernel that erases dark print (text, thin rules) so only surface boundaries remain. */
23
+ const CLOSE_KERNEL = 5;
24
+ /** Candidate lines kept per orientation family. The search is O(n⁴), so keep this small. */
25
+ const MAX_LINES_PER_FAMILY = 14;
26
+ /** Distances (working pixels) sampled on each side of a candidate edge. */
27
+ const SAMPLE_OFFSET_MIN = 3;
28
+ const SAMPLE_OFFSET_MAX = 9;
29
+ /** Minimum Lab colour difference between the two sides for an edge sample to count. */
30
+ const MIN_CONTRAST = 8;
31
+ /** Cost of an unsupported stretch of side, relative to the gain of a supported one. */
32
+ const UNSUPPORTED_COST = 0.5;
33
+ /** Cost per pixel of using the image border as a side (document running off the frame). */
34
+ const BORDER_COST = 0.1;
35
+ /** Minimum normalised score for a suggestion; below it the full image is used. */
36
+ const MIN_SCORE = 0.3;
37
+ const MIN_AREA_FRACTION = 0.1;
38
+
39
+ /** A line in normal form: x·cosθ + y·sinθ = ρ. */
40
+ interface Line {
41
+ theta: number;
42
+ rho: number;
43
+ weight: number;
44
+ border?: boolean;
45
+ }
46
+
47
+ /**
48
+ * Finds the most likely document outline in an RGBA image.
49
+ *
50
+ * Print is first removed with a morphological close, so text and table rules do not
51
+ * compete with the paper's outline. Straight segments from a Hough transform are merged
52
+ * into candidate lines, and every combination of two near-vertical and two near-horizontal
53
+ * lines (the image borders included, for receipts that run off the photo) is scored by how
54
+ * much of each side both lies on an edge and separates two differently coloured surfaces.
55
+ * Scoring by supported length, rather than by the share of each side, lets a long receipt
56
+ * whose edges bend at a fold beat a small, perfectly straight section of it.
57
+ */
58
+ // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
59
+ export function findDocumentQuad(cv: any, src: any): Quad | null {
60
+ let scale = Math.min(1, WORK_SIZE / Math.max(src.cols, src.rows));
61
+ let small = new cv.Mat();
62
+ let rgb = new cv.Mat();
63
+ let colour = new cv.Mat();
64
+ let lab = new cv.Mat();
65
+ let gray = new cv.Mat();
66
+ let edges = new cv.Mat();
67
+ let support = new cv.Mat();
68
+ let segments = new cv.Mat();
69
+
70
+ try {
71
+ cv.resize(src, small, new cv.Size(Math.round(src.cols * scale), Math.round(src.rows * scale)), 0, 0, cv.INTER_AREA);
72
+ let w = small.cols;
73
+ let h = small.rows;
74
+ let minDim = Math.min(w, h);
75
+
76
+ cv.cvtColor(small, rgb, cv.COLOR_RGBA2RGB);
77
+ let closeKernel = cv.getStructuringElement(cv.MORPH_RECT, new cv.Size(CLOSE_KERNEL, CLOSE_KERNEL));
78
+ cv.morphologyEx(rgb, rgb, cv.MORPH_CLOSE, closeKernel);
79
+ closeKernel.delete();
80
+ cv.GaussianBlur(rgb, colour, new cv.Size(5, 5), 0);
81
+ cv.cvtColor(colour, lab, cv.COLOR_RGB2Lab);
82
+ // Blurred after the grey conversion, not taken from `colour`: the rounding differs,
83
+ // and the weaker receipt edges sit close enough to the Hough threshold to notice.
84
+ cv.cvtColor(rgb, gray, cv.COLOR_RGB2GRAY);
85
+ cv.GaussianBlur(gray, gray, new cv.Size(5, 5), 0);
86
+
87
+ // Low thresholds: a receipt on a white table has very little contrast.
88
+ cv.Canny(gray, edges, 20, 60);
89
+ // A tolerant copy for scoring, so a side a pixel or two off its edge still counts.
90
+ let supportKernel = cv.getStructuringElement(cv.MORPH_RECT, new cv.Size(5, 5));
91
+ cv.dilate(edges, support, supportKernel);
92
+ supportKernel.delete();
93
+ cv.HoughLinesP(edges, segments, 1, Math.PI / 180, 30, minDim * 0.12, 8);
94
+
95
+ let { vertical, horizontal } = groupLines(segments, minDim);
96
+ vertical.push({ theta: 0, rho: 0, weight: 0, border: true }, { theta: 0, rho: w - 1, weight: 0, border: true });
97
+ horizontal.push(
98
+ { theta: Math.PI / 2, rho: 0, weight: 0, border: true },
99
+ { theta: Math.PI / 2, rho: h - 1, weight: 0, border: true },
100
+ );
101
+
102
+ let labData: Uint8Array = lab.data;
103
+ let supportData: Uint8Array = support.data;
104
+ let labAt = (x: number, y: number, channel: number) => {
105
+ let xi = Math.min(w - 1, Math.max(0, Math.round(x)));
106
+ let yi = Math.min(h - 1, Math.max(0, Math.round(y)));
107
+ return labData[(yi * w + xi) * 3 + channel];
108
+ };
109
+
110
+ /** Share of a side that lies on an edge with different surfaces on either side of it. */
111
+ let coverage = (a: Point, b: Point) => {
112
+ let len = distance(a, b);
113
+ let n = Math.max(8, Math.round(len / 3));
114
+ let nx = -(b.y - a.y) / len;
115
+ let ny = (b.x - a.x) / len;
116
+ let hits = 0;
117
+ for (let i = 1; i < n; i++) {
118
+ let px = a.x + ((b.x - a.x) * i) / n;
119
+ let py = a.y + ((b.y - a.y) * i) / n;
120
+ let x = Math.round(px);
121
+ let y = Math.round(py);
122
+ if (x < 0 || y < 0 || x >= w || y >= h || !supportData[y * w + x]) continue;
123
+
124
+ let diff = [0, 0, 0];
125
+ let count = 0;
126
+ for (let d = SAMPLE_OFFSET_MIN; d <= SAMPLE_OFFSET_MAX; d += 2) {
127
+ for (let c = 0; c < 3; c++) {
128
+ diff[c] += labAt(px + nx * d, py + ny * d, c) - labAt(px - nx * d, py - ny * d, c);
129
+ }
130
+ count++;
131
+ }
132
+ // Chroma weighs double: a cream receipt on a grey-white table differs mostly in tint.
133
+ if (Math.hypot(diff[0] / count, (2 * diff[1]) / count, (2 * diff[2]) / count) > MIN_CONTRAST) hits++;
134
+ }
135
+ return hits / (n - 1);
136
+ };
137
+
138
+ let margin = minDim * 0.02;
139
+ let inBounds = (p: Point | null): p is Point =>
140
+ p !== null && p.x >= -margin && p.y >= -margin && p.x <= w + margin && p.y <= h + margin;
141
+ let minGap = minDim * 0.1;
142
+
143
+ let best: Quad | null = null;
144
+ let bestScore = -Infinity;
145
+ for (let left of vertical) {
146
+ for (let right of vertical) {
147
+ if (xAt(left, h / 2) >= xAt(right, h / 2) - minGap) continue;
148
+ for (let top of horizontal) {
149
+ for (let bottom of horizontal) {
150
+ if (yAt(top, w / 2) >= yAt(bottom, w / 2) - minGap) continue;
151
+ let sides = [top, right, bottom, left];
152
+ if (sides.filter((l) => l.border).length > 2) continue;
153
+
154
+ let corners = [
155
+ intersect(left, top),
156
+ intersect(right, top),
157
+ intersect(right, bottom),
158
+ intersect(left, bottom),
159
+ ];
160
+ if (!corners.every(inBounds)) continue;
161
+ let quad = corners as Quad;
162
+ if (polygonArea(quad) < w * h * MIN_AREA_FRACTION || !isConvex(quad)) continue;
163
+
164
+ let score = 0;
165
+ for (let s = 0; s < 4; s++) {
166
+ let a = quad[s];
167
+ let b = quad[(s + 1) % 4];
168
+ let len = distance(a, b);
169
+ if (sides[s].border) {
170
+ score -= BORDER_COST * len;
171
+ } else {
172
+ let cov = coverage(a, b);
173
+ score += len * (cov - UNSUPPORTED_COST * (1 - cov));
174
+ }
175
+ }
176
+ score /= w + h;
177
+ if (score > bestScore) {
178
+ bestScore = score;
179
+ best = quad;
180
+ }
181
+ }
182
+ }
183
+ }
184
+ }
185
+
186
+ if (!best || bestScore < MIN_SCORE) return null;
187
+ return best.map((p) => ({
188
+ x: Math.min(src.cols, Math.max(0, p.x / scale)),
189
+ y: Math.min(src.rows, Math.max(0, p.y / scale)),
190
+ })) as Quad;
191
+ } finally {
192
+ for (let mat of [small, rgb, colour, lab, gray, edges, support, segments]) mat.delete();
193
+ }
194
+ }
195
+
196
+ /**
197
+ * Merges Hough segments that lie on the same line and splits the result into near-vertical
198
+ * and near-horizontal families, each ordered by total segment length.
199
+ */
200
+ // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
201
+ function groupLines(segments: any, minDim: number): { vertical: Line[]; horizontal: Line[] } {
202
+ let lines: Line[] = [];
203
+ let maxAngle = (3 * Math.PI) / 180;
204
+ let maxOffset = minDim * 0.015;
205
+
206
+ // Count from the data, not `rows`: OpenCV 4 returns an N×1 matrix of segments and
207
+ // OpenCV 5 a 1×N one, and the loader may end up with either build.
208
+ let data: Int32Array = segments.data32S;
209
+ for (let i = 0; i + 3 < data.length; i += 4) {
210
+ let [x1, y1, x2, y2] = data.subarray(i, i + 4);
211
+ let len = Math.hypot(x2 - x1, y2 - y1);
212
+ // Normal of the segment, folded into [0, π)
213
+ let theta = Math.atan2(x1 - x2, y2 - y1);
214
+ if (theta < 0) theta += Math.PI;
215
+ if (theta >= Math.PI) theta -= Math.PI;
216
+ let rho = x1 * Math.cos(theta) + y1 * Math.sin(theta);
217
+
218
+ let match = lines.find((l) => Math.abs(l.theta - theta) < maxAngle && Math.abs(l.rho - rho) < maxOffset);
219
+ if (match) {
220
+ let total = match.weight + len;
221
+ match.theta = (match.theta * match.weight + theta * len) / total;
222
+ match.rho = (match.rho * match.weight + rho * len) / total;
223
+ match.weight = total;
224
+ } else {
225
+ lines.push({ theta, rho, weight: len });
226
+ }
227
+ }
228
+
229
+ lines.sort((a, b) => b.weight - a.weight);
230
+ let isVertical = (l: Line) => l.theta < Math.PI / 4 || l.theta > (3 * Math.PI) / 4;
231
+ return {
232
+ vertical: lines.filter(isVertical).slice(0, MAX_LINES_PER_FAMILY),
233
+ horizontal: lines.filter((l) => !isVertical(l)).slice(0, MAX_LINES_PER_FAMILY),
234
+ };
235
+ }
236
+
237
+ /** x where a near-vertical line crosses the given y. */
238
+ function xAt(line: Line, y: number): number {
239
+ return (line.rho - y * Math.sin(line.theta)) / Math.cos(line.theta);
240
+ }
241
+
242
+ /** y where a near-horizontal line crosses the given x. */
243
+ function yAt(line: Line, x: number): number {
244
+ return (line.rho - x * Math.cos(line.theta)) / Math.sin(line.theta);
245
+ }
246
+
247
+ function intersect(a: Line, b: Line): Point | null {
248
+ let ca = Math.cos(a.theta);
249
+ let sa = Math.sin(a.theta);
250
+ let cb = Math.cos(b.theta);
251
+ let sb = Math.sin(b.theta);
252
+ let det = ca * sb - sa * cb;
253
+ if (Math.abs(det) < 1e-6) return null;
254
+ return { x: (a.rho * sb - b.rho * sa) / det, y: (ca * b.rho - cb * a.rho) / det };
255
+ }
256
+
257
+ function distance(a: Point, b: Point): number {
258
+ return Math.hypot(b.x - a.x, b.y - a.y);
259
+ }
260
+
261
+ function polygonArea(points: Point[]): number {
262
+ let area = 0;
263
+ for (let i = 0; i < points.length; i++) {
264
+ let p = points[i];
265
+ let q = points[(i + 1) % points.length];
266
+ area += p.x * q.y - q.x * p.y;
267
+ }
268
+ return Math.abs(area) / 2;
269
+ }
270
+
271
+ function isConvex(points: Point[]): boolean {
272
+ let sign = 0;
273
+ for (let i = 0; i < points.length; i++) {
274
+ let a = points[i];
275
+ let b = points[(i + 1) % points.length];
276
+ let c = points[(i + 2) % points.length];
277
+ let s = Math.sign((b.x - a.x) * (c.y - b.y) - (b.y - a.y) * (c.x - b.x));
278
+ if (s === 0) continue;
279
+ if (sign && s !== sign) return false;
280
+ sign = s;
281
+ }
282
+ return true;
283
+ }
284
+
285
+ interface UseEdgeDetectionResult {
286
+ corners: Quad | null;
287
+ }
288
+
289
+ /**
290
+ * Suggests document corners for the image on `canvas`.
291
+ * Falls back to full-image corners if no document outline is found.
292
+ *
293
+ * `canvas` must hold the bare image: anything drawn over it, such as the crop overlay's
294
+ * selection border, is detected as an edge like any other.
295
+ */
296
+ // oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
297
+ export function useEdgeDetection(cv: any, canvas: HTMLCanvasElement | null, enabled: boolean): UseEdgeDetectionResult {
298
+ let [corners, setCorners] = useState<Quad | null>(null);
299
+
300
+ useEffect(() => {
301
+ if (!enabled || !cv || !canvas) return;
302
+
303
+ try {
304
+ let src = cv.imread(canvas);
305
+ let quad: Quad | null;
306
+ try {
307
+ quad = findDocumentQuad(cv, src);
308
+ } finally {
309
+ src.delete();
310
+ }
311
+ setCorners(quad ?? fullImageQuad(canvas.width, canvas.height));
312
+ } catch (err) {
313
+ console.warn("[ImageEditor] Edge detection failed, falling back to the full image:", err);
314
+ setCorners(fullImageQuad(canvas.width, canvas.height));
315
+ }
316
+ }, [cv, canvas, enabled]);
317
+
318
+ return { corners };
319
+ }