Skip to main content

outline/
config.rs

1use std::path::PathBuf;
2
3use image::imageops::FilterType;
4
5/// Environment variable name for specifying the model path.
6pub const ENV_MODEL_PATH: &str = "OUTLINE_MODEL_PATH";
7
8/// Default model path used when no explicit path is provided.
9///
10/// This is the fallback when neither `--model` nor `OUTLINE_MODEL_PATH` is set.
11/// By default it points to `model.onnx` in the current working directory.
12pub const DEFAULT_MODEL_PATH: &str = "model.onnx";
13
14/// Configuration for ONNX model inference and image preprocessing.
15///
16/// Controls the model path, image resize filters for input/output, and threading behavior.
17/// Use builder methods like [`with_input_resize_filter`](InferenceSettings::with_input_resize_filter)
18/// to customize settings.
19#[derive(Debug, Clone)]
20pub struct InferenceSettings {
21    /// Path to the ONNX model file.
22    pub model_path: PathBuf,
23    /// Filter to use when resizing the input image for the model.
24    pub input_resize_filter: FilterType,
25    /// Filter to use when resizing the output matte to the original image size.
26    pub output_resize_filter: FilterType,
27    /// Number of intra-op threads for the inference.
28    pub intra_threads: Option<usize>,
29}
30
31impl InferenceSettings {
32    /// Create new inference settings with default values.
33    pub fn new(model_path: impl Into<PathBuf>) -> Self {
34        Self {
35            model_path: model_path.into(),
36            input_resize_filter: FilterType::Triangle,
37            output_resize_filter: FilterType::Lanczos3,
38            intra_threads: None,
39        }
40    }
41
42    /// Set the model resize filter.
43    pub fn with_input_resize_filter(mut self, filter: FilterType) -> Self {
44        self.input_resize_filter = filter;
45        self
46    }
47
48    /// Set the matte resize filter.
49    pub fn with_output_resize_filter(mut self, filter: FilterType) -> Self {
50        self.output_resize_filter = filter;
51        self
52    }
53
54    /// Set the number of intra-op threads for the inference.
55    pub fn with_intra_threads(mut self, intra_threads: Option<usize>) -> Self {
56        self.intra_threads = intra_threads;
57        self
58    }
59}
60
61/// Configuration for mask post-processing operations.
62///
63/// Defines the pipeline of blur, threshold, dilation, and hole-filling operations applied
64/// to raw mattes. Used as defaults in [`Outline`](crate::Outline) and can be overridden
65/// per operation via [`MatteHandle`](crate::MatteHandle) and [`MaskHandle`](crate::MaskHandle).
66///
67/// # Explicit Configuration
68///
69/// This struct does **not** apply automatic logic. For example, setting `dilate = true` or
70/// `fill_holes = true` will **not** automatically enable `binary`. If you need a binary mask
71/// for dilation or hole-filling to work meaningfully, you must explicitly set `binary = true`
72/// or call [`threshold`](crate::MatteHandle::threshold) in your processing chain.
73///
74/// **Note**: The CLI's `--binary auto` mode *does* automatically enable thresholding when
75/// `--dilate` or `--fill-holes` is specified. The library leaves this decision to you for
76/// maximum control and predictability.
77#[derive(Debug, Clone, PartialEq)]
78pub struct MaskProcessingOptions {
79    /// Whether to apply binary thresholding to the mask.
80    pub binary: bool,
81    /// Whether to apply Gaussian blur to the mask.
82    pub blur: bool,
83    /// Standard deviation (sigma) for Gaussian blur.
84    pub blur_sigma: f32,
85    /// Threshold value (0–255) used for binary conversion and hole-filling.
86    pub mask_threshold: u8,
87    /// Whether to dilate the mask using a Euclidean distance transform.
88    pub dilate: bool,
89    /// Radius in pixels for the dilation operation.
90    pub dilation_radius: f32,
91    /// Whether to fill interior holes in the binary mask.
92    pub fill_holes: bool,
93}
94
95impl Default for MaskProcessingOptions {
96    fn default() -> Self {
97        Self {
98            binary: false,
99            blur: false,
100            blur_sigma: 6.0,
101            mask_threshold: 120,
102            dilate: false,
103            dilation_radius: 5.0,
104            fill_holes: false,
105        }
106    }
107}