GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
raster/raster.c
Go to the documentation of this file.
1 /*!
2  * \file lib/raster/raster.c
3  *
4  * \brief Raster Library - Raster cell value routines.
5  *
6  * (C) 2001-2009 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public License
9  * (>=v2). Read the file COPYING that comes with GRASS for details.
10  *
11  * \author Original author CERL
12  */
13 
14 #include <stdlib.h>
15 #include <string.h>
16 #include <grass/gis.h>
17 #include <grass/raster.h>
18 
19 /*!
20  * \brief Compares raster values.
21  *
22  * \param v1,v2 values to be compared
23  * \param data_type raster type (CELL, FCELL, DCELL)
24  *
25  * \return 1 if p > q or only q is null value
26  * \return -1 if p < q or only p is null value
27  * \return 0 if p == q or p==q==null value
28  */
29 int Rast_raster_cmp(const void *v1, const void *v2, RASTER_MAP_TYPE data_type)
30 {
31  if (Rast_is_null_value(v1, data_type)) {
32  if (Rast_is_null_value(v2, data_type))
33  return 0;
34  else
35  return -1;
36  }
37  else if (Rast_is_null_value(v2, data_type))
38  return 1;
39 
40  switch (data_type) {
41  case CELL_TYPE:
42  if (*((const CELL *)v1) > *((const CELL *)v2))
43  return 1;
44  else if (*((const CELL *)v1) == *((const CELL *)v2))
45  return 0;
46  else
47  return -1;
48  case FCELL_TYPE:
49  if (*((const FCELL *)v1) > *((const FCELL *)v2))
50  return 1;
51  else if (*((const FCELL *)v1) == *((const FCELL *)v2))
52  return 0;
53  else
54  return -1;
55  case DCELL_TYPE:
56  if (*((const DCELL *)v1) > *((const DCELL *)v2))
57  return 1;
58  else if (*((const DCELL *)v1) == *((const DCELL *)v2))
59  return 0;
60  else
61  return -1;
62  }
63 
64  return 0;
65 }
66 
67 /*!
68  * \brief Copies raster values.
69  *
70  * If \p v2 is null value, sets \p v2 to null value.
71  * \p n is typically size of the destination array
72  * and the source array is at least that large.
73  *
74  * \param v1 destination array for raster values
75  * \param v2 source array with raster values
76  * \param n number of values to copy
77  * \param data_type raster type (CELL, FCELL, DCELL)
78  */
79 void Rast_raster_cpy(void *v1, const void *v2, int n, RASTER_MAP_TYPE data_type)
80 {
81  memcpy(v1, v2, n * Rast_cell_size(data_type));
82 }
83 
84 /*!
85  * \brief Places a CELL raster value
86  *
87  * If Rast_is_c_null_value() is true, sets p to null value. Converts CELL
88  * val to data_type (type of p) and stores result in p. Used for
89  * assigning CELL values to raster cells of any type.
90  *
91  * \param rast pointer to raster cell value
92  * \param cval value to set
93  * \param data_type raster type (CELL, FCELL, DCELL)
94  */
95 void Rast_set_c_value(void *rast, CELL cval, RASTER_MAP_TYPE data_type)
96 {
97  CELL c;
98 
99  c = cval;
100  if (Rast_is_c_null_value(&c)) {
101  Rast_set_null_value(rast, 1, data_type);
102  return;
103  }
104  switch (data_type) {
105  case CELL_TYPE:
106  *((CELL *)rast) = cval;
107  break;
108  case FCELL_TYPE:
109  *((FCELL *)rast) = (FCELL)cval;
110  break;
111  case DCELL_TYPE:
112  *((DCELL *)rast) = (DCELL)cval;
113  break;
114  }
115 }
116 
117 /*!
118  * \brief Places a FCELL raster value
119  *
120  * If Rast_is_f_null_value() is true, sets p to null value. Converts
121  * FCELL val to data_type (type of p) and stores result in p. Used for
122  * assigning FCELL values to raster cells of any type.
123  *
124  * \param rast pointer to raster cell value
125  * \param fval value to set
126  * \param data_type raster type (CELL, FCELL, DCELL)
127  */
128 void Rast_set_f_value(void *rast, FCELL fval, RASTER_MAP_TYPE data_type)
129 {
130  FCELL f;
131 
132  f = fval;
133  if (Rast_is_f_null_value(&f)) {
134  Rast_set_null_value(rast, 1, data_type);
135  return;
136  }
137  switch (data_type) {
138  case CELL_TYPE:
139  *((CELL *)rast) = (CELL)fval;
140  break;
141  case FCELL_TYPE:
142  *((FCELL *)rast) = fval;
143  break;
144  case DCELL_TYPE:
145  *((DCELL *)rast) = (DCELL)fval;
146  break;
147  }
148 }
149 
150 /*!
151  * \brief Places a DCELL raster value
152  *
153  * If Rast_is_d_null_value() is true, sets p to null value. Converts
154  * DCELL val to data_type (type of p) and stores result in p. Used for
155  * assigning DCELL values to raster cells of any type.
156  *
157  * \param rast pointer to raster cell value
158  * \param fval value to set
159  * \param data_type raster type (CELL, FCELL, DCELL)
160  */
161 void Rast_set_d_value(void *rast, DCELL dval, RASTER_MAP_TYPE data_type)
162 {
163  DCELL d;
164 
165  d = dval;
166  if (Rast_is_d_null_value(&d)) {
167  Rast_set_null_value(rast, 1, data_type);
168  return;
169  }
170  switch (data_type) {
171  case CELL_TYPE:
172  *((CELL *)rast) = (CELL)dval;
173  break;
174  case FCELL_TYPE:
175  *((FCELL *)rast) = (FCELL)dval;
176  break;
177  case DCELL_TYPE:
178  *((DCELL *)rast) = dval;
179  break;
180  }
181 }
182 
183 /*!
184  * \brief Retrieves the value of give type from pointer p
185  *
186  * Retrieves the value of type data_type from pointer p, converts it
187  * to CELL type and returns the result. If null value is stored in p,
188  * returns CELL null value.
189  *
190  * Used for retrieving CELL values from raster cells of any type.
191  *
192  * Note: when data_type != CELL_TYPE, no quantization is used, only
193  * type conversion.
194  *
195  * \param rast pointer to raster cell value
196  * \param data_type raster type (CELL, FCELL, DCELL)
197  *
198  * \return raster value
199  */
200 CELL Rast_get_c_value(const void *rast, RASTER_MAP_TYPE data_type)
201 {
202  CELL c;
203 
204  if (Rast_is_null_value(rast, data_type)) {
205  Rast_set_c_null_value(&c, 1);
206  return c;
207  }
208  switch (data_type) {
209  case CELL_TYPE:
210  return *((const CELL *)rast);
211  case FCELL_TYPE:
212  return (CELL) * ((const FCELL *)rast);
213  case DCELL_TYPE:
214  return (CELL) * ((const DCELL *)rast);
215  }
216 
217  return 0;
218 }
219 
220 /*!
221  * \brief Retrieves the value of given raster type from pointer p (FCELL)
222  *
223  * Retrieves the value of type data_type from pointer p, converts it
224  * to FCELL type and returns the result. If null value is stored in p,
225  * returns FCELL null value.
226  *
227  * Used for retrieving FCELL values from raster cells of any type.
228  *
229  * \param rast pointer to raster cell value
230  * \param data_type raster type (CELL, FCELL, DCELL)
231  *
232  * \return raster value
233  */
234 FCELL Rast_get_f_value(const void *rast, RASTER_MAP_TYPE data_type)
235 {
236  FCELL f;
237 
238  if (Rast_is_null_value(rast, data_type)) {
239  Rast_set_f_null_value(&f, 1);
240  return f;
241  }
242  switch (data_type) {
243  case CELL_TYPE:
244  return (FCELL) * ((const CELL *)rast);
245  case FCELL_TYPE:
246  return *((const FCELL *)rast);
247  case DCELL_TYPE:
248  return (FCELL) * ((const DCELL *)rast);
249  }
250 
251  return 0;
252 }
253 
254 /*!
255  * \brief Retrieves the value of given type from pointer p (DCELL)
256  *
257  * Retrieves the value of type data_type from pointer p, converts it
258  * to DCELL type and returns the result. If null value is stored in p,
259  * returns DCELL null value.
260 
261  * Used for retrieving DCELL values from raster cells of any type.
262  *
263  * \param rast pointer to raster cell value
264  * \param data_type raster type (CELL, FCELL, DCELL)
265  *
266  * \return raster value
267  */
268 DCELL Rast_get_d_value(const void *rast, RASTER_MAP_TYPE data_type)
269 {
270  DCELL d;
271 
272  if (Rast_is_null_value(rast, data_type)) {
273  Rast_set_d_null_value(&d, 1);
274  return d;
275  }
276  switch (data_type) {
277  case CELL_TYPE:
278  return (DCELL) * ((const CELL *)rast);
279  case FCELL_TYPE:
280  return (DCELL) * ((const FCELL *)rast);
281  case DCELL_TYPE:
282  return *((const DCELL *)rast);
283  }
284 
285  return 0;
286 }
int Rast_is_null_value(const void *, RASTER_MAP_TYPE)
To check if a raster value is set to NULL.
Definition: null_val.c:176
#define Rast_is_f_null_value(fcellVal)
Definition: defs/raster.h:406
void Rast_set_d_null_value(DCELL *, int)
To set a number of DCELL raster values to NULL.
Definition: null_val.c:153
void Rast_set_f_null_value(FCELL *, int)
To set a number of FCELL raster values to NULL.
Definition: null_val.c:138
void Rast_set_c_null_value(CELL *, int)
To set a number of CELL raster values to NULL.
Definition: null_val.c:124
size_t Rast_cell_size(RASTER_MAP_TYPE)
Returns size of a raster cell in bytes.
Definition: alloc_cell.c:38
void Rast_set_null_value(void *, int, RASTER_MAP_TYPE)
To set one or more raster values to null.
Definition: null_val.c:98
#define Rast_is_d_null_value(dcellVal)
Definition: defs/raster.h:408
#define Rast_is_c_null_value(cellVal)
Definition: defs/raster.h:404
float FCELL
Definition: gis.h:630
double DCELL
Definition: gis.h:629
int CELL
Definition: gis.h:628
int Rast_raster_cmp(const void *v1, const void *v2, RASTER_MAP_TYPE data_type)
Compares raster values.
Definition: raster/raster.c:29
void Rast_raster_cpy(void *v1, const void *v2, int n, RASTER_MAP_TYPE data_type)
Copies raster values.
Definition: raster/raster.c:79
void Rast_set_c_value(void *rast, CELL cval, RASTER_MAP_TYPE data_type)
Places a CELL raster value.
Definition: raster/raster.c:95
DCELL Rast_get_d_value(const void *rast, RASTER_MAP_TYPE data_type)
Retrieves the value of given type from pointer p (DCELL)
void Rast_set_f_value(void *rast, FCELL fval, RASTER_MAP_TYPE data_type)
Places a FCELL raster value.
CELL Rast_get_c_value(const void *rast, RASTER_MAP_TYPE data_type)
Retrieves the value of give type from pointer p.
FCELL Rast_get_f_value(const void *rast, RASTER_MAP_TYPE data_type)
Retrieves the value of given raster type from pointer p (FCELL)
void Rast_set_d_value(void *rast, DCELL dval, RASTER_MAP_TYPE data_type)
Places a DCELL raster value.
#define FCELL_TYPE
Definition: raster.h:12
#define DCELL_TYPE
Definition: raster.h:13
#define CELL_TYPE
Definition: raster.h:11
int RASTER_MAP_TYPE
Definition: raster.h:25