tinf.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #include <limits.h>
  2. #include <float.h>
  3. #include <math.h>
  4. #include <grass/gis.h>
  5. #include <grass/raster.h>
  6. #include "tinf.h"
  7. /* To add a new multitype function, use the function below to initialize
  8. * the function pointer to each of the three typed functions. The function
  9. * pointers and the function prototypes are defined in a header file.
  10. * The actual functions follow. */
  11. int (*is_null) (void *);
  12. int (*bpe) ();
  13. void *(*get_max) (void *, void *);
  14. void *(*get_min) (void *, void *);
  15. void (*get_row) (int, void *, int);
  16. void *(*get_buf) ();
  17. void (*put_row) (int, void *);
  18. double (*slope) (void *, void *, double);
  19. void (*set_min) (void *);
  20. void (*set_max) (void *);
  21. void (*diff) (void *, void *);
  22. void (*sum) (void *, void *);
  23. void (*quot) (void *, void *);
  24. void (*prod) (void *, void *);
  25. void set_func_pointers(int in_type)
  26. {
  27. switch (in_type) {
  28. case CELL_TYPE:
  29. is_null = is_null_c;
  30. bpe = bpe_c;
  31. get_max = get_max_c;
  32. get_min = get_min_c;
  33. get_row = get_row_c;
  34. get_buf = get_buf_c;
  35. put_row = put_row_c;
  36. slope = slope_c;
  37. set_min = set_min_c;
  38. set_max = set_max_c;
  39. diff = diff_c;
  40. sum = sum_c;
  41. quot = quot_c;
  42. prod = prod_c;
  43. break;
  44. case FCELL_TYPE:
  45. is_null = is_null_f;
  46. bpe = bpe_f;
  47. get_max = get_max_f;
  48. get_min = get_min_f;
  49. get_row = get_row_f;
  50. get_buf = get_buf_f;
  51. put_row = put_row_f;
  52. slope = slope_f;
  53. set_min = set_min_f;
  54. set_max = set_max_f;
  55. diff = diff_f;
  56. sum = sum_f;
  57. quot = quot_f;
  58. prod = prod_f;
  59. break;
  60. case DCELL_TYPE:
  61. is_null = is_null_d;
  62. bpe = bpe_d;
  63. get_max = get_max_d;
  64. get_min = get_min_d;
  65. get_row = get_row_d;
  66. get_buf = get_buf_d;
  67. put_row = put_row_d;
  68. slope = slope_d;
  69. set_min = set_min_d;
  70. set_max = set_max_d;
  71. diff = diff_d;
  72. sum = sum_d;
  73. quot = quot_d;
  74. prod = prod_d;
  75. }
  76. return;
  77. }
  78. /* check for null values */
  79. int is_null_c(void *value)
  80. {
  81. return Rast_is_c_null_value((CELL *) value);
  82. }
  83. int is_null_f(void *value)
  84. {
  85. return Rast_is_f_null_value((FCELL *) value);
  86. }
  87. int is_null_d(void *value)
  88. {
  89. return Rast_is_d_null_value((DCELL *) value);
  90. }
  91. /* return the size of the current type */
  92. int bpe_c()
  93. {
  94. return sizeof(CELL);
  95. }
  96. int bpe_f()
  97. {
  98. return sizeof(FCELL);
  99. }
  100. int bpe_d()
  101. {
  102. return sizeof(DCELL);
  103. }
  104. /* return the pointer that points to the smaller of two value */
  105. void *get_min_c(void *v1, void *v2)
  106. {
  107. void *rc;
  108. rc = v2;
  109. if (*(CELL *) v1 < *(CELL *) v2)
  110. rc = v1;
  111. return rc;
  112. }
  113. void *get_min_f(void *v1, void *v2)
  114. {
  115. void *rc;
  116. rc = v2;
  117. if (*(FCELL *) v1 < *(FCELL *) v2)
  118. rc = v1;
  119. return rc;
  120. }
  121. void *get_min_d(void *v1, void *v2)
  122. {
  123. void *rc;
  124. rc = v2;
  125. if (*(DCELL *) v1 < *(DCELL *) v2)
  126. rc = v1;
  127. return rc;
  128. }
  129. /* return the pointer that points to the larger value */
  130. void *get_max_c(void *v1, void *v2)
  131. {
  132. void *rc;
  133. rc = v2;
  134. if (*(CELL *) v1 > *(CELL *) v2)
  135. rc = v1;
  136. return rc;
  137. }
  138. void *get_max_f(void *v1, void *v2)
  139. {
  140. void *rc;
  141. rc = v2;
  142. if (*(FCELL *) v1 > *(FCELL *) v2)
  143. rc = v1;
  144. return rc;
  145. }
  146. void *get_max_d(void *v1, void *v2)
  147. {
  148. void *rc;
  149. rc = v2;
  150. if (*(DCELL *) v1 > *(DCELL *) v2)
  151. rc = v1;
  152. return rc;
  153. }
  154. /* Read one line from a raster map */
  155. void get_row_c(int fd, void *row, int n)
  156. {
  157. Rast_get_c_row(fd, (CELL *) row, n);
  158. }
  159. void get_row_f(int fd, void *row, int n)
  160. {
  161. Rast_get_f_row(fd, (FCELL *) row, n);
  162. }
  163. void get_row_d(int fd, void *row, int n)
  164. {
  165. Rast_get_d_row(fd, (DCELL *) row, n);
  166. }
  167. /* Write one row to a raster map */
  168. void put_row_c(int fd, void *row)
  169. {
  170. Rast_put_c_row(fd, (CELL *) row);
  171. }
  172. void put_row_f(int fd, void *row)
  173. {
  174. Rast_put_f_row(fd, (FCELL *) row);
  175. }
  176. void put_row_d(int fd, void *row)
  177. {
  178. Rast_put_d_row(fd, (DCELL *) row);
  179. }
  180. /* Allocate memory for one line of data */
  181. void *get_buf_c()
  182. {
  183. return (void *)Rast_allocate_c_buf();
  184. }
  185. void *get_buf_f()
  186. {
  187. return (void *)Rast_allocate_f_buf();
  188. }
  189. void *get_buf_d()
  190. {
  191. return (void *)Rast_allocate_d_buf();
  192. }
  193. /* initialize memory to a minimum value */
  194. void set_min_c(void *v)
  195. {
  196. *(CELL *) v = INT_MIN;
  197. }
  198. void set_min_f(void *v)
  199. {
  200. *(FCELL *) v = FLT_MIN;
  201. }
  202. void set_min_d(void *v)
  203. {
  204. *(DCELL *) v = DBL_MIN;
  205. }
  206. /* initialize memory to a maximum value */
  207. void set_max_c(void *v)
  208. {
  209. *(CELL *) v = INT_MAX;
  210. }
  211. void set_max_f(void *v)
  212. {
  213. *(FCELL *) v = FLT_MAX;
  214. }
  215. void set_max_d(void *v)
  216. {
  217. *(DCELL *) v = DBL_MAX;
  218. }
  219. /* get the difference between two values, returned in the first pointer */
  220. void diff_c(void *v1, void *v2)
  221. {
  222. *(CELL *) v1 -= *(CELL *) v2;
  223. }
  224. void diff_f(void *v1, void *v2)
  225. {
  226. *(FCELL *) v1 -= *(FCELL *) v2;
  227. }
  228. void diff_d(void *v1, void *v2)
  229. {
  230. *(DCELL *) v1 -= *(DCELL *) v2;
  231. }
  232. /* get the sum of two values, returned in the first pointer */
  233. void sum_c(void *v1, void *v2)
  234. {
  235. *(CELL *) v1 += *(CELL *) v2;
  236. }
  237. void sum_f(void *v1, void *v2)
  238. {
  239. *(FCELL *) v1 += *(FCELL *) v2;
  240. }
  241. void sum_d(void *v1, void *v2)
  242. {
  243. *(DCELL *) v1 += *(DCELL *) v2;
  244. }
  245. /* get the quotient of two values, returned in the first pointer */
  246. void quot_c(void *v1, void *v2)
  247. {
  248. *(CELL *) v1 /= *(CELL *) v2;
  249. }
  250. void quot_f(void *v1, void *v2)
  251. {
  252. *(FCELL *) v1 /= *(FCELL *) v2;
  253. }
  254. void quot_d(void *v1, void *v2)
  255. {
  256. *(DCELL *) v1 /= *(DCELL *) v2;
  257. }
  258. /* get the product of two values, returned in the first pointer */
  259. void prod_c(void *v1, void *v2)
  260. {
  261. *(CELL *) v1 *= *(CELL *) v2;
  262. }
  263. void prod_f(void *v1, void *v2)
  264. {
  265. *(FCELL *) v1 *= *(FCELL *) v2;
  266. }
  267. void prod_d(void *v1, void *v2)
  268. {
  269. *(DCELL *) v1 *= *(DCELL *) v2;
  270. }
  271. /* probably not a function of general interest */
  272. /* calculate the slope between two cells, returned as a double */
  273. double slope_c(void *line1, void *line2, double cnst)
  274. {
  275. double rc;
  276. CELL *pedge;
  277. rc = -HUGE_VAL;
  278. pedge = (CELL *) line2;
  279. if (!Rast_is_c_null_value(pedge)) {
  280. rc = (*(CELL *) line1 - *pedge) / cnst;
  281. }
  282. return rc;
  283. }
  284. double slope_f(void *line1, void *line2, double cnst)
  285. {
  286. double rc;
  287. FCELL *pedge;
  288. rc = -HUGE_VAL;
  289. pedge = (FCELL *) line2;
  290. if (!Rast_is_f_null_value(pedge)) {
  291. rc = (*(FCELL *) line1 - *pedge) / cnst;
  292. }
  293. return rc;
  294. }
  295. double slope_d(void *line1, void *line2, double cnst)
  296. {
  297. double rc;
  298. DCELL *pedge;
  299. rc = -HUGE_VAL;
  300. pedge = (DCELL *) line2;
  301. if (!Rast_is_d_null_value(pedge)) {
  302. rc = (*(DCELL *) line1 - *pedge) / cnst;
  303. }
  304. return rc;
  305. }
  306. /* read a line and update a three-line buffer */
  307. /* moving forward through a file */
  308. int advance_band3(int fh, struct band3 *bnd)
  309. {
  310. int rc;
  311. void *hold;
  312. hold = bnd->b[0];
  313. bnd->b[0] = bnd->b[1];
  314. bnd->b[1] = bnd->b[2];
  315. bnd->b[2] = hold;
  316. if (fh == 0)
  317. rc = 0;
  318. else
  319. rc = read(fh, bnd->b[2], bnd->sz);
  320. return rc;
  321. }
  322. /* read a line and update a three-line buffer */
  323. /* moving backward through a file */
  324. int retreat_band3(int fh, struct band3 *bnd)
  325. {
  326. int rc;
  327. void *hold;
  328. hold = bnd->b[2];
  329. bnd->b[2] = bnd->b[1];
  330. bnd->b[1] = bnd->b[0];
  331. bnd->b[0] = hold;
  332. if (fh == 0)
  333. rc = 0;
  334. else {
  335. rc = read(fh, bnd->b[0], bnd->sz);
  336. lseek(fh, (off_t) - 2 * bnd->sz, SEEK_CUR);
  337. }
  338. return rc;
  339. }