raster.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*!
  2. * \file lib/raster/raster.c
  3. *
  4. * \brief Raster Library - Raster cell value routines.
  5. *
  6. * (C) 2001-2009 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author Original author CERL
  12. */
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <grass/gis.h>
  16. #include <grass/raster.h>
  17. /*!
  18. * \brief Compares raster values.
  19. *
  20. * \param v1,v2 values to be compared
  21. * \param data_type raster type (CELL, FCELL, DCELL)
  22. *
  23. * \return 1 if p > q or only q is null value
  24. * \return -1 if p < q or only p is null value
  25. * \return 0 if p == q or p==q==null value
  26. */
  27. int Rast_raster_cmp(const void *v1, const void *v2, RASTER_MAP_TYPE data_type)
  28. {
  29. if (Rast_is_null_value(v1, data_type)) {
  30. if (Rast_is_null_value(v2, data_type))
  31. return 0;
  32. else
  33. return -1;
  34. }
  35. else if (Rast_is_null_value(v2, data_type))
  36. return 1;
  37. switch (data_type) {
  38. case CELL_TYPE:
  39. if (*((const CELL *)v1) > *((const CELL *)v2))
  40. return 1;
  41. else if (*((const CELL *)v1) == *((const CELL *)v2))
  42. return 0;
  43. else
  44. return -1;
  45. case FCELL_TYPE:
  46. if (*((const FCELL *)v1) > *((const FCELL *)v2))
  47. return 1;
  48. else if (*((const FCELL *)v1) == *((const FCELL *)v2))
  49. return 0;
  50. else
  51. return -1;
  52. case DCELL_TYPE:
  53. if (*((const DCELL *)v1) > *((const DCELL *)v2))
  54. return 1;
  55. else if (*((const DCELL *)v1) == *((const DCELL *)v2))
  56. return 0;
  57. else
  58. return -1;
  59. }
  60. return 0;
  61. }
  62. /*!
  63. * \brief Copies raster values.
  64. *
  65. * If \p v2 is null value, sets \p v2 to null value.
  66. * \p n is typically size of the destination array
  67. * and the source array is at least that large.
  68. *
  69. * \param v1 destination array for raster values
  70. * \param v2 source array with raster values
  71. * \param n number of values to copy
  72. * \param data_type raster type (CELL, FCELL, DCELL)
  73. */
  74. void Rast_raster_cpy(void *v1, const void *v2, int n,
  75. RASTER_MAP_TYPE data_type)
  76. {
  77. memcpy(v1, v2, n * Rast_cell_size(data_type));
  78. }
  79. /*!
  80. * \brief Places a CELL raster value
  81. *
  82. * If Rast_is_c_null_value() is true, sets p to null value. Converts CELL
  83. * val to data_type (type of p) and stores result in p. Used for
  84. * assigning CELL values to raster cells of any type.
  85. *
  86. * \param rast pointer to raster cell value
  87. * \param cval value to set
  88. * \param data_type raster type (CELL, FCELL, DCELL)
  89. */
  90. void Rast_set_c_value(void *rast, CELL cval, RASTER_MAP_TYPE data_type)
  91. {
  92. CELL c;
  93. c = cval;
  94. if (Rast_is_c_null_value(&c)) {
  95. Rast_set_null_value(rast, 1, data_type);
  96. return;
  97. }
  98. switch (data_type) {
  99. case CELL_TYPE:
  100. *((CELL *) rast) = cval;
  101. break;
  102. case FCELL_TYPE:
  103. *((FCELL *) rast) = (FCELL) cval;
  104. break;
  105. case DCELL_TYPE:
  106. *((DCELL *) rast) = (DCELL) cval;
  107. break;
  108. }
  109. }
  110. /*!
  111. * \brief Places a FCELL raster value
  112. *
  113. * If Rast_is_f_null_value() is true, sets p to null value. Converts
  114. * FCELL val to data_type (type of p) and stores result in p. Used for
  115. * assigning FCELL values to raster cells of any type.
  116. *
  117. * \param rast pointer to raster cell value
  118. * \param fval value to set
  119. * \param data_type raster type (CELL, FCELL, DCELL)
  120. */
  121. void Rast_set_f_value(void *rast, FCELL fval, RASTER_MAP_TYPE data_type)
  122. {
  123. FCELL f;
  124. f = fval;
  125. if (Rast_is_f_null_value(&f)) {
  126. Rast_set_null_value(rast, 1, data_type);
  127. return;
  128. }
  129. switch (data_type) {
  130. case CELL_TYPE:
  131. *((CELL *) rast) = (CELL) fval;
  132. break;
  133. case FCELL_TYPE:
  134. *((FCELL *) rast) = fval;
  135. break;
  136. case DCELL_TYPE:
  137. *((DCELL *) rast) = (DCELL) fval;
  138. break;
  139. }
  140. }
  141. /*!
  142. * \brief Places a DCELL raster value
  143. *
  144. * If Rast_is_d_null_value() is true, sets p to null value. Converts
  145. * DCELL val to data_type (type of p) and stores result in p. Used for
  146. * assigning DCELL values to raster cells of any type.
  147. *
  148. * \param rast pointer to raster cell value
  149. * \param fval value to set
  150. * \param data_type raster type (CELL, FCELL, DCELL)
  151. */
  152. void Rast_set_d_value(void *rast, DCELL dval, RASTER_MAP_TYPE data_type)
  153. {
  154. DCELL d;
  155. d = dval;
  156. if (Rast_is_d_null_value(&d)) {
  157. Rast_set_null_value(rast, 1, data_type);
  158. return;
  159. }
  160. switch (data_type) {
  161. case CELL_TYPE:
  162. *((CELL *) rast) = (CELL) dval;
  163. break;
  164. case FCELL_TYPE:
  165. *((FCELL *) rast) = (FCELL) dval;
  166. break;
  167. case DCELL_TYPE:
  168. *((DCELL *) rast) = dval;
  169. break;
  170. }
  171. }
  172. /*!
  173. * \brief Retrieves the value of give type from pointer p
  174. *
  175. * Retrieves the value of type data_type from pointer p, converts it
  176. * to CELL type and returns the result. If null value is stored in p,
  177. * returns CELL null value.
  178. *
  179. * Used for retrieving CELL values from raster cells of any type.
  180. *
  181. * Note: when data_type != CELL_TYPE, no quantization is used, only
  182. * type conversion.
  183. *
  184. * \param rast pointer to raster cell value
  185. * \param data_type raster type (CELL, FCELL, DCELL)
  186. *
  187. * \return raster value
  188. */
  189. CELL Rast_get_c_value(const void *rast, RASTER_MAP_TYPE data_type)
  190. {
  191. CELL c;
  192. if (Rast_is_null_value(rast, data_type)) {
  193. Rast_set_c_null_value(&c, 1);
  194. return c;
  195. }
  196. switch (data_type) {
  197. case CELL_TYPE:
  198. return *((const CELL *)rast);
  199. case FCELL_TYPE:
  200. return (CELL) * ((const FCELL *)rast);
  201. case DCELL_TYPE:
  202. return (CELL) * ((const DCELL *)rast);
  203. }
  204. return 0;
  205. }
  206. /*!
  207. * \brief Retrieves the value of given raster type from pointer p (FCELL)
  208. *
  209. * Retrieves the value of type data_type from pointer p, converts it
  210. * to FCELL type and returns the result. If null value is stored in p,
  211. * returns FCELL null value.
  212. *
  213. * Used for retrieving FCELL values from raster cells of any type.
  214. *
  215. * \param rast pointer to raster cell value
  216. * \param data_type raster type (CELL, FCELL, DCELL)
  217. *
  218. * \return raster value
  219. */
  220. FCELL Rast_get_f_value(const void *rast, RASTER_MAP_TYPE data_type)
  221. {
  222. FCELL f;
  223. if (Rast_is_null_value(rast, data_type)) {
  224. Rast_set_f_null_value(&f, 1);
  225. return f;
  226. }
  227. switch (data_type) {
  228. case CELL_TYPE:
  229. return (FCELL) * ((const CELL *)rast);
  230. case FCELL_TYPE:
  231. return *((const FCELL *)rast);
  232. case DCELL_TYPE:
  233. return (FCELL) * ((const DCELL *)rast);
  234. }
  235. return 0;
  236. }
  237. /*!
  238. * \brief Retrieves the value of given type from pointer p (DCELL)
  239. *
  240. * Retrieves the value of type data_type from pointer p, converts it
  241. * to DCELL type and returns the result. If null value is stored in p,
  242. * returns DCELL null value.
  243. * Used for retrieving DCELL values from raster cells of any type.
  244. *
  245. * \param rast pointer to raster cell value
  246. * \param data_type raster type (CELL, FCELL, DCELL)
  247. *
  248. * \return raster value
  249. */
  250. DCELL Rast_get_d_value(const void *rast, RASTER_MAP_TYPE data_type)
  251. {
  252. DCELL d;
  253. if (Rast_is_null_value(rast, data_type)) {
  254. Rast_set_d_null_value(&d, 1);
  255. return d;
  256. }
  257. switch (data_type) {
  258. case CELL_TYPE:
  259. return (DCELL) * ((const CELL *)rast);
  260. case FCELL_TYPE:
  261. return (DCELL) * ((const FCELL *)rast);
  262. case DCELL_TYPE:
  263. return *((const DCELL *)rast);
  264. }
  265. return 0;
  266. }