screengraft 0.50.0 → 0.52.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -29,6 +29,10 @@ prototype, then put the recording inside a real photograph.
29
29
  - **Realism pass** *(optional)* — matches the screen's white balance and grain to
30
30
  the light in the room, and can lift the device's real reflections from a
31
31
  screen-off frame of the same shot.
32
+ - **Depth of field** *(optional)* — a blur that grows across the screen in one
33
+ direction, glass edge included, so a screenshot on a phone shot at an angle
34
+ goes soft where the phone does. Direction and strength are yours; *Measure
35
+ from photo* proposes them from the photograph's own screen boundary.
32
36
  - **Emissive screens** *(optional)* — a display emits light *and* reflects the
33
37
  room, which is why a switched-off phone looks dark grey rather than black.
34
38
  Paint a true-black UI on flat and it reads as a hole cut in the photo. Turn
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "screengraft",
3
- "version": "0.50.0",
3
+ "version": "0.52.0",
4
4
  "description": "Put a UI screenshot or screen recording onto a photographed device screen with the perspective exactly right \u2014 a homography you confirm by hand, not a generative guess.",
5
5
  "keywords": [
6
6
  "mockup",
package/scripts/dof.py ADDED
@@ -0,0 +1,237 @@
1
+ """Depth of field across the screen — a blur that grows in one direction.
2
+
3
+ A phone photographed at an angle is a plane receding from the camera, so
4
+ focus falls off *linearly across the quad*: sharp at the near edge, softer
5
+ toward the far one. Two numbers describe it — the direction the blur grows in
6
+ (`angle`, degrees, photo space, 0 = toward +x, 90 = toward +y) and how fast
7
+ (`strength`, 0..1) — and both can be read off the photograph, because the
8
+ bezel around the screen already carries the camera's own defocus.
9
+
10
+ Blur is applied in PHOTO space, after the warp, to the premultiplied screen
11
+ layer and its mask together (`blur_layer`). Blurring the layer alone would
12
+ pull the black outside the quad into its edge and leave a sharp alpha edge
13
+ inside a soft bezel — the tell in a bad mockup. Premultiplied, the edge
14
+ softens exactly as the colour does.
15
+
16
+ Spatially varying Gaussian: DOF_LEVELS blur levels, per pixel a linear blend
17
+ of the two nearest. Standard, cheap (five separable blurs on the quad's
18
+ window), and byte-identical to no blur at strength 0.
19
+ """
20
+ import math
21
+
22
+ import cv2
23
+ import numpy as np
24
+
25
+ DOF_LEVELS = 5 # sigma steps between 0 and sigma_max (0 is the identity)
26
+ DOF_MAX_FRAC = 0.02 # strength 1.0 = sigma of 2% of the quad's longer side (16px on an 800px screen)
27
+
28
+
29
+ def sigma_max(corners, strength: float) -> float:
30
+ """Blur at the far edge, in photo pixels, for a given strength."""
31
+ q = np.asarray(corners, dtype=np.float64)
32
+ side = max(np.linalg.norm(q[1] - q[0]), np.linalg.norm(q[2] - q[1]),
33
+ np.linalg.norm(q[3] - q[2]), np.linalg.norm(q[0] - q[3]))
34
+ return float(np.clip(strength, 0.0, 1.0)) * DOF_MAX_FRAC * float(side)
35
+
36
+
37
+ def ramp(corners, angle_deg: float, x0: int, y0: int, w: int, h: int,
38
+ start: float = 0.0) -> np.ndarray:
39
+ """Per-pixel 0..1 blur fraction along `angle` across the quad, over a window.
40
+
41
+ `start` (0..1) is where focus ends and the blur begins, as a fraction of
42
+ the quad's extent along the direction: 0 at the nearest extent (the whole
43
+ screen ramps, the default), 0.5 sharp across the near half then ramping.
44
+ 1 at the farthest extent; pixels outside the quad clamp. The window is
45
+ (x0, y0, w, h) in photo pixels.
46
+ """
47
+ a = math.radians(angle_deg)
48
+ d = np.array([math.cos(a), math.sin(a)], dtype=np.float64)
49
+ q = np.asarray(corners, dtype=np.float64)
50
+ proj = q @ d
51
+ lo, hi = float(proj.min()), float(proj.max())
52
+ start = float(np.clip(start, 0.0, 0.95))
53
+ lo = lo + start * (hi - lo)
54
+ if hi - lo < 1e-6:
55
+ return np.zeros((h, w), dtype=np.float32)
56
+ xs = np.arange(x0, x0 + w, dtype=np.float64)[None, :]
57
+ ys = np.arange(y0, y0 + h, dtype=np.float64)[:, None]
58
+ t = (xs * d[0] + ys * d[1] - lo) / (hi - lo)
59
+ return np.clip(t, 0.0, 1.0).astype(np.float32)
60
+
61
+
62
+ def level_weights(t: np.ndarray, levels: int = DOF_LEVELS):
63
+ """Blend weights (levels, h, w): each pixel splits between its two nearest
64
+ sigma steps. Sums to 1 everywhere."""
65
+ pos = t * (levels - 1)
66
+ lo = np.floor(pos).astype(np.int32)
67
+ lo = np.clip(lo, 0, levels - 2)
68
+ f = (pos - lo).astype(np.float32)
69
+ W = np.zeros((levels,) + t.shape, dtype=np.float32)
70
+ rows, cols = np.indices(t.shape)
71
+ W[lo, rows, cols] = 1.0 - f
72
+ W[lo + 1, rows, cols] = f
73
+ return W
74
+
75
+
76
+ def _blur(img: np.ndarray, sigma: float) -> np.ndarray:
77
+ if sigma <= 0.0:
78
+ return img
79
+ k = int(2 * math.ceil(3 * sigma) + 1)
80
+ return cv2.GaussianBlur(img, (k, k), sigma, borderType=cv2.BORDER_REPLICATE)
81
+
82
+
83
+ class Field:
84
+ """Everything that does not change per frame: the ramp, the weights, the
85
+ blurred masks. `blur_layer` then costs `levels - 1` blurs of the colour."""
86
+
87
+ def __init__(self, corners, angle_deg: float, strength: float, mask: np.ndarray,
88
+ x0: int, y0: int, start: float = 0.0):
89
+ h, w = mask.shape[:2]
90
+ self.x0, self.y0 = x0, y0
91
+ self.smax = sigma_max(corners, strength)
92
+ self.sigmas = [self.smax * k / (DOF_LEVELS - 1) for k in range(DOF_LEVELS)]
93
+ t = ramp(corners, angle_deg, x0, y0, w, h, start)
94
+ self.W = level_weights(t) # (L, h, w)
95
+ m = mask.astype(np.float32) / 255.0
96
+ self.masks = [_blur(m, s) for s in self.sigmas] # each (h, w)
97
+ self.alpha = np.zeros_like(m)
98
+ for k in range(DOF_LEVELS):
99
+ self.alpha += self.W[k] * self.masks[k]
100
+ self.alpha3 = np.repeat(self.alpha[:, :, None], 3, axis=2)
101
+
102
+ @property
103
+ def pad(self) -> int:
104
+ """How far the soft edge reaches outside the sharp mask."""
105
+ return int(math.ceil(3 * self.smax))
106
+
107
+ def blur_layer(self, colour: np.ndarray) -> np.ndarray:
108
+ """Blur a warped screen window (float32 BGR, h×w×3) by the field.
109
+
110
+ Premultiplied by the sharp mask, blurred per level, blended by the
111
+ weights, then un-premultiplied by the blended mask — so the colour at
112
+ a soft edge is the screen's own, not the screen mixed with whatever
113
+ the warp put outside the quad. (The warp replicates the edge outward,
114
+ so in practice that is nearly the screen's own colour already; the
115
+ premultiplication is what keeps it exact rather than nearly.)
116
+ """
117
+ m0 = self.masks[0][:, :, None]
118
+ pm = colour * m0
119
+ num = np.zeros_like(colour)
120
+ for k in range(DOF_LEVELS):
121
+ bk = pm if self.sigmas[k] <= 0.0 else _blur(pm, self.sigmas[k])
122
+ num += self.W[k][:, :, None] * bk
123
+ den = np.maximum(self.alpha3, 1e-4)
124
+ out = num / den
125
+ return np.where(self.alpha3 > 1e-4, out, colour)
126
+
127
+
128
+ # ---------------------------------------------------------------- measure --
129
+
130
+ DOF_PROFILE_HALF = 14 # px either side of the screen boundary to fit the step
131
+ DOF_MIN_SPREAD = 1.6 # far-edge sigma / near-edge sigma below this = flat
132
+ DOF_MIN_SIGMA = 0.9 # a step narrower than this is the render's own antialiasing
133
+
134
+
135
+ def _edge_profiles(gray: np.ndarray, A, B, n, half: int, trim: float = 0.12):
136
+ """Intensity profiles ACROSS edge A→B, one column per sample point along
137
+ it, from `half` px inside the screen to `half` px outside, along the
138
+ outward unit normal `n`. Rectified with remap, so a tilted edge reads as
139
+ a straight step."""
140
+ A = np.asarray(A, dtype=np.float64); B = np.asarray(B, dtype=np.float64)
141
+ u = B - A
142
+ L = float(np.linalg.norm(u))
143
+ if L < 8:
144
+ return None
145
+ u /= L
146
+ s = np.linspace(trim * L, (1 - trim) * L, max(8, int((1 - 2 * trim) * L / 2)))
147
+ d = np.arange(-half, half + 1, dtype=np.float64)
148
+ xs = A[0] + s[None, :] * u[0] + d[:, None] * n[0]
149
+ ys = A[1] + s[None, :] * u[1] + d[:, None] * n[1]
150
+ return cv2.remap(gray.astype(np.float32), xs.astype(np.float32), ys.astype(np.float32),
151
+ cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT)
152
+
153
+
154
+ def step_sigma(profiles: np.ndarray) -> float:
155
+ """Blur of a step edge, in pixels, from profiles across it (rows = across,
156
+ columns = samples along the edge).
157
+
158
+ A step of amplitude A blurred by a Gaussian of sigma has a peak derivative
159
+ of A / (sigma * sqrt(2*pi)), so sigma = A / (sqrt(2*pi) * peak). Only the
160
+ DOMINANT transition in each column is read: the strongest gradient near
161
+ the boundary, with A taken between the plateaus 3 sigma-ish either side of
162
+ it. The second-moment estimate was tried first and saturated at ~5px on
163
+ sharp renders, because a ±14px profile crosses glass edge, bezel and body
164
+ and its derivative is spread over all three. Reduced by the median across
165
+ columns so a corner arc or a reflection on a few samples does not move it;
166
+ columns with no real step are left out.
167
+ """
168
+ smooth = cv2.GaussianBlur(profiles, (1, 3), 0.6) # tame sensor noise along the profile
169
+ g = np.diff(smooth, axis=0) # (2*half, N)
170
+ ag = np.abs(g)
171
+ n_across, N = ag.shape
172
+ half = n_across // 2
173
+ lo_i, hi_i = max(1, half - 6), min(n_across - 1, half + 6) # the boundary is near the centre
174
+ peak = lo_i + ag[lo_i:hi_i].argmax(axis=0)
175
+ out = []
176
+ for j in range(N):
177
+ pk = int(peak[j]); gm = float(ag[pk, j])
178
+ if gm < 1.5:
179
+ continue
180
+ # plateaus: 4..9 px either side of the peak, clamped to the profile
181
+ a0, a1 = max(0, pk - 9), max(0, pk - 4)
182
+ b0, b1 = min(n_across, pk + 5), min(n_across, pk + 10)
183
+ if a1 <= a0 or b1 <= b0:
184
+ continue
185
+ A = abs(float(smooth[b0:b1, j].mean()) - float(smooth[a0:a1, j].mean()))
186
+ if A < 12.0: # no step worth reading
187
+ continue
188
+ out.append(A / (math.sqrt(2 * math.pi) * gm))
189
+ if len(out) < 4:
190
+ return 0.0
191
+ return float(np.median(out))
192
+
193
+
194
+ def measure(photo: np.ndarray, corners) -> dict:
195
+ """Direction and strength of defocus from the screen's own boundary.
196
+
197
+ Four blur widths, one per edge (sigma in pixels, `step_sigma`), the
198
+ edge midpoints as positions → a plane sigma(x, y) = a·x + b·y + c by least
199
+ squares. The blur grows along the plane's gradient: angle = atan2(b, a).
200
+ Strength is the far edge's sigma against DOF_MAX_FRAC of the longer side,
201
+ which is what the blur uses, so a measured strength reproduces the measured
202
+ blur. Below DOF_MIN_SPREAD between the softest and sharpest edge the photo
203
+ is in focus across the screen (or uniformly soft, which is not depth of
204
+ field) and the answer is `flat`.
205
+ """
206
+ gray = cv2.cvtColor(photo, cv2.COLOR_BGR2GRAY) if photo.ndim == 3 else photo
207
+ q = np.asarray(corners, dtype=np.float64)
208
+ centre = q.mean(axis=0)
209
+ sig, mids = [], []
210
+ for i in range(4):
211
+ A, B = q[i], q[(i + 1) % 4]
212
+ u = B - A
213
+ n = np.array([-u[1], u[0]]) / max(np.linalg.norm(u), 1e-6)
214
+ mid = (A + B) / 2
215
+ if np.dot(n, centre - mid) > 0: # pointing in: flip to outward
216
+ n = -n
217
+ prof = _edge_profiles(gray, A, B, n, DOF_PROFILE_HALF)
218
+ if prof is None:
219
+ continue
220
+ sg = step_sigma(prof)
221
+ if sg > 0.0:
222
+ sig.append(sg); mids.append(mid)
223
+ if len(sig) < 3:
224
+ return {"angle": 0.0, "strength": 0.0, "flat": True, "sigma": [round(v, 2) for v in sig]}
225
+ sig = np.array(sig); mids = np.array(mids)
226
+ lo = max(float(sig.min()), DOF_MIN_SIGMA)
227
+ spread = float(sig.max() / lo)
228
+ out = {"sigma": sig.round(2).tolist(), "spread": round(spread, 2)}
229
+ if spread < DOF_MIN_SPREAD:
230
+ out.update({"angle": 0.0, "strength": 0.0, "flat": True})
231
+ return out
232
+ Amat = np.column_stack([mids[:, 0], mids[:, 1], np.ones(len(mids))])
233
+ (a, b, c), *_ = np.linalg.lstsq(Amat, sig, rcond=None)
234
+ angle = math.degrees(math.atan2(b, a)) % 360.0
235
+ strength = float(np.clip((sig.max() - lo) / max(sigma_max(q, 1.0), 1e-6), 0.0, 1.0))
236
+ out.update({"angle": round(angle, 1), "strength": round(strength, 3), "flat": False})
237
+ return out
package/scripts/scan.py CHANGED
@@ -53,7 +53,13 @@ def thumb(item, out_dir: str, size: int = 320):
53
53
  """Write a small JPEG thumbnail; returns its path or None."""
54
54
  os.makedirs(out_dir, exist_ok=True)
55
55
  base = os.path.splitext(item["name"])[0]
56
- out = os.path.join(out_dir, f"{abs(hash(item['path']))}_{base[:40]}.jpg")
56
+ # The mtime is part of the name: a file overwritten in place gets a new
57
+ # thumbnail instead of the one made from its old bytes (12 Sep 2026).
58
+ try:
59
+ stamp = int(os.path.getmtime(item["path"]))
60
+ except OSError:
61
+ stamp = 0
62
+ out = os.path.join(out_dir, f"{abs(hash(item['path']))}_{stamp}_{base[:40]}.jpg")
57
63
  if os.path.exists(out):
58
64
  return out
59
65
  if sys.platform == "darwin":
package/scripts/ui.py CHANGED
@@ -49,6 +49,7 @@ import numpy as np # noqa: E402
49
49
  import detect as D # noqa: E402
50
50
  import fitfile as FF # noqa: E402
51
51
  import fits as FIT # noqa: E402
52
+ import dof as DOF # noqa: E402
52
53
  import scan as S # noqa: E402
53
54
  import warp as W # noqa: E402
54
55
 
@@ -577,7 +578,8 @@ PREVIEW_SECONDS = 6.0
577
578
 
578
579
  def _render_worker(photo, video_path, corners, dest, radius_px, gr, grain, preset, fit_frame,
579
580
  blend="replace", reflection=None, result=None, kind="render",
580
- start_frame=0, max_frames=None, *, smoothing=0.0):
581
+ start_frame=0, max_frames=None, *, smoothing=0.0,
582
+ dof_angle=0.0, dof_strength=0.0, dof_start=0.0):
581
583
  """Encode the clip, and only if that SUCCEEDS publish what it produced.
582
584
 
583
585
  `result` is the sidecar this render would write. It is handed to the worker
@@ -602,7 +604,9 @@ def _render_worker(photo, video_path, corners, dest, radius_px, gr, grain, prese
602
604
  blend=blend,
603
605
  reflection=(W.DEFAULT_REFLECTION if reflection is None
604
606
  else reflection),
605
- start_frame=start_frame, max_frames=max_frames)
607
+ start_frame=start_frame, max_frames=max_frames,
608
+ dof_angle=dof_angle, dof_strength=dof_strength,
609
+ dof_start=dof_start)
606
610
  if kind == "preview":
607
611
  # A preview publishes NOTHING. It is not a save: no sidecar, no fit
608
612
  # file, and above all not the session output -- /api/import reads
@@ -664,6 +668,20 @@ def _blend_args(b):
664
668
  return "emissive", float(max(0.0, min(1.0, float(r))))
665
669
 
666
670
 
671
+ def _dof_args(b):
672
+ """(dof_angle, dof_strength, dof_start) from the page. Absent or null
673
+ strength means 0 -- no field, and every path in Plan untouched -- so a
674
+ client that predates depth of field and a sidecar replayed through the CLI
675
+ both reproduce. `start` absent means 0: the whole screen ramps."""
676
+ try:
677
+ strength = float(b.get("dof_strength") or 0.0)
678
+ angle = float(b.get("dof_angle") or 0.0)
679
+ start = float(b.get("dof_start") or 0.0)
680
+ except (TypeError, ValueError):
681
+ return 0.0, 0.0, 0.0
682
+ return angle % 360.0, float(min(max(strength, 0.0), 1.0)), float(min(max(start, 0.0), 0.95))
683
+
684
+
667
685
  ROLES = ("photo", "screenshot")
668
686
 
669
687
 
@@ -699,6 +717,15 @@ def _adopt(role, path):
699
717
  e = FIT.recall(FIT.key_for(im))
700
718
  if e:
701
719
  meta["remembered"] = e
720
+ # mtime rides along as the page's cache-buster: the same path with new
721
+ # bytes (imported, cleared, overwritten on disk, imported again) used to
722
+ # come back as the OLD picture in every <img> the browser had cached --
723
+ # chip swatch, popover preview, the canvas itself for a photo -- while the
724
+ # composite, which the server renders from disk, was already new.
725
+ try:
726
+ meta["mtime"] = int(os.path.getmtime(real))
727
+ except OSError:
728
+ meta["mtime"] = 0
702
729
  return {"path": real, "size": [im.shape[1], im.shape[0]], **meta}
703
730
 
704
731
 
@@ -941,6 +968,29 @@ class Handler(BaseHTTPRequestHandler):
941
968
  "with status=done.",
942
969
  }))
943
970
 
971
+ if u.path == "/api/dof":
972
+ # Depth of field read off the photograph's own screen boundary:
973
+ # the direction the blur grows in and how far, from the blur
974
+ # width of each edge of the quad. `flat` means the bezel is
975
+ # sharp all round and the feature has nothing to match.
976
+ ppath = SESSION.state.get("photo")
977
+ if not ppath:
978
+ return self._json({"error": "no photo chosen"}, 400)
979
+ photo, _ = _read_image(ppath)
980
+ corners = _quad(b["corners"])
981
+ return self._json(DOF.measure(photo, corners))
982
+
983
+ if u.path == "/api/job/cancel":
984
+ # The page gave up on a pending job. Marked, not deleted: the
985
+ # agent's complete_job then answers "already cancelled" instead
986
+ # of finding no job and guessing, and wait_for_job skips it.
987
+ job = SESSION.read_job()
988
+ if job and job.get("status") == "pending":
989
+ job["status"] = "cancelled"
990
+ job["completed"] = time.time()
991
+ _write_json_atomic(SESSION.job_path, job)
992
+ return self._json(job or {"status": "none"})
993
+
944
994
  if u.path == "/api/job/adopt":
945
995
  # page calls this once job.status == done, to make the export the screenshot
946
996
  with open(SESSION.job_path) as f:
@@ -1129,7 +1179,10 @@ class Handler(BaseHTTPRequestHandler):
1129
1179
  gr, grain, "web", fit_frame,
1130
1180
  blend, reflection, None, "preview",
1131
1181
  fit_frame, max_frames),
1132
- kwargs={"smoothing": _smoothing(b)}).start()
1182
+ kwargs={"smoothing": _smoothing(b),
1183
+ "dof_angle": _dof_args(b)[0],
1184
+ "dof_strength": _dof_args(b)[1],
1185
+ "dof_start": _dof_args(b)[2]}).start()
1133
1186
  except BaseException:
1134
1187
  with RENDER_LOCK:
1135
1188
  RENDER.update(state="error", message="could not start the preview")
@@ -1163,6 +1216,7 @@ class Handler(BaseHTTPRequestHandler):
1163
1216
  gr = float(b.get("grade") if b.get("grade") is not None else 0.0)
1164
1217
  grain = bool(b.get("grain", gr > 0))
1165
1218
  blend, reflection = _blend_args(b)
1219
+ dof_angle, dof_strength, dof_start = _dof_args(b)
1166
1220
  preset = "prores" if b.get("preset") == "prores" else "web"
1167
1221
  ext = ".mov" if preset == "prores" else ".mp4"
1168
1222
  os.makedirs(OUT_DIR, exist_ok=True)
@@ -1184,6 +1238,7 @@ class Handler(BaseHTTPRequestHandler):
1184
1238
  "grade": gr, "grain": grain,
1185
1239
  "video": True, "preset": preset, "fit_frame": fit_frame,
1186
1240
  "blend": blend, "reflection": reflection,
1241
+ "dof_angle": dof_angle, "dof_strength": dof_strength, "dof_start": dof_start,
1187
1242
  # A render is always the whole clip; only the preview
1188
1243
  # passes a segment. Recorded anyway, because the
1189
1244
  # sidecar's promise is EVERY argument that changes the
@@ -1210,7 +1265,10 @@ class Handler(BaseHTTPRequestHandler):
1210
1265
  args=(photo, spath, corners, dest, radius_px,
1211
1266
  gr, grain, preset, fit_frame,
1212
1267
  blend, reflection, result),
1213
- kwargs={"smoothing": _smoothing(b)}).start()
1268
+ kwargs={"smoothing": _smoothing(b),
1269
+ "dof_angle": dof_angle,
1270
+ "dof_strength": dof_strength,
1271
+ "dof_start": dof_start}).start()
1214
1272
  except BaseException:
1215
1273
  # If the thread cannot even be created, the flag must not
1216
1274
  # outlive the request.
@@ -1233,10 +1291,13 @@ class Handler(BaseHTTPRequestHandler):
1233
1291
  gr = float(b.get("grade") if b.get("grade") is not None else 0.0)
1234
1292
  blend, reflection = _blend_args(b)
1235
1293
  smoothing = _smoothing(b)
1294
+ dof_angle, dof_strength, dof_start = _dof_args(b)
1236
1295
  out = W.compose(photo, shot, corners, radius_px,
1237
1296
  corner_smoothing=smoothing,
1238
1297
  grade=gr, grain=bool(b.get("grain", gr > 0)),
1239
- blend=blend, reflection=reflection)
1298
+ blend=blend, reflection=reflection,
1299
+ dof_angle=dof_angle, dof_strength=dof_strength,
1300
+ dof_start=dof_start)
1240
1301
  SESSION.update(corners=corners, radius_frac=frac, device=b.get("device"),
1241
1302
  grade=gr)
1242
1303
  if u.path == "/api/preview":
@@ -1272,6 +1333,7 @@ class Handler(BaseHTTPRequestHandler):
1272
1333
  "corner_smoothing": smoothing,
1273
1334
  "grade": gr, "grain": bool(b.get("grain", gr > 0)),
1274
1335
  "blend": blend, "reflection": reflection,
1336
+ "dof_angle": dof_angle, "dof_strength": dof_strength, "dof_start": dof_start,
1275
1337
  "saved": time.time()}
1276
1338
  # A fit is remembered when it PRODUCED something, not while it
1277
1339
  # is being dragged: a quad on the canvas is a work in progress,
package/scripts/warp.py CHANGED
@@ -37,6 +37,7 @@ import cv2
37
37
  import numpy as np
38
38
 
39
39
  import grade as _grade # M2: the realism pass
40
+ import dof as _dof # depth of field across the screen
40
41
 
41
42
 
42
43
  MASK_SS = 4 # destination-space supersampling for the screen's edge
@@ -267,7 +268,8 @@ class Plan:
267
268
  def __init__(self, photo: np.ndarray, frame_shape, corners,
268
269
  corner_radius: float = 0.0, grain: bool = False,
269
270
  blend: str = "replace", reflection: float = DEFAULT_REFLECTION,
270
- corner_smoothing: float = 0.0):
271
+ corner_smoothing: float = 0.0,
272
+ dof_angle: float = 0.0, dof_strength: float = 0.0, dof_start: float = 0.0):
271
273
  dst_quad = np.array(corners, dtype=np.float32)
272
274
  if shoelace_area(dst_quad) < 1.0:
273
275
  raise ValueError("degenerate quad (near-zero area) — check corner order TL,TR,BR,BL")
@@ -313,9 +315,24 @@ class Plan:
313
315
  # Integer bbox of the quad, clamped to the canvas and padded by a pixel
314
316
  # so the antialiased edge is never clipped.
315
317
  xs, ys = dst_quad[:, 0], dst_quad[:, 1]
316
- bx0, by0 = max(0, int(np.floor(xs.min())) - 1), max(0, int(np.floor(ys.min())) - 1)
317
- bx1, by1 = min(pw, int(np.ceil(xs.max())) + 2), min(ph, int(np.ceil(ys.max())) + 2)
318
+ # Depth of field widens the window: the soft edge reaches 3 sigma past
319
+ # the sharp mask, and a window that clipped it would leave a hard line
320
+ # exactly where the blur was meant to remove one.
321
+ self.dof_strength = float(np.clip(dof_strength, 0.0, 1.0))
322
+ self.dof_angle = float(dof_angle)
323
+ self.dof_start = float(np.clip(dof_start, 0.0, 0.95))
324
+ reach = int(np.ceil(3 * _dof.sigma_max(dst_quad, self.dof_strength))) if self.dof_strength > 0 else 0
325
+ bx0, by0 = max(0, int(np.floor(xs.min())) - 1 - reach), max(0, int(np.floor(ys.min())) - 1 - reach)
326
+ bx1, by1 = min(pw, int(np.ceil(xs.max())) + 2 + reach), min(ph, int(np.ceil(ys.max())) + 2 + reach)
318
327
  self.bbox = (bx0, by0, bx1, by1) if bx1 > bx0 and by1 > by0 else None
328
+ # The field is built once: ramp, level weights, the blurred masks.
329
+ # At strength 0 there is no field and every path below is untouched,
330
+ # which is what keeps every earlier sidecar reproducing byte for byte.
331
+ self.dof = None
332
+ if self.dof_strength > 0 and self.bbox is not None:
333
+ self.dof = _dof.Field(dst_quad, self.dof_angle, self.dof_strength,
334
+ self.warped_mask[by0:by1, bx0:bx1], bx0, by0,
335
+ start=self.dof_start)
319
336
 
320
337
  def _prep(self, frame: np.ndarray, bbox=None) -> np.ndarray:
321
338
  """Warp one frame. With `bbox`, warp only that window of the canvas.
