mask.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <grass/gis.h>
  2. #include "mask.h"
  3. #include "local_proto.h"
  4. int init_d_mask_rules (d_Mask *d_mask)
  5. {
  6. d_mask->list = NULL;
  7. return 0;
  8. }
  9. int add_d_mask_rule (d_Mask *d_mask, double a, double b, int inf)
  10. {
  11. d_Interval *I;
  12. I = (d_Interval *) G_malloc (sizeof(d_Interval));
  13. I->low = a <= b ? a : b ;
  14. I->high = a >= b ? a : b ;
  15. I->inf = inf;
  16. I->next = d_mask->list;
  17. d_mask->list = I;
  18. return 0;
  19. }
  20. int mask_raster_array (void *rast, int ncols,
  21. int change_null, RASTER_MAP_TYPE data_type)
  22. {
  23. DCELL x;
  24. while (ncols-- > 0)
  25. {
  26. x = G_get_raster_value_d(rast, data_type);
  27. if(change_null && G_is_null_value(rast, data_type))
  28. G_set_raster_value_d(rast, new_null, data_type);
  29. if (mask_d_select (&x, &d_mask))
  30. G_set_null_value(rast, 1, data_type);
  31. rast = G_incr_void_ptr(rast, G_raster_size(data_type));
  32. }
  33. return 0;
  34. }
  35. int mask_d_select (DCELL *x, d_Mask *mask)
  36. {
  37. d_Interval *I;
  38. if (mask->list == NULL) return 0;
  39. for (I = mask->list; I; I = I->next)
  40. {
  41. if (mask_match_d_interval (*x, I))
  42. return 1;
  43. }
  44. return 0;
  45. }
  46. int mask_match_d_interval (DCELL x, d_Interval *I)
  47. {
  48. if (G_is_d_null_value(&x))
  49. return 0;
  50. if (I->inf < 0)
  51. return x <= I->low;
  52. if (I->inf > 0)
  53. return x >= I->high;
  54. if (I->low != I->low && I->high != I->high)
  55. return x != x;
  56. return x >= I->low && x <= I->high;
  57. }