1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: pcond2.c,v 3.12 2004/03/28 20:33:14 schorsch Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Input and output conditioning routines for pcond. |
6 |
* Added white-balance adjustment 10/01 (GW). |
7 |
*/ |
8 |
|
9 |
#include "pcond.h" |
10 |
#include "warp3d.h" |
11 |
|
12 |
|
13 |
RGBPRIMP inprims = stdprims; /* input primaries */ |
14 |
COLORMAT inrgb2xyz; /* convert input RGB to XYZ */ |
15 |
RGBPRIMP outprims = stdprims; /* output primaries */ |
16 |
|
17 |
double (*lumf)() = rgblum; /* input luminance function */ |
18 |
double inpexp = 1.0; /* input exposure value */ |
19 |
|
20 |
char *mbcalfile = NULL; /* macbethcal mapping file */ |
21 |
char *cwarpfile = NULL; /* color space warping file */ |
22 |
|
23 |
static struct mbc { |
24 |
COLORMAT cmat; |
25 |
float xa[3][6], ya[3][6]; |
26 |
COLOR cmin, cmax; |
27 |
} mbcond; /* macbethcal conditioning struct */ |
28 |
|
29 |
static WARP3D *cwarp; /* color warping structure */ |
30 |
|
31 |
static COLOR *scanbuf; /* scanline processing buffer */ |
32 |
static int nread; /* number of scanlines processed */ |
33 |
|
34 |
static void sfscan(COLOR *sl, int len, double sf); |
35 |
static void matscan(COLOR *sl, int len, COLORMAT mat); |
36 |
static void mbscan(COLOR *sl, int len, struct mbc *mb); |
37 |
static void cwscan(COLOR *sl, int len, WARP3D *wp); |
38 |
static void getmbcalfile(char *fn, struct mbc *mb); |
39 |
|
40 |
|
41 |
extern double |
42 |
rgblum( /* compute (scotopic) luminance of RGB color */ |
43 |
COLOR clr, |
44 |
int scotopic |
45 |
) |
46 |
{ |
47 |
if (scotopic) /* approximate */ |
48 |
return( WHTSEFFICACY * (colval(clr,RED)*.062 + |
49 |
colval(clr,GRN)*.608 + colval(clr,BLU)*.330) ); |
50 |
return( WHTEFFICACY * (colval(clr,RED)*inrgb2xyz[1][0] + |
51 |
colval(clr,GRN)*inrgb2xyz[1][1] + |
52 |
colval(clr,BLU)*inrgb2xyz[1][2]) ); |
53 |
} |
54 |
|
55 |
|
56 |
extern double |
57 |
cielum( /* compute (scotopic) luminance of CIE color */ |
58 |
COLOR xyz, |
59 |
int scotopic |
60 |
) |
61 |
{ |
62 |
if (scotopic) /* approximate */ |
63 |
return(colval(xyz,CIEY) * |
64 |
(1.33*(1. + (colval(xyz,CIEY)+colval(xyz,CIEZ))/ |
65 |
colval(xyz,CIEX)) - 1.68)); |
66 |
return(colval(xyz,CIEY)); |
67 |
} |
68 |
|
69 |
|
70 |
extern COLOR * |
71 |
nextscan(void) /* read and condition next scanline */ |
72 |
{ |
73 |
if (nread >= numscans(&inpres)) { |
74 |
if (cwarpfile != NULL) |
75 |
free3dw(cwarp); |
76 |
free((void *)scanbuf); |
77 |
return(scanbuf = NULL); |
78 |
} |
79 |
if (what2do&DO_ACUITY) |
80 |
acuscan(scanbuf, nread); |
81 |
else if (freadscan(scanbuf, scanlen(&inpres), infp) < 0) { |
82 |
fprintf(stderr, "%s: %s: scanline read error\n", |
83 |
progname, infn); |
84 |
exit(1); |
85 |
} |
86 |
if (what2do&DO_VEIL) /* add veiling */ |
87 |
addveil(scanbuf, nread); |
88 |
if (what2do&DO_COLOR) /* scotopic color loss */ |
89 |
scotscan(scanbuf, scanlen(&inpres)); |
90 |
if (what2do&DO_LINEAR) /* map luminances */ |
91 |
sfscan(scanbuf, scanlen(&inpres), scalef); |
92 |
else |
93 |
mapscan(scanbuf, scanlen(&inpres)); |
94 |
if (mbcalfile != NULL) /* device color correction */ |
95 |
mbscan(scanbuf, scanlen(&inpres), &mbcond); |
96 |
else if (cwarpfile != NULL) /* device color space warp */ |
97 |
cwscan(scanbuf, scanlen(&inpres), cwarp); |
98 |
else if ((lumf == cielum) | (inprims != outprims)) |
99 |
matscan(scanbuf, scanlen(&inpres), mbcond.cmat); |
100 |
nread++; |
101 |
return(scanbuf); |
102 |
} |
103 |
|
104 |
|
105 |
extern COLOR * |
106 |
firstscan(void) /* return first processed scanline */ |
107 |
{ |
108 |
if (mbcalfile != NULL) /* load macbethcal file */ |
109 |
getmbcalfile(mbcalfile, &mbcond); |
110 |
else if (cwarpfile != NULL) { |
111 |
if ((cwarp = load3dw(cwarpfile, NULL)) == NULL) |
112 |
syserror(cwarpfile); |
113 |
} else |
114 |
if (lumf == rgblum) |
115 |
comprgb2rgbWBmat(mbcond.cmat, inprims, outprims); |
116 |
else |
117 |
compxyz2rgbWBmat(mbcond.cmat, outprims); |
118 |
if (what2do&DO_ACUITY && !initacuity()) { |
119 |
fprintf(stderr, "%s: warning - cannot initialize acuity pass\n", |
120 |
progname); |
121 |
what2do &= ~DO_ACUITY; |
122 |
} |
123 |
scanbuf = (COLOR *)malloc(scanlen(&inpres)*sizeof(COLOR)); |
124 |
if (scanbuf == NULL) |
125 |
syserror("malloc"); |
126 |
nread = 0; |
127 |
return(nextscan()); |
128 |
} |
129 |
|
130 |
|
131 |
static void |
132 |
sfscan( /* apply scalefactor to scanline */ |
133 |
register COLOR *sl, |
134 |
int len, |
135 |
double sf |
136 |
) |
137 |
{ |
138 |
while (len--) { |
139 |
scalecolor(sl[0], sf); |
140 |
sl++; |
141 |
} |
142 |
} |
143 |
|
144 |
|
145 |
static void |
146 |
matscan( /* apply color matrix to scaline */ |
147 |
register COLOR *sl, |
148 |
int len, |
149 |
COLORMAT mat |
150 |
) |
151 |
{ |
152 |
while (len--) { |
153 |
colortrans(sl[0], mat, sl[0]); |
154 |
clipgamut(sl[0], bright(sl[0]), CGAMUT, cblack, cwhite); |
155 |
sl++; |
156 |
} |
157 |
} |
158 |
|
159 |
|
160 |
static void |
161 |
mbscan( /* apply macbethcal adj. to scaline */ |
162 |
COLOR *sl, |
163 |
int len, |
164 |
register struct mbc *mb |
165 |
) |
166 |
{ |
167 |
double d; |
168 |
register int i, j; |
169 |
|
170 |
while (len--) { |
171 |
colortrans(sl[0], mb->cmat, sl[0]); |
172 |
clipgamut(sl[0], bright(sl[0]), CGAMUT, mb->cmin, mb->cmax); |
173 |
for (i = 0; i < 3; i++) { |
174 |
d = colval(sl[0],i); |
175 |
for (j = 0; j < 4 && mb->xa[i][j+1] <= d; j++) |
176 |
; |
177 |
colval(sl[0],i) = ( (mb->xa[i][j+1] - d)*mb->ya[i][j] + |
178 |
(d - mb->xa[i][j])*mb->ya[i][j+1] ) / |
179 |
(mb->xa[i][j+1] - mb->xa[i][j]); |
180 |
} |
181 |
sl++; |
182 |
} |
183 |
} |
184 |
|
185 |
|
186 |
static void |
187 |
cwscan( /* apply color space warp to scaline */ |
188 |
COLOR *sl, |
189 |
int len, |
190 |
WARP3D *wp |
191 |
) |
192 |
{ |
193 |
int rval; |
194 |
|
195 |
while (len--) { |
196 |
rval = warp3d(sl[0], sl[0], wp); |
197 |
if (rval & W3ERROR) |
198 |
syserror("warp3d"); |
199 |
if (rval & W3BADMAP) { |
200 |
fprintf(stderr, "%s: %s: bad color space map\n", |
201 |
progname, cwarpfile); |
202 |
exit(1); |
203 |
} |
204 |
clipgamut(sl[0], bright(sl[0]), CGAMUT, cblack, cwhite); |
205 |
sl++; |
206 |
} |
207 |
} |
208 |
|
209 |
|
210 |
static void |
211 |
getmbcalfile( /* load macbethcal file */ |
212 |
char *fn, |
213 |
register struct mbc *mb |
214 |
) |
215 |
{ |
216 |
char buf[128]; |
217 |
FILE *fp; |
218 |
int inpflags = 0; |
219 |
register int i; |
220 |
|
221 |
if ((fp = fopen(fn, "r")) == NULL) |
222 |
syserror(fn); |
223 |
while (fgets(buf, sizeof(buf), fp) != NULL) { |
224 |
if (!(inpflags & 01) && |
225 |
sscanf(buf, |
226 |
"rxa(i) : select(i,%f,%f,%f,%f,%f,%f)", |
227 |
&mb->xa[0][0], &mb->xa[0][1], |
228 |
&mb->xa[0][2], &mb->xa[0][3], |
229 |
&mb->xa[0][4], &mb->xa[0][5] |
230 |
) == 6) |
231 |
inpflags |= 01; |
232 |
else if (!(inpflags & 02) && |
233 |
sscanf(buf, |
234 |
"rya(i) : select(i,%f,%f,%f,%f,%f,%f)", |
235 |
&mb->ya[0][0], &mb->ya[0][1], |
236 |
&mb->ya[0][2], &mb->ya[0][3], |
237 |
&mb->ya[0][4], &mb->ya[0][5] |
238 |
) == 6) |
239 |
inpflags |= 02; |
240 |
else if (!(inpflags & 04) && |
241 |
sscanf(buf, |
242 |
"gxa(i) : select(i,%f,%f,%f,%f,%f,%f)", |
243 |
&mb->xa[1][0], &mb->xa[1][1], |
244 |
&mb->xa[1][2], &mb->xa[1][3], |
245 |
&mb->xa[1][4], &mb->xa[1][5] |
246 |
) == 6) |
247 |
inpflags |= 04; |
248 |
else if (!(inpflags & 010) && |
249 |
sscanf(buf, |
250 |
"gya(i) : select(i,%f,%f,%f,%f,%f,%f)", |
251 |
&mb->ya[1][0], &mb->ya[1][1], |
252 |
&mb->ya[1][2], &mb->ya[1][3], |
253 |
&mb->ya[1][4], &mb->ya[1][5] |
254 |
) == 6) |
255 |
inpflags |= 010; |
256 |
else if (!(inpflags & 020) && |
257 |
sscanf(buf, |
258 |
"bxa(i) : select(i,%f,%f,%f,%f,%f,%f)", |
259 |
&mb->xa[2][0], &mb->xa[2][1], |
260 |
&mb->xa[2][2], &mb->xa[2][3], |
261 |
&mb->xa[2][4], &mb->xa[2][5] |
262 |
) == 6) |
263 |
inpflags |= 020; |
264 |
else if (!(inpflags & 040) && |
265 |
sscanf(buf, |
266 |
"bya(i) : select(i,%f,%f,%f,%f,%f,%f)", |
267 |
&mb->ya[2][0], &mb->ya[2][1], |
268 |
&mb->ya[2][2], &mb->ya[2][3], |
269 |
&mb->ya[2][4], &mb->ya[2][5] |
270 |
) == 6) |
271 |
inpflags |= 040; |
272 |
else if (!(inpflags & 0100) && |
273 |
sscanf(buf, |
274 |
"ro = %f*rn + %f*gn + %f*bn", |
275 |
&mb->cmat[0][0], &mb->cmat[0][1], |
276 |
&mb->cmat[0][2]) == 3) |
277 |
inpflags |= 0100; |
278 |
else if (!(inpflags & 0200) && |
279 |
sscanf(buf, |
280 |
"go = %f*rn + %f*gn + %f*bn", |
281 |
&mb->cmat[1][0], &mb->cmat[1][1], |
282 |
&mb->cmat[1][2]) == 3) |
283 |
inpflags |= 0200; |
284 |
else if (!(inpflags & 0400) && |
285 |
sscanf(buf, |
286 |
"bo = %f*rn + %f*gn + %f*bn", |
287 |
&mb->cmat[2][0], &mb->cmat[2][1], |
288 |
&mb->cmat[2][2]) == 3) |
289 |
inpflags |= 0400; |
290 |
} |
291 |
if (inpflags != 0777) { |
292 |
fprintf(stderr, |
293 |
"%s: cannot grok macbethcal file \"%s\" (inpflags==0%o)\n", |
294 |
progname, fn, inpflags); |
295 |
exit(1); |
296 |
} |
297 |
fclose(fp); |
298 |
/* compute gamut */ |
299 |
for (i = 0; i < 3; i++) { |
300 |
colval(mb->cmin,i) = mb->xa[i][0] - |
301 |
mb->ya[i][0] * |
302 |
(mb->xa[i][1]-mb->xa[i][0]) / |
303 |
(mb->ya[i][1]-mb->ya[i][0]); |
304 |
colval(mb->cmax,i) = mb->xa[i][4] + |
305 |
(1.-mb->ya[i][4]) * |
306 |
(mb->xa[i][5] - mb->xa[i][4]) / |
307 |
(mb->ya[i][5] - mb->ya[i][4]); |
308 |
} |
309 |
} |