xsub.c 1.4 KB

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