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 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
|
/**********************************************************************
*
* G_quant_init (quant)
* struct Quant *quant;
*
* initializes new quantization structure. calls
* G_quant_clear() before it returns.
*
* note: dies if G_malloc dies.
*
**********************************************************************
*
* G_quant_is_truncate (quant)
* struct Quant *quant;
*
* Returns wether or not quant rules are set to truncate map
*
**********************************************************************
*
* G_quant_is_round (quant)
* struct Quant *quant;
*
* Returns wether or not quant rules are set to round map
*
**********************************************************************
*
* G_quant_truncate (quant)
* struct Quant *quant;
*
* sets the quant rules to perform simple truncation on floats.
*
**********************************************************************
*
* G_quant_round (quant)
* struct Quant *quant;
*
* sets the quant rules to perform simple rounding on floats.
*
**********************************************************************
*
* G_quant_organize_fp_lookup (quant)
* struct Quant *quant;
*
* Organizes fp_lookup table for faster (logarithmic) lookup time
* G_quant_organize_fp_lookup() creates a list of min and max for
* each quant rule, sorts this list, and stores the pointer to quant
* rule that should be used inbetween any 2 numbers in this list.
* Also it stores extreme points for 2 infinite rules, if exist.
* After the call to G_quant_organize_fp_lookup()
* instead of linearly searching through list of rules to find
* a rule to apply, quant lookup will perform a binary search
* to find an interval containing floating point value, and then use
* the rule associated with this interval.
* when the value doesn't fall within any interval, check for the
* infinite rules.
*
**********************************************************************
*
* void
* G_quant_free (q)
*
* struct Quant *q;
*
* resets the number of defined rules to 0 and free's space allocated
* for rules. calls G_quant_clear ().
*
**********************************************************************
*
* void
* G_quant_clear (q)
*
* struct Quant *q;
*
* resets the number of defined rules and number of infinite rules to 0.
*
**********************************************************************
*
* int
* G_quant_get_limits (q, dMin, dMax, cMin, cMax)
*
* struct Quant *q;
* DCELL *dMin, *dMax;
* CELL *cMin, *cMax;
*
* returns the minimum and maximum cell and dcell values of all
* the ranges defined.
*
* returns: -1 if q->truncate or q->round are true or
after G_quant_init (), or any call to
* G_quant_clear () or G_quant_free ()
* no explicit rules have been added
* In this case the returned minimum and maximum
* CELL and DCELL values are null.
* 1 otherwise. in this case the values returned correspond
* to the extreme values of the defined rules
*
**********************************************************************
*
* int
* G_quant_nof_rules (q)
*
* struct Quant *q;
*
* returns the number of quantization rules defined. This number does
* not include the 2 infinite intervals.
*
**********************************************************************
*
* void
* G_quant_get_ith_rule (q, i, dLow, dHigh, cLow, cHigh)
*
* struct Quant *q;
* int i;
* DCELL *dLow, *dHigh;
* CELL *cLow, *cHigh;
*
* returns the i'th quantization rule, for 0 <= i < G_quant_nof_rules().
* a larger value for i means that the rule has been added later.
*
**********************************************************************
* void
* G_quant_set_neg_infinite_rule (q, dLeft, c)
*
* struct Quant *q;
* DCELL dLeft;
* CELL c;
*
* defines a rule for values "dLeft" and smaller. values in this range
* are mapped to "c" if none of the "finite" quantization rules applies.
*
* **********************************************************************
*
* int
* G_quant_get_neg_infinite_rule (q, dLeft, c)
*
* struct Quant *q;
* DCELL *dLeft;
* CELL *c;
*
* returns in "dLeft" and "c" the rule values for the negative infinite
* interval (see G_quant_set_neg_infinite_rule ()).
*
* returns: 0 if this rule is not defined
* 1 otherwise.
*
* **********************************************************************
*
* struct Quant_table *
* G__quant_get_rule_for_d_raster_val (q, val)
*
* struct Quant *q;
* DCELL val;
*
* returns quant rule which will be applied when looking up the
* integer quant value for val. (used when organizing fp_lookup.
*
* returns: pointer to the Quant_table (color rule)
* NULL otherwise.
*
**********************************************************************
* void
* G_quant_set_pos_infinite_rule (q, dRight, c)
*
* struct Quant *q;
* DCELL dRight;
* CELL c;
*
* defines a rule for values "dRight" and larger. values in this range
* are mapped to "c" if none of the "finite" quantization rules or the
* negative infinite rule applies.
*
* **********************************************************************
*
* int
* G_quant_get_pos_infinite_rule (q, dRight, c)
*
* struct Quant *q;
* DCELL *dRight;
* CELL *c;
*
* returns in "dRight" and "c" the rule values for the positive infinite
* interval (see G_quant_set_pos_infinite_rule ()).
*
* returns: 0 if this rule is not defined
* 1 otherwise.
*
**********************************************************************
*
* void
* G_quant_reverse_rule_order (q)
*
* struct Quant *q;
*
* reverses the order in which the qunatization rules are stored. (see
* also G_quant_get_ith_rule () and G_quant_perform_d ()).
*
**********************************************************************
*
* void
* G_quant_add_rule (q, dLow, dHigh, cLow, cHigh)
*
* struct Quant *q;
* DCELL dLow, dHigh;
* CELL cLow, cHigh;
*
* adds a new rule to the set of quantization rules. if dLow < dHigh
* the rule will be stored with the low and high values interchanged.
*
* Note: currently no cleanup of rules is performed, i.e. redundant
* rules are not removed. This can't be changed because Categories
* structure HEAVILY depends of quant rules stored in exactly the
* same order they are entered. So if the cleanup or rearrangement
* is done in the future make a flag for add_rule wether or not
* to do it, then quant will not set this flag.
*
**********************************************************************
*
* CELL
* G_quant_get_cell_value (q, cellValue)
*
* struct Quant *q;
* DCELL *cellValue;
*
* returns in "cell" the quantized CELL values corresponding to the
* DCELL value "cellValue".
*
* if several quantization rules apply for cellValue, the one which has
* been inserted latest (i.e. the one of them which is returned by
* G_quant_get_ith_rule() for the largest i) is used. if no such rule
* applies the cellValue is first tested against the negative infinite
* rule, and finally against the positive infinite rule. if none of
* these rules apply, NO_DATA is returned. the actual value of NO_DATA
* is found by calling G_c_set_null_value().
*
* NOTE: see G_quant_organize_fp_lookup() for details on how
* the values are looked up from fp_lookup table when it is active.
*
* if after G_quant_init (), or any call to G_quant_clear () or
* G_quant_free () neither G_quant_add_rule (),
* G_quant_set_neg_infinite_rule (), G_quant_set_pos_infinite_rule ()
* are used NO_DATA is returned independently
* of cellValue.
*
**********************************************************************
*
* void
* G_quant_perform_d (q, dcell, cell, n)
*
* struct Quant *q;
* DCELL *dcell;
* CELL *cell;
* int n;
*
* returns in "cell" the quantized CELL values corresponding to the
* DCELL values stored in "dcell". the number of elements quantized
* is n. quantization is performed by repeated application of
* G_quant_get_cell_value ().
*
**********************************************************************
*
* void
* G_quant_perform_f (q, fcell, cell, n)
*
* struct Quant *q;
* FCELL *fcell;
* CELL *cell;
* int n;
*
* same as G_quant_perform_d (), except the type.
*
**********************************************************************/
/*--------------------------------------------------------------------------*/
/*
the quantization table is stored as a linear array. rules are added starting
from index 0. redundant rules are not eliminated. rules are tested from the
highest index downto 0. there are two "infinite" rules. support is provided
to reverse the order of the rules.
*/
/*--------------------------------------------------------------------------*/
#include <stdlib.h>
#include <grass/gis.h>
/*--------------------------------------------------------------------------*/
static int double_comp(const void *, const void *);
#define USE_LOOKUP 1
#define MAX_LOOKUP_TABLE_SIZE 2048
#define NO_DATA (G_set_c_null_value (&tmp, 1), (CELL) tmp)
#undef MIN
#undef MAX
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define NO_LEFT_INFINITE_RULE (! q->infiniteLeftSet)
#define NO_RIGHT_INFINITE_RULE (! q->infiniteRightSet)
#define NO_FINITE_RULE (q->nofRules <= 0)
#define NO_EXPLICIT_RULE (NO_FINITE_RULE && \
NO_LEFT_INFINITE_RULE && NO_RIGHT_INFINITE_RULE)
/*--------------------------------------------------------------------------*/
void G_quant_clear(struct Quant *q)
{
q->nofRules = 0;
q->infiniteRightSet = q->infiniteLeftSet = 0;
}
/*--------------------------------------------------------------------------*/
void G_quant_free(struct Quant *q)
{
G_quant_clear(q);
if (q->maxNofRules > 0)
G_free(q->table);
if (q->fp_lookup.active) {
G_free(q->fp_lookup.vals);
G_free(q->fp_lookup.rules);
q->fp_lookup.nalloc = 0;
q->fp_lookup.active = 0;
}
q->maxNofRules = 0;
}
/*--------------------------------------------------------------------------*/
int G__quant_organize_fp_lookup(struct Quant *q)
{
int i;
DCELL val;
CELL tmp;
struct Quant_table *p;
if (q->nofRules * 2 > MAX_LOOKUP_TABLE_SIZE)
return -1;
if (q->nofRules == 0)
return -1;
q->fp_lookup.vals = (DCELL *)
G_calloc(q->nofRules * 2, sizeof(DCELL));
/* 2 endpoints for each rule */
q->fp_lookup.rules = (struct Quant_table **)
G_calloc(q->nofRules * 2, sizeof(struct Quant_table *));
/* first we organize finite rules into a table */
if (!NO_FINITE_RULE) {
i = 0;
/* get the list of DCELL values from set of all dLows and dHighs
of all rules */
/* NOTE: if dLow==DHigh in a rule, the value appears twice in a list
but if dLow==DHigh of the previous, rule the value appears only once */
for (p = &(q->table[q->nofRules - 1]); p >= q->table; p--) {
/* check if the min is the same as previous maximum */
if (i == 0 || p->dLow != q->fp_lookup.vals[i - 1])
q->fp_lookup.vals[i++] = p->dLow;
q->fp_lookup.vals[i++] = p->dHigh;
}
q->fp_lookup.nalloc = i;
/* now sort the values */
qsort((char *)q->fp_lookup.vals, q->fp_lookup.nalloc,
sizeof(DCELL), double_comp);
/* now find the rule to apply inbetween each 2 values in a list */
for (i = 0; i < q->fp_lookup.nalloc - 1; i++) {
/*debug
fprintf (stderr, "%lf %lf ", q->fp_lookup.vals[i], q->fp_lookup.vals[i+1]);
*/
val = (q->fp_lookup.vals[i] + q->fp_lookup.vals[i + 1]) / 2.;
q->fp_lookup.rules[i] =
G__quant_get_rule_for_d_raster_val(q, val);
/* debug
if(q->fp_lookup.rules[i])
fprintf (stderr, "%lf %lf %d %d\n", q->fp_lookup.rules[i]->dLow, q->fp_lookup.rules[i]->dHigh, q->fp_lookup.rules[i]->cLow, q->fp_lookup.rules[i]->cHigh);
else fprintf (stderr, "null\n");
*/
}
} /* organizing finite rules */
if (!NO_LEFT_INFINITE_RULE) {
q->fp_lookup.inf_dmin = q->infiniteDLeft;
q->fp_lookup.inf_min = q->infiniteCLeft;
}
else {
if (q->fp_lookup.nalloc)
q->fp_lookup.inf_dmin = q->fp_lookup.vals[0];
q->fp_lookup.inf_min = NO_DATA;
}
if (!NO_RIGHT_INFINITE_RULE) {
if (q->fp_lookup.nalloc)
q->fp_lookup.inf_dmax = q->infiniteDRight;
q->fp_lookup.inf_max = q->infiniteCRight;
}
else {
q->fp_lookup.inf_dmax = q->fp_lookup.vals[q->fp_lookup.nalloc - 1];
q->fp_lookup.inf_max = NO_DATA;
}
q->fp_lookup.active = 1;
return 1;
}
/*--------------------------------------------------------------------------*/
/*!
* \brief
*
* Initializes the <em>q</em> struct.
*
* \param q
* \return int
*/
int G_quant_init(struct Quant *quant)
{
quant->fp_lookup.active = 0;
quant->maxNofRules = 0;
quant->truncate_only = 0;
quant->round_only = 0;
G_quant_clear(quant);
return 1;
}
/*--------------------------------------------------------------------------*/
int G_quant_is_truncate(const struct Quant *quant)
{
return quant->truncate_only;
}
/*--------------------------------------------------------------------------*/
int G_quant_is_round(const struct Quant *quant)
{
return quant->round_only;
}
/*--------------------------------------------------------------------------*/
/*!
* \brief
*
* sets the quant for <em>q</em>
* rules to perform simple truncation on floats.
*
* \param q
* \return int
*/
/*!
* \brief
*
* sets the quant for <em>q</em>
* rules to perform simple rounding on floats.
*
* \param q
* \return int
*/
int G_quant_truncate(struct Quant *quant)
{
quant->truncate_only = 1;
return 1;
}
/*--------------------------------------------------------------------------*/
int G_quant_round(struct Quant *quant)
{
quant->round_only = 1;
return 1;
}
/*--------------------------------------------------------------------------*/
static void quant_set_limits(struct Quant *q,
DCELL dLow, DCELL dHigh, CELL cLow, CELL cHigh)
{
q->dMin = dLow;
q->dMax = dHigh;
q->cMin = cLow;
q->cMax = cHigh;
}
/*--------------------------------------------------------------------------*/
static void quant_update_limits(struct Quant *q,
DCELL dLow, DCELL dHigh,
CELL cLow, DCELL cHigh)
{
if (NO_EXPLICIT_RULE) {
quant_set_limits(q, dLow, dHigh, cLow, cHigh);
return;
}
q->dMin = MIN(q->dMin, MIN(dLow, dHigh));
q->dMax = MAX(q->dMax, MAX(dLow, dHigh));
q->cMin = MIN(q->cMin, MIN(cLow, cHigh));
q->cMax = MAX(q->cMax, MAX(cLow, cHigh));
}
/*--------------------------------------------------------------------------*/
/*!
* \brief
*
* Extracts the minimum and maximum floating-point
* and integer values from all the rules (except the <tt>"infinite"</tt> rules)
* in <em>q</em> into <em>dmin</em>, <em>dmax</em>, <em>cmin</em>, and <em>cmax</em>. Returns 1
* if there are any explicit rules. If there are no explicit rules, (this
* includes cases when q is set to truncate or round map), it returns 0 and sets
* <em>dmin</em>, <em>dmax</em>, <em>cmin</em>, and <em>cmax</em> to NULL.
*
* \param q
* \param dmin
* \param dmax
* \param cmin
* \param cmax
* \return int
*/
int G_quant_get_limits(const struct Quant *q,
DCELL * dMin, DCELL * dMax, CELL * cMin, CELL * cMax)
{
if (NO_EXPLICIT_RULE) {
G_set_c_null_value(cMin, 1);
G_set_c_null_value(cMax, 1);
G_set_d_null_value(dMin, 1);
G_set_d_null_value(dMax, 1);
return -1;
}
*dMin = q->dMin;
*dMax = q->dMax;
*cMin = q->cMin;
*cMax = q->cMax;
return 1;
}
/*--------------------------------------------------------------------------*/
int G_quant_nof_rules(const struct Quant *q)
{
return q->nofRules;
}
/*--------------------------------------------------------------------------*/
void G_quant_get_ith_rule(const struct Quant *q,
int i,
DCELL * dLow, DCELL * dHigh,
CELL * cLow, CELL * cHigh)
{
*dLow = q->table[i].dLow;
*dHigh = q->table[i].dHigh;
*cLow = q->table[i].cLow;
*cHigh = q->table[i].cHigh;
}
/*--------------------------------------------------------------------------*/
static void quant_table_increase(struct Quant *q)
{
if (q->nofRules < q->maxNofRules)
return;
if (q->maxNofRules == 0) {
q->maxNofRules = 50;
q->table = (struct Quant_table *)
G_malloc(q->maxNofRules * sizeof(struct Quant_table));
}
else {
q->maxNofRules += 50;
q->table = (struct Quant_table *)
G_realloc((char *)q->table,
q->maxNofRules * sizeof(struct Quant_table));
}
}
/*--------------------------------------------------------------------------*/
void G_quant_set_neg_infinite_rule(struct Quant *q, DCELL dLeft, CELL c)
{
q->infiniteDLeft = dLeft;
q->infiniteCLeft = c;
quant_update_limits(q, dLeft, dLeft, c, c);
/* update lookup table */
if (q->fp_lookup.active) {
q->fp_lookup.inf_dmin = q->infiniteDLeft;
q->fp_lookup.inf_min = q->infiniteCLeft;
}
q->infiniteLeftSet = 1;
}
/*--------------------------------------------------------------------------*/
int G_quant_get_neg_infinite_rule(const struct Quant *q,
DCELL * dLeft, CELL * c)
{
if (q->infiniteLeftSet == 0)
return 0;
*dLeft = q->infiniteDLeft;
*c = q->infiniteCLeft;
return 1;
}
/*--------------------------------------------------------------------------*/
void G_quant_set_pos_infinite_rule(struct Quant *q, DCELL dRight, CELL c)
{
q->infiniteDRight = dRight;
q->infiniteCRight = c;
quant_update_limits(q, dRight, dRight, c, c);
/* update lookup table */
if (q->fp_lookup.active) {
q->fp_lookup.inf_dmax = q->infiniteDRight;
q->fp_lookup.inf_max = q->infiniteCRight;
}
q->infiniteRightSet = 1;
}
/*--------------------------------------------------------------------------*/
int G_quant_get_pos_infinite_rule(const struct Quant *q,
DCELL * dRight, CELL * c)
{
if (q->infiniteRightSet == 0)
return 0;
*dRight = q->infiniteDRight;
*c = q->infiniteCRight;
return 1;
}
/*--------------------------------------------------------------------------*/
void G_quant_add_rule(struct Quant *q,
DCELL dLow, DCELL dHigh, CELL cLow, CELL cHigh)
{
int i;
struct Quant_table *p;
quant_table_increase(q);
i = q->nofRules;
p = &(q->table[i]);
if (dHigh >= dLow) {
p->dLow = dLow;
p->dHigh = dHigh;
p->cLow = cLow;
p->cHigh = cHigh;
}
else {
p->dLow = dHigh;
p->dHigh = dLow;
p->cLow = cHigh;
p->cHigh = cLow;
}
/* destroy lookup table, it has to be rebuilt */
if (q->fp_lookup.active) {
G_free(q->fp_lookup.vals);
G_free(q->fp_lookup.rules);
q->fp_lookup.active = 0;
q->fp_lookup.nalloc = 0;
}
quant_update_limits(q, dLow, dHigh, cLow, cHigh);
q->nofRules++;
}
/*--------------------------------------------------------------------------*/
void G_quant_reverse_rule_order(struct Quant *q)
{
struct Quant_table tmp;
struct Quant_table *pLeft, *pRight;
pLeft = q->table;
pRight = &(q->table[q->nofRules - 1]);
while (pLeft < pRight) {
tmp.dLow = pLeft->dLow;
tmp.dHigh = pLeft->dHigh;
tmp.cLow = pLeft->cLow;
tmp.cHigh = pLeft->cHigh;
pLeft->dLow = pRight->dLow;
pLeft->dHigh = pRight->dHigh;
pLeft->cLow = pRight->cLow;
pLeft->cHigh = pRight->cHigh;
pRight->dLow = tmp.dLow;
pRight->dHigh = tmp.dHigh;
pRight->cLow = tmp.cLow;
pRight->cHigh = tmp.cHigh;
pLeft++;
pRight--;
}
}
/*--------------------------------------------------------------------------*/
static CELL quant_interpolate(DCELL dLow, DCELL dHigh,
CELL cLow, CELL cHigh, DCELL dValue)
{
if (cLow == cHigh)
return cLow;
if (dLow == dHigh)
return cLow;
return (CELL) ((dValue - dLow) / (dHigh - dLow) * (DCELL) (cHigh - cLow) +
(DCELL) cLow);
}
/*--------------------------------------------------------------------------*/
static int less_or_equal(double x, double y)
{
if (x <= y)
return 1;
else
return 0;
}
static int less(double x, double y)
{
if (x < y)
return 1;
else
return 0;
}
/*!
* \brief
*
*
* Returns a CELL category for the floating-point <em>value</em> based on the
* quantization rules in <em>q</em>. The first rule found that applies is used.
* The rules are searched in the reverse order they are added to <em>q</em>. If no
* rule is found, the <em>value</em> is first tested against the negative infinite
* rule, and finally against the positive infinite rule. if none of these rules
* apply, the NULL-value is returned.
* <b>NOTE.</b> See G_quant_organize_fp_lookup() for details on how the
* values are looked up from fp_lookup table when it is active. (Right now
* fp_lookup is automatically organized during the first call to
* G_quant_get_cell_value()
*
* \param q
* \param value
* \return CELL
*/
CELL G_quant_get_cell_value(struct Quant * q, DCELL dcellVal)
{
CELL tmp;
DCELL dtmp;
int try, min_ind, max_ind;
struct Quant_table *p;
int (*lower) ();
dtmp = dcellVal;
/* I know the functions which call me already check for null values,
but I am a public function, and can be called from outside */
if (G_is_d_null_value(&dtmp))
return NO_DATA;
if (q->truncate_only)
return (CELL) dtmp;
if (q->round_only) {
if (dcellVal > 0)
return (CELL) (dcellVal + .5);
return (CELL) (dcellVal - .5);
}
if (NO_EXPLICIT_RULE)
return NO_DATA;
if (NO_EXPLICIT_RULE)
return NO_DATA;
if (USE_LOOKUP &&
(q->fp_lookup.active || G__quant_organize_fp_lookup(q) > 0)) {
/* first check if values fall within range */
/* if value is below the range */
if (dcellVal < q->fp_lookup.vals[0]) {
if (dcellVal <= q->fp_lookup.inf_dmin)
return q->fp_lookup.inf_min;
else
return NO_DATA;
}
/* if value is below above range */
if (dcellVal > q->fp_lookup.vals[q->fp_lookup.nalloc - 1]) {
if (dcellVal >= q->fp_lookup.inf_dmax)
return q->fp_lookup.inf_max;
else
return NO_DATA;
}
/* make binary search to find which interval our value belongs to
and apply the rule for this interval */
try = (q->fp_lookup.nalloc - 1) / 2;
min_ind = 0;
max_ind = q->fp_lookup.nalloc - 2;
while (1) {
/* DEBUG
fprintf (stderr, "%d %d %d\n", min_ind, max_ind, try);
*/
/* when the ruke for the interval is NULL, we exclude the end points.
when it exists, we include the end-points */
if (q->fp_lookup.rules[try])
lower = less;
else
lower = less_or_equal;
if (lower(q->fp_lookup.vals[try + 1], dcellVal)) { /* recurse to the second half */
min_ind = try + 1;
/* must be still < nalloc-1, since number is within the range */
try = (max_ind + min_ind) / 2;
continue;
}
if (lower(dcellVal, q->fp_lookup.vals[try])) { /* recurse to the second half */
max_ind = try - 1;
/* must be still >= 0, since number is within the range */
try = (max_ind + min_ind) / 2;
continue;
}
/* the value fits into the interval! */
p = q->fp_lookup.rules[try];
if (p)
return quant_interpolate(p->dLow, p->dHigh, p->cLow, p->cHigh,
dcellVal);
/* otherwise when finite rule for this interval doesn't exist */
else { /* first check if maybe infinite rule applies */
if (dcellVal <= q->fp_lookup.inf_dmin)
return q->fp_lookup.inf_min;
if (dcellVal >= q->fp_lookup.inf_dmax)
return q->fp_lookup.inf_max;
else
return NO_DATA;
}
} /* while */
} /* looking up in fp_lookup */
if (!NO_FINITE_RULE) {
p = G__quant_get_rule_for_d_raster_val(q, dcellVal);
if (!p)
return NO_DATA;
return quant_interpolate(p->dLow, p->dHigh, p->cLow, p->cHigh,
dcellVal);
}
if ((!NO_LEFT_INFINITE_RULE) && (dcellVal <= q->infiniteDLeft))
return q->infiniteCLeft;
if ((NO_RIGHT_INFINITE_RULE) || (dcellVal < q->infiniteDRight))
return NO_DATA;
return q->infiniteCRight;
}
/*--------------------------------------------------------------------------*/
void G_quant_perform_d(struct Quant *q,
const DCELL * dcell, CELL * cell, int n)
{
int i;
for (i = 0; i < n; i++, dcell++)
if (!G_is_d_null_value(dcell))
*cell++ = G_quant_get_cell_value(q, *dcell);
else
G_set_c_null_value(cell++, 1);
}
/*--------------------------------------------------------------------------*/
void G_quant_perform_f(struct Quant *q,
const FCELL * fcell, CELL * cell, int n)
{
int i;
for (i = 0; i < n; i++, fcell++)
if (!G_is_f_null_value(fcell))
*cell++ = G_quant_get_cell_value(q, (DCELL) * fcell);
else
G_set_c_null_value(cell++, 1);
}
/*--------------------------------------------------------------------------*/
static int double_comp(const void *xx, const void *yy)
{
const DCELL *x = xx;
const DCELL *y = yy;
if (G_is_d_null_value(x))
return 0;
if (*x < *y)
return -1;
else if (*x == *y)
return 0;
else
return 1;
}
/*--------------------------------------------------------------------------*/
struct Quant_table *G__quant_get_rule_for_d_raster_val(const struct Quant *q,
DCELL val)
{
const struct Quant_table *p;
for (p = &(q->table[q->nofRules - 1]); p >= q->table; p--)
if ((val >= p->dLow) && (val <= p->dHigh))
break;
if (p >= q->table)
return (struct Quant_table *)p;
else
return (struct Quant_table *)NULL;
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
|