null_val.c 17 KB

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