Rotates the image by a specified angle. Supported angles are 90, 180, 270, -90, -180, -270.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this | |||
integer, | intent(in) | :: | angle |
elemental pure subroutine rotate(this, angle) class(format_pnm), intent(inout) :: this integer, intent(in) :: angle integer(ik), dimension(:,:), allocatable :: rotated_pixels integer :: target_height, target_width integer :: i, j ! Determine the target height and width based on the rotation angle select case (angle) case (90, -90, 270, -270) target_height = this%width target_width = this%height case (180, -180) target_height = this%height target_width = this%width case default error stop "Invalid rotation angle. Valid angles are 90, 180, 270, -90, -180, -270." end select select case (this%file_format) case ('pbm', 'pgm') ! Allocate memory for rotated_pixels array allocate(rotated_pixels(target_height, target_width)) ! Rotate pixels based on the specified angle select case (angle) case (90, -270) do i = 1, this%height do j = 1, this%width rotated_pixels(j, this%height-i+1) = this%pixels(i, j) end do end do case (180, -180) do i = 1, this%height do j = 1, this%width rotated_pixels(this%height-i+1, this%width-j+1) = this%pixels(i, j) end do end do case (270, -90) do i = 1, this%height do j = 1, this%width rotated_pixels(this%width-j+1, i) = this%pixels(i, j) end do end do end select case ('ppm') ! Allocate memory for rotated_pixels array allocate(rotated_pixels(target_height, 3*target_width)) ! Rotate pixels based on the specified angle select case (angle) case (90, -270) do i = 1, this%height do j = 1, this%width rotated_pixels(j, 3*(this%height-i+1)-2:3*(this%height-i+1)) = this%pixels(i, 3*j-2:3*j) end do end do case (180, -180) do i = 1, this%height do j = 1, this%width rotated_pixels(this%height-i+1, 3*(this%width-j+1)-2:3*(this%width-j+1)) = this%pixels(i, 3*j-2:3*j) end do end do case (270, -90) do i = 1, this%height do j = 1, this%width rotated_pixels(this%width-j+1, 3*i-2:3*i) = this%pixels(i, 3*j-2:3*j) end do end do end select end select ! Update height and width of the image call this%set_height(target_height) call this%set_width(target_width) deallocate(this%pixels) call this%allocate_pixels() ! Update the original pixels with rotated pixels call this%set_pixels(rotated_pixels) ! Deallocate rotated_pixels array deallocate(rotated_pixels) end subroutine rotate