1 
2 /**
3  * \file auto_mask.c
4  *
5  * \brief Raster Library - Auto masking routines.
6  *
7  * (C) 2001-2008 by the GRASS Development Team
8  *
9  * This program is free software under the GNU General Public License
10  * (>=v2). Read the file COPYING that comes with GRASS for details.
11  *
12  * \author GRASS GIS Development Team
13  *
14  * \date 1999-2008
15  */
16 
17 #include <stdlib.h>
18 
19 #include <grass/gis.h>
20 #include <grass/raster.h>
21 #include <grass/glocale.h>
22 
23 #include "R.h"
24 
25 
26 /**
27  * \brief Checks for auto masking.
28  *
29  * On first call, opens the mask file if declared and available and
30  * allocates buffer for reading mask rows.
31  * On second call, returns 0 or 1.
32  *
33  * \return 0 if mask unset or unavailable
34  * \return 1 if mask set and available and ready to use
35  */
36 
Rast__check_for_auto_masking(void)37 int Rast__check_for_auto_masking(void)
38 {
39     struct Cell_head cellhd;
40 
41     Rast__init();
42 
43     /* if mask is switched off (-2) return -2
44        if R__.auto_mask is not set (-1) or set (>=0) recheck the MASK */
45 
46     if (R__.auto_mask < -1)
47 	return R__.auto_mask;
48 
49     /* if(R__.mask_fd > 0) G_free (R__.mask_buf); */
50 
51     /* look for the existence of the MASK file */
52     R__.auto_mask = (G_find_raster("MASK", G_mapset()) != 0);
53 
54     if (R__.auto_mask <= 0)
55 	return 0;
56 
57     /* check MASK projection/zone against current region */
58     Rast_get_cellhd("MASK", G_mapset(), &cellhd);
59     if (cellhd.zone != G_zone() || cellhd.proj != G_projection()) {
60 	R__.auto_mask = 0;
61 	return 0;
62     }
63 
64     if (R__.mask_fd >= 0)
65 	Rast_unopen(R__.mask_fd);
66     R__.mask_fd = Rast__open_old("MASK", G_mapset());
67     if (R__.mask_fd < 0) {
68 	R__.auto_mask = 0;
69 	G_warning(_("Unable to open automatic MASK file"));
70 	return 0;
71     }
72 
73     /*    R__.mask_buf = Rast_allocate_c_buf(); */
74 
75     R__.auto_mask = 1;
76 
77     return 1;
78 }
79 
80 
81 /**
82  * \brief Suppresses masking.
83  *
84  * \return
85  */
86 
Rast_suppress_masking(void)87 void Rast_suppress_masking(void)
88 {
89     Rast__init();
90 
91     if (R__.auto_mask > 0) {
92 	Rast_close(R__.mask_fd);
93 	/* G_free (R__.mask_buf); */
94 	R__.mask_fd = -1;
95     }
96     R__.auto_mask = -2;
97 }
98 
99 
100 /**
101  * \brief Unsuppresses masking.
102  *
103  * \return
104  */
105 
Rast_unsuppress_masking(void)106 void Rast_unsuppress_masking(void)
107 {
108     Rast__init();
109 
110     if (R__.auto_mask < -1) {
111 	R__.mask_fd = -1;
112 	Rast__check_for_auto_masking();
113     }
114 }
115