Skip to content

Commit

Permalink
Merge pull request #2270 from image-rs/clippy
Browse files Browse the repository at this point in the history
A bunch of Clippy fixes
  • Loading branch information
HeroicKatora committed Jun 29, 2024
2 parents 4589a4a + 0aa70f9 commit 9429cb9
Show file tree
Hide file tree
Showing 53 changed files with 432 additions and 356 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "image"
version = "0.25.1"
version = "0.25.2"
edition = "2021"
resolver = "2"

Expand Down
2 changes: 1 addition & 1 deletion benches/copy_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn bench_copy_from(c: &mut Criterion) {
let mut dst = ImageBuffer::from_pixel(2048, 2048, Rgba([0u8, 0, 0, 255]));

c.bench_function("copy_from", |b| {
b.iter(|| dst.copy_from(black_box(&src), 0, 0))
b.iter(|| dst.copy_from(black_box(&src), 0, 0));
});
}

Expand Down
2 changes: 1 addition & 1 deletion benches/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn bench_load(c: &mut Criterion, def: &BenchDef) {
group.bench_function(file_name.to_owned(), |b| {
b.iter(|| {
image::load_from_memory_with_format(&buf, def.format).unwrap();
})
});
});
}
}
Expand Down
8 changes: 4 additions & 4 deletions benches/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn encode_all(c: &mut Criterion) {
];

for definition in BENCH_DEFS {
encode_definition(c, definition)
encode_definition(c, definition);
}
}

