Skip to content

Commit

Permalink
Fix BresenhamLineIter panic (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
cospectrum authored Apr 13, 2024
1 parent 4e6a5dc commit 6a797d3
Show file tree
Hide file tree
Showing 11 changed files with 13 additions and 40 deletions.
14 changes: 2 additions & 12 deletions src/corners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion src/definitions.rs
Original file line number Diff line number Diff line change
@@ -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<P::Subpixel>`.
/// Most operations in this library only support inputs of type `Image`, rather
Expand Down
1 change: 0 additions & 1 deletion src/distance_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions src/drawing/bezier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
2 changes: 0 additions & 2 deletions src/drawing/conics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
1 change: 0 additions & 1 deletion src/drawing/cross.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down
26 changes: 10 additions & 16 deletions src/drawing/line.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<I: GenericImage>((x, y): (i32, i32), image: &I) -> bool {
x >= 0 && x < image.width() as i32 && y >= 0 && y < image.height() as i32
}

fn clamp_point<I: GenericImage>(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
Expand Down Expand Up @@ -119,8 +113,8 @@ impl<'a, P: Pixel> Iterator for BresenhamLinePixelIter<'a, P> {

fn next(&mut self) -> Option<Self::Item> {
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))
}
}

Expand All @@ -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));
Expand All @@ -159,8 +153,8 @@ impl<'a, P: Pixel> Iterator for BresenhamLinePixelIterMut<'a, P> {

fn next(&mut self) -> Option<Self::Item> {
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) })
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/drawing/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I, L>(
Expand Down
1 change: 0 additions & 1 deletion src/morphology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
2 changes: 1 addition & 1 deletion src/region_labelling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
1 change: 0 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down

0 comments on commit 6a797d3

Please sign in to comment.