Move preserved header list, drop sBIT

This commit is contained in:
Andrew 2023-05-03 18:30:16 +12:00
parent a5832706bd
commit 31edea99f7
2 changed files with 9 additions and 11 deletions

View file

@ -69,7 +69,7 @@ pub enum Headers {
None, None,
/// Remove specific chunks /// Remove specific chunks
Strip(Vec<String>), Strip(Vec<String>),
/// Headers that won't affect rendering (all but cICP, iCCP, sBIT, sRGB, pHYs) /// Remove all chunks that won't affect rendering
Safe, Safe,
/// Remove all non-critical chunks except these /// Remove all non-critical chunks except these
Keep(IndexSet<String>), Keep(IndexSet<String>),
@ -77,6 +77,11 @@ pub enum Headers {
All, 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] #[inline]
pub fn file_header_is_valid(bytes: &[u8]) -> bool { pub fn file_header_is_valid(bytes: &[u8]) -> bool {
let expected_header: [u8; 8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]; let expected_header: [u8; 8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];

View file

@ -881,16 +881,9 @@ fn perform_strip(png: &mut PngData, opts: &Options) {
raw.aux_headers.remove(hdr.as_bytes()); raw.aux_headers.remove(hdr.as_bytes());
} }
} }
Headers::Safe => { Headers::Safe => raw
const PRESERVED_HEADERS: [[u8; 4]; 5] = .aux_headers
[*b"cICP", *b"iCCP", *b"sBIT", *b"sRGB", *b"pHYs"]; .retain(|hdr, _| Headers::KEEP_SAFE.contains(hdr)),
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::All => { Headers::All => {
raw.aux_headers = IndexMap::new(); raw.aux_headers = IndexMap::new();
} }