GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-847944e18e
smain.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <grass/bitmap.h>
4 
5 static int dump_map(struct BM *map);
6 
7 int main(int argc, char *argv[])
8 {
9  int SIZE;
10  struct BM *map, *map2;
11  int i, x, y;
12  int dump;
13  FILE *fp;
14 
15  if (argc < 2)
16  SIZE = 11;
17  else
18  SIZE = atoi(argv[1]);
19 
20  if (NULL == getenv("NODUMP"))
21  dump = 1;
22  else
23  dump = 0;
24 
25  map = BM_create_sparse(SIZE, SIZE);
26 
27  /* turn on bits in X pattern */
28  for (i = 0; i < SIZE; i++) {
29  BM_set(map, i, i, 1);
30  BM_set(map, (SIZE - 1) - i, i, 1);
31  }
32 
33  if (dump)
34  dump_map(map);
35 
36  fprintf(stdout, "Size = %d\n", BM_get_map_size(map));
37 
38  /*
39  BM_dump_map_sparse (map);
40  */
41 
42  fprintf(stdout, "\n\n");
43  /* now invert it */
44  for (y = 0; y < SIZE; y++)
45  for (x = 0; x < SIZE; x++) {
46  BM_set(map, x, y, !BM_get(map, x, y));
47 #ifdef FOO
48  /*DEBUG*/ if (y == 0)
49  /*DEBUG*/ BM_dump_map_row_sparse(map, y);
50 #endif
51  }
52 
53  if (dump)
54  dump_map(map);
55 
56  fprintf(stdout, "Size = %d\n", BM_get_map_size(map));
57  /*
58  fprintf (stdout, "\n\n");
59  BM_dump_map_sparse (map);
60  */
61  {
62  fp = fopen("dumpfile", "w");
63  if (0 > BM_file_write(fp, map)) {
64  fprintf(stderr, "File_write failed\n");
65  goto nowrite;
66  }
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(map2);
76 nowrite:
77 
78  BM_destroy(map);
79 
80  return 0;
81 }
82 
83 static int dump_map(struct BM *map)
84 {
85  int x, y;
86 
87  for (y = 0; y < map->rows; y++) {
88  for (x = 0; x < map->cols; x++) {
89  fprintf(stdout, "%c", BM_get(map, x, y) ? '#' : '.');
90  }
91  fprintf(stdout, "\n");
92  }
93 }
#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_dump_map_row_sparse(struct BM *, int)
Debugging code to dump out structure of links for single row.
Definition: sparse.c:323
struct BM * BM_create_sparse(int, int)
Create a sparse bitmap of dimension 'x'/'y'.
Definition: sparse.c:42
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
int main(int argc, char *argv[])
Definition: smain.c:7
Definition: bitmap.h:17
int rows
Definition: bitmap.h:18
int cols
Definition: bitmap.h:19
#define x