[art] GardenerLayers: The gardener in layered parts, painted over one guide in register, checked on accept

This commit is contained in:
2026-11-17 21:53:22 +01:00
parent 0b72ae05a2
commit 229cced136
82 changed files with 1026 additions and 83 deletions
+57 -1
View File
@@ -96,9 +96,53 @@ async function rgba(input: Buffer): Promise<{ px: Buffer; width: number; height:
return { px: data, width: info.width, height: info.height };
}
export async function acceptPicture(input: Buffer, id: string, m: Manifest): Promise<Accepted> {
/** Where a cell's drawing lies, as shares of the cell (0–1), or null when nothing is drawn. */
interface Extent {
readonly x0: number;
readonly y0: number;
readonly x1: number;
readonly y1: number;
}
function extent(px: Buffer, width: number, x0: number, y0: number, x1: number, y1: number): Extent | null {
let lx = Infinity, ly = Infinity, hx = -Infinity, hy = -Infinity;
for (let y = y0; y < y1; y++) {
for (let x = x0; x < x1; x++) {
if ((px[(y * width + x) * 4 + 3] ?? 0) < ALPHA) continue;
lx = Math.min(lx, x); hx = Math.max(hx, x); ly = Math.min(ly, y); hy = Math.max(hy, y);
}
}
if (lx === Infinity) return null;
const w = x1 - x0, h = y1 - y0;
return { x0: (lx - x0) / w, y0: (ly - y0) / h, x1: (hx + 1 - x0) / w, y1: (hy + 1 - y0) / h };
}
/**
* How far a layer may reach past its guide's figure, as a share of the cell: hair and a hat may
* be a little fuller than the guide's braids and straw hat, never somewhere else.
*/
export const REGISTER = 0.08;
/** The guide's cells, as accepted into art/final/: cell id → its WebP. */
export type Guide = ReadonlyMap<string, Buffer>;
async function guideExtent(guide: Guide, cell: string): Promise<Extent | null> {
const file = guide.get(cell);
if (!file) return null;
const { px, width, height } = await rgba(file);
return extent(px, width, 0, 0, width, height);
}
export async function acceptPicture(input: Buffer, id: string, m: Manifest, guide?: Guide): Promise<Accepted> {
const target = findTarget(m, id);
const a = target.asset;
if (a.over && target.cell) {
throw new Refused(`${id}: a layer is painted whole over its guide (${a.over}), never one picture at a time; the parts would not line up. Accept ${a.id} whole.`);
}
const guideCells = a.over && a.grid ? (m.assets.find((g) => g.id === a.over)?.cells ?? []) : [];
if (a.over && guideCells.some((c) => !guide?.has(c.id))) {
throw new Refused(`${id}: its guide ${a.over} is not in art/final/ yet. Accept the guide first: every layer is checked against it.`);
}
const notes: string[] = [];
const { px, width, height } = await rgba(input).catch((e: unknown) => {
throw new Refused(`${id}: the file could not be read as a picture (${String(e)})`);
@@ -137,6 +181,18 @@ export async function acceptPicture(input: Buffer, id: string, m: Manifest): Pro
});
if (empty.length) throw new Refused(`${id}: nothing drawn in ${empty.join(', ')}. The whole sheet is refused.`);
if (painted.length) throw new Refused(`${id}: painted on a background in ${painted.join(', ')}. The whole sheet is refused.`);
if (a.over && guide) {
// In register: each picture's part lies on the guide's figure in the same cell (STYLE.md §3b).
const off: string[] = [];
for (const [i, r] of regions.entries()) {
const mine = extent(px, width, r.left, r.top, r.left + r.width, r.top + r.height);
const under = await guideExtent(guide, guideCells[i]?.id ?? '');
if (!mine || !under) continue;
const out = Math.max(under.x0 - mine.x0, under.y0 - mine.y0, mine.x1 - under.x1, mine.y1 - under.y1);
if (out > REGISTER) off.push(`${r.id} (${Math.round(out * 100)} % of the cell outside the guide's figure)`);
}
if (off.length) throw new Refused(`${id}: not in register with ${a.over} in ${off.join(', ')}. Paint it over the guide again; the whole sheet is refused.`);
}
const files: FinalFile[] = [];
for (const r of regions) {
const out = await encode(fitCell(sharp(input).extract({ left: r.left, top: r.top, width: r.width, height: r.height })), BUDGET.cell, r.id);