GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
color_hist.c
Go to the documentation of this file.
1 
2 /**********************************************************************
3  *
4  * G_make_histogram_eq_colors (colors, statf)
5  *
6  * struct Colors *colors struct to hold colors
7  * struct Cell_stats *statf cell stats info
8  *
9  * Generates histogram equalized grey scale from
10  * cell stats structure info.
11  * Color range is 0-255.
12  *
13  **********************************************************************
14  *
15  * G_make_histogram_log_colors (colors, statf, min, max)
16  *
17  * struct Colors *colors struct to hold colors
18  * struct Cell_stats *statf cell stats info
19  *
20  * Generates histogram with normalized log transformed grey scale from
21  * cell stats structure info.
22  * Color range is 0-255.
23  *
24  **********************************************************************/
25 #include <grass/gis.h>
26 #include <math.h>
27 
28 
42 int G_make_histogram_eq_colors(struct Colors *colors,
43  struct Cell_stats *statf)
44 {
45  long count, total;
46  CELL prev = 0, cat;
47  double span, sum;
48  int first;
49  int x, grey;
50  int R, G, B;
51 
52  G_init_colors(colors);
53 
54  G_str_to_color(DEFAULT_BG_COLOR, &R, &G, &B);
55  G_set_null_value_color(R, G, B, colors);
56 
57  total = 0;
58 
59  G_rewind_cell_stats(statf);
60  while (G_next_cell_stat(&cat, &count, statf))
61  if (count > 0)
62  total += count;
63  if (total <= 0)
64  return 0;
65 
66  span = total / 256.0;
67  first = 1;
68  grey = 0;
69  sum = 0.0;
70 
71  G_rewind_cell_stats(statf);
72  while (G_next_cell_stat(&cat, &count, statf)) {
73  if (count <= 0)
74  continue;
75  x = (sum + (count / 2.0)) / span;
76  if (x < 0)
77  x = 0;
78  else if (x > 255)
79  x = 255;
80  sum += count;
81  if (first) {
82  prev = cat;
83  grey = x;
84  first = 0;
85  }
86  else if (grey != x) {
87  G_add_color_rule(prev, grey, grey, grey, cat - 1, grey, grey,
88  grey, colors);
89  grey = x;
90  prev = cat;
91  }
92  }
93  if (!first) {
94  G_add_color_rule(prev, grey, grey, grey, cat, grey, grey, grey,
95  colors);
96  }
97 
98  return 0;
99 }
100 
101 
102 int G_make_histogram_log_colors(struct Colors *colors,
103  struct Cell_stats *statf, int min, int max)
104 {
105  long count, total;
106  double lmin, lmax;
107  CELL prev = 0, cat;
108  int first;
109  int x, grey;
110  int R, G, B;
111 
112  G_init_colors(colors);
113 
114  G_str_to_color(DEFAULT_BG_COLOR, &R, &G, &B);
115  G_set_null_value_color(R, G, B, colors);
116 
117  total = 0;
118 
119  G_rewind_cell_stats(statf);
120  while (G_next_cell_stat(&cat, &count, statf))
121  if (count > 0)
122  total += count;
123  if (total <= 0)
124  return 0;
125 
126  first = 1;
127  grey = 0;
128 
129  lmin = log(min);
130  lmax = log(max);
131 
132  G_rewind_cell_stats(statf);
133  while (G_next_cell_stat(&cat, &count, statf)) {
134  if (count <= 0)
135  continue;
136 
137  /* log transform normalized */
138  x = (int)(255 * (log(cat) - lmin) / (lmax - lmin));
139 
140  if (x < 0)
141  x = 0;
142  else if (x > 255)
143  x = 255;
144  if (first) {
145  prev = cat;
146  grey = x;
147  first = 0;
148  }
149  else if (grey != x) {
150  G_add_color_rule(prev, grey, grey, grey, cat - 1, grey, grey,
151  grey, colors);
152  grey = x;
153  prev = cat;
154  }
155  }
156  if (!first) {
157  G_add_color_rule(prev, grey, grey, grey, cat, grey, grey, grey,
158  colors);
159  }
160 
161  return 0;
162 }
int G_set_null_value_color(int red, int grn, int blu, struct Colors *colors)
Sets the color (in colors) for the NULL-value to r,g,b.
Definition: color_set.c:59
#define min(x, y)
Definition: draw2.c:68
int count
#define max(x, y)
Definition: draw2.c:69
int G_next_cell_stat(CELL *cat, long *count, struct Cell_stats *s)
retrieve sorted cell stats
Definition: cell_stats.c:338
int G_make_histogram_eq_colors(struct Colors *colors, struct Cell_stats *statf)
make histogram-stretched grey colors
Definition: color_hist.c:42
log
Definition: wxnviz.py:54
int G_make_histogram_log_colors(struct Colors *colors, struct Cell_stats *statf, int min, int max)
Definition: color_hist.c:102
int
Definition: g3dcolor.c:48
int first
Definition: form/open.c:25
int G_rewind_cell_stats(struct Cell_stats *s)
reset/rewind cell stats
Definition: cell_stats.c:265
int G_add_color_rule(CELL cat1, int r1, int g1, int b1, CELL cat2, int r2, int g2, int b2, struct Colors *colors)
Set colors rules.
Definition: color_rule.c:165
CELL cat
Definition: g3dcats.c:90
int G_str_to_color(const char *str, int *red, int *grn, int *blu)
Parse color string and set red,green,blue.
Definition: color_str.c:112
G_init_colors(colors)