Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Layers: pure document model, geometry, zip naming and edit history

Claude466f0a65f94a
diff --git a/src/lib/layers.ts b/src/lib/layers.ts
new file mode 100644
index 00000000..13900d70
--- /dev/null
+++ b/src/lib/layers.ts
@@ -0,0 +1,856 @@
+/**
+ * Layers studio (Higgsfield Layers): the layer document, its geometry, the downloads and the edit
+ * history. Pure, so the editor, the server and the tests share it; no catalog import (the editor gets
+ * model names and prices from its page).
+ *
+ * A project is one source image plus versions of a {@link LayerDoc}. Each layer is an image file
+ * placed on the document: `x, y, w, h` in document pixels (the source image's own pixels), rotated
+ * clockwise around its centre, showing the `crop` part of its image (all of it when unset).
+ * Full-canvas RGBA layers (Qwen-Image-Layered) are cropped to their alpha box, so selection handles
+ * hug what you see.
+ */
+
+export type Box = [x: number, y: number, w: number, h: number];
+
+/** One version of a layer's pixels: switch between them in the Layers panel. */
+export interface LayerRevision {
+ /** Asset URL (/api/files/…), as everywhere in Fuobe (the server's readRef takes it). */
+ src: string;
+ /** The part of the image the layer shows (image px); null = the whole image. */
+ crop?: Box | null;
+ /** The image's pixel size. */
+ natural?: [number, number];
+ label: string;
+ prompt?: string;
+ at: number;
+}
+
+export interface LayerNode {
+ id: string;
+ name: string;
+ /** Stacking order, 0 = bottom; always the node's index after {@link normalize}. */
+ z: number;
+ x: number;
+ y: number;
+ w: number;
+ h: number;
+ /** Degrees, clockwise around the centre. */
+ rotation: number;
+ /** The image (asset URL) this layer shows: the active revision's. */
+ src: string;
+ crop?: Box | null;
+ natural?: [number, number];
+ hidden?: boolean;
+ /** 0–1 (1 when unset). */
+ opacity?: number;
+ /** Visibility mask (asset URL, PNG): its alpha is stretched over the layer box; transparent = hidden. */
+ maskSrc?: string;
+ /** Soft edge in document pixels (a regional edit's “Mask feather”). */
+ feather?: number;
+ /** The last “Layer edit prompt” and reference image. */
+ prompt?: string;
+ refSrc?: string;
+ revisions: LayerRevision[];
+ /** Index of the active revision. */
+ revision: number;
+ /** A regional edit's pair: the region as it was, and the edited one. */
+ role?: "original" | "new";
+}
+
+export interface LayerDoc {
+ v: 1;
+ width: number;
+ height: number;
+ /** The image the project was opened on (Compare's “before”). */
+ source: string;
+ /** “An image can only be decomposed once”: set after the first decomposition lands. */
+ decomposed: boolean;
+ nodes: LayerNode[];
+}
+
+// ---------------------------------------------------------------------------
+// Projects, versions and jobs as the API returns them
+
+export type LayerTool =
+ | "decompose"
+ | "split"
+ | "regenerate"
+ | "generate-layer"
+ | "text-detect"
+ | "text-edit"
+ | "bg-remove"
+ | "regional"
+ | "upscale"
+ | "relight"
+ | "angles"
+ | "expand";
+
+export interface LayerProject {
+ id: string;
+ name: string;
+ sourceUrl: string;
+ coverUrl: string;
+ width: number;
+ height: number;
+ /** Layers in the newest version. */
+ layers: number;
+ /** "processing" while a decomposition runs, "failed" when the only one failed. */
+ status: "ready" | "processing" | "failed";
+ createdAt: number;
+ updatedAt: number;
+}
+
+export interface LayerVersion {
+ id: string;
+ label: string;
+ createdAt: number;
+ updatedAt: number;
+}
+
+/** What a finished generation hands back (a slice of Generation, so the editor needs no catalog). */
+export interface LayerJobResult {
+ status: "queued" | "running" | "succeeded" | "failed" | "canceled";
+ progress: number | null;
+ message: string | null;
+ error: string | null;
+ provider: string;
+ outputs: { url: string; kind: string; meta?: Record<string, unknown>; width?: number; height?: number }[];
+}
+
+export interface LayerJob {
+ id: string;
+ tool: LayerTool;
+ /** “Separating Subject”, “Upscale”, ““SALE” → “OPEN””. */
+ label: string;
+ /** The layer it works on (none = the whole image). */
+ nodeId?: string;
+ /** Where its input sat in the document when it started (doc px). */
+ frame: Box;
+ /** Tool settings the editor needs when the result lands (the prompt, a region's feather…). */
+ settings?: Record<string, unknown>;
+ /** Every generation it started; the last one's outputs are the result. */
+ generationIds: string[];
+ state: "pending" | "applied" | "dismissed";
+ createdAt: number;
+ /** The result generation, joined by the server. */
+ result?: LayerJobResult | null;
+}
+
+export interface LayerProjectView {
+ project: LayerProject;
+ versions: LayerVersion[];
+ headVersionId: string;
+ doc: LayerDoc;
+ jobs: LayerJob[];
+}
+
+/** What the editor posts to start a tool. */
+export interface LayerJobInput {
+ tool: LayerTool;
+ engine: "cloud" | "open";
+ modelId?: string;
+ /** The picture to work on (an uploaded render of the layer, region or composite). */
+ image?: string;
+ /** Reference images (per-layer generate). */
+ refs?: string[];
+ prompt?: string;
+ nodeId?: string;
+ frame: Box;
+ label?: string;
+ settings?: {
+ resolution?: "1K" | "1.5K" | "2K";
+ mode?: "standard" | "fast";
+ layers?: number;
+ keepAlpha?: boolean;
+ light?: string;
+ direction?: string;
+ angle?: string;
+ rotate?: number;
+ tilt?: number;
+ zoom?: number;
+ rows?: { from: string; to: string }[];
+ pads?: { left: number; top: number; right: number; bottom: number };
+ feather?: number;
+ aspect?: string;
+ };
+}
+
+/** Models each tool offers, per engine (the first is the default). Checked against the catalog in tests. */
+export const TOOL_MODELS: Record<LayerTool, { cloud: string[]; open: string[] }> = {
+ decompose: { cloud: ["seedream-5-layers", "seedream-5-flash-layers", "qwen-image-layered-cloud"], open: ["oss-qwen-layered"] },
+ split: { cloud: ["seedream-5-layers", "seedream-5-flash-layers", "qwen-image-layered-cloud"], open: ["oss-qwen-layered", "oss-qwen-layered-control"] },
+ regenerate: { cloud: ["nano-banana-pro", "seedream-5-pro", "nano-banana-2"], open: ["oss-qwen-edit", "oss-flux-kontext"] },
+ "generate-layer": { cloud: ["nano-banana-pro", "seedream-5-pro"], open: ["oss-z-image-turbo", "oss-qwen-image"] },
+ "text-detect": { cloud: ["ideogram-3-text-layers"], open: [] },
+ "text-edit": { cloud: ["nano-banana-pro", "seedream-5-pro"], open: ["oss-qwen-edit"] },
+ "bg-remove": { cloud: ["birefnet-cloud", "ideogram-remove-bg"], open: ["oss-rmbg"] },
+ regional: { cloud: ["seedream-5-pro", "nano-banana-pro"], open: ["oss-qwen-edit", "oss-flux-kontext"] },
+ upscale: { cloud: ["topaz-image", "seedvr-image", "recraft-crisp"], open: ["oss-upscale-image", "oss-seedvr2-image"] },
+ relight: { cloud: ["nano-banana-pro"], open: ["oss-qwen-relight"] },
+ angles: { cloud: ["qwen-multiple-angles", "nano-banana-pro"], open: ["oss-qwen-edit"] },
+ expand: { cloud: ["nano-banana-pro"], open: ["oss-flux-fill"] },
+};
+
+/** Cuts the background back out after an edit model repaints a transparent layer. */
+export const CUTOUT_MODELS = { cloud: "birefnet-cloud", open: "oss-rmbg" } as const;
+
+/** Decomposition list prices per layer (research: paid-model-apis-3 §3). */
+export const LAYER_PRICES: Record<string, { perLayer: number; doubleAt2K?: boolean; maxLayers: number }> = {
+ "seedream-5-layers": { perLayer: 0.03375, doubleAt2K: true, maxLayers: 16 },
+ "seedream-5-flash-layers": { perLayer: 0.027, maxLayers: 16 },
+ "qwen-image-layered-cloud": { perLayer: 0.05, maxLayers: 8 },
+ "oss-qwen-layered": { perLayer: 0, maxLayers: 10 },
+ "oss-qwen-layered-control": { perLayer: 0, maxLayers: 10 },
+};
+
+/** Estimated price of a decomposition ("≈"): Seedream bills each layer it returns. */
+export function decomposeCost(modelId: string, layers: number, resolution: "1K" | "1.5K" | "2K" = "1K"): number | null {
+ const p = LAYER_PRICES[modelId];
+ if (!p) return null;
+ const n = Math.min(p.maxLayers, Math.max(1, Math.round(layers)));
+ return Math.round(p.perLayer * n * (p.doubleAt2K && resolution === "2K" ? 2 : 1) * 1000) / 1000;
+}
+
+/** The layer count a decomposition model can be asked for (Seedream picks its own, up to 16). */
+export function clampLayers(modelId: string, layers: number): number {
+ const max = LAYER_PRICES[modelId]?.maxLayers ?? 10;
+ return Math.min(max, Math.max(modelId === "oss-qwen-layered-control" ? 2 : 3, Math.round(layers)));
+}
+
+// ---------------------------------------------------------------------------
+// Names and downloads
+
+/** "Hero Shot / v2!" → "hero-shot-v2": lower case, runs of anything but a–z0–9 become "-", ≤ 48 chars. */
+export function slugify(name: string, max = 48): string {
+ return name
+ .toLowerCase()
+ .normalize("NFKD")
+ .replace(/[̀-ͯ]/g, "")
+ .replace(/[^a-z0-9]+/g, "-")
+ .replace(/^-+|-+$/g, "")
+ .slice(0, max)
+ .replace(/-+$/g, "");
+}
+
+/** `layer-z{z}-{slug}.png`, or `layer-z{z}-layer-{index}.png` when the name has no usable characters. */
+export function layerFileName(z: number, name: string, index = z): string {
+ return `layer-z${z}-${slugify(name) || `layer-${index}`}.png`;
+}
+
+/** The layers a zip holds, bottom to top, with their file names (every layer, hidden ones too). */
+export function zipPlan(doc: LayerDoc): { nodeId: string; file: string }[] {
+ const seen = new Set<string>();
+ return [...doc.nodes]
+ .sort((a, b) => a.z - b.z)
+ .map((n, i) => {
+ let file = layerFileName(n.z, n.name, i);
+ for (let k = 2; seen.has(file); k++) file = file.replace(/(-\d+)?\.png$/, `-${k}.png`);
+ seen.add(file);
+ return { nodeId: n.id, file };
+ });
+}
+
+/** The project's file names: "cropped-layers.zip", "positioned-layers.zip", "<slug>.png". */
+export function downloadFileName(kind: "cropped" | "positioned" | "image", projectName: string): string {
+ if (kind === "cropped") return "cropped-layers.zip";
+ if (kind === "positioned") return "positioned-layers.zip";
+ return `${slugify(projectName) || "image"}.png`;
+}
+
+/** Copy JSON: the document as a design file (absolute image links when `origin` is given). */
+export function docJson(doc: LayerDoc, origin = ""): string {
+ const abs = (u: string | undefined) => (u && u.startsWith("/") ? `${origin}${u}` : u);
+ return JSON.stringify(
+ {
+ width: doc.width,
+ height: doc.height,
+ source: abs(doc.source),
+ layers: [...doc.nodes]
+ .sort((a, b) => a.z - b.z)
+ .map((n) => ({
+ type: "image",
+ layerIndex: n.z,
+ name: n.name,
+ geometry: { x: round2(n.x), y: round2(n.y), width: round2(n.w), height: round2(n.h), rotation: round2(n.rotation) },
+ hidden: !!n.hidden,
+ opacity: n.opacity ?? 1,
+ source: abs(n.src),
+ ...(n.crop ? { crop: n.crop.map(round2) } : {}),
+ ...(n.maskSrc ? { mask: abs(n.maskSrc) } : {}),
+ })),
+ },
+ null,
+ 2,
+ );
+}
+
+// ---------------------------------------------------------------------------
+// Geometry
+
+const round2 = (n: number) => Math.round(n * 100) / 100;
+const finite = (n: unknown): n is number => typeof n === "number" && Number.isFinite(n);
+
+/** [x, y, w, h] from an array of four numbers, else null. */
+export function asBox(v: unknown): Box | null {
+ return Array.isArray(v) && v.length === 4 && v.every(finite) ? (v as Box) : null;
+}
+
+/** Axis-aligned bounds of a box rotated `deg` clockwise around its centre. */
+export function rotatedBounds(x: number, y: number, w: number, h: number, deg: number): Box {
+ const r = (deg * Math.PI) / 180;
+ const c = Math.abs(Math.cos(r));
+ const s = Math.abs(Math.sin(r));
+ const bw = w * c + h * s;
+ const bh = w * s + h * c;
+ return [x + w / 2 - bw / 2, y + h / 2 - bh / 2, bw, bh];
+}
+
+/** Overlap of two boxes, or null. */
+export function intersect(a: Box, b: Box): Box | null {
+ const x = Math.max(a[0], b[0]);
+ const y = Math.max(a[1], b[1]);
+ const r = Math.min(a[0] + a[2], b[0] + b[2]);
+ const btm = Math.min(a[1] + a[3], b[1] + b[3]);
+ return r > x && btm > y ? [x, y, r - x, btm - y] : null;
+}
+
+/**
+ * A layer's cropped download: its rotation-aware bounding box, rounded out to whole pixels and kept
+ * inside the document (null when nothing of it is on the canvas).
+ */
+export function cropRect(node: Pick<LayerNode, "x" | "y" | "w" | "h" | "rotation">, doc: Pick<LayerDoc, "width" | "height">): Box | null {
+ const [bx, by, bw, bh] = rotatedBounds(node.x, node.y, node.w, node.h, node.rotation);
+ const x0 = Math.floor(bx + 1e-6);
+ const y0 = Math.floor(by + 1e-6);
+ const x1 = Math.ceil(bx + bw - 1e-6);
+ const y1 = Math.ceil(by + bh - 1e-6);
+ return intersect([x0, y0, x1 - x0, y1 - y0], [0, 0, doc.width, doc.height]);
+}
+
+/** The tight box of the pixels with alpha above `threshold` in RGBA data, or null when all are clear. */
+export function bboxFromAlpha(data: ArrayLike<number>, width: number, height: number, threshold = 0): Box | null {
+ let minX = width;
+ let minY = height;
+ let maxX = -1;
+ let maxY = -1;
+ for (let y = 0; y < height; y++) {
+ const row = y * width * 4;
+ for (let x = 0; x < width; x++) {
+ if (data[row + x * 4 + 3] > threshold) {
+ if (x < minX) minX = x;
+ if (x > maxX) maxX = x;
+ if (y < minY) minY = y;
+ if (y > maxY) maxY = y;
+ }
+ }
+ }
+ return maxX < 0 ? null : [minX, minY, maxX - minX + 1, maxY - minY + 1];
+}
+
+/** Where a document point falls inside a (rotated) layer: [u, v] in 0–1, or null when outside. */
+export function toLocal(node: Pick<LayerNode, "x" | "y" | "w" | "h" | "rotation">, px: number, py: number): [number, number] | null {
+ const cx = node.x + node.w / 2;
+ const cy = node.y + node.h / 2;
+ const r = (-node.rotation * Math.PI) / 180;
+ const dx = px - cx;
+ const dy = py - cy;
+ const lx = dx * Math.cos(r) - dy * Math.sin(r);
+ const ly = dx * Math.sin(r) + dy * Math.cos(r);
+ const u = lx / node.w + 0.5;
+ const v = ly / node.h + 0.5;
+ const e = 1e-9;
+ return u >= -e && u <= 1 + e && v >= -e && v <= 1 + e ? [Math.min(1, Math.max(0, u)), Math.min(1, Math.max(0, v))] : null;
+}
+
+/** The image pixels a layer shows, from its crop (or the whole image). */
+export function sourceRect(node: Pick<LayerNode, "crop" | "natural">): Box | null {
+ if (node.crop) return node.crop;
+ return node.natural ? [0, 0, node.natural[0], node.natural[1]] : null;
+}
+
+/**
+ * The size to render one layer alone at its own resolution (the pixels it shows, not the document's),
+ * for handing it to a model. Capped at `max` on the long side.
+ */
+export function nativeSize(node: Pick<LayerNode, "w" | "h" | "crop" | "natural">, max = 2048): [number, number] {
+ const src = sourceRect(node);
+ const scale = src ? src[2] / node.w : 1;
+ let w = node.w * scale;
+ let h = node.h * scale;
+ const k = Math.min(1, max / Math.max(w, h));
+ w = Math.max(1, Math.round(w * k));
+ h = Math.max(1, Math.round(h * k));
+ return [w, h];
+}
+
+export type Handle = "n" | "s" | "e" | "w" | "ne" | "nw" | "se" | "sw";
+
+/**
+ * Resize a (rotated) layer by dragging a handle `dx, dy` document pixels (screen direction). The
+ * opposite edge or corner stays put; corners keep the aspect ratio unless `free`. Minimum 4 px.
+ */
+export function resizeBox(node: Pick<LayerNode, "x" | "y" | "w" | "h" | "rotation">, handle: Handle, dx: number, dy: number, free = false): Pick<LayerNode, "x" | "y" | "w" | "h"> {
+ const r = (node.rotation * Math.PI) / 180;
+ const cos = Math.cos(r);
+ const sin = Math.sin(r);
+ // The drag in the layer's own axes.
+ const lx = dx * cos + dy * sin;
+ const ly = -dx * sin + dy * cos;
+ const sx = handle.includes("e") ? 1 : handle.includes("w") ? -1 : 0;
+ const sy = handle.includes("s") ? 1 : handle.includes("n") ? -1 : 0;
+ let w = Math.max(4, node.w + sx * lx);
+ let h = Math.max(4, node.h + sy * ly);
+ if (sx && sy && !free) {
+ // Corners: follow the larger change, keep the shape.
+ const k = Math.max(w / node.w, h / node.h);
+ w = Math.max(4, node.w * k);
+ h = Math.max(4, node.h * k);
+ }
+ // The fixed point (opposite edge/corner) in local coords, relative to the old centre.
+ const fx = (-sx * node.w) / 2;
+ const fy = (-sy * node.h) / 2;
+ // New centre = fixed point + half the new size towards the handle, back in document axes.
+ const ncx = fx + (sx * w) / 2;
+ const ncy = fy + (sy * h) / 2;
+ const cx = node.x + node.w / 2 + ncx * cos - ncy * sin;
+ const cy = node.y + node.h / 2 + ncx * sin + ncy * cos;
+ return { x: cx - w / 2, y: cy - h / 2, w, h };
+}
+
+/** Rotation (degrees, clockwise, -180…180) that points a layer's top at a pointer; `snap` rounds to 15°. */
+export function rotationTo(cx: number, cy: number, px: number, py: number, snap = false): number {
+ let deg = (Math.atan2(px - cx, -(py - cy)) * 180) / Math.PI;
+ if (snap) deg = Math.round(deg / 15) * 15;
+ if (deg > 180) deg -= 360;
+ if (deg <= -180) deg += 360;
+ return Math.abs(deg) < 1e-9 ? 0 : deg;
+}
+
+/** A rectangle from two corner points, kept inside the document; null when smaller than `min` px. */
+export function rectFrom(x0: number, y0: number, x1: number, y1: number, doc: Pick<LayerDoc, "width" | "height">, min = 8): Box | null {
+ const b = intersect([Math.min(x0, x1), Math.min(y0, y1), Math.abs(x1 - x0), Math.abs(y1 - y0)], [0, 0, doc.width, doc.height]);
+ if (!b || b[2] < min || b[3] < min) return null;
+ return [Math.round(b[0]), Math.round(b[1]), Math.round(b[2]), Math.round(b[3])];
+}
+
+// ---------------------------------------------------------------------------
+// Placing model outputs
+
+export interface OutputMeta {
+ bbox?: unknown;
+ bboxNormalized?: unknown;
+ width?: unknown;
+ height?: unknown;
+}
+
+export interface Placement {
+ /** Where the layer goes (doc px). */
+ box: Box;
+ /** The part of the image to show (image px), null = all of it. */
+ crop: Box | null;
+ /** A full-canvas image with no box from the provider: crop it to its alpha (see {@link tightenToAlpha}). */
+ needsAlpha: boolean;
+}
+
+const near = (a: number, b: number, tol: number) => Math.abs(a - b) <= Math.max(tol, Math.abs(b) * 0.02);
+
+/**
+ * Where a model's layer goes in the document. The model worked on the picture that sat at `frame`;
+ * its layer is either the whole canvas (Qwen: full-size RGBA, boxes from alpha) or already cut to its
+ * box (Seedream may return cut layers with boxes in the base image's pixels). Boxes come as
+ * `bboxNormalized` (0–1000 or 0–1) or `bbox` in pixels of `meta.width × meta.height`, else of `ref`
+ * (Seedream: the base layer), else of the image itself.
+ */
+export function placeOutput(frame: Box, image: [number, number], meta: OutputMeta = {}, ref?: [number, number]): Placement {
+ const [iw, ih] = image;
+ const refW = finite(meta.width) && meta.width > 0 ? meta.width : ref?.[0] ?? iw;
+ const refH = finite(meta.height) && meta.height > 0 ? meta.height : ref?.[1] ?? ih;
+ let rel: Box | null = null;
+ const bn = asBox(meta.bboxNormalized);
+ const b = asBox(meta.bbox);
+ if (bn) {
+ const k = Math.max(...bn.map(Math.abs)) > 1.5 ? 1000 : 1;
+ rel = [bn[0] / k, bn[1] / k, bn[2] / k, bn[3] / k];
+ } else if (b) rel = [b[0] / refW, b[1] / refH, b[2] / refW, b[3] / refH];
+ if (rel && (rel[2] <= 0 || rel[3] <= 0)) rel = null;
+ const map = (r: Box): Box => [frame[0] + r[0] * frame[2], frame[1] + r[1] * frame[3], r[2] * frame[2], r[3] * frame[3]];
+ if (!rel) return { box: frame, crop: null, needsAlpha: true };
+ const whole = near(rel[0], 0, 0.005) && near(rel[1], 0, 0.005) && near(rel[2], 1, 0.005) && near(rel[3], 1, 0.005);
+ if (whole) return { box: frame, crop: null, needsAlpha: false };
+ // Already cut to its box: the image is the region's size.
+ const cut = near(iw, rel[2] * refW, 2) && near(ih, rel[3] * refH, 2);
+ const fullCanvas = !cut && near(iw / ih, refW / refH, 0.01);
+ if (!fullCanvas) return { box: map(rel), crop: null, needsAlpha: false };
+ return { box: map(rel), crop: [rel[0] * iw, rel[1] * ih, rel[2] * iw, rel[3] * ih], needsAlpha: false };
+}
+
+/** A full-canvas layer cropped to its alpha box (image px); null when the layer is empty. */
+export function tightenToAlpha(frame: Box, image: [number, number], alpha: Box | null): Placement | null {
+ if (!alpha) return null;
+ const [iw, ih] = image;
+ const rel: Box = [alpha[0] / iw, alpha[1] / ih, alpha[2] / iw, alpha[3] / ih];
+ return { box: [frame[0] + rel[0] * frame[2], frame[1] + rel[1] * frame[3], rel[2] * frame[2], rel[3] * frame[3]], crop: alpha, needsAlpha: false };
+}
+
+export interface MeasuredOutput {
+ url: string;
+ meta?: Record<string, unknown>;
+ natural: [number, number];
+ placement: Placement;
+}
+
+/** A layer's display name from a model's meta, else "Background" / "Layer n". */
+export function outputName(meta: Record<string, unknown> | undefined, z: number): string {
+ const n = typeof meta?.name === "string" ? meta.name.trim() : "";
+ return n ? n.slice(0, 80) : z === 0 ? "Background" : `Layer ${z}`;
+}
+
+/**
+ * New layer nodes from measured model outputs, bottom to top by meta.z (then output order). A split
+ * passes the layer's `frame` and `rotation`: the parts turn with it around its centre. Unnamed parts
+ * of a split are called “{baseName} · part n”.
+ */
+export function nodesFromOutputs(
+ outs: MeasuredOutput[],
+ opts: { id: () => string; label: string; at: number; baseName?: string; frame?: Box; rotation?: number },
+): LayerNode[] {
+ const rot = opts.rotation ?? 0;
+ const r = (rot * Math.PI) / 180;
+ return outs
+ .map((o, i) => ({ o, i, z: finite(o.meta?.z) ? (o.meta!.z as number) : i }))
+ .sort((a, b) => a.z - b.z || a.i - b.i)
+ .map(({ o }, i) => {
+ let [x, y] = o.placement.box;
+ const [, , w, h] = o.placement.box;
+ if (rot && opts.frame) {
+ // Turn the part's centre around the frame's centre.
+ const fcx = opts.frame[0] + opts.frame[2] / 2;
+ const fcy = opts.frame[1] + opts.frame[3] / 2;
+ const dx = x + w / 2 - fcx;
+ const dy = y + h / 2 - fcy;
+ x = fcx + dx * Math.cos(r) - dy * Math.sin(r) - w / 2;
+ y = fcy + dx * Math.sin(r) + dy * Math.cos(r) - h / 2;
+ }
+ const rev: LayerRevision = { src: o.url, crop: o.placement.crop, natural: o.natural, label: opts.label, at: opts.at };
+ // Providers without names call them "Background" / "Layer n": those read as unnamed parts.
+ const named = typeof o.meta?.name === "string" && o.meta.name.trim() && !/^(background|layer \d+)$/i.test(o.meta.name.trim());
+ const name = opts.baseName && !named ? `${opts.baseName} · part ${i + 1}`.slice(0, 80) : outputName(o.meta, i);
+ return { id: opts.id(), name, z: i, x, y, w, h, rotation: rot, src: o.url, crop: o.placement.crop, natural: o.natural, revisions: [rev], revision: 0 };
+ });
+}
+
+// ---------------------------------------------------------------------------
+// Document operations (all return a new document)
+
+/** A new project's document: the source image as its only layer. */
+export function initialDoc(source: string, width: number, height: number, at = Date.now(), id = "original"): LayerDoc {
+ const rev: LayerRevision = { src: source, crop: null, natural: [width, height], label: "Original", at };
+ return {
+ v: 1,
+ width,
+ height,
+ source,
+ decomposed: false,
+ nodes: [{ id, name: "Original image", z: 0, x: 0, y: 0, w: width, h: height, rotation: 0, src: source, crop: null, natural: [width, height], revisions: [rev], revision: 0 }],
+ };
+}
+
+/** Nodes sorted by z with z = index. */
+export function normalize(doc: LayerDoc): LayerDoc {
+ const nodes = [...doc.nodes].sort((a, b) => a.z - b.z).map((n, i) => (n.z === i ? n : { ...n, z: i }));
+ return nodes.every((n, i) => n === doc.nodes[i]) ? doc : { ...doc, nodes };
+}
+
+const withNodes = (doc: LayerDoc, nodes: LayerNode[]): LayerDoc => normalize({ ...doc, nodes: nodes.map((n, i) => (n.z === i ? n : { ...n, z: i })) });
+
+export function getNode(doc: LayerDoc, id: string | null | undefined): LayerNode | undefined {
+ return id ? doc.nodes.find((n) => n.id === id) : undefined;
+}
+
+export function updateNode(doc: LayerDoc, id: string, patch: Partial<LayerNode>): LayerDoc {
+ let changed = false;
+ const nodes = doc.nodes.map((n) => {
+ if (n.id !== id) return n;
+ const next = { ...n, ...patch };
+ changed = (Object.keys(patch) as (keyof LayerNode)[]).some((k) => n[k] !== next[k]);
+ return changed ? next : n;
+ });
+ return changed ? { ...doc, nodes } : doc;
+}
+
+/** Move a layer to stacking position `to` (0 = bottom). */
+export function moveNode(doc: LayerDoc, id: string, to: number): LayerDoc {
+ const sorted = [...doc.nodes].sort((a, b) => a.z - b.z);
+ const from = sorted.findIndex((n) => n.id === id);
+ const dest = Math.max(0, Math.min(sorted.length - 1, Math.round(to)));
+ if (from < 0 || from === dest) return doc;
+ const [n] = sorted.splice(from, 1);
+ sorted.splice(dest, 0, n);
+ return withNodes(doc, sorted);
+}
+
+/** A copy right above the original, in the same place. */
+export function duplicateNode(doc: LayerDoc, id: string, newId: string): LayerDoc {
+ const sorted = [...doc.nodes].sort((a, b) => a.z - b.z);
+ const i = sorted.findIndex((n) => n.id === id);
+ if (i < 0) return doc;
+ const src = sorted[i];
+ sorted.splice(i + 1, 0, { ...src, id: newId, name: `${src.name} copy`.slice(0, 80), revisions: src.revisions.map((r) => ({ ...r })), role: undefined });
+ return withNodes(doc, sorted);
+}
+
+export function deleteNode(doc: LayerDoc, id: string): LayerDoc {
+ return doc.nodes.some((n) => n.id === id) ? withNodes(doc, [...doc.nodes].sort((a, b) => a.z - b.z).filter((n) => n.id !== id)) : doc;
+}
+
+/** Insert layers at stacking position `at` (default: on top). */
+export function addNodes(doc: LayerDoc, add: LayerNode[], at?: number): LayerDoc {
+ const sorted = [...doc.nodes].sort((a, b) => a.z - b.z);
+ sorted.splice(at === undefined ? sorted.length : Math.max(0, Math.min(sorted.length, at)), 0, ...add);
+ return withNodes(doc, sorted);
+}
+
+/** Swap layers for others in the place of the topmost one (a split, a merged region). */
+export function replaceNodes(doc: LayerDoc, ids: string[], add: LayerNode[]): LayerDoc {
+ const sorted = [...doc.nodes].sort((a, b) => a.z - b.z);
+ const idx = sorted.map((n, i) => (ids.includes(n.id) ? i : -1)).filter((i) => i >= 0);
+ if (!idx.length) return doc;
+ const keep = sorted.filter((n) => !ids.includes(n.id));
+ const at = Math.max(...idx) - (idx.length - 1);
+ keep.splice(at, 0, ...add);
+ return withNodes(doc, keep);
+}
+
+/** Keep the centre and width, take the height from the image's shape. */
+function refit(node: LayerNode, rev: Pick<LayerRevision, "crop" | "natural">): Pick<LayerNode, "x" | "y" | "w" | "h"> {
+ const src = rev.crop ?? (rev.natural ? ([0, 0, rev.natural[0], rev.natural[1]] as Box) : null);
+ if (!src || src[2] <= 0 || src[3] <= 0) return node;
+ const aspect = src[2] / src[3];
+ if (near(node.w / node.h, aspect, 0.01)) return node;
+ const h = node.w / aspect;
+ return { x: node.x, y: node.y + node.h / 2 - h / 2, w: node.w, h };
+}
+
+/** Add a new version of a layer's pixels and switch to it. */
+export function addRevision(node: LayerNode, rev: LayerRevision): LayerNode {
+ const revisions = node.revisions.length ? [...node.revisions, rev] : [{ src: node.src, crop: node.crop ?? null, natural: node.natural, label: "Before", at: rev.at - 1 }, rev];
+ return { ...node, ...refit(node, rev), src: rev.src, crop: rev.crop ?? null, natural: rev.natural, revisions, revision: revisions.length - 1 };
+}
+
+/** Show another of a layer's revisions (“Show previous versions”, “Restore layer revision”). */
+export function setRevision(node: LayerNode, index: number): LayerNode {
+ const i = Math.max(0, Math.min(node.revisions.length - 1, Math.round(index)));
+ const rev = node.revisions[i];
+ if (!rev || i === node.revision) return node;
+ return { ...node, ...refit(node, rev), src: rev.src, crop: rev.crop ?? null, natural: rev.natural, revision: i };
+}
+
+/**
+ * Expand & Crop: the document becomes `rect` (doc px, may reach outside the old canvas). Layers keep
+ * their pixels and move with the frame. Throws when the frame misses the picture.
+ */
+export function reframeDoc(doc: LayerDoc, rect: Box): LayerDoc {
+ const [rx, ry, rw, rh] = rect.map(Math.round) as Box;
+ if (rw < 8 || rh < 8 || !intersect([rx, ry, rw, rh], [0, 0, doc.width, doc.height])) throw new Error("The frame no longer overlaps the image. Move it back over the picture.");
+ if (rx === 0 && ry === 0 && rw === doc.width && rh === doc.height) return doc;
+ return { ...doc, width: rw, height: rh, nodes: doc.nodes.map((n) => ({ ...n, x: n.x - rx, y: n.y - ry })) };
+}
+
+/** Outpaint padding for an expanded frame (whole pixels, multiples of 8 as the open workflow wants). */
+export function expandPads(doc: Pick<LayerDoc, "width" | "height">, rect: Box): { left: number; top: number; right: number; bottom: number } {
+ const up8 = (n: number) => Math.ceil(Math.max(0, n) / 8) * 8;
+ return { left: up8(-rect[0]), top: up8(-rect[1]), right: up8(rect[0] + rect[2] - doc.width), bottom: up8(rect[1] + rect[3] - doc.height) };
+}
+
+// ---------------------------------------------------------------------------
+// Edit history (undo / redo within a session; versions are saved snapshots on the server)
+
+export interface History {
+ past: LayerDoc[];
+ present: LayerDoc;
+ future: LayerDoc[];
+}
+
+export type HistoryAction = { type: "commit"; doc: LayerDoc } | { type: "undo" } | { type: "redo" } | { type: "reset"; doc: LayerDoc };
+
+export const HISTORY_LIMIT = 100;
+
+export function historyReducer(h: History, a: HistoryAction): History {
+ switch (a.type) {
+ case "commit":
+ if (a.doc === h.present) return h;
+ return { past: [...h.past, h.present].slice(-HISTORY_LIMIT), present: a.doc, future: [] };
+ case "undo":
+ if (!h.past.length) return h;
+ return { past: h.past.slice(0, -1), present: h.past[h.past.length - 1], future: [h.present, ...h.future] };
+ case "redo":
+ if (!h.future.length) return h;
+ return { past: [...h.past, h.present], present: h.future[0], future: h.future.slice(1) };
+ case "reset":
+ return { past: [], present: a.doc, future: [] };
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Edit text
+
+export const TEXT_EDIT_PROMPT = "Preserve the original design and change only the requested text.";
+
+export interface TextRow {
+ from: string;
+ to: string;
+}
+
+/** The changes asked for: rows whose text changed (an emptied row removes that text). */
+export function textChanges(rows: TextRow[]): TextRow[] {
+ return rows.filter((r) => r.from.trim() && r.to.trim() !== r.from.trim()).slice(0, 500);
+}
+
+/** The edit instruction: the fixed sentence, then each change. */
+export function textEditPrompt(rows: TextRow[]): string {
+ const lines = textChanges(rows).map((r) => (r.to.trim() ? `Change “${r.from.trim()}” to “${r.to.trim()}”.` : `Remove “${r.from.trim()}”.`));
+ return [TEXT_EDIT_PROMPT, ...lines].join(" ");
+}
+
+const cut24 = (s: string) => (s.length > 24 ? `${s.slice(0, 23)}…` : s);
+
+/** “old” → “new” / Remove “old” / {n} text changes. */
+export function textJobTitle(rows: TextRow[]): string {
+ const c = textChanges(rows);
+ if (c.length !== 1) return `${c.length} text changes`;
+ const [r] = c;
+ return r.to.trim() ? `“${cut24(r.from.trim())}” → “${cut24(r.to.trim())}”` : `Remove “${cut24(r.from.trim())}”`;
+}
+
+export interface TextBlock {
+ text: string;
+ /** 0–1 of the image, when the model said where. */
+ box: Box | null;
+}
+
+/** Text blocks from a text-layers result (OutputFile.meta.textBlocks), boxes normalised against the image size. */
+export function textBlocksFrom(meta: Record<string, unknown> | undefined, image?: [number, number]): TextBlock[] {
+ const raw = Array.isArray(meta?.textBlocks) ? meta.textBlocks : [];
+ return raw
+ .map((b): TextBlock | null => {
+ if (!b || typeof b !== "object") return null;
+ const o = b as Record<string, unknown>;
+ const text = typeof o.text === "string" ? o.text.replace(/\s+/g, " ").trim() : "";
+ if (!text) return null;
+ let box = asBox(o.bbox);
+ if (!box && o.bbox && typeof o.bbox === "object") {
+ const q = o.bbox as Record<string, unknown>;
+ box = asBox([q.x, q.y, q.w ?? q.width, q.h ?? q.height]);
+ }
+ if (box && Math.max(...box) > 1.0001) box = image ? [box[0] / image[0], box[1] / image[1], box[2] / image[0], box[3] / image[1]] : null;
+ return { text, box };
+ })
+ .filter((b): b is TextBlock => !!b);
+}
+
+// ---------------------------------------------------------------------------
+// Decomposition states (Higgsfield's copy)
+
+export type DecomposeState = "setting-up" | "submitting" | "processing" | "stalled" | "ready" | "failed" | "decomposed";
+
+export const DECOMPOSE_COPY: Record<DecomposeState, [string, string]> = {
+ "setting-up": ["Setting up layer decomposition", "One moment."],
+ submitting: ["Starting layer decomposition", "Preparing your image for processing."],
+ processing: ["Separating image layers", "Your editable layers are being prepared."],
+ stalled: ["This is taking longer than expected", "We could not prepare this image for layers. Try again, and if it keeps happening, reload the page."],
+ ready: ["Layers are ready", "Opening the layer editor."],
+ failed: ["Layer decomposition failed", "Try again, or return to the settings."],
+ decomposed: ["Decomposed", "This image is split into # layers. Edit them on the canvas or in the Layers panel."],
+};
+
+/** A queued job that hasn't moved for this long reads as stalled. */
+export const STALL_MS = 3 * 60_000;
+
+/** "In queue" / "In progress 40%" for a job's generation. */
+export function jobStatusText(r: LayerJobResult | null | undefined): string {
+ if (!r) return "Starting…";
+ if (r.status === "queued") return "In queue";
+ if (r.status === "running") return r.progress ? `In progress · ${Math.round(r.progress * 100)}%` : "In progress";
+ if (r.status === "failed") return r.error ?? "Failed";
+ if (r.status === "canceled") return "Canceled";
+ return "Done";
+}
+
+/** Tools whose result changes the document when it lands (text detection only fills the Edit text list). */
+export const APPLIES_TO_DOC = new Set<LayerTool>(["decompose", "split", "regenerate", "generate-layer", "text-edit", "bg-remove", "regional", "upscale", "relight", "angles", "expand"]);
+
+// ---------------------------------------------------------------------------
+// Zip (stored): built in the browser from rendered PNGs
+
+const CRC_TABLE = (() => {
+ const t = new Uint32Array(256);
+ for (let n = 0; n < 256; n++) {
+ let c = n;
+ for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
+ t[n] = c >>> 0;
+ }
+ return t;
+})();
+
+export function crc32(data: Uint8Array): number {
+ let c = 0xffffffff;
+ for (let i = 0; i < data.length; i++) c = CRC_TABLE[(c ^ data[i]) & 0xff] ^ (c >>> 8);
+ return (c ^ 0xffffffff) >>> 0;
+}
+
+/** A zip of the files, uncompressed (PNGs are already compressed). UTF-8 names. */
+export function storedZip(files: { name: string; data: Uint8Array }[], when = new Date()): Uint8Array {
+ const enc = new TextEncoder();
+ const time = (when.getHours() << 11) | (when.getMinutes() << 5) | Math.floor(when.getSeconds() / 2);
+ const date = when.getFullYear() < 1980 ? (1 << 5) | 1 : ((when.getFullYear() - 1980) << 9) | ((when.getMonth() + 1) << 5) | when.getDate();
+ const parts: Uint8Array[] = [];
+ const central: Uint8Array[] = [];
+ let offset = 0;
+ for (const f of files) {
+ const name = enc.encode(f.name);
+ const crc = crc32(f.data);
+ const head = new DataView(new ArrayBuffer(30));
+ head.setUint32(0, 0x04034b50, true);
+ head.setUint16(4, 20, true);
+ head.setUint16(6, 0x0800, true);
+ head.setUint16(8, 0, true);
+ head.setUint16(10, time, true);
+ head.setUint16(12, date, true);
+ head.setUint32(14, crc, true);
+ head.setUint32(18, f.data.length, true);
+ head.setUint32(22, f.data.length, true);
+ head.setUint16(26, name.length, true);
+ head.setUint16(28, 0, true);
+ parts.push(new Uint8Array(head.buffer), name, f.data);
+ const dir = new DataView(new ArrayBuffer(46));
+ dir.setUint32(0, 0x02014b50, true);
+ dir.setUint16(4, 20, true);
+ dir.setUint16(6, 20, true);
+ dir.setUint16(8, 0x0800, true);
+ dir.setUint16(10, 0, true);
+ dir.setUint16(12, time, true);
+ dir.setUint16(14, date, true);
+ dir.setUint32(16, crc, true);
+ dir.setUint32(20, f.data.length, true);
+ dir.setUint32(24, f.data.length, true);
+ dir.setUint16(28, name.length, true);
+ dir.setUint32(42, offset, true);
+ central.push(new Uint8Array(dir.buffer), name);
+ offset += 30 + name.length + f.data.length;
+ }
+ const dirSize = central.reduce((s, p) => s + p.length, 0);
+ const end = new DataView(new ArrayBuffer(22));
+ end.setUint32(0, 0x06054b50, true);
+ end.setUint16(8, files.length, true);
+ end.setUint16(10, files.length, true);
+ end.setUint32(12, dirSize, true);
+ end.setUint32(16, offset, true);
+ const all = [...parts, ...central, new Uint8Array(end.buffer)];
+ const out = new Uint8Array(all.reduce((s, p) => s + p.length, 0));
+ let o = 0;
+ for (const p of all) {
+ out.set(p, o);
+ o += p.length;
+ }
+ return out;
+}
diff --git a/tests/layers.test.ts b/tests/layers.test.ts
new file mode 100644
index 00000000..092b36c9
--- /dev/null
+++ b/tests/layers.test.ts
@@ -0,0 +1,431 @@
+import { execFileSync } from "node:child_process";
+import fs from "node:fs";
+import os from "node:os";
+import path from "node:path";
+import { describe, expect, it } from "vitest";
+import { getModel, supportsOp } from "@/lib/catalog";
+import type { Operation } from "@/lib/catalog/types";
+import {
+ addNodes,
+ addRevision,
+ bboxFromAlpha,
+ clampLayers,
+ crc32,
+ cropRect,
+ decomposeCost,
+ deleteNode,
+ docJson,
+ duplicateNode,
+ expandPads,
+ historyReducer,
+ initialDoc,
+ layerFileName,
+ moveNode,
+ nativeSize,
+ nodesFromOutputs,
+ placeOutput,
+ rectFrom,
+ reframeDoc,
+ replaceNodes,
+ resizeBox,
+ rotatedBounds,
+ rotationTo,
+ setRevision,
+ slugify,
+ storedZip,
+ textBlocksFrom,
+ textEditPrompt,
+ textJobTitle,
+ tightenToAlpha,
+ toLocal,
+ TOOL_MODELS,
+ zipPlan,
+ type Box,
+ type History,
+ type LayerDoc,
+ type LayerNode,
+} from "@/lib/layers";
+
+const node = (id: string, z: number, extra: Partial<LayerNode> = {}): LayerNode => ({
+ id,
+ name: id,
+ z,
+ x: 0,
+ y: 0,
+ w: 100,
+ h: 50,
+ rotation: 0,
+ src: `/api/files/${id}.png`,
+ natural: [100, 50],
+ revisions: [{ src: `/api/files/${id}.png`, natural: [100, 50], label: "Original", at: 1 }],
+ revision: 0,
+ ...extra,
+});
+
+const doc = (nodes: LayerNode[], w = 400, h = 300): LayerDoc => ({ v: 1, width: w, height: h, source: "/api/files/src.png", decomposed: true, nodes });
+const close = (a: number[], b: number[], digits = 6) => a.forEach((v, i) => expect(v).toBeCloseTo(b[i], digits));
+
+describe("zip naming", () => {
+ it("slugs names the way Higgsfield names layer files", () => {
+ expect(slugify("Hero Shot / v2!")).toBe("hero-shot-v2");
+ expect(slugify(" --Café Crème-- ")).toBe("cafe-creme");
+ expect(slugify("x".repeat(60))).toHaveLength(48);
+ // Cut at 48, then no dangling dash.
+ expect(slugify(`${"a".repeat(47)} b`)).toBe("a".repeat(47));
+ expect(layerFileName(3, "Red Guitar")).toBe("layer-z3-red-guitar.png");
+ expect(layerFileName(0, "背景", 0)).toBe("layer-z0-layer-0.png");
+ expect(layerFileName(2, "!!!", 5)).toBe("layer-z2-layer-5.png");
+ });
+
+ it("orders zip entries by z, keeps hidden layers and never repeats a name", () => {
+ const d = doc([node("b", 1, { name: "Subject" }), node("a", 0, { name: "Background", hidden: true }), node("c", 2, { name: "Subject" })]);
+ expect(zipPlan(d)).toEqual([
+ { nodeId: "a", file: "layer-z0-background.png" },
+ { nodeId: "b", file: "layer-z1-subject.png" },
+ { nodeId: "c", file: "layer-z2-subject.png" },
+ ]);
+ const twins = doc([node("a", 0, { name: "A" }), node("b", 0, { name: "A" })]);
+ expect(zipPlan(twins).map((e) => e.file)).toEqual(["layer-z0-a.png", "layer-z0-a-2.png"]);
+ });
+
+ it("writes a stored zip other tools can read back", () => {
+ const data = new TextEncoder().encode("hello layers");
+ // Known CRC-32 of "123456789".
+ expect(crc32(new TextEncoder().encode("123456789"))).toBe(0xcbf43926);
+ const zip = storedZip([
+ { name: "composite.png", data: new Uint8Array([137, 80, 78, 71, 1, 2, 3]) },
+ { name: "layer-z0-background.png", data },
+ ]);
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "fuobe-layers-zip-"));
+ try {
+ const file = path.join(dir, "cropped-layers.zip");
+ fs.writeFileSync(file, zip);
+ const out = execFileSync("python3", ["-c", "import sys,zipfile;z=zipfile.ZipFile(sys.argv[1]);assert z.testzip() is None;print('|'.join(i.filename+':'+str(i.file_size) for i in z.infolist()));print(z.read('layer-z0-background.png').decode())", file]).toString().trim().split("\n");
+ expect(out[0]).toBe("composite.png:7|layer-z0-background.png:12");
+ expect(out[1]).toBe("hello layers");
+ } finally {
+ fs.rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
+ it("copies the document as JSON with absolute links", () => {
+ const d = doc([node("a", 0, { name: "Background", crop: [1, 2, 3, 4] })]);
+ const j = JSON.parse(docJson(d, "http://localhost:3000"));
+ expect(j).toMatchObject({ width: 400, height: 300, source: "http://localhost:3000/api/files/src.png" });
+ expect(j.layers[0]).toEqual({
+ type: "image",
+ layerIndex: 0,
+ name: "Background",
+ geometry: { x: 0, y: 0, width: 100, height: 50, rotation: 0 },
+ hidden: false,
+ opacity: 1,
+ source: "http://localhost:3000/api/files/a.png",
+ crop: [1, 2, 3, 4],
+ });
+ });
+});
+
+describe("geometry", () => {
+ it("finds the alpha box", () => {
+ const w = 8;
+ const h = 6;
+ const data = new Uint8ClampedArray(w * h * 4);
+ expect(bboxFromAlpha(data, w, h)).toBeNull();
+ const set = (x: number, y: number, a = 255) => (data[(y * w + x) * 4 + 3] = a);
+ set(2, 1);
+ set(5, 4);
+ set(7, 0, 3); // faint: below the threshold below
+ expect(bboxFromAlpha(data, w, h, 8)).toEqual([2, 1, 4, 4]);
+ expect(bboxFromAlpha(data, w, h)).toEqual([2, 0, 6, 5]);
+ });
+
+ it("crops a layer to its rotation-aware box inside the canvas", () => {
+ expect(rotatedBounds(0, 0, 100, 50, 0)).toEqual([0, 0, 100, 50]);
+ close(rotatedBounds(0, 0, 100, 50, 90), [25, -25, 50, 100]);
+ const d = 100 * Math.SQRT1_2;
+ close(rotatedBounds(0, 0, 100, 100, 45), [50 - d, 50 - d, 2 * d, 2 * d]);
+ // Whole pixels, outward.
+ expect(cropRect({ x: 10.4, y: 20.6, w: 30.2, h: 10, rotation: 0 }, { width: 400, height: 300 })).toEqual([10, 20, 31, 11]);
+ // Clipped to the document; null once it's fully off canvas.
+ expect(cropRect({ x: -20, y: 280, w: 100, h: 50, rotation: 0 }, { width: 400, height: 300 })).toEqual([0, 280, 80, 20]);
+ expect(cropRect({ x: 500, y: 0, w: 10, h: 10, rotation: 0 }, { width: 400, height: 300 })).toBeNull();
+ // 90° turn: the box swaps sides around the centre.
+ expect(cropRect({ x: 100, y: 100, w: 100, h: 50, rotation: 90 }, { width: 400, height: 300 })).toEqual([125, 75, 50, 100]);
+ });
+
+ it("maps document points into a rotated layer", () => {
+ const n = { x: 100, y: 100, w: 100, h: 50, rotation: 0 };
+ close(toLocal(n, 150, 125)!, [0.5, 0.5]);
+ close(toLocal(n, 100, 100)!, [0, 0]);
+ expect(toLocal(n, 99, 100)).toBeNull();
+ const turned = { ...n, rotation: 90 };
+ // After a quarter turn clockwise, the layer's top-left corner sits at the right top of the rotated shape.
+ close(toLocal(turned, 175, 75)!, [0, 0]);
+ expect(toLocal(turned, 110, 125)).toBeNull();
+ });
+
+ it("resizes from a handle, keeping the opposite side fixed", () => {
+ const n = { x: 0, y: 0, w: 100, h: 50, rotation: 0 };
+ expect(resizeBox(n, "e", 20, 999)).toEqual({ x: 0, y: 0, w: 120, h: 50 });
+ expect(resizeBox(n, "w", 20, 0)).toEqual({ x: 20, y: 0, w: 80, h: 50 });
+ // Corners keep the shape unless free.
+ expect(resizeBox(n, "se", 100, 0)).toEqual({ x: 0, y: 0, w: 200, h: 100 });
+ expect(resizeBox(n, "se", 100, 0, true)).toEqual({ x: 0, y: 0, w: 200, h: 50 });
+ expect(resizeBox(n, "nw", -100, 0)).toEqual({ x: -100, y: -50, w: 200, h: 100 });
+ // Never below 4 px.
+ expect(resizeBox(n, "e", -500, 0).w).toBe(4);
+ // Rotated 90°: dragging the east handle (now pointing down) by +20 in y grows the width, west edge fixed.
+ const r = resizeBox({ ...n, rotation: 90 }, "e", 0, 20);
+ close([r.w, r.h], [120, 50]);
+ // The fixed (west) edge's midpoint stays where it was: centre (50,25) + rotate(-50,0) = (50,-25).
+ const cx = r.x + r.w / 2;
+ const cy = r.y + r.h / 2;
+ close([cx + 0, cy - r.w / 2], [50, -25]);
+ });
+
+ it("turns towards the pointer, snapping to 15°", () => {
+ expect(rotationTo(0, 0, 0, -10)).toBe(0);
+ expect(rotationTo(0, 0, 10, 0)).toBeCloseTo(90);
+ expect(rotationTo(0, 0, 0, 10)).toBeCloseTo(180);
+ expect(rotationTo(0, 0, -10, 0)).toBeCloseTo(-90);
+ expect(rotationTo(0, 0, 10, -9, true)).toBe(45);
+ });
+
+ it("draws region rectangles inside the canvas", () => {
+ expect(rectFrom(50, 60, 10, 20, { width: 400, height: 300 })).toEqual([10, 20, 40, 40]);
+ expect(rectFrom(-50, -50, 30, 30, { width: 400, height: 300 })).toEqual([0, 0, 30, 30]);
+ expect(rectFrom(0, 0, 4, 100, { width: 400, height: 300 })).toBeNull();
+ });
+
+ it("renders a layer at its own resolution", () => {
+ // A layer cropped out of a 2048-wide image but shown 512 wide: 4× the document pixels.
+ expect(nativeSize({ w: 512, h: 256, crop: [0, 0, 2048, 1024], natural: [4096, 4096] })).toEqual([2048, 1024]);
+ expect(nativeSize({ w: 512, h: 256, crop: null, natural: [1024, 512] })).toEqual([1024, 512]);
+ expect(nativeSize({ w: 512, h: 256, crop: [0, 0, 4096, 2048] }, 1000)).toEqual([1000, 500]);
+ });
+});
+
+describe("placing model outputs", () => {
+ const frame: Box = [0, 0, 1920, 1080];
+
+ it("crops full-canvas layers with pixel boxes (demo, ComfyUI)", () => {
+ // A 1024×576 full-canvas layer whose content sits at [256, 144, 512, 288].
+ const p = placeOutput(frame, [1024, 576], { bbox: [256, 144, 512, 288] });
+ expect(p.needsAlpha).toBe(false);
+ expect(p.crop).toEqual([256, 144, 512, 288]);
+ close(p.box, [480, 270, 960, 540]);
+ // ComfyUI says the size the box is measured in.
+ const c = placeOutput(frame, [640, 360], { bbox: [64, 36, 320, 180], width: 640, height: 360 });
+ close(c.box, [192, 108, 960, 540]);
+ });
+
+ it("places layers already cut to their box (Seedream) by the normalised box", () => {
+ // Base image 2048×1152; this layer is a 512×288 cut whose box is [0.25, 0.25, 0.25, 0.25] (0–1000 scale).
+ const p = placeOutput(frame, [512, 288], { bbox: [512, 288, 512, 288], bboxNormalized: [250, 250, 250, 250] }, [2048, 1152]);
+ expect(p.crop).toBeNull();
+ close(p.box, [480, 270, 480, 270]);
+ // The same layer as a full-size transparent canvas: crop the box out of it.
+ const f = placeOutput(frame, [2048, 1152], { bbox: [512, 288, 512, 288], bboxNormalized: [250, 250, 250, 250] }, [2048, 1152]);
+ close(f.crop!, [512, 288, 512, 288]);
+ close(f.box, [480, 270, 480, 270]);
+ });
+
+ it("covers the frame for the base layer and asks for alpha when there's no box (Qwen)", () => {
+ expect(placeOutput(frame, [2048, 1152], { bboxNormalized: [0, 0, 1000, 1000] })).toEqual({ box: frame, crop: null, needsAlpha: false });
+ expect(placeOutput(frame, [1024, 576], {})).toEqual({ box: frame, crop: null, needsAlpha: true });
+ const t = tightenToAlpha(frame, [1024, 576], [512, 0, 512, 288])!;
+ expect(t.crop).toEqual([512, 0, 512, 288]);
+ close(t.box, [960, 0, 960, 540]);
+ expect(tightenToAlpha(frame, [1024, 576], null)).toBeNull();
+ });
+
+ it("maps a split into the layer it came from, turning with it", () => {
+ const at = 5;
+ let i = 0;
+ const id = () => `n${++i}`;
+ const layer: Box = [100, 100, 200, 100];
+ const outs = [
+ { url: "/b.png", meta: { z: 1, name: "Layer 1", bbox: [100, 0, 100, 100] }, natural: [200, 100] as [number, number] },
+ { url: "/a.png", meta: { z: 0, name: "Background", bbox: [0, 0, 200, 100] }, natural: [200, 100] as [number, number] },
+ ].map((o) => ({ ...o, placement: placeOutput(layer, o.natural, o.meta) }));
+ const nodes = nodesFromOutputs(outs, { id, label: "Split", at, baseName: "Poster" });
+ expect(nodes.map((n) => [n.id, n.name, n.z, n.src])).toEqual([
+ ["n1", "Poster · part 1", 0, "/a.png"],
+ ["n2", "Poster · part 2", 1, "/b.png"],
+ ]);
+ close([nodes[1].x, nodes[1].y, nodes[1].w, nodes[1].h], [200, 100, 100, 100]);
+ // The same split of a layer turned 90°: the right half ends up below the centre.
+ const turned = nodesFromOutputs(outs, { id, label: "Split", at, frame: layer, rotation: 90 });
+ close([turned[1].x + turned[1].w / 2, turned[1].y + turned[1].h / 2], [200, 200]);
+ expect(turned[1].rotation).toBe(90);
+ expect(turned.map((n) => n.name)).toEqual(["Background", "Layer 1"]);
+ });
+});
+
+describe("document operations", () => {
+ const base = () => doc([node("a", 0), node("b", 1), node("c", 2)]);
+
+ it("starts from the source image as one layer", () => {
+ const d = initialDoc("/api/files/x.png", 640, 480, 7);
+ expect(d).toMatchObject({ width: 640, height: 480, decomposed: false, source: "/api/files/x.png" });
+ expect(d.nodes).toHaveLength(1);
+ expect(d.nodes[0]).toMatchObject({ name: "Original image", x: 0, y: 0, w: 640, h: 480, src: "/api/files/x.png", revision: 0 });
+ });
+
+ it("reorders, duplicates, deletes and inserts keeping z = position", () => {
+ const zs = (d: LayerDoc) => [...d.nodes].sort((x, y) => x.z - y.z).map((n) => `${n.id}${n.z}`).join(" ");
+ expect(zs(moveNode(base(), "a", 2))).toBe("b0 c1 a2");
+ expect(zs(moveNode(base(), "c", 0))).toBe("c0 a1 b2");
+ expect(moveNode(base(), "zz", 0).nodes).toEqual(base().nodes);
+ const dup = duplicateNode(base(), "a", "a2");
+ expect(zs(dup)).toBe("a0 a21 b2 c3");
+ expect(dup.nodes.find((n) => n.id === "a2")?.name).toBe("a copy");
+ expect(zs(deleteNode(base(), "b"))).toBe("a0 c1");
+ expect(zs(addNodes(base(), [node("x", 9)]))).toBe("a0 b1 c2 x3");
+ expect(zs(addNodes(base(), [node("x", 9)], 0))).toBe("x0 a1 b2 c3");
+ // A split replaces the layer in its place; a merge replaces a pair at the upper one's place.
+ expect(zs(replaceNodes(base(), ["b"], [node("b1", 0), node("b2", 0)]))).toBe("a0 b11 b22 c3");
+ expect(zs(replaceNodes(base(), ["a", "b"], [node("m", 0)]))).toBe("m0 c1");
+ });
+
+ it("keeps revisions to switch between", () => {
+ const n = node("a", 0, { x: 10, y: 10, w: 100, h: 50 });
+ const up = addRevision(n, { src: "/up.png", natural: [400, 200], label: "Upscale", at: 2 });
+ expect(up).toMatchObject({ src: "/up.png", revision: 1, x: 10, y: 10, w: 100, h: 50 });
+ expect(up.revisions.map((r) => r.label)).toEqual(["Original", "Upscale"]);
+ // A square result keeps the width and centre.
+ const sq = addRevision(up, { src: "/sq.png", natural: [300, 300], label: "Angles", at: 3 });
+ expect(sq).toMatchObject({ w: 100, h: 100, x: 10, y: -15, revision: 2 });
+ const back = setRevision(sq, 0);
+ expect(back).toMatchObject({ src: "/api/files/a.png", w: 100, h: 50, y: 10, revision: 0 });
+ expect(setRevision(back, 0)).toBe(back);
+ // A node without revisions keeps its current pixels as the first one.
+ const bare = addRevision({ ...n, revisions: [], revision: 0 }, { src: "/new.png", natural: [100, 50], label: "Relight", at: 9 });
+ expect(bare.revisions.map((r) => [r.label, r.src])).toEqual([
+ ["Before", "/api/files/a.png"],
+ ["Relight", "/new.png"],
+ ]);
+ });
+
+ it("expands and crops the canvas", () => {
+ const d = doc([node("a", 0, { x: 10, y: 20 })]);
+ const cropped = reframeDoc(d, [10, 10, 200, 100]);
+ expect(cropped).toMatchObject({ width: 200, height: 100 });
+ expect(cropped.nodes[0]).toMatchObject({ x: 0, y: 10 });
+ const grown = reframeDoc(d, [-40, -8, 480, 316]);
+ expect(grown.nodes[0]).toMatchObject({ x: 50, y: 28 });
+ expect(() => reframeDoc(d, [500, 0, 100, 100])).toThrow(/no longer overlaps/);
+ expect(reframeDoc(d, [0, 0, 400, 300])).toBe(d);
+ expect(expandPads(d, [-40, -8, 480, 316])).toEqual({ left: 40, top: 8, right: 40, bottom: 8 });
+ expect(expandPads(d, [-3, 0, 410, 300])).toEqual({ left: 8, top: 0, right: 8, bottom: 0 });
+ });
+});
+
+describe("edit history", () => {
+ it("undoes and redoes commits, and a new edit drops the redo stack", () => {
+ const d0 = initialDoc("/s.png", 10, 10, 1);
+ const d1 = { ...d0, width: 11 };
+ const d2 = { ...d0, width: 12 };
+ const d3 = { ...d0, width: 13 };
+ let h: History = { past: [], present: d0, future: [] };
+ h = historyReducer(h, { type: "commit", doc: d1 });
+ h = historyReducer(h, { type: "commit", doc: d2 });
+ expect(historyReducer(h, { type: "commit", doc: d2 })).toBe(h);
+ h = historyReducer(h, { type: "undo" });
+ expect(h.present).toBe(d1);
+ h = historyReducer(h, { type: "undo" });
+ expect(h.present).toBe(d0);
+ expect(historyReducer(h, { type: "undo" })).toBe(h);
+ h = historyReducer(h, { type: "redo" });
+ expect(h.present).toBe(d1);
+ expect(h.future).toEqual([d2]);
+ h = historyReducer(h, { type: "commit", doc: d3 });
+ expect(h).toEqual({ past: [d0, d1], present: d3, future: [] });
+ expect(historyReducer(h, { type: "redo" })).toBe(h);
+ expect(historyReducer(h, { type: "reset", doc: d0 })).toEqual({ past: [], present: d0, future: [] });
+ });
+
+ it("keeps at most 100 steps", () => {
+ let h: History = { past: [], present: initialDoc("/s.png", 1, 1, 0), future: [] };
+ for (let i = 0; i < 130; i++) h = historyReducer(h, { type: "commit", doc: { ...h.present, width: i + 2 } });
+ expect(h.past).toHaveLength(100);
+ expect(h.present.width).toBe(131);
+ });
+});
+
+describe("edit text", () => {
+ it("writes the edit prompt and job title from the changed rows", () => {
+ const rows = [
+ { from: "SUMMER SALE", to: "WINTER SALE" },
+ { from: "Today only", to: "Today only" },
+ { from: "50% off", to: "" },
+ ];
+ expect(textEditPrompt(rows)).toBe("Preserve the original design and change only the requested text. Change “SUMMER SALE” to “WINTER SALE”. Remove “50% off”.");
+ expect(textJobTitle(rows)).toBe("2 text changes");
+ expect(textJobTitle([rows[0]])).toBe("“SUMMER SALE” → “WINTER SALE”");
+ expect(textJobTitle([{ from: "An extremely long headline here", to: "x" }])).toBe("“An extremely long headl…” → “x”");
+ expect(textJobTitle([rows[2]])).toBe("Remove “50% off”");
+ });
+
+ it("reads text blocks with pixel or normalised boxes", () => {
+ const blocks = textBlocksFrom(
+ {
+ textBlocks: [
+ { text: " BIG SALE ", bbox: [100, 50, 200, 100] },
+ { text: "small", bbox: { x: 0.1, y: 0.2, width: 0.3, height: 0.1 } },
+ { text: "" },
+ { text: "no box" },
+ ],
+ },
+ [1000, 500],
+ );
+ expect(blocks).toEqual([
+ { text: "BIG SALE", box: [0.1, 0.1, 0.2, 0.2] },
+ { text: "small", box: [0.1, 0.2, 0.3, 0.1] },
+ { text: "no box", box: null },
+ ]);
+ expect(textBlocksFrom(undefined)).toEqual([]);
+ });
+});
+
+describe("tools and prices", () => {
+ const OPS: Record<string, Operation[]> = {
+ decompose: ["layers"],
+ split: ["layers"],
+ regenerate: ["edit"],
+ "generate-layer": ["t2i", "edit"],
+ "text-detect": ["text-layers"],
+ "text-edit": ["edit"],
+ "bg-remove": ["bg-remove"],
+ regional: ["edit"],
+ upscale: ["upscale-image"],
+ relight: ["edit"],
+ angles: ["edit"],
+ expand: ["edit", "outpaint"],
+ };
+
+ it("offers catalog models of the right engine and operation for every tool", () => {
+ for (const [tool, byEngine] of Object.entries(TOOL_MODELS)) {
+ for (const engine of ["cloud", "open"] as const) {
+ for (const id of byEngine[engine]) {
+ const m = getModel(id);
+ expect(m, `${tool}: ${id}`).toBeDefined();
+ expect(m!.engine, `${tool}: ${id}`).toBe(engine);
+ expect(OPS[tool].some((op) => supportsOp(m!, op)), `${tool}: ${id} supports ${OPS[tool]}`).toBe(true);
+ }
+ }
+ }
+ });
+
+ it("prices a decomposition per layer", () => {
+ expect(decomposeCost("seedream-5-layers", 8)).toBeCloseTo(0.27);
+ expect(decomposeCost("seedream-5-layers", 8, "2K")).toBeCloseTo(0.54);
+ expect(decomposeCost("seedream-5-flash-layers", 8, "2K")).toBeCloseTo(0.216);
+ expect(decomposeCost("qwen-image-layered-cloud", 16)).toBeCloseTo(0.4);
+ expect(decomposeCost("oss-qwen-layered", 8)).toBe(0);
+ expect(decomposeCost("unknown", 8)).toBeNull();
+ expect(clampLayers("oss-qwen-layered", 16)).toBe(10);
+ expect(clampLayers("seedream-5-layers", 1)).toBe(3);
+ expect(clampLayers("oss-qwen-layered-control", 2)).toBe(2);
+ });
+});