GRASS 8 Programmer's Manual 8.6.0dev(2026)-b43c9b8403
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1/****************************************************************************
2 *
3 * MODULE: bitmap
4 * AUTHOR(S): David Gerdes (CERL) (original contributor)
5 * Markus Neteler <neteler itc.it>,
6 * Bernhard Reiter <bernhard intevation.de>,
7 * Brad Douglas <rez touchofmadness.com>,
8 * Glynn Clements <glynn gclements.plus.com>
9 * PURPOSE: provides basic support for the creation and manipulation of
10 * two dimensional bitmap arrays
11 * SPDX-FileCopyrightText: 1999-2006 GRASS Development Team
12 * SPDX-License-Identifier: GPL-2.0-or-later
13 *
14 *****************************************************************************/
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <grass/bitmap.h>
19
20static int dump_map(struct BM *map);
21
22int main(int argc, char *argv[])
23{
24 int SIZE;
25 struct BM *map, *map2;
26 int i, x, y;
27 int dump;
28 FILE *fp;
29
30 if (argc < 2)
31 SIZE = 11;
32 else
33 SIZE = atoi(argv[1]);
34
35 if (NULL != getenv("NODUMP"))
36 dump = 0;
37 else
38 dump = 1;
39
40 map = BM_create(SIZE, SIZE);
41
42 /* turn on bits in X pattern */
43 for (i = 0; i < SIZE; i++) {
44 BM_set(map, i, i, 1);
45 BM_set(map, (SIZE - 1) - i, i, 1);
46 }
47
48 if (dump)
49 dump_map(map);
50 fprintf(stdout, "Size = %d\n", BM_get_map_size(map));
51
52 fprintf(stdout, "\n\n");
53
54 /* now invert it */
55 for (y = 0; y < SIZE; y++)
56 for (x = 0; x < SIZE; x++)
57 BM_set(map, x, y, !BM_get(map, x, y));
58
59 if (dump)
60 dump_map(map);
61
62 fprintf(stdout, "Size = %d\n", BM_get_map_size(map));
63
64 {
65 fp = fopen("dumpfile", "w");
66 BM_file_write(fp, map);
67 fclose(fp);
68
69 fp = fopen("dumpfile", "r");
70 map2 = BM_file_read(fp);
71 fclose(fp);
72 dump_map(map2);
73 }
74
75 BM_destroy(map);
77
78 return 0;
79}
80
81static int dump_map(struct BM *map)
82{
83 int x, y;
84
85 for (y = 0; y < map->rows; y++) {
86 for (x = 0; x < map->cols; x++) {
87 fprintf(stdout, "%c", BM_get(map, x, y) ? '#' : '.');
88 }
89 fprintf(stdout, "\n");
90 }
91}
#define NULL
Definition ccmath.h:32
int BM_get(struct BM *, int, int)
Gets 'val' from the bitmap.
Definition bitmap.c:213
int BM_file_write(FILE *, struct BM *)
Write bitmap out to file.
Definition bitmap.c:258
struct BM * BM_file_read(FILE *)
Create map structure and load it from file.
Definition bitmap.c:299
int BM_destroy(struct BM *)
Destroy bitmap and free all associated memory.
Definition bitmap.c:88
size_t BM_get_map_size(struct BM *)
Returns size in bytes that bitmap is taking up.
Definition bitmap.c:236
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
Definition bitmap.h:17
int rows
Definition bitmap.h:18
int cols
Definition bitmap.h:19
int main(void)
Definition winlocale.c:201
#define x