Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub fn rotate(settings: &Settings, degree: f32) -> RgbaImage {
- // angle in radians
- let angle = degree * PI / 180.0;
- // calculate the size of the image
- let (new_width, new_height) = {
- let (width, height) = (image.width() as f32, image.height() as f32);
- (
- (width * angle.cos().abs() + height * angle.sin().abs()) as u32,
- (height * angle.cos().abs() + width * angle.sin().abs()) as u32,
- )
- };
- let mut new_image = DynamicImage::new_rgba8(new_width, new_height).to_rgba();
- // 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.
- let (offset_x, offset_y) = (new_width - image.width(), new_height - image.height());
- for x in 0..image.width() {
- for y in 0..image.height() {
- let pixel = image.get_pixel(x, y);
- if pixel.data[3] == 0 {
- continue;
- }
- let (target_x, target_y) = (x + offset_x / 2, y + offset_y / 2);
- new_image.put_pixel(target_x, target_y, *pixel);
- }
- }
- imageproc::affine::rotate_with_default(&new_image,
- (new_width as f32 / 2.0, new_height as f32 / 2.0),
- angle,
- Rgba([0, 0, 0, 0u8]),
- Interpolation::Nearest)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement