Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed quiet mode #20

Merged
merged 1 commit into from
Mar 4, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 33 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -172,13 +172,15 @@ 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,
better.2,
better.3,
better.0,
png.idat_data.len());
}
}
}

Expand Down Expand Up @@ -225,37 +227,40 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), String> {
}
};
let mut buffer = BufWriter::new(out_file);
match buffer.write_all(&output_data) {
Ok(_) => println!("Output: {}", opts.out_file.display()),
Err(_) => {
return Err(format!("Unable to write to file {}", opts.out_file.display()))
if opts.verbosity.is_some() {
match buffer.write_all(&output_data) {
Ok(_) => println!("Output: {}", opts.out_file.display()),
Err(_) => {
return Err(format!("Unable to write to file {}", opts.out_file.display()))
}
}
}
}
}
if idat_original_size >= png.idat_data.len() {
println!(" IDAT size = {} bytes ({} bytes decrease)",
png.idat_data.len(),
idat_original_size - png.idat_data.len());
} else {
println!(" IDAT size = {} bytes ({} bytes increase)",
png.idat_data.len(),
png.idat_data.len() - idat_original_size);
}
if file_original_size >= output_data.len() {
println!(" file size = {} bytes ({} bytes = {:.2}% decrease)",
output_data.len(),
file_original_size - output_data.len(),
(file_original_size - output_data.len()) as f64 / file_original_size as f64 *
100f64);
} else {
println!(" file size = {} bytes ({} bytes = {:.2}% increase)",
output_data.len(),
output_data.len() - file_original_size,
(output_data.len() - file_original_size) as f64 / file_original_size as f64 *
100f64);
if opts.verbosity.is_some() {
if idat_original_size >= png.idat_data.len() {
println!(" IDAT size = {} bytes ({} bytes decrease)",
png.idat_data.len(),
idat_original_size - png.idat_data.len());
} else {
println!(" IDAT size = {} bytes ({} bytes increase)",
png.idat_data.len(),
png.idat_data.len() - idat_original_size);
}
if file_original_size >= output_data.len() {
println!(" file size = {} bytes ({} bytes = {:.2}% decrease)",
output_data.len(),
file_original_size - output_data.len(),
(file_original_size - output_data.len()) as f64 / file_original_size as f64 *
100f64);
} else {
println!(" file size = {} bytes ({} bytes = {:.2}% increase)",
output_data.len(),
output_data.len() - file_original_size,
(output_data.len() - file_original_size) as f64 / file_original_size as f64 *
100f64);
}
}

Ok(())
}

Expand Down