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