outline-core 0.2.0

Image background removal with flexible mask processing options
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
use std::borrow::Cow;
use std::path::Path;
use std::sync::Arc;

use image::{GrayImage, RgbImage, RgbaImage};

use crate::config::{ErosionBorderMode, MaskProcessingDefaults};
use crate::foreground::{ForegroundHandle, compose_foreground};
use crate::geometry::{
    BoundingBox, Padding, crop_bounds_fit_image, crop_gray_image, crop_rgb_image,
    mask_bounding_box, pad_gray_image, pad_rgb_image,
};
use crate::mask::{
    MaskColor, MaskHandle, MaskOperation, MaskPipeline, apply_operations, colorize_mask,
};
use crate::{MaskVectorizer, OutlineResult};

/// Inference result containing the original RGB image and raw matte prediction.
///
/// Returned by [`crate::Outline::for_image`] after running model inference.
///
/// # Example
/// ```no_run
/// use outline::Outline;
///
/// let outline = Outline::new("model.onnx");
/// let session = outline.for_image("input.png")?;
/// let matte = session.matte();
///
/// // Access the original image and raw matte directly
/// let rgb = session.rgb_image();
/// let raw_matte = session.raw_matte();
///
/// // Compose the foreground from the raw matte
/// let foreground = matte.foreground()?;
/// foreground.save("foreground.png")?;
/// # Ok::<_, outline::OutlineError>(())
/// ```
#[derive(Debug, Clone)]
pub struct InferencedMatte {
    rgb_image: Arc<RgbImage>,
    raw_matte: Arc<GrayImage>,
    mask_processing_defaults: MaskProcessingDefaults,
}

impl InferencedMatte {
    pub(crate) fn new(
        rgb_image: RgbImage,
        raw_matte: GrayImage,
        mask_processing_defaults: MaskProcessingDefaults,
    ) -> Self {
        Self {
            rgb_image: Arc::new(rgb_image),
            raw_matte: Arc::new(raw_matte),
            mask_processing_defaults,
        }
    }

    /// Get a reference to the original RGB image.
    pub fn rgb_image(&self) -> &RgbImage {
        self.rgb_image.as_ref()
    }

    /// Get a reference to the raw grayscale matte.
    pub fn raw_matte(&self) -> &GrayImage {
        self.raw_matte.as_ref()
    }

    /// Begin building a mask processing pipeline from the raw matte.
    pub fn matte(&self) -> MatteHandle {
        MatteHandle {
            rgb_image: Arc::clone(&self.rgb_image),
            raw_matte: Arc::clone(&self.raw_matte),
            mask_processing_defaults: self.mask_processing_defaults.clone(),
            operations: Vec::new(),
        }
    }
}

/// Builder for chaining mask processing operations on the raw matte.
///
/// The raw matte is the soft, grayscale alpha prediction from the model.
///
/// # Example
/// ```no_run
/// use outline::Outline;
///
/// let outline = Outline::new("model.onnx");
/// let session = outline.for_image("input.jpg")?;
///
/// // Chain operations and execute them
/// let mask = session.matte()
///     .blur_with(6.0)          // Smooth edges
///     .threshold_with(120)     // Convert to binary
///     .dilate_with(5.0)        // Expand slightly
///     .processed()?;           // Execute operations
///
/// mask.save("mask.png")?;
/// # Ok::<_, outline::OutlineError>(())
/// ```
#[derive(Debug, Clone)]
pub struct MatteHandle {
    rgb_image: Arc<RgbImage>,
    raw_matte: Arc<GrayImage>,
    mask_processing_defaults: MaskProcessingDefaults,
    operations: Vec<MaskOperation>,
}

