ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcond2.c
Revision: 3.3
Committed: Fri Oct 4 18:34:57 1996 UTC (27 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.2: +4 -1 lines
Log Message:
minor cleanup

File Contents

# Content
1 /* Copyright (c) 1996 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * Input and output conditioning routines for pcond.
9 */
10
11 #include "pcond.h"
12
13
14 RGBPRIMP inprims = stdprims; /* input primaries */
15 COLORMAT inrgb2xyz; /* convert input RGB to XYZ */
16 RGBPRIMP outprims = stdprims; /* output primaries */
17
18 double (*lumf)() = rgblum; /* input luminance function */
19 double inpexp = 1.0; /* input exposure value */
20
21 char *mbcalfile = NULL; /* macbethcal mapping file */
22
23 static struct mbc {
24 float xa[3][6], ya[3][6];
25 COLORMAT cmat;
26 } mbcond; /* macbethcal conditioning struct */
27
28 static COLOR *scanbuf; /* scanline processing buffer */
29 static int nread; /* number of scanlines processed */
30
31
32 double
33 rgblum(clr, scotopic) /* compute (scotopic) luminance of RGB color */
34 COLOR clr;
35 int scotopic;
36 {
37 if (scotopic) /* approximate */
38 return( WHTSEFFICACY * (colval(clr,RED)*.062 +
39 colval(clr,GRN)*.608 + colval(clr,BLU)*.330) );
40 return( WHTEFFICACY * (colval(clr,RED)*inrgb2xyz[1][0] +
41 colval(clr,GRN)*inrgb2xyz[1][1] +
42 colval(clr,BLU)*inrgb2xyz[1][2]) );
43 }
44
45
46 double
47 cielum(xyz, scotopic) /* compute (scotopic) luminance of CIE color */
48 COLOR xyz;
49 int scotopic;
50 {
51 if (scotopic) /* approximate */
52 return(colval(xyz,CIEY) *
53 (1.33*(1. + (colval(xyz,CIEY)+colval(xyz,CIEZ))/
54 colval(xyz,CIEX)) - 1.68));
55 return(colval(xyz,CIEY));
56 }
57
58
59 COLOR *
60 nextscan() /* read and condition next scanline */
61 {
62 if (nread >= numscans(&inpres)) {
63 #ifdef DEBUG
64 fputs("done\n", stderr);
65 #endif
66 return(NULL);
67 }
68 if (what2do&DO_ACUITY)
69 acuscan(scanbuf, nread);
70 else if (freadscan(scanbuf, scanlen(&inpres), infp) < 0) {
71 fprintf(stderr, "%s: %s: scanline read error\n",
72 progname, infn);
73 exit(1);
74 }
75 if (what2do&DO_VEIL) /* add veiling */
76 addveil(scanbuf, nread);
77 if (what2do&DO_COLOR) /* scotopic color loss */
78 scotscan(scanbuf, scanlen(&inpres));
79 if (what2do&DO_LINEAR) /* map luminances */
80 sfscan(scanbuf, scanlen(&inpres), scalef);
81 else
82 mapscan(scanbuf, scanlen(&inpres));
83 if (mbcalfile != NULL) /* device color correction */
84 mbscan(scanbuf, scanlen(&inpres), &mbcond);
85 else if (lumf == cielum | inprims != outprims)
86 matscan(scanbuf, scanlen(&inpres), mbcond.cmat);
87 nread++;
88 return(scanbuf);
89 }
90
91
92 COLOR *
93 firstscan() /* return first processed scanline */
94 {
95 if (mbcalfile != NULL) /* load macbethcal file */
96 getmbcalfile(mbcalfile, &mbcond);
97 else
98 if (lumf == rgblum)
99 comprgb2rgbmat(mbcond.cmat, inprims, outprims);
100 else
101 compxyz2rgbmat(mbcond.cmat, outprims);
102 if (what2do&DO_ACUITY) {
103 #ifdef DEBUG
104 fprintf(stderr, "%s: initializing acuity sampling...",
105 progname);
106 #endif
107 initacuity();
108 #ifdef DEBUG
109 fprintf(stderr, "done\n");
110 #endif
111 }
112 scanbuf = (COLOR *)malloc(scanlen(&inpres)*sizeof(COLOR));
113 if (scanbuf == NULL)
114 syserror("malloc");
115 nread = 0;
116 #ifdef DEBUG
117 fprintf(stderr, "%s: processing image...", progname);
118 #endif
119 return(nextscan());
120 }
121
122
123 sfscan(sl, len, sf) /* apply scalefactor to scanline */
124 register COLOR *sl;
125 int len;
126 double sf;
127 {
128 while (len--) {
129 scalecolor(sl[0], sf);
130 sl++;
131 }
132 }
133
134
135 matscan(sl, len, mat) /* apply color matrix to scaline */
136 register COLOR *sl;
137 int len;
138 COLORMAT mat;
139 {
140 while (len--) {
141 colortrans(sl[0], mat, sl[0]);
142 sl++;
143 }
144 }
145
146
147 mbscan(sl, len, mb) /* apply macbethcal adj. to scaline */
148 COLOR *sl;
149 int len;
150 register struct mbc *mb;
151 {
152 double d;
153 register int i, j;
154
155 while (len--) {
156 for (i = 0; i < 3; i++) {
157 d = colval(sl[0],i);
158 for (j = 0; j < 4 && mb->xa[i][j+1] <= d; j++)
159 ;
160 colval(sl[0],i) = ( (mb->xa[i][j+1] - d)*mb->ya[i][j] +
161 (d - mb->xa[i][j])*mb->ya[i][j+1] ) /
162 (mb->xa[i][j+1] - mb->xa[i][j]);
163 }
164 colortrans(sl[0], mb->cmat, sl[0]);
165 sl++;
166 }
167 }
168
169
170 getmbcalfile(fn, mb) /* load macbethcal file */
171 char *fn;
172 register struct mbc *mb;
173 {
174 extern char *fgets();
175 char buf[128];
176 FILE *fp;
177 int inpflags = 0;
178
179 if ((fp = fopen(fn, "r")) == NULL)
180 syserror(fn);
181 while (fgets(buf, sizeof(buf), fp) != NULL) {
182 if (!(inpflags & 01) &&
183 sscanf(buf,
184 "rxa(i) : select(i,%f,%f,%f,%f,%f,%f)",
185 &mb->xa[0][0], &mb->xa[0][1],
186 &mb->xa[0][2], &mb->xa[0][3],
187 &mb->xa[0][4], &mb->xa[0][5]
188 ) == 6)
189 inpflags |= 01;
190 else if (!(inpflags & 02) &&
191 sscanf(buf,
192 "rya(i) : select(i,%f,%f,%f,%f,%f,%f)",
193 &mb->ya[0][0], &mb->ya[0][1],
194 &mb->ya[0][2], &mb->ya[0][3],
195 &mb->ya[0][4], &mb->ya[0][5]
196 ) == 6)
197 inpflags |= 02;
198 else if (!(inpflags & 04) &&
199 sscanf(buf,
200 "gxa(i) : select(i,%f,%f,%f,%f,%f,%f)",
201 &mb->xa[1][0], &mb->xa[1][1],
202 &mb->xa[1][2], &mb->xa[1][3],
203 &mb->xa[1][4], &mb->xa[1][5]
204 ) == 6)
205 inpflags |= 04;
206 else if (!(inpflags & 010) &&
207 sscanf(buf,
208 "gya(i) : select(i,%f,%f,%f,%f,%f,%f)",
209 &mb->ya[1][0], &mb->ya[1][1],
210 &mb->ya[1][2], &mb->ya[1][3],
211 &mb->ya[1][4], &mb->ya[1][5]
212 ) == 6)
213 inpflags |= 010;
214 else if (!(inpflags & 020) &&
215 sscanf(buf,
216 "bxa(i) : select(i,%f,%f,%f,%f,%f,%f)",
217 &mb->xa[2][0], &mb->xa[2][1],
218 &mb->xa[2][2], &mb->xa[2][3],
219 &mb->xa[2][4], &mb->xa[2][5]
220 ) == 6)
221 inpflags |= 020;
222 else if (!(inpflags & 040) &&
223 sscanf(buf,
224 "bya(i) : select(i,%f,%f,%f,%f,%f,%f)",
225 &mb->ya[2][0], &mb->ya[2][1],
226 &mb->ya[2][2], &mb->ya[2][3],
227 &mb->ya[2][4], &mb->ya[2][5]
228 ) == 6)
229 inpflags |= 040;
230 else if (!(inpflags & 0100) &&
231 sscanf(buf,
232 "ro = %f*rn + %f*gn + %f*bn",
233 &mb->cmat[0][0], &mb->cmat[0][1],
234 &mb->cmat[0][2]) == 3)
235 inpflags |= 0100;
236 else if (!(inpflags & 0200) &&
237 sscanf(buf,
238 "go = %f*rn + %f*gn + %f*bn",
239 &mb->cmat[1][0], &mb->cmat[1][1],
240 &mb->cmat[1][2]) == 3)
241 inpflags |= 0200;
242 else if (!(inpflags & 0400) &&
243 sscanf(buf,
244 "bo = %f*rn + %f*gn + %f*bn",
245 &mb->cmat[2][0], &mb->cmat[2][1],
246 &mb->cmat[2][2]) == 3)
247 inpflags |= 0400;
248 }
249 if (inpflags != 0777) {
250 fprintf(stderr,
251 "%s: cannot grok macbethcal file \"%s\" (inpflags==0%o)\n",
252 progname, fn, inpflags);
253 exit(1);
254 }
255 fclose(fp);
256 }