GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71817
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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,
103  int max)
104 {
105  long count, total;
106  double lmin, lmax;
107  CELL prev = 0, cat, val2;
108  int first;
109  int x, grey;
110  int R, G, B;
111 
112  Rast_init_colors(colors);
113 
114  G_str_to_color(DEFAULT_BG_COLOR, &R, &G, &B);
115  Rast_set_null_value_color(R, G, B, colors);
116 
117  total = 0;
118 
119  Rast_rewind_cell_stats(statf);
120  while (Rast_next_cell_stat(&cat, &count, statf))
121  if (count > 0)
122  total += count;
123  if (total <= 0)
124  return;
125 
126  first = 1;
127  grey = 0;
128 
129  lmin = log(min);
130  lmax = log(max);
131 
132  Rast_rewind_cell_stats(statf);
133  while (Rast_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  val2 = cat - 1;
151  Rast_add_c_color_rule(&prev, grey, grey, grey, &val2, grey, grey,
152  grey, colors);
153  grey = x;
154  prev = cat;
155  }
156  }
157  if (!first) {
158  Rast_add_c_color_rule(&prev, grey, grey, grey, &cat, grey, grey, grey,
159  colors);
160  }
161 }
CELL cat
Definition: raster3d/cats.c:82
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
void Rast_add_c_color_rule(const CELL *cat1, int r1, int g1, int b1, const CELL *cat2, int r2, int g2, int b2, struct Colors *colors)
Adds the integer color rule (CELL version)
Definition: color_rule.c:75
#define min(x, y)
Definition: draw2.c:31
void Rast_set_null_value_color(int red, int grn, int blu, struct Colors *colors)
Set color for NULL-value.
Definition: color_set.c:79
void Rast_make_histogram_eq_colors(struct Colors *colors, struct Cell_stats *statf)
Make histogram-stretched grey colors.
Definition: color_hist.c:32
int count
void Rast_init_colors(struct Colors *colors)
Initialize color structure.
Definition: color_init.c:25
#define x
#define max(x, y)
Definition: draw2.c:32
#define DEFAULT_BG_COLOR
Definition: gis.h:349
int Rast_rewind_cell_stats(struct Cell_stats *s)
Reset/rewind cell stats.
Definition: cell_stats.c:250
int Rast_next_cell_stat(CELL *cat, long *count, struct Cell_stats *s)
Retrieve sorted cell stats.
Definition: cell_stats.c:314
Definition: gis.h:626
DCELL val2
Definition: raster3d/cats.c:83
int CELL
Definition: gis.h:580
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
int
Reads the categories file for map name in mapset and stores the categories in the pcats structure...
struct _Color_Rule_ * prev
Definition: gis.h:596