bilinear_f.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 "r.proj.h"
  13. void p_bilinear_f(struct cache *ibuffer, /* input buffer */
  14. void *obufptr, /* ptr in output buffer */
  15. int cell_type, /* raster map type of obufptr */
  16. double col_idx, /* column index */
  17. double row_idx, /* row index */
  18. struct Cell_head *cellhd /* cell header of input layer */
  19. )
  20. {
  21. /* start nearest neighbor to do some basic tests */
  22. int row, col; /* row/col of nearest neighbor */
  23. FCELL cell;
  24. /* cut indices to integer */
  25. row = (int)floor(row_idx);
  26. col = (int)floor(col_idx);
  27. /* check for out of bounds - if out of bounds set NULL value */
  28. if (row < 0 || row >= cellhd->rows || col < 0 || col >= cellhd->cols) {
  29. Rast_set_null_value(obufptr, 1, cell_type);
  30. return;
  31. }
  32. cell = CVAL(ibuffer, row, col);
  33. /* if nearest is null, all the other interps will be null */
  34. if (Rast_is_f_null_value(&cell)) {
  35. Rast_set_null_value(obufptr, 1, cell_type);
  36. return;
  37. }
  38. p_bilinear(ibuffer, obufptr, cell_type, col_idx, row_idx, cellhd);
  39. /* fallback to nearest if bilinear is null */
  40. if (Rast_is_f_null_value(obufptr))
  41. Rast_set_f_value(obufptr, cell, cell_type);
  42. }