commit
5a081dc811
1 changed files with 33 additions and 28 deletions
11
src/lib.rs
11
src/lib.rs
|
|
@ -45,7 +45,7 @@ pub struct Options {
|
|||
|
||||
pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), String> {
|
||||
// Decode PNG from file
|
||||
println!("Processing: {}", filepath.to_str().unwrap());
|
||||
if opts.verbosity.is_some() { println!("Processing: {}", filepath.to_str().unwrap()) };
|
||||
let in_file = Path::new(filepath);
|
||||
let mut png = match png::PngData::new(&in_file) {
|
||||
Ok(x) => x,
|
||||
|
|
@ -121,7 +121,7 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), String> {
|
|||
let combinations = opts.filter.len() * opts.compression.len() * opts.memory.len() *
|
||||
opts.strategies.len();
|
||||
let mut results = Vec::with_capacity(combinations);
|
||||
println!("Trying: {} combinations", combinations);
|
||||
if opts.verbosity.is_some() { println!("Trying: {} combinations", combinations) };
|
||||
crossbeam::scope(|scope| {
|
||||
for f in &opts.filter {
|
||||
let filtered = png.filter_image(*f);
|
||||
|
|
@ -172,6 +172,7 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), String> {
|
|||
|
||||
if let Some(better) = best {
|
||||
png.idat_data = better.4.clone();
|
||||
if opts.verbosity.is_some() {
|
||||
println!("Found better combination:");
|
||||
println!(" zc = {} zm = {} zs = {} f = {} {} bytes",
|
||||
better.1,
|
||||
|
|
@ -181,6 +182,7 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), String> {
|
|||
png.idat_data.len());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if opts.strip {
|
||||
// Strip headers
|
||||
|
|
@ -225,6 +227,7 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), String> {
|
|||
}
|
||||
};
|
||||
let mut buffer = BufWriter::new(out_file);
|
||||
if opts.verbosity.is_some() {
|
||||
match buffer.write_all(&output_data) {
|
||||
Ok(_) => println!("Output: {}", opts.out_file.display()),
|
||||
Err(_) => {
|
||||
|
|
@ -233,6 +236,8 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), String> {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if opts.verbosity.is_some() {
|
||||
if idat_original_size >= png.idat_data.len() {
|
||||
println!(" IDAT size = {} bytes ({} bytes decrease)",
|
||||
png.idat_data.len(),
|
||||
|
|
@ -255,7 +260,7 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), String> {
|
|||
(output_data.len() - file_original_size) as f64 / file_original_size as f64 *
|
||||
100f64);
|
||||
}
|
||||
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue