Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Add native export upscaling

Bitti3887f204e724
diff --git a/docs/slopfab-compatibility.md b/docs/slopfab-compatibility.md
index 365b1ba..65037f9 100644
--- a/docs/slopfab-compatibility.md
+++ b/docs/slopfab-compatibility.md
@@ -1,5 +1,30 @@
# Slopfab compatibility
+## API 1.22: export upscaling
+
+The bundled Windows and Linux runtimes come from SlopFab commit `d4f423f`,
+built with `SLOPFAB_WITH_FFMPEG=OFF`. `slopfab_realesrgan_upscale` exposes
+Real-ESRGAN x4plus as a streaming RGB callback API; SeedVR2 uses
+`slopfab_seedvr2_upscale`. Models stay loaded for the export. The host retains
+the frame count, timestamps, audio, WebCodecs encoder and MP4 muxer.
+
+Image and video exports offer None, Real-ESRGAN and SeedVR2. The chosen export
+resolution is the final size: Real-ESRGAN produces 4x frames followed by final
+resampling, while SeedVR2 restores frames at the chosen output size. SeedVR2
+uses five-frame temporal segments for video and a single frame for images,
+and currently requires NVIDIA CUDA. Real-ESRGAN supports CUDA and Vulkan.
+Image export preserves the source alpha channel, resized to the output size.
+
+Native upscaling uses bounded input/output queues and raw RGBA IPC. These C
+APIs require host pixel buffers, so selecting an upscaler adds GPU readback
+and upload during export. None keeps the existing GPU export path; preview
+and scrubbing do not run the upscaler. Cancellation releases the stream and
+discards partial output. Model access is serialized against local generation.
+
+Settings → Generator → Other weights contains the Real-ESRGAN model and the
+SeedVR2 3B INT8 model plus its FP16 VAE. Files use the existing native download
+cache and progress/cancellation commands; paths remain machine-local.
+
## API 1.20: single-pass image outpainting
The bundled Windows and Linux runtimes come from SlopFab commit `b7486be`,
diff --git a/lib/slopfab/libslopfab.so b/lib/slopfab/libslopfab.so
index fb5bfca..51871d5 100644
--- a/lib/slopfab/libslopfab.so
+++ b/lib/slopfab/libslopfab.so
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
-oid sha256:93aad39f36b1fd7b359114ed337c8c15f355a61349fa505c2eeefd31bf6802d3
-size 19234488
+oid sha256:97ce91c793ba94c5ae1d9e52076c156d405bd0adba9899dfe45162a61259834a
+size 19702592
diff --git a/lib/slopfab/slopfab.dll b/lib/slopfab/slopfab.dll
index a50a016..955baf7 100644
--- a/lib/slopfab/slopfab.dll
+++ b/lib/slopfab/slopfab.dll
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
-oid sha256:718d5280d4aa47d72125b7401d11bab895175f5dfb6c675831b0a5b1b46b01f7
-size 16714752
+oid sha256:3fa18522e0dd04d64a8f922bb129be2f9699565e376c10e5ed8af05bcdd30ce7
+size 17104384
diff --git a/src-tauri/core/src/slopfab/mod.rs b/src-tauri/core/src/slopfab/mod.rs
index 480eb3c..810aca4 100644
--- a/src-tauri/core/src/slopfab/mod.rs
+++ b/src-tauri/core/src/slopfab/mod.rs
@@ -24,6 +24,7 @@ mod refmod_export;
mod runtime;
mod status;
mod types;
+pub mod upscale;
pub use runtime::SlopfabRuntime;
pub use types::*;
diff --git a/src-tauri/core/src/slopfab/upscale.rs b/src-tauri/core/src/slopfab/upscale.rs
new file mode 100644
index 0000000..f73d580
--- /dev/null
+++ b/src-tauri/core/src/slopfab/upscale.rs
@@ -0,0 +1,380 @@
+//! Streaming export restoration. Codecs and timestamps remain owned by Slopus.
+use image::{imageops::FilterType, RgbaImage};
+use std::{
+ ffi::{c_char, c_void, CStr, CString},
+ sync::atomic::{AtomicBool, Ordering},
+};
+
+#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct UpscaleConfig {
+ pub method: UpscaleMethod,
+ pub model_path: String,
+ pub vae_path: Option<String>,
+}
+#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
+#[serde(rename_all = "lowercase")]
+pub enum UpscaleMethod {
+ Realesrgan,
+ Seedvr2,
+}
+
+type Read = unsafe extern "C" fn(*mut c_void, *mut f32, u64) -> i32;
+type Write = unsafe extern "C" fn(*mut c_void, *const f32, u64) -> i32;
+type Cancel = unsafe extern "C" fn(*mut c_void) -> i32;
+type Progress = unsafe extern "C" fn(*mut c_void, *const c_char);
+#[repr(C)]
+struct SeedOptions {
+ struct_size: u32,
+ transformer: *const c_char,
+ vae: *const c_char,
+ width: i32,
+ height: i32,
+ segment_frames: i32,
+ vae_tile: i32,
+ device: i32,
+ color_match: i32,
+ seed: u64,
+}
+type SeedRun = unsafe extern "C" fn(
+ *const SeedOptions,
+ Read,
+ Write,
+ Cancel,
+ Option<Progress>,
+ *mut c_void,
+ *mut u64,
+) -> i32;
+type RealRun = unsafe extern "C" fn(
+ *const c_char,
+ i32,
+ i32,
+ i32,
+ i32,
+ Read,
+ Write,
+ Cancel,
+ *mut c_void,
+ *mut u64,
+) -> i32;
+
+struct Stream<'a> {
+ read: &'a mut dyn FnMut() -> Result<Option<Vec<u8>>, String>,
+ write: &'a mut dyn FnMut(Vec<u8>) -> Result<(), String>,
+ cancelled: &'a AtomicBool,
+ input: (u32, u32),
+ native_input: (u32, u32),
+ native_output: (u32, u32),
+ output: (u32, u32),
+ error: Option<String>,
+}
+unsafe extern "C" fn read_frame(user: *mut c_void, data: *mut f32, count: u64) -> i32 {
+ let stream = &mut *(user as *mut Stream<'_>);
+ let result =
+ std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> Result<i32, String> {
+ if stream.cancelled.load(Ordering::Relaxed) {
+ return Err("Upscaling cancelled.".into());
+ }
+ let Some(bytes) = (stream.read)()? else {
+ return Ok(0);
+ };
+ let image = RgbaImage::from_raw(stream.input.0, stream.input.1, bytes)
+ .ok_or("Invalid input frame length.")?;
+ let image = if stream.input != stream.native_input {
+ image::imageops::resize(
+ &image,
+ stream.native_input.0,
+ stream.native_input.1,
+ FilterType::Lanczos3,
+ )
+ } else {
+ image
+ };
+ if count != image.width() as u64 * image.height() as u64 * 3 {
+ return Err("Unexpected upscaler input geometry.".into());
+ }
+ let target = std::slice::from_raw_parts_mut(data, count as usize);
+ for (rgb, pixel) in target.chunks_exact_mut(3).zip(image.pixels()) {
+ for channel in 0..3 {
+ rgb[channel] = pixel[channel] as f32 / 255.0;
+ }
+ }
+ Ok(1)
+ }))
+ .unwrap_or_else(|_| Err("Upscaler input callback panicked.".into()));
+ match result {
+ Ok(value) => value,
+ Err(error) => {
+ stream.error = Some(error);
+ -1
+ }
+ }
+}
+unsafe extern "C" fn write_frame(user: *mut c_void, data: *const f32, count: u64) -> i32 {
+ let stream = &mut *(user as *mut Stream<'_>);
+ let result =
+ std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> Result<(), String> {
+ let (width, height) = stream.native_output;
+ if count != width as u64 * height as u64 * 3 {
+ return Err("Unexpected upscaler output geometry.".into());
+ }
+ let source = std::slice::from_raw_parts(data, count as usize);
+ let mut rgba = RgbaImage::new(width, height);
+ for (pixel, rgb) in rgba.pixels_mut().zip(source.chunks_exact(3)) {
+ for channel in 0..3 {
+ pixel[channel] = (rgb[channel].clamp(0.0, 1.0) * 255.0).round() as u8;
+ }
+ pixel[3] = 255;
+ }
+ let rgba = if stream.native_output != stream.output {
+ image::imageops::resize(
+ &rgba,
+ stream.output.0,
+ stream.output.1,
+ FilterType::Lanczos3,
+ )
+ } else {
+ rgba
+ };
+ (stream.write)(rgba.into_raw())
+ }))
+ .unwrap_or_else(|_| Err("Upscaler output callback panicked.".into()));
+ match result {
+ Ok(()) => 0,
+ Err(error) => {
+ stream.error = Some(error);
+ -1
+ }
+ }
+}
+unsafe extern "C" fn cancelled(user: *mut c_void) -> i32 {
+ let stream = &*(user as *const Stream<'_>);
+ i32::from(stream.cancelled.load(Ordering::Relaxed))
+}
+
+pub fn validate(
+ config: &UpscaleConfig,
+ input: (u32, u32),
+ output: (u32, u32),
+) -> Result<(), String> {
+ if [input.0, input.1, output.0, output.1]
+ .iter()
+ .any(|n| *n == 0 || *n > 16384)
+ {
+ return Err("Upscale dimensions must be between 1 and 16384 pixels.".into());
+ }
+ if config.method == UpscaleMethod::Seedvr2 && (output.0 < 16 || output.1 < 16) {
+ return Err("SeedVR2 output must be at least 16 pixels on each side.".into());
+ }
+ for path in std::iter::once(Some(config.model_path.as_str()))
+ .chain((config.method == UpscaleMethod::Seedvr2).then_some(config.vae_path.as_deref()))
+ {
+ if !path.is_some_and(|path| std::path::Path::new(path).is_file()) {
+ return Err("Upscaler weights are missing. Download them in Settings → Generator → Other weights.".into());
+ }
+ }
+ Ok(())
+}
+
+pub fn run(
+ config: &UpscaleConfig,
+ input: (u32, u32),
+ output: (u32, u32),
+ segment_frames: i32,
+ stop: &AtomicBool,
+ read: &mut dyn FnMut() -> Result<Option<Vec<u8>>, String>,
+ write: &mut dyn FnMut(Vec<u8>) -> Result<(), String>,
+) -> Result<u64, String> {
+ validate(config, input, output)?;
+ // Do not compete with generation or weight preparation for GPU memory.
+ let _models = loop {
+ if stop.load(Ordering::Relaxed) {
+ return Err("Upscaling cancelled.".into());
+ }
+ match super::MODEL_ACCESS.try_write() {
+ Ok(lock) => break lock,
+ Err(std::sync::TryLockError::WouldBlock) => {
+ std::thread::sleep(std::time::Duration::from_millis(50))
+ }
+ Err(_) => return Err("Model access lock failed.".into()),
+ }
+ };
+ let path = super::default_dll_path();
+ let api = super::ffi::Api::load(&path)?;
+ let backend = super::platform::detect_platform(&api).backend();
+ if config.method == UpscaleMethod::Seedvr2 && backend != 0 {
+ return Err("SeedVR2 requires an NVIDIA GPU with CUDA.".into());
+ }
+ api.clear_reused_models()?;
+ let model = CString::new(config.model_path.as_str()).map_err(|e| e.to_string())?;
+ let vae = CString::new(config.vae_path.as_deref().unwrap_or("")).map_err(|e| e.to_string())?;
+ let seed = config.method == UpscaleMethod::Seedvr2;
+ // Real-ESRGAN always creates 4x pixels; cap its working input so even a
+ // large imported still stays within the 16384-pixel export limit.
+ let scale = (4096.0 / input.0.max(input.1) as f64).min(1.0);
+ let native_input = if seed {
+ output
+ } else {
+ (
+ (input.0 as f64 * scale).round().max(1.0) as u32,
+ (input.1 as f64 * scale).round().max(1.0) as u32,
+ )
+ };
+ let mut stream = Stream {
+ read,
+ write,
+ cancelled: stop,
+ input,
+ output,
+ error: None,
+ native_input,
+ native_output: if seed {
+ output
+ } else {
+ (native_input.0 * 4, native_input.1 * 4)
+ },
+ };
+ let user = &mut stream as *mut _ as *mut c_void;
+ let mut written = 0;
+ unsafe {
+ let library = libloading::Library::new(path).map_err(|e| e.to_string())?;
+ let status = if seed {
+ let run = library
+ .get::<SeedRun>(b"slopfab_seedvr2_upscale\0")
+ .map_err(|_| "SeedVR2 export requires an updated SlopFab runtime.")?;
+ let options = SeedOptions {
+ struct_size: std::mem::size_of::<SeedOptions>() as u32,
+ transformer: model.as_ptr(),
+ vae: vae.as_ptr(),
+ width: output.0 as i32,
+ height: output.1 as i32,
+ segment_frames,
+ vae_tile: 256,
+ device: 0,
+ color_match: 1,
+ seed: 666,
+ };
+ run(
+ &options,
+ read_frame,
+ write_frame,
+ cancelled,
+ None,
+ user,
+ &mut written,
+ )
+ } else {
+ let run = library
+ .get::<RealRun>(b"slopfab_realesrgan_upscale\0")
+ .map_err(|_| "Real-ESRGAN export requires SlopFab API 1.22 or later.")?;
+ run(
+ model.as_ptr(),
+ native_input.0 as i32,
+ native_input.1 as i32,
+ backend,
+ 128,
+ read_frame,
+ write_frame,
+ cancelled,
+ user,
+ &mut written,
+ )
+ };
+ if let Some(error) = stream.error {
+ return Err(error);
+ }
+ if status != 0 {
+ let error = library
+ .get::<unsafe extern "C" fn() -> *const c_char>(b"slopfab_last_error\0")
+ .map_err(|e| e.to_string())?;
+ let message = error();
+ return Err(if message.is_null() {
+ format!("Upscaling failed ({status}).")
+ } else {
+ CStr::from_ptr(message).to_string_lossy().into_owned()
+ });
+ }
+ }
+ Ok(written)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn callbacks_preserve_packed_rgb() {
+ let stop = AtomicBool::new(false);
+ let mut input = Some(vec![255, 128, 0, 77, 0, 64, 255, 128]);
+ let mut output = Vec::new();
+ let mut read = || Ok(input.take());
+ let mut write = |bytes| {
+ output = bytes;
+ Ok(())
+ };
+ let mut stream = Stream {
+ read: &mut read,
+ write: &mut write,
+ cancelled: &stop,
+ input: (2, 1),
+ native_input: (2, 1),
+ native_output: (2, 1),
+ output: (2, 1),
+ error: None,
+ };
+ let mut rgb = [0.0; 6];
+ let user = &mut stream as *mut _ as *mut c_void;
+ unsafe {
+ assert_eq!(read_frame(user, rgb.as_mut_ptr(), 6), 1);
+ assert_eq!(rgb, [1.0, 128.0 / 255.0, 0.0, 0.0, 64.0 / 255.0, 1.0]);
+ assert_eq!(write_frame(user, rgb.as_ptr(), 6), 0);
+ assert_eq!(read_frame(user, rgb.as_mut_ptr(), 6), 0);
+ }
+ drop(stream);
+ assert_eq!(output, [255, 128, 0, 255, 0, 64, 255, 255]);
+ }
+
+ #[test]
+ fn callbacks_report_failures_without_unwinding_across_the_abi() {
+ let stop = AtomicBool::new(false);
+ let mut read = || -> Result<Option<Vec<u8>>, String> { panic!("reader failed") };
+ let mut write = |_| Ok(());
+ let mut stream = Stream {
+ read: &mut read,
+ write: &mut write,
+ cancelled: &stop,
+ input: (1, 1),
+ native_input: (1, 1),
+ native_output: (1, 1),
+ output: (1, 1),
+ error: None,
+ };
+ let mut rgb = [0.0; 3];
+ unsafe {
+ assert_eq!(
+ read_frame(&mut stream as *mut _ as *mut c_void, rgb.as_mut_ptr(), 3),
+ -1
+ );
+ }
+ assert!(stream.error.as_ref().unwrap().contains("panicked"));
+ stop.store(true, Ordering::Relaxed);
+ unsafe {
+ assert_eq!(cancelled(&mut stream as *mut _ as *mut c_void), 1);
+ }
+ }
+
+ #[test]
+ fn seedvr2_requires_both_weights_and_rejects_invalid_sizes() {
+ let weight = tempfile::NamedTempFile::new().unwrap();
+ let mut config = UpscaleConfig {
+ method: UpscaleMethod::Seedvr2,
+ model_path: weight.path().to_string_lossy().into(),
+ vae_path: None,
+ };
+ assert!(validate(&config, (32, 32), (64, 64)).is_err());
+ config.vae_path = Some(config.model_path.clone());
+ assert!(validate(&config, (32, 32), (64, 64)).is_ok());
+ assert!(validate(&config, (0, 32), (64, 64)).is_err());
+ assert!(validate(&config, (32, 32), (8, 8)).is_err());
+ }
+}
diff --git a/src-tauri/src/app.rs b/src-tauri/src/app.rs
index ff69aa0..1c8bf55 100644
--- a/src-tauri/src/app.rs
+++ b/src-tauri/src/app.rs
@@ -187,6 +187,11 @@ pub fn run() {
commands::character_sheet::discard_character_sheet_views,
commands::artifacts::open_image_source,
commands::artifacts::export_generated_image,
+ crate::upscale::start_export_upscale,
+ crate::upscale::push_export_upscale,
+ crate::upscale::finish_export_upscale,
+ crate::upscale::read_export_upscale,
+ crate::upscale::cancel_export_upscale,
app_updater_mode,
open_app_releases,
weights::download_weight,
diff --git a/src-tauri/src/commands/artifacts.rs b/src-tauri/src/commands/artifacts.rs
index 7cd8b7a..861f831 100644
--- a/src-tauri/src/commands/artifacts.rs
+++ b/src-tauri/src/commands/artifacts.rs
@@ -246,13 +246,14 @@ pub(crate) fn write_generated_image_frame(
/// How the Export tab asks for the image: its file format, its pixel size
/// (the generated size when absent) and, for JPEG, the quality 1–100.
-#[derive(Debug, Clone, Copy, Default, serde::Deserialize)]
+#[derive(Debug, Clone, Default, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct ImageExportOptions {
pub format: Option<ImageExportFormat>,
pub width: Option<u32>,
pub height: Option<u32>,
pub quality: Option<u8>,
+ pub upscale: Option<crate::slopfab::upscale::UpscaleConfig>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
@@ -362,6 +363,7 @@ pub(crate) fn export_image_file_with(
}
let resized = (width, height) != (source_width, source_height);
let unchanged = source_format == target_format
+ && options.upscale.is_none()
&& !resized
&& (target_format == image::ImageFormat::Png || quality >= GENERATED_JPEG_QUALITY);
let bytes = if unchanged {
@@ -369,7 +371,20 @@ pub(crate) fn export_image_file_with(
} else {
let mut image = image::load_from_memory_with_format(&bytes, source_format)
.map_err(|error| format!("Could not decode image: {error}"))?;
- if resized {
+ if let Some(config) = &options.upscale {
+ let original = image.to_rgba8();
+ let mut input = Some(original.clone().into_raw());
+ let mut output = None;
+ crate::slopfab::upscale::run(config, (source_width, source_height), (width, height),
+ 1,
+ &std::sync::atomic::AtomicBool::new(false), &mut || Ok(input.take()),
+ &mut |bytes| { output = Some(bytes); Ok(()) })?;
+ let mut restored = image::RgbaImage::from_raw(width, height, output.ok_or("Upscaler returned no image.")?)
+ .ok_or("Upscaler returned invalid image dimensions.")?;
+ let alpha = image::imageops::resize(&original, width, height, image::imageops::FilterType::Lanczos3);
+ for (pixel, source) in restored.pixels_mut().zip(alpha.pixels()) { pixel[3] = source[3]; }
+ image = image::DynamicImage::ImageRgba8(restored);
+ } else if resized {
image = image.resize_exact(width, height, image::imageops::FilterType::Lanczos3);
}
let mut encoded = std::io::Cursor::new(Vec::new());
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index 8cc0f41..4ce757f 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -15,6 +15,7 @@ mod native_shell;
mod project;
mod slop;
mod updater_mode;
+mod upscale;
mod weights;
mod window;
mod worker;
diff --git a/src-tauri/src/tests/images.rs b/src-tauri/src/tests/images.rs
index 541dd0c..449771a 100644
--- a/src-tauri/src/tests/images.rs
+++ b/src-tauri/src/tests/images.rs
@@ -125,25 +125,45 @@ fn image_export_resizes_and_reencodes_at_the_chosen_quality() {
let source = folder.path().join(saved.relative_path);
let original = fs::read(&source).unwrap();
let half = folder.path().join("half.png");
- export_image_file_with(&source, &half, ImageExportOptions { format: Some(ImageExportFormat::Png), width: Some(64), height: Some(40), quality: None }).unwrap();
+ export_image_file_with(&source, &half, ImageExportOptions { format: Some(ImageExportFormat::Png), width: Some(64), height: Some(40), quality: None, ..Default::default() }).unwrap();
assert_eq!(::image::image_dimensions(&half).unwrap(), (64, 40));
let enlarged = folder.path().join("enlarged.jpg");
- export_image_file_with(&source, &enlarged, ImageExportOptions { format: Some(ImageExportFormat::Jpg), width: Some(1280), height: Some(720), quality: Some(90) }).unwrap();
+ export_image_file_with(&source, &enlarged, ImageExportOptions { format: Some(ImageExportFormat::Jpg), width: Some(1280), height: Some(720), quality: Some(90), ..Default::default() }).unwrap();
let decoded = ::image::open(&enlarged).unwrap().to_rgb8();
assert_eq!(decoded.dimensions(), (1280, 720));
// Content fills the new canvas, including its far edge, rather than padding.
assert!(decoded.get_pixel(1279, 719)[2] > 100);
assert_eq!(fs::read(&source).unwrap(), original);
let low = folder.path().join("low.jpg");
- export_image_file_with(&source, &low, ImageExportOptions { format: Some(ImageExportFormat::Jpg), width: None, height: None, quality: Some(20) }).unwrap();
+ export_image_file_with(&source, &low, ImageExportOptions { format: Some(ImageExportFormat::Jpg), width: None, height: None, quality: Some(20), ..Default::default() }).unwrap();
let low = fs::read(low).unwrap();
assert_ne!(low, original);
assert!(low.len() < original.len());
let best = folder.path().join("best.jpg");
- export_image_file_with(&source, &best, ImageExportOptions { format: Some(ImageExportFormat::Jpg), width: None, height: None, quality: Some(100) }).unwrap();
+ export_image_file_with(&source, &best, ImageExportOptions { format: Some(ImageExportFormat::Jpg), width: None, height: None, quality: Some(100), ..Default::default() }).unwrap();
assert_eq!(fs::read(best).unwrap(), original);
let zero = folder.path().join("zero.jpg");
- assert!(export_image_file_with(&source, &zero, ImageExportOptions { format: None, width: Some(0), height: Some(40), quality: None }).is_err());
+ assert!(export_image_file_with(&source, &zero, ImageExportOptions { format: None, width: Some(0), height: Some(40), quality: None, ..Default::default() }).is_err());
+}
+
+#[test]
+fn image_export_does_not_silently_skip_a_selected_upscaler() {
+ use crate::commands::artifacts::{export_image_file_with, ImageExportOptions};
+ use crate::slopfab::upscale::{UpscaleConfig, UpscaleMethod};
+ let folder = tempfile::tempdir().unwrap();
+ let source = folder.path().join("source.png");
+ ::image::RgbaImage::from_pixel(32, 32, ::image::Rgba([40, 80, 120, 100])).save(&source).unwrap();
+ let original = fs::read(&source).unwrap();
+ for method in [UpscaleMethod::Realesrgan, UpscaleMethod::Seedvr2] {
+ let destination = folder.path().join("export.png");
+ let error = export_image_file_with(&source, &destination, ImageExportOptions {
+ upscale: Some(UpscaleConfig { method, model_path: folder.path().join("missing.safetensors").to_string_lossy().into(), vae_path: None }),
+ ..Default::default()
+ }).unwrap_err();
+ assert!(error.contains("weights are missing"));
+ assert!(!destination.exists());
+ assert_eq!(fs::read(&source).unwrap(), original);
+ }
}
#[test]
diff --git a/src-tauri/src/upscale.rs b/src-tauri/src/upscale.rs
new file mode 100644
index 0000000..75e587f
--- /dev/null
+++ b/src-tauri/src/upscale.rs
@@ -0,0 +1,179 @@
+use crate::slopfab::upscale::{self, UpscaleConfig};
+use std::{
+ collections::HashMap,
+ sync::{
+ atomic::{AtomicBool, Ordering},
+ mpsc::{self, Receiver, SyncSender},
+ Arc, LazyLock, Mutex,
+ },
+ time::Duration,
+};
+use tauri::ipc::{InvokeBody, Request, Response};
+
+enum Output {
+ Frame(Vec<u8>),
+ Done(Result<u64, String>),
+}
+struct Session {
+ input: Mutex<SyncSender<Option<Vec<u8>>>>,
+ output: Mutex<Receiver<Output>>,
+ stopped: Arc<AtomicBool>,
+ frame_bytes: usize,
+}
+static SESSIONS: LazyLock<Mutex<HashMap<String, Arc<Session>>>> = LazyLock::new(Default::default);
+fn session(id: &str) -> Result<Arc<Session>, String> {
+ SESSIONS
+ .lock()
+ .map_err(|e| e.to_string())?
+ .get(id)
+ .cloned()
+ .ok_or("Upscale session has ended.".into())
+}
+fn send<T>(sender: &SyncSender<T>, mut value: T, stop: &AtomicBool) -> Result<(), String> {
+ loop {
+ if stop.load(Ordering::Relaxed) {
+ return Err("Upscaling cancelled.".into());
+ }
+ match sender.try_send(value) {
+ Ok(()) => return Ok(()),
+ Err(mpsc::TrySendError::Full(pending)) => {
+ value = pending;
+ std::thread::sleep(Duration::from_millis(10));
+ }
+ Err(mpsc::TrySendError::Disconnected(_)) => {
+ return Err("Upscaler stream closed.".into())
+ }
+ }
+ }
+}
+
+#[tauri::command]
+pub(crate) fn start_export_upscale(
+ id: String,
+ config: UpscaleConfig,
+ input_width: u32,
+ input_height: u32,
+ width: u32,
+ height: u32,
+) -> Result<(), String> {
+ upscale::validate(&config, (input_width, input_height), (width, height))?;
+ let mut sessions = SESSIONS.lock().map_err(|e| e.to_string())?;
+ if !sessions.is_empty() {
+ return Err("Another upscale export is already running.".into());
+ }
+ let (input_tx, input_rx) = mpsc::sync_channel(2);
+ let (output_tx, output_rx) = mpsc::sync_channel(2);
+ let stopped = Arc::new(AtomicBool::new(false));
+ sessions.insert(
+ id,
+ Arc::new(Session {
+ input: Mutex::new(input_tx),
+ output: Mutex::new(output_rx),
+ stopped: stopped.clone(),
+ frame_bytes: input_width as usize * input_height as usize * 4,
+ }),
+ );
+ std::thread::spawn(move || {
+ let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
+ upscale::run(
+ &config,
+ (input_width, input_height),
+ (width, height),
+ 5,
+ &stopped,
+ &mut || loop {
+ if stopped.load(Ordering::Relaxed) {
+ return Err("Upscaling cancelled.".into());
+ }
+ match input_rx.recv_timeout(Duration::from_millis(50)) {
+ Ok(frame) => return Ok(frame),
+ Err(mpsc::RecvTimeoutError::Timeout) => continue,
+ Err(_) => return Err("Upscaler input closed.".into()),
+ }
+ },
+ &mut |frame| send(&output_tx, Output::Frame(frame), &stopped),
+ )
+ }))
+ .unwrap_or_else(|_| Err("Upscale worker panicked.".into()));
+ let _ = send(&output_tx, Output::Done(result), &stopped);
+ });
+ Ok(())
+}
+
+#[tauri::command]
+pub(crate) async fn push_export_upscale(request: Request<'_>) -> Result<(), String> {
+ let id = crate::commands::binary::decoded_header(&request, "x-upscale-id")?;
+ let session = session(&id)?;
+ let InvokeBody::Raw(bytes) = request.body() else {
+ return Err("Expected raw RGBA pixels.".into());
+ };
+ if bytes.len() != session.frame_bytes {
+ return Err("Invalid upscale frame size.".into());
+ }
+ let bytes = bytes.clone();
+ tauri::async_runtime::spawn_blocking(move || {
+ send(
+ &*session.input.lock().map_err(|e| e.to_string())?,
+ Some(bytes),
+ &session.stopped,
+ )
+ })
+ .await
+ .map_err(|e| e.to_string())?
+}
+#[tauri::command]
+pub(crate) async fn finish_export_upscale(id: String) -> Result<(), String> {
+ let session = session(&id)?;
+ tauri::async_runtime::spawn_blocking(move || {
+ send(
+ &*session.input.lock().map_err(|e| e.to_string())?,
+ None,
+ &session.stopped,
+ )
+ })
+ .await
+ .map_err(|e| e.to_string())?
+}
+#[tauri::command]
+pub(crate) async fn read_export_upscale(id: String) -> Result<Response, String> {
+ let session = session(&id)?;
+ tauri::async_runtime::spawn_blocking(move || {
+ let receiver = session.output.lock().map_err(|e| e.to_string())?;
+ loop {
+ if session.stopped.load(Ordering::Relaxed) {
+ return Err("Upscaling cancelled.".into());
+ }
+ match receiver.recv_timeout(Duration::from_millis(50)) {
+ Ok(Output::Frame(bytes)) => return Ok(Response::new(bytes)),
+ Ok(Output::Done(result)) => return result.map(|_| Response::new(Vec::<u8>::new())),
+ Err(mpsc::RecvTimeoutError::Timeout) => continue,
+ Err(_) => return Err("Upscaler output closed.".into()),
+ }
+ }
+ })
+ .await
+ .map_err(|e| e.to_string())?
+}
+#[tauri::command]
+pub(crate) fn cancel_export_upscale(id: String) -> Result<(), String> {
+ if let Some(session) = SESSIONS.lock().map_err(|e| e.to_string())?.remove(&id) {
+ session.stopped.store(true, Ordering::Relaxed);
+ }
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn cancellation_releases_a_producer_blocked_by_backpressure() {
+ let (sender, _receiver) = mpsc::sync_channel(1);
+ sender.send(1).unwrap();
+ let stop = Arc::new(AtomicBool::new(false));
+ let producer_stop = stop.clone();
+ let producer = std::thread::spawn(move || send(&sender, 2, &producer_stop));
+ stop.store(true, Ordering::Relaxed);
+ assert!(producer.join().unwrap().unwrap_err().contains("cancelled"));
+ }
+}