~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to raster/r.proj/bilinear.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
1
/*
3
2
 * Name
4
3
 *  bilinear.c -- use bilinear interpolation for given row, col
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
12
 
 *
13
 
 *  See: Press, W.H. et al. (1992), Numerical recipes in C.
14
11
 */
15
12
 
16
13
#include <math.h>
17
14
#include <grass/gis.h>
18
 
#include "local_proto.h"
 
15
#include <grass/raster.h>
 
16
#include "r.proj.h"
19
17
 
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     */
26
24
    )
27
25
{
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           */
35
 
      tu,                       /* t * u                        */
36
 
      result;                   /* result of interpolation      */
37
 
 
38
 
 
 
26
    int row;                    /* row indices for interp        */
 
27
    int col;                    /* column indices for interp     */
 
28
    int i, j;
 
29
    FCELL t, u;                 /* intermediate slope            */
 
30
    FCELL result;               /* result of interpolation       */
 
31
    FCELL c[2][2];
39
32
 
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);
43
36
 
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);
50
40
        return;
51
41
    }
52
42
 
53
 
    /* get the four surrounding pixel positions     */
54
 
    row1 = (*row_idx < row) ? row - 1 : row;
55
 
    col1 = (*col_idx < col) ? col - 1 : col;
56
 
 
57
 
    row2 = row1 + 1;
58
 
    col2 = col1 + 1;
59
 
 
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;
64
 
 
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])) {
68
 
        y1 = row;
69
 
        x1 = col;
70
 
    }
71
 
 
72
 
    if (G_is_f_null_value(&ibuffer[y1][x2])) {
73
 
        y1 = row;
74
 
        x2 = col;
75
 
    }
76
 
 
77
 
    if (G_is_f_null_value(&ibuffer[y2][x1])) {
78
 
        y2 = row;
79
 
        x1 = col;
80
 
    }
81
 
 
82
 
    if (G_is_f_null_value(&ibuffer[y2][x2])) {
83
 
        y2 = row;
84
 
        x2 = col;
85
 
    }
86
 
 
87
 
    /* do the interpolation         */
88
 
    t = *col_idx - col1;
89
 
    u = *row_idx - row1;
90
 
    tu = t * u;
91
 
 
92
 
 
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]);
96
 
 
97
 
    switch (cell_type) {
98
 
    case CELL_TYPE:
99
 
        G_set_raster_value_c(obufptr, (CELL) result, cell_type);
100
 
        break;
101
 
    case FCELL_TYPE:
102
 
        G_set_raster_value_f(obufptr, (FCELL) result, cell_type);
103
 
        break;
104
 
    case DCELL_TYPE:
105
 
        G_set_raster_value_d(obufptr, (DCELL) result, cell_type);
106
 
        break;
107
 
    }
108
 
    return;
 
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);
 
48
                return;
 
49
            }
 
50
            c[i][j] = cell;
 
51
        }
 
52
 
 
53
    /* do the interpolation  */
 
54
    t = col_idx - 0.5 - col;
 
55
    u = row_idx - 0.5 - row;
 
56
 
 
57
    result = Rast_interp_bilinear(t, u, c[0][0], c[0][1], c[1][0], c[1][1]);
 
58
 
 
59
    Rast_set_f_value(obufptr, result, cell_type);
109
60
}