GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
color_xform.c
Go to the documentation of this file.
1 
2 /****************************************************************************
3  *
4  * MODULE: gis library
5  * AUTHOR(S): Glynn Clements <glynn@gclements.plus.com>
6  * COPYRIGHT: (C) 2007 Glynn Clements
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  *****************************************************************************/
19 
20 /**********************************************************************
21  *
22  * G_histogram_eq_colors (dst, src, statf)
23  *
24  * struct Colors *dst struct to hold new colors
25  * struct Colors *src struct containing original colors
26  * struct Cell_stats *statf cell stats info
27  *
28  * Generates histogram equalized version of an existing color table from
29  * cell stats structure info.
30  *
31  **********************************************************************
32  *
33  * G_log_colors (dst, src, samples)
34  *
35  * struct Colors *dst struct to hold new colors
36  * struct Colors *src struct containing original colors
37  * int samples number of samples
38  *
39  * Generates logarithmically-scaled version of an existing color table.
40  *
41  **********************************************************************/
42 #include <grass/gis.h>
43 #include <math.h>
44 
59 int G_histogram_eq_colors(struct Colors *dst,
60  struct Colors *src, struct Cell_stats *statf)
61 {
62  DCELL min, max;
63  int red, grn, blu;
64  long count, total, sum;
65  CELL cat, prev;
66  int first;
67 
68  G_init_colors(dst);
69 
70  G_get_d_color_range(&min, &max, src);
71 
72  G_get_default_color(&red, &grn, &blu, src);
73  G_set_default_color(red, grn, blu, dst);
74 
75  G_get_null_value_color(&red, &grn, &blu, src);
76  G_set_null_value_color(red, grn, blu, dst);
77 
78  total = 0;
79 
80  G_rewind_cell_stats(statf);
81  while (G_next_cell_stat(&cat, &count, statf))
82  if (count > 0)
83  total += count;
84 
85  if (total <= 0)
86  return 0;
87 
88  sum = 0;
89  prev = 0;
90  first = 1;
91 
92  G_rewind_cell_stats(statf);
93  while (G_next_cell_stat(&cat, &count, statf)) {
94  int red2, grn2, blu2;
95  DCELL x;
96 
97  if (count <= 0)
98  continue;
99 
100  x = min + (max - min) * (sum + count / 2.0) / total;
101  G_get_d_raster_color(&x, &red2, &grn2, &blu2, src);
102 
103  if (!first)
104  G_add_color_rule(prev, red, grn, blu, cat, red2, grn2, blu2, dst);
105 
106  sum += count;
107  first = 0;
108 
109  prev = cat;
110  red = red2;
111  grn = grn2;
112  blu = blu2;
113  }
114 
115  return 0;
116 }
117 
132 void G_histogram_eq_colors_fp(struct Colors *dst,
133  struct Colors *src, struct FP_stats *statf)
134 {
135  DCELL min, max;
136  int red, grn, blu;
137  unsigned long sum;
138  DCELL val;
139  int first;
140  int i;
141 
142  G_init_colors(dst);
143 
144  G_get_d_color_range(&min, &max, src);
145 
146  G_get_default_color(&red, &grn, &blu, src);
147  G_set_default_color(red, grn, blu, dst);
148 
149  G_get_null_value_color(&red, &grn, &blu, src);
150  G_set_null_value_color(red, grn, blu, dst);
151 
152  if (!statf->total)
153  return;
154 
155  sum = 0;
156  first = 1;
157 
158  for (i = 0; i <= statf->count; i++) {
159  int red2, grn2, blu2;
160  DCELL val2, x;
161 
162  val2 = statf->min + (statf->max - statf->min) * i / statf->count;
163  if (statf->geometric)
164  val2 = exp(val2);
165  if (statf->geom_abs)
166  val2 = exp(val2) - 1;
167  if (statf->flip)
168  val2 = -val2;
169  x = min + (max - min) * sum / statf->total;
170  G_get_d_raster_color(&x, &red2, &grn2, &blu2, src);
171 
172  if (!first)
173  G_add_d_raster_color_rule(&val, red, grn, blu, &val2, red2, grn2, blu2, dst);
174  first = 0;
175 
176  if (i == statf->count)
177  break;
178 
179  sum += statf->stats[i];
180 
181  val = val2;
182  red = red2;
183  grn = grn2;
184  blu = blu2;
185  }
186 }
187 
197 int G_log_colors(struct Colors *dst, struct Colors *src, int samples)
198 {
199  DCELL min, max;
200  double lmin, lmax;
201  int red, grn, blu;
202  DCELL prev;
203  int i;
204 
205  G_init_colors(dst);
206 
207  G_get_d_color_range(&min, &max, src);
208 
209  lmin = log(min);
210  lmax = log(max);
211 
212  G_get_default_color(&red, &grn, &blu, src);
213  G_set_default_color(red, grn, blu, dst);
214 
215  G_get_null_value_color(&red, &grn, &blu, src);
216  G_set_null_value_color(red, grn, blu, dst);
217 
218  for (i = 0; i <= samples; i++) {
219  int red2, grn2, blu2;
220  double lx;
221  DCELL x, y;
222 
223  y = min + (max - min) * i / samples;
224  G_get_d_raster_color(&y, &red2, &grn2, &blu2, src);
225 
226  if (i == 0)
227  x = min;
228  else if (i == samples)
229  x = max;
230  else {
231  lx = lmin + (lmax - lmin) * i / samples;
232  x = exp(lx);
233  }
234 
235  if (i > 0)
236  G_add_d_raster_color_rule(&prev, red, grn, blu,
237  &x, red2, grn2, blu2,
238  dst);
239 
240  prev = x;
241 
242  red = red2;
243  grn = grn2;
244  blu = blu2;
245  }
246 
247  return 0;
248 }
249 
259 int G_abs_log_colors(struct Colors *dst, struct Colors *src, int samples)
260 {
261  DCELL min, max;
262  double lmin, lmax;
263  DCELL amax, lamax;
264  int red, grn, blu;
265  DCELL prev;
266  int i;
267 
268  G_init_colors(dst);
269 
270  G_get_d_color_range(&min, &max, src);
271 
272  lmin = log(fabs(min) + 1.0);
273  lmax = log(fabs(max) + 1.0);
274 
275  amax = fabs(min) > fabs(max) ? fabs(min) : fabs(max);
276  lamax = lmin > lmax ? lmin : lmax;
277 
278  G_get_default_color(&red, &grn, &blu, src);
279  G_set_default_color(red, grn, blu, dst);
280 
281  G_get_null_value_color(&red, &grn, &blu, src);
282  G_set_null_value_color(red, grn, blu, dst);
283 
284  for (i = 0; i <= samples; i++) {
285  int red2, grn2, blu2;
286  double lx;
287  DCELL x, y;
288 
289  y = min + (max - min) * i / samples;
290  G_get_d_raster_color(&y, &red2, &grn2, &blu2, src);
291 
292  if (i == 0)
293  x = 1;
294  else if (i == samples)
295  x = amax;
296  else {
297  lx = 0 + lamax * i / samples;
298  x = exp(lx);
299  }
300 
301  if (i > 0) {
302  DCELL x0 = prev, x1 = x;
303  G_add_d_raster_color_rule(&x0, red, grn, blu,
304  &x1, red2, grn2, blu2,
305  dst);
306  x0 = -x0;
307  x1 = -x1;
308  G_add_d_raster_color_rule(&x0, red, grn, blu,
309  &x1, red2, grn2, blu2,
310  dst);
311  }
312 
313  prev = x;
314 
315  red = red2;
316  grn = grn2;
317  blu = blu2;
318  }
319 
320  return 0;
321 }
322 
int G_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:259
int G_set_default_color(int red, int grn, int blu, struct Colors *colors)
Sets the default color (in colors) to r,g,b. This is the color for values which do not have an explic...
Definition: color_set.c:82
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
int G_get_null_value_color(int *red, int *grn, int *blu, const struct Colors *colors)
Gets color for null value.
Definition: color_get.c:159
int G_add_d_raster_color_rule(const DCELL *val1, int r1, int g1, int b1, const DCELL *val2, int r2, int g2, int b2, struct Colors *colors)
Adds the floating-point rule (DCELL version)
Definition: color_rule.c:41
#define min(x, y)
Definition: draw2.c:68
void G_histogram_eq_colors_fp(struct Colors *dst, struct Colors *src, struct FP_stats *statf)
make histogram-stretched version of existing color table (FP version)
Definition: color_xform.c:132
int count
int y
Definition: plot.c:34
#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_get_d_raster_color(const DCELL *rast, int *red, int *grn, int *blu, struct Colors *colors)
Gets color for a DCELL raster.
Definition: color_get.c:139
DCELL val2
Definition: g3dcats.c:91
int G_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:59
log
Definition: wxnviz.py:54
int G_get_default_color(int *red, int *grn, int *blu, const struct Colors *colors)
Gets default color.
Definition: color_get.c:192
int G_get_d_color_range(DCELL *min, DCELL *max, const struct Colors *colors)
Definition: color_range.c:59
int G_log_colors(struct Colors *dst, struct Colors *src, int samples)
make logarithmically-scaled version of an existing color table
Definition: color_xform.c:197
int first
Definition: form/open.c:25
if(!YY_CURRENT_BUFFER)
Definition: lex.yy.c:799
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
G_init_colors(colors)