GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
alloc_cell.c
Go to the documentation of this file.
1 /*!
2  * \file lib/raster/alloc_cell.c
3  *
4  * \brief Raster Library - Raster allocation 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 <math.h>
15 
16 #include <grass/gis.h>
17 #include <grass/raster.h>
18 #include <grass/glocale.h>
19 
20 /* convert type "RASTER_MAP_TYPE" into index */
21 #define F2I(map_type) \
22  (map_type == CELL_TYPE ? 0 : (map_type == FCELL_TYPE ? 1 : 2))
23 
24 static const int type_size[3] = {sizeof(CELL), sizeof(FCELL), sizeof(DCELL)};
25 
26 /*!
27  * \brief Returns size of a raster cell in bytes.
28  *
29  * - If <i>data_type</i> is CELL_TYPE, returns sizeof(CELL)
30  * - If <i>data_type</i> is FCELL_TYPE, returns sizeof(FCELL)
31  * - If <i>data_type</i> is DCELL_TYPE, returns sizeof(DCELL)
32  *
33  * \param data_type raster type (CELL, FCELL, DCELL)
34  *
35  * \return raster type size
36  */
38 {
39  return (type_size[F2I(data_type)]);
40 }
41 
42 /*!
43  * \brief Allocate memory for a raster map of given type
44  *
45  * Allocate an array of CELL, FCELL, or DCELL (depending on
46  * <i>data_type</i>) based on the number of columns in the current
47  * region.
48  *
49  * \param data_type raster type (CELL, FCELL, DCELL)
50  *
51  * \return pointer to allocated buffer
52  */
54 {
55  return (void *)G_calloc(Rast_window_cols() + 1, Rast_cell_size(data_type));
56 }
57 
58 /*!
59  * \brief Allocate memory for a CELL type raster map.
60  *
61  * Allocate an array of CELL based on the number of columns in the
62  * current region.
63  *
64  * This routine allocates a buffer of type CELL just large enough to
65  * hold one row of raster data based on the number of columns in the
66  * active region.
67  *
68  \code
69  CELL *cell;
70  cell = Rast_allocate_c_buf();
71  \endcode
72  *
73  * If larger buffers are required, the routine G_malloc() can be used.
74  * The routine is generally used with each open cell file.
75  *
76  * Prints error message and calls exit() on error.
77  *
78  * \return pointer to allocated buffer
79  */
81 {
82  return (CELL *)G_calloc(Rast_window_cols() + 1, sizeof(CELL));
83 }
84 
85 /*!
86  * \brief Allocates memory for a raster map of type FCELL.
87  *
88  * Allocate an array of FCELL based on the number of columns in the
89  * current region.
90  *
91  * \return pointer to allocated buffer
92  */
94 {
95  return (FCELL *)G_calloc(Rast_window_cols() + 1, sizeof(FCELL));
96 }
97 
98 /*!
99  * \brief Allocates memory for a raster map of type DCELL.
100  *
101  * Allocate an array of DCELL based on the number of columns in the
102  * current region.
103  *
104  * \return pointer to allocated buffer
105  */
107 {
108  return (DCELL *)G_calloc(Rast_window_cols() + 1, sizeof(DCELL));
109 }
110 
111 /*!
112  * \brief Allocates memory for a null buffer.
113  *
114  * Allocate an array of char based on the number of columns in the
115  * current region.
116  *
117  * \return pointer to allocated buffer
118  */
120 {
121  return (char *)G_calloc(Rast_window_cols() + 1, sizeof(char));
122 }
123 
124 /*!
125  * \brief Allocates memory for null bits.
126  *
127  * Allocates an array of unsigned char based on <i>cols</i>.
128  *
129  * \param cols number of columns in region
130  *
131  * \return pointer to allocated buffer
132  */
133 unsigned char *Rast__allocate_null_bits(int cols)
134 {
135  return (unsigned char *)G_calloc(Rast__null_bitstream_size(cols) + 1,
136  sizeof(unsigned char));
137 }
138 
139 /*!
140  * \brief Determines null bitstream size.
141  *
142  * \param cols number of columns
143  *
144  * \return size of null bitstream
145  */
147 {
148  if (cols <= 0)
149  G_fatal_error(_("Rast__null_bitstream_size: cols (%d) is negative"),
150  cols);
151 
152  return (cols + 7) / 8;
153 }
154 
156 {
157  return G_calloc(Rast_input_window_cols() + 1, Rast_cell_size(data_type));
158 }
159 
161 {
162  return (CELL *)G_calloc(Rast_input_window_cols() + 1, sizeof(CELL));
163 }
164 
166 {
167  return (FCELL *)G_calloc(Rast_input_window_cols() + 1, sizeof(FCELL));
168 }
169 
171 {
172  return (DCELL *)G_calloc(Rast_input_window_cols() + 1, sizeof(DCELL));
173 }
174 
176 {
177  return (char *)G_calloc(Rast_input_window_cols() + 1, sizeof(char));
178 }
179 
181 {
182  return G_calloc(Rast_output_window_cols() + 1, Rast_cell_size(data_type));
183 }
184 
186 {
187  return (CELL *)G_calloc(Rast_output_window_cols() + 1, sizeof(CELL));
188 }
189 
191 {
192  return (FCELL *)G_calloc(Rast_output_window_cols() + 1, sizeof(FCELL));
193 }
194 
196 {
197  return (DCELL *)G_calloc(Rast_output_window_cols() + 1, sizeof(DCELL));
198 }
199 
201 {
202  return (char *)G_calloc(Rast_output_window_cols() + 1, sizeof(char));
203 }
char * Rast_allocate_null_input_buf(void)
Definition: alloc_cell.c:175
DCELL * Rast_allocate_d_input_buf(void)
Definition: alloc_cell.c:170
void * Rast_allocate_buf(RASTER_MAP_TYPE data_type)
Allocate memory for a raster map of given type.
Definition: alloc_cell.c:53
size_t Rast_cell_size(RASTER_MAP_TYPE data_type)
Returns size of a raster cell in bytes.
Definition: alloc_cell.c:37
FCELL * Rast_allocate_f_input_buf(void)
Definition: alloc_cell.c:165
CELL * Rast_allocate_c_output_buf(void)
Definition: alloc_cell.c:185
void * Rast_allocate_output_buf(RASTER_MAP_TYPE data_type)
Definition: alloc_cell.c:180
FCELL * Rast_allocate_f_buf(void)
Allocates memory for a raster map of type FCELL.
Definition: alloc_cell.c:93
CELL * Rast_allocate_c_buf(void)
Allocate memory for a CELL type raster map.
Definition: alloc_cell.c:80
CELL * Rast_allocate_c_input_buf(void)
Definition: alloc_cell.c:160
#define F2I(map_type)
Definition: alloc_cell.c:21
char * Rast_allocate_null_buf(void)
Allocates memory for a null buffer.
Definition: alloc_cell.c:119
unsigned char * Rast__allocate_null_bits(int cols)
Allocates memory for null bits.
Definition: alloc_cell.c:133
DCELL * Rast_allocate_d_output_buf(void)
Definition: alloc_cell.c:195
DCELL * Rast_allocate_d_buf(void)
Allocates memory for a raster map of type DCELL.
Definition: alloc_cell.c:106
void * Rast_allocate_input_buf(RASTER_MAP_TYPE data_type)
Definition: alloc_cell.c:155
char * Rast_allocate_null_output_buf(void)
Definition: alloc_cell.c:200
int Rast__null_bitstream_size(int cols)
Determines null bitstream size.
Definition: alloc_cell.c:146
FCELL * Rast_allocate_f_output_buf(void)
Definition: alloc_cell.c:190
#define G_calloc(m, n)
Definition: defs/gis.h:95
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int Rast_input_window_cols(void)
Number of columns in active input window.
int Rast_window_cols(void)
Number of columns in active window.
int Rast_output_window_cols(void)
Number of columns in active output window.
float FCELL
Definition: gis.h:636
double DCELL
Definition: gis.h:635
int CELL
Definition: gis.h:634
#define _(str)
Definition: glocale.h:10
int RASTER_MAP_TYPE
Definition: raster.h:25