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.
83 lines
3.4 KiB
TypeScript
83 lines
3.4 KiB
TypeScript
/// <reference lib="webworker" />
|
|
import { loadEngine, type WasmPhoto, type WorkerReply, type WorkerRequest } from "./engine.js";
|
|
|
|
/**
|
|
* Decodes and renders off the main thread. Holds one photo (the one being edited); the page sends settings and
|
|
* gets pixels back. Requests are handled in order; the page keeps at most one render in flight.
|
|
*/
|
|
let current: { key: string; photo: WasmPhoto } | undefined;
|
|
const opening = new Map<string, Promise<WasmPhoto>>();
|
|
|
|
const post = (msg: WorkerReply, transfer: Transferable[] = []) => (self as unknown as DedicatedWorkerGlobalScope).postMessage(msg, transfer);
|
|
|
|
async function open(key: string, url: string, bytes?: ArrayBuffer, maxEdge = 2048): Promise<WasmPhoto> {
|
|
if (current?.key === key) return current.photo;
|
|
let p = opening.get(key);
|
|
if (!p) {
|
|
p = (async () => {
|
|
const [engine, data] = await Promise.all([
|
|
loadEngine(),
|
|
bytes ??
|
|
fetch(url).then((res) => {
|
|
if (!res.ok) throw new Error(`photo download failed (${res.status})`);
|
|
return res.arrayBuffer();
|
|
}),
|
|
]);
|
|
const photo = new engine.Photo(new Uint8Array(data), maxEdge);
|
|
current?.photo.free();
|
|
current = { key, photo };
|
|
return photo;
|
|
})();
|
|
opening.set(key, p);
|
|
p.finally(() => opening.delete(key)).catch(() => {});
|
|
}
|
|
return p;
|
|
}
|
|
|
|
self.onmessage = async (e: MessageEvent<WorkerRequest>) => {
|
|
const msg = e.data;
|
|
if (msg.type === "export") {
|
|
try {
|
|
const photo = current?.key === msg.key ? current.photo : await opening.get(msg.key);
|
|
if (!photo) throw new Error("photo not open");
|
|
photo.render(msg.settings, msg.maxW, msg.maxH, false, true);
|
|
const pixels = new Uint8ClampedArray(photo.pixels().buffer);
|
|
post({ type: "exported", id: msg.id, width: photo.width, height: photo.height, pixels }, [pixels.buffer]);
|
|
} catch (err) {
|
|
post({ type: "exportError", id: msg.id, message: err instanceof Error ? err.message : String(err) });
|
|
}
|
|
return;
|
|
}
|
|
try {
|
|
if (msg.type === "open") {
|
|
const photo = await open(msg.key, msg.url, msg.bytes, msg.maxEdge);
|
|
post({ type: "opened", key: msg.key, sourceWidth: photo.sourceWidth, sourceHeight: photo.sourceHeight });
|
|
return;
|
|
}
|
|
const photo = current?.key === msg.key ? current.photo : await opening.get(msg.key);
|
|
if (msg.type === "auto") {
|
|
try {
|
|
if (!photo) throw new Error("photo not open");
|
|
const json =
|
|
msg.kind === "tone"
|
|
? photo.autoTone(msg.settings ?? "{}")
|
|
: msg.kind === "wb"
|
|
? photo.autoWb()
|
|
: msg.kind === "level"
|
|
? photo.autoLevel(msg.settings ?? "{}")
|
|
: photo.wbAt(msg.x ?? 0.5, msg.y ?? 0.5);
|
|
post({ type: "auto", id: msg.id, values: JSON.parse(json) as Record<string, number> });
|
|
} catch (err) {
|
|
post({ type: "auto", id: msg.id, message: err instanceof Error ? err.message : String(err) });
|
|
}
|
|
return;
|
|
}
|
|
if (!photo) throw new Error("photo not open");
|
|
photo.render(msg.settings, msg.maxW, msg.maxH, msg.draft, msg.applyCrop);
|
|
const pixels = new Uint8ClampedArray(photo.pixels().buffer);
|
|
post({ type: "frame", ticket: msg.ticket, key: msg.key, width: photo.width, height: photo.height, pixels }, [pixels.buffer]);
|
|
} catch (err) {
|
|
post({ type: "error", key: msg.key, ...(msg.type === "render" && { ticket: msg.ticket }), message: err instanceof Error ? err.message : String(err) });
|
|
}
|
|
};
|