1 |
– |
/* Copyright (c) 1997 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
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 */ |
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; |
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 |
|
double |
42 |
< |
rgblum(clr, scotopic) /* compute (scotopic) luminance of RGB color */ |
43 |
< |
COLOR clr; |
44 |
< |
int scotopic; |
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 + |
54 |
|
|
55 |
|
|
56 |
|
double |
57 |
< |
cielum(xyz, scotopic) /* compute (scotopic) luminance of CIE color */ |
58 |
< |
COLOR xyz; |
59 |
< |
int scotopic; |
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) * |
68 |
|
|
69 |
|
|
70 |
|
COLOR * |
71 |
< |
nextscan() /* read and condition next scanline */ |
71 |
> |
nextscan(void) /* read and condition next scanline */ |
72 |
|
{ |
73 |
|
if (nread >= numscans(&inpres)) { |
74 |
< |
free((char *)scanbuf); |
74 |
> |
if (cwarpfile != NULL) |
75 |
> |
free3dw(cwarp); |
76 |
> |
free((void *)scanbuf); |
77 |
|
return(scanbuf = NULL); |
78 |
|
} |
79 |
|
if (what2do&DO_ACUITY) |
93 |
|
mapscan(scanbuf, scanlen(&inpres)); |
94 |
|
if (mbcalfile != NULL) /* device color correction */ |
95 |
|
mbscan(scanbuf, scanlen(&inpres), &mbcond); |
96 |
< |
else if (lumf == cielum | inprims != outprims) |
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); |
103 |
|
|
104 |
|
|
105 |
|
COLOR * |
106 |
< |
firstscan() /* return first processed scanline */ |
106 |
> |
firstscan(void) /* return first processed scanline */ |
107 |
|
{ |
108 |
|
if (mbcalfile != NULL) /* load macbethcal file */ |
109 |
|
getmbcalfile(mbcalfile, &mbcond); |
110 |
< |
else |
110 |
> |
else if (cwarpfile != NULL) { |
111 |
> |
if ((cwarp = load3dw(cwarpfile, NULL)) == NULL) |
112 |
> |
syserror(cwarpfile); |
113 |
> |
} else |
114 |
|
if (lumf == rgblum) |
115 |
< |
comprgb2rgbmat(mbcond.cmat, inprims, outprims); |
115 |
> |
comprgb2rgbWBmat(mbcond.cmat, inprims, outprims); |
116 |
|
else |
117 |
< |
compxyz2rgbmat(mbcond.cmat, outprims); |
117 |
> |
compxyz2rgbWBmat(mbcond.cmat, outprims); |
118 |
|
if (what2do&DO_ACUITY) |
119 |
|
initacuity(); |
120 |
|
scanbuf = (COLOR *)malloc(scanlen(&inpres)*sizeof(COLOR)); |
125 |
|
} |
126 |
|
|
127 |
|
|
128 |
< |
sfscan(sl, len, sf) /* apply scalefactor to scanline */ |
129 |
< |
register COLOR *sl; |
130 |
< |
int len; |
131 |
< |
double sf; |
128 |
> |
static void |
129 |
> |
sfscan( /* apply scalefactor to scanline */ |
130 |
> |
COLOR *sl, |
131 |
> |
int len, |
132 |
> |
double sf |
133 |
> |
) |
134 |
|
{ |
135 |
|
while (len--) { |
136 |
|
scalecolor(sl[0], sf); |
139 |
|
} |
140 |
|
|
141 |
|
|
142 |
< |
matscan(sl, len, mat) /* apply color matrix to scaline */ |
143 |
< |
register COLOR *sl; |
144 |
< |
int len; |
145 |
< |
COLORMAT mat; |
142 |
> |
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 |
+ |
if (gryc[i] > 1) |
153 |
+ |
gryc[i] = 1; |
154 |
+ |
else if (gryc[i] < 0) |
155 |
+ |
gryc[i] = 0; |
156 |
+ |
return(bright(gryc)); |
157 |
+ |
} |
158 |
+ |
|
159 |
+ |
|
160 |
+ |
static void |
161 |
+ |
matscan( /* apply color matrix to scaline */ |
162 |
+ |
COLOR *sl, |
163 |
+ |
int len, |
164 |
+ |
COLORMAT mat |
165 |
+ |
) |
166 |
+ |
{ |
167 |
|
while (len--) { |
168 |
|
colortrans(sl[0], mat, sl[0]); |
169 |
< |
clipgamut(sl[0], bright(sl[0]), CGAMUT, cblack, cwhite); |
169 |
> |
clipgamut(sl[0], greypoint(sl[0]), CGAMUT, cblack, cwhite); |
170 |
|
sl++; |
171 |
|
} |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
< |
mbscan(sl, len, mb) /* apply macbethcal adj. to scaline */ |
176 |
< |
COLOR *sl; |
177 |
< |
int len; |
178 |
< |
register struct mbc *mb; |
175 |
> |
static void |
176 |
> |
mbscan( /* apply macbethcal adj. to scaline */ |
177 |
> |
COLOR *sl, |
178 |
> |
int len, |
179 |
> |
struct mbc *mb |
180 |
> |
) |
181 |
|
{ |
182 |
|
double d; |
183 |
< |
register int i, j; |
183 |
> |
int i, j; |
184 |
|
|
185 |
|
while (len--) { |
186 |
|
colortrans(sl[0], mb->cmat, sl[0]); |
187 |
< |
clipgamut(sl[0], bright(sl[0]), CGAMUT, mb->cmin, mb->cmax); |
187 |
> |
clipgamut(sl[0], greypoint(sl[0]), CGAMUT, mb->cmin, mb->cmax); |
188 |
|
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++) |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
< |
getmbcalfile(fn, mb) /* load macbethcal file */ |
202 |
< |
char *fn; |
203 |
< |
register struct mbc *mb; |
201 |
> |
static void |
202 |
> |
cwscan( /* apply color space warp to scaline */ |
203 |
> |
COLOR *sl, |
204 |
> |
int len, |
205 |
> |
WARP3D *wp |
206 |
> |
) |
207 |
|
{ |
208 |
< |
extern char *fgets(); |
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 |
> |
clipgamut(sl[0], greypoint(sl[0]), CGAMUT, cblack, cwhite); |
220 |
> |
sl++; |
221 |
> |
} |
222 |
> |
} |
223 |
> |
|
224 |
> |
|
225 |
> |
static void |
226 |
> |
getmbcalfile( /* load macbethcal file */ |
227 |
> |
char *fn, |
228 |
> |
struct mbc *mb |
229 |
> |
) |
230 |
> |
{ |
231 |
|
char buf[128]; |
232 |
|
FILE *fp; |
233 |
|
int inpflags = 0; |
234 |
< |
register int i; |
234 |
> |
int i; |
235 |
|
|
236 |
|
if ((fp = fopen(fn, "r")) == NULL) |
237 |
|
syserror(fn); |
286 |
|
inpflags |= 040; |
287 |
|
else if (!(inpflags & 0100) && |
288 |
|
sscanf(buf, |
289 |
< |
"r = %f*r1 + %f*g1 + %f*b1", |
289 |
> |
"ro = %f*rn + %f*gn + %f*bn", |
290 |
|
&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 |
< |
"g = %f*r1 + %f*g1 + %f*b1", |
295 |
> |
"go = %f*rn + %f*gn + %f*bn", |
296 |
|
&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 |
< |
"b = %f*r1 + %f*g1 + %f*b1", |
301 |
> |
"bo = %f*rn + %f*gn + %f*bn", |
302 |
|
&mb->cmat[2][0], &mb->cmat[2][1], |
303 |
|
&mb->cmat[2][2]) == 3) |
304 |
|
inpflags |= 0400; |