Skip to content

Commit 4b45f33

Browse files
committed
Fix to #82 + added chroma subsampling option #81
1 parent 376c76e commit 4b45f33

File tree

5 files changed

+44
-19
lines changed

5 files changed

+44
-19
lines changed

Cargo.lock

+7-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ rayon = "1.10"
2222
human_bytes = { version = "0.4", default-features = false }
2323
kamadak-exif = "0.6"
2424
imagesize = "0.13"
25-
libcaesium = "0.17.2"
25+
libcaesium = "0.17.3"
2626
clap = { version = "4.5", features = ["derive"] }
2727

2828
[dev-dependencies]

src/compressor.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::options::{OutputFormat, OverwritePolicy};
22
use crate::scan_files::get_file_mime_type;
3-
use caesium::parameters::CSParameters;
3+
use caesium::parameters::{CSParameters, ChromaSubsampling};
44
use caesium::{compress_in_memory, compress_to_size_in_memory, convert_in_memory, SupportedFileTypes};
55
use indicatif::ProgressBar;
66
use rayon::iter::ParallelIterator;
@@ -28,7 +28,6 @@ pub struct CompressionResult {
2828
pub message: String,
2929
}
3030

31-
#[derive(Debug)]
3231
pub struct CompressionOptions {
3332
pub quality: Option<u32>,
3433
pub max_size: Option<usize>,
@@ -48,6 +47,7 @@ pub struct CompressionOptions {
4847
pub format: OutputFormat,
4948
pub keep_dates: bool,
5049
pub keep_structure: bool,
50+
pub jpeg_chroma_subsampling: ChromaSubsampling,
5151
}
5252

5353
pub fn start_compression(
@@ -251,6 +251,8 @@ fn build_compression_parameters(
251251

252252
parameters.keep_metadata = options.exif;
253253

254+
parameters.jpeg.chroma_subsampling = options.jpeg_chroma_subsampling;
255+
254256
parameters.png.optimization_level = options.png_opt_level;
255257
parameters.png.force_zopfli = options.zopfli;
256258

@@ -774,6 +776,7 @@ mod tests {
774776
keep_dates: false,
775777
exif: true,
776778
png_opt_level: 0,
779+
jpeg_chroma_subsampling: ChromaSubsampling::Auto,
777780
zopfli: false,
778781
base_path: PathBuf::new(),
779782
}

src/main.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::compressor::{start_compression, CompressionOptions, CompressionResult, CompressionStatus};
2-
use crate::options::CommandLineArgs;
2+
use crate::options::{CommandLineArgs, JpegChromaSubsampling};
33
use crate::scan_files::scan_files;
4+
use caesium::parameters::ChromaSubsampling;
45
use clap::Parser;
56
use human_bytes::human_bytes;
67
use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
@@ -137,11 +138,22 @@ fn build_compression_options(args: &CommandLineArgs, base_path: &Path) -> Compre
137138
keep_dates: args.keep_dates,
138139
exif: args.exif,
139140
png_opt_level: args.png_opt_level,
141+
jpeg_chroma_subsampling: parse_jpeg_chroma_subsampling(args.jpeg_chroma_subsampling),
140142
zopfli: args.zopfli,
141143
base_path: PathBuf::from(base_path),
142144
}
143145
}
144146

147+
fn parse_jpeg_chroma_subsampling(arg: JpegChromaSubsampling) -> ChromaSubsampling {
148+
match arg {
149+
JpegChromaSubsampling::ChromaSubsampling444 => ChromaSubsampling::CS444,
150+
JpegChromaSubsampling::ChromaSubsampling422 => ChromaSubsampling::CS422,
151+
JpegChromaSubsampling::ChromaSubsampling420 => ChromaSubsampling::CS420,
152+
JpegChromaSubsampling::ChromaSubsampling411 => ChromaSubsampling::CS411,
153+
_ => ChromaSubsampling::Auto,
154+
}
155+
}
156+
145157
#[cfg(test)]
146158
mod tests {
147159
use super::*;

src/options.rs

+18
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ pub enum OutputFormat {
1919
Tiff,
2020
Original,
2121
}
22+
23+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
24+
pub enum JpegChromaSubsampling {
25+
#[value(name = "4:4:4")]
26+
ChromaSubsampling444,
27+
#[value(name = "4:2:2")]
28+
ChromaSubsampling422,
29+
#[value(name = "4:2:0")]
30+
ChromaSubsampling420,
31+
#[value(name = "4:1:1")]
32+
ChromaSubsampling411,
33+
#[value(name = "auto")]
34+
Auto,
35+
}
2236
#[derive(Parser, Debug)]
2337
#[command(version, about, long_about = None)]
2438
pub struct CommandLineArgs {
@@ -39,6 +53,10 @@ pub struct CommandLineArgs {
3953
#[arg(long, default_value = "3")]
4054
pub png_opt_level: u8,
4155

56+
/// select level for PNG optimization, between [0-6]
57+
#[arg(long, value_enum, default_value = "auto")]
58+
pub jpeg_chroma_subsampling: JpegChromaSubsampling,
59+
4260
/// use zopfli when optimizing PNG files (it may take a very long time to complete)
4361
#[arg(long)]
4462
pub zopfli: bool,

0 commit comments

Comments
 (0)