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