]> www.wagner.pp.ru Git - oss/fgis.git/blob - epu/fill.c
First checked in version
[oss/fgis.git] / epu / fill.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "epp.h"
5 #include "eppl_ut.h"
6 int color_to_fill=0;
7 int color_to_not_expand=-1;/* 1 in eppl */
8 int cells_filled,cells_remain;
9 int verbose=0;
10 int gap_size=0;
11 void add_color(int color,int *col_list,int *col_count,int *list_len)
12 { int i;
13   if(color==color_to_fill||color==color_to_not_expand) return;
14   for (i=0;i<*list_len;i++,col_list++,col_count++)
15    if (color==col_list[i]) {col_count[i]++;return;}
16  *col_list=color;*col_count=1;(*list_len)++;
17 }
18 #define addcolor(i) add_color(i,color,count,&cc);
19 void fillrow(unsigned short *out_row,unsigned short *prev_row, 
20              unsigned short *cur_row,unsigned short *next_row, 
21              int ncols)
22 { int i; 
23   for (i=1;i<=ncols;i++)
24    if (cur_row[i]!=color_to_fill) *(out_row++)=cur_row[i];
25     else
26     { int color[8],count[8],ii,jj,cc=0,cnt=0;
27       unsigned short *clr_ptr=prev_row+i-1;   
28       addcolor(*(clr_ptr++));
29       addcolor(*(clr_ptr++));
30       addcolor(*(clr_ptr++));
31       addcolor(cur_row[i-1]);
32       addcolor(cur_row[i+1]);
33       addcolor(*(clr_ptr=next_row+i-1));
34       addcolor(*(++clr_ptr));
35       addcolor(*(++clr_ptr));
36       for(ii=0,jj=color_to_fill;ii<cc;ii++)
37       if(count[ii]>cnt) {cnt=count[ii];jj=color[ii];}
38        if (jj==color_to_fill) 
39            {cells_remain++;*(out_row++)=color_to_fill;}
40            else 
41            {cells_filled++;*(out_row++)=jj;};
42     }
43
44 int fill(char *in_file_name,char *out_file_name)
45 { unsigned short int *prev_row,*cur_row,*next_row,*rptr,*dest;
46   int i,j,nrows,seqrow,ncols;
47   EPP *in_file;
48   EPP *out_file;
49   
50   cells_filled=0;
51   cells_remain=0;
52   
53   in_file=open_epp(in_file_name);
54   if (NULL==in_file)
55   { fprintf(stderr,"Cannot open file %s\n",in_file_name);
56     exit(2);
57   }
58   Create16bit=in_file->kind==16;
59   out_file=creat_epp_as(out_file_name,in_file);
60   if (NULL==out_file)
61   { fprintf(stderr,"Cannt create file %s\n",out_file_name);
62     exit(2);
63   }
64   ncols=in_file->lc-in_file->fc;
65   prev_row=malloc(sizeof(short)*(ncols+2));
66   cur_row=malloc(sizeof(short)*(ncols+2));
67   next_row=malloc(sizeof(short)*(ncols+2));
68   for(i=0;i<=ncols+1;prev_row[i++]=color_to_fill);
69   cur_row[0]=cur_row[ncols+1]=next_row[0]=next_row[ncols+1]=color_to_fill;
70   rptr=epp_getline(in_file,in_file->fc,in_file->fr); 
71   for(i=1,dest=cur_row+1;i<=ncols;*(dest++)=*(rptr++),i++);
72   rptr=epp_getline(in_file,in_file->fc,in_file->fr+1);
73   for(i=1,dest=next_row+1;i<=ncols;*(dest++)=*(rptr++),i++);
74   nrows=in_file->lr-in_file->fr;
75   for(seqrow=1,i=in_file->fr+2;seqrow<=nrows;i++,seqrow++)
76   { 
77    if (EndLineProc)
78     if ((*EndLineProc)(i,seqrow,nrows)) return 1;
79    epp_put(out_file,out_file->fc,i-2,out_file->offsite); 
80    fillrow(out_file->row,prev_row,cur_row,next_row,ncols);
81    {unsigned short int *tmp;
82     tmp=prev_row;
83     prev_row=cur_row;
84     cur_row=next_row;
85     next_row=tmp;
86    }
87    if (i<in_file->lr) 
88    { rptr=epp_getline(in_file,in_file->fc,i);
89      for(j=1,dest=next_row+1;j<=ncols;*(dest++)=*(rptr++),j++);
90    }
91   else
92   for(j=1,dest=next_row+1;j<=ncols;*(dest++)=color_to_fill,j++);
93 }  
94 out_file->max=in_file->max;
95 out_file->min=in_file->min; 
96 close_epp(out_file);
97 close_epp(in_file);
98 return 0;
99 }
100 int main(int argc,char **argv)
101
102 #ifdef unix
103   char out_name[256]="fill.out.epp";
104 #else
105   char out_name[256]="fill.epp";
106 #endif
107   struct option long_options[]={
108   {"help",0,0,1},
109   {"version",0,0,2},
110   {"verbose",0,0,'%'}, 
111   {"gap-size",required_argument,0,'g'},
112   {"color-to-fill",required_argument,0,'f'},
113   {"exclude-color",required_argument,0,'x'},
114   {"output-file",required_argument,0,'o'}, 
115   {NULL,0,0,0} };
116   int c;     
117   int index;
118   while ((c=getopt_long(argc,argv,"%g:c:f:x:o:",long_options,&index))!=-1)
119   { switch(c)
120     { 
121       case 2:/*version*/
122              show_version("fill","$Revision: 1.1 $");
123
124       case '%':verbose=1;break;
125       case 'g':/*gap size is unused now*/
126                gap_size=atoi(optarg);break;
127       case 'c':/*color to fill*/
128                color_to_fill=atoi(optarg);break;
129       case 'x':color_to_not_expand=atoi(optarg);break;
130       case 'o':strcpy(out_name,optarg);break;
131       case 1:
132       case '?':
133       default:
134       /*help*/
135              printf("Usage %s [--help] [--version] [-v] [-g gap size]\n"
136              "\t[-c color to fill] [-x color to exclude] [-o output file]\n"
137              "\tepp file\n",argv[0]);
138              return c==1?0:2;
139      }
140    }
141    if (argc==optind) 
142     {fprintf(stderr,"No input files specified\n");
143      return 2;
144     }
145    if (argc>optind+1)
146     { fprintf(stderr,"Too many input files\n");
147       return 2;
148     }
149    install_progress_indicator(verbose?show_percent:check_int);
150    if((c=clear_progress(fill(argv[optind],out_name))))
151     {unlink(out_name);
152      return 2;
153     }
154    printf("Cells filled %d\nCells of class %d remain:%d\n",
155           cells_filled,color_to_fill,cells_remain);
156    return cells_remain?1:0;
157 }