raster.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*!
  2. * \file 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 v2 is null value, sets v2 to null value.
  66. *
  67. * \param v1, v2 raster values
  68. * \param n ?
  69. * \param data_type raster type (CELL, FCELL, DCELL)
  70. */
  71. void Rast_raster_cpy(void *v1, const void *v2, int n,
  72. RASTER_MAP_TYPE data_type)
  73. {
  74. memcpy(v1, v2, n * Rast_cell_size(data_type));
  75. }
  76. /*!
  77. * \brief Places a CELL raster value
  78. *
  79. * If Rast_is_c_null_value() is true, sets p to null value. Converts CELL
  80. * val to data_type (type of p) and stores result in p. Used for
  81. * assigning CELL values to raster cells of any type.
  82. *
  83. * \param rast pointer to raster cell value
  84. * \param cval value to set
  85. * \param data_type raster type (CELL, FCELL, DCELL)
  86. */
  87. void Rast_set_c_value(void *rast, CELL cval, RASTER_MAP_TYPE data_type)
  88. {
  89. CELL c;
  90. c = cval;
  91. if (Rast_is_c_null_value(&c)) {
  92. Rast_set_null_value(rast, 1, data_type);
  93. return;
  94. }
  95. switch (data_type) {
  96. case CELL_TYPE:
  97. *((CELL *) rast) = cval;
  98. break;
  99. case FCELL_TYPE:
  100. *((FCELL *) rast) = (FCELL) cval;
  101. break;
  102. case DCELL_TYPE:
  103. *((DCELL *) rast) = (DCELL) cval;
  104. break;
  105. }
  106. }
  107. /*!
  108. * \brief Places a FCELL raster value
  109. *
  110. * If Rast_is_f_null_value() is true, sets p to null value. Converts
  111. * FCELL val to data_type (type of p) and stores result in p. Used for
  112. * assigning FCELL values to raster cells of any type.
  113. *
  114. * \param rast pointer to raster cell value
  115. * \param fval value to set
  116. * \param data_type raster type (CELL, FCELL, DCELL)
  117. */
  118. void Rast_set_f_value(void *rast, FCELL fval, RASTER_MAP_TYPE data_type)
  119. {
  120. FCELL f;
  121. f = fval;
  122. if (Rast_is_f_null_value(&f)) {
  123. Rast_set_null_value(rast, 1, data_type);
  124. return;
  125. }
  126. switch (data_type) {
  127. case CELL_TYPE:
  128. *((CELL *) rast) = (CELL) fval;
  129. break;
  130. case FCELL_TYPE:
  131. *((FCELL *) rast) = fval;
  132. break;
  133. case DCELL_TYPE:
  134. *((DCELL *) rast) = (DCELL) fval;
  135. break;
  136. }
  137. }
  138. /*!
  139. * \brief Places a DCELL raster value
  140. *
  141. * If Rast_is_d_null_value() is true, sets p to null value. Converts
  142. * DCELL val to data_type (type of p) and stores result in p. Used for
  143. * assigning DCELL values to raster cells of any type.
  144. *
  145. * \param rast pointer to raster cell value
  146. * \param fval value to set
  147. * \param data_type raster type (CELL, FCELL, DCELL)
  148. */
  149. void Rast_set_d_value(void *rast, DCELL dval, RASTER_MAP_TYPE data_type)
  150. {
  151. DCELL d;
  152. d = dval;
  153. if (Rast_is_d_null_value(&d)) {
  154. Rast_set_null_value(rast, 1, data_type);
  155. return;
  156. }
  157. switch (data_type) {
  158. case CELL_TYPE:
  159. *((CELL *) rast) = (CELL) dval;
  160. break;
  161. case FCELL_TYPE:
  162. *((FCELL *) rast) = (FCELL) dval;
  163. break;
  164. case DCELL_TYPE:
  165. *((DCELL *) rast) = dval;
  166. break;
  167. }
  168. }
  169. /*!
  170. * \brief Retrieves the value of give type from pointer p
  171. *
  172. * Retrieves the value of type data_type from pointer p, converts it
  173. * to CELL type and returns the result. If null value is stored in p,
  174. * returns CELL null value.
  175. *
  176. * Used for retrieving CELL values from raster cells of any type.
  177. *
  178. * Note: when data_type != CELL_TYPE, no quantization is used, only
  179. * type conversion.
  180. *
  181. * \param rast pointer to raster cell value
  182. * \param data_type raster type (CELL, FCELL, DCELL)
  183. *
  184. * \return raster value
  185. */
  186. CELL Rast_get_c_value(const void *rast, RASTER_MAP_TYPE data_type)
  187. {
  188. CELL c;
  189. if (Rast_is_null_value(rast, data_type)) {
  190. Rast_set_c_null_value(&c, 1);
  191. return c;
  192. }
  193. switch (data_type) {
  194. case CELL_TYPE:
  195. return *((const CELL *)rast);
  196. case FCELL_TYPE:
  197. return (CELL) * ((const FCELL *)rast);
  198. case DCELL_TYPE:
  199. return (CELL) * ((const DCELL *)rast);
  200. }
  201. return 0;
  202. }
  203. /*!
  204. * \brief Retrieves the value of given raster type from pointer p (FCELL)
  205. *
  206. * Retrieves the value of type data_type from pointer p, converts it
  207. * to FCELL type and returns the result. If null value is stored in p,
  208. * returns FCELL null value.
  209. *
  210. * Used for retrieving FCELL values from raster cells of any type.
  211. *
  212. * \param rast pointer to raster cell value
  213. * \param data_type raster type (CELL, FCELL, DCELL)
  214. *
  215. * \return raster value
  216. */
  217. FCELL Rast_get_f_value(const void *rast, RASTER_MAP_TYPE data_type)
  218. {
  219. FCELL f;
  220. if (Rast_is_null_value(rast, data_type)) {
  221. Rast_set_f_null_value(&f, 1);
  222. return f;
  223. }
  224. switch (data_type) {
  225. case CELL_TYPE:
  226. return (FCELL) * ((const CELL *)rast);
  227. case FCELL_TYPE:
  228. return *((const FCELL *)rast);
  229. case DCELL_TYPE:
  230. return (FCELL) * ((const DCELL *)rast);
  231. }
  232. return 0;
  233. }
  234. /*!
  235. * \brief Retrieves the value of given type from pointer p (DCELL)
  236. *
  237. * Retrieves the value of type data_type from pointer p, converts it
  238. * to DCELL type and returns the result. If null value is stored in p,
  239. * returns DCELL null value.
  240. * Used for retrieving DCELL values from raster cells of any type.
  241. *
  242. * \param rast pointer to raster cell value
  243. * \param data_type raster type (CELL, FCELL, DCELL)
  244. *
  245. * \return raster value
  246. */
  247. DCELL Rast_get_d_value(const void *rast, RASTER_MAP_TYPE data_type)
  248. {
  249. DCELL d;
  250. if (Rast_is_null_value(rast, data_type)) {
  251. Rast_set_d_null_value(&d, 1);
  252. return d;
  253. }
  254. switch (data_type) {
  255. case CELL_TYPE:
  256. return (DCELL) * ((const CELL *)rast);
  257. case FCELL_TYPE:
  258. return (DCELL) * ((const FCELL *)rast);
  259. case DCELL_TYPE:
  260. return *((const DCELL *)rast);
  261. }
  262. return 0;
  263. }