ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/lamps.c
Revision: 2.7
Committed: Tue Feb 25 02:47:21 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.6: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

File Contents

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