GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
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  * COPYRIGHT: (C) 1999-2006 by the GRASS Development Team
12  *
13  * This program is free software under the GNU General Public
14  * License (>=v2). Read the file COPYING that comes with GRASS
15  * for details.
16  *
17  *****************************************************************************/
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <grass/bitmap.h>
22 
23 static int dump_map(struct BM *map);
24 
25 int main(int argc, char *argv[])
26 {
27  int SIZE;
28  struct BM *map, *map2;
29  int i, x, y;
30  int dump;
31  FILE *fp;
32 
33  if (argc < 2)
34  SIZE = 11;
35  else
36  SIZE = atoi(argv[1]);
37 
38  if (NULL != getenv("NODUMP"))
39  dump = 0;
40  else
41  dump = 1;
42 
43  map = BM_create(SIZE, SIZE);
44 
45  /* turn on bits in X pattern */
46  for (i = 0; i < SIZE; i++) {
47  BM_set(map, i, i, 1);
48  BM_set(map, (SIZE - 1) - i, i, 1);
49  }
50 
51  if (dump)
52  dump_map(map);
53  fprintf(stdout, "Size = %d\n", BM_get_map_size(map));
54 
55  fprintf(stdout, "\n\n");
56 
57  /* now invert it */
58  for (y = 0; y < SIZE; y++)
59  for (x = 0; x < SIZE; x++)
60  BM_set(map, x, y, !BM_get(map, x, y));
61 
62  if (dump)
63  dump_map(map);
64 
65  fprintf(stdout, "Size = %d\n", BM_get_map_size(map));
66 
67  {
68  fp = fopen("dumpfile", "w");
69  BM_file_write(fp, map);
70  fclose(fp);
71 
72  fp = fopen("dumpfile", "r");
73  map2 = BM_file_read(fp);
74  fclose(fp);
75  dump_map(map2);
76  }
77 
78  BM_destroy(map);
79  BM_destroy(map2);
80 
81  return 0;
82 }
83 
84 static int dump_map(struct BM *map)
85 {
86  int x, y;
87 
88  for (y = 0; y < map->rows; y++) {
89  for (x = 0; x < map->cols; x++) {
90  fprintf(stdout, "%c", BM_get(map, x, y) ? '#' : '.');
91  }
92  fprintf(stdout, "\n");
93  }
94 }
#define NULL
Definition: ccmath.h:32
int BM_get(struct BM *, int, int)
Gets 'val' from the bitmap.
Definition: bitmap.c:217
int BM_file_write(FILE *, struct BM *)
Write bitmap out to file.
Definition: bitmap.c:264
int BM_destroy(struct BM *)
Destroy bitmap and free all associated memory.
Definition: bitmap.c:89
size_t BM_get_map_size(struct BM *)
Returns size in bytes that bitmap is taking up.
Definition: bitmap.c:241
int BM_set(struct BM *, int, int, int)
Sets bitmap value to 'val' at location 'x' 'y'.
Definition: bitmap.c:185
struct BM * BM_file_read(FILE *)
Create map structure and load it from file.
Definition: bitmap.c:306
struct BM * BM_create(int, int)
Create bitmap of dimension x/y and return structure token.
Definition: bitmap.c:58
int main(int argc, char *argv[])
Definition: main.c:25
Definition: bitmap.h:17
int rows
Definition: bitmap.h:18
int cols
Definition: bitmap.h:19
#define x