color_rule.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /**
  2. * \file color_rule.c
  3. *
  4. * \brief GIS Library - Color rules.
  5. *
  6. * (C) 2001-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author GRASS GIS Development Team
  12. *
  13. * \date 1999-2008
  14. */
  15. #include <grass/gis.h>
  16. #define LIMIT(x) if (x < 0) x = 0; else if (x > 255) x = 255;
  17. static void add_color_rule(const void *, int, int, int,
  18. const void *, int, int, int,
  19. struct _Color_Info_ *, int,
  20. DCELL *, DCELL *, RASTER_MAP_TYPE);
  21. /*!
  22. * \brief Adds the floating-point rule (DCELL version)
  23. *
  24. * See G_add_raster_color_rule() for details.
  25. *
  26. * \param v1 cell value
  27. * \param r1,g1,b1 color value
  28. * \param v2 cell value
  29. * \param r2,g2,b2 color value
  30. * \param[in,out] colors pointer to color table structure
  31. * \return
  32. */
  33. void G_add_d_raster_color_rule(
  34. const DCELL * val1, int r1, int g1, int b1,
  35. const DCELL * val2, int r2, int g2, int b2,
  36. struct Colors *colors)
  37. {
  38. add_color_rule(val1, r1, g1, b1, val2, r2, g2, b2, &colors->fixed,
  39. colors->version, &colors->cmin, &colors->cmax, DCELL_TYPE);
  40. }
  41. /*!
  42. * \brief Adds the floating-point rule (FCELL version)
  43. *
  44. * See G_add_raster_color_rule() for details.
  45. *
  46. * \param v1 cell value
  47. * \param r1,g1,b1 color value
  48. * \param v2 cell value
  49. * \param r2,g2,b2 color value
  50. * \param[in,out] colors pointer to color table structure
  51. * \return
  52. */
  53. void G_add_f_raster_color_rule(
  54. const FCELL * cat1, int r1, int g1, int b1,
  55. const FCELL * cat2, int r2, int g2, int b2,
  56. struct Colors *colors)
  57. {
  58. add_color_rule(cat1, r1, g1, b1, cat2, r2, g2, b2, &colors->fixed,
  59. colors->version, &colors->cmin, &colors->cmax, FCELL_TYPE);
  60. }
  61. /*!
  62. * \brief Adds the floating-point rule (CCELL version)
  63. *
  64. * See G_add_raster_color_rule() for details.
  65. *
  66. * \param v1 cell value
  67. * \param r1,g1,b1 color value
  68. * \param v2 cell value
  69. * \param r2,g2,b2 color value
  70. * \param[in,out] colors pointer to color table structure
  71. * \return
  72. */
  73. void G_add_c_raster_color_rule(
  74. const CELL * cat1, int r1, int g1, int b1,
  75. const CELL * cat2, int r2, int g2, int b2,
  76. struct Colors *colors)
  77. {
  78. add_color_rule(cat1, r1, g1, b1, cat2, r2, g2, b2, &colors->fixed,
  79. colors->version, &colors->cmin, &colors->cmax, CELL_TYPE);
  80. }
  81. /*!
  82. * \brief Adds the floating-point rule
  83. *
  84. * Adds the floating-point rule that the range [<em>v1,v2</em>] gets a
  85. * linear ramp of colors from [<em>r1,g1,b1</em>] to
  86. * [<em>r2,g2,b2</em>].
  87. * If either <em>v1</em> or <em>v2</em> is the NULL-value, this call is converted into
  88. * <tt>G_set_null_value_color (r1, g1, b1, colors)</tt>
  89. *
  90. * - If <em>map_type</em> is CELL_TYPE, calls G_add_c_raster_color_rule()
  91. * - If <em>map_type</em> is FCELL_TYPE, calls G_add_f_raster_color_rule()
  92. * - If <em>map_type</em> is DCELL_TYPE, calls G_add_d_raster_color_rule()
  93. *
  94. * \param v1 cell value
  95. * \param r1,g1,b1 color value
  96. * \param v2 cell value
  97. * \param r2,g2,b2 color value
  98. * \param[in,out] colors pointer to color table structure
  99. * \param data_type raster data type (CELL, FCELL, DCELL)
  100. * \return
  101. */
  102. void G_add_raster_color_rule(
  103. const void *val1, int r1, int g1, int b1,
  104. const void *val2, int r2, int g2, int b2,
  105. struct Colors *colors, RASTER_MAP_TYPE data_type)
  106. {
  107. add_color_rule(val1, r1, g1, b1, val2, r2, g2, b2, &colors->fixed,
  108. colors->version, &colors->cmin, &colors->cmax, data_type);
  109. }
  110. /*!
  111. * \brief Set colors rules
  112. *
  113. * This is the heart
  114. * and soul of the new color logic. It adds a color rule to the <b>colors</b>
  115. * structure. The colors defined by the red, green, and blue values
  116. * <b>r1,g1,b1</b> and <b>r2,g2,b2</b> are assigned to <b>cat1</b> and
  117. * <b>cat2</b> respectively. Colors for data values between <b>cat1</b> and
  118. * <b>cat2</b> are not stored in the structure but are interpolated when
  119. * queried by <i>G_lookup_colors</i> and<i>G_get_color.</i> The color
  120. * components <b>r1,g1,b1</b> and <b>r2,g2,b2</b> must be in the range
  121. * 0 -- 255.
  122. * For example, to create a linear grey scale for the range 200 -- 1000:
  123. \code
  124. struct Colors colr;
  125. G_init_colors (&colr);
  126. G_add_color_rule ((CELL)200, 0,0,0, (CELL)1000, 255,255,255);
  127. \endcode
  128. * The programmer is encouraged to review Raster_Color_Table_Format how
  129. * this routine fits into the 5.x raster color logic.
  130. * <b>Note.</b> The <b>colors</b> structure must have been initialized by
  131. * <i>G_init_colors.</i> See Predefined_Color_Tables for routines to
  132. * build some predefined color tables.
  133. *
  134. * \param cat1 cell value
  135. * \param r1,g1,b1 color value
  136. * \param cat2 cell value
  137. * \param r2,g2,b2 color value
  138. * \param[in,out] colors pointer to color table structure
  139. * \return
  140. */
  141. void G_add_color_rule(
  142. CELL cat1, int r1, int g1, int b1,
  143. CELL cat2, int r2, int g2,
  144. int b2, struct Colors *colors)
  145. {
  146. add_color_rule((void *)&cat1, r1, g1, b1, (void *)&cat2, r2, g2, b2,
  147. &colors->fixed, colors->version, &colors->cmin,
  148. &colors->cmax, CELL_TYPE);
  149. }
  150. /**
  151. * \brief Add modular color rule (DCELL version)
  152. *
  153. * \param val1 cell value
  154. * \param r1,g1,b1 color value
  155. * \param val2 cell value
  156. * \param r2,g2,b2 color value
  157. * \param[in,out] colors pointer to color table structure
  158. *
  159. * \return -1 on failure
  160. * \return 1 on success
  161. */
  162. int G_add_modular_d_raster_color_rule(
  163. const DCELL * val1, int r1, int g1, int b1,
  164. const DCELL * val2, int r2, int g2, int b2,
  165. struct Colors *colors)
  166. {
  167. DCELL min, max;
  168. if (colors->version < 0)
  169. return -1; /* can't use this on 3.0 colors */
  170. min = colors->cmin;
  171. max = colors->cmax;
  172. add_color_rule(val1, r1, g1, b1, val2, r2, g2, b2, &colors->modular, 0,
  173. &colors->cmin, &colors->cmax, DCELL_TYPE);
  174. colors->cmin = min; /* don't reset these */
  175. colors->cmax = max;
  176. return 1;
  177. }
  178. /**
  179. * \brief Add modular color rule (FCELL version)
  180. *
  181. * \param val1 cell value
  182. * \param r1,g1,b1 color value
  183. * \param val2 cell value
  184. * \param r2,g2,b2 color value
  185. * \param[in,out] colors pointer to color table structure
  186. *
  187. * \return -1 on failure
  188. * \return 1 on success
  189. */
  190. int G_add_modular_f_raster_color_rule(
  191. const FCELL * val1, int r1, int g1, int b1,
  192. const FCELL * val2, int r2, int g2, int b2,
  193. struct Colors *colors)
  194. {
  195. DCELL min, max;
  196. if (colors->version < 0)
  197. return -1; /* can;t use this on 3.0 colors */
  198. min = colors->cmin;
  199. max = colors->cmax;
  200. add_color_rule(val1, r1, g1, b1, val2, r2, g2, b2, &colors->modular, 0,
  201. &colors->cmin, &colors->cmax, FCELL_TYPE);
  202. colors->cmin = min; /* don't reset these */
  203. colors->cmax = max;
  204. return 1;
  205. }
  206. /**
  207. * \brief Add modular color rule (CCELL version)
  208. *
  209. * \param val1 cell value
  210. * \param r1,g1,b1 color value
  211. * \param val2 cell value
  212. * \param r2,g2,b2 color value
  213. * \param[in,out] colors pointer to color table structure
  214. *
  215. * \return -1 on failure
  216. * \return 1 on success
  217. */
  218. int G_add_modular_c_raster_color_rule(
  219. const CELL * val1, int r1, int g1, int b1,
  220. const CELL * val2, int r2, int g2, int b2,
  221. struct Colors *colors)
  222. {
  223. return G_add_modular_color_rule(*val1, r1, g1, b1, *val2, r2, g2, b2,
  224. colors);
  225. }
  226. /**
  227. * \brief Add modular color rule
  228. *
  229. * Question: shouldn't this function call
  230. * G_add_modular_<data_type>_raster_color_rule() instead???
  231. *
  232. * \param val1 cell value
  233. * \param r1,g1,b1 color value
  234. * \param val2 cell value
  235. * \param r2,g2,b2 color value
  236. * \param[in,out] colors pointer to color table structure
  237. * \param data_type raster data type
  238. *
  239. * \return -1 on failure
  240. * \return 1 on success
  241. */
  242. int G_add_modular_raster_color_rule(
  243. const void *val1, int r1, int g1, int b1,
  244. const void *val2, int r2, int g2, int b2,
  245. struct Colors *colors, RASTER_MAP_TYPE data_type)
  246. {
  247. CELL min, max;
  248. if (colors->version < 0)
  249. return -1; /* can't use this on 3.0 colors */
  250. min = colors->cmin;
  251. max = colors->cmax;
  252. add_color_rule(val1, r1, g1, b1, val2, r2, g2, b2, &colors->modular, 0,
  253. &colors->cmin, &colors->cmax, data_type);
  254. colors->cmin = min; /* don't reset these */
  255. colors->cmax = max;
  256. return 1;
  257. }
  258. /**
  259. * \brief Add modular color rule
  260. *
  261. * This function seems to be same as
  262. * G_add_modular_raster_color_rule(). Can be removed?
  263. *
  264. * \param val1 cell value
  265. * \param r1,g1,b1 color value
  266. * \param val2 cell value
  267. * \param r2,g2,b2 color value
  268. * \param[in,out] colors pointer to color table structure
  269. * \param data_type raster data type
  270. *
  271. * \return -1 on failure
  272. * \return 1 on success
  273. */
  274. int G_add_modular_color_rule(
  275. CELL cat1, int r1, int g1,
  276. int b1, CELL cat2, int r2,
  277. int g2, int b2, struct Colors *colors)
  278. {
  279. CELL min, max;
  280. if (colors->version < 0)
  281. return -1; /* can;t use this on 3.0 colors */
  282. min = colors->cmin;
  283. max = colors->cmax;
  284. add_color_rule((void *)&cat1, r1, g1, b1, (void *)&cat2, r2, g2, b2,
  285. &colors->modular, 0, &colors->cmin, &colors->cmax,
  286. CELL_TYPE);
  287. colors->cmin = min; /* don't reset these */
  288. colors->cmax = max;
  289. return 1;
  290. }
  291. static void add_color_rule(const void *pt1, int r1, int g1, int b1,
  292. const void *pt2, int r2, int g2, int b2,
  293. struct _Color_Info_ *cp, int version, DCELL * cmin,
  294. DCELL * cmax, RASTER_MAP_TYPE data_type)
  295. {
  296. struct _Color_Rule_ *rule, *next;
  297. unsigned char red, grn, blu;
  298. DCELL min, max, val1, val2;
  299. CELL cat;
  300. val1 = G_get_raster_value_d(pt1, data_type);
  301. val2 = G_get_raster_value_d(pt2, data_type);
  302. /* allocate a low:high rule */
  303. rule = (struct _Color_Rule_ *)G_malloc(sizeof(*rule));
  304. rule->next = rule->prev = NULL;
  305. /* make sure colors are in the range [0,255] */
  306. LIMIT(r1);
  307. LIMIT(g1);
  308. LIMIT(b1);
  309. LIMIT(r2);
  310. LIMIT(g2);
  311. LIMIT(b2);
  312. /* val1==val2, use average color */
  313. /* otherwise make sure low < high */
  314. if (val1 == val2) {
  315. rule->low.value = rule->high.value = val1;
  316. rule->low.red = rule->high.red = (r1 + r2) / 2;
  317. rule->low.grn = rule->high.grn = (g1 + g2) / 2;
  318. rule->low.blu = rule->high.blu = (b1 + b2) / 2;
  319. }
  320. else if (val1 < val2) {
  321. rule->low.value = val1;
  322. rule->low.red = r1;
  323. rule->low.grn = g1;
  324. rule->low.blu = b1;
  325. rule->high.value = val2;
  326. rule->high.red = r2;
  327. rule->high.grn = g2;
  328. rule->high.blu = b2;
  329. }
  330. else {
  331. rule->low.value = val2;
  332. rule->low.red = r2;
  333. rule->low.grn = g2;
  334. rule->low.blu = b2;
  335. rule->high.value = val1;
  336. rule->high.red = r1;
  337. rule->high.grn = g1;
  338. rule->high.blu = b1;
  339. }
  340. /* keep track of the overall min and max, excluding null */
  341. if (G_is_d_null_value(&(rule->low.value)))
  342. return;
  343. if (G_is_d_null_value(&(rule->high.value)))
  344. return;
  345. min = rule->low.value;
  346. max = rule->high.value;
  347. if (min <= max) {
  348. if (cp->min > cp->max) {
  349. cp->min = min;
  350. cp->max = max;
  351. }
  352. else {
  353. if (cp->min > min)
  354. cp->min = min;
  355. if (cp->max < max)
  356. cp->max = max;
  357. }
  358. }
  359. if (*cmin > *cmax) {
  360. *cmin = cp->min;
  361. *cmax = cp->max;
  362. }
  363. else {
  364. if (*cmin > cp->min)
  365. *cmin = cp->min;
  366. if (*cmax < cp->max)
  367. *cmax = cp->max;
  368. }
  369. /* If version is old style (i.e., pre 4.0),
  370. * interpolate this rule from min to max
  371. * and insert each cat into the lookup table.
  372. * Then free the rule.
  373. * Otherwise, free the lookup table, if active.
  374. * G_organize_colors() will regenerate it
  375. * Link this rule into the list of rules
  376. */
  377. if (version < 0) {
  378. for (cat = (CELL) min; cat <= (CELL) max; cat++) {
  379. G__interpolate_color_rule((DCELL) cat, &red, &grn, &blu, rule);
  380. G__insert_color_into_lookup(cat, (int)red, (int)grn, (int)blu,
  381. cp);
  382. }
  383. G_free(rule);
  384. }
  385. else {
  386. if (cp->rules)
  387. cp->rules->prev = rule;
  388. rule->next = cp->rules;
  389. cp->rules = rule;
  390. /* prune the rules:
  391. * remove all rules that are contained by this rule
  392. */
  393. min = rule->low.value; /* mod 4.1 */
  394. max = rule->high.value; /* mod 4.1 */
  395. cp->n_rules++;
  396. for (rule = rule->next; rule; rule = next) {
  397. next = rule->next; /* has to be done here, not in for stmt */
  398. if (min <= rule->low.value && max >= rule->high.value) {
  399. if ((rule->prev->next = next)) /* remove from the list */
  400. next->prev = rule->prev;
  401. G_free(rule);
  402. cp->n_rules--;
  403. }
  404. }
  405. /* free lookup array, if allocated */
  406. G__color_free_lookup(cp);
  407. G__color_free_fp_lookup(cp);
  408. }
  409. }