color_rule.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*!
  2. \file lib/raster/color_rule.c
  3. \brief Raster Library - Color rules.
  4. (C) 2001-2009 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. */
  9. #include <grass/gis.h>
  10. #include <grass/raster.h>
  11. #define LIMIT(x) if (x < 0) x = 0; else if (x > 255) x = 255;
  12. static void add_color_rule(const void *, int, int, int,
  13. const void *, int, int, int,
  14. struct _Color_Info_ *, int,
  15. DCELL *, DCELL *, RASTER_MAP_TYPE);
  16. /*!
  17. \brief Adds the floating-point color rule (DCELL version)
  18. See Rast_add_color_rule() for details.
  19. \param val1 cell value
  20. \param r1,g1,b1 color value
  21. \param val2 cell value
  22. \param r2,g2,b2 color value
  23. \param[in,out] colors pointer to color table structure
  24. */
  25. void Rast_add_d_color_rule(const DCELL * val1, int r1, int g1, int b1,
  26. const DCELL * val2, int r2, int g2, int b2,
  27. struct Colors *colors)
  28. {
  29. add_color_rule(val1, r1, g1, b1, val2, r2, g2, b2, &colors->fixed,
  30. colors->version, &colors->cmin, &colors->cmax, DCELL_TYPE);
  31. }
  32. /*!
  33. \brief Adds the floating-point color rule (FCELL version)
  34. See Rast_add_color_rule() for details.
  35. \param cat1 cell value
  36. \param r1,g1,b1 color value
  37. \param cat2 cell value
  38. \param r2,g2,b2 color value
  39. \param[in,out] colors pointer to color table structure
  40. */
  41. void Rast_add_f_color_rule(const FCELL * cat1, int r1, int g1, int b1,
  42. const FCELL * cat2, int r2, int g2, int b2,
  43. struct Colors *colors)
  44. {
  45. add_color_rule(cat1, r1, g1, b1, cat2, r2, g2, b2, &colors->fixed,
  46. colors->version, &colors->cmin, &colors->cmax, FCELL_TYPE);
  47. }
  48. /*!
  49. \brief Adds the integer color rule (CELL version)
  50. See Rast_add_color_rule() for details.
  51. \param cat1 cell value
  52. \param r1,g1,b1 color value
  53. \param cat2 cell value
  54. \param r2,g2,b2 color value
  55. \param[in,out] colors pointer to color table structure
  56. */
  57. void Rast_add_c_color_rule(const CELL * cat1, int r1, int g1, int b1,
  58. const CELL * cat2, int r2, int g2, int b2,
  59. struct Colors *colors)
  60. {
  61. add_color_rule(cat1, r1, g1, b1, cat2, r2, g2, b2, &colors->fixed,
  62. colors->version, &colors->cmin, &colors->cmax, CELL_TYPE);
  63. }
  64. /*!
  65. \brief Adds the color rule
  66. Adds the floating-point rule that the range [<em>v1,v2</em>] gets a
  67. linear ramp of colors from [<em>r1,g1,b1</em>] to
  68. [<em>r2,g2,b2</em>].
  69. If either <em>v1</em> or <em>v2</em> is the NULL-value, this call is converted ino
  70. <tt>Rast_set_null_value_color (r1, g1, b1, colors)</tt>
  71. - If <em>map_type</em> is CELL_TYPE, calls Rast_add_c_color_rule()
  72. - If <em>map_type</em> is FCELL_TYPE, calls Rast_add_f_color_rule()
  73. - If <em>map_type</em> is DCELL_TYPE, calls Rast_add_d_color_rule()
  74. \param val1 cell value
  75. \param r1,g1,b1 color value
  76. \param val2 cell value
  77. \param r2,g2,b2 color value
  78. \param[in,out] colors pointer to color table structure
  79. \param data_type raster data type (CELL, FCELL, DCELL)
  80. */
  81. void Rast_add_color_rule(const void *val1, int r1, int g1, int b1,
  82. const void *val2, int r2, int g2, int b2,
  83. struct Colors *colors, RASTER_MAP_TYPE data_type)
  84. {
  85. add_color_rule(val1, r1, g1, b1, val2, r2, g2, b2, &colors->fixed,
  86. colors->version, &colors->cmin, &colors->cmax, data_type);
  87. }
  88. /*!
  89. \brief Add modular floating-point color rule (DCELL version)
  90. \param val1 cell value
  91. \param r1,g1,b1 color value
  92. \param val2 cell value
  93. \param r2,g2,b2 color value
  94. \param[in,out] colors pointer to color table structure
  95. \return -1 on failure
  96. \return 1 on success
  97. */
  98. int Rast_add_modular_d_color_rule(const DCELL * val1, int r1, int g1, int b1,
  99. const DCELL * val2, int r2, int g2, int b2,
  100. struct Colors *colors)
  101. {
  102. DCELL min, max;
  103. if (colors->version < 0)
  104. return -1; /* can't use this on 3.0 colors */
  105. min = colors->cmin;
  106. max = colors->cmax;
  107. add_color_rule(val1, r1, g1, b1, val2, r2, g2, b2, &colors->modular, 0,
  108. &colors->cmin, &colors->cmax, DCELL_TYPE);
  109. colors->cmin = min; /* don't reset these */
  110. colors->cmax = max;
  111. return 1;
  112. }
  113. /*!
  114. \brief Add modular floating-point color rule (FCELL version)
  115. \param val1 cell value
  116. \param r1,g1,b1 color value
  117. \param val2 cell value
  118. \param r2,g2,b2 color value
  119. \param[in,out] colors pointer to color table structure
  120. \return -1 on failure
  121. \return 1 on success
  122. */
  123. int Rast_add_modular_f_color_rule(const FCELL * val1, int r1, int g1, int b1,
  124. const FCELL * val2, int r2, int g2, int b2,
  125. struct Colors *colors)
  126. {
  127. DCELL min, max;
  128. if (colors->version < 0)
  129. return -1; /* can;t use this on 3.0 colors */
  130. min = colors->cmin;
  131. max = colors->cmax;
  132. add_color_rule(val1, r1, g1, b1, val2, r2, g2, b2, &colors->modular, 0,
  133. &colors->cmin, &colors->cmax, FCELL_TYPE);
  134. colors->cmin = min; /* don't reset these */
  135. colors->cmax = max;
  136. return 1;
  137. }
  138. /*!
  139. \brief Add modular integer color rule (CELL version)
  140. \param val1 cell value
  141. \param r1,g1,b1 color value
  142. \param val2 cell value
  143. \param r2,g2,b2 color value
  144. \param[in,out] colors pointer to color table structure
  145. \return -1 on failure
  146. \return 1 on success
  147. */
  148. int Rast_add_modular_c_color_rule(const CELL * val1, int r1, int g1, int b1,
  149. const CELL * val2, int r2, int g2, int b2,
  150. struct Colors *colors)
  151. {
  152. CELL min, max;
  153. if (colors->version < 0)
  154. return -1; /* can;t use this on 3.0 colors */
  155. min = colors->cmin;
  156. max = colors->cmax;
  157. add_color_rule(val1, r1, g1, b1, val2, r2, g2, b2, &colors->modular, 0,
  158. &colors->cmin, &colors->cmax, CELL_TYPE);
  159. colors->cmin = min; /* don't reset these */
  160. colors->cmax = max;
  161. return 1;
  162. }
  163. /*!
  164. \brief Add modular color rule
  165. \todo Question: shouldn't this function call
  166. G_add_modular_<data_type>_raster_color_rule() instead?
  167. \param val1 cell value
  168. \param r1,g1,b1 color value
  169. \param val2 cell value
  170. \param r2,g2,b2 color value
  171. \param[in,out] colors pointer to color table structure
  172. \param data_type raster data type
  173. \return -1 on failure
  174. \return 1 on success
  175. */
  176. int Rast_add_modular_color_rule(const void *val1, int r1, int g1, int b1,
  177. const void *val2, int r2, int g2, int b2,
  178. struct Colors *colors,
  179. RASTER_MAP_TYPE data_type)
  180. {
  181. CELL min, max;
  182. if (colors->version < 0)
  183. return -1; /* can't use this on 3.0 colors */
  184. min = colors->cmin;
  185. max = colors->cmax;
  186. add_color_rule(val1, r1, g1, b1, val2, r2, g2, b2, &colors->modular, 0,
  187. &colors->cmin, &colors->cmax, data_type);
  188. colors->cmin = min; /* don't reset these */
  189. colors->cmax = max;
  190. return 1;
  191. }
  192. static void add_color_rule(const void *pt1, int r1, int g1, int b1,
  193. const void *pt2, int r2, int g2, int b2,
  194. struct _Color_Info_ *cp, int version, DCELL * cmin,
  195. DCELL * cmax, RASTER_MAP_TYPE data_type)
  196. {
  197. struct _Color_Rule_ *rule, *next;
  198. unsigned char red, grn, blu;
  199. DCELL min, max, val1, val2;
  200. CELL cat;
  201. val1 = Rast_get_d_value(pt1, data_type);
  202. val2 = Rast_get_d_value(pt2, data_type);
  203. /* allocate a low:high rule */
  204. rule = (struct _Color_Rule_ *)G_malloc(sizeof(*rule));
  205. rule->next = rule->prev = NULL;
  206. /* make sure colors are in the range [0,255] */
  207. LIMIT(r1);
  208. LIMIT(g1);
  209. LIMIT(b1);
  210. LIMIT(r2);
  211. LIMIT(g2);
  212. LIMIT(b2);
  213. /* val1==val2, use average color */
  214. /* otherwise make sure low < high */
  215. if (val1 == val2) {
  216. rule->low.value = rule->high.value = val1;
  217. rule->low.red = rule->high.red = (r1 + r2) / 2;
  218. rule->low.grn = rule->high.grn = (g1 + g2) / 2;
  219. rule->low.blu = rule->high.blu = (b1 + b2) / 2;
  220. }
  221. else if (val1 < val2) {
  222. rule->low.value = val1;
  223. rule->low.red = r1;
  224. rule->low.grn = g1;
  225. rule->low.blu = b1;
  226. rule->high.value = val2;
  227. rule->high.red = r2;
  228. rule->high.grn = g2;
  229. rule->high.blu = b2;
  230. }
  231. else {
  232. rule->low.value = val2;
  233. rule->low.red = r2;
  234. rule->low.grn = g2;
  235. rule->low.blu = b2;
  236. rule->high.value = val1;
  237. rule->high.red = r1;
  238. rule->high.grn = g1;
  239. rule->high.blu = b1;
  240. }
  241. /* keep track of the overall min and max, excluding null */
  242. if (Rast_is_d_null_value(&(rule->low.value)))
  243. return;
  244. if (Rast_is_d_null_value(&(rule->high.value)))
  245. return;
  246. min = rule->low.value;
  247. max = rule->high.value;
  248. if (min <= max) {
  249. if (cp->min > cp->max) {
  250. cp->min = min;
  251. cp->max = max;
  252. }
  253. else {
  254. if (cp->min > min)
  255. cp->min = min;
  256. if (cp->max < max)
  257. cp->max = max;
  258. }
  259. }
  260. if (*cmin > *cmax) {
  261. *cmin = cp->min;
  262. *cmax = cp->max;
  263. }
  264. else {
  265. if (*cmin > cp->min)
  266. *cmin = cp->min;
  267. if (*cmax < cp->max)
  268. *cmax = cp->max;
  269. }
  270. /* If version is old style (i.e., pre 4.0),
  271. * interpolate this rule from min to max
  272. * and insert each cat into the lookup table.
  273. * Then free the rule.
  274. * Otherwise, free the lookup table, if active.
  275. * G_organize_colors() will regenerate it
  276. * Link this rule into the list of rules
  277. */
  278. if (version < 0) {
  279. for (cat = (CELL) min; cat <= (CELL) max; cat++) {
  280. Rast__interpolate_color_rule((DCELL) cat, &red, &grn, &blu, rule);
  281. Rast__insert_color_into_lookup(cat, (int)red, (int)grn, (int)blu,
  282. cp);
  283. }
  284. G_free(rule);
  285. }
  286. else {
  287. if (cp->rules)
  288. cp->rules->prev = rule;
  289. rule->next = cp->rules;
  290. cp->rules = rule;
  291. /* prune the rules:
  292. * remove all rules that are contained by this rule
  293. */
  294. min = rule->low.value; /* mod 4.1 */
  295. max = rule->high.value; /* mod 4.1 */
  296. cp->n_rules++;
  297. for (rule = rule->next; rule; rule = next) {
  298. next = rule->next; /* has to be done here, not in for stmt */
  299. if (min <= rule->low.value && max >= rule->high.value) {
  300. if ((rule->prev->next = next)) /* remove from the list */
  301. next->prev = rule->prev;
  302. G_free(rule);
  303. cp->n_rules--;
  304. }
  305. }
  306. /* free lookup array, if allocated */
  307. Rast__color_free_lookup(cp);
  308. Rast__color_free_fp_lookup(cp);
  309. }
  310. }