2
* \file lib/raster/alloc_cell.c
4
* \brief Raster Library - Raster allocation routines.
6
* (C) 2001-2009 by the GRASS Development Team
8
* This program is free software under the GNU General Public License
9
* (>=v2). Read the file COPYING that comes with GRASS for details.
11
* \author Original author CERL
16
#include <grass/gis.h>
17
#include <grass/raster.h>
18
#include <grass/glocale.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 const int type_size[3] =
25
{ sizeof(CELL), sizeof(FCELL), sizeof(DCELL) };
28
* \brief Returns size of a raster cell in bytes.
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)
34
* \param data_type raster type (CELL, FCELL, DCELL)
36
* \return raster type size
39
size_t Rast_cell_size(RASTER_MAP_TYPE data_type)
41
return (type_size[F2I(data_type)]);
45
* \brief Allocate memory for a raster map of given type
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
51
* \param data_type raster type (CELL, FCELL, DCELL)
53
* \return pointer to allocated buffer
55
void *Rast_allocate_buf(RASTER_MAP_TYPE data_type)
57
return (void *)G_calloc(Rast_window_cols() + 1, Rast_cell_size(data_type));
61
* \brief Allocate memory for a CELL type raster map.
63
* Allocate an array of CELL based on the number of columns in the
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
72
cell = Rast_allocate_c_buf();
75
* If larger buffers are required, the routine G_malloc() can be used.
76
* The routine is generally used with each open cell file.
78
* Prints error message and calls exit() on error.
80
* \return pointer to allocated buffer
82
CELL *Rast_allocate_c_buf(void)
84
return (CELL *) G_calloc(Rast_window_cols() + 1, sizeof(CELL));
88
* \brief Allocates memory for a raster map of type FCELL.
90
* Allocate an array of FCELL based on the number of columns in the
93
* \return pointer to allocated buffer
95
FCELL *Rast_allocate_f_buf(void)
97
return (FCELL *) G_calloc(Rast_window_cols() + 1, sizeof(FCELL));
101
* \brief Allocates memory for a raster map of type DCELL.
103
* Allocate an array of DCELL based on the number of columns in the
106
* \return pointer to allocated buffer
108
DCELL *Rast_allocate_d_buf(void)
110
return (DCELL *) G_calloc(Rast_window_cols() + 1, sizeof(DCELL));
114
* \brief Allocates memory for a null buffer.
116
* Allocate an array of char based on the number of columns in the
119
* \return pointer to allocated buffer
121
char *Rast_allocate_null_buf(void)
123
return (char *)G_calloc(Rast_window_cols() + 1, sizeof(char));
127
* \brief Allocates memory for null bits.
129
* Allocates an array of unsigned char based on <i>cols</i>.
131
* \param cols number of columns in region
133
* \return pointer to allocated buffer
135
unsigned char *Rast__allocate_null_bits(int cols)
137
return (unsigned char *)G_calloc(Rast__null_bitstream_size(cols) + 1,
138
sizeof(unsigned char));
142
* \brief Determines null bitstream size.
144
* \param cols number of columns
146
* \return size of null bistream
148
int Rast__null_bitstream_size(int cols)
151
G_fatal_error(_("Rast__null_bitstream_size: cols (%d) is negative"),
154
return (cols + 7) / 8;
157
void *Rast_allocate_input_buf(RASTER_MAP_TYPE data_type)
159
return G_calloc(Rast_input_window_cols() + 1, Rast_cell_size(data_type));
162
CELL *Rast_allocate_c_input_buf(void)
164
return (CELL *) G_calloc(Rast_input_window_cols() + 1, sizeof(CELL));
167
FCELL *Rast_allocate_f_input_buf(void)
169
return (FCELL *) G_calloc(Rast_input_window_cols() + 1, sizeof(FCELL));
172
DCELL *Rast_allocate_d_input_buf(void)
174
return (DCELL *) G_calloc(Rast_input_window_cols() + 1, sizeof(DCELL));
177
char *Rast_allocate_null_input_buf(void)
179
return (char *)G_calloc(Rast_input_window_cols() + 1, sizeof(char));
183
void *Rast_allocate_output_buf(RASTER_MAP_TYPE data_type)
185
return G_calloc(Rast_output_window_cols() + 1, Rast_cell_size(data_type));
188
CELL *Rast_allocate_c_output_buf(void)
190
return (CELL *) G_calloc(Rast_output_window_cols() + 1, sizeof(CELL));
193
FCELL *Rast_allocate_f_output_buf(void)
195
return (FCELL *) G_calloc(Rast_output_window_cols() + 1, sizeof(FCELL));
198
DCELL *Rast_allocate_d_output_buf(void)
200
return (DCELL *) G_calloc(Rast_output_window_cols() + 1, sizeof(DCELL));
203
char *Rast_allocate_null_output_buf(void)
205
return (char *)G_calloc(Rast_output_window_cols() + 1, sizeof(char));