예제 #1
0
int write_contrast_colors (char* raster) {
struct Colors colors;
struct Categories cats;
FCOLORS  fcolors[9]={ /* colors for positive openness */
	{-2500, 0, 0, 50, NULL},
	{-100, 0, 0, 56, NULL},
	{-15, 0, 56, 128, NULL},
	{-3, 0, 128, 255, NULL},
	{0, 255, 255, 255, NULL},
	{3, 255, 128, 0, NULL},
	{15, 128, 56, 0, NULL},
	{100, 56, 0, 0, NULL},
	{2500, 50, 0, 0, NULL}};
int i;

Rast_init_colors(&colors);

	for(i=0;i<8;++i)
Rast_add_d_color_rule(
	&fcolors[i].cat, fcolors[i].r, fcolors[i].g, fcolors[i].b,
	&fcolors[i+1].cat, fcolors[i+1].r, fcolors[i+1].g, fcolors[i+1].b,
	&colors);
Rast_write_colors(raster, G_mapset(), &colors);
Rast_free_colors(&colors);
/*
Rast_init_cats("Forms", &cats);
	for(i=0;i<8;++i)
Rast_set_cat(&ccolors[i].cat, &ccolors[i].cat, ccolors[i].label, &cats, CELL_TYPE);
Rast_write_cats(raster, &cats);
Rast_free_cats(&cats);
*/
return 0;
}
예제 #2
0
/* parse input lines with the following formats
 *   val1:r:g:b val2:r:g:b
 *   val:r:g:b          (implies cat1==cat2)
 *
 * r:g:b can be just a single grey level
 *   cat1:x cat2:y
 *   cat:x
 *
 * optional lines are
 *    invert            invert color table
 *    shift:n           where n is the amount to shift the color table
 */
static int read_new_colors(FILE * fd, struct Colors *colors)
{
    double val1, val2;
    long cat1, cat2;
    int r1, g1, b1;
    int r2, g2, b2;
    char buf[1024];
    char word1[256], word2[256];
    int n, fp_rule;
    int null, undef;
    int modular;
    DCELL shift;

    if (fgets(buf, sizeof buf, fd) == NULL)
	return -1;
    G_strip(buf);

    if (sscanf(buf + 1, "%lf %lf", &val1, &val2) == 2)
	Rast_set_d_color_range((DCELL) val1, (DCELL) val2, colors);

    modular = 0;
    while (fgets(buf, sizeof buf, fd)) {
	null = undef = fp_rule = 0;
	*word1 = *word2 = 0;
	n = sscanf(buf, "%s %s", word1, word2);
	if (n < 1)
	    continue;

	if (sscanf(word1, "shift:%lf", &shift) == 1
	    || (strcmp(word1, "shift:") == 0 &&
		sscanf(word2, "%lf", &shift) == 1)) {
	    Rast_shift_d_colors(shift, colors);
	    continue;
	}
	if (strcmp(word1, "invert") == 0) {
	    Rast_invert_colors(colors);
	    continue;
	}
	if (strcmp(word1, "%%") == 0) {
	    modular = !modular;
	    continue;
	}

	switch (sscanf(word1, "nv:%d:%d:%d", &r1, &g1, &b1)) {
	case 1:
	    null = 1;
	    b1 = g1 = r1;
	    break;
	case 3:
	    null = 1;
	    break;
	}
	if (!null)
	    switch (sscanf(word1, "*:%d:%d:%d", &r1, &g1, &b1)) {
	    case 1:
		undef = 1;
		b1 = g1 = r1;
		break;
	    case 3:
		undef = 1;
		break;
	    }
	if (!null && !undef)
	    switch (sscanf(word1, "%ld:%d:%d:%d", &cat1, &r1, &g1, &b1)) {
	    case 2:
		b1 = g1 = r1;
		break;
	    case 4:
		break;
	    default:
		if (sscanf(word1, "%lf:%d:%d:%d", &val1, &r1, &g1, &b1) == 4)
		    fp_rule = 1;
		else if (sscanf(word1, "%lf:%d", &val1, &r1) == 2) {
		    fp_rule = 1;
		    b1 = g1 = r1;
		}
		else
		    continue;	/* other lines are ignored */
	    }
	if (n == 2) {
	    switch (sscanf(word2, "%ld:%d:%d:%d", &cat2, &r2, &g2, &b2)) {
	    case 2:
		b2 = g2 = r2;
		if (fp_rule)
		    val2 = (DCELL) cat2;
		break;
	    case 4:
		if (fp_rule)
		    val2 = (DCELL) cat2;
		break;
	    default:
		if (sscanf(word2, "%lf:%d:%d:%d", &val2, &r2, &g2, &b2) == 4) {
		    if (!fp_rule)
			val1 = (DCELL) cat1;
		    fp_rule = 1;
		}
		else if (sscanf(word2, "%lf:%d", &val2, &r2) == 2) {
		    if (!fp_rule)
			val1 = (DCELL) cat1;
		    fp_rule = 1;
		    b2 = g2 = r2;
		}
		else
		    continue;	/* other lines are ignored */
	    }
	}
	else {
	    if (!fp_rule)
		cat2 = cat1;
	    else
		val2 = val1;
	    r2 = r1;
	    g2 = g1;
	    b2 = b1;
	}
	if (null)
	    Rast_set_null_value_color(r1, g1, b1, colors);
	else if (undef)
	    Rast_set_default_color(r1, g1, b1, colors);

	else if (modular) {
	    if (fp_rule)
		Rast_add_modular_d_color_rule((DCELL *) & val1, r1, g1,
						  b1, (DCELL *) & val2, r2,
						  g2, b2, colors);
	    else
		Rast_add_modular_c_color_rule((CELL *) &cat1, r1, g1, b1,
					      (CELL *) &cat2, r2, g2, b2, colors);
	}
	else {
	    if (fp_rule)
		Rast_add_d_color_rule((DCELL *) & val1, r1, g1, b1,
				      (DCELL *) & val2, r2, g2, b2,
				      colors);
	    else
		Rast_add_c_color_rule((CELL *) &cat1, r1, g1, b1,
				      (CELL *) &cat2, r2, g2, b2, colors);
	}
	/*
	   fprintf (stderr, "adding rule %d=%.2lf %d %d %d  %d=%.2lf %d %d %d\n", cat1,val1,  r1, g1, b1, cat2, val2, r2, g2, b2);
	 */
    }
    return 1;
}