1 |
greg |
2.12 |
/* Copyright (c) 1997 Regents of the University of California */ |
2 |
greg |
2.1 |
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* Calibrate a scanned MacBeth Color Checker Chart |
9 |
|
|
* |
10 |
greg |
2.14 |
* Produce a .cal file suitable for use with pcomb, |
11 |
|
|
* or .cwp file suitable for use with pcwarp. |
12 |
|
|
* |
13 |
|
|
* Warping code depends on conformance of COLOR and W3VEC types. |
14 |
greg |
2.1 |
*/ |
15 |
|
|
|
16 |
|
|
#include <stdio.h> |
17 |
greg |
2.14 |
#include <math.h> |
18 |
greg |
2.1 |
#ifdef MSDOS |
19 |
|
|
#include <fcntl.h> |
20 |
|
|
#endif |
21 |
|
|
#include "color.h" |
22 |
|
|
#include "resolu.h" |
23 |
|
|
#include "pmap.h" |
24 |
greg |
2.14 |
#include "warp3d.h" |
25 |
greg |
2.1 |
|
26 |
greg |
2.4 |
/* MacBeth colors */ |
27 |
|
|
#define DarkSkin 0 |
28 |
|
|
#define LightSkin 1 |
29 |
|
|
#define BlueSky 2 |
30 |
|
|
#define Foliage 3 |
31 |
|
|
#define BlueFlower 4 |
32 |
|
|
#define BluishGreen 5 |
33 |
|
|
#define Orange 6 |
34 |
|
|
#define PurplishBlue 7 |
35 |
|
|
#define ModerateRed 8 |
36 |
|
|
#define Purple 9 |
37 |
|
|
#define YellowGreen 10 |
38 |
|
|
#define OrangeYellow 11 |
39 |
|
|
#define Blue 12 |
40 |
|
|
#define Green 13 |
41 |
|
|
#define Red 14 |
42 |
|
|
#define Yellow 15 |
43 |
|
|
#define Magenta 16 |
44 |
|
|
#define Cyan 17 |
45 |
|
|
#define White 18 |
46 |
|
|
#define Neutral8 19 |
47 |
|
|
#define Neutral65 20 |
48 |
|
|
#define Neutral5 21 |
49 |
|
|
#define Neutral35 22 |
50 |
|
|
#define Black 23 |
51 |
|
|
/* computed from 5nm spectral measurements */ |
52 |
|
|
/* CIE 1931 2 degree obs, equal-energy white */ |
53 |
greg |
2.1 |
float mbxyY[24][3] = { |
54 |
|
|
{0.462, 0.3769, 0.0932961}, /* DarkSkin */ |
55 |
|
|
{0.4108, 0.3542, 0.410348}, /* LightSkin */ |
56 |
|
|
{0.2626, 0.267, 0.181554}, /* BlueSky */ |
57 |
|
|
{0.36, 0.4689, 0.108447}, /* Foliage */ |
58 |
|
|
{0.2977, 0.2602, 0.248407}, /* BlueFlower */ |
59 |
|
|
{0.2719, 0.3485, 0.401156}, /* BluishGreen */ |
60 |
|
|
{0.52, 0.4197, 0.357899}, /* Orange */ |
61 |
|
|
{0.229, 0.1866, 0.103911}, /* PurplishBlue */ |
62 |
|
|
{0.4909, 0.3262, 0.242615}, /* ModerateRed */ |
63 |
|
|
{0.3361, 0.2249, 0.0600102}, /* Purple */ |
64 |
|
|
{0.3855, 0.4874, 0.42963}, /* YellowGreen */ |
65 |
|
|
{0.4853, 0.4457, 0.476343}, /* OrangeYellow */ |
66 |
|
|
{0.2026, 0.1369, 0.0529249}, /* Blue */ |
67 |
|
|
{0.3007, 0.4822, 0.221226}, /* Green */ |
68 |
|
|
{0.5805, 0.3238, 0.162167}, /* Red */ |
69 |
|
|
{0.4617, 0.472, 0.64909}, /* Yellow */ |
70 |
|
|
{0.4178, 0.2625, 0.233662}, /* Magenta */ |
71 |
|
|
{0.2038, 0.2508, 0.167275}, /* Cyan */ |
72 |
|
|
{0.3358, 0.337, 0.916877}, /* White */ |
73 |
|
|
{0.3338, 0.3348, 0.604678}, /* Neutral.8 */ |
74 |
|
|
{0.3333, 0.3349, 0.364566}, /* Neutral.65 */ |
75 |
|
|
{0.3353, 0.3359, 0.200238}, /* Neutral.5 */ |
76 |
|
|
{0.3363, 0.336, 0.0878721}, /* Neutral.35 */ |
77 |
|
|
{0.3346, 0.3349, 0.0308383} /* Black */ |
78 |
|
|
}; |
79 |
|
|
|
80 |
|
|
COLOR mbRGB[24]; /* MacBeth RGB values */ |
81 |
|
|
|
82 |
|
|
#define NMBNEU 6 /* Number of MacBeth neutral colors */ |
83 |
greg |
2.4 |
short mbneu[NMBNEU] = {Black,Neutral35,Neutral5,Neutral65,Neutral8,White}; |
84 |
greg |
2.1 |
|
85 |
greg |
2.8 |
#define NEUFLGS (1L<<White|1L<<Neutral8|1L<<Neutral65| \ |
86 |
|
|
1L<<Neutral5|1L<<Neutral35|1L<<Black) |
87 |
greg |
2.4 |
|
88 |
greg |
2.8 |
#define SATFLGS (1L<<Red|1L<<Green|1L<<Blue|1L<<Magenta|1L<<Yellow| \ |
89 |
|
|
1L<<Cyan|1L<<Orange|1L<<Purple|1L<<PurplishBlue| \ |
90 |
|
|
1L<<YellowGreen|1<<OrangeYellow|1L<<BlueFlower) |
91 |
greg |
2.4 |
|
92 |
greg |
2.8 |
#define UNSFLGS (1L<<DarkSkin|1L<<LightSkin|1L<<BlueSky|1L<<Foliage| \ |
93 |
|
|
1L<<BluishGreen|1L<<ModerateRed) |
94 |
greg |
2.7 |
|
95 |
greg |
2.8 |
#define REQFLGS NEUFLGS /* need these colors */ |
96 |
|
|
#define MODFLGS (NEUFLGS|UNSFLGS) /* should be in gamut */ |
97 |
|
|
|
98 |
greg |
2.9 |
#define RG_BORD 0 /* patch border */ |
99 |
|
|
#define RG_CENT 01 /* central region of patch */ |
100 |
|
|
#define RG_ORIG 02 /* original color region */ |
101 |
|
|
#define RG_CORR 04 /* corrected color region */ |
102 |
greg |
2.7 |
|
103 |
greg |
2.11 |
int scanning = 1; /* scanned input (or recorded output)? */ |
104 |
greg |
2.14 |
double irrad = 1.0; /* irradiance multiplication factor */ |
105 |
|
|
int rawmap = 0; /* put out raw color mapping? */ |
106 |
greg |
2.11 |
|
107 |
greg |
2.1 |
int xmax, ymax; /* input image dimensions */ |
108 |
|
|
int bounds[4][2]; /* image coordinates of chart corners */ |
109 |
|
|
double imgxfm[3][3]; /* coordinate transformation matrix */ |
110 |
|
|
|
111 |
greg |
2.7 |
COLOR inpRGB[24]; /* measured or scanned input colors */ |
112 |
|
|
long inpflags = 0; /* flags of which colors were input */ |
113 |
greg |
2.8 |
long gmtflags = 0; /* flags of out-of-gamut colors */ |
114 |
greg |
2.1 |
|
115 |
greg |
2.2 |
COLOR bramp[NMBNEU][2]; /* brightness ramp (per primary) */ |
116 |
greg |
2.13 |
COLORMAT solmat; /* color mapping matrix */ |
117 |
|
|
COLOR colmin, colmax; /* gamut limits */ |
118 |
greg |
2.1 |
|
119 |
greg |
2.14 |
WARP3D *wcor = NULL; /* color space warp */ |
120 |
|
|
|
121 |
greg |
2.4 |
FILE *debugfp = NULL; /* debug output picture */ |
122 |
greg |
2.1 |
char *progname; |
123 |
|
|
|
124 |
|
|
extern char *malloc(); |
125 |
|
|
|
126 |
|
|
|
127 |
|
|
main(argc, argv) |
128 |
|
|
int argc; |
129 |
|
|
char **argv; |
130 |
|
|
{ |
131 |
|
|
int i; |
132 |
|
|
|
133 |
|
|
progname = argv[0]; |
134 |
greg |
2.7 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
135 |
|
|
switch (argv[i][1]) { |
136 |
|
|
case 'd': /* debug output */ |
137 |
|
|
i++; |
138 |
|
|
if (badarg(argc-i, argv+i, "s")) |
139 |
|
|
goto userr; |
140 |
|
|
if ((debugfp = fopen(argv[i], "w")) == NULL) { |
141 |
|
|
perror(argv[i]); |
142 |
|
|
exit(1); |
143 |
|
|
} |
144 |
greg |
2.1 |
#ifdef MSDOS |
145 |
greg |
2.7 |
setmode(fileno(debugfp), O_BINARY); |
146 |
greg |
2.1 |
#endif |
147 |
greg |
2.7 |
newheader("RADIANCE", debugfp); /* start */ |
148 |
|
|
printargs(argc, argv, debugfp); /* header */ |
149 |
|
|
break; |
150 |
|
|
case 'p': /* picture position */ |
151 |
|
|
if (badarg(argc-i-1, argv+i+1, "iiiiiiii")) |
152 |
|
|
goto userr; |
153 |
|
|
bounds[0][0] = atoi(argv[++i]); |
154 |
|
|
bounds[0][1] = atoi(argv[++i]); |
155 |
|
|
bounds[1][0] = atoi(argv[++i]); |
156 |
|
|
bounds[1][1] = atoi(argv[++i]); |
157 |
|
|
bounds[2][0] = atoi(argv[++i]); |
158 |
|
|
bounds[2][1] = atoi(argv[++i]); |
159 |
|
|
bounds[3][0] = atoi(argv[++i]); |
160 |
|
|
bounds[3][1] = atoi(argv[++i]); |
161 |
greg |
2.11 |
scanning = 2; |
162 |
greg |
2.7 |
break; |
163 |
greg |
2.14 |
case 'i': /* irradiance factor */ |
164 |
|
|
i++; |
165 |
|
|
if (badarg(argc-i, argv+i, "f")) |
166 |
|
|
goto userr; |
167 |
|
|
irrad = atof(argv[i]); |
168 |
|
|
break; |
169 |
|
|
case 'm': /* raw map output */ |
170 |
|
|
rawmap = 1; |
171 |
|
|
break; |
172 |
greg |
2.7 |
case 'c': /* color input */ |
173 |
greg |
2.11 |
scanning = 0; |
174 |
greg |
2.7 |
break; |
175 |
|
|
default: |
176 |
|
|
goto userr; |
177 |
|
|
} |
178 |
|
|
/* open files */ |
179 |
|
|
if (i < argc && freopen(argv[i], "r", stdin) == NULL) { |
180 |
greg |
2.13 |
perror(argv[i]); |
181 |
greg |
2.1 |
exit(1); |
182 |
|
|
} |
183 |
greg |
2.7 |
if (i+1 < argc && freopen(argv[i+1], "w", stdout) == NULL) { |
184 |
greg |
2.13 |
perror(argv[i+1]); |
185 |
greg |
2.1 |
exit(1); |
186 |
|
|
} |
187 |
greg |
2.11 |
if (scanning) { /* load input picture header */ |
188 |
greg |
2.1 |
#ifdef MSDOS |
189 |
greg |
2.7 |
setmode(fileno(stdin), O_BINARY); |
190 |
greg |
2.1 |
#endif |
191 |
greg |
2.7 |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
192 |
|
|
fgetresolu(&xmax, &ymax, stdin) < 0) { |
193 |
|
|
fprintf(stderr, "%s: bad input picture\n", progname); |
194 |
|
|
exit(1); |
195 |
|
|
} |
196 |
|
|
} else { /* else set default xmax and ymax */ |
197 |
|
|
xmax = 512; |
198 |
|
|
ymax = 2*512/3; |
199 |
greg |
2.1 |
} |
200 |
greg |
2.11 |
if (scanning != 2) { /* use default boundaries */ |
201 |
greg |
2.1 |
bounds[0][0] = bounds[2][0] = .029*xmax + .5; |
202 |
|
|
bounds[0][1] = bounds[1][1] = .956*ymax + .5; |
203 |
|
|
bounds[1][0] = bounds[3][0] = .971*xmax + .5; |
204 |
|
|
bounds[2][1] = bounds[3][1] = .056*ymax + .5; |
205 |
|
|
} |
206 |
|
|
init(); /* initialize */ |
207 |
greg |
2.11 |
if (scanning) /* get picture colors */ |
208 |
greg |
2.7 |
getpicture(); |
209 |
|
|
else |
210 |
|
|
getcolors(); |
211 |
greg |
2.2 |
compute(); /* compute color mapping */ |
212 |
greg |
2.14 |
if (rawmap) { /* print out raw correspondence */ |
213 |
|
|
register int j; |
214 |
|
|
|
215 |
|
|
printf("# Color correspondence produced by:\n#\t\t"); |
216 |
|
|
printargs(argc, argv, stdout); |
217 |
greg |
2.15 |
printf("#\tUsage: pcwarp %s uncorrected.pic > corrected.pic\n", |
218 |
greg |
2.11 |
i+1 < argc ? argv[i+1] : "{this_file}"); |
219 |
greg |
2.14 |
printf("#\t Or: pcond [options] -m %s orig.pic > output.pic\n", |
220 |
|
|
i+1 < argc ? argv[i+1] : "{this_file}"); |
221 |
|
|
for (j = 0; j < 24; j++) |
222 |
|
|
printf("%f %f %f %f %f %f\n", |
223 |
|
|
colval(inpRGB[j],RED), colval(inpRGB[j],GRN), |
224 |
|
|
colval(inpRGB[j],BLU), colval(mbRGB[j],RED), |
225 |
|
|
colval(mbRGB[j],GRN), colval(mbRGB[j],BLU)); |
226 |
|
|
if (scanning && debugfp != NULL) |
227 |
|
|
cwarp(); /* color warp for debugging */ |
228 |
|
|
} else { /* print color mapping */ |
229 |
|
|
/* print header */ |
230 |
|
|
printf("{\n\tColor correction file computed by:\n\t\t"); |
231 |
|
|
printargs(argc, argv, stdout); |
232 |
|
|
printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n", |
233 |
|
|
i+1 < argc ? argv[i+1] : "{this_file}"); |
234 |
|
|
if (!scanning) |
235 |
|
|
printf("\t Or: pcond [options] -f %s orig.pic > output.pic\n", |
236 |
|
|
i+1 < argc ? argv[i+1] : "{this_file}"); |
237 |
|
|
printf("}\n"); |
238 |
|
|
putmapping(); /* put out color mapping */ |
239 |
|
|
} |
240 |
greg |
2.7 |
if (debugfp != NULL) /* put out debug picture */ |
241 |
greg |
2.11 |
if (scanning) |
242 |
greg |
2.7 |
picdebug(); |
243 |
|
|
else |
244 |
|
|
clrdebug(); |
245 |
greg |
2.1 |
exit(0); |
246 |
|
|
userr: |
247 |
greg |
2.7 |
fprintf(stderr, |
248 |
greg |
2.14 |
"Usage: %s [-d dbg.pic][-p xul yul xur yur xll yll xlr ylr][-i irrad][-m] input.pic [output.{cal|cwp}]\n", |
249 |
greg |
2.1 |
progname); |
250 |
greg |
2.14 |
fprintf(stderr, " or: %s [-d dbg.pic][-i irrad][-m] -c [xyY.dat [output.{cal|cwp}]]\n", |
251 |
greg |
2.7 |
progname); |
252 |
greg |
2.1 |
exit(1); |
253 |
|
|
} |
254 |
|
|
|
255 |
|
|
|
256 |
|
|
init() /* initialize */ |
257 |
|
|
{ |
258 |
|
|
double quad[4][2]; |
259 |
greg |
2.7 |
register int i; |
260 |
greg |
2.1 |
/* make coordinate transformation */ |
261 |
|
|
quad[0][0] = bounds[0][0]; |
262 |
|
|
quad[0][1] = bounds[0][1]; |
263 |
|
|
quad[1][0] = bounds[1][0]; |
264 |
|
|
quad[1][1] = bounds[1][1]; |
265 |
|
|
quad[2][0] = bounds[3][0]; |
266 |
|
|
quad[2][1] = bounds[3][1]; |
267 |
|
|
quad[3][0] = bounds[2][0]; |
268 |
|
|
quad[3][1] = bounds[2][1]; |
269 |
|
|
|
270 |
|
|
if (pmap_quad_rect(0., 0., 6., 4., quad, imgxfm) == PMAP_BAD) { |
271 |
|
|
fprintf(stderr, "%s: bad chart boundaries\n", progname); |
272 |
|
|
exit(1); |
273 |
|
|
} |
274 |
greg |
2.7 |
/* map MacBeth colors to RGB space */ |
275 |
greg |
2.14 |
for (i = 0; i < 24; i++) { |
276 |
greg |
2.7 |
xyY2RGB(mbRGB[i], mbxyY[i]); |
277 |
greg |
2.14 |
scalecolor(mbRGB[i], irrad); |
278 |
|
|
} |
279 |
greg |
2.1 |
} |
280 |
|
|
|
281 |
|
|
|
282 |
|
|
int |
283 |
greg |
2.9 |
chartndx(x, y, np) /* find color number for position */ |
284 |
greg |
2.1 |
int x, y; |
285 |
greg |
2.9 |
int *np; |
286 |
greg |
2.1 |
{ |
287 |
|
|
double ipos[3], cpos[3]; |
288 |
|
|
int ix, iy; |
289 |
|
|
double fx, fy; |
290 |
|
|
|
291 |
|
|
ipos[0] = x; |
292 |
|
|
ipos[1] = y; |
293 |
|
|
ipos[2] = 1; |
294 |
|
|
mx3d_transform(ipos, imgxfm, cpos); |
295 |
|
|
cpos[0] /= cpos[2]; |
296 |
|
|
cpos[1] /= cpos[2]; |
297 |
|
|
if (cpos[0] < 0. || cpos[0] >= 6. || cpos[1] < 0. || cpos[1] >= 4.) |
298 |
greg |
2.9 |
return(RG_BORD); |
299 |
greg |
2.1 |
ix = cpos[0]; |
300 |
|
|
iy = cpos[1]; |
301 |
|
|
fx = cpos[0] - ix; |
302 |
|
|
fy = cpos[1] - iy; |
303 |
greg |
2.9 |
*np = iy*6 + ix; |
304 |
|
|
if (fx >= 0.35 && fx < 0.65 && fy >= 0.35 && fy < 0.65) |
305 |
|
|
return(RG_CENT); |
306 |
|
|
if (fx < 0.05 || fx >= 0.95 || fy < 0.05 || fy >= 0.95) |
307 |
|
|
return(RG_BORD); |
308 |
|
|
if (fx >= 0.5) /* right side is corrected */ |
309 |
|
|
return(RG_CORR); |
310 |
|
|
return(RG_ORIG); /* left side is original */ |
311 |
greg |
2.1 |
} |
312 |
|
|
|
313 |
|
|
|
314 |
greg |
2.7 |
getpicture() /* load in picture colors */ |
315 |
greg |
2.1 |
{ |
316 |
|
|
COLR *scanln; |
317 |
|
|
COLOR pval; |
318 |
|
|
int ccount[24]; |
319 |
|
|
double d; |
320 |
greg |
2.9 |
int y, i; |
321 |
|
|
register int x; |
322 |
greg |
2.1 |
|
323 |
|
|
scanln = (COLR *)malloc(xmax*sizeof(COLR)); |
324 |
|
|
if (scanln == NULL) { |
325 |
|
|
perror(progname); |
326 |
|
|
exit(1); |
327 |
|
|
} |
328 |
|
|
for (i = 0; i < 24; i++) { |
329 |
greg |
2.7 |
setcolor(inpRGB[i], 0., 0., 0.); |
330 |
greg |
2.1 |
ccount[i] = 0; |
331 |
|
|
} |
332 |
|
|
for (y = ymax-1; y >= 0; y--) { |
333 |
|
|
if (freadcolrs(scanln, xmax, stdin) < 0) { |
334 |
|
|
fprintf(stderr, "%s: error reading input picture\n", |
335 |
|
|
progname); |
336 |
|
|
exit(1); |
337 |
|
|
} |
338 |
greg |
2.9 |
for (x = 0; x < xmax; x++) |
339 |
|
|
if (chartndx(x, y, &i) == RG_CENT) { |
340 |
greg |
2.1 |
colr_color(pval, scanln[x]); |
341 |
greg |
2.7 |
addcolor(inpRGB[i], pval); |
342 |
greg |
2.1 |
ccount[i]++; |
343 |
|
|
} |
344 |
|
|
} |
345 |
greg |
2.7 |
for (i = 0; i < 24; i++) { /* compute averages */ |
346 |
|
|
if (ccount[i] == 0) |
347 |
|
|
continue; |
348 |
|
|
d = 1./ccount[i]; |
349 |
|
|
scalecolor(inpRGB[i], d); |
350 |
|
|
inpflags |= 1L<<i; |
351 |
|
|
} |
352 |
|
|
free((char *)scanln); |
353 |
|
|
} |
354 |
|
|
|
355 |
|
|
|
356 |
|
|
getcolors() /* get xyY colors from standard input */ |
357 |
|
|
{ |
358 |
|
|
int gotwhite = 0; |
359 |
|
|
COLOR whiteclr; |
360 |
|
|
int n; |
361 |
|
|
float xyYin[3]; |
362 |
|
|
|
363 |
|
|
while (fgetval(stdin, 'i', &n) == 1) { /* read colors */ |
364 |
|
|
if (n < 0 | n > 24 || |
365 |
|
|
fgetval(stdin, 'f', &xyYin[0]) != 1 || |
366 |
|
|
fgetval(stdin, 'f', &xyYin[1]) != 1 || |
367 |
|
|
fgetval(stdin, 'f', &xyYin[2]) != 1 || |
368 |
greg |
2.12 |
xyYin[0] < 0. | xyYin[1] < 0. || |
369 |
|
|
xyYin[0] + xyYin[1] > 1.) { |
370 |
greg |
2.7 |
fprintf(stderr, "%s: bad color input data\n", |
371 |
greg |
2.1 |
progname); |
372 |
|
|
exit(1); |
373 |
|
|
} |
374 |
greg |
2.7 |
if (n == 0) { /* calibration white */ |
375 |
|
|
xyY2RGB(whiteclr, xyYin); |
376 |
|
|
gotwhite++; |
377 |
|
|
} else { /* standard color */ |
378 |
|
|
n--; |
379 |
|
|
xyY2RGB(inpRGB[n], xyYin); |
380 |
|
|
inpflags |= 1L<<n; |
381 |
|
|
} |
382 |
greg |
2.1 |
} |
383 |
greg |
2.7 |
/* normalize colors */ |
384 |
|
|
if (!gotwhite) { |
385 |
|
|
if (!(inpflags & 1L<<White)) { |
386 |
|
|
fprintf(stderr, "%s: missing input for White\n", |
387 |
|
|
progname); |
388 |
|
|
exit(1); |
389 |
|
|
} |
390 |
|
|
setcolor(whiteclr, |
391 |
|
|
colval(inpRGB[White],RED)/colval(mbRGB[White],RED), |
392 |
|
|
colval(inpRGB[White],GRN)/colval(mbRGB[White],GRN), |
393 |
|
|
colval(inpRGB[White],BLU)/colval(mbRGB[White],BLU)); |
394 |
|
|
} |
395 |
|
|
for (n = 0; n < 24; n++) |
396 |
|
|
if (inpflags & 1L<<n) |
397 |
|
|
setcolor(inpRGB[n], |
398 |
|
|
colval(inpRGB[n],RED)/colval(whiteclr,RED), |
399 |
|
|
colval(inpRGB[n],GRN)/colval(whiteclr,GRN), |
400 |
|
|
colval(inpRGB[n],BLU)/colval(whiteclr,BLU)); |
401 |
greg |
2.1 |
} |
402 |
|
|
|
403 |
|
|
|
404 |
greg |
2.2 |
bresp(y, x) /* piecewise linear interpolation of primaries */ |
405 |
|
|
COLOR y, x; |
406 |
greg |
2.1 |
{ |
407 |
greg |
2.2 |
register int i, n; |
408 |
greg |
2.1 |
|
409 |
greg |
2.2 |
for (i = 0; i < 3; i++) { |
410 |
greg |
2.5 |
for (n = 0; n < NMBNEU-2; n++) |
411 |
|
|
if (colval(x,i) < colval(bramp[n+1][0],i)) |
412 |
|
|
break; |
413 |
greg |
2.8 |
colval(y,i) = ((colval(bramp[n+1][0],i) - colval(x,i)) * |
414 |
greg |
2.2 |
colval(bramp[n][1],i) + |
415 |
|
|
(colval(x,i) - colval(bramp[n][0],i)) * |
416 |
|
|
colval(bramp[n+1][1],i)) / |
417 |
|
|
(colval(bramp[n+1][0],i) - colval(bramp[n][0],i)); |
418 |
|
|
} |
419 |
greg |
2.1 |
} |
420 |
|
|
|
421 |
|
|
|
422 |
greg |
2.2 |
compute() /* compute color mapping */ |
423 |
greg |
2.1 |
{ |
424 |
greg |
2.8 |
COLOR clrin[24], clrout[24]; |
425 |
|
|
long cflags; |
426 |
|
|
COLOR ctmp; |
427 |
greg |
2.13 |
register int i, n; |
428 |
greg |
2.7 |
/* did we get what we need? */ |
429 |
|
|
if ((inpflags & REQFLGS) != REQFLGS) { |
430 |
|
|
fprintf(stderr, "%s: missing required input colors\n", |
431 |
|
|
progname); |
432 |
|
|
exit(1); |
433 |
greg |
2.2 |
} |
434 |
greg |
2.1 |
/* compute piecewise luminance curve */ |
435 |
|
|
for (i = 0; i < NMBNEU; i++) { |
436 |
greg |
2.7 |
copycolor(bramp[i][0], inpRGB[mbneu[i]]); |
437 |
greg |
2.2 |
copycolor(bramp[i][1], mbRGB[mbneu[i]]); |
438 |
greg |
2.1 |
} |
439 |
greg |
2.13 |
/* compute color space gamut */ |
440 |
|
|
if (scanning) { |
441 |
|
|
copycolor(colmin, cblack); |
442 |
|
|
copycolor(colmax, cwhite); |
443 |
greg |
2.14 |
scalecolor(colmax, irrad); |
444 |
greg |
2.13 |
} else |
445 |
|
|
for (i = 0; i < 3; i++) { |
446 |
|
|
colval(colmin,i) = colval(bramp[0][0],i) - |
447 |
|
|
colval(bramp[0][1],i) * |
448 |
|
|
(colval(bramp[1][0],i)-colval(bramp[0][0],i)) / |
449 |
|
|
(colval(bramp[1][1],i)-colval(bramp[1][0],i)); |
450 |
|
|
colval(colmax,i) = colval(bramp[NMBNEU-2][0],i) + |
451 |
|
|
(1.-colval(bramp[NMBNEU-2][1],i)) * |
452 |
|
|
(colval(bramp[NMBNEU-1][0],i) - |
453 |
|
|
colval(bramp[NMBNEU-2][0],i)) / |
454 |
|
|
(colval(bramp[NMBNEU-1][1],i) - |
455 |
|
|
colval(bramp[NMBNEU-2][1],i)); |
456 |
|
|
} |
457 |
greg |
2.8 |
/* compute color mapping */ |
458 |
|
|
do { |
459 |
|
|
cflags = inpflags & ~gmtflags; |
460 |
|
|
n = 0; /* compute transform matrix */ |
461 |
|
|
for (i = 0; i < 24; i++) |
462 |
|
|
if (cflags & 1L<<i) { |
463 |
|
|
bresp(clrin[n], inpRGB[i]); |
464 |
|
|
copycolor(clrout[n], mbRGB[i]); |
465 |
|
|
n++; |
466 |
|
|
} |
467 |
|
|
compsoln(clrin, clrout, n); |
468 |
greg |
2.14 |
if (irrad > 0.99 && irrad < 1.01) /* check gamut */ |
469 |
|
|
for (i = 0; i < 24; i++) |
470 |
|
|
if (cflags & 1L<<i && cvtcolor(ctmp, mbRGB[i])) |
471 |
|
|
gmtflags |= 1L<<i; |
472 |
greg |
2.8 |
} while (cflags & gmtflags); |
473 |
|
|
if (gmtflags & MODFLGS) |
474 |
|
|
fprintf(stderr, |
475 |
|
|
"%s: warning - some moderate colors are out of gamut\n", |
476 |
|
|
progname); |
477 |
greg |
2.2 |
} |
478 |
|
|
|
479 |
|
|
|
480 |
greg |
2.14 |
putmapping() /* put out color mapping */ |
481 |
greg |
2.2 |
{ |
482 |
|
|
static char cchar[3] = {'r', 'g', 'b'}; |
483 |
|
|
register int i, j; |
484 |
greg |
2.1 |
/* print brightness mapping */ |
485 |
greg |
2.2 |
for (j = 0; j < 3; j++) { |
486 |
|
|
printf("%cxa(i) : select(i", cchar[j]); |
487 |
|
|
for (i = 0; i < NMBNEU; i++) |
488 |
|
|
printf(",%g", colval(bramp[i][0],j)); |
489 |
|
|
printf(");\n"); |
490 |
|
|
printf("%cya(i) : select(i", cchar[j]); |
491 |
|
|
for (i = 0; i < NMBNEU; i++) |
492 |
|
|
printf(",%g", colval(bramp[i][1],j)); |
493 |
|
|
printf(");\n"); |
494 |
|
|
printf("%cfi(n) = if(n-%g, %d, if(%cxa(n+1)-%c, n, %cfi(n+1)));\n", |
495 |
|
|
cchar[j], NMBNEU-1.5, NMBNEU-1, cchar[j], |
496 |
|
|
cchar[j], cchar[j]); |
497 |
|
|
printf("%cndx = %cfi(1);\n", cchar[j], cchar[j]); |
498 |
greg |
2.11 |
printf("%c%c = ((%cxa(%cndx+1)-%c)*%cya(%cndx) + ", |
499 |
|
|
cchar[j], scanning?'n':'o', cchar[j], |
500 |
|
|
cchar[j], cchar[j], cchar[j], cchar[j]); |
501 |
greg |
2.2 |
printf("(%c-%cxa(%cndx))*%cya(%cndx+1)) /\n", |
502 |
|
|
cchar[j], cchar[j], cchar[j], |
503 |
|
|
cchar[j], cchar[j]); |
504 |
|
|
printf("\t\t(%cxa(%cndx+1) - %cxa(%cndx)) ;\n", |
505 |
|
|
cchar[j], cchar[j], cchar[j], cchar[j]); |
506 |
|
|
} |
507 |
greg |
2.1 |
/* print color mapping */ |
508 |
greg |
2.11 |
if (scanning) { |
509 |
|
|
printf("r = ri(1); g = gi(1); b = bi(1);\n"); |
510 |
|
|
printf("ro = %g*rn + %g*gn + %g*bn ;\n", |
511 |
|
|
solmat[0][0], solmat[0][1], solmat[0][2]); |
512 |
|
|
printf("go = %g*rn + %g*gn + %g*bn ;\n", |
513 |
|
|
solmat[1][0], solmat[1][1], solmat[1][2]); |
514 |
|
|
printf("bo = %g*rn + %g*gn + %g*bn ;\n", |
515 |
|
|
solmat[2][0], solmat[2][1], solmat[2][2]); |
516 |
|
|
} else { |
517 |
|
|
printf("r1 = ri(1); g1 = gi(1); b1 = bi(1);\n"); |
518 |
|
|
printf("r = %g*r1 + %g*g1 + %g*b1 ;\n", |
519 |
|
|
solmat[0][0], solmat[0][1], solmat[0][2]); |
520 |
|
|
printf("g = %g*r1 + %g*g1 + %g*b1 ;\n", |
521 |
|
|
solmat[1][0], solmat[1][1], solmat[1][2]); |
522 |
|
|
printf("b = %g*r1 + %g*g1 + %g*b1 ;\n", |
523 |
|
|
solmat[2][0], solmat[2][1], solmat[2][2]); |
524 |
|
|
} |
525 |
greg |
2.1 |
} |
526 |
|
|
|
527 |
|
|
|
528 |
greg |
2.4 |
compsoln(cin, cout, n) /* solve 3xN system using least-squares */ |
529 |
greg |
2.2 |
COLOR cin[], cout[]; |
530 |
greg |
2.1 |
int n; |
531 |
|
|
{ |
532 |
|
|
extern double mx3d_adjoint(), fabs(); |
533 |
|
|
double mat[3][3], invmat[3][3]; |
534 |
|
|
double det; |
535 |
|
|
double colv[3], rowv[3]; |
536 |
greg |
2.4 |
register int i, j, k; |
537 |
greg |
2.1 |
|
538 |
greg |
2.8 |
if (n < 3) { |
539 |
|
|
fprintf(stderr, "%s: too few colors to match!\n", progname); |
540 |
greg |
2.1 |
exit(1); |
541 |
|
|
} |
542 |
greg |
2.4 |
if (n == 3) |
543 |
|
|
for (i = 0; i < 3; i++) |
544 |
|
|
for (j = 0; j < 3; j++) |
545 |
|
|
mat[i][j] = colval(cin[j],i); |
546 |
|
|
else { /* compute A^t A */ |
547 |
|
|
for (i = 0; i < 3; i++) |
548 |
|
|
for (j = i; j < 3; j++) { |
549 |
|
|
mat[i][j] = 0.; |
550 |
|
|
for (k = 0; k < n; k++) |
551 |
|
|
mat[i][j] += colval(cin[k],i) * |
552 |
|
|
colval(cin[k],j); |
553 |
|
|
} |
554 |
|
|
for (i = 1; i < 3; i++) /* using symmetry */ |
555 |
|
|
for (j = 0; j < i; j++) |
556 |
|
|
mat[i][j] = mat[j][i]; |
557 |
|
|
} |
558 |
greg |
2.1 |
det = mx3d_adjoint(mat, invmat); |
559 |
|
|
if (fabs(det) < 1e-4) { |
560 |
|
|
fprintf(stderr, "%s: cannot compute color mapping\n", |
561 |
|
|
progname); |
562 |
|
|
solmat[0][0] = solmat[1][1] = solmat[2][2] = 1.; |
563 |
|
|
solmat[0][1] = solmat[0][2] = solmat[1][0] = |
564 |
|
|
solmat[1][2] = solmat[2][0] = solmat[2][1] = 0.; |
565 |
|
|
return; |
566 |
|
|
} |
567 |
|
|
for (i = 0; i < 3; i++) |
568 |
|
|
for (j = 0; j < 3; j++) |
569 |
|
|
invmat[i][j] /= det; |
570 |
|
|
for (i = 0; i < 3; i++) { |
571 |
greg |
2.4 |
if (n == 3) |
572 |
|
|
for (j = 0; j < 3; j++) |
573 |
|
|
colv[j] = colval(cout[j],i); |
574 |
|
|
else |
575 |
|
|
for (j = 0; j < 3; j++) { |
576 |
|
|
colv[j] = 0.; |
577 |
|
|
for (k = 0; k < n; k++) |
578 |
|
|
colv[j] += colval(cout[k],i) * |
579 |
|
|
colval(cin[k],j); |
580 |
|
|
} |
581 |
greg |
2.3 |
mx3d_transform(colv, invmat, rowv); |
582 |
greg |
2.1 |
for (j = 0; j < 3; j++) |
583 |
greg |
2.3 |
solmat[i][j] = rowv[j]; |
584 |
greg |
2.1 |
} |
585 |
|
|
} |
586 |
|
|
|
587 |
greg |
2.3 |
|
588 |
greg |
2.14 |
cwarp() /* compute color warp map */ |
589 |
|
|
{ |
590 |
|
|
register int i; |
591 |
|
|
|
592 |
|
|
if ((wcor = new3dw(W3EXACT)) == NULL) |
593 |
|
|
goto memerr; |
594 |
|
|
for (i = 0; i < 24; i++) |
595 |
|
|
if (!add3dpt(wcor, inpRGB[i], mbRGB[i])) |
596 |
|
|
goto memerr; |
597 |
|
|
return; |
598 |
|
|
memerr: |
599 |
|
|
perror(progname); |
600 |
|
|
exit(1); |
601 |
|
|
} |
602 |
|
|
|
603 |
|
|
|
604 |
greg |
2.13 |
int |
605 |
greg |
2.1 |
cvtcolor(cout, cin) /* convert color according to our mapping */ |
606 |
|
|
COLOR cout, cin; |
607 |
|
|
{ |
608 |
greg |
2.8 |
COLOR ctmp; |
609 |
greg |
2.13 |
int clipped; |
610 |
greg |
2.8 |
|
611 |
greg |
2.14 |
if (wcor != NULL) { |
612 |
|
|
clipped = warp3d(cout, cin, wcor); |
613 |
|
|
clipped |= clipgamut(cout,bright(cout),CGAMUT,colmin,colmax); |
614 |
|
|
} else if (scanning) { |
615 |
greg |
2.11 |
bresp(ctmp, cin); |
616 |
greg |
2.13 |
clipped = cresp(cout, ctmp); |
617 |
greg |
2.11 |
} else { |
618 |
greg |
2.13 |
clipped = cresp(ctmp, cin); |
619 |
greg |
2.11 |
bresp(cout, ctmp); |
620 |
|
|
} |
621 |
greg |
2.13 |
return(clipped); |
622 |
greg |
2.8 |
} |
623 |
|
|
|
624 |
|
|
|
625 |
greg |
2.13 |
int |
626 |
greg |
2.8 |
cresp(cout, cin) /* transform color according to matrix */ |
627 |
|
|
COLOR cout, cin; |
628 |
|
|
{ |
629 |
greg |
2.13 |
colortrans(cout, solmat, cin); |
630 |
|
|
return(clipgamut(cout, bright(cout), CGAMUT, colmin, colmax)); |
631 |
greg |
2.1 |
} |
632 |
|
|
|
633 |
|
|
|
634 |
greg |
2.7 |
xyY2RGB(rgbout, xyYin) /* convert xyY to RGB */ |
635 |
|
|
COLOR rgbout; |
636 |
|
|
register float xyYin[3]; |
637 |
greg |
2.1 |
{ |
638 |
greg |
2.7 |
COLOR ctmp; |
639 |
|
|
double d; |
640 |
|
|
|
641 |
|
|
d = xyYin[2] / xyYin[1]; |
642 |
|
|
ctmp[0] = xyYin[0] * d; |
643 |
|
|
ctmp[1] = xyYin[2]; |
644 |
|
|
ctmp[2] = (1. - xyYin[0] - xyYin[1]) * d; |
645 |
greg |
2.12 |
/* allow negative values */ |
646 |
greg |
2.13 |
colortrans(rgbout, xyz2rgbmat, ctmp); |
647 |
greg |
2.7 |
} |
648 |
|
|
|
649 |
|
|
|
650 |
|
|
picdebug() /* put out debugging picture */ |
651 |
|
|
{ |
652 |
greg |
2.8 |
static COLOR blkcol = BLKCOLOR; |
653 |
greg |
2.1 |
COLOR *scan; |
654 |
greg |
2.9 |
int y, i; |
655 |
|
|
register int x, rg; |
656 |
greg |
2.1 |
|
657 |
|
|
if (fseek(stdin, 0L, 0) == EOF) { |
658 |
|
|
fprintf(stderr, "%s: cannot seek on input picture\n", progname); |
659 |
|
|
exit(1); |
660 |
|
|
} |
661 |
|
|
getheader(stdin, NULL, NULL); /* skip input header */ |
662 |
|
|
fgetresolu(&xmax, &ymax, stdin); |
663 |
|
|
/* allocate scanline */ |
664 |
|
|
scan = (COLOR *)malloc(xmax*sizeof(COLOR)); |
665 |
|
|
if (scan == NULL) { |
666 |
|
|
perror(progname); |
667 |
|
|
exit(1); |
668 |
|
|
} |
669 |
|
|
/* finish debug header */ |
670 |
greg |
2.5 |
fputformat(COLRFMT, debugfp); |
671 |
greg |
2.1 |
putc('\n', debugfp); |
672 |
|
|
fprtresolu(xmax, ymax, debugfp); |
673 |
greg |
2.7 |
/* write debug picture */ |
674 |
greg |
2.1 |
for (y = ymax-1; y >= 0; y--) { |
675 |
|
|
if (freadscan(scan, xmax, stdin) < 0) { |
676 |
|
|
fprintf(stderr, "%s: error rereading input picture\n", |
677 |
|
|
progname); |
678 |
|
|
exit(1); |
679 |
|
|
} |
680 |
|
|
for (x = 0; x < xmax; x++) { |
681 |
greg |
2.9 |
rg = chartndx(x, y, &i); |
682 |
|
|
if (rg == RG_CENT) { |
683 |
greg |
2.13 |
if (!(1L<<i & gmtflags) || (x+y)&07) { |
684 |
greg |
2.9 |
copycolor(scan[x], mbRGB[i]); |
685 |
greg |
2.13 |
clipgamut(scan[x], bright(scan[x]), |
686 |
greg |
2.14 |
CGAMUT, colmin, colmax); |
687 |
greg |
2.13 |
} else |
688 |
greg |
2.9 |
copycolor(scan[x], blkcol); |
689 |
|
|
} else if (rg == RG_CORR) |
690 |
greg |
2.1 |
cvtcolor(scan[x], scan[x]); |
691 |
greg |
2.9 |
else if (rg != RG_ORIG) |
692 |
greg |
2.8 |
copycolor(scan[x], blkcol); |
693 |
greg |
2.1 |
} |
694 |
|
|
if (fwritescan(scan, xmax, debugfp) < 0) { |
695 |
|
|
fprintf(stderr, "%s: error writing debugging picture\n", |
696 |
|
|
progname); |
697 |
|
|
exit(1); |
698 |
|
|
} |
699 |
|
|
} |
700 |
greg |
2.7 |
/* clean up */ |
701 |
|
|
fclose(debugfp); |
702 |
|
|
free((char *)scan); |
703 |
|
|
} |
704 |
|
|
|
705 |
|
|
|
706 |
|
|
clrdebug() /* put out debug picture from color input */ |
707 |
|
|
{ |
708 |
|
|
static COLR blkclr = BLKCOLR; |
709 |
greg |
2.9 |
COLR mbclr[24], cvclr[24], orclr[24]; |
710 |
greg |
2.7 |
COLR *scan; |
711 |
greg |
2.13 |
COLOR ctmp, ct2; |
712 |
greg |
2.9 |
int y, i; |
713 |
|
|
register int x, rg; |
714 |
greg |
2.7 |
/* convert colors */ |
715 |
|
|
for (i = 0; i < 24; i++) { |
716 |
greg |
2.13 |
copycolor(ctmp, mbRGB[i]); |
717 |
|
|
clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite); |
718 |
|
|
setcolr(mbclr[i], colval(ctmp,RED), |
719 |
|
|
colval(ctmp,GRN), colval(ctmp,BLU)); |
720 |
greg |
2.7 |
if (inpflags & 1L<<i) { |
721 |
greg |
2.13 |
copycolor(ctmp, inpRGB[i]); |
722 |
|
|
clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite); |
723 |
|
|
setcolr(orclr[i], colval(ctmp,RED), |
724 |
greg |
2.7 |
colval(ctmp,GRN), colval(ctmp,BLU)); |
725 |
greg |
2.14 |
if (rawmap) |
726 |
|
|
copycolr(cvclr[i], mbclr[i]); |
727 |
|
|
else { |
728 |
|
|
bresp(ctmp, inpRGB[i]); |
729 |
|
|
colortrans(ct2, solmat, ctmp); |
730 |
|
|
clipgamut(ct2, bright(ct2), CGAMUT, |
731 |
|
|
cblack, cwhite); |
732 |
|
|
setcolr(cvclr[i], colval(ct2,RED), |
733 |
|
|
colval(ct2,GRN), |
734 |
|
|
colval(ct2,BLU)); |
735 |
|
|
} |
736 |
greg |
2.7 |
} |
737 |
|
|
} |
738 |
|
|
/* allocate scanline */ |
739 |
|
|
scan = (COLR *)malloc(xmax*sizeof(COLR)); |
740 |
|
|
if (scan == NULL) { |
741 |
|
|
perror(progname); |
742 |
|
|
exit(1); |
743 |
|
|
} |
744 |
|
|
/* finish debug header */ |
745 |
|
|
fputformat(COLRFMT, debugfp); |
746 |
|
|
putc('\n', debugfp); |
747 |
|
|
fprtresolu(xmax, ymax, debugfp); |
748 |
|
|
/* write debug picture */ |
749 |
|
|
for (y = ymax-1; y >= 0; y--) { |
750 |
greg |
2.9 |
for (x = 0; x < xmax; x++) { |
751 |
|
|
rg = chartndx(x, y, &i); |
752 |
|
|
if (rg == RG_CENT) { |
753 |
greg |
2.8 |
if (!(1L<<i & gmtflags) || (x+y)&07) |
754 |
|
|
copycolr(scan[x], mbclr[i]); |
755 |
|
|
else |
756 |
|
|
copycolr(scan[x], blkclr); |
757 |
greg |
2.9 |
} else if (rg == RG_BORD || !(1L<<i & inpflags)) |
758 |
|
|
copycolr(scan[x], blkclr); |
759 |
|
|
else if (rg == RG_ORIG) |
760 |
|
|
copycolr(scan[x], orclr[i]); |
761 |
|
|
else /* rg == RG_CORR */ |
762 |
greg |
2.7 |
copycolr(scan[x], cvclr[i]); |
763 |
greg |
2.9 |
} |
764 |
greg |
2.7 |
if (fwritecolrs(scan, xmax, debugfp) < 0) { |
765 |
|
|
fprintf(stderr, "%s: error writing debugging picture\n", |
766 |
|
|
progname); |
767 |
|
|
exit(1); |
768 |
|
|
} |
769 |
|
|
} |
770 |
|
|
/* clean up */ |
771 |
|
|
fclose(debugfp); |
772 |
greg |
2.1 |
free((char *)scan); |
773 |
|
|
} |