@@ -377,13 +394,21 @@ class Plan:
377
394
  simplest code is the one to trust, and on for video renders. Both
378
395
  produce identical bytes; test_video.py asserts it rather than assuming.
379
396
  """
380
- if fast and self.bbox is not None:
397
+ # Depth of field always takes the windowed path: the field is built on
398
+ # the window, and the still and video renders must share one code
399
+ # path here for the same reason they share Plan.
400
+ if (fast or self.dof is not None) and self.bbox is not None:
381
401
  x0, y0, x1, y1 = self.bbox
382
402
  warped_screen = self._prep(frame, self.bbox)
383
403
  if self.grade_params is not None:
384
404
  warped_screen = _grade.apply_light(warped_screen, self.grade_params)
385
405
  out = self.photo.copy()
386
406
  win = self.mask3[y0:y1, x0:x1]
407
+ if self.dof is not None:
408
+ # Blur colour and mask together, premultiplied, so the glass
409
+ # edge goes soft exactly as the far end of the bezel already is.
410
+ warped_screen = self.dof.blur_layer(warped_screen.astype(np.float32))
411
+ win = self.dof.alpha3
387
412
  pw = self.photo[y0:y1, x0:x1]
388
413
  src = self._blend(pw, warped_screen)
389
414
  out[y0:y1, x0:x1] = np.clip(
@@ -410,7 +435,9 @@ def compose(photo: np.ndarray, screenshot: np.ndarray, corners, corner_radius: f
410
435
  corner_smoothing: float = 0.0,
411
436
  grade: float = 0.0, grain: bool = False, screen_off: np.ndarray = None,
412
437
  specular: float = 0.75, blend: str = "replace",
413
- reflection: float = DEFAULT_REFLECTION) -> np.ndarray:
438
+ reflection: float = DEFAULT_REFLECTION,
439
+ dof_angle: float = 0.0, dof_strength: float = 0.0,
440
+ dof_start: float = 0.0) -> np.ndarray:
414
441
  """Warp `screenshot` into the quad `corners` (TL,TR,BR,BL, photo pixels) on `photo`.
