greyscale Subroutine

private pure elemental subroutine greyscale(this)

Converts a color image to greyscale. Only supported for PPM images.

Type Bound

format_pnm

Arguments

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

Calls

proc~~greyscale~~CallsGraph proc~greyscale format_pnm%greyscale proc~check_pixel_range format_pnm%check_pixel_range proc~greyscale->proc~check_pixel_range

Called by

proc~~greyscale~~CalledByGraph proc~greyscale format_pnm%greyscale program~demo_ppm demo_ppm program~demo_ppm->proc~greyscale program~test21 test21 program~test21->proc~greyscale

Source Code

   elemental pure subroutine greyscale(this)
      class(format_pnm), intent(inout) :: this
      integer                          :: i, j

      ! Check if the file is ppm
      if (this%file_format /= 'ppm') error stop 'greyscale: This function is only for ppm files.'

      do i = 1, this%height
         do j = 1, this%width
            ! Calculate the ITU Rec.709 weighted average of RGB channels to derive a greyscale value and assign it as an integer to all RGB channels.
            this%pixels(i, 3*j-2:3*j) = int(0.2126_rk * real(this%pixels(i, 3*j-2), kind=rk) + &
                                            0.7152_rk * real(this%pixels(i, 3*j-1), kind=rk) + &
                                            0.0722_rk * real(this%pixels(i, 3*j-0), kind=rk), kind=ik)
         end do
      end do

      call this%check_pixel_range(this%pixels)

   end subroutine greyscale