@automataframework/art 0.0.0-stage → 0.0.1-preview.12.sha-e1e5dd9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/dist/bin.d.ts +3 -0
- package/dist/bin.js +38 -0
- package/dist/features/clean/clean.d.ts +12 -0
- package/dist/features/clean/clean.js +75 -0
- package/dist/features/make/fal.d.ts +10 -0
- package/dist/features/make/fal.js +19 -0
- package/dist/features/make/ledger.d.ts +17 -0
- package/dist/features/make/ledger.js +29 -0
- package/dist/features/make/make.d.ts +20 -0
- package/dist/features/make/make.js +97 -0
- package/dist/features/make/models.d.ts +11 -0
- package/dist/features/make/models.js +7 -0
- package/dist/features/palette/duel.d.ts +2 -0
- package/dist/features/palette/duel.js +22 -0
- package/dist/features/palette/palette.d.ts +8 -0
- package/dist/features/palette/palette.js +35 -0
- package/dist/features/sidecar/sidecar.d.ts +15 -0
- package/dist/features/sidecar/sidecar.js +35 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +9 -0
- package/dist/lib/png.d.ts +4 -0
- package/dist/lib/png.js +49 -0
- package/package.json +38 -3
- package/vendor/unfake/LICENSE +21 -0
- package/vendor/unfake/SOURCE.md +24 -0
- package/vendor/unfake/lib/core-client.js +159 -0
- package/vendor/unfake/lib/core-codec.js +35 -0
- package/vendor/unfake/lib/cvfree.js +193 -0
- package/vendor/unfake/lib/edge-detect.js +127 -0
- package/vendor/unfake/lib/pixel.js +541 -0
- package/vendor/unfake/lib/utils.js +520 -0
- package/vendor/unfake/vendor/unfake-core/unfake_wasm.js +524 -0
- package/vendor/unfake/vendor/unfake-core/unfake_wasm_bg.wasm +0 -0
- package/vendor/unfake/workers/unfake-core-worker.js +259 -0
- package/README.md +0 -3
package/dist/bin.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { randomInt } from 'node:crypto';
|
|
3
|
+
import { clean } from './features/clean/clean.js';
|
|
4
|
+
import { makeArt, reviewArt } from './features/make/make.js';
|
|
5
|
+
const HELP = `Make a picture: make-art <brief.json> --ledger <spend-list.json>
|
|
6
|
+
Reimagine a picture: make-art review <picture.png> --ledger <spend-list.json>
|
|
7
|
+
|
|
8
|
+
The brief is a JSON file: { "name": "hero", "prompt": "a small knight, pixel art" }.
|
|
9
|
+
The spend list keeps every call's cost; a month stops at $50.
|
|
10
|
+
The fal.ai key goes in FAL_KEY.`;
|
|
11
|
+
/** Runs the command and gives back its exit code. */
|
|
12
|
+
export async function main(args, env, say) {
|
|
13
|
+
const at = args.indexOf('--ledger');
|
|
14
|
+
const ledger = at >= 0 ? args[at + 1] : undefined;
|
|
15
|
+
const rest = at >= 0 ? args.filter((_, i) => i !== at && i !== at + 1) : args;
|
|
16
|
+
const [first, second] = rest;
|
|
17
|
+
if (!first || !ledger || (first === 'review' && !second)) {
|
|
18
|
+
say(HELP);
|
|
19
|
+
return 1;
|
|
20
|
+
}
|
|
21
|
+
const key = env['FAL_KEY'];
|
|
22
|
+
if (!key) {
|
|
23
|
+
say('There is no fal.ai key. Put it in FAL_KEY and try again.');
|
|
24
|
+
return 1;
|
|
25
|
+
}
|
|
26
|
+
const deps = { fetch, key, ledger, now: () => new Date(), seed: () => randomInt(2 ** 31), clean };
|
|
27
|
+
try {
|
|
28
|
+
const path = first === 'review' ? await reviewArt(second ?? '', deps) : await makeArt(first, deps);
|
|
29
|
+
say(`Made ${path}`);
|
|
30
|
+
return 0;
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
say(error instanceof Error ? error.message : String(error));
|
|
34
|
+
return 1;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (import.meta.main)
|
|
38
|
+
process.exitCode = await main(process.argv.slice(2), process.env, (line) => console.log(line));
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Picture } from '../palette/palette.ts';
|
|
2
|
+
/** The unfake.js commit copied into `vendor/unfake/`, and where it came from. */
|
|
3
|
+
export declare const UNFAKE: {
|
|
4
|
+
repo: string;
|
|
5
|
+
commit: string;
|
|
6
|
+
};
|
|
7
|
+
/** What unfake.js made, and the pixel size it found (8 means each real pixel was 8 by 8). */
|
|
8
|
+
export type Cleaned = Picture & {
|
|
9
|
+
scale: number;
|
|
10
|
+
};
|
|
11
|
+
/** Runs unfake.js on a PNG, in Chromium, and gives back the cleaned picture. */
|
|
12
|
+
export declare function clean(png: Uint8Array, executablePath?: string | undefined): Promise<Cleaned>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
// Cleans an AI-made picture with unfake.js, which is a browser library, so it runs inside Chromium. unfake.js finds
|
|
10
|
+
// the picture's real pixel size and shrinks it to one pixel per cell, without new colours or blur.
|
|
11
|
+
import { execFileSync } from 'node:child_process';
|
|
12
|
+
import { readFileSync } from 'node:fs';
|
|
13
|
+
import { createServer } from 'node:http';
|
|
14
|
+
import { createRequire } from 'node:module';
|
|
15
|
+
import { dirname, extname, join, normalize } from 'node:path';
|
|
16
|
+
import { fileURLToPath } from 'node:url';
|
|
17
|
+
import { chromium } from 'playwright-core';
|
|
18
|
+
/** The unfake.js commit copied into `vendor/unfake/`, and where it came from. */
|
|
19
|
+
export const UNFAKE = {
|
|
20
|
+
repo: 'https://github.com/jenissimo/unfake.js',
|
|
21
|
+
commit: '663686899e8394dfda25efd7f2a947f008c6f676',
|
|
22
|
+
};
|
|
23
|
+
const vendor = join(dirname(fileURLToPath(import.meta.url)), '..', '..', '..', 'vendor', 'unfake');
|
|
24
|
+
const types = { '.js': 'text/javascript', '.wasm': 'application/wasm' };
|
|
25
|
+
// The browser is fetched the first time it is needed, not when the package is installed.
|
|
26
|
+
async function launch(executablePath) {
|
|
27
|
+
try {
|
|
28
|
+
return await chromium.launch(executablePath ? { executablePath } : {});
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
if (executablePath || !String(error).includes("Executable doesn't exist"))
|
|
32
|
+
throw error;
|
|
33
|
+
const cli = join(dirname(createRequire(import.meta.url).resolve('playwright-core/package.json')), 'cli.js');
|
|
34
|
+
execFileSync(process.execPath, [cli, 'install', 'chromium'], { stdio: 'inherit' });
|
|
35
|
+
return chromium.launch();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** Runs unfake.js on a PNG, in Chromium, and gives back the cleaned picture. */
|
|
39
|
+
export async function clean(png, executablePath = process.env['AUTOMATA_CHROMIUM']) {
|
|
40
|
+
const server = createServer((req, res) => {
|
|
41
|
+
const path = normalize(join(vendor, decodeURIComponent((req.url ?? '/').split('?')[0] ?? '/')));
|
|
42
|
+
if (req.url === '/')
|
|
43
|
+
return res.writeHead(200, { 'content-type': 'text/html' }).end('<!doctype html><title>art</title>');
|
|
44
|
+
try {
|
|
45
|
+
if (!path.startsWith(vendor))
|
|
46
|
+
throw new Error('outside');
|
|
47
|
+
const body = readFileSync(path);
|
|
48
|
+
res.writeHead(200, { 'content-type': types[extname(path)] ?? 'application/octet-stream' }).end(body);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
res.writeHead(404).end();
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
await new Promise((done) => server.listen(0, '127.0.0.1', done));
|
|
55
|
+
const browser = await launch(executablePath);
|
|
56
|
+
try {
|
|
57
|
+
const page = await browser.newPage();
|
|
58
|
+
await page.goto(`http://127.0.0.1:${server.address().port}/`);
|
|
59
|
+
const result = await page.evaluate(async (base64) => {
|
|
60
|
+
const bytes = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
|
|
61
|
+
const file = Object.assign(new Blob([bytes], { type: 'image/png' }), { name: 'picture.png' });
|
|
62
|
+
const { processImage } = await import(__rewriteRelativeImportExtension('/lib/pixel.js'));
|
|
63
|
+
// 256 colours lets every DUEL colour through; the palette step picks the real ones after.
|
|
64
|
+
const out = await processImage({ file, maxColors: 256 });
|
|
65
|
+
const image = out.imageData;
|
|
66
|
+
const scale = out.manifest.processing_steps.scale_detection.detected_scale;
|
|
67
|
+
return { width: image.width, height: image.height, rgba: Array.from(image.data), scale };
|
|
68
|
+
}, Buffer.from(png).toString('base64'));
|
|
69
|
+
return { ...result, rgba: Uint8Array.from(result.rgba) };
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
await browser.close();
|
|
73
|
+
server.close();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** What the art command needs from outside, so tests can stand in for fal.ai and spend nothing. */
|
|
2
|
+
export type Outside = {
|
|
3
|
+
fetch: typeof fetch;
|
|
4
|
+
key: string;
|
|
5
|
+
};
|
|
6
|
+
/** Sends `input` to a fal.ai model and gives back the first picture it made, with the seed it used. */
|
|
7
|
+
export declare function falPicture(outside: Outside, model: string, input: Record<string, unknown>): Promise<{
|
|
8
|
+
png: Uint8Array;
|
|
9
|
+
seed: number;
|
|
10
|
+
}>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// fal.ai by plain `fetch`: one call, one picture back.
|
|
2
|
+
/** Sends `input` to a fal.ai model and gives back the first picture it made, with the seed it used. */
|
|
3
|
+
export async function falPicture(outside, model, input) {
|
|
4
|
+
const res = await outside.fetch(`https://fal.run/${model}`, {
|
|
5
|
+
method: 'POST',
|
|
6
|
+
headers: { authorization: `Key ${outside.key}`, 'content-type': 'application/json' },
|
|
7
|
+
body: JSON.stringify(input),
|
|
8
|
+
});
|
|
9
|
+
if (!res.ok)
|
|
10
|
+
throw new Error(`fal.ai said no (${res.status}): ${(await res.text()).slice(0, 300)}`);
|
|
11
|
+
const answer = (await res.json());
|
|
12
|
+
const url = answer.images?.[0]?.url;
|
|
13
|
+
if (!url)
|
|
14
|
+
throw new Error('fal.ai answered without a picture.');
|
|
15
|
+
const picture = await outside.fetch(url);
|
|
16
|
+
if (!picture.ok)
|
|
17
|
+
throw new Error(`The picture fal.ai made could not be fetched (${picture.status}).`);
|
|
18
|
+
return { png: new Uint8Array(await picture.arrayBuffer()), seed: answer.seed ?? Number(input['seed']) };
|
|
19
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** One fal.ai call. Every call gets a line, whether its picture was kept or thrown away. */
|
|
2
|
+
export type LedgerLine = {
|
|
3
|
+
at: string;
|
|
4
|
+
model: string;
|
|
5
|
+
name: string;
|
|
6
|
+
cost: number;
|
|
7
|
+
kept: boolean;
|
|
8
|
+
};
|
|
9
|
+
/** The most a month may spend on art, in dollars. */
|
|
10
|
+
export declare const MONTH_CAP = 50;
|
|
11
|
+
export declare function readLedger(path: string): LedgerLine[];
|
|
12
|
+
/** What this calendar month (in UTC) has spent so far, in dollars. */
|
|
13
|
+
export declare function monthSpend(lines: LedgerLine[], now: Date): number;
|
|
14
|
+
/** Throws, in plain words, when one more call of `cost` would take the month past the cap. */
|
|
15
|
+
export declare function checkSpend(path: string, cost: number, now: Date): void;
|
|
16
|
+
/** Adds a line. One call per row in the file, so a change to it is easy to read. */
|
|
17
|
+
export declare function addLine(path: string, line: LedgerLine): void;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
/** The most a month may spend on art, in dollars. */
|
|
3
|
+
export const MONTH_CAP = 50;
|
|
4
|
+
export function readLedger(path) {
|
|
5
|
+
if (!existsSync(path))
|
|
6
|
+
return [];
|
|
7
|
+
const lines = JSON.parse(readFileSync(path, 'utf8'));
|
|
8
|
+
if (!Array.isArray(lines))
|
|
9
|
+
throw new Error(`The spend list ${path} is not a list.`);
|
|
10
|
+
return lines;
|
|
11
|
+
}
|
|
12
|
+
/** What this calendar month (in UTC) has spent so far, in dollars. */
|
|
13
|
+
export function monthSpend(lines, now) {
|
|
14
|
+
const month = now.toISOString().slice(0, 7);
|
|
15
|
+
// Counted in tenths of a cent, so many small prices add up exactly.
|
|
16
|
+
const mills = lines.filter((l) => l.at.slice(0, 7) === month).reduce((sum, l) => sum + Math.round(l.cost * 1000), 0);
|
|
17
|
+
return mills / 1000;
|
|
18
|
+
}
|
|
19
|
+
/** Throws, in plain words, when one more call of `cost` would take the month past the cap. */
|
|
20
|
+
export function checkSpend(path, cost, now) {
|
|
21
|
+
const spent = monthSpend(readLedger(path), now);
|
|
22
|
+
if (spent + cost > MONTH_CAP)
|
|
23
|
+
throw new Error(`This month has spent $${spent.toFixed(3)} on art, and the most is $${MONTH_CAP}. No picture was made.`);
|
|
24
|
+
}
|
|
25
|
+
/** Adds a line. One call per row in the file, so a change to it is easy to read. */
|
|
26
|
+
export function addLine(path, line) {
|
|
27
|
+
const lines = [...readLedger(path), line];
|
|
28
|
+
writeFileSync(path, `[\n${lines.map((l) => ` ${JSON.stringify(l)}`).join(',\n')}\n]\n`);
|
|
29
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Cleaned } from '../clean/clean.ts';
|
|
2
|
+
import { type Outside } from './fal.ts';
|
|
3
|
+
/** A brief: what to draw. `folder` is where the picture goes, next to the brief when left out. */
|
|
4
|
+
export type Brief = {
|
|
5
|
+
name: string;
|
|
6
|
+
prompt: string;
|
|
7
|
+
seed?: number;
|
|
8
|
+
folder?: string;
|
|
9
|
+
};
|
|
10
|
+
/** Everything outside the code, handed in, so tests stand in for fal.ai, the browser, the clock and chance. */
|
|
11
|
+
export type Deps = Outside & {
|
|
12
|
+
ledger: string;
|
|
13
|
+
now: () => Date;
|
|
14
|
+
seed: () => number;
|
|
15
|
+
clean: (png: Uint8Array) => Promise<Cleaned>;
|
|
16
|
+
};
|
|
17
|
+
/** Makes one picture from a brief: fal.ai, then unfake.js, then the DUEL palette. Gives back the picture's path. */
|
|
18
|
+
export declare function makeArt(briefPath: string, deps: Deps): Promise<string>;
|
|
19
|
+
/** The review step: fal.ai reimagines a picture, and its answer is saved beside it as `<name>.reimagined.png`. */
|
|
20
|
+
export declare function reviewArt(picturePath: string, deps: Deps, prompt?: string): Promise<string>;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { basename, dirname, join, resolve } from 'node:path';
|
|
3
|
+
import { encodePng } from '../../lib/png.js';
|
|
4
|
+
import { UNFAKE } from '../clean/clean.js';
|
|
5
|
+
import { DUEL_SOURCE } from '../palette/duel.js';
|
|
6
|
+
import { toPalette } from '../palette/palette.js';
|
|
7
|
+
import { writeSidecar } from '../sidecar/sidecar.js';
|
|
8
|
+
import { falPicture } from './fal.js';
|
|
9
|
+
import { addLine, checkSpend } from './ledger.js';
|
|
10
|
+
import { MODELS, SIZE } from './models.js';
|
|
11
|
+
function readBrief(path) {
|
|
12
|
+
const brief = JSON.parse(readFileSync(path, 'utf8'));
|
|
13
|
+
if (typeof brief.name !== 'string' || !/^[a-z0-9][a-z0-9-]*$/.test(brief.name))
|
|
14
|
+
throw new Error('The brief needs a "name" in small letters, numbers and dashes, like "hero-run".');
|
|
15
|
+
if (typeof brief.prompt !== 'string' || brief.prompt.trim() === '')
|
|
16
|
+
throw new Error('The brief needs a "prompt": the words that say what to draw.');
|
|
17
|
+
if (brief.seed !== undefined && !Number.isInteger(brief.seed))
|
|
18
|
+
throw new Error('The brief\'s "seed" must be a whole number.');
|
|
19
|
+
return { ...brief, folder: resolve(dirname(path), brief.folder ?? '.') };
|
|
20
|
+
}
|
|
21
|
+
// Runs one paid call: checks the month's spend first, and adds a ledger line whether the picture is kept or not.
|
|
22
|
+
async function paidCall(deps, model, cost, name, work) {
|
|
23
|
+
checkSpend(deps.ledger, cost, deps.now());
|
|
24
|
+
let kept = false;
|
|
25
|
+
try {
|
|
26
|
+
const result = await work();
|
|
27
|
+
kept = true;
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
finally {
|
|
31
|
+
addLine(deps.ledger, { at: deps.now().toISOString(), model, name, cost, kept });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** Makes one picture from a brief: fal.ai, then unfake.js, then the DUEL palette. Gives back the picture's path. */
|
|
35
|
+
export async function makeArt(briefPath, deps) {
|
|
36
|
+
const brief = readBrief(briefPath);
|
|
37
|
+
const { id, cost } = MODELS.sprite;
|
|
38
|
+
const seed = brief.seed ?? deps.seed();
|
|
39
|
+
return paidCall(deps, id, cost, brief.name, async () => {
|
|
40
|
+
const made = await falPicture(deps, id, {
|
|
41
|
+
prompt: brief.prompt,
|
|
42
|
+
seed,
|
|
43
|
+
image_size: { width: SIZE, height: SIZE },
|
|
44
|
+
output_format: 'png',
|
|
45
|
+
enable_prompt_expansion: false,
|
|
46
|
+
});
|
|
47
|
+
const cleaned = await deps.clean(made.png);
|
|
48
|
+
const picture = toPalette(cleaned);
|
|
49
|
+
const folder = brief.folder ?? '.';
|
|
50
|
+
mkdirSync(folder, { recursive: true });
|
|
51
|
+
const path = join(folder, `${brief.name}.png`);
|
|
52
|
+
writeFileSync(path, encodePng(picture.width, picture.height, picture.rgba));
|
|
53
|
+
writeSidecar(path, {
|
|
54
|
+
prompt: brief.prompt,
|
|
55
|
+
model: id,
|
|
56
|
+
seed: made.seed,
|
|
57
|
+
madeAt: deps.now().toISOString(),
|
|
58
|
+
steps: [
|
|
59
|
+
`fal.ai ${id}, ${SIZE} by ${SIZE}`,
|
|
60
|
+
`unfake.js ${UNFAKE.commit.slice(0, 7)}: ${cleaned.scale} times too big, made ${picture.width} by ${picture.height}`,
|
|
61
|
+
`DUEL palette (${DUEL_SOURCE})`,
|
|
62
|
+
],
|
|
63
|
+
cost,
|
|
64
|
+
});
|
|
65
|
+
return path;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
const REIMAGINE = 'Reimagine this pixel-art picture: keep its subject, pose and colours, and make it read clearly at a small size.';
|
|
69
|
+
/** The review step: fal.ai reimagines a picture, and its answer is saved beside it as `<name>.reimagined.png`. */
|
|
70
|
+
export async function reviewArt(picturePath, deps, prompt = REIMAGINE) {
|
|
71
|
+
const { id, cost } = MODELS.reimagine;
|
|
72
|
+
const name = basename(picturePath).replace(/\.png$/i, '');
|
|
73
|
+
const seed = deps.seed();
|
|
74
|
+
return paidCall(deps, id, cost, `${name} (review)`, async () => {
|
|
75
|
+
const dataUri = `data:image/png;base64,${readFileSync(picturePath).toString('base64')}`;
|
|
76
|
+
const made = await falPicture(deps, id, {
|
|
77
|
+
prompt,
|
|
78
|
+
seed,
|
|
79
|
+
image_urls: [dataUri],
|
|
80
|
+
image_size: { width: SIZE, height: SIZE },
|
|
81
|
+
output_format: 'png',
|
|
82
|
+
enable_prompt_expansion: false,
|
|
83
|
+
});
|
|
84
|
+
const path = join(dirname(picturePath), `${name}.reimagined.png`);
|
|
85
|
+
writeFileSync(path, made.png);
|
|
86
|
+
const sidecar = {
|
|
87
|
+
prompt,
|
|
88
|
+
model: id,
|
|
89
|
+
seed: made.seed,
|
|
90
|
+
madeAt: deps.now().toISOString(),
|
|
91
|
+
steps: [`fal.ai ${id}, from ${basename(picturePath)}`],
|
|
92
|
+
cost,
|
|
93
|
+
};
|
|
94
|
+
writeSidecar(path, sidecar);
|
|
95
|
+
return path;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const SIZE = 768;
|
|
2
|
+
export declare const MODELS: {
|
|
3
|
+
readonly sprite: {
|
|
4
|
+
readonly id: 'blackforestlabs/flux-3/klein/text-to-image';
|
|
5
|
+
readonly cost: 0.008;
|
|
6
|
+
};
|
|
7
|
+
readonly reimagine: {
|
|
8
|
+
readonly id: 'blackforestlabs/flux-3/klein/edit-image';
|
|
9
|
+
readonly cost: 0.008;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// The two fal.ai models, picked from https://fal.ai/api/models on 2026-11-27, with the price its list gives for one
|
|
2
|
+
// 768 by 768 picture. Both are the same family, so a reimagined picture looks like the sprite it came from.
|
|
3
|
+
export const SIZE = 768;
|
|
4
|
+
export const MODELS = {
|
|
5
|
+
sprite: { id: 'blackforestlabs/flux-3/klein/text-to-image', cost: 0.008 },
|
|
6
|
+
reimagine: { id: 'blackforestlabs/flux-3/klein/edit-image', cost: 0.008 },
|
|
7
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// The DUEL palette, 256 colours, in the order Lospec lists them. A cleaned picture keeps only these.
|
|
2
|
+
export const DUEL_SOURCE = 'https://lospec.com/palette-list/duel';
|
|
3
|
+
export const DUEL = `
|
|
4
|
+
000000 222323 434549 626871 828b98 a6aeba cdd2da f5f7fa 625d54 857565 9e8c79 aea189 bbafa4 ccc3b1 eadbc9 fff3d6
|
|
5
|
+
583126 733d3b 885041 9a624c ad6e51 d58d6b fbaa84 ffce7f 002735 003850 004d5e 0b667f 006f89 328ca7 24aed6 88d6ff
|
|
6
|
+
662b29 94363a b64d46 cd5e46 e37840 f99b4e ffbc4e ffe949 282b4a 3a4568 615f84 7a7799 8690b2 96b2d9 c7d6ff c6ecff
|
|
7
|
+
002219 003221 174a1b 225918 2f690c 518822 7da42d a6cc34 181f2f 23324d 25466b 366b8a 318eb8 41b2e3 52d2ff 74f5fd
|
|
8
|
+
1a332c 2f3f38 385140 325c40 417455 498960 55b67d 91daa1 5e0711 82211d b63c35 e45c5f ff7676 ff9ba8 ffbbc7 ffdbff
|
|
9
|
+
2d3136 48474d 5b5c69 73737f 848795 abaebe bac7db ebf0f6 3b303c 5a3c45 8a5258 ae6b60 c7826c d89f75 ecc581 fffaab
|
|
10
|
+
31222a 4a353c 5e4646 725a51 7e6c54 9e8a6e c0a588 ddbf9a 2e1026 49283d 663659 975475 b96d91 c178aa db99bf f8c6da
|
|
11
|
+
002e49 004051 005162 006b6d 008279 00a087 00bfa3 00deda 453125 614a3c 7e6144 997951 b29062 cca96e e8cb82 fbeaa3
|
|
12
|
+
5f0926 6e2434 904647 a76057 bd7d64 ce9770 edb67c edd493 323558 4a5280 64659d 7877c1 8e8ce2 9c9bef b8aeff dcd4ff
|
|
13
|
+
431729 712b3b 9f3b52 d94a69 f85d80 ff7daf ffa6c5 ffcdff 49251c 633432 7c4b47 98595a ac6f6e c17e7a d28d7a e59a7c
|
|
14
|
+
202900 2f4f08 495d00 617308 7c831e 969a26 b4aa33 d0cc32 622a00 753b09 854f12 9e6520 ba882e d1aa39 e8d24b fff64f
|
|
15
|
+
26233d 3b3855 56506f 75686e 917a7b b39783 cfaf8e fedfb1 1d2c43 2e3d47 394d3c 4c5f33 58712c 6b842d 789e24 7fbd39
|
|
16
|
+
372423 53393a 784c49 945d4f a96d58 bf7e63 d79374 f4a380 2d4b47 47655a 5b7b69 71957d 87ae8e 8ac196 a9d1c1 e0faeb
|
|
17
|
+
001b40 03315f 07487c 105da2 1476c0 4097ea 55b1f1 6dccff 554769 765d73 977488 b98c93 d5a39a ebbd9d ffd59b fdf786
|
|
18
|
+
1d1d21 3c3151 584a7f 7964ba 9585f1 a996ec baabf7 d1bdfe 262450 28335d 2d3d72 3d5083 5165ae 5274c5 6c82c4 8393c3
|
|
19
|
+
492129 5e414a 77535b 91606a ad7984 b58b94 d4aeaa ffe2cf 721c03 9c3327 bf5a3e e98627 ffb108 ffcf05 fff02b f7f4bf
|
|
20
|
+
`
|
|
21
|
+
.trim()
|
|
22
|
+
.split(/\s+/);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** A picture as its size and its pixels, four bytes (red, green, blue, alpha) each. */
|
|
2
|
+
export type Picture = {
|
|
3
|
+
width: number;
|
|
4
|
+
height: number;
|
|
5
|
+
rgba: Uint8Array;
|
|
6
|
+
};
|
|
7
|
+
/** Each pixel becomes the nearest palette colour, fully solid, or fully clear (all zeros). */
|
|
8
|
+
export declare function toPalette(picture: Picture, palette?: readonly string[]): Picture;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { DUEL } from './duel.js';
|
|
2
|
+
/** Below this alpha a pixel is see-through; at or above it, solid. Pixel art has no half-clear pixels. */
|
|
3
|
+
const SOLID = 128;
|
|
4
|
+
const rgb = (hex) => [
|
|
5
|
+
Number.parseInt(hex.slice(0, 2), 16),
|
|
6
|
+
Number.parseInt(hex.slice(2, 4), 16),
|
|
7
|
+
Number.parseInt(hex.slice(4, 6), 16),
|
|
8
|
+
];
|
|
9
|
+
// "Redmean" distance: close to how people see colour, and cheap.
|
|
10
|
+
function distance(a, b) {
|
|
11
|
+
const r = ((a[0] ?? 0) + (b[0] ?? 0)) / 2;
|
|
12
|
+
const dr = (a[0] ?? 0) - (b[0] ?? 0);
|
|
13
|
+
const dg = (a[1] ?? 0) - (b[1] ?? 0);
|
|
14
|
+
const db = (a[2] ?? 0) - (b[2] ?? 0);
|
|
15
|
+
return (2 + r / 256) * dr * dr + 4 * dg * dg + (2 + (255 - r) / 256) * db * db;
|
|
16
|
+
}
|
|
17
|
+
/** Each pixel becomes the nearest palette colour, fully solid, or fully clear (all zeros). */
|
|
18
|
+
export function toPalette(picture, palette = DUEL) {
|
|
19
|
+
const colours = palette.map(rgb);
|
|
20
|
+
const nearest = new Map();
|
|
21
|
+
const out = new Uint8Array(picture.rgba.length);
|
|
22
|
+
for (let i = 0; i < picture.rgba.length; i += 4) {
|
|
23
|
+
if ((picture.rgba[i + 3] ?? 0) < SOLID)
|
|
24
|
+
continue;
|
|
25
|
+
const px = [picture.rgba[i] ?? 0, picture.rgba[i + 1] ?? 0, picture.rgba[i + 2] ?? 0];
|
|
26
|
+
const key = (px[0] << 16) | (px[1] << 8) | px[2];
|
|
27
|
+
let best = nearest.get(key);
|
|
28
|
+
if (!best) {
|
|
29
|
+
best = colours.reduce((a, b) => (distance(px, b) < distance(px, a) ? b : a));
|
|
30
|
+
nearest.set(key, best);
|
|
31
|
+
}
|
|
32
|
+
out.set([...best, 255], i);
|
|
33
|
+
}
|
|
34
|
+
return { width: picture.width, height: picture.height, rgba: out };
|
|
35
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** What made a picture, kept beside it as `<name>.art.json`, so it can be made again or traced. */
|
|
2
|
+
export type Sidecar = {
|
|
3
|
+
prompt: string;
|
|
4
|
+
model: string;
|
|
5
|
+
seed: number;
|
|
6
|
+
madeAt: string;
|
|
7
|
+
steps: string[];
|
|
8
|
+
cost: number;
|
|
9
|
+
};
|
|
10
|
+
/** The sidecar's path for a picture: `hero.png` keeps its story in `hero.art.json`. */
|
|
11
|
+
export declare function sidecarPath(picturePath: string): string;
|
|
12
|
+
/** What is missing or wrong in a sidecar, one sentence each; empty when it is complete. */
|
|
13
|
+
export declare function sidecarProblems(value: unknown): string[];
|
|
14
|
+
/** Writes the sidecar beside the picture and gives back its path. */
|
|
15
|
+
export declare function writeSidecar(picturePath: string, sidecar: Sidecar): string;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { writeFileSync } from 'node:fs';
|
|
2
|
+
const fields = ['prompt', 'model', 'seed', 'madeAt', 'steps', 'cost'];
|
|
3
|
+
/** The sidecar's path for a picture: `hero.png` keeps its story in `hero.art.json`. */
|
|
4
|
+
export function sidecarPath(picturePath) {
|
|
5
|
+
return picturePath.replace(/\.png$/i, '') + '.art.json';
|
|
6
|
+
}
|
|
7
|
+
/** What is missing or wrong in a sidecar, one sentence each; empty when it is complete. */
|
|
8
|
+
export function sidecarProblems(value) {
|
|
9
|
+
if (typeof value !== 'object' || value === null)
|
|
10
|
+
return ['The sidecar is not an object.'];
|
|
11
|
+
const v = value;
|
|
12
|
+
const problems = fields.filter((f) => !(f in v)).map((f) => `The sidecar has no "${f}".`);
|
|
13
|
+
if (typeof v['prompt'] !== 'string' || v['prompt'] === '')
|
|
14
|
+
problems.push('"prompt" must be words.');
|
|
15
|
+
if (typeof v['model'] !== 'string' || v['model'] === '')
|
|
16
|
+
problems.push('"model" must name a model.');
|
|
17
|
+
if (!Number.isInteger(v['seed']))
|
|
18
|
+
problems.push('"seed" must be a whole number.');
|
|
19
|
+
if (typeof v['madeAt'] !== 'string' || Number.isNaN(Date.parse(v['madeAt'])))
|
|
20
|
+
problems.push('"madeAt" must be a time.');
|
|
21
|
+
if (!Array.isArray(v['steps']) || v['steps'].length === 0)
|
|
22
|
+
problems.push('"steps" must list what was done.');
|
|
23
|
+
if (typeof v['cost'] !== 'number' || v['cost'] < 0)
|
|
24
|
+
problems.push('"cost" must be dollars, 0 or more.');
|
|
25
|
+
return problems;
|
|
26
|
+
}
|
|
27
|
+
/** Writes the sidecar beside the picture and gives back its path. */
|
|
28
|
+
export function writeSidecar(picturePath, sidecar) {
|
|
29
|
+
const problems = sidecarProblems(sidecar);
|
|
30
|
+
if (problems.length > 0)
|
|
31
|
+
throw new Error(problems.join(' '));
|
|
32
|
+
const path = sidecarPath(picturePath);
|
|
33
|
+
writeFileSync(path, JSON.stringify(sidecar, null, 2) + '\n');
|
|
34
|
+
return path;
|
|
35
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { clean, UNFAKE } from './features/clean/clean.ts';
|
|
2
|
+
export { DUEL, DUEL_SOURCE } from './features/palette/duel.ts';
|
|
3
|
+
export { toPalette, type Picture } from './features/palette/palette.ts';
|
|
4
|
+
export { type Sidecar, sidecarProblems } from './features/sidecar/sidecar.ts';
|
|
5
|
+
export { MONTH_CAP, monthSpend, type LedgerLine } from './features/make/ledger.ts';
|
|
6
|
+
export { MODELS } from './features/make/models.ts';
|
|
7
|
+
export { type Brief, makeArt, reviewArt } from './features/make/make.ts';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Opt-in and build time only: turns AI-made pictures into clean DUEL-palette pixel art. A game never loads this,
|
|
2
|
+
// which matters because this package is GPL-3.0-or-later (its unfake.js core includes libimagequant).
|
|
3
|
+
export { clean, UNFAKE } from './features/clean/clean.js';
|
|
4
|
+
export { DUEL, DUEL_SOURCE } from './features/palette/duel.js';
|
|
5
|
+
export { toPalette } from './features/palette/palette.js';
|
|
6
|
+
export { sidecarProblems } from './features/sidecar/sidecar.js';
|
|
7
|
+
export { MONTH_CAP, monthSpend } from './features/make/ledger.js';
|
|
8
|
+
export { MODELS } from './features/make/models.js';
|
|
9
|
+
export { makeArt, reviewArt } from './features/make/make.js';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** The CRC-32 every PNG chunk ends with. */
|
|
2
|
+
export declare function crc32(bytes: Uint8Array): number;
|
|
3
|
+
/** An 8-bit RGBA PNG of `rgba` (four bytes a pixel, rows top to bottom). */
|
|
4
|
+
export declare function encodePng(width: number, height: number, rgba: Uint8Array): Uint8Array;
|
package/dist/lib/png.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// A PNG writer: the one image format the art command writes. Node's zlib does the packing, so no PNG library.
|
|
2
|
+
import { deflateSync } from 'node:zlib';
|
|
3
|
+
const table = Array.from({ length: 256 }, (_, n) => {
|
|
4
|
+
let c = n;
|
|
5
|
+
for (let k = 0; k < 8; k++)
|
|
6
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
7
|
+
return c >>> 0;
|
|
8
|
+
});
|
|
9
|
+
/** The CRC-32 every PNG chunk ends with. */
|
|
10
|
+
export function crc32(bytes) {
|
|
11
|
+
let c = 0xffffffff;
|
|
12
|
+
for (const b of bytes)
|
|
13
|
+
c = (table[(c ^ b) & 0xff] ?? 0) ^ (c >>> 8);
|
|
14
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
15
|
+
}
|
|
16
|
+
function chunk(type, data) {
|
|
17
|
+
const out = new Uint8Array(12 + data.length);
|
|
18
|
+
const view = new DataView(out.buffer);
|
|
19
|
+
view.setUint32(0, data.length);
|
|
20
|
+
out.set(new TextEncoder().encode(type), 4);
|
|
21
|
+
out.set(data, 8);
|
|
22
|
+
view.setUint32(8 + data.length, crc32(out.subarray(4, 8 + data.length)));
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
/** An 8-bit RGBA PNG of `rgba` (four bytes a pixel, rows top to bottom). */
|
|
26
|
+
export function encodePng(width, height, rgba) {
|
|
27
|
+
const header = new Uint8Array(13);
|
|
28
|
+
const view = new DataView(header.buffer);
|
|
29
|
+
view.setUint32(0, width);
|
|
30
|
+
view.setUint32(4, height);
|
|
31
|
+
header.set([8, 6, 0, 0, 0], 8);
|
|
32
|
+
// Each row starts with filter type 0 (none).
|
|
33
|
+
const rows = new Uint8Array(height * (width * 4 + 1));
|
|
34
|
+
for (let y = 0; y < height; y++)
|
|
35
|
+
rows.set(rgba.subarray(y * width * 4, (y + 1) * width * 4), y * (width * 4 + 1) + 1);
|
|
36
|
+
const parts = [
|
|
37
|
+
new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]),
|
|
38
|
+
chunk('IHDR', header),
|
|
39
|
+
chunk('IDAT', deflateSync(rows)),
|
|
40
|
+
chunk('IEND', new Uint8Array()),
|
|
41
|
+
];
|
|
42
|
+
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
43
|
+
let at = 0;
|
|
44
|
+
for (const p of parts) {
|
|
45
|
+
out.set(p, at);
|
|
46
|
+
at += p.length;
|
|
47
|
+
}
|
|
48
|
+
return out;
|
|
49
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@automataframework/art",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.1-preview.12.sha-e1e5dd9",
|
|
4
|
+
"description": "Opt-in, build time only: turns AI-made pictures into clean pixel art in the DUEL palette. Formats may change.",
|
|
5
|
+
"license": "GPL-3.0-or-later",
|
|
6
|
+
"author": "McThayDT",
|
|
7
|
+
"homepage": "https://automataframework.com",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/AutomataFramework/AutomataFramework.git",
|
|
11
|
+
"directory": "packages/art"
|
|
12
|
+
},
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public",
|
|
15
|
+
"provenance": true
|
|
16
|
+
},
|
|
17
|
+
"type": "module",
|
|
18
|
+
"bin": {
|
|
19
|
+
"make-art": "dist/bin.js"
|
|
20
|
+
},
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"import": "./dist/index.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"vendor",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"playwright-core": "1.65.1"
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=24"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsc -p tsconfig.build.json"
|
|
40
|
+
}
|
|
6
41
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 unfake.js team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# unfake.js, copied in
|
|
2
|
+
|
|
3
|
+
From https://github.com/jenissimo/unfake.js at commit 663686899e8394dfda25efd7f2a947f008c6f676 (v1.4.0, 2026-09-29),
|
|
4
|
+
copied by hand, never installed from npm or git. Only the files the pixel-art path loads are here, unchanged:
|
|
5
|
+
`lib/{pixel,utils,core-client,core-codec,cvfree,edge-detect}.js`, `workers/unfake-core-worker.js` and the built core
|
|
6
|
+
`vendor/unfake-core/unfake_wasm.js` with `unfake_wasm_bg.wasm`. They load nothing else: no other library and nothing from
|
|
7
|
+
the web. unfake.js is MIT (`LICENSE`). Its built core includes the Rust crate `imagequant` 4.4.1, which is
|
|
8
|
+
GPL-3.0-or-later; its source is at the commit above and at https://github.com/ImageOptim/libimagequant. So
|
|
9
|
+
`@automataframework/art` as a whole is GPL-3.0-or-later (`../../LICENSE`), the Owner's choice on 2026-11-27.
|
|
10
|
+
|
|
11
|
+
sha256 of each file as copied:
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
b43ed71072c8ee6e573757f71afaa93835803e4cccf948b72bb6f61db29a2859 LICENSE
|
|
15
|
+
0fd91495d5d42acc988689e00b647509035d5e78b423d21437c616995e6cea66 lib/core-client.js
|
|
16
|
+
0cb36bf54203e7898a5f12b615b6ff709893c6cd438e263ea2b39628bee23b87 lib/core-codec.js
|
|
17
|
+
abdec424c23ce91e9fc786b87bf427e431107b1dab9c5a1103e3f7f3ce530348 lib/cvfree.js
|
|
18
|
+
ae297c726ed009ecb2a32f4bff65414c70d22d02367ba42862282105edfbc485 lib/edge-detect.js
|
|
19
|
+
61633e78675707f6d92a0d928f03dd8da0eff04e8766d1850587d89a995db437 lib/pixel.js
|
|
20
|
+
ca512495e47dc23ff3cb25939c2f556cfee8bbb9fce63e7c1c5248edc7e60024 lib/utils.js
|
|
21
|
+
b9839427747d8a5ccbe0e1e2db6e6bfb308425d641913b8b42dc59e4cea52f55 vendor/unfake-core/unfake_wasm.js
|
|
22
|
+
53f3e0e4c2f7897495183b8e1e75ae420671ed1b8ff6c3ffb4dd6ffedbf9f432 vendor/unfake-core/unfake_wasm_bg.wasm
|
|
23
|
+
3fa068cd372e6e9b0ec65d7e88b5ea3f9adc0ee034259836c4ff03e7902d4f26 workers/unfake-core-worker.js
|
|
24
|
+
```
|