Files
photoreport/apps/app/src/develop/engine.ts
T
spark b3df241fdf
ci / rust (pull_request) Successful in 11m32s
ci / typescript (pull_request) Failing after 13m26s
Merge exposures into a linear DNG in the collection, and stop a card copy before a day that does not fit.
2026-10-21 13:40:07 +01:00

99 lines
3.8 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 };
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 };