11 int row1=32767, row2=-32768, col1=32767, col2=-32768;
12 void help(int exitcode)
14 printf("Usage: clip [-%%] [-m number] [-o filename] [--help][--version] file mask_file\n"
15 "\t--help - displays this message\n"
16 "\t--version - shows version number\n"
17 "\t-%% --verbose - shows progress indicator\n"
18 "\t-m --mask-value - class in mask file, which denotes area to copy.\n"
19 "\t\t (default - all onsite classes)\n"
20 "\t-o name --output-file=name - sets name of output file\n");
23 int mask_offsite(int class) {
24 return class!=mask->offsite;
27 int (*mask_func)(int)=mask_offsite;
28 int mask_class(int class) {
29 return class==mask_value;
31 int fill_cell(int col, int row, int value)
33 if (mask_func(epp_get(mask,col,row)))
34 return epp_get(src,col,row);
39 int fill_cell_transform(int col, int row, int value)
41 if (mask_func(epp_get(mask,col,row)))
42 return epp_get(src,linked_col(lnk,col),linked_row(lnk,row));
47 int find_extent(int col, int row, int value)
49 if (mask_func(value)) {
50 if (col<col1) col1=col;
51 if (col>col2) col2=col;
52 if (row<row1) row1=row;
53 if (row>row2) row2=row;
57 int main(int argc,char **argv)
58 {char output_file[1024]="clip.out.epp";
59 struct option long_options[]=
63 {"mask-value",1,0,'m'},
65 {"output-file",1,0,'o'},
67 int index,c;char *endptr;
68 int x1=32767,y1=32767,x2=-32767,y2=-32767,
70 while ((c=getopt_long(argc,argv,"m:%o:",long_options,&index))!=-1)
72 { case 2:show_version("mosaic","$Revision: 1.1 $");
73 case '%':verbose=1;break;
74 case 'm':mask_value=strtol(optarg,&endptr,0);
76 { fprintf(stderr,"Invalid mask value %s\n",optarg); return 2;
80 case 'o':strcpy(output_file,default_ext(optarg,".epp"));break;
87 if (argc-optind!=2) { help(1);}
88 src=open_epp(argv[optind]);
89 if (!src) src=open_epp(default_ext(argv[optind],".epp"));
91 fprintf(stderr,"Cannot open file %s\n",argv[optind]);
94 mask=open_epp(argv[optind+1]);
95 if (!mask) mask=open_epp(default_ext(argv[optind+1],".epp"));
97 fprintf(stderr,"Cannot open file %s\n",argv[optind+1]);
100 if (mask_value>0&&(mask_value<mask->min||mask_value>mask->max)&&
101 mask_value!=mask->offsite) {
102 fprintf(stderr,"No cells with value %d in file %s\n",mask_value,
106 /* determine limits of new file */
107 if (verbose) fprintf(stderr,"Determining output extent\n");
108 install_progress_indicator(verbose?show_percent:check_int);
109 if (for_each_cell(mask,find_extent)!=0) exit(1);
111 x1=epp_col(mask,src->XLeft);
112 x2=epp_col(mask,src->XRight)+1;
113 y1=epp_row(mask,src->YTop);
114 y2=epp_row(mask,src->YBottom)+1;
125 row1=y1<row1?row1:y1;
126 row2=y2>row2?row2:y2;
127 col1=x1<col1?col1:x1;
128 col2=x2>col2?col2:x2;
129 if (row1>=row2||col1>=col2) {
130 fprintf(stderr,"No applicable cells found\n");
133 dest=creat_epp(output_file,col1,row1,col2,row2,
134 alt_x(mask,col1),alt_y(mask,row1), alt_x(mask,col2+1),
135 alt_y(mask,row2+1), 100,0, src->offsite);
138 if (!dest) {fprintf(stderr,"Cannot create output file %s\n",output_file);
141 if (verbose) fprintf(stderr,"Writing file\n");
142 if (is_aligned(mask,src))
143 result=clear_progress(for_each_cell(dest,fill_cell));
145 lnk=link_epp(mask,src);
146 result=clear_progress(for_each_cell(dest,fill_cell_transform));
152 if (result) unlink(output_file);