ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/lamps.c
Revision: 2.9
Committed: Sun Jun 8 12:03:09 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2, rad3R7P1, rad3R6, rad3R6P1, rad3R8
Changes since 2.8: +10 -6 lines
Log Message:
Reduced compile warnings/errors on Windows.

File Contents

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