color_hist.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**********************************************************************
  2. *
  3. * G_make_histogram_eq_colors (colors, statf)
  4. *
  5. * struct Colors *colors struct to hold colors
  6. * struct Cell_stats *statf cell stats info
  7. *
  8. * Generates histogram equalized grey scale from
  9. * cell stats structure info.
  10. * Color range is 0-255.
  11. *
  12. **********************************************************************
  13. *
  14. * G_make_histogram_log_colors (colors, statf, min, max)
  15. *
  16. * struct Colors *colors struct to hold colors
  17. * struct Cell_stats *statf cell stats info
  18. *
  19. * Generates histogram with normalized log transformed grey scale from
  20. * cell stats structure info.
  21. * Color range is 0-255.
  22. *
  23. **********************************************************************/
  24. #include <grass/gis.h>
  25. #include <math.h>
  26. /*!
  27. * \brief make histogram-stretched grey colors
  28. *
  29. * Generates a histogram
  30. * contrast-stretched grey scale color table that goes from the ,histogram
  31. * information in the Cell_stats structure <b>s.</b> (See
  32. * Raster_Histograms).
  33. *
  34. * \param colors
  35. * \param s
  36. * \return
  37. */
  38. void G_make_histogram_eq_colors(struct Colors *colors,
  39. struct Cell_stats *statf)
  40. {
  41. long count, total;
  42. CELL prev = 0, cat;
  43. double span, sum;
  44. int first;
  45. int x, grey;
  46. int R, G, B;
  47. G_init_colors(colors);
  48. G_str_to_color(DEFAULT_BG_COLOR, &R, &G, &B);
  49. G_set_null_value_color(R, G, B, colors);
  50. total = 0;
  51. G_rewind_cell_stats(statf);
  52. while (G_next_cell_stat(&cat, &count, statf))
  53. if (count > 0)
  54. total += count;
  55. if (total <= 0)
  56. return;
  57. span = total / 256.0;
  58. first = 1;
  59. grey = 0;
  60. sum = 0.0;
  61. G_rewind_cell_stats(statf);
  62. while (G_next_cell_stat(&cat, &count, statf)) {
  63. if (count <= 0)
  64. continue;
  65. x = (sum + (count / 2.0)) / span;
  66. if (x < 0)
  67. x = 0;
  68. else if (x > 255)
  69. x = 255;
  70. sum += count;
  71. if (first) {
  72. prev = cat;
  73. grey = x;
  74. first = 0;
  75. }
  76. else if (grey != x) {
  77. G_add_color_rule(prev, grey, grey, grey, cat - 1, grey, grey,
  78. grey, colors);
  79. grey = x;
  80. prev = cat;
  81. }
  82. }
  83. if (!first) {
  84. G_add_color_rule(prev, grey, grey, grey, cat, grey, grey, grey,
  85. colors);
  86. }
  87. }
  88. void G_make_histogram_log_colors(struct Colors *colors,
  89. struct Cell_stats *statf, int min, int max)
  90. {
  91. long count, total;
  92. double lmin, lmax;
  93. CELL prev = 0, cat;
  94. int first;
  95. int x, grey;
  96. int R, G, B;
  97. G_init_colors(colors);
  98. G_str_to_color(DEFAULT_BG_COLOR, &R, &G, &B);
  99. G_set_null_value_color(R, G, B, colors);
  100. total = 0;
  101. G_rewind_cell_stats(statf);
  102. while (G_next_cell_stat(&cat, &count, statf))
  103. if (count > 0)
  104. total += count;
  105. if (total <= 0)
  106. return;
  107. first = 1;
  108. grey = 0;
  109. lmin = log(min);
  110. lmax = log(max);
  111. G_rewind_cell_stats(statf);
  112. while (G_next_cell_stat(&cat, &count, statf)) {
  113. if (count <= 0)
  114. continue;
  115. /* log transform normalized */
  116. x = (int)(255 * (log(cat) - lmin) / (lmax - lmin));
  117. if (x < 0)
  118. x = 0;
  119. else if (x > 255)
  120. x = 255;
  121. if (first) {
  122. prev = cat;
  123. grey = x;
  124. first = 0;
  125. }
  126. else if (grey != x) {
  127. G_add_color_rule(prev, grey, grey, grey, cat - 1, grey, grey,
  128. grey, colors);
  129. grey = x;
  130. prev = cat;
  131. }
  132. }
  133. if (!first) {
  134. G_add_color_rule(prev, grey, grey, grey, cat, grey, grey, grey,
  135. colors);
  136. }
  137. }