Advertisement
Guest User

Untitled

a guest
May 24th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.44 KB | None | 0 0
  1. pub fn rotate(settings: &Settings, degree: f32) -> RgbaImage {
  2.     // angle in radians
  3.     let angle = degree * PI / 180.0;
  4.  
  5.     // calculate the size of the image
  6.     let (new_width, new_height) = {
  7.         let (width, height) = (image.width() as f32, image.height() as f32);
  8.         (
  9.             (width * angle.cos().abs() + height * angle.sin().abs()) as u32,
  10.             (height * angle.cos().abs() + width * angle.sin().abs()) as u32,
  11.         )
  12.     };
  13.  
  14.     let mut new_image = DynamicImage::new_rgba8(new_width, new_height).to_rgba();
  15.  
  16.     // we need to write our original image to the center of the new image. after this, we can rotate the new one around its center.
  17.     let (offset_x, offset_y) = (new_width - image.width(), new_height - image.height());
  18.     for x in 0..image.width() {
  19.         for y in 0..image.height() {
  20.             let pixel = image.get_pixel(x, y);
  21.             if pixel.data[3] == 0 {
  22.                 continue;
  23.             }
  24.             let (target_x, target_y) = (x + offset_x / 2, y + offset_y / 2);
  25.             new_image.put_pixel(target_x, target_y, *pixel);
  26.         }
  27.     }
  28.  
  29.     imageproc::affine::rotate_with_default(&new_image,
  30.                                            (new_width as f32 / 2.0, new_height as f32 / 2.0),
  31.                                            angle,
  32.                                            Rgba([0, 0, 0, 0u8]),
  33.                                            Interpolation::Nearest)
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement