[GRASS-SVN] r46754 - in grass/trunk/raster: r.colors r.colors.out

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jun 22 09:33:54 EDT 2011


Author: huhabla
Date: 2011-06-22 06:33:54 -0700 (Wed, 22 Jun 2011)
New Revision: 46754

Added:
   grass/trunk/raster/r.colors.out/local_proto.h
   grass/trunk/raster/r.colors.out/r3.colors.out.html
   grass/trunk/raster/r.colors.out/raster3d_main.c
   grass/trunk/raster/r.colors.out/raster_main.c
   grass/trunk/raster/r.colors.out/write_colors.c
Removed:
   grass/trunk/raster/r.colors.out/main.c
Modified:
   grass/trunk/raster/r.colors.out/Makefile
   grass/trunk/raster/r.colors/test.r.colors.sh
Log:
Do not use hacks while implementing r3.colors.out!

Modified: grass/trunk/raster/r.colors/test.r.colors.sh
===================================================================
--- grass/trunk/raster/r.colors/test.r.colors.sh	2011-06-22 12:36:41 UTC (rev 46753)
+++ grass/trunk/raster/r.colors/test.r.colors.sh	2011-06-22 13:33:54 UTC (rev 46754)
@@ -57,4 +57,4 @@
 r3.colors -a map=volume_double_null rules=example3 && r3.colors.out --o map=volume_double_null rules=test_volume_double_example3_logabs.txt
 r3.colors    map=volume_double_null rules=example4 && r3.colors.out --o map=volume_double_null rules=test_volume_double_example4.txt
 r3.colors -n map=volume_double_null volume=volume_double_null && r3.colors.out --o map=volume_double_null rules=test_volume_double_example4_inv.txt
-r3.colors    map=volume_double_null raster=test_elev_double   && r3.colors.out --o map=volume_double_null rules=test_volume_double_example5.ref
\ No newline at end of file
+r3.colors    map=volume_double_null raster=test_elev_double   && r3.colors.out --o map=volume_double_null rules=test_volume_double_example5.txt

Modified: grass/trunk/raster/r.colors.out/Makefile
===================================================================
--- grass/trunk/raster/r.colors.out/Makefile	2011-06-22 12:36:41 UTC (rev 46753)
+++ grass/trunk/raster/r.colors.out/Makefile	2011-06-22 13:33:54 UTC (rev 46754)
@@ -1,11 +1,17 @@
 MODULE_TOPDIR = ../..
 
-PGM = r.colors.out
+LIBES2 = $(RASTERLIB) $(GISLIB)
+LIBES3 = $(G3DLIB) $(RASTERLIB) $(GISLIB)
+DEPENDENCIES = $(G3DDEP) $(GISDEP) $(RASTERDEP)
 
-LIBES = $(RASTERLIB) $(GISLIB)
-DEPENDENCIES = $(RASTERDEP) $(GISDEP)
+PROGRAMS = r.colors.out r3.colors.out
 
-include $(MODULE_TOPDIR)/include/Make/Module.make
+r_colors_out_OBJS = raster_main.o write_colors.o
+r3_colors_out_OBJS = raster3d_main.o write_colors.o
 
-default: cmd
+include $(MODULE_TOPDIR)/include/Make/Multi.make
 
+default: multi
+
+$(BIN)/r.colors.out$(EXE): LIBES = $(LIBES2)
+$(BIN)/r3.colors.out$(EXE):  LIBES = $(LIBES3)

Added: grass/trunk/raster/r.colors.out/local_proto.h
===================================================================
--- grass/trunk/raster/r.colors.out/local_proto.h	                        (rev 0)
+++ grass/trunk/raster/r.colors.out/local_proto.h	2011-06-22 13:33:54 UTC (rev 46754)
@@ -0,0 +1,8 @@
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/G3d.h>
+#include <grass/raster.h>
+#include <grass/glocale.h>
+
+void write_colors(struct Colors *, struct FPRange*, const char *, int);
\ No newline at end of file

