128 lines
6.1 KiB
TypeScript
128 lines
6.1 KiB
TypeScript
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 { type Look, WB_KEYS, TONE_KEYS, detailForCrop, lookValues, toneApplied } from "../autopilot/look.js";
|
|
import type { CropBias } from "../autopilot/cropBias.js";
|
|
|
|
/** Straightening beyond this is more likely a creative tilt than a crooked horizon (matches the engine's band). */
|
|
const MAX_LEVEL = 8;
|
|
const MIN_LEVEL = 0.25;
|
|
/** A crop this close to the whole frame is not worth making. */
|
|
const KEEP_WHOLE = 0.92;
|
|
|
|
export interface PlanSettings {
|
|
crop: boolean;
|
|
tone: boolean;
|
|
whiteBalance: boolean;
|
|
level: boolean;
|
|
toneStrength: number;
|
|
look: Look;
|
|
framing: "tight" | "full";
|
|
aspect: [number, number] | null;
|
|
minLongEdge: number;
|
|
}
|
|
|
|
/** The level angle the autopilot keeps. Outside the band, the photo stays as shot. */
|
|
export function levelAngle(angle: number | null, enabled: boolean): number {
|
|
if (!enabled || angle === null || Math.abs(angle) < MIN_LEVEL || Math.abs(angle) > MAX_LEVEL) return 0;
|
|
return angle;
|
|
}
|
|
|
|
/**
|
|
* White balance, tone and the finishing look, before the crop scales detail. Null when none of those are on:
|
|
* the same gate the server uses (it measures only when tone, white balance or level is enabled).
|
|
* Highlight and shadow figures are capped in `toneApplied`: Auto's recipe is not a reading of this photo.
|
|
*/
|
|
export function measuredValues(
|
|
auto: Record<string, number>,
|
|
settings: PlanSettings,
|
|
iso: number | null,
|
|
offsets: Record<string, number>,
|
|
): Record<string, number> | null {
|
|
if (!settings.tone && !settings.whiteBalance && !settings.level) return null;
|
|
const values: Record<string, number> = {};
|
|
if (settings.whiteBalance) for (const k of WB_KEYS) if (auto[k] !== undefined) values[k] = auto[k]! + (offsets[k] ?? 0);
|
|
if (settings.tone) {
|
|
for (const k of TONE_KEYS) {
|
|
const v = toneApplied(k, auto[k] ?? 0, settings.toneStrength) + (offsets[k] ?? 0);
|
|
if (v) values[k] = Math.round(v * 100) / 100;
|
|
}
|
|
}
|
|
for (const [k, v] of Object.entries(lookValues(settings.look, iso))) if (values[k] === undefined) values[k] = v;
|
|
return values;
|
|
}
|
|
|
|
export interface PlannedFrame {
|
|
ops: EditCommand[];
|
|
crop: Box | null;
|
|
wide: Box | null;
|
|
composition: ReturnType<typeof compose>;
|
|
plans: { half: Box | null; threeQuarter: Box | null } | null;
|
|
}
|
|
|
|
/**
|
|
* The edit the autopilot would make for one photo. The server and the desktop both call this, so a photo edited
|
|
* here is the same edit the worker would have queued.
|
|
*/
|
|
export function planFrame(input: {
|
|
detections: Detections | null;
|
|
width: number;
|
|
height: number;
|
|
reportage: boolean;
|
|
values: Record<string, number> | null;
|
|
angle: number;
|
|
focus?: FocusHint | null;
|
|
faces?: Box[];
|
|
cropFactor?: number | null;
|
|
/** Shift / open / straighten learnt from the photographer's crop corrections (in-app or Lightroom). */
|
|
cropBias?: Omit<CropBias, "scene"> | null;
|
|
/** The event's sport. Handball does not frame on a ball that is sitting on the floor. */
|
|
sport?: string | null;
|
|
settings: PlanSettings;
|
|
}): PlannedFrame {
|
|
const { detections, settings, width: W, height: H, reportage } = input;
|
|
const aspect = settings.aspect ? settings.aspect[0] / settings.aspect[1] : null;
|
|
const minWidth = minCropWidth(W, H, settings.minLongEdge);
|
|
const context = reportage ? "reportage" : "sport";
|
|
const faces = input.faces ?? [];
|
|
const focus = input.focus ?? null;
|
|
const sport = input.sport ?? null;
|
|
const composition = detections ? compose(detections, aspect, { minWidth, focus, framing: settings.framing, context, faces, sport }) : null;
|
|
const wideComposition = detections && !reportage && settings.framing === "tight" ? compose(detections, aspect, { minWidth, focus, framing: "full", context: "sport", sport }) : null;
|
|
const plans = reportage && detections ? { half: planCrop(detections, "half", aspect, { minWidth, focus, context, faces }), threeQuarter: planCrop(detections, "threeQuarter", aspect, { minWidth, focus, context, faces }) } : null;
|
|
let crop: Box | null = null;
|
|
let angle = input.angle;
|
|
if (settings.crop && composition) {
|
|
let rect = composition.rect;
|
|
const bias = input.cropBias;
|
|
const factor = bias?.scale ?? input.cropFactor ?? null;
|
|
if (bias && typeof bias.cut === "number") {
|
|
// The photographer's cut on the body (legs, head, arms), not a uniform shrink.
|
|
rect = frameOnSubject(composition.rect, composition.subject, bias);
|
|
if (bias.dx) {
|
|
const w = rect.x1 - rect.x0;
|
|
const x0 = Math.min(Math.max(0, rect.x0 + bias.dx), 1 - w);
|
|
rect = { ...rect, x0, x1: x0 + w };
|
|
}
|
|
} else if (bias && (bias.dx || bias.dy || (factor && factor !== 1))) {
|
|
rect = biasCrop(composition.rect, composition.subject, { dx: bias.dx, dy: bias.dy, scale: factor ?? 1 });
|
|
} else if (factor && factor !== 1) {
|
|
rect = factor < 1 ? tighter(composition.rect, composition.subject, factor) : biasCrop(composition.rect, composition.subject, { dx: 0, dy: 0, scale: factor });
|
|
}
|
|
if (bias?.dAngle) angle = Math.max(-MAX_LEVEL, Math.min(MAX_LEVEL, angle + bias.dAngle));
|
|
// 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);
|
|
}
|
|
const wide = settings.crop && wideComposition && wideComposition.rect.x1 - wideComposition.rect.x0 < KEEP_WHOLE ? constrainRotated(wideComposition.rect, angle, W, H) : null;
|
|
if (!crop && angle) crop = constrainRotated({ x0: 0, y0: 0, x1: 1, y1: 1 }, angle, W, H);
|
|
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 };
|
|
}
|