File: raster.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 (315 lines) | stat: -rw-r--r-- 6,769 bytes parent folder | download | duplicates (2)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "gis.h"


/*!
 * \brief 
 *
 * Advances void
 * pointer by n bytes. returns new pointer value. Usefull in raster row
 * processing loops, substitutes 
 \code
  CELL *cell; 
  cell += n;
 \endcode
 * Now 
 \code
  rast = G_incr_void_ptr(rast, G_raster_size(data_type))
 \endcode
 * (where rast is void* and data_type is RASTER_MAP_TYPE can be used instead
 * of rast++.)  very usefull to generalize the row processing - loop (i.e. void *
 * buf_ptr += G_raster_size(data_type)
 *
 *  \param ptr
 *  \param size
 *  \return void * 
 */

void *G_incr_void_ptr(
/* advances ptr by size bytes returns new position */
   void *ptr,
   int size)
{
   /* assuming that the size of unsigned char is 1 */
   return (void *) ((unsigned char *) ptr + size);
}


/*!
 * \brief 
 *
 * Compares raster vlues p and q. Returns 1 if p > q or only q is
 * null value -1 if p < q or only p is null value 0 if p == q or
 * p==q==null value
 *
 *  \param p
 *  \param q
 *  \param data_type
 *  \return int
 */

int G_raster_cmp( void *v1,void *v2, RASTER_MAP_TYPE data_type)
{
    if(G_is_null_value(v1, data_type ) )
    {
       if (G_is_null_value(v2, data_type ))
          return 0;
       else return -1;
    }
    else if(G_is_null_value(v2, data_type ) )
       return 1;

    switch (data_type)
    {
       case CELL_TYPE:  if(*((CELL *) v1) > *((CELL *) v2))
			     return 1;
                        else if(*((CELL *) v1) == *((CELL *) v2))
			     return 0;
                        else return -1;
       case FCELL_TYPE: if(*((FCELL *) v1) > *((FCELL *) v2))
			     return 1;
                        else if(*((FCELL *) v1) == *((FCELL *) v2))
			     return 0;
                        else return -1;
       case DCELL_TYPE: if(*((DCELL *) v1) > *((DCELL *) v2))
			     return 1;
                        else if(*((DCELL *) v1) == *((DCELL *) v2))
			     return 0;
                        else return -1;
     }

     return 0;
}


/*!
 * \brief 
 *
 * Copies raster values q into p. If q is null value, sets q to
 * null value.
 *
 *  \param p
 *  \param q
 *  \param n
 *  \param data_type
 *  \return int
 */

int G_raster_cpy(
    void *v1,void *v2,
    int n,
    RASTER_MAP_TYPE data_type)
{
    G_copy((char *) v1, (char *) v2, n * G_raster_size(data_type));
    return 0;
}


/*!
 * \brief 
 *
 * If G_is_c_null_value(val) is true, sets p to null value.
 * Converts CELL val to data_type (type of p) and stores result in p. Used for
 * assigning CELL values to raster cells of any type.
 *
 *  \param p
 *  \param val
 *  \param data_type
 *  \return int
 */

int G_set_raster_value_c(
    void *rast,
    CELL cval,
    RASTER_MAP_TYPE data_type)
{
    CELL c;
    c = cval;
    if(G_is_c_null_value(&c))
    {
       G_set_null_value(rast, 1, data_type);
       return 0;
    }
    switch (data_type)
    {
       case CELL_TYPE: *((CELL *)rast) = cval; break;
       case FCELL_TYPE: *((FCELL *)rast) = (FCELL ) cval; break;
       case DCELL_TYPE: *((DCELL *)rast) = (DCELL ) cval; break;
    }

    return 0;
}


/*!
 * \brief 
 *
 * If G_is_f_null_value(val) is true, sets p to null value.
 * Converts FCELL val to data_type (type of p) and stores result in p. Used for
 * assigning FCELL values to raster cells of any type.
 *
 *  \param p
 *  \param val
 *  \param data_type
 *  \return int
 */

