AI media harness for planning, generating, and editing locally
TypeScript
Preserve large images when editing bounded regions
Bittia320bd95c166
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.mdindex 7cacd95..a33f811 100644--- a/RELEASE_NOTES.md+++ b/RELEASE_NOTES.md@@ -42,6 +42,7 @@ Changes since 0.3.2.## Fixes and improvements+- Local image edits on larger originals use the selected resolution as the maximum rectangle or lasso size. Each crop is merged back into the original-sized image, preserving pixels outside the selected area. Requires updated workers.- Krea 2 and other generators without LoRA support no longer inherit adapters from the default generator when their own adapter list is omitted.- Single Shot without Context video applies Tone once across the assembled main clip and ending, avoiding separate grading of its temporary ending. Retained Previous video frames are preserved. Requires updated workers.- Shot speech language keeps the existing choices and also accepts custom language names.diff --git a/docs/slopfab-compatibility.md b/docs/slopfab-compatibility.mdindex 52d4768..e7681ef 100644--- a/docs/slopfab-compatibility.md+++ b/docs/slopfab-compatibility.md@@ -15,7 +15,7 @@ Video previews autoplay and loop inside the existing shot card player; imagepreviews update the image canvas and thumbnail. Both use the generation previewssetting and require the optional TAEH3 weights. Saved-latent upscaling remains supported.-LAN protocol is 27 so workers use the same Single Shot Tone and overlap behavior. Single Shot automatically shortens its preferred four-update+LAN protocol is 28 so workers use the same image crop merging, Single Shot Tone and overlap behavior. Single Shot automatically shortens its preferred four-updateCapstone delay when the resolved video schedule is shorter, without changing theLoRA recipe. Restart Slopus and update workers to load the new runtime.@@ -140,6 +140,26 @@ serialization, mask validation, calibration cancellation and callback paniccontainment through the bundled DLL. Full generation and visual LCS qualitywith model weights were not evaluated in this integration.+## Local edits on larger images++When an image editor's selected resolution is smaller than its source, rectangle+and lasso bounding boxes are limited to that resolution in original-image pixels.+The saved image retains its original dimensions. Each edit uses a target-sized+crop around its selection; the native sequence merges its result into the+original canvas before preparing the next crop. Pixels outside the rectangle or+lasso, including holes and transparency, remain unchanged. Rectangle feathering+blends inward once, even when the selection fills the crop. Edited results use PNG.++The optional `imageEdit.regionSize` carries the target width and height to local+generation and LAN workers. `canvasWidth`, `canvasHeight`, edit boxes and lasso+points continue to describe the original image. Planning and execution prepare+the same crop. Original files are never resized or overwritten for these jobs.+Protocol 28 prevents older workers from silently processing the full canvas.++Whole-image prompts still affect the full image: they run at the selected target+resolution and are resized back into the original canvas before local edits.+Crop previews are withheld so they cannot replace the full-image editor preview.+## Krea 2 image generationBundled Windows and Linux runtimes include Krea 2 on CUDA and Vulkan. Krea usesdiff --git a/src-tauri/core/src/slopfab/generation.rs b/src-tauri/core/src/slopfab/generation.rsindex 2bbe1f9..0a186f8 100644--- a/src-tauri/core/src/slopfab/generation.rs+++ b/src-tauri/core/src/slopfab/generation.rs@@ -139,7 +139,9 @@ fn run_edit_sequence(mut generate: impl FnMut(&QueueItem) -> Result<(OutputMetadata, rendered::RenderedVideo), String>,) -> Result<(OutputMetadata, rendered::RenderedVideo), String> {let Some(edit) = &item.request.image_edit else { return generate(item); };+ if item.cancel.load(Ordering::Acquire) { return Err("Image editing cancelled.".into()); }let count = edit.edits.len();+ let mut canvas = if edit.region_size.is_some() { Some(super::image_regions::load_source(&item.request)?) } else { None };let mut pixels = None;let mut elapsed = 0.0;let mut computed = 0;@@ -152,12 +154,18 @@ fn run_edit_sequence(if let Some(refmods) = &step.refmods { request.refmods = refmods.clone(); }request.image_edit.as_mut().unwrap().edits = vec![step.clone()];request.image_edit_pixels = pixels.take();+ let region = canvas.as_ref().map(|source| super::image_regions::RegionEdit::prepare(&request, source)).transpose()?;+ if let Some(region) = ®ion { request = region.request.clone(); }+ let cropped = region.is_some();let sink = item.events.clone();let offset = elapsed;let stage = QueueItem {request, configuration: item.configuration.clone(), references: item.references.clone(), cancel: item.cancel.clone(),events: std::sync::Arc::new(move |event| {let event = match event {+ // A crop preview has a different coordinate system from the+ // original canvas; retain the source until the merged result.+ super::events::NativeEvent::Preview(_) if cropped => return,super::events::NativeEvent::Progress(mut progress) if progress.stage != "calibrating" => {let per_edit = progress.planned_steps.max(1);progress.step = index as i32 * per_edit + progress.step.clamp(0, per_edit);@@ -174,6 +182,7 @@ fn run_edit_sequence(};let (mut metadata, result) = generate(&stage)?;if item.cancel.load(Ordering::Acquire) { return Err("Image editing cancelled.".into()); }+ if let (Some(region), Some(canvas)) = (®ion, &mut canvas) { region.merge(canvas, &result)?; }elapsed += metadata.seconds_total;computed += metadata.steps_computed;skipped += metadata.steps_skipped;@@ -182,8 +191,14 @@ fn run_edit_sequence(metadata.steps_computed = computed;metadata.steps_skipped = skipped;metadata.timing_profile.push_str(&format!("|imageEdits={count}"));+ if let Some(canvas) = canvas {+ metadata.width = canvas.width() as i32;+ metadata.height = canvas.height() as i32;+ return Ok((metadata, super::image_regions::rendered_image(&item.request.job_id, canvas)?));+ }return Ok((metadata, result));}+ if canvas.is_some() { continue; }let rgba = result.frame(0)?.ok_or("Image edit returned no pixels.")?;// Copy only the completed still, then release its generation before// starting the next edit. No intermediate file or lossy encode.diff --git a/src-tauri/core/src/slopfab/generation_tests.rs b/src-tauri/core/src/slopfab/generation_tests.rsindex 7a798c1..8d5552a 100644--- a/src-tauri/core/src/slopfab/generation_tests.rs+++ b/src-tauri/core/src/slopfab/generation_tests.rs@@ -114,6 +114,46 @@ fn item() -> QueueItem {events: Arc::new(|_| {}), cancel: Arc::new(AtomicBool::new(false)) }}+#[test]+fn cropped_edit_sequence_merges_each_result_into_the_original_canvas() {+ let folder = tempfile::tempdir().unwrap();+ let path = folder.path().join("source.png");+ let original = image::RgbaImage::from_pixel(129, 97, image::Rgba([10, 20, 30, 100]));+ original.save(&path).unwrap();+ let bytes = std::fs::read(&path).unwrap();+ let mut item = item();+ item.request = serde_json::from_value(serde_json::json!({+ "jobId":"crop-sequence", "prompt":"first", "frames":1, "stillImage":true, "steps":4, "seed":42,+ "canvasWidth":129, "canvasHeight":97, "imageEdit":{"sourceRelativePath":"source.png", "regionSize":{"width":64,"height":64}, "edits":[+ {"prompt":"first", "x":65,"y":33,"width":32,"height":32,"feather":0},+ {"prompt":"second", "x":81,"y":49,"width":32,"height":32,"feather":0}+ ]}+ })).unwrap();+ item.request.image_edit_path = Some(path.clone());+ let mut calls = 0;+ let (metadata, merged) = run_edit_sequence(&item, |stage| {+ assert_eq!((stage.request.canvas_width, stage.request.canvas_height), (64, 64));+ let input = stage.request.image_edit_pixels.as_ref().unwrap();+ if calls == 1 { assert!(input.chunks_exact(3).any(|pixel| pixel == [200, 0, 0])); }+ calls += 1;+ let color = if calls == 1 { [200, 0, 0, 255] } else { [0, 200, 0, 255] };+ let (mut metadata, _) = result(0);+ metadata.width = 64; metadata.height = 64;+ Ok((metadata, super::super::image_regions::rendered_image("crop-sequence", image::RgbaImage::from_pixel(64, 64, image::Rgba(color)))?))+ }).unwrap();+ assert_eq!(calls, 2);+ assert_eq!((metadata.width, metadata.height, merged.width, merged.height), (129, 97, 129, 97));+ let pixels = merged.frame(0).unwrap().unwrap();+ for y in 0..97 { for x in 0..129 {+ let expected = if (81..113).contains(&x) && (49..81).contains(&y) { [0, 200, 0, 255] }+ else if (65..97).contains(&x) && (33..65).contains(&y) { [200, 0, 0, 255] } else { [10, 20, 30, 100] };+ assert_eq!(&pixels[(y * 129 + x) * 4..(y * 129 + x + 1) * 4], &expected);+ }}+ assert_eq!(std::fs::read(&path).unwrap(), bytes);+ assert!(run_edit_sequence(&item, |_| Err("inference failed".into())).is_err());+ assert_eq!(std::fs::read(path).unwrap(), bytes);+}+fn single_shot_item() -> (QueueItem, tempfile::TempPath) {let source = super::super::continuation::tests::archive(124).into_temp_path();let mut item = item();diff --git a/src-tauri/core/src/slopfab/image_regions.rs b/src-tauri/core/src/slopfab/image_regions.rsnew file mode 100644index 0000000..7fb39f3--- /dev/null+++ b/src-tauri/core/src/slopfab/image_regions.rs@@ -0,0 +1,226 @@+//! Bounded inference crops are merged into a lossless, original-sized canvas.+use super::types::{GenerationRequest, ImageEditStep};+use crate::{image_mask, rendered};+use image::{imageops, RgbaImage};+use std::sync::Arc;++pub(super) fn load_source(request: &GenerationRequest) -> Result<RgbaImage, String> {+ let path = request.image_edit_path.as_ref().ok_or("Image edit source has not been resolved.")?;+ let size = image::image_dimensions(path).map_err(|e| format!("Could not read edit source: {e}"))?;+ if size != (request.canvas_width as u32, request.canvas_height as u32) {+ return Err("The source image dimensions changed. Reopen the image before editing.".into());+ }+ image::open(path).map(|image| image.into_rgba8()).map_err(|e| format!("Could not read edit source: {e}"))+}++pub(super) fn prepare_first(request: &GenerationRequest) -> Result<Option<RegionEdit>, String> {+ if request.image_edit.as_ref().and_then(|edit| edit.region_size.as_ref()).is_none() { return Ok(None); }+ RegionEdit::prepare(request, &load_source(request)?).map(Some)+}++pub(super) struct RegionEdit {+ pub request: GenerationRequest,+ x: u32,+ y: u32,+ width: u32,+ height: u32,+ step: ImageEditStep,+ mask: Option<Vec<u8>>,+}++impl RegionEdit {+ pub fn prepare(request: &GenerationRequest, source: &RgbaImage) -> Result<Self, String> {+ super::planning::validate_generation_controls(request)?;+ let edit = request.image_edit.as_ref().ok_or("Missing image edits.")?;+ let size = edit.region_size.as_ref().ok_or("Missing image edit target dimensions.")?;+ let step = edit.edits.first().ok_or("Add an image edit before generating.")?.clone();+ let whole = step.mask_polygon.is_none() && step.mask_contours.is_none()+ && step.x == 0 && step.y == 0 && step.width == request.canvas_width && step.height == request.canvas_height;+ let width = if whole { source.width() } else { (size.width as u32).min(source.width()) };+ let height = if whole { source.height() } else { (size.height as u32).min(source.height()) };+ let x = (step.x - (width as i32 - step.width) / 2).clamp(0, (source.width() - width) as i32) as u32;+ let y = (step.y - (height as i32 - step.height) / 2).clamp(0, (source.height() - height) as i32) as u32;+ let crop = imageops::crop_imm(source, x, y, width, height).to_image();+ // Whole-image prompts remain global, processed at the selected size.+ // Local edits use source pixels at 1:1 scale and never shrink the canvas.+ let crop = if whole { imageops::resize(&crop, size.width as u32, size.height as u32, imageops::FilterType::Lanczos3) } else { crop };+ let (cw, ch) = crop.dimensions();+ let mut prepared = request.clone();+ prepared.canvas_width = cw as i32;+ prepared.canvas_height = ch as i32;+ prepared.image_edit_pixels = Some(Arc::new(crop.pixels().flat_map(|pixel| [pixel[0], pixel[1], pixel[2]]).collect()));+ let prepared_edit = prepared.image_edit.as_mut().unwrap();+ prepared_edit.region_size = None;+ prepared_edit.edits = vec![step.clone()];+ let local = &mut prepared_edit.edits[0];+ local.x = if whole { 0 } else { step.x - x as i32 };+ local.y = if whole { 0 } else { step.y - y as i32 };+ local.width = if whole { cw as i32 } else { step.width };+ local.height = if whole { ch as i32 } else { step.height };+ // Rectangle feathering is applied once, during the final merge, even+ // when the selection fills its inference crop.+ local.feather = Some(0);+ let transform = |point: &mut image_mask::MaskPoint| {+ point.x = ((point.x - x as f64) * cw as f64 / width as f64).clamp(0.0, cw as f64);+ point.y = ((point.y - y as f64) * ch as f64 / height as f64).clamp(0.0, ch as f64);+ };+ for point in local.mask_polygon.iter_mut().flatten().chain(local.mask_contours.iter_mut().flatten().flatten()) { transform(point); }+ let mask = if let Some(contours) = &local.mask_contours {+ Some(image_mask::rasterize_contours(contours, cw as i32, ch as i32)?)+ } else if let Some(points) = &local.mask_polygon {+ Some(image_mask::rasterize_polygon(points, cw as i32, ch as i32)?)+ } else { None };+ Ok(Self { request: prepared, x, y, width, height, step, mask })+ }++ pub fn merge(&self, source: &mut RgbaImage, result: &rendered::RenderedVideo) -> Result<(), String> {+ let (cw, ch) = (self.request.canvas_width as u32, self.request.canvas_height as u32);+ if result.width != cw || result.height != ch || result.frame_count != 1 {+ return Err("The image edit returned unexpected crop dimensions.".into());+ }+ let pixels = result.frame(0)?.ok_or("Image edit returned no pixels.")?;+ let crop = RgbaImage::from_raw(cw, ch, pixels).ok_or("Image edit returned incomplete pixels.")?;+ let crop = if crop.dimensions() != (self.width, self.height) {+ imageops::resize(&crop, self.width, self.height, imageops::FilterType::Lanczos3)+ } else { crop };+ let step = &self.step;+ let whole = step.x == 0 && step.y == 0 && step.width == source.width() as i32 && step.height == source.height() as i32;+ let feather = if whole { 0 } else { step.feather.unwrap_or(16) };+ for y in step.y as u32..(step.y + step.height) as u32 {+ for x in step.x as u32..(step.x + step.width) as u32 {+ let (cx, cy) = (x - self.x, y - self.y);+ let coverage = if let Some(mask) = &self.mask {+ let mx = (cx as u64 * cw as u64 / self.width as u64) as usize;+ let my = (cy as u64 * ch as u64 / self.height as u64) as usize;+ mask[my * cw as usize + mx] as f32 / 255.0+ } else if feather > 0 {+ let distance = (x as i32 - step.x).min(y as i32 - step.y)+ .min(step.x + step.width - 1 - x as i32).min(step.y + step.height - 1 - y as i32);+ (distance as f32 / feather as f32).min(1.0)+ } else { 1.0 };+ if coverage == 0.0 { continue; }+ let generated = crop.get_pixel(cx, cy);+ let original = source.get_pixel_mut(x, y);+ // SlopFab already composites lasso coverage; do not blend it twice.+ let blend = if self.mask.is_some() { 1.0 } else { coverage };+ for channel in 0..3 { original[channel] = (original[channel] as f32 * (1.0 - blend) + generated[channel] as f32 * blend).round() as u8; }+ original[3] = (original[3] as f32 * (1.0 - coverage) + 255.0 * coverage).round() as u8;+ }+ }+ Ok(())+ }+}++struct Still(RgbaImage);+impl rendered::FrameSource for Still {+ fn frame_rgba(&self, _: u32, _: u32, _: u32) -> Result<Vec<u8>, String> { Ok(self.0.as_raw().clone()) }+}+pub(super) fn rendered_image(job_id: &str, image: RgbaImage) -> Result<rendered::RenderedVideo, String> {+ let (width, height) = image.dimensions();+ rendered::from_source(job_id, Box::new(Still(image)), vec![], 1, width, height, 3,+ width as usize * height as usize * 3, 1.0, 0, 0)+}++#[cfg(test)]+mod tests {+ use super::*;+ fn request() -> GenerationRequest {+ serde_json::from_value(serde_json::json!({+ "jobId":"crop", "prompt":"Change the flower", "frames":1, "stillImage":true, "steps":4, "seed":42,+ "canvasWidth":129, "canvasHeight":97, "imageEdit":{"sourceRelativePath":"source.png",+ "regionSize":{"width":64,"height":64}, "edits":[{"prompt":"Change the flower", "x":80,"y":50,"width":32,"height":32,"feather":0}]}+ })).unwrap()+ }+ fn source() -> RgbaImage { RgbaImage::from_fn(129, 97, |x, y| image::Rgba([x as u8, y as u8, 70, 120])) }+ fn generated(region: &RegionEdit) -> rendered::RenderedVideo {+ rendered_image("crop", RgbaImage::from_pixel(region.request.canvas_width as u32, region.request.canvas_height as u32, image::Rgba([250, 240, 230, 255]))).unwrap()+ }++ #[test]+ fn bounds_inference_and_merges_only_original_rectangle_including_alpha() {+ let mut source = source();+ let original = source.clone();+ let region = RegionEdit::prepare(&request(), &source).unwrap();+ assert_eq!((region.x, region.y, region.width, region.height), (64, 33, 64, 64));+ let step = ®ion.request.image_edit.as_ref().unwrap().edits[0];+ assert_eq!((step.x, step.y, step.width, step.height), (16, 17, 32, 32));+ assert_eq!(®ion.request.image_edit_pixels.as_ref().unwrap()[..3], &[64, 33, 70]);+ region.merge(&mut source, &generated(®ion)).unwrap();+ assert_eq!(source.dimensions(), (129, 97));+ for (x, y, pixel) in source.enumerate_pixels() {+ if (80..112).contains(&x) && (50..82).contains(&y) { assert_eq!(pixel.0, [250, 240, 230, 255]); }+ else { assert_eq!(pixel, original.get_pixel(x, y)); }+ }+ }++ #[test]+ fn lasso_holes_and_pixels_outside_the_outline_are_preserved() {+ let mut request = request();+ let points = |x: f64, y: f64, width: f64| vec![+ image_mask::MaskPoint { x, y }, image_mask::MaskPoint { x: x + width, y },+ image_mask::MaskPoint { x: x + width, y: y + width }, image_mask::MaskPoint { x, y: y + width }];+ request.image_edit.as_mut().unwrap().edits[0].mask_contours = Some(vec![points(80.0, 50.0, 32.0), points(88.0, 58.0, 16.0)]);+ let mut source = source();+ let original = source.clone();+ let region = RegionEdit::prepare(&request, &source).unwrap();+ region.merge(&mut source, &generated(®ion)).unwrap();+ for (x, y, pixel) in source.enumerate_pixels() {+ if (80..112).contains(&x) && (50..82).contains(&y) && !((88..104).contains(&x) && (58..74).contains(&y)) {+ assert_eq!(pixel.0, [250, 240, 230, 255]);+ } else { assert_eq!(pixel, original.get_pixel(x, y)); }+ }+ }++ #[test]+ fn feathering_stays_inside_a_selection_that_fills_the_crop() {+ let mut request = request();+ let step = &mut request.image_edit.as_mut().unwrap().edits[0];+ step.width = 64; step.height = 64; step.x = 65; step.y = 33; step.feather = Some(8);+ let mut source = source();+ let original = source.clone();+ let region = RegionEdit::prepare(&request, &source).unwrap();+ region.merge(&mut source, &generated(®ion)).unwrap();+ assert_eq!(source.get_pixel(65, 33), original.get_pixel(65, 33));+ assert_eq!(source.get_pixel(64, 50), original.get_pixel(64, 50));+ assert_eq!(source.get_pixel(90, 60).0, [250, 240, 230, 255]);+ assert_ne!(source.get_pixel(69, 50), original.get_pixel(69, 50));+ assert_ne!(source.get_pixel(69, 50).0, [250, 240, 230, 255]);+ }++ #[test]+ fn whole_image_prompts_keep_original_dimensions_with_target_sized_inference() {+ let mut request = request();+ let step = &mut request.image_edit.as_mut().unwrap().edits[0];+ step.x = 0; step.y = 0; step.width = 129; step.height = 97;+ let mut source = source();+ let region = RegionEdit::prepare(&request, &source).unwrap();+ assert_eq!((region.request.canvas_width, region.request.canvas_height), (64, 64));+ region.merge(&mut source, &generated(®ion)).unwrap();+ assert_eq!(source.dimensions(), (129, 97));+ assert!(source.pixels().all(|pixel| pixel.0 == [250, 240, 230, 255]));+ }++ #[test]+ fn native_planning_uses_the_same_crop_and_rejects_oversized_or_misaligned_areas() {+ let folder = tempfile::tempdir().unwrap();+ let path = folder.path().join("source.png");+ source().save(&path).unwrap();+ let mut request = request();+ request.image_edit_path = Some(path);+ let region = prepare_first(&request).unwrap().unwrap();+ let configuration = super::super::config::Configuration::from_settings(&Default::default());+ let api = super::super::ffi::Api::load(&configuration.dll_path).unwrap();+ for purpose in [super::super::planning::RequestPurpose::Plan, super::super::planning::RequestPurpose::Generate] {+ let handle = super::super::ffi::RequestHandle::new(&api).unwrap();+ super::super::h3::configure_request(&api, &handle, ®ion.request, &configuration,+ super::super::platform::ComputePlatform::Cuda13, purpose, &Default::default()).unwrap();+ let plan = api.resolve(&handle).unwrap();+ assert_eq!((plan.canvas_width, plan.canvas_height), (64, 64));+ }+ let edit = request.image_edit.as_mut().unwrap();+ edit.edits[0].x = 0; edit.edits[0].width = 65;+ assert!(RegionEdit::prepare(&request, &source()).err().unwrap().contains("target resolution"));+ request.image_edit.as_mut().unwrap().region_size.as_mut().unwrap().width = 63;+ assert!(RegionEdit::prepare(&request, &source()).err().unwrap().contains("multiples of 32"));+ }+}diff --git a/src-tauri/core/src/slopfab/mod.rs b/src-tauri/core/src/slopfab/mod.rsindex 51c24d2..f1227c8 100644--- a/src-tauri/core/src/slopfab/mod.rs+++ b/src-tauri/core/src/slopfab/mod.rs@@ -15,6 +15,7 @@ mod continuation;mod events;mod ffi;mod generation;+mod image_regions;mod latent_upscale;mod lcs;mod lcs_fingerprint;diff --git a/src-tauri/core/src/slopfab/planning.rs b/src-tauri/core/src/slopfab/planning.rsindex 59610a0..0ab5e21 100644--- a/src-tauri/core/src/slopfab/planning.rs+++ b/src-tauri/core/src/slopfab/planning.rs@@ -34,10 +34,11 @@ pub fn resolve_plan(}let platform = configuration.platform(detect_platform(&api));let handle = RequestHandle::new(&api)?;+ let region = super::image_regions::prepare_first(request)?;configure_request(&api,&handle,- request,+ region.as_ref().map_or(request, |region| ®ion.request),&configuration,platform,RequestPurpose::Plan,@@ -110,6 +111,25 @@ pub(super) fn validate_generation_controls(request: &GenerationRequest) -> Resulreturn Err("Latent upscale requires a video canvas of at least 64 pixels per side, in multiples of 32.".into());}if let Some(edit) = &request.image_edit {+ if let Some(size) = &edit.region_size {+ if !(32..=8192).contains(&size.width) || !(32..=8192).contains(&size.height)+ || size.width % 32 != 0 || size.height % 32 != 0 {+ return Err("Image edit target dimensions must be multiples of 32 between 32 and 8192.".into());+ }+ for step in &edit.edits {+ let whole = step.mask_polygon.is_none() && step.mask_contours.is_none()+ && step.x == 0 && step.y == 0 && step.width == request.canvas_width && step.height == request.canvas_height;+ if step.invert_mask || !whole && (step.width > size.width || step.height > size.height) {+ return Err("Local edit areas must fit within the target resolution.".into());+ }+ for point in step.mask_polygon.iter().flatten().chain(step.mask_contours.iter().flatten().flatten()) {+ if point.x < step.x as f64 - 1e-6 || point.y < step.y as f64 - 1e-6+ || point.x > step.x as f64 + step.width as f64 + 1e-6 || point.y > step.y as f64 + step.height as f64 + 1e-6 {+ return Err("Lasso points must stay inside the editable area's bounding box.".into());+ }+ }+ }+ }if !request.still_image || request.frames != 1 || request.continuation_relative_path.is_some() || request.continuation_path.is_some() || request.video_transition.is_some()|| edit.edits.is_empty() || edit.edits.len() > 500 || request.canvas_width > 8192 || request.canvas_height > 8192|| edit.edits.iter().any(|step| step.prompt.trim().is_empty() || step.x < 0 || step.y < 0 || step.width <= 0 || step.height <= 0diff --git a/src-tauri/core/src/slopfab/tests.rs b/src-tauri/core/src/slopfab/tests.rsindex b153e64..b68197b 100644--- a/src-tauri/core/src/slopfab/tests.rs+++ b/src-tauri/core/src/slopfab/tests.rs@@ -308,7 +308,7 @@ fn image_edit_recipe_disables_motion_cache_and_keeps_source_geometry() {prompt: "A red vase".into(), still_image: true, frames: 1, steps: 4, seed: 1,canvas_width: 65, canvas_height: 41,image_edit_pixels: Some(std::sync::Arc::new(vec![100; 65 * 41 * 3])),- image_edit: Some(types::ImageEditRequest { source_relative_path: "media/source.png".into(), edits: vec![+ image_edit: Some(types::ImageEditRequest { source_relative_path: "media/source.png".into(), region_size: None, edits: vec![types::ImageEditStep { mask_contours: None, mask_polygon: None, prompt: "A red vase".into(), x: 30, y: 10, width: 35, height: 31, feather: None, strength: None, invert_mask: false, outpaint_blend_overlap: None, outpaint_langevin_steps: None, reference_paths: None, refmods: None }] }), ..Default::default()};diff --git a/src-tauri/core/src/slopfab/types.rs b/src-tauri/core/src/slopfab/types.rsindex c4dec89..2f102c9 100644--- a/src-tauri/core/src/slopfab/types.rs+++ b/src-tauri/core/src/slopfab/types.rs@@ -150,9 +150,14 @@ pub struct SingleShotRequest {#[serde(rename_all = "camelCase")]pub struct ImageEditRequest {pub source_relative_path: String,+ /// Bound inference to a crop while keeping the original output canvas.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub region_size: Option<ImageEditRegionSize>,pub edits: Vec<ImageEditStep>,}#[derive(Debug, Clone, Deserialize, Serialize)]+pub struct ImageEditRegionSize { pub width: i32, pub height: i32 }+#[derive(Debug, Clone, Deserialize, Serialize)]#[serde(rename_all = "camelCase")]pub struct ImageEditStep {#[serde(default, skip_serializing_if = "Option::is_none")]diff --git a/src-tauri/core/src/worker/protocol.rs b/src-tauri/core/src/worker/protocol.rsindex c690d85..8d01221 100644--- a/src-tauri/core/src/worker/protocol.rs+++ b/src-tauri/core/src/worker/protocol.rs@@ -31,7 +31,8 @@ pub const SERVICE_TYPE: &str = "_slopus-worker._tcp.local.";// v25 emits MiniMax still-image previews as well as video previews.// v26 replaces Single Shot's 39-frame overlap, holding 22 and regenerating 17.// v27 grades Tone once after joining Single Shot's neutral ending and main clip.-pub const PROTOCOL_VERSION: u32 = 27;+// v28 bounds image-edit inference to the target region and preserves the original canvas.+pub const PROTOCOL_VERSION: u32 = 28;/// Streaming SeedVR2 restoration; model paths are resolved on the worker.#[derive(Debug, Clone, Serialize, Deserialize)]@@ -386,7 +387,31 @@ mod tests {let single = restored.single_shot.unwrap();assert_eq!(single.ending_prompt, "The subject waves goodbye");assert!(single.ending_path.is_none());- assert_eq!(PROTOCOL_VERSION, 27);+ assert_eq!(PROTOCOL_VERSION, 28);+ }++ #[test]+ fn regional_image_edits_keep_source_coordinates_and_target_limits_on_the_wire() {+ let mut request: GenerationRequest = serde_json::from_value(serde_json::json!({+ "jobId":"regional", "prompt":"Flower", "frames":1, "stillImage":true, "steps":4, "seed":42,+ "canvasWidth":4096,"canvasHeight":3072,"imageEdit":{"sourceRelativePath":"media/source.png",+ "regionSize":{"width":1024,"height":768},"edits":[{"prompt":"Flower","x":2048,"y":1536,"width":512,"height":512,+ "maskPolygon":[{"x":2048,"y":1536},{"x":2560,"y":1536},{"x":2048,"y":2048}],+ "referencePaths":["C:/project/media/source.png"]}]}+ })).unwrap();+ visit_paths(&mut request, &mut Default::default(), |kind, path| {+ assert_eq!(kind, PathKind::Input);+ *path = file_token(0);+ Ok(())+ }).unwrap();+ let restored: GenerationRequest = serde_json::from_slice(&serde_json::to_vec(&request).unwrap()).unwrap();+ assert_eq!((restored.canvas_width, restored.canvas_height), (4096, 3072));+ let edit = restored.image_edit.unwrap();+ let target = edit.region_size.unwrap();+ assert_eq!((target.width, target.height), (1024, 768));+ assert_eq!((edit.edits[0].x, edit.edits[0].y), (2048, 1536));+ assert_eq!(edit.edits[0].mask_polygon.as_ref().unwrap()[0].x, 2048.0);+ assert_eq!(edit.edits[0].reference_paths.as_ref().unwrap(), &[file_token(0)]);}#[test]diff --git a/src-tauri/src/commands/generation.rs b/src-tauri/src/commands/generation.rsindex d9045b4..48220f7 100644--- a/src-tauri/src/commands/generation.rs+++ b/src-tauri/src/commands/generation.rs@@ -359,6 +359,9 @@ fn prepare_image_edit_path(request: &mut slopfab::GenerationRequest, folder_pathlet root = crate::project::paths::ProjectRoot::open(folder_path.ok_or("Image edits require a project folder.")?)?;let mut path = root.existing(&edit.source_relative_path)?;let (width, height) = ::image::image_dimensions(&path).map_err(|error| format!("Could not read source image: {error}"))?;+ if edit.region_size.is_some() && (width != request.canvas_width as u32 || height != request.canvas_height as u32) {+ return Err("The source image dimensions changed. Reopen the image before editing.".into());+ }if width != request.canvas_width as u32 || height != request.canvas_height as u32 {// Native inpainting takes its canvas dimensions from the source file.// Prepare the selected size before planning or uploading to a LAN worker.@@ -409,6 +412,24 @@ mod image_edit_tests {request.canvas_width = 8193;assert!(prepare_image_edit_path(&mut request, folder.path().to_str()).is_err());}++ #[test]+ fn regional_edit_keeps_the_original_file_for_local_and_worker_generation() {+ let folder = tempfile::tempdir().unwrap();+ std::fs::write(folder.path().join("slopus.json"), "{}").unwrap();+ let source = folder.path().join("source.png");+ ::image::RgbaImage::from_pixel(1001, 809, ::image::Rgba([80, 120, 160, 128])).save(&source).unwrap();+ let mut request: slopfab::GenerationRequest = serde_json::from_value(serde_json::json!({+ "jobId":"regional", "prompt":"Flower", "frames":1, "stillImage":true, "steps":4, "seed":42, "canvasWidth":1001, "canvasHeight":809,+ "imageEdit":{"sourceRelativePath":"source.png", "regionSize":{"width":672,"height":544},+ "edits":[{"prompt":"Flower", "x":700,"y":600,"width":100,"height":100}]}+ })).unwrap();+ prepare_image_edit_path(&mut request, folder.path().to_str()).unwrap();+ assert_eq!(std::fs::canonicalize(request.image_edit_path.as_ref().unwrap()).unwrap(), std::fs::canonicalize(source).unwrap());+ assert!(!folder.path().join("cache/image-edits").exists());+ request.canvas_width = 672;+ assert!(prepare_image_edit_path(&mut request, folder.path().to_str()).unwrap_err().contains("dimensions changed"));+ }}#[tauri::command]diff --git a/src/components/workspace/ImageEditor.test.tsx b/src/components/workspace/ImageEditor.test.tsxindex 806b0b1..9c3401a 100644--- a/src/components/workspace/ImageEditor.test.tsx+++ b/src/components/workspace/ImageEditor.test.tsx@@ -256,7 +256,7 @@ it("edits imported-image generation settings and restores them with the draft",fireEvent.change(screen.getByLabelText("Seed"), { target: { value: "123456" } });choose("Resolution", "672 × 544");expect(current().imageScene).toMatchObject({ steps: 32, seed: 123456, outputSize: { width: 672, height: 544 } });- expect(compileImageEdits(current()).edits[0]).toMatchObject({ width: 672, height: 544 });+ expect(compileImageEdits(current()).edits[0]).toMatchObject({ width: 1001, height: 809 });expect(current().assets[0]).toMatchObject({ width: 1001, height: 809 });const reopened = parseProjectConfig(JSON.parse(JSON.stringify(current())));expect(reopened.imageScene).toEqual(current().imageScene);@@ -269,7 +269,7 @@ it("edits imported-image generation settings and restores them with the draft",expect(screen.getByLabelText("Seed")).toHaveValue(123456);expect(comboValue(screen.getByLabelText("Resolution"))).toBe("672x544");choose("Resolution", "992 × 800");- expect(compileImageEdits(current()).edits[0]).toMatchObject({ width: 992, height: 800 });+ expect(compileImageEdits(current()).edits[0]).toMatchObject({ width: 1001, height: 809 });});it.each(["media/photo.png", "references/photo.webp", "references/frames/frame.png"])("opens %s for editing after measuring its preview and preserves the original", async (relativePath) => {@@ -1202,6 +1202,34 @@ it("sets a temporary generation count, dismisses its panel and resets after oneexpect(screen.getByRole("spinbutton", { name: "Count" })).toHaveValue(1);});+it.each(["Edit Box", "Lasso"])("caps a drawn %s at the target resolution on a larger image", (tool) => {+ const initial = parseProjectConfig(fixture);+ initial.imageScene = createImageEditScene({ relativePath: "media/large.png", name: "Large", width: 4096, height: 4096 });+ initial.imageScene.outputSize = { width: 512, height: 512 };+ const current = setup(initial);+ const canvas = screen.getByLabelText("Image placement canvas");+ Object.defineProperty(canvas, "setPointerCapture", { value: vi.fn() });+ Object.defineProperty(canvas, "getBoundingClientRect", { value: () => ({ left: 0, top: 0, width: 1000, height: 1000 }) });+ const pointer = (type: "pointerDown" | "pointerMove" | "pointerUp", x: number, y: number, target: Element = canvas) => {+ const event = createEvent[type](target);+ Object.defineProperties(event, { button: { value: 0 }, pointerId: { value: 1 }, clientX: { value: x }, clientY: { value: y } });+ fireEvent(target, event);+ };+ fireEvent.click(screen.getByRole("radio", { name: tool }));+ pointer("pointerDown", 500, 500);+ pointer("pointerMove", 900, 500);+ pointer("pointerUp", tool === "Lasso" ? 500 : 900, 900);+ expect(current().imageScene!.nodes[1].box).toEqual({ x: 500, y: 500, width: 125, height: 125 });+ typePrompt(screen.getByLabelText("Edit prompt"), "A red flower");+ expect(compileImageEdits(current()).edits[0]).toMatchObject({ x: 2048, y: 2048, width: 512, height: 512 });+ if (tool === "Edit Box") {+ const handle = canvas.querySelector("[data-resize]")!;+ pointer("pointerDown", 625, 625, handle); pointer("pointerUp", 1000, 1000);+ expect(current().imageScene!.nodes[1].box).toEqual({ x: 500, y: 500, width: 125, height: 125 });+ }+ expect(current().imageScene!.sourceImage).toMatchObject({ width: 4096, height: 4096 });+});+it("draws a lasso, edits its prompt, moves its outline, and saves it independently", () => {const initial = parseProjectConfig(fixture);initial.assets = [{ id: "source", name: "Source", kind: "image", relativePath: "media/source.png", mimeType: "image/png", width: 1000, height: 800, createdAt: initial.createdAt }];diff --git a/src/components/workspace/ImageEditor.tsx b/src/components/workspace/ImageEditor.tsxindex cc186a2..5b52f21 100644--- a/src/components/workspace/ImageEditor.tsx+++ b/src/components/workspace/ImageEditor.tsx@@ -9,7 +9,7 @@ import { useEffect, useId, useMemo, useRef, useState, useSyncExternalStore, typeimport { Add14, Add16, Boxes16, Boxes16Filled, ChevronDown14, ChevronRight14, Copy16, Cube16, Cube16Filled, Cursor16, Cursor16Filled, Delete14, Delete16, Edit16, Film16, FolderAdd16, FolderOpen16, Group16, Group16Filled, Image16, Image32, Lasso16, Paste16, References16, Rename16, Sparkle16, Stop14, Text16, Text16Filled } from "../ui/icons";import { invoke } from "@tauri-apps/api/core";import { addImageNode, createImageEditScene, createImageScene, duplicateImageNode, imageDescendants, imageScenePrompt, imageLassoPath, imageLassoSchema, simplifyImageLasso, removeImageNode, resizeImageNode, type ImageBox, type ImageNode, type ImageScene, type ImageSource, type ImagePoint } from "../../lib/imageScene";-import { compileImageEdits, editGeneratedImage, imageEditDebugPrompt, imageEditOutputSize, imageEditSize } from "../../lib/imageEditing";+import { compileImageEdits, editGeneratedImage, imageEditDebugPrompt, imageEditOutputSize, imageEditSize, imageEditRegionSize, limitImageEditBox, limitImageEditScene } from "../../lib/imageEditing";import { imageSettings } from "../../lib/imageSettings";import { outputDimensions } from "../../lib/export";import { compileImagePrompt } from "../../lib/imagePrompt";@@ -65,7 +65,7 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGeneonImageOpened?: () => void;onBackNavigationChange?: BackNavigationChange;}) {- const scene = useMemo(() => config.imageScene ?? { ...createImageScene(config.brief.prompt), steps: selectedImageGeneratorTemplate().defaultSteps }, [config.imageScene, config.brief.prompt]);+ const scene = useMemo(() => limitImageEditScene(config.imageScene ?? { ...createImageScene(config.brief.prompt), steps: selectedImageGeneratorTemplate().defaultSteps }), [config.imageScene, config.brief.prompt]);const settings = imageSettings(config);const root = scene.nodes.find((node) => node.kind === "root")!;const imageRoot = scene.rootType === "image";@@ -224,6 +224,7 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGeneuseEffect(() => subscribeGeneratorTemplates(() => setTemplates(loadGeneratorTemplateSettings())), []);const commit = (next: ImageScene) => {+ next = limitImageEditScene(next);if (JSON.stringify(next) === JSON.stringify(scene)) return;undo.current = [...undo.current.slice(-99), scene]; redo.current = [];onChange((current) => ({ ...current, imageScene: { ...next, outputAssetId: current.imageScene?.outputAssetId ?? null } }));@@ -400,6 +401,14 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGeneif (!Number.isFinite(at.x) || !Number.isFinite(at.y)) return;const original = start.scene.nodes.find((node) => node.id === start.id)?.box;if (start.lasso) {+ const limit = imageEditRegionSize(start.scene);+ if (limit) {+ const xs = start.lasso.map((point) => point.x), ys = start.lasso.map((point) => point.y);+ const maxWidth = limit.width / start.scene.sourceImage!.width * 1000;+ const maxHeight = limit.height / start.scene.sourceImage!.height * 1000;+ at.x = Math.max(Math.max(...xs) - maxWidth, Math.min(Math.min(...xs) + maxWidth, at.x));+ at.y = Math.max(Math.max(...ys) - maxHeight, Math.min(Math.min(...ys) + maxHeight, at.y));+ }if (Math.hypot(at.x - start.lasso.at(-1)!.x, at.y - start.lasso.at(-1)!.y) < 2) return;start.lasso.push(at);if (start.lasso.length > 2048) start.lasso = start.lasso.filter((_, index) => index % 2 === 0);@@ -411,6 +420,7 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGeneconst y = start.resize ? original.y : Math.max(0, Math.min(1000 - original.height, original.y + at.y - start.y));start.box = { x, y, width: start.resize ? Math.max(1, Math.min(1000 - x, original.width + at.x - start.x)) : original.width, height: start.resize ? Math.max(1, Math.min(1000 - y, original.height + at.y - start.y)) : original.height };} else start.box = { x: Math.min(start.x, at.x), y: Math.min(start.y, at.y), width: Math.abs(at.x - start.x), height: Math.abs(at.y - start.y) };+ start.box = limitImageEditBox(start.scene, start.box);setDraftBox(start.box);};const pointerUp = (event: PointerEvent<SVGSVGElement>) => {@@ -424,6 +434,11 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGeneattempt(() => {const mode = event.altKey ? "subtract" : event.ctrlKey ? "add" : "new";const result = applyLassoStroke(start.scene, points, mode, selected.id);+ const limit = imageEditRegionSize(result.scene);+ if (limit && result.scene.nodes.some((node) => node.lasso && node.box &&+ (node.box.width * result.scene.sourceImage!.width / 1000 > limit.width + 1e-6 || node.box.height * result.scene.sourceImage!.height / 1000 > limit.height + 1e-6))) {+ throw new Error(`The combined lasso must fit within ${limit.width} × ${limit.height} pixels. Use separate edits for a larger area.`);+ }commit(result.scene); setSelection(result.id);setCollapsed((current) => { const next = new Set(current); next.delete(root.id); return next; });if (mode === "new") setDrawKind(null);@@ -843,6 +858,9 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene{!imageRoot && <PropRow label="Aspect ratio" htmlFor={rootField("ratio")}><ComboBox id={rootField("ratio")} aria-label="Aspect ratio" value={settings.aspectRatio} options={["16:9", "9:16", "1:1", "4:5"].map((ratio) => ({ value: ratio, label: ratio }))} onChange={(value) => { const aspectRatio = value as ProjectConfig["settings"]["aspectRatio"]; onChange((current) => ({ ...current, imageSettings: { ...imageSettings(current), aspectRatio } })); }} /></PropRow>}{imageRoot ? editSize && <PropRow label="Resolution" htmlFor={rootField("resolution")}><ComboBox id={rootField("resolution")} aria-label="Resolution" value={sizeValue(editSize)} options={editSizes.map((size) => ({ value: sizeValue(size), label: `${size.width} × ${size.height}` }))} onChange={(value) => { const size = editSizes.find((size) => sizeValue(size) === value); if (size) commit({ ...scene, outputSize: size }); }} /></PropRow> : <PropRow label="Resolution" htmlFor={rootField("resolution")}><ComboBox id={rootField("resolution")} aria-label="Resolution" value={settings.resolution} options={[...new Set([...PROJECT_RESOLUTIONS, settings.resolution])].map((resolution) => { const size = outputDimensions(resolution, settings.aspectRatio); return { value: resolution, label: `${size.width} × ${size.height}` }; })} onChange={(value) => { const resolution = value as ProjectConfig["settings"]["resolution"]; onChange((current) => ({ ...current, imageSettings: { ...imageSettings(current), resolution } })); }} /></PropRow>}</PropSection>+ {imageRoot && imageEditRegionSize(scene) && <PropRow label="Saved image" tooltip="Local boxes and lassos are limited to the selected resolution. Their edits are merged into the original image.">+ <span>{scene.sourceImage!.width} × {scene.sourceImage!.height}</span>+ </PropRow>}{!imageRoot && <PropSection title="Style" persistKey="image.style"><PropRow label="Mode" htmlFor={rootField("mode")}><ComboBox id={rootField("mode")} aria-label="Mode" value={scene.style.mode} options={[{ value: "none", label: "None" }, { value: "photo", label: "Photo" }, { value: "art", label: "Art" }]} onChange={(value) => commit({ ...scene, style: { ...scene.style, mode: value as "none" | "photo" | "art" } })} /></PropRow>{scene.style.mode !== "none" && <>diff --git a/src/lib/imageEditing.test.ts b/src/lib/imageEditing.test.tsindex a6cb627..80999dd 100644--- a/src/lib/imageEditing.test.ts+++ b/src/lib/imageEditing.test.ts@@ -1,7 +1,7 @@import { expect, it } from "vitest";import { parseProjectConfig } from "./project";import { addImageNode, createImageEditScene } from "./imageScene";-import { compileImageEdits, editGeneratedImage, imageEditSize } from "./imageEditing";+import { compileImageEdits, editGeneratedImage, imageEditSize, imageEditCanvasSize, limitImageEditScene } from "./imageEditing";import fixture from "../../fixtures/project-v1-image.json";it.each([@@ -10,12 +10,13 @@ it.each([expect(imageEditSize({ width, height })).toEqual({ width: expectedWidth, height: expectedHeight });});-it("scales local edits to the selected output size and validates saved dimensions", () => {+it("keeps local edits in original pixels when the target is smaller and validates saved dimensions", () => {const config = parseProjectConfig(fixture);config.imageScene = addImageNode(createImageEditScene({ relativePath: "media/source.png", name: "Source", width: 1001, height: 809 }), "image-root", "object", { x: 750, y: 750, width: 250, height: 250 });config.imageScene.nodes[1].description = "Add a balloon";config.imageScene.outputSize = { width: 672, height: 544 };- expect(compileImageEdits(config).edits[0]).toMatchObject({ x: 504, y: 408, width: 168, height: 136 });+ expect(compileImageEdits(config).edits[0]).toMatchObject({ x: 750, y: 606, width: 251, height: 203 });+ expect(imageEditCanvasSize(config.imageScene)).toEqual({ width: 1001, height: 809 });expect(parseProjectConfig(JSON.parse(JSON.stringify(config))).imageScene).toEqual(config.imageScene);config.imageScene.outputSize.width = 673;expect(() => parseProjectConfig(config)).toThrow();@@ -112,17 +113,34 @@ it("applies whole-image actions first and compiles only the references each editexpect(() => compileImageEdits(config)).toThrow("The prompt cites a reference that no longer exists.");});-it("disables feathering for a full-canvas edit box at the selected resolution", () => {+it("caps a local box at the target while keeping feathering against the larger original", () => {const config = parseProjectConfig(fixture);config.imageScene = addImageNode(createImageEditScene({ relativePath: "media/source.png", name: "Source", width: 1001, height: 809 }), "image-root", "object", { x: 0, y: 0, width: 1000, height: 1000 });config.imageScene.outputSize = { width: 672, height: 544 };config.imageScene.nodes[1].description = "Warm up the lighting";- expect(compileImageEdits(config).edits[0]).toMatchObject({ x: 0, y: 0, width: 672, height: 544, feather: 0 });+ expect(compileImageEdits(config).edits[0]).toMatchObject({ x: 0, y: 0, width: 672, height: 544 });+ expect(compileImageEdits(config).edits[0].feather).toBeUndefined();// A local edit touching the canvas edge still blends with the rest of the image.config.imageScene.nodes[1].box!.width = 500;expect(compileImageEdits(config).edits[0].feather).toBeUndefined();});+it("bounds lasso outlines and multiple local edits without moving their source positions", () => {+ const config = parseProjectConfig(fixture);+ let scene = createImageEditScene({ relativePath: "source.png", name: "Large", width: 4096, height: 3072 });+ scene.outputSize = { width: 1024, height: 768 };+ scene = addImageNode(scene, "image-root", "object", { x: 500, y: 500, width: 500, height: 500 });+ Object.assign(scene.nodes[1], { description: "A flower", lasso: [{ x: 0, y: 0 }, { x: 1000, y: 0 }, { x: 0, y: 1000 }] });+ scene = addImageNode(scene, "image-root", "object", { x: 100, y: 100, width: 50, height: 50 });+ scene.nodes[2].description = "A bee";+ config.imageScene = limitImageEditScene(scene);+ expect(config.imageScene.nodes[1].box).toEqual({ x: 500, y: 500, width: 250, height: 250 });+ const edits = compileImageEdits(config).edits;+ expect(edits[0]).toMatchObject({ x: 2048, y: 1536, width: 1024, height: 768,+ maskPolygon: [{ x: 2048, y: 1536 }, { x: 3072, y: 1536 }, { x: 2048, y: 2304 }] });+ expect(edits[1]).toMatchObject({ x: 409, y: 307, width: 206, height: 154 });+});+it("uses plain Fibo edit instructions, preserves structured JSON and rejects extra pictures", () => {const config = parseProjectConfig(fixture);config.imageScene = createImageEditScene({ name: "Source", relativePath: "media/source.png", width: 512, height: 512 });diff --git a/src/lib/imageEditing.ts b/src/lib/imageEditing.tsindex 4e7ea90..0d4b0d3 100644--- a/src/lib/imageEditing.ts+++ b/src/lib/imageEditing.ts@@ -1,6 +1,6 @@import { FIBO_MODEL_TYPE } from "./settings";import { imageSettings } from "./imageSettings";-import { createImageEditScene, createImageScene, imageLassoContours, imageSourceSchema, type ImageNode, type ImageScene, type ImageSource, type ImagePoint } from "./imageScene";+import { createImageEditScene, createImageScene, imageLassoContours, imageSourceSchema, type ImageBox, type ImageNode, type ImageScene, type ImageSource, type ImagePoint } from "./imageScene";import { compileImagePrompt } from "./imagePrompt";import type { ProjectConfig, ProjectReference } from "./project";@@ -30,11 +30,32 @@ export function imageEditSize(source: Pick<ImageSource, "width" | "height">, shoexport function imageEditOutputSize(scene: ImageScene) {return scene.outputSize ?? imageEditSize(scene.sourceImage!);}+/** An explicitly smaller target limits local inference, not the saved canvas. */+export function imageEditRegionSize(scene: ImageScene) {+ const source = scene.sourceImage, target = scene.outputSize;+ return source && target && (source.width > target.width || source.height > target.height) ? target : undefined;+}+export function imageEditCanvasSize(scene: ImageScene) {+ return imageEditRegionSize(scene) ? { width: scene.sourceImage!.width, height: scene.sourceImage!.height } : imageEditOutputSize(scene);+}+export function limitImageEditBox(scene: ImageScene, box: ImageBox): ImageBox {+ const limit = imageEditRegionSize(scene);+ if (!limit) return box;+ const source = scene.sourceImage!;+ // Use pixel boundaries so outward rounding cannot exceed the target by a pixel.+ const x = Math.floor(box.x * source.width / 1000), y = Math.floor(box.y * source.height / 1000);+ return { ...box, width: Math.min(box.width, (x + Math.min(limit.width, source.width - x)) * 1000 / source.width - box.x),+ height: Math.min(box.height, (y + Math.min(limit.height, source.height - y)) * 1000 / source.height - box.y) };+}+export function limitImageEditScene(scene: ImageScene): ImageScene {+ if (!imageEditRegionSize(scene)) return scene;+ return { ...scene, nodes: scene.nodes.map((node) => node.kind !== "root" && node.box ? { ...node, box: limitImageEditBox(scene, node.box) } : node) };+}export function compileImageEdits(config: ProjectConfig, options: { modelType?: string } = {}): { source: ImageSource; edits: (ImageEditStep & { references: ProjectReference[] })[] } {- const scene = config.imageScene!;+ const scene = limitImageEditScene(config.imageScene!);const source = scene.sourceImage;if (!source) throw new Error("Open an image before adding edits.");- const size = imageEditOutputSize(scene);+ const size = imageEditCanvasSize(scene);const root = scene.nodes.find((node) => node.kind === "root")!;// The root describes a whole-image edit, followed by the local edits in order.const ordered: ImageNode[] = root.description.trim() ? [{ ...root, box: { x: 0, y: 0, width: 1000, height: 1000 } }] : [];@@ -45,8 +66,9 @@ export function compileImageEdits(config: ProjectConfig, options: { modelType?:if (!node.description.trim() && !node.text) throw new Error(`Describe the edit for '${node.name}'.`);const x = Math.min(size.width - 1, Math.floor(node.box.x * size.width / 1000));const y = Math.min(size.height - 1, Math.floor(node.box.y * size.height / 1000));- const right = Math.min(size.width, Math.ceil((node.box.x + node.box.width) * size.width / 1000));- const bottom = Math.min(size.height, Math.ceil((node.box.y + node.box.height) * size.height / 1000));+ const limit = node.kind === "root" ? undefined : imageEditRegionSize(scene);+ const right = Math.min(size.width, x + (limit?.width ?? size.width), Math.ceil((node.box.x + node.box.width) * size.width / 1000));+ const bottom = Math.min(size.height, y + (limit?.height ?? size.height), Math.ceil((node.box.y + node.box.height) * size.height / 1000));const description = [options.modelType === FIBO_MODEL_TYPE ? "" : "The edited keyframe corresponds to <Picture 1> with the following change. Preserve the composition and any previously completed edits.", node.description,node.text ? `Render the exact text "${node.text}".` : ""].filter(Boolean).join(" ");const editScene = { ...createImageScene(description), rootType: "image" as const, sourceImage: source, referenceIds: [] };diff --git a/src/lib/runtime.ts b/src/lib/runtime.tsindex 1058950..ca0655a 100644--- a/src/lib/runtime.ts+++ b/src/lib/runtime.ts@@ -112,7 +112,7 @@ export interface SlopfabGenerationRequest {prompt: string;frames: number;stillImage?: boolean;- imageEdit?: { sourceRelativePath: string; edits: import("./imageEditing").ImageEditStep[] };+ imageEdit?: { sourceRelativePath: string; regionSize?: { width: number; height: number }; edits: import("./imageEditing").ImageEditStep[] };steps: number;audioSteps?: number;guidanceScale?: number;diff --git a/src/lib/workQueue.test.ts b/src/lib/workQueue.test.tsindex 3ba5755..a4da616 100644--- a/src/lib/workQueue.test.ts+++ b/src/lib/workQueue.test.ts@@ -1078,8 +1078,8 @@ describe("image generation work", () => {const session = queue.project(record);queue.enqueueImage(session, template);await waitFor(() => expect(enqueueSlopfabGeneration).toHaveBeenCalledOnce());- const expected = { steps: 32, seed: 123456, canvasWidth: 672, canvasHeight: 544,- imageEdit: { sourceRelativePath: "media/source.png", edits: [expect.objectContaining({ x: 0, y: 0, width: 672, height: 544, feather: 0 })] } };+ const expected = { steps: 32, seed: 123456, canvasWidth: 1001, canvasHeight: 809,+ imageEdit: { sourceRelativePath: "media/source.png", regionSize: { width: 672, height: 544 }, edits: [expect.objectContaining({ x: 0, y: 0, width: 1001, height: 809, feather: 0 })] } };expect(vi.mocked(resolveSlopfabPlan).mock.calls[0][0]).toMatchObject(expected);const request = vi.mocked(enqueueSlopfabGeneration).mock.calls[0][0];expect(request).toMatchObject(expected);@@ -1101,7 +1101,7 @@ describe("image generation work", () => {queue.enqueueImage(session, template);await waitFor(() => expect(enqueueSlopfabGeneration).toHaveBeenCalledTimes(index + 1));const request = vi.mocked(enqueueSlopfabGeneration).mock.calls[index][0];- expect(request).toMatchObject({ seed: 101 + index, canvasWidth: 672, canvasHeight: 544,+ expect(request).toMatchObject({ seed: 101 + index, canvasWidth: 1001, canvasHeight: 809,imageEdit: { sourceRelativePath: "media/source.png", edits: [expect.objectContaining({ prompt: expect.stringContaining(index === 2 ? "Make it moonlit" : "Warm up the lighting") })] } });vi.mocked(invoke).mockResolvedValue({ relativePath: `media/edited-${index}.png`, width: 672, height: 544 });await finish(queue, request.jobId);diff --git a/src/lib/workQueue.ts b/src/lib/workQueue.tsindex 688a5d7..52dcf25 100644--- a/src/lib/workQueue.ts+++ b/src/lib/workQueue.ts@@ -8,7 +8,7 @@ import { imageSettings } from "./imageSettings";import { listen } from "@tauri-apps/api/event";import { invoke } from "@tauri-apps/api/core";import { createImageEditScene, createImageScene, imageScenePrompt } from "./imageScene";-import { compileImageEdits, imageEditDebugPrompt, imageEditOutputSize } from "./imageEditing";+import { compileImageEdits, imageEditDebugPrompt, imageEditCanvasSize, imageEditRegionSize } from "./imageEditing";import { compileImagePrompt } from "./imagePrompt";import { compileExtendImage, type ExtendOptions } from "./extendImage";import { CHARACTER_SHEET_ORDER, CHARACTER_SHEET_VIEWS, characterSheetDimensions, characterSheetViewIndices, characterSheetViews, compileCharacterSheet, type CharacterSheetOptions } from "./characterSheet";@@ -352,7 +352,7 @@ export class WorkQueue {const projectKey = projectQueueKey(session.record);if (this.items.some((item) => item.projectKey === projectKey && item.kind === "image" && (item.imageDraftId ?? item.imageAssetId) === scene.outputAssetId && isWorkActive(item))) return;const { prompt, references } = edit ? edit.edits[0] : compileImagePrompt(current, { modelType: template.modelType });- const { width, height } = edit ? imageEditOutputSize(scene) : outputDimensions(imageSettings(current).resolution, imageSettings(current).aspectRatio);+ const { width, height } = edit ? imageEditCanvasSize(scene) : outputDimensions(imageSettings(current).resolution, imageSettings(current).aspectRatio);const id = `image-${crypto.randomUUID()}`;const negativePrompt = generatorModel(template.modelType).negativePrompt ? scene.negativePrompts?.[template.modelType] : undefined;const guidanceScale = imageCfgScale(template.modelType, scene.cfgScales, template.kreaVariant);@@ -386,7 +386,7 @@ export class WorkQueue {let finish!: () => void;const done = new Promise<void>((resolve) => { finish = resolve; });const request: SlopfabGenerationRequest = { ...(negativePrompt ? { negativePrompt } : {}), ...(guidanceScale !== undefined ? { guidanceScale } : {}), generationEffects: activeGenerationEffects(scene.generationEffects), jobId: entry.id, stillImage: true, frames: 1, prompt: edit ? edit.edits[0].prompt : prompt,- ...(edit ? { imageEdit: { sourceRelativePath: edit.source.relativePath, edits: edit.edits.map(({ references, ...step }) => ({+ ...(edit ? { imageEdit: { sourceRelativePath: edit.source.relativePath, regionSize: imageEditRegionSize(scene), edits: edit.edits.map(({ references, ...step }) => ({...step, referencePaths: referencePaths(references), refmods: referenceRefmodInputs(session.record.folderPath, references),})) } } : {}),canvasWidth: width, canvasHeight: height, steps: generationStepsWithLoras(scene.steps, config), seed,