ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/macbethcal.c
Revision: 2.21
Committed: Mon Oct 27 10:24:51 2003 UTC (20 years, 6 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.20: +2 -1 lines
Log Message:
Various compatibility fixes.

File Contents

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