GRASS Programmer's Manual
6.4.2(2012)
|
00001 00002 /********************************************************************** 00003 * 00004 * G_make_histogram_eq_colors (colors, statf) 00005 * 00006 * struct Colors *colors struct to hold colors 00007 * struct Cell_stats *statf cell stats info 00008 * 00009 * Generates histogram equalized grey scale from 00010 * cell stats structure info. 00011 * Color range is 0-255. 00012 * 00013 ********************************************************************** 00014 * 00015 * G_make_histogram_log_colors (colors, statf, min, max) 00016 * 00017 * struct Colors *colors struct to hold colors 00018 * struct Cell_stats *statf cell stats info 00019 * 00020 * Generates histogram with normalized log transformed grey scale from 00021 * cell stats structure info. 00022 * Color range is 0-255. 00023 * 00024 **********************************************************************/ 00025 #include <grass/gis.h> 00026 #include <math.h> 00027 00028 00042 int G_make_histogram_eq_colors(struct Colors *colors, 00043 struct Cell_stats *statf) 00044 { 00045 long count, total; 00046 CELL prev = 0, cat; 00047 double span, sum; 00048 int first; 00049 int x, grey; 00050 int R, G, B; 00051 00052 G_init_colors(colors); 00053 00054 G_str_to_color(DEFAULT_BG_COLOR, &R, &G, &B); 00055 G_set_null_value_color(R, G, B, colors); 00056 00057 total = 0; 00058 00059 G_rewind_cell_stats(statf); 00060 while (G_next_cell_stat(&cat, &count, statf)) 00061 if (count > 0) 00062 total += count; 00063 if (total <= 0) 00064 return 0; 00065 00066 span = total / 256.0; 00067 first = 1; 00068 grey = 0; 00069 sum = 0.0; 00070 00071 G_rewind_cell_stats(statf); 00072 while (G_next_cell_stat(&cat, &count, statf)) { 00073 if (count <= 0) 00074 continue; 00075 x = (sum + (count / 2.0)) / span; 00076 if (x < 0) 00077 x = 0; 00078 else if (x > 255) 00079 x = 255; 00080 sum += count; 00081 if (first) { 00082 prev = cat; 00083 grey = x; 00084 first = 0; 00085 } 00086 else if (grey != x) { 00087 G_add_color_rule(prev, grey, grey, grey, cat - 1, grey, grey, 00088 grey, colors); 00089 grey = x; 00090 prev = cat; 00091 } 00092 } 00093 if (!first) { 00094 G_add_color_rule(prev, grey, grey, grey, cat, grey, grey, grey, 00095 colors); 00096 } 00097 00098 return 0; 00099 } 00100 00101 00102 int G_make_histogram_log_colors(struct Colors *colors, 00103 struct Cell_stats *statf, int min, int max) 00104 { 00105 long count, total; 00106 double lmin, lmax; 00107 CELL prev = 0, cat; 00108 int first; 00109 int x, grey; 00110 int R, G, B; 00111 00112 G_init_colors(colors); 00113 00114 G_str_to_color(DEFAULT_BG_COLOR, &R, &G, &B); 00115 G_set_null_value_color(R, G, B, colors); 00116 00117 total = 0; 00118 00119 G_rewind_cell_stats(statf); 00120 while (G_next_cell_stat(&cat, &count, statf)) 00121 if (count > 0) 00122 total += count; 00123 if (total <= 0) 00124 return 0; 00125 00126 first = 1; 00127 grey = 0; 00128 00129 lmin = log(min); 00130 lmax = log(max); 00131 00132 G_rewind_cell_stats(statf); 00133 while (G_next_cell_stat(&cat, &count, statf)) { 00134 if (count <= 0) 00135 continue; 00136 00137 /* log transform normalized */ 00138 x = (int)(255 * (log(cat) - lmin) / (lmax - lmin)); 00139 00140 if (x < 0) 00141 x = 0; 00142 else if (x > 255) 00143 x = 255; 00144 if (first) { 00145 prev = cat; 00146 grey = x; 00147 first = 0; 00148 } 00149 else if (grey != x) { 00150 G_add_color_rule(prev, grey, grey, grey, cat - 1, grey, grey, 00151 grey, colors); 00152 grey = x; 00153 prev = cat; 00154 } 00155 } 00156 if (!first) { 00157 G_add_color_rule(prev, grey, grey, grey, cat, grey, grey, grey, 00158 colors); 00159 } 00160 00161 return 0; 00162 }