123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- #include <limits.h>
- #include <float.h>
- #include <math.h>
- #include <grass/gis.h>
- #include <grass/raster.h>
- #include "tinf.h"
- /* To add a new multitype function, use the function below to initialize
- * the function pointer to each of the three typed functions. The function
- * pointers and the function prototypes are defined in a header file.
- * The actual functions follow. */
- int (*is_null) (void *);
- int (*bpe) ();
- void *(*get_max) (void *, void *);
- void *(*get_min) (void *, void *);
- void (*get_row) (int, void *, int);
- void *(*get_buf) ();
- void (*put_row) (int, void *);
- double (*slope) (void *, void *, double);
- void (*set_min) (void *);
- void (*set_max) (void *);
- void (*diff) (void *, void *);
- void (*sum) (void *, void *);
- void (*quot) (void *, void *);
- void (*prod) (void *, void *);
- void set_func_pointers(int in_type)
- {
- switch (in_type) {
- case CELL_TYPE:
- is_null = is_null_c;
- bpe = bpe_c;
- get_max = get_max_c;
- get_min = get_min_c;
- get_row = get_row_c;
- get_buf = get_buf_c;
- put_row = put_row_c;
- slope = slope_c;
- set_min = set_min_c;
- set_max = set_max_c;
- diff = diff_c;
- sum = sum_c;
- quot = quot_c;
- prod = prod_c;
- break;
- case FCELL_TYPE:
- is_null = is_null_f;
- bpe = bpe_f;
- get_max = get_max_f;
- get_min = get_min_f;
- get_row = get_row_f;
- get_buf = get_buf_f;
- put_row = put_row_f;
- slope = slope_f;
- set_min = set_min_f;
- set_max = set_max_f;
- diff = diff_f;
- sum = sum_f;
- quot = quot_f;
- prod = prod_f;
- break;
- case DCELL_TYPE:
- is_null = is_null_d;
- bpe = bpe_d;
- get_max = get_max_d;
- get_min = get_min_d;
- get_row = get_row_d;
- get_buf = get_buf_d;
- put_row = put_row_d;
- slope = slope_d;
- set_min = set_min_d;
- set_max = set_max_d;
- diff = diff_d;
- sum = sum_d;
- quot = quot_d;
- prod = prod_d;
- }
- return;
- }
- /* check for null values */
- int is_null_c(void *value)
- {
- return Rast_is_c_null_value((CELL *) value);
- }
- int is_null_f(void *value)
- {
- return Rast_is_f_null_value((FCELL *) value);
- }
- int is_null_d(void *value)
- {
- return Rast_is_d_null_value((DCELL *) value);
- }
- /* return the size of the current type */
- int bpe_c()
- {
- return sizeof(CELL);
- }
- int bpe_f()
- {
- return sizeof(FCELL);
- }
- int bpe_d()
- {
- return sizeof(DCELL);
- }
- /* return the pointer that points to the smaller of two value */
- void *get_min_c(void *v1, void *v2)
- {
- void *rc;
- rc = v2;
- if (*(CELL *) v1 < *(CELL *) v2)
- rc = v1;
- return rc;
- }
- void *get_min_f(void *v1, void *v2)
- {
- void *rc;
- rc = v2;
- if (*(FCELL *) v1 < *(FCELL *) v2)
- rc = v1;
- return rc;
- }
- void *get_min_d(void *v1, void *v2)
- {
- void *rc;
- rc = v2;
- if (*(DCELL *) v1 < *(DCELL *) v2)
- rc = v1;
- return rc;
- }
- /* return the pointer that points to the larger value */
- void *get_max_c(void *v1, void *v2)
- {
- void *rc;
- rc = v2;
- if (*(CELL *) v1 > *(CELL *) v2)
- rc = v1;
- return rc;
- }
- void *get_max_f(void *v1, void *v2)
- {
- void *rc;
- rc = v2;
- if (*(FCELL *) v1 > *(FCELL *) v2)
- rc = v1;
- return rc;
- }
- void *get_max_d(void *v1, void *v2)
- {
- void *rc;
- rc = v2;
- if (*(DCELL *) v1 > *(DCELL *) v2)
- rc = v1;
- return rc;
- }
- /* Read one line from a raster map */
- void get_row_c(int fd, void *row, int n)
- {
- Rast_get_c_row(fd, (CELL *) row, n);
- }
- void get_row_f(int fd, void *row, int n)
- {
- Rast_get_f_row(fd, (FCELL *) row, n);
- }
- void get_row_d(int fd, void *row, int n)
- {
- Rast_get_d_row(fd, (DCELL *) row, n);
- }
- /* Write one row to a raster map */
- void put_row_c(int fd, void *row)
- {
- Rast_put_c_row(fd, (CELL *) row);
- }
- void put_row_f(int fd, void *row)
- {
- Rast_put_f_row(fd, (FCELL *) row);
- }
- void put_row_d(int fd, void *row)
- {
- Rast_put_d_row(fd, (DCELL *) row);
- }
- /* Allocate memory for one line of data */
- void *get_buf_c()
- {
- return (void *)Rast_allocate_c_buf();
- }
- void *get_buf_f()
- {
- return (void *)Rast_allocate_f_buf();
- }
- void *get_buf_d()
- {
- return (void *)Rast_allocate_d_buf();
- }
- /* initialize memory to a minimum value */
- void set_min_c(void *v)
- {
- *(CELL *) v = INT_MIN;
- }
- void set_min_f(void *v)
- {
- *(FCELL *) v = FLT_MIN;
- }
- void set_min_d(void *v)
- {
- *(DCELL *) v = DBL_MIN;
- }
- /* initialize memory to a maximum value */
- void set_max_c(void *v)
- {
- *(CELL *) v = INT_MAX;
- }
- void set_max_f(void *v)
- {
- *(FCELL *) v = FLT_MAX;
- }
- void set_max_d(void *v)
- {
- *(DCELL *) v = DBL_MAX;
- }
- /* get the difference between two values, returned in the first pointer */
- void diff_c(void *v1, void *v2)
- {
- *(CELL *) v1 -= *(CELL *) v2;
- }
- void diff_f(void *v1, void *v2)
- {
- *(FCELL *) v1 -= *(FCELL *) v2;
- }
- void diff_d(void *v1, void *v2)
- {
- *(DCELL *) v1 -= *(DCELL *) v2;
- }
- /* get the sum of two values, returned in the first pointer */
- void sum_c(void *v1, void *v2)
- {
- *(CELL *) v1 += *(CELL *) v2;
- }
- void sum_f(void *v1, void *v2)
- {
- *(FCELL *) v1 += *(FCELL *) v2;
- }
- void sum_d(void *v1, void *v2)
- {
- *(DCELL *) v1 += *(DCELL *) v2;
- }
- /* get the quotient of two values, returned in the first pointer */
- void quot_c(void *v1, void *v2)
- {
- *(CELL *) v1 /= *(CELL *) v2;
- }
- void quot_f(void *v1, void *v2)
- {
- *(FCELL *) v1 /= *(FCELL *) v2;
- }
- void quot_d(void *v1, void *v2)
- {
- *(DCELL *) v1 /= *(DCELL *) v2;
- }
- /* get the product of two values, returned in the first pointer */
- void prod_c(void *v1, void *v2)
- {
- *(CELL *) v1 *= *(CELL *) v2;
- }
- void prod_f(void *v1, void *v2)
- {
- *(FCELL *) v1 *= *(FCELL *) v2;
- }
- void prod_d(void *v1, void *v2)
- {
- *(DCELL *) v1 *= *(DCELL *) v2;
- }
- /* probably not a function of general interest */
- /* calculate the slope between two cells, returned as a double */
- double slope_c(void *line1, void *line2, double cnst)
- {
- double rc;
- CELL *pedge;
- rc = -HUGE_VAL;
- pedge = (CELL *) line2;
- if (!Rast_is_c_null_value(pedge)) {
- rc = (*(CELL *) line1 - *pedge) / cnst;
- }
- return rc;
- }
- double slope_f(void *line1, void *line2, double cnst)
- {
- double rc;
- FCELL *pedge;
- rc = -HUGE_VAL;
- pedge = (FCELL *) line2;
- if (!Rast_is_f_null_value(pedge)) {
- rc = (*(FCELL *) line1 - *pedge) / cnst;
- }
- return rc;
- }
- double slope_d(void *line1, void *line2, double cnst)
- {
- double rc;
- DCELL *pedge;
- rc = -HUGE_VAL;
- pedge = (DCELL *) line2;
- if (!Rast_is_d_null_value(pedge)) {
- rc = (*(DCELL *) line1 - *pedge) / cnst;
- }
- return rc;
- }
- /* read a line and update a three-line buffer */
- /* moving forward through a file */
- int advance_band3(int fh, struct band3 *bnd)
- {
- int rc;
- void *hold;
- hold = bnd->b[0];
- bnd->b[0] = bnd->b[1];
- bnd->b[1] = bnd->b[2];
- bnd->b[2] = hold;
- if (fh == 0)
- rc = 0;
- else
- rc = read(fh, bnd->b[2], bnd->sz);
- return rc;
- }
- /* read a line and update a three-line buffer */
- /* moving backward through a file */
- int retreat_band3(int fh, struct band3 *bnd)
- {
- int rc;
- void *hold;
- hold = bnd->b[2];
- bnd->b[2] = bnd->b[1];
- bnd->b[1] = bnd->b[0];
- bnd->b[0] = hold;
- if (fh == 0)
- rc = 0;
- else {
- rc = read(fh, bnd->b[0], bnd->sz);
- lseek(fh, (off_t) - 2 * bnd->sz, SEEK_CUR);
- }
- return rc;
- }
|