Deleted: grass/trunk/raster/r.colors.out/main.c
===================================================================
--- grass/trunk/raster/r.colors.out/main.c	2011-06-22 12:36:41 UTC (rev 46753)
+++ grass/trunk/raster/r.colors.out/main.c	2011-06-22 13:33:54 UTC (rev 46754)
@@ -1,168 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE:       r.colors.out
- *
- * AUTHOR(S):    Glynn Clements
- *
- * PURPOSE:      Allows export of the color table for a raster map layer.
- *
- * COPYRIGHT:    (C) 2008, 2010 Glynn Clements and the GRASS Development Team
- *
- *               This program is free software under the GNU General
- *               Public License (>=v2). Read the file COPYING that
- *               comes with GRASS for details.
- *
- ***************************************************************************/
-
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/raster.h>
-#include <grass/glocale.h>
-#ifdef USE_RASTER3D
-#include <grass/G3d.h>
-#endif
-
-static FILE *fp;
-static int perc;
-static DCELL min, max;
-
-static void write_rule(DCELL *val, int r, int g, int b)
-{
-    static DCELL v0;
-    static int r0 = -1, g0 = -1, b0 = -1;
-
-    if (v0 == *val && r0 == r && g0 == g && b0 == b)
-	return;
-    v0 = *val, r0 = r, g0 = g, b0 = b;
-
-    if (perc)
-	fprintf(fp, "%g%% %d:%d:%d\n", 100 * (*val - min) / (max - min), r, g, b);
-    else
-	fprintf(fp, "%g %d:%d:%d\n", *val, r, g, b);
-}
-
-int main(int argc, char **argv)
-{
-    struct GModule *module;
-    struct
-    {
-	struct Option *map, *file;
-    } opt;
-    struct
-    {
-	struct Flag *p;
-    } flag;
-    const char *name, *file;
-    struct Colors colors;
-    struct FPRange range;
-    int count;
-    int i;
-
-    G_gisinit(argv[0]);
-
-    module = G_define_module();
-#ifdef USE_RASTER3D
-    G_add_keyword(_("raster3d"));
-#else
-    G_add_keyword(_("raster"));
-#endif
-    
-    G_add_keyword(_("export"));
-    G_add_keyword(_("color table"));
-    
-#ifdef USE_RASTER3D
-    module->description =
-	_("Exports the color table associated with a raster3d map layer.");
-#else
-    module->description =
-	_("Exports the color table associated with a raster map layer.");
-#endif
-
-#ifdef USE_RASTER3D
-    opt.map = G_define_standard_option(G_OPT_R3_MAP);
-#else
-    opt.map = G_define_standard_option(G_OPT_R_MAP);
-#endif
-
-    opt.file = G_define_standard_option(G_OPT_F_OUTPUT);
-    opt.file->key = "rules";
-    opt.file->label = _("Path to output rules file");
-    opt.file->description = _("\"-\" to write to stdout");
-    opt.file->answer = "-";
-    
-    flag.p = G_define_flag();
-    flag.p->key = 'p';
-    flag.p->description = _("Output values as percentages");
-
-    if (G_parser(argc, argv))
-	exit(EXIT_FAILURE);
-
-    name = opt.map->answer;
-    file = opt.file->answer;
-    perc = flag.p->answer ? 1 : 0;
-
-#ifdef USE_RASTER3D
-    if (G3d_readColors(name, "", &colors) < 0)
-	G_fatal_error(_("Unable to read color table for raster3d map <%s>"), name);
-    G3d_readRange(name, "", &range);
-#else
-    if (Rast_read_colors(name, "", &colors) < 0)
-	G_fatal_error(_("Unable to read color table for raster map <%s>"), name);
-    Rast_read_fp_range(name, "", &range);
-#endif
-
-    Rast_get_fp_range_min_max(&range, &min, &max);
-
-    if (!file || strcmp(file, "-") == 0)
-	fp = stdout;
-    else {
-	fp = fopen(file, "w");
-	if (!fp)
-	    G_fatal_error(_("Unable to open output file <%s>"), file);
-    }
-
-    if (colors.version < 0) {
-	/* 3.0 format */
-	CELL lo, hi;
-
-	Rast_get_c_color_range(&lo, &hi, &colors);
-
-	for (i = lo; i <= hi; i++) {
-	    unsigned char r, g, b, set;
-	    DCELL val = (DCELL) i;
-	    Rast_lookup_c_colors(&i, &r, &g, &b, &set, 1, &colors);
-	    write_rule(&val, r, g, b);
-	}
-    }
-    else {
-	count = Rast_colors_count(&colors);
-
-	for (i = 0; i < count; i++) {
-	    DCELL val1, val2;
-	    unsigned char r1, g1, b1, r2, g2, b2;
-
-	    Rast_get_fp_color_rule(
-		&val1, &r1, &g1, &b1,
-		&val2, &r2, &g2, &b2,
-		&colors, count - 1 - i);
-
-	    write_rule(&val1, r1, g1, b1);
-	    write_rule(&val2, r2, g2, b2);
-	}
-    }
-
-    {
-	int r, g, b;
-	Rast_get_null_value_color(&r, &g, &b, &colors);
-	fprintf(fp, "nv %d:%d:%d\n", r, g, b);
-	Rast_get_default_color(&r, &g, &b, &colors);
-	fprintf(fp, "default %d:%d:%d\n", r, g, b);
-    }
-
-    if (fp != stdout)
-	fclose(fp);
-
-    exit(EXIT_SUCCESS);
-}

