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 |
free((char *)scanbuf); |
64 |
return(scanbuf = NULL); |
65 |
} |
66 |
if (what2do&DO_ACUITY) |
67 |
acuscan(scanbuf, nread); |
68 |
else if (freadscan(scanbuf, scanlen(&inpres), infp) < 0) { |
69 |
fprintf(stderr, "%s: %s: scanline read error\n", |
70 |
progname, infn); |
71 |
exit(1); |
72 |
} |
73 |
if (what2do&DO_VEIL) /* add veiling */ |
74 |
addveil(scanbuf, nread); |
75 |
if (what2do&DO_COLOR) /* scotopic color loss */ |
76 |
scotscan(scanbuf, scanlen(&inpres)); |
77 |
if (what2do&DO_LINEAR) /* map luminances */ |
78 |
sfscan(scanbuf, scanlen(&inpres), scalef); |
79 |
else |
80 |
mapscan(scanbuf, scanlen(&inpres)); |
81 |
if (mbcalfile != NULL) /* device color correction */ |
82 |
mbscan(scanbuf, scanlen(&inpres), &mbcond); |
83 |
else if (lumf == cielum | inprims != outprims) |
84 |
matscan(scanbuf, scanlen(&inpres), mbcond.cmat); |
85 |
nread++; |
86 |
return(scanbuf); |
87 |
} |
88 |
|
89 |
|
90 |
COLOR * |
91 |
firstscan() /* return first processed scanline */ |
92 |
{ |
93 |
if (mbcalfile != NULL) /* load macbethcal file */ |
94 |
getmbcalfile(mbcalfile, &mbcond); |
95 |
else |
96 |
if (lumf == rgblum) |
97 |
comprgb2rgbmat(mbcond.cmat, inprims, outprims); |
98 |
else |
99 |
compxyz2rgbmat(mbcond.cmat, outprims); |
100 |
if (what2do&DO_ACUITY) |
101 |
initacuity(); |
102 |
scanbuf = (COLOR *)malloc(scanlen(&inpres)*sizeof(COLOR)); |
103 |
if (scanbuf == NULL) |
104 |
syserror("malloc"); |
105 |
nread = 0; |
106 |
return(nextscan()); |
107 |
} |
108 |
|
109 |
|
110 |
sfscan(sl, len, sf) /* apply scalefactor to scanline */ |
111 |
register COLOR *sl; |
112 |
int len; |
113 |
double sf; |
114 |
{ |
115 |
while (len--) { |
116 |
scalecolor(sl[0], sf); |
117 |
sl++; |
118 |
} |
119 |
} |
120 |
|
121 |
|
122 |
matscan(sl, len, mat) /* apply color matrix to scaline */ |
123 |
register COLOR *sl; |
124 |
int len; |
125 |
COLORMAT mat; |
126 |
{ |
127 |
while (len--) { |
128 |
colortrans(sl[0], mat, sl[0]); |
129 |
sl++; |
130 |
} |
131 |
} |
132 |
|
133 |
|
134 |
mbscan(sl, len, mb) /* apply macbethcal adj. to scaline */ |
135 |
COLOR *sl; |
136 |
int len; |
137 |
register struct mbc *mb; |
138 |
{ |
139 |
double d; |
140 |
register int i, j; |
141 |
|
142 |
while (len--) { |
143 |
for (i = 0; i < 3; i++) { |
144 |
d = colval(sl[0],i); |
145 |
for (j = 0; j < 4 && mb->xa[i][j+1] <= d; j++) |
146 |
; |
147 |
colval(sl[0],i) = ( (mb->xa[i][j+1] - d)*mb->ya[i][j] + |
148 |
(d - mb->xa[i][j])*mb->ya[i][j+1] ) / |
149 |
(mb->xa[i][j+1] - mb->xa[i][j]); |
150 |
} |
151 |
colortrans(sl[0], mb->cmat, sl[0]); |
152 |
sl++; |
153 |
} |
154 |
} |
155 |
|
156 |
|
157 |
getmbcalfile(fn, mb) /* load macbethcal file */ |
158 |
char *fn; |
159 |
register struct mbc *mb; |
160 |
{ |
161 |
extern char *fgets(); |
162 |
char buf[128]; |
163 |
FILE *fp; |
164 |
int inpflags = 0; |
165 |
|
166 |
if ((fp = fopen(fn, "r")) == NULL) |
167 |
syserror(fn); |
168 |
while (fgets(buf, sizeof(buf), fp) != NULL) { |
169 |
if (!(inpflags & 01) && |
170 |
sscanf(buf, |
171 |
"rxa(i) : select(i,%f,%f,%f,%f,%f,%f)", |
172 |
&mb->xa[0][0], &mb->xa[0][1], |
173 |
&mb->xa[0][2], &mb->xa[0][3], |
174 |
&mb->xa[0][4], &mb->xa[0][5] |
175 |
) == 6) |
176 |
inpflags |= 01; |
177 |
else if (!(inpflags & 02) && |
178 |
sscanf(buf, |
179 |
"rya(i) : select(i,%f,%f,%f,%f,%f,%f)", |
180 |
&mb->ya[0][0], &mb->ya[0][1], |
181 |
&mb->ya[0][2], &mb->ya[0][3], |
182 |
&mb->ya[0][4], &mb->ya[0][5] |
183 |
) == 6) |
184 |
inpflags |= 02; |
185 |
else if (!(inpflags & 04) && |
186 |
sscanf(buf, |
187 |
"gxa(i) : select(i,%f,%f,%f,%f,%f,%f)", |
188 |
&mb->xa[1][0], &mb->xa[1][1], |
189 |
&mb->xa[1][2], &mb->xa[1][3], |
190 |
&mb->xa[1][4], &mb->xa[1][5] |
191 |
) == 6) |
192 |
inpflags |= 04; |
193 |
else if (!(inpflags & 010) && |
194 |
sscanf(buf, |
195 |
"gya(i) : select(i,%f,%f,%f,%f,%f,%f)", |
196 |
&mb->ya[1][0], &mb->ya[1][1], |
197 |
&mb->ya[1][2], &mb->ya[1][3], |
198 |
&mb->ya[1][4], &mb->ya[1][5] |
199 |
) == 6) |
200 |
inpflags |= 010; |
201 |
else if (!(inpflags & 020) && |
202 |
sscanf(buf, |
203 |
"bxa(i) : select(i,%f,%f,%f,%f,%f,%f)", |
204 |
&mb->xa[2][0], &mb->xa[2][1], |
205 |
&mb->xa[2][2], &mb->xa[2][3], |
206 |
&mb->xa[2][4], &mb->xa[2][5] |
207 |
) == 6) |
208 |
inpflags |= 020; |
209 |
else if (!(inpflags & 040) && |
210 |
sscanf(buf, |
211 |
"bya(i) : select(i,%f,%f,%f,%f,%f,%f)", |
212 |
&mb->ya[2][0], &mb->ya[2][1], |
213 |
&mb->ya[2][2], &mb->ya[2][3], |
214 |
&mb->ya[2][4], &mb->ya[2][5] |
215 |
) == 6) |
216 |
inpflags |= 040; |
217 |
else if (!(inpflags & 0100) && |
218 |
sscanf(buf, |
219 |
"ro = %f*rn + %f*gn + %f*bn", |
220 |
&mb->cmat[0][0], &mb->cmat[0][1], |
221 |
&mb->cmat[0][2]) == 3) |
222 |
inpflags |= 0100; |
223 |
else if (!(inpflags & 0200) && |
224 |
sscanf(buf, |
225 |
"go = %f*rn + %f*gn + %f*bn", |
226 |
&mb->cmat[1][0], &mb->cmat[1][1], |
227 |
&mb->cmat[1][2]) == 3) |
228 |
inpflags |= 0200; |
229 |
else if (!(inpflags & 0400) && |
230 |
sscanf(buf, |
231 |
"bo = %f*rn + %f*gn + %f*bn", |
232 |
&mb->cmat[2][0], &mb->cmat[2][1], |
233 |
&mb->cmat[2][2]) == 3) |
234 |
inpflags |= 0400; |
235 |
} |
236 |
if (inpflags != 0777) { |
237 |
fprintf(stderr, |
238 |
"%s: cannot grok macbethcal file \"%s\" (inpflags==0%o)\n", |
239 |
progname, fn, inpflags); |
240 |
exit(1); |
241 |
} |
242 |
fclose(fp); |
243 |
} |