From c530bba261a56842d0094358a4c0111f77c0b3f2 Mon Sep 17 00:00:00 2001 From: Kornel Date: Wed, 18 Jul 2018 04:55:21 +0100 Subject: [PATCH] --keep option (#121) --- src/headers.rs | 7 +++++-- src/lib.rs | 7 ++++++- src/main.rs | 15 ++++++++++++++- tests/flags.rs | 2 +- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/headers.rs b/src/headers.rs index d08c4a11..7979e1ba 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -1,3 +1,4 @@ +use std::collections::HashSet; use byteorder::{BigEndian, ReadBytesExt}; use colors::{BitDepth, ColorType}; use crc::crc32; @@ -28,10 +29,12 @@ pub struct IhdrData { pub enum Headers { /// None None, - /// Some, with a list of 4-character chunk codes - Some(Vec), + /// Remove specific chunks + Strip(Vec), /// Headers that won't affect rendering (all but cHRM, gAMA, iCCP, sBIT, sRGB, bKGD, hIST, pHYs, sPLT) Safe, + /// Remove all non-critical chunks except these + Keep(HashSet), /// All non-critical headers All, } diff --git a/src/lib.rs b/src/lib.rs index dd8a7068..07eb25e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -808,7 +808,12 @@ fn perform_strip(png: &mut PngData, opts: &Options) { match opts.strip { // Strip headers Headers::None => (), - Headers::Some(ref hdrs) => for hdr in hdrs { + Headers::Keep(ref hdrs) => { + png.aux_headers.retain(|chunk, _| { + hdrs.contains(chunk) + }); + }, + Headers::Strip(ref hdrs) => for hdr in hdrs { png.aux_headers.remove(hdr); }, Headers::Safe => { diff --git a/src/main.rs b/src/main.rs index 7a175ce4..960139fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,6 +94,13 @@ fn main() { .takes_value(true) .value_name("mode") .conflicts_with("strip-safe")) + .arg(Arg::with_name("keep") + .help("Strip all optional metadata except objects in the comma-separated list") + .long("keep") + .takes_value(true) + .value_name("list") + .conflicts_with("strip") + .conflicts_with("strip-safe")) .arg(Arg::with_name("alpha") .help("Perform additional alpha optimizations") .short("a") @@ -411,6 +418,12 @@ fn parse_opts_into_struct(matches: &ArgMatches) -> Result<(OutFile, Option Result<(OutFile, Option