AI media harness for planning, generating, and editing locally
TypeScript
Add reference icon generation tooling
Bitti774574118643
diff --git a/README.md b/README.mdindex 222371e..3a55c15 100644--- a/README.md+++ b/README.md@@ -55,3 +55,11 @@ My project/`polstudio.json` is schema-versioned and contains the creative brief, render settings, asset metadata, and timeline structure. The Rust shell validates it before reads and writes; the web build uses validated local-storage records as a development fallback.The web fallback seeds three deterministic showcase projects on first launch. Remove the `polstudio.web-projects.v1` local-storage entry to restore that initial demo library.++### Reference icons++`npm run reference-icons` renders every missing character, product, location, and style preset with MiniMax H3 at 30 steps. The generator reads the application catalogue directly, requests the model's minimum temporal span, and writes frame 0 directly as a high-quality 4:4:4 256×256 JPEG without creating an intermediate video or audio file. Character entries use controlled headshot lighting; product and location entries use a complete-subject view; style entries use a representative composition with lighting and rendering tailored to the named style.++VidFab, Cargo, and FFmpeg must be installed. The script uses the development paths under `D:\Projects\vidfab` by default; `VIDFAB_EXE`, `VIDFAB_TRANSFORMER`, `VIDFAB_TEXT_ENCODER`, `VIDFAB_VIDEO_VAE`, `VIDFAB_AUDIO_VAE`, `VIDFAB_BACKEND`, `CARGO_EXE`, and `FFMPEG_EXE` can override them. Use `node scripts/generate-reference-icons.mjs --id=<preset-id> --force` to regenerate one entry.++The batch defaults to two GPU workers. Set `REFERENCE_ICON_WORKERS=1` or pass `--workers=1` on a lower-memory card. Use `--type=style` to generate or replace only style icons; `--shard=1/2` selects the first alternating half for a resumable recovery pass.diff --git a/package.json b/package.jsonindex eb0be8a..13d3bb0 100644--- a/package.json+++ b/package.json@@ -9,6 +9,7 @@"preview": "vite preview","test": "vitest run","icons": "node scripts/make-icons.mjs",+ "reference-icons": "node scripts/generate-reference-icons.mjs","tauri": "tauri"},"dependencies": {diff --git a/scripts/generate-reference-icons.mjs b/scripts/generate-reference-icons.mjsnew file mode 100644index 0000000..f849766--- /dev/null+++ b/scripts/generate-reference-icons.mjs@@ -0,0 +1,300 @@+import { spawn, spawnSync } from "node:child_process";+import { createHash } from "node:crypto";+import { access, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";+import { tmpdir } from "node:os";+import { dirname, join, resolve } from "node:path";+import { fileURLToPath } from "node:url";++const scriptDirectory = dirname(fileURLToPath(import.meta.url));+const repository = resolve(scriptDirectory, "..");+const outputDirectory = join(repository, "src", "assets", "reference-icons");++const defaults = {+ executable: "D:\\Projects\\vidfab\\build\\Release\\vidfab.exe",+ transformer: "D:\\Projects\\vidfab\\weights\\transformer\\MiniMax_H3_FL2VA_pruned_nvfp4.safetensors",+ textEncoder: "D:\\Projects\\vidfab\\weights\\text_encoder\\qwen3vl_32b_minimax_h3_nvfp4_awq.safetensors",+ videoVae: "D:\\Projects\\vidfab\\weights\\vae\\video_vae_nf4.safetensors",+ audioVae: "D:\\Projects\\vidfab\\weights\\vae\\audio_vae_nf4.safetensors",+ cargo: "C:\\Users\\NN\\.cargo\\bin\\cargo.exe",+};++const settings = {+ executable: process.env.VIDFAB_EXE || defaults.executable,+ transformer: process.env.VIDFAB_TRANSFORMER || defaults.transformer,+ textEncoder: process.env.VIDFAB_TEXT_ENCODER || defaults.textEncoder,+ videoVae: process.env.VIDFAB_VIDEO_VAE || defaults.videoVae,+ audioVae: process.env.VIDFAB_AUDIO_VAE || defaults.audioVae,+ cargo: process.env.CARGO_EXE || defaults.cargo,+ ffmpeg: process.env.FFMPEG_EXE || "ffmpeg",+};++const force = process.argv.includes("--force");+const dryRun = process.argv.includes("--dry-run");+const requestedId = process.argv.find((argument) => argument.startsWith("--id="))?.slice(5);+const requestedType = process.argv.find((argument) => argument.startsWith("--type="))?.slice(7);+const requestedShard = process.argv.find((argument) => argument.startsWith("--shard="))?.slice(8);+const requestedLimit = Number(process.argv.find((argument) => argument.startsWith("--limit="))?.slice(8) || 0);+const requestedWorkers = Number(process.argv.find((argument) => argument.startsWith("--workers="))?.slice(10) || process.env.REFERENCE_ICON_WORKERS || 2);++function run(command, args, options = {}) {+ return new Promise((accept, reject) => {+ const child = spawn(command, args, { stdio: "inherit", windowsHide: true, ...options });+ child.once("error", reject);+ child.once("exit", (code, signal) => {+ if (code === 0) accept();+ else reject(new Error(`${command} stopped with ${signal ? `signal ${signal}` : `exit code ${code}`}.`));+ });+ });+}++function runQuiet(command, args, options = {}) {+ return new Promise((accept, reject) => {+ const child = spawn(command, args, { stdio: ["ignore", "pipe", "pipe"], windowsHide: true, ...options });+ let output = "";+ child.stdout.on("data", (chunk) => { output = `${output}${chunk}`.slice(-12_000); });+ child.stderr.on("data", (chunk) => { output = `${output}${chunk}`.slice(-12_000); });+ child.once("error", reject);+ child.once("exit", (code, signal) => {+ if (code === 0) accept();+ else reject(new Error(`${command} stopped with ${signal ? `signal ${signal}` : `exit code ${code}`}.\n${output}`));+ });+ });+}++function loadCatalog() {+ const runner = join(repository, "node_modules", "vite-node", "vite-node.mjs");+ const helper = join(scriptDirectory, "reference-icon-catalog.ts");+ const result = spawnSync(process.execPath, [runner, helper], {+ cwd: repository,+ encoding: "utf8",+ maxBuffer: 10 * 1024 * 1024,+ windowsHide: true,+ });+ if (result.status !== 0) throw new Error(`Could not load the reference catalogue.\n${result.stderr || result.stdout}`);+ return JSON.parse(result.stdout);+}++function backend() {+ if (process.env.VIDFAB_BACKEND === "cuda" || process.env.VIDFAB_BACKEND === "vulkan") {+ return process.env.VIDFAB_BACKEND;+ }+ const devices = spawnSync(settings.executable, ["devices"], { encoding: "utf8", windowsHide: true });+ return devices.status === 0 && /^device\s+\d+/m.test(devices.stdout || "") ? "cuda" : "vulkan";+}++function stylePrompt(entry) {+ const tailoredSubject = {+ "Architectural photography": "a distinctive modern pavilion exterior",+ "Editorial photography": "a thoughtful person in a lived-in interior",+ "Fashion photography": "a full-length fashion model wearing a structured contemporary outfit",+ "Food photography": "a carefully plated colorful meal",+ "Macro photography": "the intricate center of a small flower",+ "Product photography": "an unbranded sculptural ceramic bottle",+ "Street photography": "a pedestrian crossing a layered urban street",+ "Studio portrait": "a single person posed against a seamless backdrop",+ "Travel photography": "a traveler overlooking a dramatic mountain landscape",+ "Wildlife photography": "a small wild animal in its natural habitat",+ "Automotive campaign": "a modern performance car on a clean architectural road",+ "Beauty campaign": "a close beauty portrait with luminous natural skin",+ "Clean product ad": "an unbranded sculptural ceramic bottle on a simple pedestal",+ "Fashion lookbook": "a full-length model wearing a complete contemporary outfit",+ "Food commercial": "a freshly prepared colorful hero dish",+ "Lifestyle campaign": "a relaxed person enjoying an aspirational outdoor moment",+ "Luxury editorial": "an elegantly dressed model with one refined accessory",+ "Sports campaign": "an athlete in motion with one piece of sports equipment",+ "Technology launch": "a sleek unbranded handheld electronic device on a luminous pedestal",+ "Travel campaign": "a traveler within a striking destination landscape",+ "Motion graphics": "bold abstract geometric forms arranged around one clear focal shape",+ "UI motion design": "a layered futuristic interface of cards, controls and data shapes",+ }[entry.name];+ const scene = tailoredSubject ?? {+ Animation: "an expressive human adventurer in a dynamic pose within a simple atmospheric environment",+ Cinematic: "a lone traveler in an atmospheric city street shaped by dramatic architectural light",+ Photography: "one clear portfolio subject",+ Illustration: "a welcoming greenhouse storefront with one person and abundant plants",+ "Fine art": "a solitary figure in a textured landscape with a strong foreground and distant horizon",+ Graphic: "a bold text-free cultural poster composition built from a clear central motif and balanced geometric forms",+ Retro: "a stylish traveler beside a compact car in a period city setting",+ Experimental: "a human portrait integrated with simple architectural forms",+ Commercial: "one clear aspirational campaign subject",+ Digital: "a small futuristic pavilion with one stylized human figure for scale",+ }[entry.subcategory];+ if (!scene) throw new Error(`Style icon '${entry.id}' has unsupported subcategory '${entry.subcategory}'.`);+ const treatment = {+ Animation: `${entry.subject} production frame, entirely animated with unmistakable medium-specific character rendering and environment design, not live-action photography`,+ Cinematic: `${entry.subject} film still with unmistakable genre-specific cinematography, production design, color grade and lighting`,+ Photography: `professional ${entry.subject} portfolio image with discipline-specific lens choice, framing and lighting`,+ Illustration: `${entry.subject}, entirely hand-rendered illustration with prominent medium-specific marks, edges and surface texture, not photography`,+ "Fine art": `${entry.subject} artwork on a physical surface with prominent handmade medium texture, expressive marks and visible material character, not photography`,+ Graphic: `${entry.subject} graphic artwork with deliberate flat shapes, typography-free hierarchy and print-ready composition`,+ Retro: `authentic ${entry.subject} image with unmistakable period design, color treatment, wardrobe and capture artifacts`,+ Experimental: `${entry.subject} artwork with the named optical or mixed-media effect unmistakably dominating the entire frame`,+ Commercial: `polished ${entry.subject} campaign image with premium art direction, controlled highlights and a clean hero composition`,+ Digital: `${entry.subject}, entirely computer-rendered with unmistakable technique-specific geometry, surfaces and lighting rather than ordinary photography`,+ }[entry.subcategory];+ return `${treatment}. Subject: ${scene}. Preserve a clear focal point, refined detail and strong visual hierarchy`;+}++function promptFor(entry) {+ const description = {+ character: `a tight professional headshot portrait of ${entry.subject}, head and shoulders fully visible, facing the camera, natural neutral expression, eyes tack-sharp with clean symmetrical catchlights, realistic skin texture, flattering three-point portrait lighting with a large soft key at 45 degrees, gentle fill, subtle hair light and controlled highlights, simple light gray seamless studio background`,+ product: `a professional full product photograph of ${entry.subject}, the entire product fully visible and centered, accurate geometry and materials, tack-sharp edges and fine surface detail, large diffused softbox key light, clean fill and precise rim separation, controlled reflections, simple light gray seamless studio background`,+ location: `a comprehensive establishing photograph of ${entry.subject}, the full location clearly visible, balanced natural cinematic illumination, crisp foreground, midground and background detail, realistic materials, clean color separation, broad dynamic range with protected highlights and open shadows`,+ style: stylePrompt(entry),+ }[entry.type];+ if (!description) throw new Error(`Reference icon '${entry.id}' has unsupported type '${entry.type}'.`);+ const visualDirection = entry.type === "style"+ ? `A polished square visual style reference, ${description}, the entire frame consistently uses the requested visual treatment, high fidelity`+ : `Live-action, high-end commercial reference photography, ${description}, refined natural tonality, high image fidelity`;+ return [+ `integrated_multimodal_description: [Shot 1] ${visualDirection}, square composition, static camera, no movement, no blur, no distortion, no text, no logo, no watermark, no border.`,+ "",+ "overall_soundscape: N/A",+ "",+ "non_diegetic_music: N/A",+ ].join("\n");+}++function stableSeed(id) {+ return Number.parseInt(createHash("sha256").update(id).digest("hex").slice(0, 8), 16);+}++async function generate(entry, temporaryDirectory, inferenceBackend) {+ const destination = join(outputDirectory, `${entry.id}.jpg`);+ const promptPath = join(temporaryDirectory, `${entry.id}.txt`);+ const rawBase = join(temporaryDirectory, `${entry.id}.mp4`);+ const y4mPath = join(temporaryDirectory, `${entry.id}.y4m`);+ await writeFile(promptPath, promptFor(entry), "utf8");++ const args = [+ "generate",+ "--prompt-file", promptPath,+ "--out", rawBase,+ "--raw",+ "--resolution", "256x256",+ // H3's decoder cannot accept one temporal frame. Six is the smallest legal+ // request and resolves to 22; only frame zero is retained below.+ "--frames", "6",+ "--steps", "30",+ "--seed", String(stableSeed(entry.id)),+ "--inference-backend", inferenceBackend,+ "--attention", inferenceBackend === "cuda" ? "sage2" : "exact",+ "--transformer", settings.transformer,+ "--text-encoder", settings.textEncoder,+ "--vae", settings.videoVae,+ "--audio-vae", settings.audioVae,+ ];+ if (dryRun) args.push("--dry-run");++ if (dryRun) console.log(`\n[${entry.name}] MiniMax H3, 30 steps, ${inferenceBackend}`);+ await (dryRun ? run : runQuiet)(settings.executable, args, { cwd: repository });+ if (dryRun) return;+ await run(settings.ffmpeg, [+ "-hide_banner", "-loglevel", "error", "-y",+ "-i", y4mPath,+ "-frames:v", "1",+ "-vf", "scale=256:256:flags=lanczos",+ "-pix_fmt", "yuvj444p",+ "-q:v", "1",+ destination,+ ]);+}++const manifest = loadCatalog();+const seenIds = new Set();+for (const entry of manifest) {+ if (!entry || !/^[a-z0-9][a-z0-9-]*$/.test(entry.id ?? "")) {+ throw new Error("Every reference icon needs a path-safe lowercase id.");+ }+ if (seenIds.has(entry.id)) throw new Error(`Duplicate reference icon id '${entry.id}'.`);+ if (!entry.name?.trim() || !entry.subject?.trim()) throw new Error(`Reference icon '${entry.id}' needs a name and subject.`);+ if (!["character", "product", "location", "style"].includes(entry.type)) throw new Error(`Reference icon '${entry.id}' has unsupported type '${entry.type}'.`);+ seenIds.add(entry.id);+}+if (requestedType && !["character", "product", "location", "style"].includes(requestedType)) {+ throw new Error(`Unsupported reference icon type '${requestedType}'.`);+}+let entries = requestedId+ ? manifest.filter((entry) => entry.id === requestedId)+ : requestedType ? manifest.filter((entry) => entry.type === requestedType) : manifest;+if (requestedId && entries.length === 0) throw new Error(`No reference icon entry has id '${requestedId}'.`);+if (requestedShard) {+ const match = /^(\d+)\/(\d+)$/.exec(requestedShard);+ const shard = Number(match?.[1]);+ const total = Number(match?.[2]);+ if (!match || shard < 1 || total < 1 || shard > total) {+ throw new Error("--shard must use a one-based value such as --shard=1/2.");+ }+ entries = entries.filter((_, index) => index % total === shard - 1);+}+if (Number.isFinite(requestedLimit) && requestedLimit > 0) entries = entries.slice(0, requestedLimit);+await mkdir(outputDirectory, { recursive: true });+const temporaryDirectory = await mkdtemp(join(tmpdir(), "polstudio-reference-icons-"));+const inferenceBackend = backend();++try {+ const pending = [];+ for (const [index, entry] of entries.entries()) {+ const destination = join(outputDirectory, `${entry.id}.jpg`);+ if (!force) {+ try {+ await access(destination);+ console.log(`[${index + 1}/${entries.length}] ${entry.name}: already exists; skipping`);+ continue;+ } catch (error) {+ if (error?.code !== "ENOENT") throw error;+ }+ }+ pending.push(entry);+ }+ if (dryRun) {+ for (const entry of pending) await generate(entry, temporaryDirectory, inferenceBackend);+ } else if (pending.length > 0) {+ const specs = pending.map((entry) => ({+ id: entry.id,+ prompt: promptFor(entry),+ seed: stableSeed(entry.id),+ destination: join(outputDirectory, `${entry.id}.jpg`),+ }));+ await run(settings.cargo, [+ "build", "--quiet",+ "--manifest-path", join(repository, "src-tauri", "Cargo.toml"),+ "--bin", "reference_icons",+ ], { cwd: repository });+ const workerCount = Math.max(1, Math.min(specs.length, Number.isFinite(requestedWorkers) ? Math.floor(requestedWorkers) : 2));+ const batches = Array.from({ length: workerCount }, () => []);+ specs.forEach((spec, index) => batches[index % workerCount].push(spec));+ const executable = join(repository, "src-tauri", "target", "debug", "reference_icons.exe");+ const environment = {+ ...process.env,+ VIDFAB_EXE: settings.executable,+ VIDFAB_TRANSFORMER: settings.transformer,+ VIDFAB_TEXT_ENCODER: settings.textEncoder,+ VIDFAB_VIDEO_VAE: settings.videoVae,+ VIDFAB_BACKEND: inferenceBackend,+ };+ const batchPaths = await Promise.all(batches.map(async (batch, index) => {+ const batchPath = join(temporaryDirectory, `batch-${index + 1}.json`);+ await writeFile(batchPath, JSON.stringify(batch), "utf8");+ return batchPath;+ }));+ const runs = await Promise.allSettled(batchPaths.map(async (batchPath, index) => {+ // Optional extra workers are staggered to reduce aligned peak usage;+ // more than the two-worker default requires substantially more VRAM.+ if (workerCount > 2 && index > 0) {+ await new Promise((accept) => setTimeout(accept, index * 7_000));+ }+ await run(executable, [batchPath], {+ cwd: repository,+ env: environment,+ });+ }));+ const failedIndexes = runs.flatMap((result, index) => result.status === "rejected" ? [index] : []);+ for (const index of failedIndexes) {+ console.warn(`Reference icon worker ${index + 1} failed; retrying its batch with exclusive GPU access.`);+ await run(executable, [batchPaths[index]], { cwd: repository, env: environment });+ }+ }+} finally {+ await rm(temporaryDirectory, { recursive: true, force: true });+}diff --git a/scripts/reference-icon-catalog.ts b/scripts/reference-icon-catalog.tsnew file mode 100644index 0000000..9674cae--- /dev/null+++ b/scripts/reference-icon-catalog.ts@@ -0,0 +1,24 @@+import { KNOWN_CHARACTERS } from "../src/lib/known-characters";+import { REFERENCE_PRESETS } from "../src/lib/reference-presets";++const safeId = (value: string) => value.toLocaleLowerCase().normalize("NFKD")+ .replace(/[\u0300-\u036f]/g, "")+ .replace(/[^a-z0-9]+/g, "-")+ .replace(/(^-|-$)/g, "");++const knownSubjects = new Map(KNOWN_CHARACTERS.map(([name, actor, franchise], index) => [+ `character-known-${safeId(name)}-${index + 1}`,+ franchise === "Real Person / Celebrity" ? name : `${name} as portrayed by ${actor} in ${franchise}`,+]));++const catalog = REFERENCE_PRESETS+ .filter((preset) => preset.type === "character" || preset.type === "product" || preset.type === "location" || preset.type === "style")+ .map((preset) => ({+ id: preset.id,+ type: preset.type,+ subcategory: preset.subcategory,+ name: preset.name,+ subject: knownSubjects.get(preset.id) ?? preset.prompt.replace(/\.$/, ""),+ }));++process.stdout.write(JSON.stringify(catalog));diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lockindex e8074f5..dcb3a6e 100644--- a/src-tauri/Cargo.lock+++ b/src-tauri/Cargo.lock@@ -1688,6 +1688,12 @@ dependencies = ["syn 2.0.119",]+[[package]]+name = "jpeg-encoder"+version = "0.6.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "b454d911ac55068f53495488d8ccd0646eaa540c033a28ee15b07838afafb01f"+[[package]]name = "js-sys"version = "0.3.104"@@ -2345,6 +2351,7 @@ name = "pol-studio"version = "0.1.0"dependencies = ["getrandom 0.3.4",+ "jpeg-encoder","libloading 0.8.9","reqwest 0.12.28","serde",diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.tomlindex ad9e19e..bc57637 100644--- a/src-tauri/Cargo.toml+++ b/src-tauri/Cargo.toml@@ -23,6 +23,7 @@ tauri-plugin-dialog = "2"libloading = "0.8"getrandom = "0.3"reqwest = { version = "0.12", default-features = false, features = ["blocking", "json", "rustls-tls"] }+jpeg-encoder = "0.6"[target.'cfg(windows)'.dependencies]windows-sys = { version = "0.59", features = ["Win32_Foundation", "Win32_Storage_FileSystem"] }diff --git a/src-tauri/src/bin/reference_icons.rs b/src-tauri/src/bin/reference_icons.rsnew file mode 100644index 0000000..0455323--- /dev/null+++ b/src-tauri/src/bin/reference_icons.rs@@ -0,0 +1,33 @@+use pol_studio_lib::{generate_reference_icon_batch, ReferenceIconBatchConfig, ReferenceIconSpec};+use std::{env, fs, path::PathBuf};++fn required(name: &str) -> Result<String, String> {+ env::var(name).map_err(|_| format!("{name} is required."))+}++fn run() -> Result<(), String> {+ let manifest = env::args()+ .nth(1)+ .ok_or_else(|| "Pass the batch JSON path.".to_string())?;+ let specs: Vec<ReferenceIconSpec> = serde_json::from_slice(+ &fs::read(&manifest).map_err(|error| format!("Could not read '{manifest}': {error}"))?,+ )+ .map_err(|error| format!("Could not parse '{manifest}': {error}"))?;+ generate_reference_icon_batch(+ &specs,+ &ReferenceIconBatchConfig {+ dll_path: PathBuf::from(required("VIDFAB_EXE")?).with_file_name("vidfab.dll"),+ transformer: required("VIDFAB_TRANSFORMER")?.into(),+ text_encoder: required("VIDFAB_TEXT_ENCODER")?.into(),+ video_vae: required("VIDFAB_VIDEO_VAE")?.into(),+ backend: env::var("VIDFAB_BACKEND").unwrap_or_else(|_| "cuda".into()),+ },+ )+}++fn main() {+ if let Err(error) = run() {+ eprintln!("{error}");+ std::process::exit(1);+ }+}diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rsindex 61ffeca..a89bdb8 100644--- a/src-tauri/src/lib.rs+++ b/src-tauri/src/lib.rs@@ -15,6 +15,8 @@ mod export;mod rendered;mod vidfab;+pub use vidfab::{generate_reference_icon_batch, ReferenceIconBatchConfig, ReferenceIconSpec};+const PROJECT_FILE_NAME: &str = "polstudio.json";/// What the file was called before, newest first. Folders written by earlier/// builds still open; the next save writes `polstudio.json` and leaves the olddiff --git a/src-tauri/src/vidfab.rs b/src-tauri/src/vidfab.rsindex fe28f68..3fce7d8 100644--- a/src-tauri/src/vidfab.rs+++ b/src-tauri/src/vidfab.rs@@ -772,6 +772,140 @@ impl Drop for RequestHandle<'_> {}}+struct GenerationHandle<'a>(*mut ffi::Generation, &'a ffi::Api);+impl Drop for GenerationHandle<'_> {+ fn drop(&mut self) {+ self.1.destroy_generation(self.0);+ }+}++#[derive(Debug, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct ReferenceIconSpec {+ pub id: String,+ pub prompt: String,+ pub seed: u64,+ pub destination: PathBuf,+}++#[derive(Debug)]+pub struct ReferenceIconBatchConfig {+ pub dll_path: PathBuf,+ pub transformer: PathBuf,+ pub text_encoder: PathBuf,+ pub video_vae: PathBuf,+ pub backend: String,+}++fn write_reference_icon(+ api: &ffi::Api,+ configuration: &Configuration,+ platform: ComputePlatform,+ spec: &ReferenceIconSpec,+) -> Result<(), String> {+ let request = GenerationRequest {+ job_id: spec.id.clone(),+ prompt: spec.prompt.clone(),+ frames: 6,+ steps: 30,+ seed: i64::try_from(spec.seed)+ .map_err(|_| format!("Icon '{}' seed is too large.", spec.id))?,+ canvas_width: 256,+ canvas_height: 256,+ reference_paths: Vec::new(),+ };+ let handle = RequestHandle::new(api)?;+ configure_request(api, handle.0, &request, configuration, platform, true)?;+ api.resolve(handle.0)?;+ let generation = GenerationHandle(api.start(handle.0, None, std::ptr::null_mut())?, api);+ let status = api.wait(generation.0, -1);+ if status != 0 {+ let detail = api.generation_error(generation.0);+ return Err(if detail.is_empty() {+ format!("Icon '{}' failed with vidfab status {status}.", spec.id)+ } else {+ format!("Icon '{}' failed: {detail}", spec.id)+ });+ }+ let output = api.output(generation.0)?;+ if output.width != 256 || output.height != 256 || output.frames < 1 {+ return Err(format!(+ "Icon '{}' returned {}x{} with {} frames.",+ spec.id, output.width, output.height, output.frames+ ));+ }+ let rgba = api.frame_rgba8(generation.0, 0, 256, 256)?;+ let rgb = rgba+ .chunks_exact(4)+ .flat_map(|pixel| [pixel[0], pixel[1], pixel[2]])+ .collect::<Vec<_>>();+ if let Some(parent) = spec.destination.parent() {+ std::fs::create_dir_all(parent).map_err(|error| {+ format!(+ "Could not create icon folder '{}': {error}",+ parent.display()+ )+ })?;+ }+ let encoder = jpeg_encoder::Encoder::new_file(&spec.destination, 100).map_err(|error| {+ format!(+ "Could not create icon '{}': {error}",+ spec.destination.display()+ )+ })?;+ encoder+ .encode(&rgb, 256, 256, jpeg_encoder::ColorType::Rgb)+ .map_err(|error| {+ format!(+ "Could not encode icon '{}': {error}",+ spec.destination.display()+ )+ })+}++pub fn generate_reference_icon_batch(+ specs: &[ReferenceIconSpec],+ batch: &ReferenceIconBatchConfig,+) -> Result<(), String> {+ let backend = match batch.backend.as_str() {+ "cuda" => ComputePlatform::Cuda13,+ "vulkan" => ComputePlatform::Vulkan,+ value => return Err(format!("Unsupported icon generation backend '{value}'.")),+ };+ let configuration = Configuration {+ dll_path: batch.dll_path.clone(),+ models: [+ (0, "transformer", Some(batch.transformer.clone())),+ (1, "textEncoder", Some(batch.text_encoder.clone())),+ (2, "tokenizer", None),+ (3, "videoVae", Some(batch.video_vae.clone())),+ // Icons keep one picture and need no decoded soundtrack.+ (4, "audioVae", None),+ ],+ };+ let api = ffi::Api::load(&configuration.dll_path)?;+ api.version()?;+ let platform = if backend == ComputePlatform::Cuda13 {+ detect_platform(&api)+ } else {+ backend+ };+ let result = specs.iter().enumerate().try_for_each(|(index, spec)| {+ let started = std::time::Instant::now();+ write_reference_icon(&api, &configuration, platform, spec)?;+ println!(+ "[{}/{}] {}: generated in {:.1}s",+ index + 1,+ specs.len(),+ spec.id,+ started.elapsed().as_secs_f64()+ );+ Ok(())+ });+ let cleared = api.clear_reused_models();+ result.and(cleared)+}+struct CallbackContext {app: AppHandle,job_id: String,