GRASS Programmer's Manual  6.4.2(2012)
lib/bitmap/main.c
Go to the documentation of this file.
00001 
00002 /****************************************************************************
00003  *
00004  * MODULE:       bitmap
00005  * AUTHOR(S):    David Gerdes (CERL) (original contributor)
00006  *               Markus Neteler <neteler itc.it>, 
00007  *               Bernhard Reiter <bernhard intevation.de>, 
00008  *               Brad Douglas <rez touchofmadness.com>, 
00009  *               Glynn Clements <glynn gclements.plus.com>
00010  * PURPOSE:      provides basic support for the creation and manipulation of
00011  *               two dimensional bitmap arrays
00012  * COPYRIGHT:    (C) 1999-2006 by the GRASS Development Team
00013  *
00014  *               This program is free software under the GNU General Public
00015  *               License (>=v2). Read the file COPYING that comes with GRASS
00016  *               for details.
00017  *
00018  *****************************************************************************/
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <grass/bitmap.h>
00022 
00023 
00024 static int dump_map(struct BM *map);
00025 
00026 
00027 int main(int argc, char *argv[])
00028 {
00029     int SIZE;
00030     struct BM *map, *map2;
00031     int i, x, y;
00032     int dump;
00033     FILE *fp;
00034 
00035     if (argc < 2)
00036         SIZE = 11;
00037     else
00038         SIZE = atoi(argv[1]);
00039 
00040     if (NULL != getenv("NODUMP"))
00041         dump = 0;
00042     else
00043         dump = 1;
00044 
00045     map = BM_create(SIZE, SIZE);
00046 
00047     /* turn on bits in X pattern */
00048     for (i = 0; i < SIZE; i++) {
00049         BM_set(map, i, i, 1);
00050         BM_set(map, (SIZE - 1) - i, i, 1);
00051     }
00052 
00053 
00054     if (dump)
00055         dump_map(map);
00056     fprintf(stdout, "Size = %d\n", BM_get_map_size(map));
00057 
00058     fprintf(stdout, "\n\n");
00059 
00060     /* now invert it */
00061     for (y = 0; y < SIZE; y++)
00062         for (x = 0; x < SIZE; x++)
00063             BM_set(map, x, y, !BM_get(map, x, y));
00064 
00065     if (dump)
00066         dump_map(map);
00067 
00068     fprintf(stdout, "Size = %d\n", BM_get_map_size(map));
00069 
00070     {
00071         fp = fopen("dumpfile", "w");
00072         BM_file_write(fp, map);
00073         fclose(fp);
00074 
00075         fp = fopen("dumpfile", "r");
00076         map2 = BM_file_read(fp);
00077         fclose(fp);
00078         dump_map(map2);
00079     }
00080 
00081     BM_destroy(map);
00082     BM_destroy(map2);
00083 
00084     return 0;
00085 }
00086 
00087 
00088 static int dump_map(struct BM *map)
00089 {
00090     int x, y;
00091 
00092     for (y = 0; y < map->rows; y++) {
00093         for (x = 0; x < map->cols; x++) {
00094             fprintf(stdout, "%c", BM_get(map, x, y) ? '#' : '.');
00095 
00096         }
00097         fprintf(stdout, "\n");
00098     }
00099 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines