subroutine ch_cap ( ch ) !*****************************************************************************80 ! !! ch_cap() capitalizes a single character. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 30 November 1998 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input/output, character CH, the character to capitalize. ! implicit none character ch integer itemp itemp = ichar ( ch ) if ( 97 <= itemp .and. itemp <= 122 ) then ch = char ( itemp - 32 ) end if return end subroutine get_unit ( iunit ) !*****************************************************************************80 ! !! get_unit() returns a free FORTRAN unit number. ! ! Discussion: ! ! A "free" FORTRAN unit number is a value 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 a value 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. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 26 October 2008 ! ! 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 subroutine getint ( done, ierror, inunit, ival, string ) !*****************************************************************************80 ! !! getint() reads an integer from a file. ! ! Discussion: ! ! The file, or at least the part read by GETINT, is assumed to ! contain nothing but integers. These integers may be separated ! by spaces, or appear on separate lines. Comments, which begin ! with "#" and extend to the end of the line, may appear anywhere. ! ! Each time GETINT is called, it tries to read the next integer ! it can find. It remembers where it was in the current line ! of text. ! ! The user should open a text file on FORTRAN unit INUNIT, ! set STRING = ' ' and DONE = TRUE. The GETINT routine will take ! care of reading in a new STRING as necessary, and extracting ! as many integers as possible from the line of text before ! reading in the next line. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 October 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input/output, logical DONE. ! On input, if this is the first call, or the user has changed ! STRING, then set DONE = TRUE. ! On output, if there is no more data to be read from STRING, ! then DONE is TRUE. ! ! Output, integer IERROR, error flag. ! 0, no error occurred. ! 1, an error occurred while trying to read the integer. ! ! Input, integer INUNIT, the FORTRAN unit from which to read. ! ! Output, integer IVAL, the integer that was read. ! ! Input/output, character ( len = * ) STRING, the text of the most recently ! read line of the file. ! implicit none logical done integer i integer ierror integer inunit integer ios integer ival integer last character ( len = * ) string character ( len = 255 ) word do call word_next_rd ( string, word, done ) if ( .not. done ) then exit end if read ( inunit, '(a)', iostat = ios ) string if ( ios /= 0 ) then ierror = 1 return end if i = index ( string, '#' ) if ( i /= 0 ) then string(i:) = ' ' end if end do call s_to_i4 ( word, ival, ierror, last ) if ( ierror /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'GETINT - Fatal error!' write ( *, '(a)' ) ' Error trying to convert string to integer.' stop end if return end subroutine gray_median_news ( m, n, gray, gray2 ) !*****************************************************************************80 ! !! gray_median_news() uses a median NEWS filter on a gray scale image. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 27 August 2011 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns of pixels. ! ! Input, integer GRAY(M,N), the noisy grayscale data. ! ! Output, integer GRAY2(M,N), the grayscale data for the ! filtered image. ! implicit none integer m integer n integer gray(m,n) integer gray2(m,n) integer i integer j integer p(5) ! ! Process the main part of the image: ! do i = 2, m - 1 do j = 2, n - 1 p(1) = gray(i-1,j ) p(2) = gray(i+1,j ) p(3) = gray(i ,j+1) p(4) = gray(i ,j-1) p(5) = gray(i ,j ) call i4vec_median ( 5, p, gray2(i,j) ) end do end do ! ! Process the four borders. ! Get an odd number of data points, ! do i = 2, m - 1 j = 1 p(1) = gray(i-1,j ) p(2) = gray(i+1,j ) p(3) = gray(i ,j ) p(4) = gray(i ,j+1) p(5) = gray(i ,j+2) call i4vec_median ( 5, p, gray2(i,j) ) j = n p(1) = gray(i-1,j ) p(2) = gray(i+1,j ) p(3) = gray(i ,j-2) p(4) = gray(i ,j-1) p(5) = gray(i ,j ) call i4vec_median ( 5, p, gray2(i,j) ) end do do j = 2, n - 1 i = 1 p(1) = gray(i ,j ) p(2) = gray(i+1,j ) p(3) = gray(i+2,j ) p(4) = gray(i ,j-1) p(5) = gray(i ,j+1) call i4vec_median ( 5, p, gray2(i,j) ) i = m - 1 p(1) = gray(i-2,j ) p(2) = gray(i-1,j ) p(3) = gray(i ,j ) p(4) = gray(i ,j-1) p(5) = gray(i ,j+1) call i4vec_median ( 5, p, gray2(i,j) ) end do ! ! Process the four corners. ! i = 1 j = 1 p(1) = gray(i+1,j ) p(2) = gray(i ,j ) p(3) = gray(i ,j+1) call i4vec_median ( 3, p, gray2(i,j) ) i = 1 j = n p(1) = gray(i+1,j ) p(2) = gray(i ,j ) p(3) = gray(i ,j-1) call i4vec_median ( 3, p, gray2(i,j) ) i = m j = 1 p(1) = gray(i-1,j ) p(2) = gray(i ,j ) p(3) = gray(i ,j+1) call i4vec_median ( 3, p, gray2(i,j) ) i = m - 1 j = n - 1 p(1) = gray(i-1,j ) p(2) = gray(i ,j ) p(3) = gray(i ,j-1) call i4vec_median ( 3, p, gray2(i,j) ) return end subroutine i4vec_frac ( n, a, k, frac ) !*****************************************************************************80 ! !! i4vec_frac() searches for the K-th smallest element in an I4VEC. ! ! Discussion: ! ! An I4VEC is a vector of I4's. ! ! Hoare's algorithm is used. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 30 September 2009 ! ! Author: ! ! This version by John Burkardt ! ! Parameters: ! ! Input, integer N, the number of elements of A. ! ! Input/output, integer A(N), array to search. On output, ! the elements of A have been somewhat rearranged. ! ! Input, integer K, the fractile to be sought. If K = 1, the ! minimum entry is sought. If K = N, the maximum is sought. ! Other values of K search for the entry which is K-th in size. ! K must be at least 1, and no greater than N. ! ! Output, integer FRAC, the value of the K-th fractile of A. ! implicit none integer n integer a(n) integer frac integer i integer iryt integer ix integer j integer k integer left integer t if ( n <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'I4VEC_FRAC - Fatal error!' write ( *, '(a,i8)' ) ' Illegal nonpositive value of N = ', n stop end if if ( k <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'I4VEC_FRAC - Fatal error!' write ( *, '(a,i8)' ) ' Illegal nonpositive value of K = ', k stop end if if ( n < k ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'I4VEC_FRAC - Fatal error!' write ( *, '(a,i8)' ) ' Illegal N < K, K = ', k stop end if left = 1 iryt = n do if ( iryt <= left ) then frac = a(k) exit end if ix = a(k) i = left j = iryt do if ( j < i ) then if ( j < k ) then left = i end if if ( k < i ) then iryt = j end if exit end if ! ! Find I so that IX <= A(I). ! do while ( a(i) < ix ) i = i + 1 end do ! ! Find J so that A(J) <= IX. ! do while ( ix < a(j) ) j = j - 1 end do if ( i <= j ) then t = a(i) a(i) = a(j) a(j) = t i = i + 1 j = j - 1 end if end do end do return end subroutine i4vec_median ( n, a, median ) !*****************************************************************************80 ! !! i4vec_median() returns the median of an unsorted I4VEC. ! ! Discussion: ! ! An I4VEC is a vector of I4's. ! ! Hoare's algorithm is used. The values of the vector are ! rearranged by this routine. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 18 September 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the number of elements of A. ! ! Input/output, integer A(N), the array to search. On output, ! the order of the elements of A has been somewhat changed. ! ! Output, integer MEDIAN, the value of the median of A. ! implicit none integer n integer a(n) integer k integer median k = ( n + 1 ) / 2 call i4vec_frac ( n, a, k, median ) return end subroutine pgma_read_data ( file_in_unit, row_num, col_num, g ) !*****************************************************************************80 ! !! pgma_read_data() reads the data in an ASCII PGM file. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 04 June 2010 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_IN_UNIT, the unit number of the file. ! ! Input, integer ROW_NUM, COL_NUM, the number of rows and ! columns of data. ! ! Output, integer G(ROW_NUM,COL_NUM), the gray data. ! implicit none integer col_num integer row_num logical done integer file_in_unit integer g(row_num,col_num) integer i integer ierror integer j character ( len = 255 ) string ierror = 0 done = .true. string = ' ' do i = 1, row_num do j = 1, col_num call getint ( done, ierror, file_in_unit, g(i,j), string ) if ( ierror /= 0 ) then close ( unit = file_in_unit ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'PGMA_READ_DATA - Fatal error!' write ( *, '(a)' ) ' Problem reading G data.' stop end if end do end do return end subroutine pgma_read_header ( file_in_unit, row_num, col_num, g_max ) !*****************************************************************************80 ! !! pgma_read_header() reads the header of an ASCII PGM file. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 04 June 2010 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_IN_UNIT, the unit number of the file. ! ! Output, integer ROW_NUM, COL_NUM, the number of rows and ! columns of data. ! ! Output, integer G_MAX, the maximum gray value. ! implicit none logical done integer file_in_unit integer ierror integer ios character ( len = 2 ) magic integer g_max integer col_num integer row_num logical s_eqi character ( len = 255 ) string ! ! Read the first line of data, which must begin with the magic number. ! read ( file_in_unit, '(a)', iostat = ios ) magic if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'PGMA_READ_HEADER - Fatal error!' write ( *, '(a)' ) ' End or error while reading file.' ierror = 2 stop end if if ( .not. s_eqi ( magic, 'P2' ) ) then ierror = 3 write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'PGMA_READ_HEADER - Fatal error.' write ( *, '(a)' ) ' First two bytes are not magic number "P2".' write ( *, '(a)' ) ' First two bytes are: "' // magic // '".' stop end if ! ! Now search for COL_NUM, ROW_NUM, and G_MAX. ! done = .true. string = ' ' call getint ( done, ierror, file_in_unit, col_num, string ) if ( ierror /= 0 ) then close ( unit = file_in_unit ) ierror = 4 write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'PGMA_READ_HEADER - Fatal error!' write ( *, '(a)' ) ' Problem reading COL_NUM.' stop end if call getint ( done, ierror, file_in_unit, row_num, string ) if ( ierror /= 0 ) then ierror = 4 close ( unit = file_in_unit ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'PGMA_READ_HEADER - Fatal error!' write ( *, '(a)' ) ' Problem reading ROW_NUM.' stop end if call getint ( done, ierror, file_in_unit, g_max, string ) if ( ierror /= 0 ) then ierror = 4 close ( unit = file_in_unit ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'PGMA_READ_HEADER - Fatal error!' write ( *, '(a)' ) ' Problem reading G_MAX.' stop end if return end subroutine pgma_write ( file_out_name, row_num, col_num, g ) !*****************************************************************************80 ! !! pgma_write() writes an ASCII PGM file. ! ! Example: ! ! P2 ! # feep.pgm ! 24 7 ! 15 ! 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ! 0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0 ! 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0 ! 0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15 0 ! 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 0 0 ! 0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 0 0 0 0 ! 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 01 March 2003 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_OUT_NAME, the name of the file ! to which the data should be written. ! ! Input, integer ROW_NUM, COL_NUM, the number of rows and ! columns of data. ! ! Input, integer G(ROW_NUM,COL_NUM), the gray value of each ! pixel. These should be nonnegative. ! implicit none integer col_num integer row_num logical, parameter :: debug = .false. character ( len = * ) file_out_name integer file_out_unit integer g(row_num,col_num) integer ios integer g_max ! ! Compute the maximum color value. ! g_max = maxval ( g(1:row_num,1:col_num) ) ! ! Open the file. ! call get_unit ( file_out_unit ) open ( unit = file_out_unit, file = file_out_name, status = 'replace', & form = 'formatted', access = 'sequential', iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'PGMA_WRITE - Fatal error!' write ( *, '(a)' ) ' Could not open the file.' stop end if ! ! Write the header. ! call pgma_write_header ( file_out_name, file_out_unit, row_num, col_num, & g_max ) ! ! Write the data. ! call pgma_write_data ( file_out_unit, row_num, col_num, g ) ! ! Close the file. ! close ( unit = file_out_unit ) ! ! Report ! if ( debug ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'PGMA_WRITE - Note:' write ( *, '(a)' ) ' The data was checked and written.' write ( *, '(a,i8)' ) ' Number of data rows ROW_NUM = ', row_num write ( *, '(a,i8)' ) ' Number of data columns COL_NUM = ', col_num write ( *, '(a,i8)' ) ' Maximum gray value G_MAX = ', g_max end if return end subroutine pgma_write_data ( file_out_unit, row_num, col_num, g ) !*****************************************************************************80 ! !! pgma_write_data() writes the data of an ASCII PGM file. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 28 February 2003 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer FILE_OUT_UNIT, the output file unit number. ! ! Input, integer ROW_NUM, COL_NUM, the number of rows and ! columns of data. ! ! Input, integer G(ROW_NUM,COL_NUM), the gray value of each ! pixel. These should be nonnegative. ! implicit none integer col_num integer row_num integer file_out_unit integer g(row_num,col_num) integer i integer jhi integer jlo do i = 1, row_num do jlo = 1, col_num, 12 jhi = min ( jlo + 11, col_num ) write ( file_out_unit, '(12i5)' ) g(i,jlo:jhi) end do end do return end subroutine pgma_write_header ( file_out_name, file_out_unit, row_num, col_num, & g_max ) !*****************************************************************************80 ! !! pgma_write_header() writes the header of an ASCII PGM file. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 01 March 2003 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_OUT_NAME, the name of the output file. ! ! Input, integer FILE_OUT_UNIT, the output file unit number. ! ! Input, integer ROW_NUM, COL_NUM, the number of rows and ! columns of data. ! ! Input, integer G_MAX, the maximum gray value. ! implicit none character ( len = * ) file_out_name integer file_out_unit character ( len = 2 ) :: magic = 'P2' integer g_max integer col_num integer row_num ! ! Write the header. ! write ( file_out_unit, '(a2)' ) magic write ( file_out_unit, '(a)' ) '# ' // trim ( file_out_name ) & // ' created by PGMA_IO::PGMA_WRITE.' write ( file_out_unit, '(i8,2x,i8)' ) col_num, row_num write ( file_out_unit, '(i8)' ) g_max return end function s_eqi ( s1, s2 ) !*****************************************************************************80 ! !! s_eqi() is a case insensitive comparison of two strings for equality. ! ! Example: ! ! S_EQI ( 'Anjana', 'ANJANA' ) is .TRUE. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 14 April 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) S1, S2, the strings to compare. ! ! Output, logical S_EQI, the result of the comparison. ! implicit none character c1 character c2 integer i integer len1 integer len2 integer lenc logical s_eqi character ( len = * ) s1 character ( len = * ) s2 len1 = len ( s1 ) len2 = len ( s2 ) lenc = min ( len1, len2 ) s_eqi = .false. do i = 1, lenc c1 = s1(i:i) c2 = s2(i:i) call ch_cap ( c1 ) call ch_cap ( c2 ) if ( c1 /= c2 ) then return end if end do do i = lenc + 1, len1 if ( s1(i:i) /= ' ' ) then return end if end do do i = lenc + 1, len2 if ( s2(i:i) /= ' ' ) then return end if end do s_eqi = .true. return end subroutine s_to_i4 ( s, ival, ierror, last ) !*****************************************************************************80 ! !! s_to_i4() reads an I4 from a string. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 28 August 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) S, a string to be examined. ! ! Output, integer IVAL, the integer value read from the string. ! If blank, then IVAL will be returned 0. ! ! Output, integer IERROR, an error flag. ! 0, no error. ! 1, an error occurred. ! ! Output, integer LAST, the last character that was ! part of the representation of IVAL. ! implicit none character c integer i integer ierror integer isgn integer istate integer ival integer last integer lens character ( len = * ) s ierror = 0 istate = 0 isgn = 1 ival = 0 lens = len ( s ) i = 0 do i = i + 1 c = s(i:i) if ( istate == 0 ) then if ( c == ' ' ) then else if ( c == '-' ) then istate = 1 isgn = -1 else if ( c == '+' ) then istate = 1 isgn = + 1 else if ( lle ( '0', c ) .and. lle ( c, '9' ) ) then istate = 2 ival = ichar ( c ) - ichar ( '0' ) else ierror = 1 exit end if else if ( istate == 1 ) then if ( c == ' ' ) then else if ( lle ( '0', c ) .and. lle ( c, '9' ) ) then istate = 2 ival = ichar ( c ) - ichar ( '0' ) else ierror = 1 exit end if else if ( istate == 2 ) then if ( lle ( '0', c ) .and. lle ( c, '9' ) ) then ival = 10 * ival + ichar ( c ) - ichar ( '0' ) else istate = 3 end if end if ! ! Continue or exit? ! if ( istate == 3 ) then ival = isgn * ival last = i - 1 exit else if ( lens <= i ) then if ( istate == 2 ) then ival = isgn * ival last = lens else ierror = 1 last = 0 end if exit end if end do return end subroutine word_next_rd ( line, word, done ) !*****************************************************************************80 ! !! word_next_rd() "reads" words from a string, one at a time. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 27 June 2008 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) LINE, a string, presumably containing ! words separated by spaces. ! ! Output, character ( len = * ) WORD. ! If DONE is FALSE, ! WORD contains the "next" word read from LINE. ! Else ! WORD is blank. ! ! Input/output, logical DONE. ! On input, on the first call, or with a fresh value of LINE, ! set DONE to TRUE. ! Else ! leave it at the output value of the previous call. ! On output, if a new nonblank word was extracted from LINE ! DONE is FALSE ! ELSE ! DONE is TRUE. ! If DONE is TRUE, then you need to provide a new LINE of data. ! ! Local Parameters: ! ! NEXT is the next location in LINE that should be searched. ! implicit none logical done integer ilo integer lenl character ( len = * ) line integer, save :: next = 1 character ( len = 1 ), parameter :: TAB = char(9) character ( len = * ) word lenl = len_trim ( line ) if ( done ) then next = 1 done = .false. end if ! ! Beginning at index NEXT, search LINE for the next nonblank. ! ilo = next do ! ! ...LINE(NEXT:LENL) is blank. Return with WORD=' ', and DONE=TRUE. ! if ( lenl < ilo ) then word = ' ' done = .true. next = lenl + 1 return end if ! ! ...If the current character is blank, skip to the next one. ! if ( line(ilo:ilo) /= ' ' .and. line(ilo:ilo) /= TAB ) then exit end if ilo = ilo + 1 end do ! ! To get here, ILO must be the index of the nonblank starting ! character of the next word. ! ! Now search for the LAST nonblank character. ! next = ilo + 1 do if ( lenl < next ) then word = line(ilo:next-1) return end if if ( line(next:next) == ' ' .or. line(next:next) == TAB ) then exit end if next = next + 1 end do word = line(ilo:next-1) return end