Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to image::math::Rect #647

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions examples/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,14 @@ fn main() {
draw_line_segment_mut(&mut image, (20f32, 180f32), (20f32, 220f32), white);

// Draw a hollow rect within bounds
draw_hollow_rect_mut(&mut image, Rect::at(60, 10).of_size(20, 20), white);
draw_hollow_rect_mut(&mut image, Rect{x: 60, y:10, width:20, height:20}, white);
// Outside bounds
draw_hollow_rect_mut(&mut image, Rect::at(300, 10).of_size(20, 20), white);
// Partially outside bounds
draw_hollow_rect_mut(&mut image, Rect::at(90, -10).of_size(30, 20), white);
draw_hollow_rect_mut(&mut image, Rect{x: 300, y:10, width:20, height:20}, white);

// Draw a filled rect within bounds
draw_filled_rect_mut(&mut image, Rect::at(130, 10).of_size(20, 20), white);
draw_filled_rect_mut(&mut image, Rect{x: 130, y:10, width:20, height:20}, white);
// Outside bounds
draw_filled_rect_mut(&mut image, Rect::at(300, 10).of_size(20, 20), white);
// Partially outside bounds
draw_filled_rect_mut(&mut image, Rect::at(180, -10).of_size(30, 20), white);
draw_filled_rect_mut(&mut image, Rect{x: 300, y:10, width:20, height:20}, white);

// Draw a hollow circle within bounds
draw_hollow_circle_mut(&mut image, (100, 100), 15, white);
Expand Down
16 changes: 12 additions & 4 deletions examples/template_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,12 @@ fn run_match_template(
.unwrap();

// Show location the template was extracted from
let roi = Rect::at(args.template_x as i32, args.template_y as i32)
.of_size(args.template_w, args.template_h);
let roi = Rect {
x: args.template_x,
y: args.template_y,
width: args.template_w,
height: args.template_h,
};

draw_green_rect(&result_padded, roi)
}
Expand Down Expand Up @@ -174,8 +178,12 @@ fn main() {
);

// Show location the template was extracted from
let roi = Rect::at(args.template_x as i32, args.template_y as i32)
.of_size(args.template_w, args.template_h);
let roi = Rect {
x: args.template_x,
y: args.template_y,
width: args.template_w,
height: args.template_h,
};

let image_with_roi = draw_green_rect(&image, roi);

Expand Down
95 changes: 73 additions & 22 deletions src/drawing/rect.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::definitions::Image;
use crate::drawing::line::draw_line_segment_mut;
use crate::drawing::Canvas;
use crate::rect::Rect;
use crate::rect::{Rect, RectExt};
use image::GenericImage;
use std::f32;

Expand All @@ -23,15 +23,15 @@ pub fn draw_hollow_rect_mut<C>(canvas: &mut C, rect: Rect, color: C::Pixel)
where
C: Canvas,
{
let left = rect.left() as f32;
let right = rect.right() as f32;
let top = rect.top() as f32;
let bottom = rect.bottom() as f32;

draw_line_segment_mut(canvas, (left, top), (right, top), color);
draw_line_segment_mut(canvas, (left, bottom), (right, bottom), color);
draw_line_segment_mut(canvas, (left, top), (left, bottom), color);
draw_line_segment_mut(canvas, (right, top), (right, bottom), color);
let left_x = rect.left_x() as f32;
let right_x = rect.right_x() as f32;
let top_y = rect.top_y() as f32;
let bottom_y = rect.bottom_y() as f32;

draw_line_segment_mut(canvas, (left_x, top_y), (right_x, top_y), color);
draw_line_segment_mut(canvas, (left_x, bottom_y), (right_x, bottom_y), color);
draw_line_segment_mut(canvas, (left_x, top_y), (left_x, bottom_y), color);
draw_line_segment_mut(canvas, (right_x, top_y), (right_x, bottom_y), color);
}

/// Draws a rectangle and its contents on an image.
Expand All @@ -52,12 +52,17 @@ pub fn draw_filled_rect_mut<C>(canvas: &mut C, rect: Rect, color: C::Pixel)
where
C: Canvas,
{
let canvas_bounds = Rect::at(0, 0).of_size(canvas.width(), canvas.height());
if let Some(intersection) = canvas_bounds.intersect(rect) {
for dy in 0..intersection.height() {
for dx in 0..intersection.width() {
let x = intersection.left() as u32 + dx;
let y = intersection.top() as u32 + dy;
let canvas_bounds = Rect {
x: 0,
y: 0,
width: canvas.width(),
height: canvas.height(),
};
if let Some(intersection) = canvas_bounds.intersect(&rect) {
for dy in 0..intersection.height {
for dx in 0..intersection.width {
let x = intersection.left_x() + dx;
let y = intersection.top_y() + dy;
canvas.draw_pixel(x, y, color);
}
}
Expand All @@ -82,7 +87,16 @@ mod tests {
1, 1, 4, 1, 4;
1, 1, 4, 4, 4);

let actual = draw_hollow_rect(&image, Rect::at(2, 2).of_size(3, 3), Luma([4u8]));
let actual = draw_hollow_rect(
&image,
Rect {
x: 2,
y: 2,
width: 3,
height: 3,
},
Luma([4u8]),
);
assert_pixels_eq!(actual, expected);
}

Expand All @@ -97,7 +111,16 @@ mod tests {
1, 4, 4, 4, 1;
1, 1, 1, 1, 1);

let actual = draw_filled_rect(&image, Rect::at(1, 1).of_size(3, 3), Luma([4u8]));
let actual = draw_filled_rect(
&image,
Rect {
x: 1,
y: 1,
width: 3,
height: 3,
},
Luma([4u8]),
);
assert_pixels_eq!(actual, expected);
}

Expand All @@ -111,10 +134,24 @@ mod tests {

let mut image = Blend(RgbaImage::from_pixel(5, 5, white));

draw_filled_rect_mut(&mut image, Rect::at(1, 1).of_size(3, 3), blue);
draw_filled_rect_mut(
&mut image,
Rect::at(2, 2).of_size(1, 1),
Rect {
x: 1,
y: 1,
width: 3,
height: 3,
},
blue,
);
draw_filled_rect_mut(
&mut image,
Rect {
x: 2,
y: 2,
width: 1,
height: 1,
},
semi_transparent_red,
);

Expand All @@ -134,7 +171,16 @@ mod tests {

// Draw an opaque rectangle over the central pixel as a sanity check that
// we're blending in the correct direction only.
draw_filled_rect_mut(&mut image, Rect::at(2, 2).of_size(1, 1), blue);
draw_filled_rect_mut(
&mut image,
Rect {
x: 2,
y: 2,
width: 1,
height: 1,
},
blue,
);
assert_eq!(*image.0.get_pixel(2, 2), blue);
}
}
Expand All @@ -151,7 +197,12 @@ mod benches {
fn bench_draw_filled_rect_mut_rgb(b: &mut Bencher) {
let mut image = RgbImage::new(200, 200);
let color = Rgb([120u8, 60u8, 47u8]);
let rect = Rect::at(50, 50).of_size(80, 90);
let rect = Rect {
x: 50,
y: 50,
width: 80,
height: 90,
};
b.iter(|| {
draw_filled_rect_mut(&mut image, rect, color);
black_box(&image);
Expand Down
15 changes: 12 additions & 3 deletions src/edges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,18 @@ mod benches {

fn edge_detect_bench_image(width: u32, height: u32) -> GrayImage {
let mut image = GrayImage::new(width, height);
let (w, h) = (width as i32, height as i32);
let large = Rect::at(w / 4, h / 4).of_size(width / 2, height / 2);
let small = Rect::at(9, 9).of_size(3, 3);
let large = Rect {
x: width / 4,
y: height / 4,
width: width / 2,
height: height / 2,
};
let small = Rect {
x: 9,
y: 9,
width: 3,
height: 3,
};

draw_filled_rect_mut(&mut image, large, Luma([255]));
draw_filled_rect_mut(&mut image, small, Luma([255]));
Expand Down
Loading
Loading