lut.f90 Source File


Files dependent on this one

sourcefile~~lut.f90~~AfferentGraph sourcefile~lut.f90 lut.f90 sourcefile~forimage.f90 forimage.f90 sourcefile~forimage.f90->sourcefile~lut.f90 sourcefile~demo_color.f90 demo_color.f90 sourcefile~demo_color.f90->sourcefile~forimage.f90 sourcefile~demo_ppm.f90 demo_ppm.f90 sourcefile~demo_ppm.f90->sourcefile~forimage.f90 sourcefile~test1.f90 test1.f90 sourcefile~test1.f90->sourcefile~forimage.f90 sourcefile~test10.f90 test10.f90 sourcefile~test10.f90->sourcefile~forimage.f90 sourcefile~test11.f90 test11.f90 sourcefile~test11.f90->sourcefile~forimage.f90 sourcefile~test12.f90 test12.f90 sourcefile~test12.f90->sourcefile~forimage.f90 sourcefile~test13.f90 test13.f90 sourcefile~test13.f90->sourcefile~forimage.f90 sourcefile~test14.f90 test14.f90 sourcefile~test14.f90->sourcefile~forimage.f90 sourcefile~test15.f90 test15.f90 sourcefile~test15.f90->sourcefile~forimage.f90 sourcefile~test16.f90 test16.f90 sourcefile~test16.f90->sourcefile~forimage.f90 sourcefile~test17.f90 test17.f90 sourcefile~test17.f90->sourcefile~forimage.f90 sourcefile~test18.f90 test18.f90 sourcefile~test18.f90->sourcefile~forimage.f90 sourcefile~test19.f90 test19.f90 sourcefile~test19.f90->sourcefile~forimage.f90 sourcefile~test2.f90 test2.f90 sourcefile~test2.f90->sourcefile~forimage.f90 sourcefile~test20.f90 test20.f90 sourcefile~test20.f90->sourcefile~forimage.f90 sourcefile~test21.f90 test21.f90 sourcefile~test21.f90->sourcefile~forimage.f90 sourcefile~test22.f90 test22.f90 sourcefile~test22.f90->sourcefile~forimage.f90 sourcefile~test23.f90 test23.f90 sourcefile~test23.f90->sourcefile~forimage.f90 sourcefile~test24.f90 test24.f90 sourcefile~test24.f90->sourcefile~forimage.f90 sourcefile~test25.f90 test25.f90 sourcefile~test25.f90->sourcefile~forimage.f90 sourcefile~test26.f90 test26.f90 sourcefile~test26.f90->sourcefile~forimage.f90 sourcefile~test27.f90 test27.f90 sourcefile~test27.f90->sourcefile~forimage.f90 sourcefile~test28.f90 test28.f90 sourcefile~test28.f90->sourcefile~forimage.f90 sourcefile~test29.f90 test29.f90 sourcefile~test29.f90->sourcefile~forimage.f90 sourcefile~test3.f90 test3.f90 sourcefile~test3.f90->sourcefile~forimage.f90 sourcefile~test4.f90 test4.f90 sourcefile~test4.f90->sourcefile~forimage.f90 sourcefile~test5.f90 test5.f90 sourcefile~test5.f90->sourcefile~forimage.f90 sourcefile~test6.f90 test6.f90 sourcefile~test6.f90->sourcefile~forimage.f90 sourcefile~test7.f90 test7.f90 sourcefile~test7.f90->sourcefile~forimage.f90 sourcefile~test8.f90 test8.f90 sourcefile~test8.f90->sourcefile~forimage.f90 sourcefile~test9.f90 test9.f90 sourcefile~test9.f90->sourcefile~forimage.f90

Source Code

module lut

   implicit none

   private
   public format_lut

   !===============================================================================
   type format_lut
      integer                              :: num_colors
      integer                              :: dim_colors
      integer, dimension(:,:), allocatable :: colors
   contains
      procedure :: allocate_colors
      procedure :: set
      procedure :: set_num_colors
      procedure :: get_num_colors
      procedure :: set_dim_colors
      procedure :: get_dim_colors
      procedure :: set_colors
      procedure :: get_colors
      procedure :: export
      procedure :: import
      procedure :: finalize => deallocate_lut
   end type format_lut
   !===============================================================================

