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
Rast_cell_size(RASTER_MAP_TYPE data_type)39 size_t Rast_cell_size(RASTER_MAP_TYPE data_type)
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 */
Rast_allocate_buf(RASTER_MAP_TYPE data_type)55 void *Rast_allocate_buf(RASTER_MAP_TYPE data_type)
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 */
Rast_allocate_c_buf(void)82 CELL *Rast_allocate_c_buf(void)
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 */
Rast_allocate_f_buf(void)95 FCELL *Rast_allocate_f_buf(void)
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 */
Rast_allocate_d_buf(void)108 DCELL *Rast_allocate_d_buf(void)
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 */
Rast_allocate_null_buf(void)121 char *Rast_allocate_null_buf(void)
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 */
Rast__allocate_null_bits(int cols)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 */
Rast__null_bitstream_size(int cols)148 int Rast__null_bitstream_size(int cols)
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
Rast_allocate_input_buf(RASTER_MAP_TYPE data_type)157 void *Rast_allocate_input_buf(RASTER_MAP_TYPE data_type)
158 {
159 return G_calloc(Rast_input_window_cols() + 1, Rast_cell_size(data_type));
160 }
161
Rast_allocate_c_input_buf(void)162 CELL *Rast_allocate_c_input_buf(void)
163 {
164 return (CELL *) G_calloc(Rast_input_window_cols() + 1, sizeof(CELL));
165 }
166
Rast_allocate_f_input_buf(void)167 FCELL *Rast_allocate_f_input_buf(void)
168 {
169 return (FCELL *) G_calloc(Rast_input_window_cols() + 1, sizeof(FCELL));
170 }
171
Rast_allocate_d_input_buf(void)172 DCELL *Rast_allocate_d_input_buf(void)
173 {
174 return (DCELL *) G_calloc(Rast_input_window_cols() + 1, sizeof(DCELL));
175 }
176
Rast_allocate_null_input_buf(void)177 char *Rast_allocate_null_input_buf(void)
178 {
179 return (char *)G_calloc(Rast_input_window_cols() + 1, sizeof(char));
180 }
181
182
Rast_allocate_output_buf(RASTER_MAP_TYPE data_type)183 void *Rast_allocate_output_buf(RASTER_MAP_TYPE data_type)
184 {
185 return G_calloc(Rast_output_window_cols() + 1, Rast_cell_size(data_type));
186 }
187
Rast_allocate_c_output_buf(void)188 CELL *Rast_allocate_c_output_buf(void)
189 {
190 return (CELL *) G_calloc(Rast_output_window_cols() + 1, sizeof(CELL));
191 }
192
Rast_allocate_f_output_buf(void)193 FCELL *Rast_allocate_f_output_buf(void)
194 {
195 return (FCELL *) G_calloc(Rast_output_window_cols() + 1, sizeof(FCELL));
196 }
197
Rast_allocate_d_output_buf(void)198 DCELL *Rast_allocate_d_output_buf(void)
199 {
200 return (DCELL *) G_calloc(Rast_output_window_cols() + 1, sizeof(DCELL));
201 }
202
Rast_allocate_null_output_buf(void)203 char *Rast_allocate_null_output_buf(void)
204 {
205 return (char *)G_calloc(Rast_output_window_cols() + 1, sizeof(char));
206 }
207