GRASS GIS 8 Programmer's Manual  8.2.1dev(2022)-31a672db8
quant.c
Go to the documentation of this file.
1 /*!
2  * \file lib/raster/quant.c
3  *
4  * \brief Raster Library - Quantization rules.
5  *
6  * The quantization table is stored as a linear array. Rules are added
7  * starting from index 0. Redundant rules are not eliminated. Rules
8  * are tested from the highest index downto 0. There are two
9  * "infinite" rules. Support is provided to reverse the order of the
10  * rules.
11  *
12  * (C) 1999-2009 by the GRASS Development Team
13  *
14  * This program is free software under the GNU General Public License
15  * (>=v2). Read the file COPYING that comes with GRASS for details.
16  *
17  * \author USACERL and many others
18  */
19 
20 #include <stdlib.h>
21 #include <grass/gis.h>
22 #include <grass/raster.h>
23 
24 static int double_comp(const void *, const void *);
25 
26 #define USE_LOOKUP 1
27 #define MAX_LOOKUP_TABLE_SIZE 2048
28 #define NO_DATA (Rast_set_c_null_value (&tmp, 1), (CELL) tmp)
29 
30 #define NO_LEFT_INFINITE_RULE (! q->infiniteLeftSet)
31 #define NO_RIGHT_INFINITE_RULE (! q->infiniteRightSet)
32 #define NO_FINITE_RULE (q->nofRules <= 0)
33 #define NO_EXPLICIT_RULE (NO_FINITE_RULE && \
34  NO_LEFT_INFINITE_RULE && NO_RIGHT_INFINITE_RULE)
35 
36 /*!
37  \brief Resets the number of defined rules and number of infinite rules to 0
38 
39  \param q pointer to Quant structure to be reset
40  */
41 void Rast_quant_clear(struct Quant *q)
42 {
43  q->nofRules = 0;
45 }
46 
47 /*!
48  \brief Resets and frees allocated memory
49 
50  Resets the number of defined rules to 0 and free's space allocated
51  for rules. Calls Rast_quant_clear().
52 
53  \param q pointer to Quant structure to be reset
54  */
55 void Rast_quant_free(struct Quant *q)
56 {
58 
59  if (q->maxNofRules > 0)
60  G_free(q->table);
61  if (q->fp_lookup.active) {
62  G_free(q->fp_lookup.vals);
64  q->fp_lookup.nalloc = 0;
65  q->fp_lookup.active = 0;
66  }
67  q->maxNofRules = 0;
68 }
69 
70 /*!
71  * \brief Organized fp_lookup table.
72  *
73  * Organizes fp_lookup table for faster (logarithmic) lookup time
74  * G_quant_organize_fp_lookup() creates a list of min and max for
75  * each quant rule, sorts this list, and stores the pointer to quant
76  * rule that should be used inbetween any 2 numbers in this list.
77  * Also it stores extreme points for 2 infinite rules, if exist.
78  * After the call to G_quant_organize_fp_lookup()
79  * instead of linearly searching through list of rules to find
80  * a rule to apply, quant lookup will perform a binary search
81  * to find an interval containing floating point value, and then use
82  * the rule associated with this interval.
83  * when the value doesn't fall within any interval, check for the
84  * infinite rules.
85  *
86  * \param q pointer to Quant structure which holds quant rules info
87  *
88  * \return 1 on success
89  */
91 {
92  int i;
93  DCELL val;
94  CELL tmp;
95  struct Quant_table *p;
96 
97  if (q->nofRules * 2 > MAX_LOOKUP_TABLE_SIZE)
98  return -1;
99  if (q->nofRules == 0)
100  return -1;
101  q->fp_lookup.vals = (DCELL *)
102  G_calloc(q->nofRules * 2, sizeof(DCELL));
103  /* 2 endpoints for each rule */
104  q->fp_lookup.rules = (struct Quant_table **)
105  G_calloc(q->nofRules * 2, sizeof(struct Quant_table *));
106 
107  /* first we organize finite rules into a table */
108  if (!NO_FINITE_RULE) {
109  i = 0;
110  /* get the list of DCELL values from set of all dLows and dHighs
111  of all rules */
112  /* NOTE: if dLow==DHigh in a rule, the value appears twice in a list
113  but if dLow==DHigh of the previous, rule the value appears only once */
114 
115  for (p = &(q->table[q->nofRules - 1]); p >= q->table; p--) {
116  /* check if the min is the same as previous maximum */
117  if (i == 0 || p->dLow != q->fp_lookup.vals[i - 1])
118  q->fp_lookup.vals[i++] = p->dLow;
119  q->fp_lookup.vals[i++] = p->dHigh;
120  }
121  q->fp_lookup.nalloc = i;
122 
123  /* now sort the values */
124  qsort((char *)q->fp_lookup.vals, q->fp_lookup.nalloc,
125  sizeof(DCELL), double_comp);
126 
127  /* now find the rule to apply inbetween each 2 values in a list */
128  for (i = 0; i < q->fp_lookup.nalloc - 1; i++) {
129  /*debug
130  fprintf (stderr, "%lf %lf ", q->fp_lookup.vals[i], q->fp_lookup.vals[i+1]);
131  */
132  val = (q->fp_lookup.vals[i] + q->fp_lookup.vals[i + 1]) / 2.;
133  q->fp_lookup.rules[i] =
135  /* debug
136  if(q->fp_lookup.rules[i])
137  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);
138  else fprintf (stderr, "null\n");
139  */
140 
141  }
142  } /* organizing finite rules */
143 
144  if (!NO_LEFT_INFINITE_RULE) {
147  }
148  else {
149  if (q->fp_lookup.nalloc)
150  q->fp_lookup.inf_dmin = q->fp_lookup.vals[0];
152  }
153 
154  if (!NO_RIGHT_INFINITE_RULE) {
155  if (q->fp_lookup.nalloc)
158  }
159  else {
162  }
163  q->fp_lookup.active = 1;
164  return 1;
165 }
166 
167 /*!
168  * \brief Initialize the structure
169  *
170  * Initializes the <i>q</i> struct.
171  *
172  * \param quant pointer to Quant structure to be initialized
173  */
174 void Rast_quant_init(struct Quant *quant)
175 {
176  quant->fp_lookup.active = 0;
177  quant->maxNofRules = 0;
178  quant->truncate_only = 0;
179  quant->round_only = 0;
180  Rast_quant_clear(quant);
181 }
182 
183 /*!
184  \brief Returns whether or not quant rules are set to truncate map
185 
186  \param quant pointer to Quant structure which holds quant rules info
187 
188  \return 1 if truncate is enable
189  \return 0 if not truncated
190  */
191 int Rast_quant_is_truncate(const struct Quant *quant)
192 {
193  return quant->truncate_only;
194 }
195 
196 /*!
197  \brief Returns whether or not quant rules are set to round map
198  \param quant pointer to Quant structure which holds quant rules info
199 
200  \return 1 is round
201  \return 0 not round
202  */
203 int Rast_quant_is_round(const struct Quant *quant)
204 {
205  return quant->round_only;
206 }
207 
208 /*!
209  * \brief Sets the quant rules to perform simple truncation on floats.
210  *
211  * Sets the quant for <i>q</i> rules to perform simple truncation on
212  * floats.
213  *
214  * \param quant pointer to Quant structure which holds quant rules info
215  */
216 void Rast_quant_truncate(struct Quant *quant)
217 {
218  quant->truncate_only = 1;
219 }
220 
221 /*!
222  * \brief Sets the quant rules to perform simple rounding on floats.
223  *
224  * Sets the quant for <i>q</i> rules to perform simple rounding on
225  * floats.
226  *
227  * \param quant pointer to Quant structure which holds quant rules info
228  */
229 void Rast_quant_round(struct Quant *quant)
230 {
231  quant->round_only = 1;
232 }
233 
234 static void quant_set_limits(struct Quant *q,
236 {
237  q->dMin = dLow;
238  q->dMax = dHigh;
239  q->cMin = cLow;
240  q->cMax = cHigh;
241 }
242 
243 static void quant_update_limits(struct Quant *q,
245  CELL cLow, DCELL cHigh)
246 {
247  if (NO_EXPLICIT_RULE) {
248  quant_set_limits(q, dLow, dHigh, cLow, cHigh);
249  return;
250  }
251 
252  q->dMin = MIN(q->dMin, MIN(dLow, dHigh));
253  q->dMax = MAX(q->dMax, MAX(dLow, dHigh));
254  q->cMin = MIN(q->cMin, MIN(cLow, cHigh));
255  q->cMax = MAX(q->cMax, MAX(cLow, cHigh));
256 }
257 
258 /*!
259  * \brief Returns the minimum and maximum cell and dcell values of all
260  * the ranges defined.
261  *
262  * Extracts the minimum and maximum floating-point and integer values
263  * from all the rules (except the "infinite" rules) in <i>q</i> into
264  * <i>dmin</i>, <i>dmax</i>, <i>cmin</i>, and <i>cmax</i>.
265  *
266  * \param quant pointer to Quant structure which holds quant rules info
267  * \param[out] dmin minimum fp value
268  * \param[out] dmax maximum fp value
269  * \param[out] cmin minimum value
270  * \param[out] cmax maximum value
271  *
272  * \return -1 if q->truncate or q->round are true or after
273  * Rast_quant_init (), or any call to Rast_quant_clear () or Rast_quant_free()
274  * no explicit rules have been added. In this case the returned
275  * minimum and maximum CELL and DCELL values are null.
276  * \return 1 if there are any explicit rules
277  * \return 0 if there are no explicit rules (this includes cases when
278  * q is set to truncate or round map), and sets <i>dmin</i>,
279  * <i>dmax</i>, <i>cmin</i>, and <i>cmax</i> to NULL.
280  */
281 int Rast_quant_get_limits(const struct Quant *q,
282  DCELL * dMin, DCELL * dMax, CELL * cMin,
283  CELL * cMax)
284 {
285  if (NO_EXPLICIT_RULE) {
286  Rast_set_c_null_value(cMin, 1);
287  Rast_set_c_null_value(cMax, 1);
288  Rast_set_d_null_value(dMin, 1);
289  Rast_set_d_null_value(dMax, 1);
290  return -1;
291  }
292 
293  *dMin = q->dMin;
294  *dMax = q->dMax;
295  *cMin = q->cMin;
296  *cMax = q->cMax;
297 
298  return 1;
299 }
300 
301 /*!
302  \brief Returns the number of quantization rules defined.
303 
304  This number does not include the 2 infinite intervals.
305 
306  \param q pointer to Quant structure which holds quant rules info
307 
308  \return number of quantization rules
309  */
310 int Rast_quant_nof_rules(const struct Quant *q)
311 {
312  return q->nofRules;
313 }
314 
315 /*!
316  \brief Returns the i'th quantization rule.
317 
318  For 0 <= i < Rast_quant_nof_rules(). A larger value for i means that
319  the rule has been added later.
320 
321  \param q pointer to Quant structure which holds quant rules info
322  \param i index
323  \param[out] dLow minimum fp value
324  \param[out] dHigh maximum fp value
325  \param[out] cLow minimum value
326  \param[out] cHigh maximum value
327  */
328 void Rast_quant_get_ith_rule(const struct Quant *q,
329  int i,
330  DCELL * dLow, DCELL * dHigh,
331  CELL * cLow, CELL * cHigh)
332 {
333  *dLow = q->table[i].dLow;
334  *dHigh = q->table[i].dHigh;
335  *cLow = q->table[i].cLow;
336  *cHigh = q->table[i].cHigh;
337 }
338 
339 static void quant_table_increase(struct Quant *q)
340 {
341  if (q->nofRules < q->maxNofRules)
342  return;
343 
344  if (q->maxNofRules == 0) {
345  q->maxNofRules = 50;
346  q->table = (struct Quant_table *)
347  G_malloc(q->maxNofRules * sizeof(struct Quant_table));
348  }
349  else {
350  q->maxNofRules += 50;
351  q->table = (struct Quant_table *)
352  G_realloc((char *)q->table,
353  q->maxNofRules * sizeof(struct Quant_table));
354  }
355 }
356 
357 /*!
358  \brief Defines a rule for values "dLeft" and smaller.
359 
360  Values in this range are mapped to "c" if none of the "finite"
361  quantization rules applies.
362 
363  \param q pointer to Quant structure which holds quant rules info
364 
365  \param dLeft fp value
366  \param c value
367  */
369 {
370  q->infiniteDLeft = dLeft;
371  q->infiniteCLeft = c;
372  quant_update_limits(q, dLeft, dLeft, c, c);
373 
374  /* update lookup table */
375  if (q->fp_lookup.active) {
378  }
379  q->infiniteLeftSet = 1;
380 }
381 
382 /*!
383  \brief Returns in "dLeft" and "c" the rule values.
384 
385  For the negative infinite interval (see Rast_quant_set_neg_infinite_rule()).
386 
387  \param q pointer to Quant structure which holds quant rules info
388  \param[out] dLeft fp value
389  \param[out] c value
390 
391  \return 0 if this rule is not defined
392  \return 1 otherwise
393  */
395  DCELL * dLeft, CELL * c)
396 {
397  if (q->infiniteLeftSet == 0)
398  return 0;
399 
400  *dLeft = q->infiniteDLeft;
401  *c = q->infiniteCLeft;
402 
403  return 1;
404 }
405 
406 /*!
407  \brief Defines a rule for values "dRight" and larger.
408 
409  Values in this range are mapped to "c" if none of the "finite"
410  quantization rules or the negative infinite rule applies.
411 
412  \param q pointer to Quant structure which holds quant rules info
413  \param dRight fp value
414  \param c value
415  */
417 {
418  q->infiniteDRight = dRight;
419  q->infiniteCRight = c;
420  quant_update_limits(q, dRight, dRight, c, c);
421 
422  /* update lookup table */
423  if (q->fp_lookup.active) {
426  }
427  q->infiniteRightSet = 1;
428 }
429 
430 /*!
431  \brief Returns in "dRight" and "c" the rule values.
432 
433  For the positive infinite interval (see Rast_quant_set_pos_infinite_rule()).
434 
435  \param q pointer to Quant structure which holds quant rules info
436  \param[out] dRight fp value
437  \param[out] c value
438 
439  \return 0 if this rule is not defined
440  \return 1 otherwise
441  */
443  DCELL * dRight, CELL * c)
444 {
445  if (q->infiniteRightSet == 0)
446  return 0;
447 
448  *dRight = q->infiniteDRight;
449  *c = q->infiniteCRight;
450 
451  return 1;
452 }
453 
454 /*!
455  \brief Adds a new rule to the set of quantization rules.
456 
457  If dLow < dHigh the rule will be stored with the low and high values
458  interchanged.
459 
460  Note: currently no cleanup of rules is performed, i.e. redundant
461  rules are not removed. This can't be changed because Categories
462  structure HEAVILY depends of quant rules stored in exactly the same
463  order they are entered. So if the cleanup or rearrangement is done in
464  the future make a flag for add_rule whether or not to do it, then
465  quant will not set this flag.
466 
467  \param q pointer to Quant structure which holds quant rules info
468  \param dLow minimum fp value
469  \param dHigh maximum fp value
470  \param cLow minimum value
471  \param cHigh maximum value
472  */
473 void Rast_quant_add_rule(struct Quant *q,
475 {
476  int i;
477  struct Quant_table *p;
478 
479  quant_table_increase(q);
480 
481  i = q->nofRules;
482 
483  p = &(q->table[i]);
484  if (dHigh >= dLow) {
485  p->dLow = dLow;
486  p->dHigh = dHigh;
487  p->cLow = cLow;
488  p->cHigh = cHigh;
489  }
490  else {
491  p->dLow = dHigh;
492  p->dHigh = dLow;
493  p->cLow = cHigh;
494  p->cHigh = cLow;
495  }
496 
497  /* destroy lookup table, it has to be rebuilt */
498  if (q->fp_lookup.active) {
499  G_free(q->fp_lookup.vals);
500  G_free(q->fp_lookup.rules);
501  q->fp_lookup.active = 0;
502  q->fp_lookup.nalloc = 0;
503  }
504 
505  quant_update_limits(q, dLow, dHigh, cLow, cHigh);
506 
507  q->nofRules++;
508 }
509 
510 /*!
511  \brief Rreverses the order in which the qunatization rules are stored.
512 
513  See also Rast_quant_get_ith_rule() and Rast_quant_perform_d()).
514 
515  \param q pointer to Quant rules which holds quant rules info
516  */
518 {
519  struct Quant_table tmp;
520  struct Quant_table *pLeft, *pRight;
521 
522  pLeft = q->table;
523  pRight = &(q->table[q->nofRules - 1]);
524 
525  while (pLeft < pRight) {
526  tmp.dLow = pLeft->dLow;
527  tmp.dHigh = pLeft->dHigh;
528  tmp.cLow = pLeft->cLow;
529  tmp.cHigh = pLeft->cHigh;
530 
531  pLeft->dLow = pRight->dLow;
532  pLeft->dHigh = pRight->dHigh;
533  pLeft->cLow = pRight->cLow;
534  pLeft->cHigh = pRight->cHigh;
535 
536  pRight->dLow = tmp.dLow;
537  pRight->dHigh = tmp.dHigh;
538  pRight->cLow = tmp.cLow;
539  pRight->cHigh = tmp.cHigh;
540 
541  pLeft++;
542  pRight--;
543  }
544 }
545 
546 static CELL quant_interpolate(DCELL dLow, DCELL dHigh,
547  CELL cLow, CELL cHigh, DCELL dValue)
548 {
549  if (cLow == cHigh)
550  return cLow;
551  if (dLow == dHigh)
552  return cLow;
553 
554  return (CELL) ((dValue - dLow) / (dHigh - dLow) * (DCELL) (cHigh - cLow) +
555  (DCELL) cLow);
556 }
557 
558 static int less_or_equal(double x, double y)
559 {
560  if (x <= y)
561  return 1;
562  else
563  return 0;
564 }
565 
566 static int less(double x, double y)
567 {
568  if (x < y)
569  return 1;
570  else
571  return 0;
572 }
573 
574 /*!
575  * \brief
576  *
577  *
578  * Returns a CELL category for the floating-point <i>value</i> based
579  * on the quantization rules in <i>q</i>. The first rule found that
580  * applies is used. The rules are searched in the reverse order they
581  * are added to <i>q</i>. If no rule is found, the <i>value</i>
582  * is first tested against the negative infinite rule, and finally
583  * against the positive infinite rule. If none of these rules apply,
584  * the NULL-value is returned.
585  *
586  * <b>Note:</b> See G_quant_organize_fp_lookup() for details on how
587  * the values are looked up from fp_lookup table when it is
588  * active. Right now fp_lookup is automatically organized during the
589  * first call to Rast_quant_get_cell_value().
590  *
591  * \param q pointer to Quant structure which holds quant rules info
592  * \param dcellValue fp cell value
593  *
594  * \return cell value (integer)
595  */
597 {
598  CELL tmp;
599  DCELL dtmp;
600  int try, min_ind, max_ind;
601  struct Quant_table *p;
602  int (*lower) ();
603 
604  dtmp = dcellVal;
605  /* I know the functions which call me already check for null values,
606  but I am a public function, and can be called from outside */
607  if (Rast_is_d_null_value(&dtmp))
608  return NO_DATA;
609 
610  if (q->truncate_only)
611  return (CELL) dtmp;
612 
613  if (q->round_only) {
614  if (dcellVal > 0)
615  return (CELL) (dcellVal + .5);
616  return (CELL) (dcellVal - .5);
617  }
618 
619  if (NO_EXPLICIT_RULE)
620  return NO_DATA;
621  if (NO_EXPLICIT_RULE)
622  return NO_DATA;
623 
624  if (USE_LOOKUP &&
626  /* first check if values fall within range */
627  /* if value is below the range */
628  if (dcellVal < q->fp_lookup.vals[0]) {
629  if (dcellVal <= q->fp_lookup.inf_dmin)
630  return q->fp_lookup.inf_min;
631  else
632  return NO_DATA;
633  }
634  /* if value is below above range */
635  if (dcellVal > q->fp_lookup.vals[q->fp_lookup.nalloc - 1]) {
636  if (dcellVal >= q->fp_lookup.inf_dmax)
637  return q->fp_lookup.inf_max;
638  else
639  return NO_DATA;
640  }
641  /* make binary search to find which interval our value belongs to
642  and apply the rule for this interval */
643  try = (q->fp_lookup.nalloc - 1) / 2;
644  min_ind = 0;
645  max_ind = q->fp_lookup.nalloc - 2;
646  while (1) {
647  /* DEBUG
648  fprintf (stderr, "%d %d %d\n", min_ind, max_ind, try);
649  */
650  /* when the ruke for the interval is NULL, we exclude the end points.
651  when it exists, we include the end-points */
652  if (q->fp_lookup.rules[try])
653  lower = less;
654  else
655  lower = less_or_equal;
656 
657  if (lower(q->fp_lookup.vals[try + 1], dcellVal)) { /* recurse to the second half */
658  min_ind = try + 1;
659  /* must be still < nalloc-1, since number is within the range */
660  try = (max_ind + min_ind) / 2;
661  continue;
662  }
663  if (lower(dcellVal, q->fp_lookup.vals[try])) { /* recurse to the second half */
664  max_ind = try - 1;
665  /* must be still >= 0, since number is within the range */
666  try = (max_ind + min_ind) / 2;
667  continue;
668  }
669  /* the value fits into the interval! */
670  p = q->fp_lookup.rules[try];
671  if (p)
672  return quant_interpolate(p->dLow, p->dHigh, p->cLow, p->cHigh,
673  dcellVal);
674  /* otherwise when finite rule for this interval doesn't exist */
675  else { /* first check if maybe infinite rule applies */
676  if (dcellVal <= q->fp_lookup.inf_dmin)
677  return q->fp_lookup.inf_min;
678  if (dcellVal >= q->fp_lookup.inf_dmax)
679  return q->fp_lookup.inf_max;
680  else
681  return NO_DATA;
682  }
683  } /* while */
684  } /* looking up in fp_lookup */
685 
686  if (!NO_FINITE_RULE) {
687  p = Rast__quant_get_rule_for_d_raster_val(q, dcellVal);
688  if (!p)
689  return NO_DATA;
690  return quant_interpolate(p->dLow, p->dHigh, p->cLow, p->cHigh,
691  dcellVal);
692  }
693 
694  if ((!NO_LEFT_INFINITE_RULE) && (dcellVal <= q->infiniteDLeft))
695  return q->infiniteCLeft;
696 
697  if ((NO_RIGHT_INFINITE_RULE) || (dcellVal < q->infiniteDRight))
698  return NO_DATA;
699 
700  return q->infiniteCRight;
701 }
702 
703 /*!
704  \brief Returns in "cell" the quantized CELL values.
705 
706  Returns in "cell" the quantized CELL values corresponding to the
707  DCELL values stored in "dcell". the number of elements quantized
708  is n. quantization is performed by repeated application of
709  Rast_quant_get_cell_value().
710 
711  \param q pointer to Quant structure which holds quant rules info
712  \param dcell pointer to fp cell values array
713  \param[out] cell pointer cell values array
714  \param n number of cells
715  */
717  const DCELL * dcell, CELL * cell, int n)
718 {
719  int i;
720 
721  for (i = 0; i < n; i++, dcell++)
722  if (!Rast_is_d_null_value(dcell))
723  *cell++ = Rast_quant_get_cell_value(q, *dcell);
724  else
725  Rast_set_c_null_value(cell++, 1);
726 }
727 
728 /*!
729  \brief Same as Rast_quant_perform_d(), except the type.
730 
731  \param q pointer to Quant structure which holds quant rules info
732  \param fcell pointer to fp cell values array
733  \param[out] cell pointer cell values array
734  \param n number of cells
735  */
737  const FCELL * fcell, CELL * cell, int n)
738 {
739  int i;
740 
741  for (i = 0; i < n; i++, fcell++)
742  if (!Rast_is_f_null_value(fcell))
743  *cell++ = Rast_quant_get_cell_value(q, (DCELL) * fcell);
744  else
745  Rast_set_c_null_value(cell++, 1);
746 }
747 
748 static int double_comp(const void *xx, const void *yy)
749 {
750  const DCELL *x = xx;
751  const DCELL *y = yy;
752 
753  if (Rast_is_d_null_value(x))
754  return 0;
755  if (*x < *y)
756  return -1;
757  else if (*x == *y)
758  return 0;
759  else
760  return 1;
761 }
762 
763 /*!
764  \brief Returns quant rule which will be applied.
765 
766  Returns quant rule which will be applied when looking up the integer
767  quant value for val (used when organizing fp_lookup).
768 
769  \param q pointer to Quant structure which holds quant rules info
770  \param val fp cell value
771 
772  \return pointer to the Quant_table (color rule)
773  \return NULL otherwise
774  */
776  *q, DCELL val)
777 {
778  const struct Quant_table *p;
779 
780  for (p = &(q->table[q->nofRules - 1]); p >= q->table; p--)
781  if ((val >= p->dLow) && (val <= p->dHigh))
782  break;
783  if (p >= q->table)
784  return (struct Quant_table *)p;
785  else
786  return (struct Quant_table *)NULL;
787 }
#define G_malloc(n)
Definition: defs/gis.h:112
int Rast_quant_get_neg_infinite_rule(const struct Quant *q, DCELL *dLeft, CELL *c)
Returns in "dLeft" and "c" the rule values.
Definition: quant.c:394
void Rast_set_c_null_value(CELL *, int)
To set a number of CELL raster values to NULL.
Definition: null_val.c:124
#define USE_LOOKUP
Definition: quant.c:26
void Rast_quant_truncate(struct Quant *quant)
Sets the quant rules to perform simple truncation on floats.
Definition: quant.c:216
DCELL inf_dmax
Definition: raster.h:118
#define NO_RIGHT_INFINITE_RULE
Definition: quant.c:31
#define Rast_is_d_null_value(dcellVal)
Definition: defs/raster.h:414
struct Quant::@5 fp_lookup
double DCELL
Definition: gis.h:614
CELL inf_max
Definition: raster.h:120
int Rast_quant_is_truncate(const struct Quant *quant)
Returns whether or not quant rules are set to truncate map.
Definition: quant.c:191
void Rast_quant_clear(struct Quant *q)
Resets the number of defined rules and number of infinite rules to 0.
Definition: quant.c:41
int Rast_quant_nof_rules(const struct Quant *q)
Returns the number of quantization rules defined.
Definition: quant.c:310
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
DCELL dHigh
Definition: raster.h:79
#define NO_LEFT_INFINITE_RULE
Definition: quant.c:30
int Rast_quant_get_pos_infinite_rule(const struct Quant *q, DCELL *dRight, CELL *c)
Returns in "dRight" and "c" the rule values.
Definition: quant.c:442
#define Rast_is_f_null_value(fcellVal)
Definition: defs/raster.h:412
void Rast_quant_free(struct Quant *q)
Resets and frees allocated memory.
Definition: quant.c:55
int maxNofRules
Definition: raster.h:93
#define NULL
Definition: ccmath.h:32
int infiniteRightSet
Definition: raster.h:91
void Rast_quant_reverse_rule_order(struct Quant *q)
Rreverses the order in which the qunatization rules are stored.
Definition: quant.c:517
#define x
void Rast_quant_add_rule(struct Quant *q, DCELL dLow, DCELL dHigh, CELL cLow, CELL cHigh)
Adds a new rule to the set of quantization rules.
Definition: quant.c:473
#define G_calloc(m, n)
Definition: defs/gis.h:113
int truncate_only
Definition: raster.h:86
CELL Rast_quant_get_cell_value(struct Quant *q, DCELL dcellVal)
Returns a CELL category for the floating-point value based on the quantization rules in q...
Definition: quant.c:596
int infiniteLeftSet
Definition: raster.h:90
DCELL inf_dmin
Definition: raster.h:117
void Rast_quant_perform_f(struct Quant *q, const FCELL *fcell, CELL *cell, int n)
Same as Rast_quant_perform_d(), except the type.
Definition: quant.c:736
CELL cMin
Definition: raster.h:105
int active
Definition: raster.h:116
struct Quant_table * Rast__quant_get_rule_for_d_raster_val(const struct Quant *q, DCELL val)
Returns quant rule which will be applied.
Definition: quant.c:775
CELL cLow
Definition: raster.h:80
#define NO_FINITE_RULE
Definition: quant.c:32
#define NO_EXPLICIT_RULE
Definition: quant.c:33
int Rast__quant_organize_fp_lookup(struct Quant *q)
Organized fp_lookup table.
Definition: quant.c:90
struct Quant_table * table
Definition: raster.h:107
Definition: raster.h:84
DCELL dLow
Definition: raster.h:78
#define MAX_LOOKUP_TABLE_SIZE
Definition: quant.c:27
DCELL dMax
Definition: raster.h:104
DCELL * vals
Definition: raster.h:111
CELL cMax
Definition: raster.h:106
#define MIN(a, b)
Definition: gis.h:140
#define NO_DATA
Definition: quant.c:28
DCELL infiniteDLeft
Definition: raster.h:99
struct Quant_table ** rules
Definition: raster.h:114
void Rast_quant_perform_d(struct Quant *q, const DCELL *dcell, CELL *cell, int n)
Returns in "cell" the quantized CELL values.
Definition: quant.c:716
#define MAX(a, b)
Definition: gis.h:135
int nalloc
Definition: raster.h:115
int Rast_quant_is_round(const struct Quant *quant)
Returns whether or not quant rules are set to round map.
Definition: quant.c:203
float FCELL
Definition: gis.h:615
CELL inf_min
Definition: raster.h:119
DCELL infiniteDRight
Definition: raster.h:100
int CELL
Definition: gis.h:613
int round_only
Definition: raster.h:87
#define G_realloc(p, n)
Definition: defs/gis.h:114
void Rast_quant_init(struct Quant *quant)
Initialize the structure.
Definition: quant.c:174
void Rast_quant_round(struct Quant *quant)
Sets the quant rules to perform simple rounding on floats.
Definition: quant.c:229
void Rast_quant_set_pos_infinite_rule(struct Quant *q, DCELL dRight, CELL c)
Defines a rule for values "dRight" and larger.
Definition: quant.c:416
CELL cHigh
Definition: raster.h:81
int nofRules
Definition: raster.h:94
CELL infiniteCLeft
Definition: raster.h:101
void Rast_quant_get_ith_rule(const struct Quant *q, int i, DCELL *dLow, DCELL *dHigh, CELL *cLow, CELL *cHigh)
Returns the i&#39;th quantization rule.
Definition: quant.c:328
DCELL dMin
Definition: raster.h:103
CELL infiniteCRight
Definition: raster.h:102
void Rast_quant_set_neg_infinite_rule(struct Quant *q, DCELL dLeft, CELL c)
Defines a rule for values "dLeft" and smaller.
Definition: quant.c:368
int Rast_quant_get_limits(const struct Quant *q, DCELL *dMin, DCELL *dMax, CELL *cMin, CELL *cMax)
Returns the minimum and maximum cell and dcell values of all the ranges defined.
Definition: quant.c:281
void Rast_set_d_null_value(DCELL *, int)
To set a number of DCELL raster values to NULL.
Definition: null_val.c:155