use clap::Parser;
use derive_builder::Builder;
use fast_image_resize::{FilterType, ResizeAlg};
use serde::Deserialize;
#[derive(Debug, Clone, Deserialize, Parser, Builder)]
#[builder(pattern = "owned")]
pub struct ResizeConfig {
#[clap(name = "resize-images-filter-type", long, default_value = "lanczos3")]
pub filter_type: String,
#[clap(
name = "resize-images-algorithm",
long,
default_value = "interpolation"
)]
pub algorithm: String,
#[clap(name = "resize-images-cache-size", long, default_value_t = 200)]
pub cache_size: usize,
}
impl ResizeConfig {
pub fn builder() -> ResizeConfigBuilder {
ResizeConfigBuilder {
filter_type: Some("lanczos3".into()),
algorithm: Some("interpolation".into()),
cache_size: Some(200),
}
}
pub fn resize_algorithm(&self) -> ResizeAlg {
let filter_type = match self.filter_type.as_str() {
"lanczos3" => FilterType::Lanczos3,
"gaussian" => FilterType::Gaussian,
"catmull-rom" => FilterType::CatmullRom,
"hamming" => FilterType::Hamming,
"mitchell" => FilterType::Mitchell,
"bilinear" => FilterType::Bilinear,
"box" => FilterType::Box,
_ => panic!("Unsupported filter type"),
};
match self.algorithm.as_str() {
"super-sampling8x" => ResizeAlg::SuperSampling(filter_type, 8),
"super-sampling4x" => ResizeAlg::SuperSampling(filter_type, 4),
"super-sampling2x" => ResizeAlg::SuperSampling(filter_type, 2),
"interpolation" => ResizeAlg::Interpolation(filter_type),
"convolution" => ResizeAlg::Convolution(filter_type),
"nearest" => ResizeAlg::Nearest,
_ => panic!("Unsupported resize algorithm"),
}
}
}