Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Move image export beside Save and support PNG

Bittic8fddc460856
diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock
index e986d74..df4a931 100644
--- a/src-tauri/Cargo.lock
+++ b/src-tauri/Cargo.lock
@@ -199,6 +199,12 @@ version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
+[[package]]
+name = "byteorder-lite"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495"
+
[[package]]
name = "bytes"
version = "1.12.1"
@@ -1566,6 +1572,21 @@ dependencies = [
"icu_properties",
]
+[[package]]
+name = "image"
+version = "0.25.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "529feb3e6769d234375c4cf1ee2ce713682b8e76538cb13f9fc23e1400a591e7"
+dependencies = [
+ "bytemuck",
+ "byteorder-lite",
+ "moxcms",
+ "num-traits",
+ "png 0.18.1",
+ "zune-core",
+ "zune-jpeg",
+]
+
[[package]]
name = "indexmap"
version = "1.9.3"
@@ -1970,6 +1991,16 @@ dependencies = [
"windows-sys 0.61.2",
]
+[[package]]
+name = "moxcms"
+version = "0.7.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac9557c559cd6fc9867e122e20d2cbefc9ca29d80d027a8e39310920ed2f0a97"
+dependencies = [
+ "num-traits",
+ "pxfm",
+]
+
[[package]]
name = "muda"
version = "0.19.3"
@@ -2554,6 +2585,12 @@ dependencies = [
"unicode-ident",
]
+[[package]]
+name = "pxfm"
+version = "0.1.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea"
+
[[package]]
name = "quick-xml"
version = "0.41.0"
@@ -3326,6 +3363,7 @@ name = "slopus"
version = "0.1.1"
dependencies = [
"getrandom 0.3.4",
+ "image",
"jpeg-encoder",
"libloading 0.8.9",
"reqwest 0.12.28",
@@ -5349,3 +5387,18 @@ name = "zmij"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
+
+[[package]]
+name = "zune-core"
+version = "0.4.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3f423a2c17029964870cfaabb1f13dfab7d092a62a29a89264f4d36990ca414a"
+
+[[package]]
+name = "zune-jpeg"
+version = "0.4.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "29ce2c8a9384ad323cf564b67da86e21d3cfdff87908bc1223ed5c99bc792713"
+dependencies = [
+ "zune-core",
+]
diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml
index 17c9c86..2a524e5 100644
--- a/src-tauri/Cargo.toml
+++ b/src-tauri/Cargo.toml
@@ -27,6 +27,8 @@ libloading = "0.8"
getrandom = "0.3"
reqwest = { version = "0.12", default-features = false, features = ["blocking", "json", "rustls-tls"] }
jpeg-encoder = "0.6"
+# Keep image decoding compatible with the package's Rust 1.87 minimum.
+image = { version = "=0.25.8", default-features = false, features = ["jpeg", "png"] }
tokio = { version = "1", features = ["rt", "time", "net", "macros"] }
[target.'cfg(windows)'.dependencies]
diff --git a/src-tauri/src/commands/artifacts.rs b/src-tauri/src/commands/artifacts.rs
index 542f613..228a79a 100644
--- a/src-tauri/src/commands/artifacts.rs
+++ b/src-tauri/src/commands/artifacts.rs
@@ -203,7 +203,8 @@ pub(crate) async fn export_generated_image(
.file()
.set_title("Export image")
.add_filter("JPEG image", &["jpg", "jpeg"])
- .set_file_name("image.jpg")
+ .add_filter("PNG image", &["png"])
+ .set_file_name("image")
.blocking_save_file()
else {
return Ok(false);
@@ -211,14 +212,47 @@ pub(crate) async fn export_generated_image(
let destination = destination
.into_path()
.map_err(|_| "Choose a local image destination.")?;
- let bytes = fs::read(source).map_err(|error| format!("Could not read image: {error}"))?;
- crate::storage::atomic::write_atomically(&destination, &bytes)?;
+ export_image_file(&source, &destination)?;
Ok(true)
})
.await
.map_err(|error| error.to_string())?
}
+/// Copy JPEGs without another lossy encode; PNG exports retain the decoded
+/// source pixels and full resolution. File extensions always match the bytes.
+pub(crate) fn export_image_file(
+ source: &std::path::Path,
+ destination: &std::path::Path,
+) -> Result<(), String> {
+ let destination = if destination.extension().is_none() {
+ destination.with_extension("jpg")
+ } else {
+ destination.to_path_buf()
+ };
+ let extension = destination
+ .extension()
+ .and_then(|value| value.to_str())
+ .unwrap_or("")
+ .to_ascii_lowercase();
+ if !matches!(extension.as_str(), "jpg" | "jpeg" | "png") {
+ return Err("Choose a .jpg, .jpeg or .png filename for the exported image.".into());
+ }
+ let bytes = fs::read(source).map_err(|error| format!("Could not read image: {error}"))?;
+ let bytes = if extension == "png" {
+ let image = image::load_from_memory_with_format(&bytes, image::ImageFormat::Jpeg)
+ .map_err(|error| format!("Could not decode image: {error}"))?;
+ let mut encoded = std::io::Cursor::new(Vec::new());
+ image
+ .write_to(&mut encoded, image::ImageFormat::Png)
+ .map_err(|error| format!("Could not encode PNG: {error}"))?;
+ encoded.into_inner()
+ } else {
+ bytes
+ };
+ crate::storage::atomic::write_atomically(&destination, &bytes)
+}
+
#[tauri::command]
pub(crate) fn list_builtin_reference_icons(app: AppHandle) -> Result<Vec<String>, String> {
reference_icons::list_builtin(&app_paths::data_directory(&app))
diff --git a/src-tauri/src/tests/images.rs b/src-tauri/src/tests/images.rs
index 5c79b18..a1a3081 100644
--- a/src-tauri/src/tests/images.rs
+++ b/src-tauri/src/tests/images.rs
@@ -24,6 +24,36 @@ fn generated_jpeg_preserves_resolution_and_stays_inside_the_project() {
assert!(write_generated_image_frame(&root, "bad-pixels", 640, 416, &[0; 4]).is_err());
}
+#[test]
+fn image_export_writes_real_png_or_copies_jpeg_without_changing_pixels_or_resolution() {
+ let folder = project_folder();
+ let root = folder.path().to_string_lossy();
+ let rgba: Vec<u8> = (0..64 * 40).flat_map(|index| [(index % 256) as u8, 80, 160, 255]).collect();
+ let saved = write_generated_image_frame(&root, "export-test", 64, 40, &rgba).unwrap();
+ let source = folder.path().join(saved.relative_path);
+ let jpeg = fs::read(&source).unwrap();
+ for extension in ["jpg", "jpeg", "JPG"] {
+ let destination = folder.path().join(format!("export.{extension}"));
+ export_image_file(&source, &destination).unwrap();
+ assert_eq!(fs::read(destination).unwrap(), jpeg);
+ }
+ let destination = folder.path().join("export.PNG");
+ export_image_file(&source, &destination).unwrap();
+ let png = fs::read(&destination).unwrap();
+ assert!(png.starts_with(b"\x89PNG\r\n\x1a\n"));
+ let decoded = ::image::load_from_memory_with_format(&png, ::image::ImageFormat::Png).unwrap();
+ let original = ::image::load_from_memory_with_format(&jpeg, ::image::ImageFormat::Jpeg).unwrap();
+ assert_eq!((decoded.width(), decoded.height()), (64, 40));
+ assert_eq!(decoded.to_rgb8(), original.to_rgb8());
+ export_image_file(&source, &folder.path().join("no-extension")).unwrap();
+ assert_eq!(fs::read(folder.path().join("no-extension.jpg")).unwrap(), jpeg);
+ let unsupported = folder.path().join("export.gif");
+ fs::write(&unsupported, b"existing file").unwrap();
+ assert!(export_image_file(&source, &unsupported).is_err());
+ assert_eq!(fs::read(unsupported).unwrap(), b"existing file");
+ assert_eq!(fs::read(source).unwrap(), jpeg);
+}
+
#[test]
fn image_tree_survives_folder_creation_save_and_reopen() {
let root = tempfile::tempdir().unwrap();
diff --git a/src/components/ImageProject.integration.test.tsx b/src/components/ImageProject.integration.test.tsx
index 9b9ac15..83b89ba 100644
--- a/src/components/ImageProject.integration.test.tsx
+++ b/src/components/ImageProject.integration.test.tsx
@@ -9,12 +9,48 @@ import { executeAgentCommands } from "../lib/runtime";
import { imageScenePrompt } from "../lib/imageScene";
import { saveDebugOptionsEnabled } from "../lib/settings";
import fixture from "../../fixtures/project-v1-image.json";
+import { invoke } from "@tauri-apps/api/core";
+import * as persistence from "../lib/persistence";
vi.mock("@tauri-apps/api/core", () => ({ invoke: vi.fn() }));
vi.mock("@tauri-apps/api/event", () => ({ listen: vi.fn(async () => () => undefined) }));
-afterEach(() => { cleanup(); localStorage.clear(); });
+afterEach(() => { cleanup(); localStorage.clear(); vi.restoreAllMocks(); });
const record = (): ProjectRecord => ({ folderPath: "D:/Images", config: parseProjectConfig(fixture) });
+it("exports the selected image from beside Save and handles cancellation and errors", async () => {
+ vi.spyOn(persistence, "isTauri").mockReturnValue(true);
+ vi.spyOn(persistence, "readMediaFileUrl").mockResolvedValue(null);
+ const project = record();
+ project.config.assets = ["First", "Second"].map((name) => ({ id: name, name, kind: "image", relativePath: `media/generated/${name}.jpg`, mimeType: "image/jpeg", createdAt: project.config.createdAt }));
+ project.config.imageScene!.outputAssetId = "First";
+ let complete!: (value: boolean) => void;
+ const exporting = new Promise<boolean>((resolve) => { complete = resolve; });
+ vi.mocked(invoke).mockImplementation(async (command) => {
+ if (command === "export_generated_image") return await exporting as never;
+ return undefined as never;
+ });
+ render(<ProjectWorkspace project={project} onBack={vi.fn()} onSave={vi.fn()} />);
+ const button = screen.getByRole("button", { name: "Export" });
+ expect(button.parentElement).toContainElement(screen.getByRole("button", { name: "Save" }));
+ expect(screen.queryByRole("button", { name: "Export image" })).not.toBeInTheDocument();
+ fireEvent.click(screen.getByRole("button", { name: "View Second" }));
+ fireEvent.click(button);
+ await waitFor(() => expect(invoke).toHaveBeenCalledWith("export_generated_image", { folderPath: "D:/Images", relativePath: "media/generated/Second.jpg" }));
+ expect(screen.getByRole("button", { name: "Exporting…" })).toBeDisabled();
+ await act(async () => complete(false));
+ expect(button).toBeEnabled();
+ expect(screen.queryByText("Couldn’t export image")).not.toBeInTheDocument();
+ vi.mocked(invoke).mockRejectedValueOnce("Destination is not writable");
+ fireEvent.click(button);
+ expect(await screen.findByText("Destination is not writable")).toBeInTheDocument();
+ expect(button).toBeEnabled();
+});
+
+it("disables image export until a generated image is selected", () => {
+ render(<ProjectWorkspace project={record()} onBack={vi.fn()} onSave={vi.fn()} />);
+ expect(screen.getByRole("button", { name: "Export" })).toBeDisabled();
+});
+
it("shows the full image prompt at the end of the inspector only when debug is enabled", () => {
const project = record();
project.config.settings.defaultLook = "watercolor";
diff --git a/src/components/ProjectWorkspace.tsx b/src/components/ProjectWorkspace.tsx
index 3ce8f6b..39a28aa 100644
--- a/src/components/ProjectWorkspace.tsx
+++ b/src/components/ProjectWorkspace.tsx
@@ -12,6 +12,8 @@ import { ReferencesView } from "./workspace/ReferencesView";
import { TimelineView, type ConfigUpdate } from "./workspace/TimelineView";
import { UnsavedProjectDialog } from "./UnsavedProjectDialog";
import { PromptComposer } from "./PromptComposer";
+import { invoke } from "@tauri-apps/api/core";
+import { isTauri } from "../lib/persistence";
export type ProjectView = "timeline" | "generator" | "references" | "agent" | "export" | "editor";
@@ -48,6 +50,9 @@ export function ProjectWorkspace({ project, initialView = "timeline", runtime =
const { config, saving, dirty, saveError } = useSyncExternalStore(session.subscribe, session.getSnapshot);
const items = useSyncExternalStore(queue.subscribe, queue.getSnapshot);
const imageProject = config.generationType === "image";
+ const imageOutput = config.assets.find((asset) => asset.kind === "image" && asset.id === config.imageScene?.outputAssetId);
+ const [exportingImage, setExportingImage] = useState(false);
+ const [imageExportError, setImageExportError] = useState<string | null>(null);
const [view, setView] = useState<ProjectView>(imageProject && !["agent", "references"].includes(initialView) ? "editor" : initialView);
const [leaving, setLeaving] = useState(false);
const [savingToLeave, setSavingToLeave] = useState(false);
@@ -62,6 +67,13 @@ export function ProjectWorkspace({ project, initialView = "timeline", runtime =
const changeConfig = (next: ConfigUpdate) => session.update(next);
const recordMeasurement = (update: (current: ProjectConfig) => ProjectConfig) => session.update(update, false);
const save = () => session.save().catch(() => undefined);
+ const exportImage = async () => {
+ if (!imageOutput?.relativePath || exportingImage) return;
+ setExportingImage(true); setImageExportError(null);
+ try { await invoke("export_generated_image", { folderPath: project.folderPath, relativePath: imageOutput.relativePath }); }
+ catch (reason) { setImageExportError(String(reason)); }
+ finally { setExportingImage(false); }
+ };
const leave = async (keepChanges: boolean) => {
setSavingToLeave(true);
try {
@@ -122,17 +134,16 @@ export function ProjectWorkspace({ project, initialView = "timeline", runtime =
</nav>
<div className="project-topbar__actions">
- {/* A real view now: it renders the timeline through WebCodecs and writes
- an .mp4. It stays honest about what it cannot do inside itself. */}
- {!imageProject && <button
+ <button
className="secondary-button"
type="button"
- aria-current={view === "export" ? "page" : undefined}
- onClick={() => setView("export")}
- title="Render the timeline to a video file"
+ aria-current={!imageProject && view === "export" ? "page" : undefined}
+ disabled={imageProject && (!imageOutput?.relativePath || !isTauri() || exportingImage)}
+ onClick={() => imageProject ? void exportImage() : setView("export")}
+ title={imageProject ? "Save the selected image as JPG or PNG" : "Render the timeline to a video file"}
>
- <Download size={16} aria-hidden="true" /> Export
- </button>}
+ <Download size={16} aria-hidden="true" /> {exportingImage ? "Exporting…" : "Export"}
+ </button>
{/* The standing "All changes saved" pill is gone; the button itself is
now the only save state there is, so it has to carry it. Off means
the file on disk already matches what is on screen. */}
@@ -165,5 +176,6 @@ export function ProjectWorkspace({ project, initialView = "timeline", runtime =
}}
/></footer>
{saveError && <div className="toast" role="alert"><strong>Couldn’t save project</strong><span>{saveError}</span><button onClick={session.dismissError}>Dismiss</button></div>}
+ {imageExportError && <div className="toast" role="alert"><strong>Couldn’t export image</strong><span>{imageExportError}</span><button onClick={() => setImageExportError(null)}>Dismiss</button></div>}
</div>;
}
diff --git a/src/components/workspace/ImageEditor.tsx b/src/components/workspace/ImageEditor.tsx
index f6e42cb..f3d9f8d 100644
--- a/src/components/workspace/ImageEditor.tsx
+++ b/src/components/workspace/ImageEditor.tsx
@@ -1,6 +1,5 @@
import { useEffect, useMemo, useRef, useState, useSyncExternalStore, type CSSProperties, type DragEvent, type PointerEvent } from "react";
-import { Box, ChevronDown, ChevronRight, ClipboardPaste, Copy, Download, FolderPlus, Image, Pencil, Plus, Redo2, Sparkles, Square, Trash2, Type, Undo2 } from "lucide-react";
-import { invoke } from "@tauri-apps/api/core";
+import { Box, ChevronDown, ChevronRight, ClipboardPaste, Copy, FolderPlus, Image, Pencil, Plus, Redo2, Sparkles, Square, Trash2, Type, Undo2 } from "lucide-react";
import { addImageNode, createImageScene, duplicateImageNode, imageDescendants, imageScenePrompt, removeImageNode, resizeImageNode, type ImageBox, type ImageNode, type ImageScene } from "../../lib/imageScene";
import { outputDimensions } from "../../lib/export";
import { compileImagePrompt } from "../../lib/imagePrompt";
@@ -272,7 +271,6 @@ export function ImageEditor({ config, folderPath, onChange, onGenerate, onCancel
<button className={`icon-button ${boxes ? "active" : ""}`} aria-label="Show placement boxes" aria-pressed={boxes} onClick={() => { setBoxes(!boxes); setDrawKind(null); }}><Square size={16} /></button>
<select aria-label="Canvas tool" value={drawKind ?? "select"} onChange={(event) => { setDrawKind(event.target.value === "select" ? null : event.target.value as typeof drawKind); setBoxes(true); }}><option value="select">Select / move</option><option value="object">Draw object</option><option value="text">Draw text</option><option value="group">Draw group</option></select>
<button className="secondary-button" onClick={() => setView({ zoom: 1, x: 0, y: 0 })}>Fit</button><input aria-label="Image zoom" type="range" min={MIN_IMAGE_ZOOM} max={MAX_IMAGE_ZOOM} step="0.01" value={view.zoom} onChange={(event) => { const zoom = Number(event.target.value); setView((current) => ({ zoom, x: current.x * zoom / current.zoom, y: current.y * zoom / current.zoom })); }} /><span>{Math.round(view.zoom * 100)}%</span>
- <button className="icon-button" aria-label="Export image" title="Export image" disabled={!output || !isTauri()} onClick={() => attempt(() => invoke("export_generated_image", { folderPath, relativePath: output!.relativePath }))}><Download size={16} /></button>
</div>
<div ref={viewport} className={`image-viewport${panning ? " panning" : ""}`} title="Scroll to zoom · Drag with the middle mouse button to pan"
onPointerDownCapture={(event) => {