use clap::{Parser, ValueEnum}; use image::{imageops, GenericImage, Pixel}; use serde::{Deserialize, Serialize}; use crunch_cli::utils::TypedOutputFormat; #[derive(Copy, Clone, Serialize, Deserialize, ValueEnum, Debug)] pub enum FlipDirection { #[serde(rename = "vertical")] Vertical, #[serde(rename = "horizontal")] Horizontal, #[serde(rename = "both")] Both, } /// Flip an image along one or more axis #[derive(Debug, Clone, Parser, Serialize, Deserialize)] #[clap(author, version = "0.3.0")] pub struct Flip { /// The path to the image file #[serde(default)] pub input: String, /// The path to write the flipped image #[serde(default)] pub output: String, /// The axis along which the image should be flipped #[clap(short, long, value_enum)] direction: FlipDirection, } impl Flip { pub fn run<T: GenericImage>(&self, image: &T) -> anyhow::Result<TypedOutputFormat<T>> where T::Pixel: 'static, <T::Pixel as Pixel>::Subpixel: 'static, { use FlipDirection::*; match self.direction { Vertical => Ok(imageops::flip_vertical(image)), Horizontal => Ok(imageops::flip_horizontal(image)), Both => { let mut image = imageops::flip_horizontal(image); imageops::flip_vertical_in_place(&mut image); Ok(image) } } } }