quant.c 26 KB

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