Skip to content

Commit

Permalink
refactor: name ColorTypeExt as ColorSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
Daru-san committed Dec 14, 2024
1 parent 5910b5b commit f6ac36a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
11 changes: 7 additions & 4 deletions src/app/command/recolor.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
use crate::image::color::{BitDepth, ColorInfo, ColorTypeExt};
use crate::image::color::{BitDepth, ColorInfo, ColorSpace};

use anyhow::Result;

use clap::Parser;
use image::DynamicImage;

#[derive(Parser)]
#[derive(Parser, Debug)]
pub struct RecolorArgs {
/// Color space of the image
#[clap(short, long, value_enum)]
color_type: ColorTypeExt,
color_space: ColorSpace,

/// Bit depth of the image
#[clap(short = 'B', long, value_enum)]
bit_depth: BitDepth,
}

impl RecolorArgs {
pub fn run(&self, image: &mut DynamicImage) -> Result<()> {
let color_info = ColorInfo::new(self.color_type.clone(), self.bit_depth.clone());
let color_info = ColorInfo::new(&self.color_space, &self.bit_depth);

color_info.convert_image(image);
Ok(())
Expand Down
56 changes: 28 additions & 28 deletions src/image/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use image::{ColorType, DynamicImage};
#[derive(Debug, Clone)]
pub struct ColorInfo {
pub bit_depth: BitDepth,
pub color_type: ColorTypeExt,
pub color_space: ColorSpace,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub enum BitDepth {
B8 = 8,
B16 = 16,
Expand Down Expand Up @@ -48,79 +48,79 @@ impl FromStr for BitDepth {
}
}

#[derive(ValueEnum, Debug, Clone)]
pub enum ColorTypeExt {
#[derive(ValueEnum, Debug, Clone, Copy)]
pub enum ColorSpace {
Rgb,
RgbA,
Luma,
LumaA,
Unknown,
}

impl Display for ColorTypeExt {
impl Display for ColorSpace {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}

impl ColorInfo {
pub fn new(color_type: ColorTypeExt, bit_depth: BitDepth) -> Self {
pub fn new(color_space: &ColorSpace, bit_depth: &BitDepth) -> Self {
ColorInfo {
color_type,
bit_depth,
color_space: *color_space,
bit_depth: *bit_depth,
}
}
pub fn from_image(image: &DynamicImage) -> Self {
match image.color() {
ColorType::L8 => ColorInfo {
bit_depth: BitDepth::B8,
color_type: ColorTypeExt::Luma,
color_space: ColorSpace::Luma,
},
ColorType::La8 => ColorInfo {
bit_depth: BitDepth::B8,
color_type: ColorTypeExt::LumaA,
color_space: ColorSpace::LumaA,
},
ColorType::L16 => ColorInfo {
bit_depth: BitDepth::B16,
color_type: ColorTypeExt::Luma,
color_space: ColorSpace::Luma,
},
ColorType::La16 => ColorInfo {
bit_depth: BitDepth::B16,
color_type: ColorTypeExt::LumaA,
color_space: ColorSpace::LumaA,
},
ColorType::Rgb8 => ColorInfo {
bit_depth: BitDepth::B8,
color_type: ColorTypeExt::Rgb,
color_space: ColorSpace::Rgb,
},
ColorType::Rgba8 => ColorInfo {
bit_depth: BitDepth::B8,
color_type: ColorTypeExt::RgbA,
color_space: ColorSpace::RgbA,
},
ColorType::Rgb16 => ColorInfo {
bit_depth: BitDepth::B16,
color_type: ColorTypeExt::Rgb,
color_space: ColorSpace::Rgb,
},
ColorType::Rgba16 => ColorInfo {
bit_depth: BitDepth::B16,
color_type: ColorTypeExt::Rgb,
color_space: ColorSpace::Rgb,
},
ColorType::Rgb32F => ColorInfo {
bit_depth: BitDepth::B32,
color_type: ColorTypeExt::Rgb,
color_space: ColorSpace::Rgb,
},
ColorType::Rgba32F => ColorInfo {
bit_depth: BitDepth::B32,
color_type: ColorTypeExt::RgbA,
color_space: ColorSpace::RgbA,
},
_ => ColorInfo {
bit_depth: BitDepth::B8,
color_type: ColorTypeExt::Unknown,
color_space: ColorSpace::Unknown,
},
}
}
pub fn convert_image(&self, image: &mut DynamicImage) {
let color_type = self.to_color_type();
let colored_image = match color_type {
let color_space = self.to_color_space();
let colored_image = match color_space {
ColorType::L8 => DynamicImage::ImageLuma8(image.to_luma8()),
ColorType::La8 => DynamicImage::ImageLumaA8(image.to_luma_alpha8()),
ColorType::L16 => DynamicImage::ImageLuma16(image.to_luma16()),
Expand All @@ -135,29 +135,29 @@ impl ColorInfo {
};
*image = colored_image;
}
fn to_color_type(&self) -> ColorType {
match self.color_type {
ColorTypeExt::Rgb => match self.bit_depth {
fn to_color_space(&self) -> ColorType {
match self.color_space {
ColorSpace::Rgb => match self.bit_depth {
BitDepth::B8 => ColorType::Rgb8,
BitDepth::B16 => ColorType::Rgb16,
BitDepth::B32 => ColorType::Rgb32F,
},
ColorTypeExt::RgbA => match self.bit_depth {
ColorSpace::RgbA => match self.bit_depth {
BitDepth::B8 => ColorType::Rgba8,
BitDepth::B16 => ColorType::Rgba16,
BitDepth::B32 => ColorType::Rgba32F,
},
ColorTypeExt::Luma => match self.bit_depth {
ColorSpace::Luma => match self.bit_depth {
BitDepth::B8 => ColorType::L8,
BitDepth::B16 => ColorType::L16,
BitDepth::B32 => ColorType::L16,
},
ColorTypeExt::LumaA => match self.bit_depth {
ColorSpace::LumaA => match self.bit_depth {
BitDepth::B8 => ColorType::La8,
BitDepth::B16 => ColorType::La16,
BitDepth::B32 => ColorType::La16,
},
ColorTypeExt::Unknown => ColorType::Rgb8,
ColorSpace::Unknown => ColorType::Rgb8,
}
}
}
2 changes: 1 addition & 1 deletion src/image/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn print_info(image: &DynamicImage, path: PathBuf, do_short: bool) {
println!("Format: {}", format.to_mime_type());

if !do_short {
println!("Color space: {}", color_info.color_type);
println!("Color space: {}", color_info.color_space);
println!("Bit depth: {}", color_info.bit_depth);
}
}

0 comments on commit f6ac36a

Please sign in to comment.