Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Crop local image edits to selection bounds

Bittidfd2c43e9216
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index a5ab4d7..59fbe91 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -21,6 +21,7 @@ Changes since 0.3.2.
## Fixes and improvements
+- Local image edits now generate only the selected bounding box or lasso bounds, padded to multiples of 32, and merge into the original image without changing surrounding pixels.
- Generate's dropdown arrow now shares its neutral disabled colors while keeping available upscale actions accessible.
- Single Shot with Context video now matches the two-minute Keystone/Capstone test's fully locked 22-frame overlap, preserving earlier video and audio instead of regenerating their tail. Update LAN workers alongside the app.
- Corrected Motion thumbnail sizing and title/duration alignment in the Timeline's list and grid layouts.
diff --git a/src-tauri/core/src/slopfab/generation_tests.rs b/src-tauri/core/src/slopfab/generation_tests.rs
index 8d5552a..52e8d25 100644
--- a/src-tauri/core/src/slopfab/generation_tests.rs
+++ b/src-tauri/core/src/slopfab/generation_tests.rs
@@ -130,18 +130,25 @@ fn cropped_edit_sequence_merges_each_result_into_the_original_canvas() {
]}
})).unwrap();
item.request.image_edit_path = Some(path.clone());
+ item.request.reference_paths = vec![path.to_string_lossy().into_owned()];
let mut calls = 0;
+ let mut crop_references = Vec::new();
let (metadata, merged) = run_edit_sequence(&item, |stage| {
- assert_eq!((stage.request.canvas_width, stage.request.canvas_height), (64, 64));
+ assert_eq!((stage.request.canvas_width, stage.request.canvas_height), (32, 32));
let input = stage.request.image_edit_pixels.as_ref().unwrap();
+ let reference = std::path::PathBuf::from(&stage.request.reference_paths[0]);
+ assert_ne!(reference, path);
+ assert_eq!(image::open(&reference).unwrap().to_rgb8().as_raw(), input.as_ref());
+ crop_references.push(reference);
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)))?))
+ metadata.width = 32; metadata.height = 32;
+ Ok((metadata, super::super::image_regions::rendered_image("crop-sequence", image::RgbaImage::from_pixel(32, 32, image::Rgba(color)))?))
}).unwrap();
assert_eq!(calls, 2);
+ assert!(crop_references.iter().all(|path| !path.exists()));
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 {
diff --git a/src-tauri/core/src/slopfab/image_regions.rs b/src-tauri/core/src/slopfab/image_regions.rs
index 7fb39f3..b35791f 100644
--- a/src-tauri/core/src/slopfab/image_regions.rs
+++ b/src-tauri/core/src/slopfab/image_regions.rs
@@ -24,8 +24,11 @@ pub(super) struct RegionEdit {
y: u32,
width: u32,
height: u32,
+ whole: bool,
step: ImageEditStep,
mask: Option<Vec<u8>>,
+ // Keep the cropped prompt reference alive through planning/inference.
+ _reference: Option<tempfile::NamedTempFile>,
}
impl RegionEdit {
@@ -36,22 +39,49 @@ impl RegionEdit {
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 align = |value: i32| ((value + 31) / 32 * 32) as u32;
+ let cw = if whole { size.width as u32 } else { align(step.width) };
+ let ch = if whole { size.height as u32 } else { align(step.height) };
+ let width = if whole { source.width() } else { cw.min(source.width()) };
+ let height = if whole { source.height() } else { ch.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 crop = if whole {
+ imageops::resize(&crop, cw, ch, imageops::FilterType::Lanczos3)
+ } else if (width, height) != (cw, ch) {
+ // Extend edge pixels when the image itself is smaller than the grid.
+ // Padding must never rescale the selection or its mask.
+ RgbaImage::from_fn(cw, ch, |x, y| *crop.get_pixel(x.min(width - 1), y.min(height - 1)))
+ } else { crop };
let mut prepared = request.clone();
+ let mut reference = None;
+ if let Some(source_path) = &request.image_edit_path {
+ let canonical = std::fs::canonicalize(source_path).ok();
+ for path in prepared.reference_paths.iter_mut().chain(
+ prepared.image_edit.iter_mut().flat_map(|edit| edit.edits.iter_mut())
+ .flat_map(|step| step.reference_paths.iter_mut().flatten())) {
+ let matches = std::path::Path::new(&*path) == source_path
+ || canonical.as_ref().is_some_and(|source| std::fs::canonicalize(&*path).as_ref().ok() == Some(source));
+ if !matches { continue; }
+ if reference.is_none() {
+ let mut file = tempfile::Builder::new().prefix("slopus-edit-").suffix(".png").tempfile()
+ .map_err(|e| format!("Could not prepare cropped image reference: {e}"))?;
+ crop.write_to(file.as_file_mut(), image::ImageFormat::Png)
+ .map_err(|e| format!("Could not save cropped image reference: {e}"))?;
+ reference = Some(file);
+ }
+ *path = reference.as_ref().unwrap().path().to_string_lossy().into_owned();
+ }
+ }
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()];
+ prepared_edit.edits.truncate(1);
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 };
@@ -61,8 +91,8 @@ impl RegionEdit {
// 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);
+ point.x -= x as f64;
+ point.y -= y 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 {
@@ -70,7 +100,7 @@ impl RegionEdit {
} 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 })
+ Ok(Self { request: prepared, x, y, width, height, whole, step, mask, _reference: reference })
}
pub fn merge(&self, source: &mut RgbaImage, result: &rendered::RenderedVideo) -> Result<(), String> {
@@ -80,7 +110,7 @@ impl RegionEdit {
}
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) {
+ let crop = if self.whole && crop.dimensions() != (self.width, self.height) {
imageops::resize(&crop, self.width, self.height, imageops::FilterType::Lanczos3)
} else { crop };
let step = &self.step;
@@ -90,9 +120,7 @@ impl RegionEdit {
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
+ mask[(cy * cw + cx) as usize] 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);
@@ -141,10 +169,10 @@ mod tests {
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));
+ assert_eq!((region.x, region.y, region.width, region.height), (80, 50, 32, 32));
let step = &region.request.image_edit.as_ref().unwrap().edits[0];
- assert_eq!((step.x, step.y, step.width, step.height), (16, 17, 32, 32));
- assert_eq!(&region.request.image_edit_pixels.as_ref().unwrap()[..3], &[64, 33, 70]);
+ assert_eq!((step.x, step.y, step.width, step.height), (0, 0, 32, 32));
+ assert_eq!(&region.request.image_edit_pixels.as_ref().unwrap()[..3], &[80, 50, 70]);
region.merge(&mut source, &generated(&region)).unwrap();
assert_eq!(source.dimensions(), (129, 97));
for (x, y, pixel) in source.enumerate_pixels() {
@@ -200,6 +228,50 @@ mod tests {
assert!(source.pixels().all(|pixel| pixel.0 == [250, 240, 230, 255]));
}
+ #[test]
+ fn large_source_uses_only_aligned_selection_pixels_at_the_bottom_right_edge() {
+ let mut request = request();
+ request.canvas_width = 4352; request.canvas_height = 6529;
+ let edit = request.image_edit.as_mut().unwrap();
+ edit.region_size = Some(super::super::types::ImageEditRegionSize { width: 4352, height: 6560 });
+ let step = &mut edit.edits[0];
+ step.x = 3916; step.y = 5876; step.width = 436; step.height = 653;
+ let mut source = RgbaImage::from_fn(4352, 6529, |x, y| image::Rgba([x as u8, y as u8, 70, 120]));
+ let region = RegionEdit::prepare(&request, &source).unwrap();
+ assert_eq!((region.request.canvas_width, region.request.canvas_height), (448, 672));
+ assert_eq!(region.request.image_edit_pixels.as_ref().unwrap().len(), 448 * 672 * 3);
+ assert_eq!((region.x, region.y), (3904, 5857));
+ region.merge(&mut source, &generated(&region)).unwrap();
+ for (x, y, pixel) in source.enumerate_pixels() {
+ let expected = if x >= 3916 && y >= 5876 { [250, 240, 230, 255] } else { [x as u8, y as u8, 70, 120] };
+ assert_eq!(pixel.0, expected);
+ }
+ }
+
+ #[test]
+ fn padding_a_small_source_preserves_lasso_coordinates_and_pixels() {
+ let mut request = request();
+ request.canvas_width = 17; request.canvas_height = 31;
+ let step = &mut request.image_edit.as_mut().unwrap().edits[0];
+ step.x = 0; step.y = 0; step.width = 17; step.height = 31;
+ step.mask_polygon = Some(vec![image_mask::MaskPoint { x: 0.0, y: 0.0 },
+ image_mask::MaskPoint { x: 17.0, y: 0.0 }, image_mask::MaskPoint { x: 0.0, y: 31.0 }]);
+ let mut source = RgbaImage::from_fn(17, 31, |x, y| image::Rgba([x as u8, y as u8, 70, 120]));
+ let original = source.clone();
+ let region = RegionEdit::prepare(&request, &source).unwrap();
+ assert_eq!((region.request.canvas_width, region.request.canvas_height), (32, 32));
+ let input = region.request.image_edit_pixels.as_ref().unwrap();
+ for y in 0..32 { for x in 0..32 {
+ assert_eq!(&input[(y * 32 + x) * 3..(y * 32 + x + 1) * 3], &[x.min(16) as u8, y.min(30) as u8, 70]);
+ }}
+ assert_eq!(region.request.image_edit.as_ref().unwrap().edits[0].mask_polygon,
+ request.image_edit.as_ref().unwrap().edits[0].mask_polygon);
+ region.merge(&mut source, &generated(&region)).unwrap();
+ assert_eq!(source.dimensions(), (17, 31));
+ assert_eq!(source.get_pixel(16, 30), original.get_pixel(16, 30));
+ assert_eq!(source.get_pixel(1, 1).0, [250, 240, 230, 255]);
+ }
+
#[test]
fn native_planning_uses_the_same_crop_and_rejects_oversized_or_misaligned_areas() {
let folder = tempfile::tempdir().unwrap();
@@ -207,7 +279,15 @@ mod tests {
source().save(&path).unwrap();
let mut request = request();
request.image_edit_path = Some(path);
+ request.reference_paths = vec![request.image_edit_path.as_ref().unwrap().to_string_lossy().into_owned()];
+ request.image_edit.as_mut().unwrap().edits[0].reference_paths = Some(request.reference_paths.clone());
let region = prepare_first(&request).unwrap().unwrap();
+ let reference = std::path::PathBuf::from(&region.request.reference_paths[0]);
+ assert_ne!(reference, request.image_edit_path.as_ref().unwrap().as_path());
+ let crop = image::open(&reference).unwrap().to_rgb8();
+ assert_eq!(crop.dimensions(), (32, 32));
+ assert_eq!(crop.as_raw(), region.request.image_edit_pixels.as_ref().unwrap().as_ref());
+ assert_eq!(region.request.image_edit.as_ref().unwrap().edits[0].reference_paths.as_ref().unwrap(), &region.request.reference_paths);
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] {
@@ -215,8 +295,10 @@ mod tests {
super::super::h3::configure_request(&api, &handle, &region.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));
+ assert_eq!((plan.canvas_width, plan.canvas_height), (32, 32));
}
+ drop(region);
+ assert!(!reference.exists());
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"));
diff --git a/src-tauri/core/src/worker/protocol.rs b/src-tauri/core/src/worker/protocol.rs
index db386fe..1626e41 100644
--- a/src-tauri/core/src/worker/protocol.rs
+++ b/src-tauri/core/src/worker/protocol.rs
@@ -35,7 +35,8 @@ pub const SERVICE_TYPE: &str = "_slopus-worker._tcp.local.";
// v29 aligns Single Shot's Keystone conditioning guide with its original context anchor.
// v30 carries portable learned-drift calibration and LCS highlight controls.
// v31 matches the two-minute anchored recipe: lock 22 frames without replacing the source tail.
-pub const PROTOCOL_VERSION: u32 = 31;
+// v32 sizes local image inference from selection bounds, padded to the 32-pixel grid.
+pub const PROTOCOL_VERSION: u32 = 32;
/// Streaming SeedVR2 restoration; model paths are resolved on the worker.
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -390,7 +391,7 @@ 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, 31);
+ assert_eq!(PROTOCOL_VERSION, 32);
}
#[test]
diff --git a/src/components/workspace/ImageEditor.test.tsx b/src/components/workspace/ImageEditor.test.tsx
index aeb9c50..243c6e1 100644
--- a/src/components/workspace/ImageEditor.test.tsx
+++ b/src/components/workspace/ImageEditor.test.tsx
@@ -250,7 +250,7 @@ it("edits imported-image generation settings and restores them with the draft",
initial.assets = [{ id: "imported", name: "Imported", kind: "image", relativePath: "media/source.png", mimeType: "image/png", width: 1001, height: 809, createdAt: initial.createdAt }];
const current = setup(initial);
fireEvent.click(screen.getByRole("button", { name: "View Imported" }));
- expect(comboValue(screen.getByLabelText("Resolution"))).toBe("992x800");
+ expect(comboValue(screen.getByLabelText("Resolution"))).toBe("1024x832");
typePrompt(screen.getByLabelText("Edit prompt"), "Warm up the lighting");
fireEvent.change(screen.getByLabelText("Steps"), { target: { value: "32" } });
fireEvent.change(screen.getByLabelText("Seed"), { target: { value: "123456" } });
@@ -261,14 +261,14 @@ it("edits imported-image generation settings and restores them with the draft",
const reopened = parseProjectConfig(JSON.parse(JSON.stringify(current())));
expect(reopened.imageScene).toEqual(current().imageScene);
fireEvent.click(screen.getByRole("button", { name: "View Imported" }));
- expect(comboValue(screen.getByLabelText("Resolution"))).toBe("992x800");
+ expect(comboValue(screen.getByLabelText("Resolution"))).toBe("1024x832");
expect(screen.getByLabelText("Steps")).toHaveValue(20);
expect(screen.getByLabelText("Seed")).toHaveValue(-1);
fireEvent.click(screen.getByRole("button", { name: "View Editing Imported" }));
expect(screen.getByLabelText("Steps")).toHaveValue(32);
expect(screen.getByLabelText("Seed")).toHaveValue(123456);
expect(comboValue(screen.getByLabelText("Resolution"))).toBe("672x544");
- choose("Resolution", "992 × 800");
+ choose("Resolution", "1024 × 832");
expect(compileImageEdits(current()).edits[0]).toMatchObject({ width: 1001, height: 809 });
});
@@ -488,7 +488,7 @@ it("adds image files in edit mode and saves whole-image and drawn edits with sep
// References come from what each prompt cites; there is no list to tick.
expect(screen.queryByRole("checkbox", { name: "Mood" })).not.toBeInTheDocument();
typePrompt(screen.getByLabelText("Edit prompt"), "Make the lighting warmer like @[ref:Mood]");
- expect(compileImageEdits(current()).edits[0]).toMatchObject({ x: 0, y: 0, width: 992, height: 800 });
+ expect(compileImageEdits(current()).edits[0]).toMatchObject({ x: 0, y: 0, width: 1000, height: 800 });
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 }) });
@@ -1261,7 +1261,7 @@ it("draws a lasso, edits its prompt, moves its outline, and saves it independent
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(compileImageEdits(current()).edits[0]).toMatchObject({ feather: 0, maskPolygon: [{ x: 200, y: 160 }, { x: 500, y: 160 }, { x: 200, 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" }));
diff --git a/src/lib/imageEditing.test.ts b/src/lib/imageEditing.test.ts
index 80999dd..769eb19 100644
--- a/src/lib/imageEditing.test.ts
+++ b/src/lib/imageEditing.test.ts
@@ -1,11 +1,11 @@
import { expect, it } from "vitest";
import { parseProjectConfig } from "./project";
import { addImageNode, createImageEditScene } from "./imageScene";
-import { compileImageEdits, editGeneratedImage, imageEditSize, imageEditCanvasSize, limitImageEditScene } from "./imageEditing";
+import { compileImageEdits, editGeneratedImage, imageEditSize, imageEditCanvasSize, imageEditRegionSize, limitImageEditScene } from "./imageEditing";
import fixture from "../../fixtures/project-v1-image.json";
it.each([
- [1001, 809, 992, 800], [809, 1001, 800, 992], [640, 480, 640, 480], [17, 31, 32, 32],
+ [1001, 809, 1024, 832], [809, 1001, 832, 1024], [640, 480, 640, 480], [17, 31, 32, 32],
])("aligns a %i × %i source to %i × %i", (width, height, expectedWidth, expectedHeight) => {
expect(imageEditSize({ width, height })).toEqual({ width: expectedWidth, height: expectedHeight });
});
@@ -22,7 +22,17 @@ it("keeps local edits in original pixels when the target is smaller and validate
expect(() => parseProjectConfig(config)).toThrow();
});
-it("uses hierarchy order, skips empty groups, and converts boxes to the rounded output dimensions", () => {
+it.each([undefined, { width: 4352, height: 6560 }])("always crops local edits on a 4352 by 6529 source with target %j", (outputSize) => {
+ const config = parseProjectConfig(fixture);
+ config.imageScene = addImageNode(createImageEditScene({ relativePath: "source.png", name: "Large", width: 4352, height: 6529 }), "image-root", "object", { x: 900, y: 900, width: 100, height: 100 });
+ config.imageScene.outputSize = outputSize;
+ config.imageScene.nodes[1].description = "Change the flower";
+ expect(imageEditCanvasSize(config.imageScene)).toEqual({ width: 4352, height: 6529 });
+ expect(imageEditRegionSize(config.imageScene)).toEqual({ width: 4352, height: 6560 });
+ expect(compileImageEdits(config).edits[0]).toMatchObject({ x: 3916, y: 5876, width: 436, height: 653 });
+});
+
+it("uses hierarchy order, skips empty groups, and converts boxes to original pixel dimensions", () => {
const config = parseProjectConfig(fixture);
let scene = createImageEditScene({ relativePath: "media/imported/source.png", name: "Source", width: 101, height: 77 });
scene = addImageNode(scene, "image-root", "group");
@@ -36,10 +46,10 @@ it("uses hierarchy order, skips empty groups, and converts boxes to the rounded
config.settings.defaultLook = "watercolor";
const { edits } = compileImageEdits(config);
expect(edits).toHaveLength(2);
- expect(edits[0]).toMatchObject({ x: 86, y: 57, width: 10, height: 7 });
+ expect(edits[0]).toMatchObject({ x: 90, y: 69, width: 11, height: 8 });
expect(edits[0].prompt).toContain("A red balloon");
expect(edits[0].prompt).not.toMatch(/Use these colors|#FF0000/);
- expect(edits[1]).toMatchObject({ x: 24, y: 16, width: 48, height: 32 });
+ expect(edits[1]).toMatchObject({ x: 25, y: 19, width: 51, height: 39 });
expect(edits[1].prompt).toContain("A blue boat");
expect(edits[0].prompt).not.toContain("watercolor");
expect(edits[0].prompt).toContain("[Shot 1]");
@@ -97,7 +107,7 @@ it("applies whole-image actions first and compiles only the references each edit
config.imageScene = addImageNode(config.imageScene, root.id, "object");
Object.assign(config.imageScene.nodes[1], { description: "Add a @[ref:Balloon]" });
let edits = compileImageEdits(config).edits;
- expect(edits[0]).toMatchObject({ x: 0, y: 0, width: 96, height: 64, feather: 0 });
+ expect(edits[0]).toMatchObject({ x: 0, y: 0, width: 101, height: 77, feather: 0 });
expect(edits[1].feather).toBeUndefined();
expect(edits[0].prompt).toContain("Warm up the entire image like <Subject 1>");
expect(edits[0].prompt).toContain("<Subject 1> is Lighting, providing appearance from <Picture 2>");
diff --git a/src/lib/imageEditing.ts b/src/lib/imageEditing.ts
index 0d4b0d3..5696d3a 100644
--- a/src/lib/imageEditing.ts
+++ b/src/lib/imageEditing.ts
@@ -21,19 +21,18 @@ export interface ImageEditStep {
refmods?: Array<{ path: string; strength: number; copies: number }>;
}
export const imageEditDebugPrompt = (edits: ImageEditStep[]) => edits.map((edit, index) => `Edit ${index + 1} (${edit.x}, ${edit.y}, ${edit.width}, ${edit.height})\n${edit.prompt}`).join("\n\n");
-/** Imported images default to their own dimensions, rounded down to the model grid. */
-export function imageEditSize(source: Pick<ImageSource, "width" | "height">, shortEdge = Math.min(source.width, source.height)) {
- const scale = shortEdge / Math.min(source.width, source.height);
- const align = (value: number) => Math.max(32, Math.floor(value / 32) * 32);
+/** Cover every source pixel by default; explicit resolution choices fit the model grid. */
+export function imageEditSize(source: Pick<ImageSource, "width" | "height">, shortEdge?: number) {
+ const scale = shortEdge === undefined ? 1 : shortEdge / Math.min(source.width, source.height);
+ const align = (value: number) => Math.max(32, (shortEdge === undefined ? Math.ceil(value / 32) : Math.floor(value / 32)) * 32);
return { width: align(source.width * scale), height: align(source.height * scale) };
}
export function imageEditOutputSize(scene: ImageScene) {
return scene.outputSize ?? imageEditSize(scene.sourceImage!);
}
-/** An explicitly smaller target limits local inference, not the saved canvas. */
+/** Every edit uses a bounded inference crop and preserves the source 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;
+ return scene.sourceImage ? imageEditOutputSize(scene) : undefined;
}
export function imageEditCanvasSize(scene: ImageScene) {
return imageEditRegionSize(scene) ? { width: scene.sourceImage!.width, height: scene.sourceImage!.height } : imageEditOutputSize(scene);
diff --git a/src/lib/workQueue.test.ts b/src/lib/workQueue.test.ts
index 4a05351..a6072c8 100644
--- a/src/lib/workQueue.test.ts
+++ b/src/lib/workQueue.test.ts
@@ -1313,9 +1313,9 @@ describe("image generation work", () => {
const request = vi.mocked(enqueueSlopfabGeneration).mock.calls[0][0];
const steps = request.imageEdit!.edits;
expect(steps).toHaveLength(2);
- expect(steps[0]).toMatchObject({ x: 0, y: 0, width: 96, height: 64, feather: 0 });
- const outer = [{ x: 24, y: 16 }, { x: 72, y: 16 }, { x: 24, y: 48 }];
- expect(steps[1]).toMatchObject({ feather: 0, ...(compound ? { maskContours: [outer, [{ x: 36, y: 24 }, { x: 48, y: 24 }, { x: 36, y: 32 }]] } : { maskPolygon: outer }) });
+ expect(steps[0]).toMatchObject({ x: 0, y: 0, width: 101, height: 77, feather: 0 });
+ const outer = [{ x: 25.25, y: 19.25 }, { x: 75.75, y: 19.25 }, { x: 25.25, y: 57.75 }];
+ expect(steps[1]).toMatchObject({ feather: 0, ...(compound ? { maskContours: [outer, [{ x: 37.875, y: 28.875 }, { x: 50.5, y: 28.875 }, { x: 37.875, y: 38.5 }]] } : { maskPolygon: outer }) });
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 }]);
@@ -1356,9 +1356,9 @@ describe("image generation work", () => {
}
const draftId = session.getSnapshot().config.imageScene!.outputAssetId!;
expect(session.getSnapshot().config.assets[1]).toMatchObject({ id: draftId, imageDraft: true });
- expect(request).toMatchObject({ stillImage: true, frames: 1, canvasWidth: 96, canvasHeight: 64, imageEdit: { sourceRelativePath: "media/generated/source.jpg", edits: [
- { prompt: expect.stringContaining("A red balloon"), x: 24, y: 16, width: 48, height: 32 },
- { prompt: expect.stringContaining("A blue boat"), x: 24, y: 16, width: 48, height: 32 },
+ expect(request).toMatchObject({ stillImage: true, frames: 1, canvasWidth: 101, canvasHeight: 77, imageEdit: { sourceRelativePath: "media/generated/source.jpg", regionSize: { width: 128, height: 96 }, edits: [
+ { prompt: expect.stringContaining("A red balloon"), x: 25, y: 19, width: 51, height: 39 },
+ { prompt: expect.stringContaining("A blue boat"), x: 25, y: 19, width: 51, height: 39 },
] } });
await finish(queue, request.jobId);
expect(invoke).toHaveBeenCalledWith("save_generated_image", { folderPath: record.folderPath, jobId: request.jobId, format: "png" });
@@ -1369,7 +1369,7 @@ describe("image generation work", () => {
expect(result.assets[1]).toMatchObject({ id: draftId, imageDraft: false, relativePath: "media/generated/edited.png", parentAssetId: result.assets[0].id });
expect(result.imageScene!.outputAssetId).toBe(draftId);
expect(result.imageScene).toMatchObject({ rootType: "image", sourceImage: { relativePath: "media/generated/edited.png" } });
- expect(result.assets[1]).toMatchObject({ mimeType: "image/png", imageGeneration: { scene, prompt: expect.stringContaining("Edit 2 (24, 16, 48, 32)") } });
+ expect(result.assets[1]).toMatchObject({ mimeType: "image/png", imageGeneration: { scene, prompt: expect.stringContaining("Edit 2 (25, 19, 51, 39)") } });
expect(result.assets[1].imageGeneration!.scene.nodes).toEqual(scene.nodes);
expect(restoreGeneratedImage(result, result.assets[0].id).imageScene!.nodes).toMatchObject([{ description: "" }]);
expect(result.assets[0].imageEditing).toBeUndefined();