Skip to content
Snippets Groups Projects
rotate.rs 1.13 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 RotateDegree {
	#[serde(rename = "90")]
	One,
	#[serde(rename = "180")]
	Two,
	#[serde(rename = "270")]
	Three,
}

/// Rotate an image clockwise by the given degree
#[derive(Debug, Clone, Parser, Serialize, Deserialize)]
#[clap(author, version = "0.3.0")]
pub struct Rotate {
	/// The path to the spritesheet file
	#[serde(default)]
	pub input: String,
	/// The path to write the extruded spritesheet
	#[serde(default)]
	pub output: String,

	/// How many 90 degree steps should this image be rotated by
	#[clap(short, long, value_enum)]
	pub amount: RotateDegree,
}

impl Rotate {
	pub fn run<T: GenericImage>(&self, image: &T) -> anyhow::Result<TypedOutputFormat<T>>
	where
		T::Pixel: 'static,
		<T::Pixel as Pixel>::Subpixel: 'static,
	{
		use RotateDegree::*;
		match self.amount {
			One => Ok(imageops::rotate90(image)),
			Two => Ok(imageops::rotate180(image)),
			Three => Ok(imageops::rotate270(image)),
		}
Louis's avatar
Louis committed
	}
}