Explorar o código

G_print_colors() added (used by r.colors -p)
(merge from devbr6)


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@43864 15284696-431f-4ddb-bdfa-cd5b030d7da7

Martin Landa %!s(int64=15) %!d(string=hai) anos
pai
achega
89ab917934
Modificáronse 2 ficheiros con 72 adicións e 0 borrados
  1. 3 0
      include/rasterdefs.h
  2. 69 0
      lib/raster/color_print.c

+ 3 - 0
include/rasterdefs.h

@@ -174,6 +174,9 @@ void Rast__interpolate_color_rule(DCELL, unsigned char *, unsigned char *,
 /* color_org.c */
 void Rast__organize_colors(struct Colors *);
 
+/* color_print.c */
+int Rast_print_colors(const char *, const char *, FILE *);
+
 /* color_rand.c */
 void Rast_make_random_colors(struct Colors *, CELL, CELL);
 

+ 69 - 0
lib/raster/color_print.c

@@ -0,0 +1,69 @@
+/*!
+ * \file lib/gis/color_print.c
+ *
+ * \brief GIS Library - Print color table of raster map
+ *
+ * (C) 2010 by 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.
+ *
+ * \author Martin Landa <landa.martin gmail.com>
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <grass/gis.h>
+
+static int print_color_table(const char *, const char *, const char *, FILE *);
+
+/*!
+  \brief Print current color table of raster map to file
+
+  \param name name of raster map
+  \param mapset mapset of raster map
+  \param file file where to print color table
+
+  \return -1 on error (raster map not found)
+  \return -2 on error (reading color table failed)
+  \return 0 on success
+*/
+int Rast_print_colors(const char *name, const char *mapset, FILE *file)
+{
+    char element[512];
+    char xname[GNAME_MAX];
+        
+    strcpy(xname, name);
+    mapset = G_find_raster(xname, mapset);
+    if (!mapset)
+	return -1;
+    name = xname;
+    
+    /* first look for secondary color table in current mapset */
+    sprintf(element, "colr2/%s", mapset);
+    if (print_color_table(element, name, mapset, file) < 0)
+	if (print_color_table("colr", name, mapset, file) < 0)
+	    return -2;
+    
+    return 0;
+}
+
+int print_color_table(const char *element, const char *name,
+		      const char *mapset, FILE *file)
+{
+    FILE *fd;
+    char buf[4096];
+    
+    fd = G_fopen_old(element, name, mapset);
+    if (!fd)
+	return -1;
+    
+    while(fgets(buf, sizeof(buf), fd)) {
+	fwrite(buf, strlen(buf), 1, file);
+    }
+    
+    fclose(fd);
+    
+    return 0;
+}