Extract bw stuff to new module
This commit is contained in:
parent
be3fbc635e
commit
51e3fed4f6
3 changed files with 71 additions and 69 deletions
61
src/bw.rs
Normal file
61
src/bw.rs
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
use image::RgbaImage;
|
||||||
|
use palette::{Hsl, Hsv, IntoColor, Lab, LinSrgb, Oklab, Srgb};
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum Method {
|
||||||
|
SrgbAverage,
|
||||||
|
LinSrgbAverage,
|
||||||
|
Hsl,
|
||||||
|
Hsv,
|
||||||
|
Cielab,
|
||||||
|
Oklab,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Method {
|
||||||
|
fn to_bw(self, pixel: Srgb) -> Srgb {
|
||||||
|
match self {
|
||||||
|
Self::SrgbAverage => {
|
||||||
|
let value = (pixel.red + pixel.green + pixel.blue) / 3.0;
|
||||||
|
Srgb::new(value, value, value)
|
||||||
|
}
|
||||||
|
Self::LinSrgbAverage => {
|
||||||
|
let pixel: LinSrgb = pixel.into_color();
|
||||||
|
let value = (pixel.red + pixel.green + pixel.blue) / 3.0;
|
||||||
|
LinSrgb::new(value, value, value).into_color()
|
||||||
|
}
|
||||||
|
Self::Hsl => {
|
||||||
|
let mut pixel: Hsl = pixel.into_color();
|
||||||
|
pixel.saturation = 0.0;
|
||||||
|
pixel.into_color()
|
||||||
|
}
|
||||||
|
Self::Hsv => {
|
||||||
|
let mut pixel: Hsv = pixel.into_color();
|
||||||
|
pixel.saturation = 0.0;
|
||||||
|
pixel.into_color()
|
||||||
|
}
|
||||||
|
Self::Cielab => {
|
||||||
|
let mut pixel: Lab = pixel.into_color();
|
||||||
|
pixel.a = 0.5;
|
||||||
|
pixel.b = 0.5;
|
||||||
|
pixel.into_color()
|
||||||
|
}
|
||||||
|
Self::Oklab => {
|
||||||
|
let mut pixel: Oklab = pixel.into_color();
|
||||||
|
pixel.a = 0.0;
|
||||||
|
pixel.b = 0.0;
|
||||||
|
pixel.into_color()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bw(image: &mut RgbaImage, method: Method) {
|
||||||
|
for pixel in image.pixels_mut() {
|
||||||
|
let [r, g, b, _] = pixel.0;
|
||||||
|
let srgb = Srgb::new(r, g, b).into_format::<f32>();
|
||||||
|
let srgb = method.to_bw(srgb).into_format::<u8>();
|
||||||
|
pixel.0[0] = srgb.red;
|
||||||
|
pixel.0[1] = srgb.green;
|
||||||
|
pixel.0[2] = srgb.blue;
|
||||||
|
}
|
||||||
|
}
|
||||||
62
src/lib.rs
62
src/lib.rs
|
|
@ -1,61 +1 @@
|
||||||
use image::RgbaImage;
|
pub mod bw;
|
||||||
use palette::{Hsl, Hsv, IntoColor, Lab, LinSrgb, Oklab, Srgb};
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
||||||
pub enum BwMethod {
|
|
||||||
SrgbAverage,
|
|
||||||
LinSrgbAverage,
|
|
||||||
Hsl,
|
|
||||||
Hsv,
|
|
||||||
Cielab,
|
|
||||||
Oklab,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BwMethod {
|
|
||||||
fn to_bw(self, pixel: Srgb) -> Srgb {
|
|
||||||
match self {
|
|
||||||
Self::SrgbAverage => {
|
|
||||||
let value = (pixel.red + pixel.green + pixel.blue) / 3.0;
|
|
||||||
Srgb::new(value, value, value)
|
|
||||||
}
|
|
||||||
Self::LinSrgbAverage => {
|
|
||||||
let pixel: LinSrgb = pixel.into_color();
|
|
||||||
let value = (pixel.red + pixel.green + pixel.blue) / 3.0;
|
|
||||||
LinSrgb::new(value, value, value).into_color()
|
|
||||||
}
|
|
||||||
Self::Hsl => {
|
|
||||||
let mut pixel: Hsl = pixel.into_color();
|
|
||||||
pixel.saturation = 0.0;
|
|
||||||
pixel.into_color()
|
|
||||||
}
|
|
||||||
Self::Hsv => {
|
|
||||||
let mut pixel: Hsv = pixel.into_color();
|
|
||||||
pixel.saturation = 0.0;
|
|
||||||
pixel.into_color()
|
|
||||||
}
|
|
||||||
Self::Cielab => {
|
|
||||||
let mut pixel: Lab = pixel.into_color();
|
|
||||||
pixel.a = 0.5;
|
|
||||||
pixel.b = 0.5;
|
|
||||||
pixel.into_color()
|
|
||||||
}
|
|
||||||
Self::Oklab => {
|
|
||||||
let mut pixel: Oklab = pixel.into_color();
|
|
||||||
pixel.a = 0.0;
|
|
||||||
pixel.b = 0.0;
|
|
||||||
pixel.into_color()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn bw(image: &mut RgbaImage, method: BwMethod) {
|
|
||||||
for pixel in image.pixels_mut() {
|
|
||||||
let [r, g, b, _] = pixel.0;
|
|
||||||
let srgb = Srgb::new(r, g, b).into_format::<f32>();
|
|
||||||
let srgb = method.to_bw(srgb).into_format::<u8>();
|
|
||||||
pixel.0[0] = srgb.red;
|
|
||||||
pixel.0[1] = srgb.green;
|
|
||||||
pixel.0[2] = srgb.blue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
17
src/main.rs
17
src/main.rs
|
|
@ -5,6 +5,7 @@ use std::{
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use image::{ImageFormat, RgbaImage};
|
use image::{ImageFormat, RgbaImage};
|
||||||
|
use mark::bw;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, clap::ValueEnum)]
|
#[derive(Debug, Clone, Copy, clap::ValueEnum)]
|
||||||
enum BwMethod {
|
enum BwMethod {
|
||||||
|
|
@ -16,15 +17,15 @@ enum BwMethod {
|
||||||
Oklab,
|
Oklab,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<BwMethod> for mark::BwMethod {
|
impl From<BwMethod> for bw::Method {
|
||||||
fn from(value: BwMethod) -> Self {
|
fn from(value: BwMethod) -> Self {
|
||||||
match value {
|
match value {
|
||||||
BwMethod::SrgbAverage => mark::BwMethod::SrgbAverage,
|
BwMethod::SrgbAverage => bw::Method::SrgbAverage,
|
||||||
BwMethod::LinSrgbAverage => mark::BwMethod::LinSrgbAverage,
|
BwMethod::LinSrgbAverage => bw::Method::LinSrgbAverage,
|
||||||
BwMethod::Hsl => mark::BwMethod::Hsl,
|
BwMethod::Hsl => bw::Method::Hsl,
|
||||||
BwMethod::Hsv => mark::BwMethod::Hsv,
|
BwMethod::Hsv => bw::Method::Hsv,
|
||||||
BwMethod::Cielab => mark::BwMethod::Cielab,
|
BwMethod::Cielab => bw::Method::Cielab,
|
||||||
BwMethod::Oklab => mark::BwMethod::Oklab,
|
BwMethod::Oklab => bw::Method::Oklab,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -38,7 +39,7 @@ struct BwCmd {
|
||||||
|
|
||||||
impl BwCmd {
|
impl BwCmd {
|
||||||
fn run(self, mut image: RgbaImage) -> RgbaImage {
|
fn run(self, mut image: RgbaImage) -> RgbaImage {
|
||||||
mark::bw(&mut image, self.method.into());
|
bw::bw(&mut image, self.method.into());
|
||||||
image
|
image
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue