GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
gs_bm.c
Go to the documentation of this file.
1/*!
2 \file lib/ogsf/gs_bm.c
3
4 \brief OGSF library - manipulating bitmaps (lower level functions)
5
6 GRASS OpenGL gsurf OGSF Library
7
8 SPDX-FileCopyrightText: 1999-2008 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10
11 \author Bill Brown USACERL, GMSL/University of Illinois (September 1993)
12 \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
13 */
14
15#include <grass/gis.h>
16#include <grass/glocale.h>
17#include <grass/ogsf.h>
18
19#include "gsget.h"
20
21/*!
22 \brief Do combining of bitmaps, make bitmaps from other data w/maskval
23
24 \param frombuff data buffer
25 \param maskval mask type
26 \param rows number of rows
27 \param cols number of cols
28
29 \return pointer to BM struct
30 */
31struct BM *gsbm_make_mask(typbuff *frombuff, float maskval, int rows, int cols)
32{
33 int i, j, ioff;
34 struct BM *bm;
35 float curval;
36
37 bm = BM_create(cols, rows);
38
39 if (frombuff) {
40 if (frombuff->bm) {
41 for (i = 0; i < rows; i++) {
42 ioff = i * cols;
43
44 for (j = 0; j < cols; j++) {
45 BM_set(bm, j, i, BM_get(frombuff->bm, j, i));
46 }
47 }
48 }
49 else {
50 for (i = 0; i < rows; i++) {
51 ioff = i * cols;
52
53 for (j = 0; j < cols; j++) {
54 if (GET_MAPATT(frombuff, (ioff + j), curval)) {
55 BM_set(bm, j, i, (curval == maskval));
56 }
57 else {
58 BM_set(bm, j, i, 0); /* doesn't mask nulls */
59 }
60 }
61 }
62 }
63 }
64
65 return (bm);
66}
67
68/*!
69 \brief Zero mask
70
71 \param map pointer to BM struct
72 */
73void gsbm_zero_mask(struct BM *map)
74{
75 int numbytes;
76 unsigned char *buf;
77
78 numbytes = map->bytes * map->rows;
79 buf = map->data;
80
81 while (numbytes--) {
82 *buf++ = 0;
83 }
84
85 return;
86}
87
88/*!
89 \brief mask types
90 */
91#define MASK_OR 1
92#define MASK_ORNOT 2
93#define MASK_AND 3
94#define MASK_XOR 4
95
96/*!
97 \brief Mask bitmap
98
99 Must be same size, ORs bitmaps & stores in bmvar
100
101 \param bmvar bitmap (BM) to changed
102 \param bmcon bitmap (BM)
103 \param mask_type mask type (see mask types macros)
104
105 \return -1 on failure (bitmap mispatch)
106 \return 0 on success
107 */
108static int gsbm_masks(struct BM *bmvar, struct BM *bmcon, const int mask_type)
109{
110 int i;
111 int varsize, consize, numbytes;
112
113 varsize = bmvar->rows * bmvar->cols;
114 consize = bmcon->rows * bmcon->cols;
115 numbytes = bmvar->bytes * bmvar->rows;
116
117 if (bmcon && bmvar) {
118 if (varsize != consize) {
119 G_warning(_("Bitmap mismatch"));
120 return (-1);
121 }
122
123 if (bmvar->sparse || bmcon->sparse)
124 return (-1);
125
126 switch (mask_type) {
127 case MASK_OR:
128 for (i = 0; i < numbytes; i++)
129 bmvar->data[i] |= bmcon->data[i];
130 break;
131 case MASK_ORNOT:
132 for (i = 0; i < numbytes; i++)
133 bmvar->data[i] |= ~bmcon->data[i];
134 break;
135 case MASK_AND:
136 for (i = 0; i < numbytes; i++)
137 bmvar->data[i] &= bmcon->data[i];
138 break;
139 case MASK_XOR:
140 for (i = 0; i < numbytes; i++)
141 bmvar->data[i] ^= bmcon->data[i];
142 break;
143 }
144
145 return (0);
146 }
147
148 return (-1);
149}
150
151/*!
152 \brief Mask bitmap (mask type OR)
153
154 Must be same size, ORs bitmaps & stores in bmvar
155
156 \param bmvar bitmap (BM) to changed
157 \param bmcon bitmap (BM)
158 \param mask_type mask type (see mask types macros)
159
160 \return -1 on failure (bitmap mispatch)
161 \return 0 on success
162 */
163int gsbm_or_masks(struct BM *bmvar, struct BM *bmcon)
164{
165 return gsbm_masks(bmvar, bmcon, MASK_OR);
166}
167
168/*!
169 \brief Mask bitmap (mask type ORNOT)
170
171 Must be same size, ORNOTs bitmaps & stores in bmvar
172
173 \param bmvar bitmap (BM) to changed
174 \param bmcon bitmap (BM)
175
176 \return -1 on failure (bitmap mispatch)
177 \return 0 on success
178 */
179int gsbm_ornot_masks(struct BM *bmvar, struct BM *bmcon)
180{
181 return gsbm_masks(bmvar, bmcon, MASK_ORNOT);
182}
183
184/*!
185 \brief Mask bitmap (mask type ADD)
186
187 Must be same size, ADDs bitmaps & stores in bmvar
188
189 \param bmvar bitmap (BM) to changed
190 \param bmcon bitmap (BM)
191
192 \return -1 on failure (bitmap mispatch)
193 \return 0 on success
194 */
195int gsbm_and_masks(struct BM *bmvar, struct BM *bmcon)
196{
197 return gsbm_masks(bmvar, bmcon, MASK_AND);
198}
199
200/*!
201 \brief Mask bitmap (mask type XOR)
202
203 Must be same size, XORs bitmaps & stores in bmvar
204
205 \param bmvar bitmap (BM) to changed
206 \param bmcon bitmap (BM)
207
208 \return -1 on failure (bitmap mispatch)
209 \return 0 on success
210 */
211int gsbm_xor_masks(struct BM *bmvar, struct BM *bmcon)
212{
213 return gsbm_masks(bmvar, bmcon, MASK_XOR);
214}
215
216/*!
217 \brief Update current maps
218
219 \param surf surface (geosurf)
220
221 \return 0
222 \return 1
223 */
225{
226 struct BM *b_mask, *b_topo, *b_color;
228 int row, col, offset, destroy_ok = 1;
230
231 G_debug(5, "gs_update_curmask(): id=%d", surf->gsurf_id);
232
233 if (surf->mask_needupdate) {
234 surf->mask_needupdate = 0;
235 surf->norm_needupdate = 1; /* edges will need to be recalculated */
236
238
239 if (!t_topo) {
240 surf->mask_needupdate = 1;
241
242 return (0);
243 }
244
245 if (surf->nz_topo || surf->nz_color || gs_mask_defined(surf) ||
246 t_topo->nm) {
248
249 if (!surf->curmask) {
250 surf->curmask = BM_create(surf->cols, surf->rows);
251 }
252 else {
253 gsbm_zero_mask(surf->curmask);
254 }
255
256 if (surf->nz_topo) {
257 /* no_zero elevation */
258 b_topo = gsbm_make_mask(t_topo, 0.0, surf->rows, surf->cols);
259 }
260
261 /* make_mask_from_color */
262 if (surf->nz_color && surf->att[ATT_COLOR].att_src == MAP_ATT) {
264 coloratt = &(surf->att[ATT_COLOR]);
265 b_color = BM_create(surf->cols, surf->rows);
266
267 for (row = 0; row < surf->rows; row++) {
268 for (col = 0; col < surf->cols; col++) {
269 offset = row * surf->cols + col;
270 BM_set(b_color, col, row,
271 (NULL_COLOR ==
272 gs_mapcolor(t_color, coloratt, offset)));
273 }
274 }
275 }
276
277 if (gs_mask_defined(surf)) {
279
280 if (t_mask->bm) {
281 b_mask = t_mask->bm;
282 destroy_ok = 0;
283 }
284 else {
285 b_mask = BM_create(surf->cols, surf->rows);
286 gs_set_maskmode((int)surf->att[ATT_MASK].constant);
287
288 for (row = 0; row < surf->rows; row++) {
289 for (col = 0; col < surf->cols; col++) {
290 offset = row * surf->cols + col;
291 BM_set(b_mask, col, row,
292 gs_masked(t_mask, col, row, offset));
293 }
294 }
295 }
296 }
297
298 if (b_topo) {
299 G_debug(5, "gs_update_curmask(): update topo mask");
300 gsbm_or_masks(surf->curmask, b_topo);
302 }
303
304 if (b_color) {
305 G_debug(5, "gs_update_curmask(): update color mask");
306 gsbm_or_masks(surf->curmask, b_color);
308 }
309
310 if (t_topo->nm) {
311 G_debug(5, "gs_update_curmask(): update elev null mask");
312 gsbm_or_masks(surf->curmask, t_topo->nm);
313 }
314
315 if (b_mask) {
316 G_debug(5, "gs_update_curmask(): update mask mask");
317
318 if (t_mask->bm) {
319 if (surf->att[ATT_MASK].constant) {
320 /* invert */
321 gsbm_or_masks(surf->curmask, t_mask->bm);
322 }
323 else {
324 gsbm_ornot_masks(surf->curmask, t_mask->bm);
325 }
326 }
327 else {
328 gsbm_or_masks(surf->curmask, b_mask);
329 }
330
331 if (destroy_ok) {
333 }
334 }
335
336 /* TODO: update surf->zminmasked */
337 return (1);
338 }
339 else if (surf->curmask) {
340 BM_destroy(surf->curmask);
341 surf->curmask = NULL;
342 surf->zminmasked = surf->zmin;
343 }
344 }
345
346 return (0);
347}
348
349/*!
350 \brief Print bitmap to stderr
351
352 \param bm bitmap (BM)
353 */
354void print_bm(struct BM *bm)
355{
356 int i, j;
357
358 for (i = 0; i < bm->rows; i++) {
359 for (j = 0; j < bm->cols; j++) {
360 fprintf(stderr, "%d ", BM_get(bm, j, i));
361 }
362
363 fprintf(stderr, "\n");
364 }
365
366 return;
367}
#define NULL
Definition ccmath.h:32
int BM_get(struct BM *, int, int)
Gets 'val' from the bitmap.
Definition bitmap.c:213
int BM_destroy(struct BM *)
Destroy bitmap and free all associated memory.
Definition bitmap.c:88
int BM_set(struct BM *, int, int, int)
Sets bitmap value to 'val' at location 'x' 'y'.
Definition bitmap.c:182
struct BM * BM_create(int, int)
Create bitmap of dimension x/y and return structure token.
Definition bitmap.c:57
void G_warning(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
int gs_masked(typbuff *, int, int, int)
Should only be called when setting up the current mask (gs_bm.c)
Definition gs.c:928
int gs_mapcolor(typbuff *, gsurf_att *, int)
Call this one when you already know att_src is MAP_ATT.
Definition gs.c:964
typbuff * gs_get_att_typbuff(geosurf *, int, int)
Get attribute data buffer.
Definition gs.c:677
void gs_set_maskmode(int)
Set geosurf mask mode.
Definition gs.c:895
int gs_mask_defined(geosurf *)
Check if mask is defined.
Definition gs.c:910
#define _(str)
Definition glocale.h:10
int gsbm_and_masks(struct BM *bmvar, struct BM *bmcon)
Mask bitmap (mask type ADD)
Definition gs_bm.c:195
int gsbm_ornot_masks(struct BM *bmvar, struct BM *bmcon)
Mask bitmap (mask type ORNOT)
Definition gs_bm.c:179
#define MASK_OR
mask types
Definition gs_bm.c:91
#define MASK_AND
Definition gs_bm.c:93
struct BM * gsbm_make_mask(typbuff *frombuff, float maskval, int rows, int cols)
Do combining of bitmaps, make bitmaps from other data w/maskval.
Definition gs_bm.c:31
int gsbm_or_masks(struct BM *bmvar, struct BM *bmcon)
Mask bitmap (mask type OR)
Definition gs_bm.c:163
#define MASK_ORNOT
Definition gs_bm.c:92
#define MASK_XOR
Definition gs_bm.c:94
int gs_update_curmask(geosurf *surf)
Update current maps.
Definition gs_bm.c:224
int gsbm_xor_masks(struct BM *bmvar, struct BM *bmcon)
Mask bitmap (mask type XOR)
Definition gs_bm.c:211
void print_bm(struct BM *bm)
Print bitmap to stderr.
Definition gs_bm.c:354
void gsbm_zero_mask(struct BM *map)
Zero mask.
Definition gs_bm.c:73
#define GET_MAPATT(buff, offset, att)
Definition gsget.h:29
OGSF header file (structures)
#define ATT_MASK
Definition ogsf.h:78
#define ATT_TOPO
Definition ogsf.h:76
#define NULL_COLOR
Definition ogsf.h:159
#define ATT_COLOR
Definition ogsf.h:77
#define MAP_ATT
Definition ogsf.h:86
Definition bitmap.h:17
int rows
Definition bitmap.h:18
int cols
Definition bitmap.h:19
unsigned char * data
Definition bitmap.h:21
size_t bytes
Definition bitmap.h:20
Definition ogsf.h:267