1 |
– |
/* Copyright (c) 1996 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 |
– |
float xa[3][6], ya[3][6]; |
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 |
< |
double |
41 |
< |
rgblum(clr, scotopic) /* compute (scotopic) luminance of RGB color */ |
42 |
< |
COLOR clr; |
43 |
< |
int scotopic; |
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 + |
53 |
|
} |
54 |
|
|
55 |
|
|
56 |
< |
double |
57 |
< |
cielum(xyz, scotopic) /* compute (scotopic) luminance of CIE color */ |
58 |
< |
COLOR xyz; |
59 |
< |
int scotopic; |
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) * |
67 |
|
} |
68 |
|
|
69 |
|
|
70 |
< |
COLOR * |
71 |
< |
nextscan() /* read and condition next scanline */ |
70 |
> |
extern COLOR * |
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); |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
< |
COLOR * |
106 |
< |
firstscan() /* return first processed scanline */ |
105 |
> |
extern COLOR * |
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 |
> |
register 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] > cwhite[i]) |
153 |
+ |
gryc[i] = cwhite[i]; |
154 |
+ |
else if (gryc[i] < cblack[i]) |
155 |
+ |
gryc[i] = cblack[i]; |
156 |
+ |
return(bright(gryc)); |
157 |
+ |
} |
158 |
+ |
|
159 |
+ |
|
160 |
+ |
static void |
161 |
+ |
matscan( /* apply color matrix to scaline */ |
162 |
+ |
register COLOR *sl, |
163 |
+ |
int len, |
164 |
+ |
COLORMAT mat |
165 |
+ |
) |
166 |
+ |
{ |
167 |
+ |
double gryv; |
168 |
+ |
|
169 |
|
while (len--) { |
170 |
+ |
gryv = greypoint(sl[0]); |
171 |
|
colortrans(sl[0], mat, sl[0]); |
172 |
+ |
clipgamut(sl[0], gryv, CGAMUT, cblack, cwhite); |
173 |
|
sl++; |
174 |
|
} |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
< |
mbscan(sl, len, mb) /* apply macbethcal adj. to scaline */ |
179 |
< |
COLOR *sl; |
180 |
< |
int len; |
181 |
< |
register struct mbc *mb; |
178 |
> |
static void |
179 |
> |
mbscan( /* apply macbethcal adj. to scaline */ |
180 |
> |
COLOR *sl, |
181 |
> |
int len, |
182 |
> |
register struct mbc *mb |
183 |
> |
) |
184 |
|
{ |
185 |
|
double d; |
186 |
|
register int i, j; |
187 |
|
|
188 |
|
while (len--) { |
189 |
+ |
d = greypoint(sl[0]); |
190 |
+ |
colortrans(sl[0], mb->cmat, sl[0]); |
191 |
+ |
clipgamut(sl[0], d, CGAMUT, mb->cmin, mb->cmax); |
192 |
|
for (i = 0; i < 3; i++) { |
193 |
|
d = colval(sl[0],i); |
194 |
|
for (j = 0; j < 4 && mb->xa[i][j+1] <= d; j++) |
197 |
|
(d - mb->xa[i][j])*mb->ya[i][j+1] ) / |
198 |
|
(mb->xa[i][j+1] - mb->xa[i][j]); |
199 |
|
} |
151 |
– |
colortrans(sl[0], mb->cmat, sl[0]); |
200 |
|
sl++; |
201 |
|
} |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
< |
getmbcalfile(fn, mb) /* load macbethcal file */ |
206 |
< |
char *fn; |
207 |
< |
register struct mbc *mb; |
205 |
> |
static void |
206 |
> |
cwscan( /* apply color space warp to scaline */ |
207 |
> |
COLOR *sl, |
208 |
> |
int len, |
209 |
> |
WARP3D *wp |
210 |
> |
) |
211 |
|
{ |
212 |
< |
extern char *fgets(); |
212 |
> |
int rval; |
213 |
> |
double gryv; |
214 |
> |
|
215 |
> |
while (len--) { |
216 |
> |
gryv = greypoint(sl[0]); |
217 |
> |
rval = warp3d(sl[0], sl[0], wp); |
218 |
> |
if (rval & W3ERROR) |
219 |
> |
syserror("warp3d"); |
220 |
> |
if (rval & W3BADMAP) { |
221 |
> |
fprintf(stderr, "%s: %s: bad color space map\n", |
222 |
> |
progname, cwarpfile); |
223 |
> |
exit(1); |
224 |
> |
} |
225 |
> |
clipgamut(sl[0], gryv, CGAMUT, cblack, cwhite); |
226 |
> |
sl++; |
227 |
> |
} |
228 |
> |
} |
229 |
> |
|
230 |
> |
|
231 |
> |
static void |
232 |
> |
getmbcalfile( /* load macbethcal file */ |
233 |
> |
char *fn, |
234 |
> |
register struct mbc *mb |
235 |
> |
) |
236 |
> |
{ |
237 |
|
char buf[128]; |
238 |
|
FILE *fp; |
239 |
|
int inpflags = 0; |
240 |
+ |
register int i; |
241 |
|
|
242 |
|
if ((fp = fopen(fn, "r")) == NULL) |
243 |
|
syserror(fn); |
316 |
|
exit(1); |
317 |
|
} |
318 |
|
fclose(fp); |
319 |
+ |
/* compute gamut */ |
320 |
+ |
for (i = 0; i < 3; i++) { |
321 |
+ |
colval(mb->cmin,i) = mb->xa[i][0] - |
322 |
+ |
mb->ya[i][0] * |
323 |
+ |
(mb->xa[i][1]-mb->xa[i][0]) / |
324 |
+ |
(mb->ya[i][1]-mb->ya[i][0]); |
325 |
+ |
colval(mb->cmax,i) = mb->xa[i][4] + |
326 |
+ |
(1.-mb->ya[i][4]) * |
327 |
+ |
(mb->xa[i][5] - mb->xa[i][4]) / |
328 |
+ |
(mb->ya[i][5] - mb->ya[i][4]); |
329 |
+ |
} |
330 |
|
} |