Run rustfmt since it updated to v0.8.0.

I don't really like the new changes, but we might as well follow along.
This commit is contained in:
Josh Holmer 2017-03-08 11:14:33 -05:00
parent d665c61645
commit 84cf6b09c2
9 changed files with 280 additions and 246 deletions

View file

@ -15,9 +15,9 @@ pub fn filter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8]) -> Vec
if last_line.is_empty() { if last_line.is_empty() {
filtered.extend_from_slice(data); filtered.extend_from_slice(data);
} else { } else {
filtered.extend(data.iter() filtered.extend(data.iter().zip(last_line.iter()).map(|(cur, last)| {
.zip(last_line.iter()) cur.wrapping_sub(*last)
.map(|(cur, last)| cur.wrapping_sub(*last))); }));
}; };
} }
3 => { 3 => {
@ -30,7 +30,10 @@ pub fn filter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8]) -> Vec
} else { } else {
filtered.push(match i.checked_sub(bpp) { filtered.push(match i.checked_sub(bpp) {
Some(x) => { Some(x) => {
byte.wrapping_sub(((data[x] as u16 + last_line[i] as u16) >> 1) as u8) byte.wrapping_sub(((data[x] as u16 +
last_line[i] as u16) >>
1) as
u8)
} }
None => byte.wrapping_sub(last_line[i] >> 1), None => byte.wrapping_sub(last_line[i] >> 1),
}); });
@ -47,7 +50,9 @@ pub fn filter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8]) -> Vec
} else { } else {
filtered.push(match i.checked_sub(bpp) { filtered.push(match i.checked_sub(bpp) {
Some(x) => { Some(x) => {
byte.wrapping_sub(paeth_predictor(data[x], last_line[i], last_line[x])) byte.wrapping_sub(paeth_predictor(data[x],
last_line[i],
last_line[x]))
} }
None => byte.wrapping_sub(last_line[i]), None => byte.wrapping_sub(last_line[i]),
}); });
@ -82,9 +87,9 @@ pub fn unfilter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8]) -> V
if last_line.is_empty() { if last_line.is_empty() {
unfiltered.extend_from_slice(data); unfiltered.extend_from_slice(data);
} else { } else {
unfiltered.extend(data.iter() unfiltered.extend(data.iter().zip(last_line.iter()).map(|(cur, last)| {
.zip(last_line.iter()) cur.wrapping_add(*last)
.map(|(cur, last)| cur.wrapping_add(*last))); }));
}; };
} }
3 => { 3 => {
@ -103,7 +108,9 @@ pub fn unfilter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8]) -> V
match i.checked_sub(bpp) { match i.checked_sub(bpp) {
Some(x) => { Some(x) => {
let b = unfiltered[x]; let b = unfiltered[x];
unfiltered.push(byte.wrapping_add(((b as u16 + last_line[i] as u16) >> 1) as u8)); unfiltered.push(byte.wrapping_add(((b as u16 + last_line[i] as u16) >>
1) as
u8));
} }
None => { None => {
unfiltered.push(byte.wrapping_add(last_line[i] >> 1)); unfiltered.push(byte.wrapping_add(last_line[i] >> 1));

View file

@ -58,7 +58,11 @@ pub fn parse_next_header(byte_data: &[u8],
}; };
*byte_offset += 4; *byte_offset += 4;
let mut header_bytes: Vec<u8> = byte_data.iter().skip(*byte_offset).take(4).cloned().collect(); let mut header_bytes: Vec<u8> = byte_data.iter()
.skip(*byte_offset)
.take(4)
.cloned()
.collect();
let header = match String::from_utf8(header_bytes.clone()) { let header = match String::from_utf8(header_bytes.clone()) {
Ok(x) => x, Ok(x) => x,
Err(_) => return Err(PngError::new("Invalid data found; unable to read PNG file")), Err(_) => return Err(PngError::new("Invalid data found; unable to read PNG file")),

View file

@ -346,10 +346,8 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), PngError> {
{ {
match out_file.metadata() { match out_file.metadata() {
Ok(out_meta) => { Ok(out_meta) => {
let readonly = metadata.permissions() let readonly = metadata.permissions().readonly();
.readonly(); out_meta.permissions().set_readonly(readonly);
out_meta.permissions()
.set_readonly(readonly);
} }
Err(_) => { Err(_) => {
if opts.verbosity.is_some() { if opts.verbosity.is_some() {
@ -528,7 +526,8 @@ fn optimize_png(png: &mut PngData,
let original_len = original_png.idat_data.len(); let original_len = original_png.idat_data.len();
let added_interlacing = opts.interlace == Some(1) && original_png.ihdr_data.interlaced == 0; let added_interlacing = opts.interlace == Some(1) && original_png.ihdr_data.interlaced == 0;
let best: Option<TrialWithData> = results.into_par_iter() let best: Option<TrialWithData> =
results.into_par_iter()
.weight_max() .weight_max()
.filter_map(|trial| { .filter_map(|trial| {
let filtered = &filters[&trial.0]; let filtered = &filters[&trial.0];
@ -706,7 +705,10 @@ fn perform_strip(png: &mut png::PngData, opts: &Options) {
Headers::Safe => { Headers::Safe => {
const PRESERVED_HEADERS: [&'static str; 9] = ["cHRM", "gAMA", "iCCP", "sBIT", "sRGB", const PRESERVED_HEADERS: [&'static str; 9] = ["cHRM", "gAMA", "iCCP", "sBIT", "sRGB",
"bKGD", "hIST", "pHYs", "sPLT"]; "bKGD", "hIST", "pHYs", "sPLT"];
let hdrs = png.aux_headers.keys().cloned().collect::<Vec<String>>(); let hdrs = png.aux_headers
.keys()
.cloned()
.collect::<Vec<String>>();
for hdr in hdrs { for hdr in hdrs {
if !PRESERVED_HEADERS.contains(&hdr.as_ref()) { if !PRESERVED_HEADERS.contains(&hdr.as_ref()) {
png.aux_headers.remove(&hdr); png.aux_headers.remove(&hdr);

View file

@ -239,7 +239,10 @@ fn handle_optimization(inputs: Vec<PathBuf>, opts: &Options) {
let mut current_opts = opts.clone(); let mut current_opts = opts.clone();
if input.is_dir() { if input.is_dir() {
if current_opts.recursive { if current_opts.recursive {
handle_optimization(input.read_dir().unwrap().map(|x| x.unwrap().path()).collect(), handle_optimization(input.read_dir()
.unwrap()
.map(|x| x.unwrap().path())
.collect(),
&current_opts) &current_opts)
} else { } else {
writeln!(&mut stderr(), writeln!(&mut stderr(),
@ -310,9 +313,7 @@ fn parse_opts_into_struct(matches: &ArgMatches) -> Result<Options, String> {
if let Some(x) = matches.value_of("output_dir") { if let Some(x) = matches.value_of("output_dir") {
let path = PathBuf::from(x); let path = PathBuf::from(x);
if !path.exists() { if !path.exists() {
match DirBuilder::new() match DirBuilder::new().recursive(true).create(&path) {
.recursive(true)
.create(&path) {
Ok(_) => (), Ok(_) => (),
Err(x) => return Err(format!("Could not create output directory {}", x)), Err(x) => return Err(format!("Could not create output directory {}", x)),
}; };

View file

@ -50,7 +50,10 @@ impl<'a> Iterator for ScanLines<'a> {
pass.1 = 0; pass.1 = 0;
} }
} }
let bits_per_pixel = self.png.ihdr_data.bit_depth.as_u8() as u32 * let bits_per_pixel = self.png
.ihdr_data
.bit_depth
.as_u8() as u32 *
self.png.channels_per_pixel() as u32; self.png.channels_per_pixel() as u32;
let y_steps; let y_steps;
let pixels_factor; let pixels_factor;
@ -136,7 +139,10 @@ impl<'a> Iterator for ScanLines<'a> {
} else { } else {
// Standard, non-interlaced PNG scanlines // Standard, non-interlaced PNG scanlines
let bits_per_line = self.png.ihdr_data.width as usize * let bits_per_line = self.png.ihdr_data.width as usize *
self.png.ihdr_data.bit_depth.as_u8() as usize * self.png
.ihdr_data
.bit_depth
.as_u8() as usize *
self.png.channels_per_pixel() as usize; self.png.channels_per_pixel() as usize;
let bytes_per_line = (bits_per_line as f32 / 8f32).ceil() as usize; let bytes_per_line = (bits_per_line as f32 / 8f32).ceil() as usize;
self.start = self.end; self.start = self.end;
@ -208,7 +214,10 @@ impl PngData {
pub fn from_slice(byte_data: &[u8], fix_errors: bool) -> Result<PngData, PngError> { pub fn from_slice(byte_data: &[u8], fix_errors: bool) -> Result<PngData, PngError> {
let mut byte_offset: usize = 0; let mut byte_offset: usize = 0;
// Test that png header is valid // Test that png header is valid
let header: Vec<u8> = byte_data.iter().take(8).cloned().collect(); let header: Vec<u8> = byte_data.iter()
.take(8)
.cloned()
.collect();
if !file_header_is_valid(header.as_ref()) { if !file_header_is_valid(header.as_ref()) {
return Err(PngError::new("Invalid PNG header detected")); return Err(PngError::new("Invalid PNG header detected"));
} }
@ -316,9 +325,11 @@ impl PngData {
ihdr_data.write_u8(self.ihdr_data.interlaced).ok(); ihdr_data.write_u8(self.ihdr_data.interlaced).ok();
write_png_block(b"IHDR", &ihdr_data, &mut output); write_png_block(b"IHDR", &ihdr_data, &mut output);
// Ancillary headers // Ancillary headers
for (key, header) in self.aux_headers for (key, header) in self.aux_headers.iter().filter(|&(key, _)| {
.iter() !(*key == "bKGD" ||
.filter(|&(key, _)| !(*key == "bKGD" || *key == "hIST" || *key == "tRNS")) { *key == "hIST" ||
*key == "tRNS")
}) {
write_png_block(key.as_bytes(), header, &mut output); write_png_block(key.as_bytes(), header, &mut output);
} }
// Palette // Palette
@ -333,9 +344,10 @@ impl PngData {
write_png_block(b"tRNS", transparency_pixel, &mut output); write_png_block(b"tRNS", transparency_pixel, &mut output);
} }
// Special ancillary headers that need to come after PLTE but before IDAT // Special ancillary headers that need to come after PLTE but before IDAT
for (key, header) in self.aux_headers for (key, header) in self.aux_headers.iter().filter(|&(key, _)| {
.iter() *key == "bKGD" || *key == "hIST" ||
.filter(|&(key, _)| *key == "bKGD" || *key == "hIST" || *key == "tRNS") { *key == "tRNS"
}) {
write_png_block(key.as_bytes(), header, &mut output); write_png_block(key.as_bytes(), header, &mut output);
} }
// IDAT data // IDAT data
@ -444,8 +456,8 @@ impl PngData {
// Reduce from 16 to 8 bits per channel per pixel // Reduce from 16 to 8 bits per channel per pixel
let mut reduced = Vec::with_capacity((self.ihdr_data.width * self.ihdr_data.height * let mut reduced = Vec::with_capacity((self.ihdr_data.width * self.ihdr_data.height *
self.channels_per_pixel() as u32 + self.channels_per_pixel() as u32 +
self.ihdr_data self.ihdr_data.height) as
.height) as usize); usize);
let mut high_byte = 0; let mut high_byte = 0;
for line in self.scan_lines() { for line in self.scan_lines() {
@ -499,8 +511,8 @@ impl PngData {
} else { } else {
self.palette.clone().unwrap() self.palette.clone().unwrap()
}; };
let mut indexed_palette: Vec<&[u8]> = let mut indexed_palette: Vec<&[u8]> = palette.chunks(if self.transparency_palette
palette.chunks(if self.transparency_palette.is_some() { .is_some() {
4 4
} else { } else {
3 3

View file

@ -160,7 +160,11 @@ pub fn reduce_rgba_to_palette(png: &mut PngData) -> bool {
if let Some(bkgd_header) = png.aux_headers.get_mut(&"bKGD".to_string()) { if let Some(bkgd_header) = png.aux_headers.get_mut(&"bKGD".to_string()) {
assert_eq!(bkgd_header.len(), 6); assert_eq!(bkgd_header.len(), 6);
let header_pixels = bkgd_header.iter().skip(1).step(2).cloned().collect::<Vec<u8>>(); let header_pixels = bkgd_header.iter()
.skip(1)
.step(2)
.cloned()
.collect::<Vec<u8>>();
if let Some(entry) = color_palette.chunks(3).position(|x| x == header_pixels.as_slice()) { if let Some(entry) = color_palette.chunks(3).position(|x| x == header_pixels.as_slice()) {
*bkgd_header = vec![entry as u8]; *bkgd_header = vec![entry as u8];
} else if color_palette.len() / 3 == 256 { } else if color_palette.len() / 3 == 256 {
@ -231,7 +235,11 @@ pub fn reduce_rgb_to_palette(png: &mut PngData) -> bool {
if let Some(bkgd_header) = png.aux_headers.get_mut(&"bKGD".to_string()) { if let Some(bkgd_header) = png.aux_headers.get_mut(&"bKGD".to_string()) {
assert_eq!(bkgd_header.len(), 6); assert_eq!(bkgd_header.len(), 6);
let header_pixels = bkgd_header.iter().skip(1).step(2).cloned().collect::<Vec<u8>>(); let header_pixels = bkgd_header.iter()
.skip(1)
.step(2)
.cloned()
.collect::<Vec<u8>>();
if let Some(entry) = color_palette.chunks(3).position(|x| x == header_pixels.as_slice()) { if let Some(entry) = color_palette.chunks(3).position(|x| x == header_pixels.as_slice()) {
*bkgd_header = vec![entry as u8]; *bkgd_header = vec![entry as u8];
} else if color_palette.len() == 255 { } else if color_palette.len() == 255 {