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 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' 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' end select 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.' write(nunit, '(*(g0,1x))', advance='no') transpose(this%pixels) close(nunit) case ('P4', '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.' write(nunit) transpose(achar(this%pixels)) close(nunit) end select end subroutine export_pnm