null_val.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. *
  3. *****************************************************************************
  4. *
  5. * MODULE: GRASS gis library
  6. * AUTHOR(S): Original author unknown - probably CERL
  7. * Justin Hickey - Thailand - jhickey@hpcc.nectec.or.th
  8. * PURPOSE: To provide functionality to handle NULL values for data types
  9. * CELL, FCELL, and DCELL. May need more...
  10. * COPYRIGHT: (C) 2000 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. *****************************************************************************/
  17. /*============================= Include Files ==============================*/
  18. /* System include files */
  19. #include <string.h>
  20. /* Grass and local include files */
  21. #include <grass/gis.h>
  22. #include <grass/glocale.h>
  23. static void EmbedGivenNulls(void *, char *, RASTER_MAP_TYPE, int);
  24. /****************************************************************************
  25. * int EmbedGivenNulls (void *cell, char *nulls, RASTER_MAP_TYPE map_type,
  26. * int ncols)
  27. *
  28. * PURPOSE: To insert null values into a map. Needs more.....
  29. * INPUT VARS: cell => ??
  30. * nulls => ??
  31. * map_type => type of raster - CELL, FCELL, DCELL
  32. * ncols => ??
  33. * RETURN VAL: ??
  34. *****************************************************************************/
  35. static void EmbedGivenNulls(void *cell, char *nulls, RASTER_MAP_TYPE map_type,
  36. int ncols)
  37. {
  38. CELL *c;
  39. FCELL *f;
  40. DCELL *d;
  41. int i;
  42. c = (CELL *) cell;
  43. f = (FCELL *) cell;
  44. d = (DCELL *) cell;
  45. for (i = 0; i < ncols; i++) {
  46. if (nulls[i]) {
  47. switch (map_type) {
  48. case CELL_TYPE:
  49. G_set_c_null_value((CELL *) (c + i), 1);
  50. break;
  51. case FCELL_TYPE:
  52. G_set_f_null_value((FCELL *) (f + i), 1);
  53. break;
  54. case DCELL_TYPE:
  55. G_set_d_null_value((DCELL *) (d + i), 1);
  56. break;
  57. default:
  58. G_warning(_("EmbedGivenNulls: wrong data type!"));
  59. }
  60. }
  61. }
  62. }
  63. /*========================== Library Functions =============================*/
  64. /****************************************************************************
  65. * void G__set_null_value (void *rast, int numVals, int null_is_zero,
  66. * RASTER_MAP_TYPE data_type)
  67. *
  68. * PURPOSE: To set one or more raster values to null. It also sets null
  69. * to zero if null_is_zero is TRUE.
  70. * INPUT VARS: rast => pointer to values to set to null
  71. * numVals => number of values to set to null
  72. * null_is_zero => flag to indicate if NULL = 0
  73. * data_type => type of raster - CELL, FCELL, DCELL
  74. * RETURN VAL: none
  75. *****************************************************************************/
  76. void G__set_null_value(void *rast, int numVals, int null_is_zero,
  77. RASTER_MAP_TYPE data_type)
  78. {
  79. if (null_is_zero) {
  80. G_zero((char *)rast, numVals * G_raster_size(data_type));
  81. return;
  82. }
  83. G_set_null_value(rast, numVals, data_type);
  84. }
  85. /****************************************************************************
  86. * void G_set_null_value (void *buf, int numVals, RASTER_MAP_TYPE data_type)
  87. *
  88. * PURPOSE: To set one or more raster values to null.
  89. * INPUT VARS: buf => pointer to values to set to null
  90. * numVals => number of values to set to null
  91. * data_type => type of raster - CELL, FCELL, DCELL
  92. * RETURN VAL: none
  93. *****************************************************************************/
  94. void G_set_null_value(void *buf, int numVals, RASTER_MAP_TYPE data_type)
  95. {
  96. switch (data_type) {
  97. case CELL_TYPE:
  98. G_set_c_null_value((CELL *) buf, numVals);
  99. break;
  100. case FCELL_TYPE:
  101. G_set_f_null_value((FCELL *) buf, numVals);
  102. break;
  103. case DCELL_TYPE:
  104. G_set_d_null_value((DCELL *) buf, numVals);
  105. break;
  106. default:
  107. G_warning(_("G_set_null_value: wrong data type!"));
  108. }
  109. }
  110. /****************************************************************************
  111. * void G_set_c_null_value (CELL *cellVals, int numVals)
  112. *
  113. * PURPOSE: To set a number of CELL raster values to NULL.
  114. * INPUT VARS: cellVals => pointer to CELL values to set to null
  115. * numVals => number of values to set to null
  116. * RETURN VAL: none
  117. *****************************************************************************/
  118. void G_set_c_null_value(CELL *cellVals, int numVals)
  119. {
  120. int i; /* counter */
  121. for (i = 0; i < numVals; i++)
  122. cellVals[i] = (int) 0x80000000;
  123. }
  124. /****************************************************************************
  125. * void G_set_f_null_value (FCELL *fcellVals, int numVals)
  126. *
  127. * PURPOSE: To set a number of FCELL raster values to NULL.
  128. * INPUT VARS: fcellVals => pointer to FCELL values to set to null
  129. * numVals => number of values to set to null
  130. * RETURN VAL: none
  131. *****************************************************************************/
  132. void G_set_f_null_value(FCELL *fcellVals, int numVals)
  133. {
  134. static const unsigned char null_bits[4] = {
  135. 0xFF, 0xFF, 0xFF, 0xFF};
  136. int i;
  137. for (i = 0; i < numVals; i++)
  138. memcpy(&fcellVals[i], null_bits, sizeof(null_bits));
  139. }
  140. /****************************************************************************
  141. * void G_set_d_null_value (DCELL *dcellVals, int numVals)
  142. *
  143. * PURPOSE: To set a number of DCELL raster values to NULL.
  144. * INPUT VARS: dcellVals => pointer to DCELL values to set to null
  145. * numVals => number of values to set to null
  146. * RETURN VAL: none
  147. *****************************************************************************/
  148. void G_set_d_null_value(DCELL *dcellVals, int numVals)
  149. {
  150. static const unsigned char null_bits[8] = {
  151. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  152. int i;
  153. for (i = 0; i < numVals; i++)
  154. memcpy(&dcellVals[i], null_bits, sizeof(null_bits));
  155. }
  156. /****************************************************************************
  157. * int G_is_null_value (void *rast, RASTER_MAP_TYPE data_type)
  158. *
  159. * PURPOSE: To check if a raster value is set to NULL
  160. * INPUT VARS: rast => raster value to check
  161. * data_type => type of raster - CELL, FCELL, DCELL
  162. * RETURN VAL: TRUE if raster value is NULL FALSE otherwise
  163. *****************************************************************************/
  164. /*!
  165. * \brief
  166. *
  167. * If the <em>data_type</em> is CELL_TYPE, calls G_is_c_null_value ((CELL *)
  168. * rast);
  169. * If the <em>data_type</em> is FCELL_TYPE, calls G_is_f_null_value ((FCELL
  170. * *) rast);
  171. * If the <em>data_type</em> is DCELL_TYPE, calls G_is_d_null_value ((DCELL
  172. * *) rast);
  173. *
  174. * \param rast
  175. * \param data_type
  176. * \return int
  177. */
  178. int G_is_null_value(const void *rast, RASTER_MAP_TYPE data_type)
  179. {
  180. switch (data_type) {
  181. case CELL_TYPE:
  182. return (G_is_c_null_value((CELL *) rast));
  183. case FCELL_TYPE:
  184. return (G_is_f_null_value((FCELL *) rast));
  185. case DCELL_TYPE:
  186. return (G_is_d_null_value((DCELL *) rast));
  187. default:
  188. G_warning("G_is_null_value: wrong data type!");
  189. return FALSE;
  190. }
  191. }
  192. /****************************************************************************
  193. *
  194. * int G_is_c_null_value (CELL *cellVal)
  195. *
  196. * PURPOSE: To check if a CELL raster value is set to NULL
  197. * INPUT VARS: cellVal => CELL raster value to check
  198. * RETURN VAL: TRUE if CELL raster value is NULL FALSE otherwise
  199. *****************************************************************************/
  200. /*!
  201. * \brief
  202. *
  203. * Returns 1 if <em>cell</em> is
  204. * NULL, 0 otherwise. This will test if the value <em>cell</em> is the largest <tt>int</tt>.
  205. *
  206. * \param cell
  207. * \return int
  208. */
  209. int G_is_c_null_value(const CELL * cellVal)
  210. {
  211. /* Check if the CELL value matches the null pattern */
  212. return *cellVal == (CELL) 0x80000000;
  213. }
  214. /****************************************************************************
  215. *
  216. * int G_is_f_null_value (FCELL *fcellVal)
  217. *
  218. * PURPOSE: To check if a FCELL raster value is set to NULL
  219. * INPUT VARS: fcellVal => FCELL raster value to check
  220. * RETURN VAL: TRUE if FCELL raster value is NULL FALSE otherwise
  221. *****************************************************************************/
  222. /*!
  223. * \brief
  224. *
  225. * Returns 1 if <em>fcell</em>
  226. * is NULL, 0 otherwise. This will test if the value <em>fcell</em> is a NaN. It
  227. * isn't good enough to test for a particular NaN bit pattern since the machine
  228. * code may change this bit pattern to a different NaN. The test will be
  229. \code
  230. if(fcell==0.0) return 0;
  231. if(fcell>0.0) return 0;
  232. if(fcell<0.0) return 0;
  233. return 1;
  234. \endcode
  235. * or (as suggested by Mark Line)
  236. \code
  237. return (fcell != fcell);
  238. \endcode
  239. *
  240. * \param fcell
  241. * \return int
  242. */
  243. int G_is_f_null_value(const FCELL * fcellVal)
  244. {
  245. return *fcellVal != *fcellVal;
  246. }
  247. /****************************************************************************
  248. *
  249. * int G_is_d_null_value (DCELL *dcellVal)
  250. *
  251. * PURPOSE: To check if a DCELL raster value is set to NULL
  252. * INPUT VARS: dcellVal => DCELL raster value to check
  253. * RETURN VAL: TRUE if DCELL raster value is NULL FALSE otherwise
  254. *****************************************************************************/
  255. /*!
  256. * \brief
  257. *
  258. * Returns 1 if <em>dcell</em> is
  259. * NULL, 0 otherwise. This will test if the value <em>dcell</em> is a NaN. Same
  260. * test as in <tt>G_is_f_null_value()</tt>.
  261. *
  262. * \param dcell
  263. * \return int
  264. */
  265. int G_is_d_null_value(const DCELL * dcellVal)
  266. {
  267. return *dcellVal != *dcellVal;
  268. }
  269. /****************************************************************************
  270. *
  271. * int G_insert_null_values (void *rast, char *null_row, int ncols,
  272. * RASTER_MAP_TYPE data_type)
  273. *
  274. * PURPOSE: To insert null values into a map. Needs more.....
  275. * INPUT VARS: rast => ??
  276. * null_row => ??
  277. * ncols => ??
  278. * data_type => type of raster - CELL, FCELL, DCELL
  279. * RETURN VAL: ??
  280. *****************************************************************************/
  281. /*!
  282. * \brief Insert NULL value
  283. *
  284. * If the <em>data_type</em> is
  285. * CELL_TYPE, calls G_insert_c_null_values ((CELL *) rast, flags, count);
  286. * If the <em>data_type</em> is FCELL_TYPE, calls G_insert_f_null_values
  287. * ((FCELL *) rast, flags, count);
  288. * If the <em>data_type</em> is DCELL_TYPE, calls G_insert_d_null_values
  289. * ((DCELL *) rast, flags, count);
  290. *
  291. * \param rast
  292. * \param flags
  293. * \param count
  294. * \param data_type
  295. * \return
  296. */
  297. void G_insert_null_values(void *rast, char *null_row, int ncols,
  298. RASTER_MAP_TYPE data_type)
  299. {
  300. EmbedGivenNulls(rast, null_row, data_type, ncols);
  301. }
  302. /****************************************************************************
  303. *
  304. * int G_insert_c_null_values (CELL *cellVal, char *null_row, int ncols)
  305. *
  306. * PURPOSE: To insert null values into a CELL map. Needs more.....
  307. * INPUT VARS: cellVal => ??
  308. * null_row => ??
  309. * ncols => ??
  310. * RETURN VAL: ??
  311. *****************************************************************************/
  312. /*!
  313. * \brief Insert CELL NULL value
  314. *
  315. * For each of the <em>count</em> <em>flags</em>
  316. * which is true(!=0), set the corresponding <em>cell</em> to the NULL value.
  317. *
  318. * \param cell
  319. * \param flags
  320. * \param count
  321. * \return
  322. */
  323. void G_insert_c_null_values(CELL * cellVal, char *null_row, int ncols)
  324. {
  325. EmbedGivenNulls((void *)cellVal, null_row, CELL_TYPE, ncols);
  326. }
  327. /****************************************************************************
  328. *
  329. * int G_insert_f_null_values (FCELL *fcellVal, char *null_row, int ncols)
  330. *
  331. * PURPOSE: To insert null values into a FCELL map. Needs more.....
  332. * INPUT VARS: fcellVal => ??
  333. * null_row => ??
  334. * ncols => ??
  335. * RETURN VAL: ??
  336. *****************************************************************************/
  337. /*!
  338. * \brief Insert FCELL NULL value
  339. *
  340. * For each of the <em>count</em> <em>flags</em>
  341. * which is true(!=0), set the corresponding <em>fcell</em> to the NULL value.
  342. *
  343. * \param fcell
  344. * \param flags
  345. * \param count
  346. * \return
  347. */
  348. void G_insert_f_null_values(FCELL * fcellVal, char *null_row, int ncols)
  349. {
  350. EmbedGivenNulls((void *)fcellVal, null_row, FCELL_TYPE, ncols);
  351. }
  352. /****************************************************************************
  353. *
  354. * int G_insert_d_null_values (DCELL *dcellVal, char *null_row, int ncols)
  355. *
  356. * PURPOSE: To insert null values into a DCELL map. Needs more.....
  357. * INPUT VARS: dcellVal => ??
  358. * null_row => ??
  359. * ncols => ??
  360. * RETURN VAL: ??
  361. *****************************************************************************/
  362. /*!
  363. * \brief Insert DCELL NULL value
  364. *
  365. * For each for the <em>count</em> <em>flag</em>
  366. * which is true(!=0), set the corresponding <em>dcell</em> to the NULL value.
  367. *
  368. * \param dcell
  369. * \param flags
  370. * \param count
  371. * \return
  372. */
  373. void G_insert_d_null_values(DCELL * dcellVal, char *null_row, int ncols)
  374. {
  375. EmbedGivenNulls((void *)dcellVal, null_row, DCELL_TYPE, ncols);
  376. }
  377. /****************************************************************************
  378. * int G__check_null_bit (unsigned char *flags, int bit_num, int n)
  379. *
  380. * PURPOSE: To...
  381. * INPUT VARS: flags => ??
  382. * bit_num => ??
  383. * n => ??
  384. * RETURN VAL: ??
  385. *****************************************************************************/
  386. int G__check_null_bit(const unsigned char *flags, int bit_num, int n)
  387. {
  388. int ind;
  389. int offset;
  390. /* find the index of the unsigned char in which this bit appears */
  391. ind = G__null_bitstream_size(bit_num + 1) - 1;
  392. /* find how many unsigned chars the buffer with bit_num+1 (counting from 0
  393. has and subtract 1 to get unsigned char index */
  394. if (ind > G__null_bitstream_size(n) - 1) {
  395. G_warning
  396. ("G__check_null_bit: can't access index %d. Size of flags is %d (bit # is %d",
  397. ind, G__null_bitstream_size(n) - 1, bit_num);
  398. return -1;
  399. }
  400. offset = (ind + 1) * 8 - bit_num - 1;
  401. return ((flags[ind] & ((unsigned char)1 << offset)) != 0);
  402. }
  403. /****************************************************************************
  404. * int G__set_flags_from_01_random (char *zero_ones, unsigned char *flags,
  405. * int col, int n, int ncols)
  406. *
  407. * PURPOSE: given array of 0/1 of length n starting from column col
  408. * set the corresponding bits of flags; total number of bits
  409. * in flags is ncols
  410. * INPUT VARS: zero_ones => ??
  411. * flags => ??
  412. * col => ??
  413. * n => ??
  414. * ncols => ??
  415. * RETURN VAL: ??
  416. *****************************************************************************/
  417. int G__set_flags_from_01_random(const char *zero_ones, unsigned char *flags,
  418. int col, int n, int ncols)
  419. {
  420. unsigned char v;
  421. int count;
  422. int size;
  423. int i, k;
  424. if (col == 0 && n == ncols) {
  425. G__convert_01_flags(zero_ones, flags, n);
  426. return 0;
  427. }
  428. count = 0;
  429. size = G__null_bitstream_size(ncols);
  430. for (i = 0; i < size; i++) {
  431. v = 0;
  432. k = 8;
  433. while (k-- > 0) {
  434. if (count >= col && count < (col + n)) {
  435. v = v | ((unsigned char)zero_ones[count - col] << k);
  436. }
  437. else if (count < ncols) {
  438. v = v |
  439. ((unsigned char)G__check_null_bit(flags, count, ncols) <<
  440. k);
  441. }
  442. /* otherwise keep this bit the same as it was */
  443. count++;
  444. }
  445. flags[i] = v;
  446. }
  447. return 1;
  448. }
  449. /****************************************************************************
  450. * int G__convert_01_flags (char *zero_ones, unsigned char *flags, int n)
  451. *
  452. * PURPOSE: To...
  453. * INPUT VARS: zero_ones => ??
  454. * flags => ??
  455. * n => ??
  456. * RETURN VAL: ??
  457. *****************************************************************************/
  458. void G__convert_01_flags(const char *zero_ones, unsigned char *flags, int n)
  459. {
  460. unsigned char *v;
  461. int count;
  462. int size;
  463. int i, k;
  464. /* pad the flags with 0's to make size multiple of 8 */
  465. v = flags;
  466. size = G__null_bitstream_size(n);
  467. count = 0;
  468. for (i = 0; i < size; i++) {
  469. *v = 0;
  470. k = 8;
  471. while (k-- > 0) {
  472. if (count < n) {
  473. *v = *v | ((unsigned char)zero_ones[count] << k);
  474. }
  475. count++;
  476. }
  477. v++;
  478. }
  479. }
  480. /****************************************************************************
  481. * int G__convert_flags_01 (char *zero_ones, unsigned char *flags, int n)
  482. *
  483. * PURPOSE: To...
  484. * INPUT VARS: zero_ones => ??
  485. * flags => ??
  486. * n => ??
  487. * RETURN VAL: ??
  488. *****************************************************************************/
  489. void G__convert_flags_01(char *zero_ones, const unsigned char *flags, int n)
  490. {
  491. const unsigned char *v;
  492. int count;
  493. int size;
  494. int i, k;
  495. count = 0;
  496. v = flags;
  497. size = G__null_bitstream_size(n);
  498. for (i = 0; i < size; i++) {
  499. k = 8;
  500. while (k-- > 0) {
  501. if (count < n) {
  502. zero_ones[count] = ((*v & ((unsigned char)1 << k)) != 0);
  503. count++;
  504. }
  505. }
  506. v++;
  507. }
  508. }
  509. /****************************************************************************
  510. * int G__init_null_bits (unsigned char *flags, int cols)
  511. *
  512. * PURPOSE: To...
  513. * INPUT VARS: flags => ??
  514. * cols => ??
  515. * RETURN VAL: ??
  516. *****************************************************************************/
  517. void G__init_null_bits(unsigned char *flags, int cols)
  518. {
  519. unsigned char *v;
  520. int size;
  521. int i;
  522. /* pad the flags with 0's to make size multiple of 8 */
  523. v = flags;
  524. size = G__null_bitstream_size(cols);
  525. for (i = 0; i < size; i++) {
  526. if ((i + 1) * 8 <= cols) {
  527. *v = (unsigned char)255;
  528. }
  529. else {
  530. *v = (unsigned char)255 << ((i + 1) * 8 - cols);
  531. }
  532. v++;
  533. }
  534. }