ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/macbethcal.c
Revision: 2.16
Committed: Fri Jul 16 16:12:20 1999 UTC (24 years, 9 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.15: +54 -3 lines
Log Message:
added -P option for direct display and picking of corners using ximage

File Contents

# User Rev Content
1 gwlarson 2.16 /* Copyright (c) 1999 Regents of the University of California */
2 greg 2.1
3     #ifndef lint
4 gwlarson 2.16 static char SCCSid[] = "$SunId$ SGI";
5 greg 2.1 #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 gwlarson 2.16 #ifndef DISPCOM
104     #define DISPCOM "ximage -op %s"
105     #endif
106    
107 greg 2.11 int scanning = 1; /* scanned input (or recorded output)? */
108 greg 2.14 double irrad = 1.0; /* irradiance multiplication factor */
109     int rawmap = 0; /* put out raw color mapping? */
110 greg 2.11
111 greg 2.1 int xmax, ymax; /* input image dimensions */
112     int bounds[4][2]; /* image coordinates of chart corners */
113     double imgxfm[3][3]; /* coordinate transformation matrix */
114    
115 greg 2.7 COLOR inpRGB[24]; /* measured or scanned input colors */
116     long inpflags = 0; /* flags of which colors were input */
117 greg 2.8 long gmtflags = 0; /* flags of out-of-gamut colors */
118 greg 2.1
119 greg 2.2 COLOR bramp[NMBNEU][2]; /* brightness ramp (per primary) */
120 greg 2.13 COLORMAT solmat; /* color mapping matrix */
121     COLOR colmin, colmax; /* gamut limits */
122 greg 2.1
123 greg 2.14 WARP3D *wcor = NULL; /* color space warp */
124    
125 greg 2.4 FILE *debugfp = NULL; /* debug output picture */
126 greg 2.1 char *progname;
127    
128     extern char *malloc();
129    
130    
131     main(argc, argv)
132     int argc;
133     char **argv;
134     {
135     int i;
136    
137     progname = argv[0];
138 greg 2.7 for (i = 1; i < argc && argv[i][0] == '-'; i++)
139     switch (argv[i][1]) {
140     case 'd': /* debug output */
141     i++;
142     if (badarg(argc-i, argv+i, "s"))
143     goto userr;
144     if ((debugfp = fopen(argv[i], "w")) == NULL) {
145     perror(argv[i]);
146     exit(1);
147     }
148 greg 2.1 #ifdef MSDOS
149 greg 2.7 setmode(fileno(debugfp), O_BINARY);
150 greg 2.1 #endif
151 greg 2.7 newheader("RADIANCE", debugfp); /* start */
152     printargs(argc, argv, debugfp); /* header */
153     break;
154     case 'p': /* picture position */
155     if (badarg(argc-i-1, argv+i+1, "iiiiiiii"))
156     goto userr;
157     bounds[0][0] = atoi(argv[++i]);
158     bounds[0][1] = atoi(argv[++i]);
159     bounds[1][0] = atoi(argv[++i]);
160     bounds[1][1] = atoi(argv[++i]);
161     bounds[2][0] = atoi(argv[++i]);
162     bounds[2][1] = atoi(argv[++i]);
163     bounds[3][0] = atoi(argv[++i]);
164     bounds[3][1] = atoi(argv[++i]);
165 greg 2.11 scanning = 2;
166 greg 2.7 break;
167 gwlarson 2.16 case 'P': /* pick position */
168     scanning = 3;
169     break;
170 greg 2.14 case 'i': /* irradiance factor */
171     i++;
172     if (badarg(argc-i, argv+i, "f"))
173     goto userr;
174     irrad = atof(argv[i]);
175     break;
176     case 'm': /* raw map output */
177     rawmap = 1;
178     break;
179 greg 2.7 case 'c': /* color input */
180 greg 2.11 scanning = 0;
181 greg 2.7 break;
182     default:
183     goto userr;
184     }
185     /* open files */
186     if (i < argc && freopen(argv[i], "r", stdin) == NULL) {
187 greg 2.13 perror(argv[i]);
188 greg 2.1 exit(1);
189     }
190 greg 2.7 if (i+1 < argc && freopen(argv[i+1], "w", stdout) == NULL) {
191 greg 2.13 perror(argv[i+1]);
192 greg 2.1 exit(1);
193     }
194 greg 2.11 if (scanning) { /* load input picture header */
195 greg 2.1 #ifdef MSDOS
196 greg 2.7 setmode(fileno(stdin), O_BINARY);
197 greg 2.1 #endif
198 greg 2.7 if (checkheader(stdin, COLRFMT, NULL) < 0 ||
199     fgetresolu(&xmax, &ymax, stdin) < 0) {
200     fprintf(stderr, "%s: bad input picture\n", progname);
201     exit(1);
202     }
203 gwlarson 2.16 if (scanning == 3) {
204     if (i >= argc)
205     goto userr;
206     pickchartpos(argv[i]);
207     scanning = 2;
208     }
209 greg 2.7 } else { /* else set default xmax and ymax */
210     xmax = 512;
211     ymax = 2*512/3;
212 greg 2.1 }
213 greg 2.11 if (scanning != 2) { /* use default boundaries */
214 greg 2.1 bounds[0][0] = bounds[2][0] = .029*xmax + .5;
215     bounds[0][1] = bounds[1][1] = .956*ymax + .5;
216     bounds[1][0] = bounds[3][0] = .971*xmax + .5;
217     bounds[2][1] = bounds[3][1] = .056*ymax + .5;
218     }
219     init(); /* initialize */
220 greg 2.11 if (scanning) /* get picture colors */
221 greg 2.7 getpicture();
222     else
223     getcolors();
224 greg 2.2 compute(); /* compute color mapping */
225 greg 2.14 if (rawmap) { /* print out raw correspondence */
226     register int j;
227    
228     printf("# Color correspondence produced by:\n#\t\t");
229     printargs(argc, argv, stdout);
230 greg 2.15 printf("#\tUsage: pcwarp %s uncorrected.pic > corrected.pic\n",
231 greg 2.11 i+1 < argc ? argv[i+1] : "{this_file}");
232 greg 2.14 printf("#\t Or: pcond [options] -m %s orig.pic > output.pic\n",
233     i+1 < argc ? argv[i+1] : "{this_file}");
234     for (j = 0; j < 24; j++)
235     printf("%f %f %f %f %f %f\n",
236     colval(inpRGB[j],RED), colval(inpRGB[j],GRN),
237     colval(inpRGB[j],BLU), colval(mbRGB[j],RED),
238     colval(mbRGB[j],GRN), colval(mbRGB[j],BLU));
239     if (scanning && debugfp != NULL)
240     cwarp(); /* color warp for debugging */
241     } else { /* print color mapping */
242     /* print header */
243     printf("{\n\tColor correction file computed by:\n\t\t");
244     printargs(argc, argv, stdout);
245     printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n",
246     i+1 < argc ? argv[i+1] : "{this_file}");
247     if (!scanning)
248     printf("\t Or: pcond [options] -f %s orig.pic > output.pic\n",
249     i+1 < argc ? argv[i+1] : "{this_file}");
250     printf("}\n");
251     putmapping(); /* put out color mapping */
252     }
253 greg 2.7 if (debugfp != NULL) /* put out debug picture */
254 greg 2.11 if (scanning)
255 greg 2.7 picdebug();
256     else
257     clrdebug();
258 greg 2.1 exit(0);
259     userr:
260 greg 2.7 fprintf(stderr,
261 gwlarson 2.16 "Usage: %s [-d dbg.pic][-P | -p xul yul xur yur xll yll xlr ylr][-i irrad][-m] input.pic [output.{cal|cwp}]\n",
262 greg 2.1 progname);
263 greg 2.14 fprintf(stderr, " or: %s [-d dbg.pic][-i irrad][-m] -c [xyY.dat [output.{cal|cwp}]]\n",
264 greg 2.7 progname);
265 greg 2.1 exit(1);
266     }
267    
268    
269     init() /* initialize */
270     {
271     double quad[4][2];
272 greg 2.7 register int i;
273 greg 2.1 /* make coordinate transformation */
274     quad[0][0] = bounds[0][0];
275     quad[0][1] = bounds[0][1];
276     quad[1][0] = bounds[1][0];
277     quad[1][1] = bounds[1][1];
278     quad[2][0] = bounds[3][0];
279     quad[2][1] = bounds[3][1];
280     quad[3][0] = bounds[2][0];
281     quad[3][1] = bounds[2][1];
282    
283     if (pmap_quad_rect(0., 0., 6., 4., quad, imgxfm) == PMAP_BAD) {
284     fprintf(stderr, "%s: bad chart boundaries\n", progname);
285     exit(1);
286     }
287 greg 2.7 /* map MacBeth colors to RGB space */
288 greg 2.14 for (i = 0; i < 24; i++) {
289 greg 2.7 xyY2RGB(mbRGB[i], mbxyY[i]);
290 greg 2.14 scalecolor(mbRGB[i], irrad);
291     }
292 greg 2.1 }
293    
294    
295     int
296 greg 2.9 chartndx(x, y, np) /* find color number for position */
297 greg 2.1 int x, y;
298 greg 2.9 int *np;
299 greg 2.1 {
300     double ipos[3], cpos[3];
301     int ix, iy;
302     double fx, fy;
303    
304     ipos[0] = x;
305     ipos[1] = y;
306     ipos[2] = 1;
307     mx3d_transform(ipos, imgxfm, cpos);
308     cpos[0] /= cpos[2];
309     cpos[1] /= cpos[2];
310     if (cpos[0] < 0. || cpos[0] >= 6. || cpos[1] < 0. || cpos[1] >= 4.)
311 greg 2.9 return(RG_BORD);
312 greg 2.1 ix = cpos[0];
313     iy = cpos[1];
314     fx = cpos[0] - ix;
315     fy = cpos[1] - iy;
316 greg 2.9 *np = iy*6 + ix;
317     if (fx >= 0.35 && fx < 0.65 && fy >= 0.35 && fy < 0.65)
318     return(RG_CENT);
319     if (fx < 0.05 || fx >= 0.95 || fy < 0.05 || fy >= 0.95)
320     return(RG_BORD);
321     if (fx >= 0.5) /* right side is corrected */
322     return(RG_CORR);
323     return(RG_ORIG); /* left side is original */
324 greg 2.1 }
325    
326    
327 greg 2.7 getpicture() /* load in picture colors */
328 greg 2.1 {
329     COLR *scanln;
330     COLOR pval;
331     int ccount[24];
332     double d;
333 greg 2.9 int y, i;
334     register int x;
335 greg 2.1
336     scanln = (COLR *)malloc(xmax*sizeof(COLR));
337     if (scanln == NULL) {
338     perror(progname);
339     exit(1);
340     }
341     for (i = 0; i < 24; i++) {
342 greg 2.7 setcolor(inpRGB[i], 0., 0., 0.);
343 greg 2.1 ccount[i] = 0;
344     }
345     for (y = ymax-1; y >= 0; y--) {
346     if (freadcolrs(scanln, xmax, stdin) < 0) {
347     fprintf(stderr, "%s: error reading input picture\n",
348     progname);
349     exit(1);
350     }
351 greg 2.9 for (x = 0; x < xmax; x++)
352     if (chartndx(x, y, &i) == RG_CENT) {
353 greg 2.1 colr_color(pval, scanln[x]);
354 greg 2.7 addcolor(inpRGB[i], pval);
355 greg 2.1 ccount[i]++;
356     }
357     }
358 greg 2.7 for (i = 0; i < 24; i++) { /* compute averages */
359     if (ccount[i] == 0)
360     continue;
361     d = 1./ccount[i];
362     scalecolor(inpRGB[i], d);
363     inpflags |= 1L<<i;
364     }
365     free((char *)scanln);
366     }
367    
368    
369     getcolors() /* get xyY colors from standard input */
370     {
371     int gotwhite = 0;
372     COLOR whiteclr;
373     int n;
374     float xyYin[3];
375    
376     while (fgetval(stdin, 'i', &n) == 1) { /* read colors */
377     if (n < 0 | n > 24 ||
378     fgetval(stdin, 'f', &xyYin[0]) != 1 ||
379     fgetval(stdin, 'f', &xyYin[1]) != 1 ||
380     fgetval(stdin, 'f', &xyYin[2]) != 1 ||
381 greg 2.12 xyYin[0] < 0. | xyYin[1] < 0. ||
382     xyYin[0] + xyYin[1] > 1.) {
383 greg 2.7 fprintf(stderr, "%s: bad color input data\n",
384 greg 2.1 progname);
385     exit(1);
386     }
387 greg 2.7 if (n == 0) { /* calibration white */
388     xyY2RGB(whiteclr, xyYin);
389     gotwhite++;
390     } else { /* standard color */
391     n--;
392     xyY2RGB(inpRGB[n], xyYin);
393     inpflags |= 1L<<n;
394     }
395 greg 2.1 }
396 greg 2.7 /* normalize colors */
397     if (!gotwhite) {
398     if (!(inpflags & 1L<<White)) {
399     fprintf(stderr, "%s: missing input for White\n",
400     progname);
401     exit(1);
402     }
403     setcolor(whiteclr,
404     colval(inpRGB[White],RED)/colval(mbRGB[White],RED),
405     colval(inpRGB[White],GRN)/colval(mbRGB[White],GRN),
406     colval(inpRGB[White],BLU)/colval(mbRGB[White],BLU));
407     }
408     for (n = 0; n < 24; n++)
409     if (inpflags & 1L<<n)
410     setcolor(inpRGB[n],
411     colval(inpRGB[n],RED)/colval(whiteclr,RED),
412     colval(inpRGB[n],GRN)/colval(whiteclr,GRN),
413     colval(inpRGB[n],BLU)/colval(whiteclr,BLU));
414 greg 2.1 }
415    
416    
417 greg 2.2 bresp(y, x) /* piecewise linear interpolation of primaries */
418     COLOR y, x;
419 greg 2.1 {
420 greg 2.2 register int i, n;
421 greg 2.1
422 greg 2.2 for (i = 0; i < 3; i++) {
423 greg 2.5 for (n = 0; n < NMBNEU-2; n++)
424     if (colval(x,i) < colval(bramp[n+1][0],i))
425     break;
426 greg 2.8 colval(y,i) = ((colval(bramp[n+1][0],i) - colval(x,i)) *
427 greg 2.2 colval(bramp[n][1],i) +
428     (colval(x,i) - colval(bramp[n][0],i)) *
429     colval(bramp[n+1][1],i)) /
430     (colval(bramp[n+1][0],i) - colval(bramp[n][0],i));
431     }
432 greg 2.1 }
433    
434    
435 greg 2.2 compute() /* compute color mapping */
436 greg 2.1 {
437 greg 2.8 COLOR clrin[24], clrout[24];
438     long cflags;
439     COLOR ctmp;
440 greg 2.13 register int i, n;
441 greg 2.7 /* did we get what we need? */
442     if ((inpflags & REQFLGS) != REQFLGS) {
443     fprintf(stderr, "%s: missing required input colors\n",
444     progname);
445     exit(1);
446 greg 2.2 }
447 greg 2.1 /* compute piecewise luminance curve */
448     for (i = 0; i < NMBNEU; i++) {
449 greg 2.7 copycolor(bramp[i][0], inpRGB[mbneu[i]]);
450 greg 2.2 copycolor(bramp[i][1], mbRGB[mbneu[i]]);
451 greg 2.1 }
452 greg 2.13 /* compute color space gamut */
453     if (scanning) {
454     copycolor(colmin, cblack);
455     copycolor(colmax, cwhite);
456 greg 2.14 scalecolor(colmax, irrad);
457 greg 2.13 } else
458     for (i = 0; i < 3; i++) {
459     colval(colmin,i) = colval(bramp[0][0],i) -
460     colval(bramp[0][1],i) *
461     (colval(bramp[1][0],i)-colval(bramp[0][0],i)) /
462     (colval(bramp[1][1],i)-colval(bramp[1][0],i));
463     colval(colmax,i) = colval(bramp[NMBNEU-2][0],i) +
464     (1.-colval(bramp[NMBNEU-2][1],i)) *
465     (colval(bramp[NMBNEU-1][0],i) -
466     colval(bramp[NMBNEU-2][0],i)) /
467     (colval(bramp[NMBNEU-1][1],i) -
468     colval(bramp[NMBNEU-2][1],i));
469     }
470 greg 2.8 /* compute color mapping */
471     do {
472     cflags = inpflags & ~gmtflags;
473     n = 0; /* compute transform matrix */
474     for (i = 0; i < 24; i++)
475     if (cflags & 1L<<i) {
476     bresp(clrin[n], inpRGB[i]);
477     copycolor(clrout[n], mbRGB[i]);
478     n++;
479     }
480     compsoln(clrin, clrout, n);
481 greg 2.14 if (irrad > 0.99 && irrad < 1.01) /* check gamut */
482     for (i = 0; i < 24; i++)
483     if (cflags & 1L<<i && cvtcolor(ctmp, mbRGB[i]))
484     gmtflags |= 1L<<i;
485 greg 2.8 } while (cflags & gmtflags);
486     if (gmtflags & MODFLGS)
487     fprintf(stderr,
488     "%s: warning - some moderate colors are out of gamut\n",
489     progname);
490 greg 2.2 }
491    
492    
493 greg 2.14 putmapping() /* put out color mapping */
494 greg 2.2 {
495     static char cchar[3] = {'r', 'g', 'b'};
496     register int i, j;
497 greg 2.1 /* print brightness mapping */
498 greg 2.2 for (j = 0; j < 3; j++) {
499     printf("%cxa(i) : select(i", cchar[j]);
500     for (i = 0; i < NMBNEU; i++)
501     printf(",%g", colval(bramp[i][0],j));
502     printf(");\n");
503     printf("%cya(i) : select(i", cchar[j]);
504     for (i = 0; i < NMBNEU; i++)
505     printf(",%g", colval(bramp[i][1],j));
506     printf(");\n");
507     printf("%cfi(n) = if(n-%g, %d, if(%cxa(n+1)-%c, n, %cfi(n+1)));\n",
508     cchar[j], NMBNEU-1.5, NMBNEU-1, cchar[j],
509     cchar[j], cchar[j]);
510     printf("%cndx = %cfi(1);\n", cchar[j], cchar[j]);
511 greg 2.11 printf("%c%c = ((%cxa(%cndx+1)-%c)*%cya(%cndx) + ",
512     cchar[j], scanning?'n':'o', cchar[j],
513     cchar[j], cchar[j], cchar[j], cchar[j]);
514 greg 2.2 printf("(%c-%cxa(%cndx))*%cya(%cndx+1)) /\n",
515     cchar[j], cchar[j], cchar[j],
516     cchar[j], cchar[j]);
517     printf("\t\t(%cxa(%cndx+1) - %cxa(%cndx)) ;\n",
518     cchar[j], cchar[j], cchar[j], cchar[j]);
519     }
520 greg 2.1 /* print color mapping */
521 greg 2.11 if (scanning) {
522     printf("r = ri(1); g = gi(1); b = bi(1);\n");
523     printf("ro = %g*rn + %g*gn + %g*bn ;\n",
524     solmat[0][0], solmat[0][1], solmat[0][2]);
525     printf("go = %g*rn + %g*gn + %g*bn ;\n",
526     solmat[1][0], solmat[1][1], solmat[1][2]);
527     printf("bo = %g*rn + %g*gn + %g*bn ;\n",
528     solmat[2][0], solmat[2][1], solmat[2][2]);
529     } else {
530     printf("r1 = ri(1); g1 = gi(1); b1 = bi(1);\n");
531     printf("r = %g*r1 + %g*g1 + %g*b1 ;\n",
532     solmat[0][0], solmat[0][1], solmat[0][2]);
533     printf("g = %g*r1 + %g*g1 + %g*b1 ;\n",
534     solmat[1][0], solmat[1][1], solmat[1][2]);
535     printf("b = %g*r1 + %g*g1 + %g*b1 ;\n",
536     solmat[2][0], solmat[2][1], solmat[2][2]);
537     }
538 greg 2.1 }
539    
540    
541 greg 2.4 compsoln(cin, cout, n) /* solve 3xN system using least-squares */
542 greg 2.2 COLOR cin[], cout[];
543 greg 2.1 int n;
544     {
545     extern double mx3d_adjoint(), fabs();
546     double mat[3][3], invmat[3][3];
547     double det;
548     double colv[3], rowv[3];
549 greg 2.4 register int i, j, k;
550 greg 2.1
551 greg 2.8 if (n < 3) {
552     fprintf(stderr, "%s: too few colors to match!\n", progname);
553 greg 2.1 exit(1);
554     }
555 greg 2.4 if (n == 3)
556     for (i = 0; i < 3; i++)
557     for (j = 0; j < 3; j++)
558     mat[i][j] = colval(cin[j],i);
559     else { /* compute A^t A */
560     for (i = 0; i < 3; i++)
561     for (j = i; j < 3; j++) {
562     mat[i][j] = 0.;
563     for (k = 0; k < n; k++)
564     mat[i][j] += colval(cin[k],i) *
565     colval(cin[k],j);
566     }
567     for (i = 1; i < 3; i++) /* using symmetry */
568     for (j = 0; j < i; j++)
569     mat[i][j] = mat[j][i];
570     }
571 greg 2.1 det = mx3d_adjoint(mat, invmat);
572     if (fabs(det) < 1e-4) {
573     fprintf(stderr, "%s: cannot compute color mapping\n",
574     progname);
575     solmat[0][0] = solmat[1][1] = solmat[2][2] = 1.;
576     solmat[0][1] = solmat[0][2] = solmat[1][0] =
577     solmat[1][2] = solmat[2][0] = solmat[2][1] = 0.;
578     return;
579     }
580     for (i = 0; i < 3; i++)
581     for (j = 0; j < 3; j++)
582     invmat[i][j] /= det;
583     for (i = 0; i < 3; i++) {
584 greg 2.4 if (n == 3)
585     for (j = 0; j < 3; j++)
586     colv[j] = colval(cout[j],i);
587     else
588     for (j = 0; j < 3; j++) {
589     colv[j] = 0.;
590     for (k = 0; k < n; k++)
591     colv[j] += colval(cout[k],i) *
592     colval(cin[k],j);
593     }
594 greg 2.3 mx3d_transform(colv, invmat, rowv);
595 greg 2.1 for (j = 0; j < 3; j++)
596 greg 2.3 solmat[i][j] = rowv[j];
597 greg 2.1 }
598     }
599    
600 greg 2.3
601 greg 2.14 cwarp() /* compute color warp map */
602     {
603     register int i;
604    
605     if ((wcor = new3dw(W3EXACT)) == NULL)
606     goto memerr;
607     for (i = 0; i < 24; i++)
608     if (!add3dpt(wcor, inpRGB[i], mbRGB[i]))
609     goto memerr;
610     return;
611     memerr:
612     perror(progname);
613     exit(1);
614     }
615    
616    
617 greg 2.13 int
618 greg 2.1 cvtcolor(cout, cin) /* convert color according to our mapping */
619     COLOR cout, cin;
620     {
621 greg 2.8 COLOR ctmp;
622 greg 2.13 int clipped;
623 greg 2.8
624 greg 2.14 if (wcor != NULL) {
625     clipped = warp3d(cout, cin, wcor);
626     clipped |= clipgamut(cout,bright(cout),CGAMUT,colmin,colmax);
627     } else if (scanning) {
628 greg 2.11 bresp(ctmp, cin);
629 greg 2.13 clipped = cresp(cout, ctmp);
630 greg 2.11 } else {
631 greg 2.13 clipped = cresp(ctmp, cin);
632 greg 2.11 bresp(cout, ctmp);
633     }
634 greg 2.13 return(clipped);
635 greg 2.8 }
636    
637    
638 greg 2.13 int
639 greg 2.8 cresp(cout, cin) /* transform color according to matrix */
640     COLOR cout, cin;
641     {
642 greg 2.13 colortrans(cout, solmat, cin);
643     return(clipgamut(cout, bright(cout), CGAMUT, colmin, colmax));
644 greg 2.1 }
645    
646    
647 greg 2.7 xyY2RGB(rgbout, xyYin) /* convert xyY to RGB */
648     COLOR rgbout;
649     register float xyYin[3];
650 greg 2.1 {
651 greg 2.7 COLOR ctmp;
652     double d;
653    
654     d = xyYin[2] / xyYin[1];
655     ctmp[0] = xyYin[0] * d;
656     ctmp[1] = xyYin[2];
657     ctmp[2] = (1. - xyYin[0] - xyYin[1]) * d;
658 greg 2.12 /* allow negative values */
659 greg 2.13 colortrans(rgbout, xyz2rgbmat, ctmp);
660 greg 2.7 }
661    
662    
663     picdebug() /* put out debugging picture */
664     {
665 greg 2.8 static COLOR blkcol = BLKCOLOR;
666 greg 2.1 COLOR *scan;
667 greg 2.9 int y, i;
668     register int x, rg;
669 greg 2.1
670     if (fseek(stdin, 0L, 0) == EOF) {
671     fprintf(stderr, "%s: cannot seek on input picture\n", progname);
672     exit(1);
673     }
674     getheader(stdin, NULL, NULL); /* skip input header */
675     fgetresolu(&xmax, &ymax, stdin);
676     /* allocate scanline */
677     scan = (COLOR *)malloc(xmax*sizeof(COLOR));
678     if (scan == NULL) {
679     perror(progname);
680     exit(1);
681     }
682     /* finish debug header */
683 greg 2.5 fputformat(COLRFMT, debugfp);
684 greg 2.1 putc('\n', debugfp);
685     fprtresolu(xmax, ymax, debugfp);
686 greg 2.7 /* write debug picture */
687 greg 2.1 for (y = ymax-1; y >= 0; y--) {
688     if (freadscan(scan, xmax, stdin) < 0) {
689     fprintf(stderr, "%s: error rereading input picture\n",
690     progname);
691     exit(1);
692     }
693     for (x = 0; x < xmax; x++) {
694 greg 2.9 rg = chartndx(x, y, &i);
695     if (rg == RG_CENT) {
696 greg 2.13 if (!(1L<<i & gmtflags) || (x+y)&07) {
697 greg 2.9 copycolor(scan[x], mbRGB[i]);
698 greg 2.13 clipgamut(scan[x], bright(scan[x]),
699 greg 2.14 CGAMUT, colmin, colmax);
700 greg 2.13 } else
701 greg 2.9 copycolor(scan[x], blkcol);
702     } else if (rg == RG_CORR)
703 greg 2.1 cvtcolor(scan[x], scan[x]);
704 greg 2.9 else if (rg != RG_ORIG)
705 greg 2.8 copycolor(scan[x], blkcol);
706 greg 2.1 }
707     if (fwritescan(scan, xmax, debugfp) < 0) {
708     fprintf(stderr, "%s: error writing debugging picture\n",
709     progname);
710     exit(1);
711     }
712     }
713 greg 2.7 /* clean up */
714     fclose(debugfp);
715     free((char *)scan);
716     }
717    
718    
719     clrdebug() /* put out debug picture from color input */
720     {
721     static COLR blkclr = BLKCOLR;
722 greg 2.9 COLR mbclr[24], cvclr[24], orclr[24];
723 greg 2.7 COLR *scan;
724 greg 2.13 COLOR ctmp, ct2;
725 greg 2.9 int y, i;
726     register int x, rg;
727 greg 2.7 /* convert colors */
728     for (i = 0; i < 24; i++) {
729 greg 2.13 copycolor(ctmp, mbRGB[i]);
730     clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite);
731     setcolr(mbclr[i], colval(ctmp,RED),
732     colval(ctmp,GRN), colval(ctmp,BLU));
733 greg 2.7 if (inpflags & 1L<<i) {
734 greg 2.13 copycolor(ctmp, inpRGB[i]);
735     clipgamut(ctmp, bright(ctmp), CGAMUT, cblack, cwhite);
736     setcolr(orclr[i], colval(ctmp,RED),
737 greg 2.7 colval(ctmp,GRN), colval(ctmp,BLU));
738 greg 2.14 if (rawmap)
739     copycolr(cvclr[i], mbclr[i]);
740     else {
741     bresp(ctmp, inpRGB[i]);
742     colortrans(ct2, solmat, ctmp);
743     clipgamut(ct2, bright(ct2), CGAMUT,
744     cblack, cwhite);
745     setcolr(cvclr[i], colval(ct2,RED),
746     colval(ct2,GRN),
747     colval(ct2,BLU));
748     }
749 greg 2.7 }
750     }
751     /* allocate scanline */
752     scan = (COLR *)malloc(xmax*sizeof(COLR));
753     if (scan == NULL) {
754     perror(progname);
755     exit(1);
756     }
757     /* finish debug header */
758     fputformat(COLRFMT, debugfp);
759     putc('\n', debugfp);
760     fprtresolu(xmax, ymax, debugfp);
761     /* write debug picture */
762     for (y = ymax-1; y >= 0; y--) {
763 greg 2.9 for (x = 0; x < xmax; x++) {
764     rg = chartndx(x, y, &i);
765     if (rg == RG_CENT) {
766 greg 2.8 if (!(1L<<i & gmtflags) || (x+y)&07)
767     copycolr(scan[x], mbclr[i]);
768     else
769     copycolr(scan[x], blkclr);
770 greg 2.9 } else if (rg == RG_BORD || !(1L<<i & inpflags))
771     copycolr(scan[x], blkclr);
772     else if (rg == RG_ORIG)
773     copycolr(scan[x], orclr[i]);
774     else /* rg == RG_CORR */
775 greg 2.7 copycolr(scan[x], cvclr[i]);
776 greg 2.9 }
777 greg 2.7 if (fwritecolrs(scan, xmax, debugfp) < 0) {
778     fprintf(stderr, "%s: error writing debugging picture\n",
779     progname);
780     exit(1);
781     }
782     }
783     /* clean up */
784     fclose(debugfp);
785 greg 2.1 free((char *)scan);
786 gwlarson 2.16 }
787    
788    
789     getpos(name, bnds, fp) /* get boundary position */
790     char *name;
791     int bnds[2];
792     FILE *fp;
793     {
794     char buf[64];
795    
796     fprintf(stderr, "\tSelect corner: %s\n", name);
797     if (fgets(buf, sizeof(buf), fp) == NULL ||
798     sscanf(buf, "%d %d", &bnds[0], &bnds[1]) != 2) {
799     fprintf(stderr, "%s: read error from display process\n",
800     progname);
801     exit(1);
802     }
803     }
804    
805    
806     pickchartpos(pfn) /* display picture and pick chart location */
807     char *pfn;
808     {
809     char combuf[512];
810     FILE *pfp;
811    
812     sprintf(combuf, DISPCOM, pfn);
813     if ((pfp = popen(combuf, "r")) == NULL) {
814     perror(combuf);
815     exit(1);
816     }
817     fputs("Use middle mouse button to select chart corners:\n", stderr);
818     getpos("upper left (dark skin)", bounds[0], pfp);
819     getpos("upper right (bluish green)", bounds[1], pfp);
820     getpos("lower left (white)", bounds[2], pfp);
821     getpos("lower right (black)", bounds[3], pfp);
822     fputs("Got it -- quit display program.\n", stderr);
823     pclose(pfp);
824 greg 2.1 }