GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-bea8435a9e
color_hist.c
Go to the documentation of this file.
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 
14 #include <math.h>
15 
16 #include <grass/gis.h>
17 #include <grass/raster.h>
18 #include <grass/colors.h>
19 
20 /*!
21  * \brief Make histogram-stretched grey colors
22  *
23  * Generates a histogram contrast-stretched grey scale color table
24  * that goes from the, histogram information in the Cell_stats
25  * structure (see \ref Raster_Histograms).
26  *
27  * Color range is 0-255.
28  *
29  * \param colors pointer to Colors structure which holds color info
30  * \param statf pointer to Cell_stats structure which holds cell stats info
31  */
33  struct Cell_stats *statf)
34 {
35  long count, total;
36  CELL prev = 0, cat, val2;
37  double span, sum;
38  int first;
39  int x, grey;
40  int R, G, B;
41 
42  Rast_init_colors(colors);
43 
44  G_str_to_color(DEFAULT_BG_COLOR, &R, &G, &B);
45  Rast_set_null_value_color(R, G, B, colors);
46 
47  total = 0;
48 
50  while (Rast_next_cell_stat(&cat, &count, statf))
51  if (count > 0)
52  total += count;
53  if (total <= 0)
54  return;
55 
56  span = total / 256.0;
57  first = 1;
58  grey = 0;
59  sum = 0.0;
60 
62  while (Rast_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  val2 = cat - 1;
78  Rast_add_c_color_rule(&prev, grey, grey, grey, &val2, grey, grey,
79  grey, colors);
80  grey = x;
81  prev = cat;
82  }
83  }
84  if (!first) {
85  Rast_add_c_color_rule(&prev, grey, grey, grey, &cat, grey, grey, grey,
86  colors);
87  }
88 }
89 
90 /*!
91  \brief Generates histogram with normalized log transformed grey scale.
92 
93  Generates histogram with normalized log transformed grey scale from
94  cell stats structure info. Color range is 0-255.
95 
96  \param colors pointer to Colors structure which holds color info
97  \param statf pointer to Cell_stats structure which holds cell stats info
98  \param min minimum value
99  \param max maximum value
100  */
102  struct Cell_stats *statf, int min, int max)
103 {
104  long count, total;
105  double lmin, lmax;
106  CELL prev = 0, cat, val2;
107  int first;
108  int x, grey;
109  int R, G, B;
110 
111  Rast_init_colors(colors);
112 
113  G_str_to_color(DEFAULT_BG_COLOR, &R, &G, &B);
114  Rast_set_null_value_color(R, G, B, colors);
115 
116  total = 0;
117 
118  Rast_rewind_cell_stats(statf);
119  while (Rast_next_cell_stat(&cat, &count, statf))
120  if (count > 0)
121  total += count;
122  if (total <= 0)
123  return;
124 
125  first = 1;
126  grey = 0;
127 
128  lmin = log(min);
129  lmax = log(max);
130 
131  Rast_rewind_cell_stats(statf);
132  while (Rast_next_cell_stat(&cat, &count, statf)) {
133  if (count <= 0)
134  continue;
135 
136  /* log transform normalized */
137  x = (int)(255 * (log(cat) - lmin) / (lmax - lmin));
138 
139  if (x < 0)
140  x = 0;
141  else if (x > 255)
142  x = 255;
143  if (first) {
144  prev = cat;
145  grey = x;
146  first = 0;
147  }
148  else if (grey != x) {
149  val2 = cat - 1;
150  Rast_add_c_color_rule(&prev, grey, grey, grey, &val2, grey, grey,
151  grey, colors);
152  grey = x;
153  prev = cat;
154  }
155  }
156  if (!first) {
157  Rast_add_c_color_rule(&prev, grey, grey, grey, &cat, grey, grey, grey,
158  colors);
159  }
160 }
void Rast_make_histogram_eq_colors(struct Colors *colors, struct Cell_stats *statf)
Make histogram-stretched grey colors.
Definition: color_hist.c:32
void Rast_make_histogram_log_colors(struct Colors *colors, struct Cell_stats *statf, int min, int max)
Generates histogram with normalized log transformed grey scale.
Definition: color_hist.c:101
int G_str_to_color(const char *, int *, int *, int *)
Parse color string and set red,green,blue.
Definition: color_str.c:101
void Rast_set_null_value_color(int, int, int, struct Colors *)
Set color for NULL-value.
Definition: color_set.c:79
void Rast_add_c_color_rule(const CELL *, int, int, int, const CELL *, int, int, int, struct Colors *)
Adds the integer color rule (CELL version)
Definition: color_rule.c:76
int Rast_rewind_cell_stats(struct Cell_stats *)
Reset/rewind cell stats.
Definition: cell_stats.c:248
void Rast_init_colors(struct Colors *)
Initialize color structure.
Definition: color_init.c:25
int Rast_next_cell_stat(CELL *, long *, struct Cell_stats *)
Retrieve sorted cell stats.
Definition: cell_stats.c:312
#define min(x, y)
Definition: draw2.c:29
#define max(x, y)
Definition: draw2.c:30
#define DEFAULT_BG_COLOR
Definition: gis.h:400
int CELL
Definition: gis.h:628
int count
Definition: gis.h:686
#define x