impl MatteHandle {
    fn resolved_matte(&self) -> Cow<'_, GrayImage> {
        if self.operations.is_empty() {
            Cow::Borrowed(self.raw_matte.as_ref())
        } else {
            Cow::Owned(apply_operations(self.raw_matte.as_ref(), &self.operations))
        }
    }

    fn resolve_pending_operations(mut self) -> Self {
        if self.operations.is_empty() {
            return self;
        }

        let operations = std::mem::take(&mut self.operations);
        let matte = apply_operations(self.raw_matte.as_ref(), &operations);
        Self {
            rgb_image: self.rgb_image,
            raw_matte: Arc::new(matte),
            mask_processing_defaults: self.mask_processing_defaults,
            operations: Vec::new(),
        }
    }

    /// Clone and return the raw grayscale matte.
    pub fn to_raw_matte(&self) -> GrayImage {
        (*self.raw_matte).clone()
    }

    /// Get a reference to the raw grayscale matte.
    pub fn as_raw_matte(&self) -> &GrayImage {
        self.raw_matte.as_ref()
    }

    /// Return the current matte canvas dimensions.
    pub fn dimensions(&self) -> (u32, u32) {
        self.raw_matte.dimensions()
    }

    /// Get the raw grayscale matte.
    #[deprecated(note = "use to_raw_matte()")]
    pub fn raw(&self) -> GrayImage {
        self.to_raw_matte()
    }

    /// Consume the handle and return the current matte as a grayscale image.
    pub fn into_image(self) -> GrayImage {
        if self.operations.is_empty() {
            Arc::unwrap_or_clone(self.raw_matte)
        } else {
            apply_operations(self.raw_matte.as_ref(), &self.operations)
        }
    }

    /// Save the current matte to the specified path.
    pub fn save(&self, path: impl AsRef<Path>) -> OutlineResult<()> {
        self.resolved_matte().save(path)?;
        Ok(())
    }

    /// Compute the bounding box of the current matte using a non-zero threshold.
    pub fn bounding_box(&self) -> Option<BoundingBox> {
        self.bounding_box_with(1)
    }

    /// Compute the bounding box of the current matte at or above `threshold`.
    pub fn bounding_box_with(&self, threshold: u8) -> Option<BoundingBox> {
        let mask = self.resolved_matte();
        mask_bounding_box(&mask, threshold)
    }

    /// Add a blur operation using the default sigma.
    pub fn blur(mut self) -> Self {
        let sigma = self.mask_processing_defaults.blur_sigma;
        self.operations.push(MaskOperation::Blur { sigma });
        self
    }

    /// Add a blur operation with a custom sigma.
    pub fn blur_with(mut self, sigma: f32) -> Self {
        self.operations.push(MaskOperation::Blur { sigma });
        self
    }

    /// Add a threshold operation using the default mask threshold.
    pub fn threshold(mut self) -> Self {
        let value = self.mask_processing_defaults.mask_threshold;
        self.operations.push(MaskOperation::Threshold { value });
        self
    }

    /// Add a threshold operation with a custom value.
    pub fn threshold_with(mut self, value: u8) -> Self {
        self.operations.push(MaskOperation::Threshold { value });
        self
    }

    /// Add a dilation operation using the default radius.
    ///
    /// **Note**: Dilation typically works best on binary masks. Consider calling
    /// [`threshold`](MatteHandle::threshold) before `dilate` if working with a soft matte.
    pub fn dilate(mut self) -> Self {
        let radius = self.mask_processing_defaults.dilation_radius;
        self.operations.push(MaskOperation::Dilate { radius });
        self
    }

    /// Add a dilation operation with a custom radius.
    ///
    /// **Note**: Dilation typically works best on binary masks. Consider calling
    /// [`threshold`](MatteHandle::threshold) before `dilate` if working with a soft matte.
    pub fn dilate_with(mut self, radius: f32) -> Self {
        self.operations.push(MaskOperation::Dilate { radius });
        self
    }

    /// Add an erosion operation using the default radius.
    ///
    /// **Note**: Erosion typically works best on binary masks. Consider calling
    /// [`threshold`](MatteHandle::threshold) before `erode` if working with a soft matte.
    pub fn erode(mut self) -> Self {
        let radius = self.mask_processing_defaults.erosion_radius;
        let border_mode = self.mask_processing_defaults.erosion_border_mode;
        self.operations.push(MaskOperation::Erode {
            radius,
            border_mode,
        });
        self
    }

    /// Add an erosion operation with a custom radius.
    ///
    /// **Note**: Erosion typically works best on binary masks. Consider calling
    /// [`threshold`](MatteHandle::threshold) before `erode` if working with a soft matte.
    pub fn erode_with(mut self, radius: f32) -> Self {
        let border_mode = self.mask_processing_defaults.erosion_border_mode;
        self.operations.push(MaskOperation::Erode {
            radius,
            border_mode,
        });
        self
    }

    /// Add an erosion operation with a custom radius and boundary behavior.
    ///
    /// **Note**: Erosion typically works best on binary masks. Consider calling
    /// [`threshold`](MatteHandle::threshold) before `erode` if working with a soft matte.
    pub fn erode_with_border_mode(mut self, radius: f32, border_mode: ErosionBorderMode) -> Self {
        self.operations.push(MaskOperation::Erode {
            radius,
            border_mode,
        });
        self
    }

    /// Add a hole-filling operation to the processing pipeline.
    ///
    /// **Note**: Hole-filling typically works best on binary masks. Consider calling
    /// [`threshold`](MatteHandle::threshold) before `fill_holes` if working with a soft matte.
    pub fn fill_holes(self) -> Self {
        let threshold = self.mask_processing_defaults.mask_threshold;
        self.fill_holes_with(threshold)
    }

    /// Add a hole-filling operation with a custom threshold.
    ///
    /// **Note**: Hole-filling typically works best on binary masks. Consider calling
    /// [`threshold`](MatteHandle::threshold) before `fill_holes` if working with a soft matte.
    pub fn fill_holes_with(mut self, threshold: u8) -> Self {
        self.operations.push(MaskOperation::FillHoles { threshold });
        self
    }

    /// Process the raw matte with the accumulated operations.
    pub fn processed(self) -> OutlineResult<MaskHandle> {
        self.process_with_pipeline(None)
    }

    /// Process the raw matte with the accumulated operations and a custom pipeline.
    pub fn processed_with(self, pipeline: &MaskPipeline) -> OutlineResult<MaskHandle> {
        self.process_with_pipeline(Some(pipeline))
    }

    fn process_with_pipeline(
        mut self,
        pipeline: Option<&MaskPipeline>,
    ) -> OutlineResult<MaskHandle> {
        let mut ops = std::mem::take(&mut self.operations);
        if let Some(custom) = pipeline {
            ops.extend_from_slice(custom.operations());
        }

        let mask = apply_operations(self.raw_matte.as_ref(), &ops);
        Ok(MaskHandle::new(
            Arc::clone(&self.rgb_image),
            mask,
            self.mask_processing_defaults,
        ))
    }

    /// Compose the RGBA foreground image from the RGB image and the current matte.
    pub fn foreground(&self) -> OutlineResult<ForegroundHandle> {
        let mask = self.resolved_matte();
        let rgba = compose_foreground(self.rgb_image.as_ref(), &mask)?;
        Ok(ForegroundHandle::new(rgba))
    }

    /// Colorize the current matte into a flat-color RGBA image.
    pub fn colorize(&self, color: impl Into<MaskColor>) -> RgbaImage {
        let mask = self.resolved_matte();
        colorize_mask(&mask, color)
    }

    /// Trace the current matte using the specified vectorizer and options.
    pub fn trace<V>(&self, vectorizer: &V, options: &V::Options) -> OutlineResult<V::Output>
    where
        V: MaskVectorizer,
    {
        let mask = self.resolved_matte();
        vectorizer.vectorize(&mask, options)
    }

    /// Expand the matte canvas by the given padding while keeping content at the same offset.
    ///
    /// Any pending matte operations are applied before padding so method call order is preserved.
    /// The underlying RGB canvas is padded with black to keep [`foreground`](MatteHandle::foreground)
    /// aligned with the matte.
    pub fn pad(self, padding: impl Into<Padding>) -> Self {
        let this = self.resolve_pending_operations();
        let padding = padding.into();
        let matte = Arc::new(pad_gray_image(this.raw_matte.as_ref(), padding, 0));
        let rgb = Arc::new(pad_rgb_image(this.rgb_image.as_ref(), padding, [0, 0, 0]));
        Self {
            rgb_image: rgb,
            raw_matte: matte,
            mask_processing_defaults: this.mask_processing_defaults,
            operations: Vec::new(),
        }
    }

    /// Crop the matte and source RGB image to `bounds`.
    ///
    /// Returns `None` when `bounds` is empty or outside the current matte canvas.
    pub fn crop(self, bounds: BoundingBox) -> Option<Self> {
        let this = self.resolve_pending_operations();
        if !crop_bounds_fit_image(bounds, this.raw_matte.width(), this.raw_matte.height()) {
            return None;
        }

        let matte = Arc::new(crop_gray_image(this.raw_matte.as_ref(), bounds));
        let rgb = Arc::new(crop_rgb_image(this.rgb_image.as_ref(), bounds));
        Some(Self {
            rgb_image: rgb,
            raw_matte: matte,
            mask_processing_defaults: this.mask_processing_defaults,
            operations: Vec::new(),
        })
    }

    /// Crop the matte and source RGB image to the smallest non-zero content box.
    ///
    /// Returns `None` when the current matte has no non-zero pixels.
    pub fn crop_to_content(self) -> Option<Self> {
        self.crop_to_content_with(1)
    }

    /// Crop the matte and source RGB image to the smallest content box at or above `threshold`.
    ///
    /// Returns `None` when the current matte has no pixels at or above `threshold`.
    pub fn crop_to_content_with(self, threshold: u8) -> Option<Self> {
        let this = self.resolve_pending_operations();
        let bounds = mask_bounding_box(this.raw_matte.as_ref(), threshold)?;
        this.crop(bounds)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use image::{Luma, Rgb};

    fn matte_handle() -> MatteHandle {
        MatteHandle {
            rgb_image: Arc::new(RgbImage::from_pixel(1, 1, Rgb([255, 255, 255]))),
            raw_matte: Arc::new(GrayImage::from_pixel(1, 1, Luma([255]))),
            mask_processing_defaults: MaskProcessingDefaults::default(),
            operations: Vec::new(),
        }
    }

    fn single_pixel_matte_handle() -> MatteHandle {
        MatteHandle {
            rgb_image: Arc::new(RgbImage::from_pixel(5, 5, Rgb([10, 20, 30]))),
            raw_matte: Arc::new(GrayImage::from_fn(5, 5, |x, y| {
                if x == 2 && y == 2 {
                    Luma([255])
                } else {
                    Luma([0])
                }
            })),
            mask_processing_defaults: MaskProcessingDefaults::default(),
            operations: Vec::new(),
        }
    }

    fn matte_handle_with_images(rgb_image: RgbImage, raw_matte: GrayImage) -> MatteHandle {
        MatteHandle {
            rgb_image: Arc::new(rgb_image),
            raw_matte: Arc::new(raw_matte),
            mask_processing_defaults: MaskProcessingDefaults::default(),
            operations: Vec::new(),
        }
    }

    struct BoundingBoxVectorizer;

    impl MaskVectorizer for BoundingBoxVectorizer {
        type Options = ();
        type Output = Option<BoundingBox>;

        fn vectorize(
            &self,
            mask: &GrayImage,
            _options: &Self::Options,
        ) -> OutlineResult<Self::Output> {
            Ok(mask_bounding_box(mask, 1))
        }
    }

    #[test]
    fn matte_handle_dimensions_reports_current_canvas() {
        let cropped = single_pixel_matte_handle()
            .crop(BoundingBox::new(1, 1, 2, 3))
            .expect("bounds are inside the image");
        assert_eq!(cropped.dimensions(), (2, 3));

        let padded = cropped.pad(Padding::new(1, 2, 3, 4));
        assert_eq!(padded.dimensions(), (6, 9));
    }

    #[test]
    fn matte_handle_raw_accessors_return_original_matte() {
        let handle = single_pixel_matte_handle().dilate_with(1.0);

        assert_eq!(handle.as_raw_matte().dimensions(), (5, 5));
        assert_eq!(handle.as_raw_matte().get_pixel(2, 2).0[0], 255);
        assert_eq!(handle.as_raw_matte().get_pixel(1, 2).0[0], 0);

        let cloned = handle.to_raw_matte();
        assert_eq!(cloned.as_raw(), handle.as_raw_matte().as_raw());
    }

    #[test]
    fn matte_handle_erode_uses_default_radius() {
        let handle = matte_handle().erode();
        assert!(matches!(
            handle.operations.as_slice(),
            [MaskOperation::Erode { radius, border_mode }]
                if (*radius - MaskProcessingDefaults::default().erosion_radius).abs() < f32::EPSILON
                    && *border_mode == MaskProcessingDefaults::default().erosion_border_mode
        ));
    }

    #[test]
    fn matte_handle_erode_with_uses_custom_radius() {
        let handle = matte_handle().erode_with(2.5);
        assert!(matches!(
            handle.operations.as_slice(),
            [MaskOperation::Erode { radius, border_mode }]
                if (*radius - 2.5).abs() < f32::EPSILON
                    && *border_mode == MaskProcessingDefaults::default().erosion_border_mode
        ));
    }

    #[test]
    fn matte_handle_erode_with_border_mode_uses_custom_mode() {
        let handle =
            matte_handle().erode_with_border_mode(2.5, ErosionBorderMode::OutsideIsUnknown);
        assert!(matches!(
            handle.operations.as_slice(),
            [MaskOperation::Erode { radius, border_mode }]
                if (*radius - 2.5).abs() < f32::EPSILON
                    && *border_mode == ErosionBorderMode::OutsideIsUnknown
        ));
    }

    #[test]
    fn matte_handle_fill_holes_with_uses_custom_threshold() {
        let handle = matte_handle().fill_holes_with(180);
        assert!(matches!(
            handle.operations.as_slice(),
            [MaskOperation::FillHoles { threshold: 180 }]
        ));
    }

    #[test]
    fn matte_handle_chain_and_pipeline_are_equivalent() {
        let pipeline = MaskPipeline::new()
            .threshold_with(128)
            .dilate_with(1.0)
            .erode_with(1.0);

        let chained = single_pixel_matte_handle()
            .threshold_with(128)
            .dilate_with(1.0)
            .erode_with(1.0)
            .processed()
            .expect("chain should process")
            .into_image();
        let piped = single_pixel_matte_handle()
            .processed_with(&pipeline)
            .expect("pipeline should process")
            .into_image();

        assert_eq!(chained.as_raw(), piped.as_raw());
    }

    #[test]
    fn matte_handle_processed_without_chained_operations_is_identity() {
        let source = single_pixel_matte_handle().into_image();
        let handle = MatteHandle {
            rgb_image: Arc::new(RgbImage::from_pixel(5, 5, Rgb([10, 20, 30]))),
            raw_matte: Arc::new(source.clone()),
            mask_processing_defaults: MaskProcessingDefaults::default(),
            operations: Vec::new(),
        };

        let mask = handle
            .processed()
            .expect("empty processing should succeed")
            .into_image();

        assert_eq!(mask.as_raw(), source.as_raw());
    }

    #[test]
    fn matte_handle_bounding_box_applies_pending_operations() {
        let bounds = single_pixel_matte_handle()
            .dilate_with(1.0)
            .bounding_box_with(1)
            .expect("expected bounding box");

        assert_eq!(bounds, BoundingBox::new(1, 1, 3, 3));
    }

    #[test]
    fn matte_handle_bounding_box_uses_non_zero_threshold() {
        let bounds = single_pixel_matte_handle()
            .bounding_box()
            .expect("expected bounding box");

        assert_eq!(bounds, BoundingBox::new(2, 2, 1, 1));
    }

    #[test]
    fn matte_handle_pad_updates_matte_and_foreground_canvas() {
        let rgb = RgbImage::from_pixel(2, 2, Rgb([10, 20, 30]));
        let mut matte = GrayImage::from_pixel(2, 2, Luma([0]));
        matte.put_pixel(1, 1, Luma([255]));

        let padded = matte_handle_with_images(rgb, matte).pad(Padding::new(1, 2, 3, 4));

        assert_eq!(padded.dimensions(), (6, 8));
        let foreground = padded.foreground().expect("foreground should compose");
        assert_eq!(foreground.dimensions(), (6, 8));
        assert_eq!(foreground.image().get_pixel(2, 3)[3], 255);
    }

    #[test]
    fn matte_handle_pad_applies_pending_operations_first() {
        let padded = single_pixel_matte_handle()
            .dilate_with(1.0)
            .pad(Padding::new(1, 2, 0, 0));

        assert_eq!(
            mask_bounding_box(padded.as_raw_matte(), 1),
            Some(BoundingBox::new(2, 3, 3, 3))
        );
    }

    #[test]
    fn matte_handle_crop_to_content_crops_matte_and_rgb_together() {
        let rgb = RgbImage::from_fn(4, 4, |x, y| Rgb([x as u8, y as u8, 0]));
        let mut matte = GrayImage::from_pixel(4, 4, Luma([0]));
        matte.put_pixel(2, 1, Luma([255]));
        matte.put_pixel(2, 2, Luma([255]));

        let cropped = matte_handle_with_images(rgb, matte)
            .crop_to_content()
            .expect("matte has content");

        assert_eq!(cropped.dimensions(), (1, 2));
        let foreground = cropped.foreground().expect("foreground should compose");
        assert_eq!(foreground.dimensions(), (1, 2));
        assert_eq!(foreground.image().get_pixel(0, 0)[0], 2);
        assert_eq!(foreground.image().get_pixel(0, 0)[1], 1);
        assert_eq!(foreground.image().get_pixel(0, 0)[3], 255);
    }

    #[test]
    fn matte_handle_crop_uses_requested_bounds() {
        let rgb = RgbImage::from_fn(4, 4, |x, y| Rgb([x as u8, y as u8, 0]));
        let mut matte = GrayImage::from_pixel(4, 4, Luma([0]));
        matte.put_pixel(2, 1, Luma([255]));

        let cropped = matte_handle_with_images(rgb, matte)
            .crop(BoundingBox::new(1, 1, 2, 2))
            .expect("bounds are inside the image");

        assert_eq!(cropped.dimensions(), (2, 2));
        assert_eq!(cropped.as_raw_matte().get_pixel(1, 0)[0], 255);
        let foreground = cropped.foreground().expect("foreground should compose");
        assert_eq!(foreground.image().get_pixel(1, 0)[0], 2);
        assert_eq!(foreground.image().get_pixel(1, 0)[1], 1);
        assert_eq!(foreground.image().get_pixel(1, 0)[3], 255);
    }

    #[test]
    fn matte_handle_crop_rejects_invalid_bounds() {
        assert!(
            single_pixel_matte_handle()
                .crop(BoundingBox::new(4, 4, 2, 2))
                .is_none()
        );
        assert!(
            single_pixel_matte_handle()
                .crop(BoundingBox::new(0, 0, 0, 1))
                .is_none()
        );
    }

    #[test]
    fn matte_handle_crop_to_content_with_ignores_low_values() {
        let rgb = RgbImage::from_fn(5, 3, |x, y| Rgb([x as u8, y as u8, 0]));
        let mut matte = GrayImage::from_pixel(5, 3, Luma([0]));
        matte.put_pixel(1, 1, Luma([32]));
        matte.put_pixel(3, 1, Luma([200]));

        let cropped = matte_handle_with_images(rgb, matte)
            .crop_to_content_with(128)
            .expect("matte has content above threshold");

        assert_eq!(cropped.dimensions(), (1, 1));
        assert_eq!(cropped.as_raw_matte().get_pixel(0, 0)[0], 200);
        let foreground = cropped.foreground().expect("foreground should compose");
        assert_eq!(foreground.image().get_pixel(0, 0)[0], 3);
        assert_eq!(foreground.image().get_pixel(0, 0)[1], 1);
        assert_eq!(foreground.image().get_pixel(0, 0)[3], 200);
    }

    #[test]
    fn matte_handle_into_image_applies_pending_operations() {
        let mask = single_pixel_matte_handle().dilate_with(1.0).into_image();

        assert_eq!(
            mask_bounding_box(&mask, 1),
            Some(BoundingBox::new(1, 1, 3, 3))
        );
    }

    #[test]
    fn matte_handle_save_applies_pending_operations() {
        let temp_dir = tempfile::tempdir().expect("temp dir should be created");
        let path = temp_dir.path().join("matte.png");

        single_pixel_matte_handle()
            .dilate_with(1.0)
            .save(&path)
            .expect("matte should save");

        let saved = image::open(&path)
            .expect("saved matte should load")
            .to_luma8();

        assert_eq!(
            mask_bounding_box(&saved, 1),
            Some(BoundingBox::new(1, 1, 3, 3))
        );
    }

    #[test]
    fn matte_handle_foreground_applies_pending_operations() {
        let foreground = single_pixel_matte_handle()
            .dilate_with(1.0)
            .foreground()
            .expect("foreground should compose");

        assert_eq!(foreground.image().get_pixel(1, 2)[3], 255);
        assert_eq!(foreground.image().get_pixel(0, 0)[3], 0);
    }

    #[test]
    fn matte_handle_colorize_applies_pending_operations() {
        let colorized = single_pixel_matte_handle()
            .dilate_with(1.0)
            .colorize([0, 180, 255, 255]);

        assert_eq!(colorized.get_pixel(1, 2).0, [0, 180, 255, 255]);
        assert_eq!(colorized.get_pixel(0, 0).0, [0, 180, 255, 0]);
    }

    #[test]
    fn matte_handle_trace_applies_pending_operations() {
        let bounds = single_pixel_matte_handle()
            .dilate_with(1.0)
            .trace(&BoundingBoxVectorizer, &())
            .expect("trace should run");

        assert_eq!(bounds, Some(BoundingBox::new(1, 1, 3, 3)));
    }
}