tinf.c 7.4 KB

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