Skip to content

Commit

Permalink
feat(cli): added alpha premultiply preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
SalOne22 committed Mar 26, 2024
1 parent 0bce6bc commit 33abc88
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
31 changes: 31 additions & 0 deletions src/cli/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ use zune_image::{
},
errors::ImageErrors,
image::Image,
metadata::AlphaState,
traits::{EncoderTrait, OperationsTrait},
};
use zune_imageprocs::premul_alpha::PremultiplyAlpha;

pub fn decode<P: AsRef<Path>>(f: P) -> Result<Image, ImageErrors> {
Image::open(f.as_ref()).or_else(|e| {
Expand Down Expand Up @@ -119,6 +121,35 @@ pub fn operations(matches: &ArgMatches, img: &Image) -> BTreeMap<usize, Box<dyn
}
}

if let Some(values) = matches.get_many::<bool>("premultiply") {
values
.into_iter()
.zip(matches.indices_of("premultiply").unwrap())
.for_each(|(value, idx)| {
if let Some(op) = map.get(&(idx + 2)) {
log::trace!("setup alpha premultiply for {}", op.name());

map.insert(
idx,
Box::new(PremultiplyAlpha::new(AlphaState::PreMultiplied)),
);

assert!(
map.get(&(idx + 3)).is_none(),
"There is a operation at {} aborting",
idx + 3
);

map.insert(
idx + 3,
Box::new(PremultiplyAlpha::new(AlphaState::NonPreMultiplied)),
);
} else {
log::warn!("No operation found for premultiply at index {idx}")
}
})
}

map
}

Expand Down
19 changes: 13 additions & 6 deletions src/cli/preprocessors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_imports)]

use clap::{arg, value_parser, ArgAction, ArgGroup, Command};
use clap::{arg, value_parser, Arg, ArgAction, ArgGroup, Command};
use indoc::indoc;

#[cfg(feature = "resize")]
Expand Down Expand Up @@ -59,14 +59,21 @@ impl Preprocessors for Command {
If quality is not provided, default 75 is used."#})
.value_parser(value_parser!(u8).range(1..=100))
.default_missing_value("75")
.requires("quantization")
.requires("quantization"),

position_sensitive_flag(arg!(--premultiply "Premultiply alpha before operation"))
.action(ArgAction::Append)
])
}
}

#[cfg(not(any(feature = "resize", feature = "quantization")))]
fn preprocessors(self) -> Self {
self
}
fn position_sensitive_flag(arg: Arg) -> Arg {
// Flags don't track the position of each occurrence, so we need to emulate flags with
// value-less options to get the same result
arg.num_args(0)
.value_parser(value_parser!(bool))
.default_missing_value("true")
.default_value("false")
}

pub trait Preprocessors {
Expand Down
18 changes: 2 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ use cli::{
};
use rayon::prelude::*;
use zune_core::colorspace::ColorSpace;
use zune_image::{
core_filters::colorspace::ColorspaceConv, image::Image, metadata::AlphaState,
pipelines::Pipeline,
};
use zune_imageprocs::{auto_orient::AutoOrient, premul_alpha::PremultiplyAlpha};
use zune_image::{core_filters::colorspace::ColorspaceConv, image::Image, pipelines::Pipeline};
use zune_imageprocs::auto_orient::AutoOrient;

mod cli;

Expand Down Expand Up @@ -84,17 +81,6 @@ fn main() {
operations(matches, &img)
.into_iter()
.for_each(|(_, operations)| match operations.name() {
"fast resize" => {
pipeline.add_operation(Box::new(PremultiplyAlpha::new(
AlphaState::PreMultiplied,
)));

pipeline.add_operation(operations);

pipeline.add_operation(Box::new(PremultiplyAlpha::new(
AlphaState::NonPreMultiplied,
)));
}
"quantize" => {
pipeline.add_operation(Box::new(ColorspaceConv::new(ColorSpace::RGBA)));
pipeline.add_operation(operations);
Expand Down

0 comments on commit 33abc88

Please sign in to comment.