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