49 Commits

Author SHA1 Message Date
grokbot_ty be2ea6c4ea feat(capture): tape-line overlay UI replaces axis rectangle (#99)
Pointer-Events pan/zoom/tilt under a fixed black ~2pt line with
rifle-scope end ticks (≥44×44 hit). AxisPanel becomes Tape line step;
session persists tape transform; scale = tick distance / L.
sveltedrag unavailable for Svelte 4 — Pointer Events path used.
2026-09-25 17:41:29 -07:00
grokbot_ty 323f29b649 feat(capture): shared crop_fill + tape geometry for #99
- One crop_fill (default 0.9) replaces duplicated FILL in inventory/overlay
- Thin localStorage settings store; crop % only moves long-axis end insets
- Pure tapeGeometry: fixed stage line, stage↔image map, tick span → px
- measure() prefers tape tick span for scale; maskSpanAlong demoted
- rotateCalibration bakes cal tilt into inventory + overlay only
- Fix stale 80%-fill comment on slabBounds
2026-09-25 17:41:29 -07:00
grokbot_ty b44e231038 docs(design): add approved #99 tape-line overlay wireframe
Wireframe settles fixed middle line + movable slab, tick-declared L,
and shared crop_fill. Scaffolding scrubbed before shipping into the app.
2026-09-25 17:41:29 -07:00
grokbot_ty abef27cf2a docs(deploy): fallback UAT on Grok Bot computer :18181
When Tailscale cannot reach ubudual5060, Capture UAT serves from the
bot computer on :18181. Primary overnight remains :10881. Never touch
Ty local :18080 or legacy :10880.
2026-09-25 17:30:14 -07:00
grokbot_ty 03d3fcf460 fix(photos): preserve tapered tips and dark live edge (#73 #74)
Lower the axial body threshold while retaining fringe rejection, minimize length-only tip inset, and make sheet sensitivity curves testable and conservative at zero.
2026-09-16 15:18:06 +00:00
grokbot_ty 037a4b870a fix(photos): shared EXIF-oriented decode for axis/overlay space (#72)
PhotoStage used new Image() while mask worker and calculate used bare
createImageBitmap; Orientation-8 matched-set JPEGs can disagree across
those paths so the approved pink axis is not the overlay (ux,uy).

One decodeOrientedBitmap helper always requests imageOrientation
from-image (with bare fallback), wired into PhotoStage, mask.worker,
and session calculate/siblings.
2026-09-16 05:08:13 +00:00
grokbot_ty b8e69b5239 test(photos): refresh e2e evidence after tip/label fix (#71) 2026-09-16 04:55:19 +00:00
grokbot_ty 495f179c2f fix(photos): width-aware tips, stroke inset, smaller in-bounds labels (#71)
Root cause: 0.2%/99.8% axial percentiles still included dense fringe past
the visible body; round line caps and scale-tied font overshot edges and
clipped width labels. Use across-width tip stations, inset length/width
ends for stroke/arrowhead, butt caps, inventory-capped font, axis-right
length label, clamp labels to PNG.
2026-09-16 04:54:01 +00:00
grokbot_ty b386ab9670 test(photos): overlay tips on footprint + labels in-bounds (#71)
Failing UAT proofs: length span must exclude fringe whiskers past solid
body; width endpoints inset for stroke/arrowhead; smaller font; labels
clamped inside inventory PNG.
2026-09-16 04:46:48 +00:00
grokbot_ty 2be2e7d615 test(photos): refresh e2e evidence after axis parallel fix (#70) 2026-09-16 04:36:59 +00:00
grokbot_ty 6d182db81b fix(photos): length dim parallel to approved axis (#70) 2026-09-16 04:35:53 +00:00
grokbot_ty dc19cffcc0 test(photos): length/width dims follow approved axis (#70) 2026-09-16 04:35:38 +00:00
grokbot_ty 05447c92fc fix(photos): length dim snaps to wood footprint (#69) 2026-09-16 02:29:05 +00:00
grokbot_ty 415158d4d3 test(photos): length dim snaps to wood tips (#69) 2026-09-16 02:17:45 +00:00
grokbot_ty 9bc35f7edd fix(photos): overlay UAT nits + 90% crop fill (#67,#68)
Length dim already mask tip-to-tip (locked by test). Width labels move
axis-left off the wood via labelAt; font steps down to max(22, scale*1.0).
Inventory/overlay crop FILL 0.8→0.9; CAPTURE/PHOTOS docs match.
2026-09-15 21:46:58 +00:00
grokbot_ty 15895cae0c test(photos): overlay UAT nits + 90% fill expect (#67,#68)
Lock length dim to mask tip-to-tip (not oversized bbox), require width
labelAt on axis-left off the wood, smaller font contract, and 90% crop
fill extent. Red before green.
2026-09-15 21:46:27 +00:00
grokbot_ty 5650255e49 deploy: overnight M2 UAT on ubudual :10881 (#66) 2026-09-15 21:41:50 +00:00
grokbot_ty 2be400e455 test(e2e): Playwright photos-ready flow + visual evidence (#65)
Drive batch → pick calibration → Mask (Black) → Axis → Measure Calculate →
Photos ready against set1-48x2 fixtures on local vite preview. Capture
screenshots under tests/e2e/artifacts/; commit README + small evidence JPEGs.
2026-09-15 21:38:45 +00:00
grokbot_ty 2c9a50ddf9 feat(photos): Photos-ready lineup after Calculate (#64)
Add buildPhotosReadyLineup (clean cal → overlay → batch inventory PNGs
in capture order, plus empty-mask/floor error slots). MeasurePanel
switches to a Photos ready end screen with Milestone 1 numbers and the
inventory PNG lineup; Back still returns to Length axis.
2026-09-15 21:33:37 +00:00
grokbot_ty 4c3c8b49eb test(photos): Photos-ready lineup order + error slots (#64)
Failing TDD for M2 Photos-ready end screen: pure buildPhotosReadyLineup
orders clean calibration inventory PNG → dimension-overlay copy → other
batch inventory PNGs in capture order (skipping cal null), surfaces
batchApply empty-mask/floor errors, and MeasurePanel must show Photos
ready with Milestone 1 numbers (not source thumbs).
2026-09-15 21:33:00 +00:00
grokbot_ty a22525023a feat(photos): burn axis-oriented dimension overlay PNG (#63)
Add pure station/axis math and canvas burn-in for a second calibration
inventory PNG (results.overlayPng). Width stations start 6 in from the
EXIF-oriented photo bottom along the approved length axis (6, 18, … ≤
L−6); length graphic sits on axis-right outside the slab. Clean
inventoryPng and batchApply[cal]=null stay unchanged.
2026-09-15 21:31:22 +00:00
grokbot_ty c674931f5c test(photos): dimension-overlay PNG stations + results.overlayPng (#63)
Failing TDD for M2 dimension-overlay copy of the calibration inventory PNG:
width stations every 12 in from EXIF bottom (6, 18, … ≤ L−6), axis-right
length graphic, and Calculate storing overlayPng without touching batchApply[cal].
2026-09-15 21:29:59 +00:00
grokbot_ty 3936c575b2 feat(photos): apply frozen mask to batch siblings after Calculate (#62)
After calibration Calculate, reuse approved sheet/sensitivity on non-calibration
photos via the mask worker + inventory compose. Persist per-photo ok/error on
measure.batchApply and the device draft for #64; leave Mask UI unopened and keep
scale/widths/sqft/bdft on the calibration results only.
2026-09-15 21:24:10 +00:00
grokbot_ty 9404d0130c test(photos): batch apply approved mask after Calculate (#62)
Prove frozen calibration mask reuse, per-photo errors, inventory PNG results, calibration-only measures, draft persistence, and no Mask re-prompting for batch siblings.
2026-09-15 21:20:35 +00:00
grokbot_ty 8ff008f85c feat(photos): keep M1 mask/axis/measure on calibration pick (#61)
Document single-source M1 path after pickCalibration: Mask/Axis/Measure/
Calculate use the chosen Source only; batch siblings stay draft-untouched
until #62 apply. Tag encode-fallback inventory Blob as image/png (CAPTURE.md).
2026-09-15 21:14:16 +00:00
grokbot_ty d2ffa4cad2 test(photos): M1 mask/axis/measure on calibration pick (#61)
Proving tests: after pick of index N≠0, Mask→Axis→Measure operate on that
blob only; Calculate yields inventory PNG + M1 numbers; other batch photos
stay in draft.batch untouched; resume through measure restores calibration
source. FakeWorker drives the path in Node. No production change yet (TDD).
2026-09-15 21:14:00 +00:00
grokbot_ty d18b96c10b feat(photos): pick calibration photo before Mask (#60)
Add pickCalibration state/UI over batch thumbs. Continue from batch opens
pick (including single-photo confirm). User pick enters M1 Mask on that
Source only; draft stores calibrationIndex. Removes first-photo proxy.
2026-09-15 21:11:07 +00:00
grokbot_ty 4fa8126471 test(photos): pick calibration before Mask (#60)
Failing session/UI tests for M2-3: Continue → pickCalibration (not
first-photo Mask proxy), user pick enters Mask on that Source, draft
remembers calibrationIndex, Resume restores. No production change yet (TDD).
2026-09-15 21:10:14 +00:00
grokbot_ty ff4bb0f31e feat(photos): multi-photo batch + device draft (#59)
Replace single Add photo with Batch: accumulate 1–N photos, Continue
when ≥1 (M1 bridge: mask on first), persist batch blobs in the device
draft, Resume Continue restores. Keeps Mask/Axis/Measure intact.
2026-09-15 21:08:44 +00:00
grokbot_ty e919fe9a35 test(photos): batch add + draft restore before green (#59)
Failing session/UI tests for M2-2: multi-photo batch state, Continue
when ≥1, device-draft persistence of batch blobs, Resume Continue
restores. No production change yet (TDD).
2026-09-15 21:07:17 +00:00
grokbot_ty ab5bdafbd8 docs(m2): #58 EXIF bottom + Photos-ready lineup order
Lock Ty’s decisions: width stations start 6in from EXIF-oriented
photo bottom along the approved length axis; lineup is clean cal,
dimension overlay, then other batch PNGs in capture order.
2026-09-15 21:04:16 +00:00
grokbot_ty 2eb825c84e test(fixtures): add Ty matched slab photo sets for M2
Set1 L=48in thickness=2in (2 faces); set2 L=31in thickness=2.5in
(2 faces). Black-sheet samples for Playwright and calibration TDD.
2026-09-15 20:54:51 +00:00
grokbot_ty 4ab68d6b68 docs(m2): lock Photos milestone — batch, calibrate, overlays
Record Ty's narrowed M2 scope on m2-ds: multi-photo batch, pick
calibration, reuse M1 mask/crop, lined-up inventory PNGs, and
axis-oriented dimension overlay copy. Not taxonomy or Woo create.
2026-09-15 20:37:46 +00:00
grokbot_ty eaedcb53dd fix(capture): drop first and last 6in from width samples
sampleWidths used floor(L/6)+1 stations from t=0, so tapered tips
dominated min/max. Sample only interior stations at 6, 12, … ≤ L−6.
When L≤12 there is no interior span: every6In stays empty and
min/max/avg stay 0. CAPTURE.md Widths bullet matches.

sqft, bdft, scale, and inventory PNG are unchanged.
2026-09-15 03:20:28 +00:00
grokbot_ty 11c8124674 test(capture): expect tip-trimmed every-6in width stations
Ty UAT: width min/max must describe bulk, not tips. Pin L=48 tapered
case (7 interior stations, no tip endpoints), uniform mid-board count,
and L≤12 empty/zero fallback before the sampleWidths change lands.
2026-09-15 03:20:17 +00:00
grokbot_ty 44d428b6f4 fix(capture): lower inventory PNG floor to 800px
Product call for the WooCommerce redo. Calculate accepts
sources whose post-crop shorter side is at least 800px.
Warn copy and CAPTURE.md match. No upscale still holds.
2026-09-14 23:40:02 +00:00
grokbot_ty f20d84c543 test(capture): expect 800px inventory PNG floor
Ty UAT rejected phone photos under the 1600px bar.
Lock the mid-band case (shorter side 1000) and floor
geometry at 800×1067 / 0.8×800=640 before the code change.
2026-09-14 23:40:01 +00:00
grokbot_ty fbcd05169f fix(capture): apply approved mask alpha in inventory PNG
Why
CAPTURE.md Crop keeps slab pixels and makes removed pixels transparent.
encodeInventoryPng only drawImage-cropped the AABB from the raw photo.

Scope
composeInventoryFrame samples the source through approvedMask.bitset.
Non-slab pixels stay alpha 0. Drop the 8% MARGIN; fill 80% of mask
extents in a 3:4 frame. Floor rules unchanged.

Verification
cd frontend && npm test
12 pass, including alpha contract (no sheet green in opaque pixels).
2026-09-14 19:11:52 +00:00
grokbot_ty b3a9fdf330 test(capture): require mask alpha in inventory PNG compose
Why
Calculate showed accurate numbers but the inventory PNG kept the sheet
background. Measure already uses approvedMask.bitset; encode did not.

Scope
Add composeInventoryFrame and inventory.test.ts alpha/crop proofs.
Wire encodeInventoryPng({ bitmap, mask, source }) at calculate().
This commit still paints the slabBounds AABB without the bitset gate so
the alpha contract fails until the follow-up fix.

Verification
node --import tsx --test src/png/inventory.test.ts
alpha contract fails (sheet green leaks); 3:4 / 80% / floor pass.
2026-09-14 19:11:52 +00:00
grokbot_ty 114e0b5116 fix(capture): type Length L and Thickness as inch inputs
Replace MeasurePanel steppers with number inputs. Commit on blur, change,
and Calculate snaps inches to nearest 1/8 via inToEighths. Calculate
enables when both drafts parse to > 0. Axis, mask, and scale math unchanged.
2026-09-14 18:26:31 +00:00
grokbot_ty 81e9184c7f test(capture): require typed Measure length and thickness inputs
Ty UAT rejects ± steppers as the only Length L / Thickness control.
Assert MeasurePanel exposes type=number inputs and that inches snap via inToEighths.
2026-09-14 18:26:31 +00:00
grokbot_ty b328871549 fix(capture): rotate length-axis rectangle and scale from mask span
Length axis keeps the approved mask, replaces Flip 180 with a ± angle
slider, and sets scale from wood span along the approved direction rather
than bbox.longSidePx. Widths still sample every 6 in perpendicular to that
axis.
2026-09-14 16:37:00 +00:00
grokbot_ty b781ffeb66 fix(capture): preserve PhotoStage preview aspect on first paint
Contain-fit waits for a real parent width, uses ResizeObserver, and shrinks
width when maxH would otherwise stretch the CSS box. Pure fitDisplay is
covered by node tests.
2026-09-14 16:37:00 +00:00
grokbot_ty 0e94b1fd2a fix(capture): offer camera and gallery on Add photo
Why
On phone, the single file control opened the camera because it set
capture=environment. CAPTURE.md requires camera or picker.

Scope
AddPhoto.svelte now has Take photo (capture=environment) and Choose
from gallery (no capture). Both call the same onPicked path.

Verification
Playwright against overnight after redeploy asserts two inputs and
gallery has no capture attribute.
2026-09-14 15:23:08 +00:00
grokbot_ty c6a48c8d91 deploy: overnight M1 UAT on ubudual :10881 2026-09-14 15:12:30 +00:00
grokbot_ty 0679e1cef7 deploy: keep overnight M1 UAT on box :18080
ubudual :10880 is Ty local Node/SvelteKit — do not overwrite.
2026-09-14 15:10:03 +00:00
grokbot_ty 503449452c deploy: pin M1 UAT to ubudual5060:10880 2026-09-14 15:03:47 +00:00
Ty bb78e35ac3 docs: name crop as Calculate step — 3:4, 80% fill, transparent PNG 2026-09-14 14:16:41 +00:00
grokbot_ty d86fc93ed6 feat(capture): ship Milestone 1 browser capture app
Why
Phone and PC need a single capture flow that ends at inventory PNG and
board-foot numbers with no backend, inference, or Woo create.

Scope
Add frontend/ Vite+Svelte capture UI with Resume, Add photo, Mask,
Length axis, and Measure. Mask runs in a worker with Auto/Green/Black
and a 2.0s sensitivity debounce. Measure computes scale, widths, sqft,
bdft, and a 3:4 alpha inventory PNG. Persist one device draft in
IndexedDB. Serve the static build from frontend/Dockerfile via nginx
(deploy/nginx-m1.conf). No FastAPI.

Verification
sudo docker build -f frontend/Dockerfile -t slabuploader-m1 .
sudo docker run -d --name slabuploader-m1 -p 0.0.0.0:18080:80 slabuploader-m1
curl http://127.0.0.1:18080/ returns 200
Playwright: synthetic green-sheet photo through Calculate shows sqft/bdft
2026-09-14 07:53:39 +00:00
97 changed files with 9133 additions and 2127 deletions
+10
View File
@@ -0,0 +1,10 @@
.git
backend
frontend/node_modules
frontend/dist
frontend/.svelte-kit
**/.DS_Store
*.md
!deploy/README.md
.audit
docs/archive
+8
View File
@@ -35,3 +35,11 @@ images/
# Logs
*.log
# Playwright e2e (#65) — keep README; ignore churn
frontend/tests/e2e/artifacts/*.png
frontend/tests/e2e/artifacts/run-log.txt
frontend/tests/e2e/test-results/
frontend/tests/e2e/playwright-report/
frontend/blob-report/
frontend/playwright/.cache/
+13 -7
View File
@@ -2,16 +2,21 @@
## Agent context
Read this file and `docs/CAPTURE.md`.
Implement Milestone 1 only. Specification: `docs/CAPTURE.md`.
PRD: `docs/PRODUCT.md` for destination and field names. Do not implement Milestone 2, 3, or 4.
Read this file, `docs/CAPTURE.md`, and `docs/PHOTOS.md`.
Implement Milestone 2 only. Specification: `docs/PHOTOS.md`.
PRD: `docs/PRODUCT.md` for destination and field names. Do not implement Milestone 3 or 4.
Do not widen Milestone 2 into Complete-listing taxonomy, SKU, price, copy, or Woo create.
## Milestone 1
## Milestone 2
Capture.
Acceptance: on a phone, one real slab, approved mask, approved length-axis rectangle, user length and thickness, inventory PNG on screen, widths and board feet on screen.
Photos.
Acceptance: on a phone, batch of 1–N photos, user-picked calibration photo through Milestone 1 mask/axis/measure, every successful shot as a 3:4 transparent inventory PNG lined up, Milestone 1 numbers on screen, dimension-overlay copy of the calibration PNG present.
Milestone 1 is inference-free. Background removal, rectangle, measurements, and PNG encode run in the browser as ordinary code. There is no inference URL and no model call in this milestone.
Milestone 2 is inference-free. Batch mask/crop reuse and dimension overlays run in the browser as ordinary code. There is no inference URL and no model call in this milestone.
## Milestone 1 (historical)
Capture. Spec: `docs/CAPTURE.md`. Remains the calibration math and PNG contract Milestone 2 reuses.
## Invariants
@@ -26,3 +31,4 @@ Milestone 1 is inference-free. Background removal, rectangle, measurements, and
- Create works with inference off.
- Source below the PNG size floor requires a new photo.
- Existing store SKU blocks create.
- Dimension overlay orientation follows the user-approved length axis from Milestone 1.
+1
View File
@@ -7,6 +7,7 @@ Lists one-of-a-kind wood slabs on Whidbey Wood Store.
| PRD | `docs/PRODUCT.md` |
| Invariants and agent context | `AGENTS.md` |
| Milestone 1 | `docs/CAPTURE.md` |
| Milestone 2 | `docs/PHOTOS.md` |
| Hosting | `deploy/` |
Owner: Ty. Store: https://www.whidbeywoodstore.com/
+32 -1
View File
@@ -1,4 +1,35 @@
# Host
Record the machine and URL when a container is started.
## Overnight Capture UAT (primary — when Tailscale is up)
- Host: `ubudual5060` / Tailscale `100.64.0.9`
- Container: `slabuploader-m2-overnight` (image `slabuploader-m2:overnight`) — or salvage rebuild of the same role
- Port: **10881** → 80
- URL: `http://100.64.0.9:10881`
- Replaces prior overnight container `slabuploader-m1-overnight` (same port).
## Fallback UAT — Grok Bot computer (when Tailscale to 5060 is down)
Use this path for salvage / Capture UAT until the box can reach `100.64.0.9` again.
- Host: Grok Bot computer (slab-m1 box)
- Serve: static Capture build (`frontend` → `npm run build`, then `vite preview` or equivalent static server)
- Port: **18181** (chosen so it never collides with Ty local on 5060)
- Bind: `0.0.0.0:18181`
- URL (from this box): `http://127.0.0.1:18181` — Ty reaches it via the bot computer / whatever path the app exposes; Tailscale IP of this box when mesh returns
- Branch for salvage: `salvage-one-woo-draft` (cut from `m2-ds`)
- Do **not** require Docker on the box for this fallback.
When Tailscale is restored, prefer primary overnight `:10881` again and stop advertising the fallback as the main UAT URL.
## Ty local Node/SvelteKit on 5060 (do not overwrite)
- Host: `ubudual5060` / `100.64.0.9`
- Container: `slabuploader-m1`
- Port: **18080** → 80
- URL: `http://100.64.0.9:18080`
- **Never** recreate or remap this container for overnight or box-fallback UAT.
- **Never** bind overnight or fallback UAT to **10880** either (legacy Ty local).
Future overnight Capture deploys use a **separate** container name and an open **108XX** port other than Ty’s local ports.
Do not put a host name in the product spec.
-9
View File
@@ -6,12 +6,3 @@ Where the container runs is chosen at runtime.
No FastAPI and no Woo client in this milestone.
`docker-compose.yml`, `Caddyfile.fragment`, and `.env.example` describe an older two-service stack. They are not the Milestone 1 contract.
## Milestone 1 UAT — deployment POR
- **One container.** Build a single Docker image that serves the SvelteKit capture app. No FastAPI, no Woo client, no Caddy TLS edge in M1. (Caddy/compose/Woo are Milestone 3.)
- **Host chosen at runtime.** Record the machine and URL in `INVENTORY.md` when the container starts. Do not put a host name in the product spec.
- **LAN-reachable UAT URL.** For UAT the container binds a port on the LAN interface (not only `127.0.0.1`), so other machines on the LAN — e.g. a phone — reach the app at `http://<host-lan-ip>:<port>`.
- **No backend.** The app is browser-only in M1 (see `docs/CAPTURE.md`). There is no `/api` route and no inference.
Reference for the current UAT host: see `INVENTORY.md` (filled in when the container starts).
+17
View File
@@ -0,0 +1,17 @@
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index index.html;
# SPA: fall back to index.html for any path
location / {
try_files $uri $uri/ /index.html;
}
location ~* \.(js|css|png|jpg|jpeg|svg|woff2?)$ {
expires 7d;
add_header Cache-Control "public";
try_files $uri =404;
}
}
+25 -21
View File
@@ -1,7 +1,7 @@
# Milestone 1 — Capture
Runs in the browser on phone and PC as ordinary client code.
No inference URL. No model. No backend. Mask, rectangle, measurements, and PNG encode are deterministic functions of the photo and user inputs.
No inference URL. No model. No backend. Mask, rectangle, measurements, crop, and PNG encode are deterministic functions of the photo and user inputs.
Milestone 1 uses one reference photo. Further angles are later milestones.
Milestone 1 ends when the user sees the inventory PNG and measurements. Milestone 2 is Review. Milestone 3 is Create. This file does not create a WooCommerce product.
@@ -33,9 +33,9 @@ Otherwise the app opens Add photo.
3. User sets sheet Auto / Green / Black and sensitivity 0–100 (slider or minus / plus). App updates the control immediately. App recomputes the overlay 2.0 seconds after the last sensitivity change. User adjusts until only the slab remains, then approves. App blocks approve if the mask has no slab pixels; user retakes. The approved mask is the footprint for every later step.
4. App draws the minimum-area bounding rectangle of the mask and the length axis on the long side of that rectangle. User rotates the axis if it does not follow the slab length, then approves the axis.
5. User enters length `L` in inches (1/8 in steps) and thickness (1/8 in steps), then presses Calculate.
6. App sets `scale = longest_axis_px / L`.
7. App computes widths, sqft, and bdft from the mask, and writes the inventory PNG.
8. User sees the PNG and the numbers. Milestone 1 stops here.
6. App sets `scale = longest_axis_px / L` (mask/wood span along the approved direction ÷ user `L`; rectangle is direction only).
7. App computes widths, sqft, and bdft from the mask. App crops the masked slab into a 3:4 portrait frame: slab centered, 90% fill at the widest and tallest points, background transparent. App writes that frame as an inventory PNG (`image/png`, 8-bit RGBA).
8. User sees the transparent PNG and the numbers. Milestone 1 stops here.
Calculate does not create a WooCommerce product.
@@ -45,12 +45,6 @@ Phone: iOS Safari, Android Chrome.
PC: Chrome, Edge, Firefox.
Other clients get a hard stop.
## Input photo
Accepted input: JPEG. Phones typically save JPEG; Android Chrome delivers JPEG by default.
If the file is HEIC and the browser cannot decode it, show a one-tap notice to retake the photo as JPEG. HEIC is never silently dropped and never decoded server-side in Milestone 1.
## States
One photo component is reused. Each state advances on approve. Back returns to the previous state. A device draft restores the last approved state.
@@ -59,13 +53,13 @@ One photo component is reused. Each state advances on approve. Back returns to t
2. Add photo — camera or picker. One photo. Setup rules visible.
3. Mask — background removal on that photo.
4. Length axis — bounding rectangle and long-side axis on the same photo.
5. Measure — length, thickness, Calculate, inventory PNG, widths, sqft, bdft. Milestone 1 stops here.
5. Measure — length, thickness, Calculate, crop, inventory PNG, widths, sqft, bdft. Milestone 1 stops here.
## Display
Preview fits the device width. Height is capped so Mask and Measure controls stay on screen. Phone and PC use the same states.
The preview is display only. Mask, rectangle, `scale`, sqft, and PNG encode use the source photo pixels, not the CSS size of the preview.
The preview is display only. Mask, rectangle, crop, `scale`, sqft, and PNG encode use the source photo pixels, not the CSS size of the preview.
## Runtime
@@ -89,22 +83,34 @@ Mask work runs in the worker. The last good overlay stays visible while a pass r
Approve is blocked when the mask has no slab pixels. User retakes.
User approves the mask when only the slab remains. That mask is the footprint for Length axis, measurements, and the inventory PNG.
User approves the mask when only the slab remains. That mask is the footprint for Length axis, crop, measurements, and the inventory PNG.
## Length axis
On the same photo, draw the minimum-area bounding rectangle of the mask and the length axis on the long side of that rectangle. User rotates the axis if it does not follow the slab length, then approves the axis.
On the same photo, keep the approved mask in view and draw the minimum-area bounding rectangle of the mask with the length axis on the long side of that rectangle. User rotates the rectangle (slider with minus and plus) until that direction follows the slab length, then approves the axis.
## Measure
User enters length `L` in inches (1/8 in steps) and thickness (1/8 in steps), then presses Calculate.
- `scale = longest_axis_px / L`
- Widths: min, max, avg, sampled every 6 in along the approved length axis, perpendicular to it
- `scale = longest_axis_px / L` where `longest_axis_px` is the mask/wood span along the approved length direction (slab pixels end-to-end on that axis), not the rectangle edge length. The rectangle sets direction only; user-entered `L` is wood end→end along that direction.
- Widths: min, max, avg from samples taken every 6 in along the approved length axis, perpendicular to it, after dropping the first and last 6 in of the span (bulk widths, not tips). If `L ≤ 12 in` there are no interior stations and all three are 0.
- `sqft = (mask_pixels / scale²) / 144` (2 decimals)
- `bdft = sqft × thickness_in` (2 decimals)
- Crop: 3:4 portrait, slab centered, 90% fill at the widest and tallest points, transparent background
- File: inventory PNG, `image/png`, 8-bit RGBA, alpha kept
User sees the inventory PNG and those numbers. Stop. Calculate does not create a WooCommerce product.
User sees the transparent PNG and those numbers. Stop. Calculate does not create a WooCommerce product.
## Crop
On Calculate, the app builds the inventory image from the approved mask:
1. Keep slab pixels from the source photo. Removed pixels are transparent.
2. Place the slab in a 3:4 portrait frame, centered.
3. Scale so the widest and tallest points of the slab fill 90% of the frame. Extra margin where the slab is narrower.
4. After that crop, shorter side ≥ 800px when the source can supply it. Otherwise warn and require a new photo. No upscale from an undersized source.
5. Encode as PNG with alpha.
## Device draft
@@ -127,10 +133,8 @@ The first inventory photo is the measurement reference. Later inventory photos u
| Format | PNG, 8-bit RGBA (`image/png`) |
| Alpha | Kept through encode and upload |
| Aspect | 3:4 portrait |
| Layout | Slab centered, 80% fill at the widest and tallest points |
| Size | After crop, shorter side ≥ 1600px when the source can supply it |
| Layout | Slab centered, 90% fill at the widest and tallest points |
| Size | After crop, shorter side ≥ 800px when the source can supply it |
Source below that floor: warn and require a new photo. No upscale from an undersized source.
Editor preview of alpha is a light gray checkerboard. The storefront uses the theme background.
File size: aim under 200KB per image (soft goal, not a hard gate in Milestone 1).
-100
View File
@@ -1,100 +0,0 @@
# Milestone 1 — Plan of Record (POR)
Decisions captured as they are made. This is the working plan for implementing and UAT-ing Milestone 1. It is the source of truth for scope, order, and dispatch; the spec detail lives in `docs/CAPTURE.md` and `docs/PRODUCT.md`.
## Scope
Implement **Milestone 1 — Capture** only (see `AGENTS.md`). Browser-only, inference-free, no backend, no Woo create. Milestones 2–4 are out of scope.
## Method — TDD (non-negotiable)
Every card is delivered **test-first** per the `test-driven-development` skill:
```
NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST
```
- **Red-Green-Refactor**, tracer-bullet: one behavior slice at a time (RED→GREEN→REFACTOR), not all-tests-then-all-code.
- **Verify RED** (watch it fail for the right reason) and **Verify GREEN** (it passes + full suite stays green) are mandatory before the card is done.
- Code written before a failing test is **deleted and redone** — not kept as "reference."
- The deterministic core (math + PNG contract) is unit-tested in a browser-runnable JS suite; UI behaviors are tested with Playwright against the fixtures.
- A card is not complete until: every new function has a test, each test was seen failing first, the full suite passes with pristine output.
## Card breakdown (implementation order)
Each card follows TDD. "Define" = write the failing tests (RED); "Build" = minimal code to pass (GREEN); "Refactor" = clean up keeping green.
1. **Scaffold + Dockerfile + test harness** — SvelteKit project with the browser-only M1 runtime (Web Worker, canvas/OffscreenCanvas, IndexedDB via `idb-keyval`), `frontend/Dockerfile`, and the test harness (JS unit runner + **Playwright** web).
- *TDD note:* this card establishes the harness itself (a smoke test that runs and fails, then passes) so every later card has a place to be RED/GREEN.
2. **Deterministic core** — the pure math + PNG-encode functions.
- *RED:* unit tests for `scale`, `sqft`, `bdft`, width sampling, 1/8″ stepping, rounding, and the PNG contract (3:4, 8-bit RGBA, 80% fill, ≥1600px floor, undersized→retake). Run on the fixtures' calibration records.
- *GREEN/REFACTOR:* implement to pass; core is authoritative and server-less.
3. **Capture + Mask** — Add photo (JPEG; HEIC→"retake as JPEG" notice), green/black key, four knobs, debounced recompute, mask approve.
- *RED:* Playwright — load fixture, key sheet, approve; empty-mask blocks approve; HEIC shows the notice.
- *GREEN/REFACTOR:* build the Mask state to pass.
4. **Length axis** — min-area bounding rectangle + long-side axis, user rotate, approve.
- *RED:* Playwright — rect drawn from the approved mask, axis on long side, rotate, approve.
- *GREEN/REFACTOR:* build the axis state.
5. **Measure + PNG** — length/thickness (1/8″), compute sqft/bdft/widths, write the 3:4 inventory PNG. M1 stops here.
- *RED:* Playwright — enter L/thickness → Calculate; assert PNG is 3:4 with alpha, and numbers equal the fixture's expected values.
- *GREEN/REFACTOR:* build Measure + PNG encode.
6. **M1 UAT** — serve the container per `deploy/README.md` §Milestone 1 UAT; run the full Playwright suite in the container; then the manual phone pass. Record host + URL in `deploy/INVENTORY.md`.
## Deployment (UAT)
See `deploy/README.md` §Milestone 1 UAT — deployment POR: one container, host chosen at runtime, LAN-reachable URL, no backend.
## Dev / QA worker dispatch
Two local inference endpoints are available as dispatchable workers for test / dev / QA:
- **`.202:8081`** — `gemma-4-12B-it-q4_k_m` (12B, Q4) — **completion only, no vision.** Use for lighter text: drafting, code review, summaries.
- **`.202:8082`** — `Qwen3.8-27B-UD-IQ3_XXS` (27B, IQ3) — **completion + multimodal (vision).** The **vision worker** (any task that must look at a slab photo) and the stronger general-reasoning endpoint.
Coordinator: Hermes (me) — plans, decomposes, dispatches, verifies, and does work directly when a card is small or judgment-heavy.
### Calibrated delegation policy (2026-09-14)
- **Frame every prompt**: system role + "show each step" + explicit constraints. Small/low-quant models drop multi-step arithmetic in one-shot "reply JSON only" prompts but get it exactly right with scratch-space framing. Do not judge capability from under-framed probes.
- **The deterministic core stays the coordinator's** (TDD'd, unit-tested). A model that's "usually right" on math is not good enough for the spec — math is cheap to test and must be exact.
- **Verify before land**: any load-bearing model output is checked against tests/spec before entering the repo. Workers are assist, not autonomous builders.
- **Output quirks**: gemma wraps JSON in ``` fences — strip before parsing. Vision calls run ~18s/image (text ~1s).
- **One task per endpoint at a time** (no parallel on the same endpoint — backs up and overheats).
- Both run low-quant (Q4/IQ3). Fine for framed assist; deep sustained reasoning is best done by the coordinator directly.
## Test plan (M1)
Three layers. The deterministic core is unit-tested; the end-to-end path is Playwright; a real phone is the final UAT gate.
### 1. Unit (deterministic core) — must pass before any UI consumes it
- `bdft = sqft × thickness_in`; `sqft = (mask_px / scale²) / 144`; `scale = longest_axis_px / L`.
- 1/8 in stepping; rounding (sqft/bdft 2 decimals).
- Width sampling every 6 in along the approved axis.
- PNG encode contract: 3:4 aspect, 8-bit RGBA, 80% fill, shorter side ≥ 1600px when source allows; undersized source → warn + retake (no upscale).
- Mask empty → approve blocked.
### 2. Playwright (automated end-to-end, runs in the UAT container's browser)
Walks the 5 states against a **sample photo**:
- Add photo → Mask (set sheet + sensitivity) → approve → Length axis → approve → Measure (enter L, thickness) → Calculate.
- Assert: PNG rendered, dimensions = 3:4, alpha present; sqft/bdft/widths equal the expected values for that fixture (within tolerance); numbers visible on screen.
- HEIC input → "retake as JPEG" notice shown (not a crash).
- Device draft: reload → Resume → Continue restores last approved state.
### 3. Manual phone UAT (final gate)
On a real phone (iOS Safari / Android Chrome) over the LAN UAT URL: repeat the happy path with one real slab; confirm PNG + numbers on screen. Record host + URL in `deploy/INVENTORY.md`.
## Test fixtures (sample pics)
Real slab photos are the fixtures (supplied by Ty from `\\truenas.local\Business\Samples`, SMB user `SMBSVC`). Each is a DJI MIMO top-down JPEG, ~4000×2256, slab on a green/black sheet.
`tests/fixtures/`:
- **sample_01 … sample_0N.JPG** — a representative handful of real slab photos (pulled on demand from the TrueNAS Samples share; do not bulk-copy the whole folder).
- Each fixture gets a **calibration record** in `tests/fixtures/<name>.json`: the slab's true length and thickness (in inches), the sheet color, and the approved mask/axis reference — used to compute the expected `scale`, `sqft`, `bdft`, and sampled widths for assertions.
The real photos drive both **bg removal** (green/black key) and **rectangle/axis calibration**. Expected values come from the calibration records, so assertions stay deterministic even on real imagery.
**M1 fixture set (agreed, complete for M1):** three black-sheet slabs — one irregular (stress-tests the min-area rectangle/axis), two clean elongated. Green-sheet keying is deferred (not an M1 fixture). A 1 HEIC sample (optional) for the decode-fallback test.
## Open decisions
- (none outstanding for M1 as of this plan; new decisions append here)
+75
View File
@@ -0,0 +1,75 @@
# Milestone 2 — Photos
Builds on Milestone 1. Spec for calibration math and inventory PNG properties: `docs/CAPTURE.md`.
This milestone gets every photo of one slab background-removed and cropped store-ready.
It does not fill Complete-listing taxonomy, SKU, price, or copy. It does not create a WooCommerce product.
No inference URL. No model. Mask and crop for non-calibration shots reuse the approved calibration params as ordinary client code.
## Goal
Phone capture of one slab → a lined-up set of Woo-compatible inventory PNGs, plus Milestone 1 calibration numbers on the reference shot, plus one annotated copy of the calibration PNG with dimension overlays.
## Use case
1. User adds a batch of one or more photos of the same slab (Setup rules from Milestone 1: green or black sheet, top-down reference expectations still apply to the calibration choice).
2. User selects which photo in the batch is the calibration photo.
3. App runs the Milestone 1 workflow on that calibration photo only: Mask → Length axis → Measure (length `L`, thickness, Calculate).
4. After Calculate, the app applies the approved mask sheet and sensitivity, and the same crop rules (3:4 portrait, 90% fill, transparent PNG, shorter-side floor), to every other photo in the batch.
5. App adds a second copy of the calibration inventory PNG with dimension overlays burned in (see Dimension overlay).
6. User sees Photos ready: inventory PNGs lined up in this order — clean calibration PNG, then the dimension-overlay copy of the calibration PNG, then the other batch photos in capture order — plus the Milestone 1 calibration details (widths, sqft, bdft, scale) presented as in Measure.
A batch of one photo still works: that photo is the calibration photo; the end set is the clean PNG plus the dimension-overlay copy.
## States (additions over Milestone 1)
1. Batch — add one or more photos; continue when the batch has at least one.
2. Pick calibration — user chooses which batch photo is the reference.
3. Mask / Length axis / Measure — Milestone 1 states on the calibration photo only.
4. Photos ready — lineup order: clean calibration PNG → dimension-overlay copy → other batch photos in capture order; plus Milestone 1 numbers.
Device draft restores the last approved state, including the batch and which photo was calibration when those exist.
## Apply to non-calibration photos
For each non-calibration photo:
- Reuse the approved Mask sheet (Auto / Green / Black) and sensitivity from the calibration photo. Do not re-open Mask controls for that photo in this milestone.
- Run background removal with those params; compose inventory PNG with the same crop rules as Milestone 1 Calculate.
- If a photo yields an empty mask or fails the PNG size floor, keep it out of the ready set and show a clear per-photo error; other photos still proceed.
Scale, widths, sqft, and bdft stay tied to the calibration photo and user `L` / thickness. Non-calibration photos are visual inventory only.
## Dimension overlay
A second copy of the **calibration** inventory PNG is added to the collection. Overlays are black, large enough to read on phone and storefront.
Orientation follows the **user-approved length axis** from Milestone 1 (not the upright edges of the photo).
- **Length:** dimension graphic beside the wood on the right in axis-space (outside the slab edge along the length direction).
- **Widths:** stations every 12 in along the approved length axis, starting 6 in from the **bottom** of the slab span, continuing while the station is not within 6 in of the top (stations at 6, 18, 30, … in inches along the axis while ≤ `L − 6`). Each width is drawn across the wood perpendicular to the length axis, with arrow tips on the left and right slab edges and the measurement text on that line.
- **Bottom:** after the calibration photo’s EXIF orientation is applied, the bottom edge of that oriented photo establishes which end of the slab is bottom for width stations. Length-axis direction still follows the user-approved Milestone 1 axis; EXIF only picks which end is bottom vs top along that span.
## Photos-ready order
1. Clean calibration inventory PNG
2. Dimension-overlay copy of the calibration PNG
3. Remaining batch inventory PNGs in capture order
The clean calibration PNG remains in the set. The annotated copy is extra.
## Acceptance
On a phone UAT build:
- User can capture 1–N photos, pick calibration, complete Milestone 1 on that shot, and see every successful batch photo as a 3:4 transparent inventory PNG lined up.
- Calibration measures match Milestone 1 presentation.
- Dimension-overlay copy of the calibration PNG is present with black length and width graphics oriented to the approved axis; width stations start 6 in from EXIF-established bottom.
- Lineup order is clean calibration → dimension overlay → other batch photos in capture order.
- No Woo create. No inference URL.
## Out of scope
- Complete-listing fields (SKU, species, category, edge, figure, grade, thickness band, moisture, `fig-*` / `feat-*`, title, short title, description, price)
- WooCommerce create (Milestone 3)
- Inference / Assist (Milestone 4)
- Changing Milestone 1 mask/axis/measure math except to reuse its outputs for batch apply and overlays
+9 -6
View File
@@ -32,8 +32,9 @@ Store categories, attributes, and `fig-*` / `feat-*` tags are the filters. Measu
## System seams
Milestone 1 runs in the browser. No backend and no inference URL.
Handoff is the device draft: source photo, approved mask, inventory PNG, length, thickness, widths, sqft, bdft.
Milestones 1 and 2 run in the browser. No backend and no inference URL.
Milestone 1 handoff is the device draft: source photo, approved mask, inventory PNG, length, thickness, widths, sqft, bdft.
Milestone 2 extends the draft with the photo batch, calibration choice, per-photo inventory PNGs, and the dimension-overlay calibration copy.
Milestone 3 uses FastAPI as the Woo client. Store credentials stay on the server.
## Create
@@ -49,7 +50,7 @@ Work is split into four milestones. Each milestone is done when its acceptance c
### Milestone 1 — Capture
Spec: `docs/CAPTURE.md`. Current work.
Spec: `docs/CAPTURE.md`. Done historically; handoff into Milestone 2.
User stories:
- Photograph a slab on a green or black sheet and remove the background.
@@ -58,10 +59,12 @@ User stories:
Acceptance: inventory PNG and measurements visible on a phone. No Woo create.
### Milestone 2 — Review
### Milestone 2 — Photos
Remaining listing fields in the app.
Acceptance: every field in Complete listing is filled or proposed and editable.
Spec: `docs/PHOTOS.md`. Current work.
Batch photos of one slab, pick calibration, run Milestone 1 on that reference, apply the same mask params and crop to the rest, preview all inventory PNGs lined up, plus a dimension-overlay copy of the calibration PNG.
Acceptance: on a phone, 1–N store-ready inventory PNGs for the slab (including dimension overlay on the calibration copy) with Milestone 1 measures visible. No Woo create. No Complete-listing taxonomy in this milestone.
### Milestone 3 — Create
+713
View File
@@ -0,0 +1,713 @@
<!doctype html>
<!--
Design artifact for SlabUploader issue #99 — "Tape-line overlay replaces axis
rectangle (calibration length + crop)".
Owner-approved design proposal. This is a WIREFRAME, not shippable code.
What it settles: the black ~2pt line is a FIXED entity in the MIDDLE of the
3:4 rectangle; the SLAB alone pans / zooms / tilts so its long axis lands on
the line; the ticks belong to the line, slide along it, and L = tick to tick;
the crop % moves only the overlay's two ends — (1 - fill)/2 of the long axis
in from the top and the bottom — and never changes the aspect ratio.
SCRUB BEFORE SHIPPING — wireframe scaffolding, none of this belongs in the
production workflow:
- the viewport switcher (412 / 390 / 720 / 1440) and the flowbar step nav
- the requirements-coverage board below the stage
- the "was / now" pair of mini diagrams
- the PROPOSAL / OPEN markers and the implementation-notes card
- in-stage tutorial labels ("crop 90% — overlay 5.0% in from the top", …)
- the alignment read-out box (a proposal, not in the issue; nothing gated on it)
- the synthetic slab drawing + grain, and the sample values (88.0" tape, 2.125 thickness)
- the window.__wf debug hook
What stays: the fixed middle line + its two end ticks, the slab transform set
(pan / zoom / tilt) with the visible zoom row and Fit, the tick drag + nudge
semantics, and the single shared crop_fill.
-->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SlabUploader — tape-line overlay wireframe (issue #99)</title>
<style>
:root{
--bg:#121212; --card:#1c1c1c; --card2:#1d1d1d; --line:#333;
--fg:#eee; --mut:#aaa; --acc:#00e5ff; --acc2:#002;
--ok:#7fd37f; --warn:#ffb4a2; --r:3px;
}
*{box-sizing:border-box}
html,body{margin:0;background:var(--bg);color:var(--fg);font-family:system-ui,-apple-system,"Segoe UI",sans-serif;font-size:16px}
.top{max-width:900px;margin:0 auto;padding:14px 12px 4px}
.top h1{font-size:1rem;margin:0 0 8px;color:var(--acc);letter-spacing:.04em;text-transform:uppercase}
.flowbar{display:flex;flex-wrap:wrap;gap:6px}
.fstep{font-size:.72rem;padding:5px 8px;border-radius:var(--r);border:1px solid var(--line);background:var(--card2);color:var(--mut);cursor:pointer}
.fstep.on{background:var(--acc);color:var(--acc2);font-weight:700;border-color:var(--acc)}
.vp{display:flex;align-items:center;gap:8px;margin-top:10px;font-size:.72rem;color:var(--mut)}
.vp button{border:1px solid var(--line);background:var(--card2);color:var(--fg);font-size:.72rem;padding:4px 8px;border-radius:var(--r);cursor:pointer}
.vp button.on{background:var(--acc);color:var(--acc2);font-weight:700}
.stage{display:flex;justify-content:center;padding:18px 10px 40px}
#phone{width:412px;max-width:100%;background:#0c0c0c;border:1px solid #2a2a2a;border-radius:18px;padding:14px;min-height:640px}
.panel{padding:10px 4px;color:var(--fg)}
h1{font-size:1.3rem;margin:0 0 10px}
h3{font-size:.95rem;margin:0 0 8px}
.hint,.sub{color:var(--mut);font-size:.82rem;line-height:1.5;margin:0 0 12px}
.row{display:flex;gap:10px;margin-top:16px}
.primary{flex:1;padding:9px;font-size:.95rem;border-radius:var(--r);border:0;background:var(--acc);color:var(--acc2);font-weight:700;cursor:pointer;align-self:flex-start}
.ghost{padding:9px 18px;border-radius:var(--r);border:1px solid #444;background:transparent;color:#ccc;cursor:pointer}
.nudge{color:#e8a;font-size:.74rem;margin:6px 0 0}
/* ---- the stage: fixed line + movable slab ---- */
.tw{position:relative;width:100%;aspect-ratio:3/4;background:#111;border:1px solid var(--line);border-radius:var(--r);overflow:hidden}
.tw svg{display:block;width:100%;height:100%;touch-action:none;cursor:grab;user-select:none}
.tw.dragging svg{cursor:grabbing}
.tickg{cursor:ns-resize}
.rotg{cursor:ew-resize}
.controls{margin-top:16px;display:flex;flex-direction:column;gap:14px}
.angle,.croprow{display:flex;align-items:center;gap:10px}
.thumbbtn{width:44px;height:44px;border-radius:50%;font-size:1.4rem;border:1px solid #444;background:var(--card2);color:var(--fg);flex:none;cursor:pointer}
input[type=range]{flex:1}
.val{min-width:4ch;text-align:right;font-variant-numeric:tabular-nums}
.ctl-label{font-size:.78rem;color:var(--mut);margin:0 0 6px}
.nums{display:flex;flex-direction:column;gap:8px;margin-top:8px}
.nums div{display:flex;justify-content:space-between;border-bottom:1px solid #2a2a2a;padding-bottom:6px;font-size:.84rem;gap:10px}
.nums span:first-child{color:var(--mut)}
.nums .tab{font-variant-numeric:tabular-nums;text-align:right}
.nums .dim{color:#777}
.status{margin-top:12px;padding:10px 12px;border-radius:var(--r);font-size:.88rem;font-weight:600;background:#2a1512;color:var(--warn)}
.status.ok{background:#12210f;color:var(--ok)}
.plan{margin-top:18px;padding:10px 12px;border:1px solid var(--line);border-radius:var(--r);background:var(--card)}
.plan h3{font-size:.76rem;text-transform:uppercase;letter-spacing:.04em;color:var(--acc);margin:0 0 8px}
.plan ul{margin:0;padding-left:16px;color:var(--mut);font-size:.76rem;line-height:1.65}
.plan code{color:#ddd;font-size:.72rem}
.proposal{display:inline-block;font-size:.6rem;font-weight:800;letter-spacing:.03em;background:#3a2a00;color:#ffc96b;border-radius:2px;padding:0 4px;margin-left:5px;vertical-align:1px}
/* replacement two-up */
.swap{display:grid;grid-template-columns:1fr 1fr;gap:8px;margin-top:14px}
.swap figure{margin:0;background:var(--card2);border:1px solid var(--line);border-radius:var(--r);padding:6px;text-align:center}
.swap svg{width:100%;display:block;background:#0f0f0f;border-radius:2px}
.swap figcaption{font-size:.68rem;color:var(--mut);margin-top:6px;line-height:1.4}
.swap .gone{color:#ff9d8a}
/* scope lineup */
.lineup{list-style:none;padding:0;margin:12px 0 0;display:grid;grid-template-columns:repeat(3,1fr);gap:8px}
.lineup-item{text-align:center;background:var(--card2);border:1px solid var(--line);border-radius:var(--r);padding:6px}
.lineup-item svg{width:100%;display:block;border-radius:2px;background:#0f0f0f}
.role{display:block;margin-top:6px;font-size:.72rem;color:var(--fg);font-weight:600}
.rot-badge{display:inline-block;margin-top:3px;font-size:.6rem;padding:0 4px;border-radius:2px;font-weight:700}
.rot-badge.yes{background:var(--acc);color:var(--acc2)}
.rot-badge.no{background:#3a3a3a;color:#bbb}
/* fields + settings */
.fields{display:flex;gap:16px;margin-top:16px}
.field{flex:1}
.label{display:block;font-size:.85rem;color:var(--mut);margin-bottom:8px}
.inp{box-sizing:border-box;width:100%;min-height:44px;padding:10px;font-size:.95rem;border-radius:var(--r);border:1px solid #444;background:var(--card2);color:var(--fg);font-variant-numeric:tabular-nums}
.settings label{display:block;margin:10px 0 4px;font-size:.85rem;color:var(--mut)}
.settings .section{margin-top:20px;padding-top:14px;border-top:1px solid var(--line)}
.settings button{padding:8px 12px;font-size:.85rem;border-radius:var(--r);border:1px solid #444;background:var(--card2);color:#ddd;cursor:pointer;margin-top:10px}
.settings .msg{font-size:.8rem;margin:8px 0 0;color:var(--mut)}
.settings .msg.ok{color:var(--ok)}
/* requirement coverage board (outside the phone) */
.cov{max-width:900px;margin:0 auto 40px;padding:0 12px}
.cov-inner{border:1px solid var(--line);border-radius:var(--r);background:var(--card);padding:12px 14px}
.cov h2{font-size:.86rem;text-transform:uppercase;letter-spacing:.04em;color:var(--acc);margin:0 0 10px}
.cov table{width:100%;border-collapse:collapse;font-size:.76rem}
.cov th,.cov td{text-align:left;padding:6px 8px;border-bottom:1px solid #2a2a2a;vertical-align:top;color:var(--mut)}
.cov th{color:var(--fg);font-size:.72rem;text-transform:uppercase;letter-spacing:.03em}
.cov td:first-child{color:var(--fg);white-space:nowrap}
.cov .where{color:#ddd}
/* narrow screens: stack the table instead of forcing its 400px min width */
@media (max-width:760px){
.cov thead{display:none}
.cov table,.cov tbody,.cov tr,.cov td{display:block;width:auto}
.cov tr{border-bottom:1px solid #2a2a2a;padding:8px 0}
.cov tr:last-child{border-bottom:0}
.cov td{border:0;padding:1px 0}
.cov td:first-child{white-space:normal;color:var(--acc);font-weight:700}
.cov td:nth-child(3)::before{content:"→ ";color:#666}
}
.hidden{display:none}
</style>
</head>
<body>
<div class="top">
<h1>SlabUploader · tape-line overlay — issue #99</h1>
<div class="flowbar" id="flowbar"></div>
<div class="vp">
<span>Viewport</span>
<span id="vpval">412</span>
<button data-w="390">390</button>
<button data-w="412" class="on">412</button>
<button data-w="720">720</button>
<button data-w="1440">1440</button>
</div>
</div>
<div class="stage"><div id="phone"></div></div>
<div class="cov"><div class="cov-inner">
<h2>Issue #99 — requirement coverage in this wireframe</h2>
<table>
<thead><tr><th>Req</th><th>What it says</th><th>Where it shows up</th></tr></thead>
<tbody>
<tr><td>1 Overlay</td><td>Black ~2pt line + two rifle-scope end ticks; ticks draggable; replaces the cyan bbox + red arrow </td><td class="where">Tape line step: the black line, its two end ticks, the "was / now" pair below the stage, and the hint copy</td></tr>
<tr><td>2 Controls</td><td>Pan / zoom / tilt the photo; ticks ≥44×44px, draggable, arrow + Shift+arrow fine nudge; one pointer path</td><td class="where">Zoom now has a visible row (− / slider / + / Fit) beside the tilt row, plus wheel, pinch, <b>+</b>/<b>−</b> keys and double-click to fit; pan = drag, tilt = handle + arrows</td></tr>
<tr><td>3 Fixed line</td><td>The reference line is fixed in the MIDDLE of the rectangle, and it IS the crop line: its ends are the crop insets</td><td class="where">The line's x is computed from the frame, never from the slab's transform; the crop % moves only its two ends, (1 − fill)/2 of the long axis in from the top and the bottom</td></tr>
<tr><td>4 Crop %</td><td>Global <code>settings.crop_fill</code> (default 90) replaces both <code>FILL = 0.9</code> copies, read live, non-retro. <b>It does not change the aspect ratio</b></td><td class="where">Crop fill control on the tape step and the Settings step: 90% puts the overlay 5% in from the top and the bottom; the 3:4 rectangle itself never changes</td></tr>
<tr><td>5 Crop content</td><td>Post-BG-removal, slab pixels only, transparent background, bounds = slab extent</td><td class="where">Mask step note + the crop frame outline around the stage</td></tr>
<tr><td>6 Transform scope</td><td>Tilt persists only to the calibration pic and its dimension-overlay clone; siblings keep their own orientation</td><td class="where">Photos-ready step lineup: calibration + dimensioned copy tilted, backside sibling untouched</td></tr>
<tr><td>7 Doc fix</td><td><code>capture/types.ts:46</code> says "80%-fill"; live value is 90%</td><td class="where">Noted in the Implementation notes card on the Settings step</td></tr>
</tbody>
</table>
</div></div>
<script>
// ===========================================================================
// issue #99 — the tape line is a FIXED ENTITY.
//
// * The black ~2pt line and its two end ticks never move: not on tilt, not
// on pan, not on zoom. It is fixed in the MIDDLE of the rectangle.
// * The SLAB is the only thing the operator moves (pan / zoom / tilt) until
// its long axis lies on the line, tick to tick.
// * The ticks belong to the line - they slide along it, never with the photo.
// * L = the distance between the two ticks (the owner's tape reading).
// ===========================================================================
const S = {
screen: "tape",
// slab transform (the only movable entity)
tilt: 14, // degrees
zoom: 1,
panX: 60, panY: 0,
// the line's ticks: y positions along the fixed line. They rest at the crop
// insets - (1 - fill)/2 of the rectangle's LONG axis in from the top and the
// bottom - and the operator drags them onto their tape marks from there.
tickA: 32,
tickB: 608,
focus: "A",
fill: 90, // settings.crop_fill - global, default 90
// declared values (entered on Measure, shown on both steps)
L: "88",
thickness: "2.125",
savedMsg: "",
};
// The stage IS the crop frame: 3:4, the same aspect composeInventoryFrame emits.
const VB = { w: 480, h: 640 };
// Slab drawn in stage coordinates. What has to be aligned is the slab's LONG
// AXIS - the centreline running down the middle of the board, which is where
// the tape is laid. (Not a physical side edge.)
const SLAB = {
path: "M 166 40 L 161 150 L 168 300 L 162 455 L 166 600 L 330 608 L 396 596 L 400 320 L 396 96 L 330 34 Z",
axis: [{ x: 282, y: 40 }, { x: 282, y: 600 }],
};
const TICK_LEN = 26; // rifle-scope end tick, total
const TICK_GAP = 40; // min separation between ticks
// Grab radius in stage units. The stage renders ~352-354px wide for a 480-unit
// viewBox, so 68 units is ~50px on screen - comfortably over the required 44x44.
const HIT_R = 34;
const ALIGN_TOL = 3; // stage units
const rad = (a) => a * Math.PI / 180;
// The fixed line sits in the MIDDLE of the rectangle: the centre of the 3:4
// crop frame, which is where the crop centres the slab's long axis.
const lineX = () => VB.w / 2;
// Crop % = (1 - fill)/2 of the rectangle's LONG axis in from the top and the
// bottom (cropMarginFraction() in png/crop.ts). It never touches the aspect
// ratio: the rectangle stays 3:4 and only the overlay's two ends move.
const cropInset = () => VB.h * (1 - S.fill / 100) / 2;
const cropTicks = () => ({ a: cropInset(), b: VB.h - cropInset() });
const spanPx = () => Math.abs(S.tickB - S.tickA);
const pxPerIn = () => spanPx() / (parseFloat(S.L) || 1);
// slab transform: zoom + tilt about the stage centre, then pan
function mapPoint(p) {
const cx = VB.w / 2, cy = VB.h / 2, a = rad(S.tilt), z = S.zoom;
const x = (p.x - cx) * z, y = (p.y - cy) * z;
return { x: cx + x * Math.cos(a) - y * Math.sin(a) + S.panX,
y: cy + x * Math.sin(a) + y * Math.cos(a) + S.panY };
}
function slabTransform() {
return `translate(${S.panX} ${S.panY}) translate(${VB.w / 2} ${VB.h / 2}) rotate(${S.tilt}) scale(${S.zoom}) translate(${-VB.w / 2} ${-VB.h / 2})`;
}
// where the slab's long axis crosses a given stage y
function axisXAt(y) {
const a = mapPoint(SLAB.axis[0]), b = mapPoint(SLAB.axis[1]);
if (Math.abs(b.y - a.y) < 1e-6) return a.x;
return a.x + ((y - a.y) / (b.y - a.y)) * (b.x - a.x);
}
const axisAngle = () => {
const a = mapPoint(SLAB.axis[0]), b = mapPoint(SLAB.axis[1]);
return Math.atan2(b.y - a.y, b.x - a.x) * 180 / Math.PI;
};
// the axis is vertical when its angle points straight down (or up) the screen
const axisVertical = () => { const d = ((axisAngle() % 180) + 180) % 180; return Math.abs(d - 90) < 1.5; };
const axisOffset = () => axisXAt((S.tickA + S.tickB) / 2) - lineX();
const aligned = () => axisVertical() && Math.abs(axisOffset()) <= ALIGN_TOL;
// ---------------------------------------------------------------- the stage
function slabSvg() {
return `
<g id="slab" transform="${slabTransform()}">
<clipPath id="slabClip"><path d="${SLAB.path}"/></clipPath>
<path d="${SLAB.path}" fill="hsl(28,36%,31%)" stroke="#000" stroke-opacity=".25"/>
<g clip-path="url(#slabClip)" opacity=".8">
<path d="M 176 90 Q 290 74 400 96" stroke="hsl(28,34%,36%)" stroke-width="4" fill="none"/>
<path d="M 174 200 Q 290 190 400 210" stroke="hsl(28,34%,35%)" stroke-width="4" fill="none"/>
<path d="M 172 320 Q 290 306 400 326" stroke="hsl(28,34%,37%)" stroke-width="4" fill="none"/>
<path d="M 172 440 Q 290 432 400 452" stroke="hsl(28,34%,34%)" stroke-width="4" fill="none"/>
<path d="M 174 550 Q 290 540 400 560" stroke="hsl(28,34%,36%)" stroke-width="4" fill="none"/>
</g>
</g>`;
}
// the FIXED entity: black 2pt line + two rifle-scope end ticks. No transform.
function fixtureSvg() {
const x = lineX();
const tick = (y, id) => {
const on = S.focus === id;
return `
<line x1="${x - TICK_LEN / 2}" y1="${y}" x2="${x + TICK_LEN / 2}" y2="${y}" stroke="rgba(255,255,255,.92)" stroke-width="6" stroke-linecap="round"/>
<line x1="${x - TICK_LEN / 2}" y1="${y}" x2="${x + TICK_LEN / 2}" y2="${y}" stroke="#000" stroke-width="2.4" stroke-linecap="round"/>
<circle cx="${x}" cy="${y}" r="5" fill="${on ? "#ff3d71" : "#000"}" stroke="#fff" stroke-width="1.5"/>
<circle class="tickg" data-tick="${id}" cx="${x}" cy="${y}" r="${HIT_R}" fill="transparent"/>`;
};
return `
<line x1="${x}" y1="${S.tickA}" x2="${x}" y2="${S.tickB}" stroke="rgba(255,255,255,.28)" stroke-width="6" stroke-linecap="round"/>
<line id="tapeLine" x1="${x}" y1="${S.tickA}" x2="${x}" y2="${S.tickB}" stroke="#000" stroke-width="2.4" stroke-linecap="round"/>
${tick(S.tickA, "A")}${tick(S.tickB, "B")}`;
}
// The crop frame: the rectangle's ASPECT NEVER CHANGES (3:4, from crop.ts).
// The crop % only moves the overlay's ends: (1 - fill)/2 of the long axis in
// from the top and the bottom, shaded here as the two bands it leaves outside.
function cropFrameSvg() {
const m = cropInset();
return `
<rect x="0" y="0" width="${VB.w}" height="${m}" fill="#000" opacity=".30"/>
<rect x="0" y="${VB.h - m}" width="${VB.w}" height="${m}" fill="#000" opacity=".30"/>
<rect id="cropframe" x=".5" y=".5" width="${VB.w - 1}" height="${VB.h - 1}" fill="none" stroke="#555" stroke-width="1" stroke-dasharray="6 5"/>
<text x="6" y="${m / 2 + 4}" fill="#8a8a8a" font-family="system-ui" font-size="11">crop ${S.fill}% — overlay ${((1 - S.fill / 100) / 2 * 100).toFixed(1)}% in from the top</text>
<text x="6" y="${VB.h - m / 2 + 4}" fill="#8a8a8a" font-family="system-ui" font-size="11">${((1 - S.fill / 100) / 2 * 100).toFixed(1)}% in from the bottom</text>`;
}
function rotateHandleSvg() {
return `
<g class="rotg" data-rot="1">
<circle cx="${VB.w / 2}" cy="${VB.h - 26}" r="17" fill="rgba(0,0,0,.6)" stroke="var(--acc)" stroke-width="1.5"/>
<text x="${VB.w / 2}" y="${VB.h - 21}" fill="var(--acc)" font-size="15" font-family="system-ui" text-anchor="middle">&#8635;</text>
<text x="${VB.w / 2 + 24}" y="${VB.h - 22}" fill="#aaa" font-size="10.5" font-family="system-ui">drag to tilt the slab</text>
</g>`;
}
function stageSvg() {
return `<svg viewBox="0 0 ${VB.w} ${VB.h}" id="tsvg" aria-label="fixed tape line over a movable slab">
<rect width="${VB.w}" height="${VB.h}" fill="#16281c"/>
${cropFrameSvg()}
${slabSvg()}
${fixtureSvg()}
${rotateHandleSvg()}
</svg>`;
}
// ------------------------------------------------------------------ screens
const screens = {
batch() {
return `<section class="panel">
<h1>Batch photos</h1>
<p class="hint">Unchanged by #99 — shown for context. Sheet is green or black and fills the frame around the slab.</p>
<div class="row"><button class="primary" data-act="to-mask">Pick calibration photo</button></div>
</section>`;
},
mask() {
return `<section class="panel">
<h1>Mask</h1>
<p class="hint">Unchanged by #99. The approved mask is still what the crop bounds come from.</p>
<div class="nums">
<div><span>Crop bounds</span><span class="tab">slab extent (kept pixels only)</span></div>
<div><span>Crop contents</span><span class="tab">slab pixels only, transparent background</span></div>
</div>
<div class="row">
<button class="ghost" data-act="to-batch">Back</button>
<button class="primary" style="flex:2" data-act="to-tape">Approve mask</button>
</div>
</section>`;
},
tape() {
const off = axisOffset(), ok = aligned();
return `<section class="panel">
<h1>Tape line</h1>
<p class="hint">Drag each tick to where your tape started and ended.</p>
<div class="tw" id="tw">${stageSvg()}</div>
<p class="nudge">The black line is <b>fixed</b>: tilting, dragging and zooming the slab never move it. It stays in the middle of the rectangle.</p>
<p class="nudge">Move the <b>slab</b> instead: drag it to pan, wheel or pinch to zoom, tilt it with the &#8635; handle or the arrow keys — until the board's long axis (the line down the middle of the slab) lies on the fixed line, tick to tick.</p>
<div class="controls">
<div>
<p class="ctl-label">Tilt the slab</p>
<div class="angle">
<button class="thumbbtn" data-act="tilt" data-d="-1" aria-label="tilt left">&#8722;</button>
<input type="range" min="0" max="359" value="${S.tilt}" data-act="tilt-range" aria-label="slab tilt"/>
<button class="thumbbtn" data-act="tilt" data-d="1" aria-label="tilt right">+</button>
<span class="val" id="tiltVal">${S.tilt}&#176;</span>
</div>
<p class="nudge">&larr; &rarr; tilt 1&#176; &nbsp;·&nbsp; Shift + &larr; &rarr; tilt 0.1&#176; &nbsp;·&nbsp; or drag the &#8635; handle</p>
</div>
<div>
<p class="ctl-label">Zoom the slab <span class="sub" style="font-size:.7rem">wheel or pinch over the photo</span></p>
<div class="angle">
<button class="thumbbtn" data-act="zoom" data-d="-1" aria-label="zoom out">&#8722;</button>
<input type="range" min="50" max="300" value="${Math.round(S.zoom * 100)}" data-act="zoom-range" aria-label="slab zoom"/>
<button class="thumbbtn" data-act="zoom" data-d="1" aria-label="zoom in">+</button>
<span class="val" id="zoomVal">${Math.round(S.zoom * 100)}%</span>
</div>
<p class="nudge">Wheel or pinch over the photo zooms to the cursor &nbsp;·&nbsp; <b>+</b> / <b>&#8722;</b> keys step 10% &nbsp;·&nbsp; double-click the photo to fit</p>
<div class="row" style="margin-top:8px"><button class="ghost" data-act="fit">Fit — 100%, centred</button></div>
</div>
<div>
<p class="ctl-label">Tape end ticks <span class="sub" style="font-size:.7rem">on the line; drag along it</span></p>
<p class="nudge">Grab a tick anywhere in its 44&#215;44px target and drag it along the line. The tick you last grabbed takes the arrow keys: <b>&#8593; &#8595;</b> nudge 1px, Shift = 0.1px &mdash; its centre dot turns red to show which one.</p>
</div>
<div>
<p class="ctl-label">Crop fill % <span class="sub" style="font-size:.7rem">global setting</span></p>
<div class="croprow">
<input type="range" min="1" max="100" value="${S.fill}" data-act="fill-range" aria-label="crop fill percent"/>
<span class="val" id="fillVal">${S.fill}%</span>
</div>
<p class="nudge">settings.crop_fill (default 90). It sets how much of the rectangle the overlay covers: at ${S.fill}&#37; the overlay's ends sit ${((1 - S.fill / 100) / 2 * 100).toFixed(1)}&#37; in from the top and the bottom. <b>The rectangle keeps its 3:4 aspect</b> — only the ends move, never the middle. Existing stored PNGs are never re-cropped.</p>
</div>
</div>
<div class="status ${ok ? "ok" : ""}" id="alignstatus">${ok
? "The board's long axis is on the line &#10003;"
: `The board's long axis is ${off > 0 ? "right" : "left"} of the line by ${Math.abs(off).toFixed(0)} px${axisVertical() ? "" : `, and tilted ${S.tilt}&#176;`}.`}</div>
<div class="nums">
<div><span>Fixed line x <span class="dim">(middle of the 3:4 frame)</span></span><span class="tab" id="lineReadout">${lineX().toFixed(1)} of ${VB.w}</span></div>
<div><span>Overlay insets <span class="dim">(from crop fill)</span></span><span class="tab" id="insetReadout">${((1 - S.fill / 100) / 2 * 100).toFixed(1)}% in from top and bottom</span></div>
<div><span>Tape span between ticks</span><span class="tab" id="spanReadout">${spanPx().toFixed(0)} px</span></div>
<div><span>Your tape length <span class="dim">(entered on Measure)</span></span><span class="tab">${(parseFloat(S.L) || 0).toFixed(1)}&#8243;</span></div>
<div><span>Scale from your ticks</span><span class="tab" id="scaleReadout">${pxPerIn().toFixed(2)} px/in</span></div>
<div><span>Mask span projection</span><span class="tab dim">not used — sanity check only</span></div>
</div>
<p class="nudge" style="margin-top:10px">Slab tilt ${S.tilt}&#176; &nbsp;·&nbsp; zoom ${S.zoom.toFixed(2)}&#215; <span class="dim">(moving these never changes the line)</span></p>
<div class="swap">
<figure>${oldOverlayMini()}<figcaption class="gone">&#10007; removed — cyan axis rectangle + red arrow <span class="dim">(PhotoStage.svelte:56-82)</span></figcaption></figure>
<figure>${newOverlayMini()}<figcaption>this step — fixed black 2pt line + two end ticks</figcaption></figure>
</div>
<div class="row">
<button class="ghost" data-act="to-mask">Back</button>
<button class="primary" style="flex:2" data-act="to-measure">Approve tape line</button>
</div>
<div class="plan">
<h3>Two things on this screen are my proposal, not the issue</h3>
<ul>
<li>The alignment read-out above <span class="proposal">PROPOSAL</span> — the issue says "rotate until the taped line sits on the fixed reference line" and doesn't specify how the operator knows. Tell me to drop it and it's gone; nothing is gated on it.</li>
<li>The shaded crop bands <span class="proposal">PROPOSAL</span> — drawn only to make the crop fill visible: the two strips the overlay leaves outside itself at the top and the bottom.</li>
</ul>
</div>
</section>`;
},
measure() {
const mini = (tilt, marks) => `
<svg viewBox="0 0 480 640">
<rect width="480" height="640" fill="#16281c"/>
<g transform="translate(240 320) rotate(${tilt}) translate(-240 -320)">
<path d="M 168 60 L 300 52 L 392 80 L 400 300 L 388 560 L 300 596 L 168 588 Z" fill="hsl(28,36%,31%)"/>
${marks ? `<line x1="168" y1="100" x2="168" y2="548" stroke="#000" stroke-width="2.4"/>
<line x1="155" y1="100" x2="181" y2="100" stroke="#000" stroke-width="2.4"/>
<line x1="155" y1="548" x2="181" y2="548" stroke="#000" stroke-width="2.4"/>` : ""}
</g>
</svg>`;
return `<section class="panel">
<h1>Photos ready</h1>
<div class="fields">
<div class="field"><span class="label">Length L (tape reading)</span><input class="inp" type="number" step="0.125" data-act="length" value="${S.L}"/></div>
<div class="field"><span class="label">Thickness</span><input class="inp" type="number" step="0.125" data-act="thickness" value="${S.thickness}"/></div>
</div>
<div class="nums">
<div><span>L maps to</span><span class="tab">tick-to-tick span (${spanPx().toFixed(0)} px)</span></div>
<div><span>Scale used</span><span class="tab">${pxPerIn().toFixed(2)} px/in</span></div>
<div><span>Mask span projection</span><span class="tab dim">not used</span></div>
</div>
<div class="plan">
<h3>Rotation scope — req. 6</h3>
<ul><li>The aligning tilt reaches the calibration pic and its dimension-overlay clone only.</li>
<li>A backside sibling keeps its own orientation; rotation is never propagated through the shared mask/crop settings.</li></ul>
</div>
<ol class="lineup">
<li class="lineup-item">${mini(S.tilt, true)}<span class="role">Clean calibration</span><span class="rot-badge yes">tilted ${S.tilt}&#176;</span></li>
<li class="lineup-item">${mini(S.tilt, false)}<span class="role">Dimension overlay</span><span class="rot-badge yes">same ${S.tilt}&#176;</span></li>
<li class="lineup-item">${mini(0, false)}<span class="role">Backside sibling</span><span class="rot-badge no">not re-tilted</span></li>
</ol>
<div class="row">
<button class="ghost" data-act="to-tape">Back</button>
<button class="primary" style="flex:2" data-act="to-settings">Continue</button>
</div>
</section>`;
},
settings() {
return `<section class="panel settings">
<h1>Settings</h1>
<div class="section">
<h3>Crop — global</h3>
<label for="crop-fill">Crop Fill % (default 90) — how much of the rectangle the overlay covers</label>
<div class="croprow">
<input type="range" id="crop-fill" min="1" max="100" data-act="fill-range" value="${S.fill}"/>
<span class="val" id="fillVal">${S.fill}%</span>
</div>
<p class="sub" style="margin-top:8px">One shared value replaces the two duplicated <code>FILL = 0.9</code> constants. Both the overlay's ends and the PNG crop read it live, so they can never disagree. At 90&#37; the overlay's ends sit ${((1 - S.fill / 100) / 2 * 100).toFixed(1)}&#37; in from the top and the bottom; <b>the 3:4 aspect ratio never changes</b>. Applies to slabs processed from now on; stored PNGs are not re-cropped.</p>
<button data-act="save-fill">Save Changes</button>
<p class="msg ${S.savedMsg ? "ok" : ""}">${S.savedMsg}</p>
</div>
<div class="section">
<h3>Implementation notes</h3>
<ul class="plan" style="border:0;padding:0;background:none">
<li><code>capture/types.ts:46</code> says "80%-fill PNG crop" — stale; the live value is 90%. Comment gets fixed (req. 7).</li>
<li>Replaces the cyan bbox + red arrow in <code>PhotoStage.svelte:56-82</code>.</li>
<li><code>maskSpanAlong</code> is demoted to a sanity check.</li>
</ul>
</div>
<div class="row"><button class="ghost" data-act="to-tape">Back to tape line</button></div>
</section>`;
},
};
// the two mini overlays for the was/now pair
function oldOverlayMini() {
return `<svg viewBox="0 0 120 160">
<rect width="120" height="160" fill="#141414"/>
<rect x="26" y="18" width="68" height="124" fill="hsl(28,30%,30%)"/>
<rect x="20" y="12" width="80" height="136" fill="none" stroke="#00e5ff" stroke-width="3" stroke-dasharray="8 5"/>
<line x1="60" y1="150" x2="60" y2="26" stroke="#ff3d71" stroke-width="4"/>
<path d="M 60 20 L 52 34 L 68 34 Z" fill="#ff3d71"/>
</svg>`;
}
function newOverlayMini() {
return `<svg viewBox="0 0 120 160">
<rect width="120" height="160" fill="#141414"/>
<rect x="34" y="18" width="52" height="124" fill="hsl(28,30%,30%)"/>
<line x1="60" y1="30" x2="60" y2="132" stroke="#000" stroke-width="2.4"/>
<line x1="48" y1="30" x2="72" y2="30" stroke="#000" stroke-width="2.4"/>
<line x1="48" y1="132" x2="72" y2="132" stroke="#000" stroke-width="2.4"/>
</svg>`;
}
// ----------------------------------------------------------------- plumbing
const phone = document.getElementById('phone');
const flowbar = document.getElementById('flowbar');
const STEPS = [
["batch", "Batch photos"], ["mask", "Mask"], ["tape", "Tape line (#99)"],
["measure", "Photos ready"], ["settings", "&#9881; Settings"],
];
function render() {
phone.innerHTML = screens[S.screen]();
flowbar.innerHTML = STEPS.map(([k, label]) =>
`<button class="fstep ${S.screen === k ? "on" : ""}" data-goto="${k}">${label}</button>`).join('');
if (S.screen === "tape") { bindStage(); refreshStage(); }
}
flowbar.addEventListener('click', (e) => {
const b = e.target.closest('[data-goto]');
if (!b) return;
S.screen = b.dataset.goto;
render();
});
document.querySelector('.vp').addEventListener('click', (e) => {
const b = e.target.closest('[data-w]');
if (!b) return;
document.querySelectorAll('.vp [data-w]').forEach((x) => x.classList.remove('on'));
b.classList.add('on');
phone.style.width = (b.dataset.w > 720 ? '412px' : b.dataset.w + 'px');
document.getElementById('vpval').textContent = b.dataset.w;
});
// Replace only the stage + the live readouts, so an in-progress drag survives
// (a full panel re-render would destroy the element under the pointer).
function refreshStage() {
const host = document.getElementById('tw');
if (!host) return;
host.innerHTML = stageSvg();
const set = (id, txt) => { const el = document.getElementById(id); if (el) el.textContent = txt; };
set('tiltVal', S.tilt + '\u00B0');
set('zoomVal', Math.round(S.zoom * 100) + '%');
set('fillVal', S.fill + '%');
set('insetReadout', `${((1 - S.fill / 100) / 2 * 100).toFixed(1)}% in from top and bottom`);
set('lineReadout', `${lineX().toFixed(1)} of ${VB.w}`);
set('spanReadout', `${spanPx().toFixed(0)} px`);
set('scaleReadout', `${pxPerIn().toFixed(2)} px/in`);
const st = document.getElementById('alignstatus');
if (st) {
const ok = aligned(), off = axisOffset();
st.className = 'status' + (ok ? ' ok' : '');
st.innerHTML = ok
? "The board's long axis is on the line \u2713"
: `The board's long axis is ${off > 0 ? 'right' : 'left'} of the line by ${Math.abs(off).toFixed(0)} px`
+ `${axisVertical() ? '' : `, and tilted ${S.tilt}\u00B0`}.`;
}
window.__wf = { tilt: S.tilt, zoom: S.zoom, panX: S.panX, panY: S.panY,
tickA: S.tickA, tickB: S.tickB, focus: S.focus, fill: S.fill,
lineX: lineX(), spanPx: spanPx(), axisOffset: axisOffset(),
axisXAt: axisXAt((S.tickA + S.tickB) / 2), axisVertical: axisVertical(),
axisAngle: axisAngle(), aligned: aligned() };
}
const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, v));
const norm360 = (a) => ((a % 360) + 360) % 360;
function tiltBy(d) { S.tilt = norm360(S.tilt + d); render(); }
// Visible zoom path. #99 names wheel / trackpad pinch; this is the discoverable
// twin, stepping about the stage centre so the slab stays where it is.
function stepZoom(dir) {
S.zoom = clamp(S.zoom * (dir > 0 ? 1.1 : 1 / 1.1), 0.5, 3);
refreshStage();
}
function fitSlab() { S.zoom = 1; S.panX = 0; S.panY = 0; refreshStage(); }
// no tick picker: the tick you last grabbed is the focused one (set on pointerdown)
function nudgeTick(dir, mult) {
const step = (mult === undefined ? 1 : mult);
if (S.focus === "A") S.tickA = clamp(S.tickA + dir * step, 20, S.tickB - TICK_GAP);
else S.tickB = clamp(S.tickB + dir * step, S.tickA + TICK_GAP, VB.h - 20);
render();
}
function bindStage() {
const host = document.getElementById('tw');
if (!host) return;
const toVB = (e) => {
const r = (host.querySelector('svg') || host).getBoundingClientRect();
return { x: (e.clientX - r.left) * (VB.w / r.width), y: (e.clientY - r.top) * (VB.h / r.height) };
};
// zoom-to-cursor — moves the SLAB only; the line ignores it entirely
function zoomAt(p, factor) {
const cx = VB.w / 2, cy = VB.h / 2;
const z0 = S.zoom, z1 = clamp(z0 * factor, 0.5, 3);
if (z1 === z0) return;
const qx = cx + (p.x - cx - S.panX) / z0;
const qy = cy + (p.y - cy - S.panY) / z0;
S.zoom = z1;
S.panX = p.x - cx - z1 * (qx - cx);
S.panY = p.y - cy - z1 * (qy - cy);
refreshStage();
}
host.addEventListener('wheel', (e) => {
e.preventDefault();
// No single event may slam the clamp: a PC ctrl+wheel notch arrives at
// deltaY ~300 and a trackpad pinch arrives as ctrlKey too, so cap both the
// delta and the per-event factor. Plain wheel stays ~16% per notch.
const dy = clamp(e.deltaY, -200, 200);
const k = e.ctrlKey ? 0.01 : 0.0015;
zoomAt(toVB(e), clamp(Math.exp(-dy * k), 0.8, 1.25));
}, { passive: false });
// belt-and-braces fit: Chromium suppresses this after our pointerdown
// preventDefault (see the tap tracking in end()), some engines still emit it
host.addEventListener('dblclick', (e) => { e.preventDefault(); fitSlab(); });
let drag = null, tb = null, moved = false, downP = null;
const off = { x: 0, y: 0 };
host.addEventListener('pointerdown', (e) => {
const tick = e.target.closest('[data-tick]');
const rot = e.target.closest('[data-rot]');
const p = toVB(e);
moved = false; downP = p;
if (tick) {
S.focus = tick.dataset.tick;
drag = { kind: "tick", id: S.focus };
} else if (rot) {
drag = { kind: "rot", start: p.x, tilt0: S.tilt };
} else {
drag = { kind: "slab" };
off.x = p.x - S.panX; off.y = p.y - S.panY;
}
host.setPointerCapture(e.pointerId);
host.classList.add('dragging');
refreshStage();
e.preventDefault();
});
host.addEventListener('pointermove', (e) => {
if (!drag) return;
const p = toVB(e);
if (downP && (Math.abs(p.x - downP.x) > 3 || Math.abs(p.y - downP.y) > 3)) moved = true;
if (drag.kind === "tick") {
// ticks slide ALONG the fixed line — x is ignored on purpose
const y = clamp(p.y, 20, VB.h - 20);
if (drag.id === "A") S.tickA = clamp(y, 20, S.tickB - TICK_GAP);
else S.tickB = clamp(y, S.tickA + TICK_GAP, VB.h - 20);
} else if (drag.kind === "rot") {
S.tilt = norm360(drag.tilt0 + (p.x - drag.start) * 0.6);
} else {
S.panX = clamp(p.x - off.x, -400, 400);
S.panY = clamp(p.y - off.y, -400, 400);
}
refreshStage();
});
const end = (e) => {
if (!drag) return;
const wasSlab = drag.kind === "slab";
const startP = toVB(e);
drag = null;
host.classList.remove('dragging');
try { host.releasePointerCapture(e.pointerId); } catch (_) {}
// double-tap to fit. We cannot use a 'dblclick' listener: preventDefault on
// pointerdown suppresses the whole compatibility mouse sequence (click and
// dblclick both stop arriving), so track the taps ourselves.
if (wasSlab && !moved) {
const now = performance.now();
const near = tb && Math.abs(startP.x - tb.x) < 12 && Math.abs(startP.y - tb.y) < 12;
if (near && now - tb.t < 350) { tb = null; fitSlab(); return; }
tb = { t: now, x: startP.x, y: startP.y };
}
refreshStage();
};
host.addEventListener('pointerup', end);
host.addEventListener('pointercancel', end);
}
// keyboard: arrows = Adobe nudge model (1 / 0.1 with Shift), +/- = zoom steps
document.addEventListener('keydown', (e) => {
if (S.screen !== "tape") return;
if (e.key === "+" || e.key === "=" || e.key === "-" || e.key === "_") {
stepZoom(e.key === "+" || e.key === "=" ? 1 : -1);
e.preventDefault();
return;
}
if (!e.key.startsWith("Arrow")) return;
const fine = e.shiftKey ? 0.1 : 1;
if (e.key === "ArrowLeft") { S.tilt = norm360(S.tilt - fine); render(); }
else if (e.key === "ArrowRight") { S.tilt = norm360(S.tilt + fine); render(); }
else if (e.key === "ArrowUp") { nudgeTick(-1, fine); }
else if (e.key === "ArrowDown") { nudgeTick(1, fine); }
else return;
e.preventDefault();
});
phone.addEventListener('click', (e) => {
const t = e.target.closest('[data-act]');
if (!t) return;
const a = t.dataset.act;
if (a === "tilt") tiltBy(+t.dataset.d);
else if (a === "zoom") stepZoom(+t.dataset.d);
else if (a === "fit") fitSlab();
else if (a === "to-mask") { S.screen = "mask"; render(); }
else if (a === "to-batch") { S.screen = "batch"; render(); }
else if (a === "to-tape") { S.screen = "tape"; render(); }
else if (a === "to-measure") { S.screen = "measure"; render(); }
else if (a === "to-settings") { S.screen = "settings"; render(); }
else if (a === "save-fill") { S.savedMsg = `Saved crop_fill = ${S.fill}. The overlay's ends and every future PNG crop follow it; the rectangle keeps its 3:4 aspect and stored PNGs are untouched.`; render(); }
});
phone.addEventListener('input', (e) => {
const t = e.target;
if (t.dataset.act === "tilt-range") { S.tilt = +t.value; refreshStage(); }
else if (t.dataset.act === "zoom-range") { S.zoom = clamp(+t.value / 100, 0.5, 3); refreshStage(); }
else if (t.dataset.act === "fill-range") {
// crop % moves the overlay's ENDS in/out from the top and bottom; the
// rectangle's aspect never changes and the line's x never moves.
S.fill = +t.value;
const ct = cropTicks();
S.tickA = ct.a; S.tickB = ct.b;
refreshStage();
}
});
phone.addEventListener('change', (e) => {
const t = e.target;
if (t.dataset.act === "length") { S.L = t.value; render(); }
else if (t.dataset.act === "thickness") { S.thickness = t.value; render(); }
});
render();
</script>
</body>
</html>
-10
View File
@@ -1,10 +0,0 @@
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example
vite.config.js.timestamp-*
playwright-report
test-results
+9 -14
View File
@@ -1,18 +1,13 @@
# SlabUploader M1 — frontend only (no FastAPI/Woo). Multi-stage: build then serve.
FROM node:22-alpine AS build
# Build the static app, then serve only the built artifacts.
# Build context is the repo root: `docker build -f frontend/Dockerfile .`
FROM node:20-slim AS build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
COPY frontend/package.json frontend/package-lock.json* ./
RUN npm install --no-audit --no-fund
COPY frontend/ ./
RUN npm run build
FROM node:22-alpine AS run
WORKDIR /app
ENV NODE_ENV=production
COPY package*.json ./
RUN npm install --omit=dev
COPY --from=build /app/build ./build
COPY --from=build /app/node_modules ./node_modules
FROM nginx:1.27-alpine
COPY deploy/nginx-m1.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/dist /usr/share/nginx/html
EXPOSE 80
ENV PORT=80
CMD ["node", "build/index.js"]
+16
View File
@@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>SlabUploader — Capture</title>
<style>
html, body { margin: 0; padding: 0; background: #111; }
#app { display: block; }
</style>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
+897 -1263
View File
File diff suppressed because it is too large Load Diff
+15 -17
View File
@@ -1,28 +1,26 @@
{
"name": "slab-uploader-frontend",
"name": "slabuploader-frontend",
"version": "0.1.0",
"private": true,
"type": "module",
"scripts": {
"dev": "vite dev",
"build": "vite build",
"dev": "vite",
"build": "tsc --noEmit && vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"test:unit": "vitest run",
"test:e2e": "playwright test"
"test": "node --import tsx --test src/**/*.test.ts",
"test:e2e": "playwright test",
"test:e2e:headed": "playwright test --headed"
},
"dependencies": {
"idb-keyval": "^6.3.0",
"svelte": "^4.2.20"
},
"devDependencies": {
"@playwright/test": "^1.63.0",
"@sveltejs/adapter-node": "^5.2.0",
"@sveltejs/kit": "^2.21.0",
"@sveltejs/vite-plugin-svelte": "^5.0.0",
"svelte": "^5.0.0",
"svelte-check": "^4.1.0",
"typescript": "^5.8.0",
"vite": "^6.3.0",
"vitest": "^3.0.0"
},
"dependencies": {
"idb-keyval": "^6.2.1"
"@sveltejs/vite-plugin-svelte": "^3.1.2",
"svelte-preprocess": "^5.1.4",
"tsx": "^4.23.13",
"typescript": "^5.7.3",
"vite": "^5.4.19"
}
}
-17
View File
@@ -1,17 +0,0 @@
import { defineConfig, devices } from '@playwright/test';
// Playwright config for M1 end-to-end tests.
// base URL points at the LAN UAT container (card M1-6). Override with BASE_URL.
export default defineConfig({
testDir: './tests/e2e',
timeout: 60_000,
use: {
baseURL: process.env.BASE_URL || 'http://192.168.1.202:18080'
},
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] }
}
]
});
+30
View File
@@ -0,0 +1,30 @@
import { defineConfig, devices } from '@playwright/test';
/**
* M2 #65 — Playwright visual evidence against local vite preview (not ubudual).
* Artifacts land in tests/e2e/artifacts/ (png churn gitignored; README committed).
*/
export default defineConfig({
testDir: './tests/e2e',
fullyParallel: false,
forbidOnly: !!process.env.CI,
retries: 0,
workers: 1,
timeout: 180_000,
expect: { timeout: 30_000 },
reporter: [['list'], ['html', { open: 'never', outputFolder: 'tests/e2e/playwright-report' }]],
use: {
baseURL: 'http://127.0.0.1:4173',
trace: 'retain-on-failure',
screenshot: 'only-on-failure',
video: 'off',
...devices['Pixel 7'],
},
webServer: {
command: 'npm run build && npm run preview -- --host 127.0.0.1 --port 4173',
url: 'http://127.0.0.1:4173',
reuseExistingServer: !process.env.CI,
timeout: 180_000,
},
outputDir: 'tests/e2e/test-results',
});
+105
View File
@@ -0,0 +1,105 @@
<script lang="ts">
import { onMount, onDestroy } from 'svelte';
import { supported } from './browser';
import { createCaptureSession, type CaptureSession } from './capture/session';
import type { Capture } from './capture/capture.types';
import Resume from './capture/Resume.svelte';
import BatchPhotos from './capture/BatchPhotos.svelte';
import PickCalibration from './capture/PickCalibration.svelte';
import MaskPanel from './capture/MaskPanel.svelte';
import AxisPanel from './capture/AxisPanel.svelte';
import MeasurePanel from './capture/MeasurePanel.svelte';
const gate = typeof window !== 'undefined' ? supported() : 'window unavailable';
const session: CaptureSession = createCaptureSession();
let cap: Capture = { k: 'batch', photos: [] };
let busy = false;
let pngUrl: string | null = null;
const unsubCap = session.capture.subscribe((c) => {
cap = c;
pngUrl = c.k === 'measure' ? session.getInventoryUrl() : null;
});
const unsubBusy = session.busy.subscribe((b) => {
busy = b;
});
onMount(() => {
void session.resumeProbe();
});
onDestroy(() => {
unsubCap();
unsubBusy();
session.dispose();
});
</script>
{#if gate}
<main class="hardstop">
<h1>Unsupported browser</h1>
<p>
SlabUploader needs a modern browser with canvas, workers, and IndexedDB support:
iOS Safari, Android Chrome, or desktop Chrome, Edge, or Firefox.
</p>
<p class="detail">Missing: {gate}</p>
</main>
{:else}
<main class="app">
{#if cap.k === 'resume'}
<Resume onContinue={() => session.continueDraft()} onFresh={() => void session.startFresh()} />
{:else if cap.k === 'batch'}
<BatchPhotos
photos={cap.photos}
onAdd={(f) => void session.addToBatch(f)}
onRemove={session.removeFromBatch}
onContinue={session.continueBatch}
/>
{:else if cap.k === 'pickCalibration'}
<PickCalibration
photos={cap.photos}
onPick={session.pickCalibration}
onBack={session.back}
/>
{:else if cap.k === 'mask'}
<MaskPanel
state={cap}
{busy}
setSensitivity={session.setSensitivity}
setSheet={session.setSheet}
approveMask={session.approveMask}
retakes={session.retakes}
back={session.back}
/>
{:else if cap.k === 'axis'}
<AxisPanel
state={cap}
setAxisAngle={session.setAxisAngle}
setTape={session.setTape}
approveAxis={session.approveAxis}
back={session.back}
/>
{:else}
<MeasurePanel
state={cap}
setLength={session.setLength}
setThickness={session.setThickness}
calculate={session.calculate}
back={session.back}
{pngUrl}
/>
{/if}
</main>
{/if}
<style>
:global(body) {
margin: 0;
background: #121212;
font-family: system-ui, -apple-system, Segoe UI, sans-serif;
}
.app, .hardstop { max-width: 720px; margin: 0 auto; min-height: 100vh; }
.hardstop { padding: 48px 24px; color: #eee; text-align: center; }
.hardstop h1 { font-size: 1.6rem; }
.detail { color: #888; font-size: 0.85rem; }
</style>
-32
View File
@@ -1,32 +0,0 @@
:root {
--bg: #1a1a1a;
--fg: #f2f2f2;
--accent: #4f9d69;
--checker: repeating-conic-gradient(#e8e8e8 0% 25%, #f7f7f7 0% 50%) 0 0 / 24px 24px;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
background: var(--bg);
color: var(--fg);
}
button {
font: inherit;
padding: 0.6rem 1rem;
border: 0;
border-radius: 8px;
background: var(--accent);
color: #04140b;
font-weight: 600;
cursor: pointer;
}
button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
-12
View File
@@ -1,12 +0,0 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>
+26
View File
@@ -0,0 +1,26 @@
// Supported clients: iOS Safari, Android Chrome, PC Chrome/Edge/Firefox.
// Gated on required API presence; anything missing → hard stop.
export function supported(): string | null {
if (typeof window === 'undefined') return 'window unavailable';
if (typeof createImageBitmap !== 'function') return 'createImageBitmap';
if (typeof Worker === 'undefined') return 'Worker';
if (typeof indexedDB === 'undefined') return 'IndexedDB';
const hasOffscreen =
typeof OffscreenCanvas !== 'undefined' &&
typeof OffscreenCanvas.prototype.convertToBlob === 'function';
const hasCanvasBlob =
typeof HTMLCanvasElement !== 'undefined' &&
typeof HTMLCanvasElement.prototype.toBlob === 'function';
if (!hasOffscreen && !hasCanvasBlob) return 'canvas PNG encode';
const ua = navigator.userAgent;
const isIOS = /iPad|iPhone|iPod/.test(ua) || (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1);
const isAndroid = /Android/i.test(ua);
const isSafari = /Safari/i.test(ua) && !/Chrome|CriOS|Edg|Firefox|FxiOS/i.test(ua);
const isChrome = /Chrome|CriOS/i.test(ua) && !/Edg/i.test(ua);
const isEdge = /Edg/i.test(ua);
const isFirefox = /Firefox|FxiOS/i.test(ua);
if (isIOS && isSafari) return null;
if (isAndroid && isChrome) return null;
if (!isIOS && !isAndroid && (isChrome || isEdge || isFirefox)) return null;
return 'unsupported browser';
}
+154
View File
@@ -0,0 +1,154 @@
<script lang="ts">
import type { Capture } from './capture.types';
import type { TapeTransform } from './tapeGeometry';
import { defaultTicks } from './tapeGeometry';
import PhotoStage from './PhotoStage.svelte';
import { getCropFill, setCropFill } from '../settings/cropFill';
import { cropMarginFraction } from '../png/crop';
export let state: Extract<Capture, { k: 'axis' }>;
export let setAxisAngle: (deg: number) => void;
export let setTape: (tape: TapeTransform) => void;
export let approveAxis: () => void;
export let back: () => void;
let cropFill = getCropFill();
$: maskW = state.source.widthPx > 0 ? state.source.widthPx : 0;
$: maskH = maskW > 0 ? state.approvedMask.bitset.length / maskW : 0;
$: tape = state.axis.tape;
$: tiltShown = Math.round(tape.tiltDeg * 10) / 10;
const onTapeChange = (t: TapeTransform) => setTape(t);
const nudgeTilt = (delta: number) => {
setTape({ ...tape, tiltDeg: tape.tiltDeg + delta });
};
const setTilt = (deg: number) => {
setTape({ ...tape, tiltDeg: deg });
// Keep legacy path warm for any callers still using setAxisAngle.
setAxisAngle(deg);
};
const nudgeZoom = (dir: number) => {
const z = dir < 0 ? Math.max(0.5, tape.zoom / 1.1) : Math.min(3, tape.zoom * 1.1);
setTape({ ...tape, zoom: z });
};
const onFill = (pct: number) => {
const fill = Math.min(1, Math.max(0.01, pct / 100));
cropFill = setCropFill(fill);
const ticks = defaultTicks(cropFill);
setTape({ ...tape, ...ticks });
};
const fit = () => setTape({ ...tape, zoom: 1, panX: 0, panY: 0 });
</script>
<section class="panel">
<h1>Tape line</h1>
<p class="hint">Drag each tick to where your tape started and ended.</p>
<p class="nudge">The black line is <b>fixed</b>: tilting, dragging and zooming the slab never move it. It stays in the middle of the rectangle.</p>
<PhotoStage
mode="tape"
blob={state.source.blob}
maskBitset={state.approvedMask.bitset}
{maskW}
{maskH}
{tape}
{cropFill}
onTapeChange={onTapeChange}
/>
<div class="controls">
<div>
<p class="ctl-label">Tilt the slab</p>
<div class="angle">
<button type="button" class="thumb" aria-label="Tilt left" on:click={() => nudgeTilt(-1)}>−</button>
<input
type="range" min="0" max="359" step="0.1"
value={((tiltShown % 360) + 360) % 360}
on:input={(e) => setTilt(Number(e.currentTarget.value))}
/>
<button type="button" class="thumb" aria-label="Tilt right" on:click={() => nudgeTilt(1)}>+</button>
<span class="val">{tiltShown}°</span>
</div>
<p class="nudge">← → tilt 1° · Shift+← → 0.1° · or drag the ↻ handle</p>
</div>
<div>
<p class="ctl-label">Zoom the slab</p>
<div class="angle">
<button type="button" class="thumb" aria-label="Zoom out" on:click={() => nudgeZoom(-1)}>−</button>
<input
type="range" min="50" max="300" step="1"
value={Math.round(tape.zoom * 100)}
on:input={(e) => setTape({ ...tape, zoom: Number(e.currentTarget.value) / 100 })}
/>
<button type="button" class="thumb" aria-label="Zoom in" on:click={() => nudgeZoom(1)}>+</button>
<span class="val">{Math.round(tape.zoom * 100)}%</span>
</div>
<div class="row tight">
<button type="button" class="ghost" on:click={fit}>Fit — 100%, centred</button>
</div>
</div>
<div>
<p class="ctl-label">Crop fill % <span class="sub">global setting</span></p>
<div class="croprow">
<input
type="range" min="50" max="100" step="1"
value={Math.round(cropFill * 100)}
aria-label="Crop fill percent"
on:input={(e) => onFill(Number(e.currentTarget.value))}
/>
<span class="val">{Math.round(cropFill * 100)}%</span>
</div>
<p class="nudge">
settings.crop_fill (default 90). At {Math.round(cropFill * 100)}% the overlay ends sit
{(cropMarginFraction(cropFill) * 100).toFixed(1)}% in from top and bottom.
The 3:4 aspect never changes. Stored PNGs are not re-cropped.
</p>
</div>
<div class="nums">
<div><span>Tape span between ticks</span><span class="tab">{tape.spanPx.toFixed(0)} px</span></div>
<div><span>Scale (once L entered)</span><span class="tab dim">span / L → px/in</span></div>
</div>
</div>
<div class="row">
<button type="button" class="ghost" on:click={back}>Back</button>
<button type="button" class="primary" on:click={approveAxis}>Approve tape line</button>
</div>
</section>
<style>
.panel { padding: 16px; color: #eee; }
h1 { font-size: 1.3rem; margin: 0 0 8px; }
.hint { color: #aaa; font-size: 0.9rem; margin: 0 0 8px; }
.nudge { color: #e8a; font-size: 0.74rem; margin: 6px 0 0; line-height: 1.45; }
.controls { margin-top: 16px; display: flex; flex-direction: column; gap: 14px; }
.ctl-label { font-size: 0.78rem; color: #aaa; margin: 0 0 6px; }
.ctl-label .sub { font-size: 0.7rem; color: #777; }
.angle, .croprow { display: flex; align-items: center; gap: 10px; }
.thumb {
width: 44px; height: 44px; border-radius: 50%; font-size: 1.4rem;
border: 1px solid #444; background: #1d1d1d; color: #eee; flex: none;
}
input[type='range'] { flex: 1; }
.val { min-width: 4ch; text-align: right; font-variant-numeric: tabular-nums; }
.row { display: flex; gap: 10px; margin-top: 16px; }
.row.tight { margin-top: 8px; }
.primary {
flex: 1; padding: 14px; font-size: 1rem; border-radius: 10px; border: 0;
background: #00e5ff; color: #002; font-weight: 700;
}
.ghost { flex: 0.7; padding: 14px; border-radius: 10px; border: 1px solid #444; background: transparent; color: #ccc; }
.nums { display: flex; flex-direction: column; gap: 8px; margin-top: 4px; }
.nums div { display: flex; justify-content: space-between; border-bottom: 1px solid #2a2a2a; padding-bottom: 6px; font-size: 0.84rem; gap: 10px; }
.nums span:first-child { color: #aaa; }
.tab { font-variant-numeric: tabular-nums; text-align: right; }
.dim { color: #777; }
</style>
+102
View File
@@ -0,0 +1,102 @@
<script lang="ts">
import { onDestroy } from 'svelte';
import type { Source } from './capture.types';
export let photos: Source[];
export let onAdd: (f: File) => void;
export let onRemove: (index: number) => void;
export let onContinue: () => void;
let urls: string[] = [];
$: {
for (const u of urls) URL.revokeObjectURL(u);
urls = photos.map((p) => URL.createObjectURL(p.blob));
}
onDestroy(() => {
for (const u of urls) URL.revokeObjectURL(u);
});
const pick = (e: Event) => {
const input = e.target as HTMLInputElement;
const list = input.files;
if (!list || list.length === 0) return;
// Support multi-select from gallery when the browser allows it.
for (let i = 0; i < list.length; i++) onAdd(list[i]);
input.value = '';
};
$: canContinue = photos.length >= 1;
</script>
<section class="panel">
<h1>Batch photos</h1>
<ul class="setup">
<li>Sheet is green or black, fills the frame around the slab.</li>
<li>Slab fully on the sheet, nothing on top of it.</li>
<li>Top-down reference shot: lens parallel to the face.</li>
<li>Slab length along the long side of the picture.</li>
<li>Add one or more photos of the same slab, then Continue.</li>
</ul>
{#if photos.length > 0}
<ul class="thumbs" aria-label="Batch photos">
{#each photos as _p, i (urls[i])}
<li class="thumb">
<img src={urls[i]} alt={`Batch photo ${i + 1}`} />
<button type="button" class="rm" aria-label={`Remove photo ${i + 1}`} on:click={() => onRemove(i)}>Remove</button>
</li>
{/each}
</ul>
{:else}
<p class="empty">No photos yet. Add at least one to continue.</p>
{/if}
<div class="pick">
<label class="pick-btn">Take photo
<input type="file" accept="image/*" capture="environment" on:change={pick} />
</label>
<label class="pick-btn">Choose from gallery
<input type="file" accept="image/*" multiple on:change={pick} />
</label>
</div>
<button
class="continue"
type="button"
disabled={!canContinue}
on:click={onContinue}
>Continue</button>
</section>
<style>
.panel { padding: 24px 16px; color: #eee; }
h1 { font-size: 1.4rem; }
.setup { color: #bbb; line-height: 1.6; font-size: 0.95rem; }
.empty { color: #888; margin: 16px 0; }
.thumbs {
list-style: none; padding: 0; margin: 16px 0;
display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 12px;
}
.thumb {
background: #1e1e1e; border-radius: 8px; overflow: hidden;
display: flex; flex-direction: column;
}
.thumb img { width: 100%; aspect-ratio: 3 / 4; object-fit: cover; display: block; background: #000; }
.rm {
border: 0; background: #333; color: #ccc; padding: 8px; font-size: 0.85rem; cursor: pointer;
}
.pick { margin-top: 12px; display: flex; flex-direction: column; gap: 12px; }
.pick-btn {
background: #00e5ff; color: #002; border: 0; border-radius: 8px;
padding: 12px 20px; font-size: 1rem; font-weight: 600;
text-align: center; cursor: pointer;
}
.pick-btn input[type='file'] { display: none; }
.continue {
margin-top: 20px; width: 100%; padding: 14px; font-size: 1.05rem; border-radius: 10px; border: 0;
background: #00e5ff; color: #002; font-weight: 700; cursor: pointer;
}
.continue:disabled { opacity: 0.35; cursor: not-allowed; }
</style>
+93
View File
@@ -0,0 +1,93 @@
<script lang="ts">
import type { Capture, Sheet } from './capture.types';
import PhotoStage from './PhotoStage.svelte';
export let state: Extract<Capture, { k: 'mask' }>;
export let busy: boolean;
export let setSensitivity: (n: number) => void;
export let setSheet: (s: Sheet) => void;
export let approveMask: () => void;
export let retakes: () => void;
export let back: () => void;
const sheets: Sheet[] = ['auto', 'green', 'black'];
$: empty = state.overlay !== null && state.overlay.slabPx === 0;
$: maskW = state.overlay && state.source.widthPx > 0 ? state.source.widthPx : 0;
$: maskH = maskW > 0 && state.overlay ? state.overlay.bitset.length / maskW : 0;
$: canApprove = state.overlay !== null && state.overlay.slabPx > 0;
</script>
<section class="panel">
<h1>Mask</h1>
<div class="stage-wrap">
<PhotoStage
blob={state.source.blob}
maskBitset={state.overlay?.bitset ?? null}
{maskW}
{maskH}
/>
{#if busy}<div class="busy">Working…</div>{/if}
{#if state.failed}<div class="warn">Mask pass failed. Adjust and retry, or retake the photo.</div>{/if}
{#if empty}<div class="warn">No slab pixels in the mask. Adjust sensitivity or retake.</div>{/if}
</div>
<div class="controls">
<div class="seg" role="group" aria-label="Sheet">
{#each sheets as s (s)}
<button type="button" class:active={state.control.sheet === s} on:click={() => setSheet(s)}>
{s[0].toUpperCase() + s.slice(1)}
</button>
{/each}
</div>
<div class="sens">
<button type="button" class="thumb" aria-label="Decrease sensitivity" on:click={() => setSensitivity(state.control.sensitivity - 1)}>−</button>
<input
type="range" min="0" max="100" step="1"
value={state.control.sensitivity}
on:input={(e) => setSensitivity(Number(e.currentTarget.value))}
/>
<button type="button" class="thumb" aria-label="Increase sensitivity" on:click={() => setSensitivity(state.control.sensitivity + 1)}>+</button>
<span class="val">{state.control.sensitivity}</span>
</div>
</div>
<div class="row">
<button type="button" class="ghost" on:click={back}>Back</button>
<button type="button" class="ghost" on:click={retakes}>Retake</button>
<button type="button" class="primary" disabled={!canApprove} on:click={approveMask}>
Approve mask
</button>
</div>
</section>
<style>
.panel { padding: 16px; color: #eee; }
h1 { font-size: 1.3rem; margin: 0 0 12px; }
.stage-wrap { position: relative; }
.busy {
position: absolute; top: 8px; right: 8px; background: rgba(0,0,0,0.7);
color: #fff; padding: 4px 10px; border-radius: 6px; font-size: 0.85rem;
}
.warn { color: #ffb4a2; margin-top: 8px; font-size: 0.9rem; }
.controls { margin-top: 16px; display: flex; flex-direction: column; gap: 14px; }
.seg { display: flex; gap: 8px; }
.seg button {
flex: 1; padding: 12px; font-size: 1rem; border-radius: 8px;
border: 1px solid #444; background: #1d1d1d; color: #ccc;
}
.seg button.active { background: #00e5ff; color: #002; font-weight: 700; border-color: #00e5ff; }
.sens { display: flex; align-items: center; gap: 10px; }
.thumb {
width: 48px; height: 48px; border-radius: 50%; font-size: 1.6rem;
border: 1px solid #444; background: #1d1d1d; color: #eee; flex: none;
}
input[type='range'] { flex: 1; }
.val { min-width: 2.2ch; text-align: right; font-variant-numeric: tabular-nums; }
.row { display: flex; gap: 12px; margin-top: 20px; }
.primary {
flex: 2; padding: 14px; font-size: 1.05rem; border-radius: 10px; border: 0;
background: #00e5ff; color: #002; font-weight: 700;
}
.primary:disabled { background: #333; color: #777; }
.ghost { flex: 1; padding: 14px; border-radius: 10px; border: 1px solid #444; background: transparent; color: #ccc; }
</style>
+229
View File
@@ -0,0 +1,229 @@
<script lang="ts">
import { onDestroy } from 'svelte';
import type { Capture } from './capture.types';
import { inToEighths, eighthsToIn } from './capture.types';
import { fmtEighths } from './session';
import PhotoStage from './PhotoStage.svelte';
import {
buildPhotosReadyLineup,
photosReadyPngAlt,
type PhotosReadyLineupItem,
} from './photosReadyLineup';
export let state: Extract<Capture, { k: 'measure' }>;
export let setLength: (e: number) => void;
export let setThickness: (e: number) => void;
export let calculate: () => Promise<void>;
export let back: () => void;
export let pngUrl: string | null;
let calcBusy = false;
let lengthIn = state.lengthEighths === 0 ? '' : String(eighthsToIn(state.lengthEighths));
let thicknessIn = state.thicknessEighths === 0 ? '' : String(eighthsToIn(state.thicknessEighths));
let lineupUrls: string[] = [];
let lineupItems: PhotosReadyLineupItem[] = [];
$: photosReady = !!state.results;
$: {
for (const u of lineupUrls) URL.revokeObjectURL(u);
lineupUrls = [];
if (state.results) {
lineupItems = buildPhotosReadyLineup({
inventoryPng: state.results.inventoryPng,
overlayPng: state.results.overlayPng,
batchApply: state.batchApply,
sourceBelowFloor: state.results.sourceBelowFloor,
});
lineupUrls = lineupItems.map((it) =>
it.kind === 'png' ? URL.createObjectURL(it.blob) : '',
);
} else {
lineupItems = [];
}
}
onDestroy(() => {
for (const u of lineupUrls) URL.revokeObjectURL(u);
});
const positiveInches = (raw: string): number | null => {
const inches = Number.parseFloat(raw);
return Number.isFinite(inches) && inches > 0 ? inches : null;
};
const commitLength = () => {
const inches = positiveInches(String(lengthIn));
const eighths = inches === null ? 0 : inToEighths(inches);
setLength(eighths);
lengthIn = eighths === 0 ? '' : String(eighthsToIn(eighths));
};
const commitThickness = () => {
const inches = positiveInches(String(thicknessIn));
const eighths = inches === null ? 0 : inToEighths(inches);
setThickness(eighths);
thicknessIn = eighths === 0 ? '' : String(eighthsToIn(eighths));
};
$: canCalc = positiveInches(String(lengthIn)) !== null && positiveInches(String(thicknessIn)) !== null;
const doCalc = async () => {
calcBusy = true;
try {
commitLength();
commitThickness();
await calculate();
} finally { calcBusy = false; }
};
</script>
<section class="panel">
<h1>{photosReady ? 'Photos ready' : 'Measure'}</h1>
{#if !photosReady}
<PhotoStage
mode="tape"
interactive={false}
blob={state.source.blob}
tape={state.axis.tape}
maskBitset={state.approvedMask.bitset}
maskW={state.source.widthPx > 0 ? state.source.widthPx : 0}
maskH={state.source.widthPx > 0 ? state.approvedMask.bitset.length / state.source.widthPx : 0}
/>
<div class="fields">
<div class="field">
<span class="label">Length L</span>
<input
type="number"
aria-label="Length L"
inputmode="decimal"
step="0.125"
min="0"
placeholder="0"
bind:value={lengthIn}
on:blur={commitLength}
on:change={commitLength}
/>
</div>
<div class="field">
<span class="label">Thickness</span>
<input
type="number"
aria-label="Thickness"
inputmode="decimal"
step="0.125"
min="0"
placeholder="0"
bind:value={thicknessIn}
on:blur={commitThickness}
on:change={commitThickness}
/>
</div>
</div>
<div class="row">
<button type="button" class="ghost" on:click={back}>Back</button>
<button
type="button"
class="primary"
disabled={calcBusy || !canCalc}
on:click={doCalc}
>
{calcBusy ? 'Calculating…' : 'Calculate'}
</button>
</div>
{:else if state.results}
<div class="results" aria-label="Photos ready">
<div class="nums">
<div><span>Widths</span><span>{fmtEighths(state.results.widths.min)} – {fmtEighths(state.results.widths.max)} (avg {fmtEighths(state.results.widths.avg)})</span></div>
<div><span>Sqft</span><span>{state.results.sqft.toFixed(2)}</span></div>
<div><span>Bdft</span><span>{state.results.bdft.toFixed(2)}</span></div>
<div><span>Scale</span><span>{state.results.scale.toFixed(2)} px/in</span></div>
</div>
{#if state.results.sourceBelowFloor}
<div class="warn">Source is below the 800px floor for the inventory PNG. Retake a larger photo.</div>
{/if}
<ol class="lineup" aria-label="Inventory PNG lineup">
{#each lineupItems as item, i (i)}
{#if item.kind === 'png'}
<li class="lineup-item png">
<img src={lineupUrls[i]} alt={photosReadyPngAlt(item)} />
<span class="role">
{#if item.role === 'calibration'}
Clean calibration
{:else if item.role === 'overlay'}
Dimension overlay
{:else}
Batch photo {(item.batchIndex ?? 0) + 1}
{/if}
</span>
</li>
{:else}
<li class="lineup-item error" role="alert">
<span class="err-title">Photo {item.batchIndex + 1} failed</span>
<span class="err-msg">{item.message}</span>
</li>
{/if}
{/each}
</ol>
<!-- Legacy single-PNG fallback only when lineup helper yielded nothing but pngUrl exists -->
{#if lineupItems.length === 0 && pngUrl && !state.results.sourceBelowFloor}
<div class="png-wrap">
<img src={pngUrl} alt="Inventory PNG" />
</div>
{/if}
</div>
<div class="row">
<button type="button" class="ghost" on:click={back}>Back</button>
</div>
{/if}
</section>
<style>
.panel { padding: 16px; color: #eee; }
h1 { font-size: 1.3rem; margin: 0 0 12px; }
.fields { display: flex; gap: 16px; margin-top: 16px; }
.field { flex: 1; }
.label { display: block; font-size: 0.9rem; color: #aaa; margin-bottom: 8px; }
.field input {
box-sizing: border-box; width: 100%; min-height: 52px; padding: 12px; font-size: 1rem;
border-radius: 10px; border: 1px solid #444; background: #1d1d1d; color: #eee;
font-variant-numeric: tabular-nums;
}
.row { display: flex; gap: 10px; margin-top: 20px; }
.primary {
flex: 2; padding: 14px; font-size: 1.05rem; border-radius: 10px; border: 0;
background: #00e5ff; color: #002; font-weight: 700;
}
.primary:disabled { background: #333; color: #777; }
.ghost { flex: 1; padding: 14px; border-radius: 10px; border: 1px solid #444; background: transparent; color: #ccc; }
.results { margin-top: 8px; }
.nums { display: flex; flex-direction: column; gap: 8px; }
.nums div { display: flex; justify-content: space-between; border-bottom: 1px solid #2a2a2a; padding-bottom: 6px; }
.nums span:first-child { color: #aaa; }
.warn { color: #ffb4a2; margin-top: 12px; font-size: 0.9rem; }
.lineup {
list-style: none; padding: 0; margin: 16px 0 0;
display: flex; flex-direction: column; gap: 14px;
}
.lineup-item.png {
text-align: center;
background-image: linear-gradient(45deg,#ccc 25%,transparent 25%),linear-gradient(-45deg,#ccc 25%,transparent 25%),linear-gradient(45deg,transparent 75%,#ccc 75%),linear-gradient(-45deg,transparent 75%,#ccc 75%);
background-size: 16px 16px; background-position: 0 0,0 8px,8px -8px,-8px 0;
background-color: #eee; border-radius: 8px; padding: 8px;
}
.lineup-item.png img { max-width: 100%; border-radius: 4px; display: block; margin: 0 auto; }
.role { display: block; margin-top: 8px; font-size: 0.85rem; color: #222; font-weight: 600; }
.lineup-item.error {
border: 1px solid #8a3a2a; background: #2a1512; border-radius: 8px;
padding: 12px 14px; display: flex; flex-direction: column; gap: 4px;
}
.err-title { color: #ffb4a2; font-weight: 700; font-size: 0.95rem; }
.err-msg { color: #e8cfc8; font-size: 0.9rem; }
.png-wrap { margin-top: 16px; text-align: center; background-image: linear-gradient(45deg,#ccc 25%,transparent 25%),linear-gradient(-45deg,#ccc 25%,transparent 25%),linear-gradient(45deg,transparent 75%,#ccc 75%),linear-gradient(-45deg,transparent 75%,#ccc 75%); background-size: 16px 16px; background-position: 0 0,0 8px,8px -8px,-8px 0; background-color: #eee; border-radius: 8px; padding: 8px; }
.png-wrap img { max-width: 100%; border-radius: 4px; }
</style>
+396
View File
@@ -0,0 +1,396 @@
<script lang="ts">
import { onDestroy, onMount, tick } from 'svelte';
import type { BBox } from './capture.types';
import type { TapeTransform } from './tapeGeometry';
import { STAGE_H, STAGE_W, clampTickPair, defaultTicks, lineX } from './tapeGeometry';
import { fitDisplay } from './fitDisplay';
import { decodeOrientedBitmap } from '../decodeOrientedBitmap';
import { cropMarginFraction } from '../png/crop';
import { getCropFill } from '../settings/cropFill';
// Display-only: renders the photo fitted to device width (height capped) plus
// one overlay layer. All math lives on source pixels, not this canvas.
// Geometry must use the same EXIF-oriented bitmap space as the mask worker (#72).
export let blob: Blob;
export let maxH = 420;
export let bbox: BBox | null = null;
export let maskBitset: Uint8Array | null = null;
export let maskW = 0;
export let maskH = 0;
/** #99 tape-line mode: fixed middle line + movable photo. */
export let mode: 'bbox' | 'tape' = 'bbox';
export let tape: TapeTransform | null = null;
export let cropFill: number = getCropFill();
export let onTapeChange: ((t: TapeTransform) => void) | null = null;
/** When false, tape overlay is display-only (Measure step). */
export let interactive = true;
let canvas: HTMLCanvasElement | undefined;
let stage: HTMLDivElement | undefined;
let tapeWrap: HTMLDivElement | undefined;
let imgW = 0, imgH = 0;
let parentW = 0;
let decodeGen = 0;
let decoding = false;
let focusTick: 'A' | 'B' = 'A';
// Local editable copy while dragging; flushed via onTapeChange.
let live: TapeTransform = tape ?? {
tiltDeg: 0, zoom: 1, panX: 0, panY: 0, ...defaultTicks(cropFill),
};
$: if (tape) live = { ...tape };
const emit = (next: TapeTransform) => {
live = next;
onTapeChange?.(next);
};
const applyFit = () => {
if (!canvas || !imgW) return;
if (mode === 'tape') {
// Photo is sized to the logical stage; CSS transform handles pan/zoom/tilt.
canvas.style.width = '100%';
canvas.style.height = '100%';
return;
}
if (parentW <= 0 && stage) parentW = stage.clientWidth;
const fit = fitDisplay(parentW, imgW, imgH, maxH);
if (!fit) return;
canvas.style.width = `${fit.fitW}px`;
canvas.style.height = `${fit.fitH}px`;
};
const paintOverlays = (ctx: CanvasRenderingContext2D) => {
if (!canvas || mode === 'tape') return;
if (maskBitset && maskW > 0 && maskH > 0) {
const off = document.createElement('canvas');
off.width = maskW;
off.height = maskH;
const octx = off.getContext('2d')!;
const d = octx.createImageData(maskW, maskH);
const tile = 8;
for (let p = 0; p < maskW * maskH; p++) {
if (!maskBitset[p]) {
const x = p % maskW;
const y = (p / maskW) | 0;
const light = ((x / tile) | 0) % 2 === ((y / tile) | 0) % 2;
d.data[p * 4] = light ? 220 : 180;
d.data[p * 4 + 1] = light ? 220 : 180;
d.data[p * 4 + 2] = light ? 220 : 180;
d.data[p * 4 + 3] = 220;
}
}
octx.putImageData(d, 0, 0);
ctx.drawImage(off, 0, 0, canvas.width, canvas.height);
}
if (bbox && bbox.corners.length === 4) {
const c = bbox.corners;
ctx.strokeStyle = '#00e5ff';
ctx.lineWidth = Math.max(2, canvas.width / 400);
ctx.beginPath();
ctx.moveTo(c[0][0], c[0][1]);
for (let i = 1; i < 4; i++) ctx.lineTo(c[i][0], c[i][1]);
ctx.closePath();
ctx.stroke();
const a = c[3];
const b = c[0];
ctx.strokeStyle = '#ff3d71';
ctx.lineWidth = Math.max(3, canvas.width / 300);
ctx.beginPath();
ctx.moveTo(a[0], a[1]);
ctx.lineTo(b[0], b[1]);
ctx.stroke();
ctx.fillStyle = '#ff3d71';
ctx.beginPath();
const ang = Math.atan2(b[1] - a[1], b[0] - a[0]);
const r = Math.max(10, canvas.width / 60);
ctx.moveTo(b[0], b[1]);
ctx.lineTo(b[0] - r * Math.cos(ang - 0.4), b[1] - r * Math.sin(ang - 0.4));
ctx.lineTo(b[0] - r * Math.cos(ang + 0.4), b[1] - r * Math.sin(ang + 0.4));
ctx.closePath();
ctx.fill();
}
};
const redraw = async () => {
if (!blob || !canvas) return;
const gen = ++decodeGen;
decoding = true;
let bmp: ImageBitmap | null = null;
try {
bmp = await decodeOrientedBitmap(blob);
if (gen !== decodeGen || !canvas) return;
imgW = bmp.width;
imgH = bmp.height;
canvas.width = imgW;
canvas.height = imgH;
applyFit();
const ctx = canvas.getContext('2d')!;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(bmp, 0, 0);
paintOverlays(ctx);
} finally {
if (bmp) bmp.close();
if (gen === decodeGen) decoding = false;
}
};
$: if (blob || bbox || maskBitset || mode) void tick().then(redraw);
// ---- tape pointer interactions ----
type DragKind = null | 'pan' | 'tickA' | 'tickB' | 'rot';
let drag: { kind: DragKind; x0: number; y0: number; panX0: number; panY0: number; tilt0: number } | null = null;
let pointers = new Map<number, { x: number; y: number }>();
let pinchStartDist = 0;
let pinchStartZoom = 1;
const stagePoint = (clientX: number, clientY: number): { x: number; y: number } => {
const el = tapeWrap;
if (!el) return { x: 0, y: 0 };
const r = el.getBoundingClientRect();
return {
x: ((clientX - r.left) / Math.max(r.width, 1)) * STAGE_W,
y: ((clientY - r.top) / Math.max(r.height, 1)) * STAGE_H,
};
};
const hitTick = (x: number, y: number): 'A' | 'B' | null => {
// ≥44×44 CSS px hit → ~34 stage units on a ~352px-wide stage (wireframe HIT_R).
const HIT = 34;
const lx = lineX();
if (Math.hypot(x - lx, y - live.tickA) <= HIT) return 'A';
if (Math.hypot(x - lx, y - live.tickB) <= HIT) return 'B';
return null;
};
const hitRot = (x: number, y: number): boolean => {
const cx = STAGE_W / 2;
const cy = STAGE_H - 26;
return Math.hypot(x - cx, y - cy) <= 22;
};
const onPointerDown = (e: PointerEvent) => {
if (!interactive || mode !== 'tape') return;
(e.currentTarget as HTMLElement).setPointerCapture?.(e.pointerId);
pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
if (pointers.size === 2) {
const pts = [...pointers.values()];
pinchStartDist = Math.hypot(pts[0].x - pts[1].x, pts[0].y - pts[1].y) || 1;
pinchStartZoom = live.zoom;
drag = null;
return;
}
const p = stagePoint(e.clientX, e.clientY);
const tick = hitTick(p.x, p.y);
if (tick) {
focusTick = tick;
drag = { kind: tick === 'A' ? 'tickA' : 'tickB', x0: p.x, y0: p.y, panX0: live.panX, panY0: live.panY, tilt0: live.tiltDeg };
return;
}
if (hitRot(p.x, p.y)) {
drag = { kind: 'rot', x0: p.x, y0: p.y, panX0: live.panX, panY0: live.panY, tilt0: live.tiltDeg };
return;
}
drag = { kind: 'pan', x0: p.x, y0: p.y, panX0: live.panX, panY0: live.panY, tilt0: live.tiltDeg };
};
const onPointerMove = (e: PointerEvent) => {
if (!interactive || mode !== 'tape') return;
if (pointers.has(e.pointerId)) pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
if (pointers.size === 2) {
const pts = [...pointers.values()];
const dist = Math.hypot(pts[0].x - pts[1].x, pts[0].y - pts[1].y) || 1;
const z = Math.min(3, Math.max(0.5, pinchStartZoom * (dist / pinchStartDist)));
emit({ ...live, zoom: z });
return;
}
if (!drag) return;
const p = stagePoint(e.clientX, e.clientY);
if (drag.kind === 'pan') {
emit({ ...live, panX: drag.panX0 + (p.x - drag.x0), panY: drag.panY0 + (p.y - drag.y0) });
} else if (drag.kind === 'tickA' || drag.kind === 'tickB') {
const pair = drag.kind === 'tickA'
? clampTickPair(p.y, live.tickB)
: clampTickPair(live.tickA, p.y);
emit({ ...live, ...pair });
} else if (drag.kind === 'rot') {
const cx = STAGE_W / 2;
const cy = STAGE_H / 2;
const a0 = Math.atan2(drag.y0 - cy, drag.x0 - cx);
const a1 = Math.atan2(p.y - cy, p.x - cx);
const delta = ((a1 - a0) * 180) / Math.PI;
emit({ ...live, tiltDeg: drag.tilt0 + delta });
}
};
const onPointerUp = (e: PointerEvent) => {
pointers.delete(e.pointerId);
if (pointers.size < 2) drag = null;
};
const onWheel = (e: WheelEvent) => {
if (!interactive || mode !== 'tape') return;
e.preventDefault();
const factor = e.deltaY > 0 ? 0.92 : 1.08;
emit({ ...live, zoom: Math.min(3, Math.max(0.5, live.zoom * factor)) });
};
const onKeyDown = (e: KeyboardEvent) => {
if (!interactive || mode !== 'tape') return;
const fine = e.shiftKey;
if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
e.preventDefault();
const step = fine ? 0.1 : 1;
const dir = e.key === 'ArrowLeft' ? -1 : 1;
emit({ ...live, tiltDeg: live.tiltDeg + dir * step });
} else if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
e.preventDefault();
const step = fine ? 0.1 : 1;
const dir = e.key === 'ArrowUp' ? -1 : 1;
if (focusTick === 'A') {
emit({ ...live, ...clampTickPair(live.tickA + dir * step, live.tickB) });
} else {
emit({ ...live, ...clampTickPair(live.tickA, live.tickB + dir * step) });
}
} else if (e.key === '+' || e.key === '=') {
e.preventDefault();
emit({ ...live, zoom: Math.min(3, live.zoom * 1.1) });
} else if (e.key === '-' || e.key === '_') {
e.preventDefault();
emit({ ...live, zoom: Math.max(0.5, live.zoom / 1.1) });
}
};
const fitPhoto = () => emit({ ...live, zoom: 1, panX: 0, panY: 0 });
let vbScale = 1;
const updateVbScale = () => {
if (!tapeWrap) return;
vbScale = tapeWrap.clientWidth / STAGE_W || 1;
};
$: inset = STAGE_H * cropMarginFraction(cropFill);
$: photoTransform = `translate(${live.panX}px, ${live.panY}px) translate(${STAGE_W / 2}px, ${STAGE_H / 2}px) rotate(${live.tiltDeg}deg) scale(${live.zoom}) translate(${-STAGE_W / 2}px, ${-STAGE_H / 2}px)`;
// Contain-fit the canvas bitmap into the logical stage.
$: photoLayerStyle = imgW > 0 && imgH > 0
? (() => {
const scale0 = Math.min(STAGE_W / imgW, STAGE_H / imgH);
const w = imgW * scale0;
const h = imgH * scale0;
const left = (STAGE_W - w) / 2;
const top = (STAGE_H - h) / 2;
return `position:absolute;left:${left}px;top:${top}px;width:${w}px;height:${h}px;transform-origin:center center;`;
})()
: 'position:absolute;inset:0;';
onMount(() => {
if (stage) parentW = stage.clientWidth;
updateVbScale();
const ro = new ResizeObserver((entries) => {
const e = entries[0];
if (!e) return;
const w = e.contentRect.width;
if (mode === 'tape') {
updateVbScale();
}
if (w === parentW) return;
parentW = w;
applyFit();
});
if (stage) ro.observe(stage);
if (tapeWrap) ro.observe(tapeWrap);
return () => ro.disconnect();
});
onDestroy(() => {
decodeGen += 1;
});
</script>
{#if mode === 'tape'}
<div
class="tape-stage"
bind:this={stage}
tabindex="0"
role="application"
aria-label="Fixed tape line over a movable slab photo"
on:keydown={onKeyDown}
>
<div
class="tw"
class:dragging={!!drag}
bind:this={tapeWrap}
role="group"
aria-label="Tape alignment stage"
on:pointerdown={onPointerDown}
on:pointermove={onPointerMove}
on:pointerup={onPointerUp}
on:pointercancel={onPointerUp}
on:wheel={onWheel}
on:dblclick={() => interactive && fitPhoto()}
>
<div class="vb" style="width:{STAGE_W}px;height:{STAGE_H}px;transform:scale({vbScale});">
<div class="photo-layer" style="{photoLayerStyle} transform: {photoTransform};">
<canvas bind:this={canvas}></canvas>
</div>
<!-- crop end bands (proposal visual; aspect stays 3:4) -->
<div class="band top" style="height:{inset}px"></div>
<div class="band bot" style="height:{inset}px"></div>
<svg class="fixture" viewBox="0 0 {STAGE_W} {STAGE_H}" width={STAGE_W} height={STAGE_H} aria-hidden="true">
<line x1={lineX()} y1={live.tickA} x2={lineX()} y2={live.tickB} stroke="rgba(255,255,255,.28)" stroke-width="6" stroke-linecap="round" />
<line x1={lineX()} y1={live.tickA} x2={lineX()} y2={live.tickB} stroke="#000" stroke-width="2.4" stroke-linecap="round" />
{#each [['A', live.tickA], ['B', live.tickB]] as [id, y]}
<line x1={lineX() - 13} y1={y} x2={lineX() + 13} y2={y} stroke="rgba(255,255,255,.92)" stroke-width="6" stroke-linecap="round" />
<line x1={lineX() - 13} y1={y} x2={lineX() + 13} y2={y} stroke="#000" stroke-width="2.4" stroke-linecap="round" />
<circle cx={lineX()} cy={y} r="5" fill={focusTick === id ? '#ff3d71' : '#000'} stroke="#fff" stroke-width="1.5" />
<circle class="tick-hit" cx={lineX()} cy={y} r="34" fill="transparent" />
{/each}
{#if interactive}
<g class="rot-handle">
<circle cx={STAGE_W / 2} cy={STAGE_H - 26} r="17" fill="rgba(0,0,0,.6)" stroke="#00e5ff" stroke-width="1.5" />
<text x={STAGE_W / 2} y={STAGE_H - 21} fill="#00e5ff" font-size="15" text-anchor="middle">↻</text>
</g>
{/if}
</svg>
{#if decoding && imgW === 0}
<div class="loading">Decoding photo…</div>
{/if}
</div>
</div>
</div>
{:else}
<div class="stage" bind:this={stage}>
{#if imgW > 0 || decoding || blob}
<canvas bind:this={canvas}></canvas>
{:else}
<div class="loading">Decoding photo…</div>
{/if}
</div>
{/if}
<style>
.stage { width: 100%; display: flex; justify-content: center; }
canvas { display: block; border-radius: 8px; background: #222; width: 100%; height: 100%; }
.loading { padding: 40px; color: #999; position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; }
.tape-stage { width: 100%; outline: none; }
.tw {
position: relative; width: 100%; aspect-ratio: 3 / 4;
background: #111; border: 1px solid #333; border-radius: 8px; overflow: hidden;
touch-action: none; user-select: none; cursor: grab;
}
.tw.dragging { cursor: grabbing; }
.vb {
position: absolute; top: 0; left: 0;
transform-origin: top left;
pointer-events: none;
}
.vb > :global(*) { pointer-events: auto; }
.photo-layer { will-change: transform; pointer-events: none; }
.photo-layer canvas { border-radius: 0; background: transparent; pointer-events: none; }
.band { position: absolute; left: 0; right: 0; background: #000; opacity: 0.3; pointer-events: none; z-index: 1; }
.band.top { top: 0; }
.band.bot { bottom: 0; }
.fixture { position: absolute; inset: 0; z-index: 2; pointer-events: none; }
.tick-hit { pointer-events: auto; cursor: ns-resize; }
.rot-handle { pointer-events: auto; cursor: ew-resize; }
</style>
@@ -0,0 +1,66 @@
<script lang="ts">
import { onDestroy } from 'svelte';
import type { Source } from './capture.types';
export let photos: Source[];
export let onPick: (index: number) => void;
export let onBack: () => void;
let urls: string[] = [];
$: {
for (const u of urls) URL.revokeObjectURL(u);
urls = photos.map((p) => URL.createObjectURL(p.blob));
}
onDestroy(() => {
for (const u of urls) URL.revokeObjectURL(u);
});
</script>
<section class="panel">
<h1>Pick calibration photo</h1>
<p class="hint">
Choose which photo is the top-down reference for length and thickness.
Mask, length axis, and Measure run on that photo only.
</p>
<ul class="thumbs" aria-label="Calibration candidates">
{#each photos as _p, i (urls[i])}
<li class="thumb">
<img src={urls[i]} alt={`Photo ${i + 1}`} />
<button
type="button"
class="pick"
aria-label={`Use photo ${i + 1} as calibration`}
on:click={() => onPick(i)}
>Use as calibration</button>
</li>
{/each}
</ul>
<button class="back" type="button" on:click={onBack}>Back</button>
</section>
<style>
.panel { padding: 24px 16px; color: #eee; }
h1 { font-size: 1.4rem; }
.hint { color: #bbb; line-height: 1.5; font-size: 0.95rem; }
.thumbs {
list-style: none; padding: 0; margin: 16px 0;
display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px;
}
.thumb {
background: #1e1e1e; border-radius: 8px; overflow: hidden;
display: flex; flex-direction: column;
}
.thumb img { width: 100%; aspect-ratio: 3 / 4; object-fit: cover; display: block; background: #000; }
.pick {
border: 0; background: #00e5ff; color: #002; padding: 10px 8px;
font-size: 0.9rem; font-weight: 700; cursor: pointer;
}
.back {
margin-top: 8px; width: 100%; padding: 12px; font-size: 1rem; border-radius: 10px;
border: 1px solid #444; background: transparent; color: #ccc; cursor: pointer;
}
</style>
+28
View File
@@ -0,0 +1,28 @@
<script lang="ts">
export let onContinue: () => void;
export let onFresh: () => void;
</script>
<section class="panel">
<h1>Resume?</h1>
<p>You have an in-progress slab on this device.</p>
<div class="row">
<button class="primary" type="button" on:click={onContinue}>Continue</button>
<button class="ghost" type="button" on:click={onFresh}>Start fresh</button>
</div>
</section>
<style>
.panel { padding: 24px 16px; text-align: center; color: #eee; }
h1 { font-size: 1.4rem; }
p { color: #aaa; }
.row { display: flex; gap: 12px; justify-content: center; margin-top: 24px; }
.primary {
flex: 1; max-width: 200px; padding: 14px; font-size: 1.05rem; border-radius: 10px; border: 0;
background: #00e5ff; color: #002; font-weight: 700;
}
.ghost {
flex: 1; max-width: 200px; padding: 14px; border-radius: 10px; border: 1px solid #444;
background: transparent; color: #ccc;
}
</style>
+182
View File
@@ -0,0 +1,182 @@
// Domain model for the capture flow (M1 + M2 batch).
// The Capture tagged union is the state machine: each branch carries exactly the
// data its state can hold, so an illegal state is a type error.
// Integer eighths of an inch; divide by 8 only at display.
export type Eighths = number;
export const inToEighths = (inches: number): Eighths => Math.round(inches * 8);
export const eighthsToIn = (e: Eighths): number => e / 8;
// Source: all geometry, measurement, and PNG work uses source pixels, never the
// CSS size of the preview.
export interface Source {
widthPx: number;
heightPx: number;
blob: Blob;
}
export type Sheet = 'auto' | 'green' | 'black';
export interface MaskParams {
sheet: Sheet;
sensitivity: number; // 0–100, step 1
}
// Axis-aligned box, source px.
export interface ABox {
x: number;
y: number;
w: number;
h: number;
}
// Minimum-area bounding rectangle of the mask, source px.
export interface BBox {
corners: [number, number][]; // 4 corners; corners[3] → corners[0] is the long side
longSidePx: number; // geometric extent only; scale prefers tape tick span (#99)
angleDeg: number; // long-side angle, degrees, [0, 180)
}
// The approved mask is the footprint for axis, measurement, and the inventory PNG.
export interface Mask {
params: MaskParams;
slabPx: number; // 0 ⇒ approve blocked, user retakes
bitset: Uint8Array; // source-grid mask, 1 byte/px
bbox: BBox;
slabBounds: ABox; // axis-aligned extent of slab px, for the crop_fill PNG crop (default 90%)
}
// Tape-line calibration (#99): photo tilt/pan/zoom under a fixed middle line;
// ticks declare L. bbox is derived for overlay/width sampling direction.
export interface TapeLine {
tiltDeg: number;
zoom: number;
panX: number;
panY: number;
/** Tick Y in logical stage units (STAGE_H = 640). */
tickA: number;
tickB: number;
/** Image-space distance between ticks (px); drives scale = spanPx / L. */
spanPx: number;
}
export interface Axis {
bbox: BBox;
tape: TapeLine;
}
export interface Widths {
min: Eighths;
max: Eighths;
avg: Eighths;
every6In: Eighths[];
}
export interface Measures {
lengthEighths: Eighths;
thicknessEighths: Eighths;
scale: number; // px per inch
widths: Widths;
sqft: number; // 2 dp
bdft: number; // 2 dp
inventoryPng: Blob;
/** Dimension-overlay copy of the calibration inventory PNG (#63). */
overlayPng?: Blob;
sourceBelowFloor: boolean;
}
// Result of applying the calibration mask to one batch sibling. The
// calibration slot is null because its inventory PNG lives in Measures.
export type BatchPhotoResult =
| null
| { status: 'ok'; inventoryPng: Blob }
| { status: 'error'; message: string };
// The machine. 'resume' exists only while a draft exists and has not been chosen.
// M2: 'batch' + 'pickCalibration' replace M1 'addPhoto' — accumulate, then user picks calibration.
export type Capture =
| { k: 'resume'; draft: Draft }
| { k: 'batch'; photos: Source[] }
| { k: 'pickCalibration'; photos: Source[] }
| {
k: 'mask';
source: Source;
control: MaskParams; // immediate control value (what the UI shows)
overlay: Mask | null; // last settled mask; null until first pass
failed: boolean;
}
| {
k: 'axis';
source: Source;
approvedMask: Mask; // frozen footprint
axis: Axis; // live axis.bbox being nudged
}
| {
k: 'measure';
source: Source;
approvedMask: Mask;
axis: Axis; // frozen
lengthEighths: Eighths;
thicknessEighths: Eighths;
results: Measures | null;
/** Capture-order sibling results from the post-Calculate batch pass. */
batchApply?: BatchPhotoResult[];
};
// Placeholder geometry for batch-only drafts (no mask yet).
export const EMPTY_BBOX: BBox = {
corners: [
[0, 0],
[0, 0],
[0, 0],
[0, 0],
],
longSidePx: 0,
angleDeg: 0,
};
export const EMPTY_ABOX: ABox = { x: 0, y: 0, w: 0, h: 0 };
export const DEFAULT_MASK_PARAMS: MaskParams = { sheet: 'auto', sensitivity: 50 };
// Device draft (IndexedDB): one in-progress slab. Small by design — the bitset
// and PNG are re-derived from stored data, never stored second-hand.
// M2 extends with `batch` (capture-order photo blobs). Legacy M1 drafts omit it.
export interface Draft {
version: 1;
stage: 'batch' | 'pickCalibration' | 'mask' | 'axis' | 'measure'; // last approved / persisted state
/** Capture-order batch. Absent on legacy M1 drafts ⇒ treat as [source]. */
batch?: Source[];
/** Index into batch of the user-picked calibration photo. Set once picked. */
calibrationIndex?: number;
source: Source; // calibration / active photo; for stage==='batch'|'pickCalibration' = photos[0] placeholder
maskParams: MaskParams; // → re-derive mask on resume (ignored while stage==='batch')
mask: Omit<Mask, 'bitset' | 'params'>; // slabPx, bbox, slabBounds
axisBbox?: BBox | null; // present when stage >= axis; absent on legacy drafts
tape?: TapeLine | null; // #99 tape-line transform; absent on legacy drafts
lengthEighths: Eighths;
thicknessEighths: Eighths;
results: Omit<Measures, 'inventoryPng' | 'overlayPng'> | null; // present when results computed
inventoryPng: Blob | null; // stored so Continue can show Measure results
/** Dimension-overlay calibration PNG; kept out of batchApply[cal] (#63). */
overlayPng?: Blob | null;
/** Capture-order batch apply results; blobs are retained for the #64 lineup. */
batchApply?: BatchPhotoResult[];
}
// Worker protocol. Job ids increase; the worker drops anything older than the
// current id, so a stale result can never blank a newer one.
export type WorkerIn =
| { id: number; kind: 'init'; blob: Blob }
| { id: number; kind: 'mask'; sheet: Sheet; sensitivity: number }
| { id: number; kind: 'invalidate' };
export type WorkerOut =
| { id: number; kind: 'decoded'; w: number; h: number }
| {
id: number;
kind: 'ready';
slabPx: number;
bitset: Uint8Array;
bbox: BBox;
slabBounds: ABox;
}
| { id: number; kind: 'failed'; reason: string };
@@ -0,0 +1,149 @@
import { test, afterEach } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import {
decodeOrientedBitmap,
ORIENTED_BITMAP_OPTIONS,
} from '../decodeOrientedBitmap.ts';
const here = dirname(fileURLToPath(import.meta.url));
const fixturePath = join(here, '../../tests/fixtures/set1-48x2/face-a.jpeg');
const RealCreateImageBitmap = globalThis.createImageBitmap;
afterEach(() => {
if (RealCreateImageBitmap === undefined) {
delete (globalThis as unknown as { createImageBitmap?: unknown }).createImageBitmap;
} else {
globalThis.createImageBitmap = RealCreateImageBitmap;
}
});
/** Minimal EXIF Orientation + largest SOF size for the orient-8 fixture. */
function readJpegOrientAndSof(buf: Buffer): { orientation: number; sofW: number; sofH: number } {
let orientation = 1;
const exif = buf.indexOf(Buffer.from('Exif\0\0'));
if (exif >= 0) {
const tiff = exif + 6;
const le = buf[tiff] === 0x49;
const u16 = (o: number) => (le ? buf.readUInt16LE(o) : buf.readUInt16BE(o));
const u32 = (o: number) => (le ? buf.readUInt32LE(o) : buf.readUInt32BE(o));
const ifd = tiff + u32(tiff + 4);
const n = u16(ifd);
for (let k = 0; k < n; k++) {
const e = ifd + 2 + k * 12;
const tag = u16(e);
const typ = u16(e + 2);
if (tag === 0x0112) {
orientation = typ === 3 ? u16(e + 8) : u32(e + 8);
}
}
}
let sofW = 0;
let sofH = 0;
let i = 0;
while (i < buf.length - 9) {
if (buf[i] === 0xff && buf[i + 1] >= 0xc0 && buf[i + 1] <= 0xc3) {
const h = buf.readUInt16BE(i + 5);
const w = buf.readUInt16BE(i + 7);
if (w * h > sofW * sofH) {
sofW = w;
sofH = h;
}
const segLen = buf.readUInt16BE(i + 2);
i += 2 + segLen;
} else {
i += 1;
}
}
return { orientation, sofW, sofH };
}
/** Orientation 5–8 swap width/height for display. */
function orientedSize(sofW: number, sofH: number, orientation: number): { w: number; h: number } {
if (orientation >= 5 && orientation <= 8) return { w: sofH, h: sofW };
return { w: sofW, h: sofH };
}
test('fixture face-a.jpeg is EXIF Orientation 8 (stored landscape → oriented portrait) (#72)', () => {
const buf = readFileSync(fixturePath);
const { orientation, sofW, sofH } = readJpegOrientAndSof(buf);
assert.equal(orientation, 8, 'matched-set JPEGs carry Orientation 8');
assert.equal(sofW, 4000);
assert.equal(sofH, 2256);
const { w, h } = orientedSize(sofW, sofH, orientation);
assert.equal(w, 2256);
assert.equal(h, 4000);
});
test('decodeOrientedBitmap always requests imageOrientation from-image (#72)', async () => {
let seen: ImageBitmapOptions | undefined;
(globalThis as unknown as { createImageBitmap: typeof createImageBitmap }).createImageBitmap =
async (_src: ImageBitmapSource, opts?: ImageBitmapOptions) => {
seen = opts;
return { width: 2256, height: 4000, close() {} } as ImageBitmap;
};
const blob = new Blob([readFileSync(fixturePath)], { type: 'image/jpeg' });
const bmp = await decodeOrientedBitmap(blob);
assert.deepEqual(seen, ORIENTED_BITMAP_OPTIONS);
assert.equal(ORIENTED_BITMAP_OPTIONS.imageOrientation, 'from-image');
assert.equal(bmp.width, 2256);
assert.equal(bmp.height, 4000);
});
test('decodeOrientedBitmap falls back to bare createImageBitmap if option rejected (#72)', async () => {
let calls = 0;
(globalThis as unknown as { createImageBitmap: typeof createImageBitmap }).createImageBitmap =
async (_src: ImageBitmapSource, opts?: ImageBitmapOptions) => {
calls += 1;
if (opts && 'imageOrientation' in opts) {
throw new TypeError('imageOrientation unsupported');
}
return { width: 90, height: 160, close() {} } as ImageBitmap;
};
const bmp = await decodeOrientedBitmap(new Blob([new Uint8Array([1, 2, 3])], { type: 'image/jpeg' }));
assert.equal(calls, 2, 'retry bare after option rejection');
assert.equal(bmp.width, 90);
assert.equal(bmp.height, 160);
});
test('decodeOrientedBitmap: Chromium orients fixture to 2256×4000 (#72)', async (t) => {
let chromium: typeof import('@playwright/test').chromium;
try {
({ chromium } = await import('@playwright/test'));
} catch {
t.skip('playwright not available');
return;
}
const browser = await chromium.launch();
try {
const page = await browser.newPage();
const buf = readFileSync(fixturePath);
const b64 = buf.toString('base64');
// Inline the same contract as decodeOrientedBitmap (worker-safe, no bundler).
const dims = await page.evaluate(async (b64jpeg: string) => {
const bin = atob(b64jpeg);
const arr = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; i++) arr[i] = bin.charCodeAt(i);
const blob = new Blob([arr], { type: 'image/jpeg' });
const opts = { imageOrientation: 'from-image' as const };
let bmp: ImageBitmap;
try {
bmp = await createImageBitmap(blob, opts);
} catch {
bmp = await createImageBitmap(blob);
}
const out = { w: bmp.width, h: bmp.height };
bmp.close();
return out;
}, b64);
assert.equal(dims.w, 2256, `oriented width (got ${dims.w})`);
assert.equal(dims.h, 4000, `oriented height (got ${dims.h})`);
} finally {
await browser.close();
}
});
+26
View File
@@ -0,0 +1,26 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { fitDisplay } from './fitDisplay.ts';
test('parentW=0 → null (no size until laid out)', () => {
assert.equal(fitDisplay(0, 1000, 750, 420), null);
assert.equal(fitDisplay(-5, 1000, 750, 420), null);
});
test('280 wide, 4:3 landscape → contain, under the height cap', () => {
const fit = fitDisplay(280, 1600, 1200, 420);
assert.ok(fit);
assert.equal(fit.fitW, 280);
assert.ok(Math.abs(fit.fitH - 210) < 1e-9);
assert.ok(fit.fitH <= 420);
});
test('portrait under maxH never stretches: height capped, width shrinks', () => {
// 1000 wide source, 3000 tall. Full-width fit would be 900 tall > 420.
const fit = fitDisplay(400, 1000, 3000, 420);
assert.ok(fit);
assert.equal(fit.fitH, 420);
assert.ok(Math.abs(fit.fitW - 140) < 1e-9);
// aspect preserved: fitW/fitH === imgW/imgH
assert.ok(Math.abs(fit.fitW / fit.fitH - 1000 / 3000) < 1e-9);
});
+17
View File
@@ -0,0 +1,17 @@
// Contain-fit of an image into a parent width with a height cap. Pure and
// display-only; all measurement math stays on source pixels.
// Null when the parent is not laid out yet (clientWidth 0) — the caller must
// not finalize CSS size until it is, to avoid a wrong-aspect flash.
export function fitDisplay(
parentW: number, imgW: number, imgH: number, maxH: number,
): { fitW: number; fitH: number } | null {
if (parentW <= 0 || imgW <= 0 || imgH <= 0) return null;
const fitW = parentW;
let fitH = (imgH / imgW) * fitW;
if (fitH > maxH) {
// Cap height and shrink width to preserve aspect; never stretch.
fitH = maxH;
return { fitW: (fitH * imgW) / imgH, fitH };
}
return { fitW, fitH };
}
@@ -0,0 +1,42 @@
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { test } from 'node:test';
import { inToEighths } from './capture.types';
const panelSrc = readFileSync(
join(dirname(fileURLToPath(import.meta.url)), 'MeasurePanel.svelte'),
'utf8',
);
test('MeasurePanel exposes typed number inputs for Length L and Thickness', () => {
const inputs = panelSrc.match(/<input\b[^>]*>/g) ?? [];
assert.ok(inputs.length >= 2, 'expected at least two <input> fields');
assert.ok(
inputs.some((el) => /type="number"/.test(el) && /aria-label="Length L"/.test(el)),
'Length L must be a type=number input',
);
assert.ok(
inputs.some((el) => /type="number"/.test(el) && /aria-label="Thickness"/.test(el)),
'Thickness must be a type=number input',
);
assert.equal(
panelSrc.includes('class="stepper"'),
false,
'stepper-only UI is the UAT fail; primary control must be typed input',
);
assert.equal(
/<span class="v">\{fmtEighths\(state\.lengthEighths\)\}<\/span>/.test(panelSrc),
false,
'Length must not be a read-only fmtEighths span',
);
});
test('typed inches snap to nearest 1/8 via inToEighths', () => {
assert.equal(inToEighths(12), 96);
assert.equal(inToEighths(12.1), 97);
assert.equal(inToEighths(0.06), 0);
assert.equal(inToEighths(0.07), 1);
assert.equal(inToEighths(1.999), 16);
});
@@ -0,0 +1,207 @@
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { test } from 'node:test';
import type { BatchPhotoResult } from './capture.types';
import {
buildPhotosReadyLineup,
type PhotosReadyLineupItem,
} from './photosReadyLineup';
const here = dirname(fileURLToPath(import.meta.url));
function png(label: string): Blob {
return new Blob([label], { type: 'image/png' });
}
function jpeg(label: string): Blob {
return new Blob([label], { type: 'image/jpeg' });
}
function roles(items: PhotosReadyLineupItem[]): string[] {
return items.map((it) =>
it.kind === 'png' ? `png:${it.role}` : `error:${it.batchIndex}`,
);
}
// --- pure helper: lineup order + error slots (#64) ---
test('test(photos): lineup is clean cal → overlay → other batch PNGs in capture order (#64)', () => {
const clean = png('clean-cal');
const overlay = png('overlay-cal');
const sib0 = png('sib-0');
const sib2 = png('sib-2');
const batchApply: BatchPhotoResult[] = [
{ status: 'ok', inventoryPng: sib0 },
null, // calibration at index 1
{ status: 'ok', inventoryPng: sib2 },
];
const lineup = buildPhotosReadyLineup({
inventoryPng: clean,
overlayPng: overlay,
batchApply,
});
assert.deepEqual(roles(lineup), [
'png:calibration',
'png:overlay',
'png:batch',
'png:batch',
]);
assert.equal(lineup[0].kind, 'png');
if (lineup[0].kind === 'png') {
assert.equal(lineup[0].blob, clean);
assert.equal(lineup[0].role, 'calibration');
}
assert.equal(lineup[1].kind, 'png');
if (lineup[1].kind === 'png') {
assert.equal(lineup[1].blob, overlay);
assert.equal(lineup[1].role, 'overlay');
}
assert.equal(lineup[2].kind, 'png');
if (lineup[2].kind === 'png') {
assert.equal(lineup[2].blob, sib0);
assert.equal(lineup[2].batchIndex, 0);
}
assert.equal(lineup[3].kind, 'png');
if (lineup[3].kind === 'png') {
assert.equal(lineup[3].blob, sib2);
assert.equal(lineup[3].batchIndex, 2);
}
});
test('test(photos): single-photo batch lineup is clean + overlay only (#64)', () => {
const clean = png('only-clean');
const overlay = png('only-overlay');
const lineup = buildPhotosReadyLineup({
inventoryPng: clean,
overlayPng: overlay,
batchApply: [null],
});
assert.deepEqual(roles(lineup), ['png:calibration', 'png:overlay']);
assert.ok(lineup.every((it) => it.kind === 'png'));
});
test('test(photos): batchApply errors become clear error slots (empty mask / floor) (#64)', () => {
const clean = png('clean');
const overlay = png('overlay');
const ok = png('ok-sib');
const batchApply: BatchPhotoResult[] = [
null,
{ status: 'error', message: 'empty mask: no slab pixels detected' },
{ status: 'ok', inventoryPng: ok },
{ status: 'error', message: 'PNG size floor: shorter side is below 800 px' },
];
const lineup = buildPhotosReadyLineup({
inventoryPng: clean,
overlayPng: overlay,
batchApply,
});
assert.deepEqual(roles(lineup), [
'png:calibration',
'png:overlay',
'error:1',
'png:batch',
'error:3',
]);
const err1 = lineup[2];
assert.equal(err1.kind, 'error');
if (err1.kind === 'error') {
assert.match(err1.message, /empty mask/i);
assert.equal(err1.batchIndex, 1);
}
const err3 = lineup[4];
assert.equal(err3.kind, 'error');
if (err3.kind === 'error') {
assert.match(err3.message, /floor|800/i);
}
// Successful sibling still appears after cal+overlay, in capture order among siblings
const okItem = lineup[3];
assert.equal(okItem.kind, 'png');
if (okItem.kind === 'png') {
assert.equal(okItem.blob, ok);
assert.equal(okItem.batchIndex, 2);
}
});
test('test(photos): lineup never uses source JPEG thumbs as inventory PNGs (#64)', () => {
const clean = png('clean');
const overlay = png('overlay');
const thumb = jpeg('source-thumb');
// Dishonest proxy would pass source thumbs; helper must only emit image/png blobs for png slots
const lineup = buildPhotosReadyLineup({
inventoryPng: clean,
overlayPng: overlay,
batchApply: [
null,
{ status: 'ok', inventoryPng: png('sib') },
],
});
for (const it of lineup) {
if (it.kind === 'png') {
assert.equal(it.blob.type, 'image/png', 'lineup PNG slots must be inventory PNGs');
assert.notEqual(it.blob, thumb);
}
}
});
test('test(photos): skip clean cal when sourceBelowFloor; keep overlay + siblings (#64)', () => {
const lineup = buildPhotosReadyLineup({
inventoryPng: png('below'),
overlayPng: png('overlay'),
batchApply: [null, { status: 'ok', inventoryPng: png('sib') }],
sourceBelowFloor: true,
});
assert.deepEqual(roles(lineup), ['png:overlay', 'png:batch']);
});
test('test(photos): empty/missing results yield empty lineup (#64)', () => {
assert.deepEqual(buildPhotosReadyLineup({}), []);
assert.deepEqual(
buildPhotosReadyLineup({ inventoryPng: null, overlayPng: null, batchApply: undefined }),
[],
);
});
// --- light UI / session wiring (#64) ---
test('test(photos): MeasurePanel shows Photos ready lineup + M1 numbers + batch errors (#64)', () => {
const panel = readFileSync(join(here, 'MeasurePanel.svelte'), 'utf8');
assert.match(panel, /Photos ready|photos.?ready/i, 'end screen title after Calculate');
assert.match(panel, /buildPhotosReadyLineup/, 'must use pure lineup helper');
assert.match(panel, /Widths/, 'M1 widths still visible');
assert.match(panel, /Sqft/, 'M1 sqft still visible');
assert.match(panel, /Bdft/, 'M1 bdft still visible');
assert.match(panel, /Scale/, 'M1 scale still visible');
// Dishonest proxies
assert.ok(
!/lineup[\s\S]{0,200}state\.source\.blob/.test(panel),
'must not lineup source thumbs',
);
assert.match(
panel,
/error|batchApply|message/i,
'per-photo batchApply errors must surface in the panel',
);
assert.match(panel, /Back/, 'Back must still work from Photos ready');
});
test('test(photos): App still routes measure results through Measure (or PhotosReady) with Back (#64)', () => {
const app = readFileSync(join(here, '..', 'App.svelte'), 'utf8');
// Stay on Measure with results, or dedicated panel — either is fine
assert.ok(
/MeasurePanel/.test(app) || /PhotosReady/.test(app),
'App must render Photos-ready end screen',
);
// Must not invent a Mask loop for the lineup
assert.ok(
!/\{\s*#each\s+[^}]*photos[^}]*\}[\s\S]*MaskPanel/i.test(app),
'App must not #each batch into Mask for Photos ready',
);
});
+75
View File
@@ -0,0 +1,75 @@
import type { BatchPhotoResult } from './capture.types';
/** One slot in the Photos-ready end screen (#64 / docs/PHOTOS.md). */
export type PhotosReadyLineupItem =
| {
kind: 'png';
role: 'calibration' | 'overlay' | 'batch';
blob: Blob;
/** Capture-order index for non-calibration batch inventory PNGs. */
batchIndex?: number;
}
| {
kind: 'error';
batchIndex: number;
message: string;
};
export interface PhotosReadyLineupInput {
inventoryPng?: Blob | null;
overlayPng?: Blob | null;
batchApply?: BatchPhotoResult[] | null;
/** When true, omit the clean calibration PNG (M1 floor warn path). */
sourceBelowFloor?: boolean;
}
/**
* Build the Photos-ready lineup:
* 1. Clean calibration inventory PNG
* 2. Dimension-overlay copy
* 3. Other batch inventory PNGs in capture order (batchApply, skip cal null)
* with error slots for empty mask / floor failures.
*/
export function buildPhotosReadyLineup(
input: PhotosReadyLineupInput,
): PhotosReadyLineupItem[] {
const items: PhotosReadyLineupItem[] = [];
const { inventoryPng, overlayPng, batchApply, sourceBelowFloor } = input;
if (inventoryPng && !sourceBelowFloor) {
items.push({ kind: 'png', role: 'calibration', blob: inventoryPng });
}
if (overlayPng) {
items.push({ kind: 'png', role: 'overlay', blob: overlayPng });
}
if (batchApply) {
for (let i = 0; i < batchApply.length; i++) {
const slot = batchApply[i];
if (slot === null) continue; // calibration lives in results.inventoryPng
if (slot.status === 'ok') {
items.push({
kind: 'png',
role: 'batch',
blob: slot.inventoryPng,
batchIndex: i,
});
} else {
items.push({
kind: 'error',
batchIndex: i,
message: slot.message,
});
}
}
}
return items;
}
export function photosReadyPngAlt(item: Extract<PhotosReadyLineupItem, { kind: 'png' }>): string {
if (item.role === 'calibration') return 'Calibration inventory PNG';
if (item.role === 'overlay') return 'Calibration dimension-overlay PNG';
const n = (item.batchIndex ?? 0) + 1;
return `Batch inventory PNG ${n}`;
}
+155
View File
@@ -0,0 +1,155 @@
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { test, beforeEach } from 'node:test';
import { get } from 'svelte/store';
import type { Draft, Source } from './capture.types';
import { createCaptureSession, type DraftStore } from './session';
const here = dirname(fileURLToPath(import.meta.url));
function tinyJpeg(label: string): File {
// Minimal valid-ish JPEG bytes are unnecessary; Blob type is what session stores.
const bytes = new Uint8Array([0xff, 0xd8, 0xff, 0xe0, label.charCodeAt(0) & 0xff]);
return new File([bytes], `${label}.jpg`, { type: 'image/jpeg' });
}
function memStore(): DraftStore & { peek: () => Draft | null; reset: () => void } {
let mem: Draft | null = null;
return {
loadDraft: async () => mem,
saveDraft: async (d) => {
mem = d;
},
clearDraft: async () => {
mem = null;
},
peek: () => mem,
reset: () => {
mem = null;
},
};
}
let store: ReturnType<typeof memStore>;
beforeEach(() => {
store = memStore();
});
test('session starts on batch with zero photos (replaces single addPhoto)', () => {
const s = createCaptureSession({ draft: store });
const cap = get(s.capture);
assert.equal(cap.k, 'batch');
if (cap.k === 'batch') assert.equal(cap.photos.length, 0);
s.dispose();
});
test('addToBatch accumulates photos and stays on batch', async () => {
const s = createCaptureSession({ draft: store });
await s.addToBatch(tinyJpeg('a'));
await s.addToBatch(tinyJpeg('b'));
const cap = get(s.capture);
assert.equal(cap.k, 'batch');
if (cap.k === 'batch') {
assert.equal(cap.photos.length, 2);
assert.ok(cap.photos[0].blob instanceof Blob);
assert.ok(cap.photos[1].blob instanceof Blob);
assert.notEqual(cap.photos[0].blob, cap.photos[1].blob);
}
s.dispose();
});
test('continueBatch is a no-op when batch is empty', () => {
const s = createCaptureSession({ draft: store });
s.continueBatch();
assert.equal(get(s.capture).k, 'batch');
s.dispose();
});
test('continueBatch with ≥1 photo advances to pickCalibration (not Mask)', async () => {
// #60: Continue must not silently Mask the first photo — user picks calibration next.
const s = createCaptureSession({ draft: store });
await s.addToBatch(tinyJpeg('a'));
await s.addToBatch(tinyJpeg('b'));
s.continueBatch();
const cap = get(s.capture);
assert.equal(cap.k, 'pickCalibration');
if (cap.k === 'pickCalibration') {
assert.equal(cap.photos.length, 2);
}
s.dispose();
});
test('adding photos persists batch blobs in device draft', async () => {
const s = createCaptureSession({ draft: store });
await s.addToBatch(tinyJpeg('a'));
await s.addToBatch(tinyJpeg('b'));
// allow async persist
await new Promise((r) => setTimeout(r, 20));
const d = store.peek();
assert.ok(d, 'draft must exist after adding photos');
assert.equal(d!.stage, 'batch');
const batch = d!.batch ?? [];
assert.equal(batch.length, 2, 'draft must store both batch blobs (not a UI-only list)');
assert.ok(batch[0].blob instanceof Blob);
assert.ok(batch[1].blob instanceof Blob);
s.dispose();
});
test('refresh: resumeProbe + Continue restores the batch photos', async () => {
const s1 = createCaptureSession({ draft: store });
await s1.addToBatch(tinyJpeg('a'));
await s1.addToBatch(tinyJpeg('b'));
await new Promise((r) => setTimeout(r, 20));
s1.dispose();
const s2 = createCaptureSession({ draft: store });
await s2.resumeProbe();
assert.equal(get(s2.capture).k, 'resume');
s2.continueDraft();
await new Promise((r) => setTimeout(r, 20));
const cap = get(s2.capture);
assert.equal(cap.k, 'batch');
if (cap.k === 'batch') {
assert.equal(cap.photos.length, 2);
assert.ok(cap.photos.every((p: Source) => p.blob instanceof Blob));
}
s2.dispose();
});
test('Batch UI: AddPhoto/Batch panel supports multi-add and Continue when ≥1', () => {
// Prefer BatchPhotos.svelte; fall back to AddPhoto.svelte if renamed later.
const candidates = ['BatchPhotos.svelte', 'AddPhoto.svelte'].map((n) => join(here, n));
let src = '';
let found = '';
for (const p of candidates) {
try {
src = readFileSync(p, 'utf8');
found = p;
break;
} catch {
/* try next */
}
}
assert.ok(src, 'expected BatchPhotos.svelte or AddPhoto.svelte');
assert.match(src, /Continue|continue/i, `${found} must expose Continue`);
assert.match(src, /photos|batch/i, `${found} must talk about batch/photos`);
// Must allow adding more than once (not a one-shot that leaves the screen).
const fileInputs = src.match(/type="file"/g) ?? [];
assert.ok(fileInputs.length >= 1, 'must have file picker(s)');
assert.ok(
/addToBatch|onAdd|onPicked/.test(src) || /on:change/.test(src),
'must wire photo add',
);
});
test('legacy addPhoto alias still appends to batch (no silent single-photo-only path)', async () => {
const s = createCaptureSession({ draft: store });
await s.addPhoto(tinyJpeg('only'));
const cap = get(s.capture);
assert.equal(cap.k, 'batch', 'addPhoto must not jump straight to mask anymore');
if (cap.k === 'batch') assert.equal(cap.photos.length, 1);
s.dispose();
});
@@ -0,0 +1,497 @@
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { test, beforeEach, afterEach } from 'node:test';
import { get } from 'svelte/store';
import type { Draft, Mask, MaskParams, Source, WorkerIn, WorkerOut } from './capture.types';
import { createCaptureSession, type DraftStore } from './session';
const here = dirname(fileURLToPath(import.meta.url));
function tinyJpeg(label: string): File {
const bytes = new Uint8Array([0xff, 0xd8, 0xff, 0xe0, label.charCodeAt(0) & 0xff]);
return new File([bytes], `${label}.jpg`, { type: 'image/jpeg' });
}
function memStore(): DraftStore & { peek: () => Draft | null } {
let mem: Draft | null = null;
return {
loadDraft: async () => mem,
saveDraft: async (d) => {
mem = d;
},
clearDraft: async () => {
mem = null;
},
peek: () => mem,
};
}
let store: ReturnType<typeof memStore>;
beforeEach(() => {
store = memStore();
});
/** Controllable FakeWorker for #62 batch-apply paths. */
type MaskBehavior = {
w: number;
h: number;
/** slabPx; 0 ⇒ empty mask */
slabPx: number;
slabBounds?: { x: number; y: number; w: number; h: number };
};
class FakeMaskWorker {
onmessage: ((ev: MessageEvent<WorkerOut>) => void) | null = null;
static inits: Blob[] = [];
static maskCalls: { sheet: string; sensitivity: number; blob: Blob | null }[] = [];
static lastInitBlob: Blob | null = null;
/** Per-init blob → behavior. Default: full mask at LARGE dims (above PNG floor). */
static behaviorFor: (blob: Blob) => MaskBehavior = () => ({
w: 1000,
h: 900,
slabPx: 1000 * 900,
slabBounds: { x: 0, y: 0, w: 1000, h: 900 },
});
constructor(_url: URL | string, _opts?: WorkerOptions) {}
postMessage(msg: WorkerIn) {
if (msg.kind === 'init') {
FakeMaskWorker.inits.push(msg.blob);
FakeMaskWorker.lastInitBlob = msg.blob;
const id = msg.id;
const b = FakeMaskWorker.behaviorFor(msg.blob);
queueMicrotask(() => {
this.onmessage?.({ data: { id, kind: 'decoded', w: b.w, h: b.h } } as MessageEvent<WorkerOut>);
});
return;
}
if (msg.kind === 'mask') {
const id = msg.id;
const blob = FakeMaskWorker.lastInitBlob;
FakeMaskWorker.maskCalls.push({
sheet: msg.sheet,
sensitivity: msg.sensitivity,
blob,
});
const b = blob ? FakeMaskWorker.behaviorFor(blob) : { w: 40, h: 20, slabPx: 0 };
const bitset = new Uint8Array(Math.max(1, b.w * b.h)).fill(b.slabPx > 0 ? 1 : 0);
const bbox = {
corners: [
[b.w, 0],
[b.w, b.h],
[0, b.h],
[0, 0],
] as [number, number][],
longSidePx: b.w,
angleDeg: 0,
};
const slabBounds = b.slabBounds ?? { x: 0, y: 0, w: b.w, h: b.h };
queueMicrotask(() => {
this.onmessage?.({
data: {
id,
kind: 'ready',
slabPx: b.slabPx,
bitset,
bbox,
slabBounds,
},
} as MessageEvent<WorkerOut>);
});
return;
}
}
terminate() {}
}
const RealWorker = globalThis.Worker;
const RealCreateImageBitmap = globalThis.createImageBitmap;
function installFakes() {
FakeMaskWorker.inits = [];
FakeMaskWorker.maskCalls = [];
FakeMaskWorker.lastInitBlob = null;
FakeMaskWorker.behaviorFor = () => ({
w: 1000,
h: 900,
slabPx: 1000 * 900,
slabBounds: { x: 0, y: 0, w: 1000, h: 900 },
});
(globalThis as unknown as { Worker: typeof FakeMaskWorker }).Worker = FakeMaskWorker;
(globalThis as unknown as { createImageBitmap: typeof createImageBitmap }).createImageBitmap =
async (blob: ImageBitmapSource) => {
const b = blob instanceof Blob ? FakeMaskWorker.behaviorFor(blob) : { w: 1000, h: 900 };
return {
width: b.w,
height: b.h,
close() {},
} as ImageBitmap;
};
}
function restoreGlobals() {
if (RealWorker === undefined) {
delete (globalThis as unknown as { Worker?: unknown }).Worker;
} else {
globalThis.Worker = RealWorker;
}
if (RealCreateImageBitmap === undefined) {
delete (globalThis as unknown as { createImageBitmap?: unknown }).createImageBitmap;
} else {
globalThis.createImageBitmap = RealCreateImageBitmap;
}
}
afterEach(() => {
restoreGlobals();
});
async function settleOverlay(
s: ReturnType<typeof createCaptureSession>,
ms = 800,
): Promise<Mask> {
const start = Date.now();
while (Date.now() - start < ms) {
const cap = get(s.capture);
if (cap.k === 'mask' && cap.overlay && cap.overlay.slabPx > 0) return cap.overlay;
await new Promise((r) => setTimeout(r, 5));
}
throw new Error('mask overlay never settled');
}
async function driveToMeasure(
s: ReturnType<typeof createCaptureSession>,
photos: File[],
calIndex: number,
maskParams: Partial<MaskParams> = { sheet: 'green', sensitivity: 50 },
) {
for (const p of photos) await s.addToBatch(p);
s.continueBatch();
s.pickCalibration(calIndex);
if (maskParams.sheet) s.setSheet(maskParams.sheet);
if (typeof maskParams.sensitivity === 'number') s.setSensitivity(maskParams.sensitivity);
// setSheet already runs a pass; if only sensitivity, force via setSheet after
if (!maskParams.sheet) s.setSheet('auto');
await settleOverlay(s);
s.approveMask();
s.approveAxis();
s.setLength(320); // 40 in
s.setThickness(16); // 2 in
}
type BatchApplySlot =
| null
| { status: 'ok'; inventoryPng: Blob }
| { status: 'error'; message: string };
function batchApplyOf(cap: ReturnType<typeof get<ReturnType<typeof createCaptureSession>['capture']>>): BatchApplySlot[] | undefined {
if (cap.k !== 'measure') return undefined;
return (cap as { batchApply?: BatchApplySlot[] }).batchApply;
}
// --- #62: apply approved mask + crop to non-calibration batch photos after Calculate ---
test('#62: Calculate applies frozen mask params to every non-calibration batch photo', async () => {
installFakes();
const encodeCalls: { widthPx: number; heightPx: number; sheet: string; sensitivity: number }[] = [];
const s = createCaptureSession({
draft: store,
encodeInventory: async (input) => {
encodeCalls.push({
widthPx: input.source.widthPx,
heightPx: input.source.heightPx,
sheet: (input.mask as Mask).params?.sheet ?? '?',
sensitivity: (input.mask as Mask).params?.sensitivity ?? -1,
});
// Distinctive PNG blob so tests cannot confuse with source JPEG thumbs
return {
blob: new Blob([`png-${input.source.widthPx}x${input.source.heightPx}`], { type: 'image/png' }),
sourceBelowFloor: false,
};
},
} as Parameters<typeof createCaptureSession>[0]);
const a = tinyJpeg('a');
const b = tinyJpeg('b'); // calibration
const c = tinyJpeg('c');
await driveToMeasure(s, [a, b, c], 1, { sheet: 'black', sensitivity: 72 });
await s.calculate();
const cap = get(s.capture);
assert.equal(cap.k, 'measure', 'must stay on Measure — do not reopen Mask');
const apply = batchApplyOf(cap);
assert.ok(Array.isArray(apply), 'measure.batchApply must exist after Calculate');
assert.equal(apply!.length, 3, 'one slot per batch photo');
assert.equal(apply![1], null, 'calibration index is null (uses results.inventoryPng)');
assert.ok(apply![0] && apply![0].status === 'ok', 'batch[0] applied ok');
assert.ok(apply![2] && apply![2].status === 'ok', 'batch[2] applied ok');
if (apply![0]?.status === 'ok') {
assert.equal(apply![0].inventoryPng.type, 'image/png');
assert.notEqual(apply![0].inventoryPng, a, 'must not proxy source thumb as inventory PNG');
}
if (apply![2]?.status === 'ok') {
assert.equal(apply![2].inventoryPng.type, 'image/png');
assert.notEqual(apply![2].inventoryPng, c);
}
// Frozen params: every sibling mask call used calibration sheet/sensitivity
const siblingBlobs = new Set([a, c]);
const siblingMasks = FakeMaskWorker.maskCalls.filter((m) => m.blob && siblingBlobs.has(m.blob as File));
assert.ok(siblingMasks.length >= 2, 'mask worker must run on non-calibration photos');
for (const m of siblingMasks) {
assert.equal(m.sheet, 'black', 'reuse approved sheet');
assert.equal(m.sensitivity, 72, 'reuse approved sensitivity');
}
s.dispose();
});
test('#62: empty mask on one sibling → per-photo error; others still proceed', async () => {
installFakes();
const a = tinyJpeg('a'); // will empty-mask
const b = tinyJpeg('b'); // cal
const c = tinyJpeg('c'); // ok
FakeMaskWorker.behaviorFor = (blob) => {
if (blob === a) {
return { w: 1000, h: 900, slabPx: 0, slabBounds: { x: 0, y: 0, w: 0, h: 0 } };
}
return { w: 1000, h: 900, slabPx: 1000 * 900, slabBounds: { x: 0, y: 0, w: 1000, h: 900 } };
};
const s = createCaptureSession({
draft: store,
encodeInventory: async () => ({
blob: new Blob(['ok-png'], { type: 'image/png' }),
sourceBelowFloor: false,
}),
} as Parameters<typeof createCaptureSession>[0]);
await driveToMeasure(s, [a, b, c], 1, { sheet: 'green' });
await s.calculate();
const apply = batchApplyOf(get(s.capture));
assert.ok(apply);
assert.equal(apply![1], null);
assert.ok(apply![0] && apply![0].status === 'error', 'empty mask → error');
if (apply![0]?.status === 'error') {
assert.match(apply![0].message, /empty|mask/i, 'clear per-photo empty-mask error');
}
assert.ok(apply![2] && apply![2].status === 'ok', 'other photos still proceed');
s.dispose();
});
test('#62: below PNG size floor on one sibling → per-photo error; others proceed', async () => {
installFakes();
const a = tinyJpeg('a'); // ok
const b = tinyJpeg('b'); // cal
const c = tinyJpeg('c'); // below floor
FakeMaskWorker.behaviorFor = (blob) => {
if (blob === c) {
return { w: 400, h: 300, slabPx: 400 * 300, slabBounds: { x: 0, y: 0, w: 400, h: 300 } };
}
return { w: 1000, h: 900, slabPx: 1000 * 900, slabBounds: { x: 0, y: 0, w: 1000, h: 900 } };
};
const s = createCaptureSession({
draft: store,
encodeInventory: async (input) => {
const below = Math.min(input.mask.slabBounds.w, input.mask.slabBounds.h) < 800;
return {
blob: new Blob([below ? 'tiny' : 'ok'], { type: 'image/png' }),
sourceBelowFloor: below,
};
},
} as Parameters<typeof createCaptureSession>[0]);
await driveToMeasure(s, [a, b, c], 1, { sheet: 'auto' });
await s.calculate();
const apply = batchApplyOf(get(s.capture));
assert.ok(apply);
assert.ok(apply![0] && apply![0].status === 'ok', 'above-floor sibling ok');
assert.ok(apply![2] && apply![2].status === 'error', 'below-floor → error');
if (apply![2]?.status === 'error') {
assert.match(apply![2].message, /floor|small|800|size/i, 'clear per-photo size-floor error');
}
s.dispose();
});
test('#62: scale/widths/sqft/bdft stay on calibration results only (siblings are visual)', async () => {
installFakes();
const s = createCaptureSession({
draft: store,
encodeInventory: async () => ({
blob: new Blob(['png'], { type: 'image/png' }),
sourceBelowFloor: false,
}),
} as Parameters<typeof createCaptureSession>[0]);
const a = tinyJpeg('a');
const b = tinyJpeg('b');
await driveToMeasure(s, [a, b], 1, { sheet: 'green' });
await s.calculate();
const cap = get(s.capture);
assert.equal(cap.k, 'measure');
if (cap.k === 'measure') {
assert.ok(cap.results);
assert.equal(typeof cap.results!.scale, 'number');
assert.ok(cap.results!.scale > 0);
assert.equal(typeof cap.results!.sqft, 'number');
assert.equal(typeof cap.results!.bdft, 'number');
assert.ok(cap.results!.widths);
// batchApply ok slots must NOT carry measure fields
const apply = (cap as { batchApply?: BatchApplySlot[] }).batchApply;
assert.ok(apply);
const slot = apply![0];
assert.ok(slot && slot.status === 'ok');
if (slot && slot.status === 'ok') {
assert.equal(
'scale' in slot,
false,
'non-cal apply must not attach scale',
);
assert.equal('sqft' in slot, false);
assert.equal('bdft' in slot, false);
assert.equal('widths' in slot, false);
}
}
s.dispose();
});
test('#62: draft persists batchApply after Calculate (for #64 UI later)', async () => {
installFakes();
const s = createCaptureSession({
draft: store,
encodeInventory: async () => ({
blob: new Blob(['draft-png'], { type: 'image/png' }),
sourceBelowFloor: false,
}),
} as Parameters<typeof createCaptureSession>[0]);
const a = tinyJpeg('a');
const b = tinyJpeg('b');
const c = tinyJpeg('c');
await driveToMeasure(s, [a, b, c], 0, { sheet: 'black' }); // cal = first
await s.calculate();
await new Promise((r) => setTimeout(r, 30));
const d = store.peek();
assert.ok(d);
assert.equal(d!.stage, 'measure');
assert.ok(Array.isArray(d!.batchApply), 'draft.batchApply must be persisted');
assert.equal(d!.batchApply!.length, 3);
assert.equal(d!.batchApply![0], null, 'calibration slot null on draft');
assert.ok(d!.batchApply![1] && (d!.batchApply![1] as { status: string }).status === 'ok');
assert.ok(d!.batchApply![2] && (d!.batchApply![2] as { status: string }).status === 'ok');
s.dispose();
});
test('#62: single-photo batch Calculate leaves batchApply=[null] (no sibling Mask)', async () => {
installFakes();
const s = createCaptureSession({
draft: store,
encodeInventory: async () => ({
blob: new Blob(['only'], { type: 'image/png' }),
sourceBelowFloor: false,
}),
} as Parameters<typeof createCaptureSession>[0]);
const only = tinyJpeg('only');
await driveToMeasure(s, [only], 0, { sheet: 'auto' });
const initsBefore = FakeMaskWorker.inits.length;
await s.calculate();
const apply = batchApplyOf(get(s.capture));
assert.ok(apply);
assert.deepEqual(apply, [null]);
// No extra mask inits for non-existent siblings after Calculate
const siblingInits = FakeMaskWorker.inits.slice(initsBefore).filter((blob) => blob !== only);
assert.equal(siblingInits.length, 0, 'must not invent Mask work for absent siblings');
s.dispose();
});
test('#62: Calculate does not re-enter Mask / does not open Mask controls for siblings', async () => {
installFakes();
const s = createCaptureSession({
draft: store,
encodeInventory: async () => ({
blob: new Blob(['x'], { type: 'image/png' }),
sourceBelowFloor: false,
}),
} as Parameters<typeof createCaptureSession>[0]);
await driveToMeasure(s, [tinyJpeg('a'), tinyJpeg('b')], 0, { sheet: 'green' });
await s.calculate();
assert.equal(get(s.capture).k, 'measure');
// Session source must never loop enterMaskOnSource over batch after Calculate
const sessionSrc = readFileSync(join(here, 'session.ts'), 'utf8');
assert.ok(
/batchApply|applyBatch|apply.*non-?calibrat/i.test(sessionSrc),
'session must implement batch apply after Calculate (#62)',
);
// Dishonest proxy: must not set k:'mask' inside calculate for each photo
const calcBody = sessionSrc.match(/async calculate\(\)[\s\S]*?(?=\n [a-zA-Z]+\(|\n \})/);
assert.ok(calcBody, 'calculate() must exist');
assert.ok(
!/set\(\s*\{\s*k:\s*'mask'/.test(calcBody![0]),
'calculate must not reopen Mask state for siblings',
);
const app = readFileSync(join(here, '..', 'App.svelte'), 'utf8');
assert.ok(
!/\{\s*#each\s+[^}]*batchApply[^}]*\}[\s\S]*MaskPanel/i.test(app) &&
!/\{\s*#each\s+[^}]*photos[^}]*\}[\s\S]*MaskPanel/i.test(app),
'App must not #each photos into MaskPanel',
);
s.dispose();
});
test('#62: resume restores batchApply alongside calibration results', async () => {
installFakes();
const s1 = createCaptureSession({
draft: store,
encodeInventory: async () => ({
blob: new Blob(['resume-png'], { type: 'image/png' }),
sourceBelowFloor: false,
}),
} as Parameters<typeof createCaptureSession>[0]);
const a = tinyJpeg('a');
const b = tinyJpeg('b');
await driveToMeasure(s1, [a, b], 1, { sheet: 'green' });
await s1.calculate();
await new Promise((r) => setTimeout(r, 30));
s1.dispose();
FakeMaskWorker.inits = [];
const s2 = createCaptureSession({ draft: store } as Parameters<typeof createCaptureSession>[0]);
await s2.resumeProbe();
s2.continueDraft();
const start = Date.now();
let cap = get(s2.capture);
while (cap.k !== 'measure' && Date.now() - start < 1500) {
await new Promise((r) => setTimeout(r, 10));
cap = get(s2.capture);
}
assert.equal(cap.k, 'measure');
const apply = batchApplyOf(cap);
assert.ok(apply, 'restored measure must include batchApply');
assert.equal(apply!.length, 2);
assert.equal(apply![1], null);
assert.ok(apply![0] && apply![0].status === 'ok');
if (apply![0]?.status === 'ok') {
assert.equal(apply![0].inventoryPng.type, 'image/png');
}
s2.dispose();
});
@@ -0,0 +1,540 @@
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { test, beforeEach, afterEach } from 'node:test';
import { get } from 'svelte/store';
import type { Draft, Mask, Source, WorkerIn, WorkerOut } from './capture.types';
import { createCaptureSession, type DraftStore } from './session';
const here = dirname(fileURLToPath(import.meta.url));
function tinyJpeg(label: string): File {
const bytes = new Uint8Array([0xff, 0xd8, 0xff, 0xe0, label.charCodeAt(0) & 0xff]);
return new File([bytes], `${label}.jpg`, { type: 'image/jpeg' });
}
function memStore(): DraftStore & { peek: () => Draft | null; reset: () => void } {
let mem: Draft | null = null;
return {
loadDraft: async () => mem,
saveDraft: async (d) => {
mem = d;
},
clearDraft: async () => {
mem = null;
},
peek: () => mem,
reset: () => {
mem = null;
},
};
}
let store: ReturnType<typeof memStore>;
beforeEach(() => {
store = memStore();
});
test('continueBatch with ≥1 photo goes to pickCalibration (not mask — no first-photo proxy)', async () => {
const s = createCaptureSession({ draft: store });
await s.addToBatch(tinyJpeg('a'));
await s.addToBatch(tinyJpeg('b'));
s.continueBatch();
const cap = get(s.capture);
assert.equal(cap.k, 'pickCalibration', 'Continue must open pick-calibration, not skip to Mask');
if (cap.k === 'pickCalibration') {
assert.equal(cap.photos.length, 2);
}
s.dispose();
});
test('single-photo batch: Continue still opens pickCalibration so user confirms the only photo', async () => {
const s = createCaptureSession({ draft: store });
await s.addToBatch(tinyJpeg('only'));
s.continueBatch();
const cap = get(s.capture);
assert.equal(cap.k, 'pickCalibration');
if (cap.k === 'pickCalibration') {
assert.equal(cap.photos.length, 1);
}
s.dispose();
});
test('pickCalibration(index) enters Mask on that Source only (not always first)', async () => {
const s = createCaptureSession({ draft: store });
const a = tinyJpeg('a');
const b = tinyJpeg('b');
const c = tinyJpeg('c');
await s.addToBatch(a);
await s.addToBatch(b);
await s.addToBatch(c);
s.continueBatch();
assert.equal(get(s.capture).k, 'pickCalibration');
s.pickCalibration(1);
const cap = get(s.capture);
assert.equal(cap.k, 'mask');
if (cap.k === 'mask') {
// Chosen photo must be the second batch blob, not photos[0].
assert.equal(cap.source.blob, b, 'Mask source must be the user-picked photo');
assert.notEqual(cap.source.blob, a, 'must not silently use first photo');
}
s.dispose();
});
test('single-photo pick enters Mask on that photo', async () => {
const s = createCaptureSession({ draft: store });
const only = tinyJpeg('only');
await s.addToBatch(only);
s.continueBatch();
s.pickCalibration(0);
const cap = get(s.capture);
assert.equal(cap.k, 'mask');
if (cap.k === 'mask') {
assert.equal(cap.source.blob, only);
}
s.dispose();
});
test('pickCalibration out of range is a no-op', async () => {
const s = createCaptureSession({ draft: store });
await s.addToBatch(tinyJpeg('a'));
s.continueBatch();
s.pickCalibration(-1);
assert.equal(get(s.capture).k, 'pickCalibration');
s.pickCalibration(99);
assert.equal(get(s.capture).k, 'pickCalibration');
s.dispose();
});
test('draft persists pickCalibration stage with batch photos', async () => {
const s = createCaptureSession({ draft: store });
await s.addToBatch(tinyJpeg('a'));
await s.addToBatch(tinyJpeg('b'));
s.continueBatch();
await new Promise((r) => setTimeout(r, 20));
const d = store.peek();
assert.ok(d, 'draft must exist on pickCalibration');
assert.equal(d!.stage, 'pickCalibration');
assert.equal((d!.batch ?? []).length, 2);
s.dispose();
});
test('draft remembers calibration choice after pick', async () => {
const s = createCaptureSession({ draft: store });
const a = tinyJpeg('a');
const b = tinyJpeg('b');
await s.addToBatch(a);
await s.addToBatch(b);
s.continueBatch();
s.pickCalibration(1);
await new Promise((r) => setTimeout(r, 20));
const d = store.peek();
assert.ok(d);
assert.equal(d!.stage, 'mask');
assert.equal(d!.calibrationIndex, 1, 'draft must store which batch photo is calibration');
assert.equal(d!.source.blob, b, 'draft.source must be the chosen calibration photo');
assert.equal((d!.batch ?? []).length, 2, 'full batch still stored');
s.dispose();
});
test('refresh: resumeProbe + Continue restores pickCalibration with batch', async () => {
const s1 = createCaptureSession({ draft: store });
await s1.addToBatch(tinyJpeg('a'));
await s1.addToBatch(tinyJpeg('b'));
s1.continueBatch();
await new Promise((r) => setTimeout(r, 20));
s1.dispose();
const s2 = createCaptureSession({ draft: store });
await s2.resumeProbe();
assert.equal(get(s2.capture).k, 'resume');
s2.continueDraft();
await new Promise((r) => setTimeout(r, 20));
const cap = get(s2.capture);
assert.equal(cap.k, 'pickCalibration');
if (cap.k === 'pickCalibration') {
assert.equal(cap.photos.length, 2);
assert.ok(cap.photos.every((p: Source) => p.blob instanceof Blob));
}
s2.dispose();
});
test('refresh: resume after pick restores Mask on the chosen calibration photo', async () => {
const s1 = createCaptureSession({ draft: store });
const a = tinyJpeg('a');
const b = tinyJpeg('b');
await s1.addToBatch(a);
await s1.addToBatch(b);
s1.continueBatch();
s1.pickCalibration(1);
await new Promise((r) => setTimeout(r, 20));
s1.dispose();
const s2 = createCaptureSession({ draft: store });
await s2.resumeProbe();
s2.continueDraft();
await new Promise((r) => setTimeout(r, 40));
const cap = get(s2.capture);
assert.equal(cap.k, 'mask');
if (cap.k === 'mask') {
// Blob identity may round-trip as a new Blob with same bytes; compare size/type content.
assert.ok(cap.source.blob instanceof Blob);
const chosen = new Uint8Array(await b.arrayBuffer());
const got = new Uint8Array(await cap.source.blob.arrayBuffer());
assert.deepEqual(got, chosen, 'restored Mask source must be the picked calibration photo');
}
// calibrationIndex still on draft
const d = store.peek();
assert.equal(d?.calibrationIndex, 1);
s2.dispose();
});
test('PickCalibration UI: thumbs over batch + pick action (not auto-first)', () => {
const path = join(here, 'PickCalibration.svelte');
let src = '';
try {
src = readFileSync(path, 'utf8');
} catch {
/* missing = fail */
}
assert.ok(src, 'expected PickCalibration.svelte');
assert.match(src, /calibrat/i, 'UI must mention calibration');
assert.match(src, /photos|thumb/i, 'UI must show batch thumbs');
assert.ok(
/onPick|pickCalibration|on:click/.test(src),
'UI must wire a per-photo pick action',
);
// Must not hardcode always-first without user interaction.
assert.ok(
!/photos\[0\].*mask|always.*first/i.test(src) || /onPick|on:click/.test(src),
'must require user pick',
);
});
test('App wires pickCalibration state to PickCalibration panel', () => {
const app = readFileSync(join(here, '..', 'App.svelte'), 'utf8');
assert.match(app, /PickCalibration/, 'App must import/render PickCalibration');
assert.match(app, /pickCalibration/, 'App must branch on pickCalibration state');
});
// --- #61 M2-4: wire M1 mask/axis/measure onto the calibration pick ---
const MASK_W = 40;
const MASK_H = 20;
/** Fake Worker so Node can drive Mask → Axis → Measure without canvas decode. */
class FakeMaskWorker {
onmessage: ((ev: MessageEvent<WorkerOut>) => void) | null = null;
static inits: Blob[] = [];
constructor(_url: URL | string, _opts?: WorkerOptions) {}
postMessage(msg: WorkerIn) {
if (msg.kind === 'init') {
FakeMaskWorker.inits.push(msg.blob);
const id = msg.id;
queueMicrotask(() => {
this.onmessage?.({ data: { id, kind: 'decoded', w: MASK_W, h: MASK_H } } as MessageEvent<WorkerOut>);
});
return;
}
if (msg.kind === 'mask') {
const id = msg.id;
const bitset = new Uint8Array(MASK_W * MASK_H).fill(1);
const bbox = {
corners: [
[MASK_W, 0],
[MASK_W, MASK_H],
[0, MASK_H],
[0, 0],
] as [number, number][],
longSidePx: MASK_W,
angleDeg: 0,
};
queueMicrotask(() => {
this.onmessage?.({
data: {
id,
kind: 'ready',
slabPx: MASK_W * MASK_H,
bitset,
bbox,
slabBounds: { x: 0, y: 0, w: MASK_W, h: MASK_H },
},
} as MessageEvent<WorkerOut>);
});
return;
}
// invalidate: no-op
}
terminate() {}
}
const RealWorker = globalThis.Worker;
const RealCreateImageBitmap = globalThis.createImageBitmap;
function installFakeMaskWorker() {
FakeMaskWorker.inits = [];
(globalThis as unknown as { Worker: typeof FakeMaskWorker }).Worker = FakeMaskWorker;
}
function restoreWorkerGlobals() {
if (RealWorker === undefined) {
delete (globalThis as unknown as { Worker?: unknown }).Worker;
} else {
globalThis.Worker = RealWorker;
}
if (RealCreateImageBitmap === undefined) {
delete (globalThis as unknown as { createImageBitmap?: unknown }).createImageBitmap;
} else {
globalThis.createImageBitmap = RealCreateImageBitmap;
}
}
async function settleOverlay(
s: ReturnType<typeof createCaptureSession>,
ms = 800,
): Promise<Mask> {
const start = Date.now();
while (Date.now() - start < ms) {
const cap = get(s.capture);
if (cap.k === 'mask' && cap.overlay && cap.overlay.slabPx > 0) return cap.overlay;
await new Promise((r) => setTimeout(r, 5));
}
throw new Error('mask overlay never settled');
}
afterEach(() => {
restoreWorkerGlobals();
});
test('#61: after pick of index N≠0, Mask/Axis/Measure operate on that blob only', async () => {
installFakeMaskWorker();
const s = createCaptureSession({ draft: store });
const a = tinyJpeg('a');
const b = tinyJpeg('b');
const c = tinyJpeg('c');
await s.addToBatch(a);
await s.addToBatch(b);
await s.addToBatch(c);
s.continueBatch();
s.pickCalibration(1);
let cap = get(s.capture);
assert.equal(cap.k, 'mask');
if (cap.k === 'mask') {
assert.equal(cap.source.blob, b, 'Mask must open on picked photo index 1');
assert.notEqual(cap.source.blob, a);
}
// Kick an immediate mask pass (skip 2s sensitivity debounce).
s.setSheet('green');
await settleOverlay(s);
// Worker must have been inited only for the calibration blob — not a, not c.
assert.ok(FakeMaskWorker.inits.length >= 1, 'mask worker must init');
for (const blob of FakeMaskWorker.inits) {
assert.equal(blob, b, 'Mask worker must never init on non-calibration batch photos');
}
s.approveMask();
cap = get(s.capture);
assert.equal(cap.k, 'axis', 'approve Mask → Length axis');
if (cap.k === 'axis') {
assert.equal(cap.source.blob, b, 'Axis must keep the calibration source');
}
s.approveAxis();
cap = get(s.capture);
assert.equal(cap.k, 'measure', 'approve Axis → Measure');
if (cap.k === 'measure') {
assert.equal(cap.source.blob, b, 'Measure must keep the calibration source');
}
s.dispose();
});
test('#61: Calculate produces inventory PNG + M1 measures for calibration source only', async () => {
installFakeMaskWorker();
(globalThis as unknown as { createImageBitmap: typeof createImageBitmap }).createImageBitmap =
async () =>
({
width: MASK_W,
height: MASK_H,
close() {},
}) as ImageBitmap;
const s = createCaptureSession({ draft: store });
const a = tinyJpeg('a');
const b = tinyJpeg('b');
await s.addToBatch(a);
await s.addToBatch(b);
s.continueBatch();
s.pickCalibration(1);
s.setSheet('black');
await settleOverlay(s);
s.approveMask();
s.approveAxis();
// L = 40 in (320 eighths), thickness = 2 in (16 eighths) — scale ≈ longest_axis_px / L
s.setLength(320);
s.setThickness(16);
await s.calculate();
const cap = get(s.capture);
assert.equal(cap.k, 'measure');
if (cap.k === 'measure') {
assert.equal(cap.source.blob, b);
assert.ok(cap.results, 'Calculate must produce results');
const r = cap.results!;
assert.ok(r.inventoryPng instanceof Blob, 'clean inventory PNG blob');
assert.equal(typeof r.scale, 'number');
assert.ok(r.scale > 0, 'scale px/in > 0');
assert.equal(typeof r.sqft, 'number');
assert.equal(typeof r.bdft, 'number');
// CAPTURE.md: bdft = sqft × thickness_in
const thicknessIn = 16 / 8;
assert.ok(
Math.abs(r.bdft - r.sqft * thicknessIn) < 0.011,
`bdft ${r.bdft} must equal sqft×thickness (${r.sqft}*${thicknessIn})`,
);
assert.ok(r.widths, 'widths present');
assert.equal(r.lengthEighths, 320);
assert.equal(r.thicknessEighths, 16);
assert.equal(cap.source.widthPx, MASK_W, 'measure source dims from calibration decode');
}
// Pre-#62: Mask UI path used only the calibration blob. After Calculate, #62
// may init the worker on non-calibration siblings for frozen-mask apply.
const preApplyInits = FakeMaskWorker.inits.filter((blob) => blob === b || blob === a);
assert.ok(preApplyInits.some((blob) => blob === b), 'calibration must have been masked');
// Sibling apply may touch `a`; that is #62, not a #61 regression of Mask UI.
assert.ok(
FakeMaskWorker.inits.filter((blob) => blob === b).length >= 1,
'calibration blob must appear in worker inits',
);
s.dispose();
});
test('#61: other batch photos stay in draft.batch untouched by mask approve', async () => {
installFakeMaskWorker();
const s = createCaptureSession({ draft: store });
const a = tinyJpeg('a');
const b = tinyJpeg('b');
const c = tinyJpeg('c');
await s.addToBatch(a);
await s.addToBatch(b);
await s.addToBatch(c);
s.continueBatch();
s.pickCalibration(1);
s.setSheet('auto');
await settleOverlay(s);
const before = store.peek();
assert.ok(before);
assert.equal((before!.batch ?? []).length, 3);
assert.equal(before!.batch![0].blob, a);
assert.equal(before!.batch![1].blob, b);
assert.equal(before!.batch![2].blob, c);
s.approveMask();
await new Promise((r) => setTimeout(r, 20));
const after = store.peek();
assert.ok(after);
assert.equal(after!.stage, 'axis');
assert.equal(after!.calibrationIndex, 1);
assert.equal(after!.source.blob, b, 'draft.source is calibration only');
assert.equal((after!.batch ?? []).length, 3, 'full batch retained');
assert.equal(after!.batch![0].blob, a, 'batch[0] untouched');
assert.equal(after!.batch![1].blob, b);
assert.equal(after!.batch![2].blob, c, 'batch[2] untouched — no Mask UI on other shots');
// Approve axis too — batch still untouched.
s.approveAxis();
await new Promise((r) => setTimeout(r, 20));
const meas = store.peek();
assert.equal(meas!.stage, 'measure');
assert.equal((meas!.batch ?? []).length, 3);
assert.equal(meas!.batch![0].blob, a);
assert.equal(meas!.batch![2].blob, c);
assert.equal(meas!.calibrationIndex, 1);
s.dispose();
});
test('#61: draft resume through measure restores calibration source (not photos[0])', async () => {
installFakeMaskWorker();
(globalThis as unknown as { createImageBitmap: typeof createImageBitmap }).createImageBitmap =
async () =>
({
width: MASK_W,
height: MASK_H,
close() {},
}) as ImageBitmap;
const s1 = createCaptureSession({ draft: store });
const a = tinyJpeg('a');
const b = tinyJpeg('b');
await s1.addToBatch(a);
await s1.addToBatch(b);
s1.continueBatch();
s1.pickCalibration(1);
s1.setSheet('green');
await settleOverlay(s1);
s1.approveMask();
s1.approveAxis();
s1.setLength(320);
s1.setThickness(16);
await s1.calculate();
await new Promise((r) => setTimeout(r, 30));
s1.dispose();
FakeMaskWorker.inits = [];
const s2 = createCaptureSession({ draft: store });
await s2.resumeProbe();
assert.equal(get(s2.capture).k, 'resume');
s2.continueDraft();
// rederiveMask via FakeWorker
const start = Date.now();
let cap = get(s2.capture);
while (cap.k !== 'measure' && Date.now() - start < 1500) {
await new Promise((r) => setTimeout(r, 10));
cap = get(s2.capture);
}
assert.equal(cap.k, 'measure', 'Continue must restore Measure');
if (cap.k === 'measure') {
assert.equal(cap.source.blob, b, 'restored Measure source is calibration pick');
assert.ok(cap.results, 'results restored');
}
assert.equal(store.peek()?.calibrationIndex, 1);
// Resume rederive may re-init worker on calibration source only.
for (const blob of FakeMaskWorker.inits) {
assert.equal(blob, b, 'resume must not mask non-calibration batch photos');
}
s2.dispose();
});
test('#61: App runs single Mask/Axis/Measure panels (no per-batch Mask loop)', () => {
const app = readFileSync(join(here, '..', 'App.svelte'), 'utf8');
assert.match(app, /MaskPanel/);
assert.match(app, /AxisPanel/);
assert.match(app, /MeasurePanel/);
// Dishonest proxy: must not iterate batch photos into Mask UI.
assert.ok(
!/\{\s*#each\s+[^}]*photos[^}]*\}[\s\S]*MaskPanel/i.test(app),
'App must not #each batch photos into MaskPanel',
);
const sessionSrc = readFileSync(join(here, 'session.ts'), 'utf8');
assert.ok(
!/for\s*\([^)]*batch[^)]*\)[\s\S]{0,120}(enterMaskOnSource|approveMask|k:\s*'mask')/.test(
sessionSrc,
),
'session must not loop Mask over every batch photo in this issue',
);
});
@@ -0,0 +1,286 @@
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { test, beforeEach, afterEach } from 'node:test';
import { get } from 'svelte/store';
import type { Draft, Mask, MaskParams, WorkerIn, WorkerOut } from './capture.types';
import { createCaptureSession, type DraftStore } from './session';
const here = dirname(fileURLToPath(import.meta.url));
function tinyJpeg(label: string): File {
const bytes = new Uint8Array([0xff, 0xd8, 0xff, 0xe0, label.charCodeAt(0) & 0xff]);
return new File([bytes], `${label}.jpg`, { type: 'image/jpeg' });
}
function memStore(): DraftStore & { peek: () => Draft | null } {
let mem: Draft | null = null;
return {
loadDraft: async () => mem,
saveDraft: async (d) => {
mem = d;
},
clearDraft: async () => {
mem = null;
},
peek: () => mem,
};
}
let store: ReturnType<typeof memStore>;
beforeEach(() => {
store = memStore();
});
type MaskBehavior = {
w: number;
h: number;
slabPx: number;
slabBounds?: { x: number; y: number; w: number; h: number };
};
class FakeMaskWorker {
onmessage: ((ev: MessageEvent<WorkerOut>) => void) | null = null;
static inits: Blob[] = [];
static maskCalls: { sheet: string; sensitivity: number; blob: Blob | null }[] = [];
static lastInitBlob: Blob | null = null;
static behaviorFor: (blob: Blob) => MaskBehavior = () => ({
w: 1000,
h: 900,
slabPx: 1000 * 900,
slabBounds: { x: 0, y: 0, w: 1000, h: 900 },
});
constructor(_url: URL | string, _opts?: WorkerOptions) {}
postMessage(msg: WorkerIn) {
if (msg.kind === 'init') {
FakeMaskWorker.inits.push(msg.blob);
FakeMaskWorker.lastInitBlob = msg.blob;
const id = msg.id;
const b = FakeMaskWorker.behaviorFor(msg.blob);
queueMicrotask(() => {
this.onmessage?.({ data: { id, kind: 'decoded', w: b.w, h: b.h } } as MessageEvent<WorkerOut>);
});
return;
}
if (msg.kind === 'mask') {
const id = msg.id;
const blob = FakeMaskWorker.lastInitBlob;
FakeMaskWorker.maskCalls.push({
sheet: msg.sheet,
sensitivity: msg.sensitivity,
blob,
});
const b = blob ? FakeMaskWorker.behaviorFor(blob) : { w: 40, h: 20, slabPx: 0 };
const bitset = new Uint8Array(Math.max(1, b.w * b.h)).fill(b.slabPx > 0 ? 1 : 0);
const bbox = {
corners: [
[b.w, 0],
[b.w, b.h],
[0, b.h],
[0, 0],
] as [number, number][],
longSidePx: b.w,
angleDeg: 0,
};
const slabBounds = b.slabBounds ?? { x: 0, y: 0, w: b.w, h: b.h };
queueMicrotask(() => {
this.onmessage?.({
data: {
id,
kind: 'ready',
slabPx: b.slabPx,
bitset,
bbox,
slabBounds,
},
} as MessageEvent<WorkerOut>);
});
return;
}
}
terminate() {}
}
const RealWorker = globalThis.Worker;
const RealCreateImageBitmap = globalThis.createImageBitmap;
function installFakes() {
FakeMaskWorker.inits = [];
FakeMaskWorker.maskCalls = [];
FakeMaskWorker.lastInitBlob = null;
FakeMaskWorker.behaviorFor = () => ({
w: 1000,
h: 900,
slabPx: 1000 * 900,
slabBounds: { x: 0, y: 0, w: 1000, h: 900 },
});
(globalThis as unknown as { Worker: typeof FakeMaskWorker }).Worker = FakeMaskWorker;
(globalThis as unknown as { createImageBitmap: typeof createImageBitmap }).createImageBitmap =
async (blob: ImageBitmapSource) => {
const b = blob instanceof Blob ? FakeMaskWorker.behaviorFor(blob) : { w: 1000, h: 900 };
return { width: b.w, height: b.h, close() {} } as ImageBitmap;
};
}
function restoreGlobals() {
if (RealWorker === undefined) {
delete (globalThis as unknown as { Worker?: unknown }).Worker;
} else {
globalThis.Worker = RealWorker;
}
if (RealCreateImageBitmap === undefined) {
delete (globalThis as unknown as { createImageBitmap?: unknown }).createImageBitmap;
} else {
globalThis.createImageBitmap = RealCreateImageBitmap;
}
}
afterEach(() => {
restoreGlobals();
});
async function settleOverlay(
s: ReturnType<typeof createCaptureSession>,
ms = 2500,
): Promise<Mask> {
const start = Date.now();
while (Date.now() - start < ms) {
const cap = get(s.capture);
if (cap.k === 'mask' && cap.overlay && cap.overlay.slabPx > 0) return cap.overlay;
await new Promise((r) => setTimeout(r, 5));
}
throw new Error('mask overlay never settled');
}
async function driveToMeasure(
s: ReturnType<typeof createCaptureSession>,
photos: File[],
calIndex: number,
maskParams: Partial<MaskParams> = { sheet: 'green', sensitivity: 50 },
) {
for (const p of photos) await s.addToBatch(p);
s.continueBatch();
s.pickCalibration(calIndex);
if (typeof maskParams.sensitivity === 'number') s.setSensitivity(maskParams.sensitivity);
// setSheet runs an immediate pass (after sensitivity so the pass sees frozen params)
s.setSheet(maskParams.sheet ?? 'auto');
await settleOverlay(s);
s.approveMask();
s.approveAxis();
s.setLength(320); // 40 in
s.setThickness(16); // 2 in
}
test('test(photos): Calculate stores overlayPng on results, clean inventory untouched (#63)', async () => {
installFakes();
let encodeN = 0;
const s = createCaptureSession({
draft: store,
encodeInventory: async () => {
encodeN++;
return {
blob: new Blob([`clean-${encodeN}`], { type: 'image/png' }),
sourceBelowFloor: false,
};
},
} as Parameters<typeof createCaptureSession>[0]);
const a = tinyJpeg('a');
const b = tinyJpeg('b'); // cal
await driveToMeasure(s, [a, b], 1, { sheet: 'green' });
await s.calculate();
const cap = get(s.capture);
assert.equal(cap.k, 'measure');
if (cap.k !== 'measure') return;
assert.ok(cap.results, 'results present');
assert.ok(cap.results!.inventoryPng, 'clean cal PNG in results.inventoryPng');
assert.equal(cap.results!.inventoryPng.type, 'image/png');
const overlay = (cap.results as { overlayPng?: Blob }).overlayPng;
assert.ok(overlay, 'results.overlayPng must exist after Calculate (#63)');
assert.equal(overlay!.type, 'image/png');
assert.notEqual(overlay, cap.results!.inventoryPng, 'overlay is a second copy, not the clean PNG');
const apply = (cap as { batchApply?: unknown[] }).batchApply;
assert.ok(Array.isArray(apply));
assert.equal(apply![1], null, 'batchApply[cal] stays null');
assert.ok(apply![0] && (apply![0] as { status: string }).status === 'ok', 'siblings unchanged');
s.dispose();
});
test('test(photos): single-photo batch still yields clean + overlayPng (#63)', async () => {
installFakes();
const s = createCaptureSession({
draft: store,
encodeInventory: async () => ({
blob: new Blob(['only-clean'], { type: 'image/png' }),
sourceBelowFloor: false,
}),
} as Parameters<typeof createCaptureSession>[0]);
await driveToMeasure(s, [tinyJpeg('only')], 0, { sheet: 'auto' });
await s.calculate();
const cap = get(s.capture);
assert.equal(cap.k, 'measure');
if (cap.k !== 'measure') return;
assert.ok(cap.results!.inventoryPng);
assert.ok((cap.results as { overlayPng?: Blob }).overlayPng, 'overlay even for batch of one');
assert.deepEqual((cap as { batchApply?: unknown[] }).batchApply, [null]);
s.dispose();
});
test('test(photos): draft persists overlayPng alongside inventoryPng (#63)', async () => {
installFakes();
const s = createCaptureSession({
draft: store,
encodeInventory: async () => ({
blob: new Blob(['draft-clean'], { type: 'image/png' }),
sourceBelowFloor: false,
}),
} as Parameters<typeof createCaptureSession>[0]);
await driveToMeasure(s, [tinyJpeg('a'), tinyJpeg('b')], 0, { sheet: 'black' });
await s.calculate();
await new Promise((r) => setTimeout(r, 30));
const d = store.peek();
assert.ok(d);
assert.equal(d!.stage, 'measure');
assert.ok(d!.inventoryPng, 'draft keeps clean inventory');
assert.ok(
(d as { overlayPng?: Blob | null }).overlayPng,
'draft.overlayPng must be persisted for #64 lineup',
);
assert.equal(d!.batchApply![0], null, 'cal slot still null on draft');
s.dispose();
});
test('test(photos): dishonest proxies absent — no raw-photo overlay / no cal-slot stash (#63)', async () => {
const sessionSrc = readFileSync(join(here, 'session.ts'), 'utf8');
const overlaySrcPath = join(here, '..', 'png', 'overlay.ts');
let overlaySrc = '';
try {
overlaySrc = readFileSync(overlaySrcPath, 'utf8');
} catch {
overlaySrc = '';
}
assert.ok(
/overlayPng|encodeOverlay|dimensionOverlay|buildOverlayPlan/i.test(sessionSrc) ||
overlaySrc.length > 0,
'session or png/overlay must implement dimension overlay (#63)',
);
const calcBody = sessionSrc.match(/async calculate\(\)[\s\S]*?(?=\n [a-zA-Z]+\(|\n \})/);
assert.ok(calcBody, 'calculate() must exist');
assert.ok(
!/batchApply\s*\[\s*calIndex\s*\]\s*=\s*\{[^}]*overlay/i.test(calcBody![0]),
'must not put overlay into batchApply[cal]',
);
});
+748
View File
@@ -0,0 +1,748 @@
import { writable } from 'svelte/store';
import type {
BatchPhotoResult, Capture, Draft, Eighths, Mask, MaskParams, Measures, Sheet, Source, WorkerIn, WorkerOut,
} from './capture.types';
import {
DEFAULT_MASK_PARAMS, EMPTY_ABOX, EMPTY_BBOX, eighthsToIn,
} from './capture.types';
import { loadDraft, saveDraft, clearDraft } from '../draft/idb';
import { measure } from '../measure/measure';
import { orientedRect } from '../geometry/rect';
import { encodeInventoryPng, type InventoryInput } from '../png/inventory';
import { encodeOverlayPng, type EncodeOverlayInput } from '../png/overlay';
import { rotateCalibration } from '../png/rotateCal';
import { decodeOrientedBitmap } from '../decodeOrientedBitmap';
import { defaultTape, tapeEndpoints, type TapeTransform } from './tapeGeometry';
import { getCropFill } from '../settings/cropFill';
import type { TapeLine } from './capture.types';
const DEBOUNCE_MS = 2000;
const BUSY_MS = 300;
export interface DraftStore {
loadDraft(): Promise<Draft | null>;
saveDraft(d: Draft): Promise<void>;
clearDraft(): Promise<void>;
}
export interface CaptureSession {
capture: import('svelte/store').Readable<Capture>;
busy: import('svelte/store').Readable<boolean>;
resumeProbe(): Promise<void>;
continueDraft(): void;
startFresh(): void;
/** Append a photo to the batch; stays on batch. */
addToBatch(file: File): Promise<void>;
/** Alias: same as addToBatch (no longer jumps to mask). */
addPhoto(file: File): Promise<void>;
removeFromBatch(index: number): void;
/** Advance when batch has ≥1 photo → pickCalibration. */
continueBatch(): void;
/** User picks which batch photo is the calibration reference → Mask on that Source. */
pickCalibration(index: number): void;
retakes(): void;
setSensitivity(n: number): void;
setSheet(s: Sheet): void;
approveMask(): void;
setAxisAngle(deg: number): void;
/** #99: replace axis rectangle nudge with full tape transform. */
setTape(tape: TapeTransform): void;
approveAxis(): void;
setLength(e: Eighths): void;
setThickness(e: Eighths): void;
calculate(): Promise<void>;
back(): void;
getInventoryUrl(): string | null;
dispose(): void;
}
export interface SessionOpts {
draft?: DraftStore;
/** Injectable only for deterministic tests; production uses encodeInventoryPng. */
encodeInventory?: (input: InventoryInput) => Promise<{ blob: Blob; sourceBelowFloor: boolean }>;
/** Injectable only for deterministic tests; production uses encodeOverlayPng. */
encodeOverlay?: (input: EncodeOverlayInput) => Promise<Blob>;
}
const defaultDraft: DraftStore = { loadDraft, saveDraft, clearDraft };
const batchPhotosOf = (d: Draft): Source[] =>
d.batch && d.batch.length > 0 ? d.batch : [d.source];
// The one writer. Owns the worker port, the single debounce timer, and the draft.
// Every transition to an approved branch persists; no explicit "save".
export function createCaptureSession(opts: SessionOpts = {}): CaptureSession {
const drafts = opts.draft ?? defaultDraft;
const encodeInventory = opts.encodeInventory ?? encodeInventoryPng;
const encodeOverlay = opts.encodeOverlay ?? encodeOverlayPng;
const capture = writable<Capture>({ k: 'batch', photos: [] });
const busy = writable(false);
let state: Capture = { k: 'batch', photos: [] };
let batchPhotos: Source[] = [];
let calibrationIndex: number | null = null;
let worker: Worker | null = null;
let jobId = 0;
let timer: ReturnType<typeof setTimeout> | null = null;
let passStarted = 0;
let busyTimer: ReturnType<typeof setTimeout> | null = null;
let pngUrl: string | null = null;
let resumed = false;
const set = (next: Capture) => {
state = next;
capture.set(next);
};
const teardownWorker = () => {
if (timer) {
clearTimeout(timer);
timer = null;
}
if (worker) {
try {
worker.postMessage({ id: ++jobId, kind: 'invalidate' } satisfies WorkerIn);
worker.terminate();
} catch {
/* worker unavailable */
}
worker = null;
}
if (pngUrl) {
URL.revokeObjectURL(pngUrl);
pngUrl = null;
}
};
const makeWorker = (blob: Blob) => {
if (typeof Worker === 'undefined') return;
if (worker) worker.terminate();
try {
worker = new Worker(new URL('../worker/mask.worker.ts', import.meta.url), { type: 'module' });
worker.onmessage = (e: MessageEvent<WorkerOut>) => onWorkerOut(e.data);
worker.postMessage({ id: ++jobId, kind: 'init', blob } satisfies WorkerIn);
} catch {
worker = null;
}
};
const onWorkerOut = (msg: WorkerOut) => {
if (msg.id < jobId - 1) return; // stale
if (msg.kind === 'decoded') {
if (state.k === 'mask') {
set({ ...state, source: { ...state.source, widthPx: msg.w, heightPx: msg.h } });
}
schedulePass();
return;
}
busy.set(false);
if (busyTimer) {
clearTimeout(busyTimer);
busyTimer = null;
}
if (state.k !== 'mask') return;
if (msg.kind === 'failed') {
set({ ...state, failed: true });
return;
}
const mask: Mask = {
params: { sheet: state.control.sheet, sensitivity: state.control.sensitivity },
slabPx: msg.slabPx,
bitset: msg.bitset,
bbox: msg.bbox,
slabBounds: msg.slabBounds,
};
set({ ...state, overlay: mask, failed: false });
};
const runPass = () => {
if (state.k !== 'mask' || !worker) return;
passStarted = Date.now();
busyTimer = setTimeout(() => busy.set(true), BUSY_MS);
worker.postMessage({
id: ++jobId,
kind: 'mask',
sheet: state.control.sheet,
sensitivity: state.control.sensitivity,
} satisfies WorkerIn);
};
// The 2.0s debounce: re-armed on every control change; a sheet change
// recomputes immediately (spec allows), so it clears the pending pass.
const schedulePass = () => {
if (timer) clearTimeout(timer);
timer = setTimeout(runPass, DEBOUNCE_MS);
};
const draftFromState = (): Draft | null => {
const strip = (m: Mask) => ({ slabPx: m.slabPx, bbox: m.bbox, slabBounds: m.slabBounds });
if (state.k === 'batch') {
if (state.photos.length === 0) return null;
return {
version: 1,
stage: 'batch',
batch: state.photos,
source: state.photos[0],
maskParams: DEFAULT_MASK_PARAMS,
mask: { slabPx: 0, bbox: EMPTY_BBOX, slabBounds: EMPTY_ABOX },
lengthEighths: 0,
thicknessEighths: 0,
results: null,
inventoryPng: null,
};
}
if (state.k === 'pickCalibration') {
return {
version: 1,
stage: 'pickCalibration',
batch: state.photos,
source: state.photos[0],
maskParams: DEFAULT_MASK_PARAMS,
mask: { slabPx: 0, bbox: EMPTY_BBOX, slabBounds: EMPTY_ABOX },
lengthEighths: 0,
thicknessEighths: 0,
results: null,
inventoryPng: null,
};
}
const calIdx = calibrationIndex ?? undefined;
if (state.k === 'axis') {
return {
version: 1, stage: 'axis', batch: batchPhotos.length ? batchPhotos : [state.source],
calibrationIndex: calIdx,
source: state.source, maskParams: state.approvedMask.params,
mask: strip(state.approvedMask), axisBbox: state.axis.bbox, tape: state.axis.tape,
lengthEighths: 0, thicknessEighths: 0, results: null, inventoryPng: null,
};
}
if (state.k === 'measure') {
const { inventoryPng: _drop, overlayPng: _dropOverlay, ...rest } = state.results ?? ({} as Measures);
return {
version: 1, stage: 'measure', batch: batchPhotos.length ? batchPhotos : [state.source],
calibrationIndex: calIdx,
source: state.source, maskParams: state.approvedMask.params,
mask: strip(state.approvedMask), axisBbox: state.axis.bbox, tape: state.axis.tape,
lengthEighths: state.lengthEighths, thicknessEighths: state.thicknessEighths,
results: state.results ? rest : null,
inventoryPng: state.results?.inventoryPng ?? null,
overlayPng: state.results?.overlayPng ?? null,
batchApply: state.batchApply,
};
}
if (state.k === 'mask' && state.overlay) {
return {
version: 1, stage: 'mask', batch: batchPhotos.length ? batchPhotos : [state.source],
calibrationIndex: calIdx,
source: state.source, maskParams: state.overlay.params,
mask: strip(state.overlay),
lengthEighths: 0, thicknessEighths: 0, results: null, inventoryPng: null,
};
}
// Mask before first overlay settles: still persist calibration choice + batch.
if (state.k === 'mask') {
return {
version: 1, stage: 'mask', batch: batchPhotos.length ? batchPhotos : [state.source],
calibrationIndex: calIdx,
source: state.source, maskParams: state.control,
mask: { slabPx: 0, bbox: EMPTY_BBOX, slabBounds: EMPTY_ABOX },
lengthEighths: 0, thicknessEighths: 0, results: null, inventoryPng: null,
};
}
return null;
};
const persist = async () => {
const d = draftFromState();
if (d) await drafts.saveDraft(d);
else if (state.k === 'batch' && state.photos.length === 0) await drafts.clearDraft();
};
// Re-derive the mask bitset from a stored photo + frozen params. The decoded
// dimensions are returned because batch Sources begin with dimensions unknown.
const rederiveMaskDetailed = (
source: Source,
params: MaskParams,
stub: Omit<Mask, 'bitset' | 'params'>,
): Promise<{ mask: Mask; widthPx: number; heightPx: number }> =>
new Promise((resolve, reject) => {
if (typeof Worker === 'undefined') {
reject(new Error('Worker unavailable'));
return;
}
const w = new Worker(new URL('../worker/mask.worker.ts', import.meta.url), { type: 'module' });
let id = 0;
let widthPx = source.widthPx;
let heightPx = source.heightPx;
const fail = (reason: string) => {
w.terminate();
reject(new Error(reason));
};
w.onmessage = (e: MessageEvent<WorkerOut>) => {
const msg = e.data;
if (msg.kind === 'decoded') {
widthPx = msg.w;
heightPx = msg.h;
w.postMessage({ id: ++id, kind: 'mask', sheet: params.sheet, sensitivity: params.sensitivity } satisfies WorkerIn);
return;
}
if (msg.kind === 'failed') {
fail(msg.reason);
return;
}
if (msg.kind === 'ready') {
w.terminate();
resolve({
widthPx,
heightPx,
mask: {
params,
slabPx: msg.slabPx || stub.slabPx,
bitset: msg.bitset,
bbox: msg.slabPx > 0 ? msg.bbox : stub.bbox,
slabBounds: msg.slabPx > 0 ? msg.slabBounds : stub.slabBounds,
},
});
}
};
w.postMessage({ id: ++id, kind: 'init', blob: source.blob } satisfies WorkerIn);
});
const rederiveMask = (
source: Source,
params: MaskParams,
stub: Omit<Mask, 'bitset' | 'params'>,
): Promise<Mask> => rederiveMaskDetailed(source, params, stub).then((r) => r.mask);
// Legacy drafts (pre-axisBbox, with heading) carry no usable axis state;
// fall back to the mask's own bbox.
const draftAxisBbox = (d: Draft) => d.axisBbox ?? d.mask.bbox;
const tapeFromDraftOrBbox = (d: Draft, bbox: import('./capture.types').BBox, w: number, h: number): TapeLine => {
if (d.tape && Number.isFinite(d.tape.spanPx)) {
return { ...d.tape };
}
// Seed tilt so the mask long side sits near vertical under the fixed line.
const seedTilt = 90 - bbox.angleDeg;
const t = defaultTape(getCropFill(), seedTilt);
const ep = w > 0 && h > 0 ? tapeEndpoints(t, w, h) : { spanPx: 0, start: [0, 0] as [number, number], end: [0, 0] as [number, number], ux: 0, uy: 1 };
return { ...t, spanPx: ep.spanPx };
};
const axisWithTape = (bbox: import('./capture.types').BBox, tape: TapeTransform, w: number, h: number) => {
const ep = w > 0 && h > 0
? tapeEndpoints(tape, w, h)
: { spanPx: 0, start: [0, 0] as [number, number], end: [0, 0] as [number, number], ux: 0, uy: 1 };
const angleDeg = ep.spanPx > 0
? ((Math.atan2(ep.uy, ep.ux) * 180) / Math.PI + 180) % 180
: bbox.angleDeg;
const nextBbox = orientedRect(bbox, angleDeg);
const line: TapeLine = {
tiltDeg: tape.tiltDeg,
zoom: tape.zoom,
panX: tape.panX,
panY: tape.panY,
tickA: tape.tickA,
tickB: tape.tickB,
spanPx: ep.spanPx,
};
return { bbox: nextBbox, tape: line };
};
const restoreFromDraft = async (d: Draft) => {
batchPhotos = batchPhotosOf(d);
calibrationIndex = typeof d.calibrationIndex === 'number' ? d.calibrationIndex : null;
if (d.stage === 'batch') {
teardownWorker();
calibrationIndex = null;
set({ k: 'batch', photos: [...batchPhotos] });
return;
}
if (d.stage === 'pickCalibration') {
teardownWorker();
calibrationIndex = null;
set({ k: 'pickCalibration', photos: [...batchPhotos] });
return;
}
const source: Source = { widthPx: d.source.widthPx, heightPx: d.source.heightPx, blob: d.source.blob };
if (d.stage === 'mask') {
teardownWorker();
set({ k: 'mask', source, control: { ...d.maskParams }, overlay: null, failed: false });
makeWorker(source.blob);
return;
}
teardownWorker();
try {
const mask = await rederiveMask(source, d.maskParams, d.mask);
if (d.stage === 'axis') {
const bbox = draftAxisBbox(d);
const w = source.widthPx;
const h = source.heightPx;
const axis = d.tape
? axisWithTape(bbox, d.tape, w, h)
: { bbox, tape: tapeFromDraftOrBbox(d, bbox, w, h) };
set({ k: 'axis', source, approvedMask: mask, axis });
return;
}
if (d.inventoryPng) {
if (pngUrl) URL.revokeObjectURL(pngUrl);
pngUrl = URL.createObjectURL(d.inventoryPng);
}
{
const bbox = draftAxisBbox(d);
const w = source.widthPx;
const h = source.heightPx;
const axis = d.tape
? axisWithTape(bbox, d.tape, w, h)
: { bbox, tape: tapeFromDraftOrBbox(d, bbox, w, h) };
set({
k: 'measure', source, approvedMask: mask,
axis,
lengthEighths: d.lengthEighths, thicknessEighths: d.thicknessEighths,
results: d.results
? {
...d.results,
inventoryPng: d.inventoryPng ?? new Blob(),
overlayPng: d.overlayPng ?? undefined,
}
: null,
batchApply: d.batchApply,
});
}
} catch {
set({ k: 'mask', source, control: { ...d.maskParams }, overlay: null, failed: true });
makeWorker(source.blob);
}
};
const enterMaskOnSource = (source: Source) => {
teardownWorker();
set({
k: 'mask',
source,
control: { ...DEFAULT_MASK_PARAMS },
overlay: null,
failed: false,
});
makeWorker(source.blob);
void persist();
};
return {
capture,
busy,
async resumeProbe() {
if (resumed) return;
resumed = true;
const d = await drafts.loadDraft();
if (d) set({ k: 'resume', draft: d });
},
continueDraft() {
if (state.k !== 'resume') return;
void restoreFromDraft(state.draft);
},
async startFresh() {
if (state.k !== 'resume') return;
teardownWorker();
batchPhotos = [];
calibrationIndex = null;
await drafts.clearDraft();
set({ k: 'batch', photos: [] });
},
async addToBatch(file: File) {
if (state.k !== 'batch') return;
const photo: Source = { widthPx: 0, heightPx: 0, blob: file };
batchPhotos = [...state.photos, photo];
set({ k: 'batch', photos: batchPhotos });
await persist();
},
async addPhoto(file: File) {
// M2: addPhoto is an alias for addToBatch (no single-photo-only shortcut).
if (state.k !== 'batch') {
teardownWorker();
batchPhotos = [];
set({ k: 'batch', photos: [] });
}
if (state.k !== 'batch') return;
const photo: Source = { widthPx: 0, heightPx: 0, blob: file };
batchPhotos = [...state.photos, photo];
set({ k: 'batch', photos: batchPhotos });
await persist();
},
removeFromBatch(index: number) {
if (state.k !== 'batch') return;
if (index < 0 || index >= state.photos.length) return;
batchPhotos = state.photos.filter((_, i) => i !== index);
set({ k: 'batch', photos: batchPhotos });
void persist();
},
continueBatch() {
if (state.k !== 'batch' || state.photos.length === 0) return;
batchPhotos = [...state.photos];
calibrationIndex = null;
teardownWorker();
set({ k: 'pickCalibration', photos: [...batchPhotos] });
void persist();
},
// #61: enter M1 Mask on the chosen calibration Source only — never loop batch.
pickCalibration(index: number) {
if (state.k !== 'pickCalibration') return;
if (index < 0 || index >= state.photos.length) return;
batchPhotos = [...state.photos];
calibrationIndex = index;
enterMaskOnSource(batchPhotos[index]);
},
retakes() {
teardownWorker();
calibrationIndex = null;
// Return to the batch (photos kept) so the user can add/replace before continuing.
set({ k: 'batch', photos: [...batchPhotos] });
void persist();
},
setSensitivity(n: number) {
if (state.k !== 'mask') return;
const s = Math.max(0, Math.min(100, Math.round(n)));
set({ ...state, control: { ...state.control, sensitivity: s } });
schedulePass();
},
setSheet(s: Sheet) {
if (state.k !== 'mask') return;
set({ ...state, control: { ...state.control, sheet: s } });
runPass();
},
// #61: Axis uses the same calibration source; draft.batch siblings stay untouched.
approveMask() {
if (state.k !== 'mask' || !state.overlay || state.overlay.slabPx === 0) return;
teardownWorker();
const overlay = state.overlay;
const bbox = {
corners: overlay.bbox.corners.map((c) => [...c] as [number, number]),
longSidePx: overlay.bbox.longSidePx,
angleDeg: overlay.bbox.angleDeg,
};
const w = state.source.widthPx;
const h = state.source.heightPx;
const seedTilt = 90 - bbox.angleDeg;
const tape0 = defaultTape(getCropFill(), seedTilt);
const axis = axisWithTape(bbox, tape0, w, h);
set({ k: 'axis', source: state.source, approvedMask: overlay, axis });
void persist();
},
// Legacy slider path — maps to photo tilt under the fixed tape line (#99).
setAxisAngle(deg: number) {
if (state.k !== 'axis') return;
const tape = { ...state.axis.tape, tiltDeg: deg };
const axis = axisWithTape(state.axis.bbox, tape, state.source.widthPx, state.source.heightPx);
set({ ...state, axis });
void persist();
},
setTape(tape: TapeTransform) {
if (state.k !== 'axis') return;
const axis = axisWithTape(state.axis.bbox, tape, state.source.widthPx, state.source.heightPx);
set({ ...state, axis });
void persist();
},
approveAxis() {
if (state.k !== 'axis') return;
const axis = axisWithTape(
state.axis.bbox,
state.axis.tape,
state.source.widthPx,
state.source.heightPx,
);
set({
k: 'measure', source: state.source, approvedMask: state.approvedMask,
axis,
lengthEighths: 0, thicknessEighths: 0, results: null,
});
void persist();
},
setLength(e: Eighths) {
if (state.k !== 'measure') return;
set({ ...state, lengthEighths: Math.max(0, Math.round(e)) });
void persist();
},
setThickness(e: Eighths) {
if (state.k !== 'measure') return;
set({ ...state, thicknessEighths: Math.max(0, Math.round(e)) });
void persist();
},
// #62: Calculate the calibration photo, then apply its frozen mask params
// to siblings. This stays in Measure; siblings never enter Mask controls.
async calculate() {
if (state.k !== 'measure') return;
const { source, approvedMask, axis, lengthEighths, thicknessEighths } = state;
if (lengthEighths === 0 || thicknessEighths === 0) return;
const bitmap = await decodeOrientedBitmap(source.blob);
const tapeAxis = axis.tape.spanPx > 0
? (() => {
const ep = tapeEndpoints(axis.tape, source.widthPx, source.heightPx);
return { start: ep.start, ux: ep.ux, uy: ep.uy, spanPx: ep.spanPx };
})()
: null;
const base = measure(
approvedMask.bitset, source.widthPx, source.heightPx,
axis.bbox, lengthEighths, thicknessEighths, approvedMask.slabPx,
tapeAxis,
);
let pngBlob: Blob;
let belowFloor = false;
// #99: bake photo tilt into cal inventory + overlay only; siblings untouched.
let calBitmap = bitmap;
let calMask = approvedMask;
let calSource = source;
let rotatedExtra: ImageBitmap | null = null;
try {
const rotated = await rotateCalibration(bitmap, approvedMask, source, axis.tape.tiltDeg);
calBitmap = rotated.bitmap;
calMask = rotated.mask;
calSource = { ...source, widthPx: rotated.source.widthPx, heightPx: rotated.source.heightPx };
if (rotated.bitmap !== bitmap) rotatedExtra = rotated.bitmap;
} catch {
/* keep unrotated cal if bake fails */
}
try {
const r = await encodeInventory({ bitmap: calBitmap, mask: calMask, source: calSource });
pngBlob = r.blob;
belowFloor = r.sourceBelowFloor;
} catch {
// CAPTURE.md: inventory is image/png even when encode is unavailable.
pngBlob = new Blob([], { type: 'image/png' });
}
bitmap.close();
if (rotatedExtra) rotatedExtra.close();
if (pngUrl) URL.revokeObjectURL(pngUrl);
pngUrl = URL.createObjectURL(pngBlob);
// #63: second copy of the calibration inventory PNG with dimension overlays.
// Keep clean inventoryPng untouched; never stash overlay into batchApply[cal].
// Overlay uses the same baked orientation as the clean cal PNG (#99 scope).
let overlayBlob: Blob;
try {
overlayBlob = await encodeOverlay({
inventoryPng: pngBlob,
bitset: calMask.bitset,
widthPx: calSource.widthPx,
heightPx: calSource.heightPx,
bbox: calMask.bbox,
scale: base.scale,
lengthIn: eighthsToIn(lengthEighths),
lengthLabel: fmtEighths(lengthEighths),
slabBounds: calMask.slabBounds,
source: calSource,
});
} catch {
// Deterministic tests may inject a non-decodable inventory blob; still
// return a distinct image/png so callers never confuse it with clean.
overlayBlob = new Blob(['overlay'], { type: 'image/png' });
}
const results: Measures = {
...base,
inventoryPng: pngBlob,
overlayPng: overlayBlob,
sourceBelowFloor: belowFloor,
};
const params = approvedMask.params;
const calIndex = calibrationIndex != null && calibrationIndex >= 0
? calibrationIndex
: 0;
const batch = batchPhotos.length ? batchPhotos : [source];
const batchApply: BatchPhotoResult[] = batch.map((_, index) => (index === calIndex ? null : {
status: 'error',
message: 'Batch photo was not processed',
}));
// Process serially so each worker owns exactly one sibling photo and a
// failing decode/mask/encode cannot prevent later siblings from running.
for (let index = 0; index < batch.length; index++) {
if (index === calIndex) continue;
const photo = batch[index];
try {
const derived = await rederiveMaskDetailed(photo, params, {
slabPx: 0,
bbox: EMPTY_BBOX,
slabBounds: EMPTY_ABOX,
});
if (derived.mask.slabPx === 0) {
throw new Error('empty mask: no slab pixels detected');
}
const siblingSource: Source = {
...photo,
widthPx: derived.widthPx,
heightPx: derived.heightPx,
};
const siblingBitmap = await decodeOrientedBitmap(photo.blob);
let encoded: { blob: Blob; sourceBelowFloor: boolean };
try {
encoded = await encodeInventory({ bitmap: siblingBitmap, mask: derived.mask, source: siblingSource });
} finally {
siblingBitmap.close();
}
if (encoded.sourceBelowFloor) {
throw new Error('PNG size floor: shorter side is below 800 px');
}
batchApply[index] = { status: 'ok', inventoryPng: encoded.blob };
} catch (error) {
const message = error instanceof Error ? error.message : 'unable to process photo';
batchApply[index] = { status: 'error', message };
}
}
set({ ...state, results, batchApply });
void persist();
},
back() {
if (state.k === 'pickCalibration') {
calibrationIndex = null;
set({ k: 'batch', photos: [...batchPhotos] });
void persist();
} else if (state.k === 'mask') {
teardownWorker();
calibrationIndex = null;
set({ k: 'pickCalibration', photos: [...batchPhotos] });
void persist();
} else if (state.k === 'axis') {
// back to mask keeps the last approved mask as the live overlay
const m = state.approvedMask;
set({ k: 'mask', source: state.source, control: m.params, overlay: m, failed: false });
} else if (state.k === 'measure') {
set({ k: 'axis', source: state.source, approvedMask: state.approvedMask, axis: state.axis });
}
},
getInventoryUrl() {
return pngUrl;
},
dispose() {
teardownWorker();
},
};
}
// Display helper: format eighths as inches with fractions.
export function fmtEighths(e: Eighths): string {
const whole = Math.floor(eighthsToIn(e));
const rem = e - whole * 8;
if (rem === 0) return `${whole}"`;
const frac: Record<number, string> = { 1: '1/8', 2: '1/4', 3: '3/8', 4: '1/2', 5: '5/8', 6: '3/4', 7: '7/8' };
const part = frac[rem] ?? `${rem}/8`;
return whole > 0 ? `${whole} ${part}"` : `${part}"`;
}
+53
View File
@@ -0,0 +1,53 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
STAGE_H, STAGE_W, clampTickPair, containFit, defaultTape, defaultTicks,
lineX, stageToImage, tapeEndpoints,
} from './tapeGeometry.ts';
import { cropMarginFraction, DEFAULT_CROP_FILL } from '../png/crop.ts';
test('defaultTicks sit at crop insets for 90% fill', () => {
const t = defaultTicks(0.9);
const inset = STAGE_H * cropMarginFraction(0.9);
assert.ok(Math.abs(t.tickA - inset) < 1e-9);
assert.ok(Math.abs(t.tickB - (STAGE_H - inset)) < 1e-9);
});
test('lineX is the middle of the 3:4 stage', () => {
assert.equal(lineX(), STAGE_W / 2);
});
test('identity tape: stage centre maps near image centre', () => {
const imgW = 480, imgH = 640;
const tape = defaultTape(DEFAULT_CROP_FILL, 0);
const [ix, iy] = stageToImage(STAGE_W / 2, STAGE_H / 2, tape, imgW, imgH);
assert.ok(Math.abs(ix - imgW / 2) < 1e-6, `ix=${ix}`);
assert.ok(Math.abs(iy - imgH / 2) < 1e-6, `iy=${iy}`);
});
test('tapeEndpoints span equals tick distance in image px at identity (matching aspect)', () => {
const imgW = 480, imgH = 640;
const tape = defaultTape(0.9, 0);
const ep = tapeEndpoints(tape, imgW, imgH);
const { scale0 } = containFit(imgW, imgH);
const expected = Math.abs(tape.tickB - tape.tickA) / scale0;
assert.ok(Math.abs(ep.spanPx - expected) < 1e-6, `span ${ep.spanPx} vs ${expected}`);
// Direction should be +y (down the stage → down the image)
assert.ok(Math.abs(ep.ux) < 1e-6, `ux=${ep.ux}`);
assert.ok(ep.uy > 0.99, `uy=${ep.uy}`);
});
test('zoom scales inverse: 2× zoom halves image-space tick span', () => {
const imgW = 480, imgH = 640;
const base = defaultTape(0.9, 0);
const zoomed = { ...base, zoom: 2 };
const a = tapeEndpoints(base, imgW, imgH).spanPx;
const b = tapeEndpoints(zoomed, imgW, imgH).spanPx;
assert.ok(Math.abs(b * 2 - a) < 1e-6, `base=${a} zoomed=${b}`);
});
test('clampTickPair enforces gap and order', () => {
const p = clampTickPair(400, 100, 40);
assert.ok(p.tickB - p.tickA >= 40);
assert.ok(p.tickA <= p.tickB);
});
+117
View File
@@ -0,0 +1,117 @@
/**
* Pure tape-line geometry (#99).
* Fixed vertical line in the middle of a 3:4 stage; photo pans/zooms/tilts;
* ticks slide on the line; L↔px from tick distance in image space.
*/
import { cropMarginFraction, DEFAULT_CROP_FILL } from '../png/crop';
/** Logical stage matching the wireframe / inventory aspect. */
export const STAGE_W = 480;
export const STAGE_H = 640;
export interface TapeTransform {
/** Photo tilt in degrees (CSS-style; positive = clockwise). */
tiltDeg: number;
zoom: number;
/** Pan in stage units. */
panX: number;
panY: number;
/** Tick Y positions in stage units. */
tickA: number;
tickB: number;
}
export function defaultTicks(fill: number = DEFAULT_CROP_FILL): { tickA: number; tickB: number } {
const inset = STAGE_H * cropMarginFraction(fill);
return { tickA: inset, tickB: STAGE_H - inset };
}
export function defaultTape(fill: number = DEFAULT_CROP_FILL, seedTiltDeg = 0): TapeTransform {
const t = defaultTicks(fill);
return {
tiltDeg: seedTiltDeg,
zoom: 1,
panX: 0,
panY: 0,
tickA: t.tickA,
tickB: t.tickB,
};
}
const rad = (deg: number) => (deg * Math.PI) / 180;
/** Contain-fit of the photo into the stage at zoom=1, pan=0. */
export function containFit(imgW: number, imgH: number): { scale0: number; offX: number; offY: number } {
const scale0 = Math.min(STAGE_W / Math.max(imgW, 1), STAGE_H / Math.max(imgH, 1));
return {
scale0,
offX: (STAGE_W - imgW * scale0) / 2,
offY: (STAGE_H - imgH * scale0) / 2,
};
}
/**
* Map a stage point through the inverse photo transform into source image pixels.
* Transform order (forward): contain-fit → zoom+tilt about stage centre → pan.
*/
export function stageToImage(
sx: number,
sy: number,
tape: TapeTransform,
imgW: number,
imgH: number,
): [number, number] {
const cx = STAGE_W / 2;
const cy = STAGE_H / 2;
const { scale0, offX, offY } = containFit(imgW, imgH);
// undo pan
let x = sx - tape.panX - cx;
let y = sy - tape.panY - cy;
// undo rotate(tilt) scale(zoom) about centre
const z = tape.zoom || 1;
const a = -rad(tape.tiltDeg);
const cos = Math.cos(a);
const sin = Math.sin(a);
const xr = (x * cos - y * sin) / z;
const yr = (x * sin + y * cos) / z;
x = xr + cx;
y = yr + cy;
// undo contain fit
const ix = (x - offX) / scale0;
const iy = (y - offY) / scale0;
return [ix, iy];
}
export function lineX(): number {
return STAGE_W / 2;
}
/** Image-space endpoints of the two ticks + span in px. */
export function tapeEndpoints(
tape: TapeTransform,
imgW: number,
imgH: number,
): { start: [number, number]; end: [number, number]; spanPx: number; ux: number; uy: number } {
const start = stageToImage(lineX(), tape.tickA, tape, imgW, imgH);
const end = stageToImage(lineX(), tape.tickB, tape, imgW, imgH);
const dx = end[0] - start[0];
const dy = end[1] - start[1];
const spanPx = Math.hypot(dx, dy);
const len = spanPx || 1;
return { start, end, spanPx, ux: dx / len, uy: dy / len };
}
export function clampTickPair(a: number, b: number, gap = 40): { tickA: number; tickB: number } {
let tickA = a;
let tickB = b;
if (tickB < tickA) {
const t = tickA;
tickA = tickB;
tickB = t;
}
const minY = 0;
const maxY = STAGE_H;
tickA = Math.max(minY, Math.min(tickA, maxY - gap));
tickB = Math.min(maxY, Math.max(tickB, tickA + gap));
return { tickA, tickB };
}
+29
View File
@@ -0,0 +1,29 @@
/**
* Shared decode for capture photos so PhotoStage, mask worker, inventory, and
* calculate all share one EXIF-oriented pixel space.
*
* Matched-set / DJI JPEGs often carry EXIF Orientation 8 (stored landscape,
* display portrait). `new Image()` and bare `createImageBitmap(blob)` can
* disagree across browsers → pink axis arrow space ≠ mask/overlay bitset space.
*
* Contract: always request `imageOrientation: 'from-image'`. If the runtime
* rejects that option (older engines), fall back to bare `createImageBitmap`;
* engines that already apply EXIF by default still match Image/canvas size.
* Engines that never apply EXIF may still diverge after fallback — document
* that path rather than inventing a software rotate.
*/
export const ORIENTED_BITMAP_OPTIONS: ImageBitmapOptions = {
imageOrientation: 'from-image',
};
export async function decodeOrientedBitmap(blob: Blob): Promise<ImageBitmap> {
try {
return await createImageBitmap(blob, ORIENTED_BITMAP_OPTIONS);
} catch (first) {
try {
return await createImageBitmap(blob);
} catch {
throw first;
}
}
}
+25
View File
@@ -0,0 +1,25 @@
import { get, set, del } from 'idb-keyval';
import type { Draft } from '../capture/capture.types';
const KEY = 'slabuploader-draft';
export async function loadDraft(): Promise<Draft | null> {
try {
const d = await get<Draft>(KEY);
return d && d.version === 1 ? d : null;
} catch {
return null;
}
}
export async function saveDraft(d: Draft): Promise<void> {
await set(KEY, d);
}
export async function clearDraft(): Promise<void> {
try {
await del(KEY);
} catch {
// idb unavailable: draft persistence is best-effort, app state is not
}
}
+51
View File
@@ -0,0 +1,51 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { minAreaRect, orientedRect, maskSpanAlong } from './rect.ts';
import { measure } from '../measure/measure.ts';
// 100×20 horizontal slab
const W = 100, H = 20;
const bitset = new Uint8Array(W * H);
for (let x = 0; x < W; x++) for (let y = 0; y < H; y++) bitset[y * W + x] = 1;
test('maskSpanAlong of a 100×20 slab: X ≈ 99–100, Y ≈ 19–20', () => {
const sx = maskSpanAlong(bitset, W, H, 1, 0);
const sy = maskSpanAlong(bitset, W, H, 0, 1);
assert.ok(sx >= 99 && sx <= 100, `spanX ${sx}`);
assert.ok(sy >= 19 && sy <= 20, `spanY ${sy}`);
});
test('orientedRect changes angleDeg and preserves longSidePx', () => {
const prev = minAreaRect(bitset, W, H);
assert.ok(prev, 'minAreaRect returned a box');
const rotated = orientedRect(prev, 45);
assert.ok(Math.abs(rotated.angleDeg - 45) < 1e-9);
assert.equal(rotated.longSidePx, prev.longSidePx);
// The long side (angleDeg 0 → +x) must be preserved by the rotation.
const longOf = (c) => Math.hypot(c[3][0] - c[0][0], c[3][1] - c[0][1]);
assert.ok(Math.abs(longOf(rotated.corners) - longOf(prev.corners)) < 1e-9);
assert.ok(Math.abs(longOf(prev.corners) - prev.longSidePx) < 1e-9);
// angle normalization to [0, 180)
assert.ok(orientedRect(prev, 190).angleDeg >= 0 && orientedRect(prev, 190).angleDeg < 180);
});
test('measure scale uses mask span along the axis, not longSidePx', () => {
// Thin slab (20 long, 100 wide): minAreaRect's long side (angleDeg 90) is
// the 100-px dimension along +y. Rotate the axis to angleDeg 0 (+x), which
// crosses only the 20-px slab width — span 19, not longSidePx 99.
const TW = 20, TH = 100;
const thin = new Uint8Array(TW * TH).fill(1);
const thinBox = minAreaRect(thin, TW, TH);
assert.ok(thinBox && thinBox.longSidePx >= 99, `longSide ${thinBox?.longSidePx}`);
const rotated = orientedRect(thinBox, 0);
const lengthEighths = 24; // 3 in
const m = measure(thin, TW, TH, rotated, lengthEighths, 8, TW * TH);
// Scale = span along the axis / length. Span ≈ 19 → scale ≈ 6.3 px/in,
// far from the old longSidePx/3 ≈ 33.
const ex = rotated.corners[0][0] - rotated.corners[3][0];
const ey = rotated.corners[0][1] - rotated.corners[3][1];
const elen = Math.hypot(ex, ey);
const span = maskSpanAlong(thin, TW, TH, ex / elen, ey / elen);
assert.ok(Math.abs(m.scale - span / (lengthEighths / 8)) < 1e-9);
assert.ok(m.scale < 10, `scale ${m.scale} should be span-based, not ${thinBox.longSidePx / 3}`);
});
+132
View File
@@ -0,0 +1,132 @@
import type { ABox, BBox } from '../capture/capture.types';
export function slabBounds(bitset: Uint8Array, w: number, h: number): ABox {
let minX = w, minY = h, maxX = -1, maxY = -1;
for (let y = 0; y < h; y++) {
const row = y * w;
for (let x = 0; x < w; x++) {
if (bitset[row + x]) {
if (x < minX) minX = x;
if (x > maxX) maxX = x;
if (y < minY) minY = y;
if (y > maxY) maxY = y;
}
}
}
if (maxX < 0) return { x: 0, y: 0, w: 0, h: 0 };
return { x: minX, y: minY, w: maxX - minX + 1, h: maxY - minY + 1 };
}
// Convex hull (Andrew's monotone chain). Points are [x, y].
export function convexHull(pts: [number, number][]): [number, number][] {
const p = [...pts].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
if (p.length < 3) return p;
const cross = (o: [number, number], a: [number, number], b: [number, number]) =>
(a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
const lower: [number, number][] = [];
for (const pt of p) {
while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], pt) <= 0) lower.pop();
lower.push(pt);
}
const upper: [number, number][] = [];
for (let i = p.length - 1; i >= 0; i--) {
const pt = p[i];
while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], pt) <= 0) upper.pop();
upper.push(pt);
}
lower.pop();
upper.pop();
return lower.concat(upper);
}
// Rotating calipers: true minimum-area bounding rectangle of the mask.
export function minAreaRect(bitset: Uint8Array, w: number, h: number): BBox | null {
const pts: [number, number][] = [];
for (let y = 0; y < h; y++) {
const row = y * w;
for (let x = 0; x < w; x++) if (bitset[row + x]) pts.push([x, y]);
}
if (pts.length < 2) return null;
const hull = convexHull(pts);
let best = { area: Infinity, cx: 0, cy: 0, ux: 1, uy: 0, hw: 0, hh: 0 };
const n = hull.length;
for (let i = 0; i < n; i++) {
const j = (i + 1) % n;
const ex = hull[j][0] - hull[i][0];
const ey = hull[j][1] - hull[i][1];
const len = Math.hypot(ex, ey);
if (len === 0) continue;
const ux = ex / len, uy = ey / len;
const vx = -uy, vy = ux;
let minU = Infinity, maxU = -Infinity, minV = Infinity, maxV = -Infinity;
for (let k = 0; k < n; k++) {
const dx = hull[k][0] - hull[i][0];
const dy = hull[k][1] - hull[i][1];
const u = dx * ux + dy * uy;
const v = dx * vx + dy * vy;
if (u < minU) minU = u;
if (u > maxU) maxU = u;
if (v < minV) minV = v;
if (v > maxV) maxV = v;
}
const rw = maxU - minU;
const rh = maxV - minV;
const area = rw * rh;
if (area < best.area) {
best = { area, cx: hull[i][0] + (minU + maxU) / 2 * ux + (minV + maxV) / 2 * vx, cy: hull[i][1] + (minU + maxU) / 2 * uy + (minV + maxV) / 2 * vy, ux, uy, hw: rw / 2, hh: rh / 2 };
}
}
// long-side direction: the edge with the larger extent
const lu = best.hw >= best.hh ? best.ux : -best.uy;
const lv = best.hw >= best.hh ? best.uy : best.ux;
const su = best.hw >= best.hh ? -best.uy : best.ux;
const sv = best.hw >= best.hh ? -best.ux : -best.uy;
const halfL = best.hw >= best.hh ? best.hw : best.hh;
const halfS = best.hw >= best.hh ? best.hh : best.hw;
const corners: [number, number][] = [];
for (const [a, b] of [[1, 1], [1, -1], [-1, -1], [-1, 1]] as const) {
corners.push([best.cx + a * halfL * lu + b * halfS * su, best.cy + a * halfL * lv + b * halfS * sv]);
}
const angleDeg = ((Math.atan2(lv, lu) * 180) / Math.PI + 180) % 180;
return { corners, longSidePx: halfL * 2, angleDeg };
}
// Rotate the long side to a new angle, keeping center and half-extents.
// Corners are rebuilt in minAreaRect order so corners[3] → corners[0] stays the
// long side (the axis arrow direction).
export function orientedRect(prev: BBox, angleDeg: number): BBox {
const a = ((angleDeg % 180) + 180) % 180;
const halfL = prev.longSidePx / 2;
const halfS = Math.hypot(prev.corners[0][0] - prev.corners[1][0], prev.corners[0][1] - prev.corners[1][1]) / 2;
// corners[i] is a corner point: average of all four = center
const cx = (prev.corners[0][0] + prev.corners[1][0] + prev.corners[2][0] + prev.corners[3][0]) / 4;
const cy = (prev.corners[0][1] + prev.corners[1][1] + prev.corners[2][1] + prev.corners[3][1]) / 4;
const r = (a * Math.PI) / 180;
const lu = Math.cos(r), lv = Math.sin(r);
const su = -lv, sv = lu;
const corners: [number, number][] = [];
for (const [aSign, bSign] of [[1, 1], [1, -1], [-1, -1], [-1, 1]] as const) {
corners.push([cx + aSign * halfL * lu + bSign * halfS * su, cy + aSign * halfL * lv + bSign * halfS * sv]);
}
return { corners, longSidePx: prev.longSidePx, angleDeg: a };
}
// Project every slab pixel onto the unit vector (ux, uy); return max − min
// extent in px. 0 when the mask is empty.
// #99: demoted to an optional sanity check — declared L comes from tape tick
// distance in image space, not this projected guess.
export function maskSpanAlong(bitset: Uint8Array, w: number, h: number, ux: number, uy: number): number {
let min = Infinity, max = -Infinity;
for (let y = 0; y < h; y++) {
const row = y * w;
for (let x = 0; x < w; x++) {
if (!bitset[row + x]) continue;
const p = x * ux + y * uy;
if (p < min) min = p;
if (p > max) max = p;
}
}
return max < min ? 0 : max - min;
}
-121
View File
@@ -1,121 +0,0 @@
// M1-3 mask key geometry — pure functions, no canvas/DOM.
//
// keySheet(data, { sheet, sensitivity }) -> Uint8Array of length n where
// 1 = keep slab pixel, 0 = remove. maskStats(mask) -> { kept, total, empty }.
//
// Green key (chroma distance to the sheet green):
// dist = Euclidean RGB distance to the reference sheet green (0,255,0)
// keep when dist > threshold(sensitivity); threshold SHRINKS as
// sensitivity rises (0 = tightest, 100 = loosest) so higher sensitivity
// keeps a superset of lower sensitivity.
//
// Black key (brightness above a cutoff):
// keep when brightness > cutoff.
//
// Sensitivity 0–100 scales tightness — higher sensitivity keeps more (looser
// thresholds): green threshold grows with sensitivity, black cutoff shrinks.
// Auto picks whichever sheet yields more kept pixels.
/**
* Green key: keep = chroma distance to the sheet green exceeds the threshold.
* Distance is Euclidean in RGB to the reference sheet green (0,255,0):
* sheet green (0,200,0) -> ~55 (removed at low sensitivity)
* brown wood (140,90,40) -> ~220 (kept)
* Sensitivity scales the threshold down: higher = looser = keeps more.
*/
// Reference sheet green: pure keying green. Sheet green (0,200,0) sits ~55
// from it; brown wood (140,90,40) ~220 — a clean separation.
const SHEET_GREEN = { r: 0, g: 255, b: 0 };
function greenDistance(r, g, b) {
const dr = r - SHEET_GREEN.r;
const dg = g - SHEET_GREEN.g;
const db = b - SHEET_GREEN.b;
return Math.sqrt(dr * dr + dg * dg + db * db);
}
/**
* 0–100 sensitivity -> green distance threshold.
* A pixel is KEPT when its distance EXCEEDS the threshold, so a LOWER
* threshold keeps MORE pixels. Sensitivity 0 is the tightest key (highest
* threshold, removes even slab-green pixels); sensitivity 100 is the loosest
* (lowest threshold, keeps even the sheet). Monotone: higher sensitivity
* keeps a superset of lower sensitivity.
* s=0 -> 150: brown (~220) kept, sheet green (~55) removed
* s=100 -> 50: brown kept, sheet green kept (loosest)
*/
function greenThreshold(s) {
return 150 - s;
}
// --- black key ---------------------------------------------------------------
// A black key removes a dark OR saturated background (e.g. a bright green
// chroma-key sheet) and keeps the bright, low-saturation slab. Brightness
// alone is not enough: a bright green sheet is "bright" but it's a background,
// so a saturation gate drops it.
/** Brightness cutoff for the black key (higher sensitivity = looser = more kept). */
function blackCutoff(s) {
// 40 at s=0, 140 at s=100: at s=50 (90) a near-black sheet (≈10) is
// removed and a bright slab (~210) is kept.
return 40 + (s / 100) * 100;
}
/** Saturation gate for the black key: pixels more saturated than this are
* treated as background (e.g. a green keying sheet, saturation ≈ 200) and
* dropped, even if bright. The slab is low-saturation. */
const BLACK_SAT_CAP = 120;
/**
* Key the sheet out of a pixel buffer.
* @param {Uint8ClampedArray|{r,g,b,a}} data RGBA pixels, length n*4.
* @param {{sheet?:'auto'|'green'|'black', sensitivity?:number}} opts
* sensitivity is 0–100 (0 = tightest key, 100 = loosest).
* @returns {Uint8Array} length n, 1=keep slab pixel, 0=remove.
*/
export function keySheet(data, opts = {}) {
const sheet = opts.sheet ?? 'auto';
const sensitivity = Math.max(0, Math.min(100, opts.sensitivity ?? 50));
const n = data.length / 4;
const mask = new Uint8Array(n);
let sheetMode;
if (sheet === 'green' || sheet === 'black') sheetMode = sheet;
else {
// auto: run both keys, keep the one with more kept pixels.
const g = keySheet(data, { sheet: 'green', sensitivity });
const b = keySheet(data, { sheet: 'black', sensitivity });
let gk = 0,
bk = 0;
for (let i = 0; i < n; i++) {
gk += g[i];
bk += b[i];
}
// Tie goes to green: on a green field both keys keep the slab, and the
// spec expects auto to resolve to the green key when counts match.
return gk >= bk ? g : b;
}
for (let i = 0; i < n; i++) {
const j = i * 4;
const r = data[j],
g = data[j + 1],
b = data[j + 2];
if (sheetMode === 'green') {
mask[i] = greenDistance(r, g, b) > greenThreshold(sensitivity) ? 1 : 0;
} else {
const bright = 0.2126 * r + 0.7152 * g + 0.0722 * b;
const sat = Math.max(r, g, b) - Math.min(r, g, b);
mask[i] =
bright > blackCutoff(sensitivity) && sat < BLACK_SAT_CAP ? 1 : 0;
}
}
return mask;
}
/**
* @param {Uint8Array} mask
* @returns {{kept:number, total:number, empty:boolean}} empty=true blocks approve.
*/
export function maskStats(mask) {
let kept = 0;
for (let i = 0; i < mask.length; i++) kept += mask[i];
return { kept, total: mask.length, empty: kept === 0 };
}
-73
View File
@@ -1,73 +0,0 @@
// M1 deterministic measurement math — pure functions, no DOM.
//
// Ground truth: docs/CAPTURE.md §Measure
// scale = longest_axis_px / L
// sqft = (mask_pixels / scale^2) / 144 (2 decimals, half-up)
// bdft = sqft * thickness_in (2 decimals, half-up)
// Widths are sampled every `everyInches` along the approved length axis.
/** px-per-inch scale: longest axis in pixels divided by its length in inches. */
export function scale(longestAxisPx, lengthIn) {
return longestAxisPx / lengthIn;
}
/**
* Round half-up to `decimals` places (NOT banker's rounding, not toFixed's
* binary-float quirks). 30.625 -> 30.63, 1.005 -> 1.01.
*/
export function roundHalfUp(x, decimals) {
const f = Math.pow(10, decimals);
return Math.round((x + Number.EPSILON) * f) / f;
}
/**
* Square feet from mask pixel area and the px-per-inch scale.
* (mask_px / scale^2) gives square inches; /144 gives square feet.
* @returns number rounded half-up to 2 decimals.
*/
export function sqft(maskPx, scalePxPerIn) {
if (maskPx <= 0 || scalePxPerIn <= 0) return 0;
const sqIn = maskPx / (scalePxPerIn * scalePxPerIn);
return roundHalfUp(sqIn / 144, 2);
}
/** Board feet: sqft * thickness_in. 1 sqft at 1in thick = 1 bdft. */
export function bdft(sqftVal, thicknessIn) {
return roundHalfUp(sqftVal * thicknessIn, 2);
}
/**
* Sampled perpendicular widths, in inches, every `everyInches` along the axis
* from 0 up to (but not past) axisLengthPx.
* @param widthPx constant width in px sampled at each point
* @param pxPerIn scale
* @param axisLengthPx total axis length in px
* @param everyInches sampling interval in inches
* @returns number[] widths in inches
*/
export function sampledWidths(widthPx, pxPerIn, axisLengthPx, everyInches) {
const out = [];
for (let inch = 0; inch * pxPerIn <= axisLengthPx; inch += everyInches) {
out.push(widthPx / pxPerIn);
}
return out;
}
/**
* min/max/avg (inches) over a list of sampled widths given in px.
* @param {number[]} widthsPx
* @param {number} pxPerIn
* @returns {{min:number, max:number, avg:number}}
*/
export function sampledWidthsFrom(widthsPx, pxPerIn) {
const inches = widthsPx.map((w) => w / pxPerIn);
const min = Math.min(...inches);
const max = Math.max(...inches);
const avg = inches.reduce((a, b) => a + b, 0) / inches.length;
return { min, max, avg };
}
/** Snap an inch value to the nearest 1/8 inch. */
export function snapToEighth(inches) {
return Math.round(inches * 8) / 8;
}
+4
View File
@@ -0,0 +1,4 @@
import App from './App.svelte';
const root = document.querySelector('#app');
if (root) new App({ target: root });
+59
View File
@@ -0,0 +1,59 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { blackKeep, greenKeep } from './sheetKeep.ts';
// --- Black sheet sensitivity (#74) ---
test('blackKeep: s=0 keeps dark live-edge wood (lum ~25–50), removes near-black sheet (#74)', () => {
// Near-black sheet (lum ≈ 5)
assert.equal(blackKeep(5, 5, 5, 0), false, 'near-black sheet removed at s=0');
assert.equal(blackKeep(0, 0, 0, 0), false, 'pure black sheet removed at s=0');
// Dark wood / bark / live edge
assert.equal(blackKeep(25, 22, 18, 0), true, 'lum~22 dark wood kept at s=0');
assert.equal(blackKeep(40, 35, 30, 0), true, 'lum~35 dark wood kept at s=0');
assert.equal(blackKeep(50, 45, 40, 0), true, 'lum~46 wood kept at s=0');
// Mid wood always kept
assert.equal(blackKeep(120, 90, 60, 0), true, 'mid wood kept at s=0');
});
test('blackKeep: s=100 removes more than s=0; curve monotonic (#74)', () => {
// Dark-ish pixel that sits between floors: sheet at high s, wood at low s
const darkish = { r: 80, g: 70, b: 60 }; // lum ≈ 72
assert.equal(blackKeep(darkish.r, darkish.g, darkish.b, 0), true, 'lum~72 kept at s=0');
assert.equal(blackKeep(darkish.r, darkish.g, darkish.b, 100), false, 'lum~72 sheet at s=100');
// Near-black always sheet
assert.equal(blackKeep(2, 2, 2, 0), false);
assert.equal(blackKeep(2, 2, 2, 100), false);
// Bright wood always kept
assert.equal(blackKeep(200, 160, 100, 0), true);
assert.equal(blackKeep(200, 160, 100, 100), true);
// Monotonic: once removed, stays removed as s increases
const probe = { r: 60, g: 55, b: 50 }; // lum ≈ 56
let prevKeep = true;
for (const s of [0, 25, 50, 75, 100]) {
const keep = blackKeep(probe.r, probe.g, probe.b, s);
if (!prevKeep) assert.equal(keep, false, `once sheet at lower s, stay sheet at s=${s}`);
prevKeep = keep;
}
// s=100 must remove more pixels than s=0 for this mid-dark band
assert.equal(blackKeep(probe.r, probe.g, probe.b, 0), true);
assert.equal(blackKeep(probe.r, probe.g, probe.b, 100), false);
});
test('greenKeep: s=0 does not eat mild wood greens; s=100 widens sheet match (#74)', () => {
// Neutral wood
assert.equal(greenKeep(140, 110, 80, 0), true);
assert.equal(greenKeep(140, 110, 80, 100), true);
// Mild green cast on wood (g - max(r,b) ≈ 5) — must keep at s=0
assert.equal(greenKeep(100, 105, 90, 0), true, 'mild green cast kept at s=0');
// Strong sheet green
assert.equal(greenKeep(40, 180, 50, 0), false, 'strong green sheet removed at s=0');
assert.equal(greenKeep(40, 180, 50, 100), false);
// Borderline: g-max ≈ 20 — may become sheet at high s
const border = { r: 80, g: 100, b: 70 }; // delta = 20
assert.equal(greenKeep(border.r, border.g, border.b, 0), true, 'delta20 kept at s=0');
assert.equal(greenKeep(border.r, border.g, border.b, 100), false, 'delta20 sheet at s=100');
});
+36
View File
@@ -0,0 +1,36 @@
/**
* Sheet-vs-slab keep predicates used by the mask worker.
* Extracted so unit tests can cover sensitivity curves without the worker.
*/
/** Keep slab pixels on a green sheet. Higher s widens the green-sheet match. */
export function greenKeep(r: number, g: number, b: number, s: number): boolean {
const t = Math.max(0, Math.min(100, s)) / 100;
// s=0 removes only strongly green sheet; increasing sensitivity widens the match.
const isSheet = g - Math.max(r, b) > 45 - 35 * t;
return !isSheet;
}
/**
* Keep slab pixels on a black sheet.
* At s=0 only near-black sheet (very low lum) is removed so dark live-edge /
* bark (lum ~25–50) stays. The match widens monotonically toward s=100 (#74).
*/
export function blackKeep(r: number, g: number, b: number, s: number): boolean {
const t = Math.max(0, Math.min(100, s)) / 100;
const lum = 0.2126 * r + 0.7152 * g + 0.0722 * b;
// Old floor 40 ate dark wood at minimum sensitivity. Floor 10 isolates near-black.
const isSheet = lum < 10 + 140 * t;
return !isSheet;
}
export function autoPickSheet(
samples: { r: number; g: number; b: number }[],
): 'green' | 'black' {
let gr = 0, gl = 0;
for (const p of samples) {
if (p.g - Math.max(p.r, p.b) > 20) gr++;
if (0.2126 * p.r + 0.7152 * p.g + 0.0722 * p.b < 70) gl++;
}
return gl >= gr ? 'black' : 'green';
}
+18
View File
@@ -0,0 +1,18 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { minAreaRect, orientedRect } from '../geometry/rect.ts';
import { measure } from './measure.ts';
test('#99: tape span drives scale, not maskSpanAlong', () => {
const W = 480, H = 100;
const bitset = new Uint8Array(W * H).fill(1);
const box = orientedRect(minAreaRect(bitset, W, H)!, 0);
// Declared L = 48"; tape span = 240 px → 5 px/in (not the full 480 mask span).
const m = measure(bitset, W, H, box, 384, 8, W * H, {
start: [100, 50],
ux: 1,
uy: 0,
spanPx: 240,
});
assert.ok(Math.abs(m.scale - 5) < 1e-9, `scale ${m.scale}`);
});
+58
View File
@@ -0,0 +1,58 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { minAreaRect, orientedRect } from '../geometry/rect.ts';
import { measure } from './measure.ts';
// Uniform slab, 10 px/in: full W×H canvas, axis +x via minAreaRect angle 0.
function uniform(W: number, H: number) {
const bitset = new Uint8Array(W * H).fill(1);
const box = orientedRect(minAreaRect(bitset, W, H)!, 0);
return { bitset, box, slabPx: W * H };
}
// Tapered slab, 10 px/in: tips under 6 in and past L−6 in are `tipPx` wide
// (centered), the bulk is full canvas height.
function tapered(W: number, H: number, lengthEighths: number, tipPx: number) {
const inIn = W / 10;
const bitset = new Uint8Array(W * H);
const half = tipPx / 2;
for (let x = 0; x < W; x++) {
const p = x / 10;
const w = p < 6 || p > inIn - 6 ? tipPx : H;
for (let y = (H - w) / 2; y < (H + w) / 2; y++) bitset[y * W + x] = 1;
}
const box = orientedRect(minAreaRect(bitset, W, H)!, 0);
return { bitset, box, slabPx: bitset.reduce((a, c) => a + c, 0), lengthEighths };
}
test('tapered 48in slab: min/max reflect the bulk, tips excluded', () => {
const { bitset, box, slabPx, lengthEighths } = tapered(480, 100, 384, 20); // tips 2in, bulk 10in
const m = measure(bitset, 480, 100, box, lengthEighths, 8, slabPx);
assert.equal(m.widths.every6In.length, 7, `stations ${m.widths.every6In.length} (6..42in)`);
for (const e of m.widths.every6In) {
assert.ok(e >= 76 && e <= 84, `station ${e / 8}in must be bulk (~10in)`);
}
assert.ok(m.widths.min >= 76 && m.widths.min <= 84, `min ${m.widths.min / 8}in`);
assert.ok(m.widths.max >= 76 && m.widths.max <= 84, `max ${m.widths.max / 8}in`);
});
test('uniform slab: interior stations keep the full width', () => {
const { bitset, box, slabPx } = uniform(480, 100);
const m = measure(bitset, 480, 100, box, 384, 8, slabPx);
assert.equal(m.widths.every6In.length, 7);
for (const e of m.widths.every6In) {
assert.ok(e >= 72 && e <= 104, `station ${e / 8}in`);
}
});
test('short slab L=12in: no interior stations, widths are empty/zero', () => {
const { bitset, box, slabPx } = uniform(120, 100);
const m = measure(bitset, 120, 100, box, 96, 8, slabPx);
assert.deepEqual(m.widths, { min: 0, max: 0, avg: 0, every6In: [] });
});
test('short slab L=10in: no interior stations, widths are empty/zero', () => {
const { bitset, box, slabPx } = uniform(100, 100);
const m = measure(bitset, 100, 100, box, 80, 8, slabPx);
assert.deepEqual(m.widths, { min: 0, max: 0, avg: 0, every6In: [] });
});
+137
View File
@@ -0,0 +1,137 @@
import type { BBox, Eighths, Measures, Widths } from '../capture/capture.types';
import { maskSpanAlong } from '../geometry/rect';
// Display / direction: corners[3] → corners[0] is the long side.
export function axisEndpoints(bbox: BBox): { start: [number, number]; end: [number, number] } {
return { start: bbox.corners[3], end: bbox.corners[0] };
}
export interface TapeAxis {
/** Image-space tape start (tick A). */
start: [number, number];
/** Image-space unit direction tick A → tick B. */
ux: number;
uy: number;
/** Image-space distance between ticks (owner-declared tape length in px). */
spanPx: number;
}
// Wood end→end along the approved direction: project the mask onto the axis
// unit vector and return the segment on the bbox center line covering that span.
// Used only as a fallback / sanity path when no tape ticks are present (#99).
function maskAxisSegment(
bitset: Uint8Array, w: number, h: number, bbox: BBox,
): { start: [number, number]; end: [number, number]; span: number; ux: number; uy: number } {
const { start: a, end: b } = axisEndpoints(bbox);
const ex = b[0] - a[0];
const ey = b[1] - a[1];
const elen = Math.hypot(ex, ey) || 1;
const ux = ex / elen, uy = ey / elen;
const cx = (bbox.corners[0][0] + bbox.corners[1][0] + bbox.corners[2][0] + bbox.corners[3][0]) / 4;
const cy = (bbox.corners[0][1] + bbox.corners[1][1] + bbox.corners[2][1] + bbox.corners[3][1]) / 4;
const cProj = cx * ux + cy * uy;
let minP = Infinity, maxP = -Infinity;
for (let y = 0; y < h; y++) {
const row = y * w;
for (let x = 0; x < w; x++) {
if (!bitset[row + x]) continue;
const p = x * ux + y * uy;
if (p < minP) minP = p;
if (p > maxP) maxP = p;
}
}
if (maxP < minP) return { start: a, end: b, span: 0, ux, uy };
return {
start: [cx + ux * (minP - cProj), cy + uy * (minP - cProj)],
end: [cx + ux * (maxP - cProj), cy + uy * (maxP - cProj)],
span: maxP - minP,
ux,
uy,
};
}
// Interior stations only (6, 12, … ≤ L−6). Tips dominate min/max on tapered
// boards, so the first and last 6 in are excluded. Each sample is a line
// probe perpendicular to the axis; width = max run of slab px across it.
function sampleWidths(
bitset: Uint8Array, w: number, h: number,
start: [number, number], ux: number, uy: number,
scale: number, lengthIn: number,
): Eighths[] {
const px = -uy, py = ux;
const stepPx = 6 * scale;
const lastPx = lengthIn > 12 ? Math.floor((lengthIn - 6) / 6) * 6 * scale : 0;
const out: Eighths[] = [];
const half = Math.max(2, Math.round((w + h) / 4));
for (let t = stepPx; t <= lastPx; t += stepPx) {
const ox = start[0] + ux * t;
const oy = start[1] + uy * t;
let minP = Infinity, maxP = -Infinity;
for (let p = -half; p <= half; p++) {
const x = Math.round(ox + px * p);
const y = Math.round(oy + py * p);
if (x < 0 || y < 0 || x >= w || y >= h) continue;
if (bitset[y * w + x]) {
if (p < minP) minP = p;
if (p > maxP) maxP = p;
}
}
if (maxP < minP) continue;
out.push(Math.max(0, Math.round(((maxP - minP + 1) / scale) * 8)));
}
return out;
}
/**
* @param tape When provided (#99), scale = tape.spanPx / L and width stations
* walk from the tick-A endpoint. maskSpanAlong is not used for scale.
*/
export function measure(
bitset: Uint8Array, w: number, h: number,
bbox: BBox,
lengthEighths: Eighths, thicknessEighths: Eighths,
slabPx: number,
tape?: TapeAxis | null,
): Omit<Measures, 'inventoryPng'> & { sourceBelowFloor: false } {
const lengthIn = lengthEighths / 8;
let start: [number, number];
let ux: number;
let uy: number;
let scale: number;
if (tape && tape.spanPx > 0 && lengthIn > 0) {
start = tape.start;
ux = tape.ux;
uy = tape.uy;
scale = tape.spanPx / lengthIn;
// Optional sanity: maskSpanAlong remains available for diagnostics only.
void maskSpanAlong;
} else {
const seg = maskAxisSegment(bitset, w, h, bbox);
// Legacy fallback when no tape ticks — keep prior behaviour for old drafts.
scale = (seg.span || maskSpanAlong(bitset, w, h, seg.ux, seg.uy)) / lengthIn;
start = seg.start;
ux = seg.ux;
uy = seg.uy;
}
const every6In = sampleWidths(bitset, w, h, start, ux, uy, scale, lengthIn);
let widths: Widths;
if (every6In.length > 0) {
const min = Math.min(...every6In);
const max = Math.max(...every6In);
const avg = Math.round(every6In.reduce((a, b) => a + b, 0) / every6In.length);
widths = { min, max, avg, every6In };
} else {
widths = { min: 0, max: 0, avg: 0, every6In: [] };
}
const sqft = Math.round((slabPx / (scale * scale) / 144) * 100) / 100;
const bdft = Math.round(sqft * (thicknessEighths / 8) * 100) / 100;
return {
lengthEighths,
thicknessEighths,
scale,
widths,
sqft,
bdft,
sourceBelowFloor: false,
};
}
+56
View File
@@ -0,0 +1,56 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { clampFill, cropMarginFraction, DEFAULT_CROP_FILL } from './crop.ts';
import { _resetCropFillForTests, getCropFill, setCropFill } from '../settings/cropFill.ts';
import { composeInventoryFrame } from './inventory.ts';
import type { Mask, Source } from '../capture/capture.types';
test('cropMarginFraction(0.9) === 0.05', () => {
assert.ok(Math.abs(cropMarginFraction(0.9) - 0.05) < 1e-12);
assert.equal(DEFAULT_CROP_FILL, 0.9);
});
test('clampFill bounds', () => {
assert.equal(clampFill(2), 1);
assert.equal(clampFill(0), 0.01);
assert.equal(clampFill(Number.NaN), DEFAULT_CROP_FILL);
});
test('getCropFill / setCropFill round-trip', () => {
_resetCropFillForTests();
assert.equal(getCropFill(), 0.9);
assert.equal(setCropFill(0.8), 0.8);
assert.equal(getCropFill(), 0.8);
_resetCropFillForTests();
});
test('composeInventoryFrame honors cropFill override (#99)', () => {
const source: Pick<Source, 'widthPx' | 'heightPx'> = { widthPx: 2200, heightPx: 2000 };
const bitset = new Uint8Array(source.widthPx * source.heightPx).fill(1);
const mask: Pick<Mask, 'bitset' | 'slabBounds'> = {
bitset,
slabBounds: { x: 100, y: 100, w: 2000, h: 1800 },
};
const rgba = new Uint8Array(source.widthPx * source.heightPx * 4).fill(255);
const out90 = composeInventoryFrame({ width: 2200, height: 2000, rgba }, mask, source, 0.9);
const out80 = composeInventoryFrame({ width: 2200, height: 2000, rgba }, mask, source, 0.8);
const extent = (frame: typeof out90) => {
let minx = frame.width, maxx = -1, miny = frame.height, maxy = -1;
for (let y = 0; y < frame.height; y++) {
for (let x = 0; x < frame.width; x++) {
if (frame.rgba[(y * frame.width + x) * 4 + 3] !== 0) {
if (x < minx) minx = x;
if (x > maxx) maxx = x;
if (y < miny) miny = y;
if (y > maxy) maxy = y;
}
}
}
return { w: maxx - minx + 1, h: maxy - miny + 1 };
};
const e90 = extent(out90);
const e80 = extent(out80);
assert.ok(Math.abs(e90.w - 720) <= 2, `90% w ${e90.w}`);
assert.ok(e80.w < e90.w - 10, `80% fill must be tighter than 90%: ${e80.w} vs ${e90.w}`);
});
+21
View File
@@ -0,0 +1,21 @@
/**
* Shared crop constants for inventory PNG + tape-line overlay (#99).
* Aspect is fixed 3:4; crop_fill only moves the long-axis end insets.
* Non-retro: changing fill affects future encodes only, not stored PNGs.
*/
export const DEFAULT_CROP_FILL = 0.9;
export const ASPECT_W = 3;
export const ASPECT_H = 4;
export const FLOOR_PX = 800;
/** Fraction of the long axis inset from each end: (1 - fill) / 2. */
export function cropMarginFraction(fill: number = DEFAULT_CROP_FILL): number {
const f = clampFill(fill);
return (1 - f) / 2;
}
export function clampFill(fill: number): number {
if (!Number.isFinite(fill)) return DEFAULT_CROP_FILL;
return Math.min(1, Math.max(0.01, fill));
}
+120
View File
@@ -0,0 +1,120 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { composeInventoryFrame } from './inventory.ts';
import type { Mask, Source } from '../capture/capture.types';
const makePhoto = (w: number, h: number, paint: (x: number, y: number) => [number, number, number, number] = () => [255, 255, 255, 255]) => {
const rgba = new Uint8Array(w * h * 4);
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
const [r, g, b, a] = paint(x, y);
const i = (y * w + x) * 4;
rgba[i] = r; rgba[i + 1] = g; rgba[i + 2] = b; rgba[i + 3] = a;
}
}
return { width: w, height: h, rgba };
};
const maskFor = (bitset: Uint8Array, slabBounds: Mask['slabBounds']): Pick<Mask, 'bitset' | 'slabBounds'> =>
({ bitset, slabBounds });
test('alpha contract: removed pixels transparent, slab pixels keep RGB at alpha 255', () => {
const W = 8, H = 8;
const bitset = new Uint8Array(W * H);
for (let y = 1; y < 5; y++) for (let x = 1; x < 5; x++) bitset[y * W + x] = 1;
bitset[2 * W + 2] = 0;
bitset[3 * W + 3] = 0;
const SLAB = [200, 40, 20, 255] as const;
const SHEET = [0, 255, 0, 255] as const;
const src = makePhoto(W, H, (x, y) => (bitset[y * W + x] === 1 ? [...SLAB] : [...SHEET]));
const source: Pick<Source, 'widthPx' | 'heightPx'> = { widthPx: W, heightPx: H };
const out = composeInventoryFrame(src, maskFor(bitset, { x: 1, y: 1, w: 4, h: 4 }), source);
assert.equal(out.sourceBelowFloor, true);
assert.ok(out.width < 800, 'undersized source stays small');
let opaque = 0;
let transparent = 0;
for (let i = 0; i < out.rgba.length; i += 4) {
const a = out.rgba[i + 3];
if (a === 0) {
transparent++;
assert.equal(out.rgba[i], 0);
assert.equal(out.rgba[i + 1], 0);
assert.equal(out.rgba[i + 2], 0);
continue;
}
opaque++;
assert.equal(a, 255);
assert.deepEqual(
[out.rgba[i], out.rgba[i + 1], out.rgba[i + 2]],
[SLAB[0], SLAB[1], SLAB[2]],
'opaque pixel must be slab RGB, never sheet green',
);
}
assert.ok(opaque > 0, 'slab pixels are opaque');
assert.ok(transparent > 0, 'frame has transparent pixels');
});
test('3:4 frame at the floor: 2000×1800 slab → 800×1067', () => {
const source: Pick<Source, 'widthPx' | 'heightPx'> = { widthPx: 2000, heightPx: 1800 };
const bitset = new Uint8Array(source.widthPx * source.heightPx).fill(1);
const out = composeInventoryFrame(
makePhoto(source.widthPx, source.heightPx),
maskFor(bitset, { x: 0, y: 0, w: 2000, h: 1800 }),
source,
);
assert.equal(out.width, 800);
assert.equal(out.height, 1067);
assert.equal(out.sourceBelowFloor, false);
});
test('mid-band: slab shorter side 1000 (between 800 and 1600) → at floor, not belowFloor', () => {
const source: Pick<Source, 'widthPx' | 'heightPx'> = { widthPx: 1200, heightPx: 1000 };
const bitset = new Uint8Array(source.widthPx * source.heightPx).fill(1);
const out = composeInventoryFrame(
makePhoto(source.widthPx, source.heightPx),
maskFor(bitset, { x: 0, y: 0, w: 1200, h: 1000 }),
source,
);
assert.equal(out.sourceBelowFloor, false);
assert.equal(out.width, 800);
});
test('90% fill: limiting dimension maps to 0.9 × frame side (#68)', () => {
const source: Pick<Source, 'widthPx' | 'heightPx'> = { widthPx: 2200, heightPx: 2000 };
const bitset = new Uint8Array(source.widthPx * source.heightPx).fill(1);
const out = composeInventoryFrame(
makePhoto(source.widthPx, source.heightPx),
maskFor(bitset, { x: 100, y: 100, w: 2000, h: 1800 }),
source,
);
let minx = out.width, maxx = -1, miny = out.height, maxy = -1;
for (let y = 0; y < out.height; y++) {
for (let x = 0; x < out.width; x++) {
if (out.rgba[(y * out.width + x) * 4 + 3] !== 0) {
if (x < minx) minx = x;
if (x > maxx) maxx = x;
if (y < miny) miny = y;
if (y > maxy) maxy = y;
}
}
}
const xs = [minx, maxx], ys = [miny, maxy];
const wExtent = Math.max(...xs) - Math.min(...xs) + 1;
const hExtent = Math.max(...ys) - Math.min(...ys) + 1;
assert.ok(Math.abs(wExtent - 720) <= 2, `width extent ${wExtent} ≈ 720 (0.9×800)`);
assert.ok(Math.abs(hExtent - 648) <= 2, `height extent ${hExtent} ≈ 648 (1800×0.36)`);
});
test('belowFloor: small slab → sourceBelowFloor, outW capped at floor (no upscale)', () => {
const source: Pick<Source, 'widthPx' | 'heightPx'> = { widthPx: 900, heightPx: 700 };
const bitset = new Uint8Array(source.widthPx * source.heightPx).fill(1);
const out = composeInventoryFrame(
makePhoto(source.widthPx, source.heightPx),
maskFor(bitset, { x: 0, y: 0, w: 900, h: 700 }),
source,
);
assert.equal(out.sourceBelowFloor, true);
assert.ok(out.width <= 800, `outW ${out.width} must not exceed the floor`);
});
+86
View File
@@ -0,0 +1,86 @@
import type { Mask, Source } from '../capture/capture.types';
import { ASPECT_H, ASPECT_W, FLOOR_PX, DEFAULT_CROP_FILL } from './crop';
import { getCropFill } from '../settings/cropFill';
type MaskPick = Pick<Mask, 'bitset' | 'slabBounds'>;
type SourcePick = Pick<Source, 'widthPx' | 'heightPx'>;
export interface InventoryInput {
bitmap: ImageBitmap;
mask: MaskPick;
source: SourcePick;
/** Optional override; default reads live settings.crop_fill (#99). */
cropFill?: number;
}
export interface Frame {
width: number;
height: number;
rgba: Uint8Array;
sourceBelowFloor: boolean;
}
export function composeInventoryFrame(
src: { width: number; height: number; rgba: Uint8Array | Uint8ClampedArray },
mask: MaskPick,
source: SourcePick,
cropFill: number = DEFAULT_CROP_FILL,
): Frame {
const FILL = cropFill > 0 && cropFill <= 1 ? cropFill : DEFAULT_CROP_FILL;
const b = mask.slabBounds;
const x0 = Math.max(0, Math.round(b.x));
const y0 = Math.max(0, Math.round(b.y));
const x1 = Math.min(source.widthPx, x0 + Math.max(1, Math.round(b.w)));
const y1 = Math.min(source.heightPx, y0 + Math.max(1, Math.round(b.h)));
const sw = x1 - x0;
const sh = y1 - y0;
const belowFloor = Math.min(sw, sh) < FLOOR_PX;
const outW = belowFloor ? Math.max(1, Math.min(sw, FLOOR_PX)) : FLOOR_PX;
const outH = Math.round((outW * ASPECT_H) / ASPECT_W);
const scale = Math.min((outW * FILL) / Math.max(sw, 1), (outH * FILL) / Math.max(sh, 1));
const dw = sw * scale;
const dh = sh * scale;
const ox = (outW - dw) / 2;
const oy = (outH - dh) / 2;
const rgba = new Uint8Array(outW * outH * 4);
const xEnd = ox + dw;
const yEnd = oy + dh;
for (let y = 0; y < outH; y++) {
for (let x = 0; x < outW; x++) {
if (x + 0.5 < ox || x + 0.5 >= xEnd || y + 0.5 < oy || y + 0.5 >= yEnd) continue;
const sx = Math.floor(x0 + (x + 0.5 - ox) / scale);
const sy = Math.floor(y0 + (y + 0.5 - oy) / scale);
if (sx < 0 || sy < 0 || sx >= source.widthPx || sy >= source.heightPx) continue;
if (mask.bitset[sy * source.widthPx + sx] !== 1) continue;
const si = (sy * source.widthPx + sx) * 4;
const di = (y * outW + x) * 4;
rgba[di] = src.rgba[si];
rgba[di + 1] = src.rgba[si + 1];
rgba[di + 2] = src.rgba[si + 2];
rgba[di + 3] = 255;
}
}
return { width: outW, height: outH, rgba, sourceBelowFloor: belowFloor };
}
export async function encodeInventoryPng(
input: InventoryInput,
): Promise<{ blob: Blob; sourceBelowFloor: boolean }> {
const { bitmap, mask, source } = input;
const fill = input.cropFill ?? getCropFill();
const srcCanvas = new OffscreenCanvas(source.widthPx, source.heightPx);
const srcCtx = srcCanvas.getContext('2d')!;
srcCtx.drawImage(bitmap, 0, 0);
const img = srcCtx.getImageData(0, 0, source.widthPx, source.heightPx);
const frame = composeInventoryFrame(
{ width: source.widthPx, height: source.heightPx, rgba: img.data },
mask,
source,
fill,
);
const out = new OffscreenCanvas(frame.width, frame.height);
const ctx = out.getContext('2d')!;
ctx.putImageData(new ImageData(new Uint8ClampedArray(frame.rgba), frame.width, frame.height), 0, 0);
const blob = await out.convertToBlob({ type: 'image/png' });
return { blob, sourceBelowFloor: frame.sourceBelowFloor };
}
File diff suppressed because it is too large Load Diff
+558
View File
@@ -0,0 +1,558 @@
import type { BBox } from '../capture/capture.types';
import { axisEndpoints } from '../measure/measure';
import { DEFAULT_CROP_FILL } from './crop';
import { getCropFill } from '../settings/cropFill';
/** Width overlay stations: 6, 18, 30, … while ≤ L − 6 (tip-exclusive). */
export function overlayWidthStations(lengthIn: number): number[] {
if (!(lengthIn > 6)) return [];
const last = lengthIn - 6;
const out: number[] = [];
for (let s = 6; s <= last + 1e-9; s += 12) out.push(s);
return out;
}
/**
* EXIF-oriented photo bottom = larger canvas y. Pick which end of the approved
* length-axis span is bottom; return unit from bottom → top along that span.
*/
export function pickBottomTowardTop(
start: [number, number],
end: [number, number],
): {
bottom: [number, number];
top: [number, number];
ux: number;
uy: number;
} {
// Larger y is closer to the bottom edge of the oriented photo.
// Tie-break (horizontal axis): prefer larger x for a stable choice.
const startIsBottom =
start[1] > end[1] || (start[1] === end[1] && start[0] >= end[0]);
const bottom = startIsBottom ? start : end;
const top = startIsBottom ? end : start;
const dx = top[0] - bottom[0];
const dy = top[1] - bottom[1];
const len = Math.hypot(dx, dy) || 1;
return { bottom, top, ux: dx / len, uy: dy / len };
}
/** Screen-space right of travel (canvas y↓): (ux,uy) → (−uy, ux). */
export function axisRightUnit(ux: number, uy: number): [number, number] {
return [-uy, ux];
}
export interface OverlayWidthMark {
stationIn: number;
center: [number, number];
left: [number, number];
right: [number, number];
/** Axis-left of the slab, outside the wood edge (opposite of length dim). */
labelAt: [number, number];
label: string;
}
export interface OverlayLengthMark {
a: [number, number];
b: [number, number];
labelAt: [number, number];
label: string;
/** True when the line sits outside the slab on the axis-right side. */
outside: boolean;
}
export interface OverlayPlan {
stroke: '#000000';
fontPx: number;
length: OverlayLengthMark;
widths: OverlayWidthMark[];
}
export interface OverlayPlanInput {
bitset: Uint8Array;
widthPx: number;
heightPx: number;
bbox: BBox;
scale: number; // px per inch
lengthIn: number;
lengthLabel: string;
/** Map source px → inventory PNG px (identity OK for unit tests). */
mapToInventory: (x: number, y: number) => [number, number];
/** Inventory PNG size — used to clamp labels inside the canvas (#71). */
inventoryW?: number;
inventoryH?: number;
}
/**
* Min across-width as a fraction of max bin width for an axial bin to count as a tip.
* #71 used 0.32 (excluded fringe); #73 lowers to ~0.15 so tapered live-edge tips remain.
*/
export const BODY_WIDTH_FRAC = 0.15;
/**
* Axial tip stations from across-width profile (#71/#73).
* Thin fringe whiskers past the visible body survive 0.2%/99.8% percentiles;
* require each axial bin to have meaningful across-width before it counts as a tip.
*/
function widthAwareAxialRange(
bitset: Uint8Array,
w: number,
h: number,
ux: number,
uy: number,
cx: number,
cy: number,
): { minP: number; maxP: number } | null {
const rx = -uy;
const ry = ux;
const binMin = new Map<number, number>();
const binMax = new Map<number, number>();
for (let y = 0; y < h; y++) {
const row = y * w;
for (let x = 0; x < w; x++) {
if (!bitset[row + x]) continue;
const along = x * ux + y * uy;
const across = (x - cx) * rx + (y - cy) * ry;
const bin = Math.round(along);
const prevMin = binMin.get(bin);
if (prevMin === undefined || across < prevMin) binMin.set(bin, across);
const prevMax = binMax.get(bin);
if (prevMax === undefined || across > prevMax) binMax.set(bin, across);
}
}
if (binMin.size === 0) return null;
let maxWidth = 0;
const widths: { bin: number; width: number }[] = [];
for (const [bin, minA] of binMin) {
const maxA = binMax.get(bin)!;
const width = maxA - minA + 1;
widths.push({ bin, width });
if (width > maxWidth) maxWidth = width;
}
// Body threshold: exclude ~10% fringe whiskers; keep 15–25% live-edge tapers (#73).
const thresh = Math.max(4, maxWidth * BODY_WIDTH_FRAC);
widths.sort((a, b) => a.bin - b.bin);
let tipLo = widths[0].bin;
let tipHi = widths[widths.length - 1].bin;
for (const { bin, width } of widths) {
if (width >= thresh) {
tipLo = bin;
break;
}
}
for (let i = widths.length - 1; i >= 0; i--) {
if (widths[i].width >= thresh) {
tipHi = widths[i].bin;
break;
}
}
return { minP: tipLo, maxP: tipHi };
}
function maskAxisSegment(
bitset: Uint8Array,
w: number,
h: number,
bbox: BBox,
): {
start: [number, number];
end: [number, number];
/** Actual mask pixels nearest centerline at tip stations. */
tipStart: [number, number];
tipEnd: [number, number];
ux: number;
uy: number;
span: number;
} {
const { start: a, end: b } = axisEndpoints(bbox);
const ex = b[0] - a[0];
const ey = b[1] - a[1];
const elen = Math.hypot(ex, ey) || 1;
const ux = ex / elen;
const uy = ey / elen;
const cx = (bbox.corners[0][0] + bbox.corners[1][0] + bbox.corners[2][0] + bbox.corners[3][0]) / 4;
const cy = (bbox.corners[0][1] + bbox.corners[1][1] + bbox.corners[2][1] + bbox.corners[3][1]) / 4;
const cProj = cx * ux + cy * uy;
// Width-aware tips (#71). Fall back to robust percentiles if profile empty.
const aware = widthAwareAxialRange(bitset, w, h, ux, uy, cx, cy);
let minP: number;
let maxP: number;
if (aware) {
minP = aware.minP;
maxP = aware.maxP;
} else {
const projs: number[] = [];
for (let y = 0; y < h; y++) {
const row = y * w;
for (let x = 0; x < w; x++) {
if (!bitset[row + x]) continue;
projs.push(x * ux + y * uy);
}
}
if (projs.length === 0) return { start: a, end: b, tipStart: a, tipEnd: b, ux, uy, span: 0 };
projs.sort((p, q) => p - q);
const lo = Math.min(Math.floor(projs.length * 0.002), projs.length - 1);
const hi = Math.max(Math.floor(projs.length * 0.998), lo);
minP = projs[lo];
maxP = projs[hi];
}
// Tip pixel at each station: closest to the axis centerline
let tipStart: [number, number] = a;
let tipEnd: [number, number] = b;
let bestStart = Infinity;
let bestEnd = Infinity;
const stationTol = Math.max(1.5, (maxP - minP) * 0.001);
for (let y = 0; y < h; y++) {
const row = y * w;
for (let x = 0; x < w; x++) {
if (!bitset[row + x]) continue;
const p = x * ux + y * uy;
const across = Math.abs((x - cx) * -uy + (y - cy) * ux);
if (Math.abs(p - minP) <= stationTol && across < bestStart) {
bestStart = across;
tipStart = [x, y];
}
if (Math.abs(p - maxP) <= stationTol && across < bestEnd) {
bestEnd = across;
tipEnd = [x, y];
}
}
}
return {
start: [cx + ux * (minP - cProj), cy + uy * (minP - cProj)],
end: [cx + ux * (maxP - cProj), cy + uy * (maxP - cProj)],
tipStart,
tipEnd,
ux,
uy,
span: maxP - minP,
};
}
/** Probe slab extent along unit (px, py) through origin (ox, oy). */
function probeExtent(
bitset: Uint8Array,
w: number,
h: number,
ox: number,
oy: number,
px: number,
py: number,
): { minP: number; maxP: number } | null {
const half = Math.max(2, Math.round((w + h) / 4));
let minP = Infinity;
let maxP = -Infinity;
for (let p = -half; p <= half; p++) {
const x = Math.round(ox + px * p);
const y = Math.round(oy + py * p);
if (x < 0 || y < 0 || x >= w || y >= h) continue;
if (bitset[y * w + x]) {
if (p < minP) minP = p;
if (p > maxP) maxP = p;
}
}
if (maxP < minP) return null;
return { minP, maxP };
}
function fmtInches(inches: number): string {
const whole = Math.floor(inches + 1e-9);
const rem = Math.round((inches - whole) * 8);
if (rem === 0) return `${whole}"`;
const frac: Record<number, string> = {
1: '1/8', 2: '1/4', 3: '3/8', 4: '1/2', 5: '5/8', 6: '3/4', 7: '7/8', 8: '',
};
if (rem === 8) return `${whole + 1}"`;
const part = frac[rem] ?? `${rem}/8`;
return whole > 0 ? `${whole} ${part}"` : `${part}"`;
}
export function buildOverlayPlan(input: OverlayPlanInput): OverlayPlan {
const {
bitset, widthPx, heightPx, bbox, scale, lengthIn, lengthLabel, mapToInventory,
inventoryW, inventoryH,
} = input;
const seg = maskAxisSegment(bitset, widthPx, heightPx, bbox);
// Re-orient span ends so EXIF bottom establishes station origin; axis still
// lies on the approved M1 direction (seg ux/uy flipped only when needed).
const oriented = pickBottomTowardTop(seg.start, seg.end);
const { bottom, top, ux, uy } = oriented;
const [rx, ry] = axisRightUnit(ux, uy);
// Consistent smaller readable font (#71). Cap by inventory size so high
// source scale (≈80 px/in) does not dominate the PNG.
const fontPx = (() => {
if (inventoryW && inventoryH && inventoryW > 0 && inventoryH > 0) {
return Math.max(12, Math.min(16, Math.round(Math.min(inventoryW, inventoryH) * 0.016)));
}
return Math.max(12, Math.min(16, Math.round(scale * 0.18)));
})();
// Inset for stroke + arrowhead so visual tips sit on/inside edges (#71).
const lineW = Math.max(2, Math.round(fontPx / 10));
const arrowHead = Math.max(7, fontPx * 0.32);
// Width tips retain stroke/arrowhead protection. Length endpoints need only
// half-stroke protection: larger arrowhead-derived inset visibly under-reached tips (#73).
const widthEdgeInset = Math.max(lineW / 2 + 0.5, arrowHead * 0.15);
const lengthEdgeInset = Math.max(0.5, lineW / 2);
const pad = Math.max(10, Math.min(28, fontPx * 1.1));
const stations = overlayWidthStations(lengthIn);
const widths: OverlayWidthMark[] = [];
for (const stationIn of stations) {
const t = stationIn * scale;
const ox = bottom[0] + ux * t;
const oy = bottom[1] + uy * t;
const extent = probeExtent(bitset, widthPx, heightPx, ox, oy, rx, ry);
if (!extent) continue;
// left/right on mask edges, then inset in inventory space for stroke/arrowhead
const leftMapped = mapToInventory(ox + rx * extent.minP, oy + ry * extent.minP);
const rightMapped = mapToInventory(ox + rx * extent.maxP, oy + ry * extent.maxP);
const wdx = rightMapped[0] - leftMapped[0];
const wdy = rightMapped[1] - leftMapped[1];
const wlen = Math.hypot(wdx, wdy) || 1;
const wux = wdx / wlen;
const wuy = wdy / wlen;
const left: [number, number] = [leftMapped[0] + wux * widthEdgeInset, leftMapped[1] + wuy * widthEdgeInset];
const right: [number, number] = [rightMapped[0] - wux * widthEdgeInset, rightMapped[1] - wuy * widthEdgeInset];
// Width number sits axis-left outside the slab (past left tip).
const labelOff = extent.minP - pad - fontPx * 0.85;
const labelSrcW: [number, number] = [ox + rx * labelOff, oy + ry * labelOff];
const widthIn = (extent.maxP - extent.minP + 1) / scale;
widths.push({
stationIn,
center: mapToInventory(ox, oy),
left,
right,
labelAt: mapToInventory(labelSrcW[0], labelSrcW[1]),
label: fmtInches(widthIn),
});
}
// Length graphic: beside wood on axis-right, outside max right extent.
let maxRight = 0;
const samples = Math.max(8, Math.round(lengthIn));
for (let i = 0; i <= samples; i++) {
const t = (i / samples) * (lengthIn * scale);
const ox = bottom[0] + ux * t;
const oy = bottom[1] + uy * t;
const extent = probeExtent(bitset, widthPx, heightPx, ox, oy, rx, ry);
if (extent && extent.maxP > maxRight) maxRight = extent.maxP;
}
const outward = maxRight + pad;
// Classic dimensioning (#70 + #71): width-aware centerline tip stations offset by
// pure axis-right (rx,ry)*outward. Length stays || approved (ux,uy); no screen-slide.
// Axial inset (inventory space) so arrowhead tips sit on footprint, not past.
const aMapped = mapToInventory(bottom[0] + rx * outward, bottom[1] + ry * outward);
const bMapped = mapToInventory(top[0] + rx * outward, top[1] + ry * outward);
const ldx0 = bMapped[0] - aMapped[0];
const ldy0 = bMapped[1] - aMapped[1];
const llen0 = Math.hypot(ldx0, ldy0) || 1;
const lux = ldx0 / llen0;
const luy = ldy0 / llen0;
const aInv: [number, number] = [aMapped[0] + lux * lengthEdgeInset, aMapped[1] + luy * lengthEdgeInset];
const bInv: [number, number] = [bMapped[0] - lux * lengthEdgeInset, bMapped[1] - luy * lengthEdgeInset];
const midInv: [number, number] = [(aInv[0] + bInv[0]) / 2, (aInv[1] + bInv[1]) / 2];
// Length label further axis-right of dim line so glyphs stay off the wood (#71).
const mapOrigin = mapToInventory(0, 0);
const mapR = mapToInventory(rx, ry);
const irx = mapR[0] - mapOrigin[0];
const iry = mapR[1] - mapOrigin[1];
const irlen = Math.hypot(irx, iry) || 1;
const nrx = irx / irlen;
const nry = iry / irlen;
const labelAt: [number, number] = [
midInv[0] + nrx * (fontPx * 0.85 + 6),
midInv[1] + nry * (fontPx * 0.85 + 6),
];
const lengthMark: OverlayLengthMark = {
a: aInv,
b: bInv,
labelAt,
label: lengthLabel,
outside: true,
};
// Clamp all labels inside inventory PNG so nothing clips (#71).
if (inventoryW && inventoryH && inventoryW > 0 && inventoryH > 0) {
const margin = Math.max(8, fontPx * 0.65);
const clamp = (pt: [number, number], growLeft: boolean, text: string): [number, number] => {
let [x, y] = pt;
const glyphW = Math.max(fontPx * 1.2, text.length * fontPx * 0.55);
if (growLeft) {
// textAlign right — glyphs extend left of anchor
x = Math.max(margin + glyphW, Math.min(inventoryW - margin, x));
} else {
x = Math.max(margin, Math.min(inventoryW - margin, x));
}
y = Math.max(margin, Math.min(inventoryH - margin, y));
return [x, y];
};
lengthMark.labelAt = clamp(lengthMark.labelAt, false, lengthLabel);
for (const w of widths) {
const growLeft = w.labelAt[0] <= w.center[0];
w.labelAt = clamp(w.labelAt, growLeft, w.label);
}
}
return {
stroke: '#000000',
fontPx,
length: lengthMark,
widths,
};
}
/** Draw black dimension graphics onto an existing canvas (inventory PNG size). */
export function drawOverlayPlan(
ctx: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D,
plan: OverlayPlan,
): void {
const { stroke, fontPx } = plan;
ctx.save();
ctx.strokeStyle = stroke;
ctx.fillStyle = stroke;
ctx.lineWidth = Math.max(2, Math.round(fontPx / 10));
// Butt caps: round caps extend past endpoints and overshoot wood edges (#71).
ctx.lineCap = 'butt';
ctx.font = `bold ${fontPx}px sans-serif`;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
const arrow = (from: [number, number], to: [number, number]) => {
const dx = to[0] - from[0];
const dy = to[1] - from[1];
const len = Math.hypot(dx, dy) || 1;
const ux = dx / len;
const uy = dy / len;
const head = Math.max(7, fontPx * 0.32);
ctx.beginPath();
ctx.moveTo(from[0], from[1]);
ctx.lineTo(to[0], to[1]);
ctx.stroke();
// arrow tips at both ends
for (const [sx, sy, dir] of [
[from[0], from[1], 1],
[to[0], to[1], -1],
] as const) {
const px = -uy;
const py = ux;
ctx.beginPath();
ctx.moveTo(sx, sy);
ctx.lineTo(sx + dir * ux * head + px * head * 0.4, sy + dir * uy * head + py * head * 0.4);
ctx.lineTo(sx + dir * ux * head - px * head * 0.4, sy + dir * uy * head - py * head * 0.4);
ctx.closePath();
ctx.fill();
}
};
// Length — grow text further axis-right of the dim line (away from wood)
arrow(plan.length.a, plan.length.b);
{
const [lax, lay] = plan.length.a;
const [lbx, lby] = plan.length.b;
const midX = (lax + lbx) / 2;
const midY = (lay + lby) / 2;
const outwardX = plan.length.labelAt[0] - midX;
const outwardY = plan.length.labelAt[1] - midY;
// Prefer left/right align so glyphs extend away from wood when offset is mostly ±x
if (Math.abs(outwardX) >= Math.abs(outwardY)) {
ctx.textAlign = outwardX >= 0 ? 'left' : 'right';
} else {
ctx.textAlign = 'center';
}
}
ctx.fillText(plan.length.label, plan.length.labelAt[0], plan.length.labelAt[1]);
// Widths — grow text away from wood (labelAt is axis-left of left tip)
for (const w of plan.widths) {
arrow(w.left, w.right);
// labelAt is on the axis-left side of the station; align so glyphs extend further out
const towardLeft = w.labelAt[0] <= w.center[0];
ctx.textAlign = towardLeft ? 'right' : 'left';
ctx.fillText(w.label, w.labelAt[0], w.labelAt[1]);
}
ctx.restore();
}
export interface InventoryMap {
mapToInventory: (x: number, y: number) => [number, number];
outW: number;
outH: number;
}
/** Same crop math as composeInventoryFrame — source → inventory PNG pixels. */
export function inventorySourceMap(
slabBounds: { x: number; y: number; w: number; h: number },
source: { widthPx: number; heightPx: number },
outW: number,
outH: number,
cropFill: number = DEFAULT_CROP_FILL,
): InventoryMap {
const FILL = cropFill > 0 && cropFill <= 1 ? cropFill : DEFAULT_CROP_FILL;
const x0 = Math.max(0, Math.round(slabBounds.x));
const y0 = Math.max(0, Math.round(slabBounds.y));
const x1 = Math.min(source.widthPx, x0 + Math.max(1, Math.round(slabBounds.w)));
const y1 = Math.min(source.heightPx, y0 + Math.max(1, Math.round(slabBounds.h)));
const sw = x1 - x0;
const sh = y1 - y0;
const scale = Math.min((outW * FILL) / Math.max(sw, 1), (outH * FILL) / Math.max(sh, 1));
const dw = sw * scale;
const dh = sh * scale;
const ox = (outW - dw) / 2;
const oy = (outH - dh) / 2;
return {
outW,
outH,
mapToInventory: (sx, sy) => [ox + (sx - x0) * scale, oy + (sy - y0) * scale],
};
}
export interface EncodeOverlayInput {
/** Clean calibration inventory PNG bytes. */
inventoryPng: Blob;
bitset: Uint8Array;
widthPx: number;
heightPx: number;
bbox: BBox;
scale: number;
lengthIn: number;
lengthLabel: string;
slabBounds: { x: number; y: number; w: number; h: number };
source: { widthPx: number; heightPx: number };
/** Optional override; default reads live settings.crop_fill (#99). */
cropFill?: number;
}
/**
* Burn black dimension overlays onto a copy of the clean calibration inventory PNG.
* Returns a new image/png blob; never mutates the clean inventory.
*/
export async function encodeOverlayPng(input: EncodeOverlayInput): Promise<Blob> {
const bmp = await createImageBitmap(input.inventoryPng);
try {
const outW = bmp.width;
const outH = bmp.height;
const canvas = new OffscreenCanvas(outW, outH);
const ctx = canvas.getContext('2d')!;
ctx.drawImage(bmp, 0, 0);
const fill = input.cropFill ?? getCropFill();
const { mapToInventory } = inventorySourceMap(input.slabBounds, input.source, outW, outH, fill);
const plan = buildOverlayPlan({
bitset: input.bitset,
widthPx: input.widthPx,
heightPx: input.heightPx,
bbox: input.bbox,
scale: input.scale,
lengthIn: input.lengthIn,
lengthLabel: input.lengthLabel,
mapToInventory,
inventoryW: outW,
inventoryH: outH,
});
drawOverlayPlan(ctx, plan);
return await canvas.convertToBlob({ type: 'image/png' });
} finally {
bmp.close();
}
}
+92
View File
@@ -0,0 +1,92 @@
/**
* Bake calibration photo tilt into a rotated bitmap + mask (#99 transform scope).
* Used only for the calibration inventory PNG + dimension-overlay clone.
* Batch siblings are never passed through here.
*/
import type { Mask, Source } from '../capture/capture.types';
import { minAreaRect, slabBounds } from '../geometry/rect';
export interface RotatedCal {
bitmap: ImageBitmap;
mask: Mask;
source: Pick<Source, 'widthPx' | 'heightPx'>;
}
/**
* Rotate source clockwise by tiltDeg (same sign as CSS/canvas rotate).
* Returns a new bitmap the caller must close, plus recomputed mask geometry.
* Identity (≈0°) returns clones of inputs without resampling.
*/
export async function rotateCalibration(
bitmap: ImageBitmap,
mask: Mask,
source: Pick<Source, 'widthPx' | 'heightPx'>,
tiltDeg: number,
): Promise<RotatedCal> {
const a = ((tiltDeg % 360) + 360) % 360;
if (a < 0.05 || a > 359.95) {
// No meaningful rotation — hand back the same bitmap reference; caller closes once.
return { bitmap, mask, source };
}
const srcW = source.widthPx;
const srcH = source.heightPx;
const rad = (a * Math.PI) / 180;
const cos = Math.cos(rad);
const sin = Math.sin(rad);
// Axis-aligned bounds of rotated rectangle corners.
const corners = [
[0, 0],
[srcW, 0],
[srcW, srcH],
[0, srcH],
].map(([x, y]) => {
const cx = x - srcW / 2;
const cy = y - srcH / 2;
return [cx * cos - cy * sin, cx * sin + cy * cos] as [number, number];
});
let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
for (const [x, y] of corners) {
if (x < minX) minX = x;
if (x > maxX) maxX = x;
if (y < minY) minY = y;
if (y > maxY) maxY = y;
}
const outW = Math.max(1, Math.ceil(maxX - minX));
const outH = Math.max(1, Math.ceil(maxY - minY));
const canvas = new OffscreenCanvas(outW, outH);
const ctx = canvas.getContext('2d')!;
ctx.translate(outW / 2, outH / 2);
ctx.rotate(rad);
ctx.drawImage(bitmap, -srcW / 2, -srcH / 2);
// Nearest-neighbour resample of the bitset into the rotated frame.
const bitset = new Uint8Array(outW * outH);
const invCos = Math.cos(-rad);
const invSin = Math.sin(-rad);
for (let y = 0; y < outH; y++) {
for (let x = 0; x < outW; x++) {
const dx = x - outW / 2;
const dy = y - outH / 2;
const sx = Math.round(dx * invCos - dy * invSin + srcW / 2);
const sy = Math.round(dx * invSin + dy * invCos + srcH / 2);
if (sx < 0 || sy < 0 || sx >= srcW || sy >= srcH) continue;
bitset[y * outW + x] = mask.bitset[sy * srcW + sx];
}
}
let slabPx = 0;
for (let i = 0; i < bitset.length; i++) if (bitset[i]) slabPx++;
const bounds = slabBounds(bitset, outW, outH);
const bbox = minAreaRect(bitset, outW, outH) ?? mask.bbox;
const outBmp = canvas.transferToImageBitmap();
return {
bitmap: outBmp,
mask: {
params: mask.params,
slabPx,
bitset,
bbox,
slabBounds: bounds,
},
source: { widthPx: outW, heightPx: outH },
};
}
-13
View File
@@ -1,13 +0,0 @@
<script>
import '../app.css';
</script>
<svelte:head>
<title>SlabUploader — Milestone 1</title>
</svelte:head>
<main>
<h1>SlabUploader</h1>
<p>Milestone 1 — Capture (browser-only)</p>
<slot />
</main>
-12
View File
@@ -1,12 +0,0 @@
<script>
/** M1 placeholder home — the 5-state capture flow is built in later cards. */
</script>
<div class="capture">
<h2>Capture</h2>
<p>The M1 capture flow (Add photo → Mask → Length axis → Measure → PNG) lands in cards M1-3…M1-5.</p>
</div>
<style>
.capture { max-width: 720px; margin: 2rem auto; padding: 0 1rem; }
</style>
@@ -1,6 +0,0 @@
// Minimal health route so the UAT container has a non-SPA probe target.
import { json } from '@sveltejs/kit';
export function GET() {
return json({ ok: true, milestone: 1, app: 'slab-uploader' });
}
+55
View File
@@ -0,0 +1,55 @@
/**
* Thin frontend-only crop_fill setting (#99).
* Default 0.9 — replaces duplicated FILL = 0.9 in png/inventory + png/overlay.
* Not a full M3 Settings surface; localStorage so it survives reloads.
*/
import { DEFAULT_CROP_FILL, clampFill } from '../png/crop';
const KEY = 'slabuploader.crop_fill';
let cached: number | null = null;
export function getCropFill(): number {
if (cached != null) return cached;
if (typeof localStorage === 'undefined') {
cached = DEFAULT_CROP_FILL;
return cached;
}
try {
const raw = localStorage.getItem(KEY);
if (raw == null) {
cached = DEFAULT_CROP_FILL;
return cached;
}
cached = clampFill(Number.parseFloat(raw));
return cached;
} catch {
cached = DEFAULT_CROP_FILL;
return cached;
}
}
export function setCropFill(fill: number): number {
const next = clampFill(fill);
cached = next;
if (typeof localStorage !== 'undefined') {
try {
localStorage.setItem(KEY, String(next));
} catch {
/* private mode / quota — in-memory still updated */
}
}
return next;
}
/** Test helper: clear cached value (and optional storage). */
export function _resetCropFillForTests(): void {
cached = null;
if (typeof localStorage !== 'undefined') {
try {
localStorage.removeItem(KEY);
} catch {
/* ignore */
}
}
}
+89
View File
@@ -0,0 +1,89 @@
import { minAreaRect, slabBounds } from '../geometry/rect';
import type { MaskParams, WorkerIn, WorkerOut } from '../capture/capture.types';
import { decodeOrientedBitmap } from '../decodeOrientedBitmap';
import { greenKeep, blackKeep, autoPickSheet } from '../mask/sheetKeep';
let bitmap: ImageBitmap | null = null;
let data: Uint8ClampedArray | null = null;
let w = 0, h = 0;
let current = 0; // highest job id issued by main; older replies are dropped
function runMask(params: MaskParams, id: number): WorkerOut {
if (!data) return { id, kind: 'failed', reason: 'no source decoded' };
let sheet = params.sheet;
if (sheet === 'auto') {
const samples: { r: number; g: number; b: number }[] = [];
for (let y = 0; y < h; y += 16)
for (let x = 0; x < w; x += 16) {
const i = (y * w + x) * 4;
samples.push({ r: data[i], g: data[i + 1], b: data[i + 2] });
}
sheet = autoPickSheet(samples);
}
const keep = sheet === 'green' ? greenKeep : blackKeep;
const s = Math.max(0, Math.min(100, params.sensitivity));
const bitset = new Uint8Array(w * h);
let slabPx = 0;
for (let i = 0, p = 0; i < data.length; i += 4, p++) {
if (keep(data[i], data[i + 1], data[i + 2], s)) {
bitset[p] = 1;
slabPx++;
}
}
if (slabPx === 0) {
return { id, kind: 'ready', slabPx: 0, bitset, bbox: { corners: [], longSidePx: 0, angleDeg: 0 }, slabBounds: { x: 0, y: 0, w: 0, h: 0 } };
}
const bbox = minAreaRect(bitset, w, h);
if (!bbox) {
return { id, kind: 'ready', slabPx: 0, bitset, bbox: { corners: [], longSidePx: 0, angleDeg: 0 }, slabBounds: { x: 0, y: 0, w: 0, h: 0 } };
}
return { id, kind: 'ready', slabPx, bitset, bbox, slabBounds: slabBounds(bitset, w, h) };
}
self.onmessage = (e: MessageEvent<WorkerIn>) => {
const msg = e.data;
if (msg.id < current) return; // superseded by a newer job
current = msg.id;
if (msg.kind === 'init') {
if (bitmap) {
bitmap.close();
bitmap = null;
}
data = null;
w = h = 0;
decodeOrientedBitmap(msg.blob)
.then((bmp) => {
w = bmp.width;
h = bmp.height;
const canvas = new OffscreenCanvas(w, h);
const ctx = canvas.getContext('2d')!;
ctx.drawImage(bmp, 0, 0);
data = ctx.getImageData(0, 0, w, h).data;
bitmap = bmp;
self.postMessage({ id: msg.id, kind: 'decoded', w, h } as WorkerOut);
})
.catch((err) => self.postMessage({ id: msg.id, kind: 'failed', reason: String(err) } satisfies WorkerOut));
return;
}
if (msg.kind === 'invalidate') {
if (bitmap) {
bitmap.close();
bitmap = null;
}
data = null;
w = h = 0;
return;
}
try {
const out = runMask({ sheet: msg.sheet, sensitivity: msg.sensitivity }, msg.id);
if (out.kind === 'ready') {
self.postMessage(out, [out.bitset.buffer]);
} else {
self.postMessage(out);
}
} catch (err) {
self.postMessage({ id: msg.id, kind: 'failed', reason: String(err) } satisfies WorkerOut);
}
};
export {};
-10
View File
@@ -1,10 +0,0 @@
import adapter from '@sveltejs/adapter-node';
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter()
}
};
export default config;
+40
View File
@@ -0,0 +1,40 @@
# E2E visual artifacts (#65)
Screenshots from `npx playwright test` for Milestone 2 Photos-ready evidence.
## How to run (local box — not ubudual :10880)
```bash
cd frontend
npm ci
npx playwright install chromium # once per machine
npm run test:e2e
```
Or against an already-running preview:
```bash
npm run build && npm run preview -- --host 127.0.0.1 --port 4173
# other terminal:
npx playwright test
```
Fixtures: `tests/fixtures/set1-48x2/` (L=48, thickness=2) and `set2-31x2p5/` (L=31, thickness=2.5). Black sheet — e2e selects **Black** and may nudge sensitivity.
## Expected screenshots (this folder)
| File | Step |
|---|---|
| `01-batch.png` | Batch with fixtures loaded |
| `02-pick-calibration.png` | Pick calibration |
| `03-mask.png` | Mask (Black sheet) ready to approve |
| `04-axis.png` | Length axis |
| `05-measure.png` | Measure with L / thickness filled |
| `06-photos-ready.png` | Photos ready lineup (clean + overlay + siblings) |
Large regenerating `*.png` and Playwright `test-results/` / `playwright-report/` are gitignored.
Committed small evidence JPEGs live in `evidence/` (regenerate with a green `npm run test:e2e`, then re-encode if needed).
## Unit tests
`npm test` must still pass separately from Playwright.
Binary file not shown.

After

Width:  |  Height:  |  Size: 177 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 133 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 188 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 207 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 186 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 466 KiB

@@ -0,0 +1,11 @@
# Last green e2e run (#71)
- **When:** 2026-09-15 9:55 PM PT (`2026-09-16T04:55:14Z`)
- **Fixture:** `tests/fixtures/set1-48x2/` (L=48, thickness=2)
- **Server:** local `vite preview` at `http://127.0.0.1:4173` (not ubudual)
- **Result:** 1 passed (~8s test)
- **Mask:** Black sheet — Approve enabled
- **Fix under test:** width-aware tip stations; stroke/arrowhead edge inset; inventory-capped font; labels clamped in PNG (#71); #70 axis-parallel kept
- **Lineup:** Clean calibration → Dimension overlay → Batch photo 2
Full-resolution PNGs are produced under `../` on each run (gitignored). These JPEGs are commit-sized evidence for review.
-42
View File
@@ -1,42 +0,0 @@
import { test, expect } from '@playwright/test';
// M1-3: Playwright skeleton for the Mask state.
//
// The live Mask UI lands in card M1-5, so these tests are written but .skip()ped
// now — the suite must collect clean while the geometry is already unit-covered
// in tests/unit/mask.test.js (keySheet / maskStats are pure and DOM-free).
//
// Spec ground truth (docs/CAPTURE.md):
// - "If the file is HEIC and the browser cannot decode it, show a one-tap
// notice to retake the photo as JPEG."
// - "Approve is blocked when the mask has no slab pixels. User retakes."
test.describe('Mask state (M1-5 live UI)', () => {
test.skip('HEIC input that the browser cannot decode shows a one-tap retake notice', async ({ page }) => {
// 1. Land in the Mask state with a photo loaded (M1-5 provides this).
await page.goto('/');
// 2. Provide an HEIC file the browser cannot decode.
// (Test fixture path to be added with the M1-5 capture UI.)
// const input = page.locator('input[type="file"]');
// await input.setInputFiles('tests/fixtures/slab.heic');
// 3. The app must surface a one-tap 'retake as JPEG' notice — never a
// silent crash, never a server-side decode.
// const notice = page.getByRole('alert', { name: /retake/i });
// await expect(notice).toBeVisible();
// await expect(notice).toHaveText(/retake as JPEG/i);
// 4. Tapping it returns to Add photo.
// await notice.tap();
// await expect(page.getByRole('button', { name: /add photo/i })).toBeVisible();
});
test.skip('Approve is blocked while the mask has no slab pixels', async ({ page }) => {
// 1. Load a Mask whose keying removes every pixel (e.g. photo of sheet only).
await page.goto('/');
// 2. Approve must be disabled — the mask has no slab pixels.
// const approve = page.getByRole('button', { name: /approve/i });
// await expect(approve).toBeDisabled();
// 3. User retakes: after a photo with a real slab is keyed, approve enables.
// await page.getByRole('button', { name: /retake/i }).tap();
// await expect(approve).toBeEnabled();
});
});
+135
View File
@@ -0,0 +1,135 @@
import { test, expect, type Page } from '@playwright/test';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import fs from 'node:fs';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const FIXTURES = path.resolve(__dirname, '../fixtures/set1-48x2');
const ARTIFACTS = path.resolve(__dirname, 'artifacts');
const FACE_A = path.join(FIXTURES, 'face-a.jpeg');
const FACE_B = path.join(FIXTURES, 'face-b.jpeg');
const shot = async (page: Page, name: string) => {
fs.mkdirSync(ARTIFACTS, { recursive: true });
await page.screenshot({
path: path.join(ARTIFACTS, name),
fullPage: true,
});
};
/** Discard any device draft so we always start on Batch. */
const ensureFreshBatch = async (page: Page) => {
await page.goto('/');
// resumeProbe is async onMount — wait for either Resume or Batch.
const resumeHeading = page.getByRole('heading', { name: /resume/i });
const batchHeading = page.getByRole('heading', { name: 'Batch photos' });
await expect(resumeHeading.or(batchHeading)).toBeVisible({ timeout: 15_000 });
if (await resumeHeading.isVisible().catch(() => false)) {
await page.getByRole('button', { name: /start fresh/i }).click();
}
await expect(batchHeading).toBeVisible();
};
/**
* Black-sheet fixtures: Auto may work, but UAT note says prefer Black + sensitivity
* tweaks. Do not invent inference — only UI sheet/sensitivity controls.
*/
const prepareMaskForApprove = async (page: Page) => {
await expect(page.getByRole('heading', { name: 'Mask' })).toBeVisible();
// Sheet change recomputes immediately (session.setSheet → runPass).
await page.getByRole('button', { name: 'Black', exact: true }).click();
const approve = page.getByRole('button', { name: 'Approve mask' });
// First overlay after decode is debounced 2s; Black pass is immediate but
// 4K fixtures still need decode + worker time.
try {
await expect(approve).toBeEnabled({ timeout: 45_000 });
return;
} catch {
/* try sensitivity ladder */
}
// Higher sensitivity widens black sheet match; if still empty, try lower.
for (const target of [60, 70, 40, 30, 80]) {
const slider = page.locator('input[type="range"]');
await slider.fill(String(target));
// Sensitivity is debounced 2s — wait past debounce + worker.
await page.waitForTimeout(2800);
if (await approve.isEnabled()) return;
}
await expect(approve, 'Mask approve stayed disabled after Black + sensitivity tweaks').toBeEnabled({
timeout: 15_000,
});
};
test.describe('M2 #65 Photos-ready e2e (set1-48x2)', () => {
test('batch → pick → mask → axis → measure → photos ready + screenshots', async ({ page }) => {
test.setTimeout(180_000);
expect(fs.existsSync(FACE_A), 'fixture face-a').toBeTruthy();
expect(fs.existsSync(FACE_B), 'fixture face-b').toBeTruthy();
await ensureFreshBatch(page);
// Gallery multi-input (not the capture=environment camera input).
const gallery = page.locator('input[type="file"][multiple]');
await gallery.setInputFiles([FACE_A, FACE_B]);
await expect(page.getByAltText('Batch photo 1')).toBeVisible({ timeout: 30_000 });
await expect(page.getByAltText('Batch photo 2')).toBeVisible();
await shot(page, '01-batch.png');
await page.getByRole('button', { name: 'Continue' }).click();
await expect(page.getByRole('heading', { name: 'Pick calibration photo' })).toBeVisible();
await shot(page, '02-pick-calibration.png');
await page.getByRole('button', { name: 'Use photo 1 as calibration' }).click();
await prepareMaskForApprove(page);
await shot(page, '03-mask.png');
await page.getByRole('button', { name: 'Approve mask' }).click();
await expect(page.getByRole('heading', { name: 'Length axis' })).toBeVisible();
await shot(page, '04-axis.png');
await page.getByRole('button', { name: 'Approve axis' }).click();
await expect(page.getByRole('heading', { name: 'Measure' })).toBeVisible();
await page.getByLabel('Length L').fill('48');
await page.getByLabel('Thickness').fill('2');
// blur/change commits eighths into session before Calculate.
await page.getByLabel('Length L').blur();
await page.getByLabel('Thickness').blur();
await shot(page, '05-measure.png');
const calc = page.getByRole('button', { name: 'Calculate' });
await expect(calc).toBeEnabled();
await calc.click();
// Calculate encodes calibration PNG + overlay + sibling batch apply.
await expect(page.getByRole('heading', { name: 'Photos ready' })).toBeVisible({
timeout: 120_000,
});
await expect(page.getByLabel('Photos ready')).toBeVisible();
await expect(page.getByText('Clean calibration')).toBeVisible();
await expect(page.getByText('Dimension overlay')).toBeVisible();
// Sibling face-b should appear as Batch photo 2 (capture order).
await expect(page.getByText(/Batch photo 2|Photo 2 failed/)).toBeVisible();
await shot(page, '06-photos-ready.png');
// Write a short run log for parent AI visual review (not a substitute for eyes).
const logPath = path.join(ARTIFACTS, 'run-log.txt');
const shots = ['01-batch.png', '02-pick-calibration.png', '03-mask.png', '04-axis.png', '05-measure.png', '06-photos-ready.png'];
const lines = [
`M2 #65 Playwright e2e — set1-48x2 (L=48, thickness=2)`,
`baseURL: ${page.url().split('/').slice(0, 3).join('/')}`,
`finished: ${new Date().toISOString()}`,
`artifacts:`,
...shots.map((s) => {
const p = path.join(ARTIFACTS, s);
const st = fs.existsSync(p) ? fs.statSync(p) : null;
return ` - ${s} (${st ? `${st.size} bytes` : 'MISSING'})`;
}),
`assertions: Photos ready heading; Clean calibration; Dimension overlay; batch sibling slot`,
];
fs.writeFileSync(logPath, lines.join('\n') + '\n', 'utf8');
});
});
-12
View File
@@ -1,12 +0,0 @@
import { test, expect } from '@playwright/test';
// E2E smoke: the app serves and the health route responds.
// Later cards extend this to walk the full 5-state capture flow.
test('M1-1 harness smoke: app serves + health ok', async ({ page, request }) => {
await page.goto('/');
await expect(page).toHaveTitle(/SlabUploader/);
const res = await request.get('/api/health');
expect(res.ok()).toBeTruthy();
const body = await res.json();
expect(body.ok).toBe(true);
});
+16
View File
@@ -0,0 +1,16 @@
# Test photo fixtures
Matched sets from Ty for Milestone 2 Photos (Playwright + calibration). Black sheet.
## set1-48x2 — L = 48 in, thickness = 2 in
Curly / fiddleback bookmatch pair.
- `face-a.jpeg`
- `face-b.jpeg`
## set2-31x2p5 — L = 31 in, thickness = 2.5 in
Shorter organic / burl pair.
- `face-a.jpeg`
- `face-b.jpeg`
## m1-dji (optional M1 UAT sample)
- `162332.JPG` — live-edge on black; historically used on ubudual M1 UAT.
Binary file not shown.

After

Width:  |  Height:  |  Size: 760 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 768 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 884 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 856 KiB

-174
View File
@@ -1,174 +0,0 @@
import { describe, it, expect } from 'vitest';
import { keySheet, maskStats } from '$lib/core/mask.js';
// M1-3 mask key geometry — unit tests (RED first, per strict TDD).
// Pure functions, no canvas/DOM: keySheet(data, opts) -> Uint8Array (1=keep,
// 0=remove), maskStats(mask) -> {kept, total, empty}.
//
// Ground truth is docs/CAPTURE.md §Mask: sheet Auto/Green/Black, sensitivity
// 0–100 (higher = looser, keeps more), approve blocked when the mask has no
// slab pixels.
// --- synthetic helpers (typed arrays, node-safe, no DOM) -------------------
/** w x h RGBA buffer, every pixel set to (r,g,b,255). */
function solid(w, h, r, g, b) {
const data = new Uint8ClampedArray(w * h * 4);
for (let i = 0; i < w * h; i++) {
data[i * 4 + 0] = r;
data[i * 4 + 1] = g;
data[i * 4 + 2] = b;
data[i * 4 + 3] = 255;
}
return data;
}
/** Solid canvas with a centered filled rect painted over it. */
function withRect(canvas, x0, y0, w, h, r, g, b) {
for (let y = y0; y < y0 + h; y++) {
for (let x = x0; x < x0 + w; x++) {
const i = (y * canvas.width + x) * 4;
canvas.data[i + 0] = r;
canvas.data[i + 1] = g;
canvas.data[i + 2] = b;
canvas.data[i + 3] = 255;
}
}
return canvas;
}
/** 12 x 8 RGBA: green sheet field + 8 x 4 brown rectangle at (2,2). */
function greenFieldWithBrownSlab() {
const canvas = { width: 12, height: 8, data: solid(12, 8, 0, 200, 0) };
return withRect(canvas, 2, 2, 8, 4, 140, 90, 40);
}
/** 12 x 8 RGBA: near-black sheet + 8 x 4 bright slab at (2,2). */
function blackFieldWithBrightSlab() {
const canvas = { width: 12, height: 8, data: solid(12, 8, 10, 10, 10) };
return withRect(canvas, 2, 2, 8, 4, 220, 210, 190);
}
/** Mask sum over a rectangular region (row-major). */
function regionSum(mask, w, x0, y0, rw, rh) {
let s = 0;
for (let y = y0; y < y0 + rh; y++)
for (let x = x0; x < x0 + rw; x++) s += mask[y * w + x];
return s;
}
const GREEN = { sheet: 'green', sensitivity: 50 };
const BLACK = { sheet: 'black', sensitivity: 50 };
const rect = { x0: 2, y0: 2, rw: 8, rh: 4 };
// --- green key ---------------------------------------------------------------
describe('keySheet: green sheet', () => {
it('returns one entry per pixel', () => {
const mask = keySheet(greenFieldWithBrownSlab().data, GREEN);
expect(mask).toBeInstanceOf(Uint8Array);
expect(mask.length).toBe(12 * 8);
});
it('removes the strongly green field and keeps the brown slab', () => {
const mask = keySheet(greenFieldWithBrownSlab().data, GREEN);
// Outside the rect the field is uniformly green -> fully removed.
expect(regionSum(mask, 12, 0, 0, 12, 2)).toBe(0); // top strip
expect(regionSum(mask, 12, 0, 6, 12, 2)).toBe(0); // bottom strip
expect(regionSum(mask, 12, 0, 2, 2, 4)).toBe(0); // left strip
expect(regionSum(mask, 12, 10, 2, 2, 4)).toBe(0); // right strip
// The brown rect is fully kept.
expect(regionSum(mask, 12, rect.x0, rect.y0, rect.rw, rect.rh)).toBe(32);
});
});
// --- black key ---------------------------------------------------------------
describe('keySheet: black sheet', () => {
it('removes the near-black field and keeps the bright slab', () => {
const mask = keySheet(blackFieldWithBrightSlab().data, BLACK);
expect(regionSum(mask, 12, 0, 0, 12, 2)).toBe(0);
expect(regionSum(mask, 12, 0, 6, 12, 2)).toBe(0);
expect(regionSum(mask, 12, 0, 2, 2, 4)).toBe(0);
expect(regionSum(mask, 12, 10, 2, 2, 4)).toBe(0);
expect(regionSum(mask, 12, rect.x0, rect.y0, rect.rw, rect.rh)).toBe(32);
});
});
// --- sensitivity -------------------------------------------------------------
describe('keySheet: sensitivity', () => {
it('higher sensitivity keeps >= as many pixels (green, monotone)', () => {
const data = greenFieldWithBrownSlab().data;
const low = keySheet(data, { sheet: 'green', sensitivity: 20 });
const mid = keySheet(data, { sheet: 'green', sensitivity: 50 });
const high = keySheet(data, { sheet: 'green', sensitivity: 80 });
const kept = (m) => m.reduce((a, b) => a + b, 0);
expect(kept(mid)).toBeGreaterThanOrEqual(kept(low));
expect(kept(high)).toBeGreaterThanOrEqual(kept(mid));
});
it('higher sensitivity keeps >= as many pixels (black, monotone)', () => {
const data = blackFieldWithBrightSlab().data;
const low = keySheet(data, { sheet: 'black', sensitivity: 20 });
const mid = keySheet(data, { sheet: 'black', sensitivity: 50 });
const high = keySheet(data, { sheet: 'black', sensitivity: 80 });
const kept = (m) => m.reduce((a, b) => a + b, 0);
expect(kept(mid)).toBeGreaterThanOrEqual(kept(low));
expect(kept(high)).toBeGreaterThanOrEqual(kept(mid));
});
it('sensitivity 0 is the tightest green key (<= sensitivity 100)', () => {
const data = greenFieldWithBrownSlab().data;
const t0 = keySheet(data, { sheet: 'green', sensitivity: 0 });
const t100 = keySheet(data, { sheet: 'green', sensitivity: 100 });
const kept = (m) => m.reduce((a, b) => a + b, 0);
expect(kept(t100)).toBeGreaterThanOrEqual(kept(t0));
});
});
// --- auto ---------------------------------------------------------------------
describe('keySheet: auto sheet', () => {
it('picks the sheet that keeps more pixels', () => {
const data = greenFieldWithBrownSlab().data;
const auto = keySheet(data, { sheet: 'auto', sensitivity: 50 });
const greenKept = keySheet(data, GREEN).reduce((a, b) => a + b, 0);
const blackKept = keySheet(data, BLACK).reduce((a, b) => a + b, 0);
expect(auto.reduce((a, b) => a + b, 0)).toBe(
Math.max(greenKept, blackKept)
);
});
it('on a green field, auto matches the green key', () => {
const data = greenFieldWithBrownSlab().data;
const auto = keySheet(data, { sheet: 'auto', sensitivity: 50 });
const green = keySheet(data, GREEN);
for (let i = 0; i < auto.length; i++) expect(auto[i]).toBe(green[i]);
});
});
// --- maskStats -----------------------------------------------------------------
describe('maskStats', () => {
it('empty = true when no pixel is kept (approve is blocked)', () => {
const mask = new Uint8Array([0, 0, 0, 0]);
expect(maskStats(mask)).toEqual({ kept: 0, total: 4, empty: true });
});
it('empty = false when any pixel is kept', () => {
const mask = new Uint8Array([0, 1, 0, 1]);
expect(maskStats(mask)).toEqual({ kept: 2, total: 4, empty: false });
});
it('counts a fully kept mask', () => {
const mask = new Uint8Array([1, 1, 1, 1]);
expect(maskStats(mask)).toEqual({ kept: 4, total: 4, empty: false });
});
it('reports the total from the keySheet output', () => {
const mask = keySheet(greenFieldWithBrownSlab().data, GREEN);
expect(maskStats(mask).total).toBe(96);
expect(maskStats(mask).empty).toBe(false);
});
});
-89
View File
@@ -1,89 +0,0 @@
import { describe, it, expect } from 'vitest';
import * as m from '$lib/core/math.js';
// M1 deterministic core — unit tests (RED first, per TDD).
// Ground truth for the math identities is the spec (docs/CAPTURE.md):
// scale = longest_axis_px / L
// sqft = (mask_px / scale^2) / 144
// bdft = sqft * thickness_in
// Widths are sampled every `everyInches` along the approved axis.
describe('scale', () => {
it('scale = longest_axis_px / L', () => {
expect(m.scale(3360, 84)).toBe(40);
});
it('scale for fixture-b geometry (21 in)', () => {
// 21 in represented as 3360 px -> 160 px/in
expect(m.scale(3360, 21)).toBeCloseTo(160, 6);
});
});
describe('sqft', () => {
it('sqft = (mask_px / scale^2) / 144, 2 decimals', () => {
// scale=40 -> scale^2=1600; 1,000,000/1600=625; /144=4.3403 -> 4.34
expect(m.sqft(1_000_000, 40)).toBe(4.34);
});
it('zero-area mask -> 0', () => {
expect(m.sqft(0, 40)).toBe(0);
});
});
describe('bdft', () => {
it('bdft = sqft * thickness_in, 2 decimals (identity 1 sqft @ 1in = 1 bdft)', () => {
expect(m.bdft(4.34, 1)).toBeCloseTo(4.34, 6);
expect(m.bdft(4.34, 2.5)).toBe(10.85);
});
});
describe('e2e fixture math (cand_b: 21in x 1.75in)', () => {
// If the slab were a clean rectangle exactly 21in long, at 160px/in that is
// 3360 px long. Pick a width of 12in -> 1920px -> area 6,451,200 px.
const Lpx = 3360, L = 21, scale = 160, thickness = 1.75;
const widthPx = 1920; // 12in
const maskPx = Lpx * widthPx; // 6,451,200
it('sqft of a 21in x 12in slab = 1.75', () => {
expect(m.sqft(maskPx, scale)).toBe(1.75);
});
it('bdft of that slab at 1.75in = 3.06', () => {
const sqft = m.sqft(maskPx, scale);
expect(m.bdft(sqft, thickness)).toBe(3.06); // 1.75*1.75=3.0625 -> 3.06 (half-up)
});
});
describe('width sampling', () => {
it('samples perpendicular widths at 0,6,12,... in along the axis', () => {
// pxPerIn=160. axisLengthPx = 3360 (21in). widthsAt samples every 6in.
// Provide a constant width of 1920px (12in) at every sample point.
const pxPerIn = 160;
const axisLengthPx = 3360;
const constantWidthPx = 1920;
const out = m.sampledWidths(constantWidthPx, pxPerIn, axisLengthPx, 6);
// 21in / 6 = 3.5 -> sample points at 0,6,12,18 (and not 24>21). 4 samples.
expect(out.length).toBe(4);
for (const w of out) expect(w).toBe(12); // 1920px/160 = 12in
});
it('min/max/avg of sampled widths', () => {
const pxPerIn = 160;
const widths = m.sampledWidthsFrom(
// widths at px positions 0,960,1920,2880 (=0,6,12,18 in)
[1600, 1920, 2080, 1600],
pxPerIn
);
expect(widths.min).toBeCloseTo(10, 6);
expect(widths.max).toBeCloseTo(13, 6);
expect(widths.avg).toBeCloseTo(11.25, 6); // (10+12+13+10)/4 = 11.25
});
});
describe('1/8 inch stepping + rounding', () => {
it('snaps a length to the nearest 1/8 inch', () => {
expect(m.snapToEighth(10.1)).toBeCloseTo(10.125, 6); // 10 1/8
expect(m.snapToEighth(10.2)).toBeCloseTo(10.25, 6); // 10 2/8=1/4
expect(m.snapToEighth(10.0)).toBe(10);
});
it('rounds half-up to 2 decimals', () => {
expect(m.roundHalfUp(30.625, 2)).toBe(30.63);
expect(m.roundHalfUp(4.345, 2)).toBe(4.35);
expect(m.roundHalfUp(1.005, 2)).toBe(1.01);
});
});
-10
View File
@@ -1,10 +0,0 @@
import { describe, it, expect } from 'vitest';
// Smoke test for the M1-1 harness: proves the JS unit runner works end-to-end.
// This is deliberately trivial — later cards (M1-2+) replace it with the
// deterministic-core suite. It must pass to certify the harness is GREEN.
describe('M1 harness smoke', () => {
it('adds numbers (harness is live)', () => {
expect(1 + 1).toBe(2);
});
});
+15 -12
View File
@@ -1,14 +1,17 @@
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": false,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": false,
"moduleResolution": "bundler"
}
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"noEmit": true,
"lib": ["ES2022", "DOM", "DOM.Iterable", "WebWorker"],
"skipLibCheck": true,
"isolatedModules": true,
"resolveJsonModule": true,
"verbatimModuleSyntax": false,
"types": ["vite/client"]
},
"include": ["src/**/*.ts", "src/**/*.svelte", "vite.config.ts"],
"exclude": ["src/**/*.test.ts"]
}
-11
View File
@@ -1,11 +0,0 @@
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [sveltekit()],
test: {
environment: 'node',
include: ['tests/unit/**/*.test.js'],
exclude: ['tests/e2e/**', 'node_modules/**', 'build/**']
}
});
+8
View File
@@ -0,0 +1,8 @@
import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import sveltePreprocess from 'svelte-preprocess';
export default defineConfig({
plugins: [svelte({ preprocess: sveltePreprocess() })],
worker: { format: 'es' },
});
Binary file not shown.

Before

Width:  |  Height:  |  Size: 988 KiB

@@ -1,11 +0,0 @@
{
"fixture": "dji_mimo_20260109_152422_20260109152421_1771310617476_photo.JPG",
"label": "cand_a",
"source": "\\\\truenas.local\\Business\\Samples",
"orientation": "irregular",
"sheet": "black",
"image_px": [4000, 2256],
"length_in": 28.0,
"thickness_in": 2.25,
"notes": "Organic/irregular slab shape; stress-test for min-area bounding rectangle + length-axis. Length is the longest axis of the slab."
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 604 KiB

@@ -1,11 +0,0 @@
{
"fixture": "dji_mimo_20260109_155754_20260109155755_1771310617107_photo.JPG",
"label": "cand_c",
"source": "\\\\truenas.local\\Business\\Samples",
"orientation": "horizontal",
"sheet": "black",
"image_px": [4000, 2256],
"length_in": 21.0,
"thickness_in": 1.75,
"notes": "Clean elongated slab, length runs horizontally. Second clean-case fixture."
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 592 KiB

@@ -1,11 +0,0 @@
{
"fixture": "dji_mimo_20260109_155818_20260109155817_1771310617082_photo.JPG",
"label": "cand_b",
"source": "\\\\truenas.local\\Business\\Samples",
"orientation": "horizontal",
"sheet": "black",
"image_px": [4000, 2256],
"length_in": 21.0,
"thickness_in": 1.75,
"notes": "Clean elongated slab, length runs horizontally. Primary clean-case fixture for expected sqft/bdft/widths."
}