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