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