xsub.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <grass/gis.h>
  2. #include <grass/raster.h>
  3. #include <grass/calc.h>
  4. /****************************************************************
  5. sub(a,b) = a - b
  6. ****************************************************************/
  7. int f_sub(int argc, const int *argt, void **args)
  8. {
  9. int i;
  10. if (argc < 2)
  11. return E_ARG_LO;
  12. if (argc > 2)
  13. return E_ARG_HI;
  14. if (argt[1] != argt[0] || argt[2] != argt[0])
  15. return E_ARG_TYPE;
  16. switch (argt[0]) {
  17. case CELL_TYPE:
  18. {
  19. CELL *res = args[0];
  20. CELL *arg1 = args[1];
  21. CELL *arg2 = args[2];
  22. for (i = 0; i < columns; i++) {
  23. if (IS_NULL_C(&arg1[i]) || IS_NULL_C(&arg2[i]))
  24. SET_NULL_C(&res[i]);
  25. else
  26. res[i] = arg1[i] - arg2[i];
  27. }
  28. return 0;
  29. }
  30. case FCELL_TYPE:
  31. {
  32. FCELL *res = args[0];
  33. FCELL *arg1 = args[1];
  34. FCELL *arg2 = args[2];
  35. for (i = 0; i < columns; i++) {
  36. if (IS_NULL_F(&arg1[i]) || IS_NULL_F(&arg2[i]))
  37. SET_NULL_F(&res[i]);
  38. else
  39. res[i] = arg1[i] - arg2[i];
  40. }
  41. return 0;
  42. }
  43. case DCELL_TYPE:
  44. {
  45. DCELL *res = args[0];
  46. DCELL *arg1 = args[1];
  47. DCELL *arg2 = args[2];
  48. for (i = 0; i < columns; i++) {
  49. if (IS_NULL_D(&arg1[i]) || IS_NULL_D(&arg2[i]))
  50. SET_NULL_D(&res[i]);
  51. else
  52. res[i] = arg1[i] - arg2[i];
  53. }
  54. return 0;
  55. }
  56. default:
  57. return E_INV_TYPE;
  58. }
  59. }