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