color_rule.c 12 KB

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