bilinear_f.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Name
  3. * bilinear_f.c -- use bilinear interpolation with fallback for given row, col
  4. *
  5. * Description
  6. * bilinear interpolation for the given row, column indices.
  7. * If the interpolated value (but not the nearest) is null,
  8. * it falls back to nearest neighbor.
  9. */
  10. #include <grass/gis.h>
  11. #include <grass/raster.h>
  12. #include <math.h>
  13. #include "global.h"
  14. void p_bilinear_f(struct cache *ibuffer, /* input buffer */
  15. void *obufptr, /* ptr in output buffer */
  16. int cell_type, /* raster map type of obufptr */
  17. double *row_idx, /* row index */
  18. double *col_idx, /* column index */
  19. struct Cell_head *cellhd /* cell header of input layer */
  20. )
  21. {
  22. /* start nearest neighbor to do some basic tests */
  23. int row, col; /* row/col of nearest neighbor */
  24. DCELL *cellp, cell;
  25. /* cut indices to integer */
  26. row = (int)floor(*row_idx);
  27. col = (int)floor(*col_idx);
  28. /* check for out of bounds - if out of bounds set NULL value */
  29. if (row < 0 || row >= cellhd->rows || col < 0 || col >= cellhd->cols) {
  30. Rast_set_null_value(obufptr, 1, cell_type);
  31. return;
  32. }
  33. cellp = CPTR(ibuffer, row, col);
  34. /* if nearest is null, all the other interps will be null */
  35. if (Rast_is_d_null_value(cellp)) {
  36. Rast_set_null_value(obufptr, 1, cell_type);
  37. return;
  38. }
  39. cell = *cellp;
  40. p_bilinear(ibuffer, obufptr, cell_type, row_idx, col_idx, cellhd);
  41. /* fallback to nearest if bilinear is null */
  42. if (Rast_is_d_null_value(obufptr))
  43. Rast_set_d_value(obufptr, cell, cell_type);
  44. }