Crops the image to a specified region.
| Type | Intent | Optional | 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 |
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