ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/lamps.c
Revision: 2.5
Committed: Mon Oct 16 09:21:46 1995 UTC (28 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +1 -0 lines
Log Message:
added dependency on color.h to lamps.o for cie_rgb() macro

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1990 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * Load lamp data.
9     */
10    
11     #include <stdio.h>
12     #include <ctype.h>
13 greg 2.5 #include "color.h"
14 greg 1.1
15     extern char *eindex(), *expsave(), *malloc();
16 greg 2.4 extern FILE *frlibopen();
17 greg 1.1
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 greg 2.4 if ((fp = frlibopen(file)) == NULL)
53 greg 1.1 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 greg 2.3 if ((lp->color=(float *)malloc(6*sizeof(float)))==NULL)
97 greg 1.1 goto memerr;
98 greg 2.3 if (sscanf(cp1, "%f %f %f", &lp->color[3], &
99     lp->color[4], &lp->color[5]) != 3) {
100 greg 1.1 cp1 = "bad lamp data";
101     goto fmterr;
102     }
103     /* convert xyY to XYZ */
104 greg 2.3 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 greg 1.1 /* 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 greg 1.3 return(lastp != NULL);
119 greg 1.1 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 greg 1.2
128    
129     freelamps() /* free our lamps list */
130     {
131     register LAMP *lp1, *lp2;
132    
133 greg 2.2 for (lp1 = lamps; lp1 != NULL; lp1 = lp2) {
134 greg 1.2 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((char *)lp1->color);
140     }
141 greg 2.2 lp2 = lp1->next;
142 greg 1.2 free((char *)lp1);
143     }
144     lamps = NULL;
145     }