Expand All @@ -55,7 +55,7 @@ fn encode_zeroed(group: &mut BenchGroup, with: &dyn Encoder, size: u32, color: E
let im = vec![0; (color.bits_per_pixel() as usize * size as usize + 7) / 8 * size as usize];

group.bench_with_input(
BenchmarkId::new(format!("zero-{:?}-rawvec", color), size),
BenchmarkId::new(format!("zero-{color:?}-rawvec"), size),
&im,
|b, image| {
let mut v = vec![];
Expand All @@ -64,7 +64,7 @@ fn encode_zeroed(group: &mut BenchGroup, with: &dyn Encoder, size: u32, color: E
},
);
group.bench_with_input(
BenchmarkId::new(format!("zero-{:?}-bufvec", color), size),
BenchmarkId::new(format!("zero-{color:?}-bufvec"), size),
&im,
|b, image| {
let mut v = vec![];
Expand All @@ -73,7 +73,7 @@ fn encode_zeroed(group: &mut BenchGroup, with: &dyn Encoder, size: u32, color: E
},
);
group.bench_with_input(
BenchmarkId::new(format!("zero-{:?}-file", color), size),
BenchmarkId::new(format!("zero-{color:?}-file"), size),
&im,
|b, image| {
let file = File::create("temp.bmp").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/concat/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use image::{GenericImage, GenericImageView, ImageBuffer, Pixel, Primitive};

/// Example showcasing a generic implementation of image concatenation.
///
/// The example images are coming from https://placeholder.com/
/// The example images are coming from <https://placeholder.com>/
///
/// Run from the root of the repository with:
/// cargo run --release --example concat
Expand Down
2 changes: 1 addition & 1 deletion examples/opening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() {
// The color method returns the image's ColorType
println!("{:?}", im.color());

let fout = &mut File::create(Path::new(&format!("{}.png", file))).unwrap();
let fout = &mut File::create(Path::new(&format!("{file}.png"))).unwrap();

// Write the contents of this image to the Writer in PNG format.
im.write_to(fout, ImageFormat::Png).unwrap();
Expand Down
14 changes: 6 additions & 8 deletions examples/scaledown/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,36 @@ impl Elapsed {
impl fmt::Display for Elapsed {
fn fmt(&self, out: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match (self.0.as_secs(), self.0.subsec_nanos()) {
(0, n) if n < 1000 => write!(out, "{} ns", n),
(0, n) if n < 1000 => write!(out, "{n} ns"),
(0, n) if n < 1_000_000 => write!(out, "{} µs", n / 1000),
(0, n) => write!(out, "{} ms", n / 1_000_000),
(s, n) if s < 10 => write!(out, "{}.{:02} s", s, n / 10_000_000),
(s, _) => write!(out, "{} s", s),
(s, _) => write!(out, "{s} s"),
}
}
}

fn main() {
let img = image::open("examples/scaledown/test.jpg").unwrap();
for &(name, filter) in [
for &(name, filter) in &[
("near", FilterType::Nearest),
("tri", FilterType::Triangle),
("cmr", FilterType::CatmullRom),
("gauss", FilterType::Gaussian),
("lcz2", FilterType::Lanczos3),
]
.iter()
{
] {
let timer = Instant::now();
let scaled = img.resize(400, 400, filter);
println!("Scaled by {} in {}", name, Elapsed::from(&timer));
let mut output = File::create(&format!("test-{}.png", name)).unwrap();
let mut output = File::create(&format!("test-{name}.png")).unwrap();
scaled.write_to(&mut output, ImageFormat::Png).unwrap();
}

for size in &[20_u32, 40, 100, 200, 400] {
let timer = Instant::now();
let scaled = img.thumbnail(*size, *size);
println!("Thumbnailed to {} in {}", size, Elapsed::from(&timer));
let mut output = File::create(format!("test-thumb{}.png", size)).unwrap();
let mut output = File::create(format!("test-thumb{size}.png")).unwrap();
scaled.write_to(&mut output, ImageFormat::Png).unwrap();
}
}
14 changes: 6 additions & 8 deletions examples/scaleup/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,34 @@ impl Elapsed {
impl fmt::Display for Elapsed {
fn fmt(&self, out: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match (self.0.as_secs(), self.0.subsec_nanos()) {
(0, n) if n < 1000 => write!(out, "{} ns", n),
(0, n) if n < 1000 => write!(out, "{n} ns"),
(0, n) if n < 1_000_000 => write!(out, "{} µs", n / 1000),
(0, n) => write!(out, "{} ms", n / 1_000_000),
(s, n) if s < 10 => write!(out, "{}.{:02} s", s, n / 10_000_000),
(s, _) => write!(out, "{} s", s),
(s, _) => write!(out, "{s} s"),
}
}
}

fn main() {
let tiny = image::open("examples/scaleup/tinycross.png").unwrap();
for &(name, filter) in [
for &(name, filter) in &[
("near", FilterType::Nearest),
("tri", FilterType::Triangle),
("xcmr", FilterType::CatmullRom),
("ygauss", FilterType::Gaussian),
("zlcz2", FilterType::Lanczos3),
]
.iter()
{
] {
let timer = Instant::now();
let scaled = tiny.resize(32, 32, filter);
println!("Scaled by {} in {}", name, Elapsed::from(&timer));
let mut output = File::create(&format!("up2-{}.png", name)).unwrap();
let mut output = File::create(&format!("up2-{name}.png")).unwrap();
scaled.write_to(&mut output, ImageFormat::Png).unwrap();

let timer = Instant::now();
let scaled = tiny.resize(48, 48, filter);
println!("Scaled by {} in {}", name, Elapsed::from(&timer));
let mut output = File::create(&format!("up3-{}.png", name)).unwrap();
let mut output = File::create(&format!("up3-{name}.png")).unwrap();
scaled.write_to(&mut output, ImageFormat::Png).unwrap();
}
}
11 changes: 11 additions & 0 deletions src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct Frames<'a> {

impl<'a> Frames<'a> {
/// Creates a new `Frames` from an implementation specific iterator.
#[must_use]
pub fn new(iterator: Box<dyn Iterator<Item = ImageResult<Frame>> + 'a>) -> Self {
Frames { iterator }
}
Expand Down Expand Up @@ -69,6 +70,7 @@ pub struct Delay {

impl Frame {
/// Constructs a new frame without any delay.
#[must_use]
pub fn new(buffer: RgbaImage) -> Frame {
Frame {
delay: Delay::from_ratio(Ratio { numer: 0, denom: 1 }),
Expand All @@ -79,6 +81,7 @@ impl Frame {
}

/// Constructs a new frame
#[must_use]
pub fn from_parts(buffer: RgbaImage, left: u32, top: u32, delay: Delay) -> Frame {
Frame {
delay,
Expand All @@ -89,11 +92,13 @@ impl Frame {
}

/// Delay of this frame
#[must_use]
pub fn delay(&self) -> Delay {
self.delay
}

/// Returns the image buffer
#[must_use]
pub fn buffer(&self) -> &RgbaImage {
&self.buffer
}
Expand All @@ -104,16 +109,19 @@ impl Frame {
}

/// Returns the image buffer
#[must_use]
pub fn into_buffer(self) -> RgbaImage {
self.buffer
}

/// Returns the x offset
#[must_use]
pub fn left(&self) -> u32 {
self.left
}

/// Returns the y offset
#[must_use]
pub fn top(&self) -> u32 {
self.top
}
Expand All @@ -128,6 +136,7 @@ impl Delay {
/// use image::Delay;
/// let delay_10ms = Delay::from_numer_denom_ms(10, 1);
/// ```
#[must_use]
pub fn from_numer_denom_ms(numerator: u32, denominator: u32) -> Self {
Delay {
ratio: Ratio::new(numerator, denominator),
Expand All @@ -148,6 +157,7 @@ impl Delay {
/// let duration = Duration::from_millis(20);
/// let delay = Delay::from_saturating_duration(duration);
/// ```
#[must_use]
pub fn from_saturating_duration(duration: Duration) -> Self {
// A few notes: The largest number we can represent as a ratio is u32::MAX but we can
// sometimes represent much smaller numbers.
Expand Down Expand Up @@ -177,6 +187,7 @@ impl Delay {
///
/// This is guaranteed to be an exact conversion if the `Delay` was previously created with the
/// `from_numer_denom_ms` constructor.
#[must_use]
pub fn numer_denom_ms(self) -> (u32, u32) {
(self.ratio.numer, self.ratio.denom)
}
Expand Down
26 changes: 15 additions & 11 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ where
/// image::imageops::overlay(&mut img, &on_top, 128, 128);
/// ```
///
/// Convert an RgbaImage to a GrayImage.
/// Convert an `RgbaImage` to a `GrayImage`.
///
/// ```no_run
/// use image::{open, DynamicImage};
Expand Down Expand Up @@ -801,7 +801,7 @@ where
/// the bounds will not overflow.
fn check_image_fits(width: u32, height: u32, len: usize) -> bool {
let checked_len = Self::image_buffer_len(width, height);
checked_len.map(|min_len| min_len <= len).unwrap_or(false)
checked_len.map_or(false, |min_len| min_len <= len)
}

fn image_buffer_len(width: u32, height: u32) -> Option<usize> {
Expand Down Expand Up @@ -980,7 +980,7 @@ where
#[inline]
#[track_caller]
pub fn put_pixel(&mut self, x: u32, y: u32, pixel: P) {
*self.get_pixel_mut(x, y) = pixel
*self.get_pixel_mut(x, y) = pixel;
}
}

Expand Down Expand Up @@ -1198,22 +1198,22 @@ where
}

fn put_pixel(&mut self, x: u32, y: u32, pixel: P) {
*self.get_pixel_mut(x, y) = pixel
*self.get_pixel_mut(x, y) = pixel;
}

/// Puts a pixel at location (x, y), ignoring bounds checking.
#[inline(always)]
unsafe fn unsafe_put_pixel(&mut self, x: u32, y: u32, pixel: P) {
let indices = self.pixel_indices_unchecked(x, y);
let p = <P as Pixel>::from_slice_mut(self.data.get_unchecked_mut(indices));
*p = pixel
*p = pixel;
}

/// Put a pixel at location (x, y), taking into account alpha channels
///
/// DEPRECATED: This method will be removed. Blend the pixel directly instead.
fn blend_pixel(&mut self, x: u32, y: u32, p: P) {
self.get_pixel_mut(x, y).blend(&p)
self.get_pixel_mut(x, y).blend(&p);
}

fn copy_within(&mut self, source: Rect, x: u32, y: u32) -> bool {
Expand Down Expand Up @@ -1268,6 +1268,7 @@ impl<P: Pixel> ImageBuffer<P, Vec<P::Subpixel>> {
/// # Panics
///
/// Panics when the resulting image is larger than the maximum size of a vector.
#[must_use]
pub fn new(width: u32, height: u32) -> ImageBuffer<P, Vec<P::Subpixel>> {
let size = Self::image_buffer_len(width, height)
.expect("Buffer length in `ImageBuffer::new` overflows usize");
Expand All @@ -1279,20 +1280,20 @@ impl<P: Pixel> ImageBuffer<P, Vec<P::Subpixel>> {
}
}

/// Constructs a new ImageBuffer by copying a pixel
/// Constructs a new `ImageBuffer` by copying a pixel
///
/// # Panics
///
/// Panics when the resulting image is larger the the maximum size of a vector.
pub fn from_pixel(width: u32, height: u32, pixel: P) -> ImageBuffer<P, Vec<P::Subpixel>> {
let mut buf = ImageBuffer::new(width, height);
for p in buf.pixels_mut() {
*p = pixel
*p = pixel;
}
buf
}

/// Constructs a new ImageBuffer by repeated application of the supplied function.
/// Constructs a new `ImageBuffer` by repeated application of the supplied function.
///
/// The arguments to the function are the pixel's x and y coordinates.
///
Expand All @@ -1305,13 +1306,14 @@ impl<P: Pixel> ImageBuffer<P, Vec<P::Subpixel>> {
{
let mut buf = ImageBuffer::new(width, height);
for (x, y, p) in buf.enumerate_pixels_mut() {
*p = f(x, y)
*p = f(x, y);
}
buf
}

/// Creates an image buffer out of an existing buffer.
/// Returns None if the buffer is not big enough.
#[must_use]
pub fn from_vec(
width: u32,
height: u32,
Expand All @@ -1322,6 +1324,7 @@ impl<P: Pixel> ImageBuffer<P, Vec<P::Subpixel>> {

/// Consumes the image buffer and returns the underlying data
/// as an owned buffer
#[must_use]
pub fn into_vec(self) -> Vec<P::Subpixel> {
self.into_raw()
}
Expand All @@ -1341,6 +1344,7 @@ impl GrayImage {
/// Expands a color palette by re-using the existing buffer.
/// Assumes 8 bit per pixel. Uses an optionally transparent index to
/// adjust it's alpha value accordingly.
#[must_use]
pub fn expand_palette(
self,
palette: &[(u8, u8, u8)],
Expand Down Expand Up @@ -1397,7 +1401,7 @@ where
let mut buffer: ImageBuffer<ToType, Vec<ToType::Subpixel>> =
ImageBuffer::new(self.width, self.height);
for (to, from) in buffer.pixels_mut().zip(self.pixels()) {
to.from_color(from)
to.from_color(from);
}
buffer
}
Expand Down
2 changes: 1 addition & 1 deletion src/buffer_par.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ where
P: Pixel + Send + Sync,
P::Subpixel: Send + Sync,
{
/// Constructs a new ImageBuffer by repeated application of the supplied function,
/// Constructs a new `ImageBuffer` by repeated application of the supplied function,
/// utilizing multi-threading via `rayon`.
///
/// The arguments to the function are the pixel's x and y coordinates.
Expand Down
Loading

0 comments on commit 9429cb9

Please sign in to comment.