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~set_height format_pnm%set_height proc~crop->proc~set_height proc~set_width format_pnm%set_width proc~crop->proc~set_width

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                                  :: r1, r2, c1, c2
      integer(ik), dimension(:,:), allocatable :: cropped_pixels
      integer                                  :: cropped_height, cropped_width

      ! Check if the cropping coordinates are within the image boundaries
      r1 = max(1, start_row)
      r2 = min(this%height, end_row)
      c1 = max(1, start_col)
      c2 = min(this%width, end_col)

      ! Calculate the dimensions of the cropped image
      cropped_height = r2 - r1 + 1
      cropped_width  = c2 - c1 + 1

      select case (this%file_format)
       case ('pbm', 'pgm')
         allocate(cropped_pixels(cropped_height, cropped_width))
         cropped_pixels = this%pixels(r1:r2, c1:c2)
       case ('ppm')
         allocate(cropped_pixels(cropped_height, 3*cropped_width))
         cropped_pixels = this%pixels(r1:r2, 3*(c1-1)+1 : 3*c2)
       case default
          error stop 'crop: Unsupported file format.'
      end select

      ! Update image dimensions and pixels
      call this%set_height(cropped_height)
      call this%set_width(cropped_width)
      call move_alloc(cropped_pixels, this%pixels)
   end subroutine crop