[GRASS-SVN] r46784 - grass/trunk/raster/r.colors

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Jun 26 05:24:24 EDT 2011


Author: huhabla
Date: 2011-06-26 02:24:23 -0700 (Sun, 26 Jun 2011)
New Revision: 46784

Modified:
   grass/trunk/raster/r.colors/edit_colors.c
   grass/trunk/raster/r.colors/local_proto.h
   grass/trunk/raster/r.colors/r3.colors.html
   grass/trunk/raster/r.colors/rules.c
   grass/trunk/raster/r.colors/stats.c
   grass/trunk/raster/r.colors/test.r.colors.sh
Log:
Enabled flag "-e" for volume maps


Modified: grass/trunk/raster/r.colors/edit_colors.c
===================================================================
--- grass/trunk/raster/r.colors/edit_colors.c	2011-06-26 08:46:09 UTC (rev 46783)
+++ grass/trunk/raster/r.colors/edit_colors.c	2011-06-26 09:24:23 UTC (rev 46784)
@@ -19,9 +19,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
-#include <grass/glocale.h>
 #include "local_proto.h"
-#include <grass/G3d.h>
 
 static char **rules;
 static int nrules;
@@ -38,7 +36,7 @@
 {
     char path[GPATH_MAX];
 
-    sprintf(path, "%s/etc/colors", G_gisbase());
+    G_snprintf(path, GPATH_MAX, "%s/etc/colors", G_gisbase());
 
     rules = G__ls(path, &nrules);
 
@@ -86,7 +84,7 @@
     char *result = G_malloc(result_max);
     int i;
 
-    sprintf(path, "%s/etc/colors.desc", G_gisbase());
+    G_snprintf(path, GPATH_MAX, "%s/etc/colors.desc", G_gisbase());
     kv = G_read_key_value_file(path);
     if (!kv)
         return NULL;
@@ -153,7 +151,7 @@
     struct GModule *module;
 
     struct {
-        struct Flag *r, *w, *l, *g, *a, *n, *e; /* Flag e is not available in r3.colors*/
+        struct Flag *r, *w, *l, *g, *a, *n, *e;
     } flag; 
 
     struct {
@@ -244,15 +242,10 @@
     flag.a->description = _("Logarithmic-absolute scaling");
     flag.a->guisection = _("Define");
 
-    /* The histogram equalization is currently only available for 
-     *  raster map. Therefor no flag is defined for r3.colors.
-     */
-    if (type == RASTER_TYPE) {
-        flag.e = G_define_flag();
-        flag.e->key = 'e';
-        flag.e->description = _("Histogram equalization");
-        flag.e->guisection = _("Define");
-    }
+    flag.e = G_define_flag();
+    flag.e->key = 'e';
+    flag.e->description = _("Histogram equalization");
+    flag.e->guisection = _("Define");
 
     if (G_parser(argc, argv))
         exit(EXIT_FAILURE);
@@ -405,21 +398,17 @@
     if (flag.n->answer)
         Rast_invert_colors(&colors);
 
-    /* This is not avilable for raster3d maps yet */
-
-    if (type == RASTER_TYPE) {
-        if (flag.e->answer) {
-            if (fp) {
-                struct FP_stats fpstats;
-                get_fp_stats(name, mapset, &fpstats, min, max, flag.g->answer, flag.a->answer);
-                Rast_histogram_eq_fp_colors(&colors_tmp, &colors, &fpstats);
-            } else {
-                if (!have_stats)
-                    have_stats = get_stats(name, mapset, &statf);
-                Rast_histogram_eq_colors(&colors_tmp, &colors, &statf);
-            }
-            colors = colors_tmp;
+    if (flag.e->answer) {
+        if (fp) {
+            struct FP_stats fpstats;
+            get_fp_stats(name, mapset, &fpstats, min, max, flag.g->answer, flag.a->answer, type);
+            Rast_histogram_eq_fp_colors(&colors_tmp, &colors, &fpstats);
+        } else {
+            if (!have_stats)
+                have_stats = get_stats(name, mapset, &statf);
+            Rast_histogram_eq_colors(&colors_tmp, &colors, &statf);
         }
+        colors = colors_tmp;
     }
 
     if (flag.g->answer) {

Modified: grass/trunk/raster/r.colors/local_proto.h
===================================================================
--- grass/trunk/raster/r.colors/local_proto.h	2011-06-26 08:46:09 UTC (rev 46783)
+++ grass/trunk/raster/r.colors/local_proto.h	2011-06-26 09:24:23 UTC (rev 46784)
@@ -22,6 +22,8 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
+#include <grass/glocale.h>
+#include <grass/G3d.h>
 
 #define RASTER_TYPE   1
 #define RASTER3D_TYPE 2
@@ -30,7 +32,7 @@
 int get_stats(const char *, const char *, struct Cell_stats *);
 void get_fp_stats(const char *name, const char *mapset,
 		  struct FP_stats *statf,
-		  DCELL min, DCELL max, int geometric, int geom_abs);
+		  DCELL min, DCELL max, int geometric, int geom_abs, int);
 
 /* edit_colors.c */
 int edit_colors(int, char **, int, const char *, const char*);

Modified: grass/trunk/raster/r.colors/r3.colors.html
===================================================================
--- grass/trunk/raster/r.colors/r3.colors.html	2011-06-26 08:46:09 UTC (rev 46783)
+++ grass/trunk/raster/r.colors/r3.colors.html	2011-06-26 09:24:23 UTC (rev 46784)
@@ -1,8 +1,7 @@
 <h2>DESCRIPTION</h2>
 
 <i>r3.colors</i> has exactly the same functionality as <i>r.colors</i> but for volume maps.
-With the exception that the <b>-e</b> flag, which equalizes the original raster's color table,
-only works for raster maps. Please refer to <a href="r.colors.html">r.colors</a>.
+Please refer to <a href="r.colors.html">r.colors</a>.
 
 <h2>SEE ALSO</h2>
 

Modified: grass/trunk/raster/r.colors/rules.c
===================================================================
--- grass/trunk/raster/r.colors/rules.c	2011-06-26 08:46:09 UTC (rev 46783)
+++ grass/trunk/raster/r.colors/rules.c	2011-06-26 09:24:23 UTC (rev 46784)
@@ -21,9 +21,6 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
-#include <grass/gis.h>
-#include <grass/raster.h>
-#include <grass/glocale.h>
 #include "local_proto.h"
 
 /* color structure for default and null value */

Modified: grass/trunk/raster/r.colors/stats.c
===================================================================
--- grass/trunk/raster/r.colors/stats.c	2011-06-26 08:46:09 UTC (rev 46783)
+++ grass/trunk/raster/r.colors/stats.c	2011-06-26 09:24:23 UTC (rev 46784)
@@ -19,13 +19,9 @@
 
 #include <math.h>
 #include <stdlib.h>
-#include <grass/gis.h>
-#include <grass/raster.h>
-#include <grass/glocale.h>
 #include "local_proto.h"
 
-int get_stats(const char *name, const char *mapset, struct Cell_stats *statf)
-{
+int get_stats(const char *name, const char *mapset, struct Cell_stats *statf) {
     CELL *cell;
     int row, nrows, ncols;
     int fd;
@@ -38,11 +34,11 @@
 
     Rast_init_cell_stats(statf);
     G_verbose_message(_("Reading raster map <%s>..."),
-		      G_fully_qualified_name(name, mapset));
+            G_fully_qualified_name(name, mapset));
     for (row = 0; row < nrows; row++) {
-	G_percent(row, nrows, 2);
-	Rast_get_c_row(fd, cell, row);
-	Rast_update_cell_stats(cell, ncols, statf);
+        G_percent(row, nrows, 2);
+        Rast_get_c_row(fd, cell, row);
+        Rast_update_cell_stats(cell, ncols, statf);
     }
     G_percent(row, nrows, 2);
     Rast_close(fd);
@@ -52,82 +48,109 @@
 }
 
 void get_fp_stats(const char *name, const char *mapset,
-		  struct FP_stats *statf,
-		  DCELL min, DCELL max, int geometric, int geom_abs)
-{
+        struct FP_stats *statf,
+        DCELL min, DCELL max, int geometric, int geom_abs, int type) {
     DCELL *dcell;
-    int row, col, nrows, ncols;
+    int row, col, depth, nrows, ncols, ndepths = 1;
     int fd;
+    G3D_Map *map3d = NULL;
 
-    fd = Rast_open_old(name, mapset);
+    if (type == RASTER_TYPE) {
+        fd = Rast_open_old(name, mapset);
+        dcell = Rast_allocate_d_buf();
+        nrows = Rast_window_rows();
+        ncols = Rast_window_cols();
+    } else {
+        /* Initiate the default settings */
+        G3d_initDefaults();
 
-    dcell = Rast_allocate_d_buf();
-    nrows = Rast_window_rows();
-    ncols = Rast_window_cols();
+        map3d = G3d_openCellOld(name, mapset, G3D_DEFAULT_WINDOW,
+                G3D_TILE_SAME_AS_FILE, G3D_USE_CACHE_DEFAULT);
 
+        if (map3d == NULL)
+            G3d_fatalError(_("Error opening 3d raster map"));
+
+        nrows = map3d->window.rows;
+        ncols = map3d->window.cols;
+        ndepths = map3d->window.depths;
+    }
+
     statf->geometric = geometric;
     statf->geom_abs = geom_abs;
     statf->flip = 0;
 
     if (statf->geometric) {
-	if (min * max < 0)
-	    G_fatal_error(_("Unable to use logarithmic scaling if range includes zero"));
+        if (min * max < 0)
+            G_fatal_error(_("Unable to use logarithmic scaling if range includes zero"));
 
-	if (min < 0) {
-	    statf->flip = 1;
-	    min = -min;
-	    max = -max;
-	}
+        if (min < 0) {
+            statf->flip = 1;
+            min = -min;
+            max = -max;
+        }
 
-	min = log(min);
-	max = log(max);
+        min = log(min);
+        max = log(max);
     }
 
     if (statf->geom_abs) {
-	double a = log(fabs(min) + 1);
-	double b = log(fabs(max) + 1);
-	int has_zero = min * max < 0;
-	min = a < b ? a : b;
-	max = a > b ? a : b;
-	if (has_zero)
-	    min = 0;
+        double a = log(fabs(min) + 1);
+        double b = log(fabs(max) + 1);
+        int has_zero = min * max < 0;
+        min = a < b ? a : b;
+        max = a > b ? a : b;
+        if (has_zero)
+            min = 0;
     }
 
     statf->count = 1000;
     statf->min = min;
     statf->max = max;
-    statf->stats = G_calloc(statf->count + 1, sizeof(unsigned long));
+    statf->stats = G_calloc(statf->count + 1, sizeof (unsigned long));
     statf->total = 0;
 
-    G_verbose_message(_("Reading raster map <%s>..."),
-		      G_fully_qualified_name(name, mapset));
-    for (row = 0; row < nrows; row++) {
-	G_percent(row, nrows, 2);
+    G_verbose_message(_("Reading map <%s>..."),
+            G_fully_qualified_name(name, mapset));
 
-	Rast_get_d_row(fd, dcell, row);
-	
-	for (col = 0; col < ncols; col++) {
-	    DCELL x;
-	    int i;
+    for (depth = 0; depth < ndepths; depth++) {
+        for (row = 0; row < nrows; row++) {
+            G_percent(row, nrows, 2);
 
-	    if (Rast_is_d_null_value(&dcell[col]))
-		continue;
+            if (type == RASTER_TYPE)
+                Rast_get_d_row(fd, dcell, row);
 
-	    x = dcell[col];
-	    if (statf->flip)
-		x = -x;
-	    if (statf->geometric)
-		x = log(x);
-	    if (statf->geom_abs)
-		x = log(fabs(x) + 1);
+            for (col = 0; col < ncols; col++) {
+                DCELL x;
+                int i;
 
-	    i = (int) floor(statf->count * (x - statf->min) / (statf->max - statf->min));
-	    statf->stats[i]++;
-	    statf->total++;
-	}
+                if (type == RASTER_TYPE)
+                    x = dcell[col];
+                else
+                    x = G3d_getDouble(map3d, col, row, depth);
+
+                if (Rast_is_d_null_value(&x))
+                    continue;
+
+                if (statf->flip)
+                    x = -x;
+                if (statf->geometric)
+                    x = log(x);
+                if (statf->geom_abs)
+                    x = log(fabs(x) + 1);
+
+                i = (int) floor(statf->count * (x - statf->min) / (statf->max - statf->min));
+                statf->stats[i]++;
+                statf->total++;
+            }
+        }
     }
 
     G_percent(row, nrows, 2);
-    Rast_close(fd);
+
+    if (type == RASTER_TYPE)
+        Rast_close(fd);
+    else
+        G3d_closeCell(map3d);
+
     G_free(dcell);
 }

Modified: grass/trunk/raster/r.colors/test.r.colors.sh
===================================================================
--- grass/trunk/raster/r.colors/test.r.colors.sh	2011-06-26 08:46:09 UTC (rev 46783)
+++ grass/trunk/raster/r.colors/test.r.colors.sh	2011-06-26 09:24:23 UTC (rev 46784)
@@ -52,6 +52,7 @@
 
 # The volume maps using r3.colors and r3.colors.out
 r3.colors    map=volume_double_null rules=example1 && r3.colors.out --o map=volume_double_null rules=test_volume_double_example1.txt
+r3.colors -e map=volume_double_null rules=example1 && r3.colors.out --o map=volume_double_null rules=test_volume_double_example1_hist.txt
 r3.colors    map=volume_double_null rules=example2 && r3.colors.out --o map=volume_double_null rules=test_volume_double_example2.txt
 r3.colors -g map=volume_double_null rules=example2 && r3.colors.out --o map=volume_double_null rules=test_volume_double_example2_log.txt
 r3.colors    map=volume_double_null rules=example3 && r3.colors.out --o map=volume_double_null rules=test_volume_double_example3.txt
@@ -64,4 +65,4 @@
 # Test the removement the raster3d color table, a default color table will be created
 r3.colors -r map=volume_double_null && r3.colors.out --o map=volume_double_null rules=test_volume_double_default.txt
 # Test the removement the raster color table, a default color table will be created
-r.colors -r map=test_elev_double && r.colors.out --o map=test_elev_double rules=test_elev_double_default.txt
\ No newline at end of file
+r.colors -r map=test_elev_double && r.colors.out --o map=test_elev_double rules=test_elev_double_default.txt



More information about the grass-commit mailing list