]> www.wagner.pp.ru Git - oss/fgis.git/blob - epu/mosaic.c
First checked in version
[oss/fgis.git] / epu / mosaic.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "epp.h"
5 #include "eppl_ut.h"
6 #include <getopt.h>
7 #include <unistd.h>
8 EPP **files;
9 int count;
10
11 int fill_cell(int col,int row,int value)
12 { int i,c;
13   EPP **epp;
14   for (i=count,epp=files+count-1;i>0;i--,epp--)
15     if ((c=epp_get(*epp,col,row))!=(*epp)->offsite) return c;
16   return value;
17 }
18
19 void help(int exitcode)
20 {
21  printf("Usage: mosaic [-%%RA] [-O number] [-o filename] [--help][--version]files\n"
22 "\t--help - displays this message\n"
23 "\t--version - shows version number\n"
24 "\t-%% --verbose - shows progress indicator\n"
25 "\t-R --force-row - process misaligned files using row/col coords\n"
26 "\t-A --force-alt - process misaligned files using alternative coords\n"
27 "\t-O number --offsite=number - set offsite value of output file\n"
28 "\t-o name --output-file=name - sets name of output file\n");
29 exit (exitcode);
30 }
31
32 int main(int argc,char **argv)
33 {char output_file[1024]="mosaic.out.epp";
34  struct option long_options[]=
35
36  {"help",0,0,1},
37  {"version",0,0,2},
38  {"offsite",1,0,'O'},
39  {"verbose",0,0,'%'},
40  {"force-row",0,0,'R'},
41  {"force-alt",0,0,'A'},
42  {"output-file",1,0,'o'},
43  {NULL,0,0,0}};
44  int index,i,c;char *endptr;
45  int offsite=-1, x1=32767,y1=32767,x2=-32767,y2=-32767,
46      max=0,min=65535,force=0,use_alt=1,result,verbose=0;
47  double X1,Y1,X2,Y2;
48  EPP *out_f;
49  while ((c=getopt_long(argc,argv,"O:%RAo:",long_options,&index))!=-1)
50  { switch (c)
51    { case 2:show_version("mosaic","$Revision: 1.1 $");
52      case '%':verbose=1;break;
53      case 'O':offsite=strtol(optarg,&endptr,0);
54          if(*endptr||offsite<0||offsite>65535)
55          { fprintf(stderr,"Invalid offsite value %s\n",optarg); return 2;
56          } 
57          break;
58      case 'A':force=1;use_alt=1;break;
59      case 'R':force=1;use_alt=0;break;
60      case 'o':strcpy(output_file,default_ext(optarg,".epp"));break;
61      case  1:
62      case '?':
63      default:
64        help(c!=1);
65    }
66  } 
67   count=argc - optind;
68   if (!count) { fprintf(stderr,"No input files specified\n");return 2;}
69   files=malloc(count*sizeof(EPP *));
70   for (i=optind,index=0;i<argc;i++,index++)
71   { files[index]=open_epp(default_ext(argv[i],".epp"));
72     if (!files[index]) 
73     {fprintf(stderr,"Cannot open file %s\n",argv[i]); 
74      return 2;
75     } 
76     if (index>0&&!compare_cell_size(files[0],files[index]))
77      { fprintf(stderr,"File %s is incompatible with %s\n",argv[i],argv[optind]);
78       return 2;
79      }
80     if (index>0&&!is_aligned(files[0],files[index]))
81     { fprintf(stderr,"File %s is misaligned with %s\n",argv[i],argv[optind]);
82       if (!force) return 2;
83       if (use_alt)
84       {if (!shift_epp(files[index],epp_row(files[0],files[index]->YTop),
85                              epp_col(files[0],files[index]->XLeft)))
86        {fprintf(stderr,"Cannot align file %s\n",argv[i]) ;
87        return 2;
88        }
89       }
90       else
91       { files[index]->XLeft=alt_x(files[0],files[index]->fc);
92         files[index]->XRight=alt_x(files[0],files[index]->lc);
93         files[index]->YTop=alt_y(files[0],files[index]->fr);
94         files[index]->YBottom=alt_y(files[0],files[index]->lr);
95       }
96      }
97      if (!index||x1>files[index]->fc){ x1=files[index]->fc;X1=files[index]->XLeft;}
98      if (!index||x2<files[index]->lc){ x2=files[index]->lc;X2=files[index]->XRight;}
99      if (!index||y1>files[index]->fr){ y1=files[index]->fr;Y1=files[index]->YTop;}
100      if (!index||y2<files[index]->lr){ y2=files[index]->lr;Y2=files[index]->YBottom;}
101      if (max<files[index]->max) max=files[index]->max;
102      if (min>files[index]->min) min=files[index]->min;
103      if (files[index]->kind==16) Create16bit=1;
104
105   }
106   x2--;
107   y2--;
108   if (offsite<0)
109   { if (files[0]->offsite<min||files[0]->offsite>max)
110      offsite=files[0]->offsite;
111      else 
112      offsite=Create16bit?65535:255;
113   }
114   install_progress_indicator(verbose?show_percent:check_int);
115   out_f=creat_epp(output_file,x1,y1,x2,y2,X1,Y1,X2,Y2,
116                   100,0,offsite);
117   if (!out_f) {fprintf(stderr,"Cannot create output file %s\n",output_file);
118               return 2;
119              }
120   result=clear_progress(for_each_cell(out_f,fill_cell));
121   close_epp(out_f);
122   if (result) unlink(output_file);
123   for (i=0;i<count;i++) close_epp(files[i]);
124   return result;
125 }