Clippy lints (#401)

This commit is contained in:
Kornel 2021-05-19 15:04:31 +01:00 committed by GitHub
parent ef3bb7444b
commit 14867c7abc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 49 additions and 52 deletions

View file

@ -101,6 +101,10 @@ impl BitDepth {
}
}
/// Parse a number of bits per channel per pixel into a `BitDepth`
///
/// # Panics
///
/// If depth is unsupported
#[inline]
pub fn from_u8(depth: u8) -> BitDepth {
match depth {

View file

@ -14,15 +14,11 @@ pub enum PngError {
Other(Box<str>),
}
impl Error for PngError {
// deprecated
fn description(&self) -> &str {
""
}
}
impl Error for PngError {}
impl fmt::Display for PngError {
#[inline]
#[cold]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
PngError::DeflatedDataTooLong(_) => f.write_str("deflated data too long"),
@ -40,7 +36,7 @@ impl fmt::Display for PngError {
}
impl PngError {
#[inline]
#[cold]
pub fn new(description: &str) -> PngError {
PngError::Other(description.into())
}

View file

@ -27,11 +27,14 @@ pub struct IhdrData {
impl IhdrData {
/// Bits per pixel
#[must_use]
#[inline]
pub fn bpp(&self) -> u8 {
self.bit_depth.as_u8() * self.color_type.channels_per_pixel()
}
/// Byte length of IDAT that is correct for this IHDR
#[must_use]
pub fn raw_data_size(&self) -> usize {
let w = self.width as usize;
let h = self.height as usize;
@ -134,7 +137,7 @@ pub fn parse_next_header<'a>(
)));
}
let mut name = [0u8; 4];
let mut name = [0_u8; 4];
name.copy_from_slice(chunk_name);
Ok(Some(RawHeader { name, data }))
}
@ -160,8 +163,12 @@ pub fn parse_ihdr_header(byte_data: &[u8]) -> PngResult<IhdrData> {
16 => BitDepth::Sixteen,
_ => return Err(PngError::new("Unexpected bit depth in header")),
},
width: rdr.read_u32::<BigEndian>().unwrap(),
height: rdr.read_u32::<BigEndian>().unwrap(),
width: rdr
.read_u32::<BigEndian>()
.map_err(|_| PngError::TruncatedData)?,
height: rdr
.read_u32::<BigEndian>()
.map_err(|_| PngError::TruncatedData)?,
compression: byte_data[10],
filter: byte_data[11],
interlaced,

View file

@ -12,6 +12,7 @@
#![warn(clippy::path_buf_push_overwrite)]
#![warn(clippy::range_plus_one)]
#![allow(clippy::cognitive_complexity)]
#![allow(clippy::upper_case_acronyms)]
#![cfg_attr(
not(any(feature = "libdeflater", feature = "zopfli")),
allow(irrefutable_let_patterns),
@ -97,7 +98,7 @@ impl InFile {
pub fn path(&self) -> Option<&Path> {
match *self {
InFile::Path(ref p) => Some(p.as_path()),
_ => None,
InFile::StdIn => None,
}
}
}
@ -589,7 +590,7 @@ fn optimize_png(
);
return None;
}
_ => return None,
Err(_) => return None,
};
// update best size across all threads
@ -655,14 +656,14 @@ fn optimize_png(
" file size = {} bytes ({} bytes = {:.2}% decrease)",
output.len(),
file_original_size - output.len(),
(file_original_size - output.len()) as f64 / file_original_size as f64 * 100f64
(file_original_size - output.len()) as f64 / file_original_size as f64 * 100_f64
);
} else {
info!(
" file size = {} bytes ({} bytes = {:.2}% increase)",
output.len(),
output.len() - file_original_size,
(output.len() - file_original_size) as f64 / file_original_size as f64 * 100f64
(output.len() - file_original_size) as f64 / file_original_size as f64 * 100_f64
);
}

View file

