null_val.c 18 KB

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