Skip to content

Commit

Permalink
Fix translate() which panics on negative x offset (image-rs#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjaustirni authored and theotherphil committed Oct 19, 2019
1 parent 3d20e5a commit e025cac
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/geometric_transformations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ where
}

if w + tx > 0 {
let in_base = (y_in as usize * width as usize - (tx as usize)) * num_channels;
let in_base = (y_in as usize * width as usize + (tx.abs() as usize)) * num_channels;
let out_base = (y as usize * width as usize) * num_channels;
let len = (w + tx) as usize * num_channels;
(*out)[out_base..][..len].copy_from_slice(&(**image)[in_base..][..len]);
Expand Down Expand Up @@ -856,6 +856,22 @@ mod tests {
assert_pixels_eq!(translated, expected);
}

#[test]
fn test_translate_negative_x() {
let image = gray_image!(
00, 01, 02;
10, 11, 12;
20, 21, 22);

let expected = gray_image!(
01, 02, 02;
11, 12, 12;
21, 22, 22);

let translated = translate(&image, (-1, 0));
assert_pixels_eq!(translated, expected);
}

#[test]
fn test_translate_large_x_large_y() {
let image = gray_image!(
Expand Down

0 comments on commit e025cac

Please sign in to comment.