From 0121f950b53317f8acf5219c3da876406d028f0a Mon Sep 17 00:00:00 2001 From: spark Date: Fri, 20 Nov 2026 21:56:32 +0000 Subject: [PATCH] Keep the autopilot crop on the photograph's own aspect. A tighter window can still move across the frame. A preset must not turn the photo into another format. --- apps/api/src/autopilot/service.ts | 12 +++++- apps/api/src/composition/geometry.ts | 36 +++++++++++++++- apps/api/src/composition/planFrame.test.ts | 49 ++++++++++++++++++++++ apps/api/src/composition/planFrame.ts | 20 ++++++--- docs/photo-editing-rules.md | 4 +- packages/contracts/src/library.ts | 2 +- 6 files changed, 111 insertions(+), 12 deletions(-) create mode 100644 apps/api/src/composition/planFrame.test.ts diff --git a/apps/api/src/autopilot/service.ts b/apps/api/src/autopilot/service.ts index eb01fea..3da4dc2 100644 --- a/apps/api/src/autopilot/service.ts +++ b/apps/api/src/autopilot/service.ts @@ -292,7 +292,7 @@ export class AutopilotService { const edited = await db.photoEdit.count({ where: { photoId: framePick.id, NOT: { clientId: "autopilot" } } }); if (edited) continue; const rect = constrainRotated(planned, framePick.rec?.angle ?? 0, framePick.width ?? 3, framePick.height ?? 2); - const aspect = settings.aspect ?? [framePick.width ?? 3, framePick.height ?? 2]; + const aspect = photoAspect(framePick.width, framePick.height); await develop .append(framePick.id, framePick.uploaderId ?? event.createdById ?? null, { clientId: "autopilot", @@ -310,7 +310,7 @@ export class AutopilotService { if (crop2 && second.rec?.subject && settings.crop && !secondEdited) { // The planned crop already keeps the hands and the feet. Shrinking it again cuts them. const rect = wider ?? constrainRotated(crop2, second.rec.angle, second.width ?? 3, second.height ?? 2); - const aspect = settings.aspect ?? [second.width ?? 3, second.height ?? 2]; + const aspect = photoAspect(second.width, second.height); await develop .append(second.id, second.uploaderId ?? event.createdById ?? null, { clientId: "autopilot", @@ -533,6 +533,14 @@ function subjectFocus(d: { people: { x0: number; y0: number; x1: number; y1: num return values.length ? Math.max(...values) : 0; } +/** The photograph's own pixel size, so the crop lock is that format and not another. */ +function photoAspect(width: number | null | undefined, height: number | null | undefined): [number, number] { + if (width && height && width > 0 && height > 0) { + return [Math.round(width), Math.round(height)]; + } + return [3, 2]; +} + /** A tighter plan keeps the crop and the structure scaled to that crop, so the halo does not grow with the tighter frame. */ function cropOps(photoId: string, key: string, rect: Box, angle: number, aspect: readonly number[], detail: Record): { opId: string; command: EditCommand }[] { const ops: { opId: string; command: EditCommand }[] = []; diff --git a/apps/api/src/composition/geometry.ts b/apps/api/src/composition/geometry.ts index bef5b66..d356ea1 100644 --- a/apps/api/src/composition/geometry.ts +++ b/apps/api/src/composition/geometry.ts @@ -46,6 +46,33 @@ export function constrainRotated(r: Box, angle: number, w: number, h: number): B return { x0: 0.5 - (rw * k) / 2, y0: 0.5 - (rh * k) / 2, x1: 0.5 + (rw * k) / 2, y1: 0.5 + (rh * k) / 2 }; } +/** + * The crop on the photo's own aspect. In frame fractions that is a square: width and height match. + * The horizontal centre stays, so the crop can still sit anywhere across the frame. + */ +export function keepPhotoAspect(rect: Box): Box { + const width = rect.x1 - rect.x0; + const height = rect.y1 - rect.y0; + if (width <= 0 || height <= 0) return rect; + let side = width; + if (side > 1) side = 1; + let x0 = (rect.x0 + rect.x1) / 2 - side / 2; + let y0 = (rect.y0 + rect.y1) / 2 - side / 2; + if (x0 < 0) x0 = 0; + if (x0 + side > 1) x0 = 1 - side; + if (y0 < 0) y0 = 0; + if (y0 + side > 1) y0 = 1 - side; + const room = Math.min(side, 1 - x0, 1 - y0); + if (room < side) { + const cx = x0 + side / 2; + const cy = y0 + side / 2; + side = room; + x0 = Math.min(Math.max(0, cx - side / 2), 1 - side); + y0 = Math.min(Math.max(0, cy - side / 2), 1 - side); + } + return { x0, y0, x1: x0 + side, y1: y0 + side }; +} + /** `r` shrunk by `factor` about the subject's centre, keeping the subject inside and the crop on the photo. */ export function tighter(r: Box, subject: Box, factor: number): Box { const w = (r.x1 - r.x0) * factor; @@ -105,8 +132,13 @@ export function frameOnSubject(base: Box, subject: Box, intent: { cut?: number | /** Expand `r` by `factor` (>1) about the subject's centre, without leaving the photo. */ function looser(r: Box, subject: Box, factor: number): Box { - const w = Math.min(1, (r.x1 - r.x0) * factor); - const h = Math.min(1, (r.y1 - r.y0) * factor); + let w = (r.x1 - r.x0) * factor; + let h = (r.y1 - r.y0) * factor; + if (w > 1 || h > 1) { + const fit = Math.min(1 / w, 1 / h); + w = w * fit; + h = h * fit; + } const cx = (subject.x0 + subject.x1) / 2; const cy = (r.y0 + r.y1) / 2; const x0 = Math.min(Math.max(0, cx - w / 2), 1 - w); diff --git a/apps/api/src/composition/planFrame.test.ts b/apps/api/src/composition/planFrame.test.ts new file mode 100644 index 0000000..8d76f7b --- /dev/null +++ b/apps/api/src/composition/planFrame.test.ts @@ -0,0 +1,49 @@ +import { describe, expect, it } from "vitest"; +import { keepPhotoAspect } from "./geometry.js"; +import { planFrame, type PlanSettings } from "./planFrame.js"; + +const settings: PlanSettings = { + crop: true, + tone: false, + whiteBalance: false, + level: false, + toneStrength: 0.75, + look: "none", + framing: "full", + aspect: [4, 5], + minLongEdge: 800, +}; + +describe("keepPhotoAspect", () => { + it("keeps the horizontal place and the photograph's aspect", () => { + const crop = keepPhotoAspect({ x0: 0.2, y0: 0.1, x1: 0.7, y1: 0.95 }); + expect(crop.x1 - crop.x0).toBeCloseTo(crop.y1 - crop.y0, 5); + expect((crop.x0 + crop.x1) / 2).toBeCloseTo(0.45, 5); + }); +}); + +describe("planFrame", () => { + it("does not turn the photograph into another format", () => { + const planned = planFrame({ + detections: { + width: 6000, + height: 4000, + people: [{ x0: 0.4, y0: 0.15, x1: 0.62, y1: 0.92, score: 0.9, sharpness: 1 }], + balls: [], + }, + width: 6000, + height: 4000, + reportage: false, + values: null, + angle: 0, + settings, + }); + const crop = planned.ops.find((op) => op.command === "crop.set"); + expect(crop?.command).toBe("crop.set"); + if (!crop || crop.command !== "crop.set") return; + const rect = crop.params.rect; + const ratio = ((rect.x1 - rect.x0) * 6000) / ((rect.y1 - rect.y0) * 4000); + expect(ratio).toBeCloseTo(6000 / 4000, 2); + expect(crop.params.aspect).toEqual([6000, 4000]); + }); +}); diff --git a/apps/api/src/composition/planFrame.ts b/apps/api/src/composition/planFrame.ts index 8876132..5475943 100644 --- a/apps/api/src/composition/planFrame.ts +++ b/apps/api/src/composition/planFrame.ts @@ -1,6 +1,6 @@ import type { EditCommand } from "@photoreport/contracts"; import { type Box, type Detections, type FocusHint, clipsBall, compose, minCropWidth, planCrop } from "./compose.js"; -import { biasCrop, constrainRotated, frameOnSubject, tighter } from "./geometry.js"; +import { biasCrop, constrainRotated, frameOnSubject, keepPhotoAspect, tighter } from "./geometry.js"; import { type Look, WB_KEYS, TONE_KEYS, detailForCrop, lookValues, toneApplied } from "../autopilot/look.js"; import type { CropBias } from "../autopilot/cropBias.js"; @@ -18,6 +18,7 @@ export interface PlanSettings { toneStrength: number; look: Look; framing: "tight" | "full"; + /** Ignored. The autopilot keeps the photograph's own aspect and only moves the crop. */ aspect: [number, number] | null; minLongEdge: number; } @@ -81,7 +82,8 @@ export function planFrame(input: { settings: PlanSettings; }): PlannedFrame { const { detections, settings, width: W, height: H, reportage } = input; - const aspect = settings.aspect ? settings.aspect[0] / settings.aspect[1] : null; + // The photograph's own aspect. A preset (4:5, 16:9, a portrait of a landscape frame) is not used. + const aspect = null; const minWidth = minCropWidth(W, H, settings.minLongEdge); const context = reportage ? "reportage" : "sport"; const faces = input.faces ?? []; @@ -113,15 +115,21 @@ export function planFrame(input: { // A learnt crop must not slice a ball the frame had kept whole. const balls = detections?.balls ?? []; if (clipsBall(rect, balls)) rect = composition.rect; - if (!clipsBall(rect, balls) && rect.x1 - rect.x0 < KEEP_WHOLE) crop = constrainRotated(rect, angle, W, H); + if (!clipsBall(rect, balls) && rect.x1 - rect.x0 < KEEP_WHOLE) crop = constrainRotated(keepPhotoAspect(rect), angle, W, H); } - const wide = settings.crop && wideComposition && wideComposition.rect.x1 - wideComposition.rect.x0 < KEEP_WHOLE ? constrainRotated(wideComposition.rect, angle, W, H) : null; + const wide = settings.crop && wideComposition && wideComposition.rect.x1 - wideComposition.rect.x0 < KEEP_WHOLE ? constrainRotated(keepPhotoAspect(wideComposition.rect), angle, W, H) : null; if (!crop && angle) crop = constrainRotated({ x0: 0, y0: 0, x1: 1, y1: 1 }, angle, W, H); + const lockedPlans = plans + ? { + half: plans.half ? keepPhotoAspect(plans.half) : null, + threeQuarter: plans.threeQuarter ? keepPhotoAspect(plans.threeQuarter) : null, + } + : null; const ops: EditCommand[] = []; if (input.values) { const tuned = detailForCrop(input.values, crop, W, H); if (Object.keys(tuned).length) ops.push({ command: "develop.set", params: { values: tuned } }); } - if (crop) ops.push({ command: "crop.set", params: { rect: crop, angle, aspect: settings.aspect ?? [W, H] } }); - return { ops, crop, wide, composition, plans }; + if (crop) ops.push({ command: "crop.set", params: { rect: crop, angle, aspect: [Math.max(1, Math.round(W)), Math.max(1, Math.round(H))] } }); + return { ops, crop, wide, composition, plans: lockedPlans }; } diff --git a/docs/photo-editing-rules.md b/docs/photo-editing-rules.md index 7bac7d6..8550c76 100644 --- a/docs/photo-editing-rules.md +++ b/docs/photo-editing-rules.md @@ -32,7 +32,9 @@ Following the AP and NPPA codes: below the knee is on the floor: it does not frame the photo, even when it overlaps the player's legs and sits closer to the middle of the body. Football plays the ball on the ground, so the ball at the feet is the one that stays. A spare ball that nobody is playing never wins, in either sport. -- **Shape:** the photo's own aspect unless the event or the editor chose another (never turned vertical). +- **Shape:** the autopilot keeps the photograph's own aspect. The crop may move anywhere across the frame, + including a tighter window, but it is not turned into another format. A square, a 4:5, a 16:9, or a portrait + crop of a landscape frame is not an autopilot crop. An editor, by hand, may still choose another aspect. - **Context:** a generic event (no opponent, no fixture) is reportage. A match is sport, and a photo of that match with no ball in play is cropped as a person. Reportage does not use the handball cut. - **Framing** (per event, sport only): diff --git a/packages/contracts/src/library.ts b/packages/contracts/src/library.ts index ae447b2..1e07025 100644 --- a/packages/contracts/src/library.ts +++ b/packages/contracts/src/library.ts @@ -45,7 +45,7 @@ export const AutopilotSettings = z.object({ * "natural" Getty-like. */ look: z.enum(["agency", "natural", "none"]).default("none"), - /** Crop shape (width, height); null: the photo's own. */ + /** Unused. The autopilot always keeps the photograph's own aspect. */ aspect: z.tuple([z.number().positive(), z.number().positive()]).nullable().default(null), /** The crop keeps at least this many pixels on its long edge of the original. */ minLongEdge: z.number().int().min(800).max(12000).default(2400), -- 2.47.3