1 |
greg |
3.1 |
#ifndef lint |
2 |
greg |
3.18 |
static const char RCSid[] = "$Id: pcond2.c,v 3.17 2005/09/08 22:29:47 greg Exp $"; |
3 |
greg |
3.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Input and output conditioning routines for pcond. |
6 |
greg |
3.10 |
* Added white-balance adjustment 10/01 (GW). |
7 |
greg |
3.1 |
*/ |
8 |
|
|
|
9 |
|
|
#include "pcond.h" |
10 |
greg |
3.8 |
#include "warp3d.h" |
11 |
greg |
3.1 |
|
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 |
greg |
3.8 |
char *cwarpfile = NULL; /* color space warping file */ |
22 |
greg |
3.1 |
|
23 |
greg |
3.3 |
static struct mbc { |
24 |
greg |
3.7 |
COLORMAT cmat; |
25 |
greg |
3.3 |
float xa[3][6], ya[3][6]; |
26 |
greg |
3.7 |
COLOR cmin, cmax; |
27 |
greg |
3.3 |
} mbcond; /* macbethcal conditioning struct */ |
28 |
greg |
3.1 |
|
29 |
greg |
3.8 |
static WARP3D *cwarp; /* color warping structure */ |
30 |
|
|
|
31 |
greg |
3.1 |
static COLOR *scanbuf; /* scanline processing buffer */ |
32 |
|
|
static int nread; /* number of scanlines processed */ |
33 |
|
|
|
34 |
schorsch |
3.12 |
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 |
greg |
3.1 |
|
40 |
schorsch |
3.12 |
|
41 |
greg |
3.18 |
double |
42 |
schorsch |
3.12 |
rgblum( /* compute (scotopic) luminance of RGB color */ |
43 |
|
|
COLOR clr, |
44 |
|
|
int scotopic |
45 |
|
|
) |
46 |
greg |
3.1 |
{ |
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 |
greg |
3.18 |
double |
57 |
schorsch |
3.12 |
cielum( /* compute (scotopic) luminance of CIE color */ |
58 |
|
|
COLOR xyz, |
59 |
|
|
int scotopic |
60 |
|
|
) |
61 |
greg |
3.1 |
{ |
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 |
greg |
3.18 |
COLOR * |
71 |
schorsch |
3.12 |
nextscan(void) /* read and condition next scanline */ |
72 |
greg |
3.1 |
{ |
73 |
|
|
if (nread >= numscans(&inpres)) { |
74 |
greg |
3.8 |
if (cwarpfile != NULL) |
75 |
|
|
free3dw(cwarp); |
76 |
greg |
3.10 |
free((void *)scanbuf); |
77 |
greg |
3.4 |
return(scanbuf = NULL); |
78 |
greg |
3.1 |
} |
79 |
greg |
3.2 |
if (what2do&DO_ACUITY) |
80 |
|
|
acuscan(scanbuf, nread); |
81 |
|
|
else if (freadscan(scanbuf, scanlen(&inpres), infp) < 0) { |
82 |
greg |
3.1 |
fprintf(stderr, "%s: %s: scanline read error\n", |
83 |
|
|
progname, infn); |
84 |
|
|
exit(1); |
85 |
|
|
} |
86 |
|
|
if (what2do&DO_VEIL) /* add veiling */ |
87 |
greg |
3.2 |
addveil(scanbuf, nread); |
88 |
greg |
3.1 |
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 |
greg |
3.8 |
else if (cwarpfile != NULL) /* device color space warp */ |
97 |
|
|
cwscan(scanbuf, scanlen(&inpres), cwarp); |
98 |
schorsch |
3.11 |
else if ((lumf == cielum) | (inprims != outprims)) |
99 |
greg |
3.1 |
matscan(scanbuf, scanlen(&inpres), mbcond.cmat); |
100 |
greg |
3.2 |
nread++; |
101 |
greg |
3.1 |
return(scanbuf); |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
|
105 |
greg |
3.18 |
COLOR * |
106 |
schorsch |
3.12 |
firstscan(void) /* return first processed scanline */ |
107 |
greg |
3.1 |
{ |
108 |
|
|
if (mbcalfile != NULL) /* load macbethcal file */ |
109 |
|
|
getmbcalfile(mbcalfile, &mbcond); |
110 |
greg |
3.8 |
else if (cwarpfile != NULL) { |
111 |
|
|
if ((cwarp = load3dw(cwarpfile, NULL)) == NULL) |
112 |
|
|
syserror(cwarpfile); |
113 |
|
|
} else |
114 |
greg |
3.1 |
if (lumf == rgblum) |
115 |
greg |
3.10 |
comprgb2rgbWBmat(mbcond.cmat, inprims, outprims); |
116 |
greg |
3.1 |
else |
117 |
greg |
3.10 |
compxyz2rgbWBmat(mbcond.cmat, outprims); |
118 |
greg |
3.14 |
if (what2do&DO_ACUITY) |
119 |
|
|
initacuity(); |
120 |
greg |
3.1 |
scanbuf = (COLOR *)malloc(scanlen(&inpres)*sizeof(COLOR)); |
121 |
|
|
if (scanbuf == NULL) |
122 |
|
|
syserror("malloc"); |
123 |
|
|
nread = 0; |
124 |
|
|
return(nextscan()); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
|
128 |
schorsch |
3.12 |
static void |
129 |
|
|
sfscan( /* apply scalefactor to scanline */ |
130 |
greg |
3.18 |
COLOR *sl, |
131 |
schorsch |
3.12 |
int len, |
132 |
|
|
double sf |
133 |
|
|
) |
134 |
greg |
3.1 |
{ |
135 |
|
|
while (len--) { |
136 |
|
|
scalecolor(sl[0], sf); |
137 |
|
|
sl++; |
138 |
|
|
} |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
|
142 |
greg |
3.15 |
static double |
143 |
|
|
greypoint( /* compute gamut mapping grey target */ |
144 |
|
|
COLOR col |
145 |
|
|
) |
146 |
|
|
{ |
147 |
|
|
COLOR gryc; |
148 |
|
|
int i; |
149 |
|
|
/* improves saturated color rendering */ |
150 |
|
|
copycolor(gryc, col); |
151 |
|
|
for (i = 3; i--; ) |
152 |
greg |
3.17 |
if (gryc[i] > 1) |
153 |
|
|
gryc[i] = 1; |
154 |
|
|
else if (gryc[i] < 0) |
155 |
|
|
gryc[i] = 0; |
156 |
|
|
return(bright(gryc)); |
157 |
greg |
3.15 |
} |
158 |
|
|
|
159 |
|
|
|
160 |
schorsch |
3.12 |
static void |
161 |
|
|
matscan( /* apply color matrix to scaline */ |
162 |
greg |
3.18 |
COLOR *sl, |
163 |
schorsch |
3.12 |
int len, |
164 |
|
|
COLORMAT mat |
165 |
|
|
) |
166 |
greg |
3.1 |
{ |
167 |
|
|
while (len--) { |
168 |
|
|
colortrans(sl[0], mat, sl[0]); |
169 |
greg |
3.17 |
clipgamut(sl[0], greypoint(sl[0]), CGAMUT, cblack, cwhite); |
170 |
greg |
3.1 |
sl++; |
171 |
|
|
} |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
|
175 |
schorsch |
3.12 |
static void |
176 |
|
|
mbscan( /* apply macbethcal adj. to scaline */ |
177 |
|
|
COLOR *sl, |
178 |
|
|
int len, |
179 |
greg |
3.18 |
struct mbc *mb |
180 |
schorsch |
3.12 |
) |
181 |
greg |
3.1 |
{ |
182 |
|
|
double d; |
183 |
greg |
3.18 |
int i, j; |
184 |
greg |
3.1 |
|
185 |
|
|
while (len--) { |
186 |
greg |
3.6 |
colortrans(sl[0], mb->cmat, sl[0]); |
187 |
greg |
3.17 |
clipgamut(sl[0], greypoint(sl[0]), CGAMUT, mb->cmin, mb->cmax); |
188 |
greg |
3.1 |
for (i = 0; i < 3; i++) { |
189 |
|
|
d = colval(sl[0],i); |
190 |
|
|
for (j = 0; j < 4 && mb->xa[i][j+1] <= d; j++) |
191 |
|
|
; |
192 |
|
|
colval(sl[0],i) = ( (mb->xa[i][j+1] - d)*mb->ya[i][j] + |
193 |
|
|
(d - mb->xa[i][j])*mb->ya[i][j+1] ) / |
194 |
|
|
(mb->xa[i][j+1] - mb->xa[i][j]); |
195 |
|
|
} |
196 |
greg |
3.8 |
sl++; |
197 |
|
|
} |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
|
201 |
schorsch |
3.12 |
static void |
202 |
|
|
cwscan( /* apply color space warp to scaline */ |
203 |
|
|
COLOR *sl, |
204 |
|
|
int len, |
205 |
|
|
WARP3D *wp |
206 |
|
|
) |
207 |
greg |
3.8 |
{ |
208 |
|
|
int rval; |
209 |
|
|
|
210 |
|
|
while (len--) { |
211 |
|
|
rval = warp3d(sl[0], sl[0], wp); |
212 |
|
|
if (rval & W3ERROR) |
213 |
|
|
syserror("warp3d"); |
214 |
|
|
if (rval & W3BADMAP) { |
215 |
|
|
fprintf(stderr, "%s: %s: bad color space map\n", |
216 |
|
|
progname, cwarpfile); |
217 |
|
|
exit(1); |
218 |
|
|
} |
219 |
greg |
3.17 |
clipgamut(sl[0], greypoint(sl[0]), CGAMUT, cblack, cwhite); |
220 |
greg |
3.1 |
sl++; |
221 |
|
|
} |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
|
225 |
schorsch |
3.12 |
static void |
226 |
|
|
getmbcalfile( /* load macbethcal file */ |
227 |
|
|
char *fn, |
228 |
greg |
3.18 |
struct mbc *mb |
229 |
schorsch |
3.12 |
) |
230 |
greg |
3.1 |
{ |
231 |
|
|
char buf[128]; |
232 |
|
|
FILE *fp; |
233 |
|
|
int inpflags = 0; |
234 |
greg |
3.18 |
int i; |
235 |
greg |
3.1 |
|
236 |
|
|
if ((fp = fopen(fn, "r")) == NULL) |
237 |
|
|
syserror(fn); |
238 |
|
|
while (fgets(buf, sizeof(buf), fp) != NULL) { |
239 |
|
|
if (!(inpflags & 01) && |
240 |
|
|
sscanf(buf, |
241 |
|
|
"rxa(i) : select(i,%f,%f,%f,%f,%f,%f)", |
242 |
|
|
&mb->xa[0][0], &mb->xa[0][1], |
243 |
|
|
&mb->xa[0][2], &mb->xa[0][3], |
244 |
|
|
&mb->xa[0][4], &mb->xa[0][5] |
245 |
|
|
) == 6) |
246 |
|
|
inpflags |= 01; |
247 |
|
|
else if (!(inpflags & 02) && |
248 |
|
|
sscanf(buf, |
249 |
|
|
"rya(i) : select(i,%f,%f,%f,%f,%f,%f)", |
250 |
|
|
&mb->ya[0][0], &mb->ya[0][1], |
251 |
|
|
&mb->ya[0][2], &mb->ya[0][3], |
252 |
|
|
&mb->ya[0][4], &mb->ya[0][5] |
253 |
|
|
) == 6) |
254 |
|
|
inpflags |= 02; |
255 |
|
|
else if (!(inpflags & 04) && |
256 |
|
|
sscanf(buf, |
257 |
|
|
"gxa(i) : select(i,%f,%f,%f,%f,%f,%f)", |
258 |
|
|
&mb->xa[1][0], &mb->xa[1][1], |
259 |
|
|
&mb->xa[1][2], &mb->xa[1][3], |
260 |
|
|
&mb->xa[1][4], &mb->xa[1][5] |
261 |
|
|
) == 6) |
262 |
|
|
inpflags |= 04; |
263 |
|
|
else if (!(inpflags & 010) && |
264 |
|
|
sscanf(buf, |
265 |
|
|
"gya(i) : select(i,%f,%f,%f,%f,%f,%f)", |
266 |
|
|
&mb->ya[1][0], &mb->ya[1][1], |
267 |
|
|
&mb->ya[1][2], &mb->ya[1][3], |
268 |
|
|
&mb->ya[1][4], &mb->ya[1][5] |
269 |
|
|
) == 6) |
270 |
|
|
inpflags |= 010; |
271 |
|
|
else if (!(inpflags & 020) && |
272 |
|
|
sscanf(buf, |
273 |
|
|
"bxa(i) : select(i,%f,%f,%f,%f,%f,%f)", |
274 |
|
|
&mb->xa[2][0], &mb->xa[2][1], |
275 |
|
|
&mb->xa[2][2], &mb->xa[2][3], |
276 |
|
|
&mb->xa[2][4], &mb->xa[2][5] |
277 |
|
|
) == 6) |
278 |
|
|
inpflags |= 020; |
279 |
|
|
else if (!(inpflags & 040) && |
280 |
|
|
sscanf(buf, |
281 |
|
|
"bya(i) : select(i,%f,%f,%f,%f,%f,%f)", |
282 |
|
|
&mb->ya[2][0], &mb->ya[2][1], |
283 |
|
|
&mb->ya[2][2], &mb->ya[2][3], |
284 |
|
|
&mb->ya[2][4], &mb->ya[2][5] |
285 |
|
|
) == 6) |
286 |
|
|
inpflags |= 040; |
287 |
|
|
else if (!(inpflags & 0100) && |
288 |
|
|
sscanf(buf, |
289 |
gwlarson |
3.9 |
"ro = %f*rn + %f*gn + %f*bn", |
290 |
greg |
3.1 |
&mb->cmat[0][0], &mb->cmat[0][1], |
291 |
|
|
&mb->cmat[0][2]) == 3) |
292 |
|
|
inpflags |= 0100; |
293 |
|
|
else if (!(inpflags & 0200) && |
294 |
|
|
sscanf(buf, |
295 |
gwlarson |
3.9 |
"go = %f*rn + %f*gn + %f*bn", |
296 |
greg |
3.1 |
&mb->cmat[1][0], &mb->cmat[1][1], |
297 |
|
|
&mb->cmat[1][2]) == 3) |
298 |
|
|
inpflags |= 0200; |
299 |
|
|
else if (!(inpflags & 0400) && |
300 |
|
|
sscanf(buf, |
301 |
gwlarson |
3.9 |
"bo = %f*rn + %f*gn + %f*bn", |
302 |
greg |
3.1 |
&mb->cmat[2][0], &mb->cmat[2][1], |
303 |
|
|
&mb->cmat[2][2]) == 3) |
304 |
|
|
inpflags |= 0400; |
305 |
|
|
} |
306 |
|
|
if (inpflags != 0777) { |
307 |
|
|
fprintf(stderr, |
308 |
|
|
"%s: cannot grok macbethcal file \"%s\" (inpflags==0%o)\n", |
309 |
|
|
progname, fn, inpflags); |
310 |
|
|
exit(1); |
311 |
|
|
} |
312 |
|
|
fclose(fp); |
313 |
greg |
3.7 |
/* compute gamut */ |
314 |
|
|
for (i = 0; i < 3; i++) { |
315 |
|
|
colval(mb->cmin,i) = mb->xa[i][0] - |
316 |
|
|
mb->ya[i][0] * |
317 |
|
|
(mb->xa[i][1]-mb->xa[i][0]) / |
318 |
|
|
(mb->ya[i][1]-mb->ya[i][0]); |
319 |
|
|
colval(mb->cmax,i) = mb->xa[i][4] + |
320 |
|
|
(1.-mb->ya[i][4]) * |
321 |
|
|
(mb->xa[i][5] - mb->xa[i][4]) / |
322 |
|
|
(mb->ya[i][5] - mb->ya[i][4]); |
323 |
|
|
} |
324 |
greg |
3.1 |
} |