• Main Page
  • Related Pages
  • Data Structures
  • Files
  • File List
  • Globals

color_xform.c

Go to the documentation of this file.
00001 
00002 /****************************************************************************
00003  *
00004  * MODULE:       gis library
00005  * AUTHOR(S):    Glynn Clements <glynn@gclements.plus.com>
00006  * COPYRIGHT:    (C) 2007 Glynn Clements
00007  *
00008  *  This program is free software; you can redistribute it and/or modify
00009  *  it under the terms of the GNU General Public License as published by
00010  *  the Free Software Foundation; either version 2 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  This program is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *  GNU General Public License for more details.
00017  *
00018  *****************************************************************************/
00019 
00020 /**********************************************************************
00021  *
00022  *  G_histogram_eq_colors (dst, src, statf)
00023  *
00024  *   struct Colors *dst         struct to hold new colors
00025  *   struct Colors *src         struct containing original colors
00026  *   struct Cell_stats *statf   cell stats info
00027  *
00028  *  Generates histogram equalized version of an existing color table from
00029  *  cell stats structure info.
00030  *
00031  **********************************************************************
00032  *
00033  *  G_log_colors (dst, src, samples)
00034  *
00035  *   struct Colors *dst         struct to hold new colors
00036  *   struct Colors *src         struct containing original colors
00037  *   int samples                number of samples
00038  *
00039  *  Generates logarithmically-scaled version of an existing color table.
00040  *
00041  **********************************************************************/
00042 #include <grass/gis.h>
00043 #include <math.h>
00044 
00059 int G_histogram_eq_colors(struct Colors *dst,
00060                           struct Colors *src, struct Cell_stats *statf)
00061 {
00062     DCELL min, max;
00063     int red, grn, blu;
00064     long count, total, sum;
00065     CELL cat, prev;
00066     int first;
00067 
00068     G_init_colors(dst);
00069 
00070     G_get_d_color_range(&min, &max, src);
00071 
00072     G_get_default_color(&red, &grn, &blu, src);
00073     G_set_default_color(red, grn, blu, dst);
00074 
00075     G_get_null_value_color(&red, &grn, &blu, src);
00076     G_set_null_value_color(red, grn, blu, dst);
00077 
00078     total = 0;
00079 
00080     G_rewind_cell_stats(statf);
00081     while (G_next_cell_stat(&cat, &count, statf))
00082         if (count > 0)
00083             total += count;
00084 
00085     if (total <= 0)
00086         return 0;
00087 
00088     sum = 0;
00089     prev = 0;
00090     first = 1;
00091 
00092     G_rewind_cell_stats(statf);
00093     while (G_next_cell_stat(&cat, &count, statf)) {
00094         int red2, grn2, blu2;
00095         DCELL x;
00096 
00097         if (count <= 0)
00098             continue;
00099 
00100         x = min + (max - min) * (sum + count / 2.0) / total;
00101         G_get_d_raster_color(&x, &red2, &grn2, &blu2, src);
00102 
00103         if (!first)
00104             G_add_color_rule(prev, red, grn, blu, cat, red2, grn2, blu2, dst);
00105 
00106         sum += count;
00107         first = 0;
00108 
00109         prev = cat;
00110         red = red2;
00111         grn = grn2;
00112         blu = blu2;
00113     }
00114 
00115     return 0;
00116 }
00117 
00132 void G_histogram_eq_colors_fp(struct Colors *dst,
00133                               struct Colors *src, struct FP_stats *statf)
00134 {
00135     DCELL min, max;
00136     int red, grn, blu;
00137     unsigned long sum;
00138     DCELL val;
00139     int first;
00140     int i;
00141 
00142     G_init_colors(dst);
00143 
00144     G_get_d_color_range(&min, &max, src);
00145 
00146     G_get_default_color(&red, &grn, &blu, src);
00147     G_set_default_color(red, grn, blu, dst);
00148 
00149     G_get_null_value_color(&red, &grn, &blu, src);
00150     G_set_null_value_color(red, grn, blu, dst);
00151 
00152     if (!statf->total)
00153         return;
00154 
00155     sum = 0;
00156     first = 1;
00157 
00158     for (i = 0; i <= statf->count; i++) {
00159         int red2, grn2, blu2;
00160         DCELL val2, x;
00161 
00162         val2 = statf->min + (statf->max - statf->min) * i / statf->count;
00163         if (statf->geometric)
00164             val2 = exp(val2);
00165         if (statf->geom_abs)
00166             val2 = exp(val2) - 1;
00167         if (statf->flip)
00168             val2 = -val2;
00169         x = min + (max - min) * sum / statf->total;
00170         G_get_d_raster_color(&x, &red2, &grn2, &blu2, src);
00171 
00172         if (!first)
00173             G_add_d_raster_color_rule(&val, red, grn, blu, &val2, red2, grn2, blu2, dst);
00174         first = 0;
00175 
00176         if (i == statf->count)
00177             break;
00178 
00179         sum += statf->stats[i];
00180 
00181         val = val2;
00182         red = red2;
00183         grn = grn2;
00184         blu = blu2;
00185     }
00186 }
00187 
00197 int G_log_colors(struct Colors *dst, struct Colors *src, int samples)
00198 {
00199     DCELL min, max;
00200     double lmin, lmax;
00201     int red, grn, blu;
00202     DCELL prev;
00203     int i;
00204 
00205     G_init_colors(dst);
00206 
00207     G_get_d_color_range(&min, &max, src);
00208 
00209     lmin = log(min);
00210     lmax = log(max);
00211 
00212     G_get_default_color(&red, &grn, &blu, src);
00213     G_set_default_color(red, grn, blu, dst);
00214 
00215     G_get_null_value_color(&red, &grn, &blu, src);
00216     G_set_null_value_color(red, grn, blu, dst);
00217 
00218     for (i = 0; i <= samples; i++) {
00219         int red2, grn2, blu2;
00220         double lx;
00221         DCELL x, y;
00222 
00223         y = min + (max - min) * i / samples;
00224         G_get_d_raster_color(&y, &red2, &grn2, &blu2, src);
00225 
00226         if (i == 0)
00227             x = min;
00228         else if (i == samples)
00229             x = max;
00230         else {
00231             lx = lmin + (lmax - lmin) * i / samples;
00232             x = exp(lx);
00233         }
00234 
00235         if (i > 0)
00236             G_add_d_raster_color_rule(&prev, red, grn, blu,
00237                                       &x, red2, grn2, blu2,
00238                                       dst);
00239 
00240         prev = x;
00241 
00242         red = red2;
00243         grn = grn2;
00244         blu = blu2;
00245     }
00246 
00247     return 0;
00248 }
00249 
00259 int G_abs_log_colors(struct Colors *dst, struct Colors *src, int samples)
00260 {
00261     DCELL min, max;
00262     double lmin, lmax;
00263     DCELL amax, lamax;
00264     int red, grn, blu;
00265     DCELL prev;
00266     int i;
00267 
00268     G_init_colors(dst);
00269 
00270     G_get_d_color_range(&min, &max, src);
00271 
00272     lmin = log(fabs(min) + 1.0);
00273     lmax = log(fabs(max) + 1.0);
00274 
00275     amax = fabs(min) > fabs(max) ? fabs(min) : fabs(max);
00276     lamax = lmin > lmax ? lmin : lmax;
00277 
00278     G_get_default_color(&red, &grn, &blu, src);
00279     G_set_default_color(red, grn, blu, dst);
00280 
00281     G_get_null_value_color(&red, &grn, &blu, src);
00282     G_set_null_value_color(red, grn, blu, dst);
00283 
00284     for (i = 0; i <= samples; i++) {
00285         int red2, grn2, blu2;
00286         double lx;
00287         DCELL x, y;
00288 
00289         y = min + (max - min) * i / samples;
00290         G_get_d_raster_color(&y, &red2, &grn2, &blu2, src);
00291 
00292         if (i == 0)
00293             x = 1;
00294         else if (i == samples)
00295             x = amax;
00296         else {
00297             lx = 0 + lamax * i / samples;
00298             x = exp(lx);
00299         }
00300 
00301         if (i > 0) {
00302             DCELL x0 = prev, x1 = x;
00303             G_add_d_raster_color_rule(&x0, red, grn, blu,
00304                                       &x1, red2, grn2, blu2,
00305                                       dst);
00306             x0 = -x0;
00307             x1 = -x1;
00308             G_add_d_raster_color_rule(&x0, red, grn, blu,
00309                                       &x1, red2, grn2, blu2,
00310                                       dst);
00311         }
00312 
00313         prev = x;
00314 
00315         red = red2;
00316         grn = grn2;
00317         blu = blu2;
00318     }
00319 
00320     return 0;
00321 }
00322 

Generated on Wed Oct 13 2010 12:09:29 for GRASS Programmer's Manual by  doxygen 1.7.1