cubic_f.c 1.8 KB

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