Added: grass/trunk/raster/r.colors.out/r3.colors.out.html
===================================================================
--- grass/trunk/raster/r.colors.out/r3.colors.out.html	                        (rev 0)
+++ grass/trunk/raster/r.colors.out/r3.colors.out.html	2011-06-22 13:33:54 UTC (rev 46754)
@@ -0,0 +1,25 @@
+<h2>DESCRIPTION</h2>
+
+<em>r3.colors.out</em> allows the user to export the color table for a
+raster3d map to a file which is suitable as input
+to <em><a href="r3.colors.html">r3.colors</a></em>.
+
+<h2>EXAMPLES</h2>
+
+<div class="code"><pre>
+r3.colors.out map=volume_1 rules=rules.txt
+r3.colors map=volume_2 rules=rules.txt
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em>
+  <a href="r3.colors.html">r3.colors</a>
+  <a href="r.colors.out.html">r.colors</a>
+</em>
+
+<h2>AUTHOR</h2>
+Glynn Clements
+
+<p>
+<i>Last changed: $Date: 2010-11-01 08:41:16 +0100 (Mo, 01. Nov 2010) $</i>

Added: grass/trunk/raster/r.colors.out/raster3d_main.c
===================================================================
--- grass/trunk/raster/r.colors.out/raster3d_main.c	                        (rev 0)
+++ grass/trunk/raster/r.colors.out/raster3d_main.c	2011-06-22 13:33:54 UTC (rev 46754)
@@ -0,0 +1,65 @@
+
+/****************************************************************************
+ *
+ * MODULE:       r.colors.out
+ *
+ * AUTHOR(S):    Glynn Clements
+ *
+ * PURPOSE:      Allows export of the color table for a raster map layer.
+ *
+ * COPYRIGHT:    (C) 2008, 2010 Glynn Clements and the GRASS Development Team
+ *
+ *               This program is free software under the GNU General
+ *               Public License (>=v2). Read the file COPYING that
+ *               comes with GRASS for details.
+ *
+ ***************************************************************************/
+
+#include "local_proto.h"
+
+/* Run in raster3d mode */
+int main(int argc, char **argv)
+{    
+    struct GModule *module;
+    struct
+    {
+	struct Option *map, *file;
+    } opt;
+    struct
+    {
+	struct Flag *p;
+    } flag;
+    struct Colors colors;
+    struct FPRange range;
+    
+    G_gisinit(argv[0]);
+
+    module = G_define_module();
+    G_add_keyword(_("raster3d"));
+    G_add_keyword(_("color table"));
+    G_add_keyword(_("export"));
+    module->description =
+    _("Exports the color table associated with a raster3d map layer.");
+
+    opt.map = G_define_standard_option(G_OPT_R3_MAP);
+    opt.file = G_define_standard_option(G_OPT_F_OUTPUT);
+    opt.file->key = "rules";
+    opt.file->label = _("Path to output rules file");
+    opt.file->description = _("\"-\" to write to stdout");
+    opt.file->answer = "-";
+    
+    flag.p = G_define_flag();
+    flag.p->key = 'p';
+    flag.p->description = _("Output values as percentages");
+
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    if (G3d_readColors(opt.map->answer, "", &colors) < 0)
+        G_fatal_error(_("Unable to read color table for raster3d map <%s>"), opt.map->answer);
+    G3d_readRange(opt.map->answer, "", &range);
+
+    write_colors(&colors, &range, opt.file->answer, flag.p->answer ? 1 : 0);
+
+    exit(EXIT_SUCCESS);
+}

