crop Subroutine

private pure elemental subroutine crop(this, start_row, end_row, start_col, end_col)

Crops the image to a specified region.

Type Bound

format_pnm

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
integer, intent(in) :: start_row
integer, intent(in) :: end_row
integer, intent(in) :: start_col
integer, intent(in) :: end_col

Calls

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

Called by

proc~~crop~~CalledByGraph proc~crop format_pnm%crop program~demo_ppm demo_ppm program~demo_ppm->proc~crop program~test24 test24 program~test24->proc~crop

Source Code

   elemental pure subroutine crop(this, start_row, end_row, start_col, end_col)
      class(format_pnm), intent(inout)         :: this
      integer,           intent(in)            :: start_row, end_row, start_col, end_col
      integer                                  :: cropped_start_row, cropped_end_row, cropped_start_col, cropped_end_col
      integer(ik), dimension(:,:), allocatable :: cropped_pixels
      integer                                  :: i, j, cropped_height, cropped_width

      ! Check if the cropping coordinates are within the image boundaries
      cropped_start_row = max(1, start_row)
      cropped_end_row   = min(this%height, end_row)
      cropped_start_col = max(1, start_col)
      cropped_end_col   = min(this%width, end_col)

      ! Calculate the dimensions of the cropped image
      cropped_height = cropped_end_row - cropped_start_row + 1
      cropped_width  = cropped_end_col - cropped_start_col + 1

      select case (this%file_format)
       case ('pbm', 'pgm')

         ! Allocate memory for cropped image pixels
         allocate(cropped_pixels(cropped_height, cropped_width))

         ! Copy the cropped pixels to the new array
         do i = 1, cropped_height
            do j = 1, cropped_width
               cropped_pixels(i, j) = this%pixels(cropped_start_row-1+i, (cropped_start_col-1)+j)
            end do
         end do

       case ('ppm')

         ! Allocate memory for cropped image pixels
         allocate(cropped_pixels(cropped_height, 3*cropped_width))

         ! Copy the cropped pixels to the new array
         do i = 1, cropped_height
            do j = 1, 3*cropped_width
               cropped_pixels(i, j) = this%pixels(cropped_start_row-1+i, (cropped_start_col-1)*3+j)
            end do
         end do

      end select

      ! Update image dimensions and pixels
      call this%set_height(cropped_height)
      call this%set_width(cropped_width)
      deallocate(this%pixels)
      call this%allocate_pixels()

      call this%set_pixels(cropped_pixels)

      ! Deallocate temporary array
      deallocate(cropped_pixels)
   end subroutine crop