File: color_write.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (290 lines) | stat: -rw-r--r-- 8,727 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**********************************************************************
 *  G_write_colors (name, mapset, colors)
 *      char *name                   name of map
 *      char *mapset                 mapset that map belongs to
 *      struct Colors *colors        structure holding color info
 *
 *  Writes the color information associated with map layer "map"
 *  in mapset "mapset" from the structure "colors".
 *
 *  returns:    1  if successful
 *             -1  on fail
 *
 * If the environment variable FORCE_GRASS3_COLORS is set (to anything at all)
 * then the output format is 3.0, even if the structure contains 4.0 rules.
 * This allows users to create 3.0 color files for export to sites which
 * don't yet have 4.0
 ***********************************************************************/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "gis.h"
#define PRECISION 30
#define THRESHOLD .0000000000000000000000000000005
/* .5 * 10 ^(-30) */

static int write_new_colors( FILE *, struct Colors *);
static int write_rules ( FILE *, struct _Color_Rule_ *, DCELL, DCELL);
static int write_old_colors ( FILE *, struct Colors *);
static int forced_write_old_colors ( FILE *, struct Colors *);
static int format_min (char *, double);
static int format_max (char *, double);


/*!
 * \brief write map layer color table
 *
 * The color table is written for the
 * raster file <b>name</b> in the specified <b>mapset</b> from the
 * <b>colors</b> structure.
 * If there is an error, -1 is returned. No diagnostic is printed. Otherwise, 1
 * is returned.
 * The <b>colors</b> structure must be created properly, i.e.,
 * <i>G_init_colors</i> to initialize the structure and
 * <i>G_add_color_rule</i> to set the category colors.\remarks{These
 * routines are called by higher level routines which read or create entire
 * color tables, such as<i>G_read_colors</i> or
 * <i>G_make_ramp_colors.</i>}
 * <b>Note.</b> The calling sequence for this function deserves special
 * attention. The <b>mapset</b> parameter seems to imply that it is possible
 * to overwrite the color table for a raster file which is in another mapset.
 * However, this is not what actually happens. It is very useful for users to
 * create their own color tables for raster files in other mapsets, but without
 * overwriting other users' color tables for the same raster file. If
 * <b>mapset</b> is the current mapset, then the color file for <b>name</b>
 * will be overwritten by the new color table. But if <b>mapset</b> is not the
 * current mapset, then the color table is actually written in the current
 * mapset under the <b>colr2</b> element as: colr2/mapset/name.
 *
 *  \param name
 *  \param mapset
 *  \param colors
 *  \return int
 */

 
/*!
 * \brief 
 *
 * The rules are written out using
 * floating-point format, removing trailing zeros (possibly producing integers).
 * The flag marking the colors as floating-point is <b>not</b> written.
 *
 *  \return int
 */

int G_write_colors (char *name, char *mapset, struct Colors *colors)
{
    char element[512];
    char xname[512], xmapset[512];
    FILE *fd;
    int stat;

    if (G__name_is_fully_qualified (name, xname, xmapset))
    {
	if (strcmp (xmapset, mapset) != 0)
	    return -1;
	name = xname;
    }
/*
 * if mapset is current mapset, remove colr2 file (created by pre 3.0 grass)
 *    and then write original color table
 * else write secondary color table
 */
    sprintf (element, "colr2/%s", mapset);
    if (strcmp (mapset, G_mapset()) == 0)
    {
	G_remove (element, name);	/* get rid of existing colr2, if any */
	strcpy (element, "colr");
    }
    if (!(fd = G_fopen_new (element, name)))
	return -1;

    stat = G__write_colors (fd, colors) ;
    fclose (fd);
    return stat;
}

int G__write_colors ( FILE *fd, struct Colors *colors)
{
    if (getenv("FORCE_GRASS3_COLORS"))
	return forced_write_old_colors (fd, colors);
    else if (colors->version < 0)
	return write_old_colors (fd, colors);
    else
	return write_new_colors (fd, colors);
}

static int write_new_colors( FILE *fd, struct Colors *colors)
{
    char str1[100], str2[100];

    format_min(str1, (double) colors->cmin);
    format_max(str2, (double) colors->cmax);
    fprintf(fd, "%% %s %s\n", str1, str2);

    if (colors->shift)
    {
        sprintf(str2, "%.10f", (double) colors->shift);
        G_trim_decimal(str2);
	fprintf (fd, "shift:%s\n", str2);
    }
    if (colors->invert)
	fprintf (fd, "invert\n");

    if (colors->null_set)
    {
        fprintf(fd, "nv:%d", colors->null_red); 
	if (colors->null_red != colors->null_grn || colors->null_red 
             != colors->null_blu)
	    fprintf (fd, ":%d:%d", colors->null_grn, colors->null_blu);
	fprintf (fd, "\n");
    }
    if (colors->undef_set)
    {
        fprintf(fd, "*:%d", colors->undef_red); 
	if (colors->undef_red != colors->undef_grn || colors->undef_red 
             != colors->undef_blu)
	    fprintf (fd, ":%d:%d", colors->undef_grn, colors->undef_blu);
	fprintf (fd, "\n");
    }
    if (colors->modular.rules)
    {
	fprintf (fd, "%s\n","%%");
	write_rules(fd, colors->modular.rules, colors->cmin, colors->cmax);
	fprintf (fd, "%s\n","%%");
    }
    if (colors->fixed.rules)
	write_rules(fd, colors->fixed.rules, colors->cmin, colors->cmax);

    return 1;
}

