Writes the PNM image to a file.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(format_pnm), | intent(inout) | :: | this | |||
| character(len=*), | intent(in) | :: | file_name | |||
| character(len=*), | intent(in), | optional | :: | encoding |
impure subroutine export_pnm(this, file_name, encoding) class(format_pnm), intent(inout) :: this character(*), intent(in) :: file_name character(*), intent(in), optional :: encoding integer :: nunit integer :: iostat integer :: i integer(ik), allocatable :: row(:) if (present(encoding)) then call this%set_format(encoding) select case (this%encoding) case ('ascii','plain') select case (this%file_format) case ('pbm') this%magic_number = 'P1' case ('pgm') this%magic_number = 'P2' case ('ppm') this%magic_number = 'P3' case default error stop 'error: unsupported file format for ascii(plain) encoding.' end select case ('binary','raw') select case (this%file_format) case ('pbm') this%magic_number = 'P4' case ('pgm') this%magic_number = 'P5' case ('ppm') this%magic_number = 'P6' case default error stop 'error: unsupported file format for binary(raw) encoding.' end select case default error stop 'error: unsupported encoding. Supported encodings are ascii(plain) and binary(raw).' end select end if select case (this%magic_number) case ('P1', 'P2', 'P3') open (newunit = nunit, file = file_name//'.'//this%file_format,& status='replace', iostat=iostat, form='formatted', action='write') if (iostat /= 0) error stop 'Error opening the file.' call write_header(this, nunit) close(nunit) open (newunit = nunit, file = file_name//'.'//this%file_format,& status='old', iostat=iostat, form='formatted', action='write', position='append') if (iostat /= 0) error stop 'Error opening the file.' allocate(row(size(this%pixels, 2))) do i = 1, size(this%pixels, 1) row = this%pixels(i,:) write(nunit, '(*(g0,1x))', advance='yes') row end do close(nunit) case ('P4') open (newunit = nunit, file = file_name//'.'//this%file_format,& status='replace', iostat=iostat, form='formatted', action='write') if (iostat /= 0) error stop 'Error opening the file.' call write_header(this, nunit) close(nunit) open (newunit = nunit, file = file_name//'.'//this%file_format,& status='old', iostat=iostat, access='stream', form='unformatted', action='write', position='append') if (iostat /= 0) error stop 'Error opening the file.' write(nunit) encode_binary_pbm_pixels(this) allocate(row(size(this%pixels, 2))) do i = 1, size(this%pixels, 1) row = this%pixels(i,:) write(nunit) achar(row) end do close(nunit) case('P5', 'P6') open (newunit = nunit, file = file_name//'.'//this%file_format,& status='replace', iostat=iostat, form='formatted', action='write') if (iostat /= 0) error stop 'Error opening the file.' call write_header(this, nunit) close(nunit) open (newunit = nunit, file = file_name//'.'//this%file_format,& status='old', iostat=iostat, access='stream', form='unformatted', action='write', position='append') if (iostat /= 0) error stop 'Error opening the file.' allocate(row(size(this%pixels, 2))) do i = 1, size(this%pixels, 1) row = this%pixels(i,:) write(nunit) achar(row) end do close(nunit) case default error stop 'error: unsupported magic number. Supported magic numbers are P1, P2, P3, P4, P5, and P6.' end select end subroutine export_pnm