read_header Subroutine

private impure subroutine read_header(this, nunit, pos)

Reads the header of the PNM image from a file. Required before reading the pixels from the file.

Arguments

Type IntentOptional Attributes Name
type(format_pnm), intent(inout) :: this
integer, intent(in) :: nunit
integer, intent(out) :: pos

Called by

proc~~read_header~~CalledByGraph proc~read_header read_header proc~import_pnm format_pnm%import_pnm proc~import_pnm->proc~read_header program~demo_ppm demo_ppm program~demo_ppm->proc~import_pnm program~test10 test10 program~test10->proc~import_pnm program~test11 test11 program~test11->proc~import_pnm program~test12 test12 program~test12->proc~import_pnm program~test13 test13 program~test13->proc~import_pnm program~test14 test14 program~test14->proc~import_pnm program~test7 test7 program~test7->proc~import_pnm program~test8 test8 program~test8->proc~import_pnm program~test9 test9 program~test9->proc~import_pnm

Source Code

   impure subroutine read_header(this, nunit, pos)
      type(format_pnm), intent(inout) :: this
      integer,          intent(in)    :: nunit
      integer,          intent(out)   :: pos

      character(len=256) :: line
      integer            :: iostat
      logical            :: have_dims

      read(nunit,'(a)', iostat=iostat) line
      if (iostat /= 0) error stop 'read_header: failed to read magic number.'
      this%magic_number = adjustl(line(1:min(len_trim(line), len(this%magic_number))))

      this%comment = ''
      have_dims = .false.

      do
         inquire(nunit, pos=pos)
         read(nunit,'(a)', iostat=iostat) line
         if (iostat /= 0) error stop 'read_header: unexpected EOF in header.'

         line = adjustl(line)

         if (len_trim(line) == 0) cycle

         if (line(1:1) == '#') then
            if (len_trim(this%comment) > 0) this%comment = this%comment//' '
            if (len_trim(line) >= 2) then
               if (line(2:2) == ' ') then
                  this%comment = this%comment//trim(line(3:))
               else
                  this%comment = this%comment//trim(line(2:))
               end if
            end if
            cycle
         end if

         read(line, *, iostat=iostat) this%width, this%height
         if (iostat /= 0) error stop 'read_header: failed to parse width/height.'
         have_dims = .true.
         exit
      end do

      if (.not. have_dims) error stop 'read_header: missing width/height.'

      inquire(nunit, pos=pos)

      if (this%file_format == 'pgm' .or. this%file_format == 'ppm') then
         do
            inquire(nunit, pos=pos)
            read(nunit,'(a)', iostat=iostat) line
            if (iostat /= 0) error stop 'read_header: unexpected EOF reading max_color.'
            line = adjustl(line)
            if (len_trim(line) == 0) cycle
            if (line(1:1) == '#') cycle
            read(line, *, iostat=iostat) this%max_color
            if (iostat /= 0) error stop 'read_header: failed to parse max_color.'
            exit
         end do
         inquire(nunit, pos=pos)
      end if
   end subroutine read_header