GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
raster/color_write.c
Go to the documentation of this file.
1 /*!
2  * \file lib/raster/color_write.c
3  *
4  * \brief Raster Library - Write color table of raster map
5  *
6  * (C) 1999-2009 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public
9  * License (>=v2). Read the file COPYING that comes with GRASS
10  * for details.
11  *
12  * \author USACERL and many others
13  */
14 
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <grass/gis.h>
19 #include <grass/glocale.h>
20 #include <grass/raster.h>
21 
22 static void write_rules(FILE *, struct _Color_Rule_ *, DCELL, DCELL);
23 static void write_new_colors(FILE *, struct Colors *);
24 static void write_old_colors(FILE *, struct Colors *);
25 static void forced_write_old_colors(FILE *, struct Colors *);
26 static void format_min(char *, double);
27 static void format_max(char *, double);
28 
29 /*!
30  * \brief Write map layer color table
31  *
32  * The color table is written for the raster map <i>name</i> in the
33  * specified <i>mapset</i> from the <i>colors</i> structure.
34  *
35  * If there is an error, -1 is returned. No diagnostic is
36  * printed. Otherwise, 1 is returned.
37  *
38  * The <i>colors</i> structure must be created properly, i.e.,
39  * Rast_init_colors() to initialize the structure and Rast_add_c_color_rule()
40  * to set the category colors. These routines are called by
41  * higher level routines which read or create entire color tables,
42  * such as Rast_read_colors() or Rast_make_ramp_colors().
43  *
44  * <b>Note:</b> The calling sequence for this function deserves
45  * special attention. The <i>mapset</i> parameter seems to imply that
46  * it is possible to overwrite the color table for a raster map which
47  * is in another mapset. However, this is not what actually
48  * happens. It is very useful for users to create their own color
49  * tables for raster maps in other mapsets, but without overwriting
50  * other users' color tables for the same raster map. If <i>mapset</i>
51  * is the current mapset, then the color file for <i>name</i> will be
52  * overwritten by the new color table. But if <i>mapset</i> is not the
53  * current mapset, then the color table is actually written in the
54  * current mapset under the <tt>colr2</tt> element as:
55  * <tt>colr2/mapset/name</tt>.
56  *
57  * The rules are written out using floating-point format, removing
58  * trailing zeros (possibly producing integers). The flag marking the
59  * colors as floating-point is <b>not</b> written.
60  *
61  * If the environment variable FORCE_GRASS3_COLORS is set (to anything at all)
62  * then the output format is 3.0, even if the structure contains 4.0 rules.
63  * This allows users to create 3.0 color files for export to sites which
64  * don't yet have 4.0
65  *
66  * \param name map name
67  * \param mapset mapset name
68  * \param colors pointer to structure Colors which holds color info
69  *
70  * \return void
71  */
72 void Rast_write_colors(const char *name, const char *mapset,
73  struct Colors *colors)
74 {
75  char element[512];
76  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
77  FILE *fd;
78 
79  if (G_name_is_fully_qualified(name, xname, xmapset)) {
80  if (strcmp(xmapset, mapset) != 0)
81  G_fatal_error(_("Qualified name <%s> doesn't match mapset <%s>"),
82  name, mapset);
83  name = xname;
84  }
85  /*
86  * if mapset is current mapset, remove colr2 file (created by pre 3.0 grass)
87  * and then write original color table
88  * else write secondary color table
89  */
90  sprintf(element, "colr2/%s", mapset);
91  if (strcmp(mapset, G_mapset()) == 0) {
92  G_remove(element, name); /* get rid of existing colr2, if any */
93  strcpy(element, "colr");
94  }
95  if (!(fd = G_fopen_new(element, name)))
96  G_fatal_error(_("Unable to create <%s> file for map <%s>"),
97  element, name);
98 
99  Rast__write_colors(fd, colors);
100  fclose(fd);
101 }
102 
103 /*!
104  * \brief Write map layer color table
105  *
106  * \param fd file descriptor
107  * \param colors pointer to Colors structure which holds color info
108  */
109 void Rast__write_colors(FILE * fd, struct Colors *colors)
110 {
111  if (getenv("FORCE_GRASS3_COLORS"))
112  forced_write_old_colors(fd, colors);
113  else if (colors->version < 0)
114  write_old_colors(fd, colors);
115  else
116  write_new_colors(fd, colors);
117 }
118 
119 static void write_new_colors(FILE * fd, struct Colors *colors)
120 {
121  char str1[100], str2[100];
122 
123  format_min(str1, (double)colors->cmin);
124  format_max(str2, (double)colors->cmax);
125  fprintf(fd, "%% %s %s\n", str1, str2);
126 
127  if (colors->shift) {
128  sprintf(str2, "%.17g", (double)colors->shift);
129  G_trim_decimal(str2);
130  fprintf(fd, "shift:%s\n", str2);
131  }
132  if (colors->invert)
133  fprintf(fd, "invert\n");
134 
135  if (colors->null_set) {
136  fprintf(fd, "nv:%d", colors->null_red);
137  if (colors->null_red != colors->null_grn || colors->null_red
138  != colors->null_blu)
139  fprintf(fd, ":%d:%d", colors->null_grn, colors->null_blu);
140  fprintf(fd, "\n");
141  }
142  if (colors->undef_set) {
143  fprintf(fd, "*:%d", colors->undef_red);
144  if (colors->undef_red != colors->undef_grn || colors->undef_red
145  != colors->undef_blu)
146  fprintf(fd, ":%d:%d", colors->undef_grn, colors->undef_blu);
147  fprintf(fd, "\n");
148  }
149  if (colors->modular.rules) {
150  fprintf(fd, "%s\n", "%%");
151  write_rules(fd, colors->modular.rules, colors->cmin, colors->cmax);
152  fprintf(fd, "%s\n", "%%");
153  }
154  if (colors->fixed.rules)
155  write_rules(fd, colors->fixed.rules, colors->cmin, colors->cmax);
156 }
157 
158 /* overall min and max data values in color table */
159 static void write_rules(FILE * fd, struct _Color_Rule_ *crules, DCELL dmin,
160  DCELL dmax)
161 {
162  struct _Color_Rule_ *rule;
163  char str[100];
164 
165  /* find the end of the rules list */
166  rule = crules;
167  while (rule->next)
168  rule = rule->next;
169 
170  /* write out the rules in reverse order */
171  for (; rule; rule = rule->prev) {
172  if (rule->low.value == dmin)
173  format_min(str, (double)rule->low.value);
174  else {
175  sprintf(str, "%.17g", (double)rule->low.value);
176  G_trim_decimal(str);
177  }
178  fprintf(fd, "%s:%d", str, (int)rule->low.red);
179  if (rule->low.red != rule->low.grn || rule->low.red != rule->low.blu)
180  fprintf(fd, ":%d:%d", rule->low.grn, rule->low.blu);
181  /* even if low==high, write second end when the high is dmax */
182  if (rule->high.value == dmax || rule->low.value != rule->high.value) {
183  if (rule->high.value == dmax)
184  format_max(str, (double)rule->high.value);
185  else {
186  sprintf(str, "%.17g", (double)rule->high.value);
187  G_trim_decimal(str);
188  }
189  fprintf(fd, " %s:%d", str, (int)rule->high.red);
190  if (rule->high.red != rule->high.grn ||
191  rule->high.red != rule->high.blu)
192  fprintf(fd, ":%d:%d", rule->high.grn, rule->high.blu);
193  }
194  fprintf(fd, "\n");
195  }
196 }
197 
198 static void write_old_colors(FILE * fd, struct Colors *colors)
199 {
200  int i, n;
201 
202  fprintf(fd, "#%ld first color\n", (long)colors->fixed.min);
203  if (colors->null_set) {
204  fprintf(fd, "%d %d %d\n",
205  (int)colors->null_red,
206  (int)colors->null_grn, (int)colors->null_blu);
207  }
208  else
209  fprintf(fd, "255 255 255\n"); /* white */
210 
211  n = colors->fixed.max - colors->fixed.min + 1;
212 
213  for (i = 0; i < n; i++) {
214  fprintf(fd, "%d", (int)colors->fixed.lookup.red[i]);
215  if (colors->fixed.lookup.red[i] != colors->fixed.lookup.grn[i]
216  || colors->fixed.lookup.red[i] != colors->fixed.lookup.blu[i])
217  fprintf(fd, " %d %d",
218  (int)colors->fixed.lookup.grn[i],
219  (int)colors->fixed.lookup.blu[i]);
220  fprintf(fd, "\n");
221  }
222 }
223 
224 static void forced_write_old_colors(FILE * fd, struct Colors *colors)
225 {
226  int red, grn, blu;
227  CELL cat;
228 
229  fprintf(fd, "#%ld first color\n", (long)colors->cmin);
230  cat = 0;
231  Rast_get_c_color(&cat, &red, &grn, &blu, colors);
232  fprintf(fd, "%d %d %d\n", red, grn, blu);
233 
234  for (cat = colors->cmin; cat <= colors->cmax; cat++) {
235  Rast_get_c_color(&cat, &red, &grn, &blu, colors);
236  fprintf(fd, "%d", red);
237  if (red != grn || red != blu)
238  fprintf(fd, " %d %d", grn, blu);
239  fprintf(fd, "\n");
240  }
241 }
242 
243 
244 static void format_min(char *str, double dval)
245 {
246  double dtmp;
247 
248  sprintf(str, "%.17g", dval);
249  /* Note that G_trim_decimal() does not trim e.g. 1.0000000e-20 */
250  G_trim_decimal(str);
251  sscanf(str, "%lf", &dtmp);
252  if (dtmp != dval) { /* if no zeros after decimal point were trimmed */
253  /* lower dval by GRASS_EPSILON fraction */
254  if (dval > 0)
255  sprintf(str, "%.17g", dval * (1 - GRASS_EPSILON));
256  else
257  sprintf(str, "%.17g", dval * (1 + GRASS_EPSILON));
258  }
259 }
260 
261 
262 static void format_max(char *str, double dval)
263 {
264  double dtmp;
265 
266  sprintf(str, "%.17g", dval);
267  /* Note that G_trim_decimal() does not trim e.g. 1.0000000e-20 */
268  G_trim_decimal(str);
269  sscanf(str, "%lf", &dtmp);
270  if (dtmp != dval) { /* if no zeros after decimal point were trimmed */
271  /* increase dval by by GRASS_EPSILON fraction */
272  if (dval > 0)
273  sprintf(str, "%.17g", dval * (1 + GRASS_EPSILON));
274  else
275  sprintf(str, "%.17g", dval * (1 - GRASS_EPSILON));
276  }
277 }
struct _Color_Info_ fixed
Definition: gis.h:690
int null_set
Definition: gis.h:682
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
unsigned char null_blu
Definition: gis.h:685
int G_remove(const char *, const char *)
Remove a database file.
Definition: remove.c:44
unsigned char * blu
Definition: gis.h:658
#define GMAPSET_MAX
Definition: gis.h:178
DCELL shift
Definition: gis.h:679
unsigned char * red
Definition: gis.h:656
#define GRASS_EPSILON
Definition: gis.h:159
int version
Definition: gis.h:678
unsigned char null_grn
Definition: gis.h:684
double DCELL
Definition: gis.h:614
DCELL cmax
Definition: gis.h:693
struct _Color_Value_ low high
Definition: gis.h:644
unsigned char undef_red
Definition: gis.h:687
struct _Color_Rule_ * next
Definition: gis.h:645
unsigned char * grn
Definition: gis.h:657
Definition: lidar.h:86
DCELL cmin
Definition: gis.h:692
DCELL min
Definition: gis.h:673
void Rast_write_colors(const char *name, const char *mapset, struct Colors *colors)
Write map layer color table.
FILE * G_fopen_new(const char *, const char *)
Open a new database file.
Definition: gis/open.c:220
DCELL value
Definition: gis.h:636
unsigned char null_red
Definition: gis.h:683
Definition: gis.h:676
int Rast_get_c_color(const CELL *, int *, int *, int *, struct Colors *)
Gets color from raster map (CELL)
Definition: color_get.c:68
unsigned char undef_blu
Definition: gis.h:689
unsigned char undef_grn
Definition: gis.h:688
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
int invert
Definition: gis.h:680
#define GNAME_MAX
Definition: gis.h:177
int CELL
Definition: gis.h:613
unsigned char red
Definition: gis.h:637
int undef_set
Definition: gis.h:686
#define _(str)
Definition: glocale.h:10
unsigned char blu
Definition: gis.h:639
void Rast__write_colors(FILE *fd, struct Colors *colors)
Write map layer color table.
struct _Color_Info_::@3 lookup
struct _Color_Info_ modular
Definition: gis.h:691
DCELL max
Definition: gis.h:673
unsigned char grn
Definition: gis.h:638
const char * name
Definition: named_colr.c:7
char * getenv()
int G_name_is_fully_qualified(const char *, char *, char *)
Check if map name is fully qualified (map @ mapset)
Definition: nme_in_mps.c:36
struct _Color_Rule_ * prev
Definition: gis.h:646
struct _Color_Rule_ * rules
Definition: gis.h:651
void G_trim_decimal(char *)
Removes trailing zeros from decimal number.
Definition: trim_dec.c:24