4
#define LOOKUP_COLORS 2048
6
static int organizing = 0;
7
static int organize_lookup(struct Colors *, int);
8
static int organize_fp_lookup(struct Colors *, int);
9
static int double_comp(const void *, const void *);
11
int G__organize_colors(struct Colors *colors)
13
/* don't do anything if called recursively */
17
organize_lookup(colors, 0);
18
organize_lookup(colors, 1);
20
organize_fp_lookup(colors, 0);
21
organize_fp_lookup(colors, 1);
29
static int organize_fp_lookup(struct Colors *colors, int mod)
33
struct _Color_Info_ *cp;
34
struct _Color_Rule_ *rule;
37
cp = &colors->modular;
41
/* if one of the lookup tables exist, don't do anything */
42
if (cp->lookup.active || cp->fp_lookup.active)
47
cp->fp_lookup.vals = (DCELL *)
48
G_calloc(cp->n_rules * 2, sizeof(DCELL));
49
/* 2 endpoints for each rule */
50
cp->fp_lookup.rules = (struct _Color_Rule_ **)
51
G_calloc(cp->n_rules * 2, sizeof(struct _Color_Rule_ *));
53
/* get the list of DCELL values from set of all lows and highs
55
/* NOTE: if low==high in a rule, the value appears twice in a list
56
but if low==high of the previous, rule the value appears only once */
59
/* go through the list of rules from end to beginning,
60
because rules are sored in reverse order of reading,
61
and we want to read the in correct order, to ignore
62
the same values in the end of rule and beginning of next rule */
64
/* first go to the last rules */
65
for (rule = cp->rules; rule->next; rule = rule->next) ;
66
/* now traverse from the last to the first rule */
67
for (; rule; rule = rule->prev) {
68
/* check if the min is the same as previous maximum */
69
if (i == 0 || rule->low.value != cp->fp_lookup.vals[i - 1])
70
cp->fp_lookup.vals[i++] = rule->low.value;
71
cp->fp_lookup.vals[i++] = rule->high.value;
73
cp->fp_lookup.nalloc = i;
75
/* now sort the values */
76
qsort((char *)cp->fp_lookup.vals, cp->fp_lookup.nalloc,
77
sizeof(DCELL), &double_comp);
79
/* now find the rule to apply inbetween each 2 values in a list */
80
for (i = 0; i < cp->fp_lookup.nalloc - 1; i++) {
81
val = (cp->fp_lookup.vals[i] + cp->fp_lookup.vals[i + 1]) / 2.;
82
/* fprintf (stderr, "%lf %lf ", cp->fp_lookup.vals[i], cp->fp_lookup.vals[i+1]); */
84
for (rule = cp->rules; rule; rule = rule->next)
85
if (rule->low.value <= val && val <= rule->high.value)
87
/* if(rule) fprintf (stderr, "%d %lf %lf %d\n", i, rule->low.value, rule->high.value, rule);
88
else fprintf (stderr, "null\n");
90
cp->fp_lookup.rules[i] = rule;
92
cp->fp_lookup.active = 1;
97
static int organize_lookup(struct Colors *colors, int mod)
101
CELL cat[LOOKUP_COLORS];
102
struct _Color_Info_ *cp;
104
/* don't do anything if the color structure is float */
105
if (colors->is_float)
109
cp = &colors->modular;
113
if (cp->lookup.active)
116
n = (CELL) cp->max - (CELL) cp->min + 1;
117
if (n >= LOOKUP_COLORS || n <= 0)
121
for (i = 0; i < n; i++)
124
cp->lookup.nalloc = n;
125
cp->lookup.red = (unsigned char *)G_malloc(n);
126
cp->lookup.grn = (unsigned char *)G_malloc(n);
127
cp->lookup.blu = (unsigned char *)G_malloc(n);
128
cp->lookup.set = (unsigned char *)G_malloc(n);
130
G_zero(cp->lookup.set, n * sizeof(unsigned char));
131
G__lookup_colors((void *)cat,
132
cp->lookup.red, cp->lookup.grn, cp->lookup.blu,
133
cp->lookup.set, n, colors, mod, 1, CELL_TYPE);
135
cp->lookup.active = 1;
140
static int double_comp(const void *xx, const void *yy)
142
const DCELL *x = xx, *y = yy;