ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/macbethcal.c
Revision: 2.17
Committed: Sat Feb 22 02:07:27 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.16: +5 -9 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

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