swap_channels Subroutine

private pure elemental subroutine swap_channels(this, swap)

Swaps the RGB channels of the image. Only supported for PPM images.

Type Bound

format_pnm

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
character(len=*), intent(in) :: swap

Calls

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

Called by

proc~~swap_channels~~CalledByGraph proc~swap_channels format_pnm%swap_channels program~demo_ppm demo_ppm program~demo_ppm->proc~swap_channels program~test19 test19 program~test19->proc~swap_channels

Source Code

   elemental pure subroutine swap_channels(this, swap)
      class(format_pnm), intent(inout) :: this
      character(*),      intent(in)    :: swap
      integer(ik)                      :: temp
      integer                          :: i, j

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

      ! Swap R and G channels
      if (swap=='rg' .or. swap=='gr' .or. swap=='RG' .or. swap=='GR') then
         do i = 1, this%height
            do j = 1, this%width
               temp = this%pixels(i, 3*j-2)
               this%pixels(i, 3*j-2) = this%pixels(i, 3*j-1)
               this%pixels(i, 3*j-1) = temp
            end do
         end do
      end if

      ! Swap G and B channels
      if (swap=='gb' .or. swap=='bg' .or. swap=='GB' .or. swap=='BG') then
         do i = 1, this%height
            do j = 1, this%width
               temp = this%pixels(i, 3*j-1)
               this%pixels(i, 3*j-1) = this%pixels(i, 3*j-0)
               this%pixels(i, 3*j-0) = temp
            end do
         end do
      end if

      ! Swap R and B channels
      if (swap=='rb' .or. swap=='br' .or. swap=='RB' .or. swap=='BR') then
         do i = 1, this%height
            do j = 1, this%width
               temp = this%pixels(i, 3*j-2)
               this%pixels(i, 3*j-2) = this%pixels(i, 3*j-0)
               this%pixels(i, 3*j-0) = temp
            end do
         end do
      end if

      call this%check_pixel_range(this%pixels)
   end subroutine swap_channels