~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/gis/color_xform.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
45
 
/*!
46
 
 * \brief make histogram-stretched version of existing color table
47
 
 *
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
51
 
 * Raster_Histograms).
52
 
 *
53
 
 *  \param dst
54
 
 *  \param src
55
 
 *  \param statf
56
 
 *  \return int
57
 
 */
58
 
 
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
 
 
118
 
/*!
119
 
 * \brief make histogram-stretched version of existing color table (FP version)
120
 
 *
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).
125
 
 *
126
 
 *  \param dst
127
 
 *  \param src
128
 
 *  \param statf
129
 
 *  \return void
130
 
 */
131
 
 
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
 
 
188
 
/*!
189
 
 * \brief make logarithmically-scaled version of an existing color table
190
 
 *
191
 
 *  \param dst
192
 
 *  \param src
193
 
 *  \param samples
194
 
 *  \return int
195
 
 */
196
 
 
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
 
 
250
 
/*!
251
 
 * \brief make logarithmically-scaled version of an existing color table, allowing for signed values
252
 
 *
253
 
 *  \param dst
254
 
 *  \param src
255
 
 *  \param samples
256
 
 *  \return int
257
 
 */
258
 
 
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