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

@ -13,9 +13,9 @@ fn interlacing_16_bits(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.change_interlacing(1);
});
let mut safe_png = png.clone();
safe_png.change_interlacing(1);
});
}
#[bench]
@ -24,9 +24,9 @@ fn interlacing_8_bits(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.change_interlacing(1);
});
let mut safe_png = png.clone();
safe_png.change_interlacing(1);
});
}
#[bench]
@ -35,9 +35,9 @@ fn interlacing_4_bits(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.change_interlacing(1);
});
let mut safe_png = png.clone();
safe_png.change_interlacing(1);
});
}
#[bench]
@ -46,9 +46,9 @@ fn interlacing_2_bits(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.change_interlacing(1);
});
let mut safe_png = png.clone();
safe_png.change_interlacing(1);
});
}
#[bench]
@ -57,9 +57,9 @@ fn interlacing_1_bits(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.change_interlacing(1);
});
let mut safe_png = png.clone();
safe_png.change_interlacing(1);
});
}
#[bench]
@ -68,9 +68,9 @@ fn deinterlacing_16_bits(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.change_interlacing(0);
});
let mut safe_png = png.clone();
safe_png.change_interlacing(0);
});
}
#[bench]
@ -79,9 +79,9 @@ fn deinterlacing_8_bits(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.change_interlacing(0);
});
let mut safe_png = png.clone();
safe_png.change_interlacing(0);
});
}
#[bench]
@ -90,9 +90,9 @@ fn deinterlacing_4_bits(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.change_interlacing(0);
});
let mut safe_png = png.clone();
safe_png.change_interlacing(0);
});
}
#[bench]
@ -101,9 +101,9 @@ fn deinterlacing_2_bits(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.change_interlacing(0);
});
let mut safe_png = png.clone();
safe_png.change_interlacing(0);
});
}
#[bench]
@ -112,7 +112,7 @@ fn deinterlacing_1_bits(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.change_interlacing(0);
});
let mut safe_png = png.clone();
safe_png.change_interlacing(0);
});
}

View file

@ -13,9 +13,9 @@ fn reductions_16_to_8_bits(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_bit_depth();
});
let mut safe_png = png.clone();
safe_png.reduce_bit_depth();
});
}
#[bench]
@ -24,9 +24,9 @@ fn reductions_8_to_4_bits(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_bit_depth();
});
let mut safe_png = png.clone();
safe_png.reduce_bit_depth();
});
}
#[bench]
@ -35,9 +35,9 @@ fn reductions_8_to_2_bits(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_bit_depth();
});
let mut safe_png = png.clone();
safe_png.reduce_bit_depth();
});
}
#[bench]
@ -46,9 +46,9 @@ fn reductions_8_to_1_bits(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_bit_depth();
});
let mut safe_png = png.clone();
safe_png.reduce_bit_depth();
});
}
#[bench]
@ -57,9 +57,9 @@ fn reductions_4_to_2_bits(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_bit_depth();
});
let mut safe_png = png.clone();
safe_png.reduce_bit_depth();
});
}
#[bench]
@ -68,9 +68,9 @@ fn reductions_4_to_1_bits(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_bit_depth();
});
let mut safe_png = png.clone();
safe_png.reduce_bit_depth();
});
}
#[bench]
@ -79,9 +79,9 @@ fn reductions_2_to_1_bits(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_bit_depth();
});
let mut safe_png = png.clone();
safe_png.reduce_bit_depth();
});
}
#[bench]
@ -90,9 +90,9 @@ fn reductions_rgba_to_rgb_16(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
}
#[bench]
@ -101,9 +101,9 @@ fn reductions_rgba_to_rgb_8(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
}
#[bench]
@ -112,9 +112,9 @@ fn reductions_rgba_to_grayscale_alpha_16(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
}
#[bench]
@ -123,9 +123,9 @@ fn reductions_rgba_to_grayscale_alpha_8(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
}
#[bench]
@ -134,9 +134,9 @@ fn reductions_rgba_to_grayscale_16(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
}
#[bench]
@ -145,9 +145,9 @@ fn reductions_rgba_to_grayscale_8(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
}
#[bench]
@ -156,9 +156,9 @@ fn reductions_rgb_to_grayscale_16(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
}
#[bench]
@ -167,9 +167,9 @@ fn reductions_rgb_to_grayscale_8(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
}
#[bench]
@ -178,9 +178,9 @@ fn reductions_rgba_to_palette_8(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
}
#[bench]
@ -189,9 +189,9 @@ fn reductions_rgb_to_palette_8(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
let mut safe_png = png.clone();
safe_png.reduce_color_type();
});
}
#[bench]
@ -200,9 +200,9 @@ fn reductions_palette_duplicate_reduction(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_palette();
});
let mut safe_png = png.clone();
safe_png.reduce_palette();
});
}
#[bench]
@ -211,9 +211,9 @@ fn reductions_palette_unused_reduction(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_palette();
});
let mut safe_png = png.clone();
safe_png.reduce_palette();
});
}
#[bench]
@ -222,7 +222,7 @@ fn reductions_palette_full_reduction(b: &mut Bencher) {
let png = png::PngData::new(&input, false).unwrap();
b.iter(|| {
let mut safe_png = png.clone();
safe_png.reduce_palette();
});
let mut safe_png = png.clone();
safe_png.reduce_palette();
});
}