static int write_rules (
    FILE *fd,
    struct _Color_Rule_ *crules,
    DCELL dmin,
    DCELL dmax /* overall min and max data values in color table */
)
{
    struct _Color_Rule_ *rule;
    char str[100];

/* find the end of the rules list */
    rule = crules;
    while (rule->next)
	rule = rule->next;

/* write out the rules in reverse order */
    for ( ; rule; rule = rule->prev)
    {
	if(rule->low.value == dmin)
	   format_min(str, (double) rule->low.value);
        else
	{
	   sprintf(str, "%.10f", (double) rule->low.value);
	   G_trim_decimal(str);
        }
	fprintf (fd, "%s:%d", str, (int) rule->low.red);
	if (rule->low.red != rule->low.grn || rule->low.red != rule->low.blu)
	    fprintf (fd, ":%d:%d", rule->low.grn, rule->low.blu);
        /* even if low==high, write second end when the high is dmax */
	if (rule->high.value == dmax || rule->low.value != rule->high.value)
	{
	    if(rule->high.value == dmax)
	         format_max(str, (double) rule->high.value);
            else
	    {
	         sprintf(str, "%.10f", (double) rule->high.value);
	         G_trim_decimal(str);
            }
	    fprintf (fd, " %s:%d", str, (int) rule->high.red);
	    if (rule->high.red != rule->high.grn || rule->high.red != rule->high.blu)
		fprintf (fd, ":%d:%d", rule->high.grn, rule->high.blu);
	}
	fprintf (fd, "\n");
    }

    return 0;
}

static int write_old_colors ( FILE *fd, struct Colors *colors)
{
    int i,n;

    fprintf (fd, "#%ld first color\n", (long)colors->fixed.min) ;
    if(colors->null_set)
    { 
         fprintf (fd, "%d %d %d\n",
     	     (int)colors->null_red,
	     (int)colors->null_grn,
     	     (int)colors->null_blu);
    }
    else fprintf (fd, "255 255 255\n");  /* white */

    n = colors->fixed.max - colors->fixed.min + 1;

    for (i=0; i < n; i++)  
    {
	fprintf ( fd, "%d", (int)colors->fixed.lookup.red[i]);
	if (colors->fixed.lookup.red[i] != colors->fixed.lookup.grn[i] 
	||  colors->fixed.lookup.red[i] != colors->fixed.lookup.blu[i])
	    fprintf ( fd, " %d %d",
		(int)colors->fixed.lookup.grn[i],
		(int)colors->fixed.lookup.blu[i]) ;
	fprintf (fd, "\n");
    }

    return 1;
}

static int forced_write_old_colors ( FILE *fd, struct Colors *colors)
{
    int red,grn,blu;
    CELL cat;

    fprintf (fd, "#%ld first color\n", (long)colors->cmin) ;
    G_get_color ((CELL)0, &red, &grn, &blu, colors);
    fprintf (fd, "%d %d %d\n", red, grn, blu);

    for (cat = colors->cmin; cat <= colors->cmax; cat++)
    {
	G_get_color (cat, &red, &grn, &blu, colors);
	fprintf ( fd, "%d", red);
	if (red != grn || red != blu)
	    fprintf ( fd, " %d %d", grn, blu);
	fprintf (fd, "\n");
    }

    return 1;
}

static int format_min (char *str, double dval)
{
   double dtmp;
   sprintf(str, "%.*f", PRECISION, dval);
   G_trim_decimal(str);
   sscanf(str, "%lf", &dtmp);
   if(dtmp!=dval) /* if  no zeros after decimal point were trimmed */
   {
       sprintf(str, "%.*f", PRECISION, dval - THRESHOLD);
       /* because precision is probably higher than PRECISION */
   }

   return 0;
}

static int format_max (char *str, double dval)
{
   double dtmp;
   sprintf(str, "%.*f", PRECISION, dval);
   G_trim_decimal(str);
   sscanf(str, "%lf", &dtmp);
   if(dtmp!=dval) /* if  no zeros after decimal point were trimmed */
   {
       sprintf(str, "%.*f", PRECISION, dval + THRESHOLD);
       /* because precision is probably higher than PRECISION */
   }

   return 0;
}