5
* \brief GIS Library - Raster allocation routines.
7
* (C) 2001-2008 by the GRASS Development Team
9
* This program is free software under the GNU General Public License
10
* (>=v2). Read the file COPYING that comes with GRASS for details.
12
* \author GRASS GIS Development Team
18
#include <grass/gis.h>
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))
24
static int type_size[3] = { sizeof(CELL), sizeof(FCELL), sizeof(DCELL) };
28
* \brief Returns size of a raster CELL in bytes.
30
* If <b>data_type</b> is CELL_TYPE, returns sizeof(CELL)
31
* If <b>data_type</b> is FCELL_TYPE, returns sizeof(FCELL)
32
* If <b>data_type</b> is DCELL_TYPE, returns sizeof(DCELL)
34
* \param[in] data_type
38
size_t G_raster_size(RASTER_MAP_TYPE data_type)
40
return (type_size[F2I(data_type)]);
45
* \brief Allocate memory for a CELL type raster map.
47
* This routine allocates a buffer of type CELL just large enough to
48
* hold one row of raster data based on the number of columns in the
52
cell = G_allocate_cell_buf ();
54
* If larger buffers are required, the routine <i>G_malloc</i> can be used.
55
* The routine is generally used with each open cell file.<br>
56
* <b>Note:</b> <i>G_allocate_raster_buf()</i> or
57
* <i>G_alloc_c_raster_buf()</i> is preferred over
58
* <i>G_allocate_cell_buf()</i>.
60
* \return CELL * Pointer to allocated buffer
61
* \return Prints error message and calls <i>exit()</i> on error
64
CELL *G_allocate_cell_buf(void)
66
return (CELL *) G_calloc(G_window_cols() + 1, sizeof(CELL));
71
* \brief Allocate memory for a raster map of type <b>data_type</b>.
73
* Allocate an array of CELL, FCELL, or DCELL (depending on
74
* <b>data_type</b>) based on the number of columns in the current
77
* \param[in] data_type
81
void *G_allocate_raster_buf(RASTER_MAP_TYPE data_type)
83
return (void *)G_calloc(G_window_cols() + 1, G_raster_size(data_type));
88
* \brief Allocates memory for a raster map of type CELL.
90
* Allocate an array of CELL based on the number of columns in the
96
CELL *G_allocate_c_raster_buf(void)
98
return (CELL *) G_calloc(G_window_cols() + 1, sizeof(CELL));
103
* \brief Allocates memory for a raster map of type FCELL.
105
* Allocate an array of FCELL based on the number of columns in the
111
FCELL *G_allocate_f_raster_buf(void)
113
return (FCELL *) G_calloc(G_window_cols() + 1, sizeof(FCELL));
118
* \brief Allocates memory for a raster map of type DCELL.
120
* Allocate an array of DCELL based on the number of columns in the
126
DCELL *G_allocate_d_raster_buf(void)
128
return (DCELL *) G_calloc(G_window_cols() + 1, sizeof(DCELL));
133
* \brief Allocates memory for a null buffer.
135
* Allocate an array of char based on the number of columns in the
141
char *G_allocate_null_buf(void)
143
return (char *)G_calloc(G_window_cols() + 1, sizeof(char));
148
* \brief Allocates memory for null bits.
150
* Allocates an array of unsigned char based on <b>cols</b>.
152
* \param[in] cols number of columns in region
153
* \return unsigned char *
156
unsigned char *G__allocate_null_bits(int cols)
158
return (unsigned char *)G_calloc(G__null_bitstream_size(cols) + 1,
159
sizeof(unsigned char));
164
* \brief Determines null bitstream size.
166
* \param[in] cols number of columns
167
* \return -1 if <b>cols</b> is invalid (<= 0)
168
* \return size of null bistream
171
int G__null_bitstream_size(int cols)
176
return (cols / 8 + (cols % 8 != 0));