Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added flag to strip metadata #281

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/cli/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::PathBuf;

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

use super::{preprocessors::Preprocessors, utils::threads};
Expand Down Expand Up @@ -51,6 +51,10 @@ impl CommonArgs for Command {
By default, the number of available threads is utilized."#})
.value_parser(value_parser!(u8).range(1..=threads::num_threads() as i64)),
)
.arg(
arg!(-x --strip "Strip metadata when encoding images (where supported)")
.action(ArgAction::SetTrue)
)
.arg(
arg!(--"no-progress" "Disables progress bar.")
.long_help(indoc! {r#"Disables progress bar.
Expand Down
6 changes: 6 additions & 0 deletions src/codecs/mozjpeg/encoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ impl EncoderTrait for MozJpegEncoder {
{
use exif::experimental::Writer;

// write exif data
if let Some(metadata) = &image.metadata().exif() {
let mut writer = Writer::new();
// write first tags for exif
Expand All @@ -195,6 +196,11 @@ impl EncoderTrait for MozJpegEncoder {
log::warn!("Writing exif failed {:?}", result);
}
}

// write icc data
if let Some(metadata) = &image.metadata().icc_chunk() {
comp.write_icc_profile(metadata);
}
}

comp.write_scanlines(data)?;
Expand Down
6 changes: 6 additions & 0 deletions src/codecs/oxipng/encoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl EncoderTrait for OxiPngEncoder {

let mut buf = std::io::Cursor::new(vec![]);

// write exif data
if let Some(fields) = &image.metadata().exif() {
let mut writer = Writer::new();

Expand All @@ -112,6 +113,11 @@ impl EncoderTrait for OxiPngEncoder {
log::warn!("Writing exif failed {:?}", result);
}
}

// write icc data
if let Some(icc) = &image.metadata().icc_chunk() {
img.add_icc_profile(icc);
}
}

let mut writer = ZWriter::new(sink);
Expand Down
13 changes: 11 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ macro_rules! handle_error {
};
}

const SUPPORTS_EXIF: &[&str] = &["mozjpeg", "oxipng"];
const SUPPORTS_ICC: &[&str] = &["mozjpeg", "oxipng"];

struct Result {
output: PathBuf,
input_size: u64,
Expand Down Expand Up @@ -105,6 +108,7 @@ fn main() {

let recursive = matches.get_flag("recursive");
let backup = matches.get_flag("backup");
let strip_metadata = matches.get_flag("strip");
let quiet = matches.get_flag("quiet");
let no_progress = matches.get_flag("no-progress");

Expand Down Expand Up @@ -151,8 +155,13 @@ fn main() {
pipeline.chain_operations(Box::new(Depth::new(BitDepth::Eight)));
pipeline.chain_operations(Box::new(ColorspaceConv::new(ColorSpace::RGBA)));

pipeline.chain_operations(Box::new(AutoOrient));
pipeline.chain_operations(Box::new(ApplySRGB));
if strip_metadata || !SUPPORTS_EXIF.contains(&subcommand) {
pipeline.chain_operations(Box::new(AutoOrient));
}

if strip_metadata || !SUPPORTS_ICC.contains(&subcommand) {
pipeline.chain_operations(Box::new(ApplySRGB));
}

operations(matches, &img)
.into_iter()
Expand Down
4 changes: 3 additions & 1 deletion src/operations/icc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ impl OperationsTrait for ApplySRGB {
return Ok(());
}

ApplyICC::new(Profile::new_srgb_context(ThreadContext::new())).execute_impl(image)
ApplyICC::new(Profile::new_srgb_context(ThreadContext::new())).execute_impl(image)?;

Ok(())
}

fn supported_types(&self) -> &'static [BitType] {
Expand Down
Loading