From 939d886932fe495f6fd942b977ef5889135003c7 Mon Sep 17 00:00:00 2001 From: Joshua Holmer Date: Tue, 12 Jan 2016 11:06:52 -0500 Subject: [PATCH] Add header stripping --- src/lib.rs | 10 ++++++++-- tests/oxipng.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1f3346bb..77aa4fb1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ extern crate crc; extern crate libc; extern crate libz_sys; +use std::collections::HashMap; use std::collections::HashSet; use std::fs; use std::fs::File; @@ -73,7 +74,9 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), String> { println!(" IDAT size = {} bytes", idat_original_size); println!(" File size = {} bytes", file_original_size); } - // TODO: Bit depth/palette reduction + // TODO: Color space reduction + // TODO: 16-bit Bit depth reduction + // TODO: Palette bit depth reduction // // TODO: Apply interlacing changes // @@ -127,7 +130,10 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), String> { } } - // TODO: Perform stripping + if opts.strip { + // Strip headers + png.aux_headers = HashMap::new(); + } let output_data = png.output(); if file_original_size <= output_data.len() && !opts.force { diff --git a/tests/oxipng.rs b/tests/oxipng.rs index 3ccca87d..bd7b122d 100644 --- a/tests/oxipng.rs +++ b/tests/oxipng.rs @@ -129,3 +129,30 @@ fn reduce_palette_png() { assert!(png.ihdr_data.bit_depth == png::BitDepth::Eight); assert!(png.palette.unwrap().len() == 43 * 3); } + +#[test] +fn strip_headers() { + let input = PathBuf::from("tests/files/test_rgb.png"); + let mut opts = get_opts(&input); + opts.strip = true; + let output = opts.out_file.clone(); + + match oxipng::optimize(&input, &opts) { + Ok(_) => (), + Err(x) => panic!(x.to_owned()) + }; + assert!(output.exists()); + + let png = match png::PngData::new(&output) { + Ok(x) => { + remove_file(output).ok(); + x + }, + Err(x) => { + remove_file(output).ok(); + panic!(x.to_owned()) + }, + }; + + assert!(!png.aux_headers.contains_key("tEXt")); +}