File: recode.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (172 lines) | stat: -rw-r--r-- 5,258 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <stdio.h>
#include "global.h"

#define F2I(map_type) \
	(map_type == CELL_TYPE ? 0 : (map_type == FCELL_TYPE ? 1 : 2))

static void process_row_ii(int), process_row_if(int), process_row_id(int);
static void process_row_fi(int), process_row_ff(int), process_row_fd(int);
static void process_row_di(int), process_row_df(int), process_row_dd(int);

static void (*process_row_FtypeOtype [3][3])() =
 { {process_row_ii, process_row_if, process_row_id},
   {process_row_fi, process_row_ff, process_row_fd},
   {process_row_di, process_row_df, process_row_dd} };

#define PROCESS_ROW \
     (process_row_FtypeOtype [F2I (in_type)] [F2I (out_type)])

static void *in_rast, *out_rast;
static int nrows, ncols;

int do_recode (void)
{
   struct Cell_head window, cellhd;
   int row, i;
   struct History hist ;

/* set the window from the header for the input file */
   if (align_wind)
   {
	G_get_window (&window);
	if (G_get_cellhd (name, mapset, &cellhd) >= 0)
	{
	    G_align_window (&window, &cellhd);
	    G_set_window (&window);
	}
   }
    
   G_get_set_window (&window);

   nrows = G_window_rows();
   ncols = G_window_cols();

   /* open the input file for reading */
   in_fd = G_open_cell_old (name, mapset);
   if (in_fd < 0) G_fatal_error("Can't open input map");

   out_fd = G_open_raster_new (result, out_type);

   out_rast = G_allocate_raster_buf(out_type);
   in_rast = G_allocate_raster_buf(in_type);

   fprintf (stderr, "percent complete: ");
   for (row = 0; row < nrows; row++)
   {
     G_percent (row, nrows, 2);
     PROCESS_ROW(row);
   }
   G_percent (row, nrows, 2);
   G_close_cell (in_fd);
   G_close_cell (out_fd);
   
   /* writing history file */
   G_short_history(result, "raster", &hist);
   sprintf(hist.edhist[0], "recode of raster file %s", name); 
   /* if there are more rules than history lines allocated, write only 
      MAXEDLINES-1 rules , and "...." as a last rule */
       for(i=0; (i< nrules) && (i<MAXEDLINES-2);i++)
      sprintf(hist.edhist[i+1], "%s", rules[i]); 
   if(nrules > MAXEDLINES-1)
   {
      sprintf(hist.edhist[MAXEDLINES-1], "..."); 
      hist.edlinecnt = MAXEDLINES;
   }
   else
      hist.edlinecnt = nrules + 1;
   sprintf(hist.datsrc_1,"raster file %s", name);
   G_write_history (result, &hist);

   return 0;
}

static void process_row_ii(int row)
{
   if(no_mask) 
      G_get_c_raster_row_nomask (in_fd, (CELL *) in_rast, row);
   else
      G_get_c_raster_row(in_fd, (CELL *) in_rast, row);
   G_fpreclass_perform_ii(&rcl_struct, (CELL *) in_rast, (CELL *) out_rast, ncols);
   G_put_raster_row (out_fd, (CELL *) out_rast, CELL_TYPE);
}

static void process_row_if (int row)
{
   if(no_mask) 
      G_get_c_raster_row_nomask (in_fd, (CELL *) in_rast, row);
   else
      G_get_c_raster_row(in_fd, (CELL *) in_rast, row);
   G_fpreclass_perform_if(&rcl_struct, (CELL *) in_rast, (FCELL *) out_rast, ncols);
   G_put_f_raster_row (out_fd, (FCELL *) out_rast);
}

static void process_row_id(int row)
{
   if(no_mask) 
      G_get_c_raster_row_nomask (in_fd, (CELL *) in_rast, row);
   else
      G_get_c_raster_row(in_fd, (CELL *) in_rast, row);
   G_fpreclass_perform_id(&rcl_struct, (CELL *) in_rast, (DCELL *) out_rast, ncols);
   G_put_raster_row (out_fd, (DCELL *) out_rast,DCELL_TYPE);
}

static void process_row_fi(int row)
{
   if(no_mask) 
      G_get_f_raster_row_nomask (in_fd, (FCELL *) in_rast, row);
   else
      G_get_f_raster_row(in_fd, (FCELL *) in_rast, row);
   G_fpreclass_perform_fi(&rcl_struct, (FCELL *) in_rast, (CELL *) out_rast, ncols);
   G_put_raster_row (out_fd, (CELL *) out_rast, CELL_TYPE);
}

static void process_row_ff(int row)
{
   if(no_mask) 
      G_get_f_raster_row_nomask (in_fd, (FCELL *) in_rast, row);
   else
      G_get_f_raster_row(in_fd, (FCELL *) in_rast, row);
   G_fpreclass_perform_ff(&rcl_struct, (FCELL *) in_rast, (FCELL *) out_rast, ncols);
   G_put_f_raster_row (out_fd, (FCELL *) out_rast);
}

static void process_row_fd(int row)
{
   if(no_mask) 
      G_get_f_raster_row_nomask (in_fd, (FCELL *) in_rast, row);
   else
      G_get_f_raster_row(in_fd, (FCELL *) in_rast, row);
   G_fpreclass_perform_fd(&rcl_struct, (FCELL *) in_rast, (DCELL *) out_rast, ncols);
   G_put_raster_row (out_fd, (DCELL *) out_rast,DCELL_TYPE);
}

static void process_row_di(int row)
{
   if(no_mask) 
      G_get_d_raster_row_nomask (in_fd, (DCELL *) in_rast, row);
   else
      G_get_d_raster_row(in_fd, (DCELL *) in_rast, row);
   G_fpreclass_perform_di(&rcl_struct, (DCELL *) in_rast, (CELL *) out_rast, ncols);
   G_put_raster_row (out_fd, (CELL *) out_rast, CELL_TYPE);
}

static void process_row_df(int row)
{
   if(no_mask) 
      G_get_d_raster_row_nomask (in_fd, (DCELL *) in_rast, row);
   else
      G_get_d_raster_row(in_fd, (DCELL *) in_rast, row);
   G_fpreclass_perform_df(&rcl_struct, (DCELL *) in_rast, (FCELL *) out_rast, ncols);
   G_put_f_raster_row (out_fd, (FCELL *) out_rast);
}

static void process_row_dd(int row)
{
   if(no_mask) 
      G_get_d_raster_row_nomask (in_fd, (DCELL *) in_rast, row);
   else
      G_get_d_raster_row(in_fd, (DCELL *) in_rast, row);
   G_fpreclass_perform_dd(&rcl_struct, (DCELL *) in_rast, (DCELL *) out_rast, ncols);
   G_put_raster_row (out_fd, (DCELL *) out_rast,DCELL_TYPE);
}