diff --git a/.gitignore b/.gitignore index 673431564e4776f2ddc92de759bf02c814cb8b8c..fa2ce9d32644b8b224302f2d6c83e7cdb21d2871 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target .idea/ -exmp/ \ No newline at end of file +exmp/ +/*.png \ No newline at end of file diff --git a/src/cli_args.rs b/src/cli_args.rs index 8e8090dbd8970405105dcb0aa3dcd3a807743aaa..af4ea5db2ea4c80e444dee818c7fc8b7beba098e 100644 --- a/src/cli_args.rs +++ b/src/cli_args.rs @@ -2,7 +2,10 @@ use clap::Parser; use image::ImageFormat; use serde::{Deserialize, Serialize}; -use crate::commands::{Atlas, Extract, Extrude, Flip, Info, Palette, Pipeline, Reduce, Remap, Rotate, Scale, Split, Swap}; +use crate::commands::{ + Atlas, Extract, Extrude, Flip, Info, Palette, Pipeline, Reduce, Remap, Rotate, Scale, Split, + Swap, +}; use crate::load_image; @@ -51,8 +54,8 @@ pub enum Args { #[clap(name = "info")] #[serde(alias = "info")] Info(Info), - #[clap(name = "info")] - #[serde(alias = "info")] + #[clap(name = "swap")] + #[serde(alias = "swap")] Swap(Swap), } @@ -109,11 +112,11 @@ impl Args { let image_data = load_image(&swap.input, None)?; let palette_data = load_image(&swap.palette, None)?; let palette_swap = Palette::from_columns(&palette_data)?; - let output = Remap::remap_image(image_data, palette_swap)?; + let output = Swap::swap_pixels(image_data, palette_swap)?; output .save_with_format(&swap.output, ImageFormat::Png) .map_err(anyhow::Error::from) - }, + } Args::Reduce(reduce) => { if let Some(amount) = reduce.colours { log::info!("Num cols {}", amount); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 7a2d578e2c22fd1fbd6cbfc2fc5bf84a3a819168..0a1ea3c85f87097e311a24de80d78c2affbe3457 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -24,4 +24,4 @@ pub use remap::Remap; pub use rotate::Rotate; pub use scale::Scale; pub use split::Split; -pub use swap::Swap; \ No newline at end of file +pub use swap::Swap; diff --git a/src/commands/palette.rs b/src/commands/palette.rs index 2778712b38c7587968175a77b20e3357637efa66..f74461cb0197be541239dc2c682ac9d9015efd33 100644 --- a/src/commands/palette.rs +++ b/src/commands/palette.rs @@ -3,12 +3,12 @@ use std::cmp::{min, Ordering}; use std::collections::hash_map::RandomState; use std::collections::{HashMap, HashSet}; -use std::io::Write; use anyhow::Error; use deltae::{Delta, LabValue, DE2000}; use image::{GenericImage, Pixel, Rgba}; use num_traits::ToPrimitive; use serde::{Deserialize, Serialize}; +use std::io::Write; use crate::format::PaletteFormat; use crate::utils::{new_image, BasicRgba}; @@ -37,7 +37,7 @@ pub fn sort_by_hue(palette: &mut PixelPalette) { /// Create a palette file containing every distinct colour from the input image #[derive(Parser, Clone, Serialize, Deserialize, Debug)] -#[clap(author, version = "0.3.0")] +#[clap(author, version = "0.9.0")] pub struct Palette { /// The path to the image file #[serde(default)] @@ -98,6 +98,18 @@ impl Palette { out_image.save(self.output.as_str())?; } + PaletteFormat::Columns => { + let image_width = 2; + let image_height = colours.len(); + let mut out_image = new_image(image_width as u32, image_height as u32); + + for (idx, colour) in colours.iter().enumerate() { + out_image.put_pixel(0, idx as u32, Rgba::from(colour)); + out_image.put_pixel(1, idx as u32, Rgba::from(colour)); + } + + out_image.save(self.output.as_str())?; + } PaletteFormat::Txt => { let mut file = std::fs::File::create(self.output.as_str())?; for colour in colours.iter() { @@ -129,10 +141,10 @@ impl Palette { ]); let left_pixel = BasicRgba::from(data); let data = Rgba::from([ - left.0[0].to_u8().unwrap(), - left.0[1].to_u8().unwrap(), - left.0[2].to_u8().unwrap(), - left.0[3].to_u8().unwrap(), + right.0[0].to_u8().unwrap(), + right.0[1].to_u8().unwrap(), + right.0[2].to_u8().unwrap(), + right.0[3].to_u8().unwrap(), ]); let right_pixel = BasicRgba::from(data); mapping.insert(left_pixel, right_pixel); diff --git a/src/format.rs b/src/format.rs index eb70c53baf50de3efb046f2ccc43b33e37e40c27..9d359245cb07c2097d67f375887f03241a28d074 100644 --- a/src/format.rs +++ b/src/format.rs @@ -15,6 +15,8 @@ pub enum PaletteFormat { #[default] #[serde(alias = "png")] Png, + #[serde(alias = "columns")] + Columns, } #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]