📄 ppm_write.f90
字号:
subroutine ppm_check_data ( r, g, b, ierror, maxcol, ncol, nrow )
!*****************************************************************************80
!
!! PPM_CHECK_DATA checks pixel data.
!
! Modified:
!
! 28 May 1999
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, integer R(NROW,NCOL), G(NROW,NCOL), B(NROW,NCOL), contains the
! RGB pixel data.
!
! Output, integer IERROR, error flag.
! 0, no error detected.
! 1, the data is illegal.
!
! Input, integer MAXCOL, the maximum value.
!
! Input, integer NCOL, NROW, the number of rows and columns of data.
!
implicit none
integer ncol
integer nrow
integer b(nrow,ncol)
integer g(nrow,ncol)
integer i
integer ierror
integer j
integer maxcol
integer r(nrow,ncol)
ierror = 0
!
! Make sure no color is negative.
!
if ( minval ( r(1:nrow,1:ncol) ) < 0 .or. &
minval ( g(1:nrow,1:ncol) ) < 0 .or. &
minval ( b(1:nrow,1:ncol) ) < 0 ) then
ierror = 1
return
end if
!
! Make sure no color is greater than MAXCOL.
!
if ( maxcol < maxval ( r(1:nrow,1:ncol) ) .or. &
maxcol < maxval ( g(1:nrow,1:ncol) ) .or. &
maxcol < maxval ( b(1:nrow,1:ncol) ) ) then
ierror = 1
return
end if
return
end
subroutine ppma_write ( file_name, ierror, nrow, ncol, r, g, b )
!*****************************************************************************80
!
!! PPMA_WRITE writes an ASCII portable pixel map file.
!
! Discussion:
!
! PPM files can be viewed by XV.
!
! Programs to convert files to this format include:
!
! GIFTOPPM - GIF file
! PGMTOPPM - Portable Gray Map file
! PICTTOPPM - Macintosh PICT file
! XPMTOPPM - X11 pixmap file
!
! Various programs can convert other formats to PPM format, including:
!
! BMPTOPPM - Microsoft Windows BMP file.
!
! A PPM file can also be converted to other formats, by programs:
!
! PPMTOACAD - AutoCAD file
! PPMTOGIF - GIF file
! PPMTOPGM - Portable Gray Map file
! PPMTOPICT - Macintosh PICT file
! PPMTOPUZZ - X11 puzzle file
! PPMTORGB3 - 3 Portable Gray Map files
! PPMTOXPM - X11 pixmap file
! PPMTOYUV - Abekas YUV file
!
! Example:
!
! P3
! # feep.ppma created by PBMLIB(PPMA_WRITE).
! 4 4
! 15
! 0 0 0 0 0 0 0 0 0 15 0 15
! 0 0 0 0 15 7 0 0 0 0 0 0
! 0 0 0 0 0 0 0 15 7 0 0 0
! 15 0 15 0 0 0 0 0 0 0 0 0
!
! Modified:
!
! 07 October 2000
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, character ( len = * ) FILE_NAME, the name of the file to which
! the data should be written.
!
! Output, integer IERROR, an error flag.
! 0, no error.
! 1, the data was illegal.
! 2, the file could not be opened.
!
! Input, integer NROW, NCOL, the number of rows and columns of data.
!
! Input, integer R(NROW,NCOL), G(NROW,NCOL), B(NROW,NCOL), contain
! the red, green and blue values of each pixel. These should
! be positive.
!
implicit none
integer ncol
integer nrow
integer b(nrow,ncol)
logical, parameter :: debug = .false.
character ( len = * ) file_name
integer g(nrow,ncol)
integer i
integer ierror
integer ios
integer j
integer jhi
integer jlo
character ( len = 2 ) magic
integer maxcol
integer output_unit
integer r(nrow,ncol)
ierror = 0
!
! Compute the maximum color value.
!
maxcol = max ( &
maxval ( r(1:nrow,1:ncol) ), &
maxval ( g(1:nrow,1:ncol) ), &
maxval ( b(1:nrow,1:ncol) ) )
!
! Check the data.
!
call ppm_check_data ( r, g, b, ierror, maxcol, ncol, nrow )
if ( ierror /= 0 ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'PPMA_WRITE - Fatal error!'
write ( *, '(a)' ) ' Bad data detected by PPM_CHECK_DATA!'
ierror = 1
return
end if
!
! Open the file.
!
call get_unit ( output_unit )
open ( unit = output_unit, file = file_name, status = 'replace', &
form = 'formatted', access = 'sequential', iostat = ios )
if ( ios /= 0 ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'PPMA_WRITE - Fatal error!'
write ( *, '(a)' ) ' Could not open the file.'
ierror = 2
return
end if
!
! Write the data.
!
magic = 'P3'
write ( output_unit, '(a2)' ) magic
write ( output_unit, '(a)' ) '# ' // trim ( file_name ) &
// ' created by PPMLIB(PPMA_WRITE).'
write ( output_unit, '(i5,2x,i5)' ) ncol, nrow
write ( output_unit, '(i5)' ) maxcol
do i = 1, nrow
do jlo = 1, ncol, 4
jhi = min ( jlo + 3, ncol )
write ( output_unit, '(12i5)' ) ( r(i,j), g(i,j), b(i,j), j = jlo, jhi )
end do
end do
!
! Close the file.
!
close ( unit = output_unit )
!
! Report
!
if ( debug ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'PPMA_WRITE - Note:'
write ( *, '(a)' ) ' The data was checked and written.'
write ( *, '(a,i8)' ) ' Number of data rows NROW = ', nrow
write ( *, '(a,i8)' ) ' Number of data columns NCOL = ', ncol
write ( *, '(a,i8)' ) ' Maximum color value MAXCOL = ', maxcol
end if
return
end
subroutine timestamp ( )
!*****************************************************************************80
!
!! TIMESTAMP prints the current YMDHMS date as a time stamp.
!
! Example:
!
! 31 May 2001 9:45:54.872 AM
!
! Modified:
!
! 06 August 2005
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! None
!
implicit none
character ( len = 8 ) ampm
integer d
integer h
integer m
integer mm
character ( len = 9 ), parameter, dimension(12) :: month = (/ &
'January ', 'February ', 'March ', 'April ', &
'May ', 'June ', 'July ', 'August ', &
'September', 'October ', 'November ', 'December ' /)
integer n
integer s
integer values(8)
integer y
call date_and_time ( values = values )
y = values(1)
m = values(2)
d = values(3)
h = values(5)
n = values(6)
s = values(7)
mm = values(8)
if ( h < 12 ) then
ampm = 'AM'
else if ( h == 12 ) then
if ( n == 0 .and. s == 0 ) then
ampm = 'Noon'
else
ampm = 'PM'
end if
else
h = h - 12
if ( h < 12 ) then
ampm = 'PM'
else if ( h == 12 ) then
if ( n == 0 .and. s == 0 ) then
ampm = 'Midnight'
else
ampm = 'AM'
end if
end if
end if
write ( *, '(i2,1x,a,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) &
d, trim ( month(m) ), y, h, ':', n, ':', s, '.', mm, trim ( ampm )
return
end
subroutine get_unit ( iunit )
!*****************************************************************************80
!
!! GET_UNIT returns a free FORTRAN unit number.
!
! Discussion:
!
! A "free" FORTRAN unit number is an integer between 1 and 99 which
! is not currently associated with an I/O device. A free FORTRAN unit
! number is needed in order to open a file with the OPEN command.
!
! If IUNIT = 0, then no free FORTRAN unit could be found, although
! all 99 units were checked (except for units 5, 6 and 9, which
! are commonly reserved for console I/O).
!
! Otherwise, IUNIT is an integer between 1 and 99, representing a
! free FORTRAN unit. Note that GET_UNIT assumes that units 5 and 6
! are special, and will never return those values.
!
! Modified:
!
! 18 September 2005
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Output, integer IUNIT, the free unit number.
!
implicit none
integer i
integer ios
integer iunit
logical lopen
iunit = 0
do i = 1, 99
if ( i /= 5 .and. i /= 6 .and. i /= 9 ) then
inquire ( unit = i, opened = lopen, iostat = ios )
if ( ios == 0 ) then
if ( .not. lopen ) then
iunit = i
return
end if
end if
end if
end do
return
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -