ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/lamps.c
Revision: 2.11
Committed: Sat Jun 7 05:12:49 2025 UTC (11 days, 2 hours ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.10: +2 -3 lines
Log Message:
fix: Missed new #include needed with reorganized headers

File Contents

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