auto_mask.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * \file auto_mask.c
  3. *
  4. * \brief Raster 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/raster.h>
  18. #include <grass/glocale.h>
  19. #include "R.h"
  20. /**
  21. * \brief Checks for auto masking.
  22. *
  23. * On first call, opens the mask file if declared and available and
  24. * allocates buffer for reading mask rows.
  25. * On second call, returns 0 or 1.
  26. *
  27. * \return 0 if mask unset or unavailable
  28. * \return 1 if mask set and available and ready to use
  29. */
  30. int Rast__check_for_auto_masking(void)
  31. {
  32. struct Cell_head cellhd;
  33. Rast__init();
  34. /* if mask is switched off (-2) return -2
  35. if R__.auto_mask is not set (-1) or set (>=0) recheck the MASK */
  36. if (R__.auto_mask < -1)
  37. return R__.auto_mask;
  38. /* if(R__.mask_fd > 0) G_free (R__.mask_buf); */
  39. /* look for the existence of the MASK file */
  40. R__.auto_mask = (G_find_raster("MASK", G_mapset()) != 0);
  41. if (R__.auto_mask <= 0)
  42. return 0;
  43. /* check MASK projection/zone against current region */
  44. Rast_get_cellhd("MASK", G_mapset(), &cellhd);
  45. if (cellhd.zone != G_zone() || cellhd.proj != G_projection()) {
  46. R__.auto_mask = 0;
  47. return 0;
  48. }
  49. if (R__.mask_fd >= 0)
  50. Rast_unopen(R__.mask_fd);
  51. R__.mask_fd = Rast__open_old("MASK", G_mapset());
  52. if (R__.mask_fd < 0) {
  53. R__.auto_mask = 0;
  54. G_warning(_("Unable to open automatic MASK file"));
  55. return 0;
  56. }
  57. /* R__.mask_buf = Rast_allocate_c_buf(); */
  58. R__.auto_mask = 1;
  59. return 1;
  60. }
  61. /**
  62. * \brief Suppresses masking.
  63. *
  64. * \return
  65. */
  66. void Rast_suppress_masking(void)
  67. {
  68. Rast__init();
  69. if (R__.auto_mask > 0) {
  70. Rast_close(R__.mask_fd);
  71. /* G_free (R__.mask_buf); */
  72. R__.mask_fd = -1;
  73. }
  74. R__.auto_mask = -2;
  75. }
  76. /**
  77. * \brief Unsuppresses masking.
  78. *
  79. * \return
  80. */
  81. void Rast_unsuppress_masking(void)
  82. {
  83. Rast__init();
  84. if (R__.auto_mask < -1) {
  85. R__.mask_fd = -1;
  86. Rast__check_for_auto_masking();
  87. }
  88. }