diff --git a/src/headers.rs b/src/headers.rs index 6511f604..33ccaf88 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -69,7 +69,7 @@ pub enum Headers { None, /// Remove specific chunks Strip(Vec), - /// Headers that won't affect rendering (all but cICP, iCCP, sBIT, sRGB, pHYs) + /// Remove all chunks that won't affect rendering Safe, /// Remove all non-critical chunks except these Keep(IndexSet), @@ -77,6 +77,11 @@ pub enum Headers { All, } +impl Headers { + /// List of chunks that will be kept when using the `Safe` option + pub const KEEP_SAFE: [[u8; 4]; 4] = [*b"cICP", *b"iCCP", *b"sRGB", *b"pHYs"]; +} + #[inline] pub fn file_header_is_valid(bytes: &[u8]) -> bool { let expected_header: [u8; 8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]; diff --git a/src/lib.rs b/src/lib.rs index 4c43e287..5e4909c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -881,16 +881,9 @@ fn perform_strip(png: &mut PngData, opts: &Options) { raw.aux_headers.remove(hdr.as_bytes()); } } - Headers::Safe => { - const PRESERVED_HEADERS: [[u8; 4]; 5] = - [*b"cICP", *b"iCCP", *b"sBIT", *b"sRGB", *b"pHYs"]; - let keys: Vec<[u8; 4]> = raw.aux_headers.keys().cloned().collect(); - for hdr in &keys { - if !PRESERVED_HEADERS.contains(hdr) { - raw.aux_headers.remove(hdr); - } - } - } + Headers::Safe => raw + .aux_headers + .retain(|hdr, _| Headers::KEEP_SAFE.contains(hdr)), Headers::All => { raw.aux_headers = IndexMap::new(); }