mask.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <grass/gis.h>
  2. #include <grass/raster.h>
  3. #include "mask.h"
  4. #include "local_proto.h"
  5. int init_d_mask_rules(d_Mask * d_mask)
  6. {
  7. d_mask->list = NULL;
  8. return 0;
  9. }
  10. int add_d_mask_rule(d_Mask * d_mask, double a, double b, int inf)
  11. {
  12. d_Interval *I;
  13. I = (d_Interval *) G_malloc(sizeof(d_Interval));
  14. I->low = a <= b ? a : b;
  15. I->high = a >= b ? a : b;
  16. I->inf = inf;
  17. I->next = d_mask->list;
  18. d_mask->list = I;
  19. return 0;
  20. }
  21. int mask_raster_array(void *rast, int ncols,
  22. int change_null, RASTER_MAP_TYPE data_type)
  23. {
  24. DCELL x;
  25. while (ncols-- > 0) {
  26. x = Rast_get_d_value(rast, data_type);
  27. if (change_null && Rast_is_null_value(rast, data_type))
  28. Rast_set_d_value(rast, new_null, data_type);
  29. if (mask_d_select(&x, &d_mask))
  30. Rast_set_null_value(rast, 1, data_type);
  31. rast = G_incr_void_ptr(rast, Rast_cell_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)
  39. return 0;
  40. for (I = mask->list; I; I = I->next) {
  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 (Rast_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. }