Debian Codegraph @

imagery/i.ortho.photo/i.ortho.rectify/lanczos.c

raw symbols SCIP
1 /*
2 * Name
3 * lanczos.c -- use lanczos interpolation for given row, col
4 *
5 * Description
6 * lanczos returns the value in the buffer that is the result of
7 * lanczos interpolation for the given row, column indices.
8 * If the given row or column is outside the bounds of the input map,
9 * the corresponding point in the output map is set to NULL.
10 *
11 * If single surrounding points in the interpolation matrix are
12 * NULL they where filled with their neighbor
13 */
14
15 #include <grass/gis.h>
16 #include <grass/raster.h>
17 #include <math.h>
18 #include "global.h"
19
20 void p_lanczos(struct cache *ibuffer, /* input buffer */
21 void *obufptr, /* ptr in output buffer */
22 int cell_type, /* raster map type of obufptr */
23 double *row_idx, /* row index (decimal) */
24 double *col_idx, /* column index (decimal) */
25 struct Cell_head *cellhd /* information of output map */
26 )
27 {
28 int row; /* row indices for interp */
29 int col; /* column indices for interp */
30 int i, j, k;
31 DCELL t, u; /* intermediate slope */
32 DCELL result; /* result of interpolation */
33 DCELL cell[25];
34
35 /* cut indices to integer */
36 row = (int)floor(*row_idx);
37 col = (int)floor(*col_idx);
38
39 /* check for out of bounds of map - if out of bounds set NULL value */
40 if (row - 2 < 0 || row + 2 >= cellhd->rows || col - 2 < 0 ||
41 col + 2 >= cellhd->cols) {
42 Rast_set_null_value(obufptr, 1, cell_type);
43 return;
44 }
45
46 k = 0;
47 for (i = 0; i < 5; i++) {
48 for (j = 0; j < 5; j++) {
49 const DCELL *cellp = CPTR(ibuffer, row - 2 + i, col - 2 + j);
50
51 if (Rast_is_d_null_value(cellp)) {
52 Rast_set_null_value(obufptr, 1, cell_type);
53 return;
54 }
55 cell[k++] = *cellp;
56 }
57 }
58
59 /* do the interpolation */
60 t = *col_idx - 0.5 - col;
61 u = *row_idx - 0.5 - row;
62
63 result = Rast_interp_lanczos(t, u, cell);
64
65 Rast_set_d_value(obufptr, result, cell_type);
66 }
67
68 void p_lanczos_f(struct cache *ibuffer, /* input buffer */
69 void *obufptr, /* ptr in output buffer */
70 int cell_type, /* raster map type of obufptr */
71 double *row_idx, /* row index (decimal) */
72 double *col_idx, /* column index (decimal) */
73 struct Cell_head *cellhd /* information of output map */
74 )
75 {
76 int row; /* row indices for interp */
77 int col; /* column indices for interp */
78 DCELL *cellp, cell;
79
80 /* cut indices to integer */
81 row = (int)floor(*row_idx);
82 col = (int)floor(*col_idx);
83
84 /* check for out of bounds - if out of bounds set NULL value */
85 if (row < 0 || row >= cellhd->rows || col < 0 || col >= cellhd->cols) {
86 Rast_set_null_value(obufptr, 1, cell_type);
87 return;
88 }
89
90 cellp = CPTR(ibuffer, row, col);
91 /* if nearest is null, all the other interps will be null */
92 if (Rast_is_d_null_value(cellp)) {
93 Rast_set_null_value(obufptr, 1, cell_type);
94 return;
95 }
96 cell = *cellp;
97
98 p_lanczos(ibuffer, obufptr, cell_type, row_idx, col_idx, cellhd);
99 /* fallback to bicubic if lanczos is null */
100 if (Rast_is_d_null_value(obufptr)) {
101 p_cubic(ibuffer, obufptr, cell_type, row_idx, col_idx, cellhd);
102 /* fallback to bilinear if cubic is null */
103 if (Rast_is_d_null_value(obufptr)) {
104 p_bilinear(ibuffer, obufptr, cell_type, row_idx, col_idx, cellhd);
105 /* fallback to nearest if bilinear is null */
106 if (Rast_is_d_null_value(obufptr))
107 Rast_set_d_value(obufptr, cell, cell_type);
108 }
109 }
110 }