null_val.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. int i; /* counter */
  212. /* Check if the CELL value matches the null pattern */
  213. for (i = 0; i < sizeof(CELL); i++)
  214. if (cellVal[i] != (CELL) 0x80000000)
  215. return FALSE;
  216. return TRUE;
  217. }
  218. /****************************************************************************
  219. *
  220. * int G_is_f_null_value (FCELL *fcellVal)
  221. *
  222. * PURPOSE: To check if a FCELL raster value is set to NULL
  223. * INPUT VARS: fcellVal => FCELL raster value to check
  224. * RETURN VAL: TRUE if FCELL raster value is NULL FALSE otherwise
  225. *****************************************************************************/
  226. /*!
  227. * \brief
  228. *
  229. * Returns 1 if <em>fcell</em>
  230. * is NULL, 0 otherwise. This will test if the value <em>fcell</em> is a NaN. It
  231. * isn't good enough to test for a particular NaN bit pattern since the machine
  232. * code may change this bit pattern to a different NaN. The test will be
  233. \code
  234. if(fcell==0.0) return 0;
  235. if(fcell>0.0) return 0;
  236. if(fcell<0.0) return 0;
  237. return 1;
  238. \endcode
  239. * or (as suggested by Mark Line)
  240. \code
  241. return (fcell != fcell);
  242. \endcode
  243. *
  244. * \param fcell
  245. * \return int
  246. */
  247. int G_is_f_null_value(const FCELL * fcellVal)
  248. {
  249. return *fcellVal != *fcellVal;
  250. }
  251. /****************************************************************************
  252. *
  253. * int G_is_d_null_value (DCELL *dcellVal)
  254. *
  255. * PURPOSE: To check if a DCELL raster value is set to NULL
  256. * INPUT VARS: dcellVal => DCELL raster value to check
  257. * RETURN VAL: TRUE if DCELL raster value is NULL FALSE otherwise
  258. *****************************************************************************/
  259. /*!
  260. * \brief
  261. *
  262. * Returns 1 if <em>dcell</em> is
  263. * NULL, 0 otherwise. This will test if the value <em>dcell</em> is a NaN. Same
  264. * test as in <tt>G_is_f_null_value()</tt>.
  265. *
  266. * \param dcell
  267. * \return int
  268. */
  269. int G_is_d_null_value(const DCELL * dcellVal)
  270. {
  271. return *dcellVal != *dcellVal;
  272. }
  273. /****************************************************************************
  274. *
  275. * int G_insert_null_values (void *rast, char *null_row, int ncols,
  276. * RASTER_MAP_TYPE data_type)
  277. *
  278. * PURPOSE: To insert null values into a map. Needs more.....
  279. * INPUT VARS: rast => ??
  280. * null_row => ??
  281. * ncols => ??
  282. * data_type => type of raster - CELL, FCELL, DCELL
  283. * RETURN VAL: ??
  284. *****************************************************************************/
  285. /*!
  286. * \brief Insert NULL value
  287. *
  288. * If the <em>data_type</em> is
  289. * CELL_TYPE, calls G_insert_c_null_values ((CELL *) rast, flags, count);
  290. * If the <em>data_type</em> is FCELL_TYPE, calls G_insert_f_null_values
  291. * ((FCELL *) rast, flags, count);
  292. * If the <em>data_type</em> is DCELL_TYPE, calls G_insert_d_null_values
  293. * ((DCELL *) rast, flags, count);
  294. *
  295. * \param rast
  296. * \param flags
  297. * \param count
  298. * \param data_type
  299. * \return
  300. */
  301. void G_insert_null_values(void *rast, char *null_row, int ncols,
  302. RASTER_MAP_TYPE data_type)
  303. {
  304. EmbedGivenNulls(rast, null_row, data_type, ncols);
  305. }
  306. /****************************************************************************
  307. *
  308. * int G_insert_c_null_values (CELL *cellVal, char *null_row, int ncols)
  309. *
  310. * PURPOSE: To insert null values into a CELL map. Needs more.....
  311. * INPUT VARS: cellVal => ??
  312. * null_row => ??
  313. * ncols => ??
  314. * RETURN VAL: ??
  315. *****************************************************************************/
  316. /*!
  317. * \brief Insert CELL NULL value
  318. *
  319. * For each of the <em>count</em> <em>flags</em>
  320. * which is true(!=0), set the corresponding <em>cell</em> to the NULL value.
  321. *
  322. * \param cell
  323. * \param flags
  324. * \param count
  325. * \return
  326. */
  327. void G_insert_c_null_values(CELL * cellVal, char *null_row, int ncols)
  328. {
  329. EmbedGivenNulls((void *)cellVal, null_row, CELL_TYPE, ncols);
  330. }
  331. /****************************************************************************
  332. *
  333. * int G_insert_f_null_values (FCELL *fcellVal, char *null_row, int ncols)
  334. *
  335. * PURPOSE: To insert null values into a FCELL map. Needs more.....
  336. * INPUT VARS: fcellVal => ??
  337. * null_row => ??
  338. * ncols => ??
  339. * RETURN VAL: ??
  340. *****************************************************************************/
  341. /*!
  342. * \brief Insert FCELL NULL value
  343. *
  344. * For each of the <em>count</em> <em>flags</em>
  345. * which is true(!=0), set the corresponding <em>fcell</em> to the NULL value.
  346. *
  347. * \param fcell
  348. * \param flags
  349. * \param count
  350. * \return
  351. */
  352. void G_insert_f_null_values(FCELL * fcellVal, char *null_row, int ncols)
  353. {
  354. EmbedGivenNulls((void *)fcellVal, null_row, FCELL_TYPE, ncols);
  355. }
  356. /****************************************************************************
  357. *
  358. * int G_insert_d_null_values (DCELL *dcellVal, char *null_row, int ncols)
  359. *
  360. * PURPOSE: To insert null values into a DCELL map. Needs more.....
  361. * INPUT VARS: dcellVal => ??
  362. * null_row => ??
  363. * ncols => ??
  364. * RETURN VAL: ??
  365. *****************************************************************************/
  366. /*!
  367. * \brief Insert DCELL NULL value
  368. *
  369. * For each for the <em>count</em> <em>flag</em>
  370. * which is true(!=0), set the corresponding <em>dcell</em> to the NULL value.
  371. *
  372. * \param dcell
  373. * \param flags
  374. * \param count
  375. * \return
  376. */
  377. void G_insert_d_null_values(DCELL * dcellVal, char *null_row, int ncols)
  378. {
  379. EmbedGivenNulls((void *)dcellVal, null_row, DCELL_TYPE, ncols);
  380. }
  381. /****************************************************************************
  382. * int G__check_null_bit (unsigned char *flags, int bit_num, int n)
  383. *
  384. * PURPOSE: To...
  385. * INPUT VARS: flags => ??
  386. * bit_num => ??
  387. * n => ??
  388. * RETURN VAL: ??
  389. *****************************************************************************/
  390. int G__check_null_bit(const unsigned char *flags, int bit_num, int n)
  391. {
  392. int ind;
  393. int offset;
  394. /* find the index of the unsigned char in which this bit appears */
  395. ind = G__null_bitstream_size(bit_num + 1) - 1;
  396. /* find how many unsigned chars the buffer with bit_num+1 (counting from 0
  397. has and subtract 1 to get unsigned char index */
  398. if (ind > G__null_bitstream_size(n) - 1) {
  399. G_warning
  400. ("G__check_null_bit: can't access index %d. Size of flags is %d (bit # is %d",
  401. ind, G__null_bitstream_size(n) - 1, bit_num);
  402. return -1;
  403. }
  404. offset = (ind + 1) * 8 - bit_num - 1;
  405. return ((flags[ind] & ((unsigned char)1 << offset)) != 0);
  406. }
  407. /****************************************************************************
  408. * int G__set_flags_from_01_random (char *zero_ones, unsigned char *flags,
  409. * int col, int n, int ncols)
  410. *
  411. * PURPOSE: given array of 0/1 of length n starting from column col
  412. * set the corresponding bits of flags; total number of bits
  413. * in flags is ncols
  414. * INPUT VARS: zero_ones => ??
  415. * flags => ??
  416. * col => ??
  417. * n => ??
  418. * ncols => ??
  419. * RETURN VAL: ??
  420. *****************************************************************************/
  421. int G__set_flags_from_01_random(const char *zero_ones, unsigned char *flags,
  422. int col, int n, int ncols)
  423. {
  424. unsigned char v;
  425. int count;
  426. int size;
  427. int i, k;
  428. if (col == 0 && n == ncols) {
  429. G__convert_01_flags(zero_ones, flags, n);
  430. return 0;
  431. }
  432. count = 0;
  433. size = G__null_bitstream_size(ncols);
  434. for (i = 0; i < size; i++) {
  435. v = 0;
  436. k = 8;
  437. while (k-- > 0) {
  438. if (count >= col && count < (col + n)) {
  439. v = v | ((unsigned char)zero_ones[count - col] << k);
  440. }
  441. else if (count < ncols) {
  442. v = v |
  443. ((unsigned char)G__check_null_bit(flags, count, ncols) <<
  444. k);
  445. }
  446. /* otherwise keep this bit the same as it was */
  447. count++;
  448. }
  449. flags[i] = v;
  450. }
  451. return 1;
  452. }
  453. /****************************************************************************
  454. * int G__convert_01_flags (char *zero_ones, unsigned char *flags, int n)
  455. *
  456. * PURPOSE: To...
  457. * INPUT VARS: zero_ones => ??
  458. * flags => ??
  459. * n => ??
  460. * RETURN VAL: ??
  461. *****************************************************************************/
  462. void G__convert_01_flags(const char *zero_ones, unsigned char *flags, int n)
  463. {
  464. unsigned char *v;
  465. int count;
  466. int size;
  467. int i, k;
  468. /* pad the flags with 0's to make size multiple of 8 */
  469. v = flags;
  470. size = G__null_bitstream_size(n);
  471. count = 0;
  472. for (i = 0; i < size; i++) {
  473. *v = 0;
  474. k = 8;
  475. while (k-- > 0) {
  476. if (count < n) {
  477. *v = *v | ((unsigned char)zero_ones[count] << k);
  478. }
  479. count++;
  480. }
  481. v++;
  482. }
  483. }
  484. /****************************************************************************
  485. * int G__convert_flags_01 (char *zero_ones, unsigned char *flags, int n)
  486. *
  487. * PURPOSE: To...
  488. * INPUT VARS: zero_ones => ??
  489. * flags => ??
  490. * n => ??
  491. * RETURN VAL: ??
  492. *****************************************************************************/
  493. void G__convert_flags_01(char *zero_ones, const unsigned char *flags, int n)
  494. {
  495. const unsigned char *v;
  496. int count;
  497. int size;
  498. int i, k;
  499. count = 0;
  500. v = flags;
  501. size = G__null_bitstream_size(n);
  502. for (i = 0; i < size; i++) {
  503. k = 8;
  504. while (k-- > 0) {
  505. if (count < n) {
  506. zero_ones[count] = ((*v & ((unsigned char)1 << k)) != 0);
  507. count++;
  508. }
  509. }
  510. v++;
  511. }
  512. }
  513. /****************************************************************************
  514. * int G__init_null_bits (unsigned char *flags, int cols)
  515. *
  516. * PURPOSE: To...
  517. * INPUT VARS: flags => ??
  518. * cols => ??
  519. * RETURN VAL: ??
  520. *****************************************************************************/
  521. void G__init_null_bits(unsigned char *flags, int cols)
  522. {
  523. unsigned char *v;
  524. int size;
  525. int i;
  526. /* pad the flags with 0's to make size multiple of 8 */
  527. v = flags;
  528. size = G__null_bitstream_size(cols);
  529. for (i = 0; i < size; i++) {
  530. if ((i + 1) * 8 <= cols) {
  531. *v = (unsigned char)255;
  532. }
  533. else {
  534. *v = (unsigned char)255 << ((i + 1) * 8 - cols);
  535. }
  536. v++;
  537. }
  538. }