contains

   !===============================================================================
   !> author: Seyed Ali Ghasemi
   pure elemental subroutine allocate_colors(this)
      class(format_lut), intent(inout) :: this
      if (allocated(this%colors)) deallocate(this%colors)
      allocate(this%colors(this%num_colors, this%dim_colors))
   end subroutine allocate_colors
   !===============================================================================


   !===============================================================================
   !> author: Seyed Ali Ghasemi
   pure subroutine set(this, num_colors, dim_colors, colors)
      class(format_lut),       intent(inout) :: this
      integer,                 intent(in)    :: num_colors, dim_colors
      integer, dimension(:,:), intent(in)    :: colors

      this%num_colors = num_colors
      this%dim_colors = dim_colors
      call this%allocate_colors()
      this%colors = colors
   end subroutine set
   !===============================================================================


   !===============================================================================
   !> author: Seyed Ali Ghasemi
   pure elemental subroutine set_num_colors(this, num_colors)
      class(format_lut), intent(inout) :: this
      integer,           intent(in)    :: num_colors
      this%num_colors = num_colors
   end subroutine set_num_colors
   !===============================================================================


   !===============================================================================
   !> author: Seyed Ali Ghasemi
   pure elemental subroutine set_dim_colors(this, dim_colors)
      class(format_lut), intent(inout) :: this
      integer,           intent(in)    :: dim_colors
      this%dim_colors = dim_colors
   end subroutine set_dim_colors
   !===============================================================================


   !===============================================================================
   !> author: Seyed Ali Ghasemi
   pure elemental function get_num_colors(this) result(num_colors)
      class(format_lut), intent(in) :: this
      integer :: num_colors
      num_colors = this%num_colors
   end function get_num_colors
   !===============================================================================


   !===============================================================================
   !> author: Seyed Ali Ghasemi
   pure elemental function get_dim_colors(this) result(dim_colors)
      class(format_lut), intent(in) :: this
      integer :: dim_colors
      dim_colors = this%dim_colors
   end function get_dim_colors
   !===============================================================================


   !===============================================================================
   !> author: Seyed Ali Ghasemi
   pure subroutine set_colors(this, colors)
      class(format_lut),       intent(inout) :: this
      integer, dimension(:,:), intent(in)    :: colors
      this%colors = colors
   end subroutine set_colors
   !===============================================================================


   !===============================================================================
   !> author: Seyed Ali Ghasemi
   pure function get_colors(this) result(colors)
      class(format_lut), intent(in)        :: this
      integer, dimension(:,:), allocatable :: colors
      colors = this%colors
   end function get_colors
   !===============================================================================


   !===============================================================================
   !> author: Seyed Ali Ghasemi
   pure elemental subroutine deallocate_lut(this)
      class(format_lut), intent(inout) :: this
      if (allocated(this%colors)) deallocate(this%colors)
   end subroutine deallocate_lut
   !===============================================================================


   !===============================================================================
   !> author: Seyed Ali Ghasemi
   impure subroutine import(this, file_name, dim_colors)
      class(format_lut), intent(inout) :: this
      character(*),      intent(in)    :: file_name
      integer,           intent(in)    :: dim_colors
      integer, dimension(1,dim_colors) :: temp
      integer                          :: nunit, iostat, num_rows, i
      logical :: file_exists
      integer, dimension(dim_colors) :: buffer

      inquire(file=file_name//'.lut', exist=file_exists)
      if (file_exists) then
      open(newunit=nunit, file=file_name//'.lut', status='old', action='read', iostat=iostat)
      if (iostat /= 0) error stop 'Error opening the file.'
         num_rows = 0
         do
            read(nunit, *, iostat=iostat) temp(:,:)
            if (iostat /= 0) exit
            num_rows = num_rows + 1
         end do
         call this%set_num_colors(num_rows)
         call this%set_dim_colors(dim_colors)
         call this%allocate_colors()
         rewind(nunit)
         do i = 1, num_rows
            read(nunit, *) buffer
            this%colors(i,:) = buffer 
         end do
      close(nunit)
      else
         error stop 'File '//file_name//'.lut'//' does not exist!'
      end if
   end subroutine import
   !===============================================================================


   !===============================================================================
   !> author: Seyed Ali Ghasemi
   impure subroutine export(this, file_name)
      class(format_lut), intent(inout)    :: this
      character(*),      intent(in)       :: file_name
      integer                             :: nunit, i
      integer, dimension(this%dim_colors) :: buffer
      integer                             :: iostat

      open(newunit=nunit, file=file_name//'.lut', status='replace', action='write', iostat=iostat)
      if (iostat /= 0) error stop 'Error opening the file.'
      do i = 1, this%get_num_colors()
         buffer = this%colors(i,:)
         write(nunit, '(*(I3,1x))') buffer
      end do
      close(nunit)
   end subroutine export
   !===============================================================================

end module lut