Skip to content

Commit

Permalink
feat(cli): added oxipng codec
Browse files Browse the repository at this point in the history
  • Loading branch information
SalOne22 committed Mar 16, 2024
1 parent 37359e2 commit 9658fce
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/cli/codecs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use clap::Command;

use self::{
farbfeld::farbfeld, jpeg::jpeg, jpeg_xl::jpeg_xl, mozjpeg::mozjpeg, png::png, ppm::ppm,
qoi::qoi,
farbfeld::farbfeld, jpeg::jpeg, jpeg_xl::jpeg_xl, mozjpeg::mozjpeg, oxipng::oxipng, png::png,
ppm::ppm, qoi::qoi,
};

mod farbfeld;
mod jpeg;
mod jpeg_xl;
mod mozjpeg;
mod oxipng;
mod png;
mod ppm;
mod qoi;
Expand All @@ -20,6 +21,7 @@ impl Codecs for Command {
jpeg(),
jpeg_xl(),
mozjpeg(),
oxipng(),
png(),
ppm(),
qoi(),
Expand Down
23 changes: 23 additions & 0 deletions src/cli/codecs/oxipng.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use clap::{arg, value_parser, Command};
use indoc::indoc;

pub fn oxipng() -> Command {
Command::new("oxipng")
.alias("oxi")
.about("Encode images into PNG format using OxiPNG codec")
.args([
arg!(--interlace "Set interlace mode (progressive)"),
arg!(--effort <NUM> "Optimization level (0-6, or max)").long_help(indoc! {"Set the optimization level preset. The default level 2 is quite fast and provides good compression.
Lower levels are faster, higher levels provide better compression, though with increasingly diminishing returns.
0 => (1 trial, determined heuristically)
1 => (1 trial, determined heuristically)
2 => (4 fast trials, 1 main trial)
3 => (4 trials)
4 => (4 trials)
5 => (8 trials)
6 => (10 trials)"})
.value_parser(value_parser!(u8).range(0..=6))
.default_value("2"),
])
}
17 changes: 16 additions & 1 deletion src/cli/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::{collections::BTreeMap, fs::read, path::Path};

use clap::ArgMatches;
use mozjpeg::qtable;
use rimage::codecs::mozjpeg::{MozJpegEncoder, MozJpegOptions};
use rimage::codecs::{
mozjpeg::{MozJpegEncoder, MozJpegOptions},
oxipng::{OxiPngEncoder, OxiPngOptions},
};
use zune_core::{bytestream::ZByteReader, options::EncoderOptions};
use zune_image::{
codecs::{
Expand Down Expand Up @@ -211,6 +214,18 @@ pub fn encoder(matches: &ArgMatches) -> Result<(Box<dyn EncoderTrait>, &'static

Ok((Box::new(MozJpegEncoder::new_with_options(options)), "jpg"))
}
"oxipng" => {
let mut options =
OxiPngOptions::from_preset(*matches.get_one::<u8>("effort").unwrap_or(&2));

options.interlace = if matches.get_flag("interlace") {
Some(oxipng::Interlacing::Adam7)
} else {
None
};

Ok((Box::new(OxiPngEncoder::new_with_options(options)), "png"))
}
"png" => Ok((Box::new(PngEncoder::new()), "png")),
"ppm" => Ok((Box::new(PPMEncoder::new()), "ppm")),
"qoi" => Ok((Box::new(QoiEncoder::new()), "qoi")),
Expand Down

0 comments on commit 9658fce

Please sign in to comment.