View file

@ -7,33 +7,36 @@ pub fn filter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8]) -> Vec
1 => {
filtered.extend_from_slice(&data[0..bpp]);
filtered.extend(data.iter()
.skip(bpp)
.zip(data.iter())
.map(|(cur, last)| cur.wrapping_sub(*last)));
.skip(bpp)
.zip(data.iter())
.map(|(cur, last)| cur.wrapping_sub(*last)));
}
2 => {
if last_line.is_empty() {
filtered.extend_from_slice(data);
} else {
filtered.extend(data.iter()
.zip(last_line.iter())
.map(|(cur, last)| cur.wrapping_sub(*last)));
filtered.extend(data.iter().zip(last_line.iter()).map(|(cur, last)| {
cur.wrapping_sub(*last)
}));
};
}
3 => {
for (i, byte) in data.iter().enumerate() {
if last_line.is_empty() {
filtered.push(match i.checked_sub(bpp) {
Some(x) => byte.wrapping_sub(data[x] >> 1),
None => *byte,
});
Some(x) => byte.wrapping_sub(data[x] >> 1),
None => *byte,
});
} else {
filtered.push(match i.checked_sub(bpp) {
Some(x) => {
byte.wrapping_sub(((data[x] as u16 + last_line[i] as u16) >> 1) as u8)
}
None => byte.wrapping_sub(last_line[i] >> 1),
});
Some(x) => {
byte.wrapping_sub(((data[x] as u16 +
last_line[i] as u16) >>
1) as
u8)
}
None => byte.wrapping_sub(last_line[i] >> 1),
});
};
}
}
@ -41,16 +44,18 @@ pub fn filter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8]) -> Vec
for (i, byte) in data.iter().enumerate() {
if last_line.is_empty() {
filtered.push(match i.checked_sub(bpp) {
Some(x) => byte.wrapping_sub(data[x]),
None => *byte,
});
Some(x) => byte.wrapping_sub(data[x]),
None => *byte,
});
} else {
filtered.push(match i.checked_sub(bpp) {
Some(x) => {
byte.wrapping_sub(paeth_predictor(data[x], last_line[i], last_line[x]))
}
None => byte.wrapping_sub(last_line[i]),
});
Some(x) => {
byte.wrapping_sub(paeth_predictor(data[x],
last_line[i],
last_line[x]))
}
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() {
unfiltered.extend_from_slice(data);
} else {
unfiltered.extend(data.iter()
.zip(last_line.iter())
.map(|(cur, last)| cur.wrapping_add(*last)));
unfiltered.extend(data.iter().zip(last_line.iter()).map(|(cur, last)| {
cur.wrapping_add(*last)
}));
};
}
3 => {
@ -103,7 +108,9 @@ pub fn unfilter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8]) -> V
match i.checked_sub(bpp) {
Some(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 => {
unfiltered.push(byte.wrapping_add(last_line[i] >> 1));

View file

@ -48,17 +48,21 @@ pub fn parse_next_header(byte_data: &[u8],
fix_errors: bool)
-> Result<Option<(String, Vec<u8>)>, PngError> {
let mut rdr = Cursor::new(byte_data.iter()
.skip(*byte_offset)
.take(4)
.cloned()
.collect::<Vec<u8>>());
.skip(*byte_offset)
.take(4)
.cloned()
.collect::<Vec<u8>>());
let length: u32 = match rdr.read_u32::<BigEndian>() {
Ok(x) => x,
Err(_) => return Err(PngError::new("Invalid data found; unable to read PNG file")),
};
*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()) {
Ok(x) => x,
Err(_) => return Err(PngError::new("Invalid data found; unable to read PNG file")),
@ -76,10 +80,10 @@ pub fn parse_next_header(byte_data: &[u8],
.collect();
*byte_offset += length as usize;
let mut rdr = Cursor::new(byte_data.iter()
.skip(*byte_offset)
.take(4)
.cloned()
.collect::<Vec<u8>>());
.skip(*byte_offset)
.take(4)
.cloned()
.collect::<Vec<u8>>());
let crc: u32 = match rdr.read_u32::<BigEndian>() {
Ok(x) => x,
Err(_) => return Err(PngError::new("Invalid data found; unable to read PNG file")),
@ -97,26 +101,26 @@ pub fn parse_next_header(byte_data: &[u8],
pub fn parse_ihdr_header(byte_data: &[u8]) -> Result<IhdrData, PngError> {
let mut rdr = Cursor::new(&byte_data[0..8]);
Ok(IhdrData {
color_type: match byte_data[9] {
0 => ColorType::Grayscale,
2 => ColorType::RGB,
3 => ColorType::Indexed,
4 => ColorType::GrayscaleAlpha,
6 => ColorType::RGBA,
_ => return Err(PngError::new("Unexpected color type in header")),
},
bit_depth: match byte_data[8] {
1 => BitDepth::One,
2 => BitDepth::Two,
4 => BitDepth::Four,
8 => BitDepth::Eight,
16 => BitDepth::Sixteen,
_ => return Err(PngError::new("Unexpected bit depth in header")),
},
width: rdr.read_u32::<BigEndian>().unwrap(),
height: rdr.read_u32::<BigEndian>().unwrap(),
compression: byte_data[10],
filter: byte_data[11],
interlaced: byte_data[12],
})
color_type: match byte_data[9] {
0 => ColorType::Grayscale,
2 => ColorType::RGB,
3 => ColorType::Indexed,
4 => ColorType::GrayscaleAlpha,
6 => ColorType::RGBA,
_ => return Err(PngError::new("Unexpected color type in header")),
},
bit_depth: match byte_data[8] {
1 => BitDepth::One,
2 => BitDepth::Two,
4 => BitDepth::Four,
8 => BitDepth::Eight,
16 => BitDepth::Sixteen,
_ => return Err(PngError::new("Unexpected bit depth in header")),
},
width: rdr.read_u32::<BigEndian>().unwrap(),
height: rdr.read_u32::<BigEndian>().unwrap(),
compression: byte_data[10],
filter: byte_data[11],
interlaced: byte_data[12],
})
}

View file

@ -101,7 +101,7 @@ pub fn deinterlace_image(png: &mut PngData) {
let bit_vec = BitVec::from_bytes(&line.data);
let bits_in_line = ((png.ihdr_data.width - pass_constants.x_shift as u32) as f32 /
pass_constants.x_step as f32)
.ceil() as usize * bits_per_pixel as usize;
.ceil() as usize * bits_per_pixel as usize;
for (i, bit) in bit_vec.iter().enumerate() {
// Avoid moving padded 0's into new image
if i >= bits_in_line {

View file

@ -346,16 +346,14 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), PngError> {
{
match out_file.metadata() {
Ok(out_meta) => {
let readonly = metadata.permissions()
.readonly();
out_meta.permissions()
.set_readonly(readonly);
let readonly = metadata.permissions().readonly();
out_meta.permissions().set_readonly(readonly);
}
Err(_) => {
if opts.verbosity.is_some() {
writeln!(&mut stderr(),
"Failed to set permissions on output file")
.ok();
.ok();
}
}
}
@ -365,7 +363,7 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), PngError> {
if opts.verbosity.is_some() {
writeln!(&mut stderr(),
"Failed to read permissions on input file")
.ok();
.ok();
}
}
}
@ -438,29 +436,29 @@ fn optimize_png(png: &mut PngData,
" {}x{} pixels, PNG format",
png.ihdr_data.width,
png.ihdr_data.height)
.ok();
.ok();
if let Some(ref palette) = png.palette {
writeln!(&mut stderr(),
" {} bits/pixel, {} colors in palette",
png.ihdr_data.bit_depth,
palette.len() / 3)
.ok();
.ok();
} else {
writeln!(&mut stderr(),
" {}x{} bits/pixel, {:?}",
png.channels_per_pixel(),
png.ihdr_data.bit_depth,
png.ihdr_data.color_type)
.ok();
.ok();
}
writeln!(&mut stderr(),
" IDAT size = {} bytes",
idat_original_size)
.ok();
.ok();
writeln!(&mut stderr(),
" File size = {} bytes",
file_original_size)
.ok();
.ok();
}
let mut filter = opts.filter.clone();
@ -528,35 +526,36 @@ fn optimize_png(png: &mut PngData,
let original_len = original_png.idat_data.len();
let added_interlacing = opts.interlace == Some(1) && original_png.ihdr_data.interlaced == 0;
let best: Option<TrialWithData> = results.into_par_iter()
.weight_max()
.filter_map(|trial| {
let filtered = &filters[&trial.0];
let new_idat = if opts.deflate == Deflaters::Zlib {
deflate::deflate(filtered, trial.1, trial.2, trial.3, opts.window)
} else {
deflate::zopfli_deflate(filtered)
let best: Option<TrialWithData> =
results.into_par_iter()
.weight_max()
.filter_map(|trial| {
let filtered = &filters[&trial.0];
let new_idat = if opts.deflate == Deflaters::Zlib {
deflate::deflate(filtered, trial.1, trial.2, trial.3, opts.window)
} else {
deflate::zopfli_deflate(filtered)
}
.unwrap();
if opts.verbosity == Some(1) {
writeln!(&mut stderr(),
" zc = {} zm = {} zs = {} f = {} {} bytes",
trial.1,
trial.2,
trial.3,
trial.0,
new_idat.len())
.ok();
}
.unwrap();
if opts.verbosity == Some(1) {
writeln!(&mut stderr(),
" zc = {} zm = {} zs = {} f = {} {} bytes",
trial.1,
trial.2,
trial.3,
trial.0,
new_idat.len())
.ok();
}
if new_idat.len() < original_len || added_interlacing || opts.force {
Some((trial.0, trial.1, trial.2, trial.3, new_idat))
} else {
None
}
})
.reduce_with(|i, j| if i.4.len() <= j.4.len() { i } else { j });
if new_idat.len() < original_len || added_interlacing || opts.force {
Some((trial.0, trial.1, trial.2, trial.3, new_idat))
} else {
None
}
})
.reduce_with(|i, j| if i.4.len() <= j.4.len() { i } else { j });
if let Some(better) = best {
png.idat_data = better.4;
@ -569,7 +568,7 @@ fn optimize_png(png: &mut PngData,
better.3,
better.0,
png.idat_data.len())
.ok();
.ok();
}
} else if reduction_occurred {
png.reset_from_original(original_png);
@ -586,13 +585,13 @@ fn optimize_png(png: &mut PngData,
" IDAT size = {} bytes ({} bytes decrease)",
png.idat_data.len(),
idat_original_size - png.idat_data.len())
.ok();
.ok();
} else {
writeln!(&mut stderr(),
" IDAT size = {} bytes ({} bytes increase)",
png.idat_data.len(),
png.idat_data.len() - idat_original_size)
.ok();
.ok();
}
if file_original_size >= output.len() {
writeln!(&mut stderr(),
@ -601,7 +600,7 @@ fn optimize_png(png: &mut PngData,
file_original_size - output.len(),
(file_original_size - output.len()) as f64 / file_original_size as f64 *
100f64)
.ok();
.ok();
} else {
writeln!(&mut stderr(),
" file size = {} bytes ({} bytes = {:.2}% increase)",
@ -609,7 +608,7 @@ fn optimize_png(png: &mut PngData,
output.len() - file_original_size,
(output.len() - file_original_size) as f64 / file_original_size as f64 *
100f64)
.ok();
.ok();
}
}
@ -682,14 +681,14 @@ fn report_reduction(png: &png::PngData) {
"Reducing image to {} bits/pixel, {} colors in palette",
png.ihdr_data.bit_depth,
palette.len() / 3)
.ok();
.ok();
} else {
writeln!(&mut stderr(),
"Reducing image to {}x{} bits/pixel, {}",
png.channels_per_pixel(),
png.ihdr_data.bit_depth,
png.ihdr_data.color_type)
.ok();
.ok();
}
}
@ -706,7 +705,10 @@ fn perform_strip(png: &mut png::PngData, opts: &Options) {
Headers::Safe => {
const PRESERVED_HEADERS: [&'static str; 9] = ["cHRM", "gAMA", "iCCP", "sBIT", "sRGB",
"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 {
if !PRESERVED_HEADERS.contains(&hdr.as_ref()) {
png.aux_headers.remove(&hdr);

View file

@ -239,13 +239,16 @@ fn handle_optimization(inputs: Vec<PathBuf>, opts: &Options) {
let mut current_opts = opts.clone();
if input.is_dir() {
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)
} else {
writeln!(&mut stderr(),
"{} is a directory, skipping",
input.display())
.ok();
.ok();
}
continue;
}
@ -310,9 +313,7 @@ fn parse_opts_into_struct(matches: &ArgMatches) -> Result<Options, String> {
if let Some(x) = matches.value_of("output_dir") {
let path = PathBuf::from(x);
if !path.exists() {
match DirBuilder::new()
.recursive(true)
.create(&path) {
match DirBuilder::new().recursive(true).create(&path) {
Ok(_) => (),
Err(x) => return Err(format!("Could not create output directory {}", x)),
};
@ -394,7 +395,7 @@ fn parse_opts_into_struct(matches: &ArgMatches) -> Result<Options, String> {
if hdrs.contains(&"safe".to_owned()) || hdrs.contains(&"all".to_owned()) {
if hdrs.len() > 1 {
return Err("'safe' or 'all' presets for --strip should be used by themselves"
.to_owned());
.to_owned());
}
if hdrs[0] == "safe" {
opts.strip = Headers::Safe;
@ -437,8 +438,8 @@ fn parse_numeric_range_opts(input: &str,
max_value,
min_value,
max_value)
.as_ref())
.unwrap();
.as_ref())
.unwrap();
let mut items = HashSet::new();
if one_item.is_match(input) {

View file

@ -50,7 +50,10 @@ impl<'a> Iterator for ScanLines<'a> {
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;
let y_steps;
let pixels_factor;
@ -129,23 +132,26 @@ impl<'a> Iterator for ScanLines<'a> {
}
}
Some(ScanLine {
filter: self.png.raw_data[self.start],
data: self.png.raw_data[(self.start + 1)..self.end].to_owned(),
pass: current_pass,
})
filter: self.png.raw_data[self.start],
data: self.png.raw_data[(self.start + 1)..self.end].to_owned(),
pass: current_pass,
})
} else {
// Standard, non-interlaced PNG scanlines
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;
let bytes_per_line = (bits_per_line as f32 / 8f32).ceil() as usize;
self.start = self.end;
self.end = self.start + bytes_per_line + 1;
Some(ScanLine {
filter: self.png.raw_data[self.start],
data: self.png.raw_data[(self.start + 1)..self.end].to_owned(),
pass: None,
})
filter: self.png.raw_data[self.start],
data: self.png.raw_data[(self.start + 1)..self.end].to_owned(),
pass: None,
})
}
}
}
@ -208,7 +214,10 @@ impl PngData {
pub fn from_slice(byte_data: &[u8], fix_errors: bool) -> Result<PngData, PngError> {
let mut byte_offset: usize = 0;
// 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()) {
return Err(PngError::new("Invalid PNG header detected"));
}
@ -316,9 +325,11 @@ impl PngData {
ihdr_data.write_u8(self.ihdr_data.interlaced).ok();
write_png_block(b"IHDR", &ihdr_data, &mut output);
// Ancillary headers
for (key, header) in self.aux_headers
.iter()
.filter(|&(key, _)| !(*key == "bKGD" || *key == "hIST" || *key == "tRNS")) {
for (key, header) in self.aux_headers.iter().filter(|&(key, _)| {
!(*key == "bKGD" ||
*key == "hIST" ||
*key == "tRNS")
}) {
write_png_block(key.as_bytes(), header, &mut output);
}
// Palette
@ -333,9 +344,10 @@ impl PngData {
write_png_block(b"tRNS", transparency_pixel, &mut output);
}
// Special ancillary headers that need to come after PLTE but before IDAT
for (key, header) in self.aux_headers
.iter()
.filter(|&(key, _)| *key == "bKGD" || *key == "hIST" || *key == "tRNS") {
for (key, header) in self.aux_headers.iter().filter(|&(key, _)| {
*key == "bKGD" || *key == "hIST" ||
*key == "tRNS"
}) {
write_png_block(key.as_bytes(), header, &mut output);
}
// IDAT data
@ -362,7 +374,7 @@ impl PngData {
let mut unfiltered = Vec::with_capacity(self.raw_data.len());
let bpp = (((self.ihdr_data.bit_depth.as_u8() * self.channels_per_pixel()) as f32) /
8f32)
.ceil() as usize;
.ceil() as usize;
let mut last_line: Vec<u8> = Vec::new();
for line in self.scan_lines() {
let unfiltered_line = unfilter_line(line.filter, bpp, &line.data, &last_line);
@ -384,7 +396,7 @@ impl PngData {
let mut filtered = Vec::with_capacity(self.raw_data.len());
let bpp = (((self.ihdr_data.bit_depth.as_u8() * self.channels_per_pixel()) as f32) /
8f32)
.ceil() as usize;
.ceil() as usize;
let mut last_line: Vec<u8> = Vec::new();
let mut last_pass: Option<u8> = None;
for line in self.scan_lines() {
@ -413,11 +425,11 @@ impl PngData {
}
let (best_filter, best_line) = trials.iter()
.min_by_key(|x| {
x.1.iter().fold(0u64, |acc, &x| {
x.1.iter().fold(0u64, |acc, &x| {
let signed = x as i8;
acc + (signed as i16).abs() as u64
})
})
})
.unwrap();
filtered.push(*best_filter);
filtered.extend_from_slice(best_line);
@ -444,8 +456,8 @@ impl PngData {
// Reduce from 16 to 8 bits per channel per pixel
let mut reduced = Vec::with_capacity((self.ihdr_data.width * self.ihdr_data.height *
self.channels_per_pixel() as u32 +
self.ihdr_data
.height) as usize);
self.ihdr_data.height) as
usize);
let mut high_byte = 0;
for line in self.scan_lines() {
@ -491,21 +503,21 @@ impl PngData {
.chunks(3)
.zip(trns.iter().chain([255].iter().cycle()))
.flat_map(|(pixel, trns)| {
let mut pixel = pixel.to_owned();
pixel.push(*trns);
pixel
})
let mut pixel = pixel.to_owned();
pixel.push(*trns);
pixel
})
.collect()
} else {
self.palette.clone().unwrap()
};
let mut indexed_palette: Vec<&[u8]> =
palette.chunks(if self.transparency_palette.is_some() {
4
} else {
3
})
.collect();
let mut indexed_palette: Vec<&[u8]> = palette.chunks(if self.transparency_palette
.is_some() {
4
} else {
3
})
.collect();
// A map of old indexes to new ones, for any moved
let mut index_map: HashMap<u8, u8> = HashMap::new();

View file

@ -136,11 +136,11 @@ pub fn reduce_rgba_to_palette(png: &mut PngData) -> bool {
let mut color_palette = Vec::with_capacity(palette.len() * 3 +
if png.aux_headers
.contains_key(&"bKGD".to_string()) {
6
} else {
0
});
.contains_key(&"bKGD".to_string()) {
6
} else {
0
});
let mut trans_palette = Vec::with_capacity(palette.len());
for color in &palette {
for (i, byte) in color.iter().enumerate() {
@ -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()) {
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()) {
*bkgd_header = vec![entry as u8];
} 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()) {
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()) {
*bkgd_header = vec![entry as u8];
} else if color_palette.len() == 255 {
@ -269,9 +277,9 @@ pub fn reduce_rgb_to_grayscale(png: &mut PngData) -> bool {
.step(2)
.cloned()
.zip(cur_pixel.iter()
.skip(1)
.step(2)
.cloned())
.skip(1)
.step(2)
.cloned())
.unique()
.collect::<Vec<(u8, u8)>>();
if pixel_bytes.len() > 1 {