Skip to main content

image_provider/
config.rs

1use clap::Parser;
2use derive_builder::Builder;
3use fast_image_resize::{FilterType, ResizeAlg};
4use serde::Deserialize;
5
6#[derive(Debug, Clone, Deserialize, Parser, Builder)]
7#[builder(pattern = "owned")]
8pub struct ResizeConfig {
9    /// Filter type to use for resizing
10    /// `lanczos3`  
11    /// `gaussian`  
12    /// `catmull-rom`  
13    /// `hamming`  
14    /// `mitchell`  
15    /// `bilinear`  
16    /// `box`  
17    #[clap(name = "resize-images-filter-type", long, default_value = "lanczos3")]
18    pub filter_type: String,
19
20    /// Resize algorithm to use
21    /// `super-sampling8x`  
22    /// `super-sampling4x`  
23    /// `super-sampling2x`  
24    /// `convolution`  
25    /// `interpolation`  
26    /// `nearest`  
27    /// (nearest will ignore filter_type)
28    #[clap(
29        name = "resize-images-algorithm",
30        long,
31        default_value = "interpolation"
32    )]
33    pub algorithm: String,
34
35    /// Maximum cached images in memory
36    #[clap(name = "resize-images-cache-size", long, default_value_t = 200)]
37    pub cache_size: usize,
38}
39
40impl ResizeConfig {
41    pub fn builder() -> ResizeConfigBuilder {
42        ResizeConfigBuilder {
43            filter_type: Some("lanczos3".into()),
44            algorithm: Some("interpolation".into()),
45            cache_size: Some(200),
46        }
47    }
48
49    pub fn resize_algorithm(&self) -> ResizeAlg {
50        let filter_type = match self.filter_type.as_str() {
51            "lanczos3" => FilterType::Lanczos3,
52            "gaussian" => FilterType::Gaussian,
53            "catmull-rom" => FilterType::CatmullRom,
54            "hamming" => FilterType::Hamming,
55            "mitchell" => FilterType::Mitchell,
56            "bilinear" => FilterType::Bilinear,
57            "box" => FilterType::Box,
58            _ => panic!("Unsupported filter type"),
59        };
60
61        match self.algorithm.as_str() {
62            "super-sampling8x" => ResizeAlg::SuperSampling(filter_type, 8),
63            "super-sampling4x" => ResizeAlg::SuperSampling(filter_type, 4),
64            "super-sampling2x" => ResizeAlg::SuperSampling(filter_type, 2),
65            "interpolation" => ResizeAlg::Interpolation(filter_type),
66            "convolution" => ResizeAlg::Convolution(filter_type),
67            "nearest" => ResizeAlg::Nearest,
68            _ => panic!("Unsupported resize algorithm"),
69        }
70    }
71}