@ -208,10 +208,10 @@ impl PngData {
.fold(
0,
|prev, (index, px)| {
if px.a != 255 {
index + 1
} else {
if px.a == 255 {
prev
} else {
index + 1
}
},
);
@ -346,7 +346,7 @@ impl PngImage {
// Avoid vertical filtering on first line of each interlacing pass
for filter in if last_pass == line.pass { 0..5 } else { 0..2 } {
filter_line(filter, bpp, &line.data, last_line, &mut f_buf);
let size = f_buf.iter().fold(0u64, |acc, &x| {
let size = f_buf.iter().fold(0_u64, |acc, &x| {
let signed = x as i8;
acc + i16::from(signed).abs() as u64
});

View file

@ -1,7 +1,7 @@
use crate::png::PngImage;
#[derive(Debug, Clone)]
/// An iterator over the scan lines of a PNG image
#[derive(Debug, Clone)]
pub struct ScanLines<'a> {
iter: ScanLineRanges,
/// A reference to the PNG image being iterated upon

View file

@ -10,11 +10,7 @@ pub trait ParallelIterator: Iterator + Sized {
where
OP: Fn(Self::Item, Self::Item) -> Self::Item + Sync,
{
if let Some(a) = self.next() {
Some(self.fold(a, op))
} else {
None
}
self.next().map(|a| self.fold(a, op))
}
}

View file

@ -123,9 +123,8 @@ pub fn reduce_bit_depth_8_or_less(png: &PngImage, mut minimum_bits: usize) -> Op
};
if permutations.iter().any(|perm| *perm == byte) {
break;
} else {
minimum_bits <<= 1;
}
minimum_bits <<= 1;
}
}
}

View file

@ -150,7 +150,7 @@ pub fn reduced_color_to_palette(png: &PngImage) -> Option<PngImage> {
}
})
.max();
let trns_size = num_transparent.map(|n| n + 8).unwrap_or(0);
let trns_size = num_transparent.map_or(0, |n| n + 8);
let headers_size = palette.len() * 3 + 8 + trns_size;
if raw_data.len() + headers_size > png.data.len() {

View file

@ -6,9 +6,9 @@ use rgb::RGBA8;
use std::borrow::Cow;
pub mod alpha;
use crate::alpha::*;
use crate::alpha::reduced_alpha_channel;
pub mod bit_depth;
use crate::bit_depth::*;
use crate::bit_depth::reduce_bit_depth_8_or_less;
pub mod color;
use crate::color::*;
@ -17,7 +17,6 @@ pub(crate) use crate::bit_depth::reduce_bit_depth;
/// Attempt to reduce the number of colors in the palette
/// Returns `None` if palette hasn't changed
#[must_use]
pub fn reduced_palette(png: &PngImage) -> Option<PngImage> {
if png.ihdr.color_type != ColorType::Indexed {
// Can't reduce if there is no palette
@ -70,14 +69,14 @@ pub fn reduced_palette(png: &PngImage) -> Option<PngImage> {
.unwrap_or_else(|| RGBA8::new(0, 0, 0, 255));
((color.a as i32) << 18)
// These are coefficients for standard sRGB to luma conversion
- (color.r as i32) * 299
- (color.g as i32) * 587
- (color.b as i32) * 114
- i32::from(color.r) * 299
- i32::from(color.g) * 587
- i32::from(color.b) * 114
};
color_val(a.0).cmp(&color_val(b.0))
});
let mut next_index = 0u16;
let mut next_index = 0_u16;
let mut seen = IndexMap::with_capacity(palette.len());
for (i, used) in used_enumerated.iter().cloned() {
if !used {
@ -138,33 +137,33 @@ fn do_palette_reduction(png: &PngImage, palette_map: &[Option<u8>; 256]) -> Opti
}
fn palette_map_to_byte_map(png: &PngImage, palette_map: &[Option<u8>; 256]) -> Option<[u8; 256]> {
let len = png.palette.as_ref().map(|p| p.len()).unwrap_or(0);
let len = png.palette.as_ref().map_or(0, |p| p.len());
if (0..len).all(|i| palette_map[i].map_or(true, |to| to == i as u8)) {
// No reduction necessary
return None;
}
let mut byte_map = [0u8; 256];
let mut byte_map = [0_u8; 256];
// low bit-depths can be pre-computed for every byte value
match png.ihdr.bit_depth {
BitDepth::Eight => {
for byte in 0..=255 {
byte_map[byte as usize] = palette_map[byte as usize].unwrap_or(0)
for byte in 0..=255usize {
byte_map[byte] = palette_map[byte].unwrap_or(0)
}
}
BitDepth::Four => {
for byte in 0..=255 {
byte_map[byte as usize] = palette_map[(byte & 0x0F) as usize].unwrap_or(0)
| (palette_map[(byte >> 4) as usize].unwrap_or(0) << 4);
for byte in 0..=255usize {
byte_map[byte] = palette_map[(byte & 0x0F)].unwrap_or(0)
| (palette_map[(byte >> 4)].unwrap_or(0) << 4);
}
}
BitDepth::Two => {
for byte in 0..=255 {
byte_map[byte as usize] = palette_map[(byte & 0x03) as usize].unwrap_or(0)
| (palette_map[((byte >> 2) & 0x03) as usize].unwrap_or(0) << 2)
| (palette_map[((byte >> 4) & 0x03) as usize].unwrap_or(0) << 4)
| (palette_map[(byte >> 6) as usize].unwrap_or(0) << 6);
for byte in 0..=255usize {
byte_map[byte] = palette_map[(byte & 0x03)].unwrap_or(0)
| (palette_map[((byte >> 2) & 0x03)].unwrap_or(0) << 2)
| (palette_map[((byte >> 4) & 0x03)].unwrap_or(0) << 4)
| (palette_map[(byte >> 6)].unwrap_or(0) << 6);
}
}
_ => {}
@ -174,12 +173,7 @@ fn palette_map_to_byte_map(png: &PngImage, palette_map: &[Option<u8>; 256]) -> O
}
fn reordered_palette(palette: &[RGBA8], palette_map: &[Option<u8>; 256]) -> Vec<RGBA8> {
let max_index = palette_map
.iter()
.cloned()
.filter_map(|x| x)
.max()
.unwrap_or(0) as usize;
let max_index = palette_map.iter().cloned().flatten().max().unwrap_or(0) as usize;
let mut new_palette = vec![RGBA8::new(0, 0, 0, 255); max_index + 1];
for (&color, &map_to) in palette.iter().zip(palette_map.iter()) {
if let Some(map_to) = map_to {