]> www.wagner.pp.ru Git - oss/fgis.git/blob - epu/bil2epp.c
First checked in version
[oss/fgis.git] / epu / bil2epp.c
1 #define __USE_GNU 
2 #define __USE_XOPEN
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <epp.h>
8 #include <ctype.h>
9 #include <eppl_ut.h>
10 void error(const char * message)
11 {
12   fprintf(stderr,"%s\n",message);
13   exit(1);
14 }
15 void swab(const void *from,void *to,size_t size);
16 void dcopy(const void *from,void *to,size_t size)
17 {
18   memcpy(to,from,size);
19 }
20
21 int convert(EPP *epp,FILE *in,void (*copyrow)(const void *,void*,size_t),
22           int shift)
23 {
24   int bytes=(epp->lc-epp->fc)*2,row,col,i,rows=epp->lr-epp->fr;
25   short int *buf,*buf2,*p;
26   buf=malloc(bytes);
27   buf2=malloc(bytes);
28   for (row=epp->fr,i=1;row<epp->lr;row++,i++) {
29     fread(buf,bytes,1,in);
30     copyrow(buf,buf2,bytes);
31     for (col=epp->fc,p=buf2;col<epp->lc;col++,p++) {
32         epp_put(epp,col,row,(unsigned short)(*p+shift));
33         if (EndLineProc) 
34            if (EndLineProc(row,i,rows)) return -1;
35     }
36   }
37   return 0;
38 }
39
40 EPP *read_header(char *filename,int *swapbytes,int shift)
41 { char buf[1024],*p;
42   int rows=0,cols=0,nodata=32768;
43   FILE *f=fopen(filename,"r");
44   double X1=0,Y1=0,X2=0,Y2=0,DX=0,DY=0;
45   while (!feof(f)) {
46      fgets(buf,1024,f);
47      p=strchr(buf,'\n');
48      if (p) *p=0;
49      p=buf;
50      while(isalpha(*p)) p++;
51      *p=0;p++;
52      if (!strcmp(buf,"BYTEORDER")) {
53         while(isspace(*p)) p++;
54         *swapbytes=toupper(*p)=='M';
55      } else if (!strcmp(buf,"NROWS")) {
56         rows=atol(p);
57      } else if (!strcmp(buf,"NCOLS")) {
58         cols=atol(p);
59      } else if (!strcmp(buf,"NBITS")) {
60         if (atol(p)!=16) {
61           fprintf(stderr,"Data size in %s is not 16 bit\n",filename);
62           return NULL;
63         }
64      } else if (!strcmp(buf,"NODATA")) {
65         nodata=atol(p)+shift;
66         if (nodata<0) nodata+=65536;
67      } else if (!strcmp(buf,"ULXMAP")) {
68         X1=strtod(p,NULL);
69      } else if (!strcmp(buf,"ULYMAP")) {
70         Y1=strtod(p,NULL);
71      } else if (!strcmp(buf,"XDIM")) {
72         DX=strtod(p,NULL);
73      } else if (!strcmp(buf,"YDIM")) {
74         DY=strtod(p,NULL);
75      } 
76   }
77   fclose(f);
78   X1-=DX/2;
79   Y1-=DY/2;
80   X2=X1+DX*cols;
81   Y2=Y1+DY*rows;
82   Create16bit=1;
83   return creat_epp(force_ext(filename,".epp"),1,1,cols,
84      rows,X1,Y1,X2,Y2,100,0,nodata);
85 }
86 int main(int argc, char **argv)
87 { int shift=0;
88   char headersuffix[100]=".hdr",headername[1024];
89   int i,c,verbose=0; 
90   while ((c=getopt(argc,argv,"s:h:%"))!=EOF) {
91    switch (c) {
92     case 's': {char *erptr;       
93                shift = strtol(optarg,&erptr,0);
94                if (!*erptr) {
95                   error("Invalid shift value");
96                }
97                break;
98               }
99     case 'h': strcpy(headersuffix, optarg);
100               break;
101     case '%': verbose=1;
102               break;
103     default: error("Invalid option.\n Usage bil2epp [-h header_suffix]"
104                " [-s shift_value] [-%] files\n");   
105    }
106   }
107    
108   install_progress_indicator(verbose?show_percent:check_int);
109   if (optind==argc) error("No files specified");
110
111   for (i=optind;i<argc;i++) {
112     EPP *epp;
113     FILE *f=fopen(argv[i],"r");
114     int swapbytes;
115    
116     if (!f) {
117         fprintf(stderr,"Cannot open %s, skipping\n",argv[i]);
118         continue;
119     }
120     strcpy(headername,force_ext(argv[i],headersuffix));
121     if (!(epp=read_header(headername,&swapbytes,shift))) {
122       fprintf(stderr,"Cannot open %s, skipping %s\n",headername,argv[i]);
123       continue;
124     }        
125     if (verbose) {
126       fprintf(stderr,"\r%s\n",argv[i]);
127     }
128     if (convert (epp,f,swapbytes?swab:dcopy,shift)) {
129        clear_progress(1);
130        exit(1);
131     }
132     close_epp(epp);
133     fclose(f);
134   }
135   clear_progress(0);
136   exit(0);
137