#include #include main() /******************************************************************/ /* extract_image: This program allows a user to read in a data */ /* file written as integer*2, extract a specific portion of */ /* that file, and write out the resulting portion. */ /******************************************************************/ { FILE *inf, *opf, *fopen(); static char *cdata,hdr[10000]; static int *idata; static short int *sdata; static float *fdata; int *ivector(); short int *isvector(); char *cvector(); float *vector(); short int istat,ival; int i,j,k,l,c,nlat,nlon,nbnd,nbmin,nbmax; int sl,ss,sb,nl,ns,nb,dtype,dlen,hlen; char ans,ans2,s[10]; char infile[60],outfile[60]; printf(" Please enter the input file name: "); gets(infile); while ((inf = fopen(infile,"r")) == NULL) { printf(" Error in opening the input file, please try again! \n"); printf(" Please enter the input file name: "); gets(infile); } printf(" Please enter the data type: (i-int, s-short, c-char, f-float)"); gets(s); if (s[0] == 'i' || s[0] == 'I') { dtype = 0; dlen = 4; } else if (s[0] == 's' || s[0] == 'S') { dtype = 1; dlen = 2; } else if (s[0] == 'c' || s[0] == 'C') { dtype = 2; dlen = 1; } else if (s[0] == 'f' || s[0] == 'F') { dtype = 3; dlen = 4; } hlen=0; printf(" Please enter the header length (default=0): "); gets(s); if (s[0] != '\0') hlen = atoi(s); printf(" Please enter the number of lines: "); gets(s); nlat = atoi(s); printf(" Please enter the number of pixels per line: "); gets(s); nlon = atoi(s); printf(" Please enter the number of bands: "); gets(s); nbnd = atoi(s); printf(" Please enter the output file name: "); gets(outfile); while ((opf = fopen(outfile,"w")) == NULL) { printf(" Error in opening the output file, please try again! \n"); printf(" Please enter the output file name: "); gets(outfile); } printf(" Please enter the starting line to extract: "); gets(s); sl = atoi(s); printf(" Please enter the starting sample to extract: "); gets(s); ss = atoi(s); printf(" Please enter the starting band to extract: "); gets(s); sb = atoi(s); printf(" Please enter the number of lines to extract: "); gets(s); nl = atoi(s); printf(" Please enter the number of samples to extract: "); gets(s); ns = atoi(s); printf(" Please enter the number of bands to extract: "); gets(s); nb = atoi(s); /**************************/ /* Allocate data array */ /**************************/ if (dtype == 0) idata=ivector(nlat*nlon); else if (dtype == 1) sdata=isvector(nlat*nlon); else if (dtype == 2) cdata=cvector(nlat*nlon); else if (dtype == 3) fdata=vector(nlat*nlon); /****************************/ /* begin processing */ /****************************/ fread(hdr,1,hlen,inf); for (l=0; l= sb) { if (l+1 > sb+nb-1) exit(1); for (j=sl-1; j>8)&0xff; if (opf != NULL) { putc(c,opf); c = *rvptr&0xff; putc(c,opf); retval = 0L; } else retval = -1L; return retval; } strcpy(s,t) char s[],t[]; { int i; i = 0; while ((s[i] = t[i]) != '\0') i++; } toupper(c) char c; { if (c >= 'a' && c <= 'z') c = c - 'a' + 'A'; return c; }