null_val.c 17 KB

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