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