Skip to content

armgit / kokon

Public Imported from GitHub
0

Edit photos by describing the change, on your own GPU. A free, open-source alternative to Nano Banana and ChatGPT image editing: photos stay on your computer, no daily limit. One installer for Windows, Mac and Linux sets up the best open model (Qwen-Image 2.1). MCP server for AI agents.

Layers lab: reliable fill, uncut shadows, commands in the toolbar, Export PSD

Gabriele Lozuponeddd425674736
diff --git a/crates/qls-core/src/imaging.rs b/crates/qls-core/src/imaging.rs
index 0aacd1b..f22f7d3 100644
--- a/crates/qls-core/src/imaging.rs
+++ b/crates/qls-core/src/imaging.rs
@@ -287,10 +287,13 @@ pub fn pad_solid(img: &RgbaImage, canvas: Size, offset: (u32, u32), fill: Rgba<u
// ---------------------------------------------------------------------------------------
/// Converts a user-supplied mask image to a single-channel mask. Masks painted on a
-/// transparent layer use alpha; opaque masks use luminance (white = edit).
+/// transparent layer use alpha; opaque masks use luminance (white = edit). A layer of strokes
+/// is empty somewhere; a white-on-black mask blurred on a canvas only comes out partly
+/// transparent at the frame's edge (alpha 77 there), and read by alpha it made the whole frame
+/// the area (the lab's fill, 11-01): luminance unless some pixel is (nearly) fully transparent.
pub fn mask_from_image(img: &LoadedImage) -> GrayImage {
let p = &img.pixels;
- if img.has_transparency {
+ if img.has_transparency && p.pixels().any(|q| q[3] < 8) {
GrayImage::from_fn(p.width(), p.height(), |x, y| Luma([p.get_pixel(x, y)[3]]))
} else {
GrayImage::from_fn(p.width(), p.height(), |x, y| {
@@ -966,6 +969,14 @@ mod tests {
let li = LoadedImage { pixels: bw, format: "png", exif_orientation: 1, icc_profile: None, has_transparency: false, text: vec![] };
let m = mask_from_image(&li);
assert_eq!((m.get_pixel(1, 1)[0], m.get_pixel(8, 1)[0]), (255, 0));
+ // White on black whose edge came out partly transparent (a canvas blur): still luminance.
+ let soft = RgbaImage::from_fn(10, 10, |x, y| {
+ let a = if x == 0 || y == 0 || x == 9 || y == 9 { 77 } else { 255 };
+ if x < 5 { Rgba([255, 255, 255, a]) } else { Rgba([0, 0, 0, a]) }
+ });
+ let li = LoadedImage { pixels: soft, format: "png", exif_orientation: 1, icc_profile: None, has_transparency: true, text: vec![] };
+ let m = mask_from_image(&li);
+ assert_eq!((m.get_pixel(1, 1)[0], m.get_pixel(8, 1)[0], m.get_pixel(8, 0)[0]), (255, 0, 0));
}
#[test]
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index ffdad08..62aa812 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -207,6 +207,23 @@ async fn read_clipboard_image(app: tauri::AppHandle) -> Result<tauri::ipc::Respo
Ok(tauri::ipc::Response::new(png))
}
+/// The Layers lab's .psd (the request's raw body): asks where to save it, writes it there. Returns
+/// the path, or null when the dialog is cancelled.
+#[tauri::command]
+async fn save_psd(app: tauri::AppHandle, request: tauri::ipc::Request<'_>) -> Result<Option<String>, String> {
+ use tauri_plugin_dialog::DialogExt;
+ let tauri::ipc::InvokeBody::Raw(data) = request.body() else { return Err("No file to save.".into()) };
+ let data = data.clone();
+ tauri::async_runtime::spawn_blocking(move || {
+ let Some(path) = app.dialog().file().set_file_name("Layers.psd").add_filter("Photoshop", &["psd"]).blocking_save_file() else { return Ok(None) };
+ let path = path.into_path().map_err(|e| e.to_string())?;
+ std::fs::write(&path, data).map_err(|e| format!("Could not save the file: {e}"))?;
+ Ok(Some(path.to_string_lossy().into_owned()))
+ })
+ .await
+ .map_err(|e| e.to_string())?
+}
+
/// Before installing an update: stop the service so the installer can replace it.
#[tauri::command]
async fn prepare_update(state: tauri::State<'_, Arc<Shared>>) -> Result<(), String> {
@@ -307,7 +324,7 @@ pub fn run() {
});
Ok(())
})
- .invoke_handler(tauri::generate_handler![connect, copy_image, read_clipboard_image, reveal, open_folder, open_link, data_folder, prepare_update])
+ .invoke_handler(tauri::generate_handler![connect, copy_image, read_clipboard_image, save_psd, reveal, open_folder, open_link, data_folder, prepare_update])
.build(tauri::generate_context!())
.expect("error while building Kokon");
app.run(move |_handle, event| {
diff --git a/src/components/LabScene.ts b/src/components/LabScene.ts
index b0cddc6..14ef0a5 100644
--- a/src/components/LabScene.ts
+++ b/src/components/LabScene.ts
@@ -8,12 +8,15 @@ import { api, urls } from "../lib/api";
import { centre, type LayerDoc, type Place, type SceneEdit } from "../lib/layerDoc";
import { dilate, fileSafe, fromLogits, refineEdges } from "../lib/layers";
import { Segmenter } from "../lib/sam";
-import { alignEdit, blur, changed, darkness, IDENTITY, invertAffine, luminance, MARK_COLOURS, objectAt, scaleAffine, splitShadows, toneCurve, type Affine, type MarkColour } from "../lib/sceneEdit";
+import { alignEdit, attached, blur, changed, darkness, IDENTITY, invertAffine, luminance, MARK_COLOURS, objectAt, scaleAffine, splitShadows, toneCurve, type Affine, type MarkColour } from "../lib/sceneEdit";
import type { InputRecord } from "../lib/types";
import { loadImage, pixels, sourceInfo, toPng } from "./LayerDoc";
/** Long side of the analysis (shadows are soft: no need for every pixel). */
const SIDE = 1536;
+/** Automatic shadows: darkening above this (0-1, past darkness()'s floor) carries a shadow on from
+ * near the objects; where it stops the layer is ~10/255, no visible edge. */
+const SHADOW_LOW = 0.04;
/** What a result made: images to add to the document. Keys: object ids (shadows, looks) and
* mark colours (new objects, and their shadows). */
@@ -133,6 +136,51 @@ async function pair(beforeSrc: string, afterSrc: string, W: number, H: number, a
return { w, h, before, after, lb: blur(luminance(before, n), w, h, r), la: blur(luminance(after, n), w, h, r), valid, t, img: ai };
}
+/** Where the edit put back leaves no pixels (it came back zoomed in: a strip at the frame's edges,
+ * ~8 px of 1328 on 11-01), the value along the column, then the row, so a shadow reaches the edge
+ * instead of stopping short of it. Taken `m` px inside: the edit's own outermost rows are off (dark
+ * streaks along the whole edge, 11-01 Halloween), so they are filled too. Elsewhere (no comparison
+ * possible) 0. */
+function fillBorder(d: Float32Array, valid: Uint8Array, w: number, h: number) {
+ const m = Math.max(3, Math.round(Math.max(w, h) * 0.006));
+ const done = Uint8Array.from(valid);
+ for (let x = 0; x < w; x++) {
+ let top = -1;
+ let bottom = -1;
+ for (let y = 0; y < h; y++) {
+ if (!valid[y * w + x]) continue;
+ if (top < 0) top = y;
+ bottom = y;
+ }
+ if (top < 0 || bottom - top < 4 * m) continue;
+ top += m;
+ bottom -= m;
+ for (let y = 0; y < h; y++) {
+ if (y >= top && y <= bottom) continue;
+ d[y * w + x] = d[(y < top ? top : bottom) * w + x];
+ done[y * w + x] = 1;
+ }
+ }
+ for (let y = 0; y < h; y++) {
+ let left = -1;
+ let right = -1;
+ for (let x = 0; x < w; x++) {
+ if (!done[y * w + x]) continue;
+ if (left < 0) left = x;
+ right = x;
+ }
+ if (left >= 0 && right - left >= 4 * m) {
+ left += m;
+ right -= m;
+ }
+ for (let x = 0; x < w; x++) {
+ const i = y * w + x;
+ if (done[i] && x >= left && x <= right) continue;
+ d[i] = left < 0 ? 0 : x < left ? d[y * w + left] : x > right ? d[y * w + right] : 0;
+ }
+ }
+}
+
/** The darkening the edit added (tone change divided out), 0-1 per pixel; tone measured on the
* pixels farther than `reach` from every object and outside `busy` (the marks). */
function addedDarkness(p: Pair, objects: Uint8Array[], reach: number, busy?: Uint8Array): Float32Array {
@@ -149,7 +197,7 @@ function addedDarkness(p: Pair, objects: Uint8Array[], reach: number, busy?: Uin
for (let i = 0; i < far.length; i++) far[i] = p.valid[i] && !grown[((((i / p.w) | 0) / q) | 0) * qw + (((i % p.w) / q) | 0)] ? 1 : 0;
const lut = toneCurve(p.lb, p.la, far);
const d = darkness(p.lb, p.la, lut);
- for (let i = 0; i < d.length; i++) if (!p.valid[i]) d[i] = 0;
+ fillBorder(d, p.valid, p.w, p.h);
// A touch of smoothing: decode speckle left above the floor.
return blur(d, p.w, p.h, 1);
}
@@ -223,7 +271,7 @@ export async function readScene(doc: LayerDoc, e: SceneEdit, result: string): Pr
}
// Shadows: only of the objects asked for. A new object comes alone (its shadow is made
- // afterwards with Add shadow, the ring), whatever shadow Qwen drew for it is left out.
+ // afterwards with Automatic shadows), whatever shadow Qwen drew for it is left out.
const targets = e.shadows.filter((id) => feet.has(id)).map((id) => ({ of: id, m: feet.get(id)! }));
// One layer for all of them (Automatic shadows): the darkening within reach of the objects, taken
// out only where an object is (it covers its own pixels). No band around the outlines to fill
@@ -238,9 +286,14 @@ export async function readScene(doc: LayerDoc, e: SceneEdit, result: string): Pr
const small = new Uint8Array(qw * qh);
for (let i = 0; i < near.length; i++) if (near[i]) small[(((i / w) | 0) / q | 0) * qw + (((i % w) / q) | 0)] = 255;
const within = dilate(small, qw, qh, reach / q);
+ // Within reach is only where a shadow starts: it is kept as far as it goes on from there (the
+ // box-shaped reach cut the girl's long shadow with straight edges, 11-01 Halloween).
+ const seed = new Uint8Array(w * h);
+ for (let i = 0; i < seed.length; i++) seed[i] = within[(((i / w) | 0) / q | 0) * qw + (((i % w) / q) | 0)];
+ const keep = attached(dark, seed, w, h, SHADOW_LOW, near);
const alpha = new Uint8Array(w * h);
for (let i = 0; i < alpha.length; i++) {
- if (near[i] || !within[(((i / w) | 0) / q | 0) * qw + (((i % w) / q) | 0)]) continue;
+ if (near[i] || !keep[i]) continue;
alpha[i] = Math.round(Math.min(1, dark[i]) * 255);
}
if (total(alpha) < 255 * w * h * 0.0003) out.notes.push("No shadow was found.");
diff --git a/src/components/LabStage.tsx b/src/components/LabStage.tsx
index 0ab99c8..9ba790f 100644
--- a/src/components/LabStage.tsx
+++ b/src/components/LabStage.tsx
@@ -116,7 +116,8 @@ export function JobPct({ job, item }: { job: JobView; item: SessionItem }) {
function JobPill({ j }: { j: LabJob }) {
const layer = j.doc.layers.find((l) => l.id === j.layer);
- const what = j.layer === "shadows" ? "the shadows" : j.layer === "scene" ? "the scene" : layer ? (layer.base ? "the background" : layer.name) : "";
+ const auto = j.layer === "scene" && j.doc.scenes?.some((e) => e.one && e.source === j.item.request?.images?.[0]);
+ const what = j.layer === "shadows" || auto ? "the shadows" : j.layer === "scene" ? "the scene" : layer ? (layer.base ? "the background" : layer.name) : "";
if (j.job.status === "failed")
return (
<span className="lab-progress failed" title={j.job.error?.detail ?? undefined}>
diff --git a/src/components/LayerDoc.tsx b/src/components/LayerDoc.tsx
index 63a6e4c..4bc3265 100644
--- a/src/components/LayerDoc.tsx
+++ b/src/components/LayerDoc.tsx
@@ -7,18 +7,20 @@
// together), Group keeps them together. Whole-image edits (LabScene.ts) give Qwen the flattened
// layers: "Shadows" reads back each object's shadow as its own layer (it moves with the object),
// "New objects" adds what the user drew with the coloured pencil as new layers, "Edit together"
-// gives the selected objects the edit's look. "Save as image" flattens the layers into a new version.
+// gives the selected objects the edit's look. "Save as image" flattens the layers into a new version; "Export PSD" saves them for Photoshop (lib/psd.ts).
-import { Brush, Eraser, Eye, EyeOff, GripVertical, Group, Hand, History, ImageDown, Link2, Loader2, Lock, MousePointer2, Paintbrush, Pencil, Redo2, RotateCcw, Scissors, Sparkles, Sun, Trash2, Undo2, Ungroup, WandSparkles, X } from "lucide-react";
+import { Brush, Eraser, Eye, EyeOff, FileDown, GripVertical, Group, Hand, History, ImageDown, Link2, Loader2, Lock, MousePointer2, Paintbrush, Pencil, Redo2, RotateCcw, Scissors, Sparkles, Sun, Trash2, Undo2, Ungroup, WandSparkles, X } from "lucide-react";
import { useEffect, useMemo, useRef, useState } from "react";
import { api, urls } from "../lib/api";
-import { dilate, erode, fileSafe, grow } from "../lib/layers";
-import { centre, CROP_MAX_RATIO, cropFor, docId, waitingMarks, shadowAlpha, mapBox, reorder, unmapPoint, withGroup, xfOf, type Box, type DocLayer, type Effect, type Feed, type LayerDoc, type SceneEdit, type Sticker, type Xf } from "../lib/layerDoc";
+import { dilate, erode, fileSafe } from "../lib/layers";
+import { centre, copiedShare, CROP_MAX_RATIO, cropFor, docId, waitingMarks, shadowAlpha, mapBox, reorder, unmapPoint, withGroup, xfOf, type Box, type DocLayer, type Effect, type Feed, type LayerDoc, type SceneEdit, type Sticker, type Xf } from "../lib/layerDoc";
import { buildRequest, refFromInput } from "../lib/logic";
+import { native } from "../lib/native";
+import { writePsd } from "../lib/psd";
import { MARK_COLOURS, marksPrompt, type MarkColour } from "../lib/sceneEdit";
import { placesOf } from "./LabScene";
import { shortcut } from "../lib/platform";
-import { addResult, addVersions, cancelJob, editSource, focusPrompt, getState, itemSource, setState, submitRequest, toastError, toastInfo, updateComposer, useStore, type SessionItem } from "../lib/store";
+import { addResult, addVersions, cancelJob, editSource, focusPrompt, getState, itemOrigin, itemSource, setState, submitRequest, toastError, toastInfo, updateComposer, useStore, type SessionItem } from "../lib/store";
import type { InputRecord, JobView } from "../lib/types";
import { useJobPercent } from "./jobMotion";
import { JobPct, LabJobs, useLab, useLabJobs } from "./LabStage";
@@ -53,6 +55,15 @@ function StickerRing({ job, item }: { job: JobView; item: SessionItem }) {
);
}
+/** Where Automatic shadows' edit is: "Making the shadows · 38%". */
+function ShadowPhase({ job, item }: { job: JobView; item: SessionItem }) {
+ const { pct } = useJobPercent(job, item.request);
+ if (job.status === "queued") return <>Waiting for the edits before it</>;
+ if (job.status === "starting_backend") return <>Starting the engine</>;
+ if (job.status === "done") return <>Reading the shadows</>;
+ return <>Making the shadows · {Math.floor(pct)}%</>;
+}
+
/** What the canvas needs of a layer's version: its alpha (for clicks), where its content is, a thumbnail. */
export interface SourceInfo {
img: HTMLImageElement;
@@ -302,14 +313,6 @@ function dab(ctx: CanvasRenderingContext2D, x: number, y: number, r: number, mod
ctx.globalCompositeOperation = "source-over";
}
-/** How far a shadow edit's painted area reaches below an object, in % of the image's long side
- * (sideways half of it, upwards little: contact shadows fall under things). Kept while the app runs. */
-let lastReach = 4;
-
-/** How far the shadow area goes inside the objects, in frame px: the model redraws their bottom edge
- * with the shadow, so the contact joins (as hand-painted areas did, 10-31). Kept while the app runs. */
-let lastInset = 8;
-
/** How far a whole-image shadow edit's result is read around each object (% of the long side). */
const SCENE_REACH = 12;
/** Automatic shadows: the whole scene edited, nothing painted. Generic on purpose (11-01 bench, Halloween and
@@ -393,16 +396,6 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
// The layer being renamed (double-click), so a click on a row leaves the arrows to nudging.
const [renaming, setRenaming] = useState<string | null>(null);
const [versionsOpen, setVersionsOpen] = useState(false);
- const [inset, setInsetState] = useState(lastInset);
- const setInset = (v: number) => {
- lastInset = v;
- setInsetState(v);
- };
- const [reach, setReachState] = useState(lastReach);
- const setReach = (v: number) => {
- lastReach = v;
- setReachState(v);
- };
useEffect(() => setVersionsOpen(false), [selected]);
const undoStack = useRef<LayerDoc[]>([]);
const redoStack = useRef<LayerDoc[]>([]);
@@ -469,7 +462,11 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
const c = document.createElement("canvas");
c.width = img.naturalWidth;
c.height = img.naturalHeight;
- c.getContext("2d")!.drawImage(img, 0, 0);
+ // On black: a mask with any transparency is read by its alpha (see prepareFill).
+ const cx = c.getContext("2d")!;
+ cx.fillStyle = "#000";
+ cx.fillRect(0, 0, c.width, c.height);
+ cx.drawImage(img, 0, 0);
areaCanvas.current = c;
})
.catch(() => {});
@@ -747,7 +744,7 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
if (j.status === "failed") return "failed";
return j.status === "queued" ? "waiting" : "making";
};
- // A fill that kept grey patches (Qwen copies the flat grey now and then, 11-01) is sent once more.
+ // A fill that copied the grey (or the guide's marking) instead of a background is sent once more.
const checkedFill = useRef(new Set<string>());
useEffect(() => {
const s = live.stickers?.find((x) => x.kind && x.job && !x.retried && !x.skipped);
@@ -762,16 +759,10 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
const h = Math.max(1, Math.round((w * img.naturalHeight) / img.naturalWidth));
const a = pixels(img, w, h);
const b = pixels(m, w, h);
- let inside = 0;
- let grey = 0;
- for (let i = 0; i < w * h; i++) {
- if (b[i * 4] < 128) continue;
- inside++;
- if (Math.abs(a[i * 4] - 128) <= 12 && Math.abs(a[i * 4 + 1] - 128) <= 12 && Math.abs(a[i * 4 + 2] - 128) <= 12) grey++;
- }
const rec = gen.request?.images?.[0] && session.find((i) => itemSource(i) === gen.request!.images[0])?.input;
- if (!inside || grey / inside < 0.02 || !rec) return setSticker(s.layer, { checked: true });
- toastInfo("The background fill kept grey patches: trying once more.");
+ // 20 %: the two failed Halloween fills scored 72 %, a clean one 4.6 % (grey pavement).
+ if (copiedShare(a, b, w * h) < 0.2 || !rec) return setSticker(s.layer, { checked: true });
+ toastInfo("The background fill copied the grey or the marking instead of a background: trying once more.");
const job = await submitRequest(fillRequest(rec, mask));
setSticker(s.layer, job ? { job, retried: true } : { checked: true });
})().catch((e) => {
@@ -781,6 +772,14 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [jobs, session, live.stickers]);
+ // Automatic shadows being made (sent, generated, or read back): the layers stay where they are
+ // meanwhile (the shadows are made for this layout), and the stage says how far it is.
+ const [shadowSending, setShadowSending] = useState(false);
+ const autoScenes = (live.scenes ?? []).filter((e) => e.one);
+ const shadowJob = labJobs.find((j) => j.job.status !== "failed" && autoScenes.some((e) => e.source === j.item.request?.images?.[0]));
+ const shadowReading = autoScenes.some((e) => session.some((i) => i.gen && itemOrigin(i).from[0] === e.source && !live.known.includes(`gen:${i.gen.id}`)));
+ const shadowing = shadowSending || !!shadowJob || shadowReading;
+
const skipSticker = (s: Sticker) => {
if (s.job) void cancelJob(s.job);
setSticker(s.layer, { skipped: true });
@@ -807,6 +806,39 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [stickersLeft, stickerRows.length]);
+ // Shadow strength: the Automatic shadows layer redrawn from the model's version, its alpha as k of
+ // them stacked (1 - (1 - a)^k: darker stays a soft shadow, lighter fades evenly), as its next version.
+ const autoShadows = live.layers.find((l) => l.auto && l.effect);
+ const [strengthShown, setStrengthShown] = useState(100);
+ useEffect(() => setStrengthShown(Math.round((autoShadows?.strength ?? 1) * 100)), [autoShadows?.id, autoShadows?.strength]);
+ useEffect(() => {
+ const l = autoShadows;
+ const k = strengthShown / 100;
+ if (!l || Math.abs(k - (l.strength ?? 1)) < 0.001) return;
+ const id = window.setTimeout(async () => {
+ try {
+ const made = l.made ?? l.source;
+ const img = await loadImage(urls.source(made));
+ const c = document.createElement("canvas");
+ c.width = W;
+ c.height = H;
+ const ctx = c.getContext("2d")!;
+ ctx.drawImage(img, 0, 0, W, H);
+ const d = ctx.getImageData(0, 0, W, H);
+ for (let i = 3; i < d.data.length; i += 4) d.data[i] = Math.round(255 * (1 - Math.pow(1 - d.data[i] / 255, k)));
+ ctx.putImageData(d, 0, 0);
+ const rec = await api.upload(await toPng(c), "Shadows.png");
+ const src = `input:${rec.id}`;
+ const now = liveRef.current;
+ commitRef.current({ ...now, layers: now.layers.map((x) => (x.id === l.id ? { ...x, source: src, history: [...x.history, src], made, strength: k } : x)) });
+ } catch (e) {
+ toastError(e);
+ }
+ }, 400);
+ return () => window.clearTimeout(id);
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [strengthShown]);
+
const commit = (next: LayerDoc) => {
undoStack.current = [...undoStack.current, doc].slice(-50);
redoStack.current = [];
@@ -969,6 +1001,7 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
});
const onDown = (e: React.PointerEvent) => {
+ if (shadowing) return;
if (areaOn && (e.button === 0 || e.button === 2)) {
e.stopPropagation();
try {
@@ -1138,6 +1171,12 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
out.width = mw;
out.height = mh;
const o = out.getContext("2d")!;
+ // Opaque, black under it: blurred on transparency, the frame's edge pixels came out partly
+ // transparent, and the service reads a mask with any transparency by its alpha (white strokes
+ // on a layer) -> the whole frame was the area, the guide all red, and Qwen copied the grey or
+ // the red (11-01, Halloween). The same seed fills cleanly with the mask opaque.
+ o.fillStyle = "#000";
+ o.fillRect(0, 0, mw, mh);
o.filter = "blur(4px)"; // the soft edge of a painted area (InpaintEditor FEATHER)
o.drawImage(c, 0, 0);
const mask = await api.upload(await toPng(out), "mask.png");
@@ -1283,83 +1322,15 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
}
};
- /** Sets up a shadow edit in the prompt (the way that works, 10-31): the flattened layers
- * (without the shadow layer it replaces) as the image, a ring around the object (or every object)
- * as the painted area, reaching `inset` px into the objects (their bottom edge is redrawn with the
- * contact shadow); deeper the objects are left out. */
- const addShadows = async (of: DocLayer | null) => {
- setBusy(true);
+
+ const autoShadowsNow = async () => {
+ const objects = live.layers.filter((l) => !l.base && !l.effect && !l.hidden).map((l) => l.id);
+ if (!objects.length) return toastInfo("No visible objects to make shadows for.");
+ setShadowSending(true);
try {
- const objects = of ? [of] : live.layers.filter((l) => !l.base && !l.effect && !l.hidden);
- if (!objects.length) throw new Error("No visible object to make shadows for.");
- const existing = live.layers.find((l) => l.effect && (of ? l.follows === of.id : !l.follows));
- // On grey: holes the background still has would make the image transparent, and an edit of a
- // transparent image keeps its transparency.
- const layersOnly = await render(W, H, existing ? new Set([existing.id]) : undefined);
- const flat = document.createElement("canvas");
- flat.width = W;
- flat.height = H;
- const fctx = flat.getContext("2d")!;
- fctx.fillStyle = "#808080";
- fctx.fillRect(0, 0, W, H);
- fctx.drawImage(layersOnly, 0, 0);
- const holes = await hasHoles();
- const k = Math.min(1, 2048 / Math.max(W, H));
- const mw = Math.max(1, Math.round(W * k));
- const mh = Math.max(1, Math.round(H * k));
- const alphaOf = async (ls: DocLayer[]) => {
- const px = (await render(mw, mh, undefined, ls)).getContext("2d")!.getImageData(0, 0, mw, mh).data;
- const a = new Uint8Array(mw * mh);
- for (let i = 0; i < a.length; i++) a[i] = px[i * 4 + 3] > 128 ? 255 : 0;
- return a;
- };
- const obj = await alphaOf(objects);
- // Every visible object stays out of the painted area, not only the one getting its shadow.
- const all = of ? await alphaOf(live.layers.filter((l) => !l.base && !l.effect && !l.hidden)) : obj;
- // The area: mostly below the object (where contact shadows fall), half as far sideways, a little
- // above; `reach` % of the side (the Shadow reach slider).
- const r = Math.max(4, Math.round((Math.max(mw, mh) * reach) / 100));
- const grown = grow(obj, mw, mh, { down: r, left: r * 0.5, right: r * 0.5, up: r * 0.15 });
- // The objects' own outline `inset` px deep is in the area too (left out: everything deeper).
- const kept = inset > 0 ? erode(all, mw, mh, Math.max(1, Math.round(inset * k))) : all;
- const mc = document.createElement("canvas");
- mc.width = mw;
- mc.height = mh;
- const mctx = mc.getContext("2d")!;
- const m = mctx.createImageData(mw, mh);
- for (let i = 0; i < obj.length; i++) {
- const v = grown[i] && !kept[i] ? 255 : 0;
- m.data[i * 4] = m.data[i * 4 + 1] = m.data[i * 4 + 2] = v;
- m.data[i * 4 + 3] = 255;
- }
- mctx.putImageData(m, 0, 0);
- const soft = document.createElement("canvas");
- soft.width = mw;
- soft.height = mh;
- const sctx = soft.getContext("2d")!;
- sctx.filter = "blur(3px)";
- sctx.drawImage(mc, 0, 0);
- const mask = await api.upload(await toPng(soft), "mask.png");
- const rec = await api.upload(await toPng(flat), "Layers for shadows.png");
- addVersions([rec], 0);
- const target = `input:${rec.id}`;
- if (!editSource(target, false)) throw new Error("The flattened layers could not be opened.");
- const refKey = getState().composer.refs[0]?.key;
- const what = of ? `the ${of.name.toLowerCase()}` : "the objects";
- const prompt = `Add a natural contact shadow under ${what} and the soft shadow ${of ? "it casts" : "they cast"} on the surface, matching the light of the scene. Keep ${what} and everything else unchanged.`;
- if (refKey) updateComposer({ mask: { inputId: mask.id, refKey }, outpaint: null, mode: "auto", inpaintAction: "edit", prompt });
- const effect: Effect = { source: target, mask: mask.id, of: of?.id ?? null, at: of ? { dx: of.dx, dy: of.dy, s: of.s } : undefined, layer: existing?.id };
- commit({ ...live, pending: [...(live.pending ?? []), effect] });
- focusPrompt(true);
- toastInfo(
- holes
- ? "Ready in the prompt, but the background still has holes (grey here): Fill the holes first for real shadows."
- : "The shadow area is painted on the flattened layers and the prompt is ready: change it if you like, then Generate. The result becomes a shadow layer here.",
- );
- } catch (e) {
- toastError(e);
+ await sendScene({ shadows: objects, look: [], prompt: AUTO_SHADOWS_PROMPT, marks: false, send: true, one: true, hint: "Making the shadows: the model adds them to the whole scene where the light makes them, as one Shadows layer. Moved something? Make them again: the layer is replaced." });
} finally {
- setBusy(false);
+ setShadowSending(false);
}
};
@@ -1369,7 +1340,7 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
look: [],
marks: true,
prompt: marksPrompt(wantsNow()),
- hint: "The whole scene with your marks is in the prompt (Prompt+ sees the marks too), then Generate. Each new object comes back as its own layer at the marks' place and size (no shadow: Add shadow makes it).",
+ hint: "The whole scene with your marks is in the prompt (Prompt+ sees the marks too), then Generate. Each new object comes back as its own layer at the marks' place and size (no shadow: Automatic shadows makes them).",
});
/** Several objects edited as one scene: the prompt is the user's; they take the edit's look. */
@@ -1399,9 +1370,36 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
}
};
+ /** The layers as a Photoshop file: each one where and as big as it is here (hidden ones hidden). */
+ const exportPsd = async () => {
+ setBusy(true);
+ try {
+ const layers = [];
+ for (const l of live.layers) {
+ const c = await render(W, H, undefined, [{ ...l, hidden: false }]);
+ layers.push({ name: l.name, hidden: l.hidden, rgba: c.getContext("2d")!.getImageData(0, 0, W, H).data });
+ }
+ const flat = document.createElement("canvas");
+ flat.width = W;
+ flat.height = H;
+ const fx = flat.getContext("2d")!;
+ fx.fillStyle = "#fff";
+ fx.fillRect(0, 0, W, H);
+ fx.drawImage(await render(W, H), 0, 0);
+ const path = await native.savePsd(writePsd(W, H, layers, fx.getImageData(0, 0, W, H).data));
+ if (path) toastInfo(`Saved ${path.split(/[\\/]/).pop()}: each layer is its own layer in Photoshop.`);
+ } catch (e) {
+ toastError(e);
+ } finally {
+ setBusy(false);
+ }
+ };
+
useEffect(() => {
const onKeyDown = (e: KeyboardEvent) => {
if (isTyping(e.target)) return;
+ // Shadows being made for this layout: nothing moves or changes until they are in.
+ if (shadowing && e.key !== "Escape") return;
const k = e.key.toLowerCase();
if (e.code === "Space" && !e.repeat) {
e.preventDefault();
@@ -1502,6 +1500,17 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
</button>
</div>
)}
+ {shadowing && (
+ <div className="doc-lock" onPointerDown={(e) => e.stopPropagation()} onWheel={(e) => e.stopPropagation()}>
+ <div className="doc-lock-card">
+ <Loader2 size={18} className="spin" />
+ <span>
+ {shadowJob ? <ShadowPhase job={shadowJob.job} item={shadowJob.item} /> : shadowSending ? "Sending the scene" : "Reading the shadows"}
+ <small>The layers stay where they are until the shadows are in</small>
+ </span>
+ </div>
+ </div>
+ )}
{maskTint && !stickering && <span className="doc-mask-chip">Painted area: Generate redoes what is pink · brush it here to change it (A)</span>}
{!maskTint && waitingMarks(live).length > 0 && <span className="doc-mask-chip">Your marks: Generate adds the new objects there</span>}
<ZoomPan contentSize={contentSize} resetKey="layer-doc" enableToggleZoom={false} enablePan={panning} className="inpaint-zoom-viewport" minimapSrc={base ? urls.source(base.source) : undefined}>
@@ -1621,7 +1630,7 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
</ZoomPan>
</div>
- <aside className="layers-panel">
+ <aside className={`layers-panel ${shadowing ? "locked" : ""}`}>
<div className="layers-title">Layers</div>
{live.layers.map((l, i) => {
const info = infos[l.source];
@@ -1691,7 +1700,7 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
</div>
<p className="layers-help">
{sketch.marks.length
- ? "Say what goes at each colour. The whole scene goes to Qwen with your marks; each object comes back as its own layer at that place and size, without a shadow (Add shadow makes it). Prompt+ sees the marks too."
+ ? "Say what goes at each colour. The whole scene goes to Qwen with your marks; each object comes back as its own layer at that place and size, without a shadow (Automatic shadows makes them). Prompt+ sees the marks too."
: "Draw where a new object goes (an outline or a scribble), one colour per object."}
</p>
{usedColours.map((c) => (
@@ -1774,11 +1783,6 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
<Sparkles size={14} /> Clean edges
</button>
)}
- {!sel.base && !sel.effect && (
- <button className="btn" onClick={() => addShadows(sel)} disabled={busy} title="A brushed edit of the flattened layers around this object; the result becomes its shadow layer">
- <Sun size={14} /> {live.layers.some((l) => l.effect && l.follows === sel.id) ? "Shadow again" : "Add shadow"}
- </button>
- )}
{sel.history.length > 1 && (
<button className={`btn ${versionsOpen ? "on" : ""}`} onClick={() => setVersionsOpen((o) => !o)} aria-expanded={versionsOpen} title="Show an earlier version of this layer">
<History size={14} /> Versions · {sel.history.length}
@@ -1811,42 +1815,10 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
<p className="layers-help doc-none">Select a layer (here or on the image) to edit it with the prompt below. Shift+click selects more; the pencil (P) draws new objects.</p>
)}
- <div className="doc-footer">
- <label className="layers-opt" title="How far the area painted for shadows reaches below the objects (half as far sideways). Check the pink area before Generate.">
- Shadow reach
- <input type="range" min={1} max={12} value={reach} onChange={(e) => setReach(Number(e.target.value))} />
- <span>{reach}%</span>
- </label>
- <label className="layers-opt" title="How many pixels the shadow area goes inside the objects, so the model redraws their bottom edge with the contact shadow (0: the area stops at the objects)">
- Into objects
- <input type="range" min={0} max={30} value={inset} onChange={(e) => setInset(Number(e.target.value))} />
- <span>{inset} px</span>
- </label>
- <div className="doc-grid">
- <button
- className="btn doc-wide"
- onClick={() => {
- const objects = live.layers.filter((l) => !l.base && !l.effect && !l.hidden).map((l) => l.id);
- if (!objects.length) return toastInfo("No visible objects to make shadows for.");
- void sendScene({ shadows: objects, look: [], prompt: AUTO_SHADOWS_PROMPT, marks: false, send: true, one: true, hint: "Making the shadows: the model adds them to the whole scene where the light makes them, as one Shadows layer. Moved something? Make them again: the layer is replaced." });
- }}
- disabled={busy}
- title="Nothing to paint: the whole scene goes to the model, which adds the shadows where the light makes them, as one Shadows layer (made again after moving things: it is replaced)"
- >
- <Sun size={14} /> Automatic shadows
- </button>
- <button className="btn doc-wide" onClick={() => addShadows(null)} disabled={busy} title="One brushed edit of the flattened layers around every visible object; the result becomes a Shadows layer above the background">
- <Sun size={14} /> Shadows for all objects
- </button>
- </div>
- <button className="btn primary" onClick={flatten} disabled={busy}>
- <ImageDown size={14} /> Save as image
- </button>
- </div>
</aside>
</div>
- <div className="editor-tools">
+ <div className={`editor-tools ${shadowing ? "locked" : ""}`}>
<div className="group">
<IconButton title="Select and move (V)" active={tool === "move" && !spacePressed} onClick={() => setTool("move")}>
<MousePointer2 size={17} />
@@ -1892,6 +1864,30 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
<Redo2 size={17} />
</IconButton>
</div>
+ {/* The whole document's commands (the panel on the right is for the selected layer). */}
+ <div className="group">
+ <button
+ className="pill doc-cmd"
+ onClick={autoShadowsNow}
+ disabled={busy || shadowing}
+ title="Nothing to paint: the whole scene goes to the model, which adds the shadows where the light makes them, as one Shadows layer (made again after moving things: it is replaced)"
+ >
+ <Sun size={15} /> Automatic shadows
+ </button>
+ {autoShadows && (
+ <label className="slider" title="How dark the Shadows layer is, without making it again (100 %: as the model made it)">
+ Strength
+ <input type="range" min={50} max={200} step={10} value={strengthShown} onChange={(e) => setStrengthShown(Number(e.target.value))} />
+ <span className="doc-cmd-val">{strengthShown}%</span>
+ </label>
+ )}
+ <button className="pill doc-cmd" onClick={exportPsd} disabled={busy || shadowing} title="Save the layers as a Photoshop file (.psd), each one a layer, to open in Photoshop, Photopea, GIMP or Krita">
+ <FileDown size={15} /> Export PSD
+ </button>
+ <button className="pill doc-cmd primary" onClick={flatten} disabled={busy || shadowing}>
+ <ImageDown size={15} /> Save as image
+ </button>
+ </div>
</div>
</div>
);
diff --git a/src/components/LayersEditor.tsx b/src/components/LayersEditor.tsx
index 7d51816..6b89568 100644
--- a/src/components/LayersEditor.tsx
+++ b/src/components/LayersEditor.tsx
@@ -97,7 +97,8 @@ function LayersLab({ projectId }: { projectId: string }) {
// Automatic shadows: the one layer, just above the background, replaced by the next result.
if (s.of === "all") {
const have = layers.find((l) => l.auto);
- if (have) nextVersion(have.id, s.src);
+ // A new result starts at its own strength again.
+ if (have) layers = layers.map((l) => (l.id === have.id ? { ...l, source: s.src, history: [...l.history, s.src], made: undefined, strength: undefined } : l));
else {
const layer: DocLayer = { id: `fx-${result}-all`, name: "Shadows", source: s.src, history: [s.src], hidden: false, base: false, effect: true, auto: true, dx: 0, dy: 0, s: 1 };
const at = layers.findIndex((l) => l.base);
@@ -129,7 +130,12 @@ function LayersLab({ projectId }: { projectId: string }) {
update({ ...d, layers, known: [...d.known, ...seen, ...srcs], scenes: (d.scenes ?? []).map((x) => (x.source === e.source ? { ...x, made: madeBy } : x)) });
if (r.notes.length) toastInfo(r.notes.join(" "));
},
- (err) => toastError(err),
+ (err) => {
+ // Seen anyway: Automatic shadows holds the layers until its result is read.
+ const d = latest.current!;
+ update({ ...d, known: [...d.known, ...made.map((i) => `gen:${i.gen!.id}`)] });
+ toastError(err);
+ },
);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
diff --git a/src/lib/layerDoc.test.ts b/src/lib/layerDoc.test.ts
index cf1e7da..f4cdad8 100644
--- a/src/lib/layerDoc.test.ts
+++ b/src/lib/layerDoc.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { cropFor, docFor, keepLabsIn, labSources, loadLab, saveLab, waitingMarks, jobLayer, labItems, leftovers, putDoc, type LabStore, shadowAlpha, mapPoint, placed, reconcile, reorder, toLayer, xfOf, type DocLayer, type LayerDoc } from "./layerDoc";
+import { copiedShare, cropFor, docFor, keepLabsIn, labSources, loadLab, saveLab, waitingMarks, jobLayer, labItems, leftovers, putDoc, type LabStore, shadowAlpha, mapPoint, placed, reconcile, reorder, toLayer, xfOf, type DocLayer, type LayerDoc } from "./layerDoc";
const layer = (id: string, source: string, base = false): DocLayer => ({ id, name: id, source, history: [source], hidden: false, base, dx: 0, dy: 0, s: 1 });
const doc = (layers: DocLayer[]): LayerDoc => ({ width: 100, height: 100, layers, known: [] });
@@ -221,3 +221,12 @@ describe("where the lab is kept", () => {
}
});
});
+
+describe("copiedShare", () => {
+ const img = (px: number[][]) => px.flatMap((p) => [...p, 255]);
+ const mask = img([[255, 255, 255], [255, 255, 255], [255, 255, 255], [0, 0, 0]]);
+ it("finds the grey and the guide's red copied into the holes, not a real background", () => {
+ expect(copiedShare(img([[128, 130, 126], [184, 78, 78], [30, 60, 160], [128, 128, 128]]), mask, 4)).toBeCloseTo(2 / 3);
+ expect(copiedShare(img([[30, 60, 160], [40, 70, 170], [90, 50, 120], [128, 128, 128]]), mask, 4)).toBe(0);
+ });
+});
diff --git a/src/lib/layerDoc.ts b/src/lib/layerDoc.ts
index a705dbf..0315493 100644
--- a/src/lib/layerDoc.ts
+++ b/src/lib/layerDoc.ts
@@ -85,6 +85,9 @@ export interface DocLayer {
effect?: boolean;
/** The one layer of Automatic shadows: the next one replaces it (its next version). */
auto?: boolean;
+ /** Automatic shadows as the model made them (`strength` is applied to this), and the strength. */
+ made?: string;
+ strength?: number;
/** An object's shadow moves and scales with the object (`follows`), from where the object was
* when the shadow was made (`at`). */
follows?: string;
@@ -460,3 +463,32 @@ export function jobLayer(doc: LayerDoc, image: string | undefined, mask: string
if (!p) return null;
return p.layer ?? p.of ?? "shadows";
}
+
+/** What a failed fill copies into the holes instead of a background: the flat grey they were sent
+ * as, or the guide's marking (the highlight colour half over that grey, or the colour itself;
+ * pipeline.rs highlight, imaging.rs HIGHLIGHTS). Seen 11-01: one try kept the grey, the next the red. */
+const COPIED = [
+ [128, 128, 128],
+ ...[
+ [255, 0, 0],
+ [0, 230, 0],
+ [0, 80, 255],
+ [255, 0, 255],
+ ].flatMap((c) => [c, c.map((v) => (v + 128) >> 1)]),
+];
+
+/** The share of the painted area (`mask` > 127, RGBA both, `n` pixels) whose colour is one a failed
+ * fill copies (each channel within 28 levels of it). */
+export function copiedShare(rgba: ArrayLike<number>, mask: ArrayLike<number>, n: number): number {
+ let inside = 0;
+ let bad = 0;
+ for (let i = 0; i < n; i++) {
+ if (mask[i * 4] < 128) continue;
+ inside++;
+ const r = rgba[i * 4];
+ const g = rgba[i * 4 + 1];
+ const b = rgba[i * 4 + 2];
+ if (COPIED.some((c) => Math.abs(r - c[0]) <= 28 && Math.abs(g - c[1]) <= 28 && Math.abs(b - c[2]) <= 28)) bad++;
+ }
+ return inside ? bad / inside : 0;
+}
diff --git a/src/lib/native.ts b/src/lib/native.ts
index 4c63e50..c20d699 100644
--- a/src/lib/native.ts
+++ b/src/lib/native.ts
@@ -125,6 +125,14 @@ export const native = {
{ name: "WebP", extensions: ["webp"] },
],
}),
+ /** The Layers lab's .psd: desktop asks where (null: cancelled), a browser downloads it. */
+ savePsd: async (psd: Uint8Array): Promise<string | null> => {
+ if (!inBrowser) return invoke<string | null>("save_psd", psd);
+ const url = URL.createObjectURL(new Blob([psd as BlobPart], { type: "image/vnd.adobe.photoshop" }));
+ native.download(url, "Layers.psd");
+ setTimeout(() => URL.revokeObjectURL(url), 60_000);
+ return "Layers.psd";
+ },
/** Browser: saves the file at `url` on this device (the browser's downloads). */
download: (url: string, name: string) => {
const a = document.createElement("a");
diff --git a/src/lib/psd.test.ts b/src/lib/psd.test.ts
new file mode 100644
index 0000000..2ae5abd
--- /dev/null
+++ b/src/lib/psd.test.ts
@@ -0,0 +1,142 @@
+import { describe, expect, it } from "vitest";
+import { packBits, writePsd } from "./psd";
+
+function unpack(p: ArrayLike<number>, at: number, len: number): number[] {
+ const out: number[] = [];
+ let i = at;
+ while (i < at + len) {
+ const n = p[i++];
+ if (n < 128) for (let k = 0; k <= n; k++) out.push(p[i++]);
+ else if (n > 128) {
+ for (let k = 0; k < 257 - n; k++) out.push(p[i]);
+ i++;
+ }
+ }
+ return out;
+}
+
+/** Enough of a PSD reader for the file writePsd makes. */
+function readPsd(b: Uint8Array) {
+ const v = new DataView(b.buffer, b.byteOffset, b.byteLength);
+ let o = 0;
+ const u8 = () => b[o++];
+ const u16 = () => ((o += 2), v.getUint16(o - 2));
+ const u32 = () => ((o += 4), v.getUint32(o - 4));
+ const s = (n: number) => String.fromCharCode(...b.subarray(o, (o += n)));
+ /** A section by its u32 length (`o += u32()` would add to o as it was before the read). */
+ const skip = () => {
+ const n = u32();
+ o += n;
+ };
+ expect(s(4)).toBe("8BPS");
+ expect(u16()).toBe(1);
+ o += 6;
+ const channels = u16();
+ const height = u32();
+ const width = u32();
+ expect([u16(), u16()]).toEqual([8, 3]);
+ skip();
+ skip();
+ const masksLen = u32();
+ const masksEnd = o + masksLen;
+ const infoLen = u32();
+ const infoEnd = o + infoLen;
+ expect(infoLen % 2).toBe(0);
+ const count = u16();
+ const recs = [];
+ for (let i = 0; i < count; i++) {
+ const [top, left, bottom, right] = [u32(), u32(), u32(), u32()];
+ const ch = u16();
+ const lens = Array.from({ length: ch }, () => [v.getInt16((o += 6) - 6), v.getUint32(o - 4)]);
+ expect(s(8)).toBe("8BIMnorm");
+ const [, , flags] = [u8(), u8(), u8(), u8()];
+ const extra = u32();
+ const end = o + extra;
+ skip();
+ skip();
+ const nl = u8();
+ const short = s(nl);
+ let name = short;
+ o += (4 - ((nl + 1) % 4)) % 4;
+ while (o < end) {
+ expect(s(4)).toBe("8BIM");
+ const key = s(4);
+ const len = u32();
+ if (key === "luni") {
+ const n = v.getUint32(o);
+ name = String.fromCharCode(...Array.from({ length: n }, (_, k) => v.getUint16(o + 4 + k * 2)));
+ }
+ o += len;
+ }
+ expect(o).toBe(end);
+ recs.push({ top, left, bottom, right, lens, hidden: !!(flags & 2), name, short, pixels: [] as number[][] });
+ }
+ for (const r of recs) {
+ const h = r.bottom - r.top;
+ for (const [, len] of r.lens) {
+ const start = o;
+ const comp = u16();
+ if (comp === 1) {
+ const counts = Array.from({ length: h }, () => u16());
+ const px: number[] = [];
+ for (const n of counts) {
+ px.push(...unpack(b, o, n));
+ o += n;
+ }
+ r.pixels.push(px);
+ } else r.pixels.push([]);
+ expect(o - start).toBe(len);
+ }
+ }
+ expect(o).toBeLessThanOrEqual(infoEnd);
+ o = infoEnd;
+ expect(u32()).toBe(0);
+ expect(o).toBe(masksEnd);
+ expect(u16()).toBe(1);
+ const counts = Array.from({ length: channels * height }, () => u16());
+ const flat: number[] = [];
+ for (const n of counts) {
+ flat.push(...unpack(b, o, n));
+ o += n;
+ }
+ expect(o).toBe(b.length);
+ return { width, height, channels, recs, flat };
+}
+
+describe("psd", () => {
+ it("packBits round-trips runs, literals and long rows", () => {
+ const rows = [[1], [5, 5], [1, 2, 3], [0, 0, 0, 0, 7, 8, 9, 9, 9, 1], Array.from({ length: 700 }, (_, i) => (i < 300 ? 0 : i % 7))];
+ for (const r of rows) {
+ const p = packBits(Uint8Array.from(r));
+ expect(unpack(p, 0, p.length)).toEqual(r);
+ }
+ expect(packBits(new Uint8Array(1000)).length).toBeLessThan(20);
+ });
+
+ it("writes each layer cropped to what it shows, bottom first, with its name and visibility", () => {
+ const W = 6;
+ const H = 4;
+ const rgba = (f: (x: number, y: number) => number[] | null) => {
+ const a = new Uint8ClampedArray(W * H * 4);
+ for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) a.set(f(x, y) ?? [0, 0, 0, 0], (y * W + x) * 4);
+ return a;
+ };
+ const cup = rgba((x, y) => (x >= 2 && x <= 3 && y === 1 ? [200, 10, 20, 255] : null));
+ const bg = rgba((x) => [x * 10, 50, 60, 255]);
+ const empty = rgba(() => null);
+ const flat = rgba((x) => [x, x, x, 255]);
+ const out = readPsd(writePsd(W, H, [{ name: "Cup ☕", hidden: true, rgba: cup }, { name: "Nothing", hidden: false, rgba: empty }, { name: "Background", hidden: false, rgba: bg }], flat));
+ expect([out.width, out.height, out.channels]).toEqual([W, H, 3]);
+ expect(out.recs.map((r) => r.name)).toEqual(["Background", "Nothing", "Cup ☕"]);
+ expect(out.recs[2].short).toBe("Cup _");
+ expect(out.recs.map((r) => r.hidden)).toEqual([false, false, true]);
+ const c = out.recs[2];
+ expect([c.top, c.left, c.bottom, c.right]).toEqual([1, 2, 2, 4]);
+ expect(c.lens.map((l) => l[0])).toEqual([-1, 0, 1, 2]);
+ expect(c.pixels).toEqual([[255, 255], [200, 200], [10, 10], [20, 20]]);
+ expect(out.recs[1].pixels).toEqual([[], [], [], []]);
+ expect(out.recs[0].pixels[1].slice(0, W)).toEqual([0, 10, 20, 30, 40, 50]);
+ expect(out.flat.length).toBe(3 * W * H);
+ expect(out.flat.slice(0, W)).toEqual([0, 1, 2, 3, 4, 5]);
+ });
+});
diff --git a/src/lib/psd.ts b/src/lib/psd.ts
new file mode 100644
index 0000000..f26c61f
--- /dev/null
+++ b/src/lib/psd.ts
@@ -0,0 +1,199 @@
+// Layered Photoshop file (.psd, 8-bit RGB) of the Layers lab's document: each layer a pixel layer
+// cropped to what it shows, RLE (PackBits) channels, and the flattened image for apps that read
+// only that. Layer names in Unicode ('luni'), hidden layers kept hidden.
+
+export interface PsdLayer {
+ name: string;
+ hidden: boolean;
+ /** RGBA of the whole `width`×`height` document. */
+ rgba: Uint8ClampedArray;
+}
+
+/** PackBits of one row: runs of 2+ equal bytes (up to 128) as -(n-1), b; the rest as literals. */
+export function packBits(src: Uint8Array): number[] {
+ const out: number[] = [];
+ let i = 0;
+ while (i < src.length) {
+ let run = 1;
+ while (i + run < src.length && run < 128 && src[i + run] === src[i]) run++;
+ if (run > 1) {
+ out.push(257 - run, src[i]);
+ i += run;
+ continue;
+ }
+ // Literals until the next run of 3 (a run of 2 inside literals costs as much as copying it).
+ let n = 0;
+ while (i + n < src.length && n < 128 && !(i + n + 2 < src.length && src[i + n] === src[i + n + 1] && src[i + n] === src[i + n + 2])) n++;
+ n = Math.max(1, n);
+ out.push(n - 1);
+ for (let k = 0; k < n; k++) out.push(src[i + k]);
+ i += n;
+ }
+ return out;
+}
+
+class Writer {
+ private buf = new Uint8Array(1 << 20);
+ len = 0;
+ private room(n: number) {
+ if (this.len + n <= this.buf.length) return;
+ let size = this.buf.length * 2;
+ while (size < this.len + n) size *= 2;
+ const b = new Uint8Array(size);
+ b.set(this.buf.subarray(0, this.len));
+ this.buf = b;
+ }
+ u8(v: number) {
+ this.room(1);
+ this.buf[this.len++] = v & 255;
+ }
+ u16(v: number) {
+ this.u8(v >> 8);
+ this.u8(v);
+ }
+ u32(v: number) {
+ this.u16(v >>> 16);
+ this.u16(v);
+ }
+ bytes(b: ArrayLike<number>) {
+ this.room(b.length);
+ this.buf.set(b, this.len);
+ this.len += b.length;
+ }
+ ascii(s: string) {
+ for (let i = 0; i < s.length; i++) this.u8(s.charCodeAt(i));
+ }
+ /** Reserves a u32 for a section's length, written by the returned function at its end. */
+ length(): () => void {
+ const at = this.len;
+ this.u32(0);
+ return () => {
+ const n = this.len - at - 4;
+ this.buf[at] = n >>> 24;
+ this.buf[at + 1] = (n >>> 16) & 255;
+ this.buf[at + 2] = (n >>> 8) & 255;
+ this.buf[at + 3] = n & 255;
+ };
+ }
+ pad(multiple: number, from = 0) {
+ while ((this.len - from) % multiple) this.u8(0);
+ }
+ done = () => this.buf.slice(0, this.len);
+}
+
+/** One channel of the `w`×`h` box at (x0, y0) of an RGBA image `W` wide: the row byte counts, then
+ * the rows, PackBits. `channel` 0-3 (3: alpha). */
+function rleChannel(rgba: Uint8ClampedArray, W: number, x0: number, y0: number, w: number, h: number, channel: number) {
+ const counts: number[] = [];
+ const data: number[][] = [];
+ const row = new Uint8Array(w);
+ for (let y = 0; y < h; y++) {
+ const o = ((y0 + y) * W + x0) * 4 + channel;
+ for (let x = 0; x < w; x++) row[x] = rgba[o + x * 4];
+ const p = packBits(row);
+ counts.push(p.length);
+ data.push(p);
+ }
+ return { counts, data, size: 2 + h * 2 + counts.reduce((a, b) => a + b, 0) };
+}
+
+/** The box of pixels with any alpha (null: the layer shows nothing). */
+function opaqueBox(rgba: Uint8ClampedArray, W: number, H: number) {
+ let x0 = W;
+ let y0 = H;
+ let x1 = -1;
+ let y1 = -1;
+ for (let y = 0; y < H; y++)
+ for (let x = 0; x < W; x++) {
+ if (!rgba[(y * W + x) * 4 + 3]) continue;
+ if (x < x0) x0 = x;
+ if (x > x1) x1 = x;
+ if (y < y0) y0 = y;
+ if (y > y1) y1 = y;
+ }
+ return x1 < 0 ? null : { x0, y0, w: x1 - x0 + 1, h: y1 - y0 + 1 };
+}
+
+/** The .psd file: `layers` top first (as the lab lists them), `flat` the flattened image (opaque). */
+export function writePsd(width: number, height: number, layers: PsdLayer[], flat: Uint8ClampedArray): Uint8Array {
+ const w = new Writer();
+ // Header: RGB, 8 bits, 3 channels in the flattened image.
+ w.ascii("8BPS");
+ w.u16(1);
+ w.bytes([0, 0, 0, 0, 0, 0]);
+ w.u16(3);
+ w.u32(height);
+ w.u32(width);
+ w.u16(8);
+ w.u16(3);
+ w.u32(0); // colour mode data
+ w.u32(0); // image resources
+
+ const endMasks = w.length();
+ const endInfo = w.length();
+ const infoStart = w.len;
+ // Photoshop lists records bottom first.
+ const order = [...layers].reverse();
+ w.u16(order.length);
+ const channels = order.map((l) => {
+ const box = opaqueBox(l.rgba, width, height);
+ // Alpha (-1) first, then R, G, B.
+ const list = box ? [3, 0, 1, 2].map((c) => rleChannel(l.rgba, width, box.x0, box.y0, box.w, box.h, c)) : null;
+ return { box, list };
+ });
+ order.forEach((l, i) => {
+ const { box, list } = channels[i];
+ w.u32(box ? box.y0 : 0);
+ w.u32(box ? box.x0 : 0);
+ w.u32(box ? box.y0 + box.h : 0);
+ w.u32(box ? box.x0 + box.w : 0);
+ w.u16(4);
+ [0xffff, 0, 1, 2].forEach((id, c) => {
+ w.u16(id);
+ w.u32(list ? list[c].size : 2);
+ });
+ w.ascii("8BIMnorm");
+ w.u8(255); // opacity
+ w.u8(0); // clipping
+ w.u8((l.hidden ? 2 : 0) | 8); // bit 1: hidden; bit 3: bit 4 is meaningful
+ w.u8(0);
+ const endExtra = w.length();
+ w.u32(0); // layer mask
+ w.u32(0); // blending ranges
+ // The name as a Pascal string (ASCII, padded to 4), then in full as 'luni'.
+ const ascii = l.name.replace(/[^\x20-\x7e]/g, "_").slice(0, 255);
+ const at = w.len;
+ w.u8(ascii.length);
+ w.ascii(ascii);
+ w.pad(4, at);
+ w.ascii("8BIMluni");
+ const endLuni = w.length();
+ const lat = w.len;
+ w.u32(l.name.length);
+ for (let k = 0; k < l.name.length; k++) w.u16(l.name.charCodeAt(k));
+ w.pad(4, lat);
+ endLuni();
+ endExtra();
+ });
+ for (const { list } of channels)
+ for (let c = 0; c < 4; c++) {
+ if (!list) {
+ w.u16(0);
+ continue;
+ }
+ w.u16(1);
+ for (const n of list[c].counts) w.u16(n);
+ for (const row of list[c].data) w.bytes(row);
+ }
+ w.pad(2, infoStart);
+ endInfo();
+ w.u32(0); // global layer mask
+ endMasks();
+
+ // The flattened image: RLE, all rows' counts first, then R, G, B rows.
+ const flatChannels = [0, 1, 2].map((c) => rleChannel(flat, width, 0, 0, width, height, c));
+ w.u16(1);
+ for (const ch of flatChannels) for (const n of ch.counts) w.u16(n);
+ for (const ch of flatChannels) for (const row of ch.data) w.bytes(row);
+ return w.done();
+}
diff --git a/src/lib/sceneEdit.test.ts b/src/lib/sceneEdit.test.ts
index c3a6906..8a8bc47 100644
--- a/src/lib/sceneEdit.test.ts
+++ b/src/lib/sceneEdit.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { alignEdit, applyAffine, changed, darkness, fillHoles, IDENTITY, invertAffine, marksPrompt, objectAt, phaseShift, splitShadows, toneCurve } from "./sceneEdit";
+import { alignEdit, applyAffine, attached, changed, darkness, fillHoles, IDENTITY, invertAffine, marksPrompt, objectAt, phaseShift, splitShadows, toneCurve } from "./sceneEdit";
/** A textured test picture: smooth blobs of different sizes (edges everywhere, no repeats). */
function picture(w: number, h: number, at: (x: number, y: number) => { x: number; y: number } = (x, y) => ({ x, y })): Float32Array {
@@ -169,3 +169,23 @@ describe("scene edits", () => {
])).toMatch(/^Add a bottle where the red marks are and something that fits the scene where the blue marks are\./);
});
});
+
+describe("attached", () => {
+ it("keeps a long shadow whole from the object, through it, and leaves loose darkening out", () => {
+ const w = 40;
+ const h = 10;
+ const dark = new Float32Array(w * h);
+ const seed = new Uint8Array(w * h);
+ const obj = new Uint8Array(w * h);
+ // A shadow along row 8 from x 2 to 30, the object (no darkening read on it) at x 10-11.
+ for (let x = 2; x <= 30; x++) dark[8 * w + x] = x === 10 || x === 11 ? 0 : 0.3;
+ for (const x of [10, 11]) obj[8 * w + x] = 1;
+ seed[8 * w + 12] = 1;
+ // Noise far away, not touching it.
+ dark[1 * w + 35] = 0.5;
+ const m = attached(dark, seed, w, h, 0.02, obj);
+ expect(m[8 * w + 30]).toBe(255);
+ expect(m[8 * w + 2]).toBe(255);
+ expect(m[1 * w + 35]).toBe(0);
+ });
+});
diff --git a/src/lib/sceneEdit.ts b/src/lib/sceneEdit.ts
index 1ea6214..b07da19 100644
--- a/src/lib/sceneEdit.ts
+++ b/src/lib/sceneEdit.ts
@@ -459,6 +459,32 @@ export function objectAt(m: Uint8Array, seed: Uint8Array, w: number, h: number):
return fillHoles(out, w, h);
}
+/** The darkening that hangs together with what is near the objects: from `seed` (where to start:
+ * within reach of an object), through every pixel darker than `low` or in `through` (the objects:
+ * a shadow goes on past a leg), as far as it goes. A long cast shadow is kept whole wherever it
+ * reaches (a distance limit cut it with a straight edge, 11-01), and darkening that does not touch
+ * it (decode noise, the sky) stays out. */
+export function attached(dark: Float32Array, seed: Uint8Array, w: number, h: number, low: number, through?: Uint8Array): Uint8Array {
+ const out = new Uint8Array(w * h);
+ const ok = (i: number) => dark[i] > low || !!through?.[i];
+ const stack: number[] = [];
+ for (let i = 0; i < out.length; i++)
+ if (seed[i] && dark[i] > low) {
+ out[i] = 255;
+ stack.push(i);
+ }
+ while (stack.length) {
+ const i = stack.pop()!;
+ const x = i % w;
+ for (const j of [x > 0 ? i - 1 : -1, x < w - 1 ? i + 1 : -1, i - w, i + w])
+ if (j >= 0 && j < out.length && !out[j] && ok(j)) {
+ out[j] = 255;
+ stack.push(j);
+ }
+ }
+ return out;
+}
+
/** Fills what the mask encloses (not reachable from the border without crossing it). */
export function fillHoles(m: Uint8Array, w: number, h: number): Uint8Array {
const outside = new Uint8Array(w * h);
diff --git a/src/styles.css b/src/styles.css
index 4c09861..dae9942 100644
--- a/src/styles.css
+++ b/src/styles.css
@@ -626,9 +626,19 @@ pre.diag { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font
.doc-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 6px; padding-top: 12px; border-top: 1px solid var(--border); }
.doc-actions-title { font-weight: 600; font-size: 12px; }
.doc-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
-.doc-grid .btn, .doc-footer .btn { justify-content: center; height: 34px; min-width: 0; white-space: nowrap; }
+.doc-grid .btn { justify-content: center; height: 34px; min-width: 0; white-space: nowrap; }
.doc-grid > :only-child { grid-column: 1 / -1; }
-.doc-footer { margin-top: auto; display: flex; flex-direction: column; gap: 6px; padding-top: 12px; }
+.editor-tools .pill.doc-cmd { color: var(--text); font-weight: 500; }
+.editor-tools .pill.doc-cmd.primary { background: var(--invert-bg); color: var(--invert-text); }
+.editor-tools .pill.doc-cmd.primary:hover:not(:disabled) { opacity: 0.88; background: var(--invert-bg); }
+.editor-tools .pill:disabled { opacity: 0.45; cursor: default; }
+.editor-tools .slider .doc-cmd-val { min-width: 36px; color: var(--text); font-variant-numeric: tabular-nums; }
+.editor-tools.locked .group, .layers-panel.locked { pointer-events: none; opacity: 0.5; transition: opacity 0.2s; }
+/* Automatic shadows being made: the stage is held, the progress in the middle of the image. */
+.doc-lock { position: absolute; inset: 0; z-index: 5; display: grid; place-items: center; background: color-mix(in srgb, #000 22%, transparent); cursor: progress; }
+.doc-lock-card { display: flex; align-items: center; gap: 12px; padding: 14px 20px; border-radius: 16px; background: var(--invert-bg); color: var(--invert-text); font-size: 14px; font-weight: 500; font-variant-numeric: tabular-nums; box-shadow: var(--lift); }
+.doc-lock-card span { display: flex; flex-direction: column; gap: 2px; }
+.doc-lock-card small { font-size: 12px; font-weight: 400; opacity: 0.7; }
.doc-none { margin-top: 10px; }
.layers-panel > * { flex-shrink: 0; }
.doc-versions { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; padding: 8px; border-radius: 12px; background: var(--bg); box-shadow: var(--lift); }