1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
|
/*
*
*****************************************************************************
*
* MODULE: GRASS gis library
* AUTHOR(S): Original author unknown - probably CERL
* Justin Hickey - Thailand - jhickey@hpcc.nectec.or.th
* PURPOSE: To provide functionality to handle NULL values for data types
* CELL, FCELL, and DCELL. May need more...
* COPYRIGHT: (C) 2000 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
*****************************************************************************/
/*============================= Include Files ==============================*/
/* System include files */
#include <string.h>
/* Grass and local include files */
#include "gis.h"
#include "glocale.h"
/*======================= Internal Constants/Defines =======================*/
/* none */
/*========================== Internal Typedefs =============================*/
/* none */
/*====================== Static Variable Declaration =======================*/
/* Null pattern variables */
static CELL cellNullPattern;
static FCELL fcellNullPattern;
static DCELL dcellNullPattern;
/* Flag to indicate null patterns are initialized */
static int initialized = FALSE;
/*============================== Prototypes ================================*/
static int EmbedGivenNulls (void *, char *, RASTER_MAP_TYPE, int);
static void InitError (void);
/*======================= Internal Static Functions ========================*/
/****************************************************************************
* int EmbedGivenNulls (void *cell, char *nulls, RASTER_MAP_TYPE map_type,
* int ncols)
*
* PURPOSE: To insert null values into a map. Needs more.....
* INPUT VARS: cell => ??
* nulls => ??
* map_type => type of raster - CELL, FCELL, DCELL
* ncols => ??
* RETURN VAL: ??
*****************************************************************************/
static int EmbedGivenNulls (void *cell, char *nulls, RASTER_MAP_TYPE map_type,
int ncols)
{
CELL *c;
FCELL *f;
DCELL *d;
int i;
c = (CELL *) cell;
f = (FCELL *) cell;
d = (DCELL *) cell;
for(i = 0; i < ncols; i++)
{
if(nulls[i])
{
switch (map_type)
{
case CELL_TYPE:
G_set_c_null_value((CELL *) (c + i), 1);
break;
case FCELL_TYPE:
G_set_f_null_value((FCELL *) (f + i), 1);
break;
case DCELL_TYPE:
G_set_d_null_value((DCELL *) (d + i), 1);
break;
default:
G_warning(_("EmbedGivenNulls: wrong data type!"));
}
}
}
return 1;
}
/****************************************************************************
* void InitError (void)
*
* PURPOSE: To print an error message and exit the program. This function
* is called if something tries to access a null pattern before
* it is initialized.
* INPUT VARS: none
* RETURN VAL: none
*****************************************************************************/
static void InitError (void)
{
char errMsg[512]; /* array to hold error message */
strcpy(errMsg, _("Null values have not been initialized. "));
strcat(errMsg, _("G_gisinit() must be called first. "));
strcat(errMsg, _("Please advise GRASS developers of this error.\n"));
G_fatal_error(errMsg);
return;
}
/*========================== Library Functions =============================*/
/****************************************************************************
* void G__init_null_patterns (void)
*
* PURPOSE: To initialize the three null patterns for CELL, FCELL, and
* DCELL data types. It also sets the initialized flag to TRUE.
* This function is called by G_gisinit()
* INPUT VARS: none
* RETURN VAL: none
*****************************************************************************/
void G__init_null_patterns (void)
{
unsigned char *bytePtr; /* pointer to traverse FCELL and DCELL */
int numBits; /* number of bits for CELL type */
int i; /* counter */
if (!initialized)
{
/* Create the null pattern for the CELL data type - set the left */
/* most bit to 1 and the rest to 0, basically INT_MIN. Since CELL is */
/* some type of integer the bytes are not split into exponent and */
/* mantissa. Thus a simple left shift can be used */
numBits = sizeof(CELL) * 8;
cellNullPattern = 1 << (numBits - 1);
/* Create the null pattern for the FCELL data type - set all bits */
/* to 1, basically NaN. Need to use a byte pointer since bytes */
/* represent the exponent and mantissa */
bytePtr = (unsigned char *) &fcellNullPattern;
for (i = 0; i < sizeof(FCELL); i++)
{
*bytePtr = (unsigned char) 255;
bytePtr++;
}
/* Create the null pattern for the DCELL data type - set all bits */
/* to 1, basically NaN. Need to use a byte pointer since bytes */
/* represent the exponent and mantissa */
bytePtr = (unsigned char *) &dcellNullPattern;
for (i = 0; i < sizeof(DCELL); i++)
{
*bytePtr = (unsigned char) 255;
bytePtr++;
}
/* Set the initialized flag to TRUE */
initialized = TRUE;
}
return;
}
/****************************************************************************
* void G__set_null_value (void *rast, int numVals, int null_is_zero,
* RASTER_MAP_TYPE data_type)
*
* PURPOSE: To set one or more raster values to null. It also sets null
* to zero if null_is_zero is TRUE.
* INPUT VARS: rast => pointer to values to set to null
* numVals => number of values to set to null
* null_is_zero => flag to indicate if NULL = 0
* data_type => type of raster - CELL, FCELL, DCELL
* RETURN VAL: none
*****************************************************************************/
void G__set_null_value (void *rast, int numVals, int null_is_zero,
RASTER_MAP_TYPE data_type)
{
if (null_is_zero)
{
G_zero((char *) rast, numVals * G_raster_size(data_type));
return;
}
G_set_null_value(rast, numVals, data_type);
return;
}
/****************************************************************************
* void G_set_null_value (void *buf, int numVals, RASTER_MAP_TYPE data_type)
*
* PURPOSE: To set one or more raster values to null.
* INPUT VARS: rast => pointer to values to set to null
* numVals => number of values to set to null
* data_type => type of raster - CELL, FCELL, DCELL
* RETURN VAL: none
*****************************************************************************/
void G_set_null_value (void *buf, int numVals, RASTER_MAP_TYPE data_type)
{
switch (data_type)
{
case CELL_TYPE:
G_set_c_null_value((CELL *) buf, numVals);
break;
case FCELL_TYPE:
G_set_f_null_value((FCELL *) buf, numVals);
break;
case DCELL_TYPE:
G_set_d_null_value((DCELL *) buf, numVals);
break;
default:
G_warning(_("G_set_null_value: wrong data type!"));
}
return;
}
/****************************************************************************
* void G_set_c_null_value (CELL *cellVals, int numVals)
*
* PURPOSE: To set a number of CELL raster values to NULL.
* INPUT VARS: cellVals => pointer to CELL values to set to null
* numVals => number of values to set to null
* RETURN VAL: none
*****************************************************************************/
void G_set_c_null_value (CELL *cellVals, int numVals)
{
CELL *cellPtr; /* pointer to CELL array to set to null */
int i; /* counter */
/* Check if the null patterns have been initialized */
if (!initialized)
{
InitError();
}
/* Set numVals consecutive CELL values to null */
cellPtr = cellVals;
for (i = 0; i < numVals; i++)
{
*cellPtr = cellNullPattern;
cellPtr++;
}
return;
}
/****************************************************************************
* void G_set_f_null_value (FCELL *fcellVals, int numVals)
*
* PURPOSE: To set a number of FCELL raster values to NULL.
* INPUT VARS: fcellVals => pointer to FCELL values to set to null
* numVals => number of values to set to null
* RETURN VAL: none
*****************************************************************************/
void G_set_f_null_value (FCELL *fcellVals, int numVals)
{
FCELL *fcellPtr; /* pointer to FCELL array to set to null */
int i; /* counter */
/* Check if the null patterns have been initialized */
if (!initialized)
{
InitError();
}
/* Set numVals consecutive FCELL values to null */
fcellPtr = fcellVals;
for (i = 0; i < numVals; i++)
{
*fcellPtr = fcellNullPattern;
fcellPtr++;
}
return;
}
/****************************************************************************
* void G_set_d_null_value (DCELL *dcellVals, int numVals)
*
* PURPOSE: To set a number of DCELL raster values to NULL.
* INPUT VARS: dcellVals => pointer to DCELL values to set to null
* numVals => number of values to set to null
* RETURN VAL: none
*****************************************************************************/
void G_set_d_null_value (DCELL *dcellVals, int numVals)
{
DCELL *dcellPtr; /* pointer to DCELL array to set to null */
int i; /* counter */
/* Check if the null patterns have been initialized */
if (!initialized)
{
InitError();
}
/* Set numVals consecutive DCELL values to null */
dcellPtr = dcellVals;
for (i = 0; i < numVals; i++)
{
*dcellPtr = dcellNullPattern;
dcellPtr++;
}
return;
}
/****************************************************************************
* int G_is_null_value (void *rast, RASTER_MAP_TYPE data_type)
*
* PURPOSE: To check if a raster value is set to NULL
* INPUT VARS: rast => raster value to check
* data_type => type of raster - CELL, FCELL, DCELL
* RETURN VAL: TRUE if raster value is NULL FALSE otherwise
*****************************************************************************/
/*!
* \brief
*
* If the <em>data_type</em> is CELL_TYPE, calls G_is_c_null_value ((CELL *)
* rast);
* If the <em>data_type</em> is FCELL_TYPE, calls G_is_f_null_value ((FCELL
* *) rast);
* If the <em>data_type</em> is DCELL_TYPE, calls G_is_d_null_value ((DCELL
* *) rast);
*
* \param rast
* \param data_type
* \return int
*/
int G_is_null_value (const void *rast, RASTER_MAP_TYPE data_type)
{
switch(data_type)
{
case CELL_TYPE:
return (G_is_c_null_value((CELL *) rast));
case FCELL_TYPE:
return (G_is_f_null_value((FCELL *) rast));
case DCELL_TYPE:
return (G_is_d_null_value((DCELL *) rast));
default:
G_warning("G_is_null_value: wrong data type!");
return FALSE;
}
}
/****************************************************************************
*
* int G_is_c_null_value (CELL *cellVal)
*
* PURPOSE: To check if a CELL raster value is set to NULL
* INPUT VARS: cellVal => CELL raster value to check
* RETURN VAL: TRUE if CELL raster value is NULL FALSE otherwise
*****************************************************************************/
/*!
* \brief
*
* Returns 1 if <em>cell</em> is
* NULL, 0 otherwise. This will test if the value <em>cell</em> is the largest <tt>int</tt>.
*
* \param cell
* \return int
*/
int G_is_c_null_value (const CELL *cellVal)
{
int i; /* counter */
/* Check if the null patterns have been initialized */
if (!initialized)
{
InitError();
}
/* Check if the CELL value matches the null pattern */
for(i = 0; i < sizeof (CELL); i++)
{
if(((unsigned char *) cellVal)[i] !=
((unsigned char *) &cellNullPattern)[i])
{
return FALSE;
}
}
return TRUE;
}
/****************************************************************************
*
* int G_is_f_null_value (FCELL *fcellVal)
*
* PURPOSE: To check if a FCELL raster value is set to NULL
* INPUT VARS: fcellVal => FCELL raster value to check
* RETURN VAL: TRUE if FCELL raster value is NULL FALSE otherwise
*****************************************************************************/
/*!
* \brief
*
* Returns 1 if <em>fcell</em>
* is NULL, 0 otherwise. This will test if the value <em>fcell</em> is a NaN. It
* isn't good enough to test for a particular NaN bit pattern since the machine
* code may change this bit pattern to a different NaN. The test will be
\code
if(fcell==0.0) return 0;
if(fcell>0.0) return 0;
if(fcell<0.0) return 0;
return 1;
\endcode
* or (as suggested by Mark Line)
\code
return (fcell != fcell);
\endcode
*
* \param fcell
* \return int
*/
int G_is_f_null_value (const FCELL *fcellVal)
{
int i; /* counter */
/* Check if the null patterns have been initialized */
if (!initialized)
{
InitError();
}
/* Check if the FCELL value matches the null pattern */
for(i = 0; i < sizeof (FCELL); i++)
{
if(((unsigned char *) fcellVal)[i] !=
((unsigned char *) &fcellNullPattern)[i])
{
return FALSE;
}
}
return TRUE;
}
/****************************************************************************
*
* int G_is_d_null_value (DCELL *dcellVal)
*
* PURPOSE: To check if a DCELL raster value is set to NULL
* INPUT VARS: dcellVal => DCELL raster value to check
* RETURN VAL: TRUE if DCELL raster value is NULL FALSE otherwise
*****************************************************************************/
/*!
* \brief
*
* Returns 1 if <em>dcell</em> is
* NULL, 0 otherwise. This will test if the value <em>dcell</em> is a NaN. Same
* test as in <tt>G_is_f_null_value()</tt>.
*
* \param dcell
* \return int
*/
int G_is_d_null_value (const DCELL *dcellVal)
{
int i; /* counter */
/* Check if the null patterns have been initialized */
if (!initialized)
{
InitError();
}
/* Check if the DCELL value matches the null pattern */
for(i = 0; i < sizeof (DCELL); i++)
{
if(((unsigned char *) dcellVal)[i] !=
((unsigned char *) &dcellNullPattern)[i])
{
return FALSE;
}
}
return TRUE;
}
/****************************************************************************
*
* int G_insert_null_values (void *rast, char *null_row, int ncols,
* RASTER_MAP_TYPE data_type)
*
* PURPOSE: To insert null values into a map. Needs more.....
* INPUT VARS: rast => ??
* null_row => ??
* ncols => ??
* data_type => type of raster - CELL, FCELL, DCELL
* RETURN VAL: ??
*****************************************************************************/
/*!
* \brief Insert NULL value
*
* If the <em>data_type</em> is
* CELL_TYPE, calls G_insert_c_null_values ((CELL *) rast, flags, count);
* If the <em>data_type</em> is FCELL_TYPE, calls G_insert_f_null_values
* ((FCELL *) rast, flags, count);
* If the <em>data_type</em> is DCELL_TYPE, calls G_insert_d_null_values
* ((DCELL *) rast, flags, count);
*
* \param rast
* \param flags
* \param count
* \param data_type
* \return int
*/
int G_insert_null_values (void *rast, char *null_row, int ncols,
RASTER_MAP_TYPE data_type)
{
return (EmbedGivenNulls(rast, null_row, data_type, ncols));
}
/****************************************************************************
*
* int G_insert_c_null_values (CELL *cellVal, char *null_row, int ncols)
*
* PURPOSE: To insert null values into a CELL map. Needs more.....
* INPUT VARS: cellVal => ??
* null_row => ??
* ncols => ??
* RETURN VAL: ??
*****************************************************************************/
/*!
* \brief Insert CELL NULL value
*
* For each of the <em>count</em> <em>flags</em>
* which is true(!=0), set the corresponding <em>cell</em> to the NULL value.
*
* \param cell
* \param flags
* \param count
* \return int
*/
int G_insert_c_null_values (CELL *cellVal, char *null_row, int ncols)
{
return (EmbedGivenNulls((void *) cellVal, null_row, CELL_TYPE, ncols));
}
/****************************************************************************
*
* int G_insert_f_null_values (FCELL *fcellVal, char *null_row, int ncols)
*
* PURPOSE: To insert null values into a FCELL map. Needs more.....
* INPUT VARS: fcellVal => ??
* null_row => ??
* ncols => ??
* RETURN VAL: ??
*****************************************************************************/
/*!
* \brief Insert FCELL NULL value
*
* For each of the <em>count</em> <em>flags</em>
* which is true(!=0), set the corresponding <em>fcell</em> to the NULL value.
*
* \param fcell
* \param flags
* \param count
* \return int
*/
int G_insert_f_null_values (FCELL *fcellVal, char *null_row, int ncols)
{
return (EmbedGivenNulls((void *) fcellVal, null_row, FCELL_TYPE, ncols));
}
/****************************************************************************
*
* int G_insert_d_null_values (DCELL *dcellVal, char *null_row, int ncols)
*
* PURPOSE: To insert null values into a DCELL map. Needs more.....
* INPUT VARS: dcellVal => ??
* null_row => ??
* ncols => ??
* RETURN VAL: ??
*****************************************************************************/
/*!
* \brief Insert DCELL NULL value
*
* For each for the <em>count</em> <em>flag</em>
* which is true(!=0), set the corresponding <em>dcell</em> to the NULL value.
*
* \param dcell
* \param flags
* \param count
* \return int
*/
int G_insert_d_null_values (DCELL *dcellVal, char *null_row, int ncols)
{
return (EmbedGivenNulls((void *) dcellVal, null_row, DCELL_TYPE, ncols));
}
/****************************************************************************
* int G__check_null_bit (unsigned char *flags, int bit_num, int n)
*
* PURPOSE: To...
* INPUT VARS: flags => ??
* bit_num => ??
* n => ??
* RETURN VAL: ??
*****************************************************************************/
int G__check_null_bit (unsigned char *flags, int bit_num, int n)
{
int ind;
int offset;
/* find the index of the unsigned char in which this bit appears */
ind = G__null_bitstream_size(bit_num + 1) - 1;
/* find how many unsigned chars the buffer with bit_num+1 (counting from 0
has and subtract 1 to get unsigned char index */
if (ind > G__null_bitstream_size(n) - 1 )
{
G_warning("G__check_null_bit: can't access index %d. Size of flags is %d (bit # is %d", ind, G__null_bitstream_size(n) - 1, bit_num);
return -1;
}
offset = (ind+1)*8 - bit_num - 1;
return ((flags[ind] & ( (unsigned char) 1 << offset)) != 0);
}
/****************************************************************************
* int G__set_flags_from_01_random (char *zero_ones, unsigned char *flags,
* int col, int n, int ncols)
*
* PURPOSE: given array of 0/1 of length n starting from column col
* set the corresponding bits of flags; total number of bits
* in flags is ncols
* INPUT VARS: zero_ones => ??
* flags => ??
* col => ??
* n => ??
* ncols => ??
* RETURN VAL: ??
*****************************************************************************/
int G__set_flags_from_01_random (char *zero_ones, unsigned char *flags,
int col, int n, int ncols)
{
unsigned char v;
int count;
int size;
int i, k;
if (col==0 && n == ncols)
{
G__convert_01_flags(zero_ones, flags, n);
return 0;
}
count = 0;
size = G__null_bitstream_size(ncols);
for (i = 0; i < size; i++)
{
v = 0;
k = 8;
while (k-- > 0)
{
if (count >= col && count < (col+ n))
{
v = v | ((unsigned char) zero_ones[count - col] << k);
}
else if(count < ncols)
{
v = v |
((unsigned char) G__check_null_bit(flags, count, ncols) << k);
}
/* otherwise keep this bit the same as it was */
count++;
}
flags[i] = v;
}
return 1;
}
/****************************************************************************
* int G__convert_01_flags (char *zero_ones, unsigned char *flags, int n)
*
* PURPOSE: To...
* INPUT VARS: zero_ones => ??
* flags => ??
* n => ??
* RETURN VAL: ??
*****************************************************************************/
int G__convert_01_flags (char *zero_ones, unsigned char *flags, int n)
{
unsigned char *v;
int count;
int size;
int i, k;
/* pad the flags with 0's to make size multiple of 8 */
v = flags;
size = G__null_bitstream_size(n);
count = 0;
for (i = 0; i < size; i++)
{
*v = 0;
k = 8;
while (k-- > 0)
{
if (count < n)
{
*v = *v | ((unsigned char) zero_ones[count] << k);
}
count++;
}
v++;
}
return 0;
}
/****************************************************************************
* int G__convert_flags_01 (char *zero_ones, unsigned char *flags, int n)
*
* PURPOSE: To...
* INPUT VARS: zero_ones => ??
* flags => ??
* n => ??
* RETURN VAL: ??
*****************************************************************************/
int G__convert_flags_01 (char *zero_ones, unsigned char *flags, int n)
{
unsigned char *v;
int count;
int size;
int i, k;
count = 0;
v = flags;
size = G__null_bitstream_size(n);
for (i = 0; i < size; i++)
{
k = 8;
while (k-- > 0)
{
if (count < n)
{
zero_ones[count] = ((*v & ( (unsigned char) 1 << k)) != 0);
count++;
}
}
v++;
}
return 0;
}
/****************************************************************************
* int G__init_null_bits (unsigned char *flags, int cols)
*
* PURPOSE: To...
* INPUT VARS: flags => ??
* cols => ??
* RETURN VAL: ??
*****************************************************************************/
int G__init_null_bits (unsigned char *flags, int cols)
{
unsigned char *v;
int size;
int i;
/* pad the flags with 0's to make size multiple of 8 */
v = flags;
size = G__null_bitstream_size(cols);
for (i = 0; i < size; i++)
{
if((i+1) * 8 <= cols)
{
*v = (unsigned char) 255;
}
else
{
*v = (unsigned char) 255 << ((i+1) * 8 - cols);
}
v++;
}
return 0;
}
|