rotate Subroutine

private pure elemental subroutine rotate(this, angle)

Rotates the image by a specified angle. Supported angles are 90, 180, 270, -90, -180, -270.

Type Bound

format_pnm

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
integer, intent(in) :: angle

Calls

proc~~rotate~~CallsGraph proc~rotate format_pnm%rotate proc~allocate_pixels format_pnm%allocate_pixels proc~rotate->proc~allocate_pixels proc~set_height format_pnm%set_height proc~rotate->proc~set_height proc~set_pixels format_pnm%set_pixels proc~rotate->proc~set_pixels proc~set_width format_pnm%set_width proc~rotate->proc~set_width proc~check_pixel_range format_pnm%check_pixel_range proc~set_pixels->proc~check_pixel_range

Called by

proc~~rotate~~CalledByGraph proc~rotate format_pnm%rotate program~demo_ppm demo_ppm program~demo_ppm->proc~rotate program~test22 test22 program~test22->proc~rotate

Source Code

   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