9
8
* the point in the output map is set to NULL.
10
9
* If any of the 4 surrounding points to be used in the interpolation
11
10
* is NULL it is filled with is neighbor value
13
* See: Press, W.H. et al. (1992), Numerical recipes in C.
17
14
#include <grass/gis.h>
18
#include "local_proto.h"
15
#include <grass/raster.h>
20
void p_bilinear(FCELL ** ibuffer, /* input buffer */
21
void *obufptr, /* ptr in output buffer */
22
int cell_type, /* raster map type of obufptr */
23
double *col_idx, /* column index */
24
double *row_idx, /* row index */
25
struct Cell_head *cellhd /* information of output map */
18
void p_bilinear(struct cache *ibuffer, /* input buffer */
19
void *obufptr, /* ptr in output buffer */
20
int cell_type, /* raster map type of obufptr */
21
double col_idx, /* column index */
22
double row_idx, /* row index */
23
struct Cell_head *cellhd /* information of output map */
28
int row1, /* lower row index for interp */
29
row2, /* upper row index for interp */
30
col1, /* lower column index for interp */
31
col2, /* upper column index for interp */
32
row, col, /* Coordinates of pixel center */
33
x1, x2, y1, y2; /* temp. buffer indices */
34
FCELL t, u, /* intermediate slope */
36
result; /* result of interpolation */
26
int row; /* row indices for interp */
27
int col; /* column indices for interp */
29
FCELL t, u; /* intermediate slope */
30
FCELL result; /* result of interpolation */
40
33
/* cut indices to integer */
41
row = (int)floor(*row_idx);
42
col = (int)floor(*col_idx);
34
row = (int)floor(row_idx - 0.5);
35
col = (int)floor(col_idx - 0.5);
44
37
/* check for out of bounds - if out of bounds set NULL value and return */
45
/* check for NULL value */
46
if (row < 0 || row >= cellhd->rows ||
47
col < 0 || col >= cellhd->cols ||
48
G_is_f_null_value(&ibuffer[row][col])) {
49
G_set_null_value(obufptr, 1, cell_type);
38
if (row < 0 || row + 1 >= cellhd->rows || col < 0 || col + 1 >= cellhd->cols) {
39
Rast_set_null_value(obufptr, 1, cell_type);
53
/* get the four surrounding pixel positions */
54
row1 = (*row_idx < row) ? row - 1 : row;
55
col1 = (*col_idx < col) ? col - 1 : col;
60
x1 = (col1 < 0) ? col2 : col1;
61
x2 = (col2 >= cellhd->cols) ? col1 : col2;
62
y1 = (row1 < 0) ? row2 : row1;
63
y2 = (row2 >= cellhd->rows) ? row1 : row2;
65
/* check for NULL value - if a single pixel is NULL, */
66
/* fill it with neighbor value */
67
if (G_is_f_null_value(&ibuffer[y1][x1])) {
72
if (G_is_f_null_value(&ibuffer[y1][x2])) {
77
if (G_is_f_null_value(&ibuffer[y2][x1])) {
82
if (G_is_f_null_value(&ibuffer[y2][x2])) {
87
/* do the interpolation */
93
result = ((1 - t - u + tu) * ibuffer[y1][x1]) +
94
((t - tu) * ibuffer[y1][x2]) +
95
((u - tu) * ibuffer[y2][x1]) + (tu * ibuffer[y2][x2]);
99
G_set_raster_value_c(obufptr, (CELL) result, cell_type);
102
G_set_raster_value_f(obufptr, (FCELL) result, cell_type);
105
G_set_raster_value_d(obufptr, (DCELL) result, cell_type);
43
for (i = 0; i < 2; i++)
44
for (j = 0; j < 2; j++) {
45
const FCELL cell = CVAL(ibuffer, row + i, col + j);
46
if (Rast_is_f_null_value(&cell)) {
47
Rast_set_null_value(obufptr, 1, cell_type);
53
/* do the interpolation */
54
t = col_idx - 0.5 - col;
55
u = row_idx - 0.5 - row;
57
result = Rast_interp_bilinear(t, u, c[0][0], c[0][1], c[1][0], c[1][1]);
59
Rast_set_f_value(obufptr, result, cell_type);