Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Add lasso image editing masks

Bitti74fa078bfe45
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index aeaecf7..0442b45 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -4,6 +4,8 @@ This alpha update adds Prompt video scenes, agent image drafts, Sol attention an
## What's new
+- **Lasso image edits.** Draw a freehand area beside the existing box tool, give it a prompt in the right panel, and select or move it from the canvas or Scene tree. Saved outlines become antialiased Slopfab masks for local and LAN generation, preserving pixels outside the selection. Bundled runtimes now use API 1.32; LAN workers require the matching update.
+
- **FastH3 Omni.** Renamed First/Last Frame Fast and updated its transformer to the FastH3 Omni eight-step model. Existing bundled settings migrate automatically.
- **Generator model pages.** Open MiniMax H3, MOSS or Other from Generator settings. Each model has its own generators and LoRAs; Other contains the existing extra weights. New generators use the selected model.
diff --git a/lib/slopfab/libslopfab.so b/lib/slopfab/libslopfab.so
index ffc7055..138797c 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:93d602150b7967b3e299e53aa40136c3d915e5a5ebf93455562a80f52cc77879
-size 21338136
+oid sha256:eea1e10b19f54853e3b520fa5883aef82adce950f14bd784408d16b926bedd24
+size 21347864
diff --git a/lib/slopfab/slopfab.dll b/lib/slopfab/slopfab.dll
index 303ad97..e383dae 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:78eefe128a8a3e9b9655f8fbeb999d7b3df459df779fd464f0f4934287c67afa
-size 18542080
+oid sha256:28d2c329f6747027f503e5d5742e4e9bbdfe44163e1c45d0e358d9f304a0ca8e
+size 18546688
diff --git a/src-tauri/core/src/image_mask.rs b/src-tauri/core/src/image_mask.rs
new file mode 100644
index 0000000..66c655b
--- /dev/null
+++ b/src-tauri/core/src/image_mask.rs
@@ -0,0 +1,84 @@
+//! Resolution-independent lasso outlines become exact pixel masks at submission.
+use serde::{Deserialize, Serialize};
+
+#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
+#[serde(deny_unknown_fields)]
+pub struct MaskPoint { pub x: f64, pub y: f64 }
+
+pub fn validate_polygon(points: &[MaskPoint], width: i32, height: i32) -> Result<(), String> {
+ if !(1..=8192).contains(&width) || !(1..=8192).contains(&height) || !(3..=2048).contains(&points.len())
+ || points.iter().any(|p| !p.x.is_finite() || !p.y.is_finite() || p.x < 0.0 || p.y < 0.0 || p.x > width as f64 || p.y > height as f64) {
+ return Err("Lasso masks require 3 to 2048 finite points inside the image.".into());
+ }
+ let first = &points[0];
+ let nonempty = points.iter().find(|p| p.x != first.x || p.y != first.y).is_some_and(|second|
+ points.iter().any(|p| ((second.x - first.x) * (p.y - first.y) - (second.y - first.y) * (p.x - first.x)).abs() > 1e-9));
+ if !nonempty { return Err("The lasso must enclose an area.".into()); }
+ Ok(())
+}
+
+/// Even-odd fill, four vertical samples and exact horizontal coverage. Only
+/// the edge pixels blend; pixels outside the outline remain zero. Row storage
+/// stays bounded even for the largest supported canvas.
+pub fn rasterize_polygon(points: &[MaskPoint], width: i32, height: i32) -> Result<Vec<u8>, String> {
+ validate_polygon(points, width, height)?;
+ let mut mask = vec![0; width as usize * height as usize];
+ let mut intersections = Vec::with_capacity(points.len());
+ let mut coverage = vec![0.0_f64; width as usize];
+ let top = points.iter().map(|p| p.y).fold(f64::INFINITY, f64::min).floor() as usize;
+ let bottom = points.iter().map(|p| p.y).fold(0.0_f64, f64::max).ceil() as usize;
+ for y in top..bottom.min(height as usize) {
+ coverage.fill(0.0);
+ for sample in 0..4 {
+ let scan_y = y as f64 + (sample as f64 + 0.5) / 4.0;
+ intersections.clear();
+ for (a, b) in points.iter().zip(points.iter().cycle().skip(1)) {
+ if (a.y > scan_y) != (b.y > scan_y) {
+ intersections.push(a.x + (scan_y - a.y) * (b.x - a.x) / (b.y - a.y));
+ }
+ }
+ intersections.sort_by(f64::total_cmp);
+ for pair in intersections.chunks_exact(2) {
+ let left = pair[0].clamp(0.0, width as f64);
+ let right = pair[1].clamp(0.0, width as f64);
+ for x in left.floor() as usize..(right.ceil() as usize).min(width as usize) {
+ coverage[x] += (right.min(x as f64 + 1.0) - left.max(x as f64)).max(0.0) / 4.0;
+ }
+ }
+ }
+ for (x, value) in coverage.iter().enumerate() { mask[y * width as usize + x] = (value.clamp(0.0, 1.0) * 255.0).round() as u8; }
+ }
+ if !mask.iter().any(|value| *value != 0) { return Err("The lasso is too small at this generation resolution.".into()); }
+ Ok(mask)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ fn points(values: &[(f64, f64)]) -> Vec<MaskPoint> { values.iter().map(|&(x, y)| MaskPoint { x, y }).collect() }
+ #[test]
+ fn concave_masks_preserve_pixels_outside_the_outline() {
+ let polygon = points(&[(1.0, 1.0), (7.0, 1.0), (7.0, 3.0), (3.0, 3.0), (3.0, 7.0), (1.0, 7.0)]);
+ let mask = rasterize_polygon(&polygon, 8, 8).unwrap();
+ assert_eq!(mask[2 * 8 + 6], 255);
+ assert_eq!(mask[6 * 8 + 2], 255);
+ assert_eq!(mask[6 * 8 + 6], 0);
+ assert_eq!(mask[0], 0);
+ let mut reversed = polygon; reversed.reverse();
+ assert_eq!(mask, rasterize_polygon(&reversed, 8, 8).unwrap());
+ }
+ #[test]
+ fn antialiases_edges_and_accepts_full_canvas_and_crossed_outlines() {
+ let mask = rasterize_polygon(&points(&[(0.0, 0.0), (8.0, 0.0), (0.0, 8.0)]), 8, 8).unwrap();
+ assert!(mask.iter().any(|v| *v > 0 && *v < 255));
+ assert_eq!(mask[63], 0);
+ assert!(rasterize_polygon(&points(&[(0.0, 0.0), (8.0, 0.0), (8.0, 8.0), (0.0, 8.0)]), 8, 8).unwrap().iter().all(|v| *v == 255));
+ assert!(rasterize_polygon(&points(&[(0.0, 0.0), (8.0, 8.0), (0.0, 8.0), (8.0, 0.0)]), 8, 8).is_ok());
+ }
+ #[test]
+ fn rejects_invalid_and_empty_polygons() {
+ for polygon in [points(&[]), points(&[(1.0, 1.0), (2.0, 2.0), (3.0, 3.0)]), points(&[(-1.0, 0.0), (4.0, 0.0), (0.0, 4.0)]), points(&[(f64::NAN, 0.0), (4.0, 0.0), (0.0, 4.0)]), points(&[(0.0, 0.0), (9.0, 0.0), (0.0, 4.0)])] {
+ assert!(rasterize_polygon(&polygon, 8, 8).is_err());
+ }
+ }
+}
diff --git a/src-tauri/core/src/lib.rs b/src-tauri/core/src/lib.rs
index c56deb9..5b670e0 100644
--- a/src-tauri/core/src/lib.rs
+++ b/src-tauri/core/src/lib.rs
@@ -4,6 +4,7 @@
pub mod conditioning;
pub mod diagnostics;
pub mod models;
+pub mod image_mask;
pub mod generation_effects;
pub mod names;
pub mod reference_icons;
diff --git a/src-tauri/core/src/slopfab/ffi/mod.rs b/src-tauri/core/src/slopfab/ffi/mod.rs
index 12b7ba6..464d7e4 100644
--- a/src-tauri/core/src/slopfab/ffi/mod.rs
+++ b/src-tauri/core/src/slopfab/ffi/mod.rs
@@ -122,6 +122,14 @@ impl Api {
self.check(r)?;
self.inner.set_still_image(r.pointer.as_ptr())
}
+ #[cfg(test)]
+ pub fn disable_image_mask_for_test(&mut self) {
+ Arc::get_mut(&mut self.inner).expect("configure API before creating handles").disable_image_mask_for_test();
+ }
+ pub fn set_image_edit_mask(&self, r: &RequestHandle, mask: &[u8], width: i32, height: i32) -> Result<(), String> {
+ self.check(r)?;
+ self.inner.set_image_edit_mask(r.pointer.as_ptr(), mask, width, height)
+ }
pub fn set_image_edit_path(&self, r: &RequestHandle, path: &Path, bounds: [i32; 4], strength: f32, feather: i32) -> Result<(), String> {
self.check(r)?;
self.inner.set_image_edit_path(r.pointer.as_ptr(), path, bounds, strength, feather)
diff --git a/src-tauri/core/src/slopfab/ffi/raw/mod.rs b/src-tauri/core/src/slopfab/ffi/raw/mod.rs
index 5266b41..2cabace 100644
--- a/src-tauri/core/src/slopfab/ffi/raw/mod.rs
+++ b/src-tauri/core/src/slopfab/ffi/raw/mod.rs
@@ -96,6 +96,7 @@ pub struct Api {
set_resolution: unsafe extern "C" fn(*mut Request, i32, i32) -> i32,
set_frames: unsafe extern "C" fn(*mut Request, i32) -> i32,
set_still_image: Option<unsafe extern "C" fn(*mut Request, i32) -> i32>,
+ set_image_edit_mask: Option<unsafe extern "C" fn(*mut Request, *const u8, usize, i32, i32, usize) -> i32>,
set_image_edit_path: Option<SetImageEditPath>,
set_image_edit_rgb: Option<SetImageEditRgb>,
set_image_edit_invert_mask: Option<unsafe extern "C" fn(*mut Request, i32) -> i32>,
@@ -222,6 +223,7 @@ impl Api {
)
.ok()
.map(|symbol| *symbol),
+ set_image_edit_mask: library.get::<unsafe extern "C" fn(*mut Request, *const u8, usize, i32, i32, usize) -> i32>(b"slopfab_request_set_image_edit_mask_u8\0").ok().map(|symbol| *symbol),
set_image_edit_path: library.get::<SetImageEditPath>(b"slopfab_request_set_image_edit_path\0").ok().map(|symbol| *symbol),
set_image_edit_rgb: library.get::<SetImageEditRgb>(b"slopfab_request_set_image_edit_rgb24\0").ok().map(|symbol| *symbol),
set_image_edit_invert_mask: library.get::<unsafe extern "C" fn(*mut Request, i32) -> i32>(b"slopfab_request_set_image_edit_invert_mask\0").ok().map(|symbol| *symbol),
@@ -468,6 +470,15 @@ impl Api {
let set = self.set_still_image.ok_or("This slopfab.dll does not support still-image generation. Update the runtime to generate images and reference icons.")?;
self.error(unsafe { set(r, 1) })
}
+ #[cfg(test)]
+ pub fn disable_image_mask_for_test(&mut self) { self.set_image_edit_mask = None; }
+ pub fn set_image_edit_mask(&self, r: *mut Request, mask: &[u8], width: i32, height: i32) -> Result<(), String> {
+ let set = self.set_image_edit_mask.ok_or("Lasso editing requires SlopFab API 1.32 or later. Update the generation runtime.")?;
+ if !(1..=8192).contains(&width) || !(1..=8192).contains(&height) || mask.len() != width as usize * height as usize {
+ return Err("The image edit mask must match the source dimensions.".into());
+ }
+ self.error(unsafe { set(r, mask.as_ptr(), mask.len(), width, height, width as usize) })
+ }
pub fn set_image_edit_path(&self, r: *mut Request, path: &Path, bounds: [i32; 4], strength: f32, feather: i32) -> Result<(), String> {
let set = self.set_image_edit_path.ok_or("This slopfab.dll does not support bounding-box image editing. Update the runtime.")?;
let path = path_cstring(path)?;
diff --git a/src-tauri/core/src/slopfab/ffi/tests.rs b/src-tauri/core/src/slopfab/ffi/tests.rs
index ac9f461..43a9ef3 100644
--- a/src-tauri/core/src/slopfab/ffi/tests.rs
+++ b/src-tauri/core/src/slopfab/ffi/tests.rs
@@ -134,3 +134,22 @@ fn older_runtimes_reject_cfg_and_cross_instance_guidance_is_refused() {
let other = Api::load(&path).unwrap();
assert!(other.set_image_guidance(&request, 3.0).unwrap_err().contains("different runtime"));
}
+
+#[test]
+fn pixel_masks_validate_buffers_and_reject_older_runtimes() {
+ let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../lib/slopfab").join(crate::slopfab::DLL_FILE_NAME);
+ let api = Api::load(&path).unwrap();
+ let request = RequestHandle::new(&api).unwrap();
+ api.set_image_edit_rgb(&request, &vec![100; 64 * 32 * 3], 64, 32, [0, 0, 64, 32], 1.0, 0).unwrap();
+ assert!(api.set_image_edit_mask(&request, &[255; 8], 64, 32).is_err());
+ assert!(api.set_image_edit_mask(&request, &vec![255; 32 * 32], 32, 32).is_err());
+ let mut mask = vec![0; 64 * 32]; mask[100] = 255; mask[101] = 128;
+ api.set_image_edit_mask(&request, &mask, 64, 32).unwrap();
+ mask.fill(0); // The native request owns a snapshot.
+ api.resolve(&request).unwrap();
+ let other = Api::load(&path).unwrap();
+ assert!(other.set_image_edit_mask(&request, &mask, 64, 32).is_err());
+ let mut older = Api::load(&path).unwrap(); older.disable_image_mask_for_test();
+ let old_request = RequestHandle::new(&older).unwrap();
+ assert!(older.set_image_edit_mask(&old_request, &mask, 64, 32).unwrap_err().contains("API 1.32"));
+}
diff --git a/src-tauri/core/src/slopfab/h3.rs b/src-tauri/core/src/slopfab/h3.rs
index 7d78bb8..1c54d1d 100644
--- a/src-tauri/core/src/slopfab/h3.rs
+++ b/src-tauri/core/src/slopfab/h3.rs
@@ -95,6 +95,10 @@ pub(super) fn configure_request(
} else {
api.set_image_edit_path(handle, request.image_edit_path.as_deref().ok_or("Image edit source has not been resolved.")?, bounds, strength, feather)?;
}
+ if let Some(points) = &step.mask_polygon {
+ let mask = crate::image_mask::rasterize_polygon(points, request.canvas_width, request.canvas_height)?;
+ api.set_image_edit_mask(handle, &mask, request.canvas_width, request.canvas_height)?;
+ }
if step.invert_mask {
api.set_image_edit_invert_mask(handle)?;
if let Some(overlap) = step.outpaint_blend_overlap {
diff --git a/src-tauri/core/src/slopfab/planning.rs b/src-tauri/core/src/slopfab/planning.rs
index 54ddacd..7bebb64 100644
--- a/src-tauri/core/src/slopfab/planning.rs
+++ b/src-tauri/core/src/slopfab/planning.rs
@@ -86,6 +86,10 @@ pub(super) fn validate_generation_controls(request: &GenerationRequest) -> Resul
return Err("Image edits require one source image and nonempty boxes inside its dimensions.".into());
}
for step in &edit.edits {
+ if let Some(points) = &step.mask_polygon {
+ if step.invert_mask { return Err("Lasso masks cannot be combined with rectangular outpainting.".into()); }
+ crate::image_mask::validate_polygon(points, request.canvas_width, request.canvas_height)?;
+ }
if (step.outpaint_blend_overlap.is_some() || step.outpaint_langevin_steps.is_some()) && !step.invert_mask
|| step.outpaint_blend_overlap.is_some_and(|value| !(1..=51).contains(&value) || value % 2 == 0)
|| step.outpaint_langevin_steps.is_some_and(|value| !(0..=100).contains(&value)) {
diff --git a/src-tauri/core/src/slopfab/tests.rs b/src-tauri/core/src/slopfab/tests.rs
index 520f866..2816d8b 100644
--- a/src-tauri/core/src/slopfab/tests.rs
+++ b/src-tauri/core/src/slopfab/tests.rs
@@ -138,7 +138,7 @@ fn image_edit_recipe_disables_motion_cache_and_keeps_source_geometry() {
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![
- types::ImageEditStep { 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 }
+ types::ImageEditStep { 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()
};
validate_generation_controls(&request).unwrap();
@@ -150,7 +150,7 @@ fn image_edit_recipe_disables_motion_cache_and_keeps_source_geometry() {
let plan = api.resolve(&handle).unwrap();
assert_eq!((plan.canvas_width, plan.canvas_height, plan.aligned_frames), (96, 64, 1));
let description = api.describe(&handle).unwrap();
- assert!(description.contains("feather 16\n"), "{description}");
+ assert!(description.contains("feather 16;"), "{description}");
}
}
let mut invalid = request;
@@ -200,7 +200,7 @@ fn whole_image_edits_never_feather_at_the_native_boundary() {
configure_request(&api, &handle, &request, &configuration, platform, purpose, &ReferenceVideos::default()).unwrap();
api.resolve(&handle).unwrap();
let description = api.describe(&handle).unwrap();
- assert!(description.contains("feather 0\n"), "{description}");
+ assert!(description.contains("feather 0;"), "{description}");
}
}
}
@@ -1553,3 +1553,34 @@ fn moss_cfg_reaches_planning_and_generation_and_h3_rejects_it() {
config.model_type = "minimax-h3".into(); request.guidance_scale = Some(3.0);
assert!(config.validate_inputs(&request).unwrap_err().contains("CFG"));
}
+
+#[test]
+fn lasso_masks_survive_worker_serialization_and_reach_native_requests() {
+ let mut request: GenerationRequest = serde_json::from_value(serde_json::json!({
+ "jobId":"lasso", "prompt":"A flower", "stillImage":true, "frames":1, "steps":4, "seed":1,
+ "canvasWidth":64, "canvasHeight":32,
+ "imageEdit":{"sourceRelativePath":"source.png", "edits":[{
+ "prompt":"A flower", "x":0, "y":0, "width":32, "height":32, "feather":0,
+ "maskPolygon":[{"x":0,"y":0},{"x":32,"y":0},{"x":0,"y":32}]
+ }]}
+ })).unwrap();
+ let serialized = serde_json::to_value(&request).unwrap();
+ request = serde_json::from_value(serialized.clone()).unwrap();
+ assert_eq!(serde_json::to_value(&request).unwrap(), serialized);
+ validate_generation_controls(&request).unwrap();
+ request.image_edit_pixels = Some(std::sync::Arc::new(vec![100; 64 * 32 * 3]));
+ let configuration = Configuration::from_settings(&BTreeMap::new());
+ let api = ffi::Api::load(&configuration.dll_path).unwrap();
+ for purpose in [RequestPurpose::Plan, RequestPurpose::Generate] {
+ let handle = RequestHandle::new(&api).unwrap();
+ configure_request(&api, &handle, &request, &configuration, ComputePlatform::Vulkan, purpose, &ReferenceVideos::default()).unwrap();
+ api.resolve(&handle).unwrap();
+ let description = api.describe(&handle).unwrap();
+ assert!(description.contains("mask"), "{description}");
+ }
+ request.image_edit.as_mut().unwrap().edits[0].invert_mask = true;
+ assert!(validate_generation_controls(&request).is_err());
+ request.image_edit.as_mut().unwrap().edits[0].invert_mask = false;
+ request.image_edit.as_mut().unwrap().edits[0].mask_polygon.as_mut().unwrap()[0].x = -1.0;
+ assert!(validate_generation_controls(&request).is_err());
+}
diff --git a/src-tauri/core/src/slopfab/types.rs b/src-tauri/core/src/slopfab/types.rs
index cd628de..5371af4 100644
--- a/src-tauri/core/src/slopfab/types.rs
+++ b/src-tauri/core/src/slopfab/types.rs
@@ -125,6 +125,8 @@ pub struct ImageEditRequest {
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ImageEditStep {
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub mask_polygon: Option<Vec<crate::image_mask::MaskPoint>>,
pub prompt: String,
pub x: i32,
pub y: i32,
diff --git a/src-tauri/core/src/worker/protocol.rs b/src-tauri/core/src/worker/protocol.rs
index 90dec28..a733539 100644
--- a/src-tauri/core/src/worker/protocol.rs
+++ b/src-tauri/core/src/worker/protocol.rs
@@ -9,6 +9,7 @@ use std::collections::BTreeMap;
pub const SERVICE_TYPE: &str = "_slopus-worker._tcp.local.";
/// Bumped on any incompatible change to the routes or bodies below.
+// v12 carries lasso image masks; older workers would silently edit the box.
// v11 carries image CFG; older workers would silently discard the scale.
// v10 carries generation effects; older workers would silently discard them.
// v8 carries latent upscaling and its model weights.
@@ -17,7 +18,7 @@ pub const SERVICE_TYPE: &str = "_slopus-worker._tcp.local.";
// v6 added custom upscale weights.
// v5 added overlap locking.
// v4 added SeedVR2 export streams. v3 added inverted image masks.
-pub const PROTOCOL_VERSION: u32 = 11;
+pub const PROTOCOL_VERSION: u32 = 12;
/// Streaming SeedVR2 restoration; model paths are resolved on the worker.
#[derive(Debug, Clone, Serialize, Deserialize)]
diff --git a/src-tauri/src/project/commands/images.rs b/src-tauri/src/project/commands/images.rs
index b01f69f..0caf855 100644
--- a/src-tauri/src/project/commands/images.rs
+++ b/src-tauri/src/project/commands/images.rs
@@ -29,6 +29,7 @@ pub(super) fn new_scene(prompt: &str) -> ImageScene {
source_image: None,
output_size: None,
nodes: vec![ImageNode {
+ lasso: None,
id: "image-root".into(),
parent_id: None,
kind: "root".into(),
@@ -242,6 +243,7 @@ pub(super) fn apply(
return Err("New image nodes must be objects, text, groups or backgrounds.".into());
}
let node = ImageNode {
+ lasso: None,
id: id.clone(),
parent_id: Some(parent.clone()),
kind: kind.clone(),
diff --git a/src-tauri/src/project/image.rs b/src-tauri/src/project/image.rs
index 1fee944..9827c12 100644
--- a/src-tauri/src/project/image.rs
+++ b/src-tauri/src/project/image.rs
@@ -109,6 +109,8 @@ pub struct ImageNode {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reference_ids: Option<Vec<String>>,
pub r#box: Option<ImageBox>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub lasso: Option<Vec<slopus_core::image_mask::MaskPoint>>,
pub colors: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -207,6 +209,10 @@ impl ImageScene {
{
return Err("Invalid image node.".into());
}
+ if let Some(points) = &node.lasso {
+ if node.kind != "object" || node.r#box.is_none() { return Err("Lasso regions require an edit node and placement box.".into()); }
+ slopus_core::image_mask::validate_polygon(points, 1000, 1000)?;
+ }
if let Some(b) = &node.r#box {
if ![b.x, b.y, b.width, b.height].iter().all(|v| v.is_finite())
|| b.x < 0.0
diff --git a/src-tauri/src/tests/images.rs b/src-tauri/src/tests/images.rs
index efd8751..85df2e3 100644
--- a/src-tauri/src/tests/images.rs
+++ b/src-tauri/src/tests/images.rs
@@ -31,6 +31,10 @@ fn image_editing_draft_survives_save_and_reopen() {
snapshot["scene"]["rootType"] = serde_json::json!("image");
snapshot["scene"]["sourceImage"] = serde_json::json!({"name":"Source", "relativePath":"media/imported/source.png", "width":65, "height":41});
snapshot["scene"]["outputSize"] = serde_json::json!({"width":128,"height":64});
+ snapshot["scene"]["nodes"].as_array_mut().unwrap().push(serde_json::json!({
+ "id":"lasso", "parentId":"image-root", "kind":"object", "name":"Lasso", "description":"A flower", "text":"", "colors":[],
+ "box":{"x":100,"y":200,"width":300,"height":400}, "lasso":[{"x":0,"y":0},{"x":1000,"y":0},{"x":0,"y":1000}]
+ }));
config.assets.push(serde_json::from_value(serde_json::json!({
"id":"draft", "kind":"image", "name":"Editing Source", "relativePath":"media/imported/source.png", "mimeType":"image/png",
"width":65, "height":41, "createdAt":config.created_at, "imageDraft":true, "imageGeneration":snapshot
diff --git a/src/components/ui/icons.tsx b/src/components/ui/icons.tsx
index 40b7aa1..5d0326f 100644
--- a/src/components/ui/icons.tsx
+++ b/src/components/ui/icons.tsx
@@ -111,6 +111,7 @@ import { Filmstrip16Regular, Filmstrip20Regular } from "@fluentui/react-icons/he
import { FolderOpen16Regular, FolderOpen28Regular } from "@fluentui/react-icons/headless/svg/folder-open";
import { Group20Filled, Group20Regular } from "@fluentui/react-icons/headless/svg/group";
import { Image16Regular, Image24Regular } from "@fluentui/react-icons/headless/svg/image";
+import { Lasso20Regular } from "@fluentui/react-icons/headless/svg/lasso";
import { ImageAdd20Regular } from "@fluentui/react-icons/headless/svg/image-add";
import { LockClosed16Filled } from "@fluentui/react-icons/headless/svg/lock-closed";
import { LockOpen16Regular } from "@fluentui/react-icons/headless/svg/lock-open";
@@ -162,6 +163,7 @@ export const Image12 = /*#__PURE__*/ scaled(Image16Regular, 12);
export const Image22 = /*#__PURE__*/ scaled(Image24Regular, 22);
export const ImageAdd14 = /*#__PURE__*/ scaled(ImageAdd20Regular, 14);
export const ImageAdd16 = /*#__PURE__*/ scaled(ImageAdd20Regular, 16);
+export const Lasso16 = /*#__PURE__*/ scaled(Lasso20Regular, 16);
export const Lock14Filled = /*#__PURE__*/ scaled(LockClosed16Filled, 14);
export const More14 = /*#__PURE__*/ scaled(MoreHorizontal16Regular, 14);
export const Mute14Filled = /*#__PURE__*/ scaled(SpeakerMute16Filled, 14);
diff --git a/src/components/workspace/ImageEditor.test.tsx b/src/components/workspace/ImageEditor.test.tsx
index 8a9866f..0b69b46 100644
--- a/src/components/workspace/ImageEditor.test.tsx
+++ b/src/components/workspace/ImageEditor.test.tsx
@@ -497,7 +497,7 @@ it("starts an image root from the thumbnail Edit menu with edit tools and no typ
expect(screen.queryByLabelText("Prompt (high-level description)")).not.toBeInTheDocument();
expect(screen.queryByRole("combobox", { name: "Type" })).not.toBeInTheDocument();
expect(promptValue(screen.getByLabelText("Edit prompt"))).toBe("");
- expect(within(screen.getByRole("radiogroup", { name: "Canvas tool" })).getAllByRole("radio").map((tool) => tool.getAttribute("aria-label"))).toEqual(["Select", "Draw edit"]);
+ expect(within(screen.getByRole("radiogroup", { name: "Canvas tool" })).getAllByRole("radio").map((tool) => tool.getAttribute("aria-label"))).toEqual(["Select", "Draw edit", "Lasso"]);
const draft = screen.getByRole("button", { name: "View Editing Saved" });
expect(draft).toHaveAttribute("aria-pressed", "true");
fireEvent.contextMenu(screen.getByRole("button", { name: "Image" }));
@@ -1154,3 +1154,41 @@ it("sets a temporary generation count, dismisses its panel and resets after one
fireEvent.click(screen.getByRole("button", { name: "Generation options" }));
expect(screen.getByRole("spinbutton", { name: "Count" })).toHaveValue(1);
});
+
+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 }];
+ const current = setup(initial);
+ fireEvent.contextMenu(screen.getByRole("button", { name: "View Source" }));
+ fireEvent.click(screen.getByRole("menuitem", { name: "Edit" }));
+ 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: 800 }) });
+ 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: "Lasso" }));
+ pointer("pointerDown", 100, 80); pointer("pointerMove", 400, 80); pointer("pointerUp", 100, 320);
+ expect(screen.getByRole("radio", { name: "Select" })).toHaveAttribute("aria-checked", "true");
+ expect(screen.getByRole("button", { name: "Lasso" })).toBeInTheDocument();
+ const lasso = current().imageScene!.nodes[1];
+ expect(lasso).toMatchObject({ name: "Lasso", box: { x: 100, y: 100, width: 300, height: 300 }, lasso: [{ x: 0, y: 0 }, { x: 1000, y: 0 }, { x: 0, y: 1000 }] });
+ typePrompt(screen.getByLabelText("Edit prompt"), "Add a red flower");
+ fireEvent.click(screen.getByRole("button", { name: "Image" }));
+ const outline = canvas.querySelector("polygon[data-node]")!;
+ pointer("pointerDown", 150, 120, outline); pointer("pointerMove", 250, 200); pointer("pointerUp", 250, 200);
+ expect(current().imageScene!.nodes[1]).toMatchObject({ box: { x: 200, y: 200, width: 300, height: 300 }, lasso: lasso.lasso, description: "Add a red flower" });
+ expect(promptValue(screen.getByLabelText("Edit prompt"))).toBe("Add a red flower");
+ expect(compileImageEdits(current()).edits[0]).toMatchObject({ feather: 0, maskPolygon: [{ x: 198.4, y: 160 }, { x: 496, y: 160 }, { x: 198.4, y: 400 }] });
+ expect(parseProjectConfig(JSON.parse(JSON.stringify(current()))).imageScene).toEqual(current().imageScene);
+ expect(current().assets.find((asset) => asset.id === "source")?.imageEditing).toBeUndefined();
+ fireEvent.click(screen.getByRole("radio", { name: "Lasso" }));
+ pointer("pointerDown", 10, 10); pointer("pointerMove", 200, 100);
+ Object.defineProperty(canvas.closest(".image-viewport")!, "hasPointerCapture", { value: () => false });
+ fireEvent.pointerCancel(canvas);
+ expect(current().imageScene!.nodes).toHaveLength(2);
+ pointer("pointerDown", 10, 10); pointer("pointerUp", 200, 200);
+ expect(current().imageScene!.nodes).toHaveLength(2);
+});
diff --git a/src/components/workspace/ImageEditor.tsx b/src/components/workspace/ImageEditor.tsx
index 2feec08..51d1d0d 100644
--- a/src/components/workspace/ImageEditor.tsx
+++ b/src/components/workspace/ImageEditor.tsx
@@ -1,9 +1,9 @@
import { activeGenerationEffects } from "../../lib/generationEffects";
import { GenerationEffects } from "./GenerationEffects";
import { useEffect, useId, useMemo, useRef, useState, useSyncExternalStore, type CSSProperties, type DragEvent, type KeyboardEvent as ReactKeyboardEvent, type PointerEvent } from "react";
-import { Add14, Add16, Boxes16, Boxes16Filled, ChevronDown14, ChevronRight14, Copy16, Cube16, Cube16Filled, Cursor16, Cursor16Filled, Delete14, Delete16, Edit16, Film16, FolderAdd16, FolderOpen16, Group16, Group16Filled, Image16, Image32, Paste16, References16, Rename16, Sparkle16, Stop14, Text16, Text16Filled } from "../ui/icons";
+import { 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, removeImageNode, resizeImageNode, type ImageBox, type ImageNode, type ImageScene, type ImageSource } from "../../lib/imageScene";
+import { addImageNode, createImageEditScene, createImageScene, duplicateImageNode, imageDescendants, imageScenePrompt, imageLassoPoints, 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 { imageSettings } from "../../lib/imageSettings";
import { outputDimensions } from "../../lib/export";
@@ -122,20 +122,21 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene
const [debugPrompt, setDebugPrompt] = useState<string | null>(null);
useEffect(() => { if (!debugEnabled) setDebugPrompt(null); }, [debugEnabled]);
const [boxes, setBoxes] = useState(true);
- const [drawKind, setDrawKind] = useState<"object" | "text" | "group" | null>(null);
+ const [drawKind, setDrawKind] = useState<"object" | "text" | "group" | "lasso" | null>(null);
const [view, setView] = useState({ zoom: 1, x: 0, y: 0 });
const viewport = useRef<HTMLDivElement>(null);
const pan = useRef<{ pointerId: number; x: number; y: number } | null>(null);
const [panning, setPanning] = useState(false);
const [draftBox, setDraftBox] = useState<ImageBox | null>(null);
+ const [draftLasso, setDraftLasso] = useState<ImagePoint[] | null>(null);
const undo = useRef<ImageScene[]>([]);
const redo = useRef<ImageScene[]>([]);
const [, historyChanged] = useState(0);
- const pointer = useRef<{ x: number; y: number; scene: ImageScene; id?: string; resize: boolean } | null>(null);
+ const pointer = useRef<{ x: number; y: number; scene: ImageScene; id?: string; resize: boolean; box?: ImageBox; lasso?: ImagePoint[] } | null>(null);
useEffect(() => {
// Applied edits belong to the old source and must not be resurrected by Undo.
undo.current = []; redo.current = []; historyChanged((value) => value + 1);
- pointer.current = null; setDraftBox(null); setDrawKind(null); setRenaming(null);
+ pointer.current = null; setDraftBox(null); setDraftLasso(null); setDrawKind(null); setRenaming(null);
setSelection(root.id); setCollapsed(new Set());
}, [scene.sourceImage?.relativePath, root.id]);
useEffect(() => {
@@ -207,11 +208,11 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene
};
const patchNode = (patch: Partial<ImageNode>) => commit({ ...scene, nodes: scene.nodes.map((node) => node.id === selected.id ? { ...node, ...patch } : node) });
const insertParent = selected.kind === "object" || selected.kind === "text" ? selected.parentId ?? root.id : selected.id;
- const add = (kind: Exclude<ImageNode["kind"], "root">, box?: ImageBox) => {
+ const add = (kind: Exclude<ImageNode["kind"], "root">, box?: ImageBox, lasso?: ImagePoint[]) => {
if (imageRoot && editingBlocked) return;
const parent = imageRoot ? root.id : insertParent;
const next = addImageNode(scene, parent, imageRoot ? "object" : kind, box);
- if (imageRoot) Object.assign(next.nodes.at(-1)!, { name: "Edit" });
+ if (imageRoot) Object.assign(next.nodes.at(-1)!, { name: lasso ? "Lasso" : "Edit", ...(lasso ? { lasso } : {}) });
commit(next); setSelection(next.nodes.at(-1)!.id);
setCollapsed((current) => { const next = new Set(current); next.delete(parent); return next; });
};
@@ -234,7 +235,7 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene
const asset = config.assets.find((asset) => asset.id === id);
const snapshot = asset?.imageGeneration;
undo.current = []; redo.current = []; historyChanged((value) => value + 1);
- pointer.current = null; setDraftBox(null); setDrawKind(null); setRenaming(null);
+ pointer.current = null; setDraftBox(null); setDraftLasso(null); setDrawKind(null); setRenaming(null);
setCollapsed(new Set()); setSelection(snapshot?.scene.nodes.find((node) => node.kind === "root")?.id ?? "image-root");
if (snapshot && imageTemplates.some((candidate) => candidate.id === snapshot.generatorTemplateId)) {
setTemplateId(snapshot.generatorTemplateId);
@@ -281,7 +282,7 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene
};
const replaceScene = (next: ImageScene) => {
undo.current = []; redo.current = []; historyChanged((value) => value + 1);
- pointer.current = null; setDraftBox(null); setDrawKind(null); setCollapsed(new Set());
+ pointer.current = null; setDraftBox(null); setDraftLasso(null); setDrawKind(null); setCollapsed(new Set());
setSelection(next.nodes.find((node) => node.kind === "root")!.id);
onChange((current) => ({ ...current, imageScene: next }));
};
@@ -323,7 +324,7 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene
};
const removeSelected = () => {
if (selected.kind === "root") return;
- pointer.current = null; setDraftBox(null);
+ pointer.current = null; setDraftBox(null); setDraftLasso(null);
const parent = selected.parentId ?? root.id;
commit(removeImageNode(scene, selected.id)); setSelection(parent);
requestAnimationFrame(() => focusNode(parent));
@@ -354,25 +355,47 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene
const id = drawKind ? undefined : target.dataset.node;
if (!id && !drawKind) { setSelection(root.id); return; }
if (id) setSelection(id);
- pointer.current = { ...point(event), scene, id, resize: target.dataset.resize === "true" };
+ const at = point(event);
+ if (!Number.isFinite(at.x) || !Number.isFinite(at.y)) return;
+ pointer.current = { ...at, scene, id, resize: target.dataset.resize === "true", ...(drawKind === "lasso" ? { lasso: [at] } : {}) };
+ if (drawKind === "lasso") setDraftLasso([at]);
event.currentTarget.setPointerCapture(event.pointerId);
};
const pointerMove = (event: PointerEvent<SVGSVGElement>) => {
const start = pointer.current; if (!start) return;
- const at = point(event); const original = start.scene.nodes.find((node) => node.id === start.id)?.box;
+ const at = point(event);
+ if (!Number.isFinite(at.x) || !Number.isFinite(at.y)) return;
+ const original = start.scene.nodes.find((node) => node.id === start.id)?.box;
+ if (start.lasso) {
+ 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);
+ setDraftLasso([...start.lasso]);
+ return;
+ }
if (original) {
const x = start.resize ? original.x : Math.max(0, Math.min(1000 - original.width, original.x + at.x - start.x));
const y = start.resize ? original.y : Math.max(0, Math.min(1000 - original.height, original.y + at.y - start.y));
- setDraftBox({ 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 setDraftBox({ 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 = { 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) };
+ setDraftBox(start.box);
};
- const pointerUp = () => {
+ const pointerUp = (event: PointerEvent<SVGSVGElement>) => {
+ pointerMove(event);
const start = pointer.current;
- if (start && draftBox && draftBox.width > 0 && draftBox.height > 0) {
- if (start.id) commit(resizeImageNode(start.scene, start.id, draftBox));
- else if (drawKind && draftBox.width >= 5 && draftBox.height >= 5) { add(drawKind, draftBox); setDrawKind(null); }
+ if (start?.lasso) {
+ const points = simplifyImageLasso(start.lasso);
+ const x = Math.min(...points.map((point) => point.x)), y = Math.min(...points.map((point) => point.y));
+ const width = Math.max(...points.map((point) => point.x)) - x, height = Math.max(...points.map((point) => point.y)) - y;
+ if (imageLassoSchema.safeParse(points).success && width >= 5 && height >= 5) {
+ add("object", { x, y, width, height }, points.map((point) => ({ x: Math.max(0, Math.min(1000, (point.x - x) / width * 1000)), y: Math.max(0, Math.min(1000, (point.y - y) / height * 1000)) })));
+ setDrawKind(null);
+ }
+ } else if (start?.box && start.box.width > 0 && start.box.height > 0) {
+ if (start.id) commit(resizeImageNode(start.scene, start.id, start.box));
+ else if (drawKind && drawKind !== "lasso" && start.box.width >= 5 && start.box.height >= 5) { add(drawKind, start.box); setDrawKind(null); }
}
- pointer.current = null; setDraftBox(null);
+ pointer.current = null; setDraftBox(null); setDraftLasso(null);
};
const dropDestination = (event: DragEvent<HTMLDivElement>, target: ImageNode) => {
const source = scene.nodes.find((node) => node.id === draggedNode);
@@ -444,15 +467,17 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene
/* Canvas tools: a radio group of icon toggles with one-letter keys. */
const TOOLS = [
{ id: null, label: "Select", key: "V", icon: <Cursor16 />, onIcon: <Cursor16Filled /> },
- ...(imageRoot ? [{ id: "object", label: "Draw edit", key: "E", icon: <Edit16 />, onIcon: <Edit16 /> }] as const : [
+ ...(imageRoot ? [{ id: "object", label: "Draw edit", key: "E", icon: <Edit16 />, onIcon: <Edit16 /> },
+ { id: "lasso", label: "Lasso", key: "L", icon: <Lasso16 />, onIcon: <Lasso16 /> }] as const : [
{ id: "object", label: "Draw object", key: "O", icon: <Cube16 />, onIcon: <Cube16Filled /> },
{ id: "text", label: "Draw text", key: "T", icon: <Text16 />, onIcon: <Text16Filled /> },
{ id: "group", label: "Draw group", key: "G", icon: <Group16 />, onIcon: <Group16Filled /> },
] as const),
] as const;
- const chooseTool = (id: typeof drawKind) => { if (extendMode || (imageRoot && editingBlocked && id)) return; setDrawKind(id); if (id) setBoxes(true); };
+ const chooseTool = (id: typeof drawKind) => { if (extendMode || (imageRoot && editingBlocked && id)) return; pointer.current = null; setDraftBox(null); setDraftLasso(null); setDrawKind(id); if (id) setBoxes(true); };
useShortcut("V", () => chooseTool(null), { scope: editorRoot });
useShortcut("E", () => chooseTool("object"), { scope: editorRoot, enabled: imageRoot && !editingBlocked });
+ useShortcut("L", () => chooseTool("lasso"), { scope: editorRoot, enabled: imageRoot && !editingBlocked });
useShortcut("O", () => chooseTool("object"), { scope: editorRoot, enabled: !imageRoot });
useShortcut("T", () => chooseTool("text"), { scope: editorRoot, enabled: !imageRoot });
useShortcut("G", () => chooseTool("group"), { scope: editorRoot, enabled: !imageRoot });
@@ -532,7 +557,7 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene
event.stopPropagation();
if (event.key === "Enter" && !event.nativeEvent.isComposing) { event.preventDefault(); finishRename(true); }
if (event.key === "Escape") { event.preventDefault(); finishRename(false); }
- }} /> : <button className="image-tree__select" tabIndex={selected.id === node.id ? 0 : -1} data-tooltip={node.name} onFocus={() => setSelection(node.id)} onClick={() => setSelection(node.id)}><span className="image-tree__icon" aria-hidden="true">{node.kind === "root" ? <Image16 /> : imageRoot ? <Edit16 /> : node.kind === "text" ? <Text16 /> : node.kind === "group" ? <Group16 /> : <Cube16 />}</span><span>{node.name}</span></button>}
+ }} /> : <button className="image-tree__select" tabIndex={selected.id === node.id ? 0 : -1} data-tooltip={node.name} onFocus={() => setSelection(node.id)} onClick={() => setSelection(node.id)}><span className="image-tree__icon" aria-hidden="true">{node.kind === "root" ? <Image16 /> : node.lasso ? <Lasso16 /> : imageRoot ? <Edit16 /> : node.kind === "text" ? <Text16 /> : node.kind === "group" ? <Group16 /> : <Cube16 />}</span><span>{node.name}</span></button>}
</div>
{open && children.length > 0 && <div role="group">{children.map((child) => tree(child, depth + 1))}</div>}
</div>;
@@ -660,7 +685,7 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene
{imageMenu && <HierarchyContextMenu {...imageMenu} label="Generated image actions" onClose={closeImageMenu} items={[
...(imageMenu.source === "bar" ? [
{ label: "New empty image", icon: <Add16 />, action: () => attempt(() => {
- undo.current = []; redo.current = []; pointer.current = null; setDraftBox(null); setDrawKind(null); setRenaming(null); setCollapsed(new Set()); setSelection("image-root");
+ undo.current = []; redo.current = []; pointer.current = null; setDraftBox(null); setDraftLasso(null); setDrawKind(null); setRenaming(null); setCollapsed(new Set()); setSelection("image-root");
onChange((current) => createEmptyImage(current, template?.id, imageMenu.inFamily ? imageMenu.id ?? current.imageScene?.outputAssetId : undefined));
}) },
{ label: "Add image file", icon: <FolderOpen16 />, disabled: !isTauri(), action: () => attempt(async () => {
@@ -710,11 +735,12 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene
onAuxClick={(event) => { if (event.button === 1) event.preventDefault(); }}>
<div className="image-frame" style={{ "--image-ratio": width / height, "--image-zoom": view.zoom, "--image-pan-x": `${view.x}px`, "--image-pan-y": `${view.y}px` } as CSSProperties}>
{active && output?.imageDraft ? <div className="image-empty" role="status"><Image32 aria-hidden="true" /><strong>{work.status === "queued" ? "Queued" : "Generating"}</strong><span>The generated image will appear here.</span></div> : showResult ? <ReferenceImage key={output!.id} folderPath={folderPath} relativePath={output!.relativePath} sourcePath={output!.sourcePath} alt={output!.name} /> : imageRoot && scene.sourceImage ? <ReferenceImage folderPath={folderPath} relativePath={scene.sourceImage.relativePath} alt={scene.sourceImage.name} /> : output && !imageRoot && (output.relativePath || output.sourcePath) ? <ReferenceImage key={output.id} folderPath={folderPath} relativePath={output.relativePath} sourcePath={output.sourcePath} alt={output.name} onMeasured={!output.imageGeneration ? (size) => measureImportedImage(output.id, size) : undefined} /> : <div className="image-empty"><Image32 aria-hidden="true" /><strong>{templateSnapshot ? output!.name : imageRoot ? "Add an image to edit" : "Compose your image"}</strong><span>{templateSnapshot ? active ? "The generated image will appear here." : "No generated image yet." : imageRoot ? "Choose Add image file in the image bar menu, then enter an edit prompt." : "Add objects, text and groups, then describe them in the inspector."}</span></div>}
- <svg className={`image-overlay ${drawKind ? "drawing" : ""}`} viewBox="0 0 1000 1000" preserveAspectRatio="none" aria-label="Image placement canvas" onPointerDown={pointerDown} onPointerMove={pointerMove} onPointerUp={pointerUp} onPointerCancel={() => { pointer.current = null; setDraftBox(null); }}>
+ <svg className={`image-overlay ${drawKind ? "drawing" : ""}`} viewBox="0 0 1000 1000" preserveAspectRatio="none" aria-label="Image placement canvas" onPointerDown={pointerDown} onPointerMove={pointerMove} onPointerUp={pointerUp} onPointerCancel={() => { pointer.current = null; setDraftBox(null); setDraftLasso(null); }}>
{boxes && scene.nodes.filter((node) => node.box).map((node) => {
const box = pointer.current?.id === node.id && draftBox ? draftBox : node.box!;
- return <g key={node.id} className={selected.id === node.id ? "selected" : ""}><rect data-node={node.id} x={box.x} y={box.y} width={box.width} height={box.height} vectorEffect="non-scaling-stroke" />{selected.id === node.id && <rect className="image-box-handle" data-node={node.id} data-resize="true" x={box.x + box.width - 16} y={box.y + box.height - 16} width="16" height="16" />}</g>;
+ return <g key={node.id} className={selected.id === node.id ? "selected" : ""}>{node.lasso ? <polygon data-node={node.id} points={imageLassoPoints(node, box).map(({ x, y }) => `${x},${y}`).join(" ")} fillRule="evenodd" vectorEffect="non-scaling-stroke" /> : <rect data-node={node.id} x={box.x} y={box.y} width={box.width} height={box.height} vectorEffect="non-scaling-stroke" />}{selected.id === node.id && <rect className="image-box-handle" data-node={node.id} data-resize="true" x={box.x + box.width - 16} y={box.y + box.height - 16} width="16" height="16" />}</g>;
})}
+ {draftLasso && <polygon className="image-box-draft" points={draftLasso.map(({ x, y }) => `${x},${y}`).join(" ")} fillRule="evenodd" vectorEffect="non-scaling-stroke" />}
{draftBox && !pointer.current?.id && <rect className="image-box-draft" {...{ x: draftBox.x, y: draftBox.y, width: draftBox.width, height: draftBox.height }} />}
</svg>
{boxes && scene.nodes.filter((node) => node.box).map((node) => { const box = pointer.current?.id === node.id && draftBox ? draftBox : node.box!; return <span key={node.id} className="image-box-label" style={{ left: `${box.x / 10}%`, top: `${box.y / 10}%` }}>{node.name}</span>; })}
diff --git a/src/lib/imageEditing.ts b/src/lib/imageEditing.ts
index deb54de..c313505 100644
--- a/src/lib/imageEditing.ts
+++ b/src/lib/imageEditing.ts
@@ -1,11 +1,13 @@
import { imageSettings } from "./imageSettings";
-import { createImageEditScene, createImageScene, imageSourceSchema, type ImageNode, type ImageScene, type ImageSource } from "./imageScene";
+import { createImageEditScene, createImageScene, imageLassoPoints, imageSourceSchema, type ImageNode, type ImageScene, type ImageSource, type ImagePoint } from "./imageScene";
import { compileImagePrompt } from "./imagePrompt";
import type { ProjectConfig, ProjectReference } from "./project";
export interface ImageEditStep {
prompt: string; x: number; y: number; width: number; height: number;
feather?: number;
+ /** Pixel-space polygon, rasterized to a source-sized mask by the runtime. */
+ maskPolygon?: ImagePoint[];
/** Denoising strength; lower values retain more of the source detail. */
strength?: number;
/** Preserve this box and generate its surround in a single outpainting pass. */
@@ -50,7 +52,7 @@ export function compileImageEdits(config: ProjectConfig): { source: ImageSource;
// A full-canvas edit has no surrounding source to blend into at its edges.
const wholeImage = x === 0 && y === 0 && right === size.width && bottom === size.height;
return { prompt: compiled.prompt, references: compiled.references, x, y, width: Math.max(1, right - x), height: Math.max(1, bottom - y),
- ...(wholeImage ? { feather: 0 } : {}) };
+ ...(node.lasso ? { maskPolygon: imageLassoPoints(node).map(({ x, y }) => ({ x: x * size.width / 1000, y: y * size.height / 1000 })), feather: 0 } : wholeImage ? { feather: 0 } : {}) };
});
if (!edits.length) throw new Error("Enter a whole-image edit prompt or add an Edit node before generating.");
return { source, edits };
diff --git a/src/lib/imageScene.test.ts b/src/lib/imageScene.test.ts
index 5cc4fca..2c15b5e 100644
--- a/src/lib/imageScene.test.ts
+++ b/src/lib/imageScene.test.ts
@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import imageFixture from "../../fixtures/project-v1-image.json";
import oldFixture from "../../fixtures/project-v1-created.json";
-import { addImageNode, createImageScene, duplicateImageNode, imageScenePrompt, imageSceneSchema, removeImageNode, resizeImageNode } from "./imageScene";
+import { addImageNode, createImageScene, duplicateImageNode, imageScenePrompt, imageSceneSchema, imageLassoPoints, removeImageNode, resizeImageNode } from "./imageScene";
import { createProjectConfig, parseProjectConfig } from "./project";
describe("image documents", () => {
@@ -52,3 +52,18 @@ describe("image documents", () => {
expect(imageScenePrompt(objectScene)).toBe("sunglasses.");
});
});
+
+it("keeps lasso outlines through moves and duplication and rejects invalid shapes", () => {
+ const scene = addImageNode(createImageScene(), "image-root", "object", { x: 100, y: 200, width: 300, height: 400 });
+ scene.nodes[1].lasso = [{ x: 0, y: 0 }, { x: 1000, y: 0 }, { x: 0, y: 1000 }];
+ const moved = resizeImageNode(scene, scene.nodes[1].id, { x: 500, y: 300, width: 200, height: 600 });
+ expect(imageLassoPoints(moved.nodes[1])).toEqual([{ x: 500, y: 300 }, { x: 700, y: 300 }, { x: 500, y: 900 }]);
+ const copy = duplicateImageNode(moved, moved.nodes[1].id);
+ expect(copy.nodes[2].lasso).toEqual(scene.nodes[1].lasso);
+ copy.nodes[2].lasso![0].x = 10;
+ expect(copy.nodes[1].lasso![0].x).toBe(0);
+ for (const lasso of [[], [{ x: 0, y: 0 }, { x: 500, y: 500 }, { x: 1000, y: 1000 }], [{ x: -1, y: 0 }, { x: 1000, y: 0 }, { x: 0, y: 1000 }]]) {
+ scene.nodes[1].lasso = lasso;
+ expect(imageSceneSchema.safeParse(scene).success).toBe(false);
+ }
+});
diff --git a/src/lib/imageScene.ts b/src/lib/imageScene.ts
index 6097b2d..54cdc7d 100644
--- a/src/lib/imageScene.ts
+++ b/src/lib/imageScene.ts
@@ -3,11 +3,18 @@ import { z } from "zod";
const boxSchema = z.object({ x: z.number().min(0).max(999), y: z.number().min(0).max(999), width: z.number().positive().max(1000), height: z.number().positive().max(1000) })
.refine((box) => box.x + box.width <= 1000 && box.y + box.height <= 1000, "Placement must stay inside the image.");
+export const imagePointSchema = z.object({ x: z.number().finite().min(0).max(1000), y: z.number().finite().min(0).max(1000) });
+export type ImagePoint = z.infer<typeof imagePointSchema>;
+export const imageLassoSchema = z.array(imagePointSchema).min(3).max(2048).refine((points) => {
+ const first = points[0], second = points.find((point) => point.x !== first.x || point.y !== first.y);
+ return !!second && points.some((point) => Math.abs((second.x - first.x) * (point.y - first.y) - (second.y - first.y) * (point.x - first.x)) > 1e-9);
+}, "Lasso must enclose an area.");
export const imageNodeSchema = z.object({
id: z.string().min(1), parentId: z.string().min(1).nullable(),
kind: z.enum(["root", "group", "object", "text", "background"]),
name: z.string().min(1).max(120), description: z.string(), text: z.string(),
referenceIds: z.array(z.string().min(1)).max(100).optional(),
+ lasso: imageLassoSchema.optional(),
box: boxSchema.nullable(), colors: z.array(z.string().regex(/^#[0-9a-fA-F]{6}$/)).max(16),
});
export const imageSourceSchema = z.object({
@@ -31,6 +38,7 @@ export const imageSceneSchema = z.object({
const ids = new Map(scene.nodes.map((node) => [node.id, node]));
const roots = scene.nodes.filter((node) => node.kind === "root");
const invalid = ids.size !== scene.nodes.length || roots.length !== 1 || roots[0]?.parentId !== null || scene.nodes.some((node) => {
+ if (node.lasso && (!node.box || node.kind !== "object")) return true;
if (node.kind === "root") return false;
const seen = new Set([node.id]);
let parent = node.parentId;
@@ -75,6 +83,26 @@ export function addImageNode(scene: ImageScene, parentId: string, kind: Exclude<
const node: ImageNode = { id: crypto.randomUUID(), parentId, kind, name: kind[0].toUpperCase() + kind.slice(1), description: "", text: "", colors: [], box: box ?? (kind === "background" ? null : { x: 250, y: 250, width: 500, height: 500 }) };
return imageSceneSchema.parse({ ...scene, nodes: [...scene.nodes, node] });
}
+/** Lasso points are local to the placement box on the same 0 to 1000 grid. */
+export function imageLassoPoints(node: Pick<ImageNode, "box" | "lasso">, box = node.box): ImagePoint[] {
+ return box && node.lasso ? node.lasso.map((point) => ({ x: box.x + point.x * box.width / 1000, y: box.y + point.y * box.height / 1000 })) : [];
+}
+
+/** Keep the sampled outline bounded without changing corners of short strokes. */
+export function simplifyImageLasso(points: ImagePoint[], tolerance = 1): ImagePoint[] {
+ if (points.length < 3) return points;
+ const first = points[0], last = points.at(-1)!;
+ const dx = last.x - first.x, dy = last.y - first.y, length = dx * dx + dy * dy;
+ let farthest = tolerance * tolerance, at = -1;
+ for (let i = 1; i < points.length - 1; i++) {
+ const point = points[i];
+ const t = length ? Math.max(0, Math.min(1, ((point.x - first.x) * dx + (point.y - first.y) * dy) / length)) : 0;
+ const distance = (point.x - first.x - t * dx) ** 2 + (point.y - first.y - t * dy) ** 2;
+ if (distance > farthest) { farthest = distance; at = i; }
+ }
+ return at < 0 ? [first, last] : [...simplifyImageLasso(points.slice(0, at + 1), tolerance).slice(0, -1), ...simplifyImageLasso(points.slice(at), tolerance)];
+}
+
export function removeImageNode(scene: ImageScene, id: string): ImageScene {
if (scene.nodes.find((node) => node.id === id)?.kind === "root") return scene;
const ids = imageDescendants(scene, id);
diff --git a/src/lib/workQueue.test.ts b/src/lib/workQueue.test.ts
index 1a24774..a77c3f6 100644
--- a/src/lib/workQueue.test.ts
+++ b/src/lib/workQueue.test.ts
@@ -591,7 +591,7 @@ describe("image generation work", () => {
let scene = createImageEditScene({ name: "Source", relativePath: "media/generated/source.jpg", width: 101, height: 77 });
scene.nodes[0].description = "Make the light warmer like @[ref:Lighting]";
scene = addImageNode(scene, "image-root", "object");
- Object.assign(scene.nodes[1], { name: "Edit", description: "Add a red @[ref:Balloon]" });
+ Object.assign(scene.nodes[1], { name: "Lasso", description: "Add a red @[ref:Balloon]", lasso: [{ x: 0, y: 0 }, { x: 1000, y: 0 }, { x: 0, y: 1000 }] });
record.config.imageScene = scene;
record.config.references = ["Lighting", "Balloon"].map((name) => ({ id: name, name, kind: "image", description: name,
relativePath: `references/${name}.png`, intendedUse: [], createdAt: record.config.createdAt,
@@ -604,7 +604,7 @@ describe("image generation work", () => {
const steps = request.imageEdit!.edits;
expect(steps).toHaveLength(2);
expect(steps[0]).toMatchObject({ x: 0, y: 0, width: 96, height: 64, feather: 0 });
- expect(steps[1].feather).toBeUndefined();
+ expect(steps[1]).toMatchObject({ feather: 0, maskPolygon: [{ x: 24, y: 16 }, { x: 72, y: 16 }, { x: 24, y: 48 }] });
for (const [index, name] of ["Lighting", "Balloon"].entries()) {
expect(steps[index].referencePaths).toEqual(["C:/Image/media/generated/source.jpg", `C:/Image/references/${name}.png`]);
expect(steps[index].refmods).toEqual([{ path: `C:/Image/references/${name}.safetensors`, strength: 0.75, copies: 2 }]);
diff --git a/src/styles/image-editor.css b/src/styles/image-editor.css
index 032747e..f10f009 100644
--- a/src/styles/image-editor.css
+++ b/src/styles/image-editor.css
@@ -72,11 +72,11 @@
.image-empty strong { color: var(--text-primary); font-size: var(--text-base); font-weight: var(--weight-semibold); }
.image-empty span { max-width: 300px; font-size: var(--text-xs); }
.image-overlay { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
-.image-overlay rect { fill: color-mix(in srgb, var(--accent) 8%, transparent); stroke: var(--accent); stroke-width: 1.5; cursor: move; }
-.image-overlay .selected > rect { stroke-width: 3; }
+.image-overlay rect, .image-overlay polygon { fill: color-mix(in srgb, var(--accent) 8%, transparent); stroke: var(--accent); stroke-width: 1.5; cursor: move; }
+.image-overlay .selected > rect, .image-overlay .selected > polygon { stroke-width: 3; }
.image-box-label { position: absolute; margin: var(--space-1); padding: 0 var(--space-1); border-radius: var(--radius-sm); color: var(--text-primary); background: var(--panel); font-size: var(--text-xs); pointer-events: none; }
.image-overlay .image-box-handle { fill: var(--accent); cursor: nwse-resize; }
-.image-overlay.drawing, .image-overlay.drawing rect { cursor: crosshair; }
+.image-overlay.drawing, .image-overlay.drawing rect, .image-overlay.drawing polygon { cursor: crosshair; }
.extend-frame { background: transparent; box-shadow: none; }
.extend-source { position: absolute; }
.extend-source img { width: 100%; height: 100%; object-fit: fill; }