bilinear.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Name
  3. * bilinear.c -- use bilinear interpolation for given row, col
  4. *
  5. * Description
  6. * bilinear interpolation for the given row, column indices.
  7. * If the given row or column is outside the bounds of the input map,
  8. * the point in the output map is set to NULL.
  9. * If any of the 4 surrounding points to be used in the interpolation
  10. * is NULL it is filled with is neighbor value
  11. */
  12. #include <math.h>
  13. #include <grass/gis.h>
  14. #include <grass/raster.h>
  15. #include "r.proj.h"
  16. void p_bilinear(struct cache *ibuffer, /* input buffer */
  17. void *obufptr, /* ptr in output buffer */
  18. int cell_type, /* raster map type of obufptr */
  19. double col_idx, /* column index */
  20. double row_idx, /* row index */
  21. struct Cell_head *cellhd /* information of output map */
  22. )
  23. {
  24. int row; /* row indices for interp */
  25. int col; /* column indices for interp */
  26. int i, j;
  27. FCELL t, u; /* intermediate slope */
  28. FCELL result; /* result of interpolation */
  29. FCELL c[2][2];
  30. /* cut indices to integer */
  31. row = (int)floor(row_idx - 0.5);
  32. col = (int)floor(col_idx - 0.5);
  33. /* check for out of bounds - if out of bounds set NULL value and return */
  34. if (row < 0 || row + 1 >= cellhd->rows || col < 0 || col + 1 >= cellhd->cols) {
  35. Rast_set_null_value(obufptr, 1, cell_type);
  36. return;
  37. }
  38. for (i = 0; i < 2; i++)
  39. for (j = 0; j < 2; j++) {
  40. const FCELL cell = CVAL(ibuffer, row + i, col + j);
  41. if (Rast_is_f_null_value(&cell)) {
  42. Rast_set_null_value(obufptr, 1, cell_type);
  43. return;
  44. }
  45. c[i][j] = cell;
  46. }
  47. /* do the interpolation */
  48. t = col_idx - 0.5 - col;
  49. u = row_idx - 0.5 - row;
  50. result = Rast_interp_bilinear(t, u, c[0][0], c[0][1], c[1][0], c[1][1]);
  51. Rast_set_f_value(obufptr, result, cell_type);
  52. }