2
/****************************************************************************
5
* AUTHOR(S): Glynn Clements <glynn@gclements.plus.com>
6
* COPYRIGHT: (C) 2007 Glynn Clements
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.
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.
18
*****************************************************************************/
20
/**********************************************************************
22
* G_histogram_eq_colors (dst, src, statf)
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
28
* Generates histogram equalized version of an existing color table from
29
* cell stats structure info.
31
**********************************************************************
33
* G_log_colors (dst, src, samples)
35
* struct Colors *dst struct to hold new colors
36
* struct Colors *src struct containing original colors
37
* int samples number of samples
39
* Generates logarithmically-scaled version of an existing color table.
41
**********************************************************************/
42
#include <grass/gis.h>
46
* \brief make histogram-stretched version of existing color table
48
* Generates a histogram
49
* contrast-stretched color table that goes from the histogram
50
* information in the Cell_stats structure <b>statf.</b> (See
59
int G_histogram_eq_colors(struct Colors *dst,
60
struct Colors *src, struct Cell_stats *statf)
64
long count, total, sum;
70
G_get_d_color_range(&min, &max, src);
72
G_get_default_color(&red, &grn, &blu, src);
73
G_set_default_color(red, grn, blu, dst);
75
G_get_null_value_color(&red, &grn, &blu, src);
76
G_set_null_value_color(red, grn, blu, dst);
80
G_rewind_cell_stats(statf);
81
while (G_next_cell_stat(&cat, &count, statf))
92
G_rewind_cell_stats(statf);
93
while (G_next_cell_stat(&cat, &count, statf)) {
100
x = min + (max - min) * (sum + count / 2.0) / total;
101
G_get_d_raster_color(&x, &red2, &grn2, &blu2, src);
104
G_add_color_rule(prev, red, grn, blu, cat, red2, grn2, blu2, dst);
119
* \brief make histogram-stretched version of existing color table (FP version)
121
* Generates a histogram
122
* contrast-stretched color table that goes from the histogram
123
* information in the FP_stats structure <b>statf.</b> (See
124
* Raster_Histograms).
132
void G_histogram_eq_colors_fp(struct Colors *dst,
133
struct Colors *src, struct FP_stats *statf)
144
G_get_d_color_range(&min, &max, src);
146
G_get_default_color(&red, &grn, &blu, src);
147
G_set_default_color(red, grn, blu, dst);
149
G_get_null_value_color(&red, &grn, &blu, src);
150
G_set_null_value_color(red, grn, blu, dst);
158
for (i = 0; i <= statf->count; i++) {
159
int red2, grn2, blu2;
162
val2 = statf->min + (statf->max - statf->min) * i / statf->count;
163
if (statf->geometric)
166
val2 = exp(val2) - 1;
169
x = min + (max - min) * sum / statf->total;
170
G_get_d_raster_color(&x, &red2, &grn2, &blu2, src);
173
G_add_d_raster_color_rule(&val, red, grn, blu, &val2, red2, grn2, blu2, dst);
176
if (i == statf->count)
179
sum += statf->stats[i];
189
* \brief make logarithmically-scaled version of an existing color table
197
int G_log_colors(struct Colors *dst, struct Colors *src, int samples)
207
G_get_d_color_range(&min, &max, src);
212
G_get_default_color(&red, &grn, &blu, src);
213
G_set_default_color(red, grn, blu, dst);
215
G_get_null_value_color(&red, &grn, &blu, src);
216
G_set_null_value_color(red, grn, blu, dst);
218
for (i = 0; i <= samples; i++) {
219
int red2, grn2, blu2;
223
y = min + (max - min) * i / samples;
224
G_get_d_raster_color(&y, &red2, &grn2, &blu2, src);
228
else if (i == samples)
231
lx = lmin + (lmax - lmin) * i / samples;
236
G_add_d_raster_color_rule(&prev, red, grn, blu,
237
&x, red2, grn2, blu2,
251
* \brief make logarithmically-scaled version of an existing color table, allowing for signed values
259
int G_abs_log_colors(struct Colors *dst, struct Colors *src, int samples)
270
G_get_d_color_range(&min, &max, src);
272
lmin = log(fabs(min) + 1.0);
273
lmax = log(fabs(max) + 1.0);
275
amax = fabs(min) > fabs(max) ? fabs(min) : fabs(max);
276
lamax = lmin > lmax ? lmin : lmax;
278
G_get_default_color(&red, &grn, &blu, src);
279
G_set_default_color(red, grn, blu, dst);
281
G_get_null_value_color(&red, &grn, &blu, src);
282
G_set_null_value_color(red, grn, blu, dst);
284
for (i = 0; i <= samples; i++) {
285
int red2, grn2, blu2;
289
y = min + (max - min) * i / samples;
290
G_get_d_raster_color(&y, &red2, &grn2, &blu2, src);
294
else if (i == samples)
297
lx = 0 + lamax * i / samples;
302
DCELL x0 = prev, x1 = x;
303
G_add_d_raster_color_rule(&x0, red, grn, blu,
304
&x1, red2, grn2, blu2,
308
G_add_d_raster_color_rule(&x0, red, grn, blu,
309
&x1, red2, grn2, blu2,