Added: grass/trunk/raster/r.colors.out/raster_main.c
===================================================================
--- grass/trunk/raster/r.colors.out/raster_main.c	                        (rev 0)
+++ grass/trunk/raster/r.colors.out/raster_main.c	2011-06-22 13:33:54 UTC (rev 46754)
@@ -0,0 +1,66 @@
+
+/****************************************************************************
+ *
+ * MODULE:       r.colors.out
+ *
+ * AUTHOR(S):    Glynn Clements
+ *
+ * PURPOSE:      Allows export of the color table for a raster map layer.
+ *
+ * COPYRIGHT:    (C) 2008, 2010 Glynn Clements and the GRASS Development Team
+ *
+ *               This program is free software under the GNU General
+ *               Public License (>=v2). Read the file COPYING that
+ *               comes with GRASS for details.
+ *
+ ***************************************************************************/
+
+#include "local_proto.h"
+
+/* Run in raster mode */
+int main(int argc, char **argv)
+{    
+    struct GModule *module;
+    struct
+    {
+	struct Option *map, *file;
+    } opt;
+    struct
+    {
+	struct Flag *p;
+    } flag;
+    struct Colors colors;
+    struct FPRange range;
+
+    G_gisinit(argv[0]);
+
+    module = G_define_module();
+    G_add_keyword(_("raster"));
+    G_add_keyword(_("color table"));
+    G_add_keyword(_("export"));
+    module->description =
+    _("Exports the color table associated with a raster map layer.");
+   
+    opt.map = G_define_standard_option(G_OPT_R_MAP);
+    
+    opt.file = G_define_standard_option(G_OPT_F_OUTPUT);
+    opt.file->key = "rules";
+    opt.file->label = _("Path to output rules file");
+    opt.file->description = _("\"-\" to write to stdout");
+    opt.file->answer = "-";
+    
+    flag.p = G_define_flag();
+    flag.p->key = 'p';
+    flag.p->description = _("Output values as percentages");
+
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    if (Rast_read_colors(opt.map->answer, "", &colors) < 0)
+        G_fatal_error(_("Unable to read color table for raster map <%s>"), opt.map->answer);
+    Rast_read_fp_range(opt.map->answer, "", &range);
+
+    write_colors(&colors, &range, opt.file->answer, flag.p->answer ? 1 : 0);
+
+    exit(EXIT_SUCCESS);
+}

Copied: grass/trunk/raster/r.colors.out/write_colors.c (from rev 46750, grass/trunk/raster/r.colors.out/main.c)
===================================================================
--- grass/trunk/raster/r.colors.out/write_colors.c	                        (rev 0)
+++ grass/trunk/raster/r.colors.out/write_colors.c	2011-06-22 13:33:54 UTC (rev 46754)
@@ -0,0 +1,93 @@
+
+/****************************************************************************
+ *
+ * MODULE:       r.colors.out
+ *
+ * AUTHOR(S):    Glynn Clements
+ *
+ * PURPOSE:      Allows export of the color table for a raster map layer.
+ *
+ * COPYRIGHT:    (C) 2008, 2010 Glynn Clements and the GRASS Development Team
+ *
+ *               This program is free software under the GNU General
+ *               Public License (>=v2). Read the file COPYING that
+ *               comes with GRASS for details.
+ *
+ ***************************************************************************/
+
+#include "local_proto.h"
+
+static FILE *fp;
+static DCELL min, max;
+
+static void write_rule(DCELL *val, int r, int g, int b, int perc)
+{
+    static DCELL v0;
+    static int r0 = -1, g0 = -1, b0 = -1;
+
+    if (v0 == *val && r0 == r && g0 == g && b0 == b)
+	return;
+    v0 = *val, r0 = r, g0 = g, b0 = b;
+
+    if (perc)
+	fprintf(fp, "%g%% %d:%d:%d\n", 100 * (*val - min) / (max - min), r, g, b);
+    else
+	fprintf(fp, "%g %d:%d:%d\n", *val, r, g, b);
+}
+
+void write_colors(struct Colors *colors, struct FPRange*range, const char *file, 
+                  int perc)
+{
+    int i, count = 0;;
+
+    Rast_get_fp_range_min_max(range, &min, &max);
+
+    if (!file || strcmp(file, "-") == 0)
+	fp = stdout;
+    else {
+	fp = fopen(file, "w");
+	if (!fp)
+	    G_fatal_error(_("Unable to open output file <%s>"), file);
+    }
+
+    if (colors->version < 0) {
+	/* 3.0 format */
+	CELL lo, hi;
+
+	Rast_get_c_color_range(&lo, &hi, colors);
+
+	for (i = lo; i <= hi; i++) {
+	    unsigned char r, g, b, set;
+	    DCELL val = (DCELL) i;
+	    Rast_lookup_c_colors(&i, &r, &g, &b, &set, 1, colors);
+	    write_rule(&val, r, g, b, perc);
+	}
+    }
+    else {
+	count = Rast_colors_count(colors);
+
+	for (i = 0; i < count; i++) {
+	    DCELL val1, val2;
+	    unsigned char r1, g1, b1, r2, g2, b2;
+
+	    Rast_get_fp_color_rule(
+		&val1, &r1, &g1, &b1,
+		&val2, &r2, &g2, &b2,
+		colors, count - 1 - i);
+
+	    write_rule(&val1, r1, g1, b1, perc);
+	    write_rule(&val2, r2, g2, b2, perc);
+	}
+    }
+
+    {
+	int r, g, b;
+	Rast_get_null_value_color(&r, &g, &b, colors);
+	fprintf(fp, "nv %d:%d:%d\n", r, g, b);
+	Rast_get_default_color(&r, &g, &b, colors);
+	fprintf(fp, "default %d:%d:%d\n", r, g, b);
+    }
+
+    if (fp != stdout)
+	fclose(fp);
+}



More information about the grass-commit mailing list