use clap::Parser; use image::imageops::FilterType; use image::{imageops, GenericImage, Pixel}; use serde::{Deserialize, Serialize}; use crunch_cli::utils::TypedOutputFormat; #[inline(always)] fn one() -> f32 { 1.0 } /// Resize an image by a scale factor #[derive(Debug, Clone, Parser, Serialize, Deserialize)] #[clap(author, version = "0.3.0")] pub struct Scale { /// The path to the image file #[serde(default)] pub input: String, /// The path to write the scaled image #[serde(default)] pub output: String, /// The scale factor to use; numbers between 0-1 shrink the image; numbers > 1 enlarge #[clap(short, long, default_value_t = 1.0)] #[serde(default = "one")] factor: f32, } impl Scale { pub fn run<T: GenericImage>(&self, image: &T) -> anyhow::Result<TypedOutputFormat<T>> where T::Pixel: 'static, <T::Pixel as Pixel>::Subpixel: 'static, { let width = (image.width() as f32 * self.factor) as u32; let height = (image.height() as f32 * self.factor) as u32; Ok(imageops::resize(image, width, height, FilterType::Nearest)) } }