GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
color_xform.c
Go to the documentation of this file.
1 /*!
2  * \file lib/raster/color_xform.c
3  *
4  * \brief Raster Library - Colors management
5  *
6  * (C) 2001-2009 by 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 Original author CERL
12  */
13 
14 #include <math.h>
15 
16 #include <grass/gis.h>
17 #include <grass/raster.h>
18 
19 /*!
20  * \brief Make histogram-stretched version of existing color table
21  *
22  * Generates a histogram contrast-stretched color table that goes from
23  * the histogram information in the Cell_stats structure <i>statf</i>.
24  * (See \ref Raster_Histograms).
25  *
26  * \param[out] dst struct to hold new colors
27  * \param src struct containing original colors
28  * \param statf cell stats info
29  */
31  struct Colors *src, struct Cell_stats *statf)
32 {
33  DCELL min, max;
34  int red, grn, blu;
35  int red2, grn2, blu2;
36  long count, total, sum;
37  CELL cat, prev;
38  int first;
39 
40  Rast_init_colors(dst);
41 
42  Rast_get_d_color_range(&min, &max, src);
43 
44  Rast_get_default_color(&red, &grn, &blu, src);
45  Rast_set_default_color(red, grn, blu, dst);
46 
47  Rast_get_null_value_color(&red, &grn, &blu, src);
48  Rast_set_null_value_color(red, grn, blu, dst);
49 
50  total = 0;
51 
53  while (Rast_next_cell_stat(&cat, &count, statf))
54  if (count > 0)
55  total += count;
56 
57  if (total <= 0)
58  return;
59 
60  sum = 0;
61  prev = 0;
62  first = 1;
63 
65  while (Rast_next_cell_stat(&cat, &count, statf)) {
66  DCELL x;
67 
68  if (count <= 0)
69  continue;
70 
71  x = min + (max - min) * (sum + count / 2.0) / total;
72  Rast_get_d_color(&x, &red2, &grn2, &blu2, src);
73 
74  sum += count;
75 
76  if (!first && red2 == red && blu2 == blu && grn2 == grn)
77  continue;
78 
79  if (!first)
80  Rast_add_c_color_rule(&prev, red, grn, blu,
81  &cat, red2, grn2, blu2,
82  dst);
83 
84  first = 0;
85 
86  prev = cat;
87  red = red2;
88  grn = grn2;
89  blu = blu2;
90  }
91 
92  if (!first && cat > prev)
93  Rast_add_c_color_rule(&prev, red, grn, blu,
94  &cat, red2, grn2, blu2,
95  dst);
96 }
97 
98 /*!
99  * \brief Make histogram-stretched version of existing color table (FP version)
100  *
101  * Generates a histogram contrast-stretched color table that goes from
102  * the histogram information in the FP_stats structure <b>statf.</b>
103  * (See \ref Raster_Histograms).
104  *
105  * \param[out] dst struct to hold new colors
106  * \param src struct containing original colors
107  * \param statf cell stats info
108  */
110  struct Colors *src, struct FP_stats *statf)
111 {
112  DCELL min, max;
113  int red, grn, blu;
114  int red2, grn2, blu2;
115  unsigned long sum;
116  DCELL val, val2;
117  int first;
118  int i;
119 
120  Rast_init_colors(dst);
121 
122  Rast_get_d_color_range(&min, &max, src);
123 
124  Rast_get_default_color(&red, &grn, &blu, src);
125  Rast_set_default_color(red, grn, blu, dst);
126 
127  Rast_get_null_value_color(&red, &grn, &blu, src);
128  Rast_set_null_value_color(red, grn, blu, dst);
129 
130  if (!statf->total)
131  return;
132 
133  sum = 0;
134  first = 1;
135 
136  for (i = 0; i <= statf->count; i++) {
137  DCELL x;
138 
139  val2 = statf->min + (statf->max - statf->min) * i / statf->count;
140  if (statf->geometric)
141  val2 = exp(val2);
142  if (statf->geom_abs)
143  val2 = exp(val2) - 1;
144  if (statf->flip)
145  val2 = -val2;
146  x = min + (max - min) * sum / statf->total;
147  Rast_get_d_color(&x, &red2, &grn2, &blu2, src);
148 
149  if (i < statf->count)
150  sum += statf->stats[i];
151 
152  if (!first && red2 == red && blu2 == blu && grn2 == grn)
153  continue;
154 
155  if (!first)
156  Rast_add_d_color_rule(&val, red, grn, blu,
157  &val2, red2, grn2, blu2,
158  dst);
159 
160  first = 0;
161 
162  if (i == statf->count)
163  break;
164 
165  val = val2;
166  red = red2;
167  grn = grn2;
168  blu = blu2;
169  }
170 
171  if (!first && val2 > val)
172  Rast_add_d_color_rule(&val, red, grn, blu,
173  &val2, red2, grn2, blu2,
174  dst);
175 }
176 
177 /*!
178  * \brief Make logarithmically-scaled version of an existing color table
179  *
180  * \param[out] dst struct to hold new colors
181  * \param src struct containing original colors
182  * \param samples number of samples
183  */
184 
185 void Rast_log_colors(struct Colors *dst, struct Colors *src, int samples)
186 {
187  DCELL min, max;
188  double lmin, lmax;
189  int red, grn, blu;
190  DCELL prev;
191  int i;
192 
193  Rast_init_colors(dst);
194 
195  Rast_get_d_color_range(&min, &max, src);
196 
197  lmin = log(min);
198  lmax = log(max);
199 
200  Rast_get_default_color(&red, &grn, &blu, src);
201  Rast_set_default_color(red, grn, blu, dst);
202 
203  Rast_get_null_value_color(&red, &grn, &blu, src);
204  Rast_set_null_value_color(red, grn, blu, dst);
205 
206  for (i = 0; i <= samples; i++) {
207  int red2, grn2, blu2;
208  double lx;
209  DCELL x, y;
210 
211  y = min + (max - min) * i / samples;
212  Rast_get_d_color(&y, &red2, &grn2, &blu2, src);
213 
214  if (i == 0)
215  x = min;
216  else if (i == samples)
217  x = max;
218  else {
219  lx = lmin + (lmax - lmin) * i / samples;
220  x = exp(lx);
221  }
222 
223  if (i > 0)
224  Rast_add_d_color_rule(&prev, red, grn, blu,
225  &x, red2, grn2, blu2, dst);
226 
227  prev = x;
228 
229  red = red2;
230  grn = grn2;
231  blu = blu2;
232  }
233 }
234 
235 /*!
236  * \brief Make logarithmically-scaled version of an existing color
237  * table, allowing for signed values
238  *
239  * \param[out] dst struct to hold new colors
240  * \param src struct containing original colors
241  * \param samples number of samples
242  */
243 void Rast_abs_log_colors(struct Colors *dst, struct Colors *src, int samples)
244 {
245  DCELL min, max;
246  double lmin, lmax;
247  DCELL amax, lamax;
248  int red, grn, blu;
249  DCELL prev;
250  int i;
251 
252  Rast_init_colors(dst);
253 
254  Rast_get_d_color_range(&min, &max, src);
255 
256  lmin = log(fabs(min) + 1.0);
257  lmax = log(fabs(max) + 1.0);
258 
259  amax = fabs(min) > fabs(max) ? fabs(min) : fabs(max);
260  lamax = lmin > lmax ? lmin : lmax;
261 
262  Rast_get_default_color(&red, &grn, &blu, src);
263  Rast_set_default_color(red, grn, blu, dst);
264 
265  Rast_get_null_value_color(&red, &grn, &blu, src);
266  Rast_set_null_value_color(red, grn, blu, dst);
267 
268  for (i = 0; i <= samples; i++) {
269  int red2, grn2, blu2;
270  double lx;
271  DCELL x, y;
272 
273  y = min + (max - min) * i / samples;
274  Rast_get_d_color(&y, &red2, &grn2, &blu2, src);
275 
276  if (i == 0)
277  x = 1;
278  else if (i == samples)
279  x = amax;
280  else {
281  lx = 0 + lamax * i / samples;
282  x = exp(lx);
283  }
284 
285  if (i > 0) {
286  DCELL x0 = prev, x1 = x;
287 
288  Rast_add_d_color_rule(&x0, red, grn, blu,
289  &x1, red2, grn2, blu2, dst);
290  x0 = -x0;
291  x1 = -x1;
292  Rast_add_d_color_rule(&x0, red, grn, blu,
293  &x1, red2, grn2, blu2, dst);
294  }
295 
296  prev = x;
297 
298  red = red2;
299  grn = grn2;
300  blu = blu2;
301  }
302 }
int count
Definition: raster.h:245
if(!(yy_init))
Definition: sqlp.yy.c:775
int Rast_next_cell_stat(CELL *, long *, struct Cell_stats *)
Retrieve sorted cell stats.
Definition: cell_stats.c:314
int Rast_rewind_cell_stats(struct Cell_stats *)
Reset/rewind cell stats.
Definition: cell_stats.c:250
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:75
DCELL max
Definition: raster.h:246
#define min(x, y)
Definition: draw2.c:31
double DCELL
Definition: gis.h:603
void Rast_get_d_color_range(DCELL *, DCELL *, const struct Colors *)
Get color range values (DCELL)
Definition: color_range.c:87
int geometric
Definition: raster.h:242
int count
char * dst
Definition: lz4.h:599
void Rast_histogram_eq_fp_colors(struct Colors *dst, struct Colors *src, struct FP_stats *statf)
Make histogram-stretched version of existing color table (FP version)
Definition: color_xform.c:109
#define x
#define max(x, y)
Definition: draw2.c:32
void Rast_get_default_color(int *, int *, int *, const struct Colors *)
Gets default color.
Definition: color_get.c:155
void Rast_histogram_eq_colors(struct Colors *dst, struct Colors *src, struct Cell_stats *statf)
Make histogram-stretched version of existing color table.
Definition: color_xform.c:30
int geom_abs
Definition: raster.h:243
void Rast_get_null_value_color(int *, int *, int *, const struct Colors *)
Gets color for null value.
Definition: color_get.c:127
int flip
Definition: raster.h:244
void Rast_set_default_color(int, int, int, struct Colors *)
Set default color value.
Definition: color_set.c:100
unsigned long * stats
Definition: raster.h:247
void Rast_set_null_value_color(int, int, int, struct Colors *)
Set color for NULL-value.
Definition: color_set.c:79
void Rast_abs_log_colors(struct Colors *dst, struct Colors *src, int samples)
Make logarithmically-scaled version of an existing color table, allowing for signed values...
Definition: color_xform.c:243
unsigned long total
Definition: raster.h:248
Definition: gis.h:665
int Rast_get_d_color(const DCELL *, int *, int *, int *, struct Colors *)
Gets color from raster map (DCELL)
Definition: color_get.c:110
int CELL
Definition: gis.h:602
DCELL min
Definition: raster.h:246
void Rast_add_d_color_rule(const DCELL *, int, int, int, const DCELL *, int, int, int, struct Colors *)
Adds the floating-point color rule (DCELL version)
Definition: color_rule.c:35
void Rast_log_colors(struct Colors *dst, struct Colors *src, int samples)
Make logarithmically-scaled version of an existing color table.
Definition: color_xform.c:185
void Rast_init_colors(struct Colors *)
Initialize color structure.
Definition: color_init.c:25
struct _Color_Rule_ * prev
Definition: gis.h:635