Files
photoreport/apps/app/src/develop/engine.ts
T
spark 66ac11621c
ci / rust (pull_request) Successful in 4m32s
ci / typescript (pull_request) Successful in 7m46s
Keep folder edits on this computer and upload camera originals without waiting on detection.
The contact sheet can develop a frame and write the JPEG and XMP beside it. Merged DNGs pin D50 so the colour tags stay. Local FTP sends four originals at once and runs detection off that path.
2026-10-25 13:46:28 +00:00

103 lines
4.2 KiB
TypeScript

/**
* The develop engine in the browser: `engine/wasm` built by `infra/engine/build-wasm.sh` into `public/engine/`.
* It is loaded at run time (not bundled), so the app builds and type-checks without the Rust toolchain.
*/
export interface WasmPhoto {
readonly sourceWidth: number;
readonly sourceHeight: number;
readonly width: number;
readonly height: number;
render(settings: string, maxW: number, maxH: number, draft: boolean, applyCrop: boolean): void;
/** JSON object of control id -> value. */
autoTone(settings: string): string;
autoWb(): string;
autoLevel(settings: string): string;
/** Sports level (boards and posts). Absent on an engine built before it. */
autoLevelScene?(): string;
wbAt(x: number, y: number): string;
pixels(): Uint8Array;
free(): void;
}
export interface BracketJob {
push(bytes: Uint8Array): void;
merge(maxEdge: number): BracketMerge;
/** Scene-linear DNG. Absent on an engine built before it. */
mergeDng?(maxEdge: number): Uint8Array;
free(): void;
}
export interface BracketMerge {
readonly width: number;
readonly height: number;
jpeg(): Uint8Array;
free(): void;
}
export interface EngineModule {
default(init?: { module_or_path: string }): Promise<unknown>;
/** Capture metadata JSON from LightCraft. `{}` when the file has none. */
readCaptureMeta(bytes: Uint8Array): string;
controls(): string;
defaultSettings(): string;
apply(settings: string, command: string): string;
value(settings: string, id: string): number;
isUnedited(settings: string): boolean;
Photo: new (bytes: Uint8Array, maxEdge: number) => WasmPhoto;
BracketJob: new () => BracketJob;
}
const BASE = `${import.meta.env.BASE_URL ?? "/"}engine/`;
let loading: Promise<EngineModule> | undefined;
/**
* Imports the engine's JS glue. Some web servers send `.js` under /engine/ with a non-JavaScript type (a `types`
* block for `.wasm` replaces the default map), which browsers refuse for modules: then the same file is fetched
* and imported from a JavaScript blob. The wasm path is always passed explicitly, so the glue never needs its URL.
*/
async function importGlue(url: string): Promise<EngineModule> {
try {
return (await import(/* @vite-ignore */ url)) as EngineModule;
} catch (err) {
const res = await fetch(url);
if (!res.ok) throw err;
const blob = URL.createObjectURL(new Blob([await res.text()], { type: "text/javascript" }));
try {
return (await import(/* @vite-ignore */ blob)) as EngineModule;
} finally {
URL.revokeObjectURL(blob);
}
}
}
export function loadEngine(): Promise<EngineModule> {
loading ??= (async () => {
const mod = await importGlue(`${BASE}photoreport_engine_wasm.js`);
await mod.default({ module_or_path: `${BASE}photoreport_engine_wasm_bg.wasm` });
return mod;
})();
loading.catch(() => {
loading = undefined;
});
return loading;
}
/** Messages to and from `render.worker.ts`. */
export type WorkerRequest =
/** `bytes`: the original from this computer (desktop app); otherwise the worker downloads `url`. */
| { type: "open"; key: string; url: string; bytes?: ArrayBuffer; maxEdge?: number }
| { type: "render"; ticket: number; key: string; settings: string; maxW: number; maxH: number; draft: boolean; applyCrop: boolean }
/** Auto values from the photo itself: tone (under `settings`), white balance, or the eyedropper at x, y. */
| { type: "auto"; id: number; key: string; kind: "tone" | "wb" | "pick" | "level"; settings?: string; x?: number; y?: number }
/** One full render of the open photo, for a JPEG saved from Revelar. Does not replace the loupe frame. */
| { type: "export"; id: number; key: string; settings: string; maxW: number; maxH: number };
export type WorkerReply =
| { type: "opened"; key: string; sourceWidth: number; sourceHeight: number }
| { type: "frame"; ticket: number; key: string; width: number; height: number; pixels: Uint8ClampedArray }
| { type: "error"; key: string; ticket?: number; message: string }
| { type: "auto"; id: number; values?: Record<string, number>; message?: string }
| { type: "exported"; id: number; width: number; height: number; pixels: Uint8ClampedArray }
| { type: "exportError"; id: number; message: string };