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: automatic shadows as one layer

Gabriele Lozuponedbc88379892e
diff --git a/src/components/LabScene.ts b/src/components/LabScene.ts
index 9d02dde..b0cddc6 100644
--- a/src/components/LabScene.ts
+++ b/src/components/LabScene.ts
@@ -225,6 +225,28 @@ 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.
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
+ // back: a fill that fell short left a light ring round the spider's legs (11-01).
+ if (targets.length && e.one) {
+ const dark = addedDarkness(p, [...feet.values()], reach, marked);
+ const near = new Uint8Array(w * h);
+ for (const t of targets) for (let i = 0; i < t.m.length; i++) if (t.m[i]) near[i] = 255;
+ const q = 4;
+ const qw = Math.ceil(w / q);
+ const qh = Math.ceil(h / q);
+ 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);
+ 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;
+ 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.");
+ else out.shadows.push({ of: "all", src: `input:${(await upload(alpha, w, h, W, H, "Shadows")).id}` });
+ return out;
+ }
if (targets.length) {
const others = new Uint8Array(w * h);
for (const [id, m] of feet) if (!e.shadows.includes(id)) for (let i = 0; i < m.length; i++) if (m[i]) others[i] = 255;
diff --git a/src/components/LayerDoc.tsx b/src/components/LayerDoc.tsx
index 9054a90..63a6e4c 100644
--- a/src/components/LayerDoc.tsx
+++ b/src/components/LayerDoc.tsx
@@ -312,6 +312,10 @@ 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
+ * kitchen): the model put shadows where the light makes them and none on floating things (lettering, ghosts). */
+const AUTO_SHADOWS_PROMPT =
+ "Add natural shadows for the objects that stand or rest on a surface: a soft contact shadow where each one touches it and the shadow each one casts, following the direction of the light in the scene. Floating things cast no shadow. Keep everything else in the image exactly unchanged.";
/** The selected layers per document (the last one is the one the panel shows), kept while the
* view closes (Paint area, Expand, the Stage). */
@@ -1213,8 +1217,12 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
* grey (holes left would keep the image transparent), with the pencil's marks drawn on when there
* are any; `shadows`: objects whose shadows are read back (their shadow layers are left out of
* what is sent, so Qwen makes them again); `look`: objects that take the edit's look. */
- const sendScene = async (o: { shadows: string[]; look: string[]; prompt: string; marks: boolean; hint: string }) => {
+ /** `send`: made right away (a lab step, no Prompt+) instead of set up in the prompt; `one`: one
+ * shadow layer for all the objects. */
+ const sendScene = async (o: { shadows: string[]; look: string[]; prompt: string; marks: boolean; hint: string; send?: boolean; one?: boolean }) => {
setBusy(true);
+ // Sent right away: the flattened layers become a version on the way; the prompt's image is put back.
+ const keep = { selected: getState().selected, refs: getState().composer.refs };
try {
const skip = new Set(live.layers.filter((l) => l.effect && (o.shadows.includes(l.follows ?? "") || (!l.follows && o.shadows.length > 0))).map((l) => l.id));
const flat = document.createElement("canvas");
@@ -1239,8 +1247,10 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
}
addVersions([source], 0);
const target = `input:${source.id}`;
- if (!editSource(target, false)) throw new Error("The flattened layers could not be opened.");
- updateComposer({ mask: null, outpaint: null, mode: "auto", prompt: o.prompt });
+ if (!o.send) {
+ if (!editSource(target, false)) throw new Error("The flattened layers could not be opened.");
+ updateComposer({ mask: null, outpaint: null, mode: "auto", prompt: o.prompt });
+ }
const scene: SceneEdit = {
source: target,
before: `input:${before.id}`,
@@ -1249,10 +1259,21 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
look: o.look,
places: placesOf(live),
reach: SCENE_REACH,
+ ...(o.one ? { one: true } : {}),
};
- commit({ ...live, scenes: [...(live.scenes ?? []), scene] });
+ // The same layers again upload as the same image (the service keeps one copy): an older edit of it
+ // would read this one's result first (11-01: per-object shadows instead of the one layer asked for).
+ commit({ ...live, scenes: [...(live.scenes ?? []).filter((x) => x.source !== target), scene] });
if (marksId) setSketch({ marks: [], wants: sketch.wants });
setSels([]);
+ if (o.send) {
+ const c = getState().composer;
+ const req = buildRequest({ ...c, prompt: o.prompt, refs: [refFromInput(source)], mask: null, outpaint: null, create: false, mode: "auto", enhance: false, enhancedFrom: null, seed: null });
+ await submitRequest({ ...req, hidden: true });
+ setState((st) => ({ selected: st.session.some((i) => i.key === keep.selected) ? keep.selected : st.selected, composer: { ...st.composer, refs: keep.refs } }));
+ toastInfo((await hasHoles()) ? "Making the shadows, but the background still has holes (grey in the image): fill them first for real shadows." : o.hint);
+ return;
+ }
focusPrompt(true);
toastInfo((await hasHoles()) ? "Ready in the prompt, but the background still has holes (grey in the image): Fill the holes first." : o.hint);
} catch (e) {
@@ -1802,6 +1823,18 @@ export function LayerDoc({ projectId, doc, onChange, onNewSplit }: { projectId:
<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>
diff --git a/src/components/LayersEditor.tsx b/src/components/LayersEditor.tsx
index 6b411cd..7d51816 100644
--- a/src/components/LayersEditor.tsx
+++ b/src/components/LayersEditor.tsx
@@ -94,6 +94,18 @@ function LayersLab({ projectId }: { projectId: string }) {
}
}
for (const s of r.shadows) {
+ // 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);
+ 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);
+ layers = [...layers];
+ layers.splice(at < 0 ? layers.length : at, 0, layer);
+ }
+ continue;
+ }
// A new object's shadow follows the new layer; an object's, the object (from where it was).
const isNew = r.objects.some((o) => o.colour === s.of);
const obj = isNew ? madeBy[s.of] : s.of;
diff --git a/src/lib/layerDoc.ts b/src/lib/layerDoc.ts
index 3129950..a705dbf 100644
--- a/src/lib/layerDoc.ts
+++ b/src/lib/layerDoc.ts
@@ -83,6 +83,8 @@ export interface DocLayer {
feeds?: Feed[];
/** Shadows and light made by an edit (only the pixels it changed): not clicked on the canvas. */
effect?: boolean;
+ /** The one layer of Automatic shadows: the next one replaces it (its next version). */
+ auto?: boolean;
/** An object's shadow moves and scales with the object (`follows`), from where the object was
* when the shadow was made (`at`). */
follows?: string;
@@ -130,6 +132,8 @@ export interface SceneEdit {
places: Record<string, Place & { source: string }>;
/** How far shadows are looked for around an object, in % of the image's long side. */
reach: number;
+ /** One shadow layer for all the objects (Automatic shadows), not one per object. */
+ one?: boolean;
/** Layers its results made, so the next result becomes their next version: object id -> its
* shadow layer, colour -> the new layer, "<colour>:shadow" -> the new layer's shadow. */
made?: Record<string, string>;