raster.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 int
  93. */
  94. int G_raster_cpy(void *v1, const void *v2, int n, RASTER_MAP_TYPE data_type)
  95. {
  96. G_copy((char *)v1, (char *)v2, n * G_raster_size(data_type));
  97. return 0;
  98. }
  99. /*!
  100. * \brief Places a CELL raster value
  101. *
  102. * If G_is_c_null_value(val) is true, sets p to null value.
  103. * Converts CELL val to data_type (type of p) and stores result in p.
  104. * Used for assigning CELL values to raster cells of any type.
  105. *
  106. * \param p
  107. * \param val
  108. * \param data_type
  109. * \return int
  110. */
  111. int G_set_raster_value_c(void *rast, CELL cval, RASTER_MAP_TYPE data_type)
  112. {
  113. CELL c;
  114. c = cval;
  115. if (G_is_c_null_value(&c)) {
  116. G_set_null_value(rast, 1, data_type);
  117. return 0;
  118. }
  119. switch (data_type) {
  120. case CELL_TYPE:
  121. *((CELL *) rast) = cval;
  122. break;
  123. case FCELL_TYPE:
  124. *((FCELL *) rast) = (FCELL) cval;
  125. break;
  126. case DCELL_TYPE:
  127. *((DCELL *) rast) = (DCELL) cval;
  128. break;
  129. }
  130. return 0;
  131. }
  132. /*!
  133. * \brief Places a FCELL raster value
  134. *
  135. * If G_is_f_null_value(val) is true, sets p to null value.
  136. * Converts FCELL val to data_type (type of p) and stores result in p.
  137. * Used for assigning FCELL values to raster cells of any type.
  138. *
  139. * \param p
  140. * \param val
  141. * \param data_type
  142. * \return int
  143. */
  144. int G_set_raster_value_f(void *rast, FCELL fval, RASTER_MAP_TYPE data_type)
  145. {
  146. FCELL f;
  147. f = fval;
  148. if (G_is_f_null_value(&f)) {
  149. G_set_null_value(rast, 1, data_type);
  150. return 0;
  151. }
  152. switch (data_type) {
  153. case CELL_TYPE:
  154. *((CELL *) rast) = (CELL) fval;
  155. break;
  156. case FCELL_TYPE:
  157. *((FCELL *) rast) = fval;
  158. break;
  159. case DCELL_TYPE:
  160. *((DCELL *) rast) = (DCELL) fval;
  161. break;
  162. }
  163. return 0;
  164. }
  165. /*!
  166. * \brief Places a DCELL raster value
  167. *
  168. * If G_is_d_null_value(val) is true, sets p to null value.
  169. * Converts DCELL val to data_type (type of p) and stores result in p.
  170. * Used for assigning DCELL values to raster cells of any type.
  171. *
  172. * \param p
  173. * \param val
  174. * \param data_type
  175. * \return int
  176. */
  177. int G_set_raster_value_d(void *rast, DCELL dval, RASTER_MAP_TYPE data_type)
  178. {
  179. DCELL d;
  180. d = dval;
  181. if (G_is_d_null_value(&d)) {
  182. G_set_null_value(rast, 1, data_type);
  183. return -1;
  184. }
  185. switch (data_type) {
  186. case CELL_TYPE:
  187. *((CELL *) rast) = (CELL) dval;
  188. break;
  189. case FCELL_TYPE:
  190. *((FCELL *) rast) = (FCELL) dval;
  191. break;
  192. case DCELL_TYPE:
  193. *((DCELL *) rast) = dval;
  194. break;
  195. }
  196. return 0;
  197. }
  198. /*!
  199. * \brief Retrieves the value of type data_type from pointer p
  200. *
  201. * Retrieves the value of type data_type from pointer p,
  202. * converts it to CELL type and returns the result.
  203. * If null value is stored in p, returns CELL null value.
  204. *
  205. * Used for retreiving CELL values from raster cells of any type.
  206. *
  207. * NOTE: when data_type != CELL_TYPE, no quantization is used, only
  208. * type conversion.
  209. *
  210. * \param p
  211. * \param data_type
  212. * \return CELL
  213. */
  214. CELL G_get_raster_value_c(const void *rast, RASTER_MAP_TYPE data_type)
  215. {
  216. CELL c;
  217. if (G_is_null_value(rast, data_type)) {
  218. G_set_c_null_value(&c, 1);
  219. return c;
  220. }
  221. switch (data_type) {
  222. case CELL_TYPE:
  223. return *((const CELL *)rast);
  224. case FCELL_TYPE:
  225. return (CELL) * ((const FCELL *)rast);
  226. case DCELL_TYPE:
  227. return (CELL) * ((const DCELL *)rast);
  228. }
  229. return 0;
  230. }
  231. /*!
  232. * \brief Retrieves the value of type data_type from pointer p
  233. *
  234. * Retrieves the value of type data_type from pointer p,
  235. * converts it to FCELL type and returns the result.
  236. * If null value is stored in p, returns FCELL null value.
  237. *
  238. * Used for retreiving FCELL values from raster cells of any type.
  239. *
  240. * \param p
  241. * \param data_type
  242. * \return FCELL
  243. */
  244. FCELL G_get_raster_value_f(const void *rast, RASTER_MAP_TYPE data_type)
  245. {
  246. FCELL f;
  247. if (G_is_null_value(rast, data_type)) {
  248. G_set_f_null_value(&f, 1);
  249. return f;
  250. }
  251. switch (data_type) {
  252. case CELL_TYPE:
  253. return (FCELL) * ((const CELL *)rast);
  254. case FCELL_TYPE:
  255. return *((const FCELL *)rast);
  256. case DCELL_TYPE:
  257. return (FCELL) * ((const DCELL *)rast);
  258. }
  259. return 0;
  260. }
  261. /*!
  262. * \brief Retrieves the value of type data_type from pointer p,
  263. *
  264. * Retrieves the value of type data_type from pointer p,
  265. * converts it to DCELL type and returns the result.
  266. * If null value is stored in p, returns DCELL null value.
  267. * Used for retreiving DCELL values from raster cells of any type.
  268. *
  269. * \param p
  270. * \param data_type
  271. * \return DCELL
  272. */
  273. DCELL G_get_raster_value_d(const void *rast, RASTER_MAP_TYPE data_type)
  274. {
  275. DCELL d;
  276. if (G_is_null_value(rast, data_type)) {
  277. G_set_d_null_value(&d, 1);
  278. return d;
  279. }
  280. switch (data_type) {
  281. case CELL_TYPE:
  282. return (DCELL) * ((const CELL *)rast);
  283. case FCELL_TYPE:
  284. return (DCELL) * ((const FCELL *)rast);
  285. case DCELL_TYPE:
  286. return *((const DCELL *)rast);
  287. }
  288. return 0;
  289. }