Skip to content

Commit 3cacb49

Browse files
committed
Add constraint step fill_crop() to address purely theoretical double rounding issue
1 parent eaca629 commit 3cacb49

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

imageflow_riapi/src/sizing.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,16 @@ impl Layout {
229229
..self
230230
})
231231
}
232+
pub fn fill_crop(self, target: AspectRatio) -> Result<Layout> {
233+
let new_source = target.box_of(&self.source, BoxKind::Inner)?;
234+
Ok(Layout {
235+
source: new_source,
236+
image: target,
237+
canvas: target,
238+
..self
239+
})
240+
241+
}
232242

233243
//Also distorts 'image' in a corresponding fashion.
234244
pub fn distort_canvas(self, target: AspectRatio) -> Result<Layout> {
@@ -323,6 +333,7 @@ impl Layout {
323333
match step {
324334
Step::None | Step::BeginSequence | Step::SkipIf(_) | Step::SkipUnless(_) => Ok(self),
325335
Step::ScaleToOuter => self.scale_canvas(self.target, BoxKind::Outer),
336+
Step::FillCrop => self.fill_crop(self.target),
326337
Step::ScaleToInner => self.scale_canvas(self.target, BoxKind::Inner),
327338
Step::PadAspect => self.pad_canvas(self.target.box_of(&self.canvas, BoxKind::Outer)?),
328339
Step::Pad => self.pad_canvas(self.target),
@@ -493,6 +504,8 @@ pub enum Step {
493504
Crop, //What about intersect? Crop that doesn't fail out of bounds
494505
CropToIntersection,
495506
CropAspect,
507+
/// Use ScaleToOuterAndCrop instead of ScaleToOuter, then Crop, because the combination can reduce the dimensions below the outer box
508+
FillCrop,
496509
/// We can use a variety of hints, and we're not required to fully change the aspect ratio or achieve the target box
497510
PartialCrop,
498511
PartialCropAspect,
@@ -562,6 +575,10 @@ impl StepsBuilder {
562575
self.steps.push(Step::ScaleToOuter);
563576
self
564577
}
578+
pub fn fill_crop(mut self) -> StepsBuilder {
579+
self.steps.push(Step::FillCrop);
580+
self
581+
}
565582
pub fn distort(mut self, t: BoxParam) -> StepsBuilder {
566583
self.steps.push(Step::Distort(t));
567584
self

imageflow_riapi/src/sizing_tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ fn strategy_to_steps(s: Strategy) -> Option<Vec<Step>> {
8888

8989
//Scale_to_outer can reduce the width, then crop the height, causing both coordinates to be smaller
9090
//TODO: perhaps combine scale_to_outer and crop() into a single operation to prevent this?
91-
Strategy::CropOrAspect => { steps().skip_if(Cond::Either(Ordering::Less)).scale_to_outer().crop()
91+
Strategy::CropOrAspect => { steps().skip_if(Cond::Either(Ordering::Less)).fill_crop()
9292
.new_seq().skip_unless(Cond::Either(Ordering::Less)).crop_aspect() },
9393

9494

9595
//I think we need multiple parts, as we don't offer a way to compare against the obox
96-
Strategy::CropDownscaleOnly => { steps().skip_if(Cond::Either(Ordering::Less)).scale_to_outer().crop().new_seq().skip_unless(Cond::Larger1DSmaller1D).crop_intersection() },
96+
Strategy::CropDownscaleOnly => { steps().skip_if(Cond::Either(Ordering::Less)).fill_crop().new_seq().skip_unless(Cond::Larger1DSmaller1D).crop_intersection() },
9797
// Strategy::CropCarefulDownscale => StepSet::AnyLarger(vec![Step::ScaleToOuter,
9898
// Step::PartialCropAspect, Step::ScaleToInner]),
9999
// Strategy::ExactCropAllowUpscaling => StepSet::Always(vec![Step::ScaleToOuter,
@@ -591,7 +591,7 @@ fn test_scale_to_outer(){
591591
#[test]
592592
fn test_scale_to_outer_and_crop(){
593593
let cropper = sizing::IdentityCropProvider::new();
594-
let result = Layout::create(r(2,4), r(1,3)).execute_all(&steps().scale_to_outer().crop().into_vec(), &cropper).unwrap();
594+
let result = Layout::create(r(2,4), r(1,3)).execute_all(&steps().fill_crop().into_vec(), &cropper).unwrap();
595595
assert_eq!(result.get_source_crop(), r(1,4))
596596
}
597597

@@ -995,4 +995,4 @@ static SMALL_PRIMES:[i32;1000] = [
995995
,7573,7577,7583,7589,7591,7603,7607,7621,7639,7643
996996
,7649,7669,7673,7681,7687,7691,7699,7703,7717,7723
997997
,7727,7741,7753,7757,7759,7789,7793,7817,7823,7829
998-
,7841,7853,7867,7873,7877,7879,7883,7901,7907,7919];
998+
,7841,7853,7867,7873,7877,7879,7883,7901,7907,7919];

0 commit comments

Comments
 (0)