1 |
greg |
1.1 |
#ifndef lint |
2 |
greg |
2.10 |
static const char RCSid[] = "$Id: lamps.c,v 2.9 2003/06/08 12:03:09 schorsch Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Load lamp data. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
greg |
2.7 |
#include "copyright.h" |
9 |
greg |
2.6 |
|
10 |
greg |
1.1 |
#include <stdio.h> |
11 |
greg |
2.6 |
#include <stdlib.h> |
12 |
greg |
1.1 |
#include <ctype.h> |
13 |
schorsch |
2.8 |
|
14 |
|
|
#include "standard.h" |
15 |
greg |
2.5 |
#include "color.h" |
16 |
greg |
1.1 |
|
17 |
greg |
2.6 |
extern char *eindex(), *expsave(); |
18 |
greg |
1.1 |
|
19 |
|
|
typedef struct lamp { |
20 |
|
|
char *pattern; /* search pattern */ |
21 |
|
|
float *color; /* pointer to lamp value */ |
22 |
|
|
struct lamp *next; /* next lamp in list */ |
23 |
|
|
} LAMP; /* a lamp entry */ |
24 |
|
|
|
25 |
|
|
static LAMP *lamps = NULL; /* lamp list */ |
26 |
|
|
|
27 |
|
|
|
28 |
|
|
float * |
29 |
schorsch |
2.9 |
matchlamp( /* see if string matches any lamp */ |
30 |
|
|
char *s |
31 |
|
|
) |
32 |
greg |
1.1 |
{ |
33 |
|
|
register LAMP *lp; |
34 |
|
|
|
35 |
|
|
for (lp = lamps; lp != NULL; lp = lp->next) { |
36 |
|
|
expset(lp->pattern); |
37 |
|
|
if (eindex(s) != NULL) |
38 |
|
|
return(lp->color); |
39 |
|
|
} |
40 |
|
|
return(NULL); |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
|
44 |
schorsch |
2.9 |
int |
45 |
|
|
loadlamps( /* load lamp type file */ |
46 |
|
|
char *file |
47 |
|
|
) |
48 |
greg |
1.1 |
{ |
49 |
|
|
LAMP *lastp; |
50 |
|
|
register LAMP *lp; |
51 |
|
|
FILE *fp; |
52 |
|
|
float xyz[3]; |
53 |
|
|
char buf[128], str[128]; |
54 |
|
|
register char *cp1, *cp2; |
55 |
|
|
|
56 |
greg |
2.4 |
if ((fp = frlibopen(file)) == NULL) |
57 |
greg |
1.1 |
return(0); |
58 |
|
|
lastp = NULL; |
59 |
|
|
while (fgets(buf, sizeof(buf), fp) != NULL) { |
60 |
|
|
/* work on a copy of buffer */ |
61 |
|
|
strcpy(str, buf); |
62 |
|
|
/* get pattern for this entry */ |
63 |
|
|
for (cp1 = str; isspace(*cp1); cp1++) |
64 |
|
|
; |
65 |
|
|
if (!*cp1 || *cp1 == '#') |
66 |
|
|
continue; |
67 |
|
|
for (cp2 = cp1+1; *cp2; cp2++) |
68 |
|
|
if (*cp2 == *cp1 && cp2[-1] != '\\') |
69 |
|
|
break; |
70 |
|
|
if (!*cp2) { |
71 |
|
|
cp1 = "unclosed pattern"; |
72 |
|
|
goto fmterr; |
73 |
|
|
} |
74 |
|
|
cp1++; *cp2++ = '\0'; |
75 |
|
|
if (ecompile(cp1, 1, 0) < 0) { |
76 |
|
|
cp1 = "bad regular expression"; |
77 |
|
|
goto fmterr; |
78 |
|
|
} |
79 |
|
|
if ((lp = (LAMP *)malloc(sizeof(LAMP))) == NULL) |
80 |
|
|
goto memerr; |
81 |
|
|
if ((lp->pattern = expsave()) == NULL) |
82 |
|
|
goto memerr; |
83 |
|
|
/* get specification */ |
84 |
|
|
for (cp1 = cp2; isspace(*cp1); cp1++) |
85 |
|
|
; |
86 |
|
|
if (!isdigit(*cp1) && *cp1 != '.' && *cp1 != '(') { |
87 |
|
|
cp1 = "missing lamp specification"; |
88 |
|
|
goto fmterr; |
89 |
|
|
} |
90 |
|
|
if (*cp1 == '(') { /* find alias */ |
91 |
|
|
for (cp2 = ++cp1; *cp2; cp2++) |
92 |
|
|
if (*cp2 == ')' && cp2[-1] != '\\') |
93 |
|
|
break; |
94 |
|
|
*cp2 = '\0'; |
95 |
|
|
if ((lp->color = matchlamp(cp1)) == NULL) { |
96 |
|
|
cp1 = "unmatched alias"; |
97 |
|
|
goto fmterr; |
98 |
|
|
} |
99 |
|
|
} else { /* or read specificaion */ |
100 |
greg |
2.3 |
if ((lp->color=(float *)malloc(6*sizeof(float)))==NULL) |
101 |
greg |
1.1 |
goto memerr; |
102 |
greg |
2.3 |
if (sscanf(cp1, "%f %f %f", &lp->color[3], & |
103 |
|
|
lp->color[4], &lp->color[5]) != 3) { |
104 |
greg |
1.1 |
cp1 = "bad lamp data"; |
105 |
|
|
goto fmterr; |
106 |
|
|
} |
107 |
|
|
/* convert xyY to XYZ */ |
108 |
greg |
2.3 |
xyz[1] = lp->color[5]; |
109 |
|
|
xyz[0] = lp->color[3]/lp->color[4] * xyz[1]; |
110 |
|
|
xyz[2] = xyz[1]*(1./lp->color[4] - 1.) - xyz[0]; |
111 |
greg |
1.1 |
/* XYZ to RGB */ |
112 |
|
|
cie_rgb(lp->color, xyz); |
113 |
|
|
} |
114 |
|
|
if (lastp == NULL) |
115 |
|
|
lamps = lp; |
116 |
|
|
else |
117 |
|
|
lastp->next = lp; |
118 |
|
|
lp->next = NULL; |
119 |
|
|
lastp = lp; |
120 |
|
|
} |
121 |
|
|
fclose(fp); |
122 |
greg |
1.3 |
return(lastp != NULL); |
123 |
greg |
1.1 |
memerr: |
124 |
|
|
fputs("Out of memory in loadlamps\n", stderr); |
125 |
|
|
return(-1); |
126 |
|
|
fmterr: |
127 |
|
|
fputs(buf, stderr); |
128 |
|
|
fprintf(stderr, "%s: %s\n", file, cp1); |
129 |
|
|
return(-1); |
130 |
|
|
} |
131 |
greg |
1.2 |
|
132 |
|
|
|
133 |
schorsch |
2.9 |
void |
134 |
|
|
freelamps(void) /* free our lamps list */ |
135 |
greg |
1.2 |
{ |
136 |
|
|
register LAMP *lp1, *lp2; |
137 |
|
|
|
138 |
greg |
2.2 |
for (lp1 = lamps; lp1 != NULL; lp1 = lp2) { |
139 |
greg |
1.2 |
free(lp1->pattern); |
140 |
|
|
if (lp1->color != NULL) { |
141 |
|
|
for (lp2 = lp1->next; lp2 != NULL; lp2 = lp2->next) |
142 |
|
|
if (lp2->color == lp1->color) |
143 |
|
|
lp2->color = NULL; |
144 |
greg |
2.6 |
free((void *)lp1->color); |
145 |
greg |
1.2 |
} |
146 |
greg |
2.2 |
lp2 = lp1->next; |
147 |
greg |
2.6 |
free((void *)lp1); |
148 |
greg |
1.2 |
} |
149 |
|
|
lamps = NULL; |
150 |
|
|
} |