PSD export: a slice is clipped to the region before it is rounded to pixels

A region whose edge falls inside a pixel no longer drops a slice that overlaps it, and no longer writes a slice that lies past it. The overlap is found in document coordinates, and only that part is converted to pixels.
This commit is contained in:
dotCooCoo
2026-11-15 16:14:51 -08:00
parent 9597392c45
commit dea17e5446
2 changed files with 16 additions and 3 deletions
+5 -3
View File
@@ -457,11 +457,13 @@ fn slices(doc: &Document, region: Rect, scale: f64, (w, h): (u32, u32)) -> Resul
return Err(format!("slice {number} has no finite bounds and can't be written to PSD"));
}
let Some(options) = users.get(&id).copied().or_else(|| doc.slice_options(id)) else { continue };
let edge = |v: f64, origin: f64, max: u32| ((v - origin) * scale).clamp(0.0, f64::from(max));
let (l, t, r, b) = (edge(r.x0, region.x0, w), edge(r.y0, region.y0, h), edge(r.x1, region.x0, w), edge(r.y1, region.y0, h));
if !(l < r && t < b) {
// The part inside `region`, found before the region is rounded to whole pixels.
let (x0, y0, x1, y1) = (r.x0.max(region.x0), r.y0.max(region.y0), r.x1.min(region.x1), r.y1.min(region.y1));
if !(x0 < x1 && y0 < y1) {
continue;
}
let edge = |v: f64, origin: f64, max: u32| ((v - origin) * scale).clamp(0.0, f64::from(max));
let (l, t, r, b) = (edge(x0, region.x0, w), edge(y0, region.y0, h), edge(x1, region.x0, w), edge(y1, region.y0, h));
// Whole pixels, at least one each way.
let span = |a: f64, b: f64, max: u32| {
let max = i64::from(max);
+11
View File
@@ -439,6 +439,17 @@ fn slices_are_cut_to_the_exported_region_in_its_pixels() {
thin.slices[0].rect = Rect::new(20.0, 5.0, 20.2, 15.0);
let res = slices_of(&export(&thin, &opts(PsdOptions::default()))).unwrap();
assert_eq!(res.slices[0].rect, [20, 5, 21, 15]);
// The overlap is found before the region is rounded to whole pixels: a slice inside a region
// that ends at 10.4 keeps its last pixel, and one past a region that ends at 10.6 isn't written.
let o = opts(PsdOptions::default());
let mut edge = doc.clone();
edge.slices[0].rect = Rect::new(10.1, 5.0, 10.3, 15.0);
edge.node_mut(edge.layers[0].children().unwrap()[0].id).unwrap().extras_mut().slice = None;
let file = Renderer::new().export_region(&edge, Rect::new(0.0, 0.0, 10.4, 60.0), RasterFormat::Psd, &o).unwrap();
assert_eq!(slices_of(&file).unwrap().slices.iter().map(|s| s.rect).collect::<Vec<_>>(), [[9, 5, 10, 15]]);
edge.slices[0].rect = Rect::new(10.7, 5.0, 10.9, 15.0);
let file = Renderer::new().export_region(&edge, Rect::new(0.0, 0.0, 10.6, 60.0), RasterFormat::Psd, &o).unwrap();
assert!(slices_of(&file).is_none());
}
#[test]