1 
2 /****************************************************************************
3  *
4  * MODULE:       r.colors.out
5  *
6  * AUTHOR(S):    Glynn Clements
7  *
8  * PURPOSE:      Allows export of the color table for a raster map.
9  *
10  * COPYRIGHT:    (C) 2008, 2010-2011 Glynn Clements and the GRASS Development Team
11  *
12  *               This program is free software under the GNU General
13  *               Public License (>=v2). Read the file COPYING that
14  *               comes with GRASS for details.
15  *
16  ***************************************************************************/
17 
18 #include <stdlib.h>
19 #include <string.h>
20 #include <grass/gis.h>
21 #include <grass/raster.h>
22 #include <grass/glocale.h>
23 
24 /* Run in raster mode */
main(int argc,char ** argv)25 int main(int argc, char **argv)
26 {
27     struct GModule *module;
28     struct
29     {
30 	struct Option *map, *file;
31     } opt;
32     struct
33     {
34 	struct Flag *p;
35     } flag;
36 
37     const char *file;
38     FILE * fp;
39     struct Colors colors;
40     struct FPRange range;
41 
42     G_gisinit(argv[0]);
43 
44     module = G_define_module();
45     G_add_keyword(_("raster"));
46     G_add_keyword(_("color table"));
47     G_add_keyword(_("export"));
48     module->description =
49 	_("Exports the color table associated with a raster map.");
50 
51     opt.map = G_define_standard_option(G_OPT_R_MAP);
52 
53     opt.file = G_define_standard_option(G_OPT_F_OUTPUT);
54     opt.file->key = "rules";
55     opt.file->label = _("Path to output rules file");
56     opt.file->description = _("If not given write to standard output");
57     opt.file->required = NO;
58 
59     flag.p = G_define_flag();
60     flag.p->key = 'p';
61     flag.p->description = _("Output values as percentages");
62 
63     if (G_parser(argc, argv))
64 	exit(EXIT_FAILURE);
65 
66     file = opt.file->answer;
67 
68     if (Rast_read_colors(opt.map->answer, "", &colors) < 0)
69         G_fatal_error(_("Unable to read color table for raster map <%s>"),
70 		      opt.map->answer);
71 
72     Rast_read_fp_range(opt.map->answer, "", &range);
73 
74     if (!file || strcmp(file, "-") == 0)
75 	fp = stdout;
76     else {
77 	fp = fopen(file, "w");
78 	if (!fp)
79 	    G_fatal_error(_("Unable to open output file <%s>"), file);
80     }
81 
82     Rast_print_colors(&colors, range.min, range.max, fp,
83 		      flag.p->answer ? 1 : 0);
84 
85     exit(EXIT_SUCCESS);
86 }
87