int G_set_raster_value_f(
    void *rast,
    FCELL fval,
    RASTER_MAP_TYPE data_type)
{
    FCELL f;
    f = fval;
    if(G_is_f_null_value(&f))
    {
       G_set_null_value(rast, 1, data_type);
       return 0;
    }
    switch (data_type)
    {
       case CELL_TYPE: *((CELL *)rast) = (CELL ) fval; break;
       case FCELL_TYPE: *((FCELL *)rast) = fval; break;
       case DCELL_TYPE: *((DCELL *)rast) = (DCELL ) fval; break;
    }

    return 0;
}


/*!
 * \brief 
 *
 * If G_is_d_null_value(val) is true, sets p to null value.
 * Converts DCELL val to data_type (type of p) and stores result in p. Used for
 * assigning DCELL values to raster cells of any type.
 *
 *  \param p
 *  \param val
 *  \param data_type
 *  \return int
 */

int G_set_raster_value_d(
    void *rast,
    DCELL dval,
    RASTER_MAP_TYPE data_type)
{
    DCELL d;
    d = dval;
    if(G_is_d_null_value(&d))
    {
       G_set_null_value(rast, 1, data_type);
       return -1;
    }
    switch (data_type)
    {
       case CELL_TYPE: *((CELL *)rast) = (CELL ) dval; break;
       case FCELL_TYPE: *((FCELL *)rast) = (FCELL ) dval; break;
       case DCELL_TYPE: *((DCELL *)rast) = dval; break;
    }

    return 0;
}


/*!
 * \brief 
 *
 * Retrieves the value of type data_type from pointer p,
 * converts it to CELL type and returns the result. If null value is stored in
 * p, returns CELL null value. Used for retreiving CELL values from raster cells
 * of any type.  NOTE: when data_type != CELL_TYPE, no quantization is used,
 * only type conversion.
 *
 *  \param p
 *  \param data_type
 *  \return CELL
 */

CELL G_get_raster_value_c(
    void *rast,
    RASTER_MAP_TYPE data_type)
{
    CELL c;
    if(G_is_null_value(rast, data_type))
    {
       G_set_c_null_value(&c, 1);
       return c;
    }
    switch (data_type)
    {
       case CELL_TYPE: return *((CELL *)rast);
       case FCELL_TYPE: return (CELL) *((FCELL *)rast);
       case DCELL_TYPE: return (CELL) *((DCELL *)rast);
    }

    return 0;
}


/*!
 * \brief 
 *
 * Retrieves the value of type data_type from pointer p,
 * converts it to FCELL type and returns the result. If null value is stored in
 * p, returns FCELL null value. Used for retreiving FCELL values from raster
 * cells of any type.
 *
 *  \param p
 *  \param data_type
 *  \return FCELL
 */

FCELL G_get_raster_value_f(
    void *rast,
    RASTER_MAP_TYPE data_type)
{
    FCELL f;
    if(G_is_null_value(rast, data_type))
    {
       G_set_f_null_value(&f, 1);
       return f;
    }
    switch (data_type)
    {
       case CELL_TYPE: return (FCELL) *((CELL *)rast);
       case FCELL_TYPE: return *((FCELL *)rast);
       case DCELL_TYPE: return (FCELL) *((DCELL *)rast);
    }

    return 0;
}


/*!
 * \brief 
 *
 * Retrieves the value of type data_type from pointer p,
 * converts it to DCELL type and returns the result. If null value is stored in
 * p, returns DCELL null value. Used for retreiving DCELL values from raster
 * cells of any type.
 *
 *  \param p
 *  \param data_type
 *  \return DCELL
 */

DCELL G_get_raster_value_d(
    void *rast,
    RASTER_MAP_TYPE data_type)
{
    DCELL d;
    if(G_is_null_value(rast, data_type))
    {
       G_set_d_null_value(&d, 1);
       return d;
    }
    switch (data_type)
    {
       case CELL_TYPE: return (DCELL) *((CELL *)rast);
       case FCELL_TYPE: return (DCELL) *((FCELL *)rast);
       case DCELL_TYPE: return *((DCELL *)rast);
    }

    return 0;
}