diff --git a/src/corners.rs b/src/corners.rs index a76d0d28..59706977 100644 --- a/src/corners.rs +++ b/src/corners.rs @@ -297,12 +297,7 @@ fn is_corner_fast9(image: &GrayImage, threshold: u8, x: u32, y: u32) -> bool { // y-coordinate in the range [y - 3, y + 3]. The precondition below // guarantees that these are within image bounds. let (width, height) = image.dimensions(); - if x >= u32::max_value() - 3 - || y >= u32::max_value() - 3 - || x < 3 - || y < 3 - || width <= x + 3 - || height <= y + 3 + if x >= u32::MAX - 3 || y >= u32::MAX - 3 || x < 3 || y < 3 || width <= x + 3 || height <= y + 3 { return false; } @@ -358,12 +353,7 @@ fn is_corner_fast12(image: &GrayImage, threshold: u8, x: u32, y: u32) -> bool { // y-coordinate in the range [y - 3, y + 3]. The precondition below // guarantees that these are within image bounds. let (width, height) = image.dimensions(); - if x >= u32::max_value() - 3 - || y >= u32::max_value() - 3 - || x < 3 - || y < 3 - || width <= x + 3 - || height <= y + 3 + if x >= u32::MAX - 3 || y >= u32::MAX - 3 || x < 3 || y < 3 || width <= x + 3 || height <= y + 3 { return false; } diff --git a/src/definitions.rs b/src/definitions.rs index b97c5f40..ff95dd67 100644 --- a/src/definitions.rs +++ b/src/definitions.rs @@ -1,7 +1,6 @@ //! Trait definitions and type aliases. use image::{ImageBuffer, Luma, LumaA, Pixel, Rgb, Rgba}; -use std::{i16, u16, u8}; /// An `ImageBuffer` containing Pixels of type P with storage `Vec`. /// Most operations in this library only support inputs of type `Image`, rather diff --git a/src/distance_transform.rs b/src/distance_transform.rs index 44c699d3..78ecc435 100644 --- a/src/distance_transform.rs +++ b/src/distance_transform.rs @@ -4,7 +4,6 @@ use crate::definitions::Image; use image::{GenericImage, GenericImageView, GrayImage, ImageBuffer, Luma}; use std::cmp::min; -use std::{f64, u8}; /// How to measure distance between coordinates. /// See the [`distance_transform`](fn.distance_transform.html) documentation for examples. diff --git a/src/drawing/bezier.rs b/src/drawing/bezier.rs index 3fe82489..14a898bd 100644 --- a/src/drawing/bezier.rs +++ b/src/drawing/bezier.rs @@ -2,8 +2,6 @@ use crate::definitions::Image; use crate::drawing::line::draw_line_segment_mut; use crate::drawing::Canvas; use image::{GenericImage, ImageBuffer}; -use std::f32; -use std::i32; /// Draws a cubic Bézier curve on a new copy of an image. /// diff --git a/src/drawing/conics.rs b/src/drawing/conics.rs index 15dbfccd..ec649bcd 100644 --- a/src/drawing/conics.rs +++ b/src/drawing/conics.rs @@ -3,8 +3,6 @@ use crate::drawing::draw_if_in_bounds; use crate::drawing::line::draw_line_segment_mut; use crate::drawing::Canvas; use image::{GenericImage, ImageBuffer}; -use std::f32; -use std::i32; /// Draws the outline of an ellipse on a new copy of an image. /// diff --git a/src/drawing/cross.rs b/src/drawing/cross.rs index 7eb3ab64..95dd715e 100644 --- a/src/drawing/cross.rs +++ b/src/drawing/cross.rs @@ -1,7 +1,6 @@ use crate::definitions::Image; use crate::drawing::Canvas; use image::{GenericImage, ImageBuffer}; -use std::i32; /// Draws a colored cross on an image in place. /// diff --git a/src/drawing/line.rs b/src/drawing/line.rs index 815fecd2..dd924bf5 100644 --- a/src/drawing/line.rs +++ b/src/drawing/line.rs @@ -1,8 +1,6 @@ use crate::definitions::Image; use crate::drawing::Canvas; use image::{GenericImage, ImageBuffer, Pixel}; -use std::f32; -use std::i32; use std::mem::{swap, transmute}; /// Iterates over the coordinates in a line segment using @@ -76,18 +74,14 @@ impl Iterator for BresenhamLineIter { } } -fn clamp(x: f32, upper_bound: u32) -> f32 { - if x < 0f32 { - return 0f32; - } - if x >= upper_bound as f32 { - return (upper_bound - 1) as f32; - } - x +fn in_bounds((x, y): (i32, i32), image: &I) -> bool { + x >= 0 && x < image.width() as i32 && y >= 0 && y < image.height() as i32 } fn clamp_point(p: (f32, f32), image: &I) -> (f32, f32) { - (clamp(p.0, image.width()), clamp(p.1, image.height())) + let x = p.0.clamp(0.0, (image.width() - 1) as f32); + let y = p.1.clamp(0.0, (image.height() - 1) as f32); + (x, y) } /// Iterates over the image pixels in a line segment using @@ -119,8 +113,8 @@ impl<'a, P: Pixel> Iterator for BresenhamLinePixelIter<'a, P> { fn next(&mut self) -> Option { self.iter - .next() - .map(|p| self.image.get_pixel(p.0 as u32, p.1 as u32)) + .find(|&p| in_bounds(p, self.image)) + .map(|(x, y)| self.image.get_pixel(x as u32, y as u32)) } } @@ -146,7 +140,7 @@ impl<'a, P: Pixel> BresenhamLinePixelIterMut<'a, P> { // The next two assertions are for https://github.com/image-rs/imageproc/issues/281 assert!(P::CHANNEL_COUNT > 0); assert!( - image.width() < i32::max_value() as u32 && image.height() < i32::max_value() as u32, + image.width() < i32::MAX as u32 && image.height() < i32::MAX as u32, "Image dimensions are too large" ); let iter = BresenhamLineIter::new(clamp_point(start, image), clamp_point(end, image)); @@ -159,8 +153,8 @@ impl<'a, P: Pixel> Iterator for BresenhamLinePixelIterMut<'a, P> { fn next(&mut self) -> Option { self.iter - .next() - .map(|p| self.image.get_pixel_mut(p.0 as u32, p.1 as u32)) + .find(|&p| in_bounds(p, self.image)) + .map(|(x, y)| self.image.get_pixel_mut(x as u32, y as u32)) .map(|p| unsafe { transmute(p) }) } } diff --git a/src/drawing/polygon.rs b/src/drawing/polygon.rs index 226d0259..e1ae2191 100644 --- a/src/drawing/polygon.rs +++ b/src/drawing/polygon.rs @@ -4,8 +4,6 @@ use crate::drawing::Canvas; use crate::point::Point; use image::{GenericImage, ImageBuffer}; use std::cmp::{max, min}; -use std::f32; -use std::i32; #[must_use = "the function does not modify the original image"] fn draw_polygon_with( diff --git a/src/morphology.rs b/src/morphology.rs index 510b69d5..7ea22ac6 100644 --- a/src/morphology.rs +++ b/src/morphology.rs @@ -6,7 +6,6 @@ use crate::distance_transform::{ distance_transform_impl, distance_transform_mut, DistanceFrom, Norm, }; use image::GrayImage; -use std::u8; /// Sets all pixels within distance `k` of a foreground pixel to white. /// diff --git a/src/region_labelling.rs b/src/region_labelling.rs index 8a1cd83e..0d89921c 100644 --- a/src/region_labelling.rs +++ b/src/region_labelling.rs @@ -200,7 +200,7 @@ where } next_label += 1; } else { - let mut min_label = u32::max_value(); + let mut min_label = u32::MAX; for n in 0..num_adj { min_label = cmp::min(min_label, adj_labels[n]); } diff --git a/src/utils.rs b/src/utils.rs index 2285fada..6dbec77e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -10,7 +10,6 @@ use std::collections::HashSet; use std::fmt; use std::fmt::Write; use std::path::Path; -use std::u32; /// Helper for defining greyscale images. ///