GRASS GIS 7 Programmer's Manual  7.7.svn(2018)-r73807
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
main.c
Go to the documentation of this file.
1 
2 /****************************************************************************
3  *
4  * MODULE: bitmap
5  * AUTHOR(S): David Gerdes (CERL) (original contributor)
6  * Markus Neteler <neteler itc.it>,
7  * Bernhard Reiter <bernhard intevation.de>,
8  * Brad Douglas <rez touchofmadness.com>,
9  * Glynn Clements <glynn gclements.plus.com>
10  * PURPOSE: provides basic support for the creation and manipulation of
11  * two dimensional bitmap arrays
12  * COPYRIGHT: (C) 1999-2006 by the GRASS Development Team
13  *
14  * This program is free software under the GNU General Public
15  * License (>=v2). Read the file COPYING that comes with GRASS
16  * for details.
17  *
18  *****************************************************************************/
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <grass/bitmap.h>
22 
23 
24 static int dump_map(struct BM *map);
25 
26 
27 int main(int argc, char *argv[])
28 {
29  int SIZE;
30  struct BM *map, *map2;
31  int i, x, y;
32  int dump;
33  FILE *fp;
34 
35  if (argc < 2)
36  SIZE = 11;
37  else
38  SIZE = atoi(argv[1]);
39 
40  if (NULL != getenv("NODUMP"))
41  dump = 0;
42  else
43  dump = 1;
44 
45  map = BM_create(SIZE, SIZE);
46 
47  /* turn on bits in X pattern */
48  for (i = 0; i < SIZE; i++) {
49  BM_set(map, i, i, 1);
50  BM_set(map, (SIZE - 1) - i, i, 1);
51  }
52 
53 
54  if (dump)
55  dump_map(map);
56  fprintf(stdout, "Size = %d\n", BM_get_map_size(map));
57 
58  fprintf(stdout, "\n\n");
59 
60  /* now invert it */
61  for (y = 0; y < SIZE; y++)
62  for (x = 0; x < SIZE; x++)
63  BM_set(map, x, y, !BM_get(map, x, y));
64 
65  if (dump)
66  dump_map(map);
67 
68  fprintf(stdout, "Size = %d\n", BM_get_map_size(map));
69 
70  {
71  fp = fopen("dumpfile", "w");
72  BM_file_write(fp, map);
73  fclose(fp);
74 
75  fp = fopen("dumpfile", "r");
76  map2 = BM_file_read(fp);
77  fclose(fp);
78  dump_map(map2);
79  }
80 
81  BM_destroy(map);
82  BM_destroy(map2);
83 
84  return 0;
85 }
86 
87 
88 static int dump_map(struct BM *map)
89 {
90  int x, y;
91 
92  for (y = 0; y < map->rows; y++) {
93  for (x = 0; x < map->cols; x++) {
94  fprintf(stdout, "%c", BM_get(map, x, y) ? '#' : '.');
95 
96  }
97  fprintf(stdout, "\n");
98  }
99 }
int BM_get(struct BM *map, int x, int y)
Gets &#39;val&#39; from the bitmap.
Definition: bitmap.c:220
int main(int argc, char *argv[])
Definition: main.c:27
Definition: bitmap.h:17
int rows
Definition: bitmap.h:19
int BM_file_write(FILE *fp, struct BM *map)
Write bitmap out to file.
Definition: bitmap.c:269
#define NULL
Definition: ccmath.h:32
#define x
int BM_destroy(struct BM *map)
Destroy bitmap and free all associated memory.
Definition: bitmap.c:90
fclose(fd)
struct BM * BM_file_read(FILE *fp)
Create map structure and load it from file.
Definition: bitmap.c:313
struct BM * BM_create(int x, int y)
Create bitmap of dimension x/y and return structure token.
Definition: bitmap.c:60
size_t BM_get_map_size(struct BM *map)
Returns size in bytes that bitmap is taking up.
Definition: bitmap.c:245
int cols
Definition: bitmap.h:20
int BM_set(struct BM *map, int x, int y, int val)
Sets bitmap value to &#39;val&#39; at location &#39;x&#39; &#39;y&#39;.
Definition: bitmap.c:187
char * getenv()