quant.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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 MIN(a,b) (a < b ? a : b)
  287. #define MAX(a,b) (a > b ? a : b)
  288. #define NO_DATA (G_set_c_null_value (&tmp, 1), (CELL) tmp)
  289. #define NO_LEFT_INFINITE_RULE (! q->infiniteLeftSet)
  290. #define NO_RIGHT_INFINITE_RULE (! q->infiniteRightSet)
  291. #define NO_FINITE_RULE (q->nofRules <= 0)
  292. #define NO_EXPLICIT_RULE (NO_FINITE_RULE && \
  293. NO_LEFT_INFINITE_RULE && NO_RIGHT_INFINITE_RULE)
  294. /*--------------------------------------------------------------------------*/
  295. void G_quant_clear(struct Quant *q)
  296. {
  297. q->nofRules = 0;
  298. q->infiniteRightSet = q->infiniteLeftSet = 0;
  299. }
  300. /*--------------------------------------------------------------------------*/
  301. void G_quant_free(struct Quant *q)
  302. {
  303. G_quant_clear(q);
  304. if (q->maxNofRules > 0)
  305. G_free(q->table);
  306. if (q->fp_lookup.active) {
  307. G_free(q->fp_lookup.vals);
  308. G_free(q->fp_lookup.rules);
  309. q->fp_lookup.nalloc = 0;
  310. q->fp_lookup.active = 0;
  311. }
  312. q->maxNofRules = 0;
  313. }
  314. /*--------------------------------------------------------------------------*/
  315. int G__quant_organize_fp_lookup(struct Quant *q)
  316. {
  317. int i;
  318. DCELL val;
  319. CELL tmp;
  320. struct Quant_table *p;
  321. if (q->nofRules * 2 > MAX_LOOKUP_TABLE_SIZE)
  322. return -1;
  323. if (q->nofRules == 0)
  324. return -1;
  325. q->fp_lookup.vals = (DCELL *)
  326. G_calloc(q->nofRules * 2, sizeof(DCELL));
  327. /* 2 endpoints for each rule */
  328. q->fp_lookup.rules = (struct Quant_table **)
  329. G_calloc(q->nofRules * 2, sizeof(struct Quant_table *));
  330. /* first we organize finite rules into a table */
  331. if (!NO_FINITE_RULE) {
  332. i = 0;
  333. /* get the list of DCELL values from set of all dLows and dHighs
  334. of all rules */
  335. /* NOTE: if dLow==DHigh in a rule, the value appears twice in a list
  336. but if dLow==DHigh of the previous, rule the value appears only once */
  337. for (p = &(q->table[q->nofRules - 1]); p >= q->table; p--) {
  338. /* check if the min is the same as previous maximum */
  339. if (i == 0 || p->dLow != q->fp_lookup.vals[i - 1])
  340. q->fp_lookup.vals[i++] = p->dLow;
  341. q->fp_lookup.vals[i++] = p->dHigh;
  342. }
  343. q->fp_lookup.nalloc = i;
  344. /* now sort the values */
  345. qsort((char *)q->fp_lookup.vals, q->fp_lookup.nalloc,
  346. sizeof(DCELL), double_comp);
  347. /* now find the rule to apply inbetween each 2 values in a list */
  348. for (i = 0; i < q->fp_lookup.nalloc - 1; i++) {
  349. /*debug
  350. fprintf (stderr, "%lf %lf ", q->fp_lookup.vals[i], q->fp_lookup.vals[i+1]);
  351. */
  352. val = (q->fp_lookup.vals[i] + q->fp_lookup.vals[i + 1]) / 2.;
  353. q->fp_lookup.rules[i] =
  354. G__quant_get_rule_for_d_raster_val(q, val);
  355. /* debug
  356. if(q->fp_lookup.rules[i])
  357. 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);
  358. else fprintf (stderr, "null\n");
  359. */
  360. }
  361. } /* organizing finite rules */
  362. if (!NO_LEFT_INFINITE_RULE) {
  363. q->fp_lookup.inf_dmin = q->infiniteDLeft;
  364. q->fp_lookup.inf_min = q->infiniteCLeft;
  365. }
  366. else {
  367. if (q->fp_lookup.nalloc)
  368. q->fp_lookup.inf_dmin = q->fp_lookup.vals[0];
  369. q->fp_lookup.inf_min = NO_DATA;
  370. }
  371. if (!NO_RIGHT_INFINITE_RULE) {
  372. if (q->fp_lookup.nalloc)
  373. q->fp_lookup.inf_dmax = q->infiniteDRight;
  374. q->fp_lookup.inf_max = q->infiniteCRight;
  375. }
  376. else {
  377. q->fp_lookup.inf_dmax = q->fp_lookup.vals[q->fp_lookup.nalloc - 1];
  378. q->fp_lookup.inf_max = NO_DATA;
  379. }
  380. q->fp_lookup.active = 1;
  381. return 1;
  382. }
  383. /*--------------------------------------------------------------------------*/
  384. /*!
  385. * \brief
  386. *
  387. * Initializes the <em>q</em> struct.
  388. *
  389. * \param q
  390. * \return int
  391. */
  392. int G_quant_init(struct Quant *quant)
  393. {
  394. quant->fp_lookup.active = 0;
  395. quant->maxNofRules = 0;
  396. quant->truncate_only = 0;
  397. quant->round_only = 0;
  398. G_quant_clear(quant);
  399. return 1;
  400. }
  401. /*--------------------------------------------------------------------------*/
  402. int G_quant_is_truncate(const struct Quant *quant)
  403. {
  404. return quant->truncate_only;
  405. }
  406. /*--------------------------------------------------------------------------*/
  407. int G_quant_is_round(const struct Quant *quant)
  408. {
  409. return quant->round_only;
  410. }
  411. /*--------------------------------------------------------------------------*/
  412. /*!
  413. * \brief
  414. *
  415. * sets the quant for <em>q</em>
  416. * rules to perform simple truncation on floats.
  417. *
  418. * \param q
  419. * \return int
  420. */
  421. /*!
  422. * \brief
  423. *
  424. * sets the quant for <em>q</em>
  425. * rules to perform simple rounding on floats.
  426. *
  427. * \param q
  428. * \return int
  429. */
  430. int G_quant_truncate(struct Quant *quant)
  431. {
  432. quant->truncate_only = 1;
  433. return 1;
  434. }
  435. /*--------------------------------------------------------------------------*/
  436. int G_quant_round(struct Quant *quant)
  437. {
  438. quant->round_only = 1;
  439. return 1;
  440. }
  441. /*--------------------------------------------------------------------------*/
  442. static void quant_set_limits(struct Quant *q,
  443. DCELL dLow, DCELL dHigh, CELL cLow, CELL cHigh)
  444. {
  445. q->dMin = dLow;
  446. q->dMax = dHigh;
  447. q->cMin = cLow;
  448. q->cMax = cHigh;
  449. }
  450. /*--------------------------------------------------------------------------*/
  451. static void quant_update_limits(struct Quant *q,
  452. DCELL dLow, DCELL dHigh,
  453. CELL cLow, DCELL cHigh)
  454. {
  455. if (NO_EXPLICIT_RULE) {
  456. quant_set_limits(q, dLow, dHigh, cLow, cHigh);
  457. return;
  458. }
  459. q->dMin = MIN(q->dMin, MIN(dLow, dHigh));
  460. q->dMax = MAX(q->dMax, MAX(dLow, dHigh));
  461. q->cMin = MIN(q->cMin, MIN(cLow, cHigh));
  462. q->cMax = MAX(q->cMax, MAX(cLow, cHigh));
  463. }
  464. /*--------------------------------------------------------------------------*/
  465. /*!
  466. * \brief
  467. *
  468. * Extracts the minimum and maximum floating-point
  469. * and integer values from all the rules (except the <tt>"infinite"</tt> rules)
  470. * in <em>q</em> into <em>dmin</em>, <em>dmax</em>, <em>cmin</em>, and <em>cmax</em>. Returns 1
  471. * if there are any explicit rules. If there are no explicit rules, (this
  472. * includes cases when q is set to truncate or round map), it returns 0 and sets
  473. * <em>dmin</em>, <em>dmax</em>, <em>cmin</em>, and <em>cmax</em> to NULL.
  474. *
  475. * \param q
  476. * \param dmin
  477. * \param dmax
  478. * \param cmin
  479. * \param cmax
  480. * \return int
  481. */
  482. int G_quant_get_limits(const struct Quant *q,
  483. DCELL * dMin, DCELL * dMax, CELL * cMin, CELL * cMax)
  484. {
  485. if (NO_EXPLICIT_RULE) {
  486. G_set_c_null_value(cMin, 1);
  487. G_set_c_null_value(cMax, 1);
  488. G_set_d_null_value(dMin, 1);
  489. G_set_d_null_value(dMax, 1);
  490. return -1;
  491. }
  492. *dMin = q->dMin;
  493. *dMax = q->dMax;
  494. *cMin = q->cMin;
  495. *cMax = q->cMax;
  496. return 1;
  497. }
  498. /*--------------------------------------------------------------------------*/
  499. int G_quant_nof_rules(const struct Quant *q)
  500. {
  501. return q->nofRules;
  502. }
  503. /*--------------------------------------------------------------------------*/
  504. void G_quant_get_ith_rule(const struct Quant *q,
  505. int i,
  506. DCELL * dLow, DCELL * dHigh,
  507. CELL * cLow, CELL * cHigh)
  508. {
  509. *dLow = q->table[i].dLow;
  510. *dHigh = q->table[i].dHigh;
  511. *cLow = q->table[i].cLow;
  512. *cHigh = q->table[i].cHigh;
  513. }
  514. /*--------------------------------------------------------------------------*/
  515. static void quant_table_increase(struct Quant *q)
  516. {
  517. if (q->nofRules < q->maxNofRules)
  518. return;
  519. if (q->maxNofRules == 0) {
  520. q->maxNofRules = 50;
  521. q->table = (struct Quant_table *)
  522. G_malloc(q->maxNofRules * sizeof(struct Quant_table));
  523. }
  524. else {
  525. q->maxNofRules += 50;
  526. q->table = (struct Quant_table *)
  527. G_realloc((char *)q->table,
  528. q->maxNofRules * sizeof(struct Quant_table));
  529. }
  530. }
  531. /*--------------------------------------------------------------------------*/
  532. void G_quant_set_neg_infinite_rule(struct Quant *q, DCELL dLeft, CELL c)
  533. {
  534. q->infiniteDLeft = dLeft;
  535. q->infiniteCLeft = c;
  536. quant_update_limits(q, dLeft, dLeft, c, c);
  537. /* update lookup table */
  538. if (q->fp_lookup.active) {
  539. q->fp_lookup.inf_dmin = q->infiniteDLeft;
  540. q->fp_lookup.inf_min = q->infiniteCLeft;
  541. }
  542. q->infiniteLeftSet = 1;
  543. }
  544. /*--------------------------------------------------------------------------*/
  545. int G_quant_get_neg_infinite_rule(const struct Quant *q,
  546. DCELL * dLeft, CELL * c)
  547. {
  548. if (q->infiniteLeftSet == 0)
  549. return 0;
  550. *dLeft = q->infiniteDLeft;
  551. *c = q->infiniteCLeft;
  552. return 1;
  553. }
  554. /*--------------------------------------------------------------------------*/
  555. void G_quant_set_pos_infinite_rule(struct Quant *q, DCELL dRight, CELL c)
  556. {
  557. q->infiniteDRight = dRight;
  558. q->infiniteCRight = c;
  559. quant_update_limits(q, dRight, dRight, c, c);
  560. /* update lookup table */
  561. if (q->fp_lookup.active) {
  562. q->fp_lookup.inf_dmax = q->infiniteDRight;
  563. q->fp_lookup.inf_max = q->infiniteCRight;
  564. }
  565. q->infiniteRightSet = 1;
  566. }
  567. /*--------------------------------------------------------------------------*/
  568. int G_quant_get_pos_infinite_rule(const struct Quant *q,
  569. DCELL * dRight, CELL * c)
  570. {
  571. if (q->infiniteRightSet == 0)
  572. return 0;
  573. *dRight = q->infiniteDRight;
  574. *c = q->infiniteCRight;
  575. return 1;
  576. }
  577. /*--------------------------------------------------------------------------*/
  578. void G_quant_add_rule(struct Quant *q,
  579. DCELL dLow, DCELL dHigh, CELL cLow, CELL cHigh)
  580. {
  581. int i;
  582. struct Quant_table *p;
  583. quant_table_increase(q);
  584. i = q->nofRules;
  585. p = &(q->table[i]);
  586. if (dHigh >= dLow) {
  587. p->dLow = dLow;
  588. p->dHigh = dHigh;
  589. p->cLow = cLow;
  590. p->cHigh = cHigh;
  591. }
  592. else {
  593. p->dLow = dHigh;
  594. p->dHigh = dLow;
  595. p->cLow = cHigh;
  596. p->cHigh = cLow;
  597. }
  598. /* destroy lookup table, it has to be rebuilt */
  599. if (q->fp_lookup.active) {
  600. G_free(q->fp_lookup.vals);
  601. G_free(q->fp_lookup.rules);
  602. q->fp_lookup.active = 0;
  603. q->fp_lookup.nalloc = 0;
  604. }
  605. quant_update_limits(q, dLow, dHigh, cLow, cHigh);
  606. q->nofRules++;
  607. }
  608. /*--------------------------------------------------------------------------*/
  609. void G_quant_reverse_rule_order(struct Quant *q)
  610. {
  611. struct Quant_table tmp;
  612. struct Quant_table *pLeft, *pRight;
  613. pLeft = q->table;
  614. pRight = &(q->table[q->nofRules - 1]);
  615. while (pLeft < pRight) {
  616. tmp.dLow = pLeft->dLow;
  617. tmp.dHigh = pLeft->dHigh;
  618. tmp.cLow = pLeft->cLow;
  619. tmp.cHigh = pLeft->cHigh;
  620. pLeft->dLow = pRight->dLow;
  621. pLeft->dHigh = pRight->dHigh;
  622. pLeft->cLow = pRight->cLow;
  623. pLeft->cHigh = pRight->cHigh;
  624. pRight->dLow = tmp.dLow;
  625. pRight->dHigh = tmp.dHigh;
  626. pRight->cLow = tmp.cLow;
  627. pRight->cHigh = tmp.cHigh;
  628. pLeft++;
  629. pRight--;
  630. }
  631. }
  632. /*--------------------------------------------------------------------------*/
  633. static CELL quant_interpolate(DCELL dLow, DCELL dHigh,
  634. CELL cLow, CELL cHigh, DCELL dValue)
  635. {
  636. if (cLow == cHigh)
  637. return cLow;
  638. if (dLow == dHigh)
  639. return cLow;
  640. return (CELL) ((dValue - dLow) / (dHigh - dLow) * (DCELL) (cHigh - cLow) +
  641. (DCELL) cLow);
  642. }
  643. /*--------------------------------------------------------------------------*/
  644. static int less_or_equal(double x, double y)
  645. {
  646. if (x <= y)
  647. return 1;
  648. else
  649. return 0;
  650. }
  651. static int less(double x, double y)
  652. {
  653. if (x < y)
  654. return 1;
  655. else
  656. return 0;
  657. }
  658. /*!
  659. * \brief
  660. *
  661. *
  662. * Returns a CELL category for the floating-point <em>value</em> based on the
  663. * quantization rules in <em>q</em>. The first rule found that applies is used.
  664. * The rules are searched in the reverse order they are added to <em>q</em>. If no
  665. * rule is found, the <em>value</em> is first tested against the negative infinite
  666. * rule, and finally against the positive infinite rule. if none of these rules
  667. * apply, the NULL-value is returned.
  668. * <b>NOTE.</b> See G_quant_organize_fp_lookup() for details on how the
  669. * values are looked up from fp_lookup table when it is active. (Right now
  670. * fp_lookup is automatically organized during the first call to
  671. * G_quant_get_cell_value()
  672. *
  673. * \param q
  674. * \param value
  675. * \return CELL
  676. */
  677. CELL G_quant_get_cell_value(struct Quant * q, DCELL dcellVal)
  678. {
  679. CELL tmp;
  680. DCELL dtmp;
  681. int try, min_ind, max_ind;
  682. struct Quant_table *p;
  683. int (*lower) ();
  684. dtmp = dcellVal;
  685. /* I know the functions which call me already check for null values,
  686. but I am a public function, and can be called from outside */
  687. if (G_is_d_null_value(&dtmp))
  688. return NO_DATA;
  689. if (q->truncate_only)
  690. return (CELL) dtmp;
  691. if (q->round_only) {
  692. if (dcellVal > 0)
  693. return (CELL) (dcellVal + .5);
  694. return (CELL) (dcellVal - .5);
  695. }
  696. if (NO_EXPLICIT_RULE)
  697. return NO_DATA;
  698. if (NO_EXPLICIT_RULE)
  699. return NO_DATA;
  700. if (USE_LOOKUP &&
  701. (q->fp_lookup.active || G__quant_organize_fp_lookup(q) > 0)) {
  702. /* first check if values fall within range */
  703. /* if value is below the range */
  704. if (dcellVal < q->fp_lookup.vals[0]) {
  705. if (dcellVal <= q->fp_lookup.inf_dmin)
  706. return q->fp_lookup.inf_min;
  707. else
  708. return NO_DATA;
  709. }
  710. /* if value is below above range */
  711. if (dcellVal > q->fp_lookup.vals[q->fp_lookup.nalloc - 1]) {
  712. if (dcellVal >= q->fp_lookup.inf_dmax)
  713. return q->fp_lookup.inf_max;
  714. else
  715. return NO_DATA;
  716. }
  717. /* make binary search to find which interval our value belongs to
  718. and apply the rule for this interval */
  719. try = (q->fp_lookup.nalloc - 1) / 2;
  720. min_ind = 0;
  721. max_ind = q->fp_lookup.nalloc - 2;
  722. while (1) {
  723. /* DEBUG
  724. fprintf (stderr, "%d %d %d\n", min_ind, max_ind, try);
  725. */
  726. /* when the ruke for the interval is NULL, we exclude the end points.
  727. when it exists, we include the end-points */
  728. if (q->fp_lookup.rules[try])
  729. lower = less;
  730. else
  731. lower = less_or_equal;
  732. if (lower(q->fp_lookup.vals[try + 1], dcellVal)) { /* recurse to the second half */
  733. min_ind = try + 1;
  734. /* must be still < nalloc-1, since number is within the range */
  735. try = (max_ind + min_ind) / 2;
  736. continue;
  737. }
  738. if (lower(dcellVal, q->fp_lookup.vals[try])) { /* recurse to the second half */
  739. max_ind = try - 1;
  740. /* must be still >= 0, since number is within the range */
  741. try = (max_ind + min_ind) / 2;
  742. continue;
  743. }
  744. /* the value fits into the interval! */
  745. p = q->fp_lookup.rules[try];
  746. if (p)
  747. return quant_interpolate(p->dLow, p->dHigh, p->cLow, p->cHigh,
  748. dcellVal);
  749. /* otherwise when finite rule for this interval doesn't exist */
  750. else { /* first check if maybe infinite rule applies */
  751. if (dcellVal <= q->fp_lookup.inf_dmin)
  752. return q->fp_lookup.inf_min;
  753. if (dcellVal >= q->fp_lookup.inf_dmax)
  754. return q->fp_lookup.inf_max;
  755. else
  756. return NO_DATA;
  757. }
  758. } /* while */
  759. } /* looking up in fp_lookup */
  760. if (!NO_FINITE_RULE) {
  761. p = G__quant_get_rule_for_d_raster_val(q, dcellVal);
  762. if (!p)
  763. return NO_DATA;
  764. return quant_interpolate(p->dLow, p->dHigh, p->cLow, p->cHigh,
  765. dcellVal);
  766. }
  767. if ((!NO_LEFT_INFINITE_RULE) && (dcellVal <= q->infiniteDLeft))
  768. return q->infiniteCLeft;
  769. if ((NO_RIGHT_INFINITE_RULE) || (dcellVal < q->infiniteDRight))
  770. return NO_DATA;
  771. return q->infiniteCRight;
  772. }
  773. /*--------------------------------------------------------------------------*/
  774. void G_quant_perform_d(struct Quant *q,
  775. const DCELL * dcell, CELL * cell, int n)
  776. {
  777. int i;
  778. for (i = 0; i < n; i++, dcell++)
  779. if (!G_is_d_null_value(dcell))
  780. *cell++ = G_quant_get_cell_value(q, *dcell);
  781. else
  782. G_set_c_null_value(cell++, 1);
  783. }
  784. /*--------------------------------------------------------------------------*/
  785. void G_quant_perform_f(struct Quant *q,
  786. const FCELL * fcell, CELL * cell, int n)
  787. {
  788. int i;
  789. for (i = 0; i < n; i++, fcell++)
  790. if (!G_is_f_null_value(fcell))
  791. *cell++ = G_quant_get_cell_value(q, (DCELL) * fcell);
  792. else
  793. G_set_c_null_value(cell++, 1);
  794. }
  795. /*--------------------------------------------------------------------------*/
  796. static int double_comp(const void *xx, const void *yy)
  797. {
  798. const DCELL *x = xx;
  799. const DCELL *y = yy;
  800. if (G_is_d_null_value(x))
  801. return 0;
  802. if (*x < *y)
  803. return -1;
  804. else if (*x == *y)
  805. return 0;
  806. else
  807. return 1;
  808. }
  809. /*--------------------------------------------------------------------------*/
  810. struct Quant_table *G__quant_get_rule_for_d_raster_val(const struct Quant *q,
  811. DCELL val)
  812. {
  813. const struct Quant_table *p;
  814. for (p = &(q->table[q->nofRules - 1]); p >= q->table; p--)
  815. if ((val >= p->dLow) && (val <= p->dHigh))
  816. break;
  817. if (p >= q->table)
  818. return (struct Quant_table *)p;
  819. else
  820. return (struct Quant_table *)NULL;
  821. }
  822. /*--------------------------------------------------------------------------*/
  823. /*--------------------------------------------------------------------------*/
  824. /*--------------------------------------------------------------------------*/