PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Removed normalized-coordinate heuristic in crop_image_impl
Combiler01925241b502c1
diff --git a/crates/edit/src/images.rs b/crates/edit/src/images.rsindex 89e1573d4..5bef101e4 100644--- a/crates/edit/src/images.rs+++ b/crates/edit/src/images.rs@@ -1565,32 +1565,20 @@ fn crop_image_impl(if !keep.iter().all(|v| v.is_finite() && v.abs() <= 1e12) {return Err(EditError::Invalid("the crop box must be finite and no larger than 1e12".into()));}- let is_normalized = keep[0] >= 0.0- && keep[1] >= 0.0- && keep[2] <= 1.0- && keep[3] <= 1.0- && keep[2] > keep[0]- && keep[3] > keep[1]- && ((image.rect[2] - image.rect[0]).abs() > 1.5 || (image.rect[3] - image.rect[1]).abs() > 1.5);-- let (u0, v0, u1, v1) = if is_normalized {- (keep[0], keep[1], keep[2], keep[3])- } else {- let [x0, y0, x1, y1] = [keep[0].min(keep[2]), keep[1].min(keep[3]), keep[0].max(keep[2]), keep[1].max(keep[3])];- if x1 - x0 < 1e-6 || y1 - y0 < 1e-6 {- return Err(EditError::Invalid("the crop box has no area".into()));- }- let inv = Matrix(image.matrix).invert().ok_or_else(|| EditError::Invalid("the image has no area".into()))?;- let corners = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)].map(|(x, y)| inv.apply(x, y));- let min_u = corners.iter().map(|(u, _)| *u).fold(f64::INFINITY, f64::min).clamp(0.0, 1.0);- let max_u = corners.iter().map(|(u, _)| *u).fold(f64::NEG_INFINITY, f64::max).clamp(0.0, 1.0);- let min_v = corners.iter().map(|(_, v)| *v).fold(f64::INFINITY, f64::min).clamp(0.0, 1.0);- let max_v = corners.iter().map(|(_, v)| *v).fold(f64::NEG_INFINITY, f64::max).clamp(0.0, 1.0);- if max_u - min_u < 1e-6 || max_v - min_v < 1e-6 {- return Err(EditError::Invalid("the crop box has no area".into()));- }- (min_u, min_v, max_u, max_v)- };+ let [x0, y0, x1, y1] = [keep[0].min(keep[2]), keep[1].min(keep[3]), keep[0].max(keep[2]), keep[1].max(keep[3])];+ if x1 - x0 < 1e-6 || y1 - y0 < 1e-6 {+ return Err(EditError::Invalid("the crop box has no area".into()));+ }+ let inv = Matrix(image.matrix).invert().ok_or_else(|| EditError::Invalid("the image has no area".into()))?;+ let corners = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)].map(|(x, y)| inv.apply(x, y));+ let min_u = corners.iter().map(|(u, _)| *u).fold(f64::INFINITY, f64::min).clamp(0.0, 1.0);+ let max_u = corners.iter().map(|(u, _)| *u).fold(f64::NEG_INFINITY, f64::max).clamp(0.0, 1.0);+ let min_v = corners.iter().map(|(_, v)| *v).fold(f64::INFINITY, f64::min).clamp(0.0, 1.0);+ let max_v = corners.iter().map(|(_, v)| *v).fold(f64::NEG_INFINITY, f64::max).clamp(0.0, 1.0);+ if max_u - min_u < 1e-6 || max_v - min_v < 1e-6 {+ return Err(EditError::Invalid("the crop box has no area".into()));+ }+ let (u0, v0, u1, v1) = (min_u, min_v, max_u, max_v);if u1 - u0 < 1e-6 || v1 - v0 < 1e-6 {return Err(EditError::Invalid("the crop box has no area".into()));diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rsindex 0814dbe45..8257403e7 100644--- a/crates/edit/src/tests.rs+++ b/crates/edit/src/tests.rs@@ -2238,7 +2238,7 @@ fn crop_rotated_image_preserves_angle_and_in_place_geometry() {assert!((m_before[1] - 100.0).abs() < 1e-3);assert!((m_before[2] - (-50.0)).abs() < 1e-3);- images::crop_image(&mut doc, 0, 0, [0.25, 0.25, 0.75, 0.75]).unwrap();+ images::crop_image(&mut doc, 0, 0, [140.0, 170.0, 180.0, 210.0]).unwrap();let doc = reopen(&doc);let img_cropped = &images::page_images(&doc, 0).unwrap()[0];