raster.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include <stdlib.h>
  2. #include <grass/gis.h>
  3. /*!
  4. * \brief Advance void pointer
  5. *
  6. * Advances void pointer by <em>size</em> bytes
  7. * Returns new pointer value.
  8. *
  9. * Useful in raster row processing loops, substitutes
  10. \code
  11. CELL *cell;
  12. cell += n;
  13. \endcode
  14. *
  15. * Now
  16. \code
  17. rast = G_incr_void_ptr(rast, G_raster_size(data_type))
  18. \endcode
  19. *
  20. * (where rast is void* and data_type is RASTER_MAP_TYPE can be used instead
  21. * of rast++.)
  22. *
  23. * very useful to generalize the row processing - loop i.e.
  24. * void * buf_ptr += G_raster_size(data_type)
  25. *
  26. * \param ptr
  27. * \param size
  28. * \return void *
  29. */
  30. void *G_incr_void_ptr(const void *ptr, const size_t size)
  31. {
  32. /* assuming that the size of unsigned char is 1 */
  33. return (void *)((const unsigned char *)ptr + size);
  34. }
  35. /*!
  36. * \brief Compares raster values p and q
  37. *
  38. * Returns:
  39. * 1 if p > q or only q is null value
  40. * -1 if p < q or only p is null value
  41. * 0 if p == q or p==q==null value
  42. *
  43. * \param p
  44. * \param q
  45. * \param data_type
  46. * \return int
  47. */
  48. int G_raster_cmp(const void *v1, const void *v2, RASTER_MAP_TYPE data_type)
  49. {
  50. if (G_is_null_value(v1, data_type)) {
  51. if (G_is_null_value(v2, data_type))
  52. return 0;
  53. else
  54. return -1;
  55. }
  56. else if (G_is_null_value(v2, data_type))
  57. return 1;
  58. switch (data_type) {
  59. case CELL_TYPE:
  60. if (*((const CELL *)v1) > *((const CELL *)v2))
  61. return 1;
  62. else if (*((const CELL *)v1) == *((const CELL *)v2))
  63. return 0;
  64. else
  65. return -1;
  66. case FCELL_TYPE:
  67. if (*((const FCELL *)v1) > *((const FCELL *)v2))
  68. return 1;
  69. else if (*((const FCELL *)v1) == *((const FCELL *)v2))
  70. return 0;
  71. else
  72. return -1;
  73. case DCELL_TYPE:
  74. if (*((const DCELL *)v1) > *((const DCELL *)v2))
  75. return 1;
  76. else if (*((const DCELL *)v1) == *((const DCELL *)v2))
  77. return 0;
  78. else
  79. return -1;
  80. }
  81. return 0;
  82. }
  83. /*!
  84. * \brief Copies raster values q into p
  85. *
  86. * If q is null value, sets q to null value.
  87. *
  88. * \param p
  89. * \param q
  90. * \param n
  91. * \param data_type
  92. * \return
  93. */
  94. void G_raster_cpy(void *v1, const void *v2, int n, RASTER_MAP_TYPE data_type)
  95. {
  96. G_copy(v1, v2, n * G_raster_size(data_type));
  97. }
  98. /*!
  99. * \brief Places a CELL raster value
  100. *
  101. * If G_is_c_null_value(val) is true, sets p to null value.
  102. * Converts CELL val to data_type (type of p) and stores result in p.
  103. * Used for assigning CELL values to raster cells of any type.
  104. *
  105. * \param p
  106. * \param val
  107. * \param data_type
  108. * \return
  109. */
  110. void G_set_raster_value_c(void *rast, CELL cval, RASTER_MAP_TYPE data_type)
  111. {
  112. CELL c;
  113. c = cval;
  114. if (G_is_c_null_value(&c)) {
  115. G_set_null_value(rast, 1, data_type);
  116. return;
  117. }
  118. switch (data_type) {
  119. case CELL_TYPE:
  120. *((CELL *) rast) = cval;
  121. break;
  122. case FCELL_TYPE:
  123. *((FCELL *) rast) = (FCELL) cval;
  124. break;
  125. case DCELL_TYPE:
  126. *((DCELL *) rast) = (DCELL) cval;
  127. break;
  128. }
  129. }
  130. /*!
  131. * \brief Places a FCELL raster value
  132. *
  133. * If G_is_f_null_value(val) is true, sets p to null value.
  134. * Converts FCELL val to data_type (type of p) and stores result in p.
  135. * Used for assigning FCELL values to raster cells of any type.
  136. *
  137. * \param p
  138. * \param val
  139. * \param data_type
  140. * \return
  141. */
  142. void G_set_raster_value_f(void *rast, FCELL fval, RASTER_MAP_TYPE data_type)
  143. {
  144. FCELL f;
  145. f = fval;
  146. if (G_is_f_null_value(&f)) {
  147. G_set_null_value(rast, 1, data_type);
  148. return;
  149. }
  150. switch (data_type) {
  151. case CELL_TYPE:
  152. *((CELL *) rast) = (CELL) fval;
  153. break;
  154. case FCELL_TYPE:
  155. *((FCELL *) rast) = fval;
  156. break;
  157. case DCELL_TYPE:
  158. *((DCELL *) rast) = (DCELL) fval;
  159. break;
  160. }
  161. }
  162. /*!
  163. * \brief Places a DCELL raster value
  164. *
  165. * If G_is_d_null_value(val) is true, sets p to null value.
  166. * Converts DCELL val to data_type (type of p) and stores result in p.
  167. * Used for assigning DCELL values to raster cells of any type.
  168. *
  169. * \param p
  170. * \param val
  171. * \param data_type
  172. * \return
  173. */
  174. void G_set_raster_value_d(void *rast, DCELL dval, RASTER_MAP_TYPE data_type)
  175. {
  176. DCELL d;
  177. d = dval;
  178. if (G_is_d_null_value(&d)) {
  179. G_set_null_value(rast, 1, data_type);
  180. return;
  181. }
  182. switch (data_type) {
  183. case CELL_TYPE:
  184. *((CELL *) rast) = (CELL) dval;
  185. break;
  186. case FCELL_TYPE:
  187. *((FCELL *) rast) = (FCELL) dval;
  188. break;
  189. case DCELL_TYPE:
  190. *((DCELL *) rast) = dval;
  191. break;
  192. }
  193. }
  194. /*!
  195. * \brief Retrieves the value of type data_type from pointer p
  196. *
  197. * Retrieves the value of type data_type from pointer p,
  198. * converts it to CELL type and returns the result.
  199. * If null value is stored in p, returns CELL null value.
  200. *
  201. * Used for retrieving CELL values from raster cells of any type.
  202. *
  203. * NOTE: when data_type != CELL_TYPE, no quantization is used, only
  204. * type conversion.
  205. *
  206. * \param p
  207. * \param data_type
  208. * \return CELL
  209. */
  210. CELL G_get_raster_value_c(const void *rast, RASTER_MAP_TYPE data_type)
  211. {
  212. CELL c;
  213. if (G_is_null_value(rast, data_type)) {
  214. G_set_c_null_value(&c, 1);
  215. return c;
  216. }
  217. switch (data_type) {
  218. case CELL_TYPE:
  219. return *((const CELL *)rast);
  220. case FCELL_TYPE:
  221. return (CELL) * ((const FCELL *)rast);
  222. case DCELL_TYPE:
  223. return (CELL) * ((const DCELL *)rast);
  224. }
  225. return 0;
  226. }
  227. /*!
  228. * \brief Retrieves the value of type data_type from pointer p
  229. *
  230. * Retrieves the value of type data_type from pointer p,
  231. * converts it to FCELL type and returns the result.
  232. * If null value is stored in p, returns FCELL null value.
  233. *
  234. * Used for retrieving FCELL values from raster cells of any type.
  235. *
  236. * \param p
  237. * \param data_type
  238. * \return FCELL
  239. */
  240. FCELL G_get_raster_value_f(const void *rast, RASTER_MAP_TYPE data_type)
  241. {
  242. FCELL f;
  243. if (G_is_null_value(rast, data_type)) {
  244. G_set_f_null_value(&f, 1);
  245. return f;
  246. }
  247. switch (data_type) {
  248. case CELL_TYPE:
  249. return (FCELL) * ((const CELL *)rast);
  250. case FCELL_TYPE:
  251. return *((const FCELL *)rast);
  252. case DCELL_TYPE:
  253. return (FCELL) * ((const DCELL *)rast);
  254. }
  255. return 0;
  256. }
  257. /*!
  258. * \brief Retrieves the value of type data_type from pointer p,
  259. *
  260. * Retrieves the value of type data_type from pointer p,
  261. * converts it to DCELL type and returns the result.
  262. * If null value is stored in p, returns DCELL null value.
  263. * Used for retrieving DCELL values from raster cells of any type.
  264. *
  265. * \param p
  266. * \param data_type
  267. * \return DCELL
  268. */
  269. DCELL G_get_raster_value_d(const void *rast, RASTER_MAP_TYPE data_type)
  270. {
  271. DCELL d;
  272. if (G_is_null_value(rast, data_type)) {
  273. G_set_d_null_value(&d, 1);
  274. return d;
  275. }
  276. switch (data_type) {
  277. case CELL_TYPE:
  278. return (DCELL) * ((const CELL *)rast);
  279. case FCELL_TYPE:
  280. return (DCELL) * ((const FCELL *)rast);
  281. case DCELL_TYPE:
  282. return *((const DCELL *)rast);
  283. }
  284. return 0;
  285. }