415
442
 
416
443
  Single resampling pass at the photo's resolution; deterministic. This is the
@@ -423,7 +450,8 @@ def compose(photo: np.ndarray, screenshot: np.ndarray, corners, corner_radius: f
423
450
  # asserted byte-identical to this function's output.
424
451
  plan = Plan(photo, screenshot.shape, corners, corner_radius, grain=grain,
425
452
  corner_smoothing=corner_smoothing,
426
- blend=blend, reflection=reflection)
453
+ blend=blend, reflection=reflection,
454
+ dof_angle=dof_angle, dof_strength=dof_strength, dof_start=dof_start)
427
455
  plan.bind_grade(screenshot, grade)
428
456
  return plan.render(screenshot, screen_off=screen_off, specular=specular)
429
457
 
@@ -510,7 +538,9 @@ def compose_video(photo: np.ndarray, video_path: str, corners, output: str,
510
538
  preset: str = "web", fit_frame: int = 0, audio: bool = True,
511
539
  frames_dir: str = None, progress=None, blend: str = "replace",
512
540
  reflection: float = DEFAULT_REFLECTION,
513
- start_frame: int = 0, max_frames: int = None) -> dict:
541
+ start_frame: int = 0, max_frames: int = None,
542
+ dof_angle: float = 0.0, dof_strength: float = 0.0,
543
+ dof_start: float = 0.0) -> dict:
514
544
  """Inject a VIDEO into a still photo. The photo does not move, so there is
515
545
  exactly one homography and the whole of Plan is computed once.
516
546
 
@@ -537,7 +567,8 @@ def compose_video(photo: np.ndarray, video_path: str, corners, output: str,
537
567
  first = read_frame_at(video_path, fit_frame)
538
568
  plan = Plan(photo, first.shape, corners, corner_radius, grain=grain,
539
569
  corner_smoothing=corner_smoothing,
540
- blend=blend, reflection=reflection)
570
+ blend=blend, reflection=reflection,
571
+ dof_angle=dof_angle, dof_strength=dof_strength, dof_start=dof_start)
541
572
  plan.bind_grade(first, grade)
542
573
 
543
574
  ph, pw = photo.shape[:2]
@@ -5,7 +5,7 @@ description: Injects a UI screenshot OR a screen recording onto a photographed d
5
5
 
6
6
  # Inject a screenshot onto a photographed device
7
7
 
8
- **What ships (v0.50):** a local browser UI (`scripts/ui.py`) that walks the designer through the whole job — pick the photo and the screen source, which may be an image **or a video** (recent Desktop/Downloads images, drag-drop, browse, path, or a **Figma frame link**), auto-detect the screen as a starting position — and when detection cannot tell which region is a screen, **Point at screen**: one click inside it and the detector uses that point — or, if this photograph has been fitted before, **the fit it was saved with comes back** as the starting position instead of a detection, recognised by the photo's own pixels so a rename or a drag-drop still match — and every save also writes a **portable `.fit.json` beside the mockup** that can be dropped back onto the page later, which is how a fit survives a re-export, another machine, or someone else's hands — then **match the four edges** (drag an edge's middle to slide it, near an end to pivot; corners still draggable) with canvas navigation that follows the usual conventions — **hold ⌘ and scroll to zoom to the pointer, hold space and drag to pan** — and a rectified strip loupe. The fit and the composite sit **side by side and always have** — the result pane re-renders as you drag, which is how a corner gets judged, so it is the layout rather than a mode you can switch off. Then an on-by-default realism pass that colour-matches the source to the photo's light, **Save** (or **Render**, for a video) into the project folder (`--out-dir`), and a **Send to Claude** button that reaches you through the plugin's own MCP server. The UI is a hand port of the project's Figma design file — dark only.
8
+ **What ships (v0.52):** a local browser UI (`scripts/ui.py`) that walks the designer through the whole job — pick the photo and the screen source, which may be an image **or a video** (recent Desktop/Downloads images, drag-drop, browse, path, or a **Figma frame link**), auto-detect the screen as a starting position — and when detection cannot tell which region is a screen, **Point at screen**: one click inside it and the detector uses that point — or, if this photograph has been fitted before, **the fit it was saved with comes back** as the starting position instead of a detection, recognised by the photo's own pixels so a rename or a drag-drop still match — and every save also writes a **portable `.fit.json` beside the mockup** that can be dropped back onto the page later, which is how a fit survives a re-export, another machine, or someone else's hands — then **match the four edges** (drag an edge's middle to slide it, near an end to pivot; corners still draggable) with canvas navigation that follows the usual conventions — **hold ⌘ and scroll to zoom to the pointer, hold space and drag to pan** — and a rectified strip loupe. The fit and the composite sit **side by side and always have** — the result pane re-renders as you drag, which is how a corner gets judged, so it is the layout rather than a mode you can switch off. Then an on-by-default realism pass that colour-matches the source to the photo's light, **Save** (or **Render**, for a video) into the project folder (`--out-dir`), and a **Send to Claude** button that reaches you through the plugin's own MCP server. The UI is a hand port of the project's Figma design file — dark only.
9
9
 
10
10
  The geometry is exact (`warp.py`); the detection is advisory (`detect.py`) and the human corrects it. **When a detection is wrong and you want to know why**, ask for the candidate list: `POST /api/detect {"trace": true}` writes `<session>/candidates.json`, or run `python3 scripts/detect.py --photo P --out-corners /tmp/c.json --trace /tmp/t.json` (add `--click X,Y`). Every candidate quad is in there with its score and whether it was accepted, rejected, never reached, or filtered out by the click — which is what separates "the screen was never proposed" from "it was proposed and something else won".
11
11
 
@@ -13,6 +13,8 @@ The geometry is exact (`warp.py`); the detection is advisory (`detect.py`) and t
13
13
 
14
14
  **The realism pass ships and is ON by default** (`grade.py`): it matches the injected screen's white balance and grain to the light around it, at a strength the designer sets in the rail. It can also lift the device's real specular highlights from a screen-off reference frame, though the UI cannot supply one yet. Off is a first-class choice and keeps the screenshot's colour exactly — say so if the user is reviewing brand colour.
15
15
 
16
+ **Depth of field** *(off by default)*: a phone shot at an angle is a plane receding from the camera, so its far end is softer than its near end, and a screenshot pasted pin-sharp across all of it gives the fake away. On, the screenshot blurs across the screen in one direction — **direction** (where the blur grows toward) and **strength** — and the glass edge softens with it. **Measure from photo** reads both off the photograph's own screen boundary; on a real photograph that works, on a *mockup template* the device is usually rendered sharp with the blur only on the background, so it answers "flat" and the designer sets it by eye. The measured strength is a floor (the estimator saturates around 5px of blur), never a ceiling. **The gizmo on the result** is the primary control: a solid line over the screen where focus ends (drag to move, its end pips to turn), a dashed line where the blur reaches ~6px (drag closer for stronger, further for gentler); it fades when the pointer leaves the pane. The sliders and the gizmo are one model. Suggest it when the photo has visible bokeh — a blurred hand, table edge or background — and the composite's screen looks pasted on. The live in-place playback cannot show it; the composite and Render preview do.
17
+
16
18
  **Video ships too.** The screen source can be a video (mp4/mov/webm) as well as a still — pick it exactly like a screenshot, choose which frame to match the edges on, **press Play and the clip runs on the photo immediately** — the browser warps it onto the same four corners with the same corner radius and approximates the emissive blend, so placement and motion can be judged with no wait; **Render preview** composites a few seconds through the real pipeline when the grade, grain and true blend are what you need to see — and the primary button becomes **Render**. The photo does not move, so there is one homography and every frame gets the same geometry; the light match is measured once from the frame you fitted on, so the screen cannot pulse as the UI scrolls. Output is H.264 at CRF 16 (near-visually-lossless) or ProRes 422 HQ. This is what pairs with a prototype recording: record the prototype, then inject the recording into a real photograph.
17
19
 
18
20
  Still missing: **no ML detection** (measured, and it segments the phone body rather than the glass, so it is not shipped), **no occluder handling** — a finger or glare in front of the screen gets painted over — and **no camera motion**: the photo must be a still, so a clip of a moving phone is not this. Also, detection **abstains** rather than guessing when the background is itself neutral (a pale tiled floor, a plain wall); the edges get placed by hand there, which is normal, not a failure. Say so if any of it matters for the photo.
@@ -94,7 +96,7 @@ It parks until the user presses a button and returns within ~150 ms of the press
94
96
 
95
97
  | result | what to do |
96
98
  |---|---|
97
- | `status=job`, `job.type="figma_export"` | Extract `fileKey` and `nodeId` from `job.url` (`1-2` → `1:2`), export via the Figma MCP (`download_assets`, PNG, scale 3), save to `job.save_to`, then `complete_job(status="done", path=<saved file>)`. Re-arm. |
99
+ | `status=job`, `job.type="figma_export"` | Extract `fileKey` and `nodeId` from `job.url` (`1-2` → `1:2`), export via the Figma MCP (`download_assets`, PNG, scale 3), save to `job.save_to`, then `complete_job(status="done", path=<saved file>)`. If `complete_job` answers that the job is already **cancelled**, the user pressed Cancel in the page — say so in one line and re-arm; do not retry. Re-arm. |
98
100
  | `status=job`, `job.type="present"` | `present_files` on `job.paths`, report what you checked in the composite (§3), then `complete_job(status="done")`. Re-arm. |
99
101
  | `status=timeout` | Nothing pressed yet. Call again to keep waiting. Re-arm two or three times, then stop and say you've stopped waiting — do not loop forever burning turns. |
100
102
  | `status=ui_closed` | The UI exited. Stop; say so. |
package/ui/index.html CHANGED
@@ -625,6 +625,22 @@
625
625
 
626
626
  #outPane{display:grid}
627
627
  #outWrap{overflow:auto;background:var(--well);display:grid;box-shadow:inset 0 1px 3px rgba(0,0,0,.5)}
628
+ /* The depth-of-field gizmo: an SVG laid exactly over the composite, drawn
629
+ in PHOTO pixels (its viewBox is the photo) so every coordinate is the
630
+ engine's own. Faint until the pointer is over the pane, so it never sits
631
+ on the composite being judged; solid while a handle is being dragged. */
632
+ #outWrap{position:relative}
633
+ #dofGizmo{position:absolute;pointer-events:none;opacity:.4;transition:opacity .15s;overflow:visible}
634
+ #outWrap:hover #dofGizmo,#dofGizmo.drag{opacity:1}
635
+ #dofGizmo .case{stroke:rgba(0,0,0,.6);fill:none;vector-effect:non-scaling-stroke}
636
+ #dofGizmo .focus{stroke:#f5623d;stroke-width:1.5;fill:none;vector-effect:non-scaling-stroke}
637
+ #dofGizmo .far{stroke:#f5623d;stroke-width:1.25;stroke-dasharray:6 6;fill:none;vector-effect:non-scaling-stroke}
638
+ #dofGizmo .far.out{stroke-dasharray:3 7;opacity:.75}
639
+ #dofGizmo .h{fill:#f5623d;stroke:#fff;stroke-width:1.5;vector-effect:non-scaling-stroke;pointer-events:auto;cursor:grab}
640
+ #dofGizmo .h.hollow{fill:#1a1a1a}
641
+ #dofGizmo .h.pip{fill:#f5623d}
642
+ #dofGizmo .h:active{cursor:grabbing}
643
+ #dofGizmo text{font:600 11px system-ui,-apple-system,sans-serif;fill:#fff;paint-order:stroke;stroke:rgba(0,0,0,.75);stroke-width:3px;stroke-linejoin:round;pointer-events:none}
628
644
  #outImg,#outVid{display:block;margin:auto}
629
645
  /* The live layer stacks the clip on the photo. `position:relative` on the
630
646
  wrapper and `transform-origin:0 0` on the video are what make the matrix3d
@@ -1023,7 +1039,7 @@
1023
1039
  <span class="spacer"></span>
1024
1040
  <span class="sm" style="color:var(--mute)">shares zoom &amp; pan</span>
1025
1041
  </div>
1026
- <div id="outWrap"><span class="empty" id="outEmpty">The composite appears here and updates as you drag.</span><img id="outImg" alt="" hidden><video id="outVid" hidden loop muted playsinline controls></video><!--
1042
+ <div id="outWrap"><span class="empty" id="outEmpty">The composite appears here and updates as you drag.</span><img id="outImg" alt="" hidden><video id="outVid" hidden loop muted playsinline controls></video><svg id="dofGizmo" hidden aria-label="Depth of field: drag the solid line to place focus, its ends to turn it, the dashed line for strength"></svg><!--
1027
1043
  The live layer: the PHOTOGRAPH with the source clip warped onto the
1028
1044
  fitted quad by the browser. Not the composite — the composite already
1029
1045
  has a frame burned into it, so playing over that would show two screens.
@@ -1049,7 +1065,7 @@
1049
1065
  end. Icon-only, so each carries an aria-label — a title alone
1050
1066
  names it for a mouse and not for a screen reader. -->
1051
1067
  <button class="sm icon" id="playlive" aria-label="Play the clip on the photo"
1052
- title="Play the clip on the photo, right now. The browser applies the same four corners and corner radius, and approximates emissive with screen blending; the colour grade and grain are not in this view.">
1068
+ title="Play the clip on the photo, right now. The browser applies the same four corners and corner radius, and approximates emissive with screen blending; the colour grade, grain and depth of field are not in this view.">
1053
1069
  <svg viewBox="0 0 12 12" width="11" height="11" aria-hidden="true" focusable="false"><path d="M3 1.5 10 6 3 10.5Z" fill="currentColor"/></svg>
1054
1070
  </button>
1055
1071
  <button class="sm" id="playclip"
@@ -1113,6 +1129,35 @@
1113
1129
  </div>
1114
1130
  </div></div>
1115
1131
  </div>
1132
+ <div class="sect" id="secDof" data-on="0">
1133
+ <div class="sect-top">
1134
+ <div class="sect-left">
1135
+ <h4>Depth of field</h4>
1136
+ <button class="info" type="button" aria-label="About depth of field"><svg viewBox="0 0 16 16" fill="none" aria-hidden="true"><path fill="currentColor" d="M8.36 1.01C12.06 1.2 15 4.26 15 8l-.01.36C14.8 12.06 11.74 15 8 15l-.36-.01C4.06 14.81 1.19 11.94 1.01 8.36L1 8c0-3.87 3.13-7 7-7l.36.01ZM8 2C4.69 2 2 4.69 2 8c0 3.31 2.69 6 6 6 3.31 0 6-2.69 6-6 0-3.31-2.69-6-6-6Zm.5 8l.5 0v1L7 11v-1h.5V7H7V6h1.5v4ZM7.9 4c.33 0 .6.27.6.6a.6.6 0 1 1-1.21 0c0-.33.28-.6.61-.6Z"/></svg><span class="tip" role="tooltip">A phone shot at an angle is a plane receding from the camera, so its far end is softer than its near end — and a screenshot pasted pin-sharp across all of it gives the fake away. On, the screenshot blurs across the screen in one direction, and the glass edge softens with it. Measure reads the direction and a starting strength off your photo's own bezel; the strength it finds is a floor, so raise it if the far end of the phone looks softer than the screen.</span></button>
1137
+ </div>
1138
+ <button class="switch" id="dofBtn" role="switch" aria-checked="false" aria-label="Depth of field">
1139
+ <span class="sw-track"><span class="sw-handle"></span><span class="sw-grip"></span></span>
1140
+ </button>
1141
+ </div>
1142
+ <div class="sect-body"><div class="sect-body-in">
1143
+ <div style="display:grid;gap:var(--s2)">
1144
+ <div style="display:flex;align-items:center;justify-content:space-between;gap:var(--s2)">
1145
+ <span class="sm" style="color:var(--mute)">Blur grows toward</span>
1146
+ <span class="sm" id="dofDirVal" style="font-variant-numeric:tabular-nums"></span>
1147
+ </div>
1148
+ <input type="range" id="dofDir" min="0" max="355" step="5" value="90" aria-label="Direction the blur grows in">
1149
+ <div style="display:flex;align-items:center;justify-content:space-between;gap:var(--s2)">
1150
+ <span class="sm" style="color:var(--mute)">Strength</span>
1151
+ <span class="sm" id="dofStrVal" style="font-variant-numeric:tabular-nums"></span>
1152
+ </div>
1153
+ <input type="range" id="dofStr" min="0" max="1" step="0.05" value="0.3" aria-label="Blur strength at the far edge">
1154
+ <div style="display:flex;align-items:center;gap:var(--s2)">
1155
+ <button class="sm" id="dofMeasure" title="Read the direction and a starting strength off the photo's own bezel">Measure from photo</button>
1156
+ <span class="status sm" id="dofSt" style="min-width:0"></span>
1157
+ </div>
1158
+ </div>
1159
+ </div></div>
1160
+ </div>
1116
1161
  <div class="sect" id="secEdge" data-on="1">
1117
1162
  <div class="sect-top">
1118
1163
  <div class="sect-left">
@@ -1180,8 +1225,8 @@
1180
1225
 
1181
1226
  <div class="pop" id="pop2">
1182
1227
  <h3>Screenshot to put on the screen</h3>
1183
- <p class="sub">A PNG/JPG of the UI — or paste a Figma frame link and Claude will export it for you.</p>
1184
- <div class="row" style="margin-bottom:8px"><input type="text" id="figma" placeholder="https://www.figma.com/design/…?node-id=…" style="flex:1"><button id="figmaBtn">Export from Figma</button></div>
1228
+ <p class="sub">A PNG/JPG of the UI — or paste a Figma frame link and Claude will import it for you.</p>
1229
+ <div class="row" style="margin-bottom:8px"><input type="text" id="figma" placeholder="https://www.figma.com/design/…?node-id=…" style="flex:1"><button id="figmaBtn">Import from Figma</button></div>
1185
1230
  <div class="status sm" id="figmaSt" style="margin-bottom:10px"></div>
1186
1231
  <div class="hintline" id="figmaHint" hidden style="margin-bottom:10px"></div>
1187
1232
  <div class="picker">
@@ -1393,6 +1438,9 @@ const api = async (p, body, raw) => {
1393
1438
  const j = await r.json(); if (!r.ok) throw new Error(j.error || r.statusText); return j;
1394
1439
  };
1395
1440
  const fileURL = p => '/file?path=' + encodeURIComponent(p);
1441
+ // A source's URL carries the file's mtime, so new bytes at an old path are
1442
+ // fetched rather than served from the browser's cache (see _adopt in ui.py).
1443
+ const srcURL = (p, v) => fileURL(p) + (v ? '&v=' + v : '');
1396
1444
  const st = {photo:null, shot:null, corners:null, frac:0, type:null, preset:null, measured:null,
1397
1445
  // Where the quad on screen came from: 'detected' | 'remembered' | 'restored'
1398
1446
  // | 'default'. The tool's contract is that it never presents a guess as a
@@ -1597,7 +1645,8 @@ function setChosen(role, path, size, el, meta){
1597
1645
  if (el) el.classList.add('sel');
1598
1646
  // A video cannot be shown by <img>, so a clip gets its poster frame instead.
1599
1647
  const thumb = (meta && meta.poster) ? meta.poster : path;
1600
- $('#pv'+n).innerHTML = `<img src="${fileURL(thumb)}" alt="">`;
1648
+ const ver = (meta && meta.mtime) || Date.now();
1649
+ $('#pv'+n).innerHTML = `<img src="${srcURL(thumb, ver)}" alt="">`;
1601
1650
  const chip = $('#chip'+n);
1602
1651
  chip.classList.remove('is-empty');
1603
1652
  // The file's own name and nothing else. The size and, for a clip, the frame
@@ -1613,14 +1662,16 @@ function setChosen(role, path, size, el, meta){
1613
1662
  if ((role==='photo' ? st.photo : st.shot) !== path) dropResult();
1614
1663
  chip.title = `${homePath(path)}\n${size[0]}×${size[1]}`
1615
1664
  + (meta && meta.video ? ` · ${meta.frames} frames @ ${Math.round(meta.fps)}fps` : '');
1616
- $('#sw'+n).style.background = `center/cover no-repeat url("${fileURL(thumb)}")`;
1665
+ $('#sw'+n).style.background = `center/cover no-repeat url("${srcURL(thumb, ver)}")`;
1617
1666
  $('#clear'+n).hidden = false;
1618
1667
  // A fit the server recognised from this photograph's own pixels. Held
1619
1668
  // rather than applied here: the quad only means anything once there is a
1620
1669
  // screenshot to put inside it, which is what maybeStart() waits for.
1621
- if (role==='photo'){ st.photo = path; st.corners = null;
1670
+ if (role==='photo'){ st.photo = path; st.photoV = ver; st.corners = null;
1622
1671
  st.remembered = (meta && meta.remembered) || null; }
1623
- else { st.shot = path; st.shotSize = size; st.video = meta && meta.video ? meta : null; syncVideoUI();
1672
+ else { st.shot = path; st.shotV = ver; st.shotSize = size; st.video = meta && meta.video ? meta : null; syncVideoUI();
1673
+ // The status line may still be narrating the clear ("Screenshot removed…").
1674
+ if (st.corners){ $('#detSt').className = 'status ok'; $('#detSt').textContent = 'Screenshot changed — the fit on the photo is kept'; }
1624
1675
  // A detected quad is re-oriented for the new screenshot; a remembered or
1625
1676
  // hand-placed one is somebody's decision and is left alone.
1626
1677
  if (st.corners && st.quadFrom === 'detected' && orientQuad()){ draw(); drawStrip(); autoPreview(); } }
@@ -1693,6 +1744,7 @@ async function awaitJob({onDone, onError, cancelled}) {
1693
1744
  if (cancelled()) return;
1694
1745
  if (j.status === 'done') return onDone(j);
1695
1746
  if (j.status === 'error') return onError(j);
1747
+ if (j.status === 'cancelled') return; // this page cancelled it (or another tab did)
1696
1748
  if (j.status === 'none') {
1697
1749
  // The job file is gone — a wiped session, or a restarted server. The
1698
1750
  // server answers 'none' instantly, so looping on it would spin as fast
@@ -1702,16 +1754,37 @@ async function awaitJob({onDone, onError, cancelled}) {
1702
1754
  // 'pending' with waited:true — the long poll timed out server-side; go again.
1703
1755
  }
1704
1756
  }
1757
+ /* Import from Figma. One button, two jobs: it starts the import, and while
1758
+ the import is pending it reads "Cancel" and stops it — before this the only
1759
+ way out of a pending import was to reload the page (12 Sep 2026). Cancelling
1760
+ tells the server to mark the job cancelled, so an agent that is still
1761
+ exporting gets "already cancelled" from complete_job instead of landing a
1762
+ file on a request nobody is waiting for. */
1763
+ let figmaInFlight = null; // {stop} while an import is pending
1705
1764
  $('#figmaBtn').onclick = async () => {
1765
+ const s = $('#figmaSt'), hint = $('#figmaHint'), btn = $('#figmaBtn');
1766
+ if (figmaInFlight){
1767
+ const inflight = figmaInFlight;
1768
+ try { await api('/api/job/cancel', {}); } catch (e) { /* already finished — the poll will say */ }
1769
+ inflight.stop('', 'Import cancelled.');
1770
+ return;
1771
+ }
1706
1772
  const url = $('#figma').value.trim(); if (!url) return;
1707
- const s = $('#figmaSt'), hint = $('#figmaHint');
1708
- const btn = $('#figmaBtn'); btn.disabled = true;
1773
+ // Only a Figma frame link can be imported, and the job is answered by the
1774
+ // agent, so a wrong URL used to cost a full round trip before anyone saw it
1775
+ // (12 Sep 2026: the workbench's own address was pasted and went out as a job).
1776
+ if (!/^https:\/\/(www\.)?figma\.com\/(design|board|slides|file)\/[^/?#]+/.test(url) || !/node-id=\d+[-:]\d+/.test(url)){
1777
+ s.className = 'status sm err';
1778
+ s.textContent = 'That is not a Figma frame link. Copy the link from Figma (Share → Copy link, or right-click the frame → Copy link) — it needs node-id=… in it.';
1779
+ return;
1780
+ }
1709
1781
  const t0 = Date.now();
1710
1782
  let finished = false;
1783
+ btn.textContent = 'Cancel'; btn.title = 'Stop waiting for this import';
1711
1784
  const paint = () => {
1712
1785
  const el = Math.round((Date.now() - t0) / 1000);
1713
1786
  s.className = 'status sm busy';
1714
- s.innerHTML = `Exporting from Figma <span class="el">${el}s</span>`;
1787
+ s.innerHTML = `Importing from Figma <span class="el">${el}s</span>`;
1715
1788
  if (el >= NUDGE_AFTER && hint.hidden) {
1716
1789
  hint.hidden = false;
1717
1790
  hint.innerHTML = 'Taking longer than usual. Claude picks this up automatically while it\'s ' +
@@ -1726,26 +1799,33 @@ $('#figmaBtn').onclick = async () => {
1726
1799
  // return so the number is right the moment the designer looks at it again.
1727
1800
  document.addEventListener('visibilitychange', paint);
1728
1801
  const stop = (cls, text) => {
1729
- finished = true;
1802
+ if (finished) return;
1803
+ finished = true; figmaInFlight = null;
1730
1804
  clearInterval(tick);
1731
1805
  document.removeEventListener('visibilitychange', paint);
1732
- btn.disabled = false; hint.hidden = true;
1806
+ btn.textContent = 'Import from Figma'; btn.title = '';
1807
+ hint.hidden = true;
1733
1808
  s.className = 'status sm ' + cls; s.textContent = text;
1734
- // The popover is usually closed by now — the in-place line would go unread.
1735
- toast(cls === 'ok' ? 'ok' : 'err', cls === 'ok' ? 'Screenshot exported from Figma.' : text);
1809
+ // ONE channel. While the picker is open the line under the field is in
1810
+ // view, and a toast on top of it said the same sentence twice, overlapping
1811
+ // the popover (12 Sep 2026). A toast only when the popover is closed and
1812
+ // the line would go unread.
1813
+ if (!$('#pop2').classList.contains('on') && cls)
1814
+ toast(cls === 'ok' ? 'ok' : 'err', cls === 'ok' ? 'Screenshot imported from Figma.' : text);
1736
1815
  };
1816
+ figmaInFlight = {stop};
1737
1817
  try { await api('/api/figma', {url}); }
1738
- catch (e) { return stop('err', 'Could not queue the export: ' + e.message); }
1818
+ catch (e) { return stop('err', 'Could not start the import: ' + e.message); }
1739
1819
  await awaitJob({
1740
1820
  cancelled: () => finished,
1741
1821
  onDone: async () => {
1742
1822
  try {
1743
1823
  const r = await api('/api/job/adopt', {});
1744
- stop('ok', 'Exported');
1824
+ stop('ok', 'Imported');
1745
1825
  setChosen('screenshot', r.path, r.size, null, r);
1746
- } catch (e) { stop('err', 'Exported, but the file could not be read: ' + e.message); }
1826
+ } catch (e) { stop('err', 'Imported, but the file could not be read: ' + e.message); }
1747
1827
  },
1748
- onError: (j) => stop('err', 'Export failed: ' + (j.message || '')),
1828
+ onError: (j) => stop('err', 'Import failed: ' + (j.message || '')),
1749
1829
  });
1750
1830
  };
1751
1831
 
@@ -1820,7 +1900,7 @@ async function maybeStart(){
1820
1900
  else if (st.remembered) applyRemembered();
1821
1901
  else detect();
1822
1902
  };
1823
- img.src = fileURL(st.photo);
1903
+ img.src = srcURL(st.photo, st.photoV);
1824
1904
  }
1825
1905
  function computeFit(){
1826
1906
  const box = $('#scroller');
@@ -2467,6 +2547,179 @@ $('#emisAmt').oninput = e => { emisAmt = parseFloat(e.target.value); paintEmis(
2467
2547
  $('#emisAmt').onchange = e => { setEmis(true, parseFloat(e.target.value)); autoPreview(); };
2468
2548
 
2469
2549
 
2550
+ /* Depth of field: a blur that grows across the screen in one direction, and
2551
+ the glass edge softening with it (dof.py). Off sends nothing, so the engine
2552
+ takes the byte-identical path it took before the feature existed. The
2553
+ direction and strength are the designer's numbers; Measure proposes them
2554
+ from the photograph's own bezel and says how far it can be trusted — the
2555
+ measured strength is a floor (the estimator saturates around 5px of blur),
2556
+ never a ceiling. The live layer cannot show it; the composite does. */
2557
+ let dofOn = recall('dof','0') === '1';
2558
+ let dofAng = parseFloat(recall('dofAng','90')) || 0;
2559
+ let dofStr = parseFloat(recall('dofStr','0.3')); if (!(dofStr >= 0)) dofStr = 0.3;
2560
+ let dofStart = parseFloat(recall('dofStart','0')); if (!(dofStart >= 0)) dofStart = 0;
2561
+ function dofAngle(){ return dofOn ? dofAng : 0; }
2562
+ function dofStrength(){ return dofOn ? dofStr : 0; }
2563
+ function dofStartV(){ return dofOn ? dofStart : 0; }
2564
+ function dofWord(a){
2565
+ const names = ['the right','the bottom-right','the bottom','the bottom-left','the left','the top-left','the top','the top-right'];
2566
+ return names[Math.round(((a % 360) + 360) % 360 / 45) % 8];
2567
+ }
2568
+ function paintDof(){
2569
+ setSwitch($('#dofBtn'), dofOn);
2570
+ $('#dofDirVal').textContent = `${dofWord(dofAng)} · ${Math.round(dofAng)}°`;
2571
+ $('#dofStrVal').textContent = Math.round(dofStr * 100) + '%';
2572
+ const d = $('#dofDir'); d.value = Math.round(dofAng / 5) * 5; d.style.setProperty('--p', (dofAng % 360) / 355);
2573
+ const st = $('#dofStr'); st.value = dofStr; st.style.setProperty('--p', dofStr);
2574
+ }
2575
+ function setDof(on, ang, str){
2576
+ dofOn = on;
2577
+ if (ang !== undefined) dofAng = ((ang % 360) + 360) % 360;
2578
+ if (str !== undefined) dofStr = Math.max(0, Math.min(1, str));
2579
+ remember('dof', on ? '1' : '0'); remember('dofAng', String(dofAng)); remember('dofStr', String(dofStr)); remember('dofStart', String(dofStart));
2580
+ setSectionOpen($('#secDof'), on);
2581
+ paintDof();
2582
+ paintGizmo();
2583
+ }
2584
+ let dofMeasuredFor = null; // photo path the last measurement was for
2585
+ async function measureDof(){
2586
+ const s = $('#dofSt');
2587
+ if (!(st.photo && st.corners)){ s.className = 'status sm'; s.textContent = 'Fit the screen first.'; return; }
2588
+ s.className = 'status sm busy'; s.textContent = 'Measuring…';
2589
+ try {
2590
+ const r = await api('/api/dof', {corners: st.corners});
2591
+ dofMeasuredFor = st.photo;
2592
+ if (r.flat){
2593
+ s.className = 'status sm'; s.textContent = 'The bezel is sharp all round — nothing to match. Set by eye if you want it anyway.';
2594
+ s.title = `Edge blur ${(r.sigma || []).map(v => v.toFixed(1)).join(' / ')}px`;
2595
+ autoPreview(); // still on, at the slider's own numbers
2596
+ return;
2597
+ }
2598
+ dofStart = 0; setDof(true, r.angle, Math.max(r.strength, 0.05));
2599
+ s.className = 'status sm ok';
2600
+ s.textContent = `Measured: grows toward ${dofWord(r.angle)}. Strength is a floor — raise it if the far end looks softer.`;
2601
+ s.title = `Edge blur ${r.sigma.map(v => v.toFixed(1)).join(' / ')}px, spread ${r.spread}×`;
2602
+ autoPreview();
2603
+ } catch (e) { s.className = 'status sm err'; s.textContent = 'Could not measure: ' + e.message; }
2604
+ }
2605
+ $('#dofBtn').onclick = () => {
2606
+ setDof(!dofOn);
2607
+ // First time on for this photograph: propose from the photo rather than
2608
+ // from a remembered slider position that belongs to another one.
2609
+ if (dofOn && dofMeasuredFor !== st.photo && st.corners) measureDof(); else autoPreview();
2610
+ };
2611
+ $('#dofDir').oninput = e => { dofAng = parseFloat(e.target.value); paintDof(); };
2612
+ $('#dofDir').onchange = e => { setDof(true, parseFloat(e.target.value)); autoPreview(); };
2613
+ $('#dofStr').oninput = e => { dofStr = parseFloat(e.target.value); paintDof(); };
2614
+ $('#dofStr').onchange = e => { setDof(true, undefined, parseFloat(e.target.value)); autoPreview(); };
2615
+ $('#dofMeasure').onclick = measureDof;
2616
+
2617
+ /* ===== the depth-of-field gizmo ============================================
2618
+ Two lines over the composite, the graduated-filter idiom (Lightroom's
2619
+ gradient, Photoshop's tilt-shift). The SOLID line is where focus ends and
2620
+ the blur begins; the DASHED line is where the blur reaches a REFERENCE
2621
+ softness, GIZMO_REF of the engine's full blur (sigma_full = DOF_MAX_FRAC of
2622
+ the screen's longer side) -- 30%, about 6px on a phone, the point where
2623
+ text stops being readable. Not full blur: at 50% strength the full-blur
2624
+ line sat three quarters of a screen above the phone, off the photograph
2625
+ and out of reach. Drag the solid line's centre to move focus, either end
2626
+ pip to turn both lines (direction), the dashed line's centre to set how
2627
+ fast the blur grows (strength). The dashed line may still sit past the far
2628
+ edge for a gentle falloff, drawn out over the photograph and capped at
2629
+ GIZMO_FAR_MAX extents; gentler than that is the slider's job.
2630
+ Mapping, so the gizmo and the sliders are one model:
2631
+ start = solid line's position, 0..0.95 of the quad's extent along the direction
2632
+ strength = GIZMO_REF * (1 - start) / (far - start) -- far = dashed line's position
2633
+ Everything is drawn in PHOTO pixels: the SVG's viewBox is the photo, so no
2634
+ coordinate here is converted, and it stays aligned at every zoom for free. */
2635
+ const gz = $('#dofGizmo');
2636
+ const SIGMA_FULL_FRAC = 0.02; // mirrors dof.DOF_MAX_FRAC -- keep in step
2637
+ const GIZMO_REF = 0.3; // the dashed line marks this fraction of full blur
2638
+ const GIZMO_FAR_MAX = 1.6; // the dashed line is drawn no further than this many extents
2639
+ function gizmoGeom(){
2640
+ const C = st.corners, a = dofAng * Math.PI / 180;
2641
+ const d = [Math.cos(a), Math.sin(a)], u = [-d[1], d[0]];
2642
+ const pd = C.map(c => c[0]*d[0] + c[1]*d[1]), pu = C.map(c => c[0]*u[0] + c[1]*u[1]);
2643
+ const lo = Math.min(...pd), hi = Math.max(...pd), ulo = Math.min(...pu), uhi = Math.max(...pu);
2644
+ const side = Math.max(vlen(sub(C[1],C[0])), vlen(sub(C[2],C[1])), vlen(sub(C[3],C[2])), vlen(sub(C[0],C[3])));
2645
+ return {d, u, lo, hi, ulo, uhi, side};
2646
+ }
2647
+ // a line at fraction f along d, spanning the quad's extent along u (padded)
2648
+ function gizmoLine(g, f, pad){
2649
+ const c = g.lo + f * (g.hi - g.lo);
2650
+ const p0 = [c*g.d[0] + (g.ulo-pad)*g.u[0], c*g.d[1] + (g.ulo-pad)*g.u[1]];
2651
+ const p1 = [c*g.d[0] + (g.uhi+pad)*g.u[0], c*g.d[1] + (g.uhi+pad)*g.u[1]];
2652
+ return [p0, p1, [(p0[0]+p1[0])/2, (p0[1]+p1[1])/2]];
2653
+ }
2654
+ function paintGizmo(){
2655
+ const im = $('#outImg');
2656
+ const show = dofOn && st.corners && !im.hidden && im.naturalWidth && $('#liveWrap').hidden;
2657
+ // An SVG element has no `.hidden` property (that is HTMLElement's), so
2658
+ // assigning it makes a JS expando and leaves the attribute where it was.
2659
+ gz.toggleAttribute('hidden', !show);
2660
+ if (!show) return;
2661
+ const W = im.naturalWidth, H = im.naturalHeight;
2662
+ gz.setAttribute('viewBox', `0 0 ${W} ${H}`);
2663
+ gz.style.left = im.offsetLeft + 'px'; gz.style.top = im.offsetTop + 'px';
2664
+ gz.style.width = im.clientWidth + 'px'; gz.style.height = im.clientHeight + 'px';
2665
+ const k = W / Math.max(im.clientWidth, 1); // photo px per screen px
2666
+ const g = gizmoGeom();
2667
+ const fs = dofStart, fe = Math.min(GIZMO_FAR_MAX, fs + GIZMO_REF * (1 - fs) / Math.max(dofStr, 0.05));
2668
+ const pinned = fs + GIZMO_REF * (1 - fs) / Math.max(dofStr, 0.05) > GIZMO_FAR_MAX;
2669
+ const [a0, a1, ac] = gizmoLine(g, fs, 12 * k);
2670
+ const [b0, b1, bc] = gizmoLine(g, fe, 12 * k);
2671
+ const sigmaFull = SIGMA_FULL_FRAC * g.side;
2672
+ const r = 6 * k, rp = 4 * k, fs11 = 11 * k;
2673
+ const off = 16 * k; // label offset along d
2674
+ const L = (x, y, cls, extra='') => `<line x1="${x[0]}" y1="${x[1]}" x2="${y[0]}" y2="${y[1]}" class="${cls}" ${extra}/>`;
2675
+ const T = (p, txt, dy) => `<text x="${p[0]}" y="${p[1] + dy}" text-anchor="middle" font-size="${fs11}">${txt}</text>`;
2676
+ const beyond = fe > 1.0001;
2677
+ gz.innerHTML =
2678
+ L(a0, a1, 'case', 'stroke-width="4"') + L(a0, a1, 'focus') +
2679
+ L(b0, b1, 'case', 'stroke-width="3.5"') + L(b0, b1, 'far' + (beyond ? ' out' : '')) +
2680
+ `<circle cx="${a0[0]}" cy="${a0[1]}" r="${rp}" class="h pip" data-h="rotA"/>` +
2681
+ `<circle cx="${a1[0]}" cy="${a1[1]}" r="${rp}" class="h pip" data-h="rotB"/>` +
2682
+ `<circle cx="${ac[0]}" cy="${ac[1]}" r="${r}" class="h" data-h="start"/>` +
2683
+ `<circle cx="${bc[0]}" cy="${bc[1]}" r="${r}" class="h hollow" data-h="far"/>` +
2684
+ T([ac[0] - g.d[0]*off, ac[1] - g.d[1]*off], 'sharp', 4*k) +
2685
+ T([bc[0] + g.d[0]*off, bc[1] + g.d[1]*off], pinned ? `gentler than this: use the slider` : `σ ${(GIZMO_REF * sigmaFull).toFixed(0)}px here${beyond ? ' · past the screen' : ''}`, 4*k);
2686
+ }
2687
+ // pointer position in PHOTO pixels
2688
+ function gizmoPos(e){
2689
+ const rc = gz.getBoundingClientRect();
2690
+ const W = $('#outImg').naturalWidth, H = $('#outImg').naturalHeight;
2691
+ return [(e.clientX - rc.left) * W / rc.width, (e.clientY - rc.top) * H / rc.height];
2692
+ }
2693
+ let gzDrag = null;
2694
+ gz.addEventListener('pointerdown', e => {
2695
+ const h = e.target.dataset && e.target.dataset.h; if (!h) return;
2696
+ e.preventDefault(); gz.setPointerCapture(e.pointerId);
2697
+ gzDrag = {h}; gz.classList.add('drag');
2698
+ });
2699
+ gz.addEventListener('pointermove', e => {
2700
+ if (!gzDrag) return;
2701
+ const p = gizmoPos(e), g = gizmoGeom();
2702
+ const f = (p[0]*g.d[0] + p[1]*g.d[1] - g.lo) / (g.hi - g.lo);
2703
+ if (gzDrag.h === 'start'){
2704
+ dofStart = Math.max(0, Math.min(0.95, f));
2705
+ } else if (gzDrag.h === 'far'){
2706
+ // strength from the spacing; the far line never comes inside the screen
2707
+ // (strength is capped at 1) and never so far that the blur vanishes.
2708
+ const fe = Math.min(GIZMO_FAR_MAX, Math.max(f, dofStart + 0.05));
2709
+ dofStr = Math.max(0.05, Math.min(1, GIZMO_REF * (1 - dofStart) / Math.max(fe - dofStart, 1e-3)));
2710
+ } else {
2711
+ // Turn about the focus line's centre: the pip under the pointer follows it.
2712
+ const [, , ac] = gizmoLine(g, dofStart, 0);
2713
+ const v = [p[0]-ac[0], p[1]-ac[1]];
2714
+ const sign = gzDrag.h === 'rotB' ? 1 : -1; // rotB sits at +u, rotA at -u
2715
+ dofAng = ((Math.atan2(v[1]*sign, v[0]*sign) * 180 / Math.PI - 90) % 360 + 360) % 360;
2716
+ if (e.shiftKey) dofAng = Math.round(dofAng / 15) * 15;
2717
+ }
2718
+ paintDof(); paintGizmo(); autoPreview();
2719
+ });
2720
+ const gzUp = e => { if (!gzDrag) return; gzDrag = null; gz.classList.remove('drag'); setDof(true); autoPreview(); };
2721
+ gz.addEventListener('pointerup', gzUp); gz.addEventListener('pointercancel', gzUp);
2722
+
2470
2723
  $('#edgeBtn').onclick = () => setLoupeMode(loupeMode === 'float' ? 'dock' : 'float');
2471
2724
 
2472
2725
  /* Contrast for the strip only. A dark screen on a dark frame puts the
@@ -2672,6 +2925,7 @@ function syncOut(){
2672
2925
  const a = $('#scroller'), b = $('#outWrap');
2673
2926
  b.scrollLeft = a.scrollLeft; b.scrollTop = a.scrollTop;
2674
2927
  }
2928
+ paintGizmo();
2675
2929
  }
2676
2930
  $('#scroller').addEventListener('scroll', () => {
2677
2931
  syncOut();
@@ -2694,6 +2948,7 @@ function autoPreview(){
2694
2948
  it. */
2695
2949
  function showResult(what){
2696
2950
  $('#outImg').hidden = what !== 'still';
2951
+ if (what !== 'still') $('#dofGizmo').setAttribute('hidden', '');
2697
2952
  const v = $('#outVid');
2698
2953
  if (what !== 'clip' && !v.hidden){ try { v.pause(); } catch(e){} }
2699
2954
  v.hidden = what !== 'clip';
@@ -2713,6 +2968,9 @@ function showResult(what){
2713
2968
  geometry — exact, the same four corners the render uses
2714
2969
  corner radius — applied in the video's own pixel space, before the warp,
2715
2970
  which is where compose() applies it
2971
+ depth of field — ABSENT. A blur that grows across the screen has no
2972
+ CSS equivalent that follows a homography; the composite
2973
+ is the only view that shows it.
2716
2974
  emissive — APPROXIMATED with `screen` blending. Same idea (the
2717
2975
  screen's light plus the glass beneath it), not the same
2718
2976
  arithmetic as compose()'s emissive.
@@ -2809,10 +3067,10 @@ async function playLive(){
2809
3067
  if (!(st.photo && st.shot && st.corners && st.video)) return;
2810
3068
  if (!$('#liveWrap').hidden && !$('#liveVid').paused) return stopLive();
2811
3069
  const v = $('#liveVid'), s = $('#outSt');
2812
- $('#liveBg').src = fileURL(st.photo);
3070
+ $('#liveBg').src = srcURL(st.photo, st.photoV);
2813
3071
  if (!v.src || v.dataset.src !== st.shot){
2814
3072
  v.dataset.src = st.shot;
2815
- v.src = fileURL(st.shot);
3073
+ v.src = srcURL(st.shot, st.shotV);
2816
3074
  }
2817
3075
  showResult('live');
2818
3076
  await new Promise(r => {
@@ -2859,7 +3117,7 @@ async function playClip(){
2859
3117
  // a preview the user did not ask for, and the wait goes silent again.
2860
3118
  building = true;
2861
3119
  try{
2862
- await api('/api/preview_video', {corners: st.corners, radius_frac: radiusValue(), smoothing: smoothingValue(),
3120
+ await api('/api/preview_video', {corners: st.corners, radius_frac: radiusValue(), smoothing: smoothingValue(), dof_angle: dofAngle(), dof_strength: dofStrength(), dof_start: dofStartV(),
2863
3121
  device: st.type, grade: gradeValue(),
2864
3122
  reflection: emisValue(), fit_frame: +$('#vframe').value});
2865
3123
  }catch(e){
@@ -2918,7 +3176,7 @@ async function renderPreview(){
2918
3176
  const say = t => { if (!building && $('#liveWrap').hidden) s.textContent = t; };
2919
3177
  say('Rendering…');
2920
3178
  try{
2921
- const r = await api('/api/preview', {corners: st.corners, radius_frac: radiusValue(), smoothing: smoothingValue(), device: st.type, grade: gradeValue(), reflection: emisValue()});
3179
+ const r = await api('/api/preview', {corners: st.corners, radius_frac: radiusValue(), smoothing: smoothingValue(), dof_angle: dofAngle(), dof_strength: dofStrength(), dof_start: dofStartV(), device: st.type, grade: gradeValue(), reflection: emisValue()});
2922
3180
  const im = $('#outImg');
2923
3181
  im.onload = () => {
2924
3182
  outNat = im.naturalWidth;
@@ -3048,7 +3306,7 @@ async function renderVideo(){
3048
3306
  b.disabled = true;
3049
3307
  setRenderProgress(0);
3050
3308
  try{
3051
- await api('/api/render', {corners: st.corners, radius_frac: radiusValue(), smoothing: smoothingValue(), device: st.type,
3309
+ await api('/api/render', {corners: st.corners, radius_frac: radiusValue(), smoothing: smoothingValue(), dof_angle: dofAngle(), dof_strength: dofStrength(), dof_start: dofStartV(), device: st.type,
3052
3310
  grade: gradeValue(), reflection: emisValue(), preset, fit_frame: +$('#vframe').value});
3053
3311
  }catch(e){
3054
3312
  setRenderProgress(null);
@@ -3094,7 +3352,7 @@ $('#save').onclick = async () => {
3094
3352
  if (st.video) return renderVideo();
3095
3353
  const b = $('#save'); b.textContent = 'Saving…'; b.disabled = true;
3096
3354
  try{
3097
- const r = await api('/api/save', {corners: st.corners, radius_frac: radiusValue(), smoothing: smoothingValue(), device: st.type, grade: gradeValue(), reflection: emisValue()});
3355
+ const r = await api('/api/save', {corners: st.corners, radius_frac: radiusValue(), smoothing: smoothingValue(), dof_angle: dofAngle(), dof_strength: dofStrength(), dof_start: dofStartV(), device: st.type, grade: gradeValue(), reflection: emisValue()});
3098
3356
  b.textContent = saveLabel();
3099
3357
  // The real destination, not a hardcoded one: --out-dir means saves usually
3100
3358
  // land in the project folder now, and telling the user "~/Desktop" when
@@ -3173,6 +3431,7 @@ $('#imp').onclick = async () => {
3173
3431
  setStripHC(stripHC);
3174
3432
  setGrade(gradeOn, gradeAmt);
3175
3433
  setEmis(emisOn, emisAmt);
3434
+ setDof(dofOn, dofAng, dofStr);
3176
3435
  setRadiusOn(radiusOn);
3177
3436
  sectionsBooted = true; // animate from here on, not during the first paint
3178
3437
  // Compare is the DEFAULT view: judging a fit means comparing it with the