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