color_hist.c

Go to the documentation of this file.
00001 /**********************************************************************
00002  *
00003  *  G_make_histogram_eq_colors (colors, statf)
00004  *
00005  *   struct Colors *colors      struct to hold colors
00006  *   struct Cell_stats *statf   cell stats info
00007  *
00008  *  Generates histogram equalized grey scale from
00009  *  cell stats structure info.
00010  *  Color range is 0-255.
00011  *
00012  **********************************************************************
00013  *
00014  *  G_make_histogram_log_colors (colors, statf, min, max)
00015  *
00016  *   struct Colors *colors      struct to hold colors
00017  *   struct Cell_stats *statf   cell stats info
00018  *
00019  *  Generates histogram with normalized log transformed grey scale from
00020  *  cell stats structure info.
00021  *  Color range is 0-255.
00022  *
00023  **********************************************************************/
00024 #include <grass/gis.h>
00025 #include <math.h>
00026 
00027 
00041 int G_make_histogram_eq_colors (
00042     struct Colors *colors,
00043     struct Cell_stats *statf)
00044 {
00045     long count, total;
00046     CELL prev=0,cat;
00047     double span, sum;
00048     int first;
00049     int x, grey;
00050     int R,G,B;
00051 
00052     G_init_colors (colors);
00053 
00054     G_str_to_color(DEFAULT_BG_COLOR, &R, &G, &B);
00055     G_set_null_value_color(R, G, B, colors);
00056 
00057     total = 0;
00058 
00059     G_rewind_cell_stats (statf);
00060     while (G_next_cell_stat (&cat, &count, statf))
00061         if (count > 0)
00062             total += count;
00063     if (total <= 0)
00064         return 0;
00065 
00066     span = total/256.0;
00067     first = 1;
00068     grey = 0;
00069     sum = 0.0;
00070 
00071     G_rewind_cell_stats (statf);
00072     while (G_next_cell_stat (&cat, &count, statf))
00073     {
00074         if (count <= 0)
00075             continue;
00076         x = (sum + (count/2.0))/span;
00077         if (x < 0) x = 0;
00078         else if (x > 255) x = 255;
00079         sum += count;
00080         if (first)
00081         {
00082             prev = cat;
00083             grey = x;
00084             first = 0;
00085         }
00086         else if (grey != x)
00087         {
00088             G_add_color_rule (prev, grey, grey, grey, cat-1, grey, grey, grey, colors);
00089             grey = x;
00090             prev = cat;
00091         }
00092     }
00093     if (!first)
00094     {
00095         G_add_color_rule (prev, grey, grey, grey, cat, grey, grey, grey, colors);
00096     }
00097 
00098     return 0;
00099 }
00100 
00101 
00102 int G_make_histogram_log_colors (
00103     struct Colors *colors,
00104     struct Cell_stats *statf,
00105     int min, int max)
00106 {
00107     long count, total;
00108     CELL prev=0,cat;
00109     int first;
00110     int x, grey;
00111     int R,G,B;
00112 
00113     G_init_colors (colors);
00114 
00115     G_str_to_color(DEFAULT_BG_COLOR, &R, &G, &B);
00116     G_set_null_value_color(R, G, B, colors);
00117 
00118     total = 0;
00119 
00120     G_rewind_cell_stats (statf);
00121     while (G_next_cell_stat (&cat, &count, statf))
00122         if (count > 0)
00123             total += count;
00124     if (total <= 0)
00125         return 0;
00126 
00127     first = 1;
00128     grey = 0;
00129 
00130     G_rewind_cell_stats (statf);
00131     while (G_next_cell_stat (&cat, &count, statf))
00132     {
00133         if (count <= 0)
00134             continue;
00135             
00136         /* log transform normalized */
00137         x = (int) ( log(cat)* 255. / log(max) );
00138         
00139         if (x < 0) x = 0;
00140         else if (x > 255) x = 255;
00141         if (first)
00142         {
00143             prev = cat;
00144             grey = x;
00145             first = 0;
00146         }
00147         else if (grey != x)
00148         {
00149             G_add_color_rule (prev, grey, grey, grey, cat-1, grey, grey, grey, colors);
00150             grey = x;
00151             prev = cat;
00152         }
00153     }
00154     if (!first)
00155     {
00156         G_add_color_rule (prev, grey, grey, grey, cat, grey, grey, grey, colors);
00157     }
00158 
00159     return 0;
00160 }
00161 

Generated on Fri Nov 21 11:02:17 2008 for GRASS by  doxygen 1.5.1