@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,188 @@
1
+ import { drawBox, moveBox, resizeBox, sameBox, snapTargets, } from "./crop-geometry.js";
2
+ /*
3
+ * The distances below are pointer travel in CSS pixels on screen. They do not
4
+ * depend on how big the photo is drawn; `trackGesture` converts to source
5
+ * pixels where it needs to.
6
+ */
7
+ /** How close a dragged edge must get to a snap line before it is pulled onto it, in CSS pixels. */
8
+ export const SNAP = 8;
9
+ /** Pointer travel before a press on the selected crop, a handle or empty photo becomes a drag, in CSS pixels. Stops a shaky click from nudging a crop. */
10
+ export const SLOP = 3;
11
+ /** How long a touch must be held still before it starts drawing, in milliseconds. */
12
+ export const HOLD = 350;
13
+ /** Travel that means a touch is a scroll or swipe, not a hold or a tap, in CSS pixels. */
14
+ export const HOLD_SLOP = 8;
15
+ /** Like `SLOP`, but for a mouse press on an unselected crop, which selects it and may carry on into a drag. */
16
+ export const PICK_SLOP = 4;
17
+ /** A second pointer, or a mouse button other than the primary, never starts a gesture. */
18
+ export const acceptPress = (active, press) => active === null && (press.pointerType !== "mouse" || press.button === 0);
19
+ /**
20
+ * Starts a gesture from a press. A mouse draws at once; touch and pen wait for a
21
+ * press-and-hold so the page can still scroll. `scale` is source pixels per
22
+ * screen pixel at the moment of the press and is kept for the whole gesture.
23
+ * Starting the hold timer is the caller's job.
24
+ */
25
+ export const beginGesture = (press, start, scale) => {
26
+ const base = {
27
+ moved: false,
28
+ pointerId: press.pointerId,
29
+ result: null,
30
+ scale,
31
+ slop: SLOP,
32
+ startX: press.clientX,
33
+ startY: press.clientY,
34
+ };
35
+ switch (start.kind) {
36
+ case "draw": {
37
+ return {
38
+ ...base,
39
+ armed: press.pointerType === "mouse",
40
+ ax: start.ax,
41
+ ay: start.ay,
42
+ kind: "draw",
43
+ timer: null,
44
+ };
45
+ }
46
+ case "move": {
47
+ return {
48
+ ...base,
49
+ id: start.id,
50
+ kind: "move",
51
+ slop: start.slop ?? SLOP,
52
+ start: start.start,
53
+ };
54
+ }
55
+ case "resize": {
56
+ return {
57
+ ...base,
58
+ corner: start.corner,
59
+ id: start.id,
60
+ kind: "resize",
61
+ start: start.start,
62
+ };
63
+ }
64
+ case "pick": {
65
+ return { ...base, id: start.id, kind: "pick" };
66
+ }
67
+ default: {
68
+ return start;
69
+ }
70
+ }
71
+ };
72
+ /** The hold time has passed: from now on a move draws, and the page must not take the gesture for a scroll. */
73
+ export const armGesture = (gesture) => ({
74
+ ...gesture,
75
+ armed: true,
76
+ });
77
+ /** Where a touch hold started, in display pixels from the photo's top-left corner (where the cue is drawn). */
78
+ export const holdPoint = (gesture) => ({
79
+ x: gesture.ax / gesture.scale.x,
80
+ y: gesture.ay / gesture.scale.y,
81
+ });
82
+ const IGNORE = { type: "ignore" };
83
+ const ABANDON = { type: "abandon" };
84
+ /**
85
+ * Turn a pointer move into the next square. An edit returns the raw square for
86
+ * the live preview and keeps the snapped one as the gesture's result: the
87
+ * preview follows the pointer exactly, and the commit lands on the guides.
88
+ * Returns a new gesture inside `draft` and `edit`; the caller must store it.
89
+ */
90
+ export const trackGesture = (gesture, pointer, view) => {
91
+ if (gesture.pointerId !== pointer.pointerId) {
92
+ return IGNORE;
93
+ }
94
+ // Travel since the press, in screen (CSS) pixels.
95
+ const travelX = pointer.clientX - gesture.startX;
96
+ const travelY = pointer.clientY - gesture.startY;
97
+ const travel = Math.hypot(travelX, travelY);
98
+ // A touch pick, or a draw still waiting out its hold, never drags: moving
99
+ // means the user is scrolling the page.
100
+ if (gesture.kind === "pick" || (gesture.kind === "draw" && !gesture.armed)) {
101
+ return travel >= HOLD_SLOP ? ABANDON : IGNORE;
102
+ }
103
+ // Under the slop a press is still a click; once `moved` is set it never goes back.
104
+ if (!gesture.moved && travel < gesture.slop) {
105
+ return IGNORE;
106
+ }
107
+ // From here on, work in source pixels.
108
+ const dx = travelX * gesture.scale.x;
109
+ const dy = travelY * gesture.scale.y;
110
+ if (gesture.kind === "draw") {
111
+ const box = drawBox(gesture.ax, gesture.ay, dx, dy, view.size);
112
+ return {
113
+ box,
114
+ gesture: { ...gesture, moved: true, result: box },
115
+ type: "draft",
116
+ };
117
+ }
118
+ const entry = view.entries.find((item) => item.id === gesture.id);
119
+ if (entry === undefined) {
120
+ return IGNORE;
121
+ }
122
+ // Snap lines are the image edges and the suggestion's own and largest squares
123
+ // (a drawn crop has neither, so it only snaps to the image edges). The pull
124
+ // distance is `SNAP` screen pixels converted to source pixels.
125
+ const snap = {
126
+ targets: snapTargets(view.size, [entry.home, entry.reach]),
127
+ threshold: SNAP * gesture.scale.x,
128
+ };
129
+ const edit = gesture.kind === "move"
130
+ ? (withSnap) => moveBox(gesture.start, dx, dy, view.size, withSnap)
131
+ : (withSnap) => resizeBox(gesture.start, gesture.corner, dx, dy, view.size, withSnap);
132
+ const snapped = edit(snap);
133
+ return {
134
+ gesture: { ...gesture, moved: true, result: snapped.box },
135
+ guides: snapped.guides,
136
+ id: gesture.id,
137
+ preview: edit().box,
138
+ type: "edit",
139
+ };
140
+ };
141
+ /**
142
+ * Ends a gesture. A lift (`canceled` false) keeps the result if the pointer
143
+ * really moved: a drawn square, or an adjusted square that differs from where it
144
+ * started. A cancel (pointer canceled, capture lost, scroll detected,
145
+ * interaction disabled) keeps nothing: it only clears what the gesture had put
146
+ * on screen, and hands back any hold timer so the caller can clear it.
147
+ */
148
+ export const finishGesture = (gesture, canceled) => {
149
+ const none = {
150
+ clear: false,
151
+ clearLive: null,
152
+ commit: null,
153
+ draw: null,
154
+ select: null,
155
+ timer: null,
156
+ };
157
+ switch (gesture.kind) {
158
+ case "pick": {
159
+ return { ...none, select: canceled ? null : gesture.id };
160
+ }
161
+ case "draw": {
162
+ // A press on the empty photo that never moved is a tap: it draws nothing and clears the selection.
163
+ const keep = !canceled && gesture.moved ? gesture.result : null;
164
+ return {
165
+ ...none,
166
+ clear: !canceled && !gesture.moved,
167
+ draw: keep,
168
+ timer: gesture.timer,
169
+ };
170
+ }
171
+ case "move":
172
+ case "resize": {
173
+ const { result } = gesture;
174
+ const keep = !canceled &&
175
+ gesture.moved &&
176
+ result !== null &&
177
+ !sameBox(result, gesture.start);
178
+ return {
179
+ ...none,
180
+ clearLive: gesture.id,
181
+ commit: keep && result !== null ? { box: result, id: gesture.id } : null,
182
+ };
183
+ }
184
+ default: {
185
+ return gesture;
186
+ }
187
+ }
188
+ };
@@ -0,0 +1,10 @@
1
+ export { editorReducer, initialEditorState } from "./crop-editor-state.js";
2
+ export type { Announcement, EditorAction, EditorState, } from "./crop-editor-state.js";
3
+ export { buildEntries, withLive } from "./crop-entries.js";
4
+ export type { CropEntry, LiveEdit, UserCrop } from "./crop-entries.js";
5
+ export { exportCrop, exportSize } from "./crop-export.js";
6
+ export { MIN_SIDE, NO_GUIDES, canExpand, containSize, cropIndexAt, cropPixels, displayScale, drawBox, fitBox, growBox, keyboardBox, moveBox, resizeBox, sameBox, scaleBox, snapTargets, squareBox, toPercent, } from "./crop-geometry.js";
7
+ export type { Corner, Edit, Guides, ImageSize, PercentCrop, PixelCrop, Scale, ScreenBox, Snap, Targets, } from "./crop-geometry.js";
8
+ export { HOLD, HOLD_SLOP, PICK_SLOP, SLOP, SNAP, acceptPress, armGesture, beginGesture, finishGesture, holdPoint, trackGesture, } from "./crop-gesture.js";
9
+ export type { DrawGesture, Gesture, GestureStart, Outcome, Point, Pointer, Press, TrackView, Tracked, } from "./crop-gesture.js";
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/crops/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAC3E,YAAY,EACV,YAAY,EACZ,YAAY,EACZ,WAAW,GACZ,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC3D,YAAY,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AACvE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EACL,QAAQ,EACR,SAAS,EACT,SAAS,EACT,WAAW,EACX,WAAW,EACX,UAAU,EACV,YAAY,EACZ,OAAO,EACP,MAAM,EACN,OAAO,EACP,WAAW,EACX,OAAO,EACP,SAAS,EACT,OAAO,EACP,QAAQ,EACR,WAAW,EACX,SAAS,EACT,SAAS,GACV,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EACV,MAAM,EACN,IAAI,EACJ,MAAM,EACN,SAAS,EACT,WAAW,EACX,SAAS,EACT,KAAK,EACL,SAAS,EACT,IAAI,EACJ,OAAO,GACR,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,IAAI,EACJ,SAAS,EACT,SAAS,EACT,IAAI,EACJ,IAAI,EACJ,WAAW,EACX,UAAU,EACV,YAAY,EACZ,aAAa,EACb,SAAS,EACT,YAAY,GACb,MAAM,mBAAmB,CAAC;AAC3B,YAAY,EACV,WAAW,EACX,OAAO,EACP,YAAY,EACZ,OAAO,EACP,KAAK,EACL,OAAO,EACP,KAAK,EACL,SAAS,EACT,OAAO,GACR,MAAM,mBAAmB,CAAC"}
@@ -0,0 +1,5 @@
1
+ export { editorReducer, initialEditorState } from "./crop-editor-state.js";
2
+ export { buildEntries, withLive } from "./crop-entries.js";
3
+ export { exportCrop, exportSize } from "./crop-export.js";
4
+ export { MIN_SIDE, NO_GUIDES, canExpand, containSize, cropIndexAt, cropPixels, displayScale, drawBox, fitBox, growBox, keyboardBox, moveBox, resizeBox, sameBox, scaleBox, snapTargets, squareBox, toPercent, } from "./crop-geometry.js";
5
+ export { HOLD, HOLD_SLOP, PICK_SLOP, SLOP, SNAP, acceptPress, armGesture, beginGesture, finishGesture, holdPoint, trackGesture, } from "./crop-gesture.js";
@@ -0,0 +1,19 @@
1
+ export { analyzeSurfaceRegions } from "./analysis/analyze-surface-regions.js";
2
+ export { analysisSize, decodePhoto } from "./analysis/decode.js";
3
+ export type { DecodedPhoto } from "./analysis/decode.js";
4
+ export type { Diagnostics, StageEvent } from "./analysis/diagnostics.js";
5
+ export type { AnalysisImage, PercentRectangle, PixelRectangle, Rectangle, SurfaceSuggestion, } from "./analysis/types.js";
6
+ export { editorReducer, initialEditorState, } from "./crops/crop-editor-state.js";
7
+ export type { Announcement, EditorAction, EditorState, } from "./crops/crop-editor-state.js";
8
+ export { buildEntries, withLive } from "./crops/crop-entries.js";
9
+ export type { CropEntry, LiveEdit, UserCrop } from "./crops/crop-entries.js";
10
+ export { exportCrop, exportSize } from "./crops/crop-export.js";
11
+ export { MIN_SIDE, NO_GUIDES, canExpand, containSize, cropIndexAt, cropPixels, displayScale, drawBox, fitBox, growBox, keyboardBox, moveBox, resizeBox, sameBox, scaleBox, snapTargets, squareBox, toPercent, } from "./crops/crop-geometry.js";
12
+ export type { Corner, Edit, Guides, ImageSize, PercentCrop, PixelCrop, Scale, ScreenBox, Snap, Targets, } from "./crops/crop-geometry.js";
13
+ export { HOLD, HOLD_SLOP, PICK_SLOP, SLOP, SNAP, acceptPress, armGesture, beginGesture, finishGesture, holdPoint, trackGesture, } from "./crops/crop-gesture.js";
14
+ export type { DrawGesture, Gesture, GestureStart, Outcome, Point, Pointer, Press, TrackView, Tracked, } from "./crops/crop-gesture.js";
15
+ export type { AnalysisDiagnostics, AnalysisPhase, AnalysisResult, PhotoSource, } from "./surface-types.js";
16
+ export type { AnalysisReply, AnalysisRequest } from "./worker/protocol.js";
17
+ export { startAnalysis } from "./worker/run-analysis.js";
18
+ export type { AnalysisListeners } from "./worker/run-analysis.js";
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,uCAAuC,CAAC;AAC9E,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACjE,YAAY,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACzD,YAAY,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACzE,YAAY,EACV,aAAa,EACb,gBAAgB,EAChB,cAAc,EACd,SAAS,EACT,iBAAiB,GAClB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,aAAa,EACb,kBAAkB,GACnB,MAAM,8BAA8B,CAAC;AACtC,YAAY,EACV,YAAY,EACZ,YAAY,EACZ,WAAW,GACZ,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACjE,YAAY,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAC7E,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAChE,OAAO,EACL,QAAQ,EACR,SAAS,EACT,SAAS,EACT,WAAW,EACX,WAAW,EACX,UAAU,EACV,YAAY,EACZ,OAAO,EACP,MAAM,EACN,OAAO,EACP,WAAW,EACX,OAAO,EACP,SAAS,EACT,OAAO,EACP,QAAQ,EACR,WAAW,EACX,SAAS,EACT,SAAS,GACV,MAAM,0BAA0B,CAAC;AAClC,YAAY,EACV,MAAM,EACN,IAAI,EACJ,MAAM,EACN,SAAS,EACT,WAAW,EACX,SAAS,EACT,KAAK,EACL,SAAS,EACT,IAAI,EACJ,OAAO,GACR,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACL,IAAI,EACJ,SAAS,EACT,SAAS,EACT,IAAI,EACJ,IAAI,EACJ,WAAW,EACX,UAAU,EACV,YAAY,EACZ,aAAa,EACb,SAAS,EACT,YAAY,GACb,MAAM,yBAAyB,CAAC;AACjC,YAAY,EACV,WAAW,EACX,OAAO,EACP,YAAY,EACZ,OAAO,EACP,KAAK,EACL,OAAO,EACP,KAAK,EACL,SAAS,EACT,OAAO,GACR,MAAM,yBAAyB,CAAC;AACjC,YAAY,EACV,mBAAmB,EACnB,aAAa,EACb,cAAc,EACd,WAAW,GACZ,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAC3E,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,YAAY,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export { analyzeSurfaceRegions } from "./analysis/analyze-surface-regions.js";
2
+ export { analysisSize, decodePhoto } from "./analysis/decode.js";
3
+ export { editorReducer, initialEditorState, } from "./crops/crop-editor-state.js";
4
+ export { buildEntries, withLive } from "./crops/crop-entries.js";
5
+ export { exportCrop, exportSize } from "./crops/crop-export.js";
6
+ export { MIN_SIDE, NO_GUIDES, canExpand, containSize, cropIndexAt, cropPixels, displayScale, drawBox, fitBox, growBox, keyboardBox, moveBox, resizeBox, sameBox, scaleBox, snapTargets, squareBox, toPercent, } from "./crops/crop-geometry.js";
7
+ export { HOLD, HOLD_SLOP, PICK_SLOP, SLOP, SNAP, acceptPress, armGesture, beginGesture, finishGesture, holdPoint, trackGesture, } from "./crops/crop-gesture.js";
8
+ export { startAnalysis } from "./worker/run-analysis.js";
@@ -0,0 +1,41 @@
1
+ import type { SurfaceSuggestion } from "./analysis/analyze-surface-regions.js";
2
+ import type { StageEvent } from "./analysis/diagnostics.js";
3
+ import type { AnalysisImage } from "./analysis/types.js";
4
+ /**
5
+ * The source photo: the original, decoded once. It stays on the main thread
6
+ * until replaced or unmounted; downloads are cut from `bitmap`.
7
+ */
8
+ export interface PhotoSource {
9
+ /** Unique per opened photo, so animation keys never collide between photos. */
10
+ id: number;
11
+ /** The source pixels, with EXIF orientation applied. */
12
+ bitmap: ImageBitmap;
13
+ /** What to call the photo: the upload's file name, the example's name, or "Pasted photo". */
14
+ name: string;
15
+ /** The example photo's file name it was opened from, so a gallery can mark it; null for an upload or paste. */
16
+ file: string | null;
17
+ }
18
+ /** What the detector showed of its own working, kept only when a caller asks (the explainer does; the app never does). */
19
+ export interface AnalysisDiagnostics {
20
+ /** The small copy the detector read, kept because the worker takes the original. */
21
+ image: AnalysisImage;
22
+ /** Every decision, in pipeline order. */
23
+ events: StageEvent[];
24
+ /** Milliseconds the page took to decode the photo and shrink it. */
25
+ openMs: number;
26
+ }
27
+ /** What the worker returned for one photo. */
28
+ export interface AnalysisResult {
29
+ source: PhotoSource;
30
+ suggestions: SurfaceSuggestion[];
31
+ /** How long the detector took, in milliseconds. */
32
+ elapsedMs: number;
33
+ /** Present only when the analysis was started with `diagnostics`. */
34
+ diagnostics?: AnalysisDiagnostics;
35
+ }
36
+ /**
37
+ * Where the current choice of photo has got to. A failed choice is "error" and
38
+ * keeps the previous photo on screen, without crops.
39
+ */
40
+ export type AnalysisPhase = "idle" | "opening" | "analyzing" | "ready" | "error";
41
+ //# sourceMappingURL=surface-types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"surface-types.d.ts","sourceRoot":"","sources":["../src/surface-types.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,uCAAuC,CAAC;AAC/E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,+EAA+E;IAC/E,EAAE,EAAE,MAAM,CAAC;IACX,wDAAwD;IACxD,MAAM,EAAE,WAAW,CAAC;IACpB,6FAA6F;IAC7F,IAAI,EAAE,MAAM,CAAC;IACb,+GAA+G;IAC/G,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CACrB;AAED,0HAA0H;AAC1H,MAAM,WAAW,mBAAmB;IAClC,oFAAoF;IACpF,KAAK,EAAE,aAAa,CAAC;IACrB,yCAAyC;IACzC,MAAM,EAAE,UAAU,EAAE,CAAC;IACrB,oEAAoE;IACpE,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,8CAA8C;AAC9C,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,WAAW,CAAC;IACpB,WAAW,EAAE,iBAAiB,EAAE,CAAC;IACjC,mDAAmD;IACnD,SAAS,EAAE,MAAM,CAAC;IAClB,qEAAqE;IACrE,WAAW,CAAC,EAAE,mBAAmB,CAAC;CACnC;AAED;;;GAGG;AACH,MAAM,MAAM,aAAa,GACrB,MAAM,GACN,SAAS,GACT,WAAW,GACX,OAAO,GACP,OAAO,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ export type { AnalysisReply, AnalysisRequest } from "./protocol.js";
2
+ export { startAnalysis } from "./run-analysis.js";
3
+ export type { AnalysisListeners } from "./run-analysis.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/worker/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACpE,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,YAAY,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC"}
@@ -0,0 +1 @@
1
+ export { startAnalysis } from "./run-analysis.js";
@@ -0,0 +1,28 @@
1
+ import type { SurfaceSuggestion } from "../analysis/analyze-surface-regions.js";
2
+ import type { StageEvent } from "../analysis/diagnostics.js";
3
+ /**
4
+ * What the page sends the worker. `pixels` is the RGBA analysis image from `decodePhoto` (longest side at
5
+ * most `thresholds.input.analysisSide`), moved to the worker rather than copied. `id` is the session id of the photo it
6
+ * belongs to, echoed back in the reply.
7
+ */
8
+ export interface AnalysisRequest {
9
+ id: number;
10
+ width: number;
11
+ height: number;
12
+ pixels: ArrayBuffer;
13
+ /** Ask for the detector's stage-by-stage decisions in the reply. Absent in the app. */
14
+ diagnostics?: boolean;
15
+ }
16
+ /** The request id prevents a reply for an older photo being applied to the current one. */
17
+ export interface AnalysisReply {
18
+ id: number;
19
+ /** Best first, in percent coordinates; empty when `error` is set. */
20
+ suggestions: SurfaceSuggestion[];
21
+ /** Time the detector took inside the worker, in milliseconds (excludes decoding and messaging). */
22
+ elapsedMs: number;
23
+ /** The detector's own error message; absent on success. */
24
+ error?: string;
25
+ /** The detector's decisions in pipeline order; only present when the request asked for them. */
26
+ events?: StageEvent[];
27
+ }
28
+ //# sourceMappingURL=protocol.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../../src/worker/protocol.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wCAAwC,CAAC;AAChF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AAE7D;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,WAAW,CAAC;IACpB,uFAAuF;IACvF,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED,2FAA2F;AAC3F,MAAM,WAAW,aAAa;IAC5B,EAAE,EAAE,MAAM,CAAC;IACX,qEAAqE;IACrE,WAAW,EAAE,iBAAiB,EAAE,CAAC;IACjC,mGAAmG;IACnG,SAAS,EAAE,MAAM,CAAC;IAClB,2DAA2D;IAC3D,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,gGAAgG;IAChG,MAAM,CAAC,EAAE,UAAU,EAAE,CAAC;CACvB"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,21 @@
1
+ import type { AnalysisImage } from "../analysis/types.js";
2
+ import type { AnalysisReply } from "./protocol.js";
3
+ /** What the page hears back from one run: an answer, or a reason it will not get one. */
4
+ export interface AnalysisListeners {
5
+ /** The worker answered, successfully or with its own error. */
6
+ onReply: (reply: AnalysisReply) => void;
7
+ /** The worker crashed, sent something unreadable, or took too long. */
8
+ onFailure: (message: string) => void;
9
+ }
10
+ /**
11
+ * Run the detector on the decoded analysis pixels in a worker. Returns a
12
+ * function that stops it; call that before starting another run or leaving.
13
+ *
14
+ * The pixel buffer is transferred, so `image.data` is emptied on the caller's side afterwards; the
15
+ * full-size bitmap is untouched. The timeout only reports `onFailure`: it does not stop the worker, the
16
+ * returned function does (it also cancels the timeout), so callers should stop it after a reply or failure too.
17
+ * `id` is the session id, echoed in the reply so stale answers can be ignored.
18
+ * `diagnostics` asks the worker to send back the detector's decisions too.
19
+ */
20
+ export declare const startAnalysis: (id: number, { data, height, width }: AnalysisImage, listeners: AnalysisListeners, diagnostics?: boolean) => (() => void);
21
+ //# sourceMappingURL=run-analysis.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run-analysis.d.ts","sourceRoot":"","sources":["../../src/worker/run-analysis.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAKnD,yFAAyF;AACzF,MAAM,WAAW,iBAAiB;IAChC,+DAA+D;IAC/D,OAAO,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAC;IACxC,uEAAuE;IACvE,SAAS,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CACtC;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,OACpB,MAAM,2BACe,aAAa,aAC3B,iBAAiB,4BAE3B,CAAC,MAAM,IAAI,CA2Bb,CAAC"}
@@ -0,0 +1,36 @@
1
+ /** How long the worker may run before the page gives up on it, in milliseconds. */
2
+ const TIMEOUT_MS = 15_000;
3
+ /**
4
+ * Run the detector on the decoded analysis pixels in a worker. Returns a
5
+ * function that stops it; call that before starting another run or leaving.
6
+ *
7
+ * The pixel buffer is transferred, so `image.data` is emptied on the caller's side afterwards; the
8
+ * full-size bitmap is untouched. The timeout only reports `onFailure`: it does not stop the worker, the
9
+ * returned function does (it also cancels the timeout), so callers should stop it after a reply or failure too.
10
+ * `id` is the session id, echoed in the reply so stale answers can be ignored.
11
+ * `diagnostics` asks the worker to send back the detector's decisions too.
12
+ */
13
+ export const startAnalysis = (id, { data, height, width }, listeners, diagnostics = false) => {
14
+ const pixels = data.buffer;
15
+ const worker = new Worker(new URL("surface-worker.js", import.meta.url), {
16
+ type: "module",
17
+ });
18
+ const timer = window.setTimeout(() => {
19
+ listeners.onFailure("The image analysis took too long. Try another photo.");
20
+ }, TIMEOUT_MS);
21
+ worker.addEventListener("message", (event) => {
22
+ listeners.onReply(event.data);
23
+ });
24
+ worker.addEventListener("error", (event) => {
25
+ listeners.onFailure(event.message || "The image analysis stopped. Try another photo.");
26
+ });
27
+ worker.addEventListener("messageerror", () => {
28
+ listeners.onFailure("The image analysis result couldn’t be read. Try another photo.");
29
+ });
30
+ // Transfer detaches this small buffer on the main thread; the full-size bitmap is untouched.
31
+ worker.postMessage({ diagnostics, height, id, pixels, width }, [pixels]);
32
+ return () => {
33
+ window.clearTimeout(timer);
34
+ worker.terminate();
35
+ };
36
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=surface-worker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"surface-worker.d.ts","sourceRoot":"","sources":["../../src/worker/surface-worker.ts"],"names":[],"mappings":""}
@@ -0,0 +1,36 @@
1
+ // The worker's entry point: receives one analysis image, runs the detector off the main thread, replies with crops.
2
+ import { analyzeSurfaceRegions } from "../analysis/analyze-surface-regions.js";
3
+ /** Sends a reply to the page. The empty transfer list means suggestions are copied across, not moved. */
4
+ const reply = (message) => {
5
+ self.postMessage(message, []);
6
+ };
7
+ // One message per worker: each photo gets a fresh worker (see run-analysis.ts), so there is no state to reset.
8
+ // The detector's errors (bad dimensions or buffer) come back as `error` rather than crashing the worker.
9
+ self.addEventListener("message", (event) => {
10
+ const { diagnostics: wanted, id, width, height, pixels } = event.data;
11
+ // Collected only on request; the detector answers identically either way.
12
+ const diagnostics = wanted === true ? { events: [] } : undefined;
13
+ const started = performance.now();
14
+ try {
15
+ const suggestions = analyzeSurfaceRegions({
16
+ // A view over the transferred buffer, not a copy.
17
+ data: new Uint8ClampedArray(pixels),
18
+ height,
19
+ width,
20
+ }, diagnostics);
21
+ reply({
22
+ elapsedMs: performance.now() - started,
23
+ events: diagnostics?.events,
24
+ id,
25
+ suggestions,
26
+ });
27
+ }
28
+ catch (error) {
29
+ reply({
30
+ elapsedMs: performance.now() - started,
31
+ error: error instanceof Error ? error.message : "Surface analysis failed.",
32
+ id,
33
+ suggestions: [],
34
+ });
35
+ }
36
+ });
package/package.json CHANGED
@@ -1,6 +1,126 @@
1
1
  {
2
2
  "name": "@instruments/quadrat",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "private": false,
5
+ "description": "In-browser material surface crop detection, outlines and square-crop editing geometry.",
6
+ "license": "Proprietary",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/materialinstruments/quadrat.git",
10
+ "directory": "packages/core"
11
+ },
12
+ "files": [
13
+ "dist",
14
+ "README.md"
15
+ ],
16
+ "type": "module",
17
+ "sideEffects": false,
18
+ "main": "./dist/index.js",
19
+ "types": "./dist/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.js"
24
+ },
25
+ "./analysis": {
26
+ "types": "./dist/analysis/index.d.ts",
27
+ "import": "./dist/analysis/index.js"
28
+ },
29
+ "./analysis/analyze-surface-regions": {
30
+ "types": "./dist/analysis/analyze-surface-regions.d.ts",
31
+ "import": "./dist/analysis/analyze-surface-regions.js"
32
+ },
33
+ "./analysis/decode": {
34
+ "types": "./dist/analysis/decode.d.ts",
35
+ "import": "./dist/analysis/decode.js"
36
+ },
37
+ "./analysis/descriptors": {
38
+ "types": "./dist/analysis/descriptors.d.ts",
39
+ "import": "./dist/analysis/descriptors.js"
40
+ },
41
+ "./analysis/diagnostics": {
42
+ "types": "./dist/analysis/diagnostics.d.ts",
43
+ "import": "./dist/analysis/diagnostics.js"
44
+ },
45
+ "./analysis/outline": {
46
+ "types": "./dist/analysis/outline.d.ts",
47
+ "import": "./dist/analysis/outline.js"
48
+ },
49
+ "./analysis/repetition": {
50
+ "types": "./dist/analysis/repetition.d.ts",
51
+ "import": "./dist/analysis/repetition.js"
52
+ },
53
+ "./analysis/square-geometry": {
54
+ "types": "./dist/analysis/square-geometry.d.ts",
55
+ "import": "./dist/analysis/square-geometry.js"
56
+ },
57
+ "./analysis/surface-candidates": {
58
+ "types": "./dist/analysis/surface-candidates.d.ts",
59
+ "import": "./dist/analysis/surface-candidates.js"
60
+ },
61
+ "./analysis/thresholds": {
62
+ "types": "./dist/analysis/thresholds.d.ts",
63
+ "import": "./dist/analysis/thresholds.js"
64
+ },
65
+ "./analysis/types": {
66
+ "types": "./dist/analysis/types.d.ts",
67
+ "import": "./dist/analysis/types.js"
68
+ },
69
+ "./crops": {
70
+ "types": "./dist/crops/index.d.ts",
71
+ "import": "./dist/crops/index.js"
72
+ },
73
+ "./crops/crop-editor-state": {
74
+ "types": "./dist/crops/crop-editor-state.d.ts",
75
+ "import": "./dist/crops/crop-editor-state.js"
76
+ },
77
+ "./crops/crop-entries": {
78
+ "types": "./dist/crops/crop-entries.d.ts",
79
+ "import": "./dist/crops/crop-entries.js"
80
+ },
81
+ "./crops/crop-export": {
82
+ "types": "./dist/crops/crop-export.d.ts",
83
+ "import": "./dist/crops/crop-export.js"
84
+ },
85
+ "./crops/crop-geometry": {
86
+ "types": "./dist/crops/crop-geometry.d.ts",
87
+ "import": "./dist/crops/crop-geometry.js"
88
+ },
89
+ "./crops/crop-gesture": {
90
+ "types": "./dist/crops/crop-gesture.d.ts",
91
+ "import": "./dist/crops/crop-gesture.js"
92
+ },
93
+ "./surface-types": {
94
+ "types": "./dist/surface-types.d.ts",
95
+ "import": "./dist/surface-types.js"
96
+ },
97
+ "./worker": {
98
+ "types": "./dist/worker/index.d.ts",
99
+ "import": "./dist/worker/index.js"
100
+ },
101
+ "./worker/protocol": {
102
+ "types": "./dist/worker/protocol.d.ts",
103
+ "import": "./dist/worker/protocol.js"
104
+ },
105
+ "./worker/run-analysis": {
106
+ "types": "./dist/worker/run-analysis.d.ts",
107
+ "import": "./dist/worker/run-analysis.js"
108
+ },
109
+ "./worker/surface-worker": {
110
+ "types": "./dist/worker/surface-worker.d.ts",
111
+ "import": "./dist/worker/surface-worker.js"
112
+ }
113
+ },
114
+ "publishConfig": {
115
+ "access": "public"
116
+ },
117
+ "devDependencies": {
118
+ "typescript": "7.0.2",
119
+ "vitest": "^4.1.11"
120
+ },
121
+ "scripts": {
122
+ "build": "rm -rf dist && tsc -p tsconfig.build.json && tsc -p tsconfig.worker.build.json",
123
+ "test": "vitest run",
124
+ "typecheck": "tsc --noEmit && tsc -p tsconfig.worker.json --noEmit"
125
+ }
6
126
  }