auto_mask.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * \file auto_mask.c
  3. *
  4. * \brief GIS Library - Auto masking routines.
  5. *
  6. * (C) 2001-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author GRASS GIS Development Team
  12. *
  13. * \date 1999-2008
  14. */
  15. #include <stdlib.h>
  16. #include <grass/gis.h>
  17. #include <grass/glocale.h>
  18. #include "G.h"
  19. /**
  20. * \brief Checks for auto masking.
  21. *
  22. * On first call, opens the mask file if declared and available and
  23. * allocates buffer for reading mask rows.
  24. * On second call, returns 0 or 1.
  25. *
  26. * \return 0 if mask unset or unavailable
  27. * \return 1 if mask set and available and ready to use
  28. */
  29. int G__check_for_auto_masking(void)
  30. {
  31. struct Cell_head cellhd;
  32. /* if mask is switched off (-2) return -2
  33. if G__.auto_mask is not set (-1) or set (>=0) recheck the MASK */
  34. if (G__.auto_mask < -1)
  35. return G__.auto_mask;
  36. /* if(G__.mask_fd > 0) G_free (G__.mask_buf); */
  37. /* look for the existence of the MASK file */
  38. G__.auto_mask = (G_find_cell("MASK", G_mapset()) != 0);
  39. if (G__.auto_mask <= 0)
  40. return 0;
  41. /* check MASK projection/zone against current region */
  42. if (G_get_cellhd("MASK", G_mapset(), &cellhd) >= 0) {
  43. if (cellhd.zone != G_zone() || cellhd.proj != G_projection()) {
  44. G__.auto_mask = 0;
  45. return 0;
  46. }
  47. }
  48. G_unopen_cell(G__.mask_fd);
  49. G__.mask_fd = G__open_cell_old("MASK", G_mapset());
  50. if (G__.mask_fd < 0) {
  51. G__.auto_mask = 0;
  52. G_warning(_("Unable to open automatic MASK file"));
  53. return 0;
  54. }
  55. /* G__.mask_buf = G_allocate_cell_buf(); */
  56. G__.auto_mask = 1;
  57. return 1;
  58. }
  59. /**
  60. * \brief Suppresses masking.
  61. *
  62. * \return
  63. */
  64. void G_suppress_masking(void)
  65. {
  66. if (G__.auto_mask > 0) {
  67. G_close_cell(G__.mask_fd);
  68. /* G_free (G__.mask_buf); */
  69. G__.mask_fd = -1;
  70. }
  71. G__.auto_mask = -2;
  72. }
  73. /**
  74. * \brief Unsuppresses masking.
  75. *
  76. * \return
  77. */
  78. void G_unsuppress_masking(void)
  79. {
  80. if (G__.auto_mask < -1) {
  81. G__.mask_fd = -1;
  82. G__check_for_auto_masking();
  83. }
  84. }