Skip to content
Snippets Groups Projects
flip.rs 1.23 KiB
Newer Older
use clap::{Parser, ValueEnum};
Louis's avatar
Louis committed
use image::{imageops, GenericImage, Pixel};
Louis's avatar
Louis committed
use serde::{Deserialize, Serialize};
Louis's avatar
Louis committed
use crunch_cli::utils::TypedOutputFormat;
Louis's avatar
Louis committed

#[derive(Copy, Clone, Serialize, Deserialize, ValueEnum, Debug)]
Louis's avatar
Louis committed
pub enum FlipDirection {
	#[serde(rename = "vertical")]
	Vertical,
	#[serde(rename = "horizontal")]
	Horizontal,
	#[serde(rename = "both")]
	Both,
}

Louis's avatar
Louis committed
/// Flip an image along one or more axis
#[derive(Debug, Clone, Parser, Serialize, Deserialize)]
#[clap(author, version = "0.3.0")]
pub struct Flip {
Louis's avatar
Louis committed
	/// The path to the image file
	#[serde(default)]
	pub input: String,
Louis's avatar
Louis committed
	/// 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)
			}
Louis's avatar
Louis committed
		}
	}
}