Fix several new clippy lints

This commit is contained in:
Joshua Holmer 2016-05-13 10:46:16 -04:00
parent 0ddd875c84
commit 1d39714b0f
5 changed files with 52 additions and 127 deletions

View file

@ -1,3 +1,6 @@
**Version 0.8.1 (unreleased)**
- Minor optimizations
**Version 0.8.0** **Version 0.8.0**
- [SEMVER_MINOR] Add support for optimizing PNGs already loaded into memory via library function - [SEMVER_MINOR] Add support for optimizing PNGs already loaded into memory via library function

118
Cargo.lock generated
View file

@ -4,13 +4,13 @@ version = "0.8.0"
dependencies = [ dependencies = [
"bit-vec 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "bit-vec 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"byteorder 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"crc 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "crc 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"image 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "image 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"libz-sys 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "libz-sys 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"miniz-sys 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "miniz-sys 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.69 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.1.69 (registry+https://github.com/rust-lang/crates.io-index)",
"scoped-pool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "scoped-pool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -33,19 +33,14 @@ name = "bit-vec"
version = "0.4.3" version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bitflags"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "0.5.0" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "byteorder" name = "bitflags"
version = "0.4.2" version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
@ -55,7 +50,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "clap" name = "clap"
version = "2.4.0" version = "2.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"ansi_term 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "ansi_term 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -66,11 +61,6 @@ dependencies = [
"vec_map 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "vec_map 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "color_quant"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "crc" name = "crc"
version = "1.2.0" version = "1.2.0"
@ -84,14 +74,6 @@ name = "crossbeam"
version = "0.2.9" version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "deque"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "enum_primitive" name = "enum_primitive"
version = "0.1.0" version = "0.1.0"
@ -100,19 +82,9 @@ dependencies = [
"num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "euclid"
version = "0.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "flate2" name = "flate2"
version = "0.2.13" version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
@ -124,15 +96,6 @@ name = "gcc"
version = "0.3.28" version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "gif"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"color_quant 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lzw 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "glob" name = "glob"
version = "0.2.11" version = "0.2.11"
@ -140,16 +103,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "image" name = "image"
version = "0.8.0" version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"byteorder 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"enum_primitive 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "enum_primitive 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"gif 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"jpeg-decoder 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-rational 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"png 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"png 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -157,23 +120,12 @@ name = "inflate"
version = "0.1.1" version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "jpeg-decoder"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"euclid 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)",
"num-rational 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "kernel32-sys" name = "kernel32-sys"
version = "0.2.2" version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -197,16 +149,6 @@ dependencies = [
"pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "log"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lzw"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "0.1.11" version = "0.1.11"
@ -229,11 +171,8 @@ name = "num"
version = "0.1.32" version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"num-bigint 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-complex 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-rational 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -248,15 +187,6 @@ dependencies = [
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "num-complex"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "num-integer" name = "num-integer"
version = "0.1.32" version = "0.1.32"
@ -292,7 +222,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "num_cpus" name = "num_cpus"
version = "0.2.11" version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
@ -305,11 +235,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "png" name = "png"
version = "0.4.3" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"flate2 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", "flate2 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
"inflate 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "inflate 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
@ -323,16 +253,6 @@ dependencies = [
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "rayon"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"deque 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "regex" name = "regex"
version = "0.1.69" version = "0.1.69"
@ -414,7 +334,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.2.6" version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]

View file

@ -33,5 +33,7 @@ num_cpus = "^0.2.11"
regex = "^0.1.63" regex = "^0.1.63"
scoped-pool = "^0.1.8" scoped-pool = "^0.1.8"
[dev-dependencies] [dev-dependencies.image]
image = "^0.8.0" version = "^0.10.0"
default-features = false
features = ["png_codec"]

View file

@ -163,7 +163,7 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), String> {
let in_file = Path::new(filepath); let in_file = Path::new(filepath);
let original_size = in_file.metadata().unwrap().len() as usize; let original_size = in_file.metadata().unwrap().len() as usize;
let mut png = match png::PngData::new(&in_file, opts.fix_errors) { let mut png = match png::PngData::new(in_file, opts.fix_errors) {
Ok(x) => x, Ok(x) => x,
Err(x) => return Err(x), Err(x) => return Err(x),
}; };
@ -278,7 +278,7 @@ pub fn optimize_from_memory(data: &[u8], opts: &Options) -> Result<Vec<u8>, Stri
writeln!(&mut stderr(), "Processing from memory").ok(); writeln!(&mut stderr(), "Processing from memory").ok();
} }
let original_size = data.len() as usize; let original_size = data.len() as usize;
let mut png = match png::PngData::from_slice(&data, opts.fix_errors) { let mut png = match png::PngData::from_slice(data, opts.fix_errors) {
Ok(x) => x, Ok(x) => x,
Err(x) => return Err(x), Err(x) => return Err(x),
}; };
@ -355,7 +355,7 @@ fn optimize_png(mut png: &mut png::PngData, file_original_size: usize, opts: &Op
} }
} }
let something_changed = perform_reductions(&mut png, &opts); let something_changed = perform_reductions(&mut png, opts);
if opts.idat_recoding || something_changed { if opts.idat_recoding || something_changed {
let thread_count = opts.threads; let thread_count = opts.threads;
@ -434,7 +434,7 @@ fn optimize_png(mut png: &mut png::PngData, file_original_size: usize, opts: &Op
} }
} }
perform_strip(&mut png, &opts); perform_strip(&mut png, opts);
let output = png.output(); let output = png.output();
@ -483,7 +483,7 @@ fn perform_reductions(png: &mut png::PngData, opts: &Options) -> bool {
if png.reduce_palette() { if png.reduce_palette() {
something_changed = true; something_changed = true;
if opts.verbosity == Some(1) { if opts.verbosity == Some(1) {
report_reduction(&png); report_reduction(png);
} }
}; };
} }
@ -492,7 +492,7 @@ fn perform_reductions(png: &mut png::PngData, opts: &Options) -> bool {
if png.reduce_bit_depth() { if png.reduce_bit_depth() {
something_changed = true; something_changed = true;
if opts.verbosity == Some(1) { if opts.verbosity == Some(1) {
report_reduction(&png); report_reduction(png);
} }
}; };
} }
@ -501,13 +501,13 @@ fn perform_reductions(png: &mut png::PngData, opts: &Options) -> bool {
if png.reduce_color_type() { if png.reduce_color_type() {
something_changed = true; something_changed = true;
if opts.verbosity == Some(1) { if opts.verbosity == Some(1) {
report_reduction(&png); report_reduction(png);
} }
}; };
} }
if something_changed && opts.verbosity.is_some() { if something_changed && opts.verbosity.is_some() {
report_reduction(&png); report_reduction(png);
} }
if let Some(interlacing) = opts.interlace { if let Some(interlacing) = opts.interlace {

View file

@ -4,7 +4,7 @@ use crc::crc32;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::fmt; use std::fmt;
use std::fs::File; use std::fs::File;
use std::io::{Cursor, Read, Write}; use std::io::{Cursor, Read};
use std::iter::Iterator; use std::iter::Iterator;
use std::path::Path; use std::path::Path;
@ -408,7 +408,7 @@ impl PngData {
for (key, header) in self.aux_headers.iter().filter(|&(ref key, _)| { for (key, header) in self.aux_headers.iter().filter(|&(ref key, _)| {
!(**key == "bKGD" || **key == "hIST" || **key == "tRNS") !(**key == "bKGD" || **key == "hIST" || **key == "tRNS")
}) { }) {
write_png_block(&key.as_bytes(), &header, &mut output); write_png_block(key.as_bytes(), header, &mut output);
} }
// Palette // Palette
if let Some(palette) = self.palette.clone() { if let Some(palette) = self.palette.clone() {
@ -425,7 +425,7 @@ impl PngData {
for (key, header) in self.aux_headers.iter().filter(|&(ref key, _)| { for (key, header) in self.aux_headers.iter().filter(|&(ref key, _)| {
**key == "bKGD" || **key == "hIST" || **key == "tRNS" **key == "bKGD" || **key == "hIST" || **key == "tRNS"
}) { }) {
write_png_block(&key.as_bytes(), &header, &mut output); write_png_block(key.as_bytes(), header, &mut output);
} }
// IDAT data // IDAT data
write_png_block(b"IDAT", &self.idat_data, &mut output); write_png_block(b"IDAT", &self.idat_data, &mut output);
@ -437,7 +437,7 @@ impl PngData {
/// Return an iterator over the scanlines of the image /// Return an iterator over the scanlines of the image
pub fn scan_lines(&self) -> ScanLines { pub fn scan_lines(&self) -> ScanLines {
ScanLines { ScanLines {
png: &self, png: self,
start: 0, start: 0,
end: 0, end: 0,
pass: None, pass: None,
@ -711,14 +711,14 @@ impl PngData {
let lower = *byte & 0b00001111; let lower = *byte & 0b00001111;
let mut new_byte = 0u8; let mut new_byte = 0u8;
if let Some(new_idx) = index_map.get(&upper) { if let Some(new_idx) = index_map.get(&upper) {
new_byte = new_byte & (*new_idx << 4); new_byte &= *new_idx << 4;
} else { } else {
new_byte = new_byte & (upper << 4); new_byte &= upper << 4;
} }
if let Some(new_idx) = index_map.get(&lower) { if let Some(new_idx) = index_map.get(&lower) {
new_byte = new_byte & *new_idx; new_byte &= *new_idx;
} else { } else {
new_byte = new_byte & lower; new_byte &= lower;
} }
new_data.push(new_byte); new_data.push(new_byte);
} }
@ -731,24 +731,24 @@ impl PngData {
let four = *byte & 0b00000011; let four = *byte & 0b00000011;
let mut new_byte = 0u8; let mut new_byte = 0u8;
if let Some(new_idx) = index_map.get(&one) { if let Some(new_idx) = index_map.get(&one) {
new_byte = new_byte & (*new_idx << 6); new_byte &= *new_idx << 6;
} else { } else {
new_byte = new_byte & (one << 6); new_byte &= one << 6;
} }
if let Some(new_idx) = index_map.get(&two) { if let Some(new_idx) = index_map.get(&two) {
new_byte = new_byte & (*new_idx << 4); new_byte &= *new_idx << 4;
} else { } else {
new_byte = new_byte & (two << 4); new_byte &= two << 4;
} }
if let Some(new_idx) = index_map.get(&three) { if let Some(new_idx) = index_map.get(&three) {
new_byte = new_byte & (*new_idx << 2); new_byte &= *new_idx << 2;
} else { } else {
new_byte = new_byte & (three << 2); new_byte &= three << 2;
} }
if let Some(new_idx) = index_map.get(&four) { if let Some(new_idx) = index_map.get(&four) {
new_byte = new_byte & *new_idx; new_byte &= *new_idx;
} else { } else {
new_byte = new_byte & four; new_byte &= four;
} }
new_data.push(new_byte); new_data.push(new_byte);
} }
@ -1401,7 +1401,7 @@ fn reduce_rgb_to_palette(png: &PngData) -> Option<(Vec<u8>, Vec<u8>)> {
let mut color_palette = Vec::with_capacity(palette.len() * 3); let mut color_palette = Vec::with_capacity(palette.len() * 3);
for color in &palette { for color in &palette {
color_palette.extend_from_slice(&color); color_palette.extend_from_slice(color);
} }
Some((reduced, color_palette)) Some((reduced, color_palette))
@ -1453,7 +1453,7 @@ fn reduce_grayscale_to_palette(png: &PngData) -> Option<(Vec<u8>, Vec<u8>)> {
let mut color_palette = Vec::with_capacity(palette.len() * 3); let mut color_palette = Vec::with_capacity(palette.len() * 3);
for color in &palette { for color in &palette {
color_palette.extend_from_slice(&color); color_palette.extend_from_slice(color);
} }
Some((reduced.to_bytes(), color_palette)) Some((reduced.to_bytes(), color_palette))