123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- /*!
- * \file lib/raster/raster.c
- *
- * \brief Raster Library - Raster cell value routines.
- *
- * (C) 2001-2009 by the GRASS Development Team
- *
- * This program is free software under the GNU General Public License
- * (>=v2). Read the file COPYING that comes with GRASS for details.
- *
- * \author Original author CERL
- */
- #include <stdlib.h>
- #include <string.h>
- #include <grass/gis.h>
- #include <grass/raster.h>
- /*!
- * \brief Compares raster values.
- *
- * \param v1,v2 values to be compared
- * \param data_type raster type (CELL, FCELL, DCELL)
- *
- * \return 1 if p > q or only q is null value
- * \return -1 if p < q or only p is null value
- * \return 0 if p == q or p==q==null value
- */
- int Rast_raster_cmp(const void *v1, const void *v2, RASTER_MAP_TYPE data_type)
- {
- if (Rast_is_null_value(v1, data_type)) {
- if (Rast_is_null_value(v2, data_type))
- return 0;
- else
- return -1;
- }
- else if (Rast_is_null_value(v2, data_type))
- return 1;
- switch (data_type) {
- case CELL_TYPE:
- if (*((const CELL *)v1) > *((const CELL *)v2))
- return 1;
- else if (*((const CELL *)v1) == *((const CELL *)v2))
- return 0;
- else
- return -1;
- case FCELL_TYPE:
- if (*((const FCELL *)v1) > *((const FCELL *)v2))
- return 1;
- else if (*((const FCELL *)v1) == *((const FCELL *)v2))
- return 0;
- else
- return -1;
- case DCELL_TYPE:
- if (*((const DCELL *)v1) > *((const DCELL *)v2))
- return 1;
- else if (*((const DCELL *)v1) == *((const DCELL *)v2))
- return 0;
- else
- return -1;
- }
- return 0;
- }
- /*!
- * \brief Copies raster values.
- *
- * If \p v2 is null value, sets \p v2 to null value.
- * \p n is typically size of the destination array
- * and the source array is at least that large.
- *
- * \param v1 destination array for raster values
- * \param v2 source array with raster values
- * \param n number of values to copy
- * \param data_type raster type (CELL, FCELL, DCELL)
- */
- void Rast_raster_cpy(void *v1, const void *v2, int n,
- RASTER_MAP_TYPE data_type)
- {
- memcpy(v1, v2, n * Rast_cell_size(data_type));
- }
- /*!
- * \brief Places a CELL raster value
- *
- * If Rast_is_c_null_value() is true, sets p to null value. Converts CELL
- * val to data_type (type of p) and stores result in p. Used for
- * assigning CELL values to raster cells of any type.
- *
- * \param rast pointer to raster cell value
- * \param cval value to set
- * \param data_type raster type (CELL, FCELL, DCELL)
- */
- void Rast_set_c_value(void *rast, CELL cval, RASTER_MAP_TYPE data_type)
- {
- CELL c;
- c = cval;
- if (Rast_is_c_null_value(&c)) {
- Rast_set_null_value(rast, 1, data_type);
- return;
- }
- switch (data_type) {
- case CELL_TYPE:
- *((CELL *) rast) = cval;
- break;
- case FCELL_TYPE:
- *((FCELL *) rast) = (FCELL) cval;
- break;
- case DCELL_TYPE:
- *((DCELL *) rast) = (DCELL) cval;
- break;
- }
- }
- /*!
- * \brief Places a FCELL raster value
- *
- * If Rast_is_f_null_value() is true, sets p to null value. Converts
- * FCELL val to data_type (type of p) and stores result in p. Used for
- * assigning FCELL values to raster cells of any type.
- *
- * \param rast pointer to raster cell value
- * \param fval value to set
- * \param data_type raster type (CELL, FCELL, DCELL)
- */
- void Rast_set_f_value(void *rast, FCELL fval, RASTER_MAP_TYPE data_type)
- {
- FCELL f;
- f = fval;
- if (Rast_is_f_null_value(&f)) {
- Rast_set_null_value(rast, 1, data_type);
- return;
- }
- switch (data_type) {
- case CELL_TYPE:
- *((CELL *) rast) = (CELL) fval;
- break;
- case FCELL_TYPE:
- *((FCELL *) rast) = fval;
- break;
- case DCELL_TYPE:
- *((DCELL *) rast) = (DCELL) fval;
- break;
- }
- }
- /*!
- * \brief Places a DCELL raster value
- *
- * If Rast_is_d_null_value() is true, sets p to null value. Converts
- * DCELL val to data_type (type of p) and stores result in p. Used for
- * assigning DCELL values to raster cells of any type.
- *
- * \param rast pointer to raster cell value
- * \param fval value to set
- * \param data_type raster type (CELL, FCELL, DCELL)
- */
- void Rast_set_d_value(void *rast, DCELL dval, RASTER_MAP_TYPE data_type)
- {
- DCELL d;
- d = dval;
- if (Rast_is_d_null_value(&d)) {
- Rast_set_null_value(rast, 1, data_type);
- return;
- }
- switch (data_type) {
- case CELL_TYPE:
- *((CELL *) rast) = (CELL) dval;
- break;
- case FCELL_TYPE:
- *((FCELL *) rast) = (FCELL) dval;
- break;
- case DCELL_TYPE:
- *((DCELL *) rast) = dval;
- break;
- }
- }
- /*!
- * \brief Retrieves the value of give type from pointer p
- *
- * Retrieves the value of type data_type from pointer p, converts it
- * to CELL type and returns the result. If null value is stored in p,
- * returns CELL null value.
- *
- * Used for retrieving CELL values from raster cells of any type.
- *
- * Note: when data_type != CELL_TYPE, no quantization is used, only
- * type conversion.
- *
- * \param rast pointer to raster cell value
- * \param data_type raster type (CELL, FCELL, DCELL)
- *
- * \return raster value
- */
- CELL Rast_get_c_value(const void *rast, RASTER_MAP_TYPE data_type)
- {
- CELL c;
- if (Rast_is_null_value(rast, data_type)) {
- Rast_set_c_null_value(&c, 1);
- return c;
- }
- switch (data_type) {
- case CELL_TYPE:
- return *((const CELL *)rast);
- case FCELL_TYPE:
- return (CELL) * ((const FCELL *)rast);
- case DCELL_TYPE:
- return (CELL) * ((const DCELL *)rast);
- }
- return 0;
- }
- /*!
- * \brief Retrieves the value of given raster type from pointer p (FCELL)
- *
- * Retrieves the value of type data_type from pointer p, converts it
- * to FCELL type and returns the result. If null value is stored in p,
- * returns FCELL null value.
- *
- * Used for retrieving FCELL values from raster cells of any type.
- *
- * \param rast pointer to raster cell value
- * \param data_type raster type (CELL, FCELL, DCELL)
- *
- * \return raster value
- */
- FCELL Rast_get_f_value(const void *rast, RASTER_MAP_TYPE data_type)
- {
- FCELL f;
- if (Rast_is_null_value(rast, data_type)) {
- Rast_set_f_null_value(&f, 1);
- return f;
- }
- switch (data_type) {
- case CELL_TYPE:
- return (FCELL) * ((const CELL *)rast);
- case FCELL_TYPE:
- return *((const FCELL *)rast);
- case DCELL_TYPE:
- return (FCELL) * ((const DCELL *)rast);
- }
- return 0;
- }
- /*!
- * \brief Retrieves the value of given type from pointer p (DCELL)
- *
- * Retrieves the value of type data_type from pointer p, converts it
- * to DCELL type and returns the result. If null value is stored in p,
- * returns DCELL null value.
- * Used for retrieving DCELL values from raster cells of any type.
- *
- * \param rast pointer to raster cell value
- * \param data_type raster type (CELL, FCELL, DCELL)
- *
- * \return raster value
- */
- DCELL Rast_get_d_value(const void *rast, RASTER_MAP_TYPE data_type)
- {
- DCELL d;
- if (Rast_is_null_value(rast, data_type)) {
- Rast_set_d_null_value(&d, 1);
- return d;
- }
- switch (data_type) {
- case CELL_TYPE:
- return (DCELL) * ((const CELL *)rast);
- case FCELL_TYPE:
- return (DCELL) * ((const FCELL *)rast);
- case DCELL_TYPE:
- return *((const DCELL *)rast);
- }
- return 0;
- }
|