include "fflincorr.h" # lin_get_a -- get array of a values # This routine takes either a single number or reads a file to # obtain parameter values and A coefficients for some image format. # # Phil Hodge, 7-Jul-1992 Subroutine created. procedure lin_get_a (input, ap) char input[ARB] # i: one value or name of file pointer ap # o: pointer to array of a coefficients #-- pointer sp pointer lbuf # scratch for input buffer pointer word # scratch for word read from lbuf real a # numerical value read from string int fd # for input file int nfound # number of parameters found in A file int relay, nx, ny, nsamps, nlines, sampoff, lineoff, zoom int nchar, ip, wp # for ctor, etc int i, j bool done # for reading parameters from file int ctoi(), ctor(), ctowrd(), strncmp(), open(), getline() begin ip = 1 nchar = ctor (input, ip, a) if (nchar > 0 && input[ip] == EOS) { # It's just a single number. call malloc (ap, A_NPAR + 1, TY_STRUCT) # A_RELAY = 0 is a flag that we have just a single number. A_RELAY(ap) = 0 A_VAL(ap,1,1) = a # These are all irrelevant. A_NX(ap) = 1 A_NY(ap) = 1 A_NSAMPS(ap) = 1024 A_NLINES(ap) = 1024 A_SAMPOFF(ap) = 0 A_LINEOFF(ap) = 0 A_ZOOM(ap) = 1 } else { # It's a file name. call smark (sp) call salloc (lbuf, SZ_LINE, TY_CHAR) call salloc (word, SZ_FNAME, TY_CHAR) fd = open (input, READ_ONLY, TEXT_FILE) # Assign initial (invalid) values. relay = -1 nx = -1 ny = -1 nsamps = -1 nlines = -1 sampoff = -1 lineoff = -1 zoom = -1 # Read the parameters. done = false nfound = 0 while (!done) { nchar = getline (fd, Memc[lbuf]) if (Memc[lbuf] == '#' || Memc[lbuf] == '\n') next ip = 1 if (ctowrd (Memc[lbuf], ip, Memc[word], SZ_FNAME) < 1) call error (1, "can't read file of A values") call strlwr (Memc[word]) wp = 1 if (strncmp (Memc[word], "relay", 5) == 0) { nchar = ctowrd (Memc[lbuf], ip, Memc[word], SZ_FNAME) if (Memc[word] == '=') # read next word nchar = ctowrd (Memc[lbuf], ip, Memc[word], SZ_FNAME) if (ctoi (Memc[word], wp, relay) < 0) call error (1, "can't read RELAY from file") nfound = nfound + 1 } else if (strncmp (Memc[word], "nx", 2) == 0) { nchar = ctowrd (Memc[lbuf], ip, Memc[word], SZ_FNAME) if (Memc[word] == '=') # read next word nchar = ctowrd (Memc[lbuf], ip, Memc[word], SZ_FNAME) if (ctoi (Memc[word], wp, nx) < 0) call error (1, "can't read NX from file") nfound = nfound + 1 } else if (strncmp (Memc[word], "ny", 2) == 0) { nchar = ctowrd (Memc[lbuf], ip, Memc[word], SZ_FNAME) if (Memc[word] == '=') # read next word nchar = ctowrd (Memc[lbuf], ip, Memc[word], SZ_FNAME) if (ctoi (Memc[word], wp, ny) < 0) call error (1, "can't read NY from file") nfound = nfound + 1 } else if (strncmp (Memc[word], "nsamps", 6) == 0) { nchar = ctowrd (Memc[lbuf], ip, Memc[word], SZ_FNAME) if (Memc[word] == '=') # read next word nchar = ctowrd (Memc[lbuf], ip, Memc[word], SZ_FNAME) if (ctoi (Memc[word], wp, nsamps) < 0) call error (1, "can't read NSAMPS from file") nfound = nfound + 1 } else if (strncmp (Memc[word], "nlines", 6) == 0) { nchar = ctowrd (Memc[lbuf], ip, Memc[word], SZ_FNAME) if (Memc[word] == '=') # read next word nchar = ctowrd (Memc[lbuf], ip, Memc[word], SZ_FNAME) if (ctoi (Memc[word], wp, nlines) < 0) call error (1, "can't read NLINES from file") nfound = nfound + 1 } else if (strncmp (Memc[word], "sampoff", 7) == 0) { nchar = ctowrd (Memc[lbuf], ip, Memc[word], SZ_FNAME) if (Memc[word] == '=') # read next word nchar = ctowrd (Memc[lbuf], ip, Memc[word], SZ_FNAME) if (ctoi (Memc[word], wp, sampoff) < 0) call error (1, "can't read SAMPOFF from file") nfound = nfound + 1 } else if (strncmp (Memc[word], "lineoff", 7) == 0) { nchar = ctowrd (Memc[lbuf], ip, Memc[word], SZ_FNAME) if (Memc[word] == '=') # read next word nchar = ctowrd (Memc[lbuf], ip, Memc[word], SZ_FNAME) if (ctoi (Memc[word], wp, lineoff) < 0) call error (1, "can't read LINEOFF from file") nfound = nfound + 1 } else if (strncmp (Memc[word], "zoom", 4) == 0) { nchar = ctowrd (Memc[lbuf], ip, Memc[word], SZ_FNAME) if (Memc[word] == '=') # read next word nchar = ctowrd (Memc[lbuf], ip, Memc[word], SZ_FNAME) if (ctoi (Memc[word], wp, zoom) < 0) call error (1, "can't read ZOOM from file") nfound = nfound + 1 } else { call eprintf ("unexpected line: %s\n") call pargstr (Memc[lbuf]) call error (1, "") } if (nfound >= A_NPAR) done = true # done reading parameters } # Have we got them all? if (relay < 0 || nx < 0 || ny < 0 || nsamps < 0 || nlines < 0 || sampoff < 0 || lineoff < 0 || zoom < 0) call error (1, "not all parameters gotten from file") # Allocate space for the array, and fill in parameter values. call malloc (ap, A_NPAR + nx * ny, TY_STRUCT) A_RELAY(ap) = relay A_NX(ap) = nx A_NY(ap) = ny A_NSAMPS(ap) = nsamps A_NLINES(ap) = nlines A_SAMPOFF(ap) = 1024 - nsamps - sampoff # "flip" sampoff A_LINEOFF(ap) = lineoff A_ZOOM(ap) = zoom # Read the data. done = false j = ny # initial value while (!done) { nchar = getline (fd, Memc[lbuf]) if (nchar == EOF) { if (j > 0) call error (1, "not enough lines of data") else done = true } else if (Memc[lbuf] == '#' || Memc[lbuf] == '\n') { next # ignore comments and blank lines } else if (j < 1) { call error (1, "too many lines of data") } else { ip = 1 do i = 1, nx { if (ctor (Memc[lbuf], ip, A_VAL(ap,i,j)) < 1) call error (1, "not all A values found") } j = j - 1 } } call close (fd) call sfree (sp) } end