color_hist.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*!
  2. * \file lib/raster/color_hist.c
  3. *
  4. * \brief Raster Library - histogram grey scale colors
  5. *
  6. * (C) 2007-2009 Glynn Clements and 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 Glynn Clements <glynn@gclements.plus.com>
  12. */
  13. #include <math.h>
  14. #include <grass/gis.h>
  15. #include <grass/raster.h>
  16. #include <grass/colors.h>
  17. /*!
  18. * \brief Make histogram-stretched grey colors
  19. *
  20. * Generates a histogram contrast-stretched grey scale color table
  21. * that goes from the, histogram information in the Cell_stats
  22. * structure (see \ref Raster_Histograms).
  23. *
  24. * Color range is 0-255.
  25. *
  26. * \param colors pointer to Colors structure which holds color info
  27. * \param statf pointer to Cell_stats structure which holds cell stats info
  28. */
  29. void Rast_make_histogram_eq_colors(struct Colors *colors,
  30. struct Cell_stats *statf)
  31. {
  32. long count, total;
  33. CELL prev = 0, cat, val2;
  34. double span, sum;
  35. int first;
  36. int x, grey;
  37. int R, G, B;
  38. Rast_init_colors(colors);
  39. G_str_to_color(DEFAULT_BG_COLOR, &R, &G, &B);
  40. Rast_set_null_value_color(R, G, B, colors);
  41. total = 0;
  42. Rast_rewind_cell_stats(statf);
  43. while (Rast_next_cell_stat(&cat, &count, statf))
  44. if (count > 0)
  45. total += count;
  46. if (total <= 0)
  47. return;
  48. span = total / 256.0;
  49. first = 1;
  50. grey = 0;
  51. sum = 0.0;
  52. Rast_rewind_cell_stats(statf);
  53. while (Rast_next_cell_stat(&cat, &count, statf)) {
  54. if (count <= 0)
  55. continue;
  56. x = (sum + (count / 2.0)) / span;
  57. if (x < 0)
  58. x = 0;
  59. else if (x > 255)
  60. x = 255;
  61. sum += count;
  62. if (first) {
  63. prev = cat;
  64. grey = x;
  65. first = 0;
  66. }
  67. else if (grey != x) {
  68. val2 = cat - 1;
  69. Rast_add_c_color_rule(&prev, grey, grey, grey, &val2, grey, grey,
  70. grey, colors);
  71. grey = x;
  72. prev = cat;
  73. }
  74. }
  75. if (!first) {
  76. Rast_add_c_color_rule(&prev, grey, grey, grey, &cat, grey, grey, grey,
  77. colors);
  78. }
  79. }
  80. /*!
  81. \brief Generates histogram with normalized log transformed grey scale.
  82. Generates histogram with normalized log transformed grey scale from
  83. cell stats structure info. Color range is 0-255.
  84. \param colors pointer to Colors structure which holds color info
  85. \param statf pointer to Cell_stats structure which holds cell stats info
  86. \param min minimum value
  87. \param max maximum value
  88. */
  89. void Rast_make_histogram_log_colors(struct Colors *colors,
  90. struct Cell_stats *statf, int min,
  91. int max)
  92. {
  93. long count, total;
  94. double lmin, lmax;
  95. CELL prev = 0, cat, val2;
  96. int first;
  97. int x, grey;
  98. int R, G, B;
  99. Rast_init_colors(colors);
  100. G_str_to_color(DEFAULT_BG_COLOR, &R, &G, &B);
  101. Rast_set_null_value_color(R, G, B, colors);
  102. total = 0;
  103. Rast_rewind_cell_stats(statf);
  104. while (Rast_next_cell_stat(&cat, &count, statf))
  105. if (count > 0)
  106. total += count;
  107. if (total <= 0)
  108. return;
  109. first = 1;
  110. grey = 0;
  111. lmin = log(min);
  112. lmax = log(max);
  113. Rast_rewind_cell_stats(statf);
  114. while (Rast_next_cell_stat(&cat, &count, statf)) {
  115. if (count <= 0)
  116. continue;
  117. /* log transform normalized */
  118. x = (int)(255 * (log(cat) - lmin) / (lmax - lmin));
  119. if (x < 0)
  120. x = 0;
  121. else if (x > 255)
  122. x = 255;
  123. if (first) {
  124. prev = cat;
  125. grey = x;
  126. first = 0;
  127. }
  128. else if (grey != x) {
  129. val2 = cat - 1;
  130. Rast_add_c_color_rule(&prev, grey, grey, grey, &val2, grey, grey,
  131. grey, colors);
  132. grey = x;
  133. prev = cat;
  134. }
  135. }
  136. if (!first) {
  137. Rast_add_c_color_rule(&prev, grey, grey, grey, &cat, grey, grey, grey,
  138. colors);
  139. }
  140. }