From c11480760aa85b19910340d64b0deef5e57f3fd0 Mon Sep 17 00:00:00 2001 From: Louis Capitanchik <contact@louiscap.co> Date: Tue, 21 Feb 2023 17:47:51 +0000 Subject: [PATCH] Stub out reduce operation --- src/cli_args.rs | 18 +++++++++++++--- src/commands/mod.rs | 2 ++ src/commands/reduce.rs | 47 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 src/commands/reduce.rs diff --git a/src/cli_args.rs b/src/cli_args.rs index 717b77c..2d8923f 100644 --- a/src/cli_args.rs +++ b/src/cli_args.rs @@ -2,7 +2,7 @@ use clap::Parser; use serde::{Deserialize, Serialize}; // use crate::commands::{calculate_mapping, execute_pipeline, extrude, flip, palette, remap_image, rescale, rotate, write_palette, FlipDirection, RotateDegree, Rotate}; -use crate::commands::{Extrude, Flip, Palette, Pipeline, Remap, Rotate, Scale}; +use crate::commands::{Extrude, Flip, Palette, Pipeline, Reduce, Remap, Rotate, Scale}; use crate::load_image; @@ -33,9 +33,12 @@ pub enum Args { #[clap(name = "remap")] #[serde(alias = "remap")] Remap(Remap), - #[clap(name = "remap")] - #[serde(alias = "remap")] + #[clap(name = "pipeline")] + #[serde(alias = "pipeline")] Pipeline(Pipeline), + #[clap(name = "reduce")] + #[serde(alias = "reduce")] + Reduce(Reduce), } impl Args { @@ -85,6 +88,15 @@ impl Args { output.save(&remap.output).map_err(anyhow::Error::from) } + Args::Reduce(reduce) => { + if let Some(amount) = reduce.colours { + log::info!("Num cols {}", amount); + } else { + log::info!("No Limit"); + } + + Ok(()) + } Args::Pipeline(pipeline) => pipeline.execute(), } } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index f547598..b9cf997 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -5,6 +5,7 @@ mod pipeline; mod remap; mod rotate; mod scale; +mod reduce; pub use extrude::Extrude; pub use flip::Flip; @@ -13,3 +14,4 @@ pub use pipeline::Pipeline; pub use remap::Remap; pub use rotate::Rotate; pub use scale::Scale; +pub use reduce::Reduce; diff --git a/src/commands/reduce.rs b/src/commands/reduce.rs new file mode 100644 index 0000000..44f739d --- /dev/null +++ b/src/commands/reduce.rs @@ -0,0 +1,47 @@ +use crate::commands::palette::ColourMapping; +use crate::commands::Palette; +use crate::utils::{RgbaOutputFormat, TypedOutputFormat}; +use anyhow::Error; +use clap::Parser; +use image::{GenericImage, ImageBuffer, Rgba, RgbaImage}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; + +/// Limit the number of colours by quantity or threshold +#[derive(Debug, Clone, Parser, Serialize, Deserialize)] +#[clap(author, version = "0.3.0")] +pub struct Reduce { + /// The path to the image file + #[serde(default)] + pub input: String, + /// The path to write the recoloured image + #[serde(default)] + pub output: String, + + /// The number of colours to use for quantizing + #[clap(short, long)] + pub colours: Option<u32>, + /// The colour difference threshold to limit colour reduction + #[clap(short, long)] + pub threshold: Option<f32>, +} + +impl Reduce { + pub fn run<T: GenericImage>(&self, mut image: &T) -> anyhow::Result<RgbaOutputFormat> { + Err(Error::msg("Reduce is not implemented")) + + // TODO: Reduce impl + // let mut palette = Palette::extract_from(image)?; + // + // let similarity = HashMap::with_capacity(palette.len()); + // let mapping: ColourMapping = ColourMapping::new(); + // + // while !palette.is_empty() { + // let next = palette.pop(); + // } + // + // Ok(RgbaImage::from_fn(image.width(), image.height(), |x, y| { + // Rgba::from([0, 0, 0, 0]) + // })) + } +} -- GitLab