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 |
+ |
|
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 |
< |
#ifdef DEBUG |
75 |
< |
fputs("done\n", stderr); |
76 |
< |
#endif |
77 |
< |
return(NULL); |
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) { |
81 |
> |
else if (fread2scan(scanbuf, scanlen(&inpres), infp, |
82 |
> |
NCSAMP, WLPART) < 0) { |
83 |
|
fprintf(stderr, "%s: %s: scanline read error\n", |
84 |
|
progname, infn); |
85 |
|
exit(1); |
94 |
|
mapscan(scanbuf, scanlen(&inpres)); |
95 |
|
if (mbcalfile != NULL) /* device color correction */ |
96 |
|
mbscan(scanbuf, scanlen(&inpres), &mbcond); |
97 |
< |
else if (lumf == cielum | inprims != outprims) |
97 |
> |
else if (cwarpfile != NULL) /* device color space warp */ |
98 |
> |
cwscan(scanbuf, scanlen(&inpres), cwarp); |
99 |
> |
else if ((lumf == cielum) | (inprims != outprims)) |
100 |
|
matscan(scanbuf, scanlen(&inpres), mbcond.cmat); |
101 |
|
nread++; |
102 |
|
return(scanbuf); |
104 |
|
|
105 |
|
|
106 |
|
COLOR * |
107 |
< |
firstscan() /* return first processed scanline */ |
107 |
> |
firstscan(void) /* return first processed scanline */ |
108 |
|
{ |
109 |
|
if (mbcalfile != NULL) /* load macbethcal file */ |
110 |
|
getmbcalfile(mbcalfile, &mbcond); |
111 |
< |
else |
111 |
> |
else if (cwarpfile != NULL) { |
112 |
> |
if ((cwarp = load3dw(cwarpfile, NULL)) == NULL) |
113 |
> |
syserror(cwarpfile); |
114 |
> |
} else |
115 |
|
if (lumf == rgblum) |
116 |
< |
comprgb2rgbmat(mbcond.cmat, inprims, outprims); |
116 |
> |
comprgb2rgbWBmat(mbcond.cmat, inprims, outprims); |
117 |
|
else |
118 |
< |
compxyz2rgbmat(mbcond.cmat, outprims); |
119 |
< |
if (what2do&DO_ACUITY) { |
103 |
< |
#ifdef DEBUG |
104 |
< |
fprintf(stderr, "%s: initializing acuity sampling...", |
105 |
< |
progname); |
106 |
< |
#endif |
118 |
> |
compxyz2rgbWBmat(mbcond.cmat, outprims); |
119 |
> |
if (what2do&DO_ACUITY) |
120 |
|
initacuity(); |
108 |
– |
#ifdef DEBUG |
109 |
– |
fprintf(stderr, "done\n"); |
110 |
– |
#endif |
111 |
– |
} |
121 |
|
scanbuf = (COLOR *)malloc(scanlen(&inpres)*sizeof(COLOR)); |
122 |
|
if (scanbuf == NULL) |
123 |
|
syserror("malloc"); |
124 |
|
nread = 0; |
116 |
– |
#ifdef DEBUG |
117 |
– |
fprintf(stderr, "%s: processing image...", progname); |
118 |
– |
#endif |
125 |
|
return(nextscan()); |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
< |
sfscan(sl, len, sf) /* apply scalefactor to scanline */ |
130 |
< |
register COLOR *sl; |
131 |
< |
int len; |
132 |
< |
double sf; |
129 |
> |
static void |
130 |
> |
sfscan( /* apply scalefactor to scanline */ |
131 |
> |
COLOR *sl, |
132 |
> |
int len, |
133 |
> |
double sf |
134 |
> |
) |
135 |
|
{ |
136 |
|
while (len--) { |
137 |
|
scalecolor(sl[0], sf); |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
< |
matscan(sl, len, mat) /* apply color matrix to scaline */ |
144 |
< |
register COLOR *sl; |
145 |
< |
int len; |
146 |
< |
COLORMAT mat; |
143 |
> |
static double |
144 |
> |
greypoint( /* compute gamut mapping grey target */ |
145 |
> |
COLOR col |
146 |
> |
) |
147 |
|
{ |
148 |
+ |
COLOR gryc; |
149 |
+ |
int i; |
150 |
+ |
/* improves saturated color rendering */ |
151 |
+ |
copycolor(gryc, col); |
152 |
+ |
for (i = 3; i--; ) |
153 |
+ |
if (gryc[i] > 1) |
154 |
+ |
gryc[i] = 1; |
155 |
+ |
else if (gryc[i] < 0) |
156 |
+ |
gryc[i] = 0; |
157 |
+ |
return(bright(gryc)); |
158 |
+ |
} |
159 |
+ |
|
160 |
+ |
|
161 |
+ |
static void |
162 |
+ |
matscan( /* apply color matrix to scaline */ |
163 |
+ |
COLOR *sl, |
164 |
+ |
int len, |
165 |
+ |
COLORMAT mat |
166 |
+ |
) |
167 |
+ |
{ |
168 |
|
while (len--) { |
169 |
|
colortrans(sl[0], mat, sl[0]); |
170 |
+ |
clipgamut(sl[0], greypoint(sl[0]), CGAMUT, cblack, cwhite); |
171 |
|
sl++; |
172 |
|
} |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
< |
mbscan(sl, len, mb) /* apply macbethcal adj. to scaline */ |
177 |
< |
COLOR *sl; |
178 |
< |
int len; |
179 |
< |
register struct mbc *mb; |
176 |
> |
static void |
177 |
> |
mbscan( /* apply macbethcal adj. to scaline */ |
178 |
> |
COLOR *sl, |
179 |
> |
int len, |
180 |
> |
struct mbc *mb |
181 |
> |
) |
182 |
|
{ |
183 |
|
double d; |
184 |
< |
register int i, j; |
184 |
> |
int i, j; |
185 |
|
|
186 |
|
while (len--) { |
187 |
+ |
colortrans(sl[0], mb->cmat, sl[0]); |
188 |
+ |
clipgamut(sl[0], greypoint(sl[0]), CGAMUT, mb->cmin, mb->cmax); |
189 |
|
for (i = 0; i < 3; i++) { |
190 |
|
d = colval(sl[0],i); |
191 |
|
for (j = 0; j < 4 && mb->xa[i][j+1] <= d; j++) |
194 |
|
(d - mb->xa[i][j])*mb->ya[i][j+1] ) / |
195 |
|
(mb->xa[i][j+1] - mb->xa[i][j]); |
196 |
|
} |
164 |
– |
colortrans(sl[0], mb->cmat, sl[0]); |
197 |
|
sl++; |
198 |
|
} |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
< |
getmbcalfile(fn, mb) /* load macbethcal file */ |
203 |
< |
char *fn; |
204 |
< |
register struct mbc *mb; |
202 |
> |
static void |
203 |
> |
cwscan( /* apply color space warp to scaline */ |
204 |
> |
COLOR *sl, |
205 |
> |
int len, |
206 |
> |
WARP3D *wp |
207 |
> |
) |
208 |
|
{ |
209 |
< |
extern char *fgets(); |
209 |
> |
int rval; |
210 |
> |
|
211 |
> |
while (len--) { |
212 |
> |
rval = warp3d(sl[0], sl[0], wp); |
213 |
> |
if (rval & W3ERROR) |
214 |
> |
syserror("warp3d"); |
215 |
> |
if (rval & W3BADMAP) { |
216 |
> |
fprintf(stderr, "%s: %s: bad color space map\n", |
217 |
> |
progname, cwarpfile); |
218 |
> |
exit(1); |
219 |
> |
} |
220 |
> |
clipgamut(sl[0], greypoint(sl[0]), CGAMUT, cblack, cwhite); |
221 |
> |
sl++; |
222 |
> |
} |
223 |
> |
} |
224 |
> |
|
225 |
> |
|
226 |
> |
static void |
227 |
> |
getmbcalfile( /* load macbethcal file */ |
228 |
> |
char *fn, |
229 |
> |
struct mbc *mb |
230 |
> |
) |
231 |
> |
{ |
232 |
|
char buf[128]; |
233 |
|
FILE *fp; |
234 |
|
int inpflags = 0; |
235 |
+ |
int i; |
236 |
|
|
237 |
|
if ((fp = fopen(fn, "r")) == NULL) |
238 |
|
syserror(fn); |
311 |
|
exit(1); |
312 |
|
} |
313 |
|
fclose(fp); |
314 |
+ |
/* compute gamut */ |
315 |
+ |
for (i = 0; i < 3; i++) { |
316 |
+ |
colval(mb->cmin,i) = mb->xa[i][0] - |
317 |
+ |
mb->ya[i][0] * |
318 |
+ |
(mb->xa[i][1]-mb->xa[i][0]) / |
319 |
+ |
(mb->ya[i][1]-mb->ya[i][0]); |
320 |
+ |
colval(mb->cmax,i) = mb->xa[i][4] + |
321 |
+ |
(1.-mb->ya[i][4]) * |
322 |
+ |
(mb->xa[i][5] - mb->xa[i][4]) / |
323 |
+ |
(mb->ya[i][5] - mb->ya[i][4]); |
324 |
+ |
} |
325 |
|
} |