GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
smain.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <grass/bitmap.h>
4 
5 
6 static int dump_map(struct BM *map);
7 
8 
9 int main(int argc, char *argv[])
10 {
11  int SIZE;
12  struct BM *map, *map2;
13  int i, x, y;
14  int dump;
15  FILE *fp;
16 
17  if (argc < 2)
18  SIZE = 11;
19  else
20  SIZE = atoi(argv[1]);
21 
22  if (NULL == getenv("NODUMP"))
23  dump = 1;
24  else
25  dump = 0;
26 
27  map = BM_create_sparse(SIZE, SIZE);
28 
29  /* turn on bits in X pattern */
30  for (i = 0; i < SIZE; i++) {
31  BM_set(map, i, i, 1);
32  BM_set(map, (SIZE - 1) - i, i, 1);
33  }
34 
35  if (dump)
36  dump_map(map);
37 
38  fprintf(stdout, "Size = %d\n", BM_get_map_size(map));
39 
40  /*
41  BM_dump_map_sparse (map);
42  */
43 
44  fprintf(stdout, "\n\n");
45  /* now invert it */
46  for (y = 0; y < SIZE; y++)
47  for (x = 0; x < SIZE; x++) {
48  BM_set(map, x, y, !BM_get(map, x, y));
49 #ifdef FOO
50  /*DEBUG*/ if (y == 0)
51  /*DEBUG*/ BM_dump_map_row_sparse(map, y);
52 #endif
53  }
54 
55  if (dump)
56  dump_map(map);
57 
58  fprintf(stdout, "Size = %d\n", BM_get_map_size(map));
59  /*
60  fprintf (stdout, "\n\n");
61  BM_dump_map_sparse (map);
62  */
63  {
64  fp = fopen("dumpfile", "w");
65  if (0 > BM_file_write(fp, map)) {
66  fprintf(stderr, "File_write failed\n");
67  goto nowrite;
68  }
69  fclose(fp);
70 
71  fp = fopen("dumpfile", "r");
72  map2 = BM_file_read(fp);
73  fclose(fp);
74  dump_map(map2);
75  }
76 
77  BM_destroy(map2);
78  nowrite:
79 
80  BM_destroy(map);
81 
82  return 0;
83 }
84 
85 
86 static int dump_map(struct BM *map)
87 {
88  int x, y;
89 
90  for (y = 0; y < map->rows; y++) {
91  for (x = 0; x < map->cols; x++) {
92  fprintf(stdout, "%c", BM_get(map, x, y) ? '#' : '.');
93 
94  }
95  fprintf(stdout, "\n");
96  }
97 }
int BM_dump_map_row_sparse(struct BM *, int)
Debugging code to dump out structure of links for single row.
Definition: sparse.c:332
size_t BM_get_map_size(struct BM *)
Returns size in bytes that bitmap is taking up.
Definition: bitmap.c:245
int BM_set(struct BM *, int, int, int)
Sets bitmap value to &#39;val&#39; at location &#39;x&#39; &#39;y&#39;.
Definition: bitmap.c:187
struct BM * BM_file_read(FILE *)
Create map structure and load it from file.
Definition: bitmap.c:313
Definition: bitmap.h:17
int rows
Definition: bitmap.h:19
#define NULL
Definition: ccmath.h:32
#define x
int BM_destroy(struct BM *)
Destroy bitmap and free all associated memory.
Definition: bitmap.c:90
int main(int argc, char *argv[])
Definition: smain.c:9
int BM_file_write(FILE *, struct BM *)
Write bitmap out to file.
Definition: bitmap.c:269
int BM_get(struct BM *, int, int)
Gets &#39;val&#39; from the bitmap.
Definition: bitmap.c:220
int cols
Definition: bitmap.h:20
struct BM * BM_create_sparse(int, int)
Create a sparse bitmap of dimension &#39;x&#39;/&#39;y&#39;.
Definition: sparse